Skip to content

Truly independent web browser

LibJS: Take references and Gcs in the JIT's runtime entry points

Andreas Kling8ef71db66775
diff --git a/Libraries/LibJS/Rust/src/jit/allocation.rs b/Libraries/LibJS/Rust/src/jit/allocation.rs
index 6f66c088767..685db0e8180 100644
--- a/Libraries/LibJS/Rust/src/jit/allocation.rs
+++ b/Libraries/LibJS/Rust/src/jit/allocation.rs
@@ -416,24 +416,14 @@ pub fn lexical_environment_templates(
/// `Environment* libjs_jit_create_lexical_environment(VM*, Environment* parent, Executable*, u32 shape_cache, u32
/// capacity)`: what CreateLexicalEnvironment without a catch environment creates, for JIT code.
///
-/// # Safety
-///
/// JIT code calls this with its VM, a live parent environment and the executable of its frame.
-pub unsafe extern "C" fn libjs_jit_create_lexical_environment(
- vm: *const Vm,
- parent: *mut Environment,
- executable: *mut Executable,
+pub extern "C" fn libjs_jit_create_lexical_environment(
+ vm: &Vm,
+ parent: Gc<Environment>,
+ executable: Gc<Executable>,
shape_cache: u32,
capacity: u32,
) -> *mut DeclarativeEnvironment {
- // SAFETY: JIT code passes its VM and live cells.
- let (vm, parent, executable) = unsafe {
- (
- &*vm,
- Gc::from_non_null(NonNull::new_unchecked(parent)),
- Gc::from_non_null(NonNull::new_unchecked(executable)),
- )
- };
create_lexical_environment(
vm,
parent,
@@ -512,24 +502,16 @@ pub fn closure_template_snapshots(
/// `Object* libjs_jit_allocate_object(VM*, Shape*, u32 reserve)`: a plain object of the shape, every named property
/// undefined, with room for `reserve` properties inline if a size class has room for them.
///
-/// # Safety
-///
/// JIT code calls this with its VM and a live shape.
-pub unsafe extern "C" fn libjs_jit_allocate_object(vm: *const Vm, shape: *mut Shape, reserve: u32) -> *mut Object {
- // SAFETY: JIT code passes its VM and a shape it embeds.
- let (vm, shape) = unsafe { (&*vm, Gc::from_non_null(NonNull::new_unchecked(shape))) };
+pub extern "C" fn libjs_jit_allocate_object(vm: &Vm, shape: Gc<Shape>, reserve: u32) -> *mut Object {
Object::create_with_premade_shape_and_reserve(vm, shape, reserve).as_ptr()
}
/// `Array* libjs_jit_allocate_array(VM*, u32 count)`: an array of the running realm with `count` packed undefined
/// elements.
///
-/// # Safety
-///
/// JIT code calls this with its VM.
-pub unsafe extern "C" fn libjs_jit_allocate_array(vm: *const Vm, count: u32) -> *mut Array {
- // SAFETY: JIT code passes its VM.
- let vm = unsafe { &*vm };
+pub extern "C" fn libjs_jit_allocate_array(vm: &Vm, count: u32) -> *mut Array {
let realm = vm.current_realm().expect("JIT code runs in a realm");
let array = Array::create(vm, realm, 0, None).must();
array.set_indexed_property_elements_to_undefined(count);
@@ -539,26 +521,20 @@ pub unsafe extern "C" fn libjs_jit_allocate_array(vm: *const Vm, count: u32) ->
/// `ECMAScriptFunctionObject* libjs_jit_clone_function(VM*, ECMAScriptFunctionObject* sample, Environment*,
/// PrivateEnvironment*)`: a closure like the sample of a closure template, with the given environments.
///
-/// # Safety
-///
/// JIT code calls this with its VM, a sample it embeds and live environments (or null private environment).
-pub unsafe extern "C" fn libjs_jit_clone_function(
- vm: *const Vm,
- sample: *mut EcmascriptFunctionObject,
- environment: *mut Environment,
- private_environment: *mut PrivateEnvironment,
+pub extern "C" fn libjs_jit_clone_function(
+ vm: &Vm,
+ sample: Gc<EcmascriptFunctionObject>,
+ environment: Option<Gc<Environment>>,
+ private_environment: Option<Gc<PrivateEnvironment>>,
) -> *mut EcmascriptFunctionObject {
- // SAFETY: JIT code passes its VM and live cells.
- let (vm, sample) = unsafe { (&*vm, Gc::from_non_null(NonNull::new_unchecked(sample))) };
let realm = sample.realm().expect("an ECMAScript function has a realm");
let closure = EcmascriptFunctionObject::create_from_function_data_with_prototype(
vm,
realm,
sample.shared_data.get(),
- // SAFETY: JIT code passes live environments.
- NonNull::new(environment).map(|environment| unsafe { Gc::from_non_null(environment) }),
- // SAFETY: As above.
- NonNull::new(private_environment).map(|environment| unsafe { Gc::from_non_null(environment) }),
+ environment,
+ private_environment,
realm.function_prototype(),
);
closure.script_or_module.set(sample.script_or_module.get());
diff --git a/Libraries/LibJS/Rust/src/jit/calls.rs b/Libraries/LibJS/Rust/src/jit/calls.rs
index 68f59fd885f..79ca5f95b57 100644
--- a/Libraries/LibJS/Rust/src/jit/calls.rs
+++ b/Libraries/LibJS/Rust/src/jit/calls.rs
@@ -930,12 +930,8 @@ pub unsafe extern "C" fn libjs_jit_prepare_call_environment(
/// `u64 libjs_jit_array_push(Array*, u64 value)`: appends `value` to the array, which Array.prototype.push would
/// append it to without any observable step, and returns the new length as a value.
///
-/// # Safety
-///
/// JIT code calls this with a live array whose elements are packed.
-pub unsafe extern "C" fn libjs_jit_array_push(array: *const Array, value: u64) -> u64 {
- // SAFETY: JIT code passes a live array.
- let array = unsafe { &*array };
+pub extern "C" fn libjs_jit_array_push(array: &Array, value: u64) -> u64 {
assert!(matches!(
array.indexed_storage_kind(),
IndexedStorageKind::None | IndexedStorageKind::Packed
@@ -947,12 +943,8 @@ pub unsafe extern "C" fn libjs_jit_array_push(array: *const Array, value: u64) -
/// `u64 libjs_jit_primitive_to_string(VM*, u64 primitive)`: `String(primitive)`, which is ToString of the primitive,
/// but the descriptive string of a symbol.
///
-/// # Safety
-///
/// JIT code calls this with its VM and a primitive.
-pub unsafe extern "C" fn libjs_jit_primitive_to_string(vm: *const Vm, primitive: u64) -> u64 {
- // SAFETY: JIT code passes its VM.
- let vm = unsafe { &*vm };
+pub extern "C" fn libjs_jit_primitive_to_string(vm: &Vm, primitive: u64) -> u64 {
let primitive = Value(primitive);
assert!(!primitive.is_object());
// 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
@@ -966,12 +958,8 @@ pub unsafe extern "C" fn libjs_jit_primitive_to_string(vm: *const Vm, primitive:
/// `u64 libjs_jit_to_object(VM*, u64 primitive, u64 function)`: ToObject of a primitive other than undefined and
/// null, as the builtin function `function` makes it, or the empty value if that function is of another realm.
///
-/// # Safety
-///
/// JIT code calls this with its VM, a function object and such a primitive.
-pub unsafe extern "C" fn libjs_jit_to_object(vm: *const Vm, primitive: u64, function: u64) -> u64 {
- // SAFETY: JIT code passes its VM.
- let vm = unsafe { &*vm };
+pub extern "C" fn libjs_jit_to_object(vm: &Vm, primitive: u64, function: u64) -> u64 {
let (function, primitive) = (Value(function).as_object(), Value(primitive));
assert!(!primitive.is_object() && !primitive.is_nullish());
if vm.current_realm() != Some(function.shape().realm()) {
@@ -984,12 +972,8 @@ pub unsafe extern "C" fn libjs_jit_to_object(vm: *const Vm, primitive: u64, func
/// `u64 libjs_jit_array_create(VM*, u32 length, u64 function)`: ArrayCreate(length) as the builtin function `function`
/// (the Array constructor) makes it, or the empty value if that function is of another realm.
///
-/// # Safety
-///
/// JIT code calls this with its VM, a length below 2^31 and a function object.
-pub unsafe extern "C" fn libjs_jit_array_create(vm: *const Vm, length: u32, function: u64) -> u64 {
- // SAFETY: JIT code passes its VM.
- let vm = unsafe { &*vm };
+pub extern "C" fn libjs_jit_array_create(vm: &Vm, length: u32, function: u64) -> u64 {
let realm = Value(function).as_object().shape().realm();
if vm.current_realm() != Some(realm) {
return Value::EMPTY.0;
@@ -1001,12 +985,8 @@ pub unsafe extern "C" fn libjs_jit_array_create(vm: *const Vm, length: u32, func
/// `u64 libjs_jit_slice_arguments(VM*, ExecutionContext* frame, i32 start)`: `Array.prototype.slice.call(arguments,
/// start)` with the arguments object of the frame, which the code never created, as an array value.
///
-/// # Safety
-///
/// JIT code calls this with its VM and its frame.
-pub unsafe extern "C" fn libjs_jit_slice_arguments(vm: *const Vm, frame: *const ExecutionContext, start: i32) -> u64 {
- // SAFETY: JIT code passes live pointers.
- let (vm, frame) = unsafe { (&*vm, &*frame) };
+pub extern "C" fn libjs_jit_slice_arguments(vm: &Vm, frame: &ExecutionContext, start: i32) -> u64 {
// 23.1.3.28 Array.prototype.slice ( start, end ), https://tc39.es/ecma262/#sec-array.prototype.slice
// NB: The arguments object has every passed argument, and its length is their count.
let length = i64::from(frame.passed_argument_count.get());
diff --git a/Libraries/LibJS/Rust/src/jit/helper_calls.rs b/Libraries/LibJS/Rust/src/jit/helper_calls.rs
index a0042ae4013..01422f73b68 100644
--- a/Libraries/LibJS/Rust/src/jit/helper_calls.rs
+++ b/Libraries/LibJS/Rust/src/jit/helper_calls.rs
@@ -66,7 +66,7 @@ fn int32(value: Value) -> u64 {
}
/// `string.indexOf(search_string, position)`.
-unsafe extern "C" fn string_index_of(_vm: *const Vm, string_value: u64, search_string: u64, position: i32) -> u64 {
+extern "C" fn string_index_of(_vm: &Vm, string_value: u64, search_string: u64, position: i32) -> u64 {
let (string_value, search_string) = (string(string_value), string(search_string));
int32(StringPrototype::index_of_string(
string_value.as_string(),
@@ -76,21 +76,17 @@ unsafe extern "C" fn string_index_of(_vm: *const Vm, string_value: u64, search_s
}
/// `string.toLowerCase()`.
-unsafe extern "C" fn string_to_lowercase(vm: *const Vm, string_value: u64) -> u64 {
- // SAFETY: JIT code passes its VM.
- let vm = unsafe { &*vm };
+extern "C" fn string_to_lowercase(vm: &Vm, string_value: u64) -> u64 {
StringPrototype::to_lowercase_string(vm, string(string_value).as_string()).0
}
/// `string.toUpperCase()`.
-unsafe extern "C" fn string_to_uppercase(vm: *const Vm, string_value: u64) -> u64 {
- // SAFETY: JIT code passes its VM.
- let vm = unsafe { &*vm };
+extern "C" fn string_to_uppercase(vm: &Vm, string_value: u64) -> u64 {
StringPrototype::to_uppercase_string(vm, string(string_value).as_string()).0
}
/// `string.lastIndexOf(search_string, position)`.
-unsafe extern "C" fn string_last_index_of(_vm: *const Vm, string_value: u64, search_string: u64, position: i32) -> u64 {
+extern "C" fn string_last_index_of(_vm: &Vm, string_value: u64, search_string: u64, position: i32) -> u64 {
let (string_value, search_string) = (string(string_value), string(search_string));
int32(StringPrototype::last_index_of_string(
string_value.as_string(),
@@ -100,7 +96,7 @@ unsafe extern "C" fn string_last_index_of(_vm: *const Vm, string_value: u64, sea
}
/// `string.includes(search_string, position)`.
-unsafe extern "C" fn string_includes(_vm: *const Vm, string_value: u64, search_string: u64, position: i32) -> u64 {
+extern "C" fn string_includes(_vm: &Vm, string_value: u64, search_string: u64, position: i32) -> u64 {
let (string_value, search_string) = (string(string_value), string(search_string));
u64::from(StringPrototype::includes_string(
string_value.as_string(),
@@ -110,7 +106,7 @@ unsafe extern "C" fn string_includes(_vm: *const Vm, string_value: u64, search_s
}
/// `string.startsWith(search_string, position)`.
-unsafe extern "C" fn string_starts_with(_vm: *const Vm, string_value: u64, search_string: u64, position: i32) -> u64 {
+extern "C" fn string_starts_with(_vm: &Vm, string_value: u64, search_string: u64, position: i32) -> u64 {
let (string_value, search_string) = (string(string_value), string(search_string));
u64::from(StringPrototype::starts_with_string(
string_value.as_string(),
@@ -120,7 +116,7 @@ unsafe extern "C" fn string_starts_with(_vm: *const Vm, string_value: u64, searc
}
/// `string.endsWith(search_string, end_position)`.
-unsafe extern "C" fn string_ends_with(_vm: *const Vm, string_value: u64, search_string: u64, end_position: i32) -> u64 {
+extern "C" fn string_ends_with(_vm: &Vm, string_value: u64, search_string: u64, end_position: i32) -> u64 {
let (string_value, search_string) = (string(string_value), string(search_string));
u64::from(StringPrototype::ends_with_string(
string_value.as_string(),
@@ -130,45 +126,33 @@ unsafe extern "C" fn string_ends_with(_vm: *const Vm, string_value: u64, search_
}
/// `string.slice(start, end)`.
-unsafe extern "C" fn string_slice(vm: *const Vm, string_value: u64, start: i32, end: i32) -> u64 {
- // SAFETY: JIT code passes its VM.
- let vm = unsafe { &*vm };
+extern "C" fn string_slice(vm: &Vm, string_value: u64, start: i32, end: i32) -> u64 {
StringPrototype::slice_string(vm, string(string_value).as_string(), f64::from(start), f64::from(end)).0
}
/// `string.substring(start, end)`.
-unsafe extern "C" fn string_substring(vm: *const Vm, string_value: u64, start: i32, end: i32) -> u64 {
- // SAFETY: JIT code passes its VM.
- let vm = unsafe { &*vm };
+extern "C" fn string_substring(vm: &Vm, string_value: u64, start: i32, end: i32) -> u64 {
StringPrototype::substring_string(vm, string(string_value).as_string(), f64::from(start), f64::from(end)).0
}
/// `string.trim()`.
-unsafe extern "C" fn string_trim(vm: *const Vm, string_value: u64) -> u64 {
- // SAFETY: JIT code passes its VM.
- let vm = unsafe { &*vm };
+extern "C" fn string_trim(vm: &Vm, string_value: u64) -> u64 {
trim_primitive_string(vm, string(string_value).as_string(), TrimMode::Both).0
}
/// `string.trimStart()`.
-unsafe extern "C" fn string_trim_start(vm: *const Vm, string_value: u64) -> u64 {
- // SAFETY: JIT code passes its VM.
- let vm = unsafe { &*vm };
+extern "C" fn string_trim_start(vm: &Vm, string_value: u64) -> u64 {
trim_primitive_string(vm, string(string_value).as_string(), TrimMode::Left).0
}
/// `string.trimEnd()`.
-unsafe extern "C" fn string_trim_end(vm: *const Vm, string_value: u64) -> u64 {
- // SAFETY: JIT code passes its VM.
- let vm = unsafe { &*vm };
+extern "C" fn string_trim_end(vm: &Vm, string_value: u64) -> u64 {
trim_primitive_string(vm, string(string_value).as_string(), TrimMode::Right).0
}
/// `regexp.exec(string)`, for a RegExp object `regexp` whose exec runs no code (see
/// `regexp_builtin_exec_running_no_code()`).
-unsafe extern "C" fn regexp_exec(vm: *const Vm, regexp: u64, string_value: u64) -> u64 {
- // SAFETY: JIT code passes its VM.
- let vm = unsafe { &*vm };
+extern "C" fn regexp_exec(vm: &Vm, regexp: u64, string_value: u64) -> u64 {
regexp_builtin_exec_running_no_code(vm, Value(regexp), string(string_value).as_string())
.map_or(Value::EMPTY.0, |result| result.0)
}
diff --git a/Libraries/LibJS/Rust/src/jit/intrinsics.rs b/Libraries/LibJS/Rust/src/jit/intrinsics.rs
index 3274ee8c0e6..a3bd9098e29 100644
--- a/Libraries/LibJS/Rust/src/jit/intrinsics.rs
+++ b/Libraries/LibJS/Rust/src/jit/intrinsics.rs
@@ -134,12 +134,8 @@ fn has_own_string_property(vm: &Vm, object: Gc<Object>, name: &Utf16FlyString) -
/// `Object.prototype.hasOwnProperty` called on `object` with `key`.
///
-/// # Safety
-///
/// JIT code calls this with its VM.
-pub unsafe extern "C" fn libjs_jit_has_own_property(vm: *const Vm, object: u64, key: u64) -> u64 {
- // SAFETY: JIT code passes its VM.
- let vm = unsafe { &*vm };
+pub extern "C" fn libjs_jit_has_own_property(vm: &Vm, object: u64, key: u64) -> u64 {
let (object, key) = (Value(object), Value(key));
if !object.is_object() || !has_ordinary_lookups(object.as_object()) {
return Value::EMPTY.0;
@@ -162,12 +158,8 @@ pub unsafe extern "C" fn libjs_jit_has_own_property(vm: *const Vm, object: u64,
/// `key in object`.
///
-/// # Safety
-///
/// JIT code calls this with its VM.
-pub unsafe extern "C" fn libjs_jit_has_property(vm: *const Vm, key: u64, object: u64) -> u64 {
- // SAFETY: JIT code passes its VM.
- let vm = unsafe { &*vm };
+pub extern "C" fn libjs_jit_has_property(vm: &Vm, key: u64, object: u64) -> u64 {
let (key_value, object) = (Value(key), Value(object));
let index_key = key_value.is_int32() && key_value.as_i32() >= 0;
if !object.is_object() || !(key_value.is_string() || key_value.is_symbol() || index_key) {
@@ -219,12 +211,8 @@ pub unsafe extern "C" fn libjs_jit_has_property(vm: *const Vm, key: u64, object:
/// have the method `next` (the builtin `%ArrayIteratorPrototype%.next` of the realm of JIT code, which it passes):
/// a new array iterator over the values of the array, made like `%Array.prototype.values%` makes it.
///
-/// # Safety
-///
/// JIT code calls this with its VM.
-pub unsafe extern "C" fn libjs_jit_create_array_iterator(vm: *const Vm, iterable: u64, next: u64) -> u64 {
- // SAFETY: JIT code passes its VM.
- let vm = unsafe { &*vm };
+pub extern "C" fn libjs_jit_create_array_iterator(vm: &Vm, iterable: u64, next: u64) -> u64 {
let iterable = Value(iterable);
if !iterable.is_object() {
return Value::EMPTY.0;