forked from gen0cide/gscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
native_package.go
81 lines (69 loc) · 2.08 KB
/
native_package.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
package engine
import "github.com/robertkrimen/otto"
// NativePackage defines a golang library that is being imported into the genesis VM
type NativePackage struct {
ImportPath string
Name string
SymbolTable map[string]*NativeFunc
Types map[string]*NativeType
Consts map[string]*NativeConst
Vars map[string]*NativeVar
}
// NativeFunc defines a golang library function that is callable from within the genesis VM
type NativeFunc struct {
Name string
Signature string
Func func(call otto.FunctionCall) otto.Value
}
// NativeConst defines a golang const declared within a given library
type NativeConst struct {
Name string
Value interface{}
}
// NativeVar defines a golang top level var declaration within a given library
type NativeVar struct {
Name string
Signature string
Value interface{}
}
// ParamDef defines basic information about either an argument or return value for NativeFunc
type ParamDef struct {
Name string
GoType string
JSType string
}
// NativeType expresses a native Golang type definition that can be used within the engine
type NativeType struct {
Name string
Factory func(call otto.FunctionCall) otto.Value
Fields map[string]*NativeField
}
// NativeField expresses a struct field within a Go native type within the engine
type NativeField struct {
Label string
Signature string
}
func (e *Engine) createType(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) < 1 {
return e.Raise("argument", "not enough arguments for call to new()")
}
if len(call.ArgumentList) > 1 {
return e.Raise("argument", "too many arguments for call to new()")
}
arg := call.Argument(0)
if !arg.IsDefined() {
return e.Raise("type", "invalid type passed to new()")
}
var nt *NativeType
rarg, err := arg.Export()
if err != nil {
return e.Raise("jsexport", "could not export argument %d of function %s", 0, "new()")
}
switch v := rarg.(type) {
case *NativeType:
nt = v
default:
return e.Raise("type conversion", "argument type mismatch - expected %s, got %T", "*NativeType", v)
}
return nt.Factory(call)
}