forked from mirror/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.
222 lines
5.8 KiB
222 lines
5.8 KiB
10 years ago
|
package otto
|
||
|
|
||
|
import (
|
||
|
"strconv"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
prototypeValueObject = interface{}(nil)
|
||
10 years ago
|
prototypeValueFunction = _nativeFunctionObject{
|
||
|
call: func(_ FunctionCall) Value {
|
||
|
return Value{}
|
||
|
},
|
||
10 years ago
|
}
|
||
|
prototypeValueString = _stringASCII("")
|
||
|
// TODO Make this just false?
|
||
|
prototypeValueBoolean = Value{
|
||
10 years ago
|
kind: valueBoolean,
|
||
|
value: false,
|
||
10 years ago
|
}
|
||
|
prototypeValueNumber = Value{
|
||
10 years ago
|
kind: valueNumber,
|
||
|
value: 0,
|
||
10 years ago
|
}
|
||
|
prototypeValueDate = _dateObject{
|
||
|
epoch: 0,
|
||
|
isNaN: false,
|
||
|
time: time.Unix(0, 0).UTC(),
|
||
|
value: Value{
|
||
10 years ago
|
kind: valueNumber,
|
||
|
value: 0,
|
||
10 years ago
|
},
|
||
|
}
|
||
|
prototypeValueRegExp = _regExpObject{
|
||
|
regularExpression: nil,
|
||
|
global: false,
|
||
|
ignoreCase: false,
|
||
|
multiline: false,
|
||
|
source: "",
|
||
|
flags: "",
|
||
|
}
|
||
|
)
|
||
|
|
||
|
func newContext() *_runtime {
|
||
|
|
||
|
self := &_runtime{}
|
||
|
|
||
10 years ago
|
self.globalStash = self.newObjectStash(nil, nil)
|
||
|
self.globalObject = self.globalStash.object
|
||
10 years ago
|
|
||
|
_newContext(self)
|
||
|
|
||
10 years ago
|
self.eval = self.globalObject.property["eval"].value.(Value).value.(*_object)
|
||
|
self.globalObject.prototype = self.global.ObjectPrototype
|
||
10 years ago
|
|
||
|
return self
|
||
|
}
|
||
|
|
||
|
func (runtime *_runtime) newBaseObject() *_object {
|
||
|
self := newObject(runtime, "")
|
||
|
return self
|
||
|
}
|
||
|
|
||
|
func (runtime *_runtime) newClassObject(class string) *_object {
|
||
|
return newObject(runtime, class)
|
||
|
}
|
||
|
|
||
|
func (runtime *_runtime) newPrimitiveObject(class string, value Value) *_object {
|
||
|
self := runtime.newClassObject(class)
|
||
|
self.value = value
|
||
|
return self
|
||
|
}
|
||
|
|
||
|
func (self *_object) primitiveValue() Value {
|
||
|
switch value := self.value.(type) {
|
||
|
case Value:
|
||
|
return value
|
||
|
case _stringObject:
|
||
|
return toValue_string(value.String())
|
||
|
}
|
||
|
return Value{}
|
||
|
}
|
||
|
|
||
|
func (self *_object) hasPrimitive() bool {
|
||
|
switch self.value.(type) {
|
||
|
case Value, _stringObject:
|
||
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func (runtime *_runtime) newObject() *_object {
|
||
|
self := runtime.newClassObject("Object")
|
||
10 years ago
|
self.prototype = runtime.global.ObjectPrototype
|
||
10 years ago
|
return self
|
||
|
}
|
||
|
|
||
|
func (runtime *_runtime) newArray(length uint32) *_object {
|
||
|
self := runtime.newArrayObject(length)
|
||
10 years ago
|
self.prototype = runtime.global.ArrayPrototype
|
||
10 years ago
|
return self
|
||
|
}
|
||
|
|
||
|
func (runtime *_runtime) newArrayOf(valueArray []Value) *_object {
|
||
|
self := runtime.newArray(uint32(len(valueArray)))
|
||
|
for index, value := range valueArray {
|
||
|
if value.isEmpty() {
|
||
|
continue
|
||
|
}
|
||
|
self.defineProperty(strconv.FormatInt(int64(index), 10), value, 0111, false)
|
||
|
}
|
||
|
return self
|
||
|
}
|
||
|
|
||
|
func (runtime *_runtime) newString(value Value) *_object {
|
||
|
self := runtime.newStringObject(value)
|
||
10 years ago
|
self.prototype = runtime.global.StringPrototype
|
||
10 years ago
|
return self
|
||
|
}
|
||
|
|
||
|
func (runtime *_runtime) newBoolean(value Value) *_object {
|
||
|
self := runtime.newBooleanObject(value)
|
||
10 years ago
|
self.prototype = runtime.global.BooleanPrototype
|
||
10 years ago
|
return self
|
||
|
}
|
||
|
|
||
|
func (runtime *_runtime) newNumber(value Value) *_object {
|
||
|
self := runtime.newNumberObject(value)
|
||
10 years ago
|
self.prototype = runtime.global.NumberPrototype
|
||
10 years ago
|
return self
|
||
|
}
|
||
|
|
||
|
func (runtime *_runtime) newRegExp(patternValue Value, flagsValue Value) *_object {
|
||
|
|
||
|
pattern := ""
|
||
|
flags := ""
|
||
|
if object := patternValue._object(); object != nil && object.class == "RegExp" {
|
||
|
if flagsValue.IsDefined() {
|
||
10 years ago
|
panic(runtime.panicTypeError("Cannot supply flags when constructing one RegExp from another"))
|
||
10 years ago
|
}
|
||
|
regExp := object.regExpValue()
|
||
|
pattern = regExp.source
|
||
|
flags = regExp.flags
|
||
|
} else {
|
||
|
if patternValue.IsDefined() {
|
||
10 years ago
|
pattern = patternValue.string()
|
||
10 years ago
|
}
|
||
|
if flagsValue.IsDefined() {
|
||
10 years ago
|
flags = flagsValue.string()
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
return runtime._newRegExp(pattern, flags)
|
||
|
}
|
||
|
|
||
|
func (runtime *_runtime) _newRegExp(pattern string, flags string) *_object {
|
||
|
self := runtime.newRegExpObject(pattern, flags)
|
||
10 years ago
|
self.prototype = runtime.global.RegExpPrototype
|
||
10 years ago
|
return self
|
||
|
}
|
||
|
|
||
|
// TODO Should (probably) be one argument, right? This is redundant
|
||
|
func (runtime *_runtime) newDate(epoch float64) *_object {
|
||
|
self := runtime.newDateObject(epoch)
|
||
10 years ago
|
self.prototype = runtime.global.DatePrototype
|
||
10 years ago
|
return self
|
||
|
}
|
||
|
|
||
8 years ago
|
func (runtime *_runtime) newError(name string, message Value, stackFramesToPop int) *_object {
|
||
10 years ago
|
var self *_object
|
||
|
switch name {
|
||
|
case "EvalError":
|
||
|
return runtime.newEvalError(message)
|
||
|
case "TypeError":
|
||
|
return runtime.newTypeError(message)
|
||
|
case "RangeError":
|
||
|
return runtime.newRangeError(message)
|
||
|
case "ReferenceError":
|
||
|
return runtime.newReferenceError(message)
|
||
|
case "SyntaxError":
|
||
|
return runtime.newSyntaxError(message)
|
||
|
case "URIError":
|
||
|
return runtime.newURIError(message)
|
||
|
}
|
||
|
|
||
8 years ago
|
self = runtime.newErrorObject(name, message, stackFramesToPop)
|
||
10 years ago
|
self.prototype = runtime.global.ErrorPrototype
|
||
10 years ago
|
if name != "" {
|
||
|
self.defineProperty("name", toValue_string(name), 0111, false)
|
||
|
}
|
||
|
return self
|
||
|
}
|
||
|
|
||
8 years ago
|
func (runtime *_runtime) newNativeFunction(name, file string, line int, _nativeFunction _nativeFunction) *_object {
|
||
|
self := runtime.newNativeFunctionObject(name, file, line, _nativeFunction, 0)
|
||
10 years ago
|
self.prototype = runtime.global.FunctionPrototype
|
||
10 years ago
|
prototype := runtime.newObject()
|
||
|
self.defineProperty("prototype", toValue_object(prototype), 0100, false)
|
||
|
prototype.defineProperty("constructor", toValue_object(self), 0100, false)
|
||
|
return self
|
||
|
}
|
||
|
|
||
10 years ago
|
func (runtime *_runtime) newNodeFunction(node *_nodeFunctionLiteral, scopeEnvironment _stash) *_object {
|
||
10 years ago
|
// TODO Implement 13.2 fully
|
||
|
self := runtime.newNodeFunctionObject(node, scopeEnvironment)
|
||
10 years ago
|
self.prototype = runtime.global.FunctionPrototype
|
||
10 years ago
|
prototype := runtime.newObject()
|
||
|
self.defineProperty("prototype", toValue_object(prototype), 0100, false)
|
||
|
prototype.defineProperty("constructor", toValue_object(self), 0101, false)
|
||
|
return self
|
||
|
}
|
||
10 years ago
|
|
||
|
// FIXME Only in one place...
|
||
|
func (runtime *_runtime) newBoundFunction(target *_object, this Value, argumentList []Value) *_object {
|
||
|
self := runtime.newBoundFunctionObject(target, this, argumentList)
|
||
|
self.prototype = runtime.global.FunctionPrototype
|
||
|
prototype := runtime.newObject()
|
||
|
self.defineProperty("prototype", toValue_object(prototype), 0100, false)
|
||
|
prototype.defineProperty("constructor", toValue_object(self), 0100, false)
|
||
|
return self
|
||
|
}
|