forked from wasmerio/wasmer-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobl.php
144 lines (124 loc) ยท 3.34 KB
/
Globl.php
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
<?php
declare(strict_types=1);
namespace Wasm;
use Wasm\Type\GlobalType;
use Wasm\Type\ValType;
/**
* @api
*/
final class Globl
{
/**
* @var resource The inner `wasm_global_t` resource
*/
private $inner;
/**
* Create a Wasm\Module\Globl from a `wasm_global_t` resource.
*
* @param $global resource a `wasm_global_t` resource
*
* @throw Exception\InvalidArgumentException If the `$global` argument is not a valid `wasm_global_t` resource
*/
public function __construct($global)
{
if (false === is_resource($global) || 'wasm_global_t' !== get_resource_type($global)) {
throw new Exception\InvalidArgumentException();
}
$this->inner = $global;
}
/**
* @ignore
*/
public function __clone(): void
{
$this->inner = \wasm_global_copy($this->inner);
}
/**
* @ignore
*/
public function __destruct()
{
try {
\wasm_global_delete($this->inner);
} catch (\TypeError $error) {
if (is_resource($this->inner)) {
throw $error;
}
}
}
/**
* Return the inner global resource.
*
* @return resource A `wasm_global_t` resource
*/
public function inner()
{
return $this->inner;
}
/**
* @api
*/
public function asExtern(): Extern
{
return new Extern(\wasm_global_as_extern($this->inner));
}
/**
* @api
*/
public function get(): Val
{
return new Val(\wasm_global_get($this->inner));
}
/**
* @api
*/
public function same(self $other): bool
{
return \wasm_global_same($this->inner, $other->inner());
}
/**
* @api
*/
public function set(int | float | Val $value): void
{
if (!$value instanceof Val) {
$globaltype = $this->type();
$valtype = $globaltype->content();
$kind = $valtype->kind();
$value = match ($kind) {
ValType::KIND_I32 => Val::newI32($value),
ValType::KIND_I64 => Val::newI64($value),
ValType::KIND_F32 => Val::newF32((float) $value),
ValType::KIND_F64 => Val::newF64((float) $value),
default => throw new Exception\InvalidArgumentException(),
};
}
\wasm_global_set($this->inner, $value->inner());
}
/**
* @api
*/
public function type(): GlobalType
{
return new GlobalType(\wasm_global_type($this->inner));
}
/**
* @api
*/
public static function new(Store $store, GlobalType $globaltype, int | float | Val $val): self
{
if ($val instanceof Val) {
return new self(\wasm_global_new($store->inner(), $globaltype->inner(), $val->inner()));
}
$valtype = $globaltype->content();
$kind = $valtype->kind();
$value = match ($kind) {
ValType::KIND_I32 => Val::newI32($val),
ValType::KIND_I64 => Val::newI64($val),
ValType::KIND_F32 => Val::newF32((float) $val),
ValType::KIND_F64 => Val::newF64((float) $val),
default => throw new Exception\InvalidArgumentException(),
};
return new self(\wasm_global_new($store->inner(), $globaltype->inner(), $value->inner()));
}
}