2.4 KiB
decodal-wasm
WebAssembly runtime package for Decodal.
This package exposes the Decodal evaluator and shared language service to browsers and other JavaScript runtimes that can load WebAssembly modules.
It is generated from the Rust crate in crates/decodal-wasm with wasm-pack and is used by the official playground.
Use decodal-codemirror separately when you need CodeMirror 6 language support.
Install
npm install decodal-wasm
JSR:
deno add jsr:@hare/decodal-wasm
Usage
import init, { DecodalLanguageService } from 'decodal-wasm';
await init();
const files = {
'main.dcdl': 'let schema = import "./schema.dcdl"; in schema.Server',
'schema.dcdl': 'Server = { port = 8080; } as App.Server;',
};
const service = new DecodalLanguageService({
globals: {
App: {
Server: {
port: { $decodal: 'Int' },
},
},
},
loadImport(_currentKey, specifier) {
const key = specifier.startsWith('./') ? specifier.slice(2) : specifier;
return { kind: 'source', key, name: key, source: files[key] };
},
completeImport() {
return ['./schema.dcdl'];
},
});
const result = service.evaluate('main.dcdl', 'main.dcdl', files['main.dcdl']);
console.log(JSON.parse(result));
const completion = service.complete(
'main.dcdl',
'App.Server.po',
'App.Server.po'.length,
false,
);
console.log(JSON.parse(completion));
globals, loadImport, and completeImport are host-owned. The package does not assume a filesystem or virtual project model. Import callbacks are synchronous; preload or cache remote content before evaluation.
Plain strings, numbers, booleans, arrays, and objects are concrete values. Use { $decodal: 'String' | 'Int' | 'Float' | 'Bool' } for primitive ranges, { $decodal: 'Unknown' } for the top range, { $decodal: 'Array', item: value } for array ranges, and { $decodal: 'Map', value: value } for associative-array ranges. Primitive, unknown, array, and map descriptors also accept constraints and default.
Use { $decodal: 'Range', constraints, default } when constructing a general range directly.
An object with named fields and an open rest range uses { $decodal: 'Object', fields, rest }. The fields property is a record of values and rest is the range applied to every other field.
Methods return JSON strings so callers can handle diagnostics and successful results without depending on Rust data structures.