Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Objects with getters that fail do not trigger an error #47

Open
Arshia001 opened this issue Jan 17, 2024 · 1 comment
Open

Objects with getters that fail do not trigger an error #47

Arshia001 opened this issue Jan 17, 2024 · 1 comment

Comments

@Arshia001
Copy link
Contributor

Arshia001 commented Jan 17, 2024

In the implementation of ion::Object::get:

pub fn get<'cx, K: ToPropertyKey<'cx>>(&self, cx: &'cx Context, key: K) -> Option<Value<'cx>> {
let key = key.to_key(cx).unwrap();
if self.has(cx, &key) {
let mut rval = Value::undefined(cx);
unsafe {
JS_GetPropertyById(
cx.as_ptr(),
self.handle().into(),
key.handle().into(),
rval.handle_mut().into(),
)
};
Some(rval)
} else {
None
}
}

The return value of JS_GetPropertyById is not checked, so an object with a getter that fails does not propagate the error. This may also be the case for other calls into SpiderMonkey, but I haven't checked for more.

For context, this is the WPT test that fails as a result of this:

    test(() => {
      assert_throws_js(
        () => new TextDecoderStream('utf-8', {
          get fatal() { throw new Error(); }
        }), 'the constructor should throw');
    }, 'a throwing fatal member should cause the constructor to throw');

Note, the TextDecoderStream implementation is local to WinterJS and I put the test here just to illustrate the problem.

@Arshia001
Copy link
Contributor Author

@Redfire75369 the current implementation of get_as after fixing this issue causes conversion of null to Option<*mut JSObject> to fail. This is probably the correct implementation:

	/// Gets the value at the given key of the [Object]. as a Rust type.
	/// Returns [None] if the object does not contain the key or conversion to the Rust type fails.
	pub fn get_as<'cx, K: ToPropertyKey<'cx>, T: FromValue<'cx>>(
		&self, cx: &'cx Context, key: K, strict: bool, config: T::Config,
	) -> Result<Option<T>> {
		Ok(self.get(cx, key)?.and_then(|val| T::from_value(cx, &val, strict, config).ok()))
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant