tpwatson
raw JSON → 0.1.3 verified Fri May 01 auth: no javascript
A prototype transpiler for the WAT# language, compiling a lightweight high-level syntax to WebAssembly Text Format (WAT). Current stable version 0.1.3 (released 2024). The package is in early development with frequent bug fixes. Unlike raw WAT coding, WAT# adds familiar types (bool, i8, u8, i16, u16, i32, u32, i64, u64, f32, f64), compound types (arrays, structs, pointers), control flow (if/else, while, for, do-while), expression evaluation, inline functions, and preprocessor directives (#include, #if, #define). It aims to simplify WebAssembly module creation without requiring a runtime.
Common errors
error TypeError: tpwatson is not a function ↓
cause Using CommonJS require on an ESM-only package.
fix
Use import instead:
import tpwatson from 'tpwatson' error SyntaxError: Unexpected token ':' ↓
cause WAT# uses TypeScript-like type annotations but JavaScript does not support them natively.
fix
Ensure the WAT# code is passed as a string to the transpile function, not executed directly.
error Error: #include not found: mylib.wat# ↓
cause The #include directive cannot resolve the file path.
fix
Use an absolute path or ensure the file is relative to the working directory.
Warnings
breaking The package is in early prototype stage. APIs may change without notice. Do not use in production. ↓
fix Pin to a specific version and test thoroughly on upgrade.
gotcha Transpiler does not generate a complete WebAssembly module; you may need to wrap output in (module ... ) manually. ↓
fix Wrap output in '(module ...)' or wait for future versions.
deprecated The 'bool' type is not a standard WebAssembly type and may be removed in future versions. ↓
fix Use i32 with 0/1 values instead.
gotcha Preprocessor directives (#include) currently only support local file paths; remote includes are not implemented. ↓
fix Ensure all includes are relative to the source file.
gotcha The 'inline' keyword is parsed but not yet functional; inline functions are treated as regular functions. ↓
fix Avoid relying on inlining behavior until implemented.
Install
npm install tpwatson yarn add tpwatson pnpm add tpwatson Imports
- transpile wrong
const tpwatson = require('tpwatson'); tpwatson.transpile()correctimport { transpile } from 'tpwatson' - default (transpile function) wrong
const transpile = require('tpwatson').defaultcorrectimport transpile from 'tpwatson' - version wrong
require('tpwatson/package.json').versioncorrectimport { version } from 'tpwatson'
Quickstart
import { transpile } from 'tpwatson';
const watSharpCode = `
// WAT# example: add two numbers
function add(a: i32, b: i32): i32 {
return a + b;
}
`;
try {
const watOutput = transpile(watSharpCode);
console.log('Generated WAT:\n', watOutput);
} catch (error) {
console.error('Transpilation failed:', error);
}