-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathtest_global.py
110 lines (73 loc) · 2.33 KB
/
test_global.py
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
105
106
107
108
109
110
from wasmer import Instance, Module, Store, Global, GlobalType, Type, Value
import pytest
TEST_BYTES = """
(module
(global $x (export "x") (mut i32) (i32.const 0))
(global $y (export "y") (mut i32) (i32.const 7))
(global $z (export "z") i32 (i32.const 42))
(func (export "get_x") (result i32)
(global.get $x))
(func (export "increment_x")
(global.set $x
(i32.add (global.get $x) (i32.const 1)))))
"""
def instance():
return Instance(Module(Store(), TEST_BYTES))
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
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
def test_export():
assert isinstance(instance().exports.x, Global)
def test_type():
type = instance().exports.x.type
assert type.type == Type.I32
assert type.mutable == True
assert str(type) == 'GlobalType(type: I32, mutable: true)'
def test_global_mutable():
exports = instance().exports
assert exports.x.mutable == True
assert exports.y.mutable == True
assert exports.z.mutable == False
def test_global_read_write():
y = instance().exports.y
assert y.value == 7
y.value = 8
assert y.value == 8
def test_global_read_write_and_exported_functions():
exports = instance().exports
x = exports.x
assert x.value == 0
assert exports.get_x() == 0
x.value = 1
assert x.value == 1
assert exports.get_x() == 1
exports.increment_x()
assert x.value == 2
assert exports.get_x() == 2
def test_global_read_write_constant():
z = instance().exports.z
assert z.value == 42
with pytest.raises(RuntimeError) as context_manager:
z.value = 153
exception = context_manager.value
assert str(exception) == (
'The global variable is not mutable, cannot set a new value'
)