mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-08 06:09:58 +00:00
LibJS: Introduce NativeJavaScriptBackedFunction
This hosts the ability to compile and run JavaScript to implement native functions. This is particularly useful for any native function that is not a normal function, for example async functions such as Array.fromAsync, which require yielding. These functions are not allowed to observe anything from outside their environment. Any global identifiers will instead be assumed to be a reference to an abstract operation or a constant. The generator will inject the appropriate bytecode if the name of the global identifier matches a known name. Anything else will cause a code generation error.
This commit is contained in:
parent
899c6ebffc
commit
a63b0cfaba
Notes:
github-actions[bot]
2025-11-30 10:56:11 +00:00
Author: https://github.com/Lubrsi
Commit: a63b0cfaba
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6728
Reviewed-by: https://github.com/Hendiadyoin1
Reviewed-by: https://github.com/awesomekling
21 changed files with 412 additions and 52 deletions
|
|
@ -9,6 +9,7 @@
|
|||
#include <LibJS/Bytecode/Interpreter.h>
|
||||
#include <LibJS/Runtime/FunctionEnvironment.h>
|
||||
#include <LibJS/Runtime/NativeFunction.h>
|
||||
#include <LibJS/Runtime/NativeJavaScriptBackedFunction.h>
|
||||
#include <LibJS/Runtime/Realm.h>
|
||||
#include <LibJS/Runtime/Value.h>
|
||||
|
||||
|
|
@ -127,8 +128,21 @@ ThrowCompletionOr<Value> NativeFunction::internal_call(ExecutionContext& callee_
|
|||
// 8. Perform any necessary implementation-defined initialization of calleeContext.
|
||||
callee_context.this_value = this_argument;
|
||||
|
||||
callee_context.lexical_environment = caller_context.lexical_environment;
|
||||
callee_context.variable_environment = caller_context.variable_environment;
|
||||
if (function_environment_needed()) {
|
||||
// 7. Let localEnv be NewFunctionEnvironment(F, newTarget).
|
||||
auto local_environment = new_function_environment(as<NativeJavaScriptBackedFunction>(*this), nullptr);
|
||||
local_environment->ensure_capacity(function_environment_bindings_count());
|
||||
|
||||
// 8. Set the LexicalEnvironment of calleeContext to localEnv.
|
||||
callee_context.lexical_environment = local_environment;
|
||||
|
||||
// 9. Set the VariableEnvironment of calleeContext to localEnv.
|
||||
callee_context.variable_environment = local_environment;
|
||||
} else {
|
||||
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;
|
||||
|
|
@ -169,8 +183,20 @@ ThrowCompletionOr<GC::Ref<Object>> NativeFunction::internal_construct(ExecutionC
|
|||
// 7. Set the ScriptOrModule of calleeContext to null.
|
||||
// Note: This is already the default value.
|
||||
|
||||
callee_context.lexical_environment = caller_context.lexical_environment;
|
||||
callee_context.variable_environment = caller_context.variable_environment;
|
||||
if (function_environment_needed()) {
|
||||
// 7. Let localEnv be NewFunctionEnvironment(F, newTarget).
|
||||
auto local_environment = new_function_environment(as<NativeJavaScriptBackedFunction>(*this), nullptr);
|
||||
local_environment->ensure_capacity(function_environment_bindings_count());
|
||||
|
||||
// 8. Set the LexicalEnvironment of calleeContext to localEnv.
|
||||
callee_context.lexical_environment = local_environment;
|
||||
|
||||
// 9. Set the VariableEnvironment of calleeContext to localEnv.
|
||||
callee_context.variable_environment = local_environment;
|
||||
} else {
|
||||
callee_context.lexical_environment = caller_context.lexical_environment;
|
||||
callee_context.variable_environment = caller_context.variable_environment;
|
||||
}
|
||||
|
||||
// </8.> --------------------------------------------------------------------------
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue