Comments (7)
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
from pywasm.
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)
from pywasm.
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.
from pywasm.
Where you should write data to the memory, this information should always come from your program, let it decide.
from pywasm.
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)
Traceback (most recent call last):
File "main.py", line 131, in <module>
value = runWasm(players[board.turn], board)
File "main.py", line 46, in runWasm
runtime = pywasm.load(path, {
File "/home/one/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pywasm/__init__.py", line 99, in load
return Runtime(module, imps, opts)
File "/home/one/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pywasm/__init__.py", line 57, in __init__
self.machine.instantiate(module, extern_value_list)
File "/home/one/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pywasm/execution.py", line 1984, in instantiate
assert match_memory(a, b)
You are probably right, but I'll try it this way one more time
from pywasm.
Okay, I give up my alternative attempt, your solution works, and probably saves memory too. Thank you :)
from pywasm.
So is it impossible to supply memories as inputs? I ran into the same exception void4 did when I tried it.
from pywasm.
Related Issues (19)
- Pop out value from stack when call/invoke ends. HOT 3
- Support WASI(WebAssembly System Interface). HOT 10
- Issues with importing libraries HOT 4
- The big plan of Ancient: pywasm 1.0 HOT 3
- The structure of memoryInstance. HOT 6
- implement data count section
- Limiting the number of instruction steps HOT 4
- API Documents HOT 1
- Pywasm 1.0.4 does not install HOT 2
- Exception: pywasm: missing import env.time HOT 3
- Continuation of supplying a memory instance to a function before execution HOT 2
- Allow "minimum" and "maximum" in constructor for Limits object HOT 2
- Pythons -O flag makes an insane difference HOT 6
- Reattaching imported functions HOT 2
- Resuming a program where it left off when it reached a resource limit
- Example code has no print to terminal HOT 2
- Results seem to be different from wasmer HOT 1
- Cannot use "from pywasm import allocate_bytes, write_to_memory, address_to_utf8" HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pywasm.