Skip to content

Truly independent web browser

LibJS: Let embedders tell whether source code is still held

Aliaksandr Kalenikc7c6bb3edd02
diff --git a/Libraries/LibJS/Rust/src/embedding/abi_types.rs b/Libraries/LibJS/Rust/src/embedding/abi_types.rs
index e0091070239..144ccb5f995 100644
--- a/Libraries/LibJS/Rust/src/embedding/abi_types.rs
+++ b/Libraries/LibJS/Rust/src/embedding/abi_types.rs
@@ -435,6 +435,11 @@ pub struct JSSourceCode {
_opaque: [u8; 0],
}
+/// A weak reference to a SourceCode, which tells whether anything still holds the source code without holding it.
+pub struct JSWeakSourceCode {
+ _opaque: [u8; 0],
+}
+
/// Hands `value` to the embedder's sink.
pub fn append_to_value_sink(sink: &JSValueSink, value: Value) {
let append = sink.append.expect("a value sink has an append function");
diff --git a/Libraries/LibJS/Rust/src/embedding/source_code.rs b/Libraries/LibJS/Rust/src/embedding/source_code.rs
index c63ccf7be1a..7d5ccba34c9 100644
--- a/Libraries/LibJS/Rust/src/embedding/source_code.rs
+++ b/Libraries/LibJS/Rust/src/embedding/source_code.rs
@@ -15,7 +15,7 @@ use std::rc::Rc;
use crate::bytecode::executable::Executable;
use crate::embedding::abi_types::{
- JSOwnedUtf16String, JSSourceCode, owned_utf16_string_from_abi, owned_utf16_string_into_abi,
+ JSOwnedUtf16String, JSSourceCode, JSWeakSourceCode, owned_utf16_string_from_abi, owned_utf16_string_into_abi,
};
use crate::embedding::execution_context::JSExecutionContext;
use crate::layout::execution_context::ExecutionContext;
@@ -151,3 +151,42 @@ pub unsafe extern "C" fn js_source_code_of_execution_context(
.as_ref()
.map_or(core::ptr::null(), source_code_into_abi)
}
+
+/// A weak reference to the source code, which the caller owns and gives up with js_weak_source_code_destroy(). Only
+/// the VM's thread may call this.
+///
+/// # Safety
+///
+/// `source_code` must be live source code.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn js_source_code_downgrade(source_code: *const JSSourceCode) -> *mut JSWeakSourceCode {
+ // SAFETY: The caller passes live source code.
+ let source_code = unsafe { shared_source_code_from_abi(source_code) };
+ Box::into_raw(Box::new(Rc::downgrade(&source_code))).cast()
+}
+
+/// Whether anything still holds the source code of the weak reference. Only the VM's thread may call this.
+///
+/// # Safety
+///
+/// `weak` must be a weak reference that js_source_code_downgrade() handed out and that was not destroyed.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn js_weak_source_code_is_alive(weak: *const JSWeakSourceCode) -> bool {
+ assert!(!weak.is_null(), "the embedder passes a weak reference");
+ // SAFETY: The caller passes a live weak reference, which is a boxed Weak<SourceCode>.
+ unsafe { &*weak.cast::<std::rc::Weak<SourceCode>>() }.strong_count() > 0
+}
+
+/// Destroys a weak reference that js_source_code_downgrade() handed out. Null does nothing. Only the VM's thread may
+/// call this.
+///
+/// # Safety
+///
+/// `weak` must be null or a weak reference that was not destroyed, which the caller gives up.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn js_weak_source_code_destroy(weak: *mut JSWeakSourceCode) {
+ if !weak.is_null() {
+ // SAFETY: The caller gives up a live weak reference, which is a boxed Weak<SourceCode>.
+ drop(unsafe { Box::from_raw(weak.cast::<std::rc::Weak<SourceCode>>()) });
+ }
+}
diff --git a/Libraries/LibJS/SourceCode.cpp b/Libraries/LibJS/SourceCode.cpp
index ba606833ed7..a6902210445 100644
--- a/Libraries/LibJS/SourceCode.cpp
+++ b/Libraries/LibJS/SourceCode.cpp
@@ -51,4 +51,38 @@ void SourceCode::unref() const
js_source_code_release(source_code_to_abi(*this));
}
+static JSWeakSourceCode* weak_source_code_to_abi(FFI::WeakSourceCode* weak)
+{
+ return reinterpret_cast<JSWeakSourceCode*>(weak);
+}
+
+WeakSourceCode::WeakSourceCode(SourceCode const& source_code)
+ : m_weak(reinterpret_cast<FFI::WeakSourceCode*>(js_source_code_downgrade(source_code_to_abi(source_code))))
+{
+}
+
+WeakSourceCode::WeakSourceCode(WeakSourceCode&& other)
+ : m_weak(exchange(other.m_weak, nullptr))
+{
+}
+
+WeakSourceCode& WeakSourceCode::operator=(WeakSourceCode&& other)
+{
+ if (this != &other) {
+ js_weak_source_code_destroy(weak_source_code_to_abi(m_weak));
+ m_weak = exchange(other.m_weak, nullptr);
+ }
+ return *this;
+}
+
+WeakSourceCode::~WeakSourceCode()
+{
+ js_weak_source_code_destroy(weak_source_code_to_abi(m_weak));
+}
+
+bool WeakSourceCode::is_alive() const
+{
+ return m_weak && js_weak_source_code_is_alive(weak_source_code_to_abi(m_weak));
+}
+
}
diff --git a/Libraries/LibJS/SourceCode.h b/Libraries/LibJS/SourceCode.h
index cbda67e5b01..dcda92872d3 100644
--- a/Libraries/LibJS/SourceCode.h
+++ b/Libraries/LibJS/SourceCode.h
@@ -17,6 +17,12 @@
#include <LibJS/Forward.h>
#include <LibJS/Position.h>
+namespace JS::FFI {
+
+struct WeakSourceCode;
+
+}
+
namespace JS {
// The source code that scripts, modules and functions are compiled from. A pointer to it is the Rust runtime's source
@@ -43,6 +49,22 @@ public:
~SourceCode() = delete;
};
+// A weak reference to source code, which tells whether anything still holds it. Only the VM's thread may use it.
+class JS_API WeakSourceCode {
+ AK_MAKE_NONCOPYABLE(WeakSourceCode);
+
+public:
+ explicit WeakSourceCode(SourceCode const&);
+ WeakSourceCode(WeakSourceCode&&);
+ WeakSourceCode& operator=(WeakSourceCode&&);
+ ~WeakSourceCode();
+
+ bool is_alive() const;
+
+private:
+ FFI::WeakSourceCode* m_weak { nullptr };
+};
+
}
template<>