Skip to content
Open
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
41 changes: 41 additions & 0 deletions .github/workflows/native.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Native

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
native:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 18.x
- run: cmake -S . -B build/native -DCMAKE_BUILD_TYPE=Debug
- run: cmake --build build/native --config Debug --parallel
- run: ctest --test-dir build/native -C Debug --output-on-failure
- run: cmake --install build/native --config Debug --prefix "${{ github.workspace }}/build/native-install"
- run: cmake -S test/native/consumer -B build/native-consumer -DCMAKE_PREFIX_PATH="${{ github.workspace }}/build/native-install"
- run: cmake --build build/native-consumer --config Debug --parallel
- run: ctest --test-dir build/native-consumer -C Debug --output-on-failure
- run: npm ci
- run: npm run build
- run: npm run test-native
sanitizers:
runs-on: ubuntu-latest
strategy:
matrix:
sanitizer: ['address,undefined', thread]
steps:
- uses: actions/checkout@v7
- run: cmake -S . -B build/native -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_FLAGS="-fsanitize=${{ matrix.sanitizer }} -fno-omit-frame-pointer" -DCMAKE_CXX_FLAGS="-fsanitize=${{ matrix.sanitizer }} -fno-omit-frame-pointer"
- run: cmake --build build/native --parallel
- run: ctest --test-dir build/native --output-on-failure
- if: matrix.sanitizer == 'address,undefined'
run: clang -std=c11 -g -Iinclude -fsanitize=fuzzer,address,undefined src/lexer.c src/native.c test/native/fuzz.c -o build/native/fuzz && build/native/fuzz -runs=100000
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
dist
yarn.lock
build/toolchain
build/native*
55 changes: 55 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
cmake_minimum_required(VERSION 3.15)
project(cjs_module_lexer LANGUAGES C)

if(CMAKE_C_COMPILER_ID STREQUAL "MSVC" AND
(CMAKE_VERSION VERSION_LESS 3.18.2 OR CMAKE_C_COMPILER_VERSION VERSION_LESS 19.28))
message(FATAL_ERROR "The native lexer requires CMake 3.18.2 and MSVC 19.28 or newer when building with MSVC.")
endif()

include(GNUInstallDirs)

add_library(cjs_module_lexer STATIC src/lexer.c src/native.c)
add_library(cjs_module_lexer::cjs_module_lexer ALIAS cjs_module_lexer)
target_compile_features(cjs_module_lexer PUBLIC c_std_11)
target_include_directories(cjs_module_lexer PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
if(MSVC)
target_compile_options(cjs_module_lexer PRIVATE /W4)
else()
target_compile_options(cjs_module_lexer PRIVATE -Wall -Wextra -Wno-parentheses)
endif()

install(TARGETS cjs_module_lexer EXPORT cjs_module_lexer_targets ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES include/cjs-module-lexer.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(EXPORT cjs_module_lexer_targets FILE cjs_module_lexer-config.cmake
NAMESPACE cjs_module_lexer:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cjs_module_lexer)

include(CTest)
if(BUILD_TESTING)
enable_language(CXX)
find_package(Threads REQUIRED)
add_executable(native_test test/native/test.c)
target_link_libraries(native_test PRIVATE cjs_module_lexer)
add_test(NAME native COMMAND native_test)
add_executable(native_parse test/native/parse.c)
target_link_libraries(native_parse PRIVATE cjs_module_lexer)
add_executable(native_threads test/native/threads.cc)
target_compile_features(native_threads PRIVATE cxx_std_11)
target_link_libraries(native_threads PRIVATE cjs_module_lexer Threads::Threads)
add_test(NAME native_threads COMMAND native_threads)
add_library(native_failure OBJECT src/lexer.c src/native.c)
target_include_directories(native_failure PRIVATE include)
target_compile_features(native_failure PRIVATE c_std_11)
target_compile_definitions(native_failure PRIVATE malloc=cjs_test_malloc realloc=cjs_test_realloc free=cjs_test_free)
add_executable(native_failures test/native/failures.c $<TARGET_OBJECTS:native_failure>)
target_include_directories(native_failures PRIVATE include)
add_test(NAME native_failures COMMAND native_failures)
foreach(target native_test native_threads native_failures)
if(MSVC)
target_compile_options(${target} PRIVATE /UNDEBUG)
else()
target_compile_options(${target} PRIVATE -UNDEBUG)
endif()
endforeach()
endif()
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ WASM_OPT ?= ../binaryen/bin/wasm-opt
lib/lexer.wat: lib/lexer.wasm
$(WASM2WAT) lib/lexer.wasm -o lib/lexer.wat

lib/lexer.wasm: include-wasm/cjs-module-lexer.h src/lexer.c | lib/
lib/lexer.wasm: include/cjs-module-lexer.h src/lexer-internal.h src/lexer.c src/wasm.c | lib/
node build/wasm.js --docker

lib/:
@mkdir -p $@

optimize: lib/lexer.wasm
$(WASM_OPT) -Oz --enable-bulk-memory --strip-debug lib/lexer.wasm -o lib/lexer.wasm
$(WASM_OPT) -Oz --strip-debug lib/lexer.wasm -o lib/lexer.wasm

clean:
$(RM) lib/*
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,65 @@ test/samples/*.js (3635 KiB)
> 27.76ms
```

## Native embedding

The source distribution provides a C11 static library with no dependencies beyond the C library.
JavaScript installation does not require a native compiler.
The native build requires CMake 3.15 or later. With MSVC, use CMake 3.18.2 or later,
Visual Studio 2019 version 16.8 or later, and Windows SDK 10.0.20348.0 or later.

```sh
cmake -S . -B build/native -DBUILD_TESTING=OFF
cmake --build build/native --parallel
cmake --install build/native --prefix /your/prefix
```

CMake consumers can use `find_package(cjs_module_lexer CONFIG REQUIRED)` and link
`cjs_module_lexer::cjs_module_lexer`. Other builds can compile `src/lexer.c` and `src/native.c` with `include/`
on the header search path. The interface does not promise a cross-version shared-library ABI.

```c
#include <cjs-module-lexer.h>

/* UTF-16 code units, without a terminator. */
const uint16_t source[] = {'e', 'x', 'p', 'o', 'r', 't', 's', '.', 'a', '=', '1'};
cjs_lexer_result* result;
cjs_lexer_error error;
uint32_t status = cjs_lexer_parse_utf16(source, sizeof(source) / sizeof(source[0]), 0, &result, &error);
if (status == CJS_LEXER_OK) {
for (size_t index = 0; index < cjs_lexer_export_count(result); index++) {
cjs_lexer_string16 name = cjs_lexer_export(result, index);
/* Consume name.data[0..name.length). */
}
cjs_lexer_result_free(result);
}
```

Lengths and error offsets count UTF-16 code units. Null source is valid with zero length.
The flags `CJS_LEXER_ALLOW_MINIFIED_ENUMERABLE` and `CJS_LEXER_EXTENDED` select the corresponding JavaScript options.
The default remains conservative detection. The lexer does not validate JavaScript syntax.

Each call creates private scanner state. Calls can run concurrently, and independently owned results can coexist.
The result owns metadata and decoded strings. Names that need no decoding borrow the source.
Keep the source unchanged and alive until `cjs_lexer_result_free(result)`.
Read-only result access can run concurrently, with release synchronized by the caller.
Indexed access requires an index below the corresponding count. `cjs_lexer_result_free(NULL)` is valid.

The C interface returns decoded candidates after getter filtering and reexport replacement.
Candidates retain source order and can contain duplicates. JavaScript wrappers retain their unique arrays.
String rejection, getter filtering, and detection modes match the Wasm package.

The status distinguishes invalid arguments, lexical failure, limits, and allocation failure.
Every failure sets the result to null and initializes the optional error output.
`error.has_offset` signals that `error.offset` gives a source location.
`error.code` is a `CJS_LEXER_ERROR_*` constant from the header, or `CJS_LEXER_ERROR_NONE` if there is no scanner error.
`CJS_LEXER_ERROR_UNEXPECTED_IMPORT_META`, `CJS_LEXER_ERROR_UNEXPECTED_ESM_IMPORT`, and
`CJS_LEXER_ERROR_UNEXPECTED_ESM_EXPORT` identify ESM syntax. Nesting is limited to 2048 tracked brackets or templates.
Reaching the end of input while scanning template text reports `CJS_LEXER_ERROR_UNTERMINATED_TEMPLATE`.
Diagnostic code `4` is reserved.
Input lengths must fit `uint32_t`, `PTRDIFF_MAX - 32`, and the platform's UTF-16 allocation range.
Output locations must not overlap each other or the source.

### Wasm Build Steps

The build uses docker and make, they must be installed first.
Expand Down
11 changes: 4 additions & 7 deletions build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ WASM_LDFLAGS := -nostartfiles

# These are project-specific and are expected to be kept intact
WASM_TARGET := -target wasm32-unknown-wasi
WASM_EXTRA_CFLAGS := -I include-wasm/ -Wno-logical-op-parentheses -Wno-parentheses -Oz
WASM_EXTRA_CFLAGS := -I include/ -Wno-logical-op-parentheses -Wno-parentheses -Oz
WASM_EXTRA_CFLAGS += -flto -mllvm -inline-threshold=150
WASM_EXTRA_LDFLAGS := -Wl,-z,stack-size=13312,--no-entry,--compress-relocations,--strip-all
WASM_EXTRA_LDFLAGS += -Wl,--mllvm=-inline-threshold=150
WASM_EXTRA_LDFLAGS += -Wl,--export=__heap_base,--export=parseCJS,--export=sa
WASM_EXTRA_LDFLAGS += -Wl,--export=parseCJSWithOptions
WASM_EXTRA_LDFLAGS += -Wl,--export=e,--export=re,--export=es,--export=ee
WASM_EXTRA_LDFLAGS += -Wl,--export=rre,--export=ree,--export=res,--export=ru,--export=us,--export=ue
WASM_EXTRA_LDFLAGS += -Wl,--export=sa,--export=es,--export=ee,--export=re,--export=res,--export=ree,--export=rre,--export=us,--export=ue,--export=ru,--export=e,--export=parseCJS,--export=parseCJSWithOptions,--export=__heap_base

lib/lexer.wasm: include-wasm/cjs-module-lexer.h src/lexer.c
lib/lexer.wasm: include/cjs-module-lexer.h src/lexer-internal.h src/lexer.c src/wasm.c
@mkdir -p lib
$(WASM_CC) $(WASM_CFLAGS) $(WASM_TARGET) $(WASM_EXTRA_CFLAGS) \
src/lexer.c -o lib/lexer.wasm \
src/wasm.c -o lib/lexer.wasm \
$(WASM_LDFLAGS) $(WASM_EXTRA_LDFLAGS)

optimize: lib/lexer.wasm
Expand Down
2 changes: 1 addition & 1 deletion build/wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if (process.argv[2] === '--docker') {
--mount type=bind,source=${ROOT}/build/Makefile,target=/home/node/build/Makefile \
--mount type=bind,source=${ROOT}/build.js,target=/home/node/build/build.js \
--mount type=bind,source=${ROOT}/package.json,target=/home/node/build/package.json \
--mount type=bind,source=${ROOT}/include-wasm,target=/home/node/build/include-wasm \
--mount type=bind,source=${ROOT}/include,target=/home/node/build/include \
-t ${WASM_BUILDER_CONTAINER} node wasm.js`
console.log(`> ${cmd}\n\n`)
execSync(cmd, { stdio: 'inherit' })
Expand Down
Loading
Loading