1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-19 19:55:30 +08:00
otto/builtin_function.go
Robert Krimen c55510cb36 Inline context initialization & improve _object.value
* Context setup is now done via _newContext.
* _newContext is a function that resides in inline.go. _newContext is very flat,
resulting in almost no function calls (a 180 from the earlier status quo).
* inline.go is a Go source file that is built by Perl (via inline).
* Lots of crufty functions removed (along with all of their TODO & FIXME).
* In addition, before, the underlying value of _object.value was a pointer to
something. This made for extra work, since the type of _object.value is interface{},
which is already something of a pointer. Now, the underlying value of _object.value
in Function, Date, RegExp, ..., is a struct value.
* type_function.go was streamlined, removing superfluous struct fields and methods.
* There is now less "digging" to get to the actual value of a function, which is important
when makings lots of calls.

Before (without inline):

    PASS
    BenchmarkNew        2000           1067871 ns/op
    ok      github.com/robertkrimen/otto    3.336s
    PASS
    BenchmarkNew        2000           1077644 ns/op
    ok      github.com/robertkrimen/otto    3.367s

After (with inline):

    PASS
    BenchmarkNew       10000            364418 ns/op
    ok      github.com/robertkrimen/otto    4.616s
    PASS
    BenchmarkNew       10000            307241 ns/op
    ok      github.com/robertkrimen/otto    4.051s

This (partially) fixes #22
2013-06-09 18:28:18 -07:00

95 lines
2.5 KiB
Go

package otto
// Function
func builtinFunction(call FunctionCall) Value {
return toValue(builtinNewFunctionNative(call.runtime, call.ArgumentList))
}
func builtinNewFunction(self *_object, _ Value, argumentList []Value) Value {
return toValue(builtinNewFunctionNative(self.runtime, argumentList))
}
func builtinNewFunctionNative(runtime *_runtime, argumentList []Value) *_object {
parameterList := []string{}
bodySource := ""
argumentCount := len(argumentList)
if argumentCount > 0 {
bodySource = toString(argumentList[argumentCount-1])
argumentList = argumentList[0 : argumentCount-1]
for _, value := range argumentList {
parameterList = append(parameterList, toString(value))
}
}
parser := newParser()
parser.lexer.Source = bodySource
_programNode := parser.ParseAsFunction()
return runtime.newNodeFunction(_programNode.toFunction(parameterList), runtime.GlobalEnvironment)
}
func builtinFunction_toString(call FunctionCall) Value {
return toValue("[function]")
}
func builtinFunction_apply(call FunctionCall) Value {
if !call.This.isCallable() {
panic(newTypeError())
}
this := call.Argument(0)
if this.IsUndefined() {
// FIXME Not ECMA5
this = toValue(call.runtime.GlobalObject)
}
argumentList := call.Argument(1)
switch argumentList._valueType {
case valueUndefined, valueNull:
return call.thisObject().Call(this, []Value{})
case valueObject:
default:
panic(newTypeError())
}
arrayObject := argumentList._object()
thisObject := call.thisObject()
length := uint(toUint32(arrayObject.get("length")))
valueArray := make([]Value, length)
for index := uint(0); index < length; index++ {
valueArray[index] = arrayObject.get(arrayIndexToString(index))
}
return thisObject.Call(this, valueArray)
}
func builtinFunction_call(call FunctionCall) Value {
if !call.This.isCallable() {
panic(newTypeError())
}
thisObject := call.thisObject()
this := call.Argument(0)
if this.IsUndefined() {
// FIXME Not ECMA5
this = toValue(call.runtime.GlobalObject)
}
if len(call.ArgumentList) >= 1 {
return thisObject.Call(this, call.ArgumentList[1:])
}
return thisObject.Call(this, []Value{})
}
func builtinFunction_bind(call FunctionCall) Value {
target := call.This
if !target.isCallable() {
panic(newTypeError())
}
targetObject := target._object()
this := call.Argument(0)
argumentList := call.slice(1)
if this.IsUndefined() {
// FIXME Do this elsewhere?
this = toValue(call.runtime.GlobalObject)
}
return toValue(call.runtime.newBoundFunctionObject(targetObject, this, argumentList))
}