@tgrv/void-math 1.0.5 → 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 +72 -0
- package/index.js +3 -3
- package/{plugin.wasm → math.wasm} +0 -0
- package/package.json +2 -2
- package/types.d.ts +3 -0
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Void Math Plugin (Go)
|
|
2
|
+
|
|
3
|
+
This is a high-performance mathematical computation plugin written in **Go**, compiled to WebAssembly (WASM), and optimized for standard Node.js applications using the **Void** framework.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Prerequisites
|
|
8
|
+
* **Go** (1.24+ recommended)
|
|
9
|
+
* **Node.js** (22.6+)
|
|
10
|
+
* **Void CLI** (available via `npx` or installed globally)
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## How to Install and Use from Scratch
|
|
15
|
+
|
|
16
|
+
Follow these steps to initialize a new host application, compile the math plugin, link it, and run it.
|
|
17
|
+
|
|
18
|
+
### Step 1: Initialize Your Node.js Host Application
|
|
19
|
+
If you have an existing or new Node.js/Express project, navigate to its directory and run:
|
|
20
|
+
```bash
|
|
21
|
+
npx void init
|
|
22
|
+
```
|
|
23
|
+
This initializes standard Void configurations and automatically installs the `@tgrv/void-runtime` engine in your host application.
|
|
24
|
+
|
|
25
|
+
### Step 2: Build the Math Plugin
|
|
26
|
+
Navigate to this directory (`plugins/math`) and run the build command:
|
|
27
|
+
```bash
|
|
28
|
+
npx void build
|
|
29
|
+
```
|
|
30
|
+
This compiles the Go source code (`main.go`) to WebAssembly and places the final NPM-ready package structure inside the `@void/void-math` subdirectory (as configured in `void.json`).
|
|
31
|
+
|
|
32
|
+
### Step 3: Link the Plugin to Your Application
|
|
33
|
+
Navigate back to your Node.js application directory and run:
|
|
34
|
+
```bash
|
|
35
|
+
npx void add <path-to-this-plugin>/@void/void-math
|
|
36
|
+
```
|
|
37
|
+
*Example (if your app is in `examples/express-void`):*
|
|
38
|
+
```bash
|
|
39
|
+
npx void add ../../plugins/math/@void/void-math
|
|
40
|
+
```
|
|
41
|
+
This command automatically:
|
|
42
|
+
1. Copies/links the build output into your host app's `node_modules`.
|
|
43
|
+
2. Resolves type declarations for TypeScript so you get auto-complete in your editor.
|
|
44
|
+
|
|
45
|
+
### Step 4: Import and Run in Your Code
|
|
46
|
+
Now, you can import and call the math plugin functions directly inside your JavaScript/TypeScript files.
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import math from '@tgrv/void-math';
|
|
50
|
+
|
|
51
|
+
// 1. Basic addition (supporting multiple signatures)
|
|
52
|
+
const sum1 = math.add({ a: 10, b: 20 });
|
|
53
|
+
const sum2 = math.add({ numbers: [1, 2, 3, 4, 5] });
|
|
54
|
+
console.log(`Sum: ${sum1}, Array Sum: ${sum2}`);
|
|
55
|
+
|
|
56
|
+
// 2. Count primes up to a limit (CPU-intensive nested loop)
|
|
57
|
+
const primes = math.count_primes({ limit: 1000000 });
|
|
58
|
+
console.log(`Primes Count: ${primes}`);
|
|
59
|
+
|
|
60
|
+
// 3. Gravitational N-Body simulation (Floating-point intensive)
|
|
61
|
+
const result = math.nbody({ n: 150, steps: 1000 });
|
|
62
|
+
console.log(`Simulation checksum: ${result}`);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## Exported Functions
|
|
68
|
+
The math plugin registers and exports the following functions:
|
|
69
|
+
* `add(args: { a: number; b: number } | { numbers: number[] }): number` — Performs arithmetic addition.
|
|
70
|
+
* `format_message(args: { template: string; value: number }): string` — Formats strings within WASM.
|
|
71
|
+
* `count_primes(args: { limit: number }): number` — Heavy integer calculation (prime counting).
|
|
72
|
+
* `nbody(args: { n: number; steps: number }): number` — High-performance floating-point physics simulation.
|
package/index.js
CHANGED
|
@@ -5,8 +5,8 @@ import { dirname, join } from "path";
|
|
|
5
5
|
const __filename = fileURLToPath(import.meta.url);
|
|
6
6
|
const __dirname = dirname(__filename);
|
|
7
7
|
|
|
8
|
-
const
|
|
9
|
-
join(__dirname, "
|
|
8
|
+
const math = await runtime.load(
|
|
9
|
+
join(__dirname, "math.wasm")
|
|
10
10
|
);
|
|
11
11
|
|
|
12
|
-
export default
|
|
12
|
+
export default math;
|
|
Binary file
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -4,6 +4,9 @@ declare module '@tgrv/void-math' {
|
|
|
4
4
|
format_message(args: { template: string; value: number }): string;
|
|
5
5
|
count_primes(args: { limit: number }): number;
|
|
6
6
|
nbody(args: { n: number; steps: number }): number;
|
|
7
|
+
get_memory_stats(args?: {}): { allocated_objects: number; total_bytes_pinned: number };
|
|
8
|
+
test_mem_pin(args: { data: string }): { ptr: number; size: number };
|
|
9
|
+
test_mem_unpin(args: { ptr: number }): string;
|
|
7
10
|
}
|
|
8
11
|
const math: MathPlugin;
|
|
9
12
|
export default math;
|