Skip to content

Truly independent web browser

LibJS: Call native functions from the JIT runtime directly

Timothy Flynn37ebb761d226
diff --git a/Libraries/LibJS/Rust/src/jit/calls.rs b/Libraries/LibJS/Rust/src/jit/calls.rs
index a84bc6b8d79..77d655d8134 100644
--- a/Libraries/LibJS/Rust/src/jit/calls.rs
+++ b/Libraries/LibJS/Rust/src/jit/calls.rs
@@ -33,11 +33,11 @@ use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::execution_context::ExecutionContext;
use crate::layout::feedback::CallFeedbackForwarding;
-use crate::layout::function_object::{EcmascriptFunctionObject, RawNativeFunction};
+use crate::layout::function_object::{EcmascriptFunctionObject, NativeFunction, RawNativeFunction};
use crate::layout::object::{IndexedStorageKind, Object};
use crate::layout::value::Value;
use crate::runtime::abstract_operations::{
- create_unmapped_arguments_object, get_prototype_from_constructor, length_of_array_like,
+ call_function_object, create_unmapped_arguments_object, get_prototype_from_constructor, length_of_array_like,
};
use crate::runtime::array::Array;
use crate::runtime::bound_function::BoundFunction;
@@ -436,8 +436,6 @@ fn call_other_function_from_jit(
unwrap_depth + 1,
));
}
-
- return None;
}
if let Some(bound_function) = callee_object.downcast::<BoundFunction>() {
@@ -482,6 +480,15 @@ fn call_other_function_from_jit(
);
}
+ if let Some(function) = callee_object.downcast::<NativeFunction>() {
+ return Some(
+ match call_function_object(vm, function.upcast(), this_value, arguments) {
+ Ok(value) => site.store_result(value),
+ Err(throw) => handle_asm_exception(vm, site.pc, throw.value()),
+ },
+ );
+ }
+
None
}
diff --git a/Tests/LibJS/Runtime/jit/call-paths.js b/Tests/LibJS/Runtime/jit/call-paths.js
index e79d0611ec2..474f3d19aee 100644
--- a/Tests/LibJS/Runtime/jit/call-paths.js
+++ b/Tests/LibJS/Runtime/jit/call-paths.js
@@ -71,6 +71,29 @@ describe("calls from JIT code", () => {
for (let i = 0; i < iterations; ++i) expect(caller(i % 2 ? "{" : `${i}`)).toBe(i % 2 ? "SyntaxError" : i);
});
+ test("native functions with captured state through call, apply, and bind", () => {
+ const format = new Intl.NumberFormat("en", { useGrouping: false }).format;
+ const bound = format.bind(null);
+ const sentinel = {};
+ function caller(value) {
+ try {
+ return [format(value), format.call(null, value), format.apply(null, [value]), bound(value)];
+ } catch (error) {
+ return error;
+ }
+ }
+ for (let i = 0; i < iterations; ++i) {
+ const value = {
+ valueOf() {
+ if (i % 2) throw sentinel;
+ // Reenter the native function while it is converting an argument.
+ return Number(format(i));
+ },
+ };
+ expect(caller(value)).toEqual(i % 2 ? sentinel : Array(4).fill(String(i)));
+ }
+ });
+
test("stack traces include callers", () => {
function inner() {
return new Error().stack;