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

Can't catch exceptions when I upgrade version from 2023-12-09 to 2024-01-13 #232

Open
mengbai123 opened this issue Feb 5, 2024 · 3 comments

Comments

@mengbai123
Copy link

    LOGW("### JS START ###");
    Js_Engine.env = env;

    string testCode = "print('hello from code.'); throw new Error('error from code!')";

    JSRuntime *rt = JS_NewRuntime();
    js_std_init_handlers(rt);
    JSContext *ctx = JS_NewContext(rt);

    JSValue global_obj = JS_GetGlobalObject(ctx);
    JS_SetPropertyStr(ctx, global_obj, "print", JS_NewCFunction(ctx, jsf_print, "print", 1));
    JS_FreeValue(ctx, global_obj);

    JSValue jsValue = JS_Eval(ctx, testCode.c_str(), testCode.length(), "main",
                              JS_EVAL_TYPE_MODULE);

    JSValue error = JS_GetException(ctx);

    double dValue;
    JS_ToFloat64(ctx, &dValue, jsValue);

    LOGE("isError: %d isException: %d dValue:%f", JS_IsError(ctx, error), JS_IsException(error), dValue);

    JS_FreeValue(ctx, error);

    JS_FreeValue(ctx, jsValue);
    JS_FreeContext(ctx);
    JS_FreeRuntime(rt);

    LOGW("### JS END ###");

output:

### JS START ###
hello from code.
isError: 0 isException: 0 dValue:nan
### JS END ###

Running on an Android device, through jni calls, I did not make any changes to the source code
On the version 2023-12-09, running errors can be caught normally

@mengbai123
Copy link
Author

run same code on 2023-12-09 version output

### JS START ###
hello from code.
isError: 1 isException: 0 dValue:nan
### JS END ###

@andrieshiemstra
Copy link

Reproducible on linux, but only when using eval with JS_EVAL_TYPE_MODULE, e.g. JS_EVAL_TYPE_GLOBAL works as expected

@andrieshiemstra
Copy link

andrieshiemstra commented Feb 5, 2024

ah, since module eval now support top-level await (see https://bellard.org/quickjs/Changelog) evalling a module actually returns a Promise.. my bet is that Promise will resolve to an error if code throws error....

-- edit --

confirmed, eval_module returns a promise which is rejected to the error thrown in code..

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

2 participants