-
Notifications
You must be signed in to change notification settings - Fork 204
Add DOMException #1040
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
Add DOMException #1040
Conversation
function bjson_test_fuzz() | ||
{ | ||
var corpus = [ | ||
"FBAAAAAABGA=", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this was complaining because of the bytecode version, but I don't know how the corpus was generated so I just incremented it manually...
It's still missing a stack trace when created with the constructor... |
I was confused for a minute because browsers set |
Perhaps you could use Js_NewError and extract it? |
I think that should be fine. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with a small comment, good work!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
@bnoordhuis Any further comments on this one? |
Implemented separately from the other errors because it is defined in terms of WebIDL, where members of an interface are getters on their prototype. See the difference between `JSON.stringify(Object.getOwnPropertyDescriptors(new TypeError()))` vs `JSON.stringify(Object.getOwnPropertyDescriptors(new DOMException()))`. Note: the standard doesn't specify where to put "stack". We follow existing practice which imitates node instead of browsers.
@bnoordhuis Shall I land this? atob / btoa relies on it so maybe it's time for it :-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some comments but mostly LGTM.
quickjs.c
Outdated
JS_CGETSET_DEF("name", js_domexception_get_name, NULL ), | ||
JS_CGETSET_DEF("message", js_domexception_get_message, NULL ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These aren't wrong but if you want to DRY the code, you can do:
JS_CGETSET_DEF("name", js_domexception_get_name, NULL ), | |
JS_CGETSET_DEF("message", js_domexception_get_message, NULL ), | |
JS_CGETSET_MAGIC_DEF("name", js_domexception_getfield, NULL, | |
offsetof(JSDOMExceptionData, name)), | |
JS_CGETSET_MAGIC_DEF("message", js_domexception_getfield, NULL, | |
offsetof(JSDOMExceptionData, message)), |
See js_callsite_getfield for an example:
static JSValue js_callsite_getfield(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic)
{
JSCallSiteData *csd = JS_GetOpaque2(ctx, this_val, JS_CLASS_CALL_SITE);
if (!csd)
return JS_EXCEPTION;
JSValue *field = (void *)((char *)csd + magic);
return js_dup(*field);
}
This is another one of those things we could generalize more because ultimately it just computes:
JSValue *field = (void *)((char *)JS_VALUE_GET_OBJ(this_val)->u.opaque + magic);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, done. (At this scale though, it seems a bit closer to golfing than DRY :P)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not even code golfing, I'm afraid... I tried adding a generalized getter to the whole code base and it added about as many lines of code as it shaved off. Not worth it at the moment, I guess.
JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowReferenceError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); | ||
JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowSyntaxError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); | ||
JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowTypeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); | ||
JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(3, 4) JS_ThrowDOMException(JSContext *ctx, const char *name, JS_PRINTF_FORMAT const char *fmt, ...); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this function do if I don't call JS_AddIntrinsicDOMException first? Maybe it should have an "is initialized?" check?
(My guess is it throws a prototype-less object but I'm not 100% sure.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a user error. I've added an assertion.
Great work @bptato ! |
Implemented separately from the other errors because it is defined in terms of WebIDL, where members of an interface are getters on their prototype.
See the difference between
JSON.stringify(Object.getOwnPropertyDescriptors(new TypeError()))
vsJSON.stringify(Object.getOwnPropertyDescriptors(new DOMException()))
.(Required for btoa/atob and structuredClone; ref. #16, #1032)