Skip to content

Commit

Permalink
fixed JS module autodetection with shebang (github issue #91)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrice Bellard committed Dec 27, 2023
1 parent b4d8050 commit 2785ede
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
54 changes: 28 additions & 26 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -21225,6 +21225,31 @@ static int peek_token(JSParseState *s, BOOL no_line_terminator)
return simple_next_token(&p, no_line_terminator);
}

static void skip_shebang(const uint8_t **pp, const uint8_t *buf_end)
{
const uint8_t *p = *pp;
int c;

if (p[0] == '#' && p[1] == '!') {
p += 2;
while (p < buf_end) {
if (*p == '\n' || *p == '\r') {
break;
} else if (*p >= 0x80) {
c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
if (c == CP_LS || c == CP_PS) {
break;
} else if (c == -1) {
p++; /* skip invalid UTF-8 */
}
} else {
p++;
}
}
*pp = p;
}
}

/* return true if 'input' contains the source of a module
(heuristic). 'input' must be a zero terminated.

Expand All @@ -21235,6 +21260,8 @@ BOOL JS_DetectModule(const char *input, size_t input_len)
{
const uint8_t *p = (const uint8_t *)input;
int tok;

skip_shebang(&p, p + input_len);
switch(simple_next_token(&p, FALSE)) {
case TOK_IMPORT:
tok = simple_next_token(&p, FALSE);
Expand Down Expand Up @@ -33850,31 +33877,6 @@ JSValue JS_EvalFunction(JSContext *ctx, JSValue fun_obj)
return JS_EvalFunctionInternal(ctx, fun_obj, ctx->global_obj, NULL, NULL);
}

static void skip_shebang(JSParseState *s)
{
const uint8_t *p = s->buf_ptr;
int c;

if (p[0] == '#' && p[1] == '!') {
p += 2;
while (p < s->buf_end) {
if (*p == '\n' || *p == '\r') {
break;
} else if (*p >= 0x80) {
c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
if (c == CP_LS || c == CP_PS) {
break;
} else if (c == -1) {
p++; /* skip invalid UTF-8 */
}
} else {
p++;
}
}
s->buf_ptr = p;
}
}

/* 'input' must be zero terminated i.e. input[input_len] = '\0'. */
static JSValue __JS_EvalInternal(JSContext *ctx, JSValueConst this_obj,
const char *input, size_t input_len,
Expand All @@ -33890,7 +33892,7 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValueConst this_obj,
JSModuleDef *m;

js_parse_init(ctx, s, input, input_len, filename);
skip_shebang(s);
skip_shebang(&s->buf_ptr, s->buf_end);

eval_type = flags & JS_EVAL_TYPE_MASK;
m = NULL;
Expand Down
1 change: 1 addition & 0 deletions tests/test_std.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#! (shebang test)
import * as std from "std";
import * as os from "os";

Expand Down

0 comments on commit 2785ede

Please sign in to comment.