Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
69d211e
Add jsg::newDOMException(), a V8-typed way to create a DOMException
bartlomieju Nov 3, 2026
a2ea2a8
Throw DOMExceptions from Rust JSG
bartlomieju Nov 3, 2026
b5aed88
Add Local::to_detail_string() to Rust JSG
bartlomieju Oct 28, 2026
f01d8bd
Port MemoryCache.read() to Rust, behind an autogate
bartlomieju Nov 4, 2026
6a6700f
Add Lock::global_object() to Rust JSG
bartlomieju Nov 3, 2026
9376199
Match the C++ MemoryCache in delete() and in calling the fallback
bartlomieju Nov 3, 2026
f301a55
Throw or reject from the Rust MemoryCache.read() as C++ does
bartlomieju Nov 3, 2026
6ace636
Check that the Rust MemoryCache rejects an unserializable value as C+…
bartlomieju Nov 4, 2026
6c658c3
Run the MemoryCache JS tests against the Rust MemoryCache in every va…
bartlomieju Nov 4, 2026
db33a0c
Convert what a MemoryCache fallback produces as C++ JSG does
bartlomieju Nov 4, 2026
72551b1
Let Rust JSG deserialize errors without their stacks
bartlomieju Nov 4, 2026
3a3bf3c
Add jsg::TunneledException to Rust JSG
bartlomieju Nov 4, 2026
146a522
Reject as C++ does when serializing a fallback's value throws
bartlomieju Nov 4, 2026
6b26479
Add Lock::throw_value() and Lock::new_object_with_null_prototype()
bartlomieju Nov 4, 2026
4364343
Settle Rust MemoryCache reads after as many microtasks as C++
bartlomieju Nov 4, 2026
30be26d
Test a MemoryCache fallback whose promise another request settles
bartlomieju Nov 4, 2026
8615a39
Merge branch 'main' into biwanczuk/rust-memory-cache-read
bartlomieju Nov 7, 2026
52d7921
Merge branch 'main' into biwanczuk/rust-memory-cache-read
bartlomieju Nov 9, 2026
b3bd116
Release the Rust MemoryCache binding with the request's clock
bartlomieju Nov 9, 2026
2393ff7
Update src/rust/jsg/README.md
bartlomieju Nov 9, 2026
0db1504
Merge remote-tracking branch 'origin/main' into biwanczuk/rust-memory…
bartlomieju Nov 11, 2026
e9b51db
Let the IoContext own a span from Rust, and use what it owns in place
bartlomieju Nov 11, 2026
bafc202
Let the IoContext own the spans of a Rust memory cache fallback
bartlomieju Nov 11, 2026
b5ae6b8
Merge remote-tracking branch 'origin/main' into biwanczuk/rust-memory…
bartlomieju Nov 11, 2026
b30e0d0
Merge remote-tracking branch 'origin/main' into biwanczuk/rust-memory…
bartlomieju Nov 11, 2026
0cc8a97
Allow Windows' name for CacheValueProduceResult in memory-cache-test
bartlomieju Nov 11, 2026
e1cb715
Merge remote-tracking branch 'origin/main' into biwanczuk/rust-memory…
bartlomieju Nov 12, 2026
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
13 changes: 13 additions & 0 deletions src/rust/jsg-test/ffi.c++
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,19 @@ bool cpp_sees_empty_maybe_local(::workerd::rust::jsg::MaybeLocal value) {
return ::workerd::rust::jsg::to_ffi(kj::mv(result));
}

::workerd::rust::jsg::MaybeLocal cpp_tunnel_exception(
Isolate* isolate, ::workerd::rust::jsg::Local exception) {
auto& js = ::workerd::jsg::Lock::from(isolate);
try {
auto tunneled = js.exceptionToKj(::workerd::jsg::JsValue(
::workerd::rust::jsg::local_from_ffi<v8::Value>(kj::mv(exception))));
v8::Local<v8::Value> result = js.exceptionToJsValue(kj::mv(tunneled)).getHandle(js);
return ::workerd::rust::jsg::maybe_local_to_ffi(v8::MaybeLocal<v8::Value>(result));
} catch (::workerd::jsg::JsExceptionThrown&) {
return ::workerd::rust::jsg::maybe_local_to_ffi(v8::MaybeLocal<v8::Value>());
}
}

void mark_js_serializable(
Isolate* isolate, ::workerd::rust::jsg::Local object, ::workerd::rust::jsg::Local mark) {
auto& js = ::workerd::jsg::Lock::from(isolate);
Expand Down
5 changes: 5 additions & 0 deletions src/rust/jsg-test/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ ::rust::Vec<uint8_t> cpp_serialize(Isolate* isolate, ::workerd::rust::jsg::Local
// deserialization throws.
::workerd::rust::jsg::Local cpp_deserialize(Isolate* isolate, ::rust::Slice<const uint8_t> data);

// Converts `exception` to a kj::Exception and back, as C++ JSG does. Returns none if converting it
// threw, leaving that exception pending.
::workerd::rust::jsg::MaybeLocal cpp_tunnel_exception(
Isolate* isolate, ::workerd::rust::jsg::Local exception);

// Marks `object` as an instance of a class implemented in JavaScript with `mark`, which is its
// class's descriptor if it is cloneable (see jsg::Serializer). Throws a KJ exception if it is not
// an object.
Expand Down
27 changes: 27 additions & 0 deletions src/rust/jsg-test/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ mod ffi {
#[allow(dead_code)]
pub unsafe fn cpp_deserialize(isolate: *mut Isolate, data: &[u8]) -> Result<Local>;

/// Converts `exception` to a `kj::Exception` and back, as C++ JSG does with
/// `jsg::Lock::exceptionToKj()` and `jsg::Lock::exceptionToJsValue()`. Fails if
/// converting it throws, leaving that exception pending.
#[expect(clippy::allow_attributes)]
#[allow(dead_code)]
pub unsafe fn cpp_tunnel_exception(isolate: *mut Isolate, exception: Local) -> MaybeLocal;

/// Marks `object` as an instance of a class implemented in JavaScript with `mark`, which
/// is its class's descriptor if it is cloneable. Fails if it is not an object.
#[expect(clippy::allow_attributes)]
Expand Down Expand Up @@ -703,6 +710,26 @@ impl Harness {
}
}

/// Converts `exception` to a `kj::Exception` and back with C++ JSG, to compare with
/// [`jsg::TunneledException`].
///
/// # Errors
///
/// Fails, leaving the exception pending, if converting it throws.
pub fn cpp_tunnel_exception<'s>(
lock: &mut jsg::Lock<'s>,
exception: v8::Local<'s, v8::Value>,
) -> Result<v8::Local<'s, v8::Value>, jsg::Error> {
// SAFETY: as for `cpp_serialize`; the result belongs to the lock's scope.
unsafe {
v8::Local::from_ffi_maybe(ffi::cpp_tunnel_exception(
lock.isolate().as_ffi(),
exception.into_ffi(),
))
}
.ok_or_else(jsg::Error::pending)
}

/// Holds a value the way C++ code holds one that Rust created. C++ calls `create`, as it
/// would call a crate's `extern "Rust"` constructor, and keeps the result only in a
/// `v8::Global` while a full GC runs. It then calls `check` with the value, resets the
Expand Down
20 changes: 20 additions & 0 deletions src/rust/jsg-test/tests/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ fn call_with_object_receiver_via_into() {
});
}

/// `Lock::global_object()` is `globalThis`, the receiver C++ JSG gives a `jsg::Function`.
#[test]
fn call_with_global_object_receiver() {
let harness = crate::Harness::new();
harness.run_in_context(|lock, ctx| {
let func_val = ctx
.eval_raw(
lock,
"(function() { 'use strict'; return this === globalThis; })",
)
.unwrap();
let func = v8::Local::<v8::Function>::try_from(func_val).unwrap();

let global = lock.global_object();
let result = func.call(lock, Some(global.into()), &[])?;
assert!(bool::from_js(lock, result)?);
Ok(())
});
}

/// `Local<Function>::call` leaves what the function threw pending, which `try_catch` catches.
#[test]
fn call_leaves_thrown_exception_pending() {
Expand Down
21 changes: 21 additions & 0 deletions src/rust/jsg-test/tests/local_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,24 @@ fn stored_value_round_trips_through_traced_global() {
Ok(())
});
}

#[test]
fn to_detail_string_describes_a_value_without_running_javascript() {
crate::Harness::new().run_in_context(|lock, ctx| {
let cases = [
("new TypeError('bad')", "TypeError: bad"),
("'text'", "text"),
("42", "42"),
("undefined", "undefined"),
(
"({ toString() { throw new Error('ran'); } })",
"[object Object]",
),
];
for (code, expected) in cases {
let value = ctx.eval_raw(lock, code).unwrap();
assert_eq!(value.to_detail_string(lock)?, expected, "{code}");
}
Ok(())
});
}
65 changes: 65 additions & 0 deletions src/rust/jsg-test/tests/lock_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ impl Thrower {
lock.throw_exception(&jsg::Error::invalid_string_length());
}

/// Throws `value`, and then, if `then` is given, throws it too, which must not replace
/// `value`.
#[jsg_method]
fn throw_value<'s>(
&self,
lock: &mut jsg::Lock<'s>,
value: jsg::v8::Local<'s, jsg::v8::Value>,
then: Option<jsg::v8::Local<'s, jsg::v8::Value>>,
) -> jsg::Result<()> {
let err = lock.throw_value(value);
assert!(err.is_pending());
if let Some(then) = then {
return Err(lock.throw_value(then));
}
Err(err)
}

#[jsg_method]
fn null_prototype_object<'s>(
&self,
lock: &mut jsg::Lock<'s>,
) -> jsg::v8::Local<'s, jsg::v8::Object> {
lock.new_object_with_null_prototype()
}

#[jsg_method]
fn range_error<'s>(
&self,
Expand Down Expand Up @@ -105,3 +130,43 @@ fn range_error_is_a_range_error_object() {
Ok(())
});
}

#[test]
fn throw_value_throws_the_value_itself() {
crate::Harness::new().run_in_context(|lock, ctx| {
let thrower = jsg::Rc::new(lock, Thrower);
ctx.set_global(lock, "thrower", thrower);
assert!(caught(
lock,
ctx,
"globalThis.value = { thrown: true }; thrower.throwValue(value)",
"caught === value"
));
assert!(caught(lock, ctx, "thrower.throwValue(42)", "caught === 42"));
// A second throw leaves the first exception pending.
assert!(caught(
lock,
ctx,
"thrower.throwValue('first', 'second')",
"caught === 'first'"
));
Ok(())
});
}

#[test]
fn null_prototype_object_has_no_prototype() {
crate::Harness::new().run_in_context(|lock, ctx| {
let thrower = jsg::Rc::new(lock, Thrower);
ctx.set_global(lock, "thrower", thrower);
assert!(
ctx.eval::<bool>(
lock,
"{ const o = thrower.nullPrototypeObject();
Object.getPrototypeOf(o) === null && Reflect.ownKeys(o).length === 0 }"
)
.unwrap()
);
Ok(())
});
}
1 change: 1 addition & 0 deletions src/rust/jsg-test/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ mod to_js_creator;
mod to_js_errors;
mod traceable_gc;
mod traced_global_lifetime;
mod tunneled_exception;
mod typed_promise;
mod unwrap;
mod wildcard_property;
29 changes: 29 additions & 0 deletions src/rust/jsg-test/tests/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,35 @@ fn errors_with_enhanced_error_serialization() {
});
}

/// Without `preserve_stack_in_errors`, an error keeps the stack it gets when read, and its other
/// properties, as `jsg::Deserializer` does without `preserveStackInErrors`.
#[test]
fn errors_without_their_stacks() {
let harness = Harness::with_flags(|mut flags| flags.set_enhanced_error_serialization(true));
harness.run_in_context(|lock, ctx| {
let value = eval(
lock,
ctx,
"Object.assign(new Error('e'), { stack: 'original', code: 42 })",
);
let data = jsg::ser::serialize(lock, value)?;
let options = jsg::ser::DeserializeOptions {
preserve_stack_in_errors: false,
};
let copy = jsg::ser::deserialize_with(lock, &data, options)?;
ctx.set_global(lock, "v", copy);
assert!(
ctx.eval::<bool>(
lock,
"v instanceof Error && v.message === 'e' && v.code === 42 && \
typeof v.stack === 'string' && v.stack !== 'original'"
)
.unwrap()
);
Ok(())
});
}

/// Where `jsg::Serializer` loses part of an error, the Rust serializer loses the same part, so
/// that the bytes stay the same.
#[test]
Expand Down
Loading
Loading