Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS Symbols can't be used as Dart map keys #47670

Open
nex3 opened this issue Nov 11, 2021 · 3 comments
Open

JS Symbols can't be used as Dart map keys #47670

nex3 opened this issue Nov 11, 2021 · 3 comments
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. customer-dart-sass type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-js-interop Issues that impact all js interop

Comments

@nex3
Copy link
Member

nex3 commented Nov 11, 2021

Passing an instance of JavaScript's Symbol class as a key to a Dart Map causes an error like the following:

Uncaught TypeError: J.getInterceptor$(...).get$hashCode is not a function
    at Object.get$hashCode$ (/home/nweiz/goog/sass/dart/build/npm/sass.dart.js:9370:42)
    at JsLinkedHashMap.internalComputeHashCode$1 (/home/nweiz/goog/sass/dart/build/npm/sass.dart.js:27589:16)
    at JsLinkedHashMap.internalSet$2 (/home/nweiz/goog/sass/dart/build/npm/sass.dart.js:27464:20)
    at JsLinkedHashMap.$indexSet (/home/nweiz/goog/sass/dart/build/npm/sass.dart.js:27454:15)
    at Object.fillLiteralMap (/home/nweiz/goog/sass/dart/build/npm/sass.dart.js:6698:16)
    at Object.LinkedHashMap_LinkedHashMap$_literal (/home/nweiz/goog/sass/dart/build/npm/sass.dart.js:13378:81)
    at Object.updateValuePrototype (/home/nweiz/goog/sass/dart/build/npm/sass.dart.js:5332:16)
    at Object.main (/home/nweiz/goog/sass/dart/build/npm/sass.dart.js:1890:9)
    at main1 (/home/nweiz/goog/sass/dart/build/npm/sass.dart.js:18701:9)
@nex3 nex3 added web-js-interop Issues that impact all js interop type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) customer-dart-sass labels Nov 11, 2021
@lrhn lrhn added the area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. label Nov 11, 2021
@kevmoo
Copy link
Member

kevmoo commented Nov 11, 2021

CC @rakudrama

@rakudrama
Copy link
Member

Lets assume we make JavaScript symbols behave as a distinct Dart type that implements all the methods declared for Dart Object. There is a problem of how we implement some of these methods.

The Dart maps use hashCode, but there is no obvious way to give different JavaScript symbols different hashCode values.
You can't store the hashCode on a property of a JavaScript symbol (it is not an object with properties, so JavaScript creates an Object wrapper, stores the property there, and then discards the temporary wrapper). You can't use a JavaScript symbol as a key to a JavaScript WeakMap. The implication therefore is that all JavaScript symbols have the same hashCode value and the map lookup degenerates to a linear lookup.

Another approach would be to treat JavaScript symbols specially in Dart maps. Strings are already treated specially (they have similar problems with hashCode). The downside of treating JavaScript symbols specially is that it would add another test on a hot path to handle an issue that is rare enough that has not been noticed until recently.

@nex3
Copy link
Member Author

nex3 commented Nov 11, 2021

Even linear lookup would be preferable to having an object that simply crashes your application if you put it in a Map. That said, why not set Symbol.protoype.get$hashCode to a function that returns the hash code of Symbol.toString()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. customer-dart-sass type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-js-interop Issues that impact all js interop
Projects
None yet
Development

No branches or pull requests

4 participants