mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
712 lines
19 KiB
712 lines
19 KiB
10 years ago
|
package otto
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
9 years ago
|
"fmt"
|
||
|
"math"
|
||
8 years ago
|
"path"
|
||
10 years ago
|
"reflect"
|
||
8 years ago
|
"runtime"
|
||
|
"strconv"
|
||
10 years ago
|
"sync"
|
||
10 years ago
|
|
||
|
"github.com/robertkrimen/otto/ast"
|
||
|
"github.com/robertkrimen/otto/parser"
|
||
|
)
|
||
|
|
||
|
type _global struct {
|
||
|
Object *_object // Object( ... ), new Object( ... ) - 1 (length)
|
||
|
Function *_object // Function( ... ), new Function( ... ) - 1
|
||
|
Array *_object // Array( ... ), new Array( ... ) - 1
|
||
|
String *_object // String( ... ), new String( ... ) - 1
|
||
|
Boolean *_object // Boolean( ... ), new Boolean( ... ) - 1
|
||
|
Number *_object // Number( ... ), new Number( ... ) - 1
|
||
|
Math *_object
|
||
|
Date *_object // Date( ... ), new Date( ... ) - 7
|
||
|
RegExp *_object // RegExp( ... ), new RegExp( ... ) - 2
|
||
|
Error *_object // Error( ... ), new Error( ... ) - 1
|
||
|
EvalError *_object
|
||
|
TypeError *_object
|
||
|
RangeError *_object
|
||
|
ReferenceError *_object
|
||
|
SyntaxError *_object
|
||
|
URIError *_object
|
||
|
JSON *_object
|
||
|
|
||
|
ObjectPrototype *_object // Object.prototype
|
||
|
FunctionPrototype *_object // Function.prototype
|
||
|
ArrayPrototype *_object // Array.prototype
|
||
|
StringPrototype *_object // String.prototype
|
||
|
BooleanPrototype *_object // Boolean.prototype
|
||
|
NumberPrototype *_object // Number.prototype
|
||
|
DatePrototype *_object // Date.prototype
|
||
|
RegExpPrototype *_object // RegExp.prototype
|
||
|
ErrorPrototype *_object // Error.prototype
|
||
|
EvalErrorPrototype *_object
|
||
|
TypeErrorPrototype *_object
|
||
|
RangeErrorPrototype *_object
|
||
|
ReferenceErrorPrototype *_object
|
||
|
SyntaxErrorPrototype *_object
|
||
|
URIErrorPrototype *_object
|
||
|
}
|
||
|
|
||
|
type _runtime struct {
|
||
10 years ago
|
global _global
|
||
|
globalObject *_object
|
||
|
globalStash *_objectStash
|
||
|
scope *_scope
|
||
|
otto *Otto
|
||
|
eval *_object // The builtin eval, for determine indirect versus direct invocation
|
||
9 years ago
|
debugger func(*Otto)
|
||
9 years ago
|
random func() float64
|
||
8 years ago
|
stackLimit int
|
||
|
traceLimit int
|
||
10 years ago
|
|
||
|
labels []string // FIXME
|
||
10 years ago
|
lck sync.Mutex
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
func (self *_runtime) enterScope(scope *_scope) {
|
||
|
scope.outer = self.scope
|
||
8 years ago
|
if self.scope != nil {
|
||
|
if self.stackLimit != 0 && self.scope.depth+1 >= self.stackLimit {
|
||
|
panic(self.panicRangeError("Maximum call stack size exceeded"))
|
||
|
}
|
||
|
|
||
|
scope.depth = self.scope.depth + 1
|
||
|
}
|
||
|
|
||
10 years ago
|
self.scope = scope
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
func (self *_runtime) leaveScope() {
|
||
|
self.scope = self.scope.outer
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// FIXME This is used in two places (cloning)
|
||
|
func (self *_runtime) enterGlobalScope() {
|
||
|
self.enterScope(newScope(self.globalStash, self.globalStash, self.globalObject))
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
func (self *_runtime) enterFunctionScope(outer _stash, this Value) *_fnStash {
|
||
|
if outer == nil {
|
||
|
outer = self.globalStash
|
||
10 years ago
|
}
|
||
10 years ago
|
stash := self.newFunctionStash(outer)
|
||
10 years ago
|
var thisObject *_object
|
||
10 years ago
|
switch this.kind {
|
||
10 years ago
|
case valueUndefined, valueNull:
|
||
10 years ago
|
thisObject = self.globalObject
|
||
10 years ago
|
default:
|
||
|
thisObject = self.toObject(this)
|
||
|
}
|
||
10 years ago
|
self.enterScope(newScope(stash, stash, thisObject))
|
||
|
return stash
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
func (self *_runtime) putValue(reference _reference, value Value) {
|
||
|
name := reference.putValue(value)
|
||
|
if name != "" {
|
||
|
// Why? -- If reference.base == nil
|
||
|
// strict = false
|
||
|
self.globalObject.defineProperty(name, value, 0111, false)
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
func (self *_runtime) tryCatchEvaluate(inner func() Value) (tryValue Value, exception bool) {
|
||
|
// resultValue = The value of the block (e.g. the last statement)
|
||
|
// throw = Something was thrown
|
||
|
// throwValue = The value of what was thrown
|
||
|
// other = Something that changes flow (return, break, continue) that is not a throw
|
||
|
// Otherwise, some sort of unknown panic happened, we'll just propagate it
|
||
|
defer func() {
|
||
|
if caught := recover(); caught != nil {
|
||
|
if exception, ok := caught.(*_exception); ok {
|
||
|
caught = exception.eject()
|
||
|
}
|
||
|
switch caught := caught.(type) {
|
||
|
case _error:
|
||
|
exception = true
|
||
8 years ago
|
tryValue = toValue_object(self.newError(caught.name, caught.messageValue(), 0))
|
||
10 years ago
|
case Value:
|
||
|
exception = true
|
||
|
tryValue = caught
|
||
|
default:
|
||
|
panic(caught)
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
tryValue = inner()
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// toObject
|
||
|
|
||
|
func (self *_runtime) toObject(value Value) *_object {
|
||
10 years ago
|
switch value.kind {
|
||
10 years ago
|
case valueEmpty, valueUndefined, valueNull:
|
||
10 years ago
|
panic(self.panicTypeError())
|
||
10 years ago
|
case valueBoolean:
|
||
|
return self.newBoolean(value)
|
||
|
case valueString:
|
||
|
return self.newString(value)
|
||
|
case valueNumber:
|
||
|
return self.newNumber(value)
|
||
|
case valueObject:
|
||
|
return value._object()
|
||
|
}
|
||
10 years ago
|
panic(self.panicTypeError())
|
||
10 years ago
|
}
|
||
|
|
||
|
func (self *_runtime) objectCoerce(value Value) (*_object, error) {
|
||
10 years ago
|
switch value.kind {
|
||
10 years ago
|
case valueUndefined:
|
||
|
return nil, errors.New("undefined")
|
||
|
case valueNull:
|
||
|
return nil, errors.New("null")
|
||
|
case valueBoolean:
|
||
|
return self.newBoolean(value), nil
|
||
|
case valueString:
|
||
|
return self.newString(value), nil
|
||
|
case valueNumber:
|
||
|
return self.newNumber(value), nil
|
||
|
case valueObject:
|
||
|
return value._object(), nil
|
||
|
}
|
||
10 years ago
|
panic(self.panicTypeError())
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
func checkObjectCoercible(rt *_runtime, value Value) {
|
||
10 years ago
|
isObject, mustCoerce := testObjectCoercible(value)
|
||
|
if !isObject && !mustCoerce {
|
||
10 years ago
|
panic(rt.panicTypeError())
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// testObjectCoercible
|
||
|
|
||
|
func testObjectCoercible(value Value) (isObject bool, mustCoerce bool) {
|
||
10 years ago
|
switch value.kind {
|
||
10 years ago
|
case valueReference, valueEmpty, valueNull, valueUndefined:
|
||
|
return false, false
|
||
|
case valueNumber, valueString, valueBoolean:
|
||
8 years ago
|
return false, true
|
||
10 years ago
|
case valueObject:
|
||
8 years ago
|
return true, false
|
||
|
default:
|
||
|
panic("this should never happen")
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
func (self *_runtime) safeToValue(value interface{}) (Value, error) {
|
||
|
result := Value{}
|
||
10 years ago
|
err := catchPanic(func() {
|
||
|
result = self.toValue(value)
|
||
|
})
|
||
|
return result, err
|
||
|
}
|
||
|
|
||
9 years ago
|
// convertNumeric converts numeric parameter val from js to that of type t if it is safe to do so, otherwise it panics.
|
||
|
// This allows literals (int64), bitwise values (int32) and the general form (float64) of javascript numerics to be passed as parameters to go functions easily.
|
||
8 years ago
|
func (self *_runtime) convertNumeric(v Value, t reflect.Type) reflect.Value {
|
||
|
val := reflect.ValueOf(v.export())
|
||
|
|
||
9 years ago
|
if val.Kind() == t.Kind() {
|
||
|
return val
|
||
|
}
|
||
|
|
||
|
if val.Kind() == reflect.Interface {
|
||
|
val = reflect.ValueOf(val.Interface())
|
||
|
}
|
||
|
|
||
|
switch val.Kind() {
|
||
|
case reflect.Float32, reflect.Float64:
|
||
|
f64 := val.Float()
|
||
|
switch t.Kind() {
|
||
|
case reflect.Float64:
|
||
|
return reflect.ValueOf(f64)
|
||
|
case reflect.Float32:
|
||
|
if reflect.Zero(t).OverflowFloat(f64) {
|
||
8 years ago
|
panic(self.panicRangeError("converting float64 to float32 would overflow"))
|
||
9 years ago
|
}
|
||
|
|
||
|
return val.Convert(t)
|
||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||
|
i64 := int64(f64)
|
||
|
if float64(i64) != f64 {
|
||
8 years ago
|
panic(self.panicRangeError(fmt.Sprintf("converting %v to %v would cause loss of precision", val.Type(), t)))
|
||
9 years ago
|
}
|
||
|
|
||
|
// The float represents an integer
|
||
|
val = reflect.ValueOf(i64)
|
||
|
default:
|
||
8 years ago
|
panic(self.panicTypeError(fmt.Sprintf("cannot convert %v to %v", val.Type(), t)))
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
switch val.Kind() {
|
||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||
|
i64 := val.Int()
|
||
|
switch t.Kind() {
|
||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||
|
if reflect.Zero(t).OverflowInt(i64) {
|
||
8 years ago
|
panic(self.panicRangeError(fmt.Sprintf("converting %v to %v would overflow", val.Type(), t)))
|
||
9 years ago
|
}
|
||
|
return val.Convert(t)
|
||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||
|
if i64 < 0 {
|
||
8 years ago
|
panic(self.panicRangeError(fmt.Sprintf("converting %v to %v would underflow", val.Type(), t)))
|
||
9 years ago
|
}
|
||
|
if reflect.Zero(t).OverflowUint(uint64(i64)) {
|
||
8 years ago
|
panic(self.panicRangeError(fmt.Sprintf("converting %v to %v would overflow", val.Type(), t)))
|
||
9 years ago
|
}
|
||
|
return val.Convert(t)
|
||
|
}
|
||
|
|
||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||
|
u64 := val.Uint()
|
||
|
switch t.Kind() {
|
||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||
|
if u64 > math.MaxInt64 || reflect.Zero(t).OverflowInt(int64(u64)) {
|
||
8 years ago
|
panic(self.panicRangeError(fmt.Sprintf("converting %v to %v would overflow", val.Type(), t)))
|
||
9 years ago
|
}
|
||
|
return val.Convert(t)
|
||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||
|
if reflect.Zero(t).OverflowUint(u64) {
|
||
8 years ago
|
panic(self.panicRangeError(fmt.Sprintf("converting %v to %v would overflow", val.Type(), t)))
|
||
9 years ago
|
}
|
||
|
return val.Convert(t)
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
panic(self.panicTypeError(fmt.Sprintf("unsupported type %v for numeric conversion", val.Type())))
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
var typeOfValue = reflect.TypeOf(Value{})
|
||
|
|
||
|
// convertCallParameter converts request val to type t if possible.
|
||
9 years ago
|
// If the conversion fails due to overflow or type miss-match then it panics.
|
||
|
// If no conversion is known then the original value is returned.
|
||
8 years ago
|
func (self *_runtime) convertCallParameter(v Value, t reflect.Type) reflect.Value {
|
||
|
if t == typeOfValue {
|
||
|
return reflect.ValueOf(v)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if v.kind == valueObject {
|
||
|
if gso, ok := v._object().value.(*_goStructObject); ok {
|
||
|
if gso.value.Type().AssignableTo(t) {
|
||
|
return gso.value
|
||
|
}
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
if t.Kind() == reflect.Interface {
|
||
|
iv := reflect.ValueOf(v.export())
|
||
|
if iv.Type().AssignableTo(t) {
|
||
|
return iv
|
||
9 years ago
|
}
|
||
8 years ago
|
}
|
||
|
|
||
|
tk := t.Kind()
|
||
|
|
||
|
if tk == reflect.Ptr {
|
||
|
switch v.kind {
|
||
|
case valueEmpty, valueNull, valueUndefined:
|
||
|
return reflect.Zero(t)
|
||
|
default:
|
||
|
var vv reflect.Value
|
||
|
if err := catchPanic(func() { vv = self.convertCallParameter(v, t.Elem()) }); err == nil {
|
||
|
if vv.CanAddr() {
|
||
|
return vv.Addr()
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
pv := reflect.New(vv.Type())
|
||
|
pv.Elem().Set(vv)
|
||
|
return pv
|
||
|
}
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
switch tk {
|
||
|
case reflect.Bool:
|
||
|
return reflect.ValueOf(v.bool())
|
||
|
case reflect.String:
|
||
|
switch v.kind {
|
||
|
case valueString:
|
||
|
return reflect.ValueOf(v.value)
|
||
|
case valueNumber:
|
||
|
return reflect.ValueOf(fmt.Sprintf("%v", v.value))
|
||
|
}
|
||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
|
||
|
switch v.kind {
|
||
|
case valueNumber:
|
||
|
return self.convertNumeric(v, t)
|
||
|
}
|
||
|
case reflect.Slice:
|
||
|
if o := v._object(); o != nil {
|
||
|
if lv := o.get("length"); lv.IsNumber() {
|
||
|
l := lv.number().int64
|
||
|
|
||
|
s := reflect.MakeSlice(t, int(l), int(l))
|
||
|
|
||
|
tt := t.Elem()
|
||
|
|
||
|
if o.class == "Array" {
|
||
|
for i := int64(0); i < l; i++ {
|
||
|
p, ok := o.property[strconv.FormatInt(i, 10)]
|
||
|
if !ok {
|
||
|
continue
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
e, ok := p.value.(Value)
|
||
|
if !ok {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
ev := self.convertCallParameter(e, tt)
|
||
|
|
||
|
s.Index(int(i)).Set(ev)
|
||
|
}
|
||
|
} else if o.class == "GoArray" {
|
||
|
|
||
|
var gslice bool
|
||
|
switch o.value.(type) {
|
||
|
case *_goSliceObject:
|
||
|
gslice = true
|
||
|
case *_goArrayObject:
|
||
|
gslice = false
|
||
|
}
|
||
|
|
||
|
for i := int64(0); i < l; i++ {
|
||
|
var p *_property
|
||
|
if gslice {
|
||
|
p = goSliceGetOwnProperty(o, strconv.FormatInt(i, 10))
|
||
|
} else {
|
||
|
p = goArrayGetOwnProperty(o, strconv.FormatInt(i, 10))
|
||
|
}
|
||
|
if p == nil {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
e, ok := p.value.(Value)
|
||
|
if !ok {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
ev := self.convertCallParameter(e, tt)
|
||
|
|
||
|
s.Index(int(i)).Set(ev)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return s
|
||
|
}
|
||
9 years ago
|
}
|
||
8 years ago
|
case reflect.Map:
|
||
|
if o := v._object(); o != nil && t.Key().Kind() == reflect.String {
|
||
|
m := reflect.MakeMap(t)
|
||
|
|
||
|
o.enumerate(false, func(k string) bool {
|
||
|
m.SetMapIndex(reflect.ValueOf(k), self.convertCallParameter(o.get(k), t.Elem()))
|
||
|
return true
|
||
|
})
|
||
|
|
||
|
return m
|
||
|
}
|
||
|
case reflect.Func:
|
||
|
if t.NumOut() > 1 {
|
||
|
panic(self.panicTypeError("converting JavaScript values to Go functions with more than one return value is currently not supported"))
|
||
|
}
|
||
|
|
||
|
if o := v._object(); o != nil && o.class == "Function" {
|
||
|
return reflect.MakeFunc(t, func(args []reflect.Value) []reflect.Value {
|
||
|
l := make([]interface{}, len(args))
|
||
|
for i, a := range args {
|
||
|
if a.CanInterface() {
|
||
|
l[i] = a.Interface()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
rv, err := v.Call(nullValue, l...)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
if t.NumOut() == 0 {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
return []reflect.Value{self.convertCallParameter(rv, t.Out(0))}
|
||
|
})
|
||
9 years ago
|
}
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
if tk == reflect.String {
|
||
|
if o := v._object(); o != nil && o.hasProperty("toString") {
|
||
|
if fn := o.get("toString"); fn.IsFunction() {
|
||
|
sv, err := fn.Call(v)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
var r reflect.Value
|
||
|
if err := catchPanic(func() { r = self.convertCallParameter(sv, t) }); err == nil {
|
||
|
return r
|
||
|
}
|
||
9 years ago
|
}
|
||
|
}
|
||
8 years ago
|
|
||
|
return reflect.ValueOf(v.String())
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
s := "OTTO DOES NOT UNDERSTAND THIS TYPE"
|
||
|
switch v.kind {
|
||
|
case valueBoolean:
|
||
|
s = "boolean"
|
||
|
case valueNull:
|
||
|
s = "null"
|
||
|
case valueNumber:
|
||
|
s = "number"
|
||
|
case valueString:
|
||
|
s = "string"
|
||
|
case valueUndefined:
|
||
|
s = "undefined"
|
||
|
case valueObject:
|
||
|
s = v.Class()
|
||
|
}
|
||
|
|
||
|
panic(self.panicTypeError("can't convert from %q to %q", s, t.String()))
|
||
9 years ago
|
}
|
||
|
|
||
10 years ago
|
func (self *_runtime) toValue(value interface{}) Value {
|
||
|
switch value := value.(type) {
|
||
|
case Value:
|
||
|
return value
|
||
|
case func(FunctionCall) Value:
|
||
8 years ago
|
var name, file string
|
||
|
var line int
|
||
|
pc := reflect.ValueOf(value).Pointer()
|
||
|
fn := runtime.FuncForPC(pc)
|
||
|
if fn != nil {
|
||
|
name = fn.Name()
|
||
|
file, line = fn.FileLine(pc)
|
||
|
file = path.Base(file)
|
||
|
}
|
||
|
return toValue_object(self.newNativeFunction(name, file, line, value))
|
||
10 years ago
|
case _nativeFunction:
|
||
8 years ago
|
var name, file string
|
||
|
var line int
|
||
|
pc := reflect.ValueOf(value).Pointer()
|
||
|
fn := runtime.FuncForPC(pc)
|
||
|
if fn != nil {
|
||
|
name = fn.Name()
|
||
|
file, line = fn.FileLine(pc)
|
||
|
file = path.Base(file)
|
||
|
}
|
||
|
return toValue_object(self.newNativeFunction(name, file, line, value))
|
||
10 years ago
|
case Object, *Object, _object, *_object:
|
||
|
// Nothing happens.
|
||
10 years ago
|
// FIXME We should really figure out what can come here.
|
||
|
// This catch-all is ugly.
|
||
10 years ago
|
default:
|
||
|
{
|
||
|
value := reflect.ValueOf(value)
|
||
8 years ago
|
|
||
10 years ago
|
switch value.Kind() {
|
||
|
case reflect.Ptr:
|
||
|
switch reflect.Indirect(value).Kind() {
|
||
|
case reflect.Struct:
|
||
|
return toValue_object(self.newGoStructObject(value))
|
||
|
case reflect.Array:
|
||
|
return toValue_object(self.newGoArray(value))
|
||
|
}
|
||
8 years ago
|
case reflect.Struct:
|
||
|
return toValue_object(self.newGoStructObject(value))
|
||
|
case reflect.Map:
|
||
|
return toValue_object(self.newGoMapObject(value))
|
||
|
case reflect.Slice:
|
||
|
return toValue_object(self.newGoSlice(value))
|
||
|
case reflect.Array:
|
||
|
return toValue_object(self.newGoArray(value))
|
||
10 years ago
|
case reflect.Func:
|
||
8 years ago
|
var name, file string
|
||
|
var line int
|
||
|
if v := reflect.ValueOf(value); v.Kind() == reflect.Ptr {
|
||
|
pc := v.Pointer()
|
||
|
fn := runtime.FuncForPC(pc)
|
||
|
if fn != nil {
|
||
|
name = fn.Name()
|
||
|
file, line = fn.FileLine(pc)
|
||
|
file = path.Base(file)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
typ := value.Type()
|
||
|
|
||
|
return toValue_object(self.newNativeFunction(name, file, line, func(c FunctionCall) Value {
|
||
|
nargs := typ.NumIn()
|
||
|
|
||
|
if len(c.ArgumentList) != nargs {
|
||
|
if typ.IsVariadic() {
|
||
|
if len(c.ArgumentList) < nargs-1 {
|
||
|
panic(self.panicRangeError(fmt.Sprintf("expected at least %d arguments; got %d", nargs-1, len(c.ArgumentList))))
|
||
9 years ago
|
}
|
||
8 years ago
|
} else {
|
||
|
panic(self.panicRangeError(fmt.Sprintf("expected %d argument(s); got %d", nargs, len(c.ArgumentList))))
|
||
9 years ago
|
}
|
||
8 years ago
|
}
|
||
|
|
||
|
in := make([]reflect.Value, len(c.ArgumentList))
|
||
|
|
||
|
callSlice := false
|
||
|
|
||
|
for i, a := range c.ArgumentList {
|
||
|
var t reflect.Type
|
||
9 years ago
|
|
||
8 years ago
|
n := i
|
||
|
if n >= nargs-1 && typ.IsVariadic() {
|
||
|
if n > nargs-1 {
|
||
|
n = nargs - 1
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
|
t = typ.In(n).Elem()
|
||
9 years ago
|
} else {
|
||
8 years ago
|
t = typ.In(n)
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
|
// if this is a variadic Go function, and the caller has supplied
|
||
|
// exactly the number of JavaScript arguments required, and this
|
||
|
// is the last JavaScript argument, try treating the it as the
|
||
|
// actual set of variadic Go arguments. if that succeeds, break
|
||
|
// out of the loop.
|
||
|
if typ.IsVariadic() && len(c.ArgumentList) == nargs && i == nargs-1 {
|
||
|
var v reflect.Value
|
||
|
if err := catchPanic(func() { v = self.convertCallParameter(a, typ.In(n)) }); err == nil {
|
||
|
in[i] = v
|
||
|
callSlice = true
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
|
in[i] = self.convertCallParameter(a, t)
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
var out []reflect.Value
|
||
|
if callSlice {
|
||
|
out = value.CallSlice(in)
|
||
|
} else {
|
||
|
out = value.Call(in)
|
||
|
}
|
||
|
|
||
8 years ago
|
switch len(out) {
|
||
9 years ago
|
case 0:
|
||
10 years ago
|
return Value{}
|
||
9 years ago
|
case 1:
|
||
|
return self.toValue(out[0].Interface())
|
||
8 years ago
|
default:
|
||
|
s := make([]interface{}, len(out))
|
||
|
for i, v := range out {
|
||
|
s[i] = self.toValue(v.Interface())
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
return self.toValue(s)
|
||
9 years ago
|
}
|
||
10 years ago
|
}))
|
||
|
}
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
10 years ago
|
return toValue(value)
|
||
|
}
|
||
|
|
||
|
func (runtime *_runtime) newGoSlice(value reflect.Value) *_object {
|
||
|
self := runtime.newGoSliceObject(value)
|
||
10 years ago
|
self.prototype = runtime.global.ArrayPrototype
|
||
10 years ago
|
return self
|
||
|
}
|
||
|
|
||
|
func (runtime *_runtime) newGoArray(value reflect.Value) *_object {
|
||
|
self := runtime.newGoArrayObject(value)
|
||
10 years ago
|
self.prototype = runtime.global.ArrayPrototype
|
||
10 years ago
|
return self
|
||
|
}
|
||
|
|
||
8 years ago
|
func (runtime *_runtime) parse(filename string, src, sm interface{}) (*ast.Program, error) {
|
||
|
return parser.ParseFileWithSourceMap(nil, filename, src, sm, 0)
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func (runtime *_runtime) cmpl_parse(filename string, src, sm interface{}) (*_nodeProgram, error) {
|
||
|
program, err := parser.ParseFileWithSourceMap(nil, filename, src, sm, 0)
|
||
10 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
8 years ago
|
|
||
10 years ago
|
return cmpl_parse(program), nil
|
||
|
}
|
||
|
|
||
8 years ago
|
func (self *_runtime) parseSource(src, sm interface{}) (*_nodeProgram, *ast.Program, error) {
|
||
10 years ago
|
switch src := src.(type) {
|
||
|
case *ast.Program:
|
||
|
return nil, src, nil
|
||
|
case *Script:
|
||
|
return src.program, nil, nil
|
||
|
}
|
||
8 years ago
|
|
||
|
program, err := self.parse("", src, sm)
|
||
|
|
||
10 years ago
|
return nil, program, err
|
||
|
}
|
||
|
|
||
8 years ago
|
func (self *_runtime) cmpl_runOrEval(src, sm interface{}, eval bool) (Value, error) {
|
||
10 years ago
|
result := Value{}
|
||
8 years ago
|
cmpl_program, program, err := self.parseSource(src, sm)
|
||
10 years ago
|
if err != nil {
|
||
|
return result, err
|
||
|
}
|
||
|
if cmpl_program == nil {
|
||
|
cmpl_program = cmpl_parse(program)
|
||
|
}
|
||
|
err = catchPanic(func() {
|
||
9 years ago
|
result = self.cmpl_evaluate_nodeProgram(cmpl_program, eval)
|
||
10 years ago
|
})
|
||
10 years ago
|
switch result.kind {
|
||
10 years ago
|
case valueEmpty:
|
||
10 years ago
|
result = Value{}
|
||
10 years ago
|
case valueReference:
|
||
10 years ago
|
result = result.resolve()
|
||
10 years ago
|
}
|
||
|
return result, err
|
||
|
}
|
||
|
|
||
8 years ago
|
func (self *_runtime) cmpl_run(src, sm interface{}) (Value, error) {
|
||
|
return self.cmpl_runOrEval(src, sm, false)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func (self *_runtime) cmpl_eval(src, sm interface{}) (Value, error) {
|
||
|
return self.cmpl_runOrEval(src, sm, true)
|
||
9 years ago
|
}
|
||
|
|
||
10 years ago
|
func (self *_runtime) parseThrow(err error) {
|
||
|
if err == nil {
|
||
|
return
|
||
|
}
|
||
|
switch err := err.(type) {
|
||
|
case parser.ErrorList:
|
||
|
{
|
||
|
err := err[0]
|
||
|
if err.Message == "Invalid left-hand side in assignment" {
|
||
10 years ago
|
panic(self.panicReferenceError(err.Message))
|
||
10 years ago
|
}
|
||
10 years ago
|
panic(self.panicSyntaxError(err.Message))
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
panic(self.panicSyntaxError(err.Error()))
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func (self *_runtime) cmpl_parseOrThrow(src, sm interface{}) *_nodeProgram {
|
||
|
program, err := self.cmpl_parse("", src, sm)
|
||
10 years ago
|
self.parseThrow(err) // Will panic/throw appropriately
|
||
|
return program
|
||
|
}
|