Skip to content

Add support for Babel 7.12 reexports #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ EXPORT_STAR_LIB: `Object.keys(` IDENTIFIER$1 `).forEach(function (` IDENTIFIER$2
`if (` IDENTIFIER$2 `===` ( `'default'` | `"default"` ) `||` IDENTIFIER$2 `===` ( '__esModule' | `"__esModule"` ) `) return` `;`? |
`if (` IDENTIFIER$2 `!==` ( `'default'` | `"default"` ) `)`
)
(
`if (` IDENTIFIER$2 `in` EXPORTS_IDENTIFIER `&&` EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] ===` IDENTIFIER$1 `[` IDENTIFIER$2 `]) return` `;`?
)?
(
EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] =` IDENTIFIER$1 `[` IDENTIFIER$2 `]` `;`? |
`Object.defineProperty(` EXPORTS_IDENTIFIER `, ` IDENTIFIER$2 `, { enumerable: true, get: function () { return ` IDENTIFIER$1 `[` IDENTIFIER$2 `]` `;`? } })` `;`?
Expand Down Expand Up @@ -314,7 +317,7 @@ test/samples/*.js (3635 KiB)

To build download the WASI SDK from https://github.com/CraneStation/wasi-sdk/releases.

The Makefile assumes the existence of "wasi-sdk-10.0", "binaryen" and "wabt" (both optional) as sibling folders to this project.
The Makefile assumes the existence of "wasi-sdk-11.0", "binaryen" and "wabt" (both optional) as sibling folders to this project.

The build through the Makefile is then run via `make lib/lexer.wasm`, which can also be triggered via `npm run build-wasm` to create `dist/lexer.js`.

Expand Down
55 changes: 55 additions & 0 deletions lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,61 @@ function tryParseObjectDefineOrKeys (keys) {
}
else break;

// `if (` IDENTIFIER$2 `in` EXPORTS_IDENTIFIER `&&` EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] ===` IDENTIFIER$1 `[` IDENTIFIER$2 `]) return` `;`?
if (ch === 105/*i*/ && source.startsWith('f ', pos + 1)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I copied 'f ' from other parts of the code, but I think it could also be f(?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, yes we should probably allow the missing space.

pos += 3;
ch = commentWhitespace();
if (ch !== 40/*(*/) break;
pos++;
ch = commentWhitespace();
if (!source.startsWith(it_id, pos)) break;
pos += it_id.length;
ch = commentWhitespace();
if (ch !== 105/*i*/ || !source.startsWith('n ', pos + 1)) break;
pos += 3;
ch = commentWhitespace();
if (!readExportsOrModuleDotExports(ch)) break;
ch = commentWhitespace();
if (ch !== 38/*&*/ || source.charCodeAt(pos + 1) !== 38/*&*/) break;
pos += 2;
ch = commentWhitespace();
if (!readExportsOrModuleDotExports(ch)) break;
ch = commentWhitespace();
if (ch !== 91/*[*/) break;
pos++;
ch = commentWhitespace();
if (!source.startsWith(it_id, pos)) break;
pos += it_id.length;
ch = commentWhitespace();
if (ch !== 93/*]*/) break;
pos++;
ch = commentWhitespace();
if (ch !== 61/*=*/ || !source.startsWith('==', pos + 1)) break;
pos += 3;
ch = commentWhitespace();
if (!source.startsWith(id, pos)) break;
pos += id.length;
ch = commentWhitespace();
if (ch !== 91/*[*/) break;
pos++;
ch = commentWhitespace();
if (!source.startsWith(it_id, pos)) break;
pos += it_id.length;
ch = commentWhitespace();
if (ch !== 93/*]*/) break;
pos++;
ch = commentWhitespace();
if (ch !== 41/*)*/) break;
pos++;
ch = commentWhitespace();
if (ch !== 114/*r*/ || !source.startsWith('eturn', pos + 1)) break;
pos += 6;
ch = commentWhitespace();
if (ch === 59/*;*/)
pos++;
ch = commentWhitespace();
}

// EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] =` IDENTIFIER$1 `[` IDENTIFIER$2 `]`
if (readExportsOrModuleDotExports(ch)) {
ch = commentWhitespace();
Expand Down
Binary file modified lib/lexer.wasm
Binary file not shown.
56 changes: 56 additions & 0 deletions src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,62 @@ void tryParseObjectDefineOrKeys (bool keys) {
}
else break;


// `if (` IDENTIFIER$2 `in` EXPORTS_IDENTIFIER `&&` EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] ===` IDENTIFIER$1 `[` IDENTIFIER$2 `]) return` `;`?
if (ch == 'i' && str_eq2(pos + 1, 'f', ' ')) {
pos += 3;
ch = commentWhitespace();
if (ch != '(') break;
pos++;
ch = commentWhitespace();
if (memcmp(pos, it_id_start, it_id_len * sizeof(uint16_t)) != 0) break;
pos += it_id_len;
ch = commentWhitespace();
if (ch != 'i' || !str_eq2(pos + 1, 'n', ' ')) break;
pos += 3;
ch = commentWhitespace();
if (!readExportsOrModuleDotExports(ch)) break;
ch = commentWhitespace();
if (ch != '&' || *(pos + 1) != '&') break;
pos += 2;
ch = commentWhitespace();
if (!readExportsOrModuleDotExports(ch)) break;
ch = commentWhitespace();
if (ch != '[') break;
pos++;
ch = commentWhitespace();
if (memcmp(pos, it_id_start, it_id_len * sizeof(uint16_t)) != 0) break;
pos += it_id_len;
ch = commentWhitespace();
if (ch != ']') break;
pos++;
ch = commentWhitespace();
if (ch != '=' || !str_eq2(pos + 1, '=', '=')) break;
pos += 3;
ch = commentWhitespace();
if (memcmp(pos, id_start, id_len * sizeof(uint16_t)) != 0) break;
pos += id_len;
ch = commentWhitespace();
if (ch != '[') break;
pos++;
ch = commentWhitespace();
if (memcmp(pos, it_id_start, it_id_len * sizeof(uint16_t)) != 0) break;
pos += it_id_len;
ch = commentWhitespace();
if (ch != ']') break;
pos++;
ch = commentWhitespace();
if (ch != ')') break;
pos++;
ch = commentWhitespace();
if (ch != 'r' || !str_eq5(pos + 1, 'e', 't', 'u', 'r', 'n')) break;
pos += 6;
ch = commentWhitespace();
if (ch == ';')
pos++;
ch = commentWhitespace();
}

// EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] =` IDENTIFIER$1 `[` IDENTIFIER$2 `]`
if (readExportsOrModuleDotExports(ch)) {
ch = commentWhitespace();
Expand Down
41 changes: 34 additions & 7 deletions test/_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ suite('Lexer', () => {

var _external = require("external");

// Babel <7.12.0, loose mode
Object.keys(_external).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
exports[key] = _external[key];
});

var _external2 = require("external2");

// Babel <7.12.0
Object.keys(_external2).forEach(function (key) {
if (key === "default" || /*comment!*/ key === "__esModule") return;
Object.defineProperty(exports, key, {
Expand All @@ -63,10 +65,33 @@ suite('Lexer', () => {
}
});
});


var _external001 = require("external001");

// Babel >=7.12.0, loose mode
Object.keys(_external001).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _external001[key]) return;
exports[key] = _external001[key];
});

var _external002 = require("external002");

// Babel >=7.12.0, loose mode
Object.keys(_external002).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _external002[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _external002[key];
}
});
});

let external3 = require('external3');
const external4 = require('external4');

Object.keys(external3).forEach(function (k) {
if (k !== 'default') Object.defineProperty(exports, k, {
enumerable: true,
Expand Down Expand Up @@ -191,13 +216,15 @@ suite('Lexer', () => {
`);
assert.equal(exports.length, 1);
assert.equal(exports[0], '__esModule');
assert.equal(reexports.length, 6);
assert.equal(reexports.length, 8);
assert.equal(reexports[0], 'external');
assert.equal(reexports[1], 'external2');
assert.equal(reexports[2], 'external3');
assert.equal(reexports[3], 'external4');
assert.equal(reexports[4], 'external😃');
assert.equal(reexports[5], 'external𤭢');
assert.equal(reexports[2], 'external001');
assert.equal(reexports[3], 'external002');
assert.equal(reexports[4], 'external3');
assert.equal(reexports[5], 'external4');
assert.equal(reexports[6], 'external😃');
assert.equal(reexports[7], 'external𤭢');
});

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