@bearcove/styx-wasm 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/README.md +53 -0
- package/README.md.in +1 -0
- package/package.json +42 -0
- package/pkg/styx_wasm.js +318 -0
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# styx-wasm
|
|
2
|
+
|
|
3
|
+
[](https://crates.io/crates/styx-wasm)
|
|
4
|
+
[](https://docs.rs/styx-wasm)
|
|
5
|
+
[](./LICENSE)
|
|
6
|
+
|
|
7
|
+
WebAssembly bindings for the [Styx](https://github.com/bearcove/styx) parser. Enables parsing and validating Styx documents in browsers and other WASM runtimes.
|
|
8
|
+
|
|
9
|
+
## Sponsors
|
|
10
|
+
|
|
11
|
+
Thanks to all individual sponsors:
|
|
12
|
+
|
|
13
|
+
<p> <a href="https://github.com/sponsors/fasterthanlime">
|
|
14
|
+
<picture>
|
|
15
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/bearcove/styx/raw/main/static/sponsors-v3/github-dark.svg">
|
|
16
|
+
<img src="https://github.com/bearcove/styx/raw/main/static/sponsors-v3/github-light.svg" height="40" alt="GitHub Sponsors">
|
|
17
|
+
</picture>
|
|
18
|
+
</a> <a href="https://patreon.com/fasterthanlime">
|
|
19
|
+
<picture>
|
|
20
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/bearcove/styx/raw/main/static/sponsors-v3/patreon-dark.svg">
|
|
21
|
+
<img src="https://github.com/bearcove/styx/raw/main/static/sponsors-v3/patreon-light.svg" height="40" alt="Patreon">
|
|
22
|
+
</picture>
|
|
23
|
+
</a> </p>
|
|
24
|
+
|
|
25
|
+
...along with corporate sponsors:
|
|
26
|
+
|
|
27
|
+
<p> <a href="https://aws.amazon.com">
|
|
28
|
+
<picture>
|
|
29
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/bearcove/styx/raw/main/static/sponsors-v3/aws-dark.svg">
|
|
30
|
+
<img src="https://github.com/bearcove/styx/raw/main/static/sponsors-v3/aws-light.svg" height="40" alt="AWS">
|
|
31
|
+
</picture>
|
|
32
|
+
</a> <a href="https://zed.dev">
|
|
33
|
+
<picture>
|
|
34
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/bearcove/styx/raw/main/static/sponsors-v3/zed-dark.svg">
|
|
35
|
+
<img src="https://github.com/bearcove/styx/raw/main/static/sponsors-v3/zed-light.svg" height="40" alt="Zed">
|
|
36
|
+
</picture>
|
|
37
|
+
</a> <a href="https://depot.dev?utm_source=facet">
|
|
38
|
+
<picture>
|
|
39
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/bearcove/styx/raw/main/static/sponsors-v3/depot-dark.svg">
|
|
40
|
+
<img src="https://github.com/bearcove/styx/raw/main/static/sponsors-v3/depot-light.svg" height="40" alt="Depot">
|
|
41
|
+
</picture>
|
|
42
|
+
</a> </p>
|
|
43
|
+
|
|
44
|
+
...without whom this work could not exist.
|
|
45
|
+
|
|
46
|
+
## License
|
|
47
|
+
|
|
48
|
+
Licensed under either of:
|
|
49
|
+
|
|
50
|
+
- Apache License, Version 2.0 ([LICENSE-APACHE](https://github.com/bearcove/styx/blob/main/LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
|
|
51
|
+
- MIT license ([LICENSE-MIT](https://github.com/bearcove/styx/blob/main/LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
|
|
52
|
+
|
|
53
|
+
at your option.
|
package/README.md.in
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
WebAssembly bindings for the [Styx](https://github.com/bearcove/styx) parser. Enables parsing and validating Styx documents in browsers and other WASM runtimes.
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bearcove/styx-wasm",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "WebAssembly bindings for Styx parser",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./pkg/styx_wasm.js",
|
|
7
|
+
"types": "./pkg/styx_wasm.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./pkg/styx_wasm.d.ts",
|
|
11
|
+
"import": "./pkg/styx_wasm.js"
|
|
12
|
+
},
|
|
13
|
+
"./styx_wasm_bg.wasm": "./pkg/styx_wasm_bg.wasm"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"pkg"
|
|
17
|
+
],
|
|
18
|
+
"keywords": [
|
|
19
|
+
"styx",
|
|
20
|
+
"wasm",
|
|
21
|
+
"webassembly",
|
|
22
|
+
"parser",
|
|
23
|
+
"configuration"
|
|
24
|
+
],
|
|
25
|
+
"author": "Amos Wenger <amos@bearcove.net>",
|
|
26
|
+
"license": "MIT OR Apache-2.0",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/bearcove/styx.git",
|
|
30
|
+
"directory": "crates/styx-wasm"
|
|
31
|
+
},
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/bearcove/styx/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://styx.bearcove.net",
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "cargo build --release --target wasm32-unknown-unknown && wasm-bindgen --out-dir pkg --target web ../../target/wasm32-unknown-unknown/release/styx_wasm.wasm"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/pkg/styx_wasm.js
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/* @ts-self-types="./styx_wasm.d.ts" */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Convert a JSON string to Styx format.
|
|
5
|
+
*
|
|
6
|
+
* Returns a Styx document string representation of the JSON.
|
|
7
|
+
* Tagged values ({"$tag": "name", "$value": ...}) are converted back to tags.
|
|
8
|
+
* @param {string} json_source
|
|
9
|
+
* @returns {any}
|
|
10
|
+
*/
|
|
11
|
+
export function from_json(json_source) {
|
|
12
|
+
const ptr0 = passStringToWasm0(json_source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
13
|
+
const len0 = WASM_VECTOR_LEN;
|
|
14
|
+
const ret = wasm.from_json(ptr0, len0);
|
|
15
|
+
return ret;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Parse a Styx document and return diagnostics.
|
|
20
|
+
*
|
|
21
|
+
* Returns a JSON object with `success` boolean and `diagnostics` array.
|
|
22
|
+
* @param {string} source
|
|
23
|
+
* @returns {any}
|
|
24
|
+
*/
|
|
25
|
+
export function parse(source) {
|
|
26
|
+
const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
27
|
+
const len0 = WASM_VECTOR_LEN;
|
|
28
|
+
const ret = wasm.parse(ptr0, len0);
|
|
29
|
+
return ret;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Convert a Styx document to JSON.
|
|
34
|
+
*
|
|
35
|
+
* Returns a JSON string representation of the Styx document.
|
|
36
|
+
* Tags are represented as `{"$tag": "tagname", "$value": ...}`.
|
|
37
|
+
* Returns an error object if parsing fails.
|
|
38
|
+
* @param {string} source
|
|
39
|
+
* @returns {any}
|
|
40
|
+
*/
|
|
41
|
+
export function to_json(source) {
|
|
42
|
+
const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
43
|
+
const len0 = WASM_VECTOR_LEN;
|
|
44
|
+
const ret = wasm.to_json(ptr0, len0);
|
|
45
|
+
return ret;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Validate a Styx document and return whether it's valid.
|
|
50
|
+
* @param {string} source
|
|
51
|
+
* @returns {boolean}
|
|
52
|
+
*/
|
|
53
|
+
export function validate(source) {
|
|
54
|
+
const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
55
|
+
const len0 = WASM_VECTOR_LEN;
|
|
56
|
+
const ret = wasm.validate(ptr0, len0);
|
|
57
|
+
return ret !== 0;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Get the version of the Styx WASM library.
|
|
62
|
+
* @returns {string}
|
|
63
|
+
*/
|
|
64
|
+
export function version() {
|
|
65
|
+
let deferred1_0;
|
|
66
|
+
let deferred1_1;
|
|
67
|
+
try {
|
|
68
|
+
const ret = wasm.version();
|
|
69
|
+
deferred1_0 = ret[0];
|
|
70
|
+
deferred1_1 = ret[1];
|
|
71
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
72
|
+
} finally {
|
|
73
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function __wbg_get_imports() {
|
|
78
|
+
const import0 = {
|
|
79
|
+
__proto__: null,
|
|
80
|
+
__wbg_Error_8c4e43fe74559d73: function(arg0, arg1) {
|
|
81
|
+
const ret = Error(getStringFromWasm0(arg0, arg1));
|
|
82
|
+
return ret;
|
|
83
|
+
},
|
|
84
|
+
__wbg___wbindgen_is_string_cd444516edc5b180: function(arg0) {
|
|
85
|
+
const ret = typeof(arg0) === 'string';
|
|
86
|
+
return ret;
|
|
87
|
+
},
|
|
88
|
+
__wbg___wbindgen_throw_be289d5034ed271b: function(arg0, arg1) {
|
|
89
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
90
|
+
},
|
|
91
|
+
__wbg_new_361308b2356cecd0: function() {
|
|
92
|
+
const ret = new Object();
|
|
93
|
+
return ret;
|
|
94
|
+
},
|
|
95
|
+
__wbg_new_3eb36ae241fe6f44: function() {
|
|
96
|
+
const ret = new Array();
|
|
97
|
+
return ret;
|
|
98
|
+
},
|
|
99
|
+
__wbg_new_dca287b076112a51: function() {
|
|
100
|
+
const ret = new Map();
|
|
101
|
+
return ret;
|
|
102
|
+
},
|
|
103
|
+
__wbg_set_1eb0999cf5d27fc8: function(arg0, arg1, arg2) {
|
|
104
|
+
const ret = arg0.set(arg1, arg2);
|
|
105
|
+
return ret;
|
|
106
|
+
},
|
|
107
|
+
__wbg_set_3f1d0b984ed272ed: function(arg0, arg1, arg2) {
|
|
108
|
+
arg0[arg1] = arg2;
|
|
109
|
+
},
|
|
110
|
+
__wbg_set_f43e577aea94465b: function(arg0, arg1, arg2) {
|
|
111
|
+
arg0[arg1 >>> 0] = arg2;
|
|
112
|
+
},
|
|
113
|
+
__wbindgen_cast_0000000000000001: function(arg0) {
|
|
114
|
+
// Cast intrinsic for `F64 -> Externref`.
|
|
115
|
+
const ret = arg0;
|
|
116
|
+
return ret;
|
|
117
|
+
},
|
|
118
|
+
__wbindgen_cast_0000000000000002: function(arg0) {
|
|
119
|
+
// Cast intrinsic for `I64 -> Externref`.
|
|
120
|
+
const ret = arg0;
|
|
121
|
+
return ret;
|
|
122
|
+
},
|
|
123
|
+
__wbindgen_cast_0000000000000003: function(arg0, arg1) {
|
|
124
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
125
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
126
|
+
return ret;
|
|
127
|
+
},
|
|
128
|
+
__wbindgen_cast_0000000000000004: function(arg0) {
|
|
129
|
+
// Cast intrinsic for `U64 -> Externref`.
|
|
130
|
+
const ret = BigInt.asUintN(64, arg0);
|
|
131
|
+
return ret;
|
|
132
|
+
},
|
|
133
|
+
__wbindgen_init_externref_table: function() {
|
|
134
|
+
const table = wasm.__wbindgen_externrefs;
|
|
135
|
+
const offset = table.grow(4);
|
|
136
|
+
table.set(0, undefined);
|
|
137
|
+
table.set(offset + 0, undefined);
|
|
138
|
+
table.set(offset + 1, null);
|
|
139
|
+
table.set(offset + 2, true);
|
|
140
|
+
table.set(offset + 3, false);
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
return {
|
|
144
|
+
__proto__: null,
|
|
145
|
+
"./styx_wasm_bg.js": import0,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function getStringFromWasm0(ptr, len) {
|
|
150
|
+
ptr = ptr >>> 0;
|
|
151
|
+
return decodeText(ptr, len);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
let cachedUint8ArrayMemory0 = null;
|
|
155
|
+
function getUint8ArrayMemory0() {
|
|
156
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
157
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
158
|
+
}
|
|
159
|
+
return cachedUint8ArrayMemory0;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
163
|
+
if (realloc === undefined) {
|
|
164
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
165
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
166
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
167
|
+
WASM_VECTOR_LEN = buf.length;
|
|
168
|
+
return ptr;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
let len = arg.length;
|
|
172
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
173
|
+
|
|
174
|
+
const mem = getUint8ArrayMemory0();
|
|
175
|
+
|
|
176
|
+
let offset = 0;
|
|
177
|
+
|
|
178
|
+
for (; offset < len; offset++) {
|
|
179
|
+
const code = arg.charCodeAt(offset);
|
|
180
|
+
if (code > 0x7F) break;
|
|
181
|
+
mem[ptr + offset] = code;
|
|
182
|
+
}
|
|
183
|
+
if (offset !== len) {
|
|
184
|
+
if (offset !== 0) {
|
|
185
|
+
arg = arg.slice(offset);
|
|
186
|
+
}
|
|
187
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
188
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
189
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
190
|
+
|
|
191
|
+
offset += ret.written;
|
|
192
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
WASM_VECTOR_LEN = offset;
|
|
196
|
+
return ptr;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
200
|
+
cachedTextDecoder.decode();
|
|
201
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
202
|
+
let numBytesDecoded = 0;
|
|
203
|
+
function decodeText(ptr, len) {
|
|
204
|
+
numBytesDecoded += len;
|
|
205
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
206
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
207
|
+
cachedTextDecoder.decode();
|
|
208
|
+
numBytesDecoded = len;
|
|
209
|
+
}
|
|
210
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const cachedTextEncoder = new TextEncoder();
|
|
214
|
+
|
|
215
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
216
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
217
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
218
|
+
view.set(buf);
|
|
219
|
+
return {
|
|
220
|
+
read: arg.length,
|
|
221
|
+
written: buf.length
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
let WASM_VECTOR_LEN = 0;
|
|
227
|
+
|
|
228
|
+
let wasmModule, wasm;
|
|
229
|
+
function __wbg_finalize_init(instance, module) {
|
|
230
|
+
wasm = instance.exports;
|
|
231
|
+
wasmModule = module;
|
|
232
|
+
cachedUint8ArrayMemory0 = null;
|
|
233
|
+
wasm.__wbindgen_start();
|
|
234
|
+
return wasm;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async function __wbg_load(module, imports) {
|
|
238
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
239
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
240
|
+
try {
|
|
241
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
242
|
+
} catch (e) {
|
|
243
|
+
const validResponse = module.ok && expectedResponseType(module.type);
|
|
244
|
+
|
|
245
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
246
|
+
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);
|
|
247
|
+
|
|
248
|
+
} else { throw e; }
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const bytes = await module.arrayBuffer();
|
|
253
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
254
|
+
} else {
|
|
255
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
256
|
+
|
|
257
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
258
|
+
return { instance, module };
|
|
259
|
+
} else {
|
|
260
|
+
return instance;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function expectedResponseType(type) {
|
|
265
|
+
switch (type) {
|
|
266
|
+
case 'basic': case 'cors': case 'default': return true;
|
|
267
|
+
}
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function initSync(module) {
|
|
273
|
+
if (wasm !== undefined) return wasm;
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
if (module !== undefined) {
|
|
277
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
278
|
+
({module} = module)
|
|
279
|
+
} else {
|
|
280
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const imports = __wbg_get_imports();
|
|
285
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
286
|
+
module = new WebAssembly.Module(module);
|
|
287
|
+
}
|
|
288
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
289
|
+
return __wbg_finalize_init(instance, module);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
async function __wbg_init(module_or_path) {
|
|
293
|
+
if (wasm !== undefined) return wasm;
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
if (module_or_path !== undefined) {
|
|
297
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
298
|
+
({module_or_path} = module_or_path)
|
|
299
|
+
} else {
|
|
300
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (module_or_path === undefined) {
|
|
305
|
+
module_or_path = new URL('styx_wasm_bg.wasm', import.meta.url);
|
|
306
|
+
}
|
|
307
|
+
const imports = __wbg_get_imports();
|
|
308
|
+
|
|
309
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
310
|
+
module_or_path = fetch(module_or_path);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
314
|
+
|
|
315
|
+
return __wbg_finalize_init(instance, module);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export { initSync, __wbg_init as default };
|