strictjs-runtime 1.0.6 → 1.0.7

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 (3) hide show
  1. package/README.md +73 -17
  2. package/index.js +16 -17
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -66,30 +66,86 @@ Or load directly from a **CDN** in the browser:
66
66
  Here's a minimal example showing how to initialize StrictJS Runtime and work with strict arrays.
67
67
 
68
68
  ```js
69
- import init, { HeapType, StrictArray, StrictFunction } from "strictjs-runtime/pkg/strictjs_runtime.js";
70
69
 
71
- async function main() {
72
- await init(); // Initialize the WebAssembly runtime
73
70
 
74
- // Create a strict typed array
75
- const numbers = new StrictArray(HeapType.U8, 3);
76
- numbers.set(0, 42);
77
- numbers.set(1, 99);
71
+ // demo.js
72
+ import strictInit from "strictjs-runtime";
73
+
74
+ const run = async () => {
75
+ const { StrictObject, StrictFunction, HeapType } = await strictInit({});
76
+
77
+ console.log("=== StrictJS Demo ===\n");
78
+
79
+ // ======= Test 1: Basic Object with Schema =======
80
+ const userSchema = {
81
+ id: "u32",
82
+ name: "string",
83
+ age: "u8",
84
+ isActive: "bool",
85
+ balance: "f64"
86
+ };
87
+
88
+ const user = new StrictObject(userSchema);
89
+ user.setField("id", 1234567890);
90
+ user.setField("name", "Alice Johnson");
91
+ user.setField("age", 28);
92
+ user.setField("isActive", true);
93
+ user.setField("balance", 999.99);
94
+
95
+ console.log("ID:", user.getFieldAsNumber("id"));
96
+ console.log("Name:", user.getFieldAsString("name"));
97
+ console.log("Age:", user.getFieldAsNumber("age"));
98
+ console.log("Active:", user.getFieldAsBoolean("isActive"));
99
+ console.log("Balance:", user.getFieldAsNumber("balance"));
100
+
101
+ // ======= Test 2: Nested Objects =======
102
+ const productSchema = {
103
+ id: "u32",
104
+ name: "string",
105
+ metadata: {
106
+ category: "string",
107
+ tags: {
108
+ featured: "bool",
109
+ newArrival: "bool"
110
+ }
111
+ }
112
+ };
113
+
114
+ const product = new StrictObject(productSchema);
115
+ product.setField("metadata", {
116
+ category: "Electronics",
117
+ tags: { featured: true, newArrival: false }
118
+ });
119
+
120
+ const metadata = product.getNestedObject("metadata");
121
+ const tags = metadata.getNestedObject("tags");
122
+
123
+ console.log("\nProduct Category:", metadata.getFieldAsString("category"));
124
+ console.log("Featured:", tags.getFieldAsBoolean("featured"));
125
+ console.log("New Arrival:", tags.getFieldAsBoolean("newArrival"));
126
+
127
+ // ======= Test 3: WebAssembly-backed StrictFunction =======
128
+ const addU8 = new StrictFunction(
129
+ new Function("a", "b", "return a + b;"),
130
+ ["u8", "u8"],
131
+ "u8"
132
+ );
78
133
 
79
- console.log("First number:", numbers.get(0)); // → 42
80
- console.log("Second number:", numbers.get(1)); // → 99
134
+ console.log("\nStrictFunction Add Result (u8 overflow demo):", addU8.call([200, 56]));
81
135
 
82
- // Wrap a JS function with type safety
83
- const safeAdd = new StrictFunction(
84
- (a, b) => a + b,
85
- ["u8", "u8"], // Input types
86
- "u8" // Output type
136
+ const multiplyU16 = new StrictFunction(
137
+ new Function("x", "y", "return x * y;"),
138
+ ["u16", "u16"],
139
+ "u16"
87
140
  );
88
141
 
89
- console.log("200 + 100 =", safeAdd.call([200, 100])); // → 255 (clamped)
90
- }
142
+ console.log("StrictFunction Multiply Result:", multiplyU16.call([300, 300]));
143
+ };
144
+
145
+ run().catch(console.error);
146
+
147
+
91
148
 
92
- main();
93
149
  ```
94
150
 
95
151
  ---
package/index.js CHANGED
@@ -1,15 +1,16 @@
1
-
2
-
3
1
  import fs from "fs";
4
2
  import path from "path";
5
3
  import { fileURLToPath } from "url";
6
- import initWasm, {
4
+ import initWASM, {
5
+ StrictNumber,
6
+ StrictString,
7
7
  get_memory,
8
8
  HeapType,
9
9
  StrictArray,
10
10
  StrictForLoop,
11
11
  StrictFunction,
12
- StrictObject
12
+ StrictObject,
13
+ Schema
13
14
  } from "./pkg/strictjs_runtime.js";
14
15
 
15
16
  // Handle __dirname for ES modules
@@ -17,27 +18,26 @@ const __filename = fileURLToPath(import.meta.url);
17
18
  const __dirname = path.dirname(__filename);
18
19
 
19
20
  export default async function strictInit() {
20
- // Check if running in Node.js
21
- if (typeof window === "undefined" && typeof process !== "undefined") {
22
- // Node.js environment
21
+ if (typeof window === "undefined") {
22
+ // Node.js environment: load WASM manually
23
23
  const wasmPath = path.resolve(__dirname, "pkg/strictjs_runtime_bg.wasm");
24
24
  const wasmBuffer = fs.readFileSync(wasmPath);
25
-
26
- await initWasm({ module: wasmBuffer }); // ✅ Updated way
25
+ await initWASM(wasmBuffer);
27
26
  } else {
28
- // Browser environment
29
- const wasmUrl = new URL("./pkg/strictjs_runtime_bg.wasm", import.meta.url);
30
- await initWasm({ module: fetch(wasmUrl) }); // ✅ Updated way
27
+ // Browser environment: let it fetch automatically
28
+ await initWASM();
31
29
  }
32
30
 
33
-
34
31
  return {
32
+ StrictNumber,
33
+ StrictString,
35
34
  get_memory,
36
35
  HeapType,
37
36
  StrictArray,
38
37
  StrictForLoop,
39
38
  StrictFunction,
40
- StrictObject
39
+ StrictObject,
40
+ Schema
41
41
  };
42
42
  }
43
43
 
@@ -47,7 +47,6 @@ export {
47
47
  StrictArray,
48
48
  StrictForLoop,
49
49
  StrictFunction,
50
- StrictObject
50
+ StrictObject,
51
+ Schema
51
52
  };
52
-
53
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "strictjs-runtime",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "A lightweight low-level runtime for StrictJS with WebAssembly support.",
5
5
  "main": "index.js",
6
6
  "type": "module",