-
Notifications
You must be signed in to change notification settings - Fork 2
/
js.go
75 lines (61 loc) · 1.59 KB
/
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
package wasmexec
// errno describes an error "number".
type errno string
// This errno list is a subset of the errors in syscall/tables_js.go.
const (
eNOSYS errno = "ENOSYS"
)
// errorResponse returns a errno callback response.
func errorResponse(code errno) []any {
return []any{jsProperties{"code": string(code)}}
}
// errCallback calls the callback with the specified errno code.
func errorCallback(code errno) *jsFunction {
return &jsFunction{
fn: func(args []any) any {
if len(args) == 0 {
return nil
}
// The last item in the list should be a callback function, according to
// the fsCall() function in syscall/fs.js.go.
callback, ok := args[len(args)-1].(*jsFunction)
if !ok {
return nil
}
callback.fn(errorResponse(code))
return nil
},
}
}
// jsFunction describes the constructor of an jsObject.
type jsFunction struct {
name string
fn func(args []any) any
}
// newjsFunction returns a new function.
func newjsFunction(fn func(args []any) any) *jsFunction {
return &jsFunction{fn: fn}
}
// Name returns the name of the constructor type.
func (fn jsFunction) Name() string {
return fn.name
}
// jsProperties describe the properties on an object. This can either be a
// function or a value.
type jsProperties map[string]any
// jsObject describes a JSON object.
type jsObject struct {
properties jsProperties
}
// jsArray describes an array of elements.
type jsArray struct {
elements []any
}
// jsUint8Array describes a byte slice.
type jsUint8Array struct {
data []byte
}
// jsString represents a stored string.
type jsString struct {
data string
}