Skip to content

Commit

Permalink
Merge pull request #143 from wasmerio/WR-20
Browse files Browse the repository at this point in the history
Implement the Memory API
  • Loading branch information
jubianchi authored May 18, 2021
2 parents 7a28c98 + 3057664 commit 116eb04
Show file tree
Hide file tree
Showing 31 changed files with 891 additions and 103 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/doc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ name: Documentation

on:
pull_request:
branches-ignore:
- '**'
push:
branches:
- 'master'
tags:
branches-ignore:
- '**'
tags-ignore:
- '**'

jobs:
test:
Expand All @@ -15,7 +18,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.0
- name: Checkout code
uses: actions/checkout@v2
- name: Get Composer Cache Directory
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

## **[Unreleased]**

### Added

* Introduce the `MemoryView` class to work with memory contents
* Implement the memory API as a 1:1 binding on the C API

## **[1.0.0] - 2021-02-22**

Expand Down Expand Up @@ -83,5 +89,6 @@ wasm_func_call($run, new Wasm\Vec\Val());
* Implement the `trap` API


[Unreleased]: https://github.com/wasmerio/wasmer-php/tree/master/README.md
[1.0.0]: https://github.com/wasmerio/wasmer-php/tree/1.0.0/README.md
[1.0.0-beta1]: https://github.com/wasmerio/wasmer-php/tree/1.0.0-beta1/README.md
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ add_compile_definitions(HAVE_WASM)
set(WASMER_API ext/src/api/config.c ext/src/api/engine.c ext/src/api/store.c ext/src/api/wasmer.c ext/src/api/wat.c)
set(WASMER_API_OBJECTS ext/src/api/objects/extern.c ext/src/api/objects/foreign.c ext/src/api/objects/func.c ext/src/api/objects/frame.c ext/src/api/objects/global.c ext/src/api/objects/instance.c ext/src/api/objects/memory.c ext/src/api/objects/module.c ext/src/api/objects/table.c ext/src/api/objects/trap.c ext/src/api/objects/val.c)
set(WASMER_API_TYPES ext/src/api/types/exporttype.c ext/src/api/types/externtype.c ext/src/api/types/functype.c ext/src/api/types/globaltype.c ext/src/api/types/importtype.c ext/src/api/types/limits.c ext/src/api/types/memorytype.c ext/src/api/types/tabletype.c ext/src/api/types/valkind.c ext/src/api/types/valtype.c)
set(WASMER_SOURCES ext/src/php_wasm.h ext/src/wasm.c ext/src/wasmer_root_arginfo.h)
set(WASMER_SOURCES ext/src/php_wasm.h ext/src/wasm.c ext/src/wasmer_class_arginfo.h ext/src/wasmer_exception_arginfo.h ext/src/wasmer_root_arginfo.h ext/src/wasmer_vec_arginfo.h)
set(WASMER_ALL_SOURCES ${WASMER_API} ${WASMER_API_OBJECTS} ${WASMER_API_TYPES} ${WASMER_SOURCES})

execute_process (
Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ endif

ext/configure: ext/Makefile

.PHONY: ext/examples
ext/test: export NO_INTERACTION = 1
ext/all ext/examples ext/test: ext/configure
@cd ext; make $(subst ext/,,$@)
@cd ext; make $(subst ext/,,$@)

ext/clean: ext/Makefile
mv ext/lib/libwasmer.so ext/lib/libwasmer.so.keep
@cd ext; make clean distclean
rm -rf ext/autom4te.cache ext/build ext/modules ext/config.h ext/config.h.in ext/config.nice ext/configure ext/configure.ac ext/run-tests.php
mv ext/lib/libwasmer.so.keep ext/lib/libwasmer.so
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ You can go through more advanced examples in the dedicated directories:
| func ||
| global ||
| table | 🧑‍💻 |
| memory | 🧑‍💻 |
| memory | |
| extern ||
| instance ||

Expand Down
2 changes: 1 addition & 1 deletion ext/Makefile.frag
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ examples: all
fi;

all: ## Build PHP module
all: src/wasmer_root_arginfo.h src/wasmer_vec_arginfo.h src/wasmer_exception_arginfo.h
all: src/wasmer_root_arginfo.h src/wasmer_vec_arginfo.h src/wasmer_exception_arginfo.h src/wasmer_class_arginfo.h

test: ## Run PHP module tests

Expand Down
59 changes: 59 additions & 0 deletions ext/examples/memory.php
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);
16 changes: 16 additions & 0 deletions ext/examples/memory.wat
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))
)
Loading

0 comments on commit 116eb04

Please sign in to comment.