icl-runtime 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 +67 -0
- package/icl_runtime.d.ts +59 -0
- package/icl_runtime.js +277 -0
- package/icl_runtime_bg.wasm +0 -0
- package/package.json +20 -0
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# ICL JavaScript/TypeScript Bindings
|
|
2
|
+
|
|
3
|
+
JavaScript and TypeScript bindings for the [ICL (Intent Contract Language)](https://github.com/ICL-System/ICL-Runtime) runtime.
|
|
4
|
+
|
|
5
|
+
Built with [wasm-pack](https://rustwasm.github.io/wasm-pack/) — the canonical Rust implementation compiled to WebAssembly.
|
|
6
|
+
|
|
7
|
+
## Status: Alpha
|
|
8
|
+
|
|
9
|
+
This package is in early development. The API may change.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
# From source (requires Rust toolchain + wasm-pack)
|
|
15
|
+
cargo install wasm-pack
|
|
16
|
+
cd bindings/javascript
|
|
17
|
+
wasm-pack build --target nodejs
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
import { parseContract, normalize, verify, execute, semanticHash } from './pkg/icl_runtime.js';
|
|
24
|
+
|
|
25
|
+
const contractText = fs.readFileSync('my-contract.icl', 'utf-8');
|
|
26
|
+
|
|
27
|
+
// Parse
|
|
28
|
+
const parsed = JSON.parse(parseContract(contractText));
|
|
29
|
+
|
|
30
|
+
// Normalize (deterministic canonical form)
|
|
31
|
+
const normalized = normalize(contractText);
|
|
32
|
+
|
|
33
|
+
// Verify (type checking, invariants, determinism)
|
|
34
|
+
const result = JSON.parse(verify(contractText));
|
|
35
|
+
if (result.valid) {
|
|
36
|
+
console.log('Contract is valid!');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Execute
|
|
40
|
+
const output = JSON.parse(execute(contractText, '{"operation": "greet", "inputs": {"name": "World"}}'));
|
|
41
|
+
|
|
42
|
+
// Semantic hash (SHA-256)
|
|
43
|
+
const hash = semanticHash(contractText);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## TypeScript
|
|
47
|
+
|
|
48
|
+
Full TypeScript definitions are provided in `icl.d.ts`.
|
|
49
|
+
|
|
50
|
+
## Guarantees
|
|
51
|
+
|
|
52
|
+
- **Deterministic**: Same input always produces identical output
|
|
53
|
+
- **Identical to Rust**: All results match the canonical Rust implementation exactly
|
|
54
|
+
- **Zero logic in bindings**: All behavior comes from `icl-core` via WASM
|
|
55
|
+
|
|
56
|
+
## Development
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
# Build for Node.js
|
|
60
|
+
wasm-pack build --target nodejs
|
|
61
|
+
|
|
62
|
+
# Build for browsers
|
|
63
|
+
wasm-pack build --target web
|
|
64
|
+
|
|
65
|
+
# Run tests
|
|
66
|
+
node tests/test_icl.mjs
|
|
67
|
+
```
|
package/icl_runtime.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Execute an ICL contract with the given inputs.
|
|
6
|
+
*
|
|
7
|
+
* @param text - ICL contract source text
|
|
8
|
+
* @param inputs - JSON string with execution inputs
|
|
9
|
+
* @returns JSON string with execution result including provenance log
|
|
10
|
+
* @throws Error if the contract cannot be parsed, verified, or executed
|
|
11
|
+
*/
|
|
12
|
+
export function execute(text: string, inputs: string): string;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Normalize ICL contract text to canonical form.
|
|
16
|
+
*
|
|
17
|
+
* Guarantees:
|
|
18
|
+
* - Deterministic: same input → same output
|
|
19
|
+
* - Idempotent: normalize(normalize(x)) === normalize(x)
|
|
20
|
+
* - Semantic preserving: meaning is unchanged
|
|
21
|
+
*
|
|
22
|
+
* @param text - ICL contract source text
|
|
23
|
+
* @returns Canonical normalized ICL text
|
|
24
|
+
* @throws Error if the contract text cannot be parsed
|
|
25
|
+
*/
|
|
26
|
+
export function normalize(text: string): string;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Parse ICL contract text and return a JSON string of the parsed Contract.
|
|
30
|
+
*
|
|
31
|
+
* @param text - ICL contract source text
|
|
32
|
+
* @returns JSON string representation of the parsed Contract
|
|
33
|
+
* @throws Error if the contract text has syntax or semantic errors
|
|
34
|
+
*/
|
|
35
|
+
export function parseContract(text: string): string;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Compute the SHA-256 semantic hash of a contract.
|
|
39
|
+
*
|
|
40
|
+
* @param text - ICL contract source text
|
|
41
|
+
* @returns Hex-encoded SHA-256 hash string
|
|
42
|
+
* @throws Error if the contract text cannot be parsed
|
|
43
|
+
*/
|
|
44
|
+
export function semanticHash(text: string): string;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Verify an ICL contract for correctness.
|
|
48
|
+
*
|
|
49
|
+
* Runs all verification phases:
|
|
50
|
+
* - Type checking
|
|
51
|
+
* - Invariant verification
|
|
52
|
+
* - Determinism checking
|
|
53
|
+
* - Coherence verification
|
|
54
|
+
*
|
|
55
|
+
* @param text - ICL contract source text
|
|
56
|
+
* @returns JSON string: { valid: boolean, errors: [...], warnings: [...] }
|
|
57
|
+
* @throws Error if the contract text cannot be parsed
|
|
58
|
+
*/
|
|
59
|
+
export function verify(text: string): string;
|
package/icl_runtime.js
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/* @ts-self-types="./icl_runtime.d.ts" */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Execute an ICL contract with the given inputs.
|
|
5
|
+
*
|
|
6
|
+
* @param text - ICL contract source text
|
|
7
|
+
* @param inputs - JSON string with execution inputs
|
|
8
|
+
* @returns JSON string with execution result including provenance log
|
|
9
|
+
* @throws Error if the contract cannot be parsed, verified, or executed
|
|
10
|
+
* @param {string} text
|
|
11
|
+
* @param {string} inputs
|
|
12
|
+
* @returns {string}
|
|
13
|
+
*/
|
|
14
|
+
function execute(text, inputs) {
|
|
15
|
+
let deferred4_0;
|
|
16
|
+
let deferred4_1;
|
|
17
|
+
try {
|
|
18
|
+
const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
19
|
+
const len0 = WASM_VECTOR_LEN;
|
|
20
|
+
const ptr1 = passStringToWasm0(inputs, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
21
|
+
const len1 = WASM_VECTOR_LEN;
|
|
22
|
+
const ret = wasm.execute(ptr0, len0, ptr1, len1);
|
|
23
|
+
var ptr3 = ret[0];
|
|
24
|
+
var len3 = ret[1];
|
|
25
|
+
if (ret[3]) {
|
|
26
|
+
ptr3 = 0; len3 = 0;
|
|
27
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
28
|
+
}
|
|
29
|
+
deferred4_0 = ptr3;
|
|
30
|
+
deferred4_1 = len3;
|
|
31
|
+
return getStringFromWasm0(ptr3, len3);
|
|
32
|
+
} finally {
|
|
33
|
+
wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.execute = execute;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Normalize ICL contract text to canonical form.
|
|
40
|
+
*
|
|
41
|
+
* Guarantees:
|
|
42
|
+
* - Deterministic: same input → same output
|
|
43
|
+
* - Idempotent: normalize(normalize(x)) === normalize(x)
|
|
44
|
+
* - Semantic preserving: meaning is unchanged
|
|
45
|
+
*
|
|
46
|
+
* @param text - ICL contract source text
|
|
47
|
+
* @returns Canonical normalized ICL text
|
|
48
|
+
* @throws Error if the contract text cannot be parsed
|
|
49
|
+
* @param {string} text
|
|
50
|
+
* @returns {string}
|
|
51
|
+
*/
|
|
52
|
+
function normalize(text) {
|
|
53
|
+
let deferred3_0;
|
|
54
|
+
let deferred3_1;
|
|
55
|
+
try {
|
|
56
|
+
const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
57
|
+
const len0 = WASM_VECTOR_LEN;
|
|
58
|
+
const ret = wasm.normalize(ptr0, len0);
|
|
59
|
+
var ptr2 = ret[0];
|
|
60
|
+
var len2 = ret[1];
|
|
61
|
+
if (ret[3]) {
|
|
62
|
+
ptr2 = 0; len2 = 0;
|
|
63
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
64
|
+
}
|
|
65
|
+
deferred3_0 = ptr2;
|
|
66
|
+
deferred3_1 = len2;
|
|
67
|
+
return getStringFromWasm0(ptr2, len2);
|
|
68
|
+
} finally {
|
|
69
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.normalize = normalize;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Parse ICL contract text and return a JSON string of the parsed Contract.
|
|
76
|
+
*
|
|
77
|
+
* @param text - ICL contract source text
|
|
78
|
+
* @returns JSON string representation of the parsed Contract
|
|
79
|
+
* @throws Error if the contract text has syntax or semantic errors
|
|
80
|
+
* @param {string} text
|
|
81
|
+
* @returns {string}
|
|
82
|
+
*/
|
|
83
|
+
function parseContract(text) {
|
|
84
|
+
let deferred3_0;
|
|
85
|
+
let deferred3_1;
|
|
86
|
+
try {
|
|
87
|
+
const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
88
|
+
const len0 = WASM_VECTOR_LEN;
|
|
89
|
+
const ret = wasm.parseContract(ptr0, len0);
|
|
90
|
+
var ptr2 = ret[0];
|
|
91
|
+
var len2 = ret[1];
|
|
92
|
+
if (ret[3]) {
|
|
93
|
+
ptr2 = 0; len2 = 0;
|
|
94
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
95
|
+
}
|
|
96
|
+
deferred3_0 = ptr2;
|
|
97
|
+
deferred3_1 = len2;
|
|
98
|
+
return getStringFromWasm0(ptr2, len2);
|
|
99
|
+
} finally {
|
|
100
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.parseContract = parseContract;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Compute the SHA-256 semantic hash of a contract.
|
|
107
|
+
*
|
|
108
|
+
* @param text - ICL contract source text
|
|
109
|
+
* @returns Hex-encoded SHA-256 hash string
|
|
110
|
+
* @throws Error if the contract text cannot be parsed
|
|
111
|
+
* @param {string} text
|
|
112
|
+
* @returns {string}
|
|
113
|
+
*/
|
|
114
|
+
function semanticHash(text) {
|
|
115
|
+
let deferred3_0;
|
|
116
|
+
let deferred3_1;
|
|
117
|
+
try {
|
|
118
|
+
const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
119
|
+
const len0 = WASM_VECTOR_LEN;
|
|
120
|
+
const ret = wasm.semanticHash(ptr0, len0);
|
|
121
|
+
var ptr2 = ret[0];
|
|
122
|
+
var len2 = ret[1];
|
|
123
|
+
if (ret[3]) {
|
|
124
|
+
ptr2 = 0; len2 = 0;
|
|
125
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
126
|
+
}
|
|
127
|
+
deferred3_0 = ptr2;
|
|
128
|
+
deferred3_1 = len2;
|
|
129
|
+
return getStringFromWasm0(ptr2, len2);
|
|
130
|
+
} finally {
|
|
131
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
exports.semanticHash = semanticHash;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Verify an ICL contract for correctness.
|
|
138
|
+
*
|
|
139
|
+
* Runs all verification phases:
|
|
140
|
+
* - Type checking
|
|
141
|
+
* - Invariant verification
|
|
142
|
+
* - Determinism checking
|
|
143
|
+
* - Coherence verification
|
|
144
|
+
*
|
|
145
|
+
* @param text - ICL contract source text
|
|
146
|
+
* @returns JSON string: { valid: boolean, errors: [...], warnings: [...] }
|
|
147
|
+
* @throws Error if the contract text cannot be parsed
|
|
148
|
+
* @param {string} text
|
|
149
|
+
* @returns {string}
|
|
150
|
+
*/
|
|
151
|
+
function verify(text) {
|
|
152
|
+
let deferred3_0;
|
|
153
|
+
let deferred3_1;
|
|
154
|
+
try {
|
|
155
|
+
const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
156
|
+
const len0 = WASM_VECTOR_LEN;
|
|
157
|
+
const ret = wasm.verify(ptr0, len0);
|
|
158
|
+
var ptr2 = ret[0];
|
|
159
|
+
var len2 = ret[1];
|
|
160
|
+
if (ret[3]) {
|
|
161
|
+
ptr2 = 0; len2 = 0;
|
|
162
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
163
|
+
}
|
|
164
|
+
deferred3_0 = ptr2;
|
|
165
|
+
deferred3_1 = len2;
|
|
166
|
+
return getStringFromWasm0(ptr2, len2);
|
|
167
|
+
} finally {
|
|
168
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
exports.verify = verify;
|
|
172
|
+
|
|
173
|
+
function __wbg_get_imports() {
|
|
174
|
+
const import0 = {
|
|
175
|
+
__proto__: null,
|
|
176
|
+
__wbg_Error_8c4e43fe74559d73: function(arg0, arg1) {
|
|
177
|
+
const ret = Error(getStringFromWasm0(arg0, arg1));
|
|
178
|
+
return ret;
|
|
179
|
+
},
|
|
180
|
+
__wbindgen_init_externref_table: function() {
|
|
181
|
+
const table = wasm.__wbindgen_externrefs;
|
|
182
|
+
const offset = table.grow(4);
|
|
183
|
+
table.set(0, undefined);
|
|
184
|
+
table.set(offset + 0, undefined);
|
|
185
|
+
table.set(offset + 1, null);
|
|
186
|
+
table.set(offset + 2, true);
|
|
187
|
+
table.set(offset + 3, false);
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
return {
|
|
191
|
+
__proto__: null,
|
|
192
|
+
"./icl_runtime_bg.js": import0,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function getStringFromWasm0(ptr, len) {
|
|
197
|
+
ptr = ptr >>> 0;
|
|
198
|
+
return decodeText(ptr, len);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
let cachedUint8ArrayMemory0 = null;
|
|
202
|
+
function getUint8ArrayMemory0() {
|
|
203
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
204
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
205
|
+
}
|
|
206
|
+
return cachedUint8ArrayMemory0;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
210
|
+
if (realloc === undefined) {
|
|
211
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
212
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
213
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
214
|
+
WASM_VECTOR_LEN = buf.length;
|
|
215
|
+
return ptr;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
let len = arg.length;
|
|
219
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
220
|
+
|
|
221
|
+
const mem = getUint8ArrayMemory0();
|
|
222
|
+
|
|
223
|
+
let offset = 0;
|
|
224
|
+
|
|
225
|
+
for (; offset < len; offset++) {
|
|
226
|
+
const code = arg.charCodeAt(offset);
|
|
227
|
+
if (code > 0x7F) break;
|
|
228
|
+
mem[ptr + offset] = code;
|
|
229
|
+
}
|
|
230
|
+
if (offset !== len) {
|
|
231
|
+
if (offset !== 0) {
|
|
232
|
+
arg = arg.slice(offset);
|
|
233
|
+
}
|
|
234
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
235
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
236
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
237
|
+
|
|
238
|
+
offset += ret.written;
|
|
239
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
WASM_VECTOR_LEN = offset;
|
|
243
|
+
return ptr;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function takeFromExternrefTable0(idx) {
|
|
247
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
248
|
+
wasm.__externref_table_dealloc(idx);
|
|
249
|
+
return value;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
253
|
+
cachedTextDecoder.decode();
|
|
254
|
+
function decodeText(ptr, len) {
|
|
255
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const cachedTextEncoder = new TextEncoder();
|
|
259
|
+
|
|
260
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
261
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
262
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
263
|
+
view.set(buf);
|
|
264
|
+
return {
|
|
265
|
+
read: arg.length,
|
|
266
|
+
written: buf.length
|
|
267
|
+
};
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
let WASM_VECTOR_LEN = 0;
|
|
272
|
+
|
|
273
|
+
const wasmPath = `${__dirname}/icl_runtime_bg.wasm`;
|
|
274
|
+
const wasmBytes = require('fs').readFileSync(wasmPath);
|
|
275
|
+
const wasmModule = new WebAssembly.Module(wasmBytes);
|
|
276
|
+
const wasm = new WebAssembly.Instance(wasmModule, __wbg_get_imports()).exports;
|
|
277
|
+
wasm.__wbindgen_start();
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "icl-runtime",
|
|
3
|
+
"collaborators": [
|
|
4
|
+
"ICL Team"
|
|
5
|
+
],
|
|
6
|
+
"description": "JavaScript/TypeScript bindings for ICL (Intent Contract Language) via WebAssembly",
|
|
7
|
+
"version": "0.1.0",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/ICL-System/ICL-Runtime"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"icl_runtime_bg.wasm",
|
|
15
|
+
"icl_runtime.js",
|
|
16
|
+
"icl_runtime.d.ts"
|
|
17
|
+
],
|
|
18
|
+
"main": "icl_runtime.js",
|
|
19
|
+
"types": "icl_runtime.d.ts"
|
|
20
|
+
}
|