-
Notifications
You must be signed in to change notification settings - Fork 2
/
wasm_js.go
319 lines (279 loc) · 7.8 KB
/
wasm_js.go
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package inkwasm
import (
"errors"
"syscall/js"
"unsafe"
)
// Object represents one Javascript Object
type Object struct {
_ [0]func() // not comparable
// value holds the value of the js-object
// if typ == Object it's the value of the index
value [8]byte
typ ObjectType // type of js object
protected bool // protected prevents been released
_ [2]uint8 // padding, reserved
len uint32 // len for array/string
}
// NewObject creates a new Javascript Object.
//
// The resulting Object must be released using Free, when
// no longer in use.
func NewObject() Object {
return makeObj(nil)
}
//inkwasm:func globalThis.inkwasm.Internal.Make
func makeObj([]int32) Object
// Free deletes the reference from the array on Javascript's side.
func (o Object) Free() {
if o.protected {
return
}
switch o.typ {
case TypeFunction, TypeSymbol, TypeBigInt, TypeObject:
free(*(*int)(unsafe.Pointer(&o.value)))
}
}
//inkwasm:func globalThis.inkwasm.Internal.Free
func free(ref int)
// Call calls the method from the current Object, using args as
// arguments for the method.
//
// The resulting Object must be released using Free, when
// no longer in use.
func (o Object) Call(method string, args ...interface{}) (Object, error) {
v, err := createArgs(args)
if err != nil {
return Undefined(), err
}
r, ok := call(o, method, v)
if !ok {
return Undefined(), ErrExecutionJS
}
return r, nil
}
//inkwasm:func globalThis.inkwasm.Internal.Call
func call(o Object, k string, args []int32) (Object, bool)
// CallVoid is similar to Call, but doesn't return the resulting Object.
// Look at Call function for more details.
func (o Object) CallVoid(method string, args ...interface{}) error {
v, err := createArgs(args)
if err != nil {
return err
}
if _, ok := callVoid(o, method, v); !ok {
return ErrExecutionJS
}
return nil
}
//inkwasm:func globalThis.inkwasm.Internal.Call
func callVoid(o Object, k string, args []int32) (_, ok bool)
// Invoke invokes the current Object, calling itself with the
// provided args as arguments of the function.
//
// The Object must be a Javascript-Function, or be callable.
// The resulting Object must be released using Free, when
// no longer in use.
func (o Object) Invoke(args ...interface{}) (Object, error) {
v, err := createArgs(args)
if err != nil {
return Undefined(), err
}
r, ok := invoke(o, v)
if !ok {
return Undefined(), ErrExecutionJS
}
return r, nil
}
//inkwasm:func globalThis.inkwasm.Internal.Invoke
func invoke(o Object, args []int32) (Object, bool)
// InvokeVoid is similar to Invoke, but doesn't return the resulting Object.
// Look at Invoke function for more details.
func (o Object) InvokeVoid(args ...interface{}) error {
v, err := createArgs(args)
if err != nil {
return err
}
if _, ok := invokeVoid(o, v); !ok {
return ErrExecutionJS
}
return nil
}
//inkwasm:func globalThis.inkwasm.Internal.Invoke
func invokeVoid(o Object, args []int32) (_, ok bool)
// New uses the "new" operator from Javascript with the current object
// as the constructor and the given arg as arguments.
func (o Object) New(args ...interface{}) (Object, error) {
v, err := createArgs(args)
if err != nil {
return Undefined(), err
}
r, ok := newObj(o, v)
if !ok {
return Undefined(), ErrExecutionJS
}
return r, nil
}
//inkwasm:new .
func newObj(o Object, args []int32) (Object, bool)
// GetIndex returns given index of the current Object.
func (o Object) GetIndex(index int) Object {
return getIndex(o, index)
}
//inkwasm:get .
func getIndex(o Object, i int) Object
// GetProperty returns property of the current Object.
func (o Object) GetProperty(property string) Object {
return getProp(o, property)
}
//inkwasm:get .
func getProp(o Object, k string) Object
// SetProperty defines the given property of the current Object with
// the given value.
func (o Object) SetProperty(property string, value string) {
setProp(o, property, value)
}
//inkwasm:func Reflect.set
func setProp(o Object, k, v string)
var (
ErrInvalidType = errors.New("invalid type")
ErrExecutionJS = errors.New("error while executing/calling Javascript, see console log for details")
)
// Bool gets current Object value to bool.
// It will return error if the current Object isn't TypeBoolean.
func (o Object) Bool() (bool, error) {
switch o.typ {
case TypeBoolean:
return o.value[0] != 0, nil
case TypeNumber:
v, _ := o.Float()
return v != 0, nil
default:
return false, ErrInvalidType
}
}
// Float gets current Object value to float64.
// It will return error if the current Object isn't TypeNumber.
func (o Object) Float() (float64, error) {
if o.typ != TypeNumber {
return 0, ErrInvalidType
}
return *(*float64)(unsafe.Pointer(&o.value)), nil
}
// Int is a wrapper from Float
// It will return error if the current Object isn't TypeNumber
// or higher than int53.
func (o Object) Int() (int, error) {
const MaxFloat64 = (2 << 52) - 1
const MinFloat64 = -MaxFloat64
i, err := o.Float()
if err != nil {
return 0, err
}
if !(i > MinFloat64 && i < MaxFloat64) {
return 0, ErrInvalidType
}
return int(i), nil
}
// String return the value from the current Object as
// golang's string.
func (o Object) String() (string, error) {
if o.typ != TypeString && o.typ != TypeObject {
return "", ErrInvalidType
}
src := o
if o.typ == TypeString {
src = encodeString(o)
defer src.Free()
}
buf := make([]byte, src.len, src.len)
copyBytes(src, buf)
return *(*string)(unsafe.Pointer(&buf)), nil
}
// MustString is a wrapper to String, but suppress errors.
func (o Object) MustString() string {
r, _ := o.String()
return r
}
//inkwasm:func globalThis.inkwasm.Internal.EncodeString
func encodeString(o Object) (_ Object)
// Bytes return the value from the current Object as
// byte-slice.
//
// If buf is nil, a new byte-slice will be created and
// used instead.
func (o Object) Bytes(buf []byte) ([]byte, error) {
if o.typ != TypeObject && o.typ != TypeString {
return nil, ErrInvalidType
}
if buf == nil {
buf = make([]byte, o.len, o.len)
}
copyBytes(o, buf)
return buf, nil
}
// MustBytes is a wrapper to Bytes, but suppress errors.
func (o Object) MustBytes(buf []byte) []byte {
r, _ := o.Bytes(buf)
return r
}
//inkwasm:func globalThis.inkwasm.Internal.Copy
func copyBytes(o Object, buf []byte)
// Length returns the length of the current object,
// when the object is string or array.
//
// It uses uint32, use GetProperty("length") for larger
// results.
func (o Object) Length() uint32 {
return o.len
}
func (o Object) InstanceOf(v Object) bool {
js.Value{}.Truthy()
return instanceOf(o, v)
}
//inkwasm:func globalThis.inkwasm.Internal.InstanceOf
func instanceOf(o, v Object) bool
// Truthy returns the JavaScript "truthiness" of the value v. In JavaScript,
// false, 0, "", null, undefined, and NaN are "falsy", and everything else is
// "truthy".
//
// See https://developer.mozilla.org/en-US/docs/Glossary/Truthy.
func (o Object) Truthy() bool {
switch o.typ {
case TypeUndefined, TypeNull:
return false
case TypeBoolean:
v, _ := o.Bool()
return v
case TypeNumber:
v, _ := o.Float()
return v != 0
case TypeString:
v, _ := o.String()
return len(v) > 0
default:
return true
}
}
// Equal returns if o Object is equal (==) to v.
//
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality.
func (o Object) Equal(v Object) bool {
if !v.Truthy() && !o.Truthy() {
return true
}
if o.len != v.len {
return false
}
return equal(o, v)
}
//inkwasm:func globalThis.inkwasm.Internal.Equal
func equal(o, v Object) bool
// StrictEqual returns if o Object is strict-equal (===) to v.
//
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality.
func (o Object) StrictEqual(v Object) bool {
return strictEqual(o, v)
}
//inkwasm:func globalThis.inkwasm.Internal.StrictEqual
func strictEqual(o, v Object) bool