forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin_function.go
165 lines (148 loc) · 4.26 KB
/
builtin_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
package goja
import (
"fmt"
)
func (r *Runtime) builtin_Function(args []Value, proto *Object) *Object {
src := "(function anonymous("
if len(args) > 1 {
for _, arg := range args[:len(args)-1] {
src += arg.String() + ","
}
src = src[:len(src)-1]
}
body := ""
if len(args) > 0 {
body = args[len(args)-1].String()
}
src += "){" + body + "})"
return r.toObject(r.eval(src, false, false, _undefined))
}
func (r *Runtime) functionproto_toString(call FunctionCall) Value {
obj := r.toObject(call.This)
repeat:
switch f := obj.self.(type) {
case *funcObject:
return newStringValue(f.src)
case *nativeFuncObject:
return newStringValue(fmt.Sprintf("function %s() { [native code] }", f.nameProp.get(call.This).ToString()))
case *boundFuncObject:
return newStringValue(fmt.Sprintf("function %s() { [native code] }", f.nameProp.get(call.This).ToString()))
case *lazyObject:
obj.self = f.create(obj)
goto repeat
}
r.typeErrorResult(true, "Object is not a function")
return nil
}
func (r *Runtime) toValueArray(a Value) []Value {
obj := r.toObject(a)
l := toUInt32(obj.self.getStr("length"))
ret := make([]Value, l)
for i := uint32(0); i < l; i++ {
ret[i] = obj.self.get(valueInt(i))
}
return ret
}
func (r *Runtime) functionproto_apply(call FunctionCall) Value {
f := r.toCallable(call.This)
var args []Value
if len(call.Arguments) >= 2 {
args = r.toValueArray(call.Arguments[1])
}
return f(FunctionCall{
This: call.Argument(0),
Arguments: args,
})
}
func (r *Runtime) functionproto_call(call FunctionCall) Value {
f := r.toCallable(call.This)
var args []Value
if len(call.Arguments) > 0 {
args = call.Arguments[1:]
}
return f(FunctionCall{
This: call.Argument(0),
Arguments: args,
})
}
func (r *Runtime) boundCallable(target func(FunctionCall) Value, boundArgs []Value) func(FunctionCall) Value {
var this Value
var args []Value
if len(boundArgs) > 0 {
this = boundArgs[0]
args = make([]Value, len(boundArgs)-1)
copy(args, boundArgs[1:])
} else {
this = _undefined
}
return func(call FunctionCall) Value {
a := append(args, call.Arguments...)
return target(FunctionCall{
This: this,
Arguments: a,
})
}
}
func (r *Runtime) boundConstruct(target func([]Value) *Object, boundArgs []Value) func([]Value) *Object {
if target == nil {
return nil
}
var args []Value
if len(boundArgs) > 1 {
args = make([]Value, len(boundArgs)-1)
copy(args, boundArgs[1:])
}
return func(fargs []Value) *Object {
a := append(args, fargs...)
copy(a, args)
return target(a)
}
}
func (r *Runtime) functionproto_bind(call FunctionCall) Value {
obj := r.toObject(call.This)
f := obj.self
var fcall func(FunctionCall) Value
var construct func([]Value) *Object
repeat:
switch ff := f.(type) {
case *funcObject:
fcall = ff.Call
construct = ff.construct
case *nativeFuncObject:
fcall = ff.f
construct = ff.construct
case *boundFuncObject:
f = &ff.nativeFuncObject
goto repeat
case *lazyObject:
f = ff.create(obj)
goto repeat
default:
r.typeErrorResult(true, "Value is not callable: %s", obj.ToString())
}
l := int(toUInt32(obj.self.getStr("length")))
l -= len(call.Arguments) - 1
if l < 0 {
l = 0
}
v := &Object{runtime: r}
ff := r.newNativeFuncObj(v, r.boundCallable(fcall, call.Arguments), r.boundConstruct(construct, call.Arguments), "", nil, l)
v.self = &boundFuncObject{
nativeFuncObject: *ff,
}
//ret := r.newNativeFunc(r.boundCallable(f, call.Arguments), nil, "", nil, l)
//o := ret.self
//o.putStr("caller", r.global.throwerProperty, false)
//o.putStr("arguments", r.global.throwerProperty, false)
return v
}
func (r *Runtime) initFunction() {
o := r.global.FunctionPrototype.self
o.(*nativeFuncObject).prototype = r.global.ObjectPrototype
o._putProp("toString", r.newNativeFunc(r.functionproto_toString, nil, "toString", nil, 0), true, false, true)
o._putProp("apply", r.newNativeFunc(r.functionproto_apply, nil, "apply", nil, 2), true, false, true)
o._putProp("call", r.newNativeFunc(r.functionproto_call, nil, "call", nil, 1), true, false, true)
o._putProp("bind", r.newNativeFunc(r.functionproto_bind, nil, "bind", nil, 1), true, false, true)
r.global.Function = r.newNativeFuncConstruct(r.builtin_Function, "Function", r.global.FunctionPrototype, 1)
r.addToGlobal("Function", r.global.Function)
}