1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:28:12 +00:00

LibJS: Keep PrivateEnvironment through NativeFunction calls

Previously the variable and lexical environments were already kept in a
NativeFunction call. However when we (try to) call a private method from
within an async function we go through async_block_start which sets up
a NativeFunction to call.
This is technically not exactly as the spec describes it, as that
requires you to actually "continue" the context. Since we don't have
that concept (yet) we use this as an implementation detail to access the
private environment from within a native function.

Note that this not allow general private environment access since most
things get blocked by the parser already.
This commit is contained in:
davidot 2022-03-09 17:54:55 +01:00 committed by Linus Groh
parent 1e53cc3f5b
commit bfedec6a98
3 changed files with 127 additions and 0 deletions

View file

@ -61,3 +61,66 @@ test("method named 'async'", () => {
expect("async" in a).toBeTrue();
expect(a.async()).toBe("function named async");
});
test("can call other private methods from methods", () => {
class A {
#a() {
return 1;
}
async #b() {
return 2;
}
syncA() {
return this.#a();
}
async asyncA() {
return this.#a();
}
syncB() {
return this.#b();
}
async asyncB() {
return this.#b();
}
}
var called = false;
async function check() {
called = true;
const a = new A();
expect(a.syncA()).toBe(1);
expect(await a.asyncA()).toBe(1);
expect(await a.syncB()).toBe(2);
expect(await a.asyncB()).toBe(2);
return 3;
}
var error = null;
var failed = false;
check().then(
value => {
expect(called).toBeTrue();
expect(value).toBe(3);
},
thrownError => {
failed = true;
error = thrownError;
}
);
runQueuedPromiseJobs();
expect(called).toBeTrue();
if (failed) throw error;
expect(failed).toBeFalse();
});