-
Notifications
You must be signed in to change notification settings - Fork 586
/
type_function.go
322 lines (283 loc) · 8.29 KB
/
type_function.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
320
321
322
package otto
// constructFunction.
type constructFunction func(*object, []Value) Value
// 13.2.2 [[Construct]].
func defaultConstruct(fn *object, argumentList []Value) Value {
obj := fn.runtime.newObject()
obj.class = classObjectName
prototype := fn.get("prototype")
if prototype.kind != valueObject {
prototype = objectValue(fn.runtime.global.ObjectPrototype)
}
obj.prototype = prototype.object()
this := objectValue(obj)
value := fn.call(this, argumentList, false, nativeFrame)
if value.kind == valueObject {
return value
}
return this
}
// nativeFunction.
type nativeFunction func(FunctionCall) Value
// nativeFunctionObject.
type nativeFunctionObject struct {
call nativeFunction
construct constructFunction
name string
file string
line int
}
func (rt *runtime) newNativeFunctionProperty(name, file string, line int, native nativeFunction, length int) *object {
o := rt.newClassObject(classFunctionName)
o.value = nativeFunctionObject{
name: name,
file: file,
line: line,
call: native,
construct: defaultConstruct,
}
o.defineProperty("name", stringValue(name), 0o000, false)
o.defineProperty(propertyLength, intValue(length), 0o000, false)
return o
}
func (rt *runtime) newNativeFunctionObject(name, file string, line int, native nativeFunction, length int) *object {
o := rt.newNativeFunctionProperty(name, file, line, native, length)
o.defineOwnProperty("caller", property{
value: propertyGetSet{
rt.newNativeFunctionProperty("get", "internal", 0, func(fc FunctionCall) Value {
for sc := rt.scope; sc != nil; sc = sc.outer {
if sc.frame.fn == o {
if sc.outer == nil || sc.outer.frame.fn == nil {
return nullValue
}
return rt.toValue(sc.outer.frame.fn)
}
}
return nullValue
}, 0),
&nilGetSetObject,
},
mode: 0o000,
}, false)
return o
}
// bindFunctionObject.
type bindFunctionObject struct {
target *object
this Value
argumentList []Value
}
func (rt *runtime) newBoundFunctionObject(target *object, this Value, argumentList []Value) *object {
o := rt.newClassObject(classFunctionName)
o.value = bindFunctionObject{
target: target,
this: this,
argumentList: argumentList,
}
length := int(toInt32(target.get(propertyLength)))
length -= len(argumentList)
if length < 0 {
length = 0
}
o.defineProperty("name", stringValue("bound "+target.get("name").String()), 0o000, false)
o.defineProperty(propertyLength, intValue(length), 0o000, false)
o.defineProperty("caller", Value{}, 0o000, false) // TODO Should throw a TypeError
o.defineProperty("arguments", Value{}, 0o000, false) // TODO Should throw a TypeError
return o
}
// [[Construct]].
func (fn bindFunctionObject) construct(argumentList []Value) Value {
obj := fn.target
switch value := obj.value.(type) {
case nativeFunctionObject:
return value.construct(obj, fn.argumentList)
case nodeFunctionObject:
argumentList = append(fn.argumentList, argumentList...)
return obj.construct(argumentList)
default:
panic(fn.target.runtime.panicTypeError("construct unknown type %T", obj.value))
}
}
// nodeFunctionObject.
type nodeFunctionObject struct {
node *nodeFunctionLiteral
stash stasher
}
func (rt *runtime) newNodeFunctionObject(node *nodeFunctionLiteral, stash stasher) *object {
o := rt.newClassObject(classFunctionName)
o.value = nodeFunctionObject{
node: node,
stash: stash,
}
o.defineProperty("name", stringValue(node.name), 0o000, false)
o.defineProperty(propertyLength, intValue(len(node.parameterList)), 0o000, false)
o.defineOwnProperty("caller", property{
value: propertyGetSet{
rt.newNativeFunction("get", "internal", 0, func(fc FunctionCall) Value {
for sc := rt.scope; sc != nil; sc = sc.outer {
if sc.frame.fn == o {
if sc.outer == nil || sc.outer.frame.fn == nil {
return nullValue
}
return rt.toValue(sc.outer.frame.fn)
}
}
return nullValue
}),
&nilGetSetObject,
},
mode: 0o000,
}, false)
return o
}
// _object.
func (o *object) isCall() bool {
switch fn := o.value.(type) {
case nativeFunctionObject:
return fn.call != nil
case bindFunctionObject:
return true
case nodeFunctionObject:
return true
default:
return false
}
}
func (o *object) call(this Value, argumentList []Value, eval bool, frm frame) Value { //nolint:unparam // Isn't currently used except in recursive self.
switch fn := o.value.(type) {
case nativeFunctionObject:
// Since eval is a native function, we only have to check for it here
if eval {
eval = o == o.runtime.eval // If eval is true, then it IS a direct eval
}
// Enter a scope, name from the native object...
rt := o.runtime
if rt.scope != nil && !eval {
rt.enterFunctionScope(rt.scope.lexical, this)
rt.scope.frame = frame{
native: true,
nativeFile: fn.file,
nativeLine: fn.line,
callee: fn.name,
file: nil,
fn: o,
}
defer func() {
rt.leaveScope()
}()
}
return fn.call(FunctionCall{
runtime: o.runtime,
eval: eval,
This: this,
ArgumentList: argumentList,
Otto: o.runtime.otto,
})
case bindFunctionObject:
// TODO Passthrough site, do not enter a scope
argumentList = append(fn.argumentList, argumentList...)
return fn.target.call(fn.this, argumentList, false, frm)
case nodeFunctionObject:
rt := o.runtime
stash := rt.enterFunctionScope(fn.stash, this)
rt.scope.frame = frame{
callee: fn.node.name,
file: fn.node.file,
fn: o,
}
defer func() {
rt.leaveScope()
}()
callValue := rt.cmplCallNodeFunction(o, stash, fn.node, argumentList)
if value, valid := callValue.value.(result); valid {
return value.value
}
return callValue
}
panic(o.runtime.panicTypeError("%v is not a function", objectValue(o)))
}
func (o *object) construct(argumentList []Value) Value {
switch fn := o.value.(type) {
case nativeFunctionObject:
if fn.call == nil {
panic(o.runtime.panicTypeError("%v is not a function", objectValue(o)))
}
if fn.construct == nil {
panic(o.runtime.panicTypeError("%v is not a constructor", objectValue(o)))
}
return fn.construct(o, argumentList)
case bindFunctionObject:
return fn.construct(argumentList)
case nodeFunctionObject:
return defaultConstruct(o, argumentList)
}
panic(o.runtime.panicTypeError("%v is not a function", objectValue(o)))
}
// 15.3.5.3.
func (o *object) hasInstance(of Value) bool {
if !o.isCall() {
// We should not have a hasInstance method
panic(o.runtime.panicTypeError("Object.hasInstance not callable"))
}
if !of.IsObject() {
return false
}
prototype := o.get("prototype")
if !prototype.IsObject() {
panic(o.runtime.panicTypeError("Object.hasInstance prototype %q is not an object", prototype))
}
prototypeObject := prototype.object()
value := of.object().prototype
for value != nil {
if value == prototypeObject {
return true
}
value = value.prototype
}
return false
}
// FunctionCall is an encapsulation of a JavaScript function call.
type FunctionCall struct {
This Value
runtime *runtime
thisObj *object
Otto *Otto
ArgumentList []Value
eval bool
}
// Argument will return the value of the argument at the given index.
//
// If no such argument exists, undefined is returned.
func (f FunctionCall) Argument(index int) Value {
return valueOfArrayIndex(f.ArgumentList, index)
}
func (f FunctionCall) getArgument(index int) (Value, bool) {
return getValueOfArrayIndex(f.ArgumentList, index)
}
func (f FunctionCall) slice(index int) []Value {
if index < len(f.ArgumentList) {
return f.ArgumentList[index:]
}
return []Value{}
}
func (f *FunctionCall) thisObject() *object {
if f.thisObj == nil {
this := f.This.resolve() // FIXME Is this right?
f.thisObj = f.runtime.toObject(this)
}
return f.thisObj
}
func (f *FunctionCall) thisClassObject(class string) *object {
if o := f.thisObject(); o.class != class {
panic(f.runtime.panicTypeError("Function.Class %s != %s", o.class, class))
}
return f.thisObj
}
func (f FunctionCall) toObject(value Value) *object {
return f.runtime.toObject(value)
}
// CallerLocation will return file location information (file:line:pos) where this function is being called.
func (f FunctionCall) CallerLocation() string {
// see error.go for location()
return f.runtime.scope.outer.frame.location()
}