2015-02-16 15:28:33 +02:00
|
|
|
package otto
|
|
|
|
|
|
|
|
import (
|
2015-03-20 14:22:01 +02:00
|
|
|
"fmt"
|
2015-02-16 15:28:33 +02:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
|
|
|
|
"github.com/robertkrimen/otto/parser"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Function
|
|
|
|
|
|
|
|
func builtinFunction(call FunctionCall) Value {
|
|
|
|
return toValue_object(builtinNewFunctionNative(call.runtime, call.ArgumentList))
|
|
|
|
}
|
|
|
|
|
2015-03-20 14:22:01 +02:00
|
|
|
func builtinNewFunction(self *_object, argumentList []Value) Value {
|
2015-02-16 15:28:33 +02:00
|
|
|
return toValue_object(builtinNewFunctionNative(self.runtime, argumentList))
|
|
|
|
}
|
|
|
|
|
|
|
|
func argumentList2parameterList(argumentList []Value) []string {
|
|
|
|
parameterList := make([]string, 0, len(argumentList))
|
|
|
|
for _, value := range argumentList {
|
2015-03-20 14:22:01 +02:00
|
|
|
tmp := strings.FieldsFunc(value.string(), func(chr rune) bool {
|
2015-02-16 15:28:33 +02:00
|
|
|
return chr == ',' || unicode.IsSpace(chr)
|
|
|
|
})
|
|
|
|
parameterList = append(parameterList, tmp...)
|
|
|
|
}
|
|
|
|
return parameterList
|
|
|
|
}
|
|
|
|
|
|
|
|
var matchIdentifier = regexp.MustCompile(`^[$_\p{L}][$_\p{L}\d}]*$`)
|
|
|
|
|
|
|
|
func builtinNewFunctionNative(runtime *_runtime, argumentList []Value) *_object {
|
|
|
|
var parameterList, body string
|
|
|
|
count := len(argumentList)
|
|
|
|
if count > 0 {
|
|
|
|
tmp := make([]string, 0, count-1)
|
|
|
|
for _, value := range argumentList[0 : count-1] {
|
2015-03-20 14:22:01 +02:00
|
|
|
tmp = append(tmp, value.string())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
parameterList = strings.Join(tmp, ",")
|
2015-03-20 14:22:01 +02:00
|
|
|
body = argumentList[count-1].string()
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
2015-03-20 14:22:01 +02:00
|
|
|
// FIXME
|
2015-02-16 15:28:33 +02:00
|
|
|
function, err := parser.ParseFunction(parameterList, body)
|
|
|
|
runtime.parseThrow(err) // Will panic/throw appropriately
|
2015-03-20 14:22:01 +02:00
|
|
|
cmpl := _compiler{}
|
|
|
|
cmpl_function := cmpl.parseExpression(function)
|
2015-02-16 15:28:33 +02:00
|
|
|
|
2015-03-20 14:22:01 +02:00
|
|
|
return runtime.newNodeFunction(cmpl_function.(*_nodeFunctionLiteral), runtime.globalStash)
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func builtinFunction_toString(call FunctionCall) Value {
|
|
|
|
object := call.thisClassObject("Function") // Should throw a TypeError unless Function
|
2015-03-20 14:22:01 +02:00
|
|
|
switch fn := object.value.(type) {
|
|
|
|
case _nativeFunctionObject:
|
|
|
|
return toValue_string(fmt.Sprintf("function %s() { [native code] }", fn.name))
|
|
|
|
case _nodeFunctionObject:
|
|
|
|
return toValue_string(fn.node.source)
|
|
|
|
case _bindFunctionObject:
|
|
|
|
return toValue_string("function () { [native code] }")
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(call.runtime.panicTypeError("Function.toString()"))
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func builtinFunction_apply(call FunctionCall) Value {
|
|
|
|
if !call.This.isCallable() {
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
this := call.Argument(0)
|
|
|
|
if this.IsUndefined() {
|
|
|
|
// FIXME Not ECMA5
|
2015-03-20 14:22:01 +02:00
|
|
|
this = toValue_object(call.runtime.globalObject)
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
argumentList := call.Argument(1)
|
2015-03-20 14:22:01 +02:00
|
|
|
switch argumentList.kind {
|
2015-02-16 15:28:33 +02:00
|
|
|
case valueUndefined, valueNull:
|
2015-03-20 14:22:01 +02:00
|
|
|
return call.thisObject().call(this, nil, false, nativeFrame)
|
2015-02-16 15:28:33 +02:00
|
|
|
case valueObject:
|
|
|
|
default:
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
arrayObject := argumentList._object()
|
|
|
|
thisObject := call.thisObject()
|
|
|
|
length := int64(toUint32(arrayObject.get("length")))
|
|
|
|
valueArray := make([]Value, length)
|
|
|
|
for index := int64(0); index < length; index++ {
|
|
|
|
valueArray[index] = arrayObject.get(arrayIndexToString(index))
|
|
|
|
}
|
2015-03-20 14:22:01 +02:00
|
|
|
return thisObject.call(this, valueArray, false, nativeFrame)
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func builtinFunction_call(call FunctionCall) Value {
|
|
|
|
if !call.This.isCallable() {
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
thisObject := call.thisObject()
|
|
|
|
this := call.Argument(0)
|
|
|
|
if this.IsUndefined() {
|
|
|
|
// FIXME Not ECMA5
|
2015-03-20 14:22:01 +02:00
|
|
|
this = toValue_object(call.runtime.globalObject)
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
if len(call.ArgumentList) >= 1 {
|
2015-03-20 14:22:01 +02:00
|
|
|
return thisObject.call(this, call.ArgumentList[1:], false, nativeFrame)
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
2015-03-20 14:22:01 +02:00
|
|
|
return thisObject.call(this, nil, false, nativeFrame)
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func builtinFunction_bind(call FunctionCall) Value {
|
|
|
|
target := call.This
|
|
|
|
if !target.isCallable() {
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
targetObject := target._object()
|
|
|
|
|
|
|
|
this := call.Argument(0)
|
|
|
|
argumentList := call.slice(1)
|
|
|
|
if this.IsUndefined() {
|
|
|
|
// FIXME Do this elsewhere?
|
2015-03-20 14:22:01 +02:00
|
|
|
this = toValue_object(call.runtime.globalObject)
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return toValue_object(call.runtime.newBoundFunction(targetObject, this, argumentList))
|
|
|
|
}
|