@tinyactors/sxml 0.0.0-stage → 0.2.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 +491 -2
- package/dist/allkeys.txt +39435 -0
- package/dist/index.d.ts +490 -0
- package/dist/index.js +1687 -0
- package/dist/linker.d.ts +20 -0
- package/dist/linker.js +196 -0
- package/dist/stream.d.ts +45 -0
- package/dist/stream.js +259 -0
- package/dist/sxml.wasm +0 -0
- package/dist/wasi.d.ts +3 -0
- package/dist/wasi.js +55 -0
- package/package.json +30 -4
package/dist/linker.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** What the linker needs of the loaded sxml.wasm. */
|
|
2
|
+
export interface LinkTarget {
|
|
3
|
+
memory: WebAssembly.Memory;
|
|
4
|
+
table: WebAssembly.Table;
|
|
5
|
+
stackPointer: WebAssembly.Global;
|
|
6
|
+
/** The C library functions sxml.wasm exports for external scanners. */
|
|
7
|
+
functions: ReadonlyMap<string, Function>;
|
|
8
|
+
/** Reserves zeroed memory with sxml.wasm's malloc; never released. */
|
|
9
|
+
reserve(byteCount: number, alignment: number): number;
|
|
10
|
+
}
|
|
11
|
+
/** A linked language module. */
|
|
12
|
+
export interface LinkedLanguage {
|
|
13
|
+
/** NAME of the module's tree_sitter_NAME function. */
|
|
14
|
+
functionName: string;
|
|
15
|
+
/** What tree_sitter_NAME() returned: the address of its TSLanguage. */
|
|
16
|
+
languageAddress: number;
|
|
17
|
+
}
|
|
18
|
+
/** Links module into target and returns its language. preferredName is
|
|
19
|
+
* the name the host gave the language, if any. */
|
|
20
|
+
export declare function linkLanguageModule(module: WebAssembly.Module, target: LinkTarget, preferredName: string | undefined): Promise<LinkedLanguage>;
|
package/dist/linker.js
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// The module linker (docs/GLOSSARY.md, `module_linker`; D293): links a
|
|
2
|
+
// tree-sitter language file, a WebAssembly side module as
|
|
3
|
+
// `tree-sitter build --wasm` writes it (`tree-sitter-NAME.wasm`), into the
|
|
4
|
+
// running sxml.wasm, so that sxml's tree-sitter runtime calls its lexer
|
|
5
|
+
// and its external scanner like those of a grammar compiled in.
|
|
6
|
+
//
|
|
7
|
+
// The side module's ABI is the WebAssembly tool conventions' "Dynamic
|
|
8
|
+
// linking" (DynamicLinking.md): a `dylink.0` custom section gives the
|
|
9
|
+
// memory and table sizes it needs; it imports env.memory,
|
|
10
|
+
// env.__indirect_function_table, env.__stack_pointer, env.__memory_base
|
|
11
|
+
// and env.__table_base, and places its data at __memory_base and its
|
|
12
|
+
// functions at __table_base; it may export __wasm_apply_data_relocs and
|
|
13
|
+
// __wasm_call_ctors, which run once. tree-sitter's own loader
|
|
14
|
+
// (lib/src/wasm_store.c, ts_wasm_store_load_language) reads the same
|
|
15
|
+
// files the same way, and its external scanners may import the C
|
|
16
|
+
// library functions of lib/src/wasm-stdlib/imports.txt, plus abort and
|
|
17
|
+
// __assert_fail. sxml.wasm exports its memory, its growable function
|
|
18
|
+
// table, its stack pointer and those functions
|
|
19
|
+
// (bindings/javascript/CMakeLists.txt), so the side module shares all of
|
|
20
|
+
// them: one memory, one heap, one table, one stack.
|
|
21
|
+
//
|
|
22
|
+
// Memory for the side module's data comes from sxml.wasm's own malloc,
|
|
23
|
+
// so it never overlaps sxml's heap; its table slots are added at the end
|
|
24
|
+
// of the table. Neither is ever released: a WebAssembly instance cannot
|
|
25
|
+
// be unloaded while sxml's table holds its functions.
|
|
26
|
+
// dylink.0 subsection types (DynamicLinking.md, "The dylink.0 Section").
|
|
27
|
+
const dylink_memory_info = 1;
|
|
28
|
+
const dylink_needed = 2;
|
|
29
|
+
function refused(reason) {
|
|
30
|
+
return new Error(`sxml: the module is not a tree-sitter language file sxml can link: ${reason}`);
|
|
31
|
+
}
|
|
32
|
+
/** Reads the module's dylink.0 section. */
|
|
33
|
+
function readDylink(module) {
|
|
34
|
+
const sections = WebAssembly.Module.customSections(module, 'dylink.0');
|
|
35
|
+
const section = sections[0];
|
|
36
|
+
if (section === undefined) {
|
|
37
|
+
throw refused('it has no dylink.0 section (tree-sitter build --wasm writes one)');
|
|
38
|
+
}
|
|
39
|
+
const bytes = new Uint8Array(section);
|
|
40
|
+
let offset = 0;
|
|
41
|
+
const readByte = () => {
|
|
42
|
+
const byte = bytes[offset];
|
|
43
|
+
if (byte === undefined) {
|
|
44
|
+
throw refused('its dylink.0 section is cut short');
|
|
45
|
+
}
|
|
46
|
+
offset += 1;
|
|
47
|
+
return byte;
|
|
48
|
+
};
|
|
49
|
+
const readUnsigned = () => {
|
|
50
|
+
let value = 0;
|
|
51
|
+
let shift = 0;
|
|
52
|
+
for (;;) {
|
|
53
|
+
const byte = readByte();
|
|
54
|
+
value += (byte & 0x7f) * 2 ** shift;
|
|
55
|
+
if ((byte & 0x80) === 0) {
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
shift += 7;
|
|
59
|
+
if (shift > 28) {
|
|
60
|
+
throw refused('its dylink.0 section has a number past 32 bits');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const decoder = new TextDecoder();
|
|
65
|
+
const info = {
|
|
66
|
+
memorySize: 0,
|
|
67
|
+
memoryAlignment: 0,
|
|
68
|
+
tableSize: 0,
|
|
69
|
+
tableAlignment: 0,
|
|
70
|
+
neededLibraries: [],
|
|
71
|
+
};
|
|
72
|
+
while (offset < bytes.length) {
|
|
73
|
+
const type = readByte();
|
|
74
|
+
const size = readUnsigned();
|
|
75
|
+
const end = offset + size;
|
|
76
|
+
if (end > bytes.length) {
|
|
77
|
+
throw refused('its dylink.0 section is cut short');
|
|
78
|
+
}
|
|
79
|
+
if (type === dylink_memory_info) {
|
|
80
|
+
info.memorySize = readUnsigned();
|
|
81
|
+
info.memoryAlignment = readUnsigned();
|
|
82
|
+
info.tableSize = readUnsigned();
|
|
83
|
+
info.tableAlignment = readUnsigned();
|
|
84
|
+
}
|
|
85
|
+
else if (type === dylink_needed) {
|
|
86
|
+
const count = readUnsigned();
|
|
87
|
+
for (let index = 0; index < count; index += 1) {
|
|
88
|
+
const nameSize = readUnsigned();
|
|
89
|
+
info.neededLibraries.push(decoder.decode(bytes.subarray(offset, offset + nameSize)));
|
|
90
|
+
offset += nameSize;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// Export and import information (flags of weak or TLS symbols) do
|
|
94
|
+
// not concern a language module.
|
|
95
|
+
offset = end;
|
|
96
|
+
}
|
|
97
|
+
if (info.memoryAlignment > 16 || info.tableAlignment > 16) {
|
|
98
|
+
throw refused('its dylink.0 section asks for an alignment past 2^16');
|
|
99
|
+
}
|
|
100
|
+
return info;
|
|
101
|
+
}
|
|
102
|
+
/** The module's tree_sitter_NAME export: tree_sitter_PREFERRED when it
|
|
103
|
+
* has one, else its only one. */
|
|
104
|
+
function findLanguageFunction(module, preferred) {
|
|
105
|
+
const names = WebAssembly.Module.exports(module)
|
|
106
|
+
.filter(({ name, kind }) => kind === 'function' && /^tree_sitter_[A-Za-z0-9_]+$/.test(name))
|
|
107
|
+
.map(({ name }) => name.slice('tree_sitter_'.length));
|
|
108
|
+
if (preferred !== undefined) {
|
|
109
|
+
const functionName = preferred.replace(/-/g, '_');
|
|
110
|
+
if (names.includes(functionName)) {
|
|
111
|
+
return functionName;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const [only, ...others] = names;
|
|
115
|
+
if (only === undefined) {
|
|
116
|
+
throw refused('it exports no function tree_sitter_NAME');
|
|
117
|
+
}
|
|
118
|
+
if (others.length > 0) {
|
|
119
|
+
throw refused(`it exports several languages (${names.join(', ')}); name the one to load`);
|
|
120
|
+
}
|
|
121
|
+
return only;
|
|
122
|
+
}
|
|
123
|
+
function trap(name) {
|
|
124
|
+
return () => {
|
|
125
|
+
throw new WebAssembly.RuntimeError(`sxml: the language module called ${name}`);
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/** Links module into target and returns its language. preferredName is
|
|
129
|
+
* the name the host gave the language, if any. */
|
|
130
|
+
export async function linkLanguageModule(module, target, preferredName) {
|
|
131
|
+
const dylink = readDylink(module);
|
|
132
|
+
if (dylink.neededLibraries.length > 0) {
|
|
133
|
+
throw refused(`it needs other WebAssembly libraries: ${dylink.neededLibraries.join(', ')}`);
|
|
134
|
+
}
|
|
135
|
+
// Every import must be one sxml provides; the others are named at once.
|
|
136
|
+
const unknown = [];
|
|
137
|
+
const environment = {};
|
|
138
|
+
const baseNames = [];
|
|
139
|
+
for (const { module: importModule, name, kind } of WebAssembly.Module.imports(module)) {
|
|
140
|
+
let value;
|
|
141
|
+
if (importModule === 'env') {
|
|
142
|
+
if (name === 'memory' && kind === 'memory') {
|
|
143
|
+
value = target.memory;
|
|
144
|
+
}
|
|
145
|
+
else if (name === '__indirect_function_table' && kind === 'table') {
|
|
146
|
+
value = target.table;
|
|
147
|
+
}
|
|
148
|
+
else if (name === '__stack_pointer' && kind === 'global') {
|
|
149
|
+
value = target.stackPointer;
|
|
150
|
+
}
|
|
151
|
+
else if ((name === '__memory_base' || name === '__table_base') && kind === 'global') {
|
|
152
|
+
// Made below, once the bases are known.
|
|
153
|
+
baseNames.push(name);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
else if ((name === 'abort' || name === '__assert_fail') && kind === 'function') {
|
|
157
|
+
value = trap(name);
|
|
158
|
+
}
|
|
159
|
+
else if (kind === 'function') {
|
|
160
|
+
value = target.functions.get(name);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (value === undefined) {
|
|
164
|
+
unknown.push(`${importModule}.${name}`);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
environment[name] = value;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (unknown.length > 0) {
|
|
171
|
+
throw refused(`it imports what sxml does not provide: ${unknown.join(', ')}`);
|
|
172
|
+
}
|
|
173
|
+
const functionName = findLanguageFunction(module, preferredName);
|
|
174
|
+
// Its data in sxml's heap, zeroed (its bss is not in its data
|
|
175
|
+
// segments), and its functions after the table's last slot.
|
|
176
|
+
const memoryBase = target.reserve(dylink.memorySize, 2 ** dylink.memoryAlignment);
|
|
177
|
+
const tableAlignment = 2 ** dylink.tableAlignment;
|
|
178
|
+
const tableLength = target.table.length;
|
|
179
|
+
const tableBase = Math.ceil(tableLength / tableAlignment) * tableAlignment;
|
|
180
|
+
if (tableBase + dylink.tableSize > tableLength) {
|
|
181
|
+
target.table.grow(tableBase + dylink.tableSize - tableLength);
|
|
182
|
+
}
|
|
183
|
+
for (const name of baseNames) {
|
|
184
|
+
const base = name === '__memory_base' ? memoryBase : tableBase;
|
|
185
|
+
environment[name] = new WebAssembly.Global({ value: 'i32', mutable: false }, base);
|
|
186
|
+
}
|
|
187
|
+
const instance = await WebAssembly.instantiate(module, { env: environment });
|
|
188
|
+
for (const initializer of ['__wasm_apply_data_relocs', '__wasm_call_ctors', '_initialize']) {
|
|
189
|
+
const run = instance.exports[initializer];
|
|
190
|
+
if (typeof run === 'function') {
|
|
191
|
+
run();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
const languageFunction = instance.exports[`tree_sitter_${functionName}`];
|
|
195
|
+
return { functionName, languageAddress: languageFunction() >>> 0 };
|
|
196
|
+
}
|
package/dist/stream.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** Receives serialized output a chunk at a time: a function given each
|
|
2
|
+
* chunk, or an object whose write is given each chunk, close is called
|
|
3
|
+
* when the output is complete, and abort when the call fails (with the
|
|
4
|
+
* error it throws) or the result document was abandoned. Each chunk is
|
|
5
|
+
* the sink's own copy. A sink must not use the processor; an exception
|
|
6
|
+
* it throws stops the call, which rethrows it. */
|
|
7
|
+
export type OutputSink = ((chunk: Uint8Array) => void) | OutputSinkObject;
|
|
8
|
+
export interface OutputSinkObject {
|
|
9
|
+
write(chunk: Uint8Array): void;
|
|
10
|
+
close?(): void;
|
|
11
|
+
abort?(reason: unknown): void;
|
|
12
|
+
}
|
|
13
|
+
/** A sink that decodes UTF-8 across chunk borders (TextDecoder in stream
|
|
14
|
+
* mode: a character whose bytes two chunks share is decoded once both
|
|
15
|
+
* arrived) and hands each decoded piece, never empty, to onText. */
|
|
16
|
+
export declare function textSink(onText: (text: string) => void): OutputSinkObject;
|
|
17
|
+
/** A sink that writes to a WHATWG WritableStream: each chunk is queued with
|
|
18
|
+
* the stream's writer at once (the call cannot await the stream), and
|
|
19
|
+
* the stream is closed, or aborted, after the call. Await done for the
|
|
20
|
+
* stream to finish. */
|
|
21
|
+
export declare function writableStreamSink(stream: WritableStream<Uint8Array>): OutputSinkObject & {
|
|
22
|
+
readonly done: Promise<void>;
|
|
23
|
+
};
|
|
24
|
+
/** A ReadableStream of the output that produce writes to the sink it is
|
|
25
|
+
* given: produce runs when the stream is first read, and the stream
|
|
26
|
+
* errors with what it throws. */
|
|
27
|
+
export declare function outputReadableStream(produce: (sink: OutputSink) => void): ReadableStream<Uint8Array>;
|
|
28
|
+
/** What nodeStreamSink needs of a Node.js Writable. */
|
|
29
|
+
export interface NodeWritable {
|
|
30
|
+
write(chunk: Uint8Array): boolean;
|
|
31
|
+
end(): unknown;
|
|
32
|
+
destroy(error?: Error): unknown;
|
|
33
|
+
}
|
|
34
|
+
/** A sink that writes to a Node.js Writable (a file, an HTTP response):
|
|
35
|
+
* write() is called for each chunk, whatever it returns (the stream
|
|
36
|
+
* buffers until the call returns), then end(), or destroy() on failure. */
|
|
37
|
+
export declare function nodeStreamSink(writable: NodeWritable): OutputSinkObject;
|
|
38
|
+
/** A synchronous source of a document's bytes, as a resolver may answer
|
|
39
|
+
* for kind 'xml-document' (D294): an iterable of chunks, such as a
|
|
40
|
+
* generator that reads a file with fs.readSync. Each chunk is
|
|
41
|
+
* Uint8Array or string (encoded as UTF-8). It is read as the document is
|
|
42
|
+
* parsed: a document XSLT streams (TransformOptions.streamedSource,
|
|
43
|
+
* xsl:source-document streamable="yes") is never whole in memory; fn:doc
|
|
44
|
+
* reads it whole first. It is closed (return()) when no longer read. */
|
|
45
|
+
export type ChunkSource = Iterable<Uint8Array | string>;
|
package/dist/stream.js
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// Streamed output and input of the JavaScript package (D294): sinks that
|
|
2
|
+
// receive serialized output a chunk at a time, adapters to WHATWG and
|
|
3
|
+
// Node.js streams, and the router that hands the shim's output events
|
|
4
|
+
// (bindings/host/sxml_host.h, "Streams") to them.
|
|
5
|
+
//
|
|
6
|
+
// An evaluation runs synchronously inside sxml.wasm, so a sink is called
|
|
7
|
+
// during the call that serializes, once per chunk (64 KiB by default,
|
|
8
|
+
// ProcessorOptions.outputChunkByteCount), with a copy of the bytes: the
|
|
9
|
+
// buffer in the module is reused for the next chunk. A sink cannot make
|
|
10
|
+
// the evaluation wait. Backpressure therefore means only this: a sink that
|
|
11
|
+
// consumes each chunk at once (fs.writeSync, a hash, a decoder feeding a
|
|
12
|
+
// parser, postMessage from a worker to a thread that reads concurrently)
|
|
13
|
+
// keeps memory bounded by one chunk; a sink that queues (a WritableStream,
|
|
14
|
+
// a Node.js stream, a ReadableStream's queue) holds the chunks in
|
|
15
|
+
// JavaScript memory until the call returns and the event loop drains them,
|
|
16
|
+
// which still spares the copy in the module's memory and the one string.
|
|
17
|
+
/** A sink that decodes UTF-8 across chunk borders (TextDecoder in stream
|
|
18
|
+
* mode: a character whose bytes two chunks share is decoded once both
|
|
19
|
+
* arrived) and hands each decoded piece, never empty, to onText. */
|
|
20
|
+
export function textSink(onText) {
|
|
21
|
+
const decoder = new TextDecoder('utf-8', { fatal: true });
|
|
22
|
+
return {
|
|
23
|
+
write(chunk) {
|
|
24
|
+
const text = decoder.decode(chunk, { stream: true });
|
|
25
|
+
if (text !== '') {
|
|
26
|
+
onText(text);
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
close() {
|
|
30
|
+
const text = decoder.decode();
|
|
31
|
+
if (text !== '') {
|
|
32
|
+
onText(text);
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** A sink that writes to a WHATWG WritableStream: each chunk is queued with
|
|
38
|
+
* the stream's writer at once (the call cannot await the stream), and
|
|
39
|
+
* the stream is closed, or aborted, after the call. Await done for the
|
|
40
|
+
* stream to finish. */
|
|
41
|
+
export function writableStreamSink(stream) {
|
|
42
|
+
const writer = stream.getWriter();
|
|
43
|
+
let finish;
|
|
44
|
+
const written = [];
|
|
45
|
+
return {
|
|
46
|
+
write(chunk) {
|
|
47
|
+
const pending = writer.write(chunk);
|
|
48
|
+
// Failures surface through done.
|
|
49
|
+
pending.catch(() => undefined);
|
|
50
|
+
written.push(pending);
|
|
51
|
+
},
|
|
52
|
+
close() {
|
|
53
|
+
finish = Promise.all(written).then(() => writer.close());
|
|
54
|
+
},
|
|
55
|
+
abort(reason) {
|
|
56
|
+
finish = writer.abort(reason);
|
|
57
|
+
},
|
|
58
|
+
get done() {
|
|
59
|
+
return finish ?? Promise.all(written).then(() => undefined);
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** A ReadableStream of the output that produce writes to the sink it is
|
|
64
|
+
* given: produce runs when the stream is first read, and the stream
|
|
65
|
+
* errors with what it throws. */
|
|
66
|
+
export function outputReadableStream(produce) {
|
|
67
|
+
return new ReadableStream({
|
|
68
|
+
start(controller) {
|
|
69
|
+
try {
|
|
70
|
+
produce((chunk) => controller.enqueue(chunk));
|
|
71
|
+
controller.close();
|
|
72
|
+
}
|
|
73
|
+
catch (exception) {
|
|
74
|
+
controller.error(exception);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/** A sink that writes to a Node.js Writable (a file, an HTTP response):
|
|
80
|
+
* write() is called for each chunk, whatever it returns (the stream
|
|
81
|
+
* buffers until the call returns), then end(), or destroy() on failure. */
|
|
82
|
+
export function nodeStreamSink(writable) {
|
|
83
|
+
return {
|
|
84
|
+
write(chunk) {
|
|
85
|
+
writable.write(chunk);
|
|
86
|
+
},
|
|
87
|
+
close() {
|
|
88
|
+
writable.end();
|
|
89
|
+
},
|
|
90
|
+
abort(reason) {
|
|
91
|
+
writable.destroy(reason instanceof Error ? reason : new Error(String(reason)));
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/** Writes a chunk to a sink. @internal */
|
|
96
|
+
export function writeChunk(sink, chunk) {
|
|
97
|
+
if (typeof sink === 'function') {
|
|
98
|
+
sink(chunk);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
sink.write(chunk);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** Ends a sink: close, or abort with reason. @internal */
|
|
105
|
+
export function endSink(sink, failed, reason) {
|
|
106
|
+
if (typeof sink === 'function') {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (failed) {
|
|
110
|
+
sink.abort?.(reason);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
sink.close?.();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** sxml_host_output_event_kind. */
|
|
117
|
+
const output_event_bytes = 0;
|
|
118
|
+
const output_event_result_document_opened = 1;
|
|
119
|
+
/**
|
|
120
|
+
* Routes one call's output events (sxml_host_output_event) to sinks: bytes
|
|
121
|
+
* go to the principal sink, or to the sink of the result document opened
|
|
122
|
+
* and not yet closed, which openDocument chooses; a document without one
|
|
123
|
+
* is gathered as text. @internal
|
|
124
|
+
*/
|
|
125
|
+
export class OutputRouter {
|
|
126
|
+
principal;
|
|
127
|
+
openDocument;
|
|
128
|
+
documentUri;
|
|
129
|
+
documentSink;
|
|
130
|
+
documentText = [];
|
|
131
|
+
documentDecoder;
|
|
132
|
+
/** Every result document's sink the router opened and did not end. */
|
|
133
|
+
openSinks = [];
|
|
134
|
+
gathered = [];
|
|
135
|
+
/** Without a principal sink, the principal output as text. */
|
|
136
|
+
principalText = [];
|
|
137
|
+
principalDecoder = new TextDecoder();
|
|
138
|
+
constructor(principal, openDocument) {
|
|
139
|
+
this.principal = principal;
|
|
140
|
+
this.openDocument = openDocument;
|
|
141
|
+
}
|
|
142
|
+
/** One event; bytes is a view into the module's memory. */
|
|
143
|
+
event(kind, bytes, isAbandoned, uri) {
|
|
144
|
+
if (kind === output_event_bytes) {
|
|
145
|
+
if (this.documentUri === undefined) {
|
|
146
|
+
if (this.principal !== undefined) {
|
|
147
|
+
writeChunk(this.principal, bytes.slice());
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
this.principalText.push(this.principalDecoder.decode(bytes, { stream: true }));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else if (this.documentSink !== undefined) {
|
|
154
|
+
writeChunk(this.documentSink, bytes.slice());
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
this.documentText.push(this.documentDecoder?.decode(bytes, { stream: true }) ?? '');
|
|
158
|
+
}
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (kind === output_event_result_document_opened) {
|
|
162
|
+
this.documentUri = uri();
|
|
163
|
+
this.documentSink = this.openDocument?.(this.documentUri);
|
|
164
|
+
if (this.documentSink === undefined) {
|
|
165
|
+
this.documentDecoder = new TextDecoder();
|
|
166
|
+
this.documentText = [];
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
this.openSinks.push(this.documentSink);
|
|
170
|
+
}
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
// RESULT_DOCUMENT_CLOSED.
|
|
174
|
+
const sink = this.documentSink;
|
|
175
|
+
if (sink !== undefined) {
|
|
176
|
+
this.openSinks.splice(this.openSinks.indexOf(sink), 1);
|
|
177
|
+
endSink(sink, isAbandoned, new Error(`sxml: the result document ${this.documentUri ?? ''} was abandoned`));
|
|
178
|
+
}
|
|
179
|
+
else if (!isAbandoned) {
|
|
180
|
+
this.documentText.push(this.documentDecoder?.decode() ?? '');
|
|
181
|
+
this.gathered.push({ uri: this.documentUri ?? '', output: this.documentText.join('') });
|
|
182
|
+
}
|
|
183
|
+
this.documentUri = undefined;
|
|
184
|
+
this.documentSink = undefined;
|
|
185
|
+
this.documentDecoder = undefined;
|
|
186
|
+
this.documentText = [];
|
|
187
|
+
}
|
|
188
|
+
/** The principal output gathered without a principal sink. */
|
|
189
|
+
principalOutput() {
|
|
190
|
+
this.principalText.push(this.principalDecoder.decode());
|
|
191
|
+
return this.principalText.splice(0).join('');
|
|
192
|
+
}
|
|
193
|
+
/** Ends the principal sink and any result document left open. */
|
|
194
|
+
finish(failed, reason) {
|
|
195
|
+
for (const sink of this.openSinks.splice(0)) {
|
|
196
|
+
endSink(sink, true, reason);
|
|
197
|
+
}
|
|
198
|
+
if (this.principal !== undefined) {
|
|
199
|
+
endSink(this.principal, failed, reason);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/** Reads a chunk source into the module's buffers. @internal */
|
|
204
|
+
export class ChunkReader {
|
|
205
|
+
iterator;
|
|
206
|
+
pending = new Uint8Array(0);
|
|
207
|
+
isDone = false;
|
|
208
|
+
constructor(source) {
|
|
209
|
+
this.iterator = source[Symbol.iterator]();
|
|
210
|
+
}
|
|
211
|
+
/** Fills the front of buffer; returns [byte count, is end]. */
|
|
212
|
+
read(buffer) {
|
|
213
|
+
let filled = 0;
|
|
214
|
+
while (filled < buffer.byteLength) {
|
|
215
|
+
if (this.pending.byteLength === 0) {
|
|
216
|
+
if (this.isDone) {
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
const next = this.iterator.next();
|
|
220
|
+
if (next.done === true) {
|
|
221
|
+
this.isDone = true;
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
this.pending = typeof next.value === 'string' ? new TextEncoder().encode(next.value) : next.value;
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
const count = Math.min(this.pending.byteLength, buffer.byteLength - filled);
|
|
228
|
+
buffer.set(this.pending.subarray(0, count), filled);
|
|
229
|
+
this.pending = this.pending.subarray(count);
|
|
230
|
+
filled += count;
|
|
231
|
+
}
|
|
232
|
+
return [filled, this.isDone && this.pending.byteLength === 0];
|
|
233
|
+
}
|
|
234
|
+
/** Closes the source. */
|
|
235
|
+
close() {
|
|
236
|
+
if (!this.isDone) {
|
|
237
|
+
this.isDone = true;
|
|
238
|
+
this.iterator.return?.();
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
/** Reads a whole chunk source, for the kinds a byte reader cannot
|
|
243
|
+
* deliver. @internal */
|
|
244
|
+
export function readWhole(source) {
|
|
245
|
+
const chunks = [];
|
|
246
|
+
let total = 0;
|
|
247
|
+
for (const chunk of source) {
|
|
248
|
+
const bytes = typeof chunk === 'string' ? new TextEncoder().encode(chunk) : chunk;
|
|
249
|
+
chunks.push(bytes);
|
|
250
|
+
total += bytes.byteLength;
|
|
251
|
+
}
|
|
252
|
+
const whole = new Uint8Array(total);
|
|
253
|
+
let at = 0;
|
|
254
|
+
for (const chunk of chunks) {
|
|
255
|
+
whole.set(chunk, at);
|
|
256
|
+
at += chunk.byteLength;
|
|
257
|
+
}
|
|
258
|
+
return whole;
|
|
259
|
+
}
|
package/dist/sxml.wasm
ADDED
|
Binary file
|
package/dist/wasi.d.ts
ADDED
package/dist/wasi.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// The WASI (preview 1) imports of sxml.wasm, for Node.js and browsers alike.
|
|
2
|
+
//
|
|
3
|
+
// sxml does no I/O of its own (D10): the library never prints and reads
|
|
4
|
+
// resources only through the resolver, which the package implements. What
|
|
5
|
+
// wasi-libc still imports is the clock (fn:current-dateTime, through
|
|
6
|
+
// sxml_system_clock) and the stdio functions it links but sxml never calls
|
|
7
|
+
// with a real descriptor. So instead of node:wasi or a browser polyfill, this
|
|
8
|
+
// is the whole system interface: the clocks are real, everything else
|
|
9
|
+
// answers EBADF or ENOSYS. Any import the module names that is not here gets
|
|
10
|
+
// the ENOSYS stub, so a newer wasi-libc cannot break loading.
|
|
11
|
+
const errno_success = 0;
|
|
12
|
+
const errno_bad_file_descriptor = 8;
|
|
13
|
+
const errno_invalid = 28;
|
|
14
|
+
const errno_not_supported = 52;
|
|
15
|
+
const clock_realtime = 0;
|
|
16
|
+
const clock_monotonic = 1;
|
|
17
|
+
/** Builds the wasi_snapshot_preview1 import object for module, writing into
|
|
18
|
+
* the memory that memory() returns once the instance exists. */
|
|
19
|
+
export function wasiImports(module, memory) {
|
|
20
|
+
const implemented = {
|
|
21
|
+
clock_time_get(clockId, _precision, timeAddress) {
|
|
22
|
+
let nanoseconds;
|
|
23
|
+
if (clockId === clock_realtime) {
|
|
24
|
+
nanoseconds = BigInt(Date.now()) * 1000000n;
|
|
25
|
+
}
|
|
26
|
+
else if (clockId === clock_monotonic) {
|
|
27
|
+
nanoseconds = BigInt(Math.round(performance.now() * 1_000_000));
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
return errno_invalid;
|
|
31
|
+
}
|
|
32
|
+
new DataView(memory().buffer).setBigUint64(timeAddress, nanoseconds, true);
|
|
33
|
+
return errno_success;
|
|
34
|
+
},
|
|
35
|
+
fd_write() {
|
|
36
|
+
return errno_bad_file_descriptor;
|
|
37
|
+
},
|
|
38
|
+
fd_close() {
|
|
39
|
+
return errno_bad_file_descriptor;
|
|
40
|
+
},
|
|
41
|
+
fd_seek() {
|
|
42
|
+
return errno_bad_file_descriptor;
|
|
43
|
+
},
|
|
44
|
+
proc_exit(exitCode) {
|
|
45
|
+
throw new Error(`sxml.wasm called proc_exit(${exitCode})`);
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
const imports = {};
|
|
49
|
+
for (const entry of WebAssembly.Module.imports(module)) {
|
|
50
|
+
if (entry.module === 'wasi_snapshot_preview1') {
|
|
51
|
+
imports[entry.name] = implemented[entry.name] ?? (() => errno_not_supported);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return imports;
|
|
55
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tinyactors/sxml",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "sxml for JavaScript: XML, XPath 4.0, XQuery 4.0, XSLT 4.0 and XSD 1.1 in Node.js and browsers, over WebAssembly",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./sxml.wasm": "./dist/sxml.wasm",
|
|
13
|
+
"./sxml-full.wasm": "./dist/sxml.wasm",
|
|
14
|
+
"./allkeys.txt": "./dist/allkeys.txt"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist/"
|
|
18
|
+
],
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=22.18"
|
|
21
|
+
},
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"scripts": {
|
|
24
|
+
"typecheck": "tsc",
|
|
25
|
+
"build": "tsc -p tsconfig.build.json && node -e \"const fs = require('node:fs'); fs.copyFileSync(process.env.SXML_WASM_PATH, 'dist/sxml.wasm'); fs.copyFileSync('../../third_party/unicode-collation/allkeys.txt', 'dist/allkeys.txt')\"",
|
|
26
|
+
"test": "node --expose-gc --test test/package.test.ts"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "22.20.5",
|
|
30
|
+
"typescript": "5.9.3"
|
|
31
|
+
}
|
|
32
|
+
}
|