Skip to content

feat: add a portable native C interface - #140

Open
BridgeAR wants to merge 10 commits into
nodejs:mainfrom
BridgeAR:BridgeAR/2026-10-27-cjs-native
Open

BridgeAR wants to merge 10 commits into
nodejs:mainfrom
BridgeAR:BridgeAR/2026-10-27-cjs-native

Conversation

@BridgeAR

Copy link
Copy Markdown
Member

Native embedders need independent parses over exact UTF-16 buffers without a WebAssembly runtime. Results retain their own metadata and decoded strings, with explicit source lifetimes and per-call failures.

The native and Wasm builds share the scanner. Wasm keeps its slice interface and JavaScript decoder: the artifact shrinks from 28,959 to 28,832 bytes. Warmed CPU measurements on Node.js 22 reduce angular.min.js parsing from 0.795 to 0.729 ms across five fresh processes.

@guybedford guybedford left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, this is well tested — native, sanitizer and fuzz all clean locally, and lexer.wasm rebuilds byte-identical.

Two of the fixes here affect every backend and should land as their own PRs first, so they can be reviewed and released independently:

  • Closing tokens consumed by export helpers reuse stale brace metadata — class A{};module.exports={a}/1;exports.b=2; throws a regex syntax error on main.
  • The surrogate check lacks an upper bound, so names in U+E000–U+FFFF are dropped.

On structure, I'd like both the Wasm and native builds to be thin wrappers around a shared core. cjs_lexer_add / cjs_lexer_allocate already split this way; the remaining coupling is the CJS_LEXER_WASM branches in lexer-internal.h. Candidates / cjs_lexer_result could move into wasm.c and native.c respectively. If an index Cursor in Wasm doesn't measurably cost anything, that would remove the rest of the ifdefs from the core too.

Comment thread include/cjs-module-lexer.h Resolved
Comment thread CMakeLists.txt Resolved
BridgeAR added a commit to BridgeAR/cjs-module-lexer that referenced this pull request Nov 19, 2026
Keep storage out of the scanner without imposing an index-cursor cost. On Node.js 22, padded indices cost 7–9% on Angular and export-heavy inputs across two runs of seven alternating trials.

Name native syntax codes without changing their values. Keep the offset of the first diagnostic when a malformed regex reports a second error.

Use the configured header directory in the installed CMake target so consumers can find the header after a custom installation.

Refs: nodejs#140 (review)
Signed-off-by: Ruben Bridgewater <ruben.bridgewater@datadoghq.com>

@guybedford guybedford left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the updates. The storage split, named error codes and CMake changes all look good, and the cursor measurements settle the index question. Tests, sanitizers and fuzzing are clean locally, and lexer.wasm rebuilds byte-identical.

A few optional nits inline.

Comment thread include/cjs-module-lexer.h Resolved
Comment thread include/cjs-module-lexer.h Resolved
Comment thread test/native/test.c Resolved
Native embedders need independent parses over exact UTF-16 buffers without a
WebAssembly runtime. Results retain their own metadata and decoded strings,
with explicit source lifetimes and per-call failures.

The native and Wasm builds share the scanner. Wasm keeps its slice interface
and JavaScript decoder: the artifact shrinks from 28,959 to 28,832 bytes, and
bundle parsing improves by 3.6–7.3% in warmed CPU measurements on Node.js 22.
Quoted names above U+DFFF were classified as unpaired surrogates because
validation lacked the surrogate range's upper bound. This discarded valid
BMP exports and reexports in all backends.
Token-run entry repeats a bounds check and source read for the character
already loaded by the main loop. Seven alternating macOS arm64 processes
reduce warmed d3.min.js parsing from 0.569 to 0.542 ms
(Node.js 27.0.0-pre, V8 15.6.75.25-node.10).
MSVC C headers do not define max_align_t, so native builds fail on Windows.
The arena needs alignment only for UTF-16 text and reexport bindings.
The corpus reader must preserve their raw source bytes on Windows.
The shared scanner uses the source start as a missing-name marker. That
pointer is nonzero in Wasm, so empty objects and trailing commas add an
empty export in extended mode.
String scanning checks the input length after the loop has already bounded
the cursor. Seven alternating process trials on macOS arm64 reduce warmed
o200k_base.cjs parsing by 19.6% (tiktoken 1.0.22) and full-versions.js parsing
by 24.0% (electron-to-chromium 1.5.417), on Node 27.0.0-pre with
V8 15.6.75.25-node.10.
The existing loop and keyword guards cover each replaced read. Keep checked
access for lookahead and sentinel scans that do not have that range proof.

On Apple M4 Max with Clang 21, seven interleaved C API processes per file
reduce geometric mean parse CPU time by 2.7% across 155 files.

Seven focused Node 27 process trials with V8 15.6.75.25-node.10 reduce D3
minified parse time by 4.3% and Angular by 3.5%. Magic-string minified takes
3.6% more time, so this does not improve every input.
Keep storage out of the scanner without imposing an index-cursor cost. On Node.js 22, padded indices cost 7–9% on Angular and export-heavy inputs across two runs of seven alternating trials.

Name native syntax codes without changing their values. Keep the offset of the first diagnostic when a malformed regex reports a second error.

Use the configured header directory in the installed CMake target so consumers can find the header after a custom installation.

Refs: nodejs#140 (review)
Signed-off-by: Ruben Bridgewater <ruben.bridgewater@datadoghq.com>
EOF in template text previously reported an unbalanced delimiter. Give it a separate diagnostic without renumbering the existing codes. The nesting limit makes the old template-depth check unreachable, so leave its code reserved.

Pin the first regex diagnostic offset across backends so a later scanner error cannot replace it.

Refs: nodejs#140
CMake 3.15 can build the lexer with GCC and Clang, but cannot select C11 with MSVC. Diagnose that unsupported combination at configure time without raising the minimum for other compilers.

Refs: nodejs#140
@BridgeAR
BridgeAR force-pushed the BridgeAR/2026-10-27-cjs-native branch from 3132996 to 6a0fa4d Compare November 23, 2026 11:50
@BridgeAR
BridgeAR marked this pull request as ready for review November 23, 2026 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants