wasm-rubiks-solver 0.1.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.
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "wasm-rubiks-solver",
3
+ "type": "module",
4
+ "description": "A hyper-fast, symmetry-reduced 2-Phase Rubik's Cube Solver in Rust, optimized for both native and WebAssembly.",
5
+ "version": "0.1.0",
6
+ "license": "MIT",
7
+ "files": [
8
+ "wasm_rubiks_solver_bg.wasm",
9
+ "wasm_rubiks_solver.js",
10
+ "wasm_rubiks_solver.d.ts"
11
+ ],
12
+ "main": "wasm_rubiks_solver.js",
13
+ "types": "wasm_rubiks_solver.d.ts",
14
+ "sideEffects": [
15
+ "./snippets/*"
16
+ ]
17
+ }
@@ -0,0 +1,58 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export class WasmSolver {
5
+ free(): void;
6
+ [Symbol.dispose](): void;
7
+ /**
8
+ * Create a new solver instance with embedded tables (compiled into the WASM binary).
9
+ */
10
+ static new_embedded(): WasmSolver;
11
+ /**
12
+ * Create a new solver instance by loading precomputed pruning tables from the provided byte slice.
13
+ */
14
+ constructor(data: Uint8Array);
15
+ /**
16
+ * Solve a scramble given as a space-separated string (e.g. "R U R' F2").
17
+ * Returns the space-separated solution moves, or None/null if no solution is found or the scramble is invalid.
18
+ */
19
+ solve(scramble: string): string | undefined;
20
+ }
21
+
22
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
23
+
24
+ export interface InitOutput {
25
+ readonly memory: WebAssembly.Memory;
26
+ readonly __wbg_wasmsolver_free: (a: number, b: number) => void;
27
+ readonly wasmsolver_new_embedded: () => [number, number, number];
28
+ readonly wasmsolver_new_with_bytes: (a: number, b: number) => [number, number, number];
29
+ readonly wasmsolver_solve: (a: number, b: number, c: number) => [number, number];
30
+ readonly __wbindgen_externrefs: WebAssembly.Table;
31
+ readonly __externref_table_dealloc: (a: number) => void;
32
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
33
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
34
+ readonly __wbindgen_free: (a: number, b: number, c: number) => void;
35
+ readonly __wbindgen_start: () => void;
36
+ }
37
+
38
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
39
+
40
+ /**
41
+ * Instantiates the given `module`, which can either be bytes or
42
+ * a precompiled `WebAssembly.Module`.
43
+ *
44
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
45
+ *
46
+ * @returns {InitOutput}
47
+ */
48
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
49
+
50
+ /**
51
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
52
+ * for everything else, calls `WebAssembly.instantiate` directly.
53
+ *
54
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
55
+ *
56
+ * @returns {Promise<InitOutput>}
57
+ */
58
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
@@ -0,0 +1,282 @@
1
+ /* @ts-self-types="./wasm_rubiks_solver.d.ts" */
2
+
3
+ export class WasmSolver {
4
+ static __wrap(ptr) {
5
+ const obj = Object.create(WasmSolver.prototype);
6
+ obj.__wbg_ptr = ptr;
7
+ WasmSolverFinalization.register(obj, obj.__wbg_ptr, obj);
8
+ return obj;
9
+ }
10
+ __destroy_into_raw() {
11
+ const ptr = this.__wbg_ptr;
12
+ this.__wbg_ptr = 0;
13
+ WasmSolverFinalization.unregister(this);
14
+ return ptr;
15
+ }
16
+ free() {
17
+ const ptr = this.__destroy_into_raw();
18
+ wasm.__wbg_wasmsolver_free(ptr, 0);
19
+ }
20
+ /**
21
+ * Create a new solver instance with embedded tables (compiled into the WASM binary).
22
+ * @returns {WasmSolver}
23
+ */
24
+ static new_embedded() {
25
+ const ret = wasm.wasmsolver_new_embedded();
26
+ if (ret[2]) {
27
+ throw takeFromExternrefTable0(ret[1]);
28
+ }
29
+ return WasmSolver.__wrap(ret[0]);
30
+ }
31
+ /**
32
+ * Create a new solver instance by loading precomputed pruning tables from the provided byte slice.
33
+ * @param {Uint8Array} data
34
+ */
35
+ constructor(data) {
36
+ const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
37
+ const len0 = WASM_VECTOR_LEN;
38
+ const ret = wasm.wasmsolver_new_with_bytes(ptr0, len0);
39
+ if (ret[2]) {
40
+ throw takeFromExternrefTable0(ret[1]);
41
+ }
42
+ this.__wbg_ptr = ret[0];
43
+ WasmSolverFinalization.register(this, this.__wbg_ptr, this);
44
+ return this;
45
+ }
46
+ /**
47
+ * Solve a scramble given as a space-separated string (e.g. "R U R' F2").
48
+ * Returns the space-separated solution moves, or None/null if no solution is found or the scramble is invalid.
49
+ * @param {string} scramble
50
+ * @returns {string | undefined}
51
+ */
52
+ solve(scramble) {
53
+ const ptr0 = passStringToWasm0(scramble, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
54
+ const len0 = WASM_VECTOR_LEN;
55
+ const ret = wasm.wasmsolver_solve(this.__wbg_ptr, ptr0, len0);
56
+ let v2;
57
+ if (ret[0] !== 0) {
58
+ v2 = getStringFromWasm0(ret[0], ret[1]).slice();
59
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
60
+ }
61
+ return v2;
62
+ }
63
+ }
64
+ if (Symbol.dispose) WasmSolver.prototype[Symbol.dispose] = WasmSolver.prototype.free;
65
+ function __wbg_get_imports() {
66
+ const import0 = {
67
+ __proto__: null,
68
+ __wbg___wbindgen_throw_1506f2235d1bdba0: function(arg0, arg1) {
69
+ throw new Error(getStringFromWasm0(arg0, arg1));
70
+ },
71
+ __wbg_now_d4a620968a00fc98: function() {
72
+ const ret = Date.now();
73
+ return ret;
74
+ },
75
+ __wbindgen_cast_0000000000000001: function(arg0, arg1) {
76
+ // Cast intrinsic for `Ref(String) -> Externref`.
77
+ const ret = getStringFromWasm0(arg0, arg1);
78
+ return ret;
79
+ },
80
+ __wbindgen_init_externref_table: function() {
81
+ const table = wasm.__wbindgen_externrefs;
82
+ const offset = table.grow(4);
83
+ table.set(0, undefined);
84
+ table.set(offset + 0, undefined);
85
+ table.set(offset + 1, null);
86
+ table.set(offset + 2, true);
87
+ table.set(offset + 3, false);
88
+ },
89
+ };
90
+ return {
91
+ __proto__: null,
92
+ "./wasm_rubiks_solver_bg.js": import0,
93
+ };
94
+ }
95
+
96
+ const WasmSolverFinalization = (typeof FinalizationRegistry === 'undefined')
97
+ ? { register: () => {}, unregister: () => {} }
98
+ : new FinalizationRegistry(ptr => wasm.__wbg_wasmsolver_free(ptr, 1));
99
+
100
+ function getStringFromWasm0(ptr, len) {
101
+ return decodeText(ptr >>> 0, len);
102
+ }
103
+
104
+ let cachedUint8ArrayMemory0 = null;
105
+ function getUint8ArrayMemory0() {
106
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
107
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
108
+ }
109
+ return cachedUint8ArrayMemory0;
110
+ }
111
+
112
+ function passArray8ToWasm0(arg, malloc) {
113
+ const ptr = malloc(arg.length * 1, 1) >>> 0;
114
+ getUint8ArrayMemory0().set(arg, ptr / 1);
115
+ WASM_VECTOR_LEN = arg.length;
116
+ return ptr;
117
+ }
118
+
119
+ function passStringToWasm0(arg, malloc, realloc) {
120
+ if (realloc === undefined) {
121
+ const buf = cachedTextEncoder.encode(arg);
122
+ const ptr = malloc(buf.length, 1) >>> 0;
123
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
124
+ WASM_VECTOR_LEN = buf.length;
125
+ return ptr;
126
+ }
127
+
128
+ let len = arg.length;
129
+ let ptr = malloc(len, 1) >>> 0;
130
+
131
+ const mem = getUint8ArrayMemory0();
132
+
133
+ let offset = 0;
134
+
135
+ for (; offset < len; offset++) {
136
+ const code = arg.charCodeAt(offset);
137
+ if (code > 0x7F) break;
138
+ mem[ptr + offset] = code;
139
+ }
140
+ if (offset !== len) {
141
+ if (offset !== 0) {
142
+ arg = arg.slice(offset);
143
+ }
144
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
145
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
146
+ const ret = cachedTextEncoder.encodeInto(arg, view);
147
+
148
+ offset += ret.written;
149
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
150
+ }
151
+
152
+ WASM_VECTOR_LEN = offset;
153
+ return ptr;
154
+ }
155
+
156
+ function takeFromExternrefTable0(idx) {
157
+ const value = wasm.__wbindgen_externrefs.get(idx);
158
+ wasm.__externref_table_dealloc(idx);
159
+ return value;
160
+ }
161
+
162
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
163
+ cachedTextDecoder.decode();
164
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
165
+ let numBytesDecoded = 0;
166
+ function decodeText(ptr, len) {
167
+ numBytesDecoded += len;
168
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
169
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
170
+ cachedTextDecoder.decode();
171
+ numBytesDecoded = len;
172
+ }
173
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
174
+ }
175
+
176
+ const cachedTextEncoder = new TextEncoder();
177
+
178
+ if (!('encodeInto' in cachedTextEncoder)) {
179
+ cachedTextEncoder.encodeInto = function (arg, view) {
180
+ const buf = cachedTextEncoder.encode(arg);
181
+ view.set(buf);
182
+ return {
183
+ read: arg.length,
184
+ written: buf.length
185
+ };
186
+ };
187
+ }
188
+
189
+ let WASM_VECTOR_LEN = 0;
190
+
191
+ let wasmModule, wasmInstance, wasm;
192
+ function __wbg_finalize_init(instance, module) {
193
+ wasmInstance = instance;
194
+ wasm = instance.exports;
195
+ wasmModule = module;
196
+ cachedUint8ArrayMemory0 = null;
197
+ wasm.__wbindgen_start();
198
+ return wasm;
199
+ }
200
+
201
+ async function __wbg_load(module, imports) {
202
+ if (typeof Response === 'function' && module instanceof Response) {
203
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
204
+ try {
205
+ return await WebAssembly.instantiateStreaming(module, imports);
206
+ } catch (e) {
207
+ const validResponse = module.ok && expectedResponseType(module.type);
208
+
209
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
210
+ 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);
211
+
212
+ } else { throw e; }
213
+ }
214
+ }
215
+
216
+ const bytes = await module.arrayBuffer();
217
+ return await WebAssembly.instantiate(bytes, imports);
218
+ } else {
219
+ const instance = await WebAssembly.instantiate(module, imports);
220
+
221
+ if (instance instanceof WebAssembly.Instance) {
222
+ return { instance, module };
223
+ } else {
224
+ return instance;
225
+ }
226
+ }
227
+
228
+ function expectedResponseType(type) {
229
+ switch (type) {
230
+ case 'basic': case 'cors': case 'default': return true;
231
+ }
232
+ return false;
233
+ }
234
+ }
235
+
236
+ function initSync(module) {
237
+ if (wasm !== undefined) return wasm;
238
+
239
+
240
+ if (module !== undefined) {
241
+ if (Object.getPrototypeOf(module) === Object.prototype) {
242
+ ({module} = module)
243
+ } else {
244
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
245
+ }
246
+ }
247
+
248
+ const imports = __wbg_get_imports();
249
+ if (!(module instanceof WebAssembly.Module)) {
250
+ module = new WebAssembly.Module(module);
251
+ }
252
+ const instance = new WebAssembly.Instance(module, imports);
253
+ return __wbg_finalize_init(instance, module);
254
+ }
255
+
256
+ async function __wbg_init(module_or_path) {
257
+ if (wasm !== undefined) return wasm;
258
+
259
+
260
+ if (module_or_path !== undefined) {
261
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
262
+ ({module_or_path} = module_or_path)
263
+ } else {
264
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
265
+ }
266
+ }
267
+
268
+ if (module_or_path === undefined) {
269
+ module_or_path = new URL('wasm_rubiks_solver_bg.wasm', import.meta.url);
270
+ }
271
+ const imports = __wbg_get_imports();
272
+
273
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
274
+ module_or_path = fetch(module_or_path);
275
+ }
276
+
277
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
278
+
279
+ return __wbg_finalize_init(instance, module);
280
+ }
281
+
282
+ export { initSync, __wbg_init as default };
Binary file