Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/rust/jsg/ffi.c++
Original file line number Diff line number Diff line change
Expand Up @@ -1005,20 +1005,26 @@ DEFINE_TYPED_ARRAY_NEW(bigint64_array, BigInt64Array, int64_t)
DEFINE_TYPED_ARRAY_NEW(biguint64_array, BigUint64Array, uint64_t)

// Wrappers
MaybeLocal wrap_resource(
Isolate* isolate, kj::Rc<Wrappable> wrappable, const Global& tmpl, bool& wrappedElsewhere) {
MaybeLocal wrappable_get_wrapper(Isolate* isolate, Wrappable& wrappable, bool& wrappedElsewhere) {
// Checked first, also while an exception is pending: the caller must then leak its reference
// rather than release it, which would need the other isolate's lock. Reading the wrapper's
// isolate enters nothing in V8.
wrappedElsewhere = wrappable->hasWrapper() && wrappable->getIsolate() != isolate;
wrappedElsewhere = wrappable.hasWrapper() && wrappable.getIsolate() != isolate;
if (wrappedElsewhere || exceptionPending(isolate)) {
return MaybeLocal{.ptr = 0};
}

// Check if already wrapped
KJ_IF_SOME(handle, wrappable->tryGetHandle(isolate)) {
KJ_IF_SOME(handle, wrappable.tryGetHandle(isolate)) {
return maybe_local_to_ffi(v8::MaybeLocal<v8::Value>(handle));
}
return MaybeLocal{.ptr = 0};
}

MaybeLocal wrap_resource(
Isolate* isolate, kj::Rc<Wrappable> wrappable, const Global& tmpl, bool& wrappedElsewhere) {
auto existing = wrappable_get_wrapper(isolate, *wrappable, wrappedElsewhere);
if (existing.ptr != 0 || wrappedElsewhere || exceptionPending(isolate)) {
return existing;
}

auto& global_tmpl = global_as_ref_from_ffi<v8::FunctionTemplate>(tmpl);
auto local_tmpl = v8::Local<v8::FunctionTemplate>::New(isolate, global_tmpl);
Expand Down
4 changes: 4 additions & 0 deletions src/rust/jsg/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,10 @@ kj::uint wrappable_strong_refcount(const Wrappable& wrappable);
// then either, so that wrapping fails the same way whether or not the wrapper exists.
MaybeLocal wrap_resource(
Isolate* isolate, kj::Rc<Wrappable> wrappable, const Global& tmpl, bool& wrappedElsewhere);
// The Wrappable's existing wrapper in `isolate`, as wrap_resource() returns it, without creating
// one: empty also when the Wrappable has no wrapper yet, and `wrappedElsewhere` and a pending
// exception or termination are as for wrap_resource().
MaybeLocal wrappable_get_wrapper(Isolate* isolate, Wrappable& wrappable, bool& wrappedElsewhere);
void wrappable_attach_wrapper(kj::Rc<Wrappable> wrappable, FunctionCallbackInfo& args);
// Attaches an opaque wrapper to `wrappable` and returns it, to be the data of the functions that
// function_new() creates, or the value of a typed promise: the wrapper keeps the Wrappable alive,
Expand Down
31 changes: 27 additions & 4 deletions src/rust/jsg/v8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,13 @@ pub mod ffi {
wrapped_elsewhere: &mut bool,
) -> MaybeLocal /* v8::Local<Value> */;

// The existing wrapper, as `wrap_resource` returns it, without creating one; see `ffi.h`.
pub unsafe fn wrappable_get_wrapper(
isolate: *mut Isolate,
wrappable: Pin<&mut Wrappable>,
wrapped_elsewhere: &mut bool,
) -> MaybeLocal /* v8::Local<Value> */;

pub unsafe fn wrappable_attach_wrapper(
wrappable: KjRc<Wrappable>,
args: Pin<&mut FunctionCallbackInfo>,
Expand Down Expand Up @@ -5699,8 +5706,8 @@ impl WrappableRc {
handle.map(|handle| Self { handle })
}

/// Wraps this Wrappable as a JavaScript object of resource type `R`, using (and on first
/// use creating) `R`'s cached constructor template.
/// Returns this Wrappable's JavaScript wrapper. If it has none yet, creates one as an object
/// of resource type `R`, using (and on first use creating) `R`'s cached constructor template.
///
/// Returns `None` if the Wrappable already has a JavaScript wrapper in another isolate. That
/// is a bug in the caller, which [`resource::wrap`](crate::resource::wrap) turns into a
Expand All @@ -5715,8 +5722,24 @@ impl WrappableRc {
&self,
lock: &mut Lock<'s>,
) -> crate::Result<Option<Local<'s, Value>>> {
let constructor = lock.realm().get_constructor::<R>().as_ffi();
let mut wrapped_elsewhere = false;
// A resource returned again usually has its wrapper already, which needs no template.
// SAFETY: isolate is valid and locked, and the Wrappable is alive while `self` is.
let existing = unsafe {
ffi::wrappable_get_wrapper(
lock.isolate().as_ffi(),
self.as_pin_mut(),
&mut wrapped_elsewhere,
)
};
if wrapped_elsewhere {
return Ok(None);
}
// SAFETY: the wrapper is read into the scope of `lock`.
if let Some(wrapper) = unsafe { Local::from_ffi_maybe(existing) } {
return Ok(Some(wrapper));
}
let constructor = lock.realm().get_constructor::<R>().as_ffi();
// SAFETY: isolate is valid and locked; the constructor is a function template of the
// isolate, which the Realm's template cache keeps alive.
let handle = unsafe {
Expand All @@ -5730,7 +5753,7 @@ impl WrappableRc {
if wrapped_elsewhere {
return Ok(None);
}
// SAFETY: the wrapper is created in (or read into) the scope of `lock`.
// SAFETY: the wrapper is created in the scope of `lock`.
let wrapper = unsafe { Local::from_ffi_maybe(handle) }.ok_or_else(Error::pending)?;
Ok(Some(wrapper))
}
Expand Down
Loading