/
global.cr
167 lines (143 loc) · 4.29 KB
/
global.cr
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
require "./lib"
require "./extern"
require "./value"
module Wasmer
Mutable = GlobalMutability::Mutable
Immutable = GlobalMutability::Immutable
enum GlobalMutability : UInt8
# Represents a global that is constant
Immutable
# Represents a global that is mutable
Mutable
def to_s(io : IO)
io << case self
when .immutable? then "const"
when .mutable? then "var"
end
end
# :nodoc:
def to_unsafe
self.value
end
end
# GlobalType classifies global variables, which hold a value and can either be mutable or immutable.
# See Also:
# Specification: https://webassembly.github.io/spec/core/syntax/types.html#global-types
class GlobalType
include WithExternType
# Instantiates a new `GlobalType` from a `ValueType` and `GlobalMutability`
#
# ```
# val_type = ValueType.new(Wasmer::I32)
# global_type = GlobalType.new(val_type, Wasmer::Immutable)
# ```
def initialize(val_type : ValueType, mutability : GlobalMutability)
@owner = true
@ptr = LibWasmer.wasm_globaltype_new(val_type, mutability)
end
# :nodoc:
protected def initialize(@ptr : LibWasmer::WasmGlobaltypeT)
@owner = false
end
# Returns the `GlobalType`'s `ValueType`
#
# ```
# val_type = ValueType.new(Wasmer::I32)
# global_type = GlobalType.new(val_type, Wasmer::Immutable)
# puts global_type.value_type.kind # => i32
# ```
def value_type : ValueType
v = LibWasmer.wasm_globaltype_content(self)
ValueType.new(v)
end
# Returns the `GlobalType`'s `GlobalMutability`
#
# ```
# val_type = ValueType.new(Wasmer::I32)
# global_type = GlobalType.new(val_type, Wasmer::Immutable)
# puts global_type.mutability # => const
# ```
def mutability : GlobalMutability
v = LibWasmer.wasm_globaltype_mutability(self)
GlobalMutability.from_value(v)
end
def mutable?
mutability == Wasmer::Mutable
end
# Converts this `GlobalType` into an `Externype`
#
# ```
# val_type = ValueType.new(Wasmer::I32)
# global_type = GlobalType.new(val_type, Wasmer::Immutable)
# extern_type = global_type.to_externtype
# ```
def to_externtype : ExternType
v = LibWasmer.wasm_globaltype_as_externtype_const(self)
ExternType.new(v)
end
# :nodoc:
def to_unsafe
@ptr
end
# :nodoc:
def finalize
return if @ptr.null? || !@owner
LibWasmer.wasm_globaltype_delete(@ptr)
end
end
# `Global` stores a single value of the given `GlobalType`
# See Also
# https://webassembly.github.io/spec/core/syntax/modules.html#globals
class Global
include WithExtern
# Instantiates a new `Global` in the given `Store`
# It takes three arguments, the `Store`, the `GlobalType` and the `Value`
#
# ```
# val_type = ValueType.new(Wasmer::I32)
# global_type = GlobalType.new(val_type, Wasmer::Immutable)
# global = Global.new(store, global_type, Value.new(42))
# ```
def initialize(store : Store, type : GlobalType, value : Value)
v_ptr = value.to_unsafe
@ptr = LibWasmer.wasm_global_new(store, type, pointerof(v_ptr))
end
# :nodoc:
protected def initialize(@ptr : LibWasmer::WasmGlobalT)
end
# Converts the `Global` into an `Extern`
def to_extern : Extern
p = LibWasmer.wasm_global_as_extern(self)
Extern.new(p)
end
# Returns the `Global`'s `GlobalType`
def type : GlobalType
p = LibWasmer.wasm_global_type(self)
GlobalType.new(p)
end
# Sets the `Global`'s value
# It takes two arguments, value as a native Crystal value
def set(value) : Nil
raise WasmerError.new("The global variable is not mutable, cannot set a new value") if type.mutability == Immutable
v = Value.new(value).to_unsafe
LibWasmer.wasm_global_set(self, pointerof(v))
end
# Returns the `Global`'s value as native Crystal value
def get
LibWasmer.wasm_global_get(self, out value)
Value.new(value).unwrap
end
# Sets the `Global`'s value. it calls `set` method
def value=(value)
set(value)
end
# Returns the `Global`'s value as native Crystal value. it just calls `get`
def value
get
end
# :nodoc:
def to_unsafe
@ptr
end
end
end