Skip to content

Commit

Permalink
add support for interopRequireWildcard helper (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Mar 30, 2021
1 parent bd64f95 commit ec1c135
Show file tree
Hide file tree
Showing 8 changed files with 1,061 additions and 829 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ EXPORTS_LITERAL: MODULE_EXPORTS `=` `{` (EXPORTS_LITERAL_PROP | EXPORTS_SPREAD)
REQUIRE: `require` `(` STRING_LITERAL `)`
EXPORTS_ASSIGN: (`var` | `const` | `let`) IDENTIFIER `=` REQUIRE
EXPORTS_ASSIGN: (`var` | `const` | `let`) IDENTIFIER `=` (`_interopRequireWildcard (`)? REQUIRE
MODULE_EXPORTS_ASSIGN: MODULE_EXPORTS `=` REQUIRE
Expand Down
1 change: 1 addition & 0 deletions include-wasm/cjs-module-lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ bool str_eq9 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4,
bool str_eq10 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9, uint16_t c10);
bool str_eq13 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9, uint16_t c10, uint16_t c11, uint16_t c12, uint16_t c13);
bool str_eq18 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9, uint16_t c10, uint16_t c11, uint16_t c12, uint16_t c13, uint16_t c14, uint16_t c15, uint16_t c16, uint16_t c17, uint16_t c18);
bool str_eq22 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9, uint16_t c10, uint16_t c11, uint16_t c12, uint16_t c13, uint16_t c14, uint16_t c15, uint16_t c16, uint16_t c17, uint16_t c18, uint16_t c19, uint16_t c20, uint16_t c21, uint16_t c22);

bool readPrecedingKeyword2(uint16_t* pos, uint16_t c1, uint16_t c2);
bool readPrecedingKeyword3(uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3);
Expand Down
1 change: 1 addition & 0 deletions include/cjs-module-lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ bool str_eq9 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4,
bool str_eq10 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9, uint16_t c10);
bool str_eq13 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9, uint16_t c10, uint16_t c11, uint16_t c12, uint16_t c13);
bool str_eq18 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9, uint16_t c10, uint16_t c11, uint16_t c12, uint16_t c13, uint16_t c14, uint16_t c15, uint16_t c16, uint16_t c17, uint16_t c18);
bool str_eq22 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9, uint16_t c10, uint16_t c11, uint16_t c12, uint16_t c13, uint16_t c14, uint16_t c15, uint16_t c16, uint16_t c17, uint16_t c18, uint16_t c19, uint16_t c20, uint16_t c21, uint16_t c22);

bool readPrecedingKeyword2(uint16_t* pos, uint16_t c1, uint16_t c2);
bool readPrecedingKeyword3(uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3);
Expand Down
13 changes: 12 additions & 1 deletion lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,18 @@ function parseSource (cjsSource) {
lastTokenPos = pos;
continue;
case 95/*_*/:
if (source.startsWith('_export', pos + 1) && (keywordStart(pos) || source.charCodeAt(pos - 1) === 46/*.*/)) {
if (source.startsWith('interopRequireWildcard', pos + 1) && (keywordStart(pos) || source.charCodeAt(pos - 1) === 46/*.*/)) {
const startPos = pos;
pos += 23;
if (source.charCodeAt(pos) === 40/*(*/) {
pos++;
openTokenPosStack[openTokenDepth++] = lastTokenPos;
if (tryParseRequire(Import) && keywordStart(startPos)) {
tryBacktrackAddStarExportBinding(startPos - 1);
}
}
}
else if (source.startsWith('_export', pos + 1) && (keywordStart(pos) || source.charCodeAt(pos - 1) === 46/*.*/)) {
pos += 8;
if (source.startsWith('Star', pos))
pos += 4;
Expand Down
Binary file modified lib/lexer.wasm
Binary file not shown.
1,842 changes: 1,017 additions & 825 deletions lib/lexer.wat

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,17 @@ bool parseCJS (uint16_t* _source, uint32_t _sourceLen, void (*_addExport)(const
continue;
}
case '_':
if (str_eq7(pos + 1, '_', 'e', 'x', 'p', 'o', 'r', 't') && (keywordStart(pos) || *(pos - 1) == '.')) {
if (str_eq22(pos + 1, 'i', 'n', 't', 'e', 'r', 'o', 'p', 'R', 'e', 'q', 'u', 'i', 'r', 'e', 'W', 'i', 'l', 'd', 'c', 'a', 'r', 'd') && (keywordStart(pos) || *(pos - 1) == '.')) {
uint16_t* startPos = pos;
pos += 23;
if (*pos == '(') {
pos++;
openTokenPosStack[openTokenDepth++] = lastTokenPos;
if (tryParseRequire(Import) && keywordStart(startPos))
tryBacktrackAddStarExportBinding(startPos - 1);
}
}
else if (str_eq7(pos + 1, '_', 'e', 'x', 'p', 'o', 'r', 't') && (keywordStart(pos) || *(pos - 1) == '.')) {
pos += 8;
if (str_eq4(pos, 'S', 't', 'a', 'r'))
pos += 4;
Expand Down Expand Up @@ -1386,6 +1396,10 @@ bool str_eq18 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4
return *pos == c1 && *(pos + 1) == c2 && *(pos + 2) == c3 && *(pos + 3) == c4 && *(pos + 4) == c5 && *(pos + 5) == c6 && *(pos + 6) == c7 && *(pos + 7) == c8 && *(pos + 8) == c9 && *(pos + 9) == c10 && *(pos + 10) == c11 && *(pos + 11) == c12 && *(pos + 12) == c13 && *(pos + 13) == c14 && *(pos + 14) == c15 && *(pos + 15) == c16 && *(pos + 16) == c17 && *(pos + 17) == c18;
}

bool str_eq22 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9, uint16_t c10, uint16_t c11, uint16_t c12, uint16_t c13, uint16_t c14, uint16_t c15, uint16_t c16, uint16_t c17, uint16_t c18, uint16_t c19, uint16_t c20, uint16_t c21, uint16_t c22) {
return *pos == c1 && *(pos + 1) == c2 && *(pos + 2) == c3 && *(pos + 3) == c4 && *(pos + 4) == c5 && *(pos + 5) == c6 && *(pos + 6) == c7 && *(pos + 7) == c8 && *(pos + 8) == c9 && *(pos + 9) == c10 && *(pos + 10) == c11 && *(pos + 11) == c12 && *(pos + 12) == c13 && *(pos + 13) == c14 && *(pos + 14) == c15 && *(pos + 15) == c16 && *(pos + 16) == c17 && *(pos + 17) == c18 && *(pos + 18) == c19 && *(pos + 19) == c20 && *(pos + 20) == c21 && *(pos + 21) == c22;
}

bool keywordStart (uint16_t* pos) {
return pos == source || isBrOrWsOrPunctuatorNotDot(*(pos - 1));
}
Expand Down
15 changes: 14 additions & 1 deletion test/_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,22 @@ suite('Lexer', () => {
}
});
});
var _Accordion = _interopRequireWildcard(require("./Accordion"));
Object.keys(_Accordion).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _Accordion[key];
}
});
});
`);
assert.equal(exports.length, 1);
assert.equal(exports[0], '__esModule');
assert.equal(reexports.length, 14);
assert.equal(reexports.length, 15);
assert.equal(reexports[0], 'external');
assert.equal(reexports[1], 'external2');
assert.equal(reexports[2], 'external001');
Expand All @@ -377,6 +389,7 @@ suite('Lexer', () => {
assert.equal(reexports[11], 'external𤭢');
assert.equal(reexports[12], './styles');
assert.equal(reexports[13], './styles2');
assert.equal(reexports[14], './Accordion');
});

test('invalid exports cases', () => {
Expand Down

0 comments on commit ec1c135

Please sign in to comment.