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

Syntax errors are ignored in loaded modules #438

Open
windbridges opened this issue Mar 17, 2020 · 2 comments
Open

Syntax errors are ignored in loaded modules #438

windbridges opened this issue Mar 17, 2020 · 2 comments
Assignees
Labels

Comments

@windbridges
Copy link

When required script contains syntax errors, it silently skips it just like file was not included.

            $v8 = new V8Js();
            $v8->setModuleLoader(function ($path) {
                return 'synta}{ error!'; // returns undefined
                // return 'module.exports = 1'; // this works
            });
            $script = 'let a = require("file.js"); print(a)';
            $r = $v8->executeString($script);
            echo $r;

Is there any way to get syntax error exception when module loader returns invalid code?

@redbullmarky
Copy link
Collaborator

redbullmarky commented Jun 1, 2022

@stesie is this literally as simple as adding (in v8js_methods.cc):

v8js_throw_script_exception(c->isolate, &try_catch);

in place of:

info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Module script compile failed")));

to somewhat match the one found in v8js_compile_script ? or perhaps refactoring to use v8js_compile_script for modules, too?

trying it out seems to do what you'd want; i'm just not 100% sure what the other side-effects of doing so might be.

@redbullmarky redbullmarky self-assigned this Feb 24, 2023
@redbullmarky
Copy link
Collaborator

@stesie here's what i'm proposing in v8js_methods.cc:

if (script.IsEmpty()) {
    efree(normalised_module_id);
    efree(normalised_path);
    
    if (try_catch.HasCaught()) {
        if (c->in_execution < 1) {
            v8js_throw_script_exception(c->isolate, &try_catch);
            return;
        }

        /* Rethrow back to JS */
        try_catch.ReThrow();
    }
    return;
}

produces (using the OP's test example):

PHP Fatal error:  Uncaught V8JsScriptException: file.js:1: SyntaxError: Unexpected token '{' in......

If that seems fine, i'll get a PR for php7 & 8 sorted :)

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

No branches or pull requests

2 participants