Skip to content

Commit

Permalink
Introduce basic improts
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Apr 17, 2023
1 parent 188e708 commit 47d1f7e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
19 changes: 19 additions & 0 deletions examples/imports.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

$instance = Wasm\InstanceBuilder::fromWat(
<<<'EOWAT'
(module
(import "env" "global" (global $global (mut i32)))
(func (export "read_g") (result i32)
global.get $global)
(func (export "write_g") (param i32)
local.get 0
global.set $global))
EOWAT
)->withImports([
'env' => [
'global' => 33
]
])->build();

var_dump($instance->read_g());
14 changes: 8 additions & 6 deletions ext-wasm.stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
// Stubs for ext-wasm

namespace Wasm {
class InstanceBuilder {
public static function fromWat(string $wat): \Wasm\InstanceBuilder {}

public function build(): \Wasm\WasmInstance {}
}

class WasmInstance {
public static function fromBuilder(\Wasm\InstanceBuilder $builder): \Wasm\WasmInstance {}

Expand All @@ -18,4 +12,12 @@ public function __get(string $accessor): mixed {}

public function __set(string $accessor, mixed $value): mixed {}
}

class InstanceBuilder {
public static function fromWat(string $wat): \Wasm\InstanceBuilder {}

public function withImports(array $imports): \Wasm\InstanceBuilder {}

public function build(): \Wasm\WasmInstance {}
}
}
41 changes: 38 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

mod types;

use std::collections::HashMap;

use ext_php_rs::prelude::*;
use ext_php_rs::zend::ModuleEntry;
use ext_php_rs::types::ZendClassObject;
use ext_php_rs::types::{ZendClassObject};
use ext_php_rs::*;
use crate::types::Value;

Expand Down Expand Up @@ -60,25 +62,58 @@ impl WasmInstance {
}
}

type ImportsType = HashMap<String, HashMap<String, Value>>;

#[php_class(name="Wasm\\InstanceBuilder")]
#[derive(Default)]
pub struct InstanceBuilder {
pub wat: Box<String>,
pub imports : Box<ImportsType>,
}

#[php_impl]
impl InstanceBuilder {
pub fn from_wat(wat: String) -> InstanceBuilder {
InstanceBuilder {
wat: wat.clone().into()
wat: wat.clone().into(),
..Default::default()
}.into()
}

pub fn with_imports(
#[this] this: &mut ZendClassObject<InstanceBuilder>,
imports: ImportsType
) -> &mut ZendClassObject<InstanceBuilder> {
this.imports = imports.into();

this
}

pub fn build(&mut self) -> PhpResult<WasmInstance> {
let mut store = wasmer::Store::default();
let module = wasmer::Module::new(&store, &*self.wat)
.map_err(|err| PhpException::default(err.to_string()))?;

// Build imports
let mut import_object = wasmer::Imports::new();
for (namespace_name, namespace_dict) in (&*self.imports).into_iter() {
let namespace_name = namespace_name.to_string();
let namespace_dict_wasmer: Vec<(String, wasmer::Extern)> = namespace_dict
.into_iter()
.map(|(key, value)| (
key.clone(),
wasmer::Extern::Global(
wasmer::Global::new_mut(&mut store, value.clone().into()) // TODO : define mutability..
)
))
.collect();

import_object.register_namespace(
&namespace_name,
namespace_dict_wasmer
);
}

let import_object = wasmer::imports! {};
let instance = wasmer::Instance::new(&mut store, &module, &import_object)
.map_err(|err| PhpException::default(err.to_string()))?;

Expand Down

0 comments on commit 47d1f7e

Please sign in to comment.