2024-02-24 21:00:04 +00:00
|
|
|
package otto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
|
|
|
|
"github.com/robertkrimen/otto/parser"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Function
|
|
|
|
|
|
|
|
func builtinFunction(call FunctionCall) Value {
|
2024-04-04 14:46:14 +00:00
|
|
|
return objectValue(builtinNewFunctionNative(call.runtime, call.ArgumentList))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinNewFunction(obj *object, argumentList []Value) Value {
|
|
|
|
return objectValue(builtinNewFunctionNative(obj.runtime, argumentList))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func argumentList2parameterList(argumentList []Value) []string {
|
|
|
|
parameterList := make([]string, 0, len(argumentList))
|
|
|
|
for _, value := range argumentList {
|
|
|
|
tmp := strings.FieldsFunc(value.string(), func(chr rune) bool {
|
|
|
|
return chr == ',' || unicode.IsSpace(chr)
|
|
|
|
})
|
|
|
|
parameterList = append(parameterList, tmp...)
|
|
|
|
}
|
|
|
|
return parameterList
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinNewFunctionNative(rt *runtime, argumentList []Value) *object {
|
2024-02-24 21:00:04 +00:00
|
|
|
var parameterList, body string
|
2024-04-04 14:46:14 +00:00
|
|
|
if count := len(argumentList); count > 0 {
|
2024-02-24 21:00:04 +00:00
|
|
|
tmp := make([]string, 0, count-1)
|
|
|
|
for _, value := range argumentList[0 : count-1] {
|
|
|
|
tmp = append(tmp, value.string())
|
|
|
|
}
|
|
|
|
parameterList = strings.Join(tmp, ",")
|
|
|
|
body = argumentList[count-1].string()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME
|
|
|
|
function, err := parser.ParseFunction(parameterList, body)
|
2024-04-04 14:46:14 +00:00
|
|
|
rt.parseThrow(err) // Will panic/throw appropriately
|
|
|
|
cmpl := compiler{}
|
|
|
|
cmplFunction := cmpl.parseExpression(function)
|
2024-02-24 21:00:04 +00:00
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
return rt.newNodeFunction(cmplFunction.(*nodeFunctionLiteral), rt.globalStash)
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinFunctionToString(call FunctionCall) Value {
|
|
|
|
obj := call.thisClassObject(classFunctionName) // Should throw a TypeError unless Function
|
|
|
|
switch fn := obj.value.(type) {
|
|
|
|
case nativeFunctionObject:
|
|
|
|
return stringValue(fmt.Sprintf("function %s() { [native code] }", fn.name))
|
|
|
|
case nodeFunctionObject:
|
|
|
|
return stringValue(fn.node.source)
|
|
|
|
case bindFunctionObject:
|
|
|
|
return stringValue("function () { [native code] }")
|
|
|
|
default:
|
|
|
|
panic(call.runtime.panicTypeError("Function.toString unknown type %T", obj.value))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinFunctionApply(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
if !call.This.isCallable() {
|
2024-04-04 14:46:14 +00:00
|
|
|
panic(call.runtime.panicTypeError("Function.apply %q is not callable", call.This))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
this := call.Argument(0)
|
|
|
|
if this.IsUndefined() {
|
|
|
|
// FIXME Not ECMA5
|
2024-04-04 14:46:14 +00:00
|
|
|
this = objectValue(call.runtime.globalObject)
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
argumentList := call.Argument(1)
|
|
|
|
switch argumentList.kind {
|
|
|
|
case valueUndefined, valueNull:
|
|
|
|
return call.thisObject().call(this, nil, false, nativeFrame)
|
|
|
|
case valueObject:
|
|
|
|
default:
|
2024-04-04 14:46:14 +00:00
|
|
|
panic(call.runtime.panicTypeError("Function.apply unknown type %T for second argument"))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
arrayObject := argumentList.object()
|
2024-02-24 21:00:04 +00:00
|
|
|
thisObject := call.thisObject()
|
|
|
|
length := int64(toUint32(arrayObject.get(propertyLength)))
|
|
|
|
valueArray := make([]Value, length)
|
|
|
|
for index := int64(0); index < length; index++ {
|
|
|
|
valueArray[index] = arrayObject.get(arrayIndexToString(index))
|
|
|
|
}
|
|
|
|
return thisObject.call(this, valueArray, false, nativeFrame)
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinFunctionCall(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
if !call.This.isCallable() {
|
2024-04-04 14:46:14 +00:00
|
|
|
panic(call.runtime.panicTypeError("Function.call %q is not callable", call.This))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
thisObject := call.thisObject()
|
|
|
|
this := call.Argument(0)
|
|
|
|
if this.IsUndefined() {
|
|
|
|
// FIXME Not ECMA5
|
2024-04-04 14:46:14 +00:00
|
|
|
this = objectValue(call.runtime.globalObject)
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
if len(call.ArgumentList) >= 1 {
|
|
|
|
return thisObject.call(this, call.ArgumentList[1:], false, nativeFrame)
|
|
|
|
}
|
|
|
|
return thisObject.call(this, nil, false, nativeFrame)
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinFunctionBind(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
target := call.This
|
|
|
|
if !target.isCallable() {
|
2024-04-04 14:46:14 +00:00
|
|
|
panic(call.runtime.panicTypeError("Function.bind %q is not callable", call.This))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
targetObject := target.object()
|
2024-02-24 21:00:04 +00:00
|
|
|
|
|
|
|
this := call.Argument(0)
|
|
|
|
argumentList := call.slice(1)
|
|
|
|
if this.IsUndefined() {
|
|
|
|
// FIXME Do this elsewhere?
|
2024-04-04 14:46:14 +00:00
|
|
|
this = objectValue(call.runtime.globalObject)
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
return objectValue(call.runtime.newBoundFunction(targetObject, this, argumentList))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|