@@ -17,15 +17,21 @@ function getLfortranExportedFuncs() {
17
17
} ) ;
18
18
}
19
19
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 ) {
24
22
const flushBuffer = ( ) => {
25
- stdout_print ( outputBuffer . join ( " " ) + "\n" ) ;
23
+ stdout_print ( outputBuffer . join ( "" ) ) ;
26
24
outputBuffer . length = 0 ;
27
25
}
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 ;
29
35
const cpu_time = ( time ) => ( Date . now ( ) / 1000 ) ; // Date.now() returns milliseconds, so divide by 1000
30
36
const show_image = ( cols , rows , arr ) => {
31
37
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) {
41
47
imgData . data [ i + 3 ] = 255 ; // alpha channel (from 0-255), 0 is transparent and 255 is fully visible
42
48
}
43
49
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 ` )
45
51
flushBuffer ( ) ;
46
52
}
47
53
const show_image_color = ( cols , rows , arr ) => {
@@ -59,20 +65,17 @@ function define_imports(memory, outputBuffer, exit_code, stdout_print) {
59
65
imgData . data [ i ] = arr2D_data . getInt32 ( 4 * i , true ) ;
60
66
}
61
67
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 ` )
63
69
flushBuffer ( ) ;
64
70
}
65
71
var imports = {
72
+ wasi_snapshot_preview1 : {
73
+ /* wasi functions */
74
+ fd_write : fd_write ,
75
+ proc_exit : proc_exit ,
76
+ } ,
66
77
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 */
76
79
cpu_time : cpu_time ,
77
80
show_img : show_image ,
78
81
show_img_color : show_image_color
@@ -81,15 +84,6 @@ function define_imports(memory, outputBuffer, exit_code, stdout_print) {
81
84
return imports ;
82
85
}
83
86
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
-
93
87
async function setup_lfortran_funcs ( lfortran_funcs , myPrint ) {
94
88
const compiler_funcs = await getLfortranExportedFuncs ( ) ;
95
89
@@ -133,15 +127,20 @@ async function setup_lfortran_funcs(lfortran_funcs, myPrint) {
133
127
lfortran_funcs . execute_code = async function ( bytes , stdout_print ) {
134
128
var exit_code = { val : 1 } ; /* non-zero exit code */
135
129
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.` ) ;
141
143
}
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 ;
145
144
} ;
146
145
}
147
146
0 commit comments