forked from robertkrimen/otto
/
type_function.go
277 lines (234 loc) · 7.5 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
package otto
type _functionObject struct {
call _callFunction
construct _constructFunction
}
func (self0 _functionObject) clone(clone *_clone) _functionObject {
return _functionObject{
clone.callFunction(self0.call),
self0.construct,
}
}
func (runtime *_runtime) newNativeFunctionObject(native _nativeFunction, length int) *_object {
self := runtime.newClassObject("Function")
self.value = _functionObject{
call: newNativeCallFunction(native),
construct: defaultConstructFunction,
}
self.defineProperty("length", toValue_int(length), 0000, false)
return self
}
func (runtime *_runtime) newNodeFunctionObject(node *_functionNode, scopeEnvironment _environment) *_object {
self := runtime.newClassObject("Function")
self.value = _functionObject{
call: newNodeCallFunction(node, scopeEnvironment),
construct: defaultConstructFunction,
}
self.defineProperty("length", toValue_int(len(node.ParameterList)), 0000, false)
return self
}
func (runtime *_runtime) newBoundFunctionObject(target *_object, this Value, argumentList []Value) *_object {
self := runtime.newClassObject("Function")
self.value = _functionObject{
call: newBoundCallFunction(target, this, argumentList),
construct: defaultConstructFunction,
}
// FIXME
self.defineProperty("length", toValue_int(0), 0000, false)
return self
}
func (self *_object) functionValue() _functionObject {
value, _ := self.value.(_functionObject)
return value
}
func (self *_object) Call(this Value, argumentList ...interface{}) Value {
if self.functionValue().call == nil {
panic(newTypeError("%v is not a function", toValue_object(self)))
}
return self.runtime.Call(self, this, self.runtime.toValueArray(argumentList...), false)
// ... -> runtime -> self.Function.Call.Dispatch -> ...
}
func (self *_object) Construct(this Value, argumentList ...interface{}) Value {
function := self.functionValue()
if function.call == nil {
panic(newTypeError("%v is not a function", toValue_object(self)))
}
if function.construct == nil {
panic(newTypeError("%v is not a constructor", toValue_object(self)))
}
return function.construct(self, this, self.runtime.toValueArray(argumentList...))
}
func defaultConstructFunction(self *_object, this Value, argumentList []Value) Value {
newObject := self.runtime.newObject()
newObject.class = "Object"
prototypeValue := self.get("prototype")
if !prototypeValue.IsObject() {
prototypeValue = toValue_object(self.runtime.Global.ObjectPrototype)
}
newObject.prototype = prototypeValue._object()
newObjectValue := toValue_object(newObject)
result := self.Call(newObjectValue, argumentList)
if result.IsObject() {
return result
}
return newObjectValue
}
func (self *_object) CallGet(name string) Value {
return self.runtime.Call(self, toValue_object(self), []Value{toValue_string(name)}, false)
}
func (self *_object) CallSet(name string, value Value) {
self.runtime.Call(self, toValue_object(self), []Value{toValue_string(name), value}, false)
}
// 15.3.5.3
func (self *_object) HasInstance(of Value) bool {
if self.functionValue().call == nil {
// We should not have a HasInstance method
panic(newTypeError())
}
if !of.IsObject() {
return false
}
prototype := self.get("prototype")
if !prototype.IsObject() {
panic(newTypeError())
}
prototypeObject := prototype._object()
value := of._object().prototype
for value != nil {
if value == prototypeObject {
return true
}
value = value.prototype
}
return false
}
type _nativeFunction func(FunctionCall) Value
// _constructFunction
type _constructFunction func(*_object, Value, []Value) Value
// _callFunction
type _callFunction interface {
Dispatch(*_object, *_functionEnvironment, *_runtime, Value, []Value, bool) Value
Source() string
ScopeEnvironment() _environment
clone(clone *_clone) _callFunction
}
// _nativeCallFunction
type _nativeCallFunction _nativeFunction
func newNativeCallFunction(native _nativeFunction) _nativeCallFunction {
return _nativeCallFunction(native)
}
func (self _nativeCallFunction) Dispatch(_ *_object, _ *_functionEnvironment, runtime *_runtime, this Value, argumentList []Value, evalHint bool) Value {
return self(FunctionCall{
runtime: runtime,
evalHint: evalHint,
This: this,
ArgumentList: argumentList,
Otto: runtime.Otto,
})
}
func (self _nativeCallFunction) ScopeEnvironment() _environment {
return nil
}
func (self _nativeCallFunction) Source() string {
return ""
}
func (self0 _nativeCallFunction) clone(clone *_clone) _callFunction {
return self0
}
// _nodeCallFunction
type _nodeCallFunction struct {
node *_functionNode
scopeEnvironment _environment // Can be either Lexical or Variable
}
func newNodeCallFunction(node *_functionNode, scopeEnvironment _environment) *_nodeCallFunction {
self := &_nodeCallFunction{
node: node,
}
self.scopeEnvironment = scopeEnvironment
return self
}
func (self _nodeCallFunction) Dispatch(function *_object, environment *_functionEnvironment, runtime *_runtime, this Value, argumentList []Value, _ bool) Value {
return runtime._callNode(function, environment, self.node, this, argumentList)
}
func (self _nodeCallFunction) ScopeEnvironment() _environment {
return self.scopeEnvironment
}
func (self _nodeCallFunction) Source() string {
return ""
}
func (self0 _nodeCallFunction) clone(clone *_clone) _callFunction {
return _nodeCallFunction{
node: self0.node,
scopeEnvironment: clone.environment(self0.scopeEnvironment),
}
}
// _boundCallFunction
type _boundCallFunction struct {
target *_object
this Value
argumentList []Value
}
func newBoundCallFunction(target *_object, this Value, argumentList []Value) *_boundCallFunction {
self := &_boundCallFunction{
target: target,
this: this,
argumentList: argumentList,
}
return self
}
func (self _boundCallFunction) Dispatch(_ *_object, _ *_functionEnvironment, runtime *_runtime, this Value, argumentList []Value, _ bool) Value {
argumentList = append(self.argumentList, argumentList...)
return runtime.Call(self.target, self.this, argumentList, false)
}
func (self _boundCallFunction) ScopeEnvironment() _environment {
return nil
}
func (self _boundCallFunction) Source() string {
return ""
}
func (self0 _boundCallFunction) clone(clone *_clone) _callFunction {
return _boundCallFunction{
target: clone.object(self0.target),
this: clone.value(self0.this),
argumentList: clone.valueArray(self0.argumentList),
}
}
// FunctionCall{}
// FunctionCall is an enscapulation of a JavaScript function call.
type FunctionCall struct {
runtime *_runtime
_thisObject *_object
evalHint bool
This Value
ArgumentList []Value
Otto *Otto
}
// Argument will return the value of the argument at the given index.
//
// If no such argument exists, undefined is returned.
func (self FunctionCall) Argument(index int) Value {
return valueOfArrayIndex(self.ArgumentList, index)
}
func (self FunctionCall) slice(index int) []Value {
if index < len(self.ArgumentList) {
return self.ArgumentList[index:]
}
return []Value{}
}
func (self *FunctionCall) thisObject() *_object {
if self._thisObject == nil {
this := self.runtime.GetValue(self.This) // FIXME Is this right?
self._thisObject = self.runtime.toObject(this)
}
return self._thisObject
}
func (self *FunctionCall) thisClassObject(class string) *_object {
thisObject := self.thisObject()
if thisObject.class != class {
panic(newTypeError())
}
return self._thisObject
}
func (self FunctionCall) toObject(value Value) *_object {
return self.runtime.toObject(value)
}