NAK-569 Use industry best practices in wasm #13
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR compiles the C++ equations-parser to WebAssembly using Emscripten, wires it into a generalized JS wrapper, and adds a browser playground for interactive evaluation. It also introduces a modern project structure (ESM/CJS entries, types, tests, lint/format), aligning with the Parsec Web vision in AGENTS.md.
Context & Motivation
Changes
High-level:
wasm/equations_parser.js) for easy browser loading.js/equations_parser_wrapper.jsexposing a Parsec class withinitialize()andeval().Commits (latest first):
Files added/updated (relative to
main):.eslintrc.cjs,.prettierrc,.prettierignore.github/tests.yml,.github/workflows/tests.ymlAGENTS.md,CLAUDE.md(development guide).gitignore,README.md,build.shcpp/equations_parser_wrapper.cpphtml/equations-parser-test.htmlindex.cjs,index.mjs,types.d.tsjs/equations_parser_wrapper.jspackage.json,package-lock.json,vitest.config.jswasm/equations_parser.js(SINGLE_FILE JS with embedded WASM)cpp/math_functions.cpp,html/test.html,js/math_wrapper.js,test-phase1.sh,wasm/math_functions.js,docs/phase1-guide.mdImplementation Details
Emscripten build (see
build.sh):-s MODULARIZE=1,-s EXPORT_ES6=1,-s SINGLE_FILE=1,-s ENVIRONMENT=web,-O3,--bind,-s ALLOW_MEMORY_GROWTH=1.wasm/equations_parser.js— ES module exposingEquationsModulefactory. The.wasmbinary is embedded in the JS (no separate file).cpp/equations_parser_wrapper.cpp+ all files underequations-parser/parser/.JS wrapper (
js/equations_parser_wrapper.js):Parsecclass withinitialize()to load the WASM module andeval(expression)to evaluate a string expression.Package setup (
package.json):index.cjs(CommonJS),index.mjs(ESM), plustypes.d.ts.build,test,lint,format,style:fix,dev/serve.jsdomenvironment.How to Test
Automated (Vitest):
npm installnpm test— run all testsnpm run test:watch/npm run test:coverageas neededManual (Browser UI):
npm run dev(serves on http://localhost:8000)http://localhost:8000/html/equations-parser-test.html2 + 3 * 4and click “Evaluate” → expect14.sin(pi/2)→ expect1.concat("Hello", " ", "World")→ expect"Hello World".5 > 3 ? "yes" : "no"→ expect"yes".Expected:
Risks & Considerations
-s ENVIRONMENT=web). If Node runtime support is needed for the WASM module itself, we can extend toweb,worker,node.SINGLE_FILEoutput eases hosting but increases JS bundle size. If separate.wasmis preferred, we can remove-s SINGLE_FILE=1and adjust loader.Rollback Plan
Checklist
npm run buildnpm testnpm run style:fixhtml/equations-parser-test.html