8000 module: use Wasm CJS lexer when available · nodejs/node@354f358 · GitHub
[go: up one dir, main page]

Skip to content

Commit 354f358

Browse files
guybedfordMylesBorins
authored andcommitted
module: use Wasm CJS lexer when available
PR-URL: #35583 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
1 parent a93ca2d commit 354f358

File tree

7 files changed

+70
-2
lines changed
  • test
  • 7 files changed

    +70
    -2
    lines changed

    benchmark/esm/cjs-parse.js

    Lines changed: 40 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,40 @@
    1+
    'use strict';
    2+
    const fs = require('fs');
    3+
    const path = require('path');
    4+
    const common = require('../common.js');
    5+
    const { strictEqual } = require('assert');
    6+
    7+
    const tmpdir = require('../../test/common/tmpdir');
    8+
    const benchmarkDirectory =
    9+
    path.resolve(tmpdir.path, 'benchmark-esm-parse');
    10+
    11+
    const bench = common.createBenchmark(main, {
    12+
    n: [1e2]
    13+
    });
    14+
    15+
    async function main({ n }) {
    16+
    tmpdir.refresh();
    17+
    18+
    fs.mkdirSync(benchmarkDirectory);
    19+
    20+
    let sampleSource = 'try {\n';
    21+
    for (let i = 0; i < 1000; i++) {
    22+
    sampleSource += 'sample.js(() => file = /test/);\n';
    23+
    }
    24+
    sampleSource += '} catch {}\nexports.p = 5;\n';
    25+
    26+
    for (let i = 0; i < n; i++) {
    8000 27+
    const sampleFile = path.join(benchmarkDirectory, `sample${i}.js`);
    28+
    fs.writeFileSync(sampleFile, sampleSource);
    29+
    }
    30+
    31+
    bench.start();
    32+
    for (let i = 0; i < n; i++) {
    33+
    const sampleFile = path.join(benchmarkDirectory, `sample${i}.js`);
    34+
    const m = await import('file:' + sampleFile);
    35+
    strictEqual(m.p, 5);
    36+
    }
    37+
    bench.end(n);
    38+
    39+
    tmpdir.refresh();
    40+
    }

    deps/cjs-module-lexer/dist/lexer.js

    Lines changed: 1 addition & 0 deletions
    Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

    deps/cjs-module-lexer/dist/lexer.mjs

    Lines changed: 2 additions & 0 deletions
    Large diffs are not rendered by default.

    lib/internal/modules/esm/translators.js

    Lines changed: 19 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -56,7 +56,24 @@ const { getOptionValue } = require('internal/options');
    5656
    const experimentalImportMetaResolve =
    5757
    getOptionValue('--experimental-import-meta-resolve');
    5858
    const asyncESM = require('internal/process/esm_loader');
    59-
    const cjsParse = require('internal/deps/cjs-module-lexer/lexer');
    59+
    60+
    let cjsParse;
    61+
    async function initCJSParse() {
    62+
    if (typeof WebAssembly !== 'undefined') {
    63+
    const { parse, init } =
    64+
    require('internal/deps/cjs-module-lexer/dist/lexer');
    65+
    await init();
    66+
    let exports;
    67+
    try {
    68+
    ({ exports } = parse('exports.a=1'));
    69+
    if (exports.length === 1) {
    70+
    cjsParse = parse;
    71+
    return;
    72+
    }
    73+
    } catch {}
    74+
    }
    75+
    cjsParse = require('internal/deps/cjs-module-lexer/lexer');
    76+
    }
    6077

    6178
    const translators = new SafeMap();
    6279
    exports.translators = translators;
    @@ -167,6 +184,7 @@ translators.set('commonjs', async function commonjsStrategy(url, isMain) {
    167184
    if (isWindows)
    168185
    filename = StringPrototypeReplace(filename, winSepRegEx, '\\');
    169186

    187+
    if (!cjsParse) await initCJSParse();
    170188
    const { module, exportNames } = cjsPreparseModuleExports(filename);
    171189
    const namesWithDefault = exportNames.has('default') ?
    172190
    [...exportNames] : ['default', ...exportNames];

    node.gyp

    Lines changed: 1 addition & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -250,6 +250,7 @@
    250250
    'deps/acorn-plugins/acorn-private-methods/index.js',
    251251
    'deps/acorn-plugins/acorn-static-class-features/index.js',
    252252
    'deps/cjs-module-lexer/lexer.js',
    253+
    'deps/cjs-module-lexer/dist/lexer.js',
    253254
    ],
    254255
    'node_mksnapshot_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)node_mksnapshot<(EXECUTABLE_SUFFIX)',
    255256
    'mkcodecache_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)mkcodecache<(EXECUTABLE_SUFFIX)',
    Lines changed: 7 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,7 @@
    1+
    'use strict';
    2+
    3+
    require('../common');
    4+
    5+
    const runBenchmark = require('../common/benchmark');
    6+
    7+
    runBenchmark('esm', { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });

    test/parallel/test-bootstrap-modules.js

    Lines changed: 0 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -54,7 +54,6 @@ const expectedModules = new Set([
    5454
    'NativeModule internal/modules/cjs/helpers',
    5555
    'NativeModule internal/modules/cjs/loader',
    5656
    'NativeModule internal/modules/esm/create_dynamic_module',
    57-
    'NativeModule internal/deps/cjs-module-lexer/lexer',
    5857
    'NativeModule internal/modules/esm/get_format',
    5958
    'NativeModule internal/modules/esm/get_source',
    6059
    'NativeModule internal/modules/esm/loader',

    0 commit comments

    Comments
     (0)
    0