strictjs-runtime 2.0.6 → 2.0.10

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.
package/index.js CHANGED
@@ -1,67 +1,238 @@
1
1
  import initWASM, {
2
- StrictNumber,
3
- StrictString,
4
- get_memory,
5
- HeapType,
6
- StrictArray,
7
- StrictForLoop,
8
- StrictFunction,
9
- StrictObject,
10
- Schema
2
+ StrictNumber, StrictString, get_memory, HeapType, StrictArray, StrictForLoop, StrictFunction, StrictObject, Schema,
3
+ // Additional exports from your wasm file
4
+ StrictBoolean, StrictBigInt, StrictPromise, StrictTimeout, StrictWhileLoop, StrictAsync,
5
+ ThreadManager, ThreadPool, ThreadTask, GPUMemoryManager, JsGPUType, JsSIMDType, JsHeapType,
6
+ JsTypeCapabilities, OptimizationMode, StringEncoding, TaskPriority, ThreadPriority, ThreadState,
7
+ getIterator, createTensor, createVector, createMatrix, createZeros, createOnes, createRange,
8
+ strict_fetch, init_thread_manager, create_simd_f32x4, create_simd_i32x4, create_simd_u8x16,
9
+ createGPUType, getAvailableGPUTypes, createSIMDType, getAvailableSIMDTypes, getSIMDTypeForUseCase
11
10
  } from "./pkg/strictjs_runtime.js";
12
11
 
13
12
  let nodeInitWASM = null;
13
+ let isInitialized = false;
14
14
 
15
- // Only import `fs` and `path` in Node environment
16
- if (typeof window === "undefined") {
17
- const fs = await import("fs");
18
- const path = await import("path");
19
- const { fileURLToPath } = await import("url");
20
-
21
- const __filename = fileURLToPath(import.meta.url);
22
- const __dirname = path.dirname(__filename);
15
+ /**
16
+ * This function dynamically loads WASM in Node.js
17
+ */
18
+ async function setupNodeLoader() {
19
+ try {
20
+ const fs = await import("fs");
21
+ const path = await import("path");
22
+ const { fileURLToPath } = await import("url");
23
+
24
+ const __filename = fileURLToPath(import.meta.url);
25
+ const __dirname = path.dirname(__filename);
26
+
27
+ return async () => {
28
+ try {
29
+ const wasmPath = path.resolve(__dirname, "pkg/strictjs_runtime_bg.wasm");
30
+
31
+ // Check if file exists before reading
32
+ if (!fs.existsSync(wasmPath)) {
33
+ // Try alternative path
34
+ const altWasmPath = path.resolve(__dirname, "strictjs_runtime_bg.wasm");
35
+ if (!fs.existsSync(altWasmPath)) {
36
+ throw new Error(`WASM file not found at ${wasmPath} or ${altWasmPath}`);
37
+ }
38
+ const wasmBuffer = fs.readFileSync(altWasmPath);
39
+ await initWASM(wasmBuffer);
40
+ } else {
41
+ const wasmBuffer = fs.readFileSync(wasmPath);
42
+ await initWASM(wasmBuffer);
43
+ }
44
+
45
+ isInitialized = true;
46
+ } catch (error) {
47
+ throw new Error(`Failed to load WASM in Node.js: ${error.message}`);
48
+ }
49
+ };
50
+ } catch (error) {
51
+ throw new Error(`Failed to setup Node.js loader: ${error.message}`);
52
+ }
53
+ }
23
54
 
24
- nodeInitWASM = async () => {
25
- const wasmPath = path.resolve(__dirname, "pkg/strictjs_runtime_bg.wasm");
26
- const wasmBuffer = fs.readFileSync(wasmPath);
27
- await initWASM(wasmBuffer);
28
- };
55
+ // Lazy initialize Node loader if in Node.js environment
56
+ if (typeof window === "undefined" && typeof process !== "undefined" && process.versions != null && process.versions.node != null) {
57
+ nodeInitWASM = await setupNodeLoader();
29
58
  }
30
59
 
31
60
  /**
32
61
  * Initializes StrictJS Runtime
33
62
  * - Node: Loads WASM from filesystem
34
63
  * - Browser: WASM auto-fetch
64
+ *
65
+ * @param {Object} options - Initialization options
66
+ * @param {string} [options.wasmPath] - Custom path to WASM file (browser only)
67
+ * @param {boolean} [options.sync=false] - Use synchronous initialization (Node.js only)
68
+ * @returns {Promise<Object>} Runtime exports
69
+ * @throws {Error} If initialization fails
35
70
  */
36
- export default async function strictInit() {
37
- if (typeof window === "undefined") {
38
- // Node.js environment
39
- await nodeInitWASM();
40
- } else {
41
- // Browser environment
42
- await initWASM();
71
+ export default async function strictInit(options = {}) {
72
+ // Prevent multiple initializations
73
+ if (isInitialized) {
74
+ return getExports();
43
75
  }
76
+
77
+ try {
78
+ if (typeof window === "undefined") {
79
+ // Node.js environment
80
+ if (!nodeInitWASM) {
81
+ throw new Error("Node.js loader not initialized");
82
+ }
83
+ await nodeInitWASM();
84
+ } else {
85
+ // Browser environment
86
+ if (options.wasmPath) {
87
+ // Custom WASM path
88
+ const response = await fetch(options.wasmPath);
89
+ if (!response.ok) {
90
+ throw new Error(`Failed to fetch WASM: ${response.statusText}`);
91
+ }
92
+ const buffer = await response.arrayBuffer();
93
+ await initWASM(buffer);
94
+ } else {
95
+ // Default - let the wasm bindings fetch it
96
+ await initWASM();
97
+ }
98
+ }
99
+
100
+ isInitialized = true;
101
+ return getExports();
102
+ } catch (error) {
103
+ throw new Error(`StrictJS initialization failed: ${error.message}`);
104
+ }
105
+ }
44
106
 
107
+ /**
108
+ * Get runtime exports
109
+ * @private
110
+ */
111
+ function getExports() {
45
112
  return {
113
+ // Core types
46
114
  StrictNumber,
47
115
  StrictString,
48
- get_memory,
49
- HeapType,
50
116
  StrictArray,
51
- StrictForLoop,
52
- StrictFunction,
117
+ StrictBoolean,
118
+ StrictBigInt,
53
119
  StrictObject,
54
- Schema
120
+ StrictFunction,
121
+ StrictPromise,
122
+
123
+ // Loops and async
124
+ StrictForLoop,
125
+ StrictWhileLoop,
126
+ StrictTimeout,
127
+ StrictAsync,
128
+
129
+ // Threading
130
+ ThreadManager,
131
+ ThreadPool,
132
+ ThreadTask,
133
+
134
+ // GPU and SIMD
135
+ GPUMemoryManager,
136
+ JsGPUType,
137
+ JsSIMDType,
138
+ JsHeapType,
139
+ JsTypeCapabilities,
140
+
141
+ // Schema
142
+ Schema,
143
+
144
+ // Memory
145
+ get_memory,
146
+
147
+ // Enums
148
+ HeapType,
149
+ OptimizationMode,
150
+ StringEncoding,
151
+ TaskPriority,
152
+ ThreadPriority,
153
+ ThreadState,
154
+
155
+ // Factory functions
156
+ getIterator,
157
+ createTensor,
158
+ createVector,
159
+ createMatrix,
160
+ createZeros,
161
+ createOnes,
162
+ createRange,
163
+ strict_fetch,
164
+ init_thread_manager,
165
+ create_simd_f32x4,
166
+ create_simd_i32x4,
167
+ create_simd_u8x16,
168
+ createGPUType,
169
+ getAvailableGPUTypes,
170
+ createSIMDType,
171
+ getAvailableSIMDTypes,
172
+ getSIMDTypeForUseCase
55
173
  };
56
174
  }
57
175
 
58
- // Named exports
176
+ // Named exports for core functionality
59
177
  export {
60
- get_memory,
61
- HeapType,
178
+ // Core types
179
+ StrictNumber,
180
+ StrictString,
62
181
  StrictArray,
63
- StrictForLoop,
64
- StrictFunction,
182
+ StrictBoolean,
183
+ StrictBigInt,
65
184
  StrictObject,
66
- Schema
67
- };
185
+ StrictFunction,
186
+ StrictPromise,
187
+
188
+ // Loops and async
189
+ StrictForLoop,
190
+ StrictWhileLoop,
191
+ StrictTimeout,
192
+ StrictAsync,
193
+
194
+ // Threading
195
+ ThreadManager,
196
+ ThreadPool,
197
+ ThreadTask,
198
+
199
+ // GPU and SIMD
200
+ GPUMemoryManager,
201
+ JsGPUType,
202
+ JsSIMDType,
203
+ JsHeapType,
204
+ JsTypeCapabilities,
205
+
206
+ // Schema
207
+ Schema,
208
+
209
+ // Memory
210
+ get_memory,
211
+
212
+ // Enums
213
+ HeapType,
214
+ OptimizationMode,
215
+ StringEncoding,
216
+ TaskPriority,
217
+ ThreadPriority,
218
+ ThreadState,
219
+
220
+ // Factory functions
221
+ getIterator,
222
+ createTensor,
223
+ createVector,
224
+ createMatrix,
225
+ createZeros,
226
+ createOnes,
227
+ createRange,
228
+ strict_fetch,
229
+ init_thread_manager,
230
+ create_simd_f32x4,
231
+ create_simd_i32x4,
232
+ create_simd_u8x16,
233
+ createGPUType,
234
+ getAvailableGPUTypes,
235
+ createSIMDType,
236
+ getAvailableSIMDTypes,
237
+ getSIMDTypeForUseCase
238
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "strictjs-runtime",
3
- "version": "2.0.6",
3
+ "version": "2.0.10",
4
4
  "description": "A lightweight low-level runtime for StrictJS with WebAssembly support.",
5
5
  "main": "index.js",
6
6
  "module": "index.js",
package/pkg/README.md DELETED
@@ -1,20 +0,0 @@
1
-
2
- # StrictJS Runtime
3
-
4
- Type-safe JavaScript runtime with WebAssembly - Bringing Rust's safety guarantees to JavaScript.
5
-
6
- ## Features
7
-
8
- - 🔒 **Type-safe numbers** with automatic clamping
9
- - 📏 **Bounded strings** with character limits
10
- - 🧮 **Safe arrays** with bounds checking
11
- - 🏗️ **Schema-based objects** with type guarantees
12
- - ⚡ **WebAssembly performance** with JavaScript convenience
13
-
14
- ## Installation
15
-
16
- ### CDN (Browser)
17
- ```html
18
- <script type="module">
19
- import { StrictNumber, HeapType, StrictString, StrictArray, StrictObject } from 'https://cdn.jsdelivr.net/npm/strictjs-runtime@latest/pkg/strictjs_runtime.js';
20
- </script>
package/pkg/package.json DELETED
@@ -1,24 +0,0 @@
1
- {
2
- "name": "strictjs-runtime",
3
- "type": "module",
4
- "collaborators": [
5
- "Your Name kennethmburu21@gmail.com"
6
- ],
7
- "description": "Type-safe JavaScript runtime with WebAssembly - Bringing Rust's safety guarantees to JavaScript",
8
- "version": "0.1.0",
9
- "license": "MIT",
10
- "repository": {
11
- "type": "git",
12
- "url": "https://github.com/yourusername/strictjs-runtime"
13
- },
14
- "files": [
15
- "strictjs_runtime_bg.wasm",
16
- "strictjs_runtime.js",
17
- "strictjs_runtime.d.ts"
18
- ],
19
- "main": "strictjs_runtime.js",
20
- "types": "strictjs_runtime.d.ts",
21
- "sideEffects": [
22
- "./snippets/*"
23
- ]
24
- }