Repository navigation
Expand file tree
/
Copy pathrun.js
More file actions
168 lines (152 loc) · 4.24 KB
/
Copy pathrun.js
File metadata and controls
168 lines (152 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"use strict";
let instance,
pollables = [],
pollableIndex = 0,
scriptResult,
pollablesToWaitForLength = 0;
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, Number(ms));
});
}
class Pollable {
constructor(promise, index) {
this.promise = promise.then(() => index);
this.index = index;
promise
.then(() => {
this.ready = true;
})
.catch(() => {
this.ready = true;
this.failed = true;
});
}
ready() {
return this.ready;
}
getIndex() {
return this.index;
}
getPromise() {
return this.promise.then(() => this);
}
}
function findPollable(id) {
for (const pollable of pollables) {
if (pollable.getIndex() === id) {
return pollable;
}
}
}
const importObject = {
"wasi:io/poll@0.2.1": {
poll: function (ptr, length, returnPtr) {
// this is a simplification, ie. arrays do not necessarily take 4 bytes of space
// per element, but in here I know we deal with i32 values
const pollableIds = new Uint32Array(
instance.exports.memory.buffer,
ptr,
length,
);
const pollablesToWaitFor = [];
for (const id of pollableIds) {
pollablesToWaitFor.push(findPollable(id).getPromise());
}
pollablesToWaitForLength = pollablesToWaitFor.length;
if (pollablesToWaitForLength === 0) {
if (typeof process !== "undefined") {
process.exit(scriptResult);
}
}
Promise.race(pollablesToWaitFor).then((ready) => {
const dataView = new DataView(instance.exports.memory.buffer);
dataView.setInt32(returnPtr, 1, true);
dataView.setInt32(returnPtr + 4, ready.getIndex(), true);
instance.exports["main_loop"]();
});
},
},
"wasi:clocks/monotonic-clock@0.2.1": {
"subscribe-duration": function (durationNanos) {
let index = pollableIndex;
let pollable = new Pollable(
sleep(durationNanos / BigInt(1000000)),
index,
);
pollables.push(pollable);
pollableIndex++;
return index;
},
},
console: { log: (value) => console.log(`WebAssembly log: ${value}`) },
wasi_snapshot_preview1: {
proc_exit(code) {
if (typeof process !== undefined) {
process.exit(code);
} else {
console.log("exit code: ", code);
}
},
fd_write(fd, iovsPtr, iovsLength, bytesWrittenPtr) {
const iovs = new Uint32Array(
instance.exports.memory.buffer,
iovsPtr,
iovsLength * 2,
);
if (fd === 1) {
//stdout
let text = "";
let totalBytesWritten = 0;
const decoder = new TextDecoder();
for (let i = 0; i < iovsLength * 2; i += 2) {
const offset = iovs[i];
const length = iovs[i + 1];
const textChunk = decoder.decode(
new Int8Array(instance.exports.memory.buffer, offset, length),
);
text += textChunk;
totalBytesWritten += length;
}
const dataView = new DataView(instance.exports.memory.buffer);
dataView.setInt32(bytesWrittenPtr, totalBytesWritten, true);
if (typeof process !== "undefined") {
process.stdout.write(text);
} else {
console.log(text);
}
}
return 0;
},
},
};
(async function () {
let bytes;
if (typeof process !== "undefined") {
const fs = require("node:fs");
const path = process.argv[2] || "out.wasm";
bytes = fs.readFileSync(path, { encoding: null });
} else if (typeof read !== "undefined") {
bytes = read("wasm/generated.wasm", "binary");
} else {
const response = await fetch("wasm/generated.wasm");
bytes = await response.arrayBuffer();
}
let compiled = await WebAssembly.compile(bytes, { builtins: ["js-string"] });
instance = await WebAssembly.instantiate(compiled, importObject);
const exports = instance.exports;
let entrypoint;
if (exports["wasi:cli/run@0.2.1#run"]) {
entrypoint = exports["wasi:cli/run@0.2.1#run"];
} else {
entrypoint = exports["_start"];
}
scriptResult = entrypoint();
if (pollablesToWaitForLength === 0) {
if (typeof process !== "undefined") {
process.exit(scriptResult);
}
}
})();