quickjs-ng / quickjs

QuickJS, the Next Generation: a mighty JavaScript engine
MIT License
691 stars 66 forks source link

[Feature Request] Add `JS_ThrowError()` convenience function #375

Closed TooTallNate closed 1 month ago

TooTallNate commented 2 months ago

There are convenience functions such as JS_ThrowTypeError() which create a new TypeError using printf syntax, however there is no such function for a regular Error instance. Would it make sense to add that? Or is there some reason why it doesn't exist already?

saghul commented 2 months ago

I guess it was skipped because it's a 2 liner? You can JS_NewError, assign the message and then JS_Throw the error object. That said, the printf syntax is very convenient, I think we should have it.

TooTallNate commented 2 months ago

JS_NewError only takes a ctx arg, not a message value, so you need to do the whole JS_DefinePropertyValueStr(ctx, err, "message", JS_NewString(ctx, message), JS_PROP_C_W) dance

saghul commented 2 months ago

Yeah I realized midways writing the reply 😅 . A PR would be welcome!

Icemic commented 1 month ago

In fact, static JSValue JS_ThrowError(JSContext *ctx, JSErrorEnum error_num, const char *fmt, va_list ap) is already exists, but it is a un-exported static function inside quickjs.c.

saghul commented 1 month ago

Not quite, that function doesn't throw plain Error objects.

saghul commented 1 month ago

PR: https://github.com/quickjs-ng/quickjs/pull/411