Coder Social home page Coder Social logo

Comments (7)

mohanson avatar mohanson commented on September 18, 2024

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.

void4 avatar void4 commented on September 18, 2024

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.

mohanson avatar mohanson commented on September 18, 2024

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.

mohanson avatar mohanson commented on September 18, 2024

Where you should write data to the memory, this information should always come from your program, let it decide.

from pywasm.

void4 avatar void4 commented on September 18, 2024

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.

void4 avatar void4 commented on September 18, 2024

Okay, I give up my alternative attempt, your solution works, and probably saves memory too. Thank you :)

from pywasm.

prescod avatar prescod commented on September 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.