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:
parent
1e53cc3f5b
commit
bfedec6a98
Notes:
sideshowbarker
2024-07-17 17:39:15 +09:00
Author: https://github.com/davidot Commit: https://github.com/SerenityOS/serenity/commit/bfedec6a98 Pull-request: https://github.com/SerenityOS/serenity/pull/12967 Reviewed-by: https://github.com/linusg ✅
3 changed files with 127 additions and 0 deletions
Userland/Libraries/LibJS
|
@ -139,6 +139,9 @@ ThrowCompletionOr<Value> NativeFunction::internal_call(Value this_argument, Mark
|
|||
|
||||
callee_context.lexical_environment = caller_context.lexical_environment;
|
||||
callee_context.variable_environment = caller_context.variable_environment;
|
||||
// Note: Keeping the private environment is probably only needed because of async methods in classes
|
||||
// calling async_block_start which goes through a NativeFunction here.
|
||||
callee_context.private_environment = caller_context.private_environment;
|
||||
|
||||
// NOTE: This is a LibJS specific hack for NativeFunction to inherit the strictness of its caller.
|
||||
callee_context.is_strict_mode = vm.in_strict_mode();
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
|
|
|
@ -81,3 +81,64 @@ test("static function named 'async'", () => {
|
|||
expect("async" in A).toBeTrue();
|
||||
expect(A.async()).toBe("static function named async");
|
||||
});
|
||||
|
||||
test("can call other private methods from static method", () => {
|
||||
class A {
|
||||
static #a() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static async #b() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
static syncA() {
|
||||
return this.#a();
|
||||
}
|
||||
|
||||
static async asyncA() {
|
||||
return this.#a();
|
||||
}
|
||||
|
||||
static syncB() {
|
||||
return this.#b();
|
||||
}
|
||||
|
||||
static async asyncB() {
|
||||
return this.#b();
|
||||
}
|
||||
}
|
||||
|
||||
var called = false;
|
||||
|
||||
async function check() {
|
||||
called = true;
|
||||
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();
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue