jz 0.5.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/README.md +315 -318
  2. package/bench/README.md +369 -0
  3. package/bench/bench.svg +102 -0
  4. package/cli.js +104 -30
  5. package/dist/interop.js +1 -0
  6. package/dist/jz.js +6024 -0
  7. package/index.d.ts +126 -0
  8. package/index.js +362 -84
  9. package/interop.js +159 -186
  10. package/jz.svg +5 -0
  11. package/jzify/arguments.js +97 -0
  12. package/jzify/bundler.js +382 -0
  13. package/jzify/classes.js +328 -0
  14. package/jzify/hoist-vars.js +177 -0
  15. package/jzify/index.js +51 -0
  16. package/jzify/names.js +37 -0
  17. package/jzify/switch.js +106 -0
  18. package/jzify/transform.js +349 -0
  19. package/layout.js +184 -0
  20. package/module/array.js +377 -176
  21. package/module/collection.js +639 -145
  22. package/module/console.js +55 -43
  23. package/module/core.js +264 -153
  24. package/module/date.js +15 -3
  25. package/module/function.js +73 -6
  26. package/module/index.js +2 -1
  27. package/module/json.js +226 -61
  28. package/module/math.js +551 -187
  29. package/module/number.js +327 -60
  30. package/module/object.js +474 -184
  31. package/module/regex.js +255 -25
  32. package/module/schema.js +24 -6
  33. package/module/simd.js +117 -0
  34. package/module/string.js +621 -226
  35. package/module/symbol.js +1 -1
  36. package/module/timer.js +9 -14
  37. package/module/typedarray.js +459 -73
  38. package/package.json +58 -14
  39. package/src/abi/index.js +39 -23
  40. package/src/abi/string.js +38 -41
  41. package/src/ast.js +460 -0
  42. package/src/autoload.js +29 -24
  43. package/src/bridge.js +111 -0
  44. package/src/compile/analyze-scans.js +824 -0
  45. package/src/compile/analyze.js +1600 -0
  46. package/src/compile/emit-assign.js +411 -0
  47. package/src/compile/emit.js +3512 -0
  48. package/src/compile/flow-types.js +103 -0
  49. package/src/{compile.js → compile/index.js} +778 -128
  50. package/src/{infer.js → compile/infer.js} +62 -98
  51. package/src/compile/loop-divmod.js +155 -0
  52. package/src/{narrow.js → compile/narrow.js} +409 -106
  53. package/src/compile/peel-stencil.js +261 -0
  54. package/src/compile/plan/advise.js +370 -0
  55. package/src/compile/plan/common.js +150 -0
  56. package/src/compile/plan/index.js +122 -0
  57. package/src/compile/plan/inline.js +682 -0
  58. package/src/compile/plan/literals.js +1199 -0
  59. package/src/compile/plan/loops.js +472 -0
  60. package/src/compile/plan/scope.js +649 -0
  61. package/src/compile/program-facts.js +423 -0
  62. package/src/ctx.js +220 -64
  63. package/src/ir.js +589 -172
  64. package/src/kind-traits.js +132 -0
  65. package/src/kind.js +524 -0
  66. package/src/op-policy.js +57 -0
  67. package/src/{optimize.js → optimize/index.js} +1432 -473
  68. package/src/optimize/vectorize.js +4660 -0
  69. package/src/param-reps.js +65 -0
  70. package/src/parse.js +44 -0
  71. package/src/{prepare.js → prepare/index.js} +649 -205
  72. package/src/prepare/lift-iife.js +149 -0
  73. package/src/reps.js +116 -0
  74. package/src/resolve.js +12 -3
  75. package/src/static.js +208 -0
  76. package/src/type.js +651 -0
  77. package/src/{assemble.js → wat/assemble.js} +275 -55
  78. package/src/wat/optimize.js +3938 -0
  79. package/transform.js +21 -0
  80. package/wasi.js +47 -5
  81. package/src/analyze.js +0 -3818
  82. package/src/emit.js +0 -3040
  83. package/src/jzify.js +0 -1580
  84. package/src/plan.js +0 -2132
  85. package/src/vectorize.js +0 -1088
  86. /package/src/{codegen.js → wat/codegen.js} +0 -0
package/index.d.ts ADDED
@@ -0,0 +1,126 @@
1
+ // Type declarations for jz — minimal functional JS subset compiling to WASM.
2
+ // The public surface is small: `jz` (compile + instantiate), `compile` (raw bytes),
3
+ // `compileModule` (compile once), `instantiate` (run a module), and `jz.memory`.
4
+ // See README for semantics and the JS↔WASM value ABI.
5
+
6
+ /** Optimization level / preset. `2` is the default (all stable passes). */
7
+ export type OptimizeLevel = boolean | 0 | 1 | 2 | 3 | 'speed' | 'size'
8
+
9
+ /** Runtime-service lowering target. */
10
+ export type Host = 'js' | 'wasi'
11
+
12
+ /** Value injectable as a compile-time `define` constant. */
13
+ export type DefineValue = number | boolean | string | null | DefineValue[] | { [k: string]: DefineValue }
14
+
15
+ export interface CompileOptions {
16
+ /** Static ES imports to bundle: `{ './dep.js': 'export let x = 1' }`. */
17
+ modules?: Record<string, string>
18
+ /** Host imports wired at runtime: `{ math: Math, host: { log: console.log } }`. */
19
+ imports?: Record<string, unknown>
20
+ /** `N` initial pages of owned memory, or a shared `jz.memory()` / `WebAssembly.Memory`. */
21
+ memory?: number | WebAssembly.Memory | JzMemory
22
+ /** Cap memory growth at this many 64 KiB pages (default: unbounded). */
23
+ maxMemory?: number
24
+ /** Import `env.memory` instead of exporting own memory. */
25
+ importMemory?: boolean
26
+ /** Runtime-service lowering. Default `'js'`. */
27
+ host?: Host
28
+ /** Optimization level or named preset. Default `2`. */
29
+ optimize?: OptimizeLevel
30
+ /** Compile-time constants injected as top-level bindings. */
31
+ define?: Record<string, DefineValue>
32
+ /** Enforce the pure canonical subset: skip jzify lowering and reject dynamic fallbacks. */
33
+ strict?: boolean
34
+ /** Omit `_alloc`/`_clear` allocator exports for standalone scalar modules. */
35
+ alloc?: boolean
36
+ /** Disable auto-vectorization (no jz-emitted `v128`). Explicit intrinsics still compile. */
37
+ noSimd?: boolean
38
+ /** Use ordinary call frames instead of `return_call` tail calls. */
39
+ tailCall?: boolean
40
+ /** `Math.random` seeding: a number fixes the stream; `true` forces host entropy. */
41
+ randomSeed?: number | boolean
42
+ /** Emit a WASM `name` section (function symbols) for profilers/debuggers. */
43
+ names?: boolean
44
+ /** `compile()` returns WAT text instead of a WASM binary. */
45
+ wat?: boolean
46
+ /** Resolve bare specifiers via Node.js module resolution (CLI/build use). */
47
+ resolve?: boolean
48
+ /** Mutable sink that collects per-stage compile timings. */
49
+ profile?: { entries?: unknown[]; totals?: Record<string, number> } & Record<string, unknown>
50
+ }
51
+
52
+ /**
53
+ * An enhanced `WebAssembly.Memory` that marshals JS values across the WASM boundary.
54
+ * Allocators (`String`/`Array`/`Object`/typed-array ctors) return NaN-boxed `f64`
55
+ * pointers; `read` decodes one back. The heap never frees implicitly — call `reset`
56
+ * between independent batches (all previously returned pointers become invalid).
57
+ */
58
+ export interface JzMemory extends WebAssembly.Memory {
59
+ /** Allocate a UTF-8 string; returns a pointer. */
60
+ String(str: string): number
61
+ /** Allocate an array (numbers, strings, nested arrays/objects); returns a pointer. */
62
+ Array(data: ArrayLike<unknown>): number
63
+ /** Allocate a fixed-layout object (keys must match a compiled schema); returns a pointer. */
64
+ Object(obj: Record<string, unknown>): number
65
+ Float64Array(data: ArrayLike<number>): number
66
+ Float32Array(data: ArrayLike<number>): number
67
+ Int32Array(data: ArrayLike<number>): number
68
+ Uint32Array(data: ArrayLike<number>): number
69
+ Int16Array(data: ArrayLike<number>): number
70
+ Uint16Array(data: ArrayLike<number>): number
71
+ Int8Array(data: ArrayLike<number>): number
72
+ Uint8Array(data: ArrayLike<number>): number
73
+ /** Decode one pointer (or a multi-value tuple of pointers) back to a JS value. */
74
+ read(ptr: number | number[]): unknown
75
+ /** Reserve `bytes` of raw heap; returns the offset. */
76
+ alloc(bytes: number): number
77
+ /** Rewind the bump pointer — drops every allocation since the last reset. */
78
+ reset(): void
79
+ }
80
+
81
+ /** A compiled-and-instantiated jz module. */
82
+ export interface JzInstance {
83
+ /** JS-wrapped exports: marshals arguments in, decodes pointer returns, throws real `Error`s. */
84
+ exports: Record<string, (...args: any[]) => any> & Record<string, any>
85
+ /** The value codec, or `null` for a pure-scalar module with no heap. */
86
+ memory: JzMemory | null
87
+ /** The raw `WebAssembly.Instance` (numbers pass through; pointers come back NaN-boxed). */
88
+ instance: WebAssembly.Instance
89
+ /** The underlying `WebAssembly.Module`. */
90
+ module: WebAssembly.Module
91
+ }
92
+
93
+ /** Create a shared memory that modules compile into (schemas accumulate across modules). */
94
+ export interface MemoryFactory {
95
+ (src?: WebAssembly.Memory): JzMemory
96
+ }
97
+
98
+ /** The default export: compile + instantiate, as a call or a tagged template. */
99
+ export interface Jz {
100
+ /** Compile and instantiate a source string. */
101
+ (code: string, opts?: CompileOptions): JzInstance
102
+ /** Tagged-template form: interpolations are baked into the source at compile time. */
103
+ (strings: TemplateStringsArray, ...values: unknown[]): JzInstance
104
+ /** Compile only — raw WASM binary (or WAT text with `{ wat: true }`). */
105
+ compile: typeof compile
106
+ /** Shared-memory factory: `jz.memory()` or `jz.memory(existing)`. */
107
+ memory: MemoryFactory
108
+ }
109
+
110
+ declare const jz: Jz
111
+ export default jz
112
+ export { jz }
113
+
114
+ /** Compile to a raw WASM binary. */
115
+ export function compile(code: string, opts?: CompileOptions & { wat?: false }): Uint8Array
116
+ /** Compile to WAT text. */
117
+ export function compile(code: string, opts: CompileOptions & { wat: true }): string
118
+
119
+ /** Compile once to a `WebAssembly.Module` (pays AOT + validate cost once); instantiate many. */
120
+ export function compileModule(code: string, opts?: CompileOptions): WebAssembly.Module
121
+
122
+ /** Instantiate a compiled module or raw WASM bytes, wiring the allocator and value codec. */
123
+ export function instantiate(
124
+ module: WebAssembly.Module | Uint8Array | ArrayBuffer,
125
+ opts?: CompileOptions,
126
+ ): JzInstance