2015-02-16 13:28:33 +00:00
|
|
|
package otto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-03-20 12:22:01 +00:00
|
|
|
"fmt"
|
2015-02-16 13:28:33 +00:00
|
|
|
"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{}
|
2015-03-20 12:22:01 +00:00
|
|
|
err := json.Unmarshal([]byte(call.Argument(0).string()), &root)
|
2015-02-16 13:28:33 +00:00
|
|
|
if err != nil {
|
2015-03-20 12:22:01 +00:00
|
|
|
panic(call.runtime.panicSyntaxError(err.Error()))
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
value, exists := builtinJSON_parseWalk(ctx, root)
|
|
|
|
if !exists {
|
2015-03-20 12:22:01 +00:00
|
|
|
value = Value{}
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
return ctx.reviver.call(ctx.call.runtime, toValue_object(holder), name, value)
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func builtinJSON_parseWalk(ctx _builtinJSON_parseContext, rawValue interface{}) (Value, bool) {
|
|
|
|
switch value := rawValue.(type) {
|
|
|
|
case nil:
|
2015-03-20 12:22:01 +00:00
|
|
|
return nullValue, true
|
2015-02-16 13:28:33 +00:00
|
|
|
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)))
|
2015-03-20 12:22:01 +00:00
|
|
|
switch value.kind {
|
2015-02-16 13:28:33 +00:00
|
|
|
case valueObject:
|
|
|
|
switch value.value.(*_object).class {
|
|
|
|
case "String":
|
|
|
|
case "Number":
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case valueString:
|
|
|
|
case valueNumber:
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
name := value.string()
|
2015-02-16 13:28:33 +00:00
|
|
|
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 {
|
2015-03-20 12:22:01 +00:00
|
|
|
if spaceValue.kind == valueObject {
|
2015-02-16 13:28:33 +00:00
|
|
|
switch spaceValue.value.(*_object).class {
|
|
|
|
case "String":
|
2015-03-20 12:22:01 +00:00
|
|
|
spaceValue = toValue_string(spaceValue.string())
|
2015-02-16 13:28:33 +00:00
|
|
|
case "Number":
|
2015-03-20 12:22:01 +00:00
|
|
|
spaceValue = spaceValue.numberValue()
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
switch spaceValue.kind {
|
2015-02-16 13:28:33 +00:00
|
|
|
case valueString:
|
2015-03-20 12:22:01 +00:00
|
|
|
value := spaceValue.string()
|
2015-02-16 13:28:33 +00:00
|
|
|
if len(value) > 10 {
|
|
|
|
ctx.gap = value[0:10]
|
|
|
|
} else {
|
|
|
|
ctx.gap = value
|
|
|
|
}
|
|
|
|
case valueNumber:
|
2015-03-20 12:22:01 +00:00
|
|
|
value := spaceValue.number().int64
|
2015-02-16 13:28:33 +00:00
|
|
|
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 {
|
2015-03-20 12:22:01 +00:00
|
|
|
return Value{}
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
valueJSON, err := json.Marshal(value)
|
|
|
|
if err != nil {
|
2015-03-20 12:22:01 +00:00
|
|
|
panic(call.runtime.panicTypeError(err.Error()))
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
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() {
|
2015-03-20 12:22:01 +00:00
|
|
|
value = toJSON.call(ctx.call.runtime, value, key)
|
2015-02-16 13:28:33 +00:00
|
|
|
} 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 {
|
2015-03-20 12:22:01 +00:00
|
|
|
value = (*ctx.replacerFunction).call(ctx.call.runtime, toValue_object(holder), key, value)
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 12:22:01 +00:00
|
|
|
if value.kind == valueObject {
|
2015-02-16 13:28:33 +00:00
|
|
|
switch value.value.(*_object).class {
|
|
|
|
case "Boolean":
|
|
|
|
value = value._object().value.(Value)
|
|
|
|
case "String":
|
2015-03-20 12:22:01 +00:00
|
|
|
value = toValue_string(value.string())
|
2015-02-16 13:28:33 +00:00
|
|
|
case "Number":
|
2015-03-20 12:22:01 +00:00
|
|
|
value = value.numberValue()
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 12:22:01 +00:00
|
|
|
switch value.kind {
|
2015-02-16 13:28:33 +00:00
|
|
|
case valueBoolean:
|
2015-03-20 12:22:01 +00:00
|
|
|
return value.bool(), true
|
2015-02-16 13:28:33 +00:00
|
|
|
case valueString:
|
2015-03-20 12:22:01 +00:00
|
|
|
return value.string(), true
|
2015-02-16 13:28:33 +00:00
|
|
|
case valueNumber:
|
2015-03-20 12:22:01 +00:00
|
|
|
integer := value.number()
|
|
|
|
switch integer.kind {
|
|
|
|
case numberInteger:
|
|
|
|
return integer.int64, true
|
|
|
|
case numberFloat:
|
|
|
|
return integer.float64, true
|
|
|
|
default:
|
2015-02-16 13:28:33 +00:00
|
|
|
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 {
|
2015-03-20 12:22:01 +00:00
|
|
|
panic(ctx.call.runtime.panicTypeError("Converting circular structure to JSON"))
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.stack = append(ctx.stack, value)
|
|
|
|
defer func() { ctx.stack = ctx.stack[:len(ctx.stack)-1] }()
|
|
|
|
}
|
|
|
|
if isArray(holder) {
|
2015-03-20 12:22:01 +00:00
|
|
|
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)))
|
|
|
|
}
|
2015-02-16 13:28:33 +00:00
|
|
|
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
|
|
|
|
}
|