@noiselang/core 0.1.1 → 0.1.2
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/package.json +3 -3
- package/wasm/noise.d.ts +57 -0
- package/wasm/noise.js +299 -0
- package/wasm/noise_bg.wasm +0 -0
- package/wasm/noise_bg.wasm.d.ts +11 -0
- package/wasm/package.json +20 -0
- package/wasm/snippets/noise-core-868c65e4ce60d3b3/inline0.js +45 -0
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noiselang/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "The Noise probabilistic language engine, compiled to WebAssembly. Parse and run .noise programs (with variable introspection) from any JS/TS project — the .wasm ships in the package and bundles into your build.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Manu Mtz.-Almeida <manu.valladolid@gmail.com>",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
|
-
"url": "https://github.com/manucorporat/noise-lang",
|
|
9
|
+
"url": "git+https://github.com/manucorporat/noise-lang.git",
|
|
10
10
|
"directory": "packages/core"
|
|
11
11
|
},
|
|
12
12
|
"type": "module",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"wasm"
|
|
25
25
|
],
|
|
26
26
|
"scripts": {
|
|
27
|
-
"build:wasm": "cd ../.. && PATH=\"$HOME/.cargo/bin:$PATH\" wasm-pack build crates/noise-wasm --target web --out-dir ../../packages/core/wasm --out-name noise --release",
|
|
27
|
+
"build:wasm": "cd ../.. && PATH=\"$HOME/.cargo/bin:$PATH\" wasm-pack build crates/noise-wasm --target web --out-dir ../../packages/core/wasm --out-name noise --release && rm -f packages/core/wasm/.gitignore",
|
|
28
28
|
"build:ts": "tsc -p tsconfig.json",
|
|
29
29
|
"build": "pnpm run build:wasm && pnpm run build:ts",
|
|
30
30
|
"prepublishOnly": "pnpm run build"
|
package/wasm/noise.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Run a Noise program. Always returns a JSON string (never throws); the JS side reads
|
|
6
|
+
* `ok`/`value`/`output`/`error`/`stats`/`log`.
|
|
7
|
+
*/
|
|
8
|
+
export function run(src: string): string;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Run a program, then resolve a list of introspection requests against its (retained) scope.
|
|
12
|
+
* `requests_json` is a JSON array of [`Request`]. Always returns a JSON string. The bindings and
|
|
13
|
+
* per-request results let the playground show a variable's distribution, two variables' relationship,
|
|
14
|
+
* or what drives a variable — all without the source containing a single `describe`/`corr` call.
|
|
15
|
+
*/
|
|
16
|
+
export function run_with_introspection(src: string, requests_json: string): string;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The crate version, surfaced in the playground footer ("Noise vX.Y.Z").
|
|
20
|
+
*/
|
|
21
|
+
export function version(): string;
|
|
22
|
+
|
|
23
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
24
|
+
|
|
25
|
+
export interface InitOutput {
|
|
26
|
+
readonly memory: WebAssembly.Memory;
|
|
27
|
+
readonly run: (a: number, b: number) => [number, number];
|
|
28
|
+
readonly run_with_introspection: (a: number, b: number, c: number, d: number) => [number, number];
|
|
29
|
+
readonly version: () => [number, number];
|
|
30
|
+
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
31
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
32
|
+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
33
|
+
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
34
|
+
readonly __wbindgen_start: () => void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
41
|
+
* a precompiled `WebAssembly.Module`.
|
|
42
|
+
*
|
|
43
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
44
|
+
*
|
|
45
|
+
* @returns {InitOutput}
|
|
46
|
+
*/
|
|
47
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
51
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
52
|
+
*
|
|
53
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
54
|
+
*
|
|
55
|
+
* @returns {Promise<InitOutput>}
|
|
56
|
+
*/
|
|
57
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|
package/wasm/noise.js
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/* @ts-self-types="./noise.d.ts" */
|
|
2
|
+
import { nz_kernel_new, nz_kernel_run, nz_kernel_seed } from './snippets/noise-core-868c65e4ce60d3b3/inline0.js';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Run a Noise program. Always returns a JSON string (never throws); the JS side reads
|
|
7
|
+
* `ok`/`value`/`output`/`error`/`stats`/`log`.
|
|
8
|
+
* @param {string} src
|
|
9
|
+
* @returns {string}
|
|
10
|
+
*/
|
|
11
|
+
export function run(src) {
|
|
12
|
+
let deferred2_0;
|
|
13
|
+
let deferred2_1;
|
|
14
|
+
try {
|
|
15
|
+
const ptr0 = passStringToWasm0(src, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
16
|
+
const len0 = WASM_VECTOR_LEN;
|
|
17
|
+
const ret = wasm.run(ptr0, len0);
|
|
18
|
+
deferred2_0 = ret[0];
|
|
19
|
+
deferred2_1 = ret[1];
|
|
20
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
21
|
+
} finally {
|
|
22
|
+
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Run a program, then resolve a list of introspection requests against its (retained) scope.
|
|
28
|
+
* `requests_json` is a JSON array of [`Request`]. Always returns a JSON string. The bindings and
|
|
29
|
+
* per-request results let the playground show a variable's distribution, two variables' relationship,
|
|
30
|
+
* or what drives a variable — all without the source containing a single `describe`/`corr` call.
|
|
31
|
+
* @param {string} src
|
|
32
|
+
* @param {string} requests_json
|
|
33
|
+
* @returns {string}
|
|
34
|
+
*/
|
|
35
|
+
export function run_with_introspection(src, requests_json) {
|
|
36
|
+
let deferred3_0;
|
|
37
|
+
let deferred3_1;
|
|
38
|
+
try {
|
|
39
|
+
const ptr0 = passStringToWasm0(src, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
40
|
+
const len0 = WASM_VECTOR_LEN;
|
|
41
|
+
const ptr1 = passStringToWasm0(requests_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
42
|
+
const len1 = WASM_VECTOR_LEN;
|
|
43
|
+
const ret = wasm.run_with_introspection(ptr0, len0, ptr1, len1);
|
|
44
|
+
deferred3_0 = ret[0];
|
|
45
|
+
deferred3_1 = ret[1];
|
|
46
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
47
|
+
} finally {
|
|
48
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The crate version, surfaced in the playground footer ("Noise vX.Y.Z").
|
|
54
|
+
* @returns {string}
|
|
55
|
+
*/
|
|
56
|
+
export function version() {
|
|
57
|
+
let deferred1_0;
|
|
58
|
+
let deferred1_1;
|
|
59
|
+
try {
|
|
60
|
+
const ret = wasm.version();
|
|
61
|
+
deferred1_0 = ret[0];
|
|
62
|
+
deferred1_1 = ret[1];
|
|
63
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
64
|
+
} finally {
|
|
65
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function __wbg_get_imports() {
|
|
69
|
+
const import0 = {
|
|
70
|
+
__proto__: null,
|
|
71
|
+
__wbg_nz_kernel_new_f697efd6130216b0: function(arg0, arg1) {
|
|
72
|
+
const ret = nz_kernel_new(getArrayU8FromWasm0(arg0, arg1));
|
|
73
|
+
return ret;
|
|
74
|
+
},
|
|
75
|
+
__wbg_nz_kernel_run_b0b42b4e000e6298: function(arg0, arg1, arg2, arg3) {
|
|
76
|
+
nz_kernel_run(arg0, getArrayF64FromWasm0(arg1, arg2), arg3 >>> 0);
|
|
77
|
+
},
|
|
78
|
+
__wbg_nz_kernel_seed_767562e23f2614ac: function(arg0, arg1, arg2) {
|
|
79
|
+
nz_kernel_seed(arg0, getArrayU64FromWasm0(arg1, arg2));
|
|
80
|
+
},
|
|
81
|
+
__wbindgen_init_externref_table: function() {
|
|
82
|
+
const table = wasm.__wbindgen_externrefs;
|
|
83
|
+
const offset = table.grow(4);
|
|
84
|
+
table.set(0, undefined);
|
|
85
|
+
table.set(offset + 0, undefined);
|
|
86
|
+
table.set(offset + 1, null);
|
|
87
|
+
table.set(offset + 2, true);
|
|
88
|
+
table.set(offset + 3, false);
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
return {
|
|
92
|
+
__proto__: null,
|
|
93
|
+
"./noise_bg.js": import0,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function getArrayF64FromWasm0(ptr, len) {
|
|
98
|
+
ptr = ptr >>> 0;
|
|
99
|
+
return getFloat64ArrayMemory0().subarray(ptr / 8, ptr / 8 + len);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function getArrayU64FromWasm0(ptr, len) {
|
|
103
|
+
ptr = ptr >>> 0;
|
|
104
|
+
return getBigUint64ArrayMemory0().subarray(ptr / 8, ptr / 8 + len);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function getArrayU8FromWasm0(ptr, len) {
|
|
108
|
+
ptr = ptr >>> 0;
|
|
109
|
+
return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let cachedBigUint64ArrayMemory0 = null;
|
|
113
|
+
function getBigUint64ArrayMemory0() {
|
|
114
|
+
if (cachedBigUint64ArrayMemory0 === null || cachedBigUint64ArrayMemory0.byteLength === 0) {
|
|
115
|
+
cachedBigUint64ArrayMemory0 = new BigUint64Array(wasm.memory.buffer);
|
|
116
|
+
}
|
|
117
|
+
return cachedBigUint64ArrayMemory0;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
let cachedFloat64ArrayMemory0 = null;
|
|
121
|
+
function getFloat64ArrayMemory0() {
|
|
122
|
+
if (cachedFloat64ArrayMemory0 === null || cachedFloat64ArrayMemory0.byteLength === 0) {
|
|
123
|
+
cachedFloat64ArrayMemory0 = new Float64Array(wasm.memory.buffer);
|
|
124
|
+
}
|
|
125
|
+
return cachedFloat64ArrayMemory0;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function getStringFromWasm0(ptr, len) {
|
|
129
|
+
return decodeText(ptr >>> 0, len);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
let cachedUint8ArrayMemory0 = null;
|
|
133
|
+
function getUint8ArrayMemory0() {
|
|
134
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
135
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
136
|
+
}
|
|
137
|
+
return cachedUint8ArrayMemory0;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
141
|
+
if (realloc === undefined) {
|
|
142
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
143
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
144
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
145
|
+
WASM_VECTOR_LEN = buf.length;
|
|
146
|
+
return ptr;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
let len = arg.length;
|
|
150
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
151
|
+
|
|
152
|
+
const mem = getUint8ArrayMemory0();
|
|
153
|
+
|
|
154
|
+
let offset = 0;
|
|
155
|
+
|
|
156
|
+
for (; offset < len; offset++) {
|
|
157
|
+
const code = arg.charCodeAt(offset);
|
|
158
|
+
if (code > 0x7F) break;
|
|
159
|
+
mem[ptr + offset] = code;
|
|
160
|
+
}
|
|
161
|
+
if (offset !== len) {
|
|
162
|
+
if (offset !== 0) {
|
|
163
|
+
arg = arg.slice(offset);
|
|
164
|
+
}
|
|
165
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
166
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
167
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
168
|
+
|
|
169
|
+
offset += ret.written;
|
|
170
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
WASM_VECTOR_LEN = offset;
|
|
174
|
+
return ptr;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
178
|
+
cachedTextDecoder.decode();
|
|
179
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
180
|
+
let numBytesDecoded = 0;
|
|
181
|
+
function decodeText(ptr, len) {
|
|
182
|
+
numBytesDecoded += len;
|
|
183
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
184
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
185
|
+
cachedTextDecoder.decode();
|
|
186
|
+
numBytesDecoded = len;
|
|
187
|
+
}
|
|
188
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const cachedTextEncoder = new TextEncoder();
|
|
192
|
+
|
|
193
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
194
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
195
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
196
|
+
view.set(buf);
|
|
197
|
+
return {
|
|
198
|
+
read: arg.length,
|
|
199
|
+
written: buf.length
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
let WASM_VECTOR_LEN = 0;
|
|
205
|
+
|
|
206
|
+
let wasmModule, wasmInstance, wasm;
|
|
207
|
+
function __wbg_finalize_init(instance, module) {
|
|
208
|
+
wasmInstance = instance;
|
|
209
|
+
wasm = instance.exports;
|
|
210
|
+
wasmModule = module;
|
|
211
|
+
cachedBigUint64ArrayMemory0 = null;
|
|
212
|
+
cachedFloat64ArrayMemory0 = null;
|
|
213
|
+
cachedUint8ArrayMemory0 = null;
|
|
214
|
+
wasm.__wbindgen_start();
|
|
215
|
+
return wasm;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async function __wbg_load(module, imports) {
|
|
219
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
220
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
221
|
+
try {
|
|
222
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
223
|
+
} catch (e) {
|
|
224
|
+
const validResponse = module.ok && expectedResponseType(module.type);
|
|
225
|
+
|
|
226
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
227
|
+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
228
|
+
|
|
229
|
+
} else { throw e; }
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const bytes = await module.arrayBuffer();
|
|
234
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
235
|
+
} else {
|
|
236
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
237
|
+
|
|
238
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
239
|
+
return { instance, module };
|
|
240
|
+
} else {
|
|
241
|
+
return instance;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function expectedResponseType(type) {
|
|
246
|
+
switch (type) {
|
|
247
|
+
case 'basic': case 'cors': case 'default': return true;
|
|
248
|
+
}
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function initSync(module) {
|
|
254
|
+
if (wasm !== undefined) return wasm;
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
if (module !== undefined) {
|
|
258
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
259
|
+
({module} = module)
|
|
260
|
+
} else {
|
|
261
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const imports = __wbg_get_imports();
|
|
266
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
267
|
+
module = new WebAssembly.Module(module);
|
|
268
|
+
}
|
|
269
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
270
|
+
return __wbg_finalize_init(instance, module);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async function __wbg_init(module_or_path) {
|
|
274
|
+
if (wasm !== undefined) return wasm;
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
if (module_or_path !== undefined) {
|
|
278
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
279
|
+
({module_or_path} = module_or_path)
|
|
280
|
+
} else {
|
|
281
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (module_or_path === undefined) {
|
|
286
|
+
module_or_path = new URL('noise_bg.wasm', import.meta.url);
|
|
287
|
+
}
|
|
288
|
+
const imports = __wbg_get_imports();
|
|
289
|
+
|
|
290
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
291
|
+
module_or_path = fetch(module_or_path);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
295
|
+
|
|
296
|
+
return __wbg_finalize_init(instance, module);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export { initSync, __wbg_init as default };
|
|
Binary file
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
export const memory: WebAssembly.Memory;
|
|
4
|
+
export const run: (a: number, b: number) => [number, number];
|
|
5
|
+
export const run_with_introspection: (a: number, b: number, c: number, d: number) => [number, number];
|
|
6
|
+
export const version: () => [number, number];
|
|
7
|
+
export const __wbindgen_externrefs: WebAssembly.Table;
|
|
8
|
+
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
9
|
+
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
10
|
+
export const __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
11
|
+
export const __wbindgen_start: () => void;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "noise-wasm",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"collaborators": [
|
|
5
|
+
"Manu Mtz.-Almeida <manu.valladolid@gmail.com>"
|
|
6
|
+
],
|
|
7
|
+
"description": "WebAssembly bindings for the Noise probabilistic language (browser playground).",
|
|
8
|
+
"version": "0.1.1",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"files": [
|
|
11
|
+
"noise_bg.wasm",
|
|
12
|
+
"noise.js",
|
|
13
|
+
"noise.d.ts"
|
|
14
|
+
],
|
|
15
|
+
"main": "noise.js",
|
|
16
|
+
"types": "noise.d.ts",
|
|
17
|
+
"sideEffects": [
|
|
18
|
+
"./snippets/*"
|
|
19
|
+
]
|
|
20
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
|
|
2
|
+
const _CAP = 64;
|
|
3
|
+
const _byHash = new Map(); // hash -> id
|
|
4
|
+
const _byId = new Map(); // id (insertion-ordered) -> instance
|
|
5
|
+
let _next = 1;
|
|
6
|
+
// round-half-away-from-zero, matching Rust's f64::round (Math.round rounds half toward +inf).
|
|
7
|
+
const _round = (x) => { const a = Math.floor(Math.abs(x) + 0.5); return x < 0 ? -a : a; };
|
|
8
|
+
// `ln`/`sin`/`cos` are inlined in the kernel now; only these three remain host calls.
|
|
9
|
+
const _imports = { m: { atan: Math.atan, round: _round, pow: Math.pow } };
|
|
10
|
+
|
|
11
|
+
function _hash(bytes) { // FNV-1a over the kernel bytes (cheap; bytes are small and run once per program)
|
|
12
|
+
let h = 0x811c9dc5;
|
|
13
|
+
for (let i = 0; i < bytes.length; i++) { h = (h ^ bytes[i]) >>> 0; h = Math.imul(h, 0x01000193) >>> 0; }
|
|
14
|
+
return (h >>> 0) + ":" + bytes.length;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function nz_kernel_new(bytes) {
|
|
18
|
+
const key = _hash(bytes);
|
|
19
|
+
const cached = _byHash.get(key);
|
|
20
|
+
if (cached !== undefined && _byId.has(cached)) return cached;
|
|
21
|
+
let instance;
|
|
22
|
+
try {
|
|
23
|
+
instance = new WebAssembly.Instance(new WebAssembly.Module(bytes), _imports);
|
|
24
|
+
} catch (_e) {
|
|
25
|
+
return -1;
|
|
26
|
+
}
|
|
27
|
+
const id = _next++;
|
|
28
|
+
_byHash.set(key, id);
|
|
29
|
+
_byId.set(id, instance);
|
|
30
|
+
if (_byId.size > _CAP) { const oldest = _byId.keys().next().value; _byId.delete(oldest); }
|
|
31
|
+
return id;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function nz_kernel_seed(handle, state) {
|
|
35
|
+
const inst = _byId.get(handle);
|
|
36
|
+
// `state` is a BigUint64Array view over wasm memory (from Rust `&[u64]`); copy it into the child.
|
|
37
|
+
new BigUint64Array(inst.exports.memory.buffer, 0, state.length).set(state);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function nz_kernel_run(handle, out, n) {
|
|
41
|
+
const inst = _byId.get(handle);
|
|
42
|
+
inst.exports.kernel(4096, n, 0); // kernel(out_ptr, n, state_ptr) over the child's own memory
|
|
43
|
+
// `out` is a live Float64Array view over wasm memory (from Rust `&mut [f64]`); fill it directly.
|
|
44
|
+
out.set(new Float64Array(inst.exports.memory.buffer, 4096, n));
|
|
45
|
+
}
|