decodal-wasm 0.1.2
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 +82 -0
- package/decodal_wasm.d.ts +41 -0
- package/decodal_wasm.js +231 -0
- package/decodal_wasm_bg.wasm +0 -0
- package/package.json +27 -0
package/README.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# Decodal
|
|
2
|
+
|
|
3
|
+
Decodal is a small deterministic DSL for describing, composing, validating, and materializing structured data.
|
|
4
|
+
|
|
5
|
+
It is designed around a lightweight Rust library:
|
|
6
|
+
|
|
7
|
+
- host-supplied imports through `SourceLoader`
|
|
8
|
+
- no filesystem access in the library core
|
|
9
|
+
- concrete and abstract values with constraints and defaults
|
|
10
|
+
- deterministic expression evaluation
|
|
11
|
+
- optional regex support behind a Cargo feature
|
|
12
|
+
- browser playground support through WebAssembly
|
|
13
|
+
|
|
14
|
+
## Library crate
|
|
15
|
+
|
|
16
|
+
Embedded hosts should depend on `decodal` and provide imports with a `SourceLoader`.
|
|
17
|
+
|
|
18
|
+
```toml
|
|
19
|
+
[dependencies]
|
|
20
|
+
decodal = "0.1"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Derive support
|
|
24
|
+
|
|
25
|
+
For embedded Rust applications, Decodal can generate a schema and typed decoder from a Rust struct with the `derive` feature.
|
|
26
|
+
|
|
27
|
+
```toml
|
|
28
|
+
[dependencies]
|
|
29
|
+
decodal = { version = "0.1", features = ["derive"] }
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```rust
|
|
33
|
+
use decodal::{Decodal, DecodalDecode, DecodalSchema, Engine};
|
|
34
|
+
|
|
35
|
+
#[derive(Decodal)]
|
|
36
|
+
struct Service {
|
|
37
|
+
name: String,
|
|
38
|
+
#[decodal(gt = 443, default = 8443)]
|
|
39
|
+
port: i64,
|
|
40
|
+
#[decodal(rename = "feature.enable", default = true)]
|
|
41
|
+
feature_enabled: bool,
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The derive implements:
|
|
46
|
+
|
|
47
|
+
- `DecodalSchema`, which produces a host schema for `Engine::bind_global`
|
|
48
|
+
- `DecodalDecode`, which converts materialized `Data` into the Rust struct
|
|
49
|
+
|
|
50
|
+
## CLI
|
|
51
|
+
|
|
52
|
+
A standalone CLI is kept in this repository as the `decodal-cli` workspace package.
|
|
53
|
+
It builds a `decodal` binary, but it is not the primary crates.io package.
|
|
54
|
+
|
|
55
|
+
Run a Decodal file from the repository:
|
|
56
|
+
|
|
57
|
+
```sh
|
|
58
|
+
cargo run -q -p decodal-cli -- examples/advanced/main.dcdl
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Enable optional regex support when needed:
|
|
62
|
+
|
|
63
|
+
```sh
|
|
64
|
+
cargo run -q -p decodal-cli --features regex -- examples/regex/main.dcdl
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Web playground
|
|
68
|
+
|
|
69
|
+
The static documentation site and browser playground live under:
|
|
70
|
+
|
|
71
|
+
```text
|
|
72
|
+
site/decodal-site/
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
Licensed under either of:
|
|
78
|
+
|
|
79
|
+
- Apache License, Version 2.0
|
|
80
|
+
- MIT license
|
|
81
|
+
|
|
82
|
+
at your option.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
export function evaluate(source: string): string;
|
|
5
|
+
|
|
6
|
+
export function evaluateProject(entry: string, files_json: string): string;
|
|
7
|
+
|
|
8
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
9
|
+
|
|
10
|
+
export interface InitOutput {
|
|
11
|
+
readonly memory: WebAssembly.Memory;
|
|
12
|
+
readonly evaluate: (a: number, b: number) => [number, number];
|
|
13
|
+
readonly evaluateProject: (a: number, b: number, c: number, d: number) => [number, number];
|
|
14
|
+
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
15
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
16
|
+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
17
|
+
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
18
|
+
readonly __wbindgen_start: () => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
25
|
+
* a precompiled `WebAssembly.Module`.
|
|
26
|
+
*
|
|
27
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
28
|
+
*
|
|
29
|
+
* @returns {InitOutput}
|
|
30
|
+
*/
|
|
31
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
35
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
36
|
+
*
|
|
37
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
38
|
+
*
|
|
39
|
+
* @returns {Promise<InitOutput>}
|
|
40
|
+
*/
|
|
41
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|
package/decodal_wasm.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/* @ts-self-types="./decodal_wasm.d.ts" */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {string} source
|
|
5
|
+
* @returns {string}
|
|
6
|
+
*/
|
|
7
|
+
export function evaluate(source) {
|
|
8
|
+
let deferred2_0;
|
|
9
|
+
let deferred2_1;
|
|
10
|
+
try {
|
|
11
|
+
const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
12
|
+
const len0 = WASM_VECTOR_LEN;
|
|
13
|
+
const ret = wasm.evaluate(ptr0, len0);
|
|
14
|
+
deferred2_0 = ret[0];
|
|
15
|
+
deferred2_1 = ret[1];
|
|
16
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
17
|
+
} finally {
|
|
18
|
+
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {string} entry
|
|
24
|
+
* @param {string} files_json
|
|
25
|
+
* @returns {string}
|
|
26
|
+
*/
|
|
27
|
+
export function evaluateProject(entry, files_json) {
|
|
28
|
+
let deferred3_0;
|
|
29
|
+
let deferred3_1;
|
|
30
|
+
try {
|
|
31
|
+
const ptr0 = passStringToWasm0(entry, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
32
|
+
const len0 = WASM_VECTOR_LEN;
|
|
33
|
+
const ptr1 = passStringToWasm0(files_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
34
|
+
const len1 = WASM_VECTOR_LEN;
|
|
35
|
+
const ret = wasm.evaluateProject(ptr0, len0, ptr1, len1);
|
|
36
|
+
deferred3_0 = ret[0];
|
|
37
|
+
deferred3_1 = ret[1];
|
|
38
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
39
|
+
} finally {
|
|
40
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function __wbg_get_imports() {
|
|
44
|
+
const import0 = {
|
|
45
|
+
__proto__: null,
|
|
46
|
+
__wbindgen_init_externref_table: function() {
|
|
47
|
+
const table = wasm.__wbindgen_externrefs;
|
|
48
|
+
const offset = table.grow(4);
|
|
49
|
+
table.set(0, undefined);
|
|
50
|
+
table.set(offset + 0, undefined);
|
|
51
|
+
table.set(offset + 1, null);
|
|
52
|
+
table.set(offset + 2, true);
|
|
53
|
+
table.set(offset + 3, false);
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
return {
|
|
57
|
+
__proto__: null,
|
|
58
|
+
"./decodal_wasm_bg.js": import0,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function getStringFromWasm0(ptr, len) {
|
|
63
|
+
return decodeText(ptr >>> 0, len);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let cachedUint8ArrayMemory0 = null;
|
|
67
|
+
function getUint8ArrayMemory0() {
|
|
68
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
69
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
70
|
+
}
|
|
71
|
+
return cachedUint8ArrayMemory0;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
75
|
+
if (realloc === undefined) {
|
|
76
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
77
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
78
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
79
|
+
WASM_VECTOR_LEN = buf.length;
|
|
80
|
+
return ptr;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let len = arg.length;
|
|
84
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
85
|
+
|
|
86
|
+
const mem = getUint8ArrayMemory0();
|
|
87
|
+
|
|
88
|
+
let offset = 0;
|
|
89
|
+
|
|
90
|
+
for (; offset < len; offset++) {
|
|
91
|
+
const code = arg.charCodeAt(offset);
|
|
92
|
+
if (code > 0x7F) break;
|
|
93
|
+
mem[ptr + offset] = code;
|
|
94
|
+
}
|
|
95
|
+
if (offset !== len) {
|
|
96
|
+
if (offset !== 0) {
|
|
97
|
+
arg = arg.slice(offset);
|
|
98
|
+
}
|
|
99
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
100
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
101
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
102
|
+
|
|
103
|
+
offset += ret.written;
|
|
104
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
WASM_VECTOR_LEN = offset;
|
|
108
|
+
return ptr;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
112
|
+
cachedTextDecoder.decode();
|
|
113
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
114
|
+
let numBytesDecoded = 0;
|
|
115
|
+
function decodeText(ptr, len) {
|
|
116
|
+
numBytesDecoded += len;
|
|
117
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
118
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
119
|
+
cachedTextDecoder.decode();
|
|
120
|
+
numBytesDecoded = len;
|
|
121
|
+
}
|
|
122
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const cachedTextEncoder = new TextEncoder();
|
|
126
|
+
|
|
127
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
128
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
129
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
130
|
+
view.set(buf);
|
|
131
|
+
return {
|
|
132
|
+
read: arg.length,
|
|
133
|
+
written: buf.length
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
let WASM_VECTOR_LEN = 0;
|
|
139
|
+
|
|
140
|
+
let wasmModule, wasmInstance, wasm;
|
|
141
|
+
function __wbg_finalize_init(instance, module) {
|
|
142
|
+
wasmInstance = instance;
|
|
143
|
+
wasm = instance.exports;
|
|
144
|
+
wasmModule = module;
|
|
145
|
+
cachedUint8ArrayMemory0 = null;
|
|
146
|
+
wasm.__wbindgen_start();
|
|
147
|
+
return wasm;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async function __wbg_load(module, imports) {
|
|
151
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
152
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
153
|
+
try {
|
|
154
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
155
|
+
} catch (e) {
|
|
156
|
+
const validResponse = module.ok && expectedResponseType(module.type);
|
|
157
|
+
|
|
158
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
159
|
+
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);
|
|
160
|
+
|
|
161
|
+
} else { throw e; }
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const bytes = await module.arrayBuffer();
|
|
166
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
167
|
+
} else {
|
|
168
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
169
|
+
|
|
170
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
171
|
+
return { instance, module };
|
|
172
|
+
} else {
|
|
173
|
+
return instance;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function expectedResponseType(type) {
|
|
178
|
+
switch (type) {
|
|
179
|
+
case 'basic': case 'cors': case 'default': return true;
|
|
180
|
+
}
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function initSync(module) {
|
|
186
|
+
if (wasm !== undefined) return wasm;
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
if (module !== undefined) {
|
|
190
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
191
|
+
({module} = module)
|
|
192
|
+
} else {
|
|
193
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const imports = __wbg_get_imports();
|
|
198
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
199
|
+
module = new WebAssembly.Module(module);
|
|
200
|
+
}
|
|
201
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
202
|
+
return __wbg_finalize_init(instance, module);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function __wbg_init(module_or_path) {
|
|
206
|
+
if (wasm !== undefined) return wasm;
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
if (module_or_path !== undefined) {
|
|
210
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
211
|
+
({module_or_path} = module_or_path)
|
|
212
|
+
} else {
|
|
213
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (module_or_path === undefined) {
|
|
218
|
+
module_or_path = new URL('decodal_wasm_bg.wasm', import.meta.url);
|
|
219
|
+
}
|
|
220
|
+
const imports = __wbg_get_imports();
|
|
221
|
+
|
|
222
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
223
|
+
module_or_path = fetch(module_or_path);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
227
|
+
|
|
228
|
+
return __wbg_finalize_init(instance, module);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export { initSync, __wbg_init as default };
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "decodal-wasm",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"description": "WebAssembly wrapper for evaluating Decodal in browser playgrounds.",
|
|
5
|
+
"version": "0.1.2",
|
|
6
|
+
"license": "MIT OR Apache-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://gitea.hareworks.net/Hare/Decodal"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"decodal_wasm_bg.wasm",
|
|
13
|
+
"decodal_wasm.js",
|
|
14
|
+
"decodal_wasm.d.ts"
|
|
15
|
+
],
|
|
16
|
+
"main": "decodal_wasm.js",
|
|
17
|
+
"types": "decodal_wasm.d.ts",
|
|
18
|
+
"sideEffects": [
|
|
19
|
+
"./snippets/*"
|
|
20
|
+
],
|
|
21
|
+
"keywords": [
|
|
22
|
+
"decodal",
|
|
23
|
+
"wasm",
|
|
24
|
+
"dsl",
|
|
25
|
+
"config"
|
|
26
|
+
]
|
|
27
|
+
}
|