Truly independent web browser
LibJS+LibWeb: Make the bits of a Value private to libjs_rust
Shannon Boothb5ea62cf8afc
diff --git a/Libraries/LibJS/Rust/src/layout/value.rs b/Libraries/LibJS/Rust/src/layout/value.rsindex f9c2977063f..725ad8c721d 100644--- a/Libraries/LibJS/Rust/src/layout/value.rs+++ b/Libraries/LibJS/Rust/src/layout/value.rs@@ -6,10 +6,10 @@use libjs_abi::value as nan_box;-/// A NaN-boxed JS::Value.+/// A NaN-boxed JS::Value. Only libjs_rust makes values from bits, as bits can name a cell that is not there.#[derive(Clone, Copy, PartialEq, Eq)]#[repr(transparent)]-pub struct Value(pub u64);+pub struct Value(pub(crate) u64);impl Value {pub const EMPTY: Value = Value(nan_box::EMPTY_VALUE);@@ -17,6 +17,18 @@ impl Value {pub const NULL: Value = Value(nan_box::NULL_VALUE);pub const FALSE: Value = Value(nan_box::FALSE_VALUE);pub const TRUE: Value = Value(nan_box::TRUE_VALUE);++ /// # Safety+ ///+ /// `bits` must be a NaN-boxed value, such as one C++ code hands over, and a cell it names must be live.+ pub const unsafe fn from_raw(bits: u64) -> Value {+ Value(bits)+ }++ /// The NaN-boxed bits, for handing the value to C++ code.+ pub const fn to_raw(self) -> u64 {+ self.0+ }}const _: () = assert!(size_of::<Value>() == 8);diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/callback.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/callback.rsindex 804f46faeea..9fd13bce0e2 100644--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/callback.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/callback.rs@@ -106,7 +106,7 @@ impl CallbackFunction {web_bindings_invoke_callback_function(self.0.0.as_ptr(),this_value.is_some(),- this_value.unwrap_or(Value::UNDEFINED).0,+ this_value.unwrap_or(Value::UNDEFINED).to_raw(),ak::Span::new(arguments),)}@@ -129,7 +129,7 @@ impl PromiseCallbackFunction {promise_from_abi(web_bindings_invoke_promise_callback_function(self.0.0.as_ptr(),this_value.is_some(),- this_value.unwrap_or(Value::UNDEFINED).0,+ this_value.unwrap_or(Value::UNDEFINED).to_raw(),ak::Span::new(arguments),))}diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rsindex f01b2cf64a0..8bf77590fd5 100644--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs@@ -406,8 +406,13 @@ fn run_with_temporary_execution_context<F: FnOnce() -> R, R>(realm: Gc<Realm>, cpub(crate) fn rejected_promise(vm: &Vm, throw: Throw) -> Value {let current_realm = vm.current_realm().expect("a native function runs in a realm");// SAFETY: The realm is live, and the values are live while the native function runs.- let promise =- unsafe { web_bindings_rejected_promise(current_realm.as_ptr().cast(), vm.this_value().0, throw.value().0) };+ let promise = unsafe {+ web_bindings_rejected_promise(+ current_realm.as_ptr().cast(),+ vm.this_value().to_raw(),+ throw.value().to_raw(),+ )+ };let promise = NonNull::new(promise.cast::<Object>()).expect("LibWeb returns the promise");// SAFETY: LibWeb returns a live promise.Value::from_object(unsafe { Gc::from_non_null(promise) })diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/structured_data.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/structured_data.rsindex 66a74aff7e2..46c20b4a80a 100644--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/structured_data.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/structured_data.rs@@ -67,7 +67,7 @@ impl SerializationWriter<'_> {/// https://html.spec.whatwg.org/multipage/structured-data.html#sub-serializationpub(crate) fn write_value(&mut self, value: Value) -> WebIdlResult<()> {// SAFETY: The writer is lent for the steps, and the value is live while they run.- unsafe { web_bindings_serialize_value(self.raw.as_ptr(), value.0) }.into_completion()?;+ unsafe { web_bindings_serialize_value(self.raw.as_ptr(), value.to_raw()) }.into_completion()?;Ok(())}diff --git a/Libraries/LibWeb/Bindings/Rust/src/streams/abstract_operations.rs b/Libraries/LibWeb/Bindings/Rust/src/streams/abstract_operations.rsindex 62e662b37b7..d3c2a14a23d 100644--- a/Libraries/LibWeb/Bindings/Rust/src/streams/abstract_operations.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/streams/abstract_operations.rs@@ -108,7 +108,7 @@ pub(crate) fn structured_clone(cx: &CallContext, value: Value) -> WebIdlResult<V// 1. Let serialized be ? StructuredSerialize(v).// 2. Return ? StructuredDeserialize(serialized, the current Realm).// SAFETY: The realm and the value are live.- Ok(unsafe { web_streams_structured_clone(cx.realm().as_ptr().cast(), value.0) }.into_completion()?)+ Ok(unsafe { web_streams_structured_clone(cx.realm().as_ptr().cast(), value.to_raw()) }.into_completion()?)}// https://streams.spec.whatwg.org/#abstract-opdef-cancopydatablockbytesdiff --git a/Libraries/LibWeb/Bindings/Rust/src/streams/cpp.rs b/Libraries/LibWeb/Bindings/Rust/src/streams/cpp.rsindex 3678fbf9043..ce1f486f668 100644--- a/Libraries/LibWeb/Bindings/Rust/src/streams/cpp.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/streams/cpp.rs@@ -89,7 +89,7 @@ impl CppPromiseAlgorithmWithArgument {unsafe {promise_from_abi(web_streams_call_promise_algorithm_with_argument(self.0.as_ptr(),- argument.0,+ argument.to_raw(),))}}@@ -103,7 +103,7 @@ pub(crate) struct CppReadRequest(pub(crate) CppCell);impl CppReadRequest {pub(crate) fn chunk_steps(&self, chunk: Value) {// SAFETY: The cell is a live Streams::ReadRequest.- unsafe { web_streams_read_request_chunk_steps(self.0.as_ptr(), chunk.0) };+ unsafe { web_streams_read_request_chunk_steps(self.0.as_ptr(), chunk.to_raw()) };}pub(crate) fn close_steps(&self) {@@ -113,7 +113,7 @@ impl CppReadRequest {pub(crate) fn error_steps(&self, error: Value) {// SAFETY: The cell is a live Streams::ReadRequest.- unsafe { web_streams_read_request_error_steps(self.0.as_ptr(), error.0) };+ unsafe { web_streams_read_request_error_steps(self.0.as_ptr(), error.to_raw()) };}}@@ -137,7 +137,7 @@ pub(crate) struct CppValueSteps(pub(crate) CppCell);impl CppValueSteps {pub(crate) fn call(&self, value: Value) {// SAFETY: The cell is a live GC::Function<void(JS::Value)>.- unsafe { web_streams_call_value_steps(self.0.as_ptr(), value.0) };+ unsafe { web_streams_call_value_steps(self.0.as_ptr(), value.to_raw()) };}}@@ -200,8 +200,8 @@ impl AbortSignal {// https://dom.spec.whatwg.org/#abortsignal-abort-reasonpub(crate) fn reason(&self) -> Value {- // SAFETY: The signal is live.- Value(unsafe { web_streams_abort_signal_reason(self.0.as_ptr()) })+ // SAFETY: The signal is live, and so is its reason.+ unsafe { Value::from_raw(web_streams_abort_signal_reason(self.0.as_ptr())) }}/// Adds the abort algorithm, a native function with no arguments, to the signal,@@ -222,6 +222,6 @@ impl AbortSignal {// https://dom.spec.whatwg.org/#abortsignal-signal-abortpub(crate) fn signal_abort(&self, reason: Value, realm: Gc<libjs_rust::api::Realm>) {// SAFETY: The signal, the reason and the realm are live.- unsafe { web_streams_abort_signal_signal_abort(self.0.as_ptr(), reason.0, realm.as_ptr().cast()) };+ unsafe { web_streams_abort_signal_signal_abort(self.0.as_ptr(), reason.to_raw(), realm.as_ptr().cast()) };}}diff --git a/Libraries/LibWeb/Bindings/Rust/src/streams/cross_realm_transform.rs b/Libraries/LibWeb/Bindings/Rust/src/streams/cross_realm_transform.rsindex 74de910fe5b..b1468c005ac 100644--- a/Libraries/LibWeb/Bindings/Rust/src/streams/cross_realm_transform.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/streams/cross_realm_transform.rs@@ -128,8 +128,8 @@ impl CrossRealmTransformPort {},|vm: &Vm, function: &NativeSteps<(C, Gc<Realm>), MessageSteps<C>>| {let (state, realm) = function.captures;- // SAFETY: The port's message event handler is called with a MessageEvent.- let data = Value(unsafe { web_streams_message_event_data(vm.argument(0).0) });+ // SAFETY: The port's message event handler is called with a MessageEvent, whose data is live.+ let data = unsafe { Value::from_raw(web_streams_message_event_data(vm.argument(0).to_raw())) };(function.steps)(&CallContext::new(vm, realm), state, data);Ok(Value::UNDEFINED)},@@ -193,7 +193,7 @@ impl CrossRealmTransformPort {web_streams_message_port_post_message(cx.realm().as_ptr().cast(),self.as_ptr(),- Value::from_object(message).0,+ Value::from_object(message).to_raw(),)}.into_completion()?;@@ -643,11 +643,11 @@ pub(crate) fn deserialize_message_port(/// Sets the data holder to ! StructuredSerializeWithTransfer(value, « value »).pub(crate) fn serialize_with_transfer(data_holder: &mut TransferDataWriter, value: Value) {// SAFETY: The data holder is lent for the transfer steps, and the value is live.- unsafe { web_streams_serialize_with_transfer(data_holder.as_raw(), value.0) };+ unsafe { web_streams_serialize_with_transfer(data_holder.as_raw(), value.to_raw()) };}/// ! StructuredDeserializeWithTransfer(dataHolder, the current Realm).[[Deserialized]]pub(crate) fn deserialize_with_transfer(data_holder: &mut TransferDataReader) -> Value {- // SAFETY: The data holder is lent for the transfer-receiving steps.- Value(unsafe { web_streams_deserialize_with_transfer(data_holder.as_raw()) })+ // SAFETY: The data holder is lent for the transfer-receiving steps, which return a live value.+ unsafe { Value::from_raw(web_streams_deserialize_with_transfer(data_holder.as_raw())) }}diff --git a/Libraries/LibWeb/Bindings/Rust/src/streams/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/streams/exports.rsindex 3d6bb8c4174..9a9914f6940 100644--- a/Libraries/LibWeb/Bindings/Rust/src/streams/exports.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/streams/exports.rs@@ -161,16 +161,22 @@ pub unsafe extern "C" fn web_rust_readable_stream_close_requested(stream: *mut c////// # Safety///-/// The pointers must be the VM and a live ReadableStream.+/// The pointers must be the VM and a live ReadableStream, and `chunk` a live value.#[unsafe(no_mangle)]pub unsafe extern "C" fn web_rust_readable_stream_enqueue(vm: *mut c_void,stream: *mut c_void,chunk: u64,) -> RawNativeFunctionResult {- // SAFETY: The caller passes the VM and a live ReadableStream.- let (vm, stream) = unsafe { (vm_argument(vm), cell_from_abi::<ReadableStream>(stream)) };- let result = ReadableStream::enqueue(stream, vm, Value(chunk));+ // SAFETY: The caller passes the VM, a live ReadableStream and a live value.+ let (vm, stream, chunk) = unsafe {+ (+ vm_argument(vm),+ cell_from_abi::<ReadableStream>(stream),+ Value::from_raw(chunk),+ )+ };+ let result = ReadableStream::enqueue(stream, vm, chunk);completion(&stream.context(vm), result)}@@ -212,28 +218,40 @@ pub unsafe extern "C" fn web_rust_readable_stream_close(vm: *mut c_void, stream:////// # Safety///-/// The pointers must be the VM and a live ReadableStream.+/// The pointers must be the VM and a live ReadableStream, and `error` a live value.#[unsafe(no_mangle)]pub unsafe extern "C" fn web_rust_readable_stream_error(vm: *mut c_void, stream: *mut c_void, error: u64) {- // SAFETY: The caller passes the VM and a live ReadableStream.- let (vm, stream) = unsafe { (vm_argument(vm), cell_from_abi::<ReadableStream>(stream)) };- ReadableStream::error(stream, vm, Value(error));+ // SAFETY: The caller passes the VM, a live ReadableStream and a live value.+ let (vm, stream, error) = unsafe {+ (+ vm_argument(vm),+ cell_from_abi::<ReadableStream>(stream),+ Value::from_raw(error),+ )+ };+ ReadableStream::error(stream, vm, error);}/// ! ReadableStreamCancel(stream, reason), https://streams.spec.whatwg.org/#readable-stream-cancel////// # Safety///-/// The pointers must be the VM and a live ReadableStream.+/// The pointers must be the VM and a live ReadableStream, and `reason` a live value.#[unsafe(no_mangle)]pub unsafe extern "C" fn web_rust_readable_stream_cancel(vm: *mut c_void,stream: *mut c_void,reason: u64,) -> *mut c_void {- // SAFETY: The caller passes the VM and a live ReadableStream.- let (vm, stream) = unsafe { (vm_argument(vm), cell_from_abi::<ReadableStream>(stream)) };- promise_into_abi(readable_stream_cancel(&stream.context(vm), stream, Value(reason)))+ // SAFETY: The caller passes the VM, a live ReadableStream and a live value.+ let (vm, stream, reason) = unsafe {+ (+ vm_argument(vm),+ cell_from_abi::<ReadableStream>(stream),+ Value::from_raw(reason),+ )+ };+ promise_into_abi(readable_stream_cancel(&stream.context(vm), stream, reason))}/// To tee a ReadableStream stream, https://streams.spec.whatwg.org/#readablestream-tee, which sets the branches.@@ -386,19 +404,25 @@ pub unsafe extern "C" fn web_rust_readable_stream_default_reader_read(////// # Safety///-/// The pointers must be the VM and a live ReadableStreamDefaultReader.+/// The pointers must be the VM and a live ReadableStreamDefaultReader, and `reason` a live value.#[unsafe(no_mangle)]pub unsafe extern "C" fn web_rust_readable_stream_default_reader_cancel(vm: *mut c_void,reader: *mut c_void,reason: u64,) -> *mut c_void {- // SAFETY: The caller passes the VM and a live reader.- let (vm, reader) = unsafe { (vm_argument(vm), cell_from_abi::<ReadableStreamDefaultReader>(reader)) };+ // SAFETY: The caller passes the VM, a live reader and a live value.+ let (vm, reader, reason) = unsafe {+ (+ vm_argument(vm),+ cell_from_abi::<ReadableStreamDefaultReader>(reader),+ Value::from_raw(reason),+ )+ };promise_into_abi(ReadableStreamGenericReader::cancel(vm,default_reader(reader),- Some(Value(reason)),+ Some(reason),))}@@ -470,20 +494,26 @@ pub unsafe extern "C" fn web_rust_transform_stream_set_up(////// # Safety///-/// The pointers must be the VM and a live TransformStream.+/// The pointers must be the VM and a live TransformStream, and `chunk` a live value.#[unsafe(no_mangle)]pub unsafe extern "C" fn web_rust_transform_stream_enqueue(vm: *mut c_void,stream: *mut c_void,chunk: u64,) -> RawNativeFunctionResult {- // SAFETY: The caller passes the VM and a live TransformStream.- let (vm, stream) = unsafe { (vm_argument(vm), cell_from_abi::<TransformStream>(stream)) };+ // SAFETY: The caller passes the VM, a live TransformStream and a live value.+ let (vm, stream, chunk) = unsafe {+ (+ vm_argument(vm),+ cell_from_abi::<TransformStream>(stream),+ Value::from_raw(chunk),+ )+ };let cx = CallContext::new(vm, stream.backpressure_change_promise_realm());let controller = stream.controller().expect("a TransformStream that is set up has a controller");- let result = transform_stream_default_controller_enqueue(&cx, controller, Value(chunk));+ let result = transform_stream_default_controller_enqueue(&cx, controller, chunk);completion(&cx, result)}diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.pyindex 1e23ffce712..fabeca22c27 100644--- a/Meta/Generators/libweb_bindings/rust_bindings.py+++ b/Meta/Generators/libweb_bindings/rust_bindings.py@@ -1106,7 +1106,7 @@ def write_rust_item_value(out: TextIO, context: GenerationContext, interface: Inreturn false;}};let value = {conversion}.expect("converting an object to a JS value does not throw");- *out_value = value.0;+ *out_value = value.to_raw();true}}"""