strictjs-runtime 1.0.4 → 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.
- package/README.md +73 -17
- package/index.js +16 -10
- 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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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("
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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("
|
|
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,13 +1,16 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
2
|
import path from "path";
|
|
3
3
|
import { fileURLToPath } from "url";
|
|
4
|
-
import
|
|
4
|
+
import initWASM, {
|
|
5
|
+
StrictNumber,
|
|
6
|
+
StrictString,
|
|
5
7
|
get_memory,
|
|
6
8
|
HeapType,
|
|
7
9
|
StrictArray,
|
|
8
10
|
StrictForLoop,
|
|
9
11
|
StrictFunction,
|
|
10
|
-
StrictObject
|
|
12
|
+
StrictObject,
|
|
13
|
+
Schema
|
|
11
14
|
} from "./pkg/strictjs_runtime.js";
|
|
12
15
|
|
|
13
16
|
// Handle __dirname for ES modules
|
|
@@ -15,24 +18,26 @@ const __filename = fileURLToPath(import.meta.url);
|
|
|
15
18
|
const __dirname = path.dirname(__filename);
|
|
16
19
|
|
|
17
20
|
export default async function strictInit() {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
// Node.js environment
|
|
21
|
+
if (typeof window === "undefined") {
|
|
22
|
+
// Node.js environment: load WASM manually
|
|
21
23
|
const wasmPath = path.resolve(__dirname, "pkg/strictjs_runtime_bg.wasm");
|
|
22
24
|
const wasmBuffer = fs.readFileSync(wasmPath);
|
|
23
|
-
await
|
|
25
|
+
await initWASM(wasmBuffer);
|
|
24
26
|
} else {
|
|
25
|
-
// Browser environment
|
|
26
|
-
await
|
|
27
|
+
// Browser environment: let it fetch automatically
|
|
28
|
+
await initWASM();
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
return {
|
|
32
|
+
StrictNumber,
|
|
33
|
+
StrictString,
|
|
30
34
|
get_memory,
|
|
31
35
|
HeapType,
|
|
32
36
|
StrictArray,
|
|
33
37
|
StrictForLoop,
|
|
34
38
|
StrictFunction,
|
|
35
|
-
StrictObject
|
|
39
|
+
StrictObject,
|
|
40
|
+
Schema
|
|
36
41
|
};
|
|
37
42
|
}
|
|
38
43
|
|
|
@@ -42,5 +47,6 @@ export {
|
|
|
42
47
|
StrictArray,
|
|
43
48
|
StrictForLoop,
|
|
44
49
|
StrictFunction,
|
|
45
|
-
StrictObject
|
|
50
|
+
StrictObject,
|
|
51
|
+
Schema
|
|
46
52
|
};
|