sh-ast 0.3.0 → 0.4.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/README.md +165 -133
- package/dist/analyze/ansi-c-escapes.d.ts +1 -1
- package/dist/analyze/ansi-c-escapes.js +1 -1
- package/dist/analyze/enumerate-commands.d.ts +11 -16
- package/dist/analyze/enumerate-commands.d.ts.map +1 -1
- package/dist/analyze/enumerate-commands.js +82 -92
- package/dist/analyze/enumerate-commands.js.map +1 -1
- package/dist/analyze/index.d.ts +3 -2
- package/dist/analyze/index.d.ts.map +1 -1
- package/dist/analyze/index.js +1 -1
- package/dist/analyze/node-helpers.d.ts +10 -21
- package/dist/analyze/node-helpers.d.ts.map +1 -1
- package/dist/analyze/node-helpers.js +16 -27
- package/dist/analyze/node-helpers.js.map +1 -1
- package/dist/analyze/resolve-argv0.d.ts +6 -6
- package/dist/analyze/resolve-argv0.js +6 -6
- package/dist/analyze/resolve-word.d.ts +7 -10
- package/dist/analyze/resolve-word.d.ts.map +1 -1
- package/dist/analyze/resolve-word.js +39 -22
- package/dist/analyze/resolve-word.js.map +1 -1
- package/dist/analyze.d.ts +3780 -397
- package/dist/errors.d.ts +6 -79
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +5 -64
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +16 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -4
- package/dist/index.js.map +1 -1
- package/dist/sh-ast.d.ts +3503 -1027
- package/dist/syntax-errors.d.ts +56 -0
- package/dist/syntax-errors.d.ts.map +1 -0
- package/dist/syntax-errors.js +80 -0
- package/dist/syntax-errors.js.map +1 -0
- package/dist/syntax-snapshot.d.ts +47 -0
- package/dist/syntax-snapshot.d.ts.map +1 -0
- package/dist/syntax-snapshot.js +121 -0
- package/dist/syntax-snapshot.js.map +1 -0
- package/dist/syntax-wire.d.ts +31 -0
- package/dist/syntax-wire.d.ts.map +1 -0
- package/dist/syntax-wire.js +202 -0
- package/dist/syntax-wire.js.map +1 -0
- package/dist/syntax.d.ts +65 -0
- package/dist/syntax.d.ts.map +1 -0
- package/dist/syntax.js +152 -0
- package/dist/syntax.js.map +1 -0
- package/dist/wasm-instance.d.ts +5 -8
- package/dist/wasm-instance.d.ts.map +1 -1
- package/dist/wasm-instance.js +44 -47
- package/dist/wasm-instance.js.map +1 -1
- package/generated/syntax-constants.d.ts +1652 -0
- package/generated/syntax-constants.js +1384 -0
- package/generated/syntax-schema.d.ts +60 -0
- package/generated/syntax-schema.js +1740 -0
- package/generated/syntax-types.d.ts +1977 -0
- package/package.json +2 -2
- package/shim/sh-ast.wasm +0 -0
- package/dist/normalize.d.ts +0 -52
- package/dist/normalize.d.ts.map +0 -1
- package/dist/normalize.js +0 -260
- package/dist/normalize.js.map +0 -1
- package/dist/parse.d.ts +0 -50
- package/dist/parse.d.ts.map +0 -1
- package/dist/parse.js +0 -125
- package/dist/parse.js.map +0 -1
- package/dist/types.d.ts +0 -74
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- package/dist/visitor-keys.d.ts +0 -21
- package/dist/visitor-keys.d.ts.map +0 -1
- package/dist/visitor-keys.js +0 -23
- package/dist/visitor-keys.js.map +0 -1
- package/dist/walk.d.ts +0 -18
- package/dist/walk.d.ts.map +0 -1
- package/dist/walk.js +0 -40
- package/dist/walk.js.map +0 -1
- package/generated/child-type-schema.d.ts +0 -14
- package/generated/child-type-schema.js +0 -188
- package/generated/node-types.d.ts +0 -1002
- package/generated/position-fields.d.ts +0 -21
- package/generated/position-fields.js +0 -50
- package/generated/visitor-keys.d.ts +0 -11
- package/generated/visitor-keys.js +0 -50
package/dist/syntax.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synchronous access to the pinned upstream syntax model. Parsing and derived
|
|
3
|
+
* node bounds run in Go; this module transports exact data and adapts errors,
|
|
4
|
+
* leaving ESLint locations, traversal, and policy to consumers.
|
|
5
|
+
*/
|
|
6
|
+
import { SYNTAX_SCHEMA } from '../generated/syntax-schema.js';
|
|
7
|
+
import { ShInternalError } from './errors.js';
|
|
8
|
+
import { assertParseDepthWithinLimit } from './parse-depth-guard.js';
|
|
9
|
+
import { InputError, LangError, ParseError } from './syntax-errors.js';
|
|
10
|
+
import { assertUnicodeString, decodeWire, encodeWire, isGoString, isRecord, isSyntaxFile, isSyntaxNode, isSyntaxPos, } from './syntax-wire.js';
|
|
11
|
+
import { callSyntax } from './wasm-instance.js';
|
|
12
|
+
import { createPositionSnapshot } from './syntax-snapshot.js';
|
|
13
|
+
export { InputError, LangError, ParseError } from './syntax-errors.js';
|
|
14
|
+
/**
|
|
15
|
+
* Parses exact source bytes, or well-formed Unicode text encoded as UTF-8. First
|
|
16
|
+
* use synchronously initializes WASM. The existing conservative nesting guard is
|
|
17
|
+
* an explicit binding resource limit, not an upstream grammar restriction.
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
export function parseSync(source, options = {}) {
|
|
21
|
+
return invokeParse('parse', source, options).file;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Parses once while observing every real node's upstream bounds in Go. The
|
|
25
|
+
* separate immutable lookup describes this original parse; it never replaces
|
|
26
|
+
* live nodePos/nodeEnd or tracks edits. Source adapters avoid reconstructing
|
|
27
|
+
* each descendant subtree just to display the original source locations.
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export function parseWithPositionsSync(source, options = {}) {
|
|
31
|
+
const { file, response } = invokeParse('parse-with-positions', source, options);
|
|
32
|
+
return Object.freeze({ File: file, Positions: createPositionSnapshot(file, response.Positions) });
|
|
33
|
+
}
|
|
34
|
+
/** Shared source/options/error semantics for both whole-file parsing operations. */
|
|
35
|
+
function invokeParse(operation, source, options) {
|
|
36
|
+
if (typeof source !== 'string' && !(source instanceof Uint8Array))
|
|
37
|
+
throw new InputError('Source must be a string or Uint8Array');
|
|
38
|
+
if (typeof source === 'string')
|
|
39
|
+
assertUnicodeString(source);
|
|
40
|
+
const bytes = typeof source === 'string' ? new TextEncoder().encode(source) : source;
|
|
41
|
+
// Decoding here supplies only an ASCII-structure risk estimate. The original
|
|
42
|
+
// bytes, including malformed UTF-8, are always sent unchanged to the parser.
|
|
43
|
+
assertParseDepthWithinLimit(typeof source === 'string' ? source : new TextDecoder().decode(source));
|
|
44
|
+
validateOptions(options);
|
|
45
|
+
const result = invoke(operation, bytes, encodeWire(options));
|
|
46
|
+
if (!isSyntaxFile(result.File))
|
|
47
|
+
throw new ShInternalError('The syntax parser returned an incomplete or invalid File record');
|
|
48
|
+
return { file: result.File, response: result };
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Computes a node's starting position from its current fields using upstream's
|
|
52
|
+
* Pos method. No cached parse-time position can override subsequent tree edits.
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
export function nodePos(node) {
|
|
56
|
+
return positionOf('pos', node);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Computes a node's ending position using upstream's End method, including
|
|
60
|
+
* recovered/invalid behavior and changes to text or child nodes.
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
63
|
+
export function nodeEnd(node) {
|
|
64
|
+
return positionOf('end', node);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Constructs an upstream position. uint64 arguments use bigint when outside the
|
|
68
|
+
* safe-number range; upstream clamps offsets and zeroes overflowing line/column
|
|
69
|
+
* values. The binding does not replace those rules with its own range policy.
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
72
|
+
export function newPos(offset, line, column) {
|
|
73
|
+
const result = invoke('newpos', new TextEncoder().encode(encodeWire({ Offset: offset, Line: line, Col: column })));
|
|
74
|
+
if (!isSyntaxPos(result.Position))
|
|
75
|
+
throw new ShInternalError('The syntax constructor returned an invalid position record');
|
|
76
|
+
return result.Position;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Represents an unnamed value in an upstream uint32 operator family. Named
|
|
80
|
+
* constants remain precise literal unions; this explicit path preserves other Go
|
|
81
|
+
* values without claiming an operation such as Print accepts them.
|
|
82
|
+
* @public
|
|
83
|
+
*/
|
|
84
|
+
export function rawOperator(family, value) {
|
|
85
|
+
const knownFamily = Object.values(SYNTAX_SCHEMA).some((record) => Object.values(record.fields).some((field) => field.kind === 'operator' && field.type === family));
|
|
86
|
+
if (!knownFamily || !Number.isInteger(value) || value < 0 || value > 0xffff_ffff) {
|
|
87
|
+
throw new InputError('An operator value must belong to a known family and be an integral uint32');
|
|
88
|
+
}
|
|
89
|
+
// The runtime check establishes the opaque numeric brand; no value is changed.
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
/** Exact Go options are validated without silently ignoring misspelled keys. */
|
|
93
|
+
function validateOptions(options) {
|
|
94
|
+
if (!isRecord(options))
|
|
95
|
+
throw new InputError('Parser options must be a data record');
|
|
96
|
+
const allowed = new Set(['Filename', 'KeepComments', 'Variant', 'StopAt', 'RecoverErrors']);
|
|
97
|
+
for (const [name, value] of Object.entries(options)) {
|
|
98
|
+
if (!allowed.has(name))
|
|
99
|
+
throw new InputError(`Unknown parser option: ${name}`);
|
|
100
|
+
if ((name === 'Filename' || name === 'StopAt') && !isGoString(value))
|
|
101
|
+
throw new InputError(`${name} must be a string or byte array`);
|
|
102
|
+
if (name === 'KeepComments' && typeof value !== 'boolean')
|
|
103
|
+
throw new InputError('KeepComments must be boolean');
|
|
104
|
+
if ((name === 'Variant' || name === 'RecoverErrors') &&
|
|
105
|
+
typeof value !== 'bigint' &&
|
|
106
|
+
(typeof value !== 'number' || !Number.isSafeInteger(value))) {
|
|
107
|
+
throw new InputError(`${name} must be a safe integer or bigint`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/** Only the requested upstream method runs; partial nodes can support one bound. */
|
|
112
|
+
function positionOf(operation, node) {
|
|
113
|
+
if (!isSyntaxNode(node))
|
|
114
|
+
throw new InputError('Expected a complete, acyclic upstream Node record');
|
|
115
|
+
const result = invoke(operation, new TextEncoder().encode(encodeWire(node)));
|
|
116
|
+
if (!isSyntaxPos(result.Position))
|
|
117
|
+
throw new ShInternalError('The syntax bridge returned an invalid derived position');
|
|
118
|
+
return result.Position;
|
|
119
|
+
}
|
|
120
|
+
/** Decodes one complete response before translating its upstream failure kind. */
|
|
121
|
+
function invoke(operation, payload, options = '{}') {
|
|
122
|
+
const result = decodeWire(callSyntax(operation, payload, options));
|
|
123
|
+
if (!isRecord(result))
|
|
124
|
+
throw new ShInternalError('The syntax bridge returned a non-record response');
|
|
125
|
+
if (result.Error !== undefined) {
|
|
126
|
+
const error = result.Error;
|
|
127
|
+
if (!isRecord(error) || !isGoString(error.Message))
|
|
128
|
+
throw new ShInternalError('The syntax bridge returned a malformed error');
|
|
129
|
+
if (error.Kind === 'InputError')
|
|
130
|
+
throw new InputError(error.Message);
|
|
131
|
+
if (error.Kind === 'InternalError')
|
|
132
|
+
throw new ShInternalError(typeof error.Message === 'string' ? error.Message : new TextDecoder().decode(error.Message));
|
|
133
|
+
const file = result.File === null ? null : isSyntaxFile(result.File) ? result.File : undefined;
|
|
134
|
+
if (file === undefined || !isGoString(error.Filename) || !isSyntaxPos(error.Pos))
|
|
135
|
+
throw new ShInternalError('The syntax bridge dropped a partial result or error position');
|
|
136
|
+
if (error.Kind === 'ParseError' &&
|
|
137
|
+
isGoString(error.Text) &&
|
|
138
|
+
typeof error.Incomplete === 'boolean') {
|
|
139
|
+
throw new ParseError(error.Message, error.Filename, error.Pos, error.Text, error.Incomplete, file);
|
|
140
|
+
}
|
|
141
|
+
if (error.Kind === 'LangError' &&
|
|
142
|
+
isGoString(error.Feature) &&
|
|
143
|
+
Array.isArray(error.Langs) &&
|
|
144
|
+
error.Langs.every((lang) => typeof lang === 'number' && Number.isSafeInteger(lang)) &&
|
|
145
|
+
typeof error.LangUsed === 'number') {
|
|
146
|
+
throw new LangError(error.Message, error.Filename, error.Pos, error.Feature, error.Langs, error.LangUsed, file);
|
|
147
|
+
}
|
|
148
|
+
throw new ShInternalError('The syntax bridge returned an unknown or incomplete upstream error');
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
//# sourceMappingURL=syntax.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"syntax.js","sourceRoot":"","sources":["../src/syntax.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAG9D,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,2BAA2B,EAAE,MAAM,wBAAwB,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACvE,OAAO,EACL,mBAAmB,EACnB,UAAU,EACV,UAAU,EACV,UAAU,EACV,QAAQ,EACR,YAAY,EACZ,YAAY,EACZ,WAAW,GACZ,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,sBAAsB,EAAmB,MAAM,sBAAsB,CAAC;AAG/E,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAqBvE;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAAC,MAA2B,EAAE,UAAwB,EAAE;IAC/E,OAAO,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC;AACpD,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,sBAAsB,CACpC,MAA2B,EAC3B,UAAwB,EAAE;IAE1B,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,WAAW,CAAC,sBAAsB,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IAChF,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,IA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*
|
|
7
|
-
* cost — see design/ARCHITECTURE.md's "Performance"), and frees every buffer
|
|
8
|
-
* the call allocated.
|
|
9
|
-
*
|
|
3
|
+
* Calls the lossless syntax protocol synchronously with byte-exact payloads.
|
|
4
|
+
* Every returned allocation is released even when a later allocation, exported
|
|
5
|
+
* operation, or response validation fails. Views are recreated after each call
|
|
6
|
+
* because Go allocation can grow and detach linear memory.
|
|
10
7
|
* @internal
|
|
11
8
|
*/
|
|
12
|
-
export declare function
|
|
9
|
+
export declare function callSyntax(operation: string, payload: Uint8Array, options?: string): string;
|
|
13
10
|
//# sourceMappingURL=wasm-instance.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wasm-instance.d.ts","sourceRoot":"","sources":["../src/wasm-instance.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"wasm-instance.d.ts","sourceRoot":"","sources":["../src/wasm-instance.ts"],"names":[],"mappings":"AAIA,OAAO,sBAAsB,CAAC;AAsE9B;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,SAAO,GAAG,MAAM,CAkCzF"}
|
package/dist/wasm-instance.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/** Synchronous singleton WASM ownership and byte-exact syntax protocol transport. */
|
|
1
2
|
import fs from 'node:fs';
|
|
2
3
|
import path from 'node:path';
|
|
3
4
|
import { fileURLToPath } from 'node:url';
|
|
@@ -6,19 +7,18 @@ import { ShInternalError } from './errors.js';
|
|
|
6
7
|
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
7
8
|
// `shim/` is a sibling of both `src/` (test-time) and `dist/` (build-time).
|
|
8
9
|
const wasmPath = path.join(here, '..', 'shim', 'sh-ast.wasm');
|
|
10
|
+
/** Validates shared memory ownership primitives before any buffer crosses the bridge. */
|
|
9
11
|
function isShimExports(value) {
|
|
10
12
|
const candidate = value;
|
|
11
13
|
return (candidate.mem instanceof WebAssembly.Memory &&
|
|
12
14
|
typeof candidate.alloc === 'function' &&
|
|
13
|
-
typeof candidate.free === 'function'
|
|
14
|
-
typeof candidate.process === 'function');
|
|
15
|
+
typeof candidate.free === 'function');
|
|
15
16
|
}
|
|
16
17
|
let wasmModule;
|
|
17
18
|
let shimExports;
|
|
18
19
|
/**
|
|
19
20
|
* Instantiates the WASM shim synchronously in Node, exactly once, and
|
|
20
|
-
* reuses the instance across parses
|
|
21
|
-
* "Synchronous parse: solved, two mechanisms"). Node imposes no size limit
|
|
21
|
+
* reuses the instance across parses and position operations. Node imposes no size limit
|
|
22
22
|
* on synchronous `WebAssembly.Module` compilation — that restriction is
|
|
23
23
|
* browser-main-thread-only.
|
|
24
24
|
*/
|
|
@@ -30,7 +30,7 @@ function ensureInstance() {
|
|
|
30
30
|
const instance = new WebAssembly.Instance(wasmModule, go.importObject);
|
|
31
31
|
void go.run(instance);
|
|
32
32
|
if (!isShimExports(instance.exports)) {
|
|
33
|
-
throw new ShInternalError('bridge: WASM shim did not export the expected alloc/
|
|
33
|
+
throw new ShInternalError('bridge: WASM shim did not export the expected alloc/free/mem linear-memory ABI');
|
|
34
34
|
}
|
|
35
35
|
shimExports = instance.exports;
|
|
36
36
|
return shimExports;
|
|
@@ -38,51 +38,48 @@ function ensureInstance() {
|
|
|
38
38
|
const encoder = new TextEncoder();
|
|
39
39
|
const decoder = new TextDecoder();
|
|
40
40
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
|
|
45
|
-
function writeString(exports, text) {
|
|
46
|
-
const bytes = encoder.encode(text);
|
|
47
|
-
const ptr = exports.alloc(bytes.length);
|
|
48
|
-
// `exports.mem.buffer` is re-read here rather than cached across calls:
|
|
49
|
-
// a Go-side allocation can grow WASM linear memory, which detaches any
|
|
50
|
-
// previously created `ArrayBuffer` view (see `callParse` below for the
|
|
51
|
-
// same reasoning on the read side).
|
|
52
|
-
new Uint8Array(exports.mem.buffer, ptr, bytes.length).set(bytes);
|
|
53
|
-
return { ptr, len: bytes.length };
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Calls into the WASM shim's `process` export, instantiating the shim on
|
|
57
|
-
* first use. Encodes `text`/`dialect`/`filename` into shim-owned linear
|
|
58
|
-
* memory, reads the NUL-terminated JSON result back via `TextDecoder`
|
|
59
|
-
* (rather than syscall/js's `js.ValueOf(string)`, which dominated the parse
|
|
60
|
-
* cost — see design/ARCHITECTURE.md's "Performance"), and frees every buffer
|
|
61
|
-
* the call allocated.
|
|
62
|
-
*
|
|
41
|
+
* Calls the lossless syntax protocol synchronously with byte-exact payloads.
|
|
42
|
+
* Every returned allocation is released even when a later allocation, exported
|
|
43
|
+
* operation, or response validation fails. Views are recreated after each call
|
|
44
|
+
* because Go allocation can grow and detach linear memory.
|
|
63
45
|
* @internal
|
|
64
46
|
*/
|
|
65
|
-
export function
|
|
47
|
+
export function callSyntax(operation, payload, options = '{}') {
|
|
66
48
|
const exports = ensureInstance();
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
49
|
+
const process = exports.syntaxProcess;
|
|
50
|
+
if (typeof process !== 'function') {
|
|
51
|
+
throw new ShInternalError('The installed WASM asset does not export the lossless syntax protocol');
|
|
52
|
+
}
|
|
53
|
+
const allocations = [];
|
|
54
|
+
const write = (bytes) => {
|
|
55
|
+
const ptr = exports.alloc(bytes.length);
|
|
56
|
+
allocations.push(ptr);
|
|
57
|
+
if (ptr === 0)
|
|
58
|
+
throw new ShInternalError('The syntax bridge returned a null input allocation');
|
|
59
|
+
new Uint8Array(exports.mem.buffer, ptr, bytes.length).set(bytes);
|
|
60
|
+
return { ptr, len: bytes.length };
|
|
61
|
+
};
|
|
62
|
+
try {
|
|
63
|
+
const op = write(encoder.encode(operation));
|
|
64
|
+
const input = write(payload);
|
|
65
|
+
const opts = write(encoder.encode(options));
|
|
66
|
+
const result = process(op.ptr, op.len, input.ptr, input.len, opts.ptr, opts.len);
|
|
67
|
+
if (result === 0)
|
|
68
|
+
throw new ShInternalError('The syntax bridge returned a null result');
|
|
69
|
+
allocations.push(result);
|
|
70
|
+
const memory = new Uint8Array(exports.mem.buffer);
|
|
71
|
+
if (result >= memory.length)
|
|
72
|
+
throw new ShInternalError('The syntax bridge returned an out-of-bounds result');
|
|
73
|
+
let end = result;
|
|
74
|
+
while (end < memory.length && memory[end] !== 0)
|
|
75
|
+
end++;
|
|
76
|
+
if (end === memory.length)
|
|
77
|
+
throw new ShInternalError('The syntax bridge result has no terminator');
|
|
78
|
+
return decoder.decode(memory.subarray(result, end));
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
for (const ptr of allocations)
|
|
82
|
+
exports.free(ptr);
|
|
76
83
|
}
|
|
77
|
-
// Re-read `mem.buffer` fresh (see `writeString`'s comment) and scan for the
|
|
78
|
-
// shim's NUL sentinel — valid JSON from Go's encoding/json never contains
|
|
79
|
-
// a raw 0x00 byte, since every control character is \u-escaped.
|
|
80
|
-
const memory = new Uint8Array(exports.mem.buffer);
|
|
81
|
-
let end = resultPtr;
|
|
82
|
-
while (memory[end] !== 0)
|
|
83
|
-
end++;
|
|
84
|
-
const json = decoder.decode(memory.subarray(resultPtr, end));
|
|
85
|
-
exports.free(resultPtr);
|
|
86
|
-
return json;
|
|
87
84
|
}
|
|
88
85
|
//# sourceMappingURL=wasm-instance.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wasm-instance.js","sourceRoot":"","sources":["../src/wasm-instance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,sBAAsB,CAAC;AAM9B,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C,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