Commit e4a889a0 authored by vipwzw's avatar vipwzw

add js vm

parent a1236aaa
......@@ -2,13 +2,36 @@ package executor
import (
"github.com/33cn/chain33/types"
ptypes "github.com/33cn/plugin/plugin/dapp/js/types"
"github.com/33cn/plugin/plugin/dapp/js/types/jsproto"
)
func (c *js) Exec_Create(payload *jsproto.Create, tx *types.Transaction, index int) (*types.Receipt, error) {
return &types.Receipt{}, nil
db := c.GetStateDB()
_, err := db.Get(calcCodeKey(payload.Name))
if err != nil && err != types.ErrNotFound {
return nil, err
}
if err == nil {
return nil, ptypes.ErrDupName
}
r := &types.Receipt{Ty: types.ExecOk}
//code must be utf-8 encoding
r.KV = append(r.KV, &types.KeyValue{
Key: calcCodeKey(payload.Name), Value: []byte(payload.Code)})
return r, nil
}
func (c *js) Exec_Call(payload *jsproto.Call, tx *types.Transaction, index int) (*types.Receipt, error) {
return &types.Receipt{}, nil
jsvalue, err := c.callVM("exec", payload, tx, index)
if err != nil {
return nil, err
}
r := &types.Receipt{Ty: types.ExecOk}
kvs, err := parseKVS(jsvalue)
if err != nil {
return nil, err
}
r.KV = kvs
return r, nil
}
......@@ -10,5 +10,15 @@ func (c *js) ExecDelLocal_Create(payload *jsproto.Create, tx *types.Transaction,
}
func (c *js) ExecDelLocal_Call(payload *jsproto.Call, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return &types.LocalDBSet{}, nil
jsvalue, err := c.callVM("execdellocal", payload, tx, index)
if err != nil {
return nil, err
}
r := &types.LocalDBSet{}
kvs, err := parseKVS(jsvalue)
if err != nil {
return nil, err
}
r.KV = kvs
return r, nil
}
......@@ -10,5 +10,15 @@ func (c *js) ExecLocal_Create(payload *jsproto.Create, tx *types.Transaction, re
}
func (c *js) ExecLocal_Call(payload *jsproto.Call, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return &types.LocalDBSet{}, nil
jsvalue, err := c.callVM("execlocal", payload, tx, index)
if err != nil {
return nil, err
}
r := &types.LocalDBSet{}
kvs, err := parseKVS(jsvalue)
if err != nil {
return nil, err
}
r.KV = kvs
return r, nil
}
package executor
import (
"encoding/json"
"github.com/33cn/chain33/common"
log "github.com/33cn/chain33/common/log/log15"
drivers "github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
ptypes "github.com/33cn/plugin/plugin/dapp/js/types"
"github.com/33cn/plugin/plugin/dapp/js/types/jsproto"
"github.com/robertkrimen/otto"
)
var (
......@@ -43,3 +48,131 @@ func GetName() string {
func (u *js) GetDriverName() string {
return driverName
}
func (u *js) callVM(prefix string, payload *jsproto.Call, tx *types.Transaction, index int) (*otto.Object, error) {
vm, err := u.createVM(tx, index)
if err != nil {
return nil, err
}
db := u.GetStateDB()
code, err := db.Get(calcCodeKey(payload.Name))
if err != nil {
return nil, err
}
vm.Set("code", code)
vm.Set("f", prefix+"_"+payload.Funcname)
vm.Set("args", payload.Args)
callfunc := "callcode(context, f, args)"
jsvalue, err := vm.Run(callcode + string(code) + "\n" + callfunc)
if err != nil {
return nil, err
}
if !jsvalue.IsObject() {
return nil, ptypes.ErrJsReturnNotObject
}
return jsvalue.Object(), nil
}
func (u *js) getContext(tx *types.Transaction, index int64) *blockContext {
return &blockContext{
Height: u.GetHeight(),
Name: u.GetName(),
Blocktime: u.GetBlockTime(),
Curname: u.GetCurrentExecName(),
DriverName: u.GetDriverName(),
Difficulty: u.GetDifficulty(),
TxHash: common.ToHex(tx.Hash()),
Index: index,
}
}
func (u *js) createVM(tx *types.Transaction, index int) (*otto.Otto, error) {
data, err := json.Marshal(u.getContext(tx, int64(index)))
if err != nil {
return nil, err
}
vm := otto.New()
vm.Set("context", string(data))
vm.Set("getstatedb", func(call otto.FunctionCall) otto.Value {
key, err := call.Argument(0).ToString()
if err != nil {
return errReturn(vm, err)
}
v, err := u.getstatedb(key)
if err != nil {
return errReturn(vm, err)
}
return okReturn(vm, v)
})
vm.Set("getlocaldb", func(call otto.FunctionCall) otto.Value {
key, err := call.Argument(0).ToString()
if err != nil {
return errReturn(vm, err)
}
v, err := u.getlocaldb(key)
if err != nil {
return errReturn(vm, err)
}
return okReturn(vm, v)
})
//List(prefix, key []byte, count, direction int32) ([][]byte, error)
vm.Set("listdb", func(call otto.FunctionCall) otto.Value {
prefix, err := call.Argument(0).ToString()
if err != nil {
return errReturn(vm, err)
}
key, err := call.Argument(1).ToString()
if err != nil {
return errReturn(vm, err)
}
count, err := call.Argument(2).ToInteger()
if err != nil {
return errReturn(vm, err)
}
direction, err := call.Argument(3).ToInteger()
if err != nil {
return errReturn(vm, err)
}
v, err := u.listdb(prefix, key, int32(count), int32(direction))
if err != nil {
return errReturn(vm, err)
}
return listReturn(vm, v)
})
return vm, nil
}
func errReturn(vm *otto.Otto, err error) otto.Value {
v, _ := vm.ToValue(&dbReturn{Err: err.Error()})
return v
}
func okReturn(vm *otto.Otto, value string) otto.Value {
v, _ := vm.ToValue(&dbReturn{Value: value})
return v
}
func listReturn(vm *otto.Otto, value []string) otto.Value {
v, _ := vm.ToValue(&listdbReturn{Value: value})
return v
}
func (u *js) getstatedb(key string) (value string, err error) {
s, err := u.GetStateDB().Get([]byte(key))
value = string(s)
return value, err
}
func (u *js) getlocaldb(key string) (value string, err error) {
s, err := u.GetLocalDB().Get([]byte(key))
value = string(s)
return value, err
}
func (u *js) listdb(prefix, key string, count, direction int32) (value []string, err error) {
values, err := u.GetLocalDB().List([]byte(prefix), []byte(key), count, direction)
for _, v := range values {
value = append(value, string(v))
}
return value, err
}
package executor
import (
"errors"
"fmt"
"github.com/33cn/chain33/types"
ptypes "github.com/33cn/plugin/plugin/dapp/js/types"
"github.com/robertkrimen/otto"
)
//ErrInvalidFuncFormat 错误的函数调用格式(没有_)
var errInvalidFuncFormat = errors.New("chain33.js: invalid function name format")
//ErrInvalidFuncPrefix not exec_ execloal execdellocal
var errInvalidFuncPrefix = errors.New("chain33.js: invalid function prefix format")
//ErrFuncNotFound 函数没有找到
var errFuncNotFound = errors.New("chain33.js: invalid function name not found")
var callcode = `
function callcode(context, f, args) {
var farr = f.split("_", 2)
if (farr.length != 2) {
throw new Error("chain33.js: invalid function name format")
}
var prefix = farr[0]
var funcname = farr[1]
var runobj = {}
if (prefix == "exec") {
runobj = new Exec(JSON.parse(context))
} else if (prefix == "execlocal") {
runobj = new ExecLocal(JSON.parse(context))
} else if (prefix == "execdellocal") {
runobj = new ExecDelLocal(JSON.parse(context))
} else {
throw new Error("chain33.js: invalid function prefix format")
}
var arg = JSON.parse(args)
if (typeof runobj[funcname] != "function") {
throw new Error("chain33.js: invalid function name not found")
}
return runobj[funcname](arg)
}
`
type blockContext struct {
Height int64 `json:"height"`
Blocktime int64 `json:"blocktime"`
DriverName string `json:"driverName"`
Name string `json:"name"`
Curname string `json:"curname"`
Difficulty uint64 `json:"difficulty"`
TxHash string `json:"txhash"`
Index int64 `json:"index"`
}
type dbReturn struct {
Value string `json:"value"`
Err string `json:"err"`
}
type listdbReturn struct {
Value []string `json:"value"`
Err string `json:"err"`
}
func parseKVS(jsvalue *otto.Object) (kvlist []*types.KeyValue, err error) {
obj, err := getObject(jsvalue, "kvs")
if err != nil {
return nil, ptypes.ErrJsReturnKVSFormat
}
if obj.Class() != "Array" {
return nil, ptypes.ErrJsReturnKVSFormat
}
size, err := getInt(obj, "length")
if err != nil {
return nil, err
}
for i := 0; i < int(size); i++ {
data, err := getObject(obj, fmt.Sprint(i))
if err != nil {
return nil, err
}
kv, err := parseKV(data)
if err != nil {
return nil, err
}
kvlist = append(kvlist, kv)
}
return kvlist, nil
}
func getString(data *otto.Object, key string) (string, error) {
v, err := data.Get(key)
if err != nil {
return "", err
}
return v.ToString()
}
func getInt(data *otto.Object, key string) (int64, error) {
v, err := data.Get(key)
if err != nil {
return 0, err
}
return v.ToInteger()
}
func getObject(data *otto.Object, key string) (*otto.Object, error) {
v, err := data.Get(key)
if err != nil {
return nil, err
}
if !v.IsObject() {
return nil, errors.New("chain33.js object get key " + key + " is not object")
}
return v.Object(), nil
}
func parseKV(data *otto.Object) (kv *types.KeyValue, err error) {
key, err := getString(data, "key")
if err != nil {
return nil, err
}
value, err := getString(data, "value")
if err != nil {
return nil, err
}
return &types.KeyValue{Key: []byte(key), Value: []byte(value)}, nil
}
package executor
import (
"encoding/json"
"strings"
"testing"
"github.com/robertkrimen/otto"
"github.com/stretchr/testify/assert"
)
var jscode = `
function Exec(context) {
this.context = context
}
function ExecLocal(context) {
this.context = context
}
function ExecDelLocal(context) {
this.context = context
}
Exec.prototype.hello = function(args) {
return {args: args, action:"exec", context: this.context}
}
ExecLocal.prototype.hello = function(args) {
return {args: args, action:"execlocal", context: this.context}
}
ExecDelLocal.prototype.hello = function(args) {
return {args: args, action:"execdellocal", context: this.context}
}
`
func callJsFunc(context *blockContext, code string, f string, args string) (otto.Value, error) {
data, err := json.Marshal(context)
if err != nil {
return otto.Value{}, err
}
vm := otto.New()
vm.Set("context", string(data))
vm.Set("code", code)
vm.Set("f", f)
vm.Set("args", args)
callfunc := "callcode(context, f, args)"
return vm.Run(callcode + code + "\n" + callfunc)
}
func TestCallcode(t *testing.T) {
value, err := callJsFunc(&blockContext{Height: 1}, jscode, "exec_hello", `{"hello2":"world2"}`)
assert.Nil(t, err)
assert.Equal(t, true, value.IsObject())
action, err := value.Object().Get("action")
assert.Nil(t, err)
assert.Equal(t, "exec", action.String())
args, err := value.Object().Get("args")
assert.Nil(t, err)
arg, err := args.Object().Get("hello2")
assert.Nil(t, err)
assert.Equal(t, "world2", arg.String())
context, err := value.Object().Get("context")
assert.Nil(t, err)
cvalue, err := context.Object().Get("height")
assert.Nil(t, err)
assert.Equal(t, "1", cvalue.String())
//test call error(invalid json input)
_, err = callJsFunc(&blockContext{Height: 1}, jscode, "exec_hello", `{hello2":"world2"}`)
_, ok := err.(*otto.Error)
assert.Equal(t, true, ok)
assert.Equal(t, true, strings.Contains(err.Error(), "SyntaxError"))
_, err = callJsFunc(&blockContext{Height: 1}, jscode, "hello", `{"hello2":"world2"}`)
assert.Equal(t, true, strings.Contains(err.Error(), errInvalidFuncFormat.Error()))
_, err = callJsFunc(&blockContext{Height: 1}, jscode, "hello_hello", `{"hello2":"world2"}`)
assert.Equal(t, true, strings.Contains(err.Error(), errInvalidFuncPrefix.Error()))
_, err = callJsFunc(&blockContext{Height: 1}, jscode, "exec_hello2", `{"hello2":"world2"}`)
assert.Equal(t, true, strings.Contains(err.Error(), errFuncNotFound.Error()))
}
package executor
func calcCodeKey(name string) []byte {
return append([]byte("mavl-js-code-"), []byte(name)...)
}
......@@ -9,8 +9,9 @@ message Create {
// call action
message Call {
string funcname = 1; //call function name
string args = 2; //json args
string name = 1; //exec name
string funcname = 2; //call function name
string args = 3; //json args
}
message JsAction {
......
package types
import (
"errors"
"github.com/33cn/chain33/types"
"github.com/33cn/plugin/plugin/dapp/js/types/jsproto"
)
......@@ -28,6 +30,13 @@ var (
//JsX 插件名字
var JsX = "js"
//错误常量
var (
ErrDupName = errors.New("ErrDupName")
ErrJsReturnNotObject = errors.New("ErrJsReturnNotObject")
ErrJsReturnKVSFormat = errors.New("ErrJsReturnKVSFormat")
)
func init() {
types.AllowUserExec = append(types.AllowUserExec, []byte(JsX))
types.RegistorExecutor(JsX, NewType())
......
......@@ -31,7 +31,7 @@ func (m *Create) Reset() { *m = Create{} }
func (m *Create) String() string { return proto.CompactTextString(m) }
func (*Create) ProtoMessage() {}
func (*Create) Descriptor() ([]byte, []int) {
return fileDescriptor_js_5c2b7f71b678e48e, []int{0}
return fileDescriptor_js_26fa46fa936181bc, []int{0}
}
func (m *Create) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Create.Unmarshal(m, b)
......@@ -67,8 +67,9 @@ func (m *Create) GetName() string {
// call action
type Call struct {
Funcname string `protobuf:"bytes,1,opt,name=funcname,proto3" json:"funcname,omitempty"`
Args string `protobuf:"bytes,2,opt,name=args,proto3" json:"args,omitempty"`
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
Funcname string `protobuf:"bytes,2,opt,name=funcname,proto3" json:"funcname,omitempty"`
Args string `protobuf:"bytes,3,opt,name=args,proto3" json:"args,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
......@@ -78,7 +79,7 @@ func (m *Call) Reset() { *m = Call{} }
func (m *Call) String() string { return proto.CompactTextString(m) }
func (*Call) ProtoMessage() {}
func (*Call) Descriptor() ([]byte, []int) {
return fileDescriptor_js_5c2b7f71b678e48e, []int{1}
return fileDescriptor_js_26fa46fa936181bc, []int{1}
}
func (m *Call) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Call.Unmarshal(m, b)
......@@ -98,6 +99,13 @@ func (m *Call) XXX_DiscardUnknown() {
var xxx_messageInfo_Call proto.InternalMessageInfo
func (m *Call) GetName() string {
if m != nil {
return m.Name
}
return ""
}
func (m *Call) GetFuncname() string {
if m != nil {
return m.Funcname
......@@ -127,7 +135,7 @@ func (m *JsAction) Reset() { *m = JsAction{} }
func (m *JsAction) String() string { return proto.CompactTextString(m) }
func (*JsAction) ProtoMessage() {}
func (*JsAction) Descriptor() ([]byte, []int) {
return fileDescriptor_js_5c2b7f71b678e48e, []int{2}
return fileDescriptor_js_26fa46fa936181bc, []int{2}
}
func (m *JsAction) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_JsAction.Unmarshal(m, b)
......@@ -266,26 +274,26 @@ func _JsAction_OneofSizer(msg proto.Message) (n int) {
}
func init() {
proto.RegisterType((*Create)(nil), "js.Create")
proto.RegisterType((*Call)(nil), "js.Call")
proto.RegisterType((*JsAction)(nil), "js.JsAction")
}
func init() { proto.RegisterFile("js.proto", fileDescriptor_js_5c2b7f71b678e48e) }
var fileDescriptor_js_5c2b7f71b678e48e = []byte{
// 195 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x3c, 0x8f, 0x3f, 0xaf, 0x82, 0x30,
0x14, 0xc5, 0xa1, 0x0f, 0x78, 0x7d, 0x97, 0xe4, 0x0d, 0x9d, 0x88, 0x83, 0x21, 0xc4, 0x81, 0x89,
0x18, 0x4c, 0xdc, 0x95, 0x85, 0x38, 0xf6, 0x1b, 0xd4, 0x5a, 0x0d, 0xa4, 0x52, 0x43, 0x8b, 0x09,
0xdf, 0xde, 0xf4, 0xa2, 0x6e, 0xe7, 0x9e, 0x73, 0x7f, 0xf7, 0x0f, 0xd0, 0xde, 0x56, 0x8f, 0xd1,
0x38, 0xc3, 0x48, 0x6f, 0x8b, 0x2d, 0x24, 0xcd, 0xa8, 0x84, 0x53, 0x8c, 0x41, 0x24, 0xcd, 0x45,
0x65, 0x61, 0x1e, 0x96, 0x7f, 0x1c, 0xb5, 0xf7, 0x06, 0x71, 0x57, 0x19, 0x59, 0x3c, 0xaf, 0x8b,
0x3d, 0x44, 0x8d, 0xd0, 0x9a, 0xad, 0x80, 0x5e, 0xa7, 0x41, 0x62, 0xbe, 0x30, 0xdf, 0xda, 0x73,
0x62, 0xbc, 0xd9, 0x0f, 0xe7, 0x75, 0xd1, 0x01, 0x3d, 0xd9, 0x83, 0x74, 0x9d, 0x19, 0xd8, 0x06,
0x12, 0x89, 0x5b, 0x91, 0x4c, 0x6b, 0xa8, 0x7a, 0x5b, 0x2d, 0x77, 0xb4, 0x01, 0x7f, 0x67, 0x6c,
0x0d, 0x91, 0x14, 0x5a, 0xe3, 0x94, 0xb4, 0xa6, 0xd8, 0x23, 0xb4, 0x6e, 0x03, 0x8e, 0x3e, 0xfb,
0x07, 0xe2, 0xe6, 0xec, 0x27, 0x0f, 0xcb, 0x98, 0x13, 0x37, 0x1f, 0x7f, 0x21, 0x7e, 0x0a, 0x3d,
0xa9, 0x73, 0x82, 0xff, 0xed, 0x5e, 0x01, 0x00, 0x00, 0xff, 0xff, 0x15, 0xae, 0x42, 0xe3, 0xeb,
0x00, 0x00, 0x00,
proto.RegisterType((*Create)(nil), "jsproto.Create")
proto.RegisterType((*Call)(nil), "jsproto.Call")
proto.RegisterType((*JsAction)(nil), "jsproto.JsAction")
}
func init() { proto.RegisterFile("js.proto", fileDescriptor_js_26fa46fa936181bc) }
var fileDescriptor_js_26fa46fa936181bc = []byte{
// 199 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x4c, 0x8e, 0xcd, 0xae, 0x82, 0x30,
0x10, 0x85, 0x81, 0xcb, 0xdf, 0x1d, 0x72, 0xaf, 0x49, 0x57, 0xc4, 0x95, 0xc1, 0x8d, 0x6e, 0x88,
0xc1, 0x27, 0x50, 0x36, 0x84, 0x65, 0xdf, 0xa0, 0xd6, 0x6a, 0x24, 0x95, 0x1a, 0x5a, 0x4c, 0x78,
0x7b, 0xd3, 0x81, 0x80, 0xbb, 0xaf, 0x73, 0xbe, 0xce, 0x1c, 0x88, 0x1b, 0x9d, 0xbf, 0x3a, 0x65,
0x14, 0x89, 0x1a, 0x8d, 0x90, 0x1d, 0x20, 0x2c, 0x3b, 0xc1, 0x8c, 0x20, 0x04, 0x7c, 0xae, 0xae,
0x22, 0x75, 0x37, 0xee, 0xee, 0x97, 0x22, 0xdb, 0x59, 0xcb, 0x9e, 0x22, 0xf5, 0xc6, 0x99, 0xe5,
0xac, 0x06, 0xbf, 0x64, 0x52, 0xce, 0x99, 0xbb, 0x64, 0x64, 0x0d, 0xf1, 0xad, 0x6f, 0xf9, 0xd7,
0x9f, 0xf9, 0x6d, 0x7d, 0xd6, 0xdd, 0x75, 0xfa, 0x33, 0xfa, 0x96, 0x33, 0x0d, 0x71, 0xad, 0x4f,
0xdc, 0x3c, 0x54, 0x4b, 0xf6, 0x10, 0x72, 0x6c, 0x82, 0x1b, 0x93, 0x62, 0x95, 0x4f, 0x1d, 0xf3,
0xb1, 0x60, 0xe5, 0xd0, 0x49, 0x20, 0x5b, 0xf0, 0x39, 0x93, 0x12, 0x4f, 0x24, 0xc5, 0xdf, 0x22,
0x32, 0x29, 0x2b, 0x87, 0x62, 0x48, 0xfe, 0xc1, 0x33, 0x03, 0x5e, 0x0b, 0xa8, 0x67, 0x86, 0x73,
0x04, 0xc1, 0x9b, 0xc9, 0x5e, 0x5c, 0x42, 0x94, 0x8f, 0x9f, 0x00, 0x00, 0x00, 0xff, 0xff, 0x35,
0x5c, 0xc0, 0x76, 0x0e, 0x01, 0x00, 0x00,
}
/.test
/otto/otto
/otto/otto-*
/test/test-*.js
/test/tester
* Designate the filename of "anonymous" source code by the hash (md5/sha1, etc.)
Copyright (c) 2012 Robert Krimen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
.PHONY: test test-race test-release release release-check test-262
.PHONY: parser
.PHONY: otto assets underscore
TESTS := \
~
TEST := -v --run
TEST := -v
TEST := -v --run Test\($(subst $(eval) ,\|,$(TESTS))\)
TEST := .
test: parser inline.go
go test -i
go test $(TEST)
@echo PASS
parser:
$(MAKE) -C parser
inline.go: inline.pl
./$< > $@
#################
# release, test #
#################
release: test-race test-release
for package in . parser token ast file underscore registry; do (cd $$package && godocdown --signature > README.markdown); done
@echo \*\*\* make release-check
@echo PASS
release-check: .test
$(MAKE) -C test build test
$(MAKE) -C .test/test262 build test
@echo PASS
test-262: .test
$(MAKE) -C .test/test262 build test
@echo PASS
test-release:
go test -i
go test
test-race:
go test -race -i
go test -race
#################################
# otto, assets, underscore, ... #
#################################
otto:
$(MAKE) -C otto
assets:
mkdir -p .assets
for file in underscore/test/*.js; do tr "\`" "_" < $$file > .assets/`basename $$file`; done
underscore:
$(MAKE) -C $@
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
package ast
import (
"fmt"
"github.com/robertkrimen/otto/file"
)
// CommentPosition determines where the comment is in a given context
type CommentPosition int
const (
_ CommentPosition = iota
LEADING // Before the pertinent expression
TRAILING // After the pertinent expression
KEY // Before a key in an object
COLON // After a colon in a field declaration
FINAL // Final comments in a block, not belonging to a specific expression or the comment after a trailing , in an array or object literal
IF // After an if keyword
WHILE // After a while keyword
DO // After do keyword
FOR // After a for keyword
WITH // After a with keyword
TBD
)
// Comment contains the data of the comment
type Comment struct {
Begin file.Idx
Text string
Position CommentPosition
}
// NewComment creates a new comment
func NewComment(text string, idx file.Idx) *Comment {
comment := &Comment{
Begin: idx,
Text: text,
Position: TBD,
}
return comment
}
// String returns a stringified version of the position
func (cp CommentPosition) String() string {
switch cp {
case LEADING:
return "Leading"
case TRAILING:
return "Trailing"
case KEY:
return "Key"
case COLON:
return "Colon"
case FINAL:
return "Final"
case IF:
return "If"
case WHILE:
return "While"
case DO:
return "Do"
case FOR:
return "For"
case WITH:
return "With"
default:
return "???"
}
}
// String returns a stringified version of the comment
func (c Comment) String() string {
return fmt.Sprintf("Comment: %v", c.Text)
}
// Comments defines the current view of comments from the parser
type Comments struct {
// CommentMap is a reference to the parser comment map
CommentMap CommentMap
// Comments lists the comments scanned, not linked to a node yet
Comments []*Comment
// future lists the comments after a line break during a sequence of comments
future []*Comment
// Current is node for which comments are linked to
Current Expression
// wasLineBreak determines if a line break occured while scanning for comments
wasLineBreak bool
// primary determines whether or not processing a primary expression
primary bool
// afterBlock determines whether or not being after a block statement
afterBlock bool
}
func NewComments() *Comments {
comments := &Comments{
CommentMap: CommentMap{},
}
return comments
}
func (c *Comments) String() string {
return fmt.Sprintf("NODE: %v, Comments: %v, Future: %v(LINEBREAK:%v)", c.Current, len(c.Comments), len(c.future), c.wasLineBreak)
}
// FetchAll returns all the currently scanned comments,
// including those from the next line
func (c *Comments) FetchAll() []*Comment {
defer func() {
c.Comments = nil
c.future = nil
}()
return append(c.Comments, c.future...)
}
// Fetch returns all the currently scanned comments
func (c *Comments) Fetch() []*Comment {
defer func() {
c.Comments = nil
}()
return c.Comments
}
// ResetLineBreak marks the beginning of a new statement
func (c *Comments) ResetLineBreak() {
c.wasLineBreak = false
}
// MarkPrimary will mark the context as processing a primary expression
func (c *Comments) MarkPrimary() {
c.primary = true
c.wasLineBreak = false
}
// AfterBlock will mark the context as being after a block.
func (c *Comments) AfterBlock() {
c.afterBlock = true
}
// AddComment adds a comment to the view.
// Depending on the context, comments are added normally or as post line break.
func (c *Comments) AddComment(comment *Comment) {
if c.primary {
if !c.wasLineBreak {
c.Comments = append(c.Comments, comment)
} else {
c.future = append(c.future, comment)
}
} else {
if !c.wasLineBreak || (c.Current == nil && !c.afterBlock) {
c.Comments = append(c.Comments, comment)
} else {
c.future = append(c.future, comment)
}
}
}
// MarkComments will mark the found comments as the given position.
func (c *Comments) MarkComments(position CommentPosition) {
for _, comment := range c.Comments {
if comment.Position == TBD {
comment.Position = position
}
}
for _, c := range c.future {
if c.Position == TBD {
c.Position = position
}
}
}
// Unset the current node and apply the comments to the current expression.
// Resets context variables.
func (c *Comments) Unset() {
if c.Current != nil {
c.applyComments(c.Current, c.Current, TRAILING)
c.Current = nil
}
c.wasLineBreak = false
c.primary = false
c.afterBlock = false
}
// SetExpression sets the current expression.
// It is applied the found comments, unless the previous expression has not been unset.
// It is skipped if the node is already set or if it is a part of the previous node.
func (c *Comments) SetExpression(node Expression) {
// Skipping same node
if c.Current == node {
return
}
if c.Current != nil && c.Current.Idx1() == node.Idx1() {
c.Current = node
return
}
previous := c.Current
c.Current = node
// Apply the found comments and futures to the node and the previous.
c.applyComments(node, previous, TRAILING)
}
// PostProcessNode applies all found comments to the given node
func (c *Comments) PostProcessNode(node Node) {
c.applyComments(node, nil, TRAILING)
}
// applyComments applies both the comments and the future comments to the given node and the previous one,
// based on the context.
func (c *Comments) applyComments(node, previous Node, position CommentPosition) {
if previous != nil {
c.CommentMap.AddComments(previous, c.Comments, position)
c.Comments = nil
} else {
c.CommentMap.AddComments(node, c.Comments, position)
c.Comments = nil
}
// Only apply the future comments to the node if the previous is set.
// This is for detecting end of line comments and which node comments on the following lines belongs to
if previous != nil {
c.CommentMap.AddComments(node, c.future, position)
c.future = nil
}
}
// AtLineBreak will mark a line break
func (c *Comments) AtLineBreak() {
c.wasLineBreak = true
}
// CommentMap is the data structure where all found comments are stored
type CommentMap map[Node][]*Comment
// AddComment adds a single comment to the map
func (cm CommentMap) AddComment(node Node, comment *Comment) {
list := cm[node]
list = append(list, comment)
cm[node] = list
}
// AddComments adds a slice of comments, given a node and an updated position
func (cm CommentMap) AddComments(node Node, comments []*Comment, position CommentPosition) {
for _, comment := range comments {
if comment.Position == TBD {
comment.Position = position
}
cm.AddComment(node, comment)
}
}
// Size returns the size of the map
func (cm CommentMap) Size() int {
size := 0
for _, comments := range cm {
size += len(comments)
}
return size
}
// MoveComments moves comments with a given position from a node to another
func (cm CommentMap) MoveComments(from, to Node, position CommentPosition) {
for i, c := range cm[from] {
if c.Position == position {
cm.AddComment(to, c)
// Remove the comment from the "from" slice
cm[from][i] = cm[from][len(cm[from])-1]
cm[from][len(cm[from])-1] = nil
cm[from] = cm[from][:len(cm[from])-1]
}
}
}
package ast
import (
"github.com/robertkrimen/otto/file"
"testing"
)
func TestCommentMap(t *testing.T) {
statement := &EmptyStatement{file.Idx(1)}
comment := &Comment{1, "test", LEADING}
cm := CommentMap{}
cm.AddComment(statement, comment)
if cm.Size() != 1 {
t.Errorf("the number of comments is %v, not 1", cm.Size())
}
if len(cm[statement]) != 1 {
t.Errorf("the number of comments is %v, not 1", cm.Size())
}
if cm[statement][0].Text != "test" {
t.Errorf("the text is %v, not \"test\"", cm[statement][0].Text)
}
}
func TestCommentMap_move(t *testing.T) {
statement1 := &EmptyStatement{file.Idx(1)}
statement2 := &EmptyStatement{file.Idx(2)}
comment := &Comment{1, "test", LEADING}
cm := CommentMap{}
cm.AddComment(statement1, comment)
if cm.Size() != 1 {
t.Errorf("the number of comments is %v, not 1", cm.Size())
}
if len(cm[statement1]) != 1 {
t.Errorf("the number of comments is %v, not 1", cm.Size())
}
if len(cm[statement2]) != 0 {
t.Errorf("the number of comments is %v, not 0", cm.Size())
}
cm.MoveComments(statement1, statement2, LEADING)
if cm.Size() != 1 {
t.Errorf("the number of comments is %v, not 1", cm.Size())
}
if len(cm[statement2]) != 1 {
t.Errorf("the number of comments is %v, not 1", cm.Size())
}
if len(cm[statement1]) != 0 {
t.Errorf("the number of comments is %v, not 0", cm.Size())
}
}
This diff is collapsed.
package ast
import "fmt"
// Visitor Enter method is invoked for each node encountered by Walk.
// If the result visitor w is not nil, Walk visits each of the children
// of node with the visitor v, followed by a call of the Exit method.
type Visitor interface {
Enter(n Node) (v Visitor)
Exit(n Node)
}
// Walk traverses an AST in depth-first order: It starts by calling
// v.Enter(node); node must not be nil. If the visitor v returned by
// v.Enter(node) is not nil, Walk is invoked recursively with visitor
// v for each of the non-nil children of node, followed by a call
// of v.Exit(node).
func Walk(v Visitor, n Node) {
if n == nil {
return
}
if v = v.Enter(n); v == nil {
return
}
defer v.Exit(n)
switch n := n.(type) {
case *ArrayLiteral:
if n != nil {
for _, ex := range n.Value {
Walk(v, ex)
}
}
case *AssignExpression:
if n != nil {
Walk(v, n.Left)
Walk(v, n.Right)
}
case *BadExpression:
case *BinaryExpression:
if n != nil {
Walk(v, n.Left)
Walk(v, n.Right)
}
case *BlockStatement:
if n != nil {
for _, s := range n.List {
Walk(v, s)
}
}
case *BooleanLiteral:
case *BracketExpression:
if n != nil {
Walk(v, n.Left)
Walk(v, n.Member)
}
case *BranchStatement:
if n != nil {
Walk(v, n.Label)
}
case *CallExpression:
if n != nil {
Walk(v, n.Callee)
for _, a := range n.ArgumentList {
Walk(v, a)
}
}
case *CaseStatement:
if n != nil {
Walk(v, n.Test)
for _, c := range n.Consequent {
Walk(v, c)
}
}
case *CatchStatement:
if n != nil {
Walk(v, n.Parameter)
Walk(v, n.Body)
}
case *ConditionalExpression:
if n != nil {
Walk(v, n.Test)
Walk(v, n.Consequent)
Walk(v, n.Alternate)
}
case *DebuggerStatement:
case *DoWhileStatement:
if n != nil {
Walk(v, n.Test)
Walk(v, n.Body)
}
case *DotExpression:
if n != nil {
Walk(v, n.Left)
}
case *EmptyExpression:
case *EmptyStatement:
case *ExpressionStatement:
if n != nil {
Walk(v, n.Expression)
}
case *ForInStatement:
if n != nil {
Walk(v, n.Into)
Walk(v, n.Source)
Walk(v, n.Body)
}
case *ForStatement:
if n != nil {
Walk(v, n.Initializer)
Walk(v, n.Update)
Walk(v, n.Test)
Walk(v, n.Body)
}
case *FunctionLiteral:
if n != nil {
Walk(v, n.Name)
for _, p := range n.ParameterList.List {
Walk(v, p)
}
Walk(v, n.Body)
}
case *FunctionStatement:
if n != nil {
Walk(v, n.Function)
}
case *Identifier:
case *IfStatement:
if n != nil {
Walk(v, n.Test)
Walk(v, n.Consequent)
Walk(v, n.Alternate)
}
case *LabelledStatement:
if n != nil {
Walk(v, n.Statement)
}
case *NewExpression:
if n != nil {
Walk(v, n.Callee)
for _, a := range n.ArgumentList {
Walk(v, a)
}
}
case *NullLiteral:
case *NumberLiteral:
case *ObjectLiteral:
if n != nil {
for _, p := range n.Value {
Walk(v, p.Value)
}
}
case *Program:
if n != nil {
for _, b := range n.Body {
Walk(v, b)
}
}
case *RegExpLiteral:
case *ReturnStatement:
if n != nil {
Walk(v, n.Argument)
}
case *SequenceExpression:
if n != nil {
for _, e := range n.Sequence {
Walk(v, e)
}
}
case *StringLiteral:
case *SwitchStatement:
if n != nil {
Walk(v, n.Discriminant)
for _, c := range n.Body {
Walk(v, c)
}
}
case *ThisExpression:
case *ThrowStatement:
if n != nil {
Walk(v, n.Argument)
}
case *TryStatement:
if n != nil {
Walk(v, n.Body)
Walk(v, n.Catch)
Walk(v, n.Finally)
}
case *UnaryExpression:
if n != nil {
Walk(v, n.Operand)
}
case *VariableExpression:
if n != nil {
Walk(v, n.Initializer)
}
case *VariableStatement:
if n != nil {
for _, e := range n.List {
Walk(v, e)
}
}
case *WhileStatement:
if n != nil {
Walk(v, n.Test)
Walk(v, n.Body)
}
case *WithStatement:
if n != nil {
Walk(v, n.Object)
Walk(v, n.Body)
}
default:
panic(fmt.Sprintf("Walk: unexpected node type %T", n))
}
}
package ast_test
import (
"fmt"
"log"
"github.com/robertkrimen/otto/ast"
"github.com/robertkrimen/otto/file"
"github.com/robertkrimen/otto/parser"
)
type walkExample struct {
source string
shift file.Idx
}
func (w *walkExample) Enter(n ast.Node) ast.Visitor {
if id, ok := n.(*ast.Identifier); ok && id != nil {
idx := n.Idx0() + w.shift - 1
s := w.source[:idx] + "new_" + w.source[idx:]
w.source = s
w.shift += 4
}
if v, ok := n.(*ast.VariableExpression); ok && v != nil {
idx := n.Idx0() + w.shift - 1
s := w.source[:idx] + "varnew_" + w.source[idx:]
w.source = s
w.shift += 7
}
return w
}
func (w *walkExample) Exit(n ast.Node) {
// AST node n has had all its children walked. Pop it out of your
// stack, or do whatever processing you need to do, if any.
}
func ExampleVisitor_codeRewrite() {
source := `var b = function() {test(); try {} catch(e) {} var test = "test(); var test = 1"} // test`
program, err := parser.ParseFile(nil, "", source, 0)
if err != nil {
log.Fatal(err)
}
w := &walkExample{source: source}
ast.Walk(w, program)
fmt.Println(w.source)
// Output: var varnew_b = function() {new_test(); try {} catch(new_e) {} var varnew_test = "test(); var test = 1"} // test
}
package ast_test
import (
"log"
"testing"
"github.com/robertkrimen/otto/ast"
"github.com/robertkrimen/otto/file"
"github.com/robertkrimen/otto/parser"
)
type walker struct {
stack []ast.Node
source string
shift file.Idx
}
// push and pop below are to prove the symmetry of Enter/Exit calls
func (w *walker) push(n ast.Node) {
w.stack = append(w.stack, n)
}
func (w *walker) pop(n ast.Node) {
size := len(w.stack)
if size <= 0 {
panic("pop of empty stack")
}
toPop := w.stack[size-1]
if toPop != n {
panic("pop: nodes do not equal")
}
w.stack[size-1] = nil
w.stack = w.stack[:size-1]
}
func (w *walker) Enter(n ast.Node) ast.Visitor {
w.push(n)
if id, ok := n.(*ast.Identifier); ok && id != nil {
idx := n.Idx0() + w.shift - 1
s := w.source[:idx] + "new_" + w.source[idx:]
w.source = s
w.shift += 4
}
if v, ok := n.(*ast.VariableExpression); ok && v != nil {
idx := n.Idx0() + w.shift - 1
s := w.source[:idx] + "varnew_" + w.source[idx:]
w.source = s
w.shift += 7
}
return w
}
func (w *walker) Exit(n ast.Node) {
w.pop(n)
}
func TestVisitorRewrite(t *testing.T) {
source := `var b = function() {test(); try {} catch(e) {} var test = "test(); var test = 1"} // test`
program, err := parser.ParseFile(nil, "", source, 0)
if err != nil {
log.Fatal(err)
}
w := &walker{source: source}
ast.Walk(w, program)
xformed := `var varnew_b = function() {new_test(); try {} catch(new_e) {} var varnew_test = "test(); var test = 1"} // test`
if w.source != xformed {
t.Errorf("source is `%s` not `%s`", w.source, xformed)
}
if len(w.stack) != 0 {
t.Errorf("stack should be empty, but is length: %d", len(w.stack))
}
}
This diff is collapsed.
package otto
import (
"encoding/hex"
"math"
"net/url"
"regexp"
"strconv"
"strings"
"unicode/utf16"
"unicode/utf8"
)
// Global
func builtinGlobal_eval(call FunctionCall) Value {
src := call.Argument(0)
if !src.IsString() {
return src
}
runtime := call.runtime
program := runtime.cmpl_parseOrThrow(src.string(), nil)
if !call.eval {
// Not a direct call to eval, so we enter the global ExecutionContext
runtime.enterGlobalScope()
defer runtime.leaveScope()
}
returnValue := runtime.cmpl_evaluate_nodeProgram(program, true)
if returnValue.isEmpty() {
return Value{}
}
return returnValue
}
func builtinGlobal_isNaN(call FunctionCall) Value {
value := call.Argument(0).float64()
return toValue_bool(math.IsNaN(value))
}
func builtinGlobal_isFinite(call FunctionCall) Value {
value := call.Argument(0).float64()
return toValue_bool(!math.IsNaN(value) && !math.IsInf(value, 0))
}
// radix 3 => 2 (ASCII 50) +47
// radix 11 => A/a (ASCII 65/97) +54/+86
var parseInt_alphabetTable = func() []string {
table := []string{"", "", "01"}
for radix := 3; radix <= 36; radix += 1 {
alphabet := table[radix-1]
if radix <= 10 {
alphabet += string(radix + 47)
} else {
alphabet += string(radix+54) + string(radix+86)
}
table = append(table, alphabet)
}
return table
}()
func digitValue(chr rune) int {
switch {
case '0' <= chr && chr <= '9':
return int(chr - '0')
case 'a' <= chr && chr <= 'z':
return int(chr - 'a' + 10)
case 'A' <= chr && chr <= 'Z':
return int(chr - 'A' + 10)
}
return 36 // Larger than any legal digit value
}
func builtinGlobal_parseInt(call FunctionCall) Value {
input := strings.Trim(call.Argument(0).string(), builtinString_trim_whitespace)
if len(input) == 0 {
return NaNValue()
}
radix := int(toInt32(call.Argument(1)))
negative := false
switch input[0] {
case '+':
input = input[1:]
case '-':
negative = true
input = input[1:]
}
strip := true
if radix == 0 {
radix = 10
} else {
if radix < 2 || radix > 36 {
return NaNValue()
} else if radix != 16 {
strip = false
}
}
switch len(input) {
case 0:
return NaNValue()
case 1:
default:
if strip {
if input[0] == '0' && (input[1] == 'x' || input[1] == 'X') {
input = input[2:]
radix = 16
}
}
}
base := radix
index := 0
for ; index < len(input); index++ {
digit := digitValue(rune(input[index])) // If not ASCII, then an error anyway
if digit >= base {
break
}
}
input = input[0:index]
value, err := strconv.ParseInt(input, radix, 64)
if err != nil {
if err.(*strconv.NumError).Err == strconv.ErrRange {
base := float64(base)
// Could just be a very large number (e.g. 0x8000000000000000)
var value float64
for _, chr := range input {
digit := float64(digitValue(chr))
if digit >= base {
goto error
}
value = value*base + digit
}
if negative {
value *= -1
}
return toValue_float64(value)
}
error:
return NaNValue()
}
if negative {
value *= -1
}
return toValue_int64(value)
}
var parseFloat_matchBadSpecial = regexp.MustCompile(`[\+\-]?(?:[Ii]nf$|infinity)`)
var parseFloat_matchValid = regexp.MustCompile(`[0-9eE\+\-\.]|Infinity`)
func builtinGlobal_parseFloat(call FunctionCall) Value {
// Caveat emptor: This implementation does NOT match the specification
input := strings.Trim(call.Argument(0).string(), builtinString_trim_whitespace)
if parseFloat_matchBadSpecial.MatchString(input) {
return NaNValue()
}
value, err := strconv.ParseFloat(input, 64)
if err != nil {
for end := len(input); end > 0; end-- {
input := input[0:end]
if !parseFloat_matchValid.MatchString(input) {
return NaNValue()
}
value, err = strconv.ParseFloat(input, 64)
if err == nil {
break
}
}
if err != nil {
return NaNValue()
}
}
return toValue_float64(value)
}
// encodeURI/decodeURI
func _builtinGlobal_encodeURI(call FunctionCall, escape *regexp.Regexp) Value {
value := call.Argument(0)
var input []uint16
switch vl := value.value.(type) {
case []uint16:
input = vl
default:
input = utf16.Encode([]rune(value.string()))
}
if len(input) == 0 {
return toValue_string("")
}
output := []byte{}
length := len(input)
encode := make([]byte, 4)
for index := 0; index < length; {
value := input[index]
decode := utf16.Decode(input[index : index+1])
if value >= 0xDC00 && value <= 0xDFFF {
panic(call.runtime.panicURIError("URI malformed"))
}
if value >= 0xD800 && value <= 0xDBFF {
index += 1
if index >= length {
panic(call.runtime.panicURIError("URI malformed"))
}
// input = ..., value, value1, ...
value1 := input[index]
if value1 < 0xDC00 || value1 > 0xDFFF {
panic(call.runtime.panicURIError("URI malformed"))
}
decode = []rune{((rune(value) - 0xD800) * 0x400) + (rune(value1) - 0xDC00) + 0x10000}
}
index += 1
size := utf8.EncodeRune(encode, decode[0])
encode := encode[0:size]
output = append(output, encode...)
}
{
value := escape.ReplaceAllFunc(output, func(target []byte) []byte {
// Probably a better way of doing this
if target[0] == ' ' {
return []byte("%20")
}
return []byte(url.QueryEscape(string(target)))
})
return toValue_string(string(value))
}
}
var encodeURI_Regexp = regexp.MustCompile(`([^~!@#$&*()=:/,;?+'])`)
func builtinGlobal_encodeURI(call FunctionCall) Value {
return _builtinGlobal_encodeURI(call, encodeURI_Regexp)
}
var encodeURIComponent_Regexp = regexp.MustCompile(`([^~!*()'])`)
func builtinGlobal_encodeURIComponent(call FunctionCall) Value {
return _builtinGlobal_encodeURI(call, encodeURIComponent_Regexp)
}
// 3B/2F/3F/3A/40/26/3D/2B/24/2C/23
var decodeURI_guard = regexp.MustCompile(`(?i)(?:%)(3B|2F|3F|3A|40|26|3D|2B|24|2C|23)`)
func _decodeURI(input string, reserve bool) (string, bool) {
if reserve {
input = decodeURI_guard.ReplaceAllString(input, "%25$1")
}
input = strings.Replace(input, "+", "%2B", -1) // Ugly hack to make QueryUnescape work with our use case
output, err := url.QueryUnescape(input)
if err != nil || !utf8.ValidString(output) {
return "", true
}
return output, false
}
func builtinGlobal_decodeURI(call FunctionCall) Value {
output, err := _decodeURI(call.Argument(0).string(), true)
if err {
panic(call.runtime.panicURIError("URI malformed"))
}
return toValue_string(output)
}
func builtinGlobal_decodeURIComponent(call FunctionCall) Value {
output, err := _decodeURI(call.Argument(0).string(), false)
if err {
panic(call.runtime.panicURIError("URI malformed"))
}
return toValue_string(output)
}
// escape/unescape
func builtin_shouldEscape(chr byte) bool {
if 'A' <= chr && chr <= 'Z' || 'a' <= chr && chr <= 'z' || '0' <= chr && chr <= '9' {
return false
}
return !strings.ContainsRune("*_+-./", rune(chr))
}
const escapeBase16 = "0123456789ABCDEF"
func builtin_escape(input string) string {
output := make([]byte, 0, len(input))
length := len(input)
for index := 0; index < length; {
if builtin_shouldEscape(input[index]) {
chr, width := utf8.DecodeRuneInString(input[index:])
chr16 := utf16.Encode([]rune{chr})[0]
if 256 > chr16 {
output = append(output, '%',
escapeBase16[chr16>>4],
escapeBase16[chr16&15],
)
} else {
output = append(output, '%', 'u',
escapeBase16[chr16>>12],
escapeBase16[(chr16>>8)&15],
escapeBase16[(chr16>>4)&15],
escapeBase16[chr16&15],
)
}
index += width
} else {
output = append(output, input[index])
index += 1
}
}
return string(output)
}
func builtin_unescape(input string) string {
output := make([]rune, 0, len(input))
length := len(input)
for index := 0; index < length; {
if input[index] == '%' {
if index <= length-6 && input[index+1] == 'u' {
byte16, err := hex.DecodeString(input[index+2 : index+6])
if err == nil {
value := uint16(byte16[0])<<8 + uint16(byte16[1])
chr := utf16.Decode([]uint16{value})[0]
output = append(output, chr)
index += 6
continue
}
}
if index <= length-3 {
byte8, err := hex.DecodeString(input[index+1 : index+3])
if err == nil {
value := uint16(byte8[0])
chr := utf16.Decode([]uint16{value})[0]
output = append(output, chr)
index += 3
continue
}
}
}
output = append(output, rune(input[index]))
index += 1
}
return string(output)
}
func builtinGlobal_escape(call FunctionCall) Value {
return toValue_string(builtin_escape(call.Argument(0).string()))
}
func builtinGlobal_unescape(call FunctionCall) Value {
return toValue_string(builtin_unescape(call.Argument(0).string()))
}
This diff is collapsed.
package otto
// Boolean
func builtinBoolean(call FunctionCall) Value {
return toValue_bool(call.Argument(0).bool())
}
func builtinNewBoolean(self *_object, argumentList []Value) Value {
return toValue_object(self.runtime.newBoolean(valueOfArrayIndex(argumentList, 0)))
}
func builtinBoolean_toString(call FunctionCall) Value {
value := call.This
if !value.IsBoolean() {
// Will throw a TypeError if ThisObject is not a Boolean
value = call.thisClassObject("Boolean").primitiveValue()
}
return toValue_string(value.string())
}
func builtinBoolean_valueOf(call FunctionCall) Value {
value := call.This
if !value.IsBoolean() {
value = call.thisClassObject("Boolean").primitiveValue()
}
return value
}
This diff is collapsed.
package otto
import (
"fmt"
)
func builtinError(call FunctionCall) Value {
return toValue_object(call.runtime.newError("Error", call.Argument(0), 1))
}
func builtinNewError(self *_object, argumentList []Value) Value {
return toValue_object(self.runtime.newError("Error", valueOfArrayIndex(argumentList, 0), 0))
}
func builtinError_toString(call FunctionCall) Value {
thisObject := call.thisObject()
if thisObject == nil {
panic(call.runtime.panicTypeError())
}
name := "Error"
nameValue := thisObject.get("name")
if nameValue.IsDefined() {
name = nameValue.string()
}
message := ""
messageValue := thisObject.get("message")
if messageValue.IsDefined() {
message = messageValue.string()
}
if len(name) == 0 {
return toValue_string(message)
}
if len(message) == 0 {
return toValue_string(name)
}
return toValue_string(fmt.Sprintf("%s: %s", name, message))
}
func (runtime *_runtime) newEvalError(message Value) *_object {
self := runtime.newErrorObject("EvalError", message, 0)
self.prototype = runtime.global.EvalErrorPrototype
return self
}
func builtinEvalError(call FunctionCall) Value {
return toValue_object(call.runtime.newEvalError(call.Argument(0)))
}
func builtinNewEvalError(self *_object, argumentList []Value) Value {
return toValue_object(self.runtime.newEvalError(valueOfArrayIndex(argumentList, 0)))
}
func (runtime *_runtime) newTypeError(message Value) *_object {
self := runtime.newErrorObject("TypeError", message, 0)
self.prototype = runtime.global.TypeErrorPrototype
return self
}
func builtinTypeError(call FunctionCall) Value {
return toValue_object(call.runtime.newTypeError(call.Argument(0)))
}
func builtinNewTypeError(self *_object, argumentList []Value) Value {
return toValue_object(self.runtime.newTypeError(valueOfArrayIndex(argumentList, 0)))
}
func (runtime *_runtime) newRangeError(message Value) *_object {
self := runtime.newErrorObject("RangeError", message, 0)
self.prototype = runtime.global.RangeErrorPrototype
return self
}
func builtinRangeError(call FunctionCall) Value {
return toValue_object(call.runtime.newRangeError(call.Argument(0)))
}
func builtinNewRangeError(self *_object, argumentList []Value) Value {
return toValue_object(self.runtime.newRangeError(valueOfArrayIndex(argumentList, 0)))
}
func (runtime *_runtime) newURIError(message Value) *_object {
self := runtime.newErrorObject("URIError", message, 0)
self.prototype = runtime.global.URIErrorPrototype
return self
}
func (runtime *_runtime) newReferenceError(message Value) *_object {
self := runtime.newErrorObject("ReferenceError", message, 0)
self.prototype = runtime.global.ReferenceErrorPrototype
return self
}
func builtinReferenceError(call FunctionCall) Value {
return toValue_object(call.runtime.newReferenceError(call.Argument(0)))
}
func builtinNewReferenceError(self *_object, argumentList []Value) Value {
return toValue_object(self.runtime.newReferenceError(valueOfArrayIndex(argumentList, 0)))
}
func (runtime *_runtime) newSyntaxError(message Value) *_object {
self := runtime.newErrorObject("SyntaxError", message, 0)
self.prototype = runtime.global.SyntaxErrorPrototype
return self
}
func builtinSyntaxError(call FunctionCall) Value {
return toValue_object(call.runtime.newSyntaxError(call.Argument(0)))
}
func builtinNewSyntaxError(self *_object, argumentList []Value) Value {
return toValue_object(self.runtime.newSyntaxError(valueOfArrayIndex(argumentList, 0)))
}
func builtinURIError(call FunctionCall) Value {
return toValue_object(call.runtime.newURIError(call.Argument(0)))
}
func builtinNewURIError(self *_object, argumentList []Value) Value {
return toValue_object(self.runtime.newURIError(valueOfArrayIndex(argumentList, 0)))
}
package otto
import (
"fmt"
"regexp"
"strings"
"unicode"
"github.com/robertkrimen/otto/parser"
)
// Function
func builtinFunction(call FunctionCall) Value {
return toValue_object(builtinNewFunctionNative(call.runtime, call.ArgumentList))
}
func builtinNewFunction(self *_object, argumentList []Value) Value {
return toValue_object(builtinNewFunctionNative(self.runtime, argumentList))
}
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
}
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] {
tmp = append(tmp, value.string())
}
parameterList = strings.Join(tmp, ",")
body = argumentList[count-1].string()
}
// FIXME
function, err := parser.ParseFunction(parameterList, body)
runtime.parseThrow(err) // Will panic/throw appropriately
cmpl := _compiler{}
cmpl_function := cmpl.parseExpression(function)
return runtime.newNodeFunction(cmpl_function.(*_nodeFunctionLiteral), runtime.globalStash)
}
func builtinFunction_toString(call FunctionCall) Value {
object := call.thisClassObject("Function") // Should throw a TypeError unless Function
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()"))
}
func builtinFunction_apply(call FunctionCall) Value {
if !call.This.isCallable() {
panic(call.runtime.panicTypeError())
}
this := call.Argument(0)
if this.IsUndefined() {
// FIXME Not ECMA5
this = toValue_object(call.runtime.globalObject)
}
argumentList := call.Argument(1)
switch argumentList.kind {
case valueUndefined, valueNull:
return call.thisObject().call(this, nil, false, nativeFrame)
case valueObject:
default:
panic(call.runtime.panicTypeError())
}
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))
}
return thisObject.call(this, valueArray, false, nativeFrame)
}
func builtinFunction_call(call FunctionCall) Value {
if !call.This.isCallable() {
panic(call.runtime.panicTypeError())
}
thisObject := call.thisObject()
this := call.Argument(0)
if this.IsUndefined() {
// FIXME Not ECMA5
this = toValue_object(call.runtime.globalObject)
}
if len(call.ArgumentList) >= 1 {
return thisObject.call(this, call.ArgumentList[1:], false, nativeFrame)
}
return thisObject.call(this, nil, false, nativeFrame)
}
func builtinFunction_bind(call FunctionCall) Value {
target := call.This
if !target.isCallable() {
panic(call.runtime.panicTypeError())
}
targetObject := target._object()
this := call.Argument(0)
argumentList := call.slice(1)
if this.IsUndefined() {
// FIXME Do this elsewhere?
this = toValue_object(call.runtime.globalObject)
}
return toValue_object(call.runtime.newBoundFunction(targetObject, this, argumentList))
}
package otto
import (
"bytes"
"encoding/json"
"fmt"
"strings"
)
type _builtinJSON_parseContext struct {
call FunctionCall
reviver Value
}
func builtinJSON_parse(call FunctionCall) Value {
ctx := _builtinJSON_parseContext{
call: call,
}
revive := false
if reviver := call.Argument(1); reviver.isCallable() {
revive = true
ctx.reviver = reviver
}
var root interface{}
err := json.Unmarshal([]byte(call.Argument(0).string()), &root)
if err != nil {
panic(call.runtime.panicSyntaxError(err.Error()))
}
value, exists := builtinJSON_parseWalk(ctx, root)
if !exists {
value = Value{}
}
if revive {
root := ctx.call.runtime.newObject()
root.put("", value, false)
return builtinJSON_reviveWalk(ctx, root, "")
}
return value
}
func builtinJSON_reviveWalk(ctx _builtinJSON_parseContext, holder *_object, name string) Value {
value := holder.get(name)
if object := value._object(); object != nil {
if isArray(object) {
length := int64(objectLength(object))
for index := int64(0); index < length; index += 1 {
name := arrayIndexToString(index)
value := builtinJSON_reviveWalk(ctx, object, name)
if value.IsUndefined() {
object.delete(name, false)
} else {
object.defineProperty(name, value, 0111, false)
}
}
} else {
object.enumerate(false, func(name string) bool {
value := builtinJSON_reviveWalk(ctx, object, name)
if value.IsUndefined() {
object.delete(name, false)
} else {
object.defineProperty(name, value, 0111, false)
}
return true
})
}
}
return ctx.reviver.call(ctx.call.runtime, toValue_object(holder), name, value)
}
func builtinJSON_parseWalk(ctx _builtinJSON_parseContext, rawValue interface{}) (Value, bool) {
switch value := rawValue.(type) {
case nil:
return nullValue, true
case bool:
return toValue_bool(value), true
case string:
return toValue_string(value), true
case float64:
return toValue_float64(value), true
case []interface{}:
arrayValue := make([]Value, len(value))
for index, rawValue := range value {
if value, exists := builtinJSON_parseWalk(ctx, rawValue); exists {
arrayValue[index] = value
}
}
return toValue_object(ctx.call.runtime.newArrayOf(arrayValue)), true
case map[string]interface{}:
object := ctx.call.runtime.newObject()
for name, rawValue := range value {
if value, exists := builtinJSON_parseWalk(ctx, rawValue); exists {
object.put(name, value, false)
}
}
return toValue_object(object), true
}
return Value{}, false
}
type _builtinJSON_stringifyContext struct {
call FunctionCall
stack []*_object
propertyList []string
replacerFunction *Value
gap string
}
func builtinJSON_stringify(call FunctionCall) Value {
ctx := _builtinJSON_stringifyContext{
call: call,
stack: []*_object{nil},
}
replacer := call.Argument(1)._object()
if replacer != nil {
if isArray(replacer) {
length := objectLength(replacer)
seen := map[string]bool{}
propertyList := make([]string, length)
length = 0
for index, _ := range propertyList {
value := replacer.get(arrayIndexToString(int64(index)))
switch value.kind {
case valueObject:
switch value.value.(*_object).class {
case "String":
case "Number":
default:
continue
}
case valueString:
case valueNumber:
default:
continue
}
name := value.string()
if seen[name] {
continue
}
seen[name] = true
length += 1
propertyList[index] = name
}
ctx.propertyList = propertyList[0:length]
} else if replacer.class == "Function" {
value := toValue_object(replacer)
ctx.replacerFunction = &value
}
}
if spaceValue, exists := call.getArgument(2); exists {
if spaceValue.kind == valueObject {
switch spaceValue.value.(*_object).class {
case "String":
spaceValue = toValue_string(spaceValue.string())
case "Number":
spaceValue = spaceValue.numberValue()
}
}
switch spaceValue.kind {
case valueString:
value := spaceValue.string()
if len(value) > 10 {
ctx.gap = value[0:10]
} else {
ctx.gap = value
}
case valueNumber:
value := spaceValue.number().int64
if value > 10 {
value = 10
} else if value < 0 {
value = 0
}
ctx.gap = strings.Repeat(" ", int(value))
}
}
holder := call.runtime.newObject()
holder.put("", call.Argument(0), false)
value, exists := builtinJSON_stringifyWalk(ctx, "", holder)
if !exists {
return Value{}
}
valueJSON, err := json.Marshal(value)
if err != nil {
panic(call.runtime.panicTypeError(err.Error()))
}
if ctx.gap != "" {
valueJSON1 := bytes.Buffer{}
json.Indent(&valueJSON1, valueJSON, "", ctx.gap)
valueJSON = valueJSON1.Bytes()
}
return toValue_string(string(valueJSON))
}
func builtinJSON_stringifyWalk(ctx _builtinJSON_stringifyContext, key string, holder *_object) (interface{}, bool) {
value := holder.get(key)
if value.IsObject() {
object := value._object()
if toJSON := object.get("toJSON"); toJSON.IsFunction() {
value = toJSON.call(ctx.call.runtime, value, key)
} else {
// If the object is a GoStruct or something that implements json.Marshaler
if object.objectClass.marshalJSON != nil {
marshaler := object.objectClass.marshalJSON(object)
if marshaler != nil {
return marshaler, true
}
}
}
}
if ctx.replacerFunction != nil {
value = (*ctx.replacerFunction).call(ctx.call.runtime, toValue_object(holder), key, value)
}
if value.kind == valueObject {
switch value.value.(*_object).class {
case "Boolean":
value = value._object().value.(Value)
case "String":
value = toValue_string(value.string())
case "Number":
value = value.numberValue()
}
}
switch value.kind {
case valueBoolean:
return value.bool(), true
case valueString:
return value.string(), true
case valueNumber:
integer := value.number()
switch integer.kind {
case numberInteger:
return integer.int64, true
case numberFloat:
return integer.float64, true
default:
return nil, true
}
case valueNull:
return nil, true
case valueObject:
holder := value._object()
if value := value._object(); nil != value {
for _, object := range ctx.stack {
if holder == object {
panic(ctx.call.runtime.panicTypeError("Converting circular structure to JSON"))
}
}
ctx.stack = append(ctx.stack, value)
defer func() { ctx.stack = ctx.stack[:len(ctx.stack)-1] }()
}
if isArray(holder) {
var length uint32
switch value := holder.get("length").value.(type) {
case uint32:
length = value
case int:
if value >= 0 {
length = uint32(value)
}
default:
panic(ctx.call.runtime.panicTypeError(fmt.Sprintf("JSON.stringify: invalid length: %v (%[1]T)", value)))
}
array := make([]interface{}, length)
for index, _ := range array {
name := arrayIndexToString(int64(index))
value, _ := builtinJSON_stringifyWalk(ctx, name, holder)
array[index] = value
}
return array, true
} else if holder.class != "Function" {
object := map[string]interface{}{}
if ctx.propertyList != nil {
for _, name := range ctx.propertyList {
value, exists := builtinJSON_stringifyWalk(ctx, name, holder)
if exists {
object[name] = value
}
}
} else {
// Go maps are without order, so this doesn't conform to the ECMA ordering
// standard, but oh well...
holder.enumerate(false, func(name string) bool {
value, exists := builtinJSON_stringifyWalk(ctx, name, holder)
if exists {
object[name] = value
}
return true
})
}
return object, true
}
}
return nil, false
}
package otto
import (
"math"
"math/rand"
)
// Math
func builtinMath_abs(call FunctionCall) Value {
number := call.Argument(0).float64()
return toValue_float64(math.Abs(number))
}
func builtinMath_acos(call FunctionCall) Value {
number := call.Argument(0).float64()
return toValue_float64(math.Acos(number))
}
func builtinMath_asin(call FunctionCall) Value {
number := call.Argument(0).float64()
return toValue_float64(math.Asin(number))
}
func builtinMath_atan(call FunctionCall) Value {
number := call.Argument(0).float64()
return toValue_float64(math.Atan(number))
}
func builtinMath_atan2(call FunctionCall) Value {
y := call.Argument(0).float64()
if math.IsNaN(y) {
return NaNValue()
}
x := call.Argument(1).float64()
if math.IsNaN(x) {
return NaNValue()
}
return toValue_float64(math.Atan2(y, x))
}
func builtinMath_cos(call FunctionCall) Value {
number := call.Argument(0).float64()
return toValue_float64(math.Cos(number))
}
func builtinMath_ceil(call FunctionCall) Value {
number := call.Argument(0).float64()
return toValue_float64(math.Ceil(number))
}
func builtinMath_exp(call FunctionCall) Value {
number := call.Argument(0).float64()
return toValue_float64(math.Exp(number))
}
func builtinMath_floor(call FunctionCall) Value {
number := call.Argument(0).float64()
return toValue_float64(math.Floor(number))
}
func builtinMath_log(call FunctionCall) Value {
number := call.Argument(0).float64()
return toValue_float64(math.Log(number))
}
func builtinMath_max(call FunctionCall) Value {
switch len(call.ArgumentList) {
case 0:
return negativeInfinityValue()
case 1:
return toValue_float64(call.ArgumentList[0].float64())
}
result := call.ArgumentList[0].float64()
if math.IsNaN(result) {
return NaNValue()
}
for _, value := range call.ArgumentList[1:] {
value := value.float64()
if math.IsNaN(value) {
return NaNValue()
}
result = math.Max(result, value)
}
return toValue_float64(result)
}
func builtinMath_min(call FunctionCall) Value {
switch len(call.ArgumentList) {
case 0:
return positiveInfinityValue()
case 1:
return toValue_float64(call.ArgumentList[0].float64())
}
result := call.ArgumentList[0].float64()
if math.IsNaN(result) {
return NaNValue()
}
for _, value := range call.ArgumentList[1:] {
value := value.float64()
if math.IsNaN(value) {
return NaNValue()
}
result = math.Min(result, value)
}
return toValue_float64(result)
}
func builtinMath_pow(call FunctionCall) Value {
// TODO Make sure this works according to the specification (15.8.2.13)
x := call.Argument(0).float64()
y := call.Argument(1).float64()
if math.Abs(x) == 1 && math.IsInf(y, 0) {
return NaNValue()
}
return toValue_float64(math.Pow(x, y))
}
func builtinMath_random(call FunctionCall) Value {
var v float64
if call.runtime.random != nil {
v = call.runtime.random()
} else {
v = rand.Float64()
}
return toValue_float64(v)
}
func builtinMath_round(call FunctionCall) Value {
number := call.Argument(0).float64()
value := math.Floor(number + 0.5)
if value == 0 {
value = math.Copysign(0, number)
}
return toValue_float64(value)
}
func builtinMath_sin(call FunctionCall) Value {
number := call.Argument(0).float64()
return toValue_float64(math.Sin(number))
}
func builtinMath_sqrt(call FunctionCall) Value {
number := call.Argument(0).float64()
return toValue_float64(math.Sqrt(number))
}
func builtinMath_tan(call FunctionCall) Value {
number := call.Argument(0).float64()
return toValue_float64(math.Tan(number))
}
package otto
import (
"math"
"strconv"
)
// Number
func numberValueFromNumberArgumentList(argumentList []Value) Value {
if len(argumentList) > 0 {
return argumentList[0].numberValue()
}
return toValue_int(0)
}
func builtinNumber(call FunctionCall) Value {
return numberValueFromNumberArgumentList(call.ArgumentList)
}
func builtinNewNumber(self *_object, argumentList []Value) Value {
return toValue_object(self.runtime.newNumber(numberValueFromNumberArgumentList(argumentList)))
}
func builtinNumber_toString(call FunctionCall) Value {
// Will throw a TypeError if ThisObject is not a Number
value := call.thisClassObject("Number").primitiveValue()
radix := 10
radixArgument := call.Argument(0)
if radixArgument.IsDefined() {
integer := toIntegerFloat(radixArgument)
if integer < 2 || integer > 36 {
panic(call.runtime.panicRangeError("toString() radix must be between 2 and 36"))
}
radix = int(integer)
}
if radix == 10 {
return toValue_string(value.string())
}
return toValue_string(numberToStringRadix(value, radix))
}
func builtinNumber_valueOf(call FunctionCall) Value {
return call.thisClassObject("Number").primitiveValue()
}
func builtinNumber_toFixed(call FunctionCall) Value {
precision := toIntegerFloat(call.Argument(0))
if 20 < precision || 0 > precision {
panic(call.runtime.panicRangeError("toFixed() precision must be between 0 and 20"))
}
if call.This.IsNaN() {
return toValue_string("NaN")
}
value := call.This.float64()
if math.Abs(value) >= 1e21 {
return toValue_string(floatToString(value, 64))
}
return toValue_string(strconv.FormatFloat(call.This.float64(), 'f', int(precision), 64))
}
func builtinNumber_toExponential(call FunctionCall) Value {
if call.This.IsNaN() {
return toValue_string("NaN")
}
precision := float64(-1)
if value := call.Argument(0); value.IsDefined() {
precision = toIntegerFloat(value)
if 0 > precision {
panic(call.runtime.panicRangeError("toString() radix must be between 2 and 36"))
}
}
return toValue_string(strconv.FormatFloat(call.This.float64(), 'e', int(precision), 64))
}
func builtinNumber_toPrecision(call FunctionCall) Value {
if call.This.IsNaN() {
return toValue_string("NaN")
}
value := call.Argument(0)
if value.IsUndefined() {
return toValue_string(call.This.string())
}
precision := toIntegerFloat(value)
if 1 > precision {
panic(call.runtime.panicRangeError("toPrecision() precision must be greater than 1"))
}
return toValue_string(strconv.FormatFloat(call.This.float64(), 'g', int(precision), 64))
}
func builtinNumber_toLocaleString(call FunctionCall) Value {
return builtinNumber_toString(call)
}
package otto
import (
"fmt"
)
// Object
func builtinObject(call FunctionCall) Value {
value := call.Argument(0)
switch value.kind {
case valueUndefined, valueNull:
return toValue_object(call.runtime.newObject())
}
return toValue_object(call.runtime.toObject(value))
}
func builtinNewObject(self *_object, argumentList []Value) Value {
value := valueOfArrayIndex(argumentList, 0)
switch value.kind {
case valueNull, valueUndefined:
case valueNumber, valueString, valueBoolean:
return toValue_object(self.runtime.toObject(value))
case valueObject:
return value
default:
}
return toValue_object(self.runtime.newObject())
}
func builtinObject_valueOf(call FunctionCall) Value {
return toValue_object(call.thisObject())
}
func builtinObject_hasOwnProperty(call FunctionCall) Value {
propertyName := call.Argument(0).string()
thisObject := call.thisObject()
return toValue_bool(thisObject.hasOwnProperty(propertyName))
}
func builtinObject_isPrototypeOf(call FunctionCall) Value {
value := call.Argument(0)
if !value.IsObject() {
return falseValue
}
prototype := call.toObject(value).prototype
thisObject := call.thisObject()
for prototype != nil {
if thisObject == prototype {
return trueValue
}
prototype = prototype.prototype
}
return falseValue
}
func builtinObject_propertyIsEnumerable(call FunctionCall) Value {
propertyName := call.Argument(0).string()
thisObject := call.thisObject()
property := thisObject.getOwnProperty(propertyName)
if property != nil && property.enumerable() {
return trueValue
}
return falseValue
}
func builtinObject_toString(call FunctionCall) Value {
result := ""
if call.This.IsUndefined() {
result = "[object Undefined]"
} else if call.This.IsNull() {
result = "[object Null]"
} else {
result = fmt.Sprintf("[object %s]", call.thisObject().class)
}
return toValue_string(result)
}
func builtinObject_toLocaleString(call FunctionCall) Value {
toString := call.thisObject().get("toString")
if !toString.isCallable() {
panic(call.runtime.panicTypeError())
}
return toString.call(call.runtime, call.This)
}
func builtinObject_getPrototypeOf(call FunctionCall) Value {
objectValue := call.Argument(0)
object := objectValue._object()
if object == nil {
panic(call.runtime.panicTypeError())
}
if object.prototype == nil {
return nullValue
}
return toValue_object(object.prototype)
}
func builtinObject_getOwnPropertyDescriptor(call FunctionCall) Value {
objectValue := call.Argument(0)
object := objectValue._object()
if object == nil {
panic(call.runtime.panicTypeError())
}
name := call.Argument(1).string()
descriptor := object.getOwnProperty(name)
if descriptor == nil {
return Value{}
}
return toValue_object(call.runtime.fromPropertyDescriptor(*descriptor))
}
func builtinObject_defineProperty(call FunctionCall) Value {
objectValue := call.Argument(0)
object := objectValue._object()
if object == nil {
panic(call.runtime.panicTypeError())
}
name := call.Argument(1).string()
descriptor := toPropertyDescriptor(call.runtime, call.Argument(2))
object.defineOwnProperty(name, descriptor, true)
return objectValue
}
func builtinObject_defineProperties(call FunctionCall) Value {
objectValue := call.Argument(0)
object := objectValue._object()
if object == nil {
panic(call.runtime.panicTypeError())
}
properties := call.runtime.toObject(call.Argument(1))
properties.enumerate(false, func(name string) bool {
descriptor := toPropertyDescriptor(call.runtime, properties.get(name))
object.defineOwnProperty(name, descriptor, true)
return true
})
return objectValue
}
func builtinObject_create(call FunctionCall) Value {
prototypeValue := call.Argument(0)
if !prototypeValue.IsNull() && !prototypeValue.IsObject() {
panic(call.runtime.panicTypeError())
}
object := call.runtime.newObject()
object.prototype = prototypeValue._object()
propertiesValue := call.Argument(1)
if propertiesValue.IsDefined() {
properties := call.runtime.toObject(propertiesValue)
properties.enumerate(false, func(name string) bool {
descriptor := toPropertyDescriptor(call.runtime, properties.get(name))
object.defineOwnProperty(name, descriptor, true)
return true
})
}
return toValue_object(object)
}
func builtinObject_isExtensible(call FunctionCall) Value {
object := call.Argument(0)
if object := object._object(); object != nil {
return toValue_bool(object.extensible)
}
panic(call.runtime.panicTypeError())
}
func builtinObject_preventExtensions(call FunctionCall) Value {
object := call.Argument(0)
if object := object._object(); object != nil {
object.extensible = false
} else {
panic(call.runtime.panicTypeError())
}
return object
}
func builtinObject_isSealed(call FunctionCall) Value {
object := call.Argument(0)
if object := object._object(); object != nil {
if object.extensible {
return toValue_bool(false)
}
result := true
object.enumerate(true, func(name string) bool {
property := object.getProperty(name)
if property.configurable() {
result = false
}
return true
})
return toValue_bool(result)
}
panic(call.runtime.panicTypeError())
}
func builtinObject_seal(call FunctionCall) Value {
object := call.Argument(0)
if object := object._object(); object != nil {
object.enumerate(true, func(name string) bool {
if property := object.getOwnProperty(name); nil != property && property.configurable() {
property.configureOff()
object.defineOwnProperty(name, *property, true)
}
return true
})
object.extensible = false
} else {
panic(call.runtime.panicTypeError())
}
return object
}
func builtinObject_isFrozen(call FunctionCall) Value {
object := call.Argument(0)
if object := object._object(); object != nil {
if object.extensible {
return toValue_bool(false)
}
result := true
object.enumerate(true, func(name string) bool {
property := object.getProperty(name)
if property.configurable() || property.writable() {
result = false
}
return true
})
return toValue_bool(result)
}
panic(call.runtime.panicTypeError())
}
func builtinObject_freeze(call FunctionCall) Value {
object := call.Argument(0)
if object := object._object(); object != nil {
object.enumerate(true, func(name string) bool {
if property, update := object.getOwnProperty(name), false; nil != property {
if property.isDataDescriptor() && property.writable() {
property.writeOff()
update = true
}
if property.configurable() {
property.configureOff()
update = true
}
if update {
object.defineOwnProperty(name, *property, true)
}
}
return true
})
object.extensible = false
} else {
panic(call.runtime.panicTypeError())
}
return object
}
func builtinObject_keys(call FunctionCall) Value {
if object, keys := call.Argument(0)._object(), []Value(nil); nil != object {
object.enumerate(false, func(name string) bool {
keys = append(keys, toValue_string(name))
return true
})
return toValue_object(call.runtime.newArrayOf(keys))
}
panic(call.runtime.panicTypeError())
}
func builtinObject_getOwnPropertyNames(call FunctionCall) Value {
if object, propertyNames := call.Argument(0)._object(), []Value(nil); nil != object {
object.enumerate(true, func(name string) bool {
if object.hasOwnProperty(name) {
propertyNames = append(propertyNames, toValue_string(name))
}
return true
})
return toValue_object(call.runtime.newArrayOf(propertyNames))
}
panic(call.runtime.panicTypeError())
}
package otto
import (
"fmt"
)
// RegExp
func builtinRegExp(call FunctionCall) Value {
pattern := call.Argument(0)
flags := call.Argument(1)
if object := pattern._object(); object != nil {
if object.class == "RegExp" && flags.IsUndefined() {
return pattern
}
}
return toValue_object(call.runtime.newRegExp(pattern, flags))
}
func builtinNewRegExp(self *_object, argumentList []Value) Value {
return toValue_object(self.runtime.newRegExp(
valueOfArrayIndex(argumentList, 0),
valueOfArrayIndex(argumentList, 1),
))
}
func builtinRegExp_toString(call FunctionCall) Value {
thisObject := call.thisObject()
source := thisObject.get("source").string()
flags := []byte{}
if thisObject.get("global").bool() {
flags = append(flags, 'g')
}
if thisObject.get("ignoreCase").bool() {
flags = append(flags, 'i')
}
if thisObject.get("multiline").bool() {
flags = append(flags, 'm')
}
return toValue_string(fmt.Sprintf("/%s/%s", source, flags))
}
func builtinRegExp_exec(call FunctionCall) Value {
thisObject := call.thisObject()
target := call.Argument(0).string()
match, result := execRegExp(thisObject, target)
if !match {
return nullValue
}
return toValue_object(execResultToArray(call.runtime, target, result))
}
func builtinRegExp_test(call FunctionCall) Value {
thisObject := call.thisObject()
target := call.Argument(0).string()
match, _ := execRegExp(thisObject, target)
return toValue_bool(match)
}
func builtinRegExp_compile(call FunctionCall) Value {
// This (useless) function is deprecated, but is here to provide some
// semblance of compatibility.
// Caveat emptor: it may not be around for long.
return Value{}
}
This diff is collapsed.
package otto
import (
"testing"
)
func TestString_substr(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
[
"abc".substr(0,1), // "a"
"abc".substr(0,2), // "ab"
"abc".substr(0,3), // "abc"
"abc".substr(0,4), // "abc"
"abc".substr(0,9), // "abc"
];
`, "a,ab,abc,abc,abc")
test(`
[
"abc".substr(1,1), // "b"
"abc".substr(1,2), // "bc"
"abc".substr(1,3), // "bc"
"abc".substr(1,4), // "bc"
"abc".substr(1,9), // "bc"
];
`, "b,bc,bc,bc,bc")
test(`
[
"abc".substr(2,1), // "c"
"abc".substr(2,2), // "c"
"abc".substr(2,3), // "c"
"abc".substr(2,4), // "c"
"abc".substr(2,9), // "c"
];
`, "c,c,c,c,c")
test(`
[
"abc".substr(3,1), // ""
"abc".substr(3,2), // ""
"abc".substr(3,3), // ""
"abc".substr(3,4), // ""
"abc".substr(3,9), // ""
];
`, ",,,,")
test(`
[
"abc".substr(0), // "abc"
"abc".substr(1), // "bc"
"abc".substr(2), // "c"
"abc".substr(3), // ""
"abc".substr(9), // ""
];
`, "abc,bc,c,,")
test(`
[
"abc".substr(-9), // "abc"
"abc".substr(-3), // "abc"
"abc".substr(-2), // "bc"
"abc".substr(-1), // "c"
];
`, "abc,abc,bc,c")
test(`
[
"abc".substr(-9, 1), // "a"
"abc".substr(-3, 1), // "a"
"abc".substr(-2, 1), // "b"
"abc".substr(-1, 1), // "c"
"abc".substr(-1, 2), // "c"
];
`, "a,a,b,c,c")
test(`"abcd".substr(3, 5)`, "d")
})
}
func Test_builtin_escape(t *testing.T) {
tt(t, func() {
is(builtin_escape("abc"), "abc")
is(builtin_escape("="), "%3D")
is(builtin_escape("abc=%+32"), "abc%3D%25+32")
is(builtin_escape("世界"), "%u4E16%u754C")
})
}
func Test_builtin_unescape(t *testing.T) {
tt(t, func() {
is(builtin_unescape("abc"), "abc")
is(builtin_unescape("=%3D"), "==")
is(builtin_unescape("abc%3D%25+32"), "abc=%+32")
is(builtin_unescape("%u4E16%u754C"), "世界")
})
}
func TestGlobal_escape(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
[
escape("abc"), // "abc"
escape("="), // "%3D"
escape("abc=%+32"), // "abc%3D%25+32"
escape("\u4e16\u754c"), // "%u4E16%u754C"
];
`, "abc,%3D,abc%3D%25+32,%u4E16%u754C")
})
}
func TestGlobal_unescape(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
[
unescape("abc"), // "abc"
unescape("=%3D"), // "=="
unescape("abc%3D%25+32"), // "abc=%+32"
unescape("%u4E16%u754C"), // "世界"
];
`, "abc,==,abc=%+32,世界")
})
}
This diff is collapsed.
package otto
import (
"fmt"
)
type _clone struct {
runtime *_runtime
_object map[*_object]*_object
_objectStash map[*_objectStash]*_objectStash
_dclStash map[*_dclStash]*_dclStash
_fnStash map[*_fnStash]*_fnStash
}
func (in *_runtime) clone() *_runtime {
in.lck.Lock()
defer in.lck.Unlock()
out := &_runtime{
debugger: in.debugger,
random: in.random,
stackLimit: in.stackLimit,
traceLimit: in.traceLimit,
}
clone := _clone{
runtime: out,
_object: make(map[*_object]*_object),
_objectStash: make(map[*_objectStash]*_objectStash),
_dclStash: make(map[*_dclStash]*_dclStash),
_fnStash: make(map[*_fnStash]*_fnStash),
}
globalObject := clone.object(in.globalObject)
out.globalStash = out.newObjectStash(globalObject, nil)
out.globalObject = globalObject
out.global = _global{
clone.object(in.global.Object),
clone.object(in.global.Function),
clone.object(in.global.Array),
clone.object(in.global.String),
clone.object(in.global.Boolean),
clone.object(in.global.Number),
clone.object(in.global.Math),
clone.object(in.global.Date),
clone.object(in.global.RegExp),
clone.object(in.global.Error),
clone.object(in.global.EvalError),
clone.object(in.global.TypeError),
clone.object(in.global.RangeError),
clone.object(in.global.ReferenceError),
clone.object(in.global.SyntaxError),
clone.object(in.global.URIError),
clone.object(in.global.JSON),
clone.object(in.global.ObjectPrototype),
clone.object(in.global.FunctionPrototype),
clone.object(in.global.ArrayPrototype),
clone.object(in.global.StringPrototype),
clone.object(in.global.BooleanPrototype),
clone.object(in.global.NumberPrototype),
clone.object(in.global.DatePrototype),
clone.object(in.global.RegExpPrototype),
clone.object(in.global.ErrorPrototype),
clone.object(in.global.EvalErrorPrototype),
clone.object(in.global.TypeErrorPrototype),
clone.object(in.global.RangeErrorPrototype),
clone.object(in.global.ReferenceErrorPrototype),
clone.object(in.global.SyntaxErrorPrototype),
clone.object(in.global.URIErrorPrototype),
}
out.eval = out.globalObject.property["eval"].value.(Value).value.(*_object)
out.globalObject.prototype = out.global.ObjectPrototype
// Not sure if this is necessary, but give some help to the GC
clone.runtime = nil
clone._object = nil
clone._objectStash = nil
clone._dclStash = nil
clone._fnStash = nil
return out
}
func (clone *_clone) object(in *_object) *_object {
if out, exists := clone._object[in]; exists {
return out
}
out := &_object{}
clone._object[in] = out
return in.objectClass.clone(in, out, clone)
}
func (clone *_clone) dclStash(in *_dclStash) (*_dclStash, bool) {
if out, exists := clone._dclStash[in]; exists {
return out, true
}
out := &_dclStash{}
clone._dclStash[in] = out
return out, false
}
func (clone *_clone) objectStash(in *_objectStash) (*_objectStash, bool) {
if out, exists := clone._objectStash[in]; exists {
return out, true
}
out := &_objectStash{}
clone._objectStash[in] = out
return out, false
}
func (clone *_clone) fnStash(in *_fnStash) (*_fnStash, bool) {
if out, exists := clone._fnStash[in]; exists {
return out, true
}
out := &_fnStash{}
clone._fnStash[in] = out
return out, false
}
func (clone *_clone) value(in Value) Value {
out := in
switch value := in.value.(type) {
case *_object:
out.value = clone.object(value)
}
return out
}
func (clone *_clone) valueArray(in []Value) []Value {
out := make([]Value, len(in))
for index, value := range in {
out[index] = clone.value(value)
}
return out
}
func (clone *_clone) stash(in _stash) _stash {
if in == nil {
return nil
}
return in.clone(clone)
}
func (clone *_clone) property(in _property) _property {
out := in
switch value := in.value.(type) {
case Value:
out.value = clone.value(value)
case _propertyGetSet:
p := _propertyGetSet{}
if value[0] != nil {
p[0] = clone.object(value[0])
}
if value[1] != nil {
p[1] = clone.object(value[1])
}
out.value = p
default:
panic(fmt.Errorf("in.value.(Value) != true; in.value is %T", in.value))
}
return out
}
func (clone *_clone) dclProperty(in _dclProperty) _dclProperty {
out := in
out.value = clone.value(in.value)
return out
}
package otto
import (
"testing"
)
func TestCloneGetterSetter(t *testing.T) {
vm := New()
vm.Run(`var x = Object.create(null, {
x: {
get: function() {},
set: function() {},
},
})`)
vm.Copy()
}
package otto
import (
"github.com/robertkrimen/otto/ast"
"github.com/robertkrimen/otto/file"
)
type _file struct {
name string
src string
base int // This will always be 1 or greater
}
type _compiler struct {
file *file.File
program *ast.Program
}
func (cmpl *_compiler) parse() *_nodeProgram {
if cmpl.program != nil {
cmpl.file = cmpl.program.File
}
return cmpl._parse(cmpl.program)
}
package otto
import (
"strconv"
)
func (self *_runtime) cmpl_evaluate_nodeProgram(node *_nodeProgram, eval bool) Value {
if !eval {
self.enterGlobalScope()
defer func() {
self.leaveScope()
}()
}
self.cmpl_functionDeclaration(node.functionList)
self.cmpl_variableDeclaration(node.varList)
self.scope.frame.file = node.file
return self.cmpl_evaluate_nodeStatementList(node.body)
}
func (self *_runtime) cmpl_call_nodeFunction(function *_object, stash *_fnStash, node *_nodeFunctionLiteral, this Value, argumentList []Value) Value {
indexOfParameterName := make([]string, len(argumentList))
// function(abc, def, ghi)
// indexOfParameterName[0] = "abc"
// indexOfParameterName[1] = "def"
// indexOfParameterName[2] = "ghi"
// ...
argumentsFound := false
for index, name := range node.parameterList {
if name == "arguments" {
argumentsFound = true
}
value := Value{}
if index < len(argumentList) {
value = argumentList[index]
indexOfParameterName[index] = name
}
// strict = false
self.scope.lexical.setValue(name, value, false)
}
if !argumentsFound {
arguments := self.newArgumentsObject(indexOfParameterName, stash, len(argumentList))
arguments.defineProperty("callee", toValue_object(function), 0101, false)
stash.arguments = arguments
// strict = false
self.scope.lexical.setValue("arguments", toValue_object(arguments), false)
for index, _ := range argumentList {
if index < len(node.parameterList) {
continue
}
indexAsString := strconv.FormatInt(int64(index), 10)
arguments.defineProperty(indexAsString, argumentList[index], 0111, false)
}
}
self.cmpl_functionDeclaration(node.functionList)
self.cmpl_variableDeclaration(node.varList)
result := self.cmpl_evaluate_nodeStatement(node.body)
if result.kind == valueResult {
return result
}
return Value{}
}
func (self *_runtime) cmpl_functionDeclaration(list []*_nodeFunctionLiteral) {
executionContext := self.scope
eval := executionContext.eval
stash := executionContext.variable
for _, function := range list {
name := function.name
value := self.cmpl_evaluate_nodeExpression(function)
if !stash.hasBinding(name) {
stash.createBinding(name, eval == true, value)
} else {
// TODO 10.5.5.e
stash.setBinding(name, value, false) // TODO strict
}
}
}
func (self *_runtime) cmpl_variableDeclaration(list []string) {
executionContext := self.scope
eval := executionContext.eval
stash := executionContext.variable
for _, name := range list {
if !stash.hasBinding(name) {
stash.createBinding(name, eval == true, Value{}) // TODO strict?
}
}
}
This diff is collapsed.
This diff is collapsed.
package otto
import (
"testing"
"github.com/robertkrimen/otto/parser"
)
func Test_cmpl(t *testing.T) {
tt(t, func() {
vm := New()
test := func(src string, expect ...interface{}) {
program, err := parser.ParseFile(nil, "", src, 0)
is(err, nil)
{
program := cmpl_parse(program)
value := vm.runtime.cmpl_evaluate_nodeProgram(program, false)
if len(expect) > 0 {
is(value, expect[0])
}
}
}
test(``, Value{})
test(`var abc = 1; abc;`, 1)
test(`var abc = 1 + 1; abc;`, 2)
test(`1 + 2;`, 3)
})
}
func TestParse_cmpl(t *testing.T) {
tt(t, func() {
test := func(src string) {
program, err := parser.ParseFile(nil, "", src, 0)
is(err, nil)
is(cmpl_parse(program), "!=", nil)
}
test(``)
test(`var abc = 1; abc;`)
test(`
function abc() {
return;
}
`)
})
}
package otto
import (
"fmt"
"os"
"strings"
)
func formatForConsole(argumentList []Value) string {
output := []string{}
for _, argument := range argumentList {
output = append(output, fmt.Sprintf("%v", argument))
}
return strings.Join(output, " ")
}
func builtinConsole_log(call FunctionCall) Value {
fmt.Fprintln(os.Stdout, formatForConsole(call.ArgumentList))
return Value{}
}
func builtinConsole_error(call FunctionCall) Value {
fmt.Fprintln(os.Stdout, formatForConsole(call.ArgumentList))
return Value{}
}
// Nothing happens.
func builtinConsole_dir(call FunctionCall) Value {
return Value{}
}
func builtinConsole_time(call FunctionCall) Value {
return Value{}
}
func builtinConsole_timeEnd(call FunctionCall) Value {
return Value{}
}
func builtinConsole_trace(call FunctionCall) Value {
return Value{}
}
func builtinConsole_assert(call FunctionCall) Value {
return Value{}
}
func (runtime *_runtime) newConsole() *_object {
return newConsoleObject(runtime)
}
This diff is collapsed.
// This file was AUTOMATICALLY GENERATED by dbg-import (smuggol) for github.com/robertkrimen/dbg
package otto
import (
Dbg "github.com/robertkrimen/otto/dbg"
)
var dbg, dbgf = Dbg.New()
// This file was AUTOMATICALLY GENERATED by dbg-import (smuggol) from github.com/robertkrimen/dbg
/*
Package dbg is a println/printf/log-debugging utility library.
import (
Dbg "github.com/robertkrimen/dbg"
)
dbg, dbgf := Dbg.New()
dbg("Emit some debug stuff", []byte{120, 121, 122, 122, 121}, math.Pi)
# "2013/01/28 16:50:03 Emit some debug stuff [120 121 122 122 121] 3.141592653589793"
dbgf("With a %s formatting %.2f", "little", math.Pi)
# "2013/01/28 16:51:55 With a little formatting (3.14)"
dbgf("%/fatal//A fatal debug statement: should not be here")
# "A fatal debug statement: should not be here"
# ...and then, os.Exit(1)
dbgf("%/panic//Can also panic %s", "this")
# "Can also panic this"
# ...as a panic, equivalent to: panic("Can also panic this")
dbgf("Any %s arguments without a corresponding %%", "extra", "are treated like arguments to dbg()")
# "2013/01/28 17:14:40 Any extra arguments (without a corresponding %) are treated like arguments to dbg()"
dbgf("%d %d", 1, 2, 3, 4, 5)
# "2013/01/28 17:16:32 Another example: 1 2 3 4 5"
dbgf("%@: Include the function name for a little context (via %s)", "%@")
# "2013... github.com/robertkrimen/dbg.TestSynopsis: Include the function name for a little context (via %@)"
By default, dbg uses log (log.Println, log.Printf, log.Panic, etc.) for output.
However, you can also provide your own output destination by invoking dbg.New with
a customization function:
import (
"bytes"
Dbg "github.com/robertkrimen/dbg"
"os"
)
# dbg to os.Stderr
dbg, dbgf := Dbg.New(func(dbgr *Dbgr) {
dbgr.SetOutput(os.Stderr)
})
# A slightly contrived example:
var buffer bytes.Buffer
dbg, dbgf := New(func(dbgr *Dbgr) {
dbgr.SetOutput(&buffer)
})
*/
package dbg
import (
"bytes"
"fmt"
"io"
"log"
"os"
"regexp"
"runtime"
"strings"
"unicode"
)
type _frmt struct {
ctl string
format string
operandCount int
panic bool
fatal bool
check bool
}
var (
ctlTest = regexp.MustCompile(`^\s*%/`)
ctlScan = regexp.MustCompile(`%?/(panic|fatal|check)(?:\s|$)`)
)
func operandCount(format string) int {
count := 0
end := len(format)
for at := 0; at < end; {
for at < end && format[at] != '%' {
at++
}
at++
if at < end {
if format[at] != '%' && format[at] != '@' {
count++
}
at++
}
}
return count
}
func parseFormat(format string) (frmt _frmt) {
if ctlTest.MatchString(format) {
format = strings.TrimLeftFunc(format, unicode.IsSpace)
index := strings.Index(format, "//")
if index != -1 {
frmt.ctl = format[0:index]
format = format[index+2:] // Skip the second slash via +2 (instead of +1)
} else {
frmt.ctl = format
format = ""
}
for _, tmp := range ctlScan.FindAllStringSubmatch(frmt.ctl, -1) {
for _, value := range tmp[1:] {
switch value {
case "panic":
frmt.panic = true
case "fatal":
frmt.fatal = true
case "check":
frmt.check = true
}
}
}
}
frmt.format = format
frmt.operandCount = operandCount(format)
return
}
type Dbgr struct {
emit _emit
}
type DbgFunction func(values ...interface{})
func NewDbgr() *Dbgr {
self := &Dbgr{}
return self
}
/*
New will create and return a pair of debugging functions. You can customize where
they output to by passing in an (optional) customization function:
import (
Dbg "github.com/robertkrimen/dbg"
"os"
)
# dbg to os.Stderr
dbg, dbgf := Dbg.New(func(dbgr *Dbgr) {
dbgr.SetOutput(os.Stderr)
})
*/
func New(options ...interface{}) (dbg DbgFunction, dbgf DbgFunction) {
dbgr := NewDbgr()
if len(options) > 0 {
if fn, ok := options[0].(func(*Dbgr)); ok {
fn(dbgr)
}
}
return dbgr.DbgDbgf()
}
func (self Dbgr) Dbg(values ...interface{}) {
self.getEmit().emit(_frmt{}, "", values...)
}
func (self Dbgr) Dbgf(values ...interface{}) {
self.dbgf(values...)
}
func (self Dbgr) DbgDbgf() (dbg DbgFunction, dbgf DbgFunction) {
dbg = func(vl ...interface{}) {
self.Dbg(vl...)
}
dbgf = func(vl ...interface{}) {
self.dbgf(vl...)
}
return dbg, dbgf // Redundant, but...
}
func (self Dbgr) dbgf(values ...interface{}) {
var frmt _frmt
if len(values) > 0 {
tmp := fmt.Sprint(values[0])
frmt = parseFormat(tmp)
values = values[1:]
}
buffer_f := bytes.Buffer{}
format := frmt.format
end := len(format)
for at := 0; at < end; {
last := at
for at < end && format[at] != '%' {
at++
}
if at > last {
buffer_f.WriteString(format[last:at])
}
if at >= end {
break
}
// format[at] == '%'
at++
// format[at] == ?
if format[at] == '@' {
depth := 2
pc, _, _, _ := runtime.Caller(depth)
name := runtime.FuncForPC(pc).Name()
buffer_f.WriteString(name)
} else {
buffer_f.WriteString(format[at-1 : at+1])
}
at++
}
//values_f := append([]interface{}{}, values[0:frmt.operandCount]...)
values_f := values[0:frmt.operandCount]
values_dbg := values[frmt.operandCount:]
if len(values_dbg) > 0 {
// Adjust frmt.format:
// (%v instead of %s because: frmt.check)
{
tmp := format
if len(tmp) > 0 {
if unicode.IsSpace(rune(tmp[len(tmp)-1])) {
buffer_f.WriteString("%v")
} else {
buffer_f.WriteString(" %v")
}
} else if frmt.check {
// Performing a check, so no output
} else {
buffer_f.WriteString("%v")
}
}
// Adjust values_f:
if !frmt.check {
tmp := []string{}
for _, value := range values_dbg {
tmp = append(tmp, fmt.Sprintf("%v", value))
}
// First, make a copy of values_f, so we avoid overwriting values_dbg when appending
values_f = append([]interface{}{}, values_f...)
values_f = append(values_f, strings.Join(tmp, " "))
}
}
format = buffer_f.String()
if frmt.check {
// We do not actually emit to the log, but panic if
// a non-nil value is detected (e.g. a non-nil error)
for _, value := range values_dbg {
if value != nil {
if format == "" {
panic(value)
} else {
panic(fmt.Sprintf(format, append(values_f, value)...))
}
}
}
} else {
self.getEmit().emit(frmt, format, values_f...)
}
}
// Idiot-proof &Dbgr{}, etc.
func (self *Dbgr) getEmit() _emit {
if self.emit == nil {
self.emit = standardEmit()
}
return self.emit
}
// SetOutput will accept the following as a destination for output:
//
// *log.Logger Print*/Panic*/Fatal* of the logger
// io.Writer -
// nil Reset to the default output (os.Stderr)
// "log" Print*/Panic*/Fatal* via the "log" package
//
func (self *Dbgr) SetOutput(output interface{}) {
if output == nil {
self.emit = standardEmit()
return
}
switch output := output.(type) {
case *log.Logger:
self.emit = _emitLogger{
logger: output,
}
return
case io.Writer:
self.emit = _emitWriter{
writer: output,
}
return
case string:
if output == "log" {
self.emit = _emitLog{}
return
}
}
panic(output)
}
// ======== //
// = emit = //
// ======== //
func standardEmit() _emit {
return _emitWriter{
writer: os.Stderr,
}
}
func ln(tmp string) string {
length := len(tmp)
if length > 0 && tmp[length-1] != '\n' {
return tmp + "\n"
}
return tmp
}
type _emit interface {
emit(_frmt, string, ...interface{})
}
type _emitWriter struct {
writer io.Writer
}
func (self _emitWriter) emit(frmt _frmt, format string, values ...interface{}) {
if format == "" {
fmt.Fprintln(self.writer, values...)
} else {
if frmt.panic {
panic(fmt.Sprintf(format, values...))
}
fmt.Fprintf(self.writer, ln(format), values...)
if frmt.fatal {
os.Exit(1)
}
}
}
type _emitLogger struct {
logger *log.Logger
}
func (self _emitLogger) emit(frmt _frmt, format string, values ...interface{}) {
if format == "" {
self.logger.Println(values...)
} else {
if frmt.panic {
self.logger.Panicf(format, values...)
} else if frmt.fatal {
self.logger.Fatalf(format, values...)
} else {
self.logger.Printf(format, values...)
}
}
}
type _emitLog struct {
}
func (self _emitLog) emit(frmt _frmt, format string, values ...interface{}) {
if format == "" {
log.Println(values...)
} else {
if frmt.panic {
log.Panicf(format, values...)
} else if frmt.fatal {
log.Fatalf(format, values...)
} else {
log.Printf(format, values...)
}
}
}
package otto
import (
"fmt"
)
func ExampleSynopsis() {
vm := New()
vm.Run(`
abc = 2 + 2;
console.log("The value of abc is " + abc); // 4
`)
value, _ := vm.Get("abc")
{
value, _ := value.ToInteger()
fmt.Println(value)
}
vm.Set("def", 11)
vm.Run(`
console.log("The value of def is " + def);
`)
vm.Set("xyzzy", "Nothing happens.")
vm.Run(`
console.log(xyzzy.length);
`)
value, _ = vm.Run("xyzzy.length")
{
value, _ := value.ToInteger()
fmt.Println(value)
}
value, err := vm.Run("abcdefghijlmnopqrstuvwxyz.length")
fmt.Println(value)
fmt.Println(err)
vm.Set("sayHello", func(call FunctionCall) Value {
fmt.Printf("Hello, %s.\n", call.Argument(0).String())
return UndefinedValue()
})
vm.Set("twoPlus", func(call FunctionCall) Value {
right, _ := call.Argument(0).ToInteger()
result, _ := vm.ToValue(2 + right)
return result
})
value, _ = vm.Run(`
sayHello("Xyzzy");
sayHello();
result = twoPlus(2.0);
`)
fmt.Println(value)
// Output:
// The value of abc is 4
// 4
// The value of def is 11
// 16
// 16
// undefined
// ReferenceError: 'abcdefghijlmnopqrstuvwxyz' is not defined
// Hello, Xyzzy.
// Hello, undefined.
// 4
}
func ExampleConsole() {
vm := New()
console := map[string]interface{}{
"log": func(call FunctionCall) Value {
fmt.Println("console.log:", formatForConsole(call.ArgumentList))
return UndefinedValue()
},
}
err := vm.Set("console", console)
value, err := vm.Run(`
console.log("Hello, World.");
`)
fmt.Println(value)
fmt.Println(err)
// Output:
// console.log: Hello, World.
// undefined
// <nil>
}
package otto
import (
"errors"
"fmt"
"github.com/robertkrimen/otto/file"
)
type _exception struct {
value interface{}
}
func newException(value interface{}) *_exception {
return &_exception{
value: value,
}
}
func (self *_exception) eject() interface{} {
value := self.value
self.value = nil // Prevent Go from holding on to the value, whatever it is
return value
}
type _error struct {
name string
message string
trace []_frame
offset int
}
func (err _error) format() string {
if len(err.name) == 0 {
return err.message
}
if len(err.message) == 0 {
return err.name
}
return fmt.Sprintf("%s: %s", err.name, err.message)
}
func (err _error) formatWithStack() string {
str := err.format() + "\n"
for _, frame := range err.trace {
str += " at " + frame.location() + "\n"
}
return str
}
type _frame struct {
native bool
nativeFile string
nativeLine int
file *file.File
offset int
callee string
fn interface{}
}
var (
nativeFrame = _frame{}
)
type _at int
func (fr _frame) location() string {
str := "<unknown>"
switch {
case fr.native:
str = "<native code>"
if fr.nativeFile != "" && fr.nativeLine != 0 {
str = fmt.Sprintf("%s:%d", fr.nativeFile, fr.nativeLine)
}
case fr.file != nil:
if p := fr.file.Position(file.Idx(fr.offset)); p != nil {
path, line, column := p.Filename, p.Line, p.Column
if path == "" {
path = "<anonymous>"
}
str = fmt.Sprintf("%s:%d:%d", path, line, column)
}
}
if fr.callee != "" {
str = fmt.Sprintf("%s (%s)", fr.callee, str)
}
return str
}
// An Error represents a runtime error, e.g. a TypeError, a ReferenceError, etc.
type Error struct {
_error
}
// Error returns a description of the error
//
// TypeError: 'def' is not a function
//
func (err Error) Error() string {
return err.format()
}
// String returns a description of the error and a trace of where the
// error occurred.
//
// TypeError: 'def' is not a function
// at xyz (<anonymous>:3:9)
// at <anonymous>:7:1/
//
func (err Error) String() string {
return err.formatWithStack()
}
func (err _error) describe(format string, in ...interface{}) string {
return fmt.Sprintf(format, in...)
}
func (self _error) messageValue() Value {
if self.message == "" {
return Value{}
}
return toValue_string(self.message)
}
func (rt *_runtime) typeErrorResult(throw bool) bool {
if throw {
panic(rt.panicTypeError())
}
return false
}
func newError(rt *_runtime, name string, stackFramesToPop int, in ...interface{}) _error {
err := _error{
name: name,
offset: -1,
}
description := ""
length := len(in)
if rt != nil && rt.scope != nil {
scope := rt.scope
for i := 0; i < stackFramesToPop; i++ {
if scope.outer != nil {
scope = scope.outer
}
}
frame := scope.frame
if length > 0 {
if at, ok := in[length-1].(_at); ok {
in = in[0 : length-1]
if scope != nil {
frame.offset = int(at)
}
length--
}
if length > 0 {
description, in = in[0].(string), in[1:]
}
}
limit := rt.traceLimit
err.trace = append(err.trace, frame)
if scope != nil {
for scope = scope.outer; scope != nil; scope = scope.outer {
if limit--; limit == 0 {
break
}
if scope.frame.offset >= 0 {
err.trace = append(err.trace, scope.frame)
}
}
}
} else {
if length > 0 {
description, in = in[0].(string), in[1:]
}
}
err.message = err.describe(description, in...)
return err
}
func (rt *_runtime) panicTypeError(argumentList ...interface{}) *_exception {
return &_exception{
value: newError(rt, "TypeError", 0, argumentList...),
}
}
func (rt *_runtime) panicReferenceError(argumentList ...interface{}) *_exception {
return &_exception{
value: newError(rt, "ReferenceError", 0, argumentList...),
}
}
func (rt *_runtime) panicURIError(argumentList ...interface{}) *_exception {
return &_exception{
value: newError(rt, "URIError", 0, argumentList...),
}
}
func (rt *_runtime) panicSyntaxError(argumentList ...interface{}) *_exception {
return &_exception{
value: newError(rt, "SyntaxError", 0, argumentList...),
}
}
func (rt *_runtime) panicRangeError(argumentList ...interface{}) *_exception {
return &_exception{
value: newError(rt, "RangeError", 0, argumentList...),
}
}
func catchPanic(function func()) (err error) {
defer func() {
if caught := recover(); caught != nil {
if exception, ok := caught.(*_exception); ok {
caught = exception.eject()
}
switch caught := caught.(type) {
case *Error:
err = caught
return
case _error:
err = &Error{caught}
return
case Value:
if vl := caught._object(); vl != nil {
switch vl := vl.value.(type) {
case _error:
err = &Error{vl}
return
}
}
err = errors.New(caught.string())
return
}
panic(caught)
}
}()
function()
return nil
}
package otto
import (
"testing"
)
// this is its own file because the tests in it rely on the line numbers of
// some of the functions defined here. putting it in with the rest of the
// tests would probably be annoying.
func TestErrorContextNative(t *testing.T) {
tt(t, func() {
vm := New()
vm.Set("N", func(c FunctionCall) Value {
v, err := c.Argument(0).Call(NullValue())
if err != nil {
panic(err)
}
return v
})
s, _ := vm.Compile("test.js", `
function F() { throw new Error('wow'); }
function G() { return N(F); }
`)
vm.Run(s)
f1, _ := vm.Get("G")
_, err := f1.Call(NullValue())
err1 := err.(*Error)
is(err1.message, "wow")
is(len(err1.trace), 3)
is(err1.trace[0].location(), "F (test.js:2:29)")
is(err1.trace[1].location(), "github.com/robertkrimen/otto.TestErrorContextNative.func1.1 (error_native_test.go:15)")
is(err1.trace[2].location(), "G (test.js:3:26)")
})
}
package otto
import (
"testing"
)
func TestError(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
[ Error.prototype.name, Error.prototype.message, Error.prototype.hasOwnProperty("message") ];
`, "Error,,true")
})
}
func TestError_instanceof(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`(new TypeError()) instanceof Error`, true)
})
}
func TestPanicValue(t *testing.T) {
tt(t, func() {
test, vm := test()
vm.Set("abc", func(call FunctionCall) Value {
value, err := call.Otto.Run(`({ def: 3.14159 })`)
is(err, nil)
panic(value)
})
test(`
try {
abc();
}
catch (err) {
error = err;
}
[ error instanceof Error, error.message, error.def ];
`, "false,,3.14159")
})
}
func Test_catchPanic(t *testing.T) {
tt(t, func() {
vm := New()
_, err := vm.Run(`
A syntax error that
does not define
var;
abc;
`)
is(err, "!=", nil)
_, err = vm.Call(`abc.def`, nil)
is(err, "!=", nil)
})
}
func TestErrorContext(t *testing.T) {
tt(t, func() {
vm := New()
_, err := vm.Run(`
undefined();
`)
{
err := err.(*Error)
is(err.message, "'undefined' is not a function")
is(len(err.trace), 1)
is(err.trace[0].location(), "<anonymous>:2:13")
}
_, err = vm.Run(`
({}).abc();
`)
{
err := err.(*Error)
is(err.message, "'abc' is not a function")
is(len(err.trace), 1)
is(err.trace[0].location(), "<anonymous>:2:14")
}
_, err = vm.Run(`
("abc").abc();
`)
{
err := err.(*Error)
is(err.message, "'abc' is not a function")
is(len(err.trace), 1)
is(err.trace[0].location(), "<anonymous>:2:14")
}
_, err = vm.Run(`
var ghi = "ghi";
ghi();
`)
{
err := err.(*Error)
is(err.message, "'ghi' is not a function")
is(len(err.trace), 1)
is(err.trace[0].location(), "<anonymous>:3:13")
}
_, err = vm.Run(`
function def() {
undefined();
}
function abc() {
def();
}
abc();
`)
{
err := err.(*Error)
is(err.message, "'undefined' is not a function")
is(len(err.trace), 3)
is(err.trace[0].location(), "def (<anonymous>:3:17)")
is(err.trace[1].location(), "abc (<anonymous>:6:17)")
is(err.trace[2].location(), "<anonymous>:8:13")
}
_, err = vm.Run(`
function abc() {
xyz();
}
abc();
`)
{
err := err.(*Error)
is(err.message, "'xyz' is not defined")
is(len(err.trace), 2)
is(err.trace[0].location(), "abc (<anonymous>:3:17)")
is(err.trace[1].location(), "<anonymous>:5:13")
}
_, err = vm.Run(`
mno + 1;
`)
{
err := err.(*Error)
is(err.message, "'mno' is not defined")
is(len(err.trace), 1)
is(err.trace[0].location(), "<anonymous>:2:13")
}
_, err = vm.Run(`
eval("xyz();");
`)
{
err := err.(*Error)
is(err.message, "'xyz' is not defined")
is(len(err.trace), 1)
is(err.trace[0].location(), "<anonymous>:1:1")
}
_, err = vm.Run(`
xyzzy = "Nothing happens."
eval("xyzzy();");
`)
{
err := err.(*Error)
is(err.message, "'xyzzy' is not a function")
is(len(err.trace), 1)
is(err.trace[0].location(), "<anonymous>:1:1")
}
_, err = vm.Run(`
throw Error("xyzzy");
`)
{
err := err.(*Error)
is(err.message, "xyzzy")
is(len(err.trace), 1)
is(err.trace[0].location(), "<anonymous>:2:19")
}
_, err = vm.Run(`
throw new Error("xyzzy");
`)
{
err := err.(*Error)
is(err.message, "xyzzy")
is(len(err.trace), 1)
is(err.trace[0].location(), "<anonymous>:2:23")
}
script1, err := vm.Compile("file1.js",
`function A() {
throw new Error("test");
}
function C() {
var o = null;
o.prop = 1;
}
`)
is(err, nil)
_, err = vm.Run(script1)
is(err, nil)
script2, err := vm.Compile("file2.js",
`function B() {
A()
}
`)
is(err, nil)
_, err = vm.Run(script2)
is(err, nil)
script3, err := vm.Compile("file3.js", "B()")
is(err, nil)
_, err = vm.Run(script3)
{
err := err.(*Error)
is(err.message, "test")
is(len(err.trace), 3)
is(err.trace[0].location(), "A (file1.js:2:15)")
is(err.trace[1].location(), "B (file2.js:2:5)")
is(err.trace[2].location(), "file3.js:1:1")
}
{
f, _ := vm.Get("B")
_, err := f.Call(UndefinedValue())
err1 := err.(*Error)
is(err1.message, "test")
is(len(err1.trace), 2)
is(err1.trace[0].location(), "A (file1.js:2:15)")
is(err1.trace[1].location(), "B (file2.js:2:5)")
}
{
f, _ := vm.Get("C")
_, err := f.Call(UndefinedValue())
err1 := err.(*Error)
is(err1.message, "Cannot access member 'prop' of null")
is(len(err1.trace), 1)
is(err1.trace[0].location(), "C (file1.js:7:5)")
}
})
}
func TestMakeCustomErrorReturn(t *testing.T) {
tt(t, func() {
vm := New()
vm.Set("A", func(c FunctionCall) Value {
return vm.MakeCustomError("CarrotError", "carrots is life, carrots is love")
})
s, _ := vm.Compile("test.js", `
function B() { return A(); }
function C() { return B(); }
function D() { return C(); }
`)
if _, err := vm.Run(s); err != nil {
panic(err)
}
v, err := vm.Call("D", nil)
if err != nil {
panic(err)
}
is(v.Class(), "Error")
name, err := v.Object().Get("name")
if err != nil {
panic(err)
}
is(name.String(), "CarrotError")
message, err := v.Object().Get("message")
if err != nil {
panic(err)
}
is(message.String(), "carrots is life, carrots is love")
str, err := v.Object().Call("toString")
if err != nil {
panic(err)
}
is(str, "CarrotError: carrots is life, carrots is love")
i, err := v.Export()
if err != nil {
panic(err)
}
t.Logf("%#v\n", i)
})
}
func TestMakeCustomError(t *testing.T) {
tt(t, func() {
vm := New()
vm.Set("A", func(c FunctionCall) Value {
panic(vm.MakeCustomError("CarrotError", "carrots is life, carrots is love"))
return UndefinedValue()
})
s, _ := vm.Compile("test.js", `
function B() { A(); }
function C() { B(); }
function D() { C(); }
`)
if _, err := vm.Run(s); err != nil {
panic(err)
}
_, err := vm.Call("D", nil)
if err == nil {
panic("error should not be nil")
}
is(err.Error(), "CarrotError: carrots is life, carrots is love")
er := err.(*Error)
is(er.name, "CarrotError")
is(er.message, "carrots is life, carrots is love")
})
}
func TestMakeCustomErrorFreshVM(t *testing.T) {
tt(t, func() {
vm := New()
e := vm.MakeCustomError("CarrotError", "carrots is life, carrots is love")
str, err := e.ToString()
if err != nil {
panic(err)
}
is(str, "CarrotError: carrots is life, carrots is love")
})
}
func TestMakeTypeError(t *testing.T) {
tt(t, func() {
vm := New()
vm.Set("A", func(c FunctionCall) Value {
panic(vm.MakeTypeError("these aren't my glasses"))
return UndefinedValue()
})
s, _ := vm.Compile("test.js", `
function B() { A(); }
function C() { B(); }
function D() { C(); }
`)
if _, err := vm.Run(s); err != nil {
panic(err)
}
_, err := vm.Call("D", nil)
if err == nil {
panic("error should not be nil")
}
is(err.Error(), "TypeError: these aren't my glasses")
er := err.(*Error)
is(er.name, "TypeError")
is(er.message, "these aren't my glasses")
})
}
func TestMakeRangeError(t *testing.T) {
tt(t, func() {
vm := New()
vm.Set("A", func(c FunctionCall) Value {
panic(vm.MakeRangeError("too many"))
return UndefinedValue()
})
s, _ := vm.Compile("test.js", `
function B() { A(); }
function C() { B(); }
function D() { C(); }
`)
if _, err := vm.Run(s); err != nil {
panic(err)
}
_, err := vm.Call("D", nil)
if err == nil {
panic("error should not be nil")
}
is(err.Error(), "RangeError: too many")
er := err.(*Error)
is(er.name, "RangeError")
is(er.message, "too many")
})
}
func TestMakeSyntaxError(t *testing.T) {
tt(t, func() {
vm := New()
vm.Set("A", func(c FunctionCall) Value {
panic(vm.MakeSyntaxError("i think you meant \"you're\""))
return UndefinedValue()
})
s, _ := vm.Compile("test.js", `
function B() { A(); }
function C() { B(); }
function D() { C(); }
`)
if _, err := vm.Run(s); err != nil {
panic(err)
}
_, err := vm.Call("D", nil)
if err == nil {
panic("error should not be nil")
}
is(err.Error(), "SyntaxError: i think you meant \"you're\"")
er := err.(*Error)
is(er.name, "SyntaxError")
is(er.message, "i think you meant \"you're\"")
})
}
func TestErrorStackProperty(t *testing.T) {
tt(t, func() {
vm := New()
s, err := vm.Compile("test.js", `
function A() { throw new TypeError('uh oh'); }
function B() { return A(); }
function C() { return B(); }
var s = null;
try { C(); } catch (e) { s = e.stack; }
s;
`)
if err != nil {
panic(err)
}
v, err := vm.Run(s)
if err != nil {
panic(err)
}
is(v.String(), "TypeError: uh oh\n at A (test.js:2:29)\n at B (test.js:3:26)\n at C (test.js:4:26)\n at test.js:8:10\n")
})
}
package otto
import (
"fmt"
"math"
"strings"
"github.com/robertkrimen/otto/token"
)
func (self *_runtime) evaluateMultiply(left float64, right float64) Value {
// TODO 11.5.1
return Value{}
}
func (self *_runtime) evaluateDivide(left float64, right float64) Value {
if math.IsNaN(left) || math.IsNaN(right) {
return NaNValue()
}
if math.IsInf(left, 0) && math.IsInf(right, 0) {
return NaNValue()
}
if left == 0 && right == 0 {
return NaNValue()
}
if math.IsInf(left, 0) {
if math.Signbit(left) == math.Signbit(right) {
return positiveInfinityValue()
} else {
return negativeInfinityValue()
}
}
if math.IsInf(right, 0) {
if math.Signbit(left) == math.Signbit(right) {
return positiveZeroValue()
} else {
return negativeZeroValue()
}
}
if right == 0 {
if math.Signbit(left) == math.Signbit(right) {
return positiveInfinityValue()
} else {
return negativeInfinityValue()
}
}
return toValue_float64(left / right)
}
func (self *_runtime) evaluateModulo(left float64, right float64) Value {
// TODO 11.5.3
return Value{}
}
func (self *_runtime) calculateBinaryExpression(operator token.Token, left Value, right Value) Value {
leftValue := left.resolve()
switch operator {
// Additive
case token.PLUS:
leftValue = toPrimitive(leftValue)
rightValue := right.resolve()
rightValue = toPrimitive(rightValue)
if leftValue.IsString() || rightValue.IsString() {
return toValue_string(strings.Join([]string{leftValue.string(), rightValue.string()}, ""))
} else {
return toValue_float64(leftValue.float64() + rightValue.float64())
}
case token.MINUS:
rightValue := right.resolve()
return toValue_float64(leftValue.float64() - rightValue.float64())
// Multiplicative
case token.MULTIPLY:
rightValue := right.resolve()
return toValue_float64(leftValue.float64() * rightValue.float64())
case token.SLASH:
rightValue := right.resolve()
return self.evaluateDivide(leftValue.float64(), rightValue.float64())
case token.REMAINDER:
rightValue := right.resolve()
return toValue_float64(math.Mod(leftValue.float64(), rightValue.float64()))
// Logical
case token.LOGICAL_AND:
left := leftValue.bool()
if !left {
return falseValue
}
return toValue_bool(right.resolve().bool())
case token.LOGICAL_OR:
left := leftValue.bool()
if left {
return trueValue
}
return toValue_bool(right.resolve().bool())
// Bitwise
case token.AND:
rightValue := right.resolve()
return toValue_int32(toInt32(leftValue) & toInt32(rightValue))
case token.OR:
rightValue := right.resolve()
return toValue_int32(toInt32(leftValue) | toInt32(rightValue))
case token.EXCLUSIVE_OR:
rightValue := right.resolve()
return toValue_int32(toInt32(leftValue) ^ toInt32(rightValue))
// Shift
// (Masking of 0x1f is to restrict the shift to a maximum of 31 places)
case token.SHIFT_LEFT:
rightValue := right.resolve()
return toValue_int32(toInt32(leftValue) << (toUint32(rightValue) & 0x1f))
case token.SHIFT_RIGHT:
rightValue := right.resolve()
return toValue_int32(toInt32(leftValue) >> (toUint32(rightValue) & 0x1f))
case token.UNSIGNED_SHIFT_RIGHT:
rightValue := right.resolve()
// Shifting an unsigned integer is a logical shift
return toValue_uint32(toUint32(leftValue) >> (toUint32(rightValue) & 0x1f))
case token.INSTANCEOF:
rightValue := right.resolve()
if !rightValue.IsObject() {
panic(self.panicTypeError("Expecting a function in instanceof check, but got: %v", rightValue))
}
return toValue_bool(rightValue._object().hasInstance(leftValue))
case token.IN:
rightValue := right.resolve()
if !rightValue.IsObject() {
panic(self.panicTypeError())
}
return toValue_bool(rightValue._object().hasProperty(leftValue.string()))
}
panic(hereBeDragons(operator))
}
func valueKindDispatchKey(left _valueKind, right _valueKind) int {
return (int(left) << 2) + int(right)
}
var equalDispatch map[int](func(Value, Value) bool) = makeEqualDispatch()
func makeEqualDispatch() map[int](func(Value, Value) bool) {
key := valueKindDispatchKey
return map[int](func(Value, Value) bool){
key(valueNumber, valueObject): func(x Value, y Value) bool { return x.float64() == y.float64() },
key(valueString, valueObject): func(x Value, y Value) bool { return x.float64() == y.float64() },
key(valueObject, valueNumber): func(x Value, y Value) bool { return x.float64() == y.float64() },
key(valueObject, valueString): func(x Value, y Value) bool { return x.float64() == y.float64() },
}
}
type _lessThanResult int
const (
lessThanFalse _lessThanResult = iota
lessThanTrue
lessThanUndefined
)
func calculateLessThan(left Value, right Value, leftFirst bool) _lessThanResult {
x := Value{}
y := x
if leftFirst {
x = toNumberPrimitive(left)
y = toNumberPrimitive(right)
} else {
y = toNumberPrimitive(right)
x = toNumberPrimitive(left)
}
result := false
if x.kind != valueString || y.kind != valueString {
x, y := x.float64(), y.float64()
if math.IsNaN(x) || math.IsNaN(y) {
return lessThanUndefined
}
result = x < y
} else {
x, y := x.string(), y.string()
result = x < y
}
if result {
return lessThanTrue
}
return lessThanFalse
}
// FIXME Probably a map is not the most efficient way to do this
var lessThanTable [4](map[_lessThanResult]bool) = [4](map[_lessThanResult]bool){
// <
map[_lessThanResult]bool{
lessThanFalse: false,
lessThanTrue: true,
lessThanUndefined: false,
},
// >
map[_lessThanResult]bool{
lessThanFalse: false,
lessThanTrue: true,
lessThanUndefined: false,
},
// <=
map[_lessThanResult]bool{
lessThanFalse: true,
lessThanTrue: false,
lessThanUndefined: false,
},
// >=
map[_lessThanResult]bool{
lessThanFalse: true,
lessThanTrue: false,
lessThanUndefined: false,
},
}
func (self *_runtime) calculateComparison(comparator token.Token, left Value, right Value) bool {
// FIXME Use strictEqualityComparison?
// TODO This might be redundant now (with regards to evaluateComparison)
x := left.resolve()
y := right.resolve()
kindEqualKind := false
result := true
negate := false
switch comparator {
case token.LESS:
result = lessThanTable[0][calculateLessThan(x, y, true)]
case token.GREATER:
result = lessThanTable[1][calculateLessThan(y, x, false)]
case token.LESS_OR_EQUAL:
result = lessThanTable[2][calculateLessThan(y, x, false)]
case token.GREATER_OR_EQUAL:
result = lessThanTable[3][calculateLessThan(x, y, true)]
case token.STRICT_NOT_EQUAL:
negate = true
fallthrough
case token.STRICT_EQUAL:
if x.kind != y.kind {
result = false
} else {
kindEqualKind = true
}
case token.NOT_EQUAL:
negate = true
fallthrough
case token.EQUAL:
if x.kind == y.kind {
kindEqualKind = true
} else if x.kind <= valueNull && y.kind <= valueNull {
result = true
} else if x.kind <= valueNull || y.kind <= valueNull {
result = false
} else if x.kind <= valueString && y.kind <= valueString {
result = x.float64() == y.float64()
} else if x.kind == valueBoolean {
result = self.calculateComparison(token.EQUAL, toValue_float64(x.float64()), y)
} else if y.kind == valueBoolean {
result = self.calculateComparison(token.EQUAL, x, toValue_float64(y.float64()))
} else if x.kind == valueObject {
result = self.calculateComparison(token.EQUAL, toPrimitive(x), y)
} else if y.kind == valueObject {
result = self.calculateComparison(token.EQUAL, x, toPrimitive(y))
} else {
panic(hereBeDragons("Unable to test for equality: %v ==? %v", x, y))
}
default:
panic(fmt.Errorf("Unknown comparator %s", comparator.String()))
}
if kindEqualKind {
switch x.kind {
case valueUndefined, valueNull:
result = true
case valueNumber:
x := x.float64()
y := y.float64()
if math.IsNaN(x) || math.IsNaN(y) {
result = false
} else {
result = x == y
}
case valueString:
result = x.string() == y.string()
case valueBoolean:
result = x.bool() == y.bool()
case valueObject:
result = x._object() == y._object()
default:
goto ERROR
}
}
if negate {
result = !result
}
return result
ERROR:
panic(hereBeDragons("%v (%v) %s %v (%v)", x, x.kind, comparator, y, y.kind))
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment