strictjs-runtime 1.0.6 → 1.0.8

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 +47 -26
  3. package/package.json +10 -4
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,53 +1,74 @@
1
-
2
-
3
- import fs from "fs";
4
- import path from "path";
5
1
  import { fileURLToPath } from "url";
6
- import initWasm, {
2
+ import {
3
+ StrictNumber,
4
+ StrictString,
7
5
  get_memory,
8
6
  HeapType,
9
7
  StrictArray,
10
8
  StrictForLoop,
11
9
  StrictFunction,
12
- StrictObject
10
+ StrictObject,
11
+ Schema
13
12
  } from "./pkg/strictjs_runtime.js";
14
13
 
15
- // Handle __dirname for ES modules
16
- const __filename = fileURLToPath(import.meta.url);
17
- const __dirname = path.dirname(__filename);
14
+ // Check if we're in Node.js environment
15
+ const isNode = typeof process !== 'undefined' &&
16
+ process.versions != null &&
17
+ process.versions.node != null;
18
18
 
19
- export default async function strictInit() {
20
- // Check if running in Node.js
21
- if (typeof window === "undefined" && typeof process !== "undefined") {
22
- // Node.js environment
23
- const wasmPath = path.resolve(__dirname, "pkg/strictjs_runtime_bg.wasm");
24
- const wasmBuffer = fs.readFileSync(wasmPath);
19
+ let initWASM;
20
+ let wasmInitialized = false;
25
21
 
26
- await initWasm({ module: wasmBuffer }); // ✅ Updated way
27
- } 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
31
- }
22
+ // Dynamic import to handle different environments
23
+ if (isNode) {
24
+ // Node.js environment
25
+ const fs = await import("fs");
26
+ const path = await import("path");
27
+
28
+ const __filename = fileURLToPath(import.meta.url);
29
+ const __dirname = path.dirname(__filename);
30
+
31
+ initWASM = (await import("./pkg/strictjs_runtime.js")).initWASM;
32
+
33
+ const wasmPath = path.resolve(__dirname, "pkg/strictjs_runtime_bg.wasm");
34
+ const wasmBuffer = fs.readFileSync(wasmPath);
35
+
36
+ await initWASM(wasmBuffer);
37
+ wasmInitialized = true;
38
+
39
+ } else {
40
+ // Browser environment
41
+ initWASM = (await import("./pkg/strictjs_runtime.js")).initWASM;
42
+ }
32
43
 
44
+ export default async function strictInit() {
45
+ if (!wasmInitialized && !isNode) {
46
+ // Initialize WASM for browser
47
+ await initWASM();
48
+ wasmInitialized = true;
49
+ }
33
50
 
34
51
  return {
52
+ StrictNumber,
53
+ StrictString,
35
54
  get_memory,
36
55
  HeapType,
37
56
  StrictArray,
38
57
  StrictForLoop,
39
58
  StrictFunction,
40
- StrictObject
59
+ StrictObject,
60
+ Schema
41
61
  };
42
62
  }
43
63
 
44
64
  export {
65
+ StrictNumber,
66
+ StrictString,
45
67
  get_memory,
46
68
  HeapType,
47
69
  StrictArray,
48
70
  StrictForLoop,
49
71
  StrictFunction,
50
- StrictObject
51
- };
52
-
53
-
72
+ StrictObject,
73
+ Schema
74
+ };
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
+
2
+
1
3
  {
2
4
  "name": "strictjs-runtime",
3
- "version": "1.0.6",
5
+ "version": "1.0.8",
4
6
  "description": "A lightweight low-level runtime for StrictJS with WebAssembly support.",
5
7
  "main": "index.js",
8
+ "module": "index.js",
9
+ "browser": "index.js",
6
10
  "type": "module",
7
11
  "files": [
8
12
  "pkg",
@@ -21,14 +25,16 @@
21
25
  "javascript",
22
26
  "webassembly"
23
27
  ],
24
- "author": "Kenneth Mburu kennethmburu21@gmail.com",
28
+ "author": "Kenneth Mburu <kennethmburu21@gmail.com>",
25
29
  "repository": {
26
30
  "type": "git",
27
31
  "url": "https://github.com/Kenneth732/strictJS.git"
28
32
  },
29
33
  "bugs": {
30
- "url": "https://github.com/Kenneth732/strictJS.git"
34
+ "url": "https://github.com/Kenneth732/strictJS/issues"
31
35
  },
32
- "homepage": "https://github.com/Kenneth732/strictJS.git",
36
+ "homepage": "https://github.com/Kenneth732/strictJS",
33
37
  "license": "MIT"
34
38
  }
39
+
40
+