-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Supplying a memory instance to a function before execution #38
Comments
It is very difficult for us to maintain the memory structure myself. The more I suggest is by importing external functions such as: Source C code char data[1024] = {};
extern int load_data(int addr_from, int addr_to);
int main() {
load_data(&data[0], &data[1023]);
return data[0];
} Compile C to wasm program (module
(type $FUNCSIG$iii (func (param i32 i32) (result i32)))
(import "env" "load_data" (func $load_data (param i32 i32) (result i32)))
(table 0 anyfunc)
(memory $0 1)
(export "memory" (memory $0))
(export "main" (func $main))
(func $main (; 1 ;) (result i32)
(drop
(call $load_data
(i32.const 16)
(i32.const 1039)
)
)
(i32.load8_s offset=16
(i32.const 0)
)
)
) And used in pywasm as import pywasm
your_data = [4 for i in range(1024)]
def load_data(ctx: pywasm.Ctx, addr_from: int, addr_to: int):
ctx.memory_list[0].data[addr_from:addr_to] = your_data
return 0
runtime = pywasm.load('/tmp/program.wasm', {'env': {'load_data': load_data}})
r = runtime.exec('main', [])
print(r) # 4 |
Nice! I think it might also work this way: data = [1,2,3,4,5]
gamestate = pywasm.Memory(pywasm.binary.MemoryType())
gamestate.grow(len(data))
for figindex, fig in enumerate(data):
gamestate.data[figindex] = fig
runtime = pywasm.load(path, {
'env': {
'abort': env_abort,
"gamestate": gamestate
}
}, opts=option) |
You did not use it correctly. The memory is managed by your program. For example, if your program uses alloc to apply for a block of memory (always starting from address 0), then this will overwrite your initial data. |
Where you should write data to the memory, this information should always come from your program, let it decide. |
In this case, I may have to inject the function call manually somehow if I did it this way. The AssemblyScript compiler seems to have an --importMemory flag (also here), which imports env.memory. But I didn't get it to work yet, it fails with runtime = pywasm.load(path, {
'env': {
'abort': env_abort,
"memory": gamestate
}
}, opts=option)
You are probably right, but I'll try it this way one more time |
Okay, I give up my alternative attempt, your solution works, and probably saves memory too. Thank you :) |
So is it impossible to supply memories as inputs? I ran into the same exception void4 did when I tried it. |
In Python, I have a list of integers in the range 0-255, so in the uint8 range.
I want to supply them to the WebAssembly runtime before it starts execution, so the called function will be able to access it when it runs. Can I do this with pywasm?
It might look like this:
The text was updated successfully, but these errors were encountered: