-
Notifications
You must be signed in to change notification settings - Fork 234
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
cannot write to private class fields if TypeScript class is preloaded #1138
Comments
XS supports private fields in a preloaded class. You can confirm that by changing the externs of your main.ts to ".js" and doing a clean build. What's going on? The error reported is about an immutable WeakMaps. XS does not implement private fields using WeakMaps. And your code doesn't use a WeakMap directly. But TypeScript emulates private fields using them. Here's the code output by the TypeScript compiler for your project: var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _MySensor_foo;
class MySensor {
constructor() {
_MySensor_foo.set(this, 123);
__classPrivateFieldSet(this, _MySensor_foo, 123, "f");
}
}
_MySensor_foo = new WeakMap();
// Object.freeze(MySensor.prototype)
export default function main() {
const sensor = new MySensor();
trace("done\n");
}
//# sourceMappingURL=main.js.map The solution is to get TypeScript to use native engine support for private fields. I'm sure that's some adjustment to the tsconfig.json. I tried bumping ahead to ES2022, but that didn't help. Maybe you know how? |
Thanks for checking this out! |
Good news: it is enough to update to ES2022. I just did it wrong the first time. Try updating mcmanifest.js:
And rebuild tools so that is used. I'm not sure if ES2023 is supported yet by tsc. |
yup, thanks! just came to the same conclusion... |
Already committed. Will roll out later in the week. |
Build environment: any
Moddable SDK version: 3.9.2
Target device: any
Description
Trying to write to a private field of a preloaded & frozen class fails
Steps to Reproduce
mcconfig -d -m -p lin
#foo
tofoo
, re-run, the error is gone tooOther information
I suspect this is just the way it is due to the way private fields are implemented. If so, it would be good to document this limitation in
moddable/tree/public/documentation/xs/preload.md
main.js:
manifest.json:
The text was updated successfully, but these errors were encountered: