Skip to content

[js] Generate String constructor without new - #13165

Open
tobil4sk wants to merge 6 commits into
HaxeFoundation:developmentfrom
tobil4sk:fix/js-new-string
Open

tobil4sk wants to merge 6 commits into
HaxeFoundation:developmentfrom
tobil4sk:fix/js-new-string

Conversation

@tobil4sk

@tobil4sk tobil4sk commented Nov 8, 2026

Copy link
Copy Markdown
Member

new String() on javascript creates a boxed string, which makes it fail on strict equality. This caused inconsistent edge cases with switch statements, as reported in: #9623. Haxe sometimes generates if statements for simple switches, which worked fine, but when it generated a real native switch statement it wouldn't work correctly.

We can turn new String() into String() (using @:selfCall), which creates a normal string primitive instead of an object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/String#return_value

The mozilla docs also say:

Warning: You should rarely find yourself using String as a constructor.

Type.createInstance is handled as well, but Type.createEmptyInstance creates a completely broken value currently: #13164

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant