Skip to content

Commit d9f1049

Browse files
committed
WASM: Update JS glue to utilize rt_lib.wasm
1 parent f2ab275 commit d9f1049

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/libasr/codegen/wasm_assembler.h

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -777,10 +777,10 @@ void emit_branch_if(Vec<uint8_t> &code, Allocator &al, uint32_t label_idx) {
777777

778778
void save_js_glue(std::string filename) {
779779
std::string js_glue =
780-
R"(function define_imports(memory, outputBuffer, exit_code, stdout_print) {
780+
R"(function define_imports(exports, outputBuffer, exit_code, stdout_print) {
781781
const printNum = (num) => outputBuffer.push(num.toString());
782782
const printStr = (startIdx, strSize) => outputBuffer.push(
783-
new TextDecoder("utf8").decode(new Uint8Array(memory.buffer, startIdx, strSize)));
783+
new TextDecoder("utf8").decode(new Uint8Array(exports.memory.buffer, startIdx, strSize)));
784784
const flushBuffer = () => {
785785
stdout_print(outputBuffer.join(" ") + "\n");
786786
outputBuffer.length = 0;
@@ -789,7 +789,6 @@ void save_js_glue(std::string filename) {
789789
const cpu_time = (time) => (Date.now() / 1000); // Date.now() returns milliseconds, so divide by 1000
790790
var imports = {
791791
js: {
792-
memory: memory,
793792
/* functions */
794793
print_i32: printNum,
795794
print_i64: printNum,
@@ -798,7 +797,8 @@ void save_js_glue(std::string filename) {
798797
print_str: printStr,
799798
flush_buf: flushBuffer,
800799
set_exit_code: set_exit_code,
801-
cpu_time: cpu_time
800+
cpu_time: cpu_time,
801+
...exports
802802
},
803803
};
804804
return imports;
@@ -812,27 +812,37 @@ async function run_wasm(bytes, imports) {
812812
} catch(e) { console.log(e); }
813813
}
814814
815-
async function execute_code(bytes, stdout_print) {
815+
async function execute_code(bytes, exports, stdout_print) {
816816
var exit_code = {val: 1}; /* non-zero exit code */
817817
var outputBuffer = [];
818-
var memory = new WebAssembly.Memory({ initial: 100, maximum: 100 }); // fixed 6.4 Mb memory currently
819-
var imports = define_imports(memory, outputBuffer, exit_code, stdout_print);
818+
// var memory = new WebAssembly.Memory({ initial: 100, maximum: 100 }); // fixed 6.4 Mb memory currently
819+
var imports = define_imports(exports, outputBuffer, exit_code, stdout_print);
820820
await run_wasm(bytes, imports);
821821
return exit_code.val;
822822
}
823823
824-
function main() {
824+
function main(functionExports) {
825825
const fs = require("fs");
826826
const wasmBuffer = fs.readFileSync(")" +
827827
filename + R"(");
828-
execute_code(wasmBuffer, (text) => process.stdout.write(text))
828+
execute_code(wasmBuffer, functionExports, (text) => process.stdout.write(text))
829829
.then((exit_code) => {
830830
process.exit(exit_code);
831831
})
832832
.catch((e) => console.log(e))
833833
}
834834
835-
main();
835+
async function main2() {
836+
const fs = require("fs");
837+
const wasi = require('wasi');
838+
const importOjbect = {wasi_snapshot_preview1: (new wasi.WASI()).wasiImport}
839+
const wasmBuffer = fs.readFileSync("src/libasr/runtime/rt_lib.wasm");
840+
var res = await WebAssembly.instantiate(wasmBuffer, importOjbect);
841+
const exports = res.instance.exports;
842+
main(exports);
843+
}
844+
845+
main2().then().catch((e) => console.log(e));
836846
)";
837847
filename += ".js";
838848
std::ofstream out(filename);

0 commit comments

Comments
 (0)