Closed poorna2152 closed 2 years ago
Using this method doesn't allow us to push content to an array because array length is predefined. What can be done?
Option 2 is the way to go, look at array in C runtime. we do the same there.
Another option is to wrap javascript array shall we give that a try.
Array provides gc support in wasm. If we use C or Js wouldn't it remove that advantage
1) Not to use C, look at the algorithm we use to grow an array and replicate it in wasm. 2) JS should get GC shouldn't it? I am not sure how it happens when ref escapes to wasm land.
If we are to create a new array when pushing to an array. Then we change the reference of the array. Then the following program fails.
import ballerina/io;
public function main() {
any[] v = [0];
any[] y = v;
io:println(v === y); // @output true
v.push(0);
io:println(v === y); // @output true
}
The expected output should be true
, true
. But the output we get is true
, false
because when pushing reference gets changed.
I think I didn't make myself clear enough in above 1.
You will need to create a fixed length array and warp it with a another GC-able structure such as struct and return that.
Provide a function that will accept an element and a wrapper (let's call this a list
) and insert to the inner array.
It should grow the inner array by some factor if it runs out, look at the ballerina runtime C code to get the exact starting length and factor.
Current implementation:
Using a struct
to represent a list.
(type $List (struct (field $arr (mut (ref $AnyList))) (field $len (mut i64))))
(type $AnyList (array (mut eqref)))
$arr
: which stores the array and the $len
: which stores the length of the array.
Using the array type described in the gc proposal to represent the lists.
Sample
Procedure:
Define new array type which can set of store anyref values:
(type $any_list (array (mut anyref)))
Create a variable of the above mentioned type with the length of the array. First parameter is the length of the array:
(array.new_default_with_rtt $any_list (i32.const 3) (rtt.canon $any_list)))
Set the values of the array.
(array.set $any_list (local.get $3) (i32.const 0) (local.get $0))
Call
println
function with the array reference.(call $println (local.get $3))
println
checks the type of the reference passed to it using theget_type
function exported from the wasm module.If the reference is of type array then println uses the exported function
len
from the wasm module to get the length of the array.Loop over the length of the array get each element by passing the array reference and index to the
array_get
function exported from the wasm module.Print the values.