From 6dd2ce308a282bc26c192dfa314f484cbab655db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Tue, 10 Sep 2024 13:07:00 +0200 Subject: [PATCH] Fix JS_DetectModule if the first statement is an await --- .github/workflows/ci.yml | 3 +++ Makefile | 1 + quickjs.c | 11 +++++++++-- tests/test_module_detect.js | 5 +++++ 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 tests/test_module_detect.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 21162c8..fb3d86c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -218,6 +218,7 @@ jobs: build\${{matrix.buildType}}\qjs.exe tests\test_std.js build\${{matrix.buildType}}\qjs.exe tests\test_worker.js build\${{matrix.buildType}}\qjs.exe tests\test_queue_microtask.js + build\${{matrix.buildType}}\qjs.exe tests\test_module_detect.js build\${{matrix.buildType}}\function_source.exe windows-clang: @@ -253,6 +254,7 @@ jobs: build\${{matrix.buildType}}\qjs.exe tests\test_std.js build\${{matrix.buildType}}\qjs.exe tests\test_worker.js build\${{matrix.buildType}}\qjs.exe tests\test_queue_microtask.js + build\${{matrix.buildType}}\qjs.exe tests\test_module_detect.js build\${{matrix.buildType}}\function_source.exe windows-ninja: @@ -292,6 +294,7 @@ jobs: build\qjs.exe tests\test_std.js build\qjs.exe tests\test_worker.js build\qjs.exe tests\test_queue_microtask.js + build\qjs.exe tests\test_module_detect.js build\function_source.exe windows-mingw: diff --git a/Makefile b/Makefile index 0f3b565..72efeb9 100644 --- a/Makefile +++ b/Makefile @@ -88,6 +88,7 @@ test: $(QJS) $(QJS) tests/test_std.js $(QJS) tests/test_worker.js $(QJS) tests/test_queue_microtask.js + $(QJS) tests/test_module_detect.js testconv: $(BUILD_DIR)/test_conv $(BUILD_DIR)/test_conv diff --git a/quickjs.c b/quickjs.c index 33ea6a7..1a48a26 100644 --- a/quickjs.c +++ b/quickjs.c @@ -20007,6 +20007,9 @@ static int simple_next_token(const uint8_t **pp, BOOL no_line_terminator) p[2] == 'c' && p[3] == 't' && p[4] == 'i' && p[5] == 'o' && p[6] == 'n' && !lre_js_is_ident_next(p[7])) { return TOK_FUNCTION; + } else if (c == 'a' && p[0] == 'w' && p[1] == 'a' && + p[2] == 'i' && p[3] == 't' && !lre_js_is_ident_next(p[4])) { + return TOK_AWAIT; } return TOK_IDENT; } @@ -20048,8 +20051,11 @@ static void skip_shebang(const uint8_t **pp, const uint8_t *buf_end) /* return true if 'input' contains the source of a module (heuristic). 'input' must be a zero terminated. - Heuristic: skip comments and expect 'import' keyword not followed - by '(' or '.' or export keyword. + Heuristic: + - Skip comments + - Expect 'import' keyword not followed by '(' or '.' + - Expect 'export' keyword + - Expect 'await' keyword */ /* input is pure ASCII or UTF-8 encoded source code */ BOOL JS_DetectModule(const char *input, size_t input_len) @@ -20062,6 +20068,7 @@ BOOL JS_DetectModule(const char *input, size_t input_len) case TOK_IMPORT: tok = simple_next_token(&p, FALSE); return (tok != '.' && tok != '('); + case TOK_AWAIT: case TOK_EXPORT: return TRUE; default: diff --git a/tests/test_module_detect.js b/tests/test_module_detect.js new file mode 100644 index 0000000..7ce9d22 --- /dev/null +++ b/tests/test_module_detect.js @@ -0,0 +1,5 @@ +// This needs to be parsed as a module or will throw SyntaxError. +// + +await 0; +