| 1 | diff --git a/quickjs-amalgam.c b/quickjs-amalgam.c |
| 2 | index 6f9fdd5..3f252df 100644 |
| 3 | --- a/quickjs-amalgam.c |
| 4 | +++ b/quickjs-amalgam.c |
| 5 | @@ -71723,6 +71723,135 @@ bool JS_DetectModule(const char *input, size_t input_len) |
| 6 | #endif // QJS_DISABLE_PARSER |
| 7 | } |
| 8 | |
| 9 | +/* |
| 10 | + * QTS_DetectModule - bellard/quickjs style module detection heuristic |
| 11 | + * Copied from bellard/quickjs, Copyright (c) 2017-2021 Fabrice Bellard |
| 12 | + * MIT licensed. |
| 13 | + * |
| 14 | + * Heuristic: skip comments and expect 'import' keyword not followed |
| 15 | + * by '(' or '.' or 'export' keyword. |
| 16 | + */ |
| 17 | + |
| 18 | +#define QTS_TOK_IMPORT 0x100 |
| 19 | +#define QTS_TOK_EXPORT 0x101 |
| 20 | +#define QTS_TOK_IDENT 0x102 |
| 21 | + |
| 22 | +static int qts_match_identifier(const uint8_t *p, const char *s) { |
| 23 | + uint32_t c; |
| 24 | + while (*s) { |
| 25 | + if ((uint8_t)*s++ != *p++) |
| 26 | + return 0; |
| 27 | + } |
| 28 | + c = *p; |
| 29 | + if (c >= 128) |
| 30 | + c = utf8_decode(p, &p); |
| 31 | + return !lre_js_is_ident_next(c); |
| 32 | +} |
| 33 | + |
| 34 | +static void qts_skip_shebang(const uint8_t **pp, const uint8_t *buf_end) |
| 35 | +{ |
| 36 | + const uint8_t *p = *pp; |
| 37 | + int c; |
| 38 | + |
| 39 | + if (p[0] == '#' && p[1] == '!') { |
| 40 | + p += 2; |
| 41 | + while (p < buf_end) { |
| 42 | + if (*p == '\n' || *p == '\r') { |
| 43 | + break; |
| 44 | + } else if (*p >= 0x80) { |
| 45 | + c = utf8_decode(p, &p); |
| 46 | + if (c == CP_LS || c == CP_PS) { |
| 47 | + break; |
| 48 | + } else if (c == -1) { |
| 49 | + p++; /* skip invalid UTF-8 */ |
| 50 | + } |
| 51 | + } else { |
| 52 | + p++; |
| 53 | + } |
| 54 | + } |
| 55 | + *pp = p; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +static int qts_simple_next_token(const uint8_t **pp) |
| 60 | +{ |
| 61 | + const uint8_t *p; |
| 62 | + uint32_t c; |
| 63 | + |
| 64 | + /* skip spaces and comments */ |
| 65 | + p = *pp; |
| 66 | + for (;;) { |
| 67 | + switch(c = *p++) { |
| 68 | + case '\r': |
| 69 | + case '\n': |
| 70 | + case ' ': |
| 71 | + case '\t': |
| 72 | + case '\v': |
| 73 | + case '\f': |
| 74 | + continue; |
| 75 | + case '/': |
| 76 | + if (*p == '/') { |
| 77 | + while (*p && *p != '\r' && *p != '\n') |
| 78 | + p++; |
| 79 | + continue; |
| 80 | + } |
| 81 | + if (*p == '*') { |
| 82 | + while (*++p) { |
| 83 | + if (*p == '*' && p[1] == '/') { |
| 84 | + p += 2; |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + continue; |
| 89 | + } |
| 90 | + break; |
| 91 | + case 'i': |
| 92 | + if (qts_match_identifier(p, "mport")) { |
| 93 | + *pp = p + 5; |
| 94 | + return QTS_TOK_IMPORT; |
| 95 | + } |
| 96 | + return QTS_TOK_IDENT; |
| 97 | + case 'e': |
| 98 | + if (qts_match_identifier(p, "xport")) |
| 99 | + return QTS_TOK_EXPORT; |
| 100 | + return QTS_TOK_IDENT; |
| 101 | + case '\\': |
| 102 | + if (*p == 'u') { |
| 103 | + if (lre_js_is_ident_first(lre_parse_escape(&p, true))) |
| 104 | + return QTS_TOK_IDENT; |
| 105 | + } |
| 106 | + break; |
| 107 | + default: |
| 108 | + if (c >= 128) { |
| 109 | + c = utf8_decode(p - 1, &p); |
| 110 | + } |
| 111 | + if (lre_is_space(c)) |
| 112 | + continue; |
| 113 | + if (lre_js_is_ident_first(c)) |
| 114 | + return QTS_TOK_IDENT; |
| 115 | + break; |
| 116 | + } |
| 117 | + return c; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +bool QTS_DetectModule(const char *input, size_t input_len) |
| 122 | +{ |
| 123 | + const uint8_t *p = (const uint8_t *)input; |
| 124 | + int tok; |
| 125 | + |
| 126 | + qts_skip_shebang(&p, p + input_len); |
| 127 | + switch(qts_simple_next_token(&p)) { |
| 128 | + case QTS_TOK_IMPORT: |
| 129 | + tok = qts_simple_next_token(&p); |
| 130 | + return (tok != '.' && tok != '('); |
| 131 | + case QTS_TOK_EXPORT: |
| 132 | + return true; |
| 133 | + default: |
| 134 | + return false; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | uintptr_t js_std_cmd(int cmd, ...) { |
| 139 | JSContext *ctx; |
| 140 | JSRuntime *rt; |
| 141 | |