forked from wasmerio/wasmer-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVal.php
140 lines (121 loc) ยท 2.82 KB
/
Val.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
<?php
declare(strict_types=1);
namespace Wasm;
/**
* @api
*/
final class Val
{
/**
* @var resource The inner `wasm_val_t` resource
*/
private $inner;
/**
* Create a Wasm\Module\Val from a `wasm_val_t` resource.
*
* @param $val resource a `wasm_val_t` resource
*
* @throw Exception\InvalidArgumentException If the `$val` argument is not a valid `wasm_val_t` resource
*/
public function __construct($val)
{
if (false === is_resource($val) || 'wasm_val_t' !== get_resource_type($val)) {
throw new Exception\InvalidArgumentException();
}
$this->inner = $val;
}
/**
* @ignore
*/
public function __destruct()
{
try {
\wasm_val_delete($this->inner);
} catch (\TypeError $error) {
if (is_resource($this->inner)) {
throw $error;
}
}
}
/**
* Return the inner val resource.
*
* @return resource A `wasm_val_t` resource
*/
public function inner()
{
return $this->inner;
}
/**
* @api
*/
public function kind(): int
{
return \wasm_val_kind($this->inner);
}
/**
* @api
*/
public function value(): int | float
{
return \wasm_val_value($this->inner);
}
/**
* @api
*/
public static function new(mixed $val): self
{
if (is_resource($val) && 'wasm_val_t' === get_resource_type($val)) {
$val = \wasm_val_value($val);
}
if (is_int($val)) {
try {
return self::newI32($val);
} catch (Exception\InvalidArgumentException) {
return self::newI64($val);
}
}
if (is_float($val)) {
try {
return self::newF32($val);
} catch (Exception\InvalidArgumentException) {
return self::newF64($val);
}
}
throw new Exception\InvalidArgumentException();
}
/**
* @api
*/
public static function newI32(int $val): self
{
if ($val < -0x7FFFFFFF || $val > 0x7FFFFFFF) {
throw new Exception\InvalidArgumentException();
}
return new self(\wasm_val_i32($val));
}
/**
* @api
*/
public static function newI64(int $val): self
{
return new self(\wasm_val_i64($val));
}
/**
* @api
*/
public static function newF32(float $val): self
{
if ($val < -3.40282347e+38 || $val > 3.40282347e+38) {
throw new Exception\InvalidArgumentException();
}
return new self(\wasm_val_f32($val));
}
/**
* @api
*/
public static function newF64(float $val): self
{
return new self(\wasm_val_f64($val));
}
}