-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
bridge.go
282 lines (248 loc) · 7.94 KB
/
bridge.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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package common
import (
"context"
"reflect"
"strings"
"github.com/dop251/goja"
"github.com/pkg/errors"
"github.com/serenize/snaker"
)
var (
ctxPtrT = reflect.TypeOf((*context.Context)(nil))
ctxT = reflect.TypeOf((*context.Context)(nil)).Elem()
errorT = reflect.TypeOf((*error)(nil)).Elem()
jsValT = reflect.TypeOf((*goja.Value)(nil)).Elem()
fnCallT = reflect.TypeOf((*goja.FunctionCall)(nil)).Elem()
constructWrap = goja.MustCompile(
"__constructor__",
`(function(impl) { return function() { return impl.apply(this, arguments); } })`,
true,
)
)
// if a fieldName is the key of this map exactly than the value for the given key should be used as
// the name of the field in js
//nolint: gochecknoglobals
var fieldNameExceptions = map[string]string{
"OCSP": "ocsp",
}
// FieldName Returns the JS name for an exported struct field. The name is snake_cased, with respect for
// certain common initialisms (URL, ID, HTTP, etc).
func FieldName(t reflect.Type, f reflect.StructField) string {
// PkgPath is non-empty for unexported fields.
if f.PkgPath != "" {
return ""
}
// Allow a `js:"name"` tag to override the default name.
if tag := f.Tag.Get("js"); tag != "" {
// Matching encoding/json, `js:"-"` hides a field.
if tag == "-" {
return ""
}
return tag
}
if exception, ok := fieldNameExceptions[f.Name]; ok {
return exception
}
// Default to lowercasing the first character of the field name.
return snaker.CamelToSnake(f.Name)
}
// if a methodName is the key of this map exactly than the value for the given key should be used as
// the name of the method in js
//nolint: gochecknoglobals
var methodNameExceptions = map[string]string{
"JSON": "json",
"HTML": "html",
"URL": "url",
"OCSP": "ocsp",
}
// MethodName Returns the JS name for an exported method. The first letter of the method's name is
// lowercased, otherwise it is unaltered.
func MethodName(t reflect.Type, m reflect.Method) string {
// A field with a name beginning with an X is a constructor, and just gets the prefix stripped.
// Note: They also get some special treatment from Bridge(), see further down.
if m.Name[0] == 'X' {
return m.Name[1:]
}
if exception, ok := methodNameExceptions[m.Name]; ok {
return exception
}
// Lowercase the first character of the method name.
return strings.ToLower(m.Name[0:1]) + m.Name[1:]
}
// FieldNameMapper for goja.Runtime.SetFieldNameMapper()
type FieldNameMapper struct{}
// FieldName is part of the goja.FieldNameMapper interface
// https://godoc.org/github.com/dop251/goja#FieldNameMapper
func (FieldNameMapper) FieldName(t reflect.Type, f reflect.StructField) string {
return FieldName(t, f)
}
// MethodName is part of the goja.FieldNameMapper interface
// https://godoc.org/github.com/dop251/goja#FieldNameMapper
func (FieldNameMapper) MethodName(t reflect.Type, m reflect.Method) string { return MethodName(t, m) }
// BindToGlobal Binds an object's members to the global scope. Returns a function that un-binds them.
// Note that this will panic if passed something that isn't a struct; please don't do that.
func BindToGlobal(rt *goja.Runtime, data map[string]interface{}) func() {
keys := make([]string, len(data))
i := 0
for k, v := range data {
rt.Set(k, v)
keys[i] = k
i++
}
return func() {
for _, k := range keys {
rt.Set(k, goja.Undefined())
}
}
}
// Bind the provided value v to the provided runtime
func Bind(rt *goja.Runtime, v interface{}, ctxPtr *context.Context) map[string]interface{} {
exports := make(map[string]interface{})
val := reflect.ValueOf(v)
typ := val.Type()
for i := 0; i < typ.NumMethod(); i++ {
meth := typ.Method(i)
name := MethodName(typ, meth)
fn := val.Method(i)
// Figure out if we want to do any wrapping of it.
fnT := fn.Type()
numIn := fnT.NumIn()
numOut := fnT.NumOut()
hasError := (numOut > 1 && fnT.Out(1) == errorT)
wantsContext := false
wantsContextPtr := false
if numIn > 0 {
in0 := fnT.In(0)
switch in0 {
case ctxT:
wantsContext = true
case ctxPtrT:
wantsContextPtr = true
}
}
if hasError || wantsContext || wantsContextPtr {
isVariadic := fnT.IsVariadic()
realFn := fn
fn = reflect.ValueOf(func(call goja.FunctionCall) goja.Value {
// Number of arguments: the higher number between the function's required arguments
// and the number of arguments actually given.
args := make([]reflect.Value, numIn)
// Inject any requested parameters, and reserve them to offset user args.
reservedArgs := 0
if wantsContext {
if ctxPtr == nil || *ctxPtr == nil {
Throw(rt, errors.Errorf("%s() can only be called from within default()", name))
}
args[0] = reflect.ValueOf(*ctxPtr)
reservedArgs++
} else if wantsContextPtr {
args[0] = reflect.ValueOf(ctxPtr)
reservedArgs++
}
// Copy over arguments.
for i := 0; i < numIn; i++ {
if i < reservedArgs {
continue
}
T := fnT.In(i)
// A function that takes a goja.FunctionCall takes only that arg (+ injected).
if T == fnCallT {
args[i] = reflect.ValueOf(call)
break
}
// The last arg to a varadic function is a slice of the remainder.
if isVariadic && i == numIn-1 {
varArgsLen := len(call.Arguments) - (i - reservedArgs)
if varArgsLen <= 0 {
args[i] = reflect.Zero(T)
break
}
varArgs := reflect.MakeSlice(T, varArgsLen, varArgsLen)
emT := T.Elem()
for j := 0; j < varArgsLen; j++ {
arg := call.Arguments[i+j-reservedArgs]
v := reflect.New(emT)
if err := rt.ExportTo(arg, v.Interface()); err != nil {
Throw(rt, err)
}
varArgs.Index(j).Set(v.Elem())
}
args[i] = varArgs
break
}
arg := call.Argument(i - reservedArgs)
// Optimization: no need to allocate a pointer and export for a zero value.
if goja.IsUndefined(arg) {
if T == jsValT {
args[i] = reflect.ValueOf(goja.Undefined())
continue
}
args[i] = reflect.Zero(T)
continue
}
// Allocate a T* and export the JS value to it.
v := reflect.New(T)
if err := rt.ExportTo(arg, v.Interface()); err != nil {
Throw(rt, err)
}
args[i] = v.Elem()
}
var ret []reflect.Value
if isVariadic {
ret = realFn.CallSlice(args)
} else {
ret = realFn.Call(args)
}
if len(ret) > 0 {
if hasError && !ret[1].IsNil() {
Throw(rt, ret[1].Interface().(error))
}
return rt.ToValue(ret[0].Interface())
}
return goja.Undefined()
})
}
// X-Prefixed methods are assumed to be constructors; use a closure to wrap them in a
// pure-JS function to allow them to be `new`d. (This is an awful hack...)
if meth.Name[0] == 'X' {
wrapperV, _ := rt.RunProgram(constructWrap)
wrapper, _ := goja.AssertFunction(wrapperV)
v, _ := wrapper(goja.Undefined(), rt.ToValue(fn.Interface()))
exports[name] = v
} else {
exports[name] = fn.Interface()
}
}
// If v is a pointer, we need to indirect it to access fields.
if typ.Kind() == reflect.Ptr {
val = val.Elem()
typ = val.Type()
}
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
name := FieldName(typ, field)
if name != "" {
exports[name] = val.Field(i).Interface()
}
}
return exports
}