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
9 changes: 5 additions & 4 deletions src/rust/jsg-macros/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,11 +684,12 @@ fn invocation(
match receiver {
Receiver::SelfRef => quote! {
let this = args.this();
// A callback without a signature may receive a wrong receiver. Throw V8's
// "Illegal invocation" rather than panic, which would terminate execution.
// A callback without a signature may receive a wrong receiver. Throw the
// "Illegal invocation" TypeError that V8's signature check throws, rather than
// panic, which would terminate execution.
let resource = #resolve;
let Some(resource) = resource else {
lock.throw_exception(&jsg::Error::new_type_error("Illegal invocation"));
lock.throw_exception(&jsg::Error::new_illegal_invocation());
return;
};
// SAFETY: `resource` points at the `Self` of `this`, whose wrapper keeps it alive
Expand All @@ -705,7 +706,7 @@ fn invocation(
let this = match <jsg::Rc<Self> as jsg::FromJS<'_>>::try_from_js(lock, args.this()) {
Ok(Some(this)) => this,
Ok(None) => {
lock.throw_exception(&jsg::Error::new_type_error("Illegal invocation"));
lock.throw_exception(&jsg::Error::new_illegal_invocation());
return;
}
Err(err) => {
Expand Down
2 changes: 1 addition & 1 deletion src/rust/jsg-macros/wildcard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl Signature {
// TypeError rather than a panic.
let resource = jsg::v8::resolve_receiver::<Self>(lock, this);
let Some(resource) = resource else {
lock.throw_exception(&jsg::Error::new_type_error("Illegal invocation"));
lock.throw_exception(&jsg::Error::new_illegal_invocation());
return jsg::Intercepted::Yes;
};
// V8 calls the interceptor only for string names (`kOnlyInterceptStrings`), whose
Expand Down
6 changes: 3 additions & 3 deletions src/rust/jsg-test/tests/js_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn coerces_method_arguments() {
assert!(ctx.eval::<bool>(lock, "strings.optional() === undefined").unwrap());
assert!(ctx.eval::<bool>(lock, "strings.optional(null) === 'null'").unwrap());
assert!(ctx.eval::<bool>(lock, "strings.optional(42) === '42'").unwrap());
assert!(ctx.eval::<bool>(lock, "try { strings.echo(); false } catch (e) { e instanceof TypeError && e.message.includes('string') }").unwrap());
assert!(ctx.eval::<bool>(lock, "try { strings.echo(); false } catch (e) { e instanceof TypeError && e.message.endsWith(\"parameter 1 is not of type 'String'.\") }").unwrap());
Ok(())
});
}
Expand All @@ -74,8 +74,8 @@ fn preserves_utf16_and_exposes_string_operations() {
let round_tripped: v8::Local<'_, v8::Value> = v8::Local::<v8::String>::from(string).into();
assert_eq!(round_tripped, value);
assert_eq!(v8::Local::<v8::Value>::from(JsString::from(handle)), value);
assert_eq!(JsString::class_name(), "string");
assert_eq!(JsString::type_name(), "string");
assert_eq!(JsString::class_name(), "String");
assert_eq!(JsString::type_name(), "String");

let strings = jsg::Rc::new(lock, Strings);

Expand Down
12 changes: 7 additions & 5 deletions src/rust/jsg-test/tests/resource_this.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,17 +304,19 @@ fn cycle_through_this_and_a_traced_global_is_collected() {
});
}

/// Asserts that `code` throws a `TypeError` about an illegal invocation.
/// Asserts that `code` throws the `TypeError` that C++ JSG throws for an illegal invocation.
fn assert_illegal_invocation(lock: &mut jsg::Lock<'_>, ctx: &crate::EvalContext<'_>, code: &str) {
let err = ctx
.eval::<String>(lock, code)
.unwrap_err()
.unwrap_jsg_err(lock);
assert_eq!(err.name, ExceptionType::TypeError, "{code}");
assert!(
err.message.contains("Illegal invocation"),
"{code}: {}",
err.message
assert_eq!(
err.message,
"Illegal invocation: function called with incorrect `this` reference. See \
https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors \
for details.",
"{code}"
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/rust/jsg/fast_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ unsafe fn call_method<R: Resource, T: FastApiReturn>(
let trait_ptr = unsafe { ffi::fast_api_unwrap_receiver(options, receiver) };
let Some(resource) = trait_ptr.resolve::<R>() else {
// SAFETY: as above.
return unsafe { throw(options, &Error::new_type_error("Illegal invocation")) };
return unsafe { throw(options, &Error::new_illegal_invocation()) };
};
// SAFETY: The receiver keeps the resource alive for the call. A fast callback cannot run
// JavaScript, so nothing else can reach the resource while this borrow lives.
Expand Down
12 changes: 12 additions & 0 deletions src/rust/jsg/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,18 @@ impl_error_constructors! {
ReferenceError => new_reference_error,
}

impl Error {
/// The `TypeError` for a callback whose `this` is not a wrapper of its resource, with the
/// message that C++ JSG and V8's signature check throw (`kIllegalInvocation`).
pub fn new_illegal_invocation() -> Self {
Self::new_type_error(
"Illegal invocation: function called with incorrect `this` reference. See \
https://developers.cloudflare.com/workers/observability/errors/\
#illegal-invocation-errors for details.",
)
}
}

impl<'s> FromJS<'s> for Error {
type ResultType = Self;

Expand Down
6 changes: 4 additions & 2 deletions src/rust/jsg/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ impl<'s> From<JsString<'s>> for v8::Local<'s, v8::Value> {
impl Type for JsString<'_> {
const UNION_TYPES: UnionTypes = UnionTypes::STRING;

/// `String`, as C++ JSG names a `jsg::JsString`; a `String` parameter is a `string`, as C++
/// JSG names a `kj::String`.
fn class_name() -> &'static str {
"string"
"String"
}
}

Expand All @@ -68,7 +70,7 @@ impl<'s> FromJS<'s> for JsString<'s> {
const UNION_MEMBER: UnionMember = UnionMember::String;

fn type_name() -> String {
"string".to_owned()
Self::class_name().to_owned()
}

fn from_js(lock: &mut Lock<'s>, value: v8::Local<'s, v8::Value>) -> Result<Self, Error> {
Expand Down
Loading