-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathimports_exports.py
More file actions
104 lines (90 loc) · 2.91 KB
/
imports_exports.py
File metadata and controls
104 lines (90 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# A Wasm module can import and export entities, like functions,
# memories, globals and tables. This example illustrates the basics
# of using these entities.
#
# In this example we'll be using a sample Wasm module which exports
# some entities and requires us to also import some of them.
#
# The goal here is to give you an idea of how to work with imports and
# exports. We won't go into the details of each entities, they'll be
# covered in more details in the other examples.
#
# You can run the example directly by executing in Wasmer root:
#
# ```shell
# $ python examples/imports_exports.py
# ```
#
# Ready?
from wasmer import engine, wat2wasm, Store, Module, ImportObject, Function, Global, Instance, Type, Value
from wasmer_compiler_cranelift import Compiler
# Let's declare the Wasm module with the text representation.
# We are using the text representation of the module here
wasm_bytes = wat2wasm(
"""
(module
(func $host_function (import "" "host_function") (result i32))
(global $host_global (import "env" "host_global") i32)
(func $function (export "guest_function") (result i32) (global.get $global))
(global $global (export "guest_global") i32 (i32.const 42))
(table $table (export "guest_table") 1 1 funcref)
(memory $memory (export "guest_memory") 1))
"""
)
# Create a store. Engines and compilers are explained in other
# examples.
store = Store(engine.Universal(Compiler))
# Let's compile the Wasm module.
module = Module(store, wasm_bytes)
# Let's write the Python function that is going to be imported,
# i.e. called by the WebAssembly module.
def host_function_implementation() -> int:
return 42
host_function = Function(store, host_function_implementation)
# Let's then create a global that is going to be imported.
host_global = Global(store, Value.i32(42))
# Create an import object.
#
# Imports are stored in namespaces. We'll need to register each of the
# namespaces with a name and add the imported entities there.
#
# Note that the namespace can also have an empty name.
#
# Our module requires us to import:
# * A function `host_function` in a namespace with an empty name;
# * A global `host_global` in the `env` namespace.
#
# Let's do this!
import_object = ImportObject()
import_object.register(
"",
{
"host_function": host_function,
}
)
import_object.register(
"env",
{
"host_global": host_global,
}
)
# Let's instantiate the module!
instance = Instance(module, import_object)
# And finally, let's play.
#
# The Wasm module exports some entities:
#
# * A function: `guest_function`,
# * A global: `guest_global`,
# * A memory: `guest_memory`,
# * A table: `guest_table`,
#
# Let's get them.
function = instance.exports.guest_function
print(function.type)
global_ = instance.exports.guest_global
print(global_.type)
memory = instance.exports.guest_memory
print(memory.type)
table = instance.exports.guest_table
print(table.type)