@@ -777,10 +777,10 @@ void emit_branch_if(Vec<uint8_t> &code, Allocator &al, uint32_t label_idx) {
777
777
778
778
void save_js_glue (std::string filename) {
779
779
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) {
781
781
const printNum = (num) => outputBuffer.push(num.toString());
782
782
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)));
784
784
const flushBuffer = () => {
785
785
stdout_print(outputBuffer.join(" ") + "\n");
786
786
outputBuffer.length = 0;
@@ -789,7 +789,6 @@ void save_js_glue(std::string filename) {
789
789
const cpu_time = (time) => (Date.now() / 1000); // Date.now() returns milliseconds, so divide by 1000
790
790
var imports = {
791
791
js: {
792
- memory: memory,
793
792
/* functions */
794
793
print_i32: printNum,
795
794
print_i64: printNum,
@@ -798,7 +797,8 @@ void save_js_glue(std::string filename) {
798
797
print_str: printStr,
799
798
flush_buf: flushBuffer,
800
799
set_exit_code: set_exit_code,
801
- cpu_time: cpu_time
800
+ cpu_time: cpu_time,
801
+ ...exports
802
802
},
803
803
};
804
804
return imports;
@@ -812,27 +812,37 @@ async function run_wasm(bytes, imports) {
812
812
} catch(e) { console.log(e); }
813
813
}
814
814
815
- async function execute_code(bytes, stdout_print) {
815
+ async function execute_code(bytes, exports, stdout_print) {
816
816
var exit_code = {val: 1}; /* non-zero exit code */
817
817
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);
820
820
await run_wasm(bytes, imports);
821
821
return exit_code.val;
822
822
}
823
823
824
- function main() {
824
+ function main(functionExports ) {
825
825
const fs = require("fs");
826
826
const wasmBuffer = fs.readFileSync(")" +
827
827
filename + R"( ");
828
- execute_code(wasmBuffer, (text) => process.stdout.write(text))
828
+ execute_code(wasmBuffer, functionExports, (text) => process.stdout.write(text))
829
829
.then((exit_code) => {
830
830
process.exit(exit_code);
831
831
})
832
832
.catch((e) => console.log(e))
833
833
}
834
834
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));
836
846
)" ;
837
847
filename += " .js" ;
838
848
std::ofstream out (filename);
0 commit comments