@fraczak/k 6.3.3 → 6.5.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/CHANGELOG.md +17 -0
- package/DOCS/CODECS.md +258 -0
- package/DOCS/DICTIONARY.md +1172 -0
- package/DOCS/KIR_SPECIALIZATION_DESIGN.md +51 -46
- package/DOCS/KIR_V1.md +221 -0
- package/DOCS/KVM_EXECUTION_MODEL.md +56 -19
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +2 -1
- package/DOCS/PROFILING_AND_TRACING.md +196 -0
- package/DOCS/REPL.md +61 -4
- package/DOCS/WEB_REPL_REQUIREMENTS.md +135 -0
- package/DOCS/dependency-graph.md +144 -0
- package/Examples/arithmetics.k +108 -82
- package/Examples/bnat.k +1 -1
- package/Examples/buda.k +44 -0
- package/Examples/ieee.k +300 -8671
- package/Examples/poly.k +67 -0
- package/README.md +68 -19
- package/Value.mjs +8 -2
- package/augmentation.mjs +9 -7
- package/backend-api.mjs +11 -2
- package/backends/arm64/README.md +104 -0
- package/backends/arm64/bin/k-arm64-compile.mjs +101 -0
- package/backends/arm64/bin/k-arm64-run.mjs +58 -0
- package/backends/arm64/bin/k-arm64.mjs +88 -0
- package/backends/arm64/scripts/run-tests.mjs +41 -0
- package/backends/arm64/src/arm64.mjs +229 -0
- package/backends/arm64/src/cli.mjs +155 -0
- package/backends/arm64/src/kvm2arm64.mjs +906 -0
- package/backends/arm64/src/runtime.c +728 -0
- package/backends/arm64/tests/integration.sh +112 -0
- package/backends/arm64/tests/test-arm64.mjs +240 -0
- package/backends/llvm/README.md +135 -0
- package/backends/llvm/bin/k-llvm-build.mjs +71 -0
- package/backends/llvm/bin/k-llvm-compile.mjs +68 -0
- package/backends/llvm/bin/k-llvm-jit.mjs +160 -0
- package/backends/llvm/bin/k-llvm-run.mjs +90 -0
- package/backends/llvm/runtime/krt.c +1508 -0
- package/backends/llvm/runtime/krt.h +109 -0
- package/backends/llvm/scripts/conformance.mjs +76 -0
- package/backends/llvm/scripts/run-tests.mjs +28 -0
- package/backends/llvm/src/executable.mjs +562 -0
- package/backends/llvm/src/kvm2llvm.mjs +998 -0
- package/backends/llvm/src/llvm.mjs +1092 -0
- package/backends/llvm/tests/composition-driver.c +22 -0
- package/backends/llvm/tests/identity-driver.c +27 -0
- package/backends/llvm/tests/integration.sh +153 -0
- package/backends/llvm/tests/perf-support.mjs +735 -0
- package/backends/llvm/tests/product-driver.c +23 -0
- package/backends/llvm/tests/projection-driver.c +19 -0
- package/backends/llvm/tests/relation-driver.c +29 -0
- package/backends/llvm/tests/test-llvm.mjs +240 -0
- package/backends/llvm/tests/union-driver.c +31 -0
- package/backends/llvm/tests/variant-driver.c +19 -0
- package/backends/llvm/tests/variant-projection-driver.c +21 -0
- package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +233 -0
- package/backends/wasm/README.md +97 -0
- package/backends/wasm/TECHNICAL-DEBT.md +58 -0
- package/backends/wasm/bin/k-wasm-compile.mjs +50 -0
- package/backends/wasm/bin/k-wasm-run.mjs +140 -0
- package/backends/wasm/bin/k-wasm.mjs +49 -0
- package/backends/wasm/demo_ieee.mjs +319 -0
- package/backends/wasm/runtime.wat +475 -0
- package/backends/wasm/scripts/run-tests.mjs +41 -0
- package/backends/wasm/src/cli.mjs +133 -0
- package/backends/wasm/src/kvm2wasm.mjs +697 -0
- package/backends/wasm/src/wasm.mjs +1054 -0
- package/backends/wasm/tests/integration.sh +182 -0
- package/backends/wasm/tests/perf-support.mjs +336 -0
- package/backends/wasm/tests/test-wasm.mjs +625 -0
- package/browser/repl-browser.mjs +1584 -0
- package/browser/shims/assert.mjs +19 -0
- package/browser/shims/buffer.mjs +224 -0
- package/browser/shims/crypto.mjs +103 -0
- package/browser/shims/fs.mjs +159 -0
- package/browser/shims/os.mjs +17 -0
- package/browser/shims/path.mjs +54 -0
- package/browser/shims/process.mjs +44 -0
- package/browser/shims/readline.mjs +11 -0
- package/browser/shims/url.mjs +12 -0
- package/browser/vfs-data.mjs +19 -0
- package/codecs/README.md +3 -0
- package/codecs/runtime/show-value.mjs +12 -3
- package/conformance/helper-reuse/case.json +8 -0
- package/conformance/helper-reuse/expected.kv +1 -0
- package/conformance/helper-reuse/input.kv +1 -0
- package/conformance/helper-reuse/program.k +2 -0
- package/conformance/identity/case.json +8 -0
- package/conformance/identity/expected.kv +1 -0
- package/conformance/identity/input.kv +1 -0
- package/conformance/identity/program.k +1 -0
- package/conformance/product-projection/case.json +8 -0
- package/conformance/product-projection/expected.kv +1 -0
- package/conformance/product-projection/input.kv +1 -0
- package/conformance/product-projection/program.k +1 -0
- package/conformance/run.mjs +149 -0
- package/conformance/variant-construction/case.json +8 -0
- package/conformance/variant-construction/expected.kv +1 -0
- package/conformance/variant-construction/input.kv +1 -0
- package/conformance/variant-construction/program.k +1 -0
- package/harness/adapters/arm64.mjs +105 -0
- package/harness/adapters/kvm.mjs +144 -0
- package/harness/adapters/llvm.mjs +118 -0
- package/harness/adapters/native.mjs +77 -0
- package/harness/adapters/wasm.mjs +169 -0
- package/harness/backend.mjs +95 -0
- package/harness/cli.mjs +130 -0
- package/harness/format.mjs +95 -0
- package/harness/index.mjs +25 -0
- package/harness/persistent.mjs +121 -0
- package/harness/registry.mjs +44 -0
- package/harness/suite.mjs +257 -0
- package/harness/suites/poly.mjs +156 -0
- package/harness/trace.mjs +50 -0
- package/harness/util.mjs +92 -0
- package/intrinsics.mjs +41 -0
- package/kir.mjs +399 -0
- package/kvm.mjs +585 -81
- package/objects/README.md +37 -3
- package/objects/compile.mjs +15 -22
- package/objects/inspect.mjs +96 -0
- package/objects/validate.mjs +415 -0
- package/package.json +51 -19
- package/repl-codecs.mjs +105 -6
- package/repl.mjs +335 -49
- package/run.mjs +2 -7
- package/scripts/benchmark.mjs +406 -22
- package/scripts/build-repl-html.mjs +1784 -0
- package/scripts/perf-ieee.mjs +586 -0
- package/scripts/perf-int.mjs +531 -0
- package/scripts/perf-poly.mjs +945 -0
- package/scripts/render-benchmark.mjs +12 -8
- package/scripts/run-tests.mjs +6 -0
- package/tests/integration.sh +19 -2
- package/tests/test-arithmetics.mjs +54 -0
- package/tests/test-ieee-arithmetic.mjs +4 -0
- package/tests/test-kir.mjs +85 -0
- package/tests/test-kvm-polymorphic.mjs +278 -0
- package/tests/test-kvm.mjs +25 -6
- package/tests/test-poly.mjs +129 -0
- package/tests/test-repl-html.mjs +303 -0
- package/tests/test-repl.mjs +144 -1
- package/tests/test-validate-object.mjs +40 -0
- package/a.klib +0 -1
- package/a.kvm +0 -142676
- package/a.wasm +0 -0
- package/benchmark-results.html +0 -532
- package/benchmark-results.json +0 -480
- package/k-vscode/.vscode/launch.json +0 -14
- package/k-vscode/README.md +0 -75
- package/k-vscode/extension.js +0 -273
- package/k-vscode/language-configuration.json +0 -42
- package/k-vscode/package.json +0 -79
- package/k-vscode/snippets/k.json +0 -92
- package/k-vscode/syntaxes/k.tmLanguage.json +0 -599
- package/k-vscode/test/background-ranges.test.js +0 -50
- package/k-vscode/themes/k-contrast-color-theme.json +0 -104
- package/scripts/git-hooks/pre-commit +0 -10
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function assert(val, msg) {
|
|
2
|
+
if (!val) throw new Error(msg || "Assertion failed");
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
assert.equal = function(a, b, msg) {
|
|
6
|
+
if (a != b) throw new Error(msg || `${a} != ${b}`);
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
assert.strictEqual = function(a, b, msg) {
|
|
10
|
+
if (a !== b) throw new Error(msg || `${a} !== ${b}`);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
assert.deepEqual = function(a, b, msg) {
|
|
14
|
+
if (JSON.stringify(a) !== JSON.stringify(b)) throw new Error(msg || "Deep equality assertion failed");
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
assert.ok = assert;
|
|
18
|
+
|
|
19
|
+
export default assert;
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
export class Buffer extends Uint8Array {
|
|
2
|
+
static from(data, encoding) {
|
|
3
|
+
if (typeof data === "string") {
|
|
4
|
+
return new Buffer(new TextEncoder().encode(data));
|
|
5
|
+
}
|
|
6
|
+
if (data instanceof Uint8Array || Array.isArray(data) || data instanceof ArrayBuffer) {
|
|
7
|
+
return new Buffer(data);
|
|
8
|
+
}
|
|
9
|
+
if (data?.buffer instanceof ArrayBuffer) {
|
|
10
|
+
return new Buffer(data.buffer, data.byteOffset, data.byteLength);
|
|
11
|
+
}
|
|
12
|
+
return new Buffer(data || 0);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static isBuffer(val) {
|
|
16
|
+
return val instanceof Uint8Array;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static alloc(size, fill = 0) {
|
|
20
|
+
const buf = new Buffer(size);
|
|
21
|
+
if (fill !== 0) buf.fill(fill);
|
|
22
|
+
return buf;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static concat(list, totalLength) {
|
|
26
|
+
const total = totalLength ?? list.reduce((acc, c) => acc + (c?.length || 0), 0);
|
|
27
|
+
const result = new Buffer(total);
|
|
28
|
+
let offset = 0;
|
|
29
|
+
for (const chunk of list) {
|
|
30
|
+
if (!chunk) continue;
|
|
31
|
+
result.set(chunk, offset);
|
|
32
|
+
offset += chunk.length;
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static compare(a, b) {
|
|
38
|
+
const len = Math.min(a.length, b.length);
|
|
39
|
+
for (let i = 0; i < len; i++) {
|
|
40
|
+
if (a[i] !== b[i]) return a[i] < b[i] ? -1 : 1;
|
|
41
|
+
}
|
|
42
|
+
return a.length === b.length ? 0 : a.length < b.length ? -1 : 1;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
toString(encoding = "utf8") {
|
|
46
|
+
if (encoding === "hex") {
|
|
47
|
+
return Array.from(this).map(b => b.toString(16).padStart(2, "0")).join("");
|
|
48
|
+
}
|
|
49
|
+
return new TextDecoder("utf-8").decode(this);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
writeUInt32BE(val, offset = 0) {
|
|
53
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setUint32(offset, Number(val), false);
|
|
54
|
+
return offset + 4;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
readUInt32BE(offset = 0) {
|
|
58
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getUint32(offset, false);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
writeUInt32LE(val, offset = 0) {
|
|
62
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setUint32(offset, Number(val), true);
|
|
63
|
+
return offset + 4;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
readUInt32LE(offset = 0) {
|
|
67
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getUint32(offset, true);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
writeInt32BE(val, offset = 0) {
|
|
71
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setInt32(offset, Number(val), false);
|
|
72
|
+
return offset + 4;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
readInt32BE(offset = 0) {
|
|
76
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt32(offset, false);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
writeInt32LE(val, offset = 0) {
|
|
80
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setInt32(offset, Number(val), true);
|
|
81
|
+
return offset + 4;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
readInt32LE(offset = 0) {
|
|
85
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt32(offset, true);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
writeUInt16BE(val, offset = 0) {
|
|
89
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setUint16(offset, Number(val), false);
|
|
90
|
+
return offset + 2;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
readUInt16BE(offset = 0) {
|
|
94
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getUint16(offset, false);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
writeUInt16LE(val, offset = 0) {
|
|
98
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setUint16(offset, Number(val), true);
|
|
99
|
+
return offset + 2;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
readUInt16LE(offset = 0) {
|
|
103
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getUint16(offset, true);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
writeInt16BE(val, offset = 0) {
|
|
107
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setInt16(offset, Number(val), false);
|
|
108
|
+
return offset + 2;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
readInt16BE(offset = 0) {
|
|
112
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt16(offset, false);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
writeInt16LE(val, offset = 0) {
|
|
116
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setInt16(offset, Number(val), true);
|
|
117
|
+
return offset + 2;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
readInt16LE(offset = 0) {
|
|
121
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt16(offset, true);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
writeUInt8(val, offset = 0) {
|
|
125
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setUint8(offset, Number(val));
|
|
126
|
+
return offset + 1;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
readUInt8(offset = 0) {
|
|
130
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getUint8(offset);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
writeInt8(val, offset = 0) {
|
|
134
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setInt8(offset, Number(val));
|
|
135
|
+
return offset + 1;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
readInt8(offset = 0) {
|
|
139
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt8(offset);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
writeDoubleBE(val, offset = 0) {
|
|
143
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setFloat64(offset, Number(val), false);
|
|
144
|
+
return offset + 8;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
readDoubleBE(offset = 0) {
|
|
148
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getFloat64(offset, false);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
writeDoubleLE(val, offset = 0) {
|
|
152
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setFloat64(offset, Number(val), true);
|
|
153
|
+
return offset + 8;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
readDoubleLE(offset = 0) {
|
|
157
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getFloat64(offset, true);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
writeFloatBE(val, offset = 0) {
|
|
161
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setFloat32(offset, Number(val), false);
|
|
162
|
+
return offset + 4;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
readFloatBE(offset = 0) {
|
|
166
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getFloat32(offset, false);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
writeFloatLE(val, offset = 0) {
|
|
170
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setFloat32(offset, Number(val), true);
|
|
171
|
+
return offset + 4;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
readFloatLE(offset = 0) {
|
|
175
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getFloat32(offset, true);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
writeBigUInt64BE(val, offset = 0) {
|
|
179
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setBigUint64(offset, BigInt(val), false);
|
|
180
|
+
return offset + 8;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
readBigUInt64BE(offset = 0) {
|
|
184
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getBigUint64(offset, false);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
writeBigUInt64LE(val, offset = 0) {
|
|
188
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setBigUint64(offset, BigInt(val), true);
|
|
189
|
+
return offset + 8;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
readBigUInt64LE(offset = 0) {
|
|
193
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getBigUint64(offset, true);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
writeBigInt64BE(val, offset = 0) {
|
|
197
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setBigInt64(offset, BigInt(val), false);
|
|
198
|
+
return offset + 8;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
readBigInt64BE(offset = 0) {
|
|
202
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getBigInt64(offset, false);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
writeBigInt64LE(val, offset = 0) {
|
|
206
|
+
new DataView(this.buffer, this.byteOffset, this.byteLength).setBigInt64(offset, BigInt(val), true);
|
|
207
|
+
return offset + 8;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
readBigInt64LE(offset = 0) {
|
|
211
|
+
return new DataView(this.buffer, this.byteOffset, this.byteLength).getBigInt64(offset, true);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
subarray(start, end) {
|
|
215
|
+
const sub = super.subarray(start, end);
|
|
216
|
+
return new Buffer(sub.buffer, sub.byteOffset, sub.byteLength);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (typeof globalThis !== "undefined") {
|
|
221
|
+
globalThis.Buffer = Buffer;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export default Buffer;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Buffer } from "./buffer.mjs";
|
|
2
|
+
|
|
3
|
+
function sha256(input) {
|
|
4
|
+
const K = [
|
|
5
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
6
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
7
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
8
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
9
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
10
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
11
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
12
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
13
|
+
];
|
|
14
|
+
const H = [
|
|
15
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
|
16
|
+
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
|
|
17
|
+
];
|
|
18
|
+
const bytes = typeof input === "string" ? new TextEncoder().encode(input) : input;
|
|
19
|
+
const bitLen = bytes.length * 8;
|
|
20
|
+
const padLen = ((bytes.length + 8) >> 6) + 1;
|
|
21
|
+
const words = new Uint32Array(padLen * 16);
|
|
22
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
23
|
+
words[i >> 2] |= bytes[i] << (24 - (i % 4) * 8);
|
|
24
|
+
}
|
|
25
|
+
words[bytes.length >> 2] |= 0x80 << (24 - (bytes.length % 4) * 8);
|
|
26
|
+
words[words.length - 2] = Math.floor(bitLen / 0x100000000);
|
|
27
|
+
words[words.length - 1] = bitLen >>> 0;
|
|
28
|
+
|
|
29
|
+
const w = new Uint32Array(64);
|
|
30
|
+
for (let c = 0; c < words.length; c += 16) {
|
|
31
|
+
for (let i = 0; i < 16; i++) w[i] = words[c + i];
|
|
32
|
+
for (let i = 16; i < 64; i++) {
|
|
33
|
+
const s0 = ((w[i - 15] >>> 7) | (w[i - 15] << 25)) ^
|
|
34
|
+
((w[i - 15] >>> 18) | (w[i - 15] << 14)) ^
|
|
35
|
+
(w[i - 15] >>> 3);
|
|
36
|
+
const s1 = ((w[i - 2] >>> 17) | (w[i - 2] << 15)) ^
|
|
37
|
+
((w[i - 2] >>> 19) | (w[i - 2] << 13)) ^
|
|
38
|
+
(w[i - 2] >>> 10);
|
|
39
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) | 0;
|
|
40
|
+
}
|
|
41
|
+
let a = H[0], b = H[1], c0 = H[2], d = H[3], e = H[4], f = H[5], g = H[6], h = H[7];
|
|
42
|
+
for (let i = 0; i < 64; i++) {
|
|
43
|
+
const s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
|
|
44
|
+
const ch = (e & f) ^ (~e & g);
|
|
45
|
+
const temp1 = (h + s1 + ch + K[i] + w[i]) | 0;
|
|
46
|
+
const s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
|
|
47
|
+
const maj = (a & b) ^ (a & c0) ^ (b & c0);
|
|
48
|
+
const temp2 = (s0 + maj) | 0;
|
|
49
|
+
h = g; g = f; f = e; e = (d + temp1) | 0;
|
|
50
|
+
d = c0; c0 = b; b = a; a = (temp1 + temp2) | 0;
|
|
51
|
+
}
|
|
52
|
+
H[0] = (H[0] + a) | 0;
|
|
53
|
+
H[1] = (H[1] + b) | 0;
|
|
54
|
+
H[2] = (H[2] + c0) | 0;
|
|
55
|
+
H[3] = (H[3] + d) | 0;
|
|
56
|
+
H[4] = (H[4] + e) | 0;
|
|
57
|
+
H[5] = (H[5] + f) | 0;
|
|
58
|
+
H[6] = (H[6] + g) | 0;
|
|
59
|
+
H[7] = (H[7] + h) | 0;
|
|
60
|
+
}
|
|
61
|
+
const out = new Uint8Array(32);
|
|
62
|
+
for (let i = 0; i < 8; i++) {
|
|
63
|
+
out[i * 4] = (H[i] >>> 24) & 0xff;
|
|
64
|
+
out[i * 4 + 1] = (H[i] >>> 16) & 0xff;
|
|
65
|
+
out[i * 4 + 2] = (H[i] >>> 8) & 0xff;
|
|
66
|
+
out[i * 4 + 3] = H[i] & 0xff;
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function createHash(algorithm) {
|
|
72
|
+
if (algorithm.toLowerCase() !== "sha256") {
|
|
73
|
+
throw new Error(`Unsupported hash algorithm in browser: ${algorithm}`);
|
|
74
|
+
}
|
|
75
|
+
let chunks = [];
|
|
76
|
+
return {
|
|
77
|
+
update(data) {
|
|
78
|
+
if (typeof data === "string") {
|
|
79
|
+
chunks.push(new TextEncoder().encode(data));
|
|
80
|
+
} else if (data instanceof Uint8Array) {
|
|
81
|
+
chunks.push(data);
|
|
82
|
+
} else {
|
|
83
|
+
chunks.push(new TextEncoder().encode(String(data)));
|
|
84
|
+
}
|
|
85
|
+
return this;
|
|
86
|
+
},
|
|
87
|
+
digest(encoding) {
|
|
88
|
+
let totalLength = chunks.reduce((acc, c) => acc + c.length, 0);
|
|
89
|
+
let merged = new Uint8Array(totalLength);
|
|
90
|
+
let offset = 0;
|
|
91
|
+
for (const c of chunks) {
|
|
92
|
+
merged.set(c, offset);
|
|
93
|
+
offset += c.length;
|
|
94
|
+
}
|
|
95
|
+
const raw = sha256(merged);
|
|
96
|
+
const buf = Buffer.from(raw);
|
|
97
|
+
if (encoding) return buf.toString(encoding);
|
|
98
|
+
return buf;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export default { createHash };
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { Buffer } from "./buffer.mjs";
|
|
2
|
+
import { vfsData } from "../vfs-data.mjs";
|
|
3
|
+
|
|
4
|
+
const vfs = new Map();
|
|
5
|
+
if (vfsData && typeof vfsData === "object") {
|
|
6
|
+
for (const [k, v] of Object.entries(vfsData)) {
|
|
7
|
+
vfs.set(k, v);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function cleanPath(p) {
|
|
12
|
+
let s = (p instanceof URL ? p.pathname : (p?.pathname || String(p))).replace(/\\/g, "/");
|
|
13
|
+
if (s.startsWith("file://")) s = s.replace(/^file:\/\//, "");
|
|
14
|
+
if (s.endsWith("runtime.wat")) return "backends/wasm/runtime.wat";
|
|
15
|
+
const exIdx = s.lastIndexOf("Examples/");
|
|
16
|
+
if (exIdx !== -1) return s.slice(exIdx);
|
|
17
|
+
const wasmIdx = s.lastIndexOf("backends/wasm/");
|
|
18
|
+
if (wasmIdx !== -1) return s.slice(wasmIdx);
|
|
19
|
+
const codecsIdx = s.lastIndexOf("codecs/");
|
|
20
|
+
if (codecsIdx !== -1) return s.slice(codecsIdx);
|
|
21
|
+
if (s.endsWith("/core.k") || s === "core.k") return "core.k";
|
|
22
|
+
if (s.startsWith("./")) s = s.slice(2);
|
|
23
|
+
while (s.startsWith("/")) s = s.slice(1);
|
|
24
|
+
return s;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function setVfsFile(path, content) {
|
|
28
|
+
const cleaned = cleanPath(path);
|
|
29
|
+
vfs.set(cleaned, content);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function getVfsFile(path) {
|
|
33
|
+
const cleaned = cleanPath(path);
|
|
34
|
+
if (vfs.has(cleaned)) return vfs.get(cleaned);
|
|
35
|
+
if (cleaned.startsWith("Examples/")) {
|
|
36
|
+
const stripped = cleaned.slice("Examples/".length);
|
|
37
|
+
if (vfs.has(stripped)) return vfs.get(stripped);
|
|
38
|
+
} else {
|
|
39
|
+
const withEx = `Examples/${cleaned}`;
|
|
40
|
+
if (vfs.has(withEx)) return vfs.get(withEx);
|
|
41
|
+
}
|
|
42
|
+
if (cleaned.startsWith("codecs/")) {
|
|
43
|
+
const stripped = cleaned.slice("codecs/".length);
|
|
44
|
+
if (vfs.has(stripped)) return vfs.get(stripped);
|
|
45
|
+
} else {
|
|
46
|
+
const withCodecs = `codecs/${cleaned}`;
|
|
47
|
+
if (vfs.has(withCodecs)) return vfs.get(withCodecs);
|
|
48
|
+
}
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const CURATED_VFS_FILES = [
|
|
53
|
+
"core.k",
|
|
54
|
+
"arithmetics.k",
|
|
55
|
+
"ieee.k",
|
|
56
|
+
"json.mjs",
|
|
57
|
+
"utf8.mjs",
|
|
58
|
+
"int.mjs",
|
|
59
|
+
"unit.mjs",
|
|
60
|
+
"ieee.mjs"
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
export function getAllVfsFiles() {
|
|
64
|
+
const result = new Set();
|
|
65
|
+
for (const name of CURATED_VFS_FILES) {
|
|
66
|
+
if (getVfsFile(name) !== undefined) {
|
|
67
|
+
result.add(name);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
for (const key of vfs.keys()) {
|
|
71
|
+
if (key.endsWith("runtime.wat") || key.startsWith("backends/")) continue;
|
|
72
|
+
if (key.startsWith("Examples/") || key.startsWith("codecs/")) continue;
|
|
73
|
+
result.add(key);
|
|
74
|
+
}
|
|
75
|
+
return Array.from(result);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function readFileSync(filePath, options) {
|
|
79
|
+
const data = getVfsFile(filePath);
|
|
80
|
+
if (data === undefined) {
|
|
81
|
+
throw new Error(`ENOENT: no such file or directory, open '${filePath}'`);
|
|
82
|
+
}
|
|
83
|
+
const encoding = typeof options === "string" ? options : options?.encoding;
|
|
84
|
+
if (encoding === "utf8" || encoding === "utf-8") {
|
|
85
|
+
if (typeof data === "string") return data;
|
|
86
|
+
return new TextDecoder().decode(data);
|
|
87
|
+
}
|
|
88
|
+
if (typeof data === "string") {
|
|
89
|
+
return Buffer.from(data, "utf8");
|
|
90
|
+
}
|
|
91
|
+
return Buffer.from(data);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function writeFileSync(filePath, data) {
|
|
95
|
+
const cleaned = cleanPath(filePath);
|
|
96
|
+
vfs.set(cleaned, data);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function existsSync(filePath) {
|
|
100
|
+
return getVfsFile(filePath) !== undefined;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function readdirSync(dirPath, options = {}) {
|
|
104
|
+
const cleanedDir = cleanPath(dirPath);
|
|
105
|
+
const prefix = (!cleanedDir || cleanedDir === ".") ? "" : (cleanedDir.endsWith("/") ? cleanedDir : cleanedDir + "/");
|
|
106
|
+
const results = new Set();
|
|
107
|
+
for (const key of vfs.keys()) {
|
|
108
|
+
if (prefix === "" || key.startsWith(prefix)) {
|
|
109
|
+
const rest = key.slice(prefix.length);
|
|
110
|
+
const name = rest.split("/")[0];
|
|
111
|
+
if (name) results.add(name);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const withFileTypes = options?.withFileTypes ?? false;
|
|
115
|
+
if (withFileTypes) {
|
|
116
|
+
return Array.from(results).map(name => {
|
|
117
|
+
const subPrefix = prefix + name + "/";
|
|
118
|
+
let isDir = false;
|
|
119
|
+
for (const k of vfs.keys()) {
|
|
120
|
+
if (k.startsWith(subPrefix)) {
|
|
121
|
+
isDir = true;
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
name,
|
|
127
|
+
isDirectory: () => isDir,
|
|
128
|
+
isFile: () => !isDir
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
return Array.from(results);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function realpathSync(p) {
|
|
136
|
+
return cleanPath(p);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function statSync(p) {
|
|
140
|
+
const data = readFileSync(p);
|
|
141
|
+
const size = typeof data === "string" ? new TextEncoder().encode(data).length : data.length;
|
|
142
|
+
return {
|
|
143
|
+
size,
|
|
144
|
+
isFile: () => true,
|
|
145
|
+
isDirectory: () => false
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export default {
|
|
150
|
+
readFileSync,
|
|
151
|
+
writeFileSync,
|
|
152
|
+
existsSync,
|
|
153
|
+
readdirSync,
|
|
154
|
+
realpathSync,
|
|
155
|
+
statSync,
|
|
156
|
+
setVfsFile,
|
|
157
|
+
getVfsFile,
|
|
158
|
+
getAllVfsFiles
|
|
159
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export function normalize(path) {
|
|
2
|
+
const parts = path.split("/").filter(Boolean);
|
|
3
|
+
const stack = [];
|
|
4
|
+
for (const part of parts) {
|
|
5
|
+
if (part === ".") continue;
|
|
6
|
+
if (part === "..") {
|
|
7
|
+
stack.pop();
|
|
8
|
+
} else {
|
|
9
|
+
stack.push(part);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
const prefix = path.startsWith("/") ? "/" : "";
|
|
13
|
+
return prefix + stack.join("/");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function join(...parts) {
|
|
17
|
+
return normalize(parts.filter(Boolean).join("/"));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function dirname(path) {
|
|
21
|
+
const normalized = normalize(path);
|
|
22
|
+
const idx = normalized.lastIndexOf("/");
|
|
23
|
+
if (idx === -1) return ".";
|
|
24
|
+
if (idx === 0) return "/";
|
|
25
|
+
return normalized.slice(0, idx);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function basename(path, ext) {
|
|
29
|
+
const normalized = normalize(path);
|
|
30
|
+
let base = normalized.slice(normalized.lastIndexOf("/") + 1);
|
|
31
|
+
if (ext && base.endsWith(ext)) {
|
|
32
|
+
base = base.slice(0, -ext.length);
|
|
33
|
+
}
|
|
34
|
+
return base;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function extname(path) {
|
|
38
|
+
const base = basename(path);
|
|
39
|
+
const idx = base.lastIndexOf(".");
|
|
40
|
+
return idx === -1 ? "" : base.slice(idx);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function resolve(...parts) {
|
|
44
|
+
return join(...parts);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export default {
|
|
48
|
+
normalize,
|
|
49
|
+
join,
|
|
50
|
+
dirname,
|
|
51
|
+
basename,
|
|
52
|
+
extname,
|
|
53
|
+
resolve
|
|
54
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export const argv = ["browser"];
|
|
2
|
+
export const env = {};
|
|
3
|
+
|
|
4
|
+
export function exit(code = 0) {
|
|
5
|
+
console.warn(`process.exit(${code}) called in browser`);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const stdin = {
|
|
9
|
+
on() {},
|
|
10
|
+
resume() {},
|
|
11
|
+
pause() {}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const stdout = {
|
|
15
|
+
write(chunk) {
|
|
16
|
+
console.log(chunk);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const hrtime = function() {
|
|
21
|
+
const now = performance.now();
|
|
22
|
+
const sec = Math.floor(now / 1000);
|
|
23
|
+
const nsec = Math.floor((now % 1000) * 1e6);
|
|
24
|
+
return [sec, nsec];
|
|
25
|
+
};
|
|
26
|
+
hrtime.bigint = function() {
|
|
27
|
+
return BigInt(Math.round(performance.now() * 1e6));
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const processShim = {
|
|
31
|
+
argv,
|
|
32
|
+
env,
|
|
33
|
+
exit,
|
|
34
|
+
stdin,
|
|
35
|
+
stdout,
|
|
36
|
+
hrtime,
|
|
37
|
+
versions: { wabt_browser: "1.0" }
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
if (typeof globalThis !== "undefined" && (!globalThis.process || !globalThis.process.hrtime)) {
|
|
41
|
+
globalThis.process = processShim;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default processShim;
|