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:
Luke Wilde
2025-11-06 19:00:36 +00:00
committed by Andreas Kling
parent 899c6ebffc
commit a63b0cfaba
Notes: github-actions[bot] 2025-11-30 10:56:11 +00:00
21 changed files with 412 additions and 52 deletions

View File

@@ -217,7 +217,7 @@ ThrowCompletionOr<void> ECMAScriptFunctionObject::get_stack_frame_size(size_t& r
if (is_module_wrapper()) {
executable = TRY(Bytecode::compile(vm(), ecmascript_code(), kind(), name()));
} else {
executable = TRY(Bytecode::compile(vm(), shared_data()));
executable = TRY(Bytecode::compile(vm(), shared_data(), Bytecode::BuiltinAbstractOperationsEnabled::No));
}
}
registers_and_constants_and_locals_count = executable->registers_and_constants_and_locals_count;
@@ -607,9 +607,9 @@ ThrowCompletionOr<Value> ECMAScriptFunctionObject::ordinary_call_evaluate_body(V
return result;
if (kind() == FunctionKind::AsyncGenerator)
return AsyncGenerator::create(*context.realm, result, this, context.copy());
return AsyncGenerator::create(*context.realm, result, GC::Ref { *this }, context.copy());
auto generator_object = GeneratorObject::create(*context.realm, result, this, context.copy());
auto generator_object = GeneratorObject::create(*context.realm, result, GC::Ref { *this }, context.copy());
// NOTE: Async functions are entirely transformed to generator functions, and wrapped in a custom driver that returns a promise
// See AwaitExpression::generate_bytecode() for the transformation.