pikelet-wasm 0.7.0

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.
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ const PANCAKE_ERROR_CODES = Object.freeze({
4
+ INVALID_ARGUMENT: 'INVALID_ARGUMENT',
5
+ DIMENSION_MISMATCH: 'DIMENSION_MISMATCH',
6
+ INVALID_VECTOR: 'INVALID_VECTOR',
7
+ INDEX_FULL: 'INDEX_FULL',
8
+ INDEX_DISPOSED: 'INDEX_DISPOSED',
9
+ COMPACTION_REQUIRED: 'COMPACTION_REQUIRED',
10
+ SNAPSHOT_INVALID: 'SNAPSHOT_INVALID',
11
+ SNAPSHOT_CONFIG_MISMATCH: 'SNAPSHOT_CONFIG_MISMATCH',
12
+ SNAPSHOT_CAPACITY_EXCEEDED: 'SNAPSHOT_CAPACITY_EXCEEDED',
13
+ WASM_LOAD_FAILED: 'WASM_LOAD_FAILED',
14
+ WASM_ALLOCATION_FAILED: 'WASM_ALLOCATION_FAILED',
15
+ FILE_IO_FAILED: 'FILE_IO_FAILED',
16
+ PARSE_FAILED: 'PARSE_FAILED',
17
+ INTERNAL_INVARIANT: 'INTERNAL_INVARIANT',
18
+ // Reserved for the deferred shared-runtime API.
19
+ INDEX_LIMIT: 'INDEX_LIMIT',
20
+ });
21
+
22
+ class PancakeError extends Error {
23
+ constructor(code, message, details, cause) {
24
+ super(message);
25
+ this.name = 'PancakeError';
26
+ this.code = code;
27
+ if (details !== undefined) this.details = details;
28
+ if (cause !== undefined) this.cause = cause;
29
+ }
30
+ }
31
+
32
+ function pancakeError(code, message, details, cause) {
33
+ return new PancakeError(code, message, details, cause);
34
+ }
35
+
36
+ module.exports = {
37
+ PancakeError,
38
+ PANCAKE_ERROR_CODES,
39
+ pancakeError,
40
+ };
@@ -0,0 +1,66 @@
1
+ 'use strict';
2
+
3
+ function toCompileSource(source) {
4
+ if (source instanceof WebAssembly.Module) return source;
5
+ if (source instanceof ArrayBuffer) return source;
6
+ if (typeof SharedArrayBuffer !== 'undefined' && source instanceof SharedArrayBuffer) {
7
+ return source;
8
+ }
9
+ if (ArrayBuffer.isView(source)) {
10
+ return source.buffer.slice(source.byteOffset, source.byteOffset + source.byteLength);
11
+ }
12
+ throw new TypeError(`Unsupported WASM source type: ${typeof source}`);
13
+ }
14
+
15
+ function createCachedModuleLoader(loadSource) {
16
+ const sourcePromises = new Map();
17
+ const modulePromises = new Map();
18
+
19
+ function getSource(variant) {
20
+ let promise = sourcePromises.get(variant);
21
+ if (!promise) {
22
+ promise = Promise.resolve().then(() => loadSource(variant));
23
+ sourcePromises.set(variant, promise);
24
+ }
25
+ return promise;
26
+ }
27
+
28
+ function getModule(variant) {
29
+ let promise = modulePromises.get(variant);
30
+ if (!promise) {
31
+ promise = getSource(variant).then((source) => {
32
+ const compileSource = toCompileSource(source);
33
+ return compileSource instanceof WebAssembly.Module
34
+ ? compileSource
35
+ : WebAssembly.compile(compileSource);
36
+ });
37
+ modulePromises.set(variant, promise);
38
+ }
39
+ return promise;
40
+ }
41
+
42
+ async function supports(variant) {
43
+ const source = toCompileSource(await getSource(variant));
44
+ if (source instanceof WebAssembly.Module) return true;
45
+ try {
46
+ return WebAssembly.validate(source);
47
+ } catch {
48
+ return false;
49
+ }
50
+ }
51
+
52
+ async function instantiate(factory, variant) {
53
+ const compiled = await getModule(variant);
54
+ return factory({
55
+ instantiateWasm(imports, successCallback) {
56
+ const instance = new WebAssembly.Instance(compiled, imports);
57
+ successCallback(instance, compiled);
58
+ return instance.exports;
59
+ },
60
+ });
61
+ }
62
+
63
+ return Object.freeze({ getModule, instantiate, supports });
64
+ }
65
+
66
+ module.exports = { createCachedModuleLoader };