Initial reorg.

This is largely the same as #305, but updated for the current tree.
This commit is contained in:
Dan Gohman
2019-11-07 17:11:06 -08:00
parent 2c69546a24
commit 22641de629
351 changed files with 52 additions and 52 deletions

15
crates/misc/py/.gitignore vendored Normal file
View File

@@ -0,0 +1,15 @@
*.bk
*.swp
*.swo
*.swx
tags
target
Cargo.lock
.*.rustfmt
cranelift.dbg*
rusty-tags.*
*~
\#*\#
build
dist
*.egg-info

31
crates/misc/py/Cargo.toml Normal file
View File

@@ -0,0 +1,31 @@
[package]
name = "wasmtime-py"
version = "0.2.0"
authors = ["The Wasmtime Project Developers"]
description = "Python extension for Wasmtime"
license = "Apache-2.0 WITH LLVM-exception"
categories = ["wasm", "python"]
edition = "2018"
[lib]
name = "_wasmtime"
crate-type = ["cdylib"]
[dependencies]
cranelift-codegen = { version = "0.49" }
cranelift-native = { version = "0.49" }
cranelift-entity = { version = "0.49" }
cranelift-wasm = { version = "0.49" }
cranelift-frontend = { version = "0.49" }
wasmtime-environ = { path = "../../environ" }
wasmtime-interface-types = { path = "../../interface-types" }
wasmtime-jit = { path = "../../jit" }
wasmtime-runtime = { path = "../../runtime" }
target-lexicon = { version = "0.9.0", default-features = false }
anyhow = "1.0.19"
region = "2.0.0"
wasmparser = "0.39.2"
[dependencies.pyo3]
version = "0.8.0"
features = ["extension-module"]

220
crates/misc/py/LICENSE Normal file
View File

@@ -0,0 +1,220 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--- LLVM Exceptions to the Apache 2.0 License ----
As an exception, if, as a result of your compiling your source code, portions
of this Software are embedded into an Object form of such source code, you
may redistribute such embedded portions in such Object form without complying
with the conditions of Sections 4(a), 4(b) and 4(d) of the License.
In addition, if you combine or link compiled forms of this Software with
software that is licensed under the GPLv2 ("Combined Software") and if a
court of competent jurisdiction determines that the patent provision (Section
3), the indemnity provision (Section 9) or other Section of the License
conflicts with the conditions of the GPLv2, you may retroactively and
prospectively choose to deem waived or otherwise exclude such Section(s) of
the License, but only in their entirety and only with respect to the Combined
Software.

20
crates/misc/py/README.md Normal file
View File

@@ -0,0 +1,20 @@
Python 3 extension for interface with Wasmtime/Cranelift.
# Build
First, you'll need to install some Python dependencies:
```
$ pip3 install setuptools wheel==0.31.1 setuptools-rust
```
Next you can build the extension with:
```
rustup run nightly python3 setup.py build
```
Note that a nightly version of Rust is required due to our usage of PyO3.
This will create a directory called `build/lib` which you can add to
`PYTHONPATH` in order to get `import wasmtime` working.

View File

@@ -0,0 +1 @@
gcd.wasm

View File

@@ -0,0 +1,15 @@
# Build example's file
To build `gcd.wasm` use rustc (nightly) for wasm32 target with debug information:
```
rustc +nightly --target=wasm32-unknown-unknown -g gcd.rs --crate-type=cdylib
```
# Run example
Point path to the built wasmtime_py library location when running python, e.g.
```
PYTHONPATH=../../target/debug python3 run.py
```

View File

@@ -0,0 +1,19 @@
#[inline(never)]
#[no_mangle]
pub extern fn gcd(m_: u32, n_: u32) -> u32
{
let mut m = m_;
let mut n = n_;
while m > 0 {
let tmp = m;
m = n % m;
n = tmp;
}
return n;
}
#[no_mangle]
pub extern fn test() -> u32 {
gcd(24, 9)
}

View File

@@ -0,0 +1,5 @@
import wasmtime
import gcd
print("gcd(27, 6) =", gcd.gcd(27, 6))

View File

@@ -0,0 +1,3 @@
import.wasm
main.wasm
__pycache__

View File

@@ -0,0 +1,15 @@
# Build example's file
To build `demo.wasm` use rustc (nightly) for wasm32 target with debug information:
```
rustc +nightly --target=wasm32-unknown-unknown demo.rs --crate-type=cdylib
```
# Run example
Point path to the built `wasmtime_py` library location when running python, e.g.
```
PYTHONPATH=../../target/debug python3 run.py
```

View File

@@ -0,0 +1,11 @@
extern "C" {
fn callback(s: *const u8, s_len: u32) -> u32;
}
#[no_mangle]
pub extern "C" fn test() {
let msg = "Hello, world!";
unsafe {
callback(msg.as_ptr(), msg.len() as u32);
}
}

View File

@@ -0,0 +1,10 @@
def callback(msg_p: 'i32', msg_len: 'i32') -> 'i32':
print('callback:', msg_p, msg_len)
# global memory
# mv = memoryview(memory)
# msg = bytes(mv[msg_p:(msg_p + msg_len)]).decode('utf-8')
# print(msg)
return 42

View File

@@ -0,0 +1,4 @@
import wasmtime
import demo
demo.test()

View File

@@ -0,0 +1,3 @@
one.wasm
two.wasm
__pycache__

View File

@@ -0,0 +1,20 @@
# Build example's file
To build `one.wasm` use rustc (nightly) for wasm32 target with debug information:
```
rustc +nightly --target=wasm32-unknown-unknown one.rs --crate-type=cdylib
```
To build `two.wasm` use wabt.
```
wat2wasm two.wat -o two.wasm
```
# Run example
Point path to the built wasmtime_py library location when running python, e.g.
```
PYTHONPATH=../../target/debug python3 run.py
```

View File

@@ -0,0 +1,2 @@
def answer() -> 'i32':
return 42

View File

@@ -0,0 +1,17 @@
extern "C" {
fn answer() -> u32;
}
// For the purpose of this wasm example, we don't worry about multi-threading,
// and will be using the PLACE in unsafe manner below.
static mut PLACE: u32 = 23;
#[no_mangle]
pub extern fn bar() -> *const u32 {
unsafe {
PLACE = answer();
// Return a pointer to the exported memory.
(&PLACE) as *const u32
}
}

View File

@@ -0,0 +1,4 @@
import wasmtime
import two
print("answer() returned", two.ask())

View File

@@ -0,0 +1,11 @@
(module
(import "one" "memory" (memory $memory 0))
(import "one" "bar" (func $bar (result i32)))
(export "ask" (func $foo))
(func $foo (result i32)
call $bar
;; Deference returned pointer to the value from imported memory
i32.load
)
)

View File

@@ -0,0 +1,49 @@
from .lib_wasmtime import imported_modules, instantiate
import sys
import os.path
from importlib import import_module
from importlib.abc import Loader, MetaPathFinder
from importlib.util import spec_from_file_location
# Mostly copied from
# https://stackoverflow.com/questions/43571737/how-to-implement-an-import-hook-that-can-modify-the-source-code-on-the-fly-using
class MyMetaFinder(MetaPathFinder):
def find_spec(self, fullname, path, target=None):
if path is None or path == "":
path = [os.getcwd()] # top level import --
path.extend(sys.path)
if "." in fullname:
*parents, name = fullname.split(".")
else:
name = fullname
for entry in path:
filename = os.path.join(entry, name + ".wasm")
if not os.path.exists(filename):
continue
return spec_from_file_location(fullname, filename, loader=MyLoader(filename))
return None
class MyLoader(Loader):
def __init__(self, filename):
self.filename = filename
def create_module(self, spec):
return None # use default module creation semantics
def exec_module(self, module):
with open(self.filename, "rb") as f:
data = f.read()
imports = {}
for module_name, fields in imported_modules(data).items():
imports[module_name] = {}
imported_module = import_module(module_name)
for field_name in fields:
imports[module_name][field_name] = imported_module.__dict__[field_name]
res = instantiate(data, imports)
module.__dict__.update(res.instance.exports)
sys.meta_path.insert(0, MyMetaFinder())

18
crates/misc/py/setup.py Normal file
View File

@@ -0,0 +1,18 @@
from setuptools import setup
from setuptools_rust import Binding, RustExtension
setup(name='wasmtime',
version="0.0.1",
classifiers=[
"Development Status :: 1 - Planning",
"Intended Audience :: Developers",
"Programming Language :: Python",
"Programming Language :: Rust",
"Operating System :: POSIX",
"Operating System :: MacOS :: MacOS X",
"Operating System :: Microsoft :: Windows",
],
packages=['wasmtime'],
package_dir={'wasmtime': 'python/wasmtime'},
rust_extensions=[RustExtension('wasmtime.lib_wasmtime', 'Cargo.toml', binding=Binding.PyO3)],
zip_safe=False)

View File

@@ -0,0 +1,68 @@
//! Support for a calling of a bounds (exported) function.
extern crate alloc;
use pyo3::prelude::*;
use pyo3::types::PyTuple;
use crate::value::{pyobj_to_value, value_to_pyobj};
use alloc::rc::Rc;
use core::cell::RefCell;
use cranelift_codegen::ir;
use wasmtime_interface_types::ModuleData;
use wasmtime_jit::{Context, InstanceHandle};
use wasmtime_runtime::Export;
// TODO support non-export functions
#[pyclass]
pub struct Function {
pub context: Rc<RefCell<Context>>,
pub instance: InstanceHandle,
pub export_name: String,
pub args_types: Vec<ir::Type>,
pub data: Rc<ModuleData>,
}
impl Function {
pub fn get_signature(&self) -> ir::Signature {
let mut instance = self.instance.clone();
if let Some(Export::Function { signature, .. }) = instance.lookup(&self.export_name) {
signature
} else {
panic!()
}
}
}
#[pymethods]
impl Function {
#[__call__]
#[args(args = "*")]
fn call(&self, py: Python, args: &PyTuple) -> PyResult<PyObject> {
let mut runtime_args = Vec::new();
for item in args.iter() {
runtime_args.push(pyobj_to_value(py, item)?);
}
let mut instance = self.instance.clone();
let mut cx = self.context.borrow_mut();
let results = self
.data
.invoke(
&mut cx,
&mut instance,
self.export_name.as_str(),
&runtime_args,
)
.map_err(crate::err2py)?;
let mut py_results = Vec::new();
for result in results {
py_results.push(value_to_pyobj(py, result)?);
}
if py_results.len() == 1 {
Ok(py_results[0].clone_ref(py))
} else {
Ok(PyTuple::new(py, py_results).to_object(py))
}
}
}

View File

@@ -0,0 +1,381 @@
//! Support for a calling of an imported function.
extern crate alloc;
use pyo3::prelude::*;
use pyo3::types::{PyAny, PyDict, PyTuple};
use crate::function::Function;
use crate::memory::Memory;
use crate::value::{read_value_from, write_value_to};
use cranelift_codegen::ir::types;
use cranelift_codegen::ir::{InstBuilder, StackSlotData, StackSlotKind};
use cranelift_codegen::Context;
use cranelift_codegen::{binemit, ir, isa};
use cranelift_entity::{EntityRef, PrimaryMap};
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
use cranelift_wasm::{DefinedFuncIndex, FuncIndex};
use target_lexicon::HOST;
use wasmtime_environ::{CompiledFunction, Export, Module};
use wasmtime_jit::CodeMemory;
use wasmtime_runtime::{Imports, InstanceHandle, VMContext, VMFunctionBody};
use alloc::rc::Rc;
use core::cell::RefCell;
use core::cmp;
use std::collections::{HashMap, HashSet};
struct BoundPyFunction {
name: String,
obj: PyObject,
}
struct ImportObjState {
calls: Vec<BoundPyFunction>,
#[allow(dead_code)]
code_memory: CodeMemory,
}
unsafe extern "C" fn stub_fn(vmctx: *mut VMContext, call_id: u32, values_vec: *mut i64) {
let gil = Python::acquire_gil();
let py = gil.python();
let mut instance = InstanceHandle::from_vmctx(vmctx);
let (_name, obj) = {
let state = instance
.host_state()
.downcast_mut::<ImportObjState>()
.expect("state");
let name = state.calls[call_id as usize].name.to_owned();
let obj = state.calls[call_id as usize].obj.clone_ref(py);
(name, obj)
};
let module = instance.module_ref();
let signature = &module.signatures[module.functions[FuncIndex::new(call_id as usize)]];
let mut args = Vec::new();
for i in 1..signature.params.len() {
args.push(read_value_from(
py,
values_vec.offset(i as isize - 1),
signature.params[i].value_type,
))
}
let result = obj.call(py, PyTuple::new(py, args), None).expect("result");
for i in 0..signature.returns.len() {
let val = if result.is_none() {
0.into_py(py) // FIXME default ???
} else {
if i > 0 {
panic!("multiple returns unsupported");
}
result.clone_ref(py)
};
write_value_to(
py,
values_vec.offset(i as isize),
signature.returns[i].value_type,
val,
);
}
}
/// Create a trampoline for invoking a python function.
fn make_trampoline(
isa: &dyn isa::TargetIsa,
code_memory: &mut CodeMemory,
fn_builder_ctx: &mut FunctionBuilderContext,
call_id: u32,
signature: &ir::Signature,
) -> *const VMFunctionBody {
// Mostly reverse copy of the similar method from wasmtime's
// wasmtime-jit/src/compiler.rs.
let pointer_type = isa.pointer_type();
let mut stub_sig = ir::Signature::new(isa.frontend_config().default_call_conv);
// Add the `vmctx` parameter.
stub_sig.params.push(ir::AbiParam::special(
pointer_type,
ir::ArgumentPurpose::VMContext,
));
// Add the `call_id` parameter.
stub_sig.params.push(ir::AbiParam::new(types::I32));
// Add the `values_vec` parameter.
stub_sig.params.push(ir::AbiParam::new(pointer_type));
let values_vec_len = 8 * cmp::max(signature.params.len() - 1, signature.returns.len()) as u32;
let mut context = Context::new();
context.func =
ir::Function::with_name_signature(ir::ExternalName::user(0, 0), signature.clone());
let ss = context.func.create_stack_slot(StackSlotData::new(
StackSlotKind::ExplicitSlot,
values_vec_len,
));
let value_size = 8;
{
let mut builder = FunctionBuilder::new(&mut context.func, fn_builder_ctx);
let block0 = builder.create_ebb();
builder.append_ebb_params_for_function_params(block0);
builder.switch_to_block(block0);
builder.seal_block(block0);
let values_vec_ptr_val = builder.ins().stack_addr(pointer_type, ss, 0);
let mflags = ir::MemFlags::trusted();
for i in 1..signature.params.len() {
if i == 0 {
continue;
}
let val = builder.func.dfg.ebb_params(block0)[i];
builder.ins().store(
mflags,
val,
values_vec_ptr_val,
((i - 1) * value_size) as i32,
);
}
let vmctx_ptr_val = builder.func.dfg.ebb_params(block0)[0];
let call_id_val = builder.ins().iconst(types::I32, call_id as i64);
let callee_args = vec![vmctx_ptr_val, call_id_val, values_vec_ptr_val];
let new_sig = builder.import_signature(stub_sig.clone());
let callee_value = builder
.ins()
.iconst(pointer_type, stub_fn as *const VMFunctionBody as i64);
builder
.ins()
.call_indirect(new_sig, callee_value, &callee_args);
let mflags = ir::MemFlags::trusted();
let mut results = Vec::new();
for (i, r) in signature.returns.iter().enumerate() {
let load = builder.ins().load(
r.value_type,
mflags,
values_vec_ptr_val,
(i * value_size) as i32,
);
results.push(load);
}
builder.ins().return_(&results);
builder.finalize()
}
let mut code_buf: Vec<u8> = Vec::new();
let mut reloc_sink = RelocSink {};
let mut trap_sink = binemit::NullTrapSink {};
let mut stackmap_sink = binemit::NullStackmapSink {};
context
.compile_and_emit(
isa,
&mut code_buf,
&mut reloc_sink,
&mut trap_sink,
&mut stackmap_sink,
)
.expect("compile_and_emit");
let mut unwind_info = Vec::new();
context.emit_unwind_info(isa, &mut unwind_info);
code_memory
.allocate_for_function(&CompiledFunction {
body: code_buf,
jt_offsets: context.func.jt_offsets,
unwind_info,
})
.expect("allocate_for_function")
.as_ptr()
}
fn parse_annotation_type(s: &str) -> ir::Type {
match s {
"I32" | "i32" => types::I32,
"I64" | "i64" => types::I64,
"F32" | "f32" => types::F32,
"F64" | "f64" => types::F64,
_ => panic!("unknown type in annotations"),
}
}
fn get_signature_from_py_annotation(
annot: &PyDict,
pointer_type: ir::Type,
call_conv: isa::CallConv,
) -> PyResult<ir::Signature> {
let mut params = Vec::new();
params.push(ir::AbiParam::special(
pointer_type,
ir::ArgumentPurpose::VMContext,
));
let mut returns = None;
for (name, value) in annot.iter() {
let ty = parse_annotation_type(&value.to_string());
match name.to_string().as_str() {
"return" => returns = Some(ty),
_ => params.push(ir::AbiParam::new(ty)),
}
}
Ok(ir::Signature {
params,
returns: match returns {
Some(r) => vec![ir::AbiParam::new(r)],
None => vec![],
},
call_conv,
})
}
pub fn into_instance_from_obj(
py: Python,
global_exports: Rc<RefCell<HashMap<String, Option<wasmtime_runtime::Export>>>>,
obj: &PyAny,
) -> PyResult<InstanceHandle> {
let isa = {
let isa_builder =
cranelift_native::builder().expect("host machine is not a supported target");
let flag_builder = cranelift_codegen::settings::builder();
isa_builder.finish(cranelift_codegen::settings::Flags::new(flag_builder))
};
let mut fn_builder_ctx = FunctionBuilderContext::new();
let mut module = Module::new();
let mut finished_functions: PrimaryMap<DefinedFuncIndex, *const VMFunctionBody> =
PrimaryMap::new();
let mut code_memory = CodeMemory::new();
let pointer_type = types::Type::triple_pointer_type(&HOST);
let call_conv = isa::CallConv::triple_default(&HOST);
let obj = obj.cast_as::<PyDict>()?;
let mut bound_functions = Vec::new();
let mut dependencies = HashSet::new();
let mut memories = PrimaryMap::new();
for (name, item) in obj.iter() {
if item.is_callable() {
let sig = if item.get_type().is_subclass::<Function>()? {
// TODO faster calls?
let wasm_fn = item.cast_as::<Function>()?;
dependencies.insert(wasm_fn.instance.clone());
wasm_fn.get_signature()
} else if item.hasattr("__annotations__")? {
let annot = item.getattr("__annotations__")?.cast_as::<PyDict>()?;
get_signature_from_py_annotation(&annot, pointer_type, call_conv)?
} else {
// TODO support calls without annotations?
continue;
};
let sig_id = module.signatures.push(sig.clone());
let func_id = module.functions.push(sig_id);
module
.exports
.insert(name.to_string(), Export::Function(func_id));
let trampoline = make_trampoline(
isa.as_ref(),
&mut code_memory,
&mut fn_builder_ctx,
func_id.index() as u32,
&sig,
);
finished_functions.push(trampoline);
bound_functions.push(BoundPyFunction {
name: name.to_string(),
obj: item.into_py(py),
});
} else if item.get_type().is_subclass::<Memory>()? {
let wasm_mem = item.cast_as::<Memory>()?;
dependencies.insert(wasm_mem.instance.clone());
let plan = wasm_mem.get_plan();
let mem_id = module.memory_plans.push(plan);
let _mem_id_2 = memories.push(wasm_mem.into_import());
assert_eq!(mem_id, _mem_id_2);
let _mem_id_3 = module
.imported_memories
.push((String::from(""), String::from("")));
assert_eq!(mem_id, _mem_id_3);
module
.exports
.insert(name.to_string(), Export::Memory(mem_id));
}
}
let imports = Imports::new(
dependencies,
PrimaryMap::new(),
PrimaryMap::new(),
memories,
PrimaryMap::new(),
);
let data_initializers = Vec::new();
let signatures = PrimaryMap::new();
code_memory.publish();
let import_obj_state = ImportObjState {
calls: bound_functions,
code_memory,
};
Ok(InstanceHandle::new(
Rc::new(module),
global_exports,
finished_functions.into_boxed_slice(),
imports,
&data_initializers,
signatures.into_boxed_slice(),
None,
Box::new(import_obj_state),
)
.expect("instance"))
}
/// We don't expect trampoline compilation to produce any relocations, so
/// this `RelocSink` just asserts that it doesn't recieve any.
struct RelocSink {}
impl binemit::RelocSink for RelocSink {
fn reloc_ebb(
&mut self,
_offset: binemit::CodeOffset,
_reloc: binemit::Reloc,
_ebb_offset: binemit::CodeOffset,
) {
panic!("trampoline compilation should not produce ebb relocs");
}
fn reloc_external(
&mut self,
_offset: binemit::CodeOffset,
_reloc: binemit::Reloc,
_name: &ir::ExternalName,
_addend: binemit::Addend,
) {
panic!("trampoline compilation should not produce external symbol relocs");
}
fn reloc_constant(
&mut self,
_code_offset: binemit::CodeOffset,
_reloc: binemit::Reloc,
_constant_offset: ir::ConstantOffset,
) {
panic!("trampoline compilation should not produce constant relocs");
}
fn reloc_jt(
&mut self,
_offset: binemit::CodeOffset,
_reloc: binemit::Reloc,
_jt: ir::JumpTable,
) {
panic!("trampoline compilation should not produce jump table relocs");
}
}

View File

@@ -0,0 +1,108 @@
//! WebAssembly Instance API object.
extern crate alloc;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use crate::function::Function;
use crate::memory::Memory;
use alloc::rc::Rc;
use core::cell::RefCell;
use cranelift_codegen::ir;
use cranelift_codegen::ir::types;
use wasmtime_environ::Export;
use wasmtime_interface_types::ModuleData;
use wasmtime_jit::{Context, InstanceHandle};
use wasmtime_runtime::Export as RuntimeExport;
#[pyclass]
pub struct Instance {
pub context: Rc<RefCell<Context>>,
pub instance: InstanceHandle,
pub data: Rc<ModuleData>,
}
fn get_type_annot(ty: ir::Type) -> &'static str {
match ty {
types::I32 => "i32",
types::I64 => "i64",
types::F32 => "f32",
types::F64 => "f64",
_ => panic!("unknown type"),
}
}
#[pymethods]
impl Instance {
#[getter(exports)]
fn get_exports(&mut self) -> PyResult<PyObject> {
let gil = Python::acquire_gil();
let py = gil.python();
let exports = PyDict::new(py);
let mut function_exports = Vec::new();
let mut memory_exports = Vec::new();
for (name, export) in self.instance.exports() {
match export {
Export::Function(_) => function_exports.push(name.to_string()),
Export::Memory(_) => memory_exports.push(name.to_string()),
_ => {
// Skip unknown export type.
continue;
}
}
}
for name in memory_exports {
if let Some(RuntimeExport::Memory { .. }) = self.instance.lookup(&name) {
let f = Py::new(
py,
Memory {
context: self.context.clone(),
instance: self.instance.clone(),
export_name: name.clone(),
},
)?;
exports.set_item(name, f)?;
} else {
panic!("memory");
}
}
for name in function_exports {
if let Some(RuntimeExport::Function { signature, .. }) = self.instance.lookup(&name) {
let annot = PyDict::new(py);
let mut args_types = Vec::new();
for index in 1..signature.params.len() {
let ty = signature.params[index].value_type;
args_types.push(ty);
annot.set_item(format!("param{}", index - 1), get_type_annot(ty))?;
}
match signature.returns.len() {
0 => (),
1 => {
annot
.set_item("return", get_type_annot(signature.returns[0].value_type))?;
}
_ => panic!("multi-return"),
}
let f = Py::new(
py,
Function {
context: self.context.clone(),
instance: self.instance.clone(),
data: self.data.clone(),
export_name: name.clone(),
args_types,
},
)?;
// FIXME set the f object the `__annotations__` attribute somehow?
let _ = annot;
exports.set_item(name, f)?;
} else {
panic!("function");
}
}
Ok(exports.to_object(py))
}
}

138
crates/misc/py/src/lib.rs Normal file
View File

@@ -0,0 +1,138 @@
use pyo3::exceptions::Exception;
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyDict, PySet};
use pyo3::wrap_pyfunction;
use crate::import::into_instance_from_obj;
use crate::instance::Instance;
use crate::memory::Memory;
use crate::module::Module;
use core::cell::RefCell;
use std::rc::Rc;
use wasmtime_interface_types::ModuleData;
mod function;
mod import;
mod instance;
mod memory;
mod module;
mod value;
fn err2py(err: anyhow::Error) -> PyErr {
PyErr::new::<Exception, _>(format!("{:?}", err))
}
#[pyclass]
pub struct InstantiateResultObject {
instance: Py<Instance>,
module: Py<Module>,
}
#[pymethods]
impl InstantiateResultObject {
#[getter(instance)]
fn get_instance(&self) -> PyResult<Py<Instance>> {
let gil = Python::acquire_gil();
let py = gil.python();
Ok(self.instance.clone_ref(py))
}
#[getter(module)]
fn get_module(&self) -> PyResult<Py<Module>> {
let gil = Python::acquire_gil();
let py = gil.python();
Ok(self.module.clone_ref(py))
}
}
/// WebAssembly instantiate API method.
#[pyfunction]
pub fn instantiate(
py: Python,
buffer_source: &PyBytes,
import_obj: &PyDict,
) -> PyResult<Py<InstantiateResultObject>> {
let wasm_data = buffer_source.as_bytes();
let generate_debug_info = false;
let isa = {
let isa_builder = cranelift_native::builder().map_err(|s| PyErr::new::<Exception, _>(s))?;
let flag_builder = cranelift_codegen::settings::builder();
isa_builder.finish(cranelift_codegen::settings::Flags::new(flag_builder))
};
let mut context = wasmtime_jit::Context::with_isa(isa, wasmtime_jit::CompilationStrategy::Auto);
context.set_debug_info(generate_debug_info);
let global_exports = context.get_global_exports();
for (name, obj) in import_obj.iter() {
context.name_instance(
name.to_string(),
into_instance_from_obj(py, global_exports.clone(), obj)?,
)
}
let data = Rc::new(ModuleData::new(wasm_data).map_err(err2py)?);
let instance = context
.instantiate_module(None, wasm_data)
.map_err(|e| err2py(e.into()))?;
let module = Py::new(
py,
Module {
module: instance.module(),
},
)?;
let instance = Py::new(
py,
Instance {
context: Rc::new(RefCell::new(context)),
instance,
data,
},
)?;
Py::new(py, InstantiateResultObject { instance, module })
}
#[pyfunction]
pub fn imported_modules<'p>(py: Python<'p>, buffer_source: &PyBytes) -> PyResult<&'p PyDict> {
let wasm_data = buffer_source.as_bytes();
let dict = PyDict::new(py);
// TODO: error handling
let mut parser = wasmparser::ModuleReader::new(wasm_data).unwrap();
while !parser.eof() {
let section = parser.read().unwrap();
match section.code {
wasmparser::SectionCode::Import => {}
_ => continue,
};
let reader = section.get_import_section_reader().unwrap();
for import in reader {
let import = import.unwrap();
let set = match dict.get_item(import.module) {
Some(set) => set.downcast_ref::<PySet>().unwrap(),
None => {
let set = PySet::new::<PyObject>(py, &[])?;
dict.set_item(import.module, set)?;
set
}
};
set.add(import.field)?;
}
}
Ok(dict)
}
#[pymodule]
fn lib_wasmtime(_: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<Instance>()?;
m.add_class::<Memory>()?;
m.add_class::<Module>()?;
m.add_class::<InstantiateResultObject>()?;
m.add_wrapped(wrap_pyfunction!(instantiate))?;
m.add_wrapped(wrap_pyfunction!(imported_modules))?;
Ok(())
}

View File

@@ -0,0 +1,133 @@
//! WebAssembly Memory API object.
extern crate alloc;
use pyo3::class::PyBufferProtocol;
use pyo3::exceptions::BufferError;
use pyo3::ffi;
use pyo3::prelude::*;
use alloc::rc::Rc;
use core::cell::RefCell;
use core::ptr;
use std::ffi::CStr;
use std::os::raw::{c_int, c_void};
use wasmtime_environ::MemoryPlan;
use wasmtime_jit::{Context, InstanceHandle};
use wasmtime_runtime::{Export, VMMemoryDefinition, VMMemoryImport};
#[pyclass]
pub struct Memory {
pub context: Rc<RefCell<Context>>,
pub instance: InstanceHandle,
pub export_name: String,
}
impl Memory {
fn descriptor(&self) -> *mut VMMemoryDefinition {
let mut instance = self.instance.clone();
if let Some(Export::Memory { definition, .. }) = instance.lookup(&self.export_name) {
definition
} else {
panic!("memory is expected");
}
}
}
impl Memory {
pub fn get_plan(&self) -> MemoryPlan {
let mut instance = self.instance.clone();
if let Some(Export::Memory { memory, .. }) = instance.lookup(&self.export_name) {
memory
} else {
panic!()
}
}
pub fn into_import(&self) -> VMMemoryImport {
let mut instance = self.instance.clone();
if let Some(Export::Memory {
definition, vmctx, ..
}) = instance.lookup(&self.export_name)
{
VMMemoryImport {
from: definition,
vmctx,
}
} else {
panic!()
}
}
}
#[pymethods]
impl Memory {
#[getter(current)]
pub fn current(&self) -> u32 {
let current_length = unsafe { (*self.descriptor()).current_length };
(current_length >> 16) as u32
}
pub fn grow(&self, _number: u32) -> u32 {
(-1i32) as u32
}
}
#[pyproto]
impl PyBufferProtocol for Memory {
fn bf_getbuffer(&self, view: *mut ffi::Py_buffer, flags: c_int) -> PyResult<()> {
if view.is_null() {
return Err(BufferError::py_err("View is null"));
}
unsafe {
/*
As a special case, for temporary buffers that are wrapped by
PyMemoryView_FromBuffer() or PyBuffer_FillInfo() this field is NULL.
In general, exporting objects MUST NOT use this scheme.
*/
(*view).obj = ptr::null_mut();
}
let readonly = if (flags & ffi::PyBUF_WRITABLE) == ffi::PyBUF_WRITABLE {
0
} else {
1
};
let VMMemoryDefinition {
base,
current_length,
} = unsafe { *self.descriptor() };
unsafe {
(*view).buf = base as *mut c_void;
(*view).len = current_length as isize;
(*view).readonly = readonly;
(*view).itemsize = 1;
(*view).format = ptr::null_mut();
if (flags & ffi::PyBUF_FORMAT) == ffi::PyBUF_FORMAT {
let msg = CStr::from_bytes_with_nul(b"B\0").unwrap();
(*view).format = msg.as_ptr() as *mut _;
}
(*view).ndim = 1;
(*view).shape = ptr::null_mut();
if (flags & ffi::PyBUF_ND) == ffi::PyBUF_ND {
(*view).shape = (&((*view).len)) as *const _ as *mut _;
}
(*view).strides = ptr::null_mut();
if (flags & ffi::PyBUF_STRIDES) == ffi::PyBUF_STRIDES {
(*view).strides = &((*view).itemsize) as *const _ as *mut _;
}
(*view).suboffsets = ptr::null_mut();
(*view).internal = ptr::null_mut();
}
Ok(())
}
}

View File

@@ -0,0 +1,12 @@
//! WebAssembly Module API object.
extern crate alloc;
use pyo3::prelude::*;
use alloc::rc::Rc;
#[pyclass]
pub struct Module {
pub module: Rc<wasmtime_environ::Module>,
}

View File

@@ -0,0 +1,61 @@
//! Utility functions to handle values conversion between abstractions/targets.
use pyo3::exceptions::Exception;
use pyo3::prelude::*;
use pyo3::types::PyAny;
use core::ptr;
use cranelift_codegen::ir;
use wasmtime_interface_types::Value;
pub fn pyobj_to_value(_: Python, p: &PyAny) -> PyResult<Value> {
if let Ok(n) = p.extract() {
Ok(Value::I32(n))
} else if let Ok(n) = p.extract() {
Ok(Value::U32(n))
} else if let Ok(n) = p.extract() {
Ok(Value::I64(n))
} else if let Ok(n) = p.extract() {
Ok(Value::U64(n))
} else if let Ok(n) = p.extract() {
Ok(Value::F64(n))
} else if let Ok(n) = p.extract() {
Ok(Value::F32(n))
} else if let Ok(s) = p.extract() {
Ok(Value::String(s))
} else {
Err(PyErr::new::<Exception, _>("unsupported value type"))
}
}
pub fn value_to_pyobj(py: Python, value: Value) -> PyResult<PyObject> {
Ok(match value {
Value::I32(i) => i.into_py(py),
Value::U32(i) => i.into_py(py),
Value::I64(i) => i.into_py(py),
Value::U64(i) => i.into_py(py),
Value::F32(i) => i.into_py(py),
Value::F64(i) => i.into_py(py),
Value::String(i) => i.into_py(py),
})
}
pub unsafe fn read_value_from(py: Python, ptr: *mut i64, ty: ir::Type) -> PyObject {
match ty {
ir::types::I32 => ptr::read(ptr as *const i32).into_py(py),
ir::types::I64 => ptr::read(ptr as *const i64).into_py(py),
ir::types::F32 => ptr::read(ptr as *const f32).into_py(py),
ir::types::F64 => ptr::read(ptr as *const f64).into_py(py),
_ => panic!("TODO add PyResult to read_value_from"),
}
}
pub unsafe fn write_value_to(py: Python, ptr: *mut i64, ty: ir::Type, val: PyObject) {
match ty {
ir::types::I32 => ptr::write(ptr as *mut i32, val.extract::<i32>(py).expect("i32")),
ir::types::I64 => ptr::write(ptr as *mut i64, val.extract::<i64>(py).expect("i64")),
ir::types::F32 => ptr::write(ptr as *mut f32, val.extract::<f32>(py).expect("f32")),
ir::types::F64 => ptr::write(ptr as *mut f64, val.extract::<f64>(py).expect("f64")),
_ => panic!("TODO add PyResult to write_value_to"),
}
}

View File

@@ -0,0 +1,21 @@
[package]
name = "wasmtime-rust"
version = "0.2.0"
authors = ["Alex Crichton <alex@alexcrichton.com>"]
edition = "2018"
categories = ["wasm", "rust"]
license = "Apache-2.0 WITH LLVM-exception"
description = "Rust extension for Wasmtime"
[lib]
test = false
doctest = false
[dependencies]
cranelift-codegen = { version = "0.49" }
cranelift-native = { version = "0.49" }
wasmtime-interface-types = { path = "../../interface-types" }
wasmtime-jit = { path = "../../jit" }
wasmtime-rust-macro = { path = "./macro" }
wasmtime-wasi = { path = "../../wasi" }
anyhow = "1.0.19"

View File

@@ -0,0 +1,37 @@
# `wasmtime-rust` - Using WebAssembly from Rust
This crate is intended to be an example of how to load WebAssembly files from a
native Rust application. You can always use `wasmtime` and its family of crates
directly, but the purpose of this crate is to provide an ergonomic macro:
```rust
#[wasmtime_rust::wasmtime]
trait WasmMarkdown {
fn render(&mut self, input: &str) -> String;
}
fn main() -> anyhow::Result<()> {
let mut markdown = WasmMarkdown::load_file("markdown.wasm")?;
println!("{}", markdown.render("# Hello, Rust!"));
Ok(())
}
```
The `wasmtime` macro defined in the `wasmtime-rust` crate is placed on a `trait`
which includes the set of functionality which a wasm module should export. In
this case we're expecting one `render` function which takes and returns a
string.
The macro expands to a `struct` with all of the methods on the trait (they must
all be `&mut self`) and one function called `load_file` to actually instantiate
the module.
Note that this macro is still in early stages of development, so error messages
aren't great yet and all functionality isn't supported yet.
## Missing features
Currently if the wasm module imports any symbols outside of the WASI namespace
the module will not load. It's intended that support for this will be added soon
though!

View File

@@ -0,0 +1,13 @@
use wasmtime_rust::wasmtime;
#[wasmtime]
trait WasmMarkdown {
fn render(&mut self, input: &str) -> String;
}
fn main() -> anyhow::Result<()> {
let mut markdown = WasmMarkdown::load_file("markdown.wasm")?;
println!("{}", markdown.render("# Hello, Rust!"));
Ok(())
}

View File

@@ -0,0 +1,17 @@
[package]
name = "wasmtime-rust-macro"
version = "0.2.0"
authors = ["Alex Crichton <alex@alexcrichton.com>"]
edition = "2018"
license = "Apache-2.0 WITH LLVM-exception"
description = "Macro support crate for wasmtime-rust"
[lib]
proc-macro = true
test = false
doctest = false
[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "1.0", features = ['full'] }

View File

@@ -0,0 +1,5 @@
# `wasmtime-rust-macro`
This is the actual definition of the `#[wasmtime]` macro, but it's intended that
this crate isn't used directly but rather the `wasmtime-rust` crate is used
instead.

View File

@@ -0,0 +1,175 @@
extern crate proc_macro;
use proc_macro2::TokenStream;
use quote::quote;
use syn::spanned::Spanned;
#[proc_macro_attribute]
pub fn wasmtime(
_attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let item = syn::parse_macro_input!(item as syn::ItemTrait);
expand(item).unwrap_or_else(|e| e.to_compile_error()).into()
}
fn expand(item: syn::ItemTrait) -> syn::Result<TokenStream> {
let definition = generate_struct(&item)?;
let load = generate_load(&item)?;
let methods = generate_methods(&item)?;
let name = &item.ident;
Ok(quote! {
#definition
impl #name {
#load
#methods
}
})
}
fn generate_struct(item: &syn::ItemTrait) -> syn::Result<TokenStream> {
let vis = &item.vis;
let name = &item.ident;
let root = root();
Ok(quote! {
#vis struct #name {
cx: #root::wasmtime_jit::Context,
handle: #root::wasmtime_jit::InstanceHandle,
data: #root::wasmtime_interface_types::ModuleData,
}
})
}
fn generate_load(item: &syn::ItemTrait) -> syn::Result<TokenStream> {
let vis = &item.vis;
let name = &item.ident;
let root = root();
Ok(quote! {
#vis fn load_file(path: impl AsRef<std::path::Path>) -> #root::anyhow::Result<#name> {
let bytes = std::fs::read(path)?;
let isa = {
let isa_builder = #root::cranelift_native::builder()
.map_err(|s| #root::anyhow::format_err!("{}", s))?;
let flag_builder = #root::cranelift_codegen::settings::builder();
isa_builder.finish(#root::cranelift_codegen::settings::Flags::new(flag_builder))
};
let mut cx = #root::wasmtime_jit::Context::with_isa(
isa,
#root::wasmtime_jit::CompilationStrategy::Auto
).with_features(#root::wasmtime_jit::Features {
multi_value: true,
..Default::default()
});
let data = #root::wasmtime_interface_types::ModuleData::new(&bytes)?;
if let Some(module_name) = data.find_wasi_module_name() {
let wasi_handle = wasmtime_wasi::instantiate_wasi(
"",
cx.get_global_exports(),
&[],
&[],
&[],
)?;
cx.name_instance(module_name, wasi_handle);
}
let handle = cx.instantiate_module(None, &bytes)?;
Ok(#name { cx, handle, data })
}
})
}
fn generate_methods(item: &syn::ItemTrait) -> syn::Result<TokenStream> {
macro_rules! bail {
($e:expr, $($fmt:tt)*) => (
return Err(syn::Error::new($e.span(), format!($($fmt)*)));
)
}
let mut result = TokenStream::new();
let root = root();
for item in item.items.iter() {
let method = match item {
syn::TraitItem::Method(f) => f,
other => bail!(other, "only methods are allowed"),
};
if let Some(e) = &method.default {
bail!(e, "cannot specify an implementation of methods");
}
if let Some(t) = &method.sig.constness {
bail!(t, "cannot be `const`");
}
if let Some(t) = &method.sig.asyncness {
bail!(t, "cannot be `async`");
}
let mut args = Vec::new();
for arg in method.sig.inputs.iter() {
let arg = match arg {
syn::FnArg::Receiver(_) => continue,
syn::FnArg::Typed(arg) => arg,
};
let ident = match &*arg.pat {
syn::Pat::Ident(i) => i,
other => bail!(other, "must use bare idents for arguments"),
};
if let Some(t) = &ident.by_ref {
bail!(t, "arguments cannot bind by reference");
}
if let Some(t) = &ident.mutability {
bail!(t, "arguments cannot be mutable");
}
if let Some((_, t)) = &ident.subpat {
bail!(t, "arguments cannot have sub-bindings");
}
let ident = &ident.ident;
args.push(quote! {
#root::wasmtime_interface_types::Value::from(#ident)
});
}
let convert_ret = match &method.sig.output {
syn::ReturnType::Default => {
quote! {
<() as #root::FromVecValue>::from(results)
}
}
syn::ReturnType::Type(_, ty) => match &**ty {
syn::Type::Tuple(..) => {
quote! { <#ty as #root::FromVecValue>::from(results) }
}
_ => {
quote! { <(#ty,) as #root::FromVecValue>::from(results).map(|t| t.0) }
}
},
};
let sig = &method.sig;
let attrs = &method.attrs;
let name = &method.sig.ident;
result.extend(quote! {
#(#attrs)*
#sig {
let args = [
#(#args),*
];
let results = self.data.invoke(
&mut self.cx,
&mut self.handle,
stringify!(#name),
&args,
).expect("wasm execution failed");
#convert_ret.expect("failed to convert return type")
}
});
}
Ok(result)
}
fn root() -> TokenStream {
quote! { wasmtime_rust::__rt }
}

View File

@@ -0,0 +1,47 @@
pub use wasmtime_rust_macro::wasmtime;
// modules used by the macro
#[doc(hidden)]
pub mod __rt {
pub use anyhow;
pub use cranelift_codegen;
pub use cranelift_native;
pub use wasmtime_interface_types;
pub use wasmtime_jit;
use std::convert::{TryFrom, TryInto};
use wasmtime_interface_types::Value;
pub trait FromVecValue: Sized {
fn from(list: Vec<Value>) -> anyhow::Result<Self>;
}
macro_rules! tuple {
($(($($a:ident),*),)*) => ($(
impl<$($a: TryFrom<Value>),*> FromVecValue for ($($a,)*)
where $(anyhow::Error: From<$a::Error>,)*
{
#[allow(non_snake_case)]
fn from(list: Vec<Value>) -> anyhow::Result<Self> {
let mut iter = list.into_iter();
$(
let $a = iter.next()
.ok_or_else(|| anyhow::format_err!("not enough values"))?
.try_into()?;
)*
if iter.next().is_some() {
anyhow::bail!("too many return values");
}
Ok(($($a,)*))
}
}
)*)
}
tuple! {
(),
(A),
(A, B),
(A, B, C),
}
}