Skip to content

Commit 2785ede

Browse files
author
Fabrice Bellard
committed
fixed JS module autodetection with shebang (github issue bellard#91)
1 parent b4d8050 commit 2785ede

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

quickjs.c

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21225,6 +21225,31 @@ static int peek_token(JSParseState *s, BOOL no_line_terminator)
2122521225
return simple_next_token(&p, no_line_terminator);
2122621226
}
2122721227

21228+
static void skip_shebang(const uint8_t **pp, const uint8_t *buf_end)
21229+
{
21230+
const uint8_t *p = *pp;
21231+
int c;
21232+
21233+
if (p[0] == '#' && p[1] == '!') {
21234+
p += 2;
21235+
while (p < buf_end) {
21236+
if (*p == '\n' || *p == '\r') {
21237+
break;
21238+
} else if (*p >= 0x80) {
21239+
c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
21240+
if (c == CP_LS || c == CP_PS) {
21241+
break;
21242+
} else if (c == -1) {
21243+
p++; /* skip invalid UTF-8 */
21244+
}
21245+
} else {
21246+
p++;
21247+
}
21248+
}
21249+
*pp = p;
21250+
}
21251+
}
21252+
2122821253
/* return true if 'input' contains the source of a module
2122921254
(heuristic). 'input' must be a zero terminated.
2123021255

@@ -21235,6 +21260,8 @@ BOOL JS_DetectModule(const char *input, size_t input_len)
2123521260
{
2123621261
const uint8_t *p = (const uint8_t *)input;
2123721262
int tok;
21263+
21264+
skip_shebang(&p, p + input_len);
2123821265
switch(simple_next_token(&p, FALSE)) {
2123921266
case TOK_IMPORT:
2124021267
tok = simple_next_token(&p, FALSE);
@@ -33850,31 +33877,6 @@ JSValue JS_EvalFunction(JSContext *ctx, JSValue fun_obj)
3385033877
return JS_EvalFunctionInternal(ctx, fun_obj, ctx->global_obj, NULL, NULL);
3385133878
}
3385233879

33853-
static void skip_shebang(JSParseState *s)
33854-
{
33855-
const uint8_t *p = s->buf_ptr;
33856-
int c;
33857-
33858-
if (p[0] == '#' && p[1] == '!') {
33859-
p += 2;
33860-
while (p < s->buf_end) {
33861-
if (*p == '\n' || *p == '\r') {
33862-
break;
33863-
} else if (*p >= 0x80) {
33864-
c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
33865-
if (c == CP_LS || c == CP_PS) {
33866-
break;
33867-
} else if (c == -1) {
33868-
p++; /* skip invalid UTF-8 */
33869-
}
33870-
} else {
33871-
p++;
33872-
}
33873-
}
33874-
s->buf_ptr = p;
33875-
}
33876-
}
33877-
3387833880
/* 'input' must be zero terminated i.e. input[input_len] = '\0'. */
3387933881
static JSValue __JS_EvalInternal(JSContext *ctx, JSValueConst this_obj,
3388033882
const char *input, size_t input_len,
@@ -33890,7 +33892,7 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValueConst this_obj,
3389033892
JSModuleDef *m;
3389133893

3389233894
js_parse_init(ctx, s, input, input_len, filename);
33893-
skip_shebang(s);
33895+
skip_shebang(&s->buf_ptr, s->buf_end);
3389433896

3389533897
eval_type = flags & JS_EVAL_TYPE_MASK;
3389633898
m = NULL;

tests/test_std.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#! (shebang test)
12
import * as std from "std";
23
import * as os from "os";
34

0 commit comments

Comments
 (0)