Beispiel #1
0
 def test_call_i32_i64_f32_f64_f64(self):
     self.assertEqual(
         round(
             Instance(TEST_BYTES).call('i32_i64_f32_f64_f64', [
                 Value.i32(1),
                 Value.i64(2),
                 Value.f32(3.4),
                 Value.f64(5.6)
             ]), 6), 1 + 2 + 3.4 + 5.6)
def test_import_global():
    store = Store()
    module = Module(
        store, """
        (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))
        """)

    global_ = Global(store, Value.i32(7), mutable=True)

    import_object = ImportObject()
    import_object.register("env", {"global": global_})

    instance = Instance(module, import_object)

    assert instance.exports.read_g() == 7
    global_.value = 153
    assert instance.exports.read_g() == 153
    instance.exports.write_g(11)
    assert global_.value == 11
Beispiel #3
0
def test_constructor():
    store = Store()
    global_ = Global(store, Value.i32(42))

    assert global_.value == 42

    type = global_.type

    assert type.type == Type.I32
    assert type.mutable == False

    global_ = Global(store, Value.i64(153), mutable=False)

    assert global_.value == 153

    type = global_.type

    assert type.type == Type.I64
    assert type.mutable == False
Beispiel #4
0
def test_constructor_mutable():
    store = Store()
    global_ = Global(store, Value.i32(42), mutable=True)

    assert global_.value == 42

    type = global_.type

    assert type.type == Type.I32
    assert type.mutable == True

    global_.value = 153

    assert global_.value == 153
Beispiel #5
0
 def test_call_f32_f32(self):
     self.assertEqual(
         Instance(TEST_BYTES).call('f32_f32', [Value.f32(7.)]), 7.)
Beispiel #6
0
 def test_call_i32_i32(self):
     self.assertEqual(
         Instance(TEST_BYTES).call('i32_i32', [Value.i32(7)]), 7)
Beispiel #7
0
 def test_call_i64_i64(self):
     self.assertEqual(
         Instance(TEST_BYTES).call('i64_i64', [Value.i64(7)]), 7)
Beispiel #8
0
def test_cannot_construct():
    Value()
Beispiel #9
0
 def test_basic_sum(self):
     self.assertEqual(
         Instance(TEST_BYTES).call(
             'sum', [Value.i32(1), Value.i32(2)]), 3)
Beispiel #10
0
 def test_f64(self):
     self.assertEqual(repr(Value.f64(4.2)), 'F64(4.2)')
Beispiel #11
0
def test_v128():
    assert repr(Value.v128(340282366920938463463374607431768211455)) == 'V128(340282366920938463463374607431768211455)'
Beispiel #12
0
 def test_f32(self):
     self.assertEqual(repr(Value.f32(4.2)), 'F32(4.2)')
Beispiel #13
0
 def test_i64(self):
     self.assertEqual(repr(Value.i64(42)), 'I64(42)')
Beispiel #14
0
def test_i64():
    assert repr(Value.i64(42)) == 'I64(42)'
Beispiel #15
0
def test_f32():
    assert repr(Value.f32(4.2)) == 'F32(4.2)'
Beispiel #16
0
def test_i32():
    assert repr(Value.i32(42)) == 'I32(42)'
Beispiel #17
0
from wasmer import Instance, Value
import os

__dir__ = os.path.dirname(os.path.realpath(__file__))

wasm_bytes = open(__dir__ + '/simple.wasm', 'rb').read()
instance = Instance(wasm_bytes)
result = instance.call('sum', [Value.i32(5), Value.i32(37)])

print(result) # 42!
Beispiel #18
0
 def test_f64_auto_cast(self):
     self.assertEqual(repr(Value.f64(42)), 'F64(42.0)')
Beispiel #19
0
 def test_call_f64_f64(self):
     self.assertEqual(
         Instance(TEST_BYTES).call('f64_f64', [Value.f64(7.)]), 7.)
Beispiel #20
0
 def test_i32(self):
     self.assertEqual(repr(Value.i32(42)), 'I32(42)')
Beispiel #21
0
def test_f32_auto_cast():
    assert repr(Value.f32(42)) == 'F32(42.0)'
Beispiel #22
0
 def test_f32_auto_cast(self):
     self.assertEqual(repr(Value.f32(42)), 'F32(42.0)')
Beispiel #23
0
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("", {
Beispiel #24
0
def test_f64():
    assert repr(Value.f64(4.2)) == 'F64(4.2)'
Beispiel #25
0
def test_f64_auto_cast():
    assert repr(Value.f64(42)) == 'F64(42.0)'
Beispiel #26
0
 def test_cannot_construct(self):
     Value()