-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from wasmerio/WR-20
Implement the Memory API
- Loading branch information
Showing
31 changed files
with
891 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
echo 'Initializing...'.PHP_EOL; | ||
$engine = wasm_engine_new(); | ||
$store = wasm_store_new($engine); | ||
|
||
echo 'Loading binary...'.PHP_EOL; | ||
$wat = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'memory.wat'); | ||
|
||
echo 'Loading binary...'.PHP_EOL; | ||
$wasm = wat2wasm($wat); | ||
|
||
echo 'Compiling module...'.PHP_EOL; | ||
$module = wasm_module_new($store, $wasm); | ||
|
||
echo 'Instantiating module...'.PHP_EOL; | ||
$externs = new Wasm\Vec\Extern(); | ||
$instance = wasm_instance_new($store, $module, $externs); | ||
|
||
echo 'Extracting export...'.PHP_EOL; | ||
$exports = wasm_instance_exports($instance); | ||
$getAt = wasm_extern_as_func($exports[0]); | ||
$setAt = wasm_extern_as_func($exports[1]); | ||
$memSize = wasm_extern_as_func($exports[2]); | ||
$memory = wasm_extern_as_memory($exports[3]); | ||
|
||
wasm_module_delete($module); | ||
wasm_instance_delete($instance); | ||
|
||
echo 'Querying memory size...'.PHP_EOL; | ||
assert(1 === wasm_memory_size($memory)); | ||
assert(65536 === wasm_memory_data_size($memory)); | ||
|
||
$result = wasm_func_call($memSize, new Wasm\Vec\Val()); | ||
assert(1 === wasm_val_value($result[0])); | ||
|
||
echo 'Growing memory...'.PHP_EOL; | ||
wasm_memory_grow($memory, 2); | ||
assert(3 === wasm_memory_size($memory)); | ||
assert(wasm_memory_data_size($memory) === 65536 * 3); | ||
|
||
$memAddr = 0; | ||
$val = 5; | ||
wasm_func_call($setAt, new Wasm\Vec\Val([ | ||
wasm_val_i32($memAddr), | ||
wasm_val_i32($val), | ||
])); | ||
|
||
$result = wasm_func_call($getAt, new Wasm\Vec\Val([wasm_val_i32($memAddr)])); | ||
assert(wasm_val_value($result[0]) === $val); | ||
|
||
$view = wasm_memory_data($memory); | ||
assert($view->getI32($memAddr) == wasm_val_value($result[0])); | ||
|
||
echo 'Shutting down...'.PHP_EOL; | ||
wasm_store_delete($store); | ||
wasm_engine_delete($engine); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
(module | ||
(type $mem_size_t (func (result i32))) | ||
(type $get_at_t (func (param i32) (result i32))) | ||
(type $set_at_t (func (param i32) (param i32))) | ||
(memory $mem 1) | ||
(func $get_at (type $get_at_t) (param $idx i32) (result i32) | ||
(i32.load (local.get $idx))) | ||
(func $set_at (type $set_at_t) (param $idx i32) (param $val i32) | ||
(i32.store (local.get $idx) (local.get $val))) | ||
(func $mem_size (type $mem_size_t) (result i32) | ||
(memory.size)) | ||
(export "get_at" (func $get_at)) | ||
(export "set_at" (func $set_at)) | ||
(export "mem_size" (func $mem_size)) | ||
(export "memory" (memory $mem)) | ||
) |
Oops, something went wrong.