@rybosome/tspice 0.0.3 → 0.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.
- package/README.md +145 -84
- package/backend-contract/dist/.tsbuildinfo +1 -1
- package/backend-contract/dist/domains/cells-windows.d.ts +94 -0
- package/backend-contract/dist/domains/cells-windows.js +10 -0
- package/backend-contract/dist/domains/coords-vectors.d.ts +53 -3
- package/backend-contract/dist/domains/dsk.d.ts +49 -0
- package/backend-contract/dist/domains/dsk.js +2 -0
- package/backend-contract/dist/domains/ek.d.ts +186 -0
- package/backend-contract/dist/domains/ek.js +8 -0
- package/backend-contract/dist/domains/ephemeris.d.ts +141 -3
- package/backend-contract/dist/domains/error.d.ts +42 -0
- package/backend-contract/dist/domains/error.js +33 -0
- package/backend-contract/dist/domains/file-io.d.ts +114 -0
- package/backend-contract/dist/domains/file-io.js +8 -0
- package/backend-contract/dist/domains/frames.d.ts +44 -4
- package/backend-contract/dist/domains/geometry-gf.d.ts +44 -0
- package/backend-contract/dist/domains/geometry-gf.js +14 -0
- package/backend-contract/dist/domains/geometry.d.ts +21 -1
- package/backend-contract/dist/domains/ids-names-normalize.d.ts +3 -0
- package/backend-contract/dist/domains/ids-names-normalize.js +74 -0
- package/backend-contract/dist/domains/ids-names.d.ts +37 -0
- package/backend-contract/dist/domains/kernel-pool.d.ts +134 -0
- package/backend-contract/dist/domains/kernel-pool.js +2 -0
- package/backend-contract/dist/domains/kernels-utils.d.ts +44 -0
- package/backend-contract/dist/domains/kernels-utils.js +265 -0
- package/backend-contract/dist/domains/kernels.d.ts +39 -3
- package/backend-contract/dist/domains/time.d.ts +102 -0
- package/backend-contract/dist/index.d.ts +34 -15
- package/backend-contract/dist/index.js +15 -1
- package/backend-contract/dist/shared/errors.d.ts +6 -0
- package/backend-contract/dist/shared/errors.js +8 -0
- package/backend-contract/dist/shared/mat3.d.ts +52 -0
- package/backend-contract/dist/shared/mat3.js +150 -0
- package/backend-contract/dist/shared/mat6.d.ts +34 -0
- package/backend-contract/dist/shared/mat6.js +116 -0
- package/backend-contract/dist/shared/spice-handles.d.ts +20 -0
- package/backend-contract/dist/shared/spice-handles.js +82 -0
- package/backend-contract/dist/shared/spice-int.d.ts +32 -0
- package/backend-contract/dist/shared/spice-int.js +41 -0
- package/backend-contract/dist/shared/types.d.ts +136 -5
- package/backend-contract/dist/shared/types.js +1 -1
- package/backend-contract/dist/shared/vec.d.ts +54 -0
- package/backend-contract/dist/shared/vec.js +162 -0
- package/backend-fake/dist/.tsbuildinfo +1 -1
- package/backend-fake/dist/index.d.ts +21 -1
- package/backend-fake/dist/index.js +1112 -33
- package/backend-node/dist/.tsbuildinfo +1 -1
- package/backend-node/dist/codec/arrays.d.ts +9 -0
- package/backend-node/dist/codec/arrays.js +36 -0
- package/backend-node/dist/codec/errors.d.ts +6 -6
- package/backend-node/dist/codec/errors.js +6 -6
- package/backend-node/dist/domains/cells-windows.d.ts +5 -0
- package/backend-node/dist/domains/cells-windows.js +112 -0
- package/backend-node/dist/domains/coords-vectors.d.ts +1 -0
- package/backend-node/dist/domains/coords-vectors.js +66 -0
- package/backend-node/dist/domains/dsk.d.ts +6 -0
- package/backend-node/dist/domains/dsk.js +108 -0
- package/backend-node/dist/domains/ek.d.ts +10 -0
- package/backend-node/dist/domains/ek.js +100 -0
- package/backend-node/dist/domains/ephemeris.d.ts +5 -1
- package/backend-node/dist/domains/ephemeris.js +150 -1
- package/backend-node/dist/domains/error.d.ts +5 -0
- package/backend-node/dist/domains/error.js +34 -0
- package/backend-node/dist/domains/file-io.d.ts +7 -0
- package/backend-node/dist/domains/file-io.js +105 -0
- package/backend-node/dist/domains/frames.d.ts +1 -0
- package/backend-node/dist/domains/frames.js +58 -6
- package/backend-node/dist/domains/geometry-gf.d.ts +5 -0
- package/backend-node/dist/domains/geometry-gf.js +74 -0
- package/backend-node/dist/domains/geometry.d.ts +1 -0
- package/backend-node/dist/domains/geometry.js +62 -0
- package/backend-node/dist/domains/ids-names.d.ts +2 -1
- package/backend-node/dist/domains/ids-names.js +30 -0
- package/backend-node/dist/domains/kernel-pool.d.ts +5 -0
- package/backend-node/dist/domains/kernel-pool.js +74 -0
- package/backend-node/dist/domains/kernels.d.ts +1 -0
- package/backend-node/dist/domains/kernels.js +100 -13
- package/backend-node/dist/domains/time.d.ts +1 -0
- package/backend-node/dist/domains/time.js +75 -1
- package/backend-node/dist/index.d.ts +5 -1
- package/backend-node/dist/index.js +62 -1
- package/backend-node/dist/lowlevel/binding.d.ts +3 -0
- package/backend-node/dist/lowlevel/binding.js +115 -0
- package/backend-node/dist/runtime/addon.d.ts +273 -2
- package/backend-node/dist/runtime/addon.js +3 -0
- package/backend-node/dist/runtime/kernel-staging.d.ts +17 -0
- package/backend-node/dist/runtime/kernel-staging.js +80 -7
- package/backend-node/dist/runtime/spice-handles.d.ts +3 -0
- package/backend-node/dist/runtime/spice-handles.js +2 -0
- package/backend-node/dist/runtime/virtual-output-staging.d.ts +16 -0
- package/backend-node/dist/runtime/virtual-output-staging.js +148 -0
- package/backend-wasm/dist/.tsbuildinfo +1 -1
- package/backend-wasm/dist/codec/alloc.d.ts +19 -0
- package/backend-wasm/dist/codec/alloc.js +64 -0
- package/backend-wasm/dist/codec/calls.d.ts +2 -0
- package/backend-wasm/dist/codec/calls.js +13 -24
- package/backend-wasm/dist/codec/errors.d.ts +6 -0
- package/backend-wasm/dist/codec/errors.js +34 -2
- package/backend-wasm/dist/codec/found.d.ts +2 -0
- package/backend-wasm/dist/codec/found.js +20 -43
- package/backend-wasm/dist/codec/strings.d.ts +31 -1
- package/backend-wasm/dist/codec/strings.js +93 -6
- package/backend-wasm/dist/domains/cells-windows.d.ts +9 -0
- package/backend-wasm/dist/domains/cells-windows.js +392 -0
- package/backend-wasm/dist/domains/coords-vectors.d.ts +1 -0
- package/backend-wasm/dist/domains/coords-vectors.js +377 -184
- package/backend-wasm/dist/domains/dsk.d.ts +6 -0
- package/backend-wasm/dist/domains/dsk.js +179 -0
- package/backend-wasm/dist/domains/ek.d.ts +6 -0
- package/backend-wasm/dist/domains/ek.js +543 -0
- package/backend-wasm/dist/domains/ephemeris.d.ts +4 -1
- package/backend-wasm/dist/domains/ephemeris.js +405 -46
- package/backend-wasm/dist/domains/error.d.ts +5 -0
- package/backend-wasm/dist/domains/error.js +109 -0
- package/backend-wasm/dist/domains/file-io.d.ts +7 -0
- package/backend-wasm/dist/domains/file-io.js +462 -0
- package/backend-wasm/dist/domains/frames.d.ts +1 -0
- package/backend-wasm/dist/domains/frames.js +139 -6
- package/backend-wasm/dist/domains/geometry-gf.d.ts +5 -0
- package/backend-wasm/dist/domains/geometry-gf.js +178 -0
- package/backend-wasm/dist/domains/geometry.d.ts +1 -0
- package/backend-wasm/dist/domains/geometry.js +210 -0
- package/backend-wasm/dist/domains/ids-names.d.ts +2 -1
- package/backend-wasm/dist/domains/ids-names.js +89 -0
- package/backend-wasm/dist/domains/kernel-pool.d.ts +5 -0
- package/backend-wasm/dist/domains/kernel-pool.js +357 -0
- package/backend-wasm/dist/domains/kernels.d.ts +1 -0
- package/backend-wasm/dist/domains/kernels.js +108 -4
- package/backend-wasm/dist/domains/time.d.ts +2 -0
- package/backend-wasm/dist/domains/time.js +235 -133
- package/backend-wasm/dist/index.d.ts +4 -2
- package/backend-wasm/dist/lowlevel/exports.d.ts +215 -1
- package/backend-wasm/dist/lowlevel/exports.js +217 -38
- package/backend-wasm/dist/runtime/create-backend-options.d.ts +21 -0
- package/backend-wasm/dist/runtime/create-backend.node.d.ts +11 -2
- package/backend-wasm/dist/runtime/create-backend.node.js +283 -14
- package/backend-wasm/dist/runtime/create-backend.web.d.ts +5 -2
- package/backend-wasm/dist/runtime/create-backend.web.js +40 -6
- package/backend-wasm/dist/runtime/fs.d.ts +6 -0
- package/backend-wasm/dist/runtime/fs.js +29 -3
- package/backend-wasm/dist/runtime/spice-handles.d.ts +3 -0
- package/backend-wasm/dist/runtime/spice-handles.js +2 -0
- package/backend-wasm/dist/runtime/virtual-outputs.d.ts +16 -0
- package/backend-wasm/dist/runtime/virtual-outputs.js +35 -0
- package/backend-wasm/dist/tspice_backend_wasm.node.js +3 -3
- package/backend-wasm/dist/tspice_backend_wasm.wasm +0 -0
- package/backend-wasm/dist/tspice_backend_wasm.web.js +1 -1
- package/core/dist/.tsbuildinfo +1 -1
- package/core/dist/index.d.ts +21 -0
- package/core/dist/index.js +57 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/backend.d.ts +15 -6
- package/dist/backend.js +3 -6
- package/dist/clients/createSpiceAsyncFromTransport.d.ts +5 -0
- package/dist/clients/createSpiceAsyncFromTransport.js +90 -0
- package/dist/clients/createSpiceSyncFromTransport.d.ts +5 -0
- package/dist/clients/createSpiceSyncFromTransport.js +88 -0
- package/dist/clients/spiceClients.d.ts +59 -0
- package/dist/clients/spiceClients.js +292 -0
- package/dist/errors.d.ts +4 -0
- package/dist/errors.js +4 -0
- package/dist/index.d.ts +12 -7
- package/dist/index.js +5 -2
- package/dist/kernels/defaultKernelPathFromUrl.d.ts +8 -0
- package/dist/kernels/defaultKernelPathFromUrl.js +32 -0
- package/dist/kernels/kernelPack.d.ts +88 -0
- package/dist/kernels/kernelPack.js +122 -0
- package/dist/kernels/kernels.d.ts +98 -0
- package/dist/kernels/kernels.js +217 -0
- package/dist/kernels/naifKernelId.d.ts +2 -0
- package/dist/kernels/naifKernelId.js +2 -0
- package/dist/kit/index.d.ts +4 -0
- package/dist/kit/index.js +3 -0
- package/dist/kit/math/mat3.d.ts +31 -0
- package/dist/kit/math/mat3.js +82 -0
- package/dist/kit/spice/create-kit.d.ts +12 -0
- package/dist/kit/spice/create-kit.js +23 -0
- package/dist/kit/spice/frames.d.ts +8 -0
- package/dist/kit/spice/frames.js +16 -0
- package/dist/kit/spice/kernels.d.ts +14 -0
- package/dist/kit/spice/kernels.js +39 -0
- package/dist/kit/spice/state.d.ts +7 -0
- package/dist/kit/spice/state.js +36 -0
- package/dist/kit/spice/time.d.ts +9 -0
- package/dist/kit/spice/time.js +31 -0
- package/dist/kit/types/spice-types.d.ts +51 -0
- package/dist/spice.d.ts +10 -1
- package/dist/spice.js +84 -72
- package/dist/transport/caching/policy.d.ts +16 -0
- package/dist/transport/caching/policy.js +77 -0
- package/dist/transport/caching/withCaching.d.ts +125 -0
- package/dist/transport/caching/withCaching.js +335 -0
- package/dist/transport/caching/withCachingSync.d.ts +24 -0
- package/dist/transport/caching/withCachingSync.js +161 -0
- package/dist/transport/rpc/protocol.d.ts +35 -0
- package/dist/transport/rpc/protocol.js +56 -0
- package/dist/transport/rpc/taskScheduling.d.ts +20 -0
- package/dist/transport/rpc/taskScheduling.js +98 -0
- package/dist/transport/rpc/valueCodec.d.ts +5 -0
- package/dist/transport/rpc/valueCodec.js +106 -0
- package/dist/transport/types.d.ts +7 -0
- package/dist/transport/types.js +2 -0
- package/dist/types.d.ts +8 -17
- package/dist/types.js +2 -1
- package/dist/worker/browser/createSpiceWorker.d.ts +22 -0
- package/dist/worker/browser/createSpiceWorker.js +41 -0
- package/dist/worker/browser/createSpiceWorkerClient.d.ts +40 -0
- package/dist/worker/browser/createSpiceWorkerClient.js +99 -0
- package/dist/worker/browser/spiceWorkerEntry.d.ts +2 -0
- package/dist/worker/browser/spiceWorkerEntry.js +129 -0
- package/dist/worker/browser/spiceWorkerInlineSource.d.ts +2 -0
- package/dist/worker/browser/spiceWorkerInlineSource.js +4 -0
- package/dist/worker/index.d.ts +10 -0
- package/dist/worker/index.js +7 -0
- package/dist/worker/transport/createWorkerTransport.d.ts +69 -0
- package/dist/worker/transport/createWorkerTransport.js +398 -0
- package/dist/worker/transport/exposeTransportToWorker.d.ts +51 -0
- package/dist/worker/transport/exposeTransportToWorker.js +196 -0
- package/package.json +4 -4
- package/dist/spice-types.d.ts +0 -36
- /package/dist/{spice-types.js → kit/types/spice-types.js} +0 -0
|
@@ -1,5 +1,37 @@
|
|
|
1
|
+
import { decodeWasmSpiceError, WASM_ERR_MAX_BYTES, withMalloc } from "./alloc.js";
|
|
2
|
+
function readLastErrorField(module, which) {
|
|
3
|
+
const outMaxBytes = WASM_ERR_MAX_BYTES;
|
|
4
|
+
return withMalloc(module, outMaxBytes, (outPtr) => {
|
|
5
|
+
module.HEAPU8[outPtr] = 0;
|
|
6
|
+
if (which === "short")
|
|
7
|
+
module._tspice_get_last_error_short(outPtr, outMaxBytes);
|
|
8
|
+
if (which === "long")
|
|
9
|
+
module._tspice_get_last_error_long(outPtr, outMaxBytes);
|
|
10
|
+
if (which === "trace")
|
|
11
|
+
module._tspice_get_last_error_trace(outPtr, outMaxBytes);
|
|
12
|
+
const s = module.UTF8ToString(outPtr, outMaxBytes).trim();
|
|
13
|
+
return s || undefined;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
/** Throw a JS Error for the last CSPICE error recorded in the WASM module. */
|
|
1
17
|
export function throwWasmSpiceError(module, errPtr, errMaxBytes, code) {
|
|
2
|
-
const message = module
|
|
3
|
-
|
|
18
|
+
const message = decodeWasmSpiceError(module, errPtr, errMaxBytes, code);
|
|
19
|
+
// Best-effort structured fields.
|
|
20
|
+
//
|
|
21
|
+
// The C shim stores SPICE fields out-of-band, so for non-CSPICE validation
|
|
22
|
+
// errors we must avoid accidentally attaching stale fields from a previous
|
|
23
|
+
// CSPICE failure.
|
|
24
|
+
const spiceShort = readLastErrorField(module, "short");
|
|
25
|
+
const shouldAttachSpiceFields = !!spiceShort && message.includes(spiceShort);
|
|
26
|
+
const spiceLong = shouldAttachSpiceFields ? readLastErrorField(module, "long") : undefined;
|
|
27
|
+
const spiceTrace = shouldAttachSpiceFields ? readLastErrorField(module, "trace") : undefined;
|
|
28
|
+
const err = new Error(message);
|
|
29
|
+
if (shouldAttachSpiceFields && spiceShort)
|
|
30
|
+
err.spiceShort = spiceShort;
|
|
31
|
+
if (shouldAttachSpiceFields && spiceLong)
|
|
32
|
+
err.spiceLong = spiceLong;
|
|
33
|
+
if (shouldAttachSpiceFields && spiceTrace)
|
|
34
|
+
err.spiceTrace = spiceTrace;
|
|
35
|
+
throw err;
|
|
4
36
|
}
|
|
5
37
|
//# sourceMappingURL=errors.js.map
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { Found } from "#backend-contract";
|
|
2
2
|
import type { EmscriptenModule } from "../lowlevel/exports.js";
|
|
3
|
+
/** Invoke a WASM function that follows the SPICE `found`-flag convention (int output + found flag). */
|
|
3
4
|
export declare function tspiceCallFoundInt(module: EmscriptenModule, fn: (argPtr: number, outIntPtr: number, foundPtr: number, errPtr: number, errMaxBytes: number) => number, arg: string): Found<{
|
|
4
5
|
value: number;
|
|
5
6
|
}>;
|
|
7
|
+
/** Invoke a WASM function that follows the SPICE `found`-flag convention (string output + found flag). */
|
|
6
8
|
export declare function tspiceCallFoundString(module: EmscriptenModule, fn: (code: number, outStrPtr: number, outStrMaxBytes: number, foundPtr: number, errPtr: number, errMaxBytes: number) => number, code: number): Found<{
|
|
7
9
|
value: string;
|
|
8
10
|
}>;
|
|
@@ -1,52 +1,34 @@
|
|
|
1
|
+
import { WASM_ERR_MAX_BYTES, withAllocs } from "./alloc.js";
|
|
1
2
|
import { throwWasmSpiceError } from "./errors.js";
|
|
2
3
|
import { writeUtf8CString } from "./strings.js";
|
|
4
|
+
/** Invoke a WASM function that follows the SPICE `found`-flag convention (int output + found flag). */
|
|
3
5
|
export function tspiceCallFoundInt(module, fn, arg) {
|
|
4
|
-
const errMaxBytes =
|
|
5
|
-
const errPtr = module._malloc(errMaxBytes);
|
|
6
|
+
const errMaxBytes = WASM_ERR_MAX_BYTES;
|
|
6
7
|
const argPtr = writeUtf8CString(module, arg);
|
|
7
|
-
const outPtr = module._malloc(4);
|
|
8
|
-
const foundPtr = module._malloc(4);
|
|
9
|
-
if (!errPtr || !argPtr || !outPtr || !foundPtr) {
|
|
10
|
-
for (const ptr of [foundPtr, outPtr, argPtr, errPtr]) {
|
|
11
|
-
if (ptr)
|
|
12
|
-
module._free(ptr);
|
|
13
|
-
}
|
|
14
|
-
throw new Error("WASM malloc failed");
|
|
15
|
-
}
|
|
16
8
|
try {
|
|
17
|
-
module
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
9
|
+
return withAllocs(module, [errMaxBytes, 4, 4], (errPtr, outPtr, foundPtr) => {
|
|
10
|
+
module.HEAP32[outPtr >> 2] = 0;
|
|
11
|
+
module.HEAP32[foundPtr >> 2] = 0;
|
|
12
|
+
const result = fn(argPtr, outPtr, foundPtr, errPtr, errMaxBytes);
|
|
13
|
+
if (result !== 0) {
|
|
14
|
+
throwWasmSpiceError(module, errPtr, errMaxBytes, result);
|
|
15
|
+
}
|
|
16
|
+
const found = (module.HEAP32[foundPtr >> 2] ?? 0) !== 0;
|
|
17
|
+
if (!found) {
|
|
18
|
+
return { found: false };
|
|
19
|
+
}
|
|
20
|
+
return { found: true, value: module.HEAP32[outPtr >> 2] ?? 0 };
|
|
21
|
+
});
|
|
28
22
|
}
|
|
29
23
|
finally {
|
|
30
|
-
module._free(foundPtr);
|
|
31
|
-
module._free(outPtr);
|
|
32
24
|
module._free(argPtr);
|
|
33
|
-
module._free(errPtr);
|
|
34
25
|
}
|
|
35
26
|
}
|
|
27
|
+
/** Invoke a WASM function that follows the SPICE `found`-flag convention (string output + found flag). */
|
|
36
28
|
export function tspiceCallFoundString(module, fn, code) {
|
|
37
|
-
const errMaxBytes =
|
|
38
|
-
const errPtr = module._malloc(errMaxBytes);
|
|
29
|
+
const errMaxBytes = WASM_ERR_MAX_BYTES;
|
|
39
30
|
const outMaxBytes = 256;
|
|
40
|
-
|
|
41
|
-
const foundPtr = module._malloc(4);
|
|
42
|
-
if (!errPtr || !outPtr || !foundPtr) {
|
|
43
|
-
for (const ptr of [foundPtr, outPtr, errPtr]) {
|
|
44
|
-
if (ptr)
|
|
45
|
-
module._free(ptr);
|
|
46
|
-
}
|
|
47
|
-
throw new Error("WASM malloc failed");
|
|
48
|
-
}
|
|
49
|
-
try {
|
|
31
|
+
return withAllocs(module, [errMaxBytes, outMaxBytes, 4], (errPtr, outPtr, foundPtr) => {
|
|
50
32
|
module.HEAPU8[outPtr] = 0;
|
|
51
33
|
module.HEAP32[foundPtr >> 2] = 0;
|
|
52
34
|
const result = fn(code, outPtr, outMaxBytes, foundPtr, errPtr, errMaxBytes);
|
|
@@ -58,11 +40,6 @@ export function tspiceCallFoundString(module, fn, code) {
|
|
|
58
40
|
return { found: false };
|
|
59
41
|
}
|
|
60
42
|
return { found: true, value: module.UTF8ToString(outPtr, outMaxBytes).trim() };
|
|
61
|
-
}
|
|
62
|
-
finally {
|
|
63
|
-
module._free(foundPtr);
|
|
64
|
-
module._free(outPtr);
|
|
65
|
-
module._free(errPtr);
|
|
66
|
-
}
|
|
43
|
+
});
|
|
67
44
|
}
|
|
68
45
|
//# sourceMappingURL=found.js.map
|
|
@@ -1,3 +1,33 @@
|
|
|
1
1
|
import type { EmscriptenModule } from "../lowlevel/exports.js";
|
|
2
|
-
|
|
2
|
+
type Utf8CStringWriteModule = Pick<EmscriptenModule, "_malloc" | "HEAPU8">;
|
|
3
|
+
type Utf8CStringArrayWriteModule = Pick<EmscriptenModule, "_malloc" | "_free" | "HEAPU8" | "HEAPU32">;
|
|
4
|
+
type Utf8CStringArrayFreeModule = Pick<EmscriptenModule, "_free">;
|
|
5
|
+
type Utf8CStringArray = {
|
|
6
|
+
/** Pointer to a contiguous `char*[]` array (written via `HEAPU32`), or `0` for empty arrays. */
|
|
7
|
+
ptr: number;
|
|
8
|
+
/** Pointers to each allocated null-terminated string. */
|
|
9
|
+
itemPtrs: number[];
|
|
10
|
+
};
|
|
11
|
+
/** Allocate and write a null-terminated UTF-8 C string into the WASM heap; returns the pointer. */
|
|
12
|
+
export declare function writeUtf8CString(module: Utf8CStringWriteModule, value: string): number;
|
|
13
|
+
/**
|
|
14
|
+
* Allocate a `char*[]` array (4-byte pointers) and a null-terminated utf-8 string for each
|
|
15
|
+
* item.
|
|
16
|
+
*
|
|
17
|
+
* The caller owns the returned memory and must free it with {@link freeUtf8CStringArray}.
|
|
18
|
+
*/
|
|
19
|
+
export declare function writeUtf8CStringArray(module: Utf8CStringArrayWriteModule, values: string[]): Utf8CStringArray;
|
|
20
|
+
/** Free memory allocated by {@link writeUtf8CStringArray} (idempotent). */
|
|
21
|
+
export declare function freeUtf8CStringArray(module: Utf8CStringArrayFreeModule, arr: Utf8CStringArray): void;
|
|
22
|
+
/**
|
|
23
|
+
* Reads a fixed-width C string (padded/truncated) from the WASM heap.
|
|
24
|
+
*
|
|
25
|
+
* - Decodes at most `width` bytes.
|
|
26
|
+
* - Treats the first `\0` byte as the terminator (embedded NUL ends the string).
|
|
27
|
+
* - Trims *right* whitespace only (SPICE commonly pads with trailing spaces).
|
|
28
|
+
*/
|
|
29
|
+
export declare function readFixedWidthCString(module: Pick<EmscriptenModule, "HEAPU8">, ptr: number, width: number): string;
|
|
30
|
+
/** Read an array of fixed-width C strings from the WASM heap (see {@link readFixedWidthCString}). */
|
|
31
|
+
export declare function readFixedWidthCStringArray(module: Pick<EmscriptenModule, "HEAPU8">, ptr: number, count: number, width: number): string[];
|
|
32
|
+
export {};
|
|
3
33
|
//# sourceMappingURL=strings.d.ts.map
|
|
@@ -1,12 +1,99 @@
|
|
|
1
|
+
import { mallocOrThrow } from "./alloc.js";
|
|
2
|
+
// Re-use a shared decoder to avoid allocating one per call.
|
|
3
|
+
const UTF8_DECODER = new TextDecoder();
|
|
4
|
+
// Re-use a shared encoder to avoid allocating one per call.
|
|
5
|
+
const UTF8_ENCODER = new TextEncoder();
|
|
6
|
+
/** Allocate and write a null-terminated UTF-8 C string into the WASM heap; returns the pointer. */
|
|
1
7
|
export function writeUtf8CString(module, value) {
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
const ptr = module._malloc(encoded.length + 1);
|
|
5
|
-
if (!ptr) {
|
|
6
|
-
throw new Error("WASM malloc failed");
|
|
7
|
-
}
|
|
8
|
+
const encoded = UTF8_ENCODER.encode(value);
|
|
9
|
+
const ptr = mallocOrThrow(module, encoded.length + 1);
|
|
8
10
|
module.HEAPU8.set(encoded, ptr);
|
|
9
11
|
module.HEAPU8[ptr + encoded.length] = 0;
|
|
10
12
|
return ptr;
|
|
11
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Allocate a `char*[]` array (4-byte pointers) and a null-terminated utf-8 string for each
|
|
16
|
+
* item.
|
|
17
|
+
*
|
|
18
|
+
* The caller owns the returned memory and must free it with {@link freeUtf8CStringArray}.
|
|
19
|
+
*/
|
|
20
|
+
export function writeUtf8CStringArray(module, values) {
|
|
21
|
+
if (values.length === 0) {
|
|
22
|
+
return { ptr: 0, itemPtrs: [] };
|
|
23
|
+
}
|
|
24
|
+
// This helper intentionally targets wasm32 (32-bit pointers).
|
|
25
|
+
// If we ever add wasm64, this should be revisited to use the correct pointer heap.
|
|
26
|
+
const ptrBytes = module.HEAPU32.BYTES_PER_ELEMENT;
|
|
27
|
+
if (ptrBytes !== 4) {
|
|
28
|
+
throw new Error('writeUtf8CStringArray assumes 32-bit pointers (wasm32).');
|
|
29
|
+
}
|
|
30
|
+
const arr = { ptr: 0, itemPtrs: [] };
|
|
31
|
+
arr.ptr = mallocOrThrow(module, values.length * ptrBytes);
|
|
32
|
+
try {
|
|
33
|
+
if (arr.ptr % ptrBytes !== 0) {
|
|
34
|
+
throw new Error(`Internal error: unaligned pointer array base pointer (ptr=${arr.ptr}, ptrBytes=${ptrBytes})`);
|
|
35
|
+
}
|
|
36
|
+
const baseIndex = arr.ptr / ptrBytes;
|
|
37
|
+
// IMPORTANT: With `ALLOW_MEMORY_GROWTH=1`, any allocation (including the `_malloc()`
|
|
38
|
+
// inside `writeUtf8CString()`) may grow the WASM memory and cause Emscripten to
|
|
39
|
+
// recreate the `HEAP*` typed array views. Do not cache `module.HEAPU8` /
|
|
40
|
+
// `module.HEAPU32` across allocations; always access them at point-of-use after
|
|
41
|
+
// the allocation completes.
|
|
42
|
+
for (let i = 0; i < values.length; i++) {
|
|
43
|
+
const itemPtr = writeUtf8CString(module, values[i]);
|
|
44
|
+
// Safety: HEAPU32 stores unsigned 32-bit pointers; guard against accidental truncation.
|
|
45
|
+
if (!Number.isInteger(itemPtr) || itemPtr < 0 || itemPtr > 0xffff_ffff) {
|
|
46
|
+
throw new Error(`Internal error: string item pointer out of u32 range (itemPtr=${itemPtr})`);
|
|
47
|
+
}
|
|
48
|
+
arr.itemPtrs.push(itemPtr);
|
|
49
|
+
module.HEAPU32[baseIndex + i] = itemPtr;
|
|
50
|
+
}
|
|
51
|
+
return arr;
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
freeUtf8CStringArray(module, arr);
|
|
55
|
+
throw e;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** Free memory allocated by {@link writeUtf8CStringArray} (idempotent). */
|
|
59
|
+
export function freeUtf8CStringArray(module, arr) {
|
|
60
|
+
for (const itemPtr of arr.itemPtrs) {
|
|
61
|
+
if (itemPtr) {
|
|
62
|
+
module._free(itemPtr);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
// Make the helper idempotent (safe to call twice).
|
|
66
|
+
arr.itemPtrs.length = 0;
|
|
67
|
+
if (arr.ptr) {
|
|
68
|
+
module._free(arr.ptr);
|
|
69
|
+
}
|
|
70
|
+
arr.ptr = 0;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Reads a fixed-width C string (padded/truncated) from the WASM heap.
|
|
74
|
+
*
|
|
75
|
+
* - Decodes at most `width` bytes.
|
|
76
|
+
* - Treats the first `\0` byte as the terminator (embedded NUL ends the string).
|
|
77
|
+
* - Trims *right* whitespace only (SPICE commonly pads with trailing spaces).
|
|
78
|
+
*/
|
|
79
|
+
export function readFixedWidthCString(module, ptr, width) {
|
|
80
|
+
if (width <= 0) {
|
|
81
|
+
return "";
|
|
82
|
+
}
|
|
83
|
+
const bytes = module.HEAPU8.subarray(ptr, ptr + width);
|
|
84
|
+
const nulIndex = bytes.indexOf(0);
|
|
85
|
+
const end = nulIndex === -1 ? bytes.length : nulIndex;
|
|
86
|
+
return UTF8_DECODER.decode(bytes.subarray(0, end)).trimEnd();
|
|
87
|
+
}
|
|
88
|
+
/** Read an array of fixed-width C strings from the WASM heap (see {@link readFixedWidthCString}). */
|
|
89
|
+
export function readFixedWidthCStringArray(module, ptr, count, width) {
|
|
90
|
+
if (count <= 0 || width <= 0) {
|
|
91
|
+
return [];
|
|
92
|
+
}
|
|
93
|
+
const out = new Array(count);
|
|
94
|
+
for (let i = 0; i < count; i++) {
|
|
95
|
+
out[i] = readFixedWidthCString(module, ptr + i * width, width);
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
12
99
|
//# sourceMappingURL=strings.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { CellsWindowsApi } from "#backend-contract";
|
|
2
|
+
import type { EmscriptenModule } from "../lowlevel/exports.js";
|
|
3
|
+
/** Assert that a cell handle was allocated by (and is still owned by) this WASM backend instance. */
|
|
4
|
+
export declare function assertWasmOwnedCellHandle(module: EmscriptenModule, handle: number, context: string): void;
|
|
5
|
+
/** Assert that a window handle was allocated by (and is still owned by) this WASM backend instance. */
|
|
6
|
+
export declare function assertWasmOwnedWindowHandle(module: EmscriptenModule, handle: number, context: string): void;
|
|
7
|
+
/** Create a {@link CellsWindowsApi} implementation backed by a WASM Emscripten module. */
|
|
8
|
+
export declare function createCellsWindowsApi(module: EmscriptenModule): CellsWindowsApi;
|
|
9
|
+
//# sourceMappingURL=cells-windows.d.ts.map
|