소스 검색

Merge branch 'master' into es6

# Conflicts:
#	builtin_json.go
Dmitry Panov 5 년 전
부모
커밋
3e17424602
2개의 변경된 파일16개의 추가작업 그리고 2개의 파일을 삭제
  1. 2 2
      builtin_json.go
  2. 14 0
      builtin_json_test.go

+ 2 - 2
builtin_json.go

@@ -136,7 +136,7 @@ func (r *Runtime) builtinJSON_reviveWalk(reviver func(FunctionCall) Value, holde
 		value = _undefined
 	}
 
-	if object := value.(*Object); object != nil {
+	if object, ok := value.(*Object); ok {
 		if isArray(object) {
 			length := object.self.getStr("length", nil).ToInteger()
 			for index := int64(0); index < length; index++ {
@@ -150,7 +150,7 @@ func (r *Runtime) builtinJSON_reviveWalk(reviver func(FunctionCall) Value, holde
 			}
 		} else {
 			for _, itemName := range object.self.ownKeys(false, nil) {
-				value := r.builtinJSON_reviveWalk(reviver, object, name)
+				value := r.builtinJSON_reviveWalk(reviver, object, itemName)
 				if value == _undefined {
 					object.self.deleteStr(itemName.string(), false)
 				} else {

+ 14 - 0
builtin_json_test.go

@@ -47,6 +47,20 @@ func TestJSONMarshalObjectCircular(t *testing.T) {
 	}
 }
 
+func TestJSONParseReviver(t *testing.T) {
+	// example from
+	// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
+	const SCRIPT = `
+	JSON.parse('{"p": 5}', function(key, value) {
+	  return typeof value === 'number'
+        ? value * 2 // return value * 2 for numbers
+	    : value     // return everything else unchanged
+	 })["p"]
+	`
+
+	testScript1(SCRIPT, intToValue(10), t)
+}
+
 func BenchmarkJSONStringify(b *testing.B) {
 	b.StopTimer()
 	vm := New()