Skip to content

Commit 059a524

Browse files
authored
Merge pull request #64 from lfortran/wasi_support
WASI support
2 parents 30cce80 + d3dea2e commit 059a524

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set -ex
55
# curl https://lfortran.github.io/wasm_builds/data.json -o data.json
66
#latest_commit=`curl https://lfortran.github.io/wasm_builds/dev/latest_commit`
77
# Set a specific commit to use:
8-
latest_commit="df33df422"
8+
latest_commit="c969e0bc3"
99
curl "https://lfortran.github.io/wasm_builds/dev/$latest_commit/lfortran.js" -o public/lfortran.js
1010
curl "https://lfortran.github.io/wasm_builds/dev/$latest_commit/lfortran.wasm" -o public/lfortran.wasm
1111
curl "https://lfortran.github.io/wasm_builds/dev/$latest_commit/lfortran.data" -o public/lfortran.data

components/LoadLFortran.js

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@ function getLfortranExportedFuncs() {
1717
});
1818
}
1919

20-
function define_imports(memory, outputBuffer, exit_code, stdout_print) {
21-
const printNum = (num) => outputBuffer.push(num.toString());
22-
const printStr = (startIdx, strSize) => outputBuffer.push(
23-
new TextDecoder("utf8").decode(new Uint8Array(memory.buffer, startIdx, strSize)));
20+
var memory;
21+
function define_imports(outputBuffer, exit_code, stdout_print) {
2422
const flushBuffer = () => {
25-
stdout_print(outputBuffer.join(" ") + "\n");
23+
stdout_print(outputBuffer.join(""));
2624
outputBuffer.length = 0;
2725
}
28-
const set_exit_code = (exit_code_val) => exit_code.val = exit_code_val;
26+
const fd_write = (file_type, iov_location, no_of_iovs, return_val_mem_loc) => {
27+
const mem_data = new DataView(memory.buffer, iov_location, Int32Array.BYTES_PER_ELEMENT * 2);
28+
const strLoc = mem_data.getInt32(0, true);
29+
const strLen = mem_data.getInt32(4, true);
30+
const s = new TextDecoder("utf8").decode(new Uint8Array(memory.buffer, strLoc, strLen));
31+
outputBuffer.push(s);
32+
return 0;
33+
}
34+
const proc_exit = (exit_code_val) => exit_code.val = exit_code_val;
2935
const cpu_time = (time) => (Date.now() / 1000); // Date.now() returns milliseconds, so divide by 1000
3036
const show_image = (cols, rows, arr) => {
3137
var arr2D_data = new DataView(memory.buffer, arr, Int32Array.BYTES_PER_ELEMENT * rows * cols);
@@ -41,7 +47,7 @@ function define_imports(memory, outputBuffer, exit_code, stdout_print) {
4147
imgData.data[i + 3] = 255; // alpha channel (from 0-255), 0 is transparent and 255 is fully visible
4248
}
4349
ctx.putImageData(imgData, 0, 0);
44-
outputBuffer.push(`<img alt="constructed image" src="${canvas.toDataURL('image/jpeg')}" height="${rows}" width="${cols}" style="aspect-ratio: 1 / 1;"/>`)
50+
outputBuffer.push(`<img alt="constructed image" src="${canvas.toDataURL('image/jpeg')}" height="${rows}" width="${cols}" style="aspect-ratio: 1 / 1;"/>\n`)
4551
flushBuffer();
4652
}
4753
const show_image_color = (cols, rows, arr) => {
@@ -59,20 +65,17 @@ function define_imports(memory, outputBuffer, exit_code, stdout_print) {
5965
imgData.data[i] = arr2D_data.getInt32(4*i, true);
6066
}
6167
ctx.putImageData(imgData, 0, 0);
62-
outputBuffer.push(`<img alt="constructed image" src="${canvas.toDataURL('image/jpeg')}" height="${rows}" width="${cols}" style="aspect-ratio: 1 / 1;"/>`)
68+
outputBuffer.push(`<img alt="constructed image" src="${canvas.toDataURL('image/jpeg')}" height="${rows}" width="${cols}" style="aspect-ratio: 1 / 1;"/>\n`)
6369
flushBuffer();
6470
}
6571
var imports = {
72+
wasi_snapshot_preview1: {
73+
/* wasi functions */
74+
fd_write: fd_write,
75+
proc_exit: proc_exit,
76+
},
6677
js: {
67-
memory: memory,
68-
/* functions */
69-
print_i32: printNum,
70-
print_i64: printNum,
71-
print_f32: printNum,
72-
print_f64: printNum,
73-
print_str: printStr,
74-
flush_buf: flushBuffer,
75-
set_exit_code: set_exit_code,
78+
/* custom functions */
7679
cpu_time: cpu_time,
7780
show_img: show_image,
7881
show_img_color: show_image_color
@@ -81,15 +84,6 @@ function define_imports(memory, outputBuffer, exit_code, stdout_print) {
8184
return imports;
8285
}
8386

84-
async function run_wasm(bytes, imports) {
85-
try {
86-
var res = await WebAssembly.instantiate(bytes, imports);
87-
const { _lcompilers_main } = res.instance.exports;
88-
_lcompilers_main();
89-
} catch(e) { return e; }
90-
return "Success"
91-
}
92-
9387
async function setup_lfortran_funcs(lfortran_funcs, myPrint) {
9488
const compiler_funcs = await getLfortranExportedFuncs();
9589

@@ -133,15 +127,20 @@ async function setup_lfortran_funcs(lfortran_funcs, myPrint) {
133127
lfortran_funcs.execute_code = async function (bytes, stdout_print) {
134128
var exit_code = {val: 1}; /* non-zero exit code */
135129
var outputBuffer = [];
136-
var memory = new WebAssembly.Memory({ initial: 100, maximum: 100 }); // fixed 6.4 Mb memory currently
137-
var imports = define_imports(memory, outputBuffer, exit_code, stdout_print);
138-
var err_msg = await run_wasm(bytes, imports);
139-
if (exit_code.val == 0) {
140-
return 1;
130+
var imports = define_imports(outputBuffer, exit_code, stdout_print);
131+
try {
132+
var res = await WebAssembly.instantiate(bytes, imports);
133+
memory = res.instance.exports.memory;
134+
res.instance.exports._start();
135+
stdout_print(outputBuffer.join(""));
136+
} catch(err_msg) {
137+
stdout_print(outputBuffer.join(""));
138+
if (exit_code.val == 0) {
139+
return;
140+
}
141+
console.log(err_msg);
142+
stdout_print(`\n${err_msg}\nERROR: The code could not be executed. Either there is a runtime error or there is an issue at our end.`);
141143
}
142-
console.log(err_msg);
143-
myPrint(err_msg + "\nERROR: The code could not be executed. Either there is a runtime error or there is an issue at our end.");
144-
return 0;
145144
};
146145
}
147146

pages/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ export default function Home() {
6767
new Uint8Array(compile_result),
6868
(text) => stdout.push(text)
6969
);
70-
if (exec_res) {
71-
setOutput(stdout.join(""));
72-
}
70+
setOutput(stdout.join(""));
7371
}
7472
}
7573
} else if (key == "AST") {

0 commit comments

Comments
 (0)