Skip to content

Commit

Permalink
Merge pull request #165 from MStoykov/fixParseReviver
Browse files Browse the repository at this point in the history
fix JSON.parse with reviver
  • Loading branch information
dop251 authored Jul 14, 2020
2 parents bfd5970 + 9025f72 commit 1f8a2ff
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions builtin_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,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").ToInteger()
for index := int64(0); index < length; index++ {
Expand All @@ -167,7 +167,7 @@ func (r *Runtime) builtinJSON_reviveWalk(reviver func(FunctionCall) Value, holde
}
} else {
for item, f := object.self.enumerate(false, false)(); f != nil; item, f = f() {
value := r.builtinJSON_reviveWalk(reviver, object, name)
value := r.builtinJSON_reviveWalk(reviver, object, newStringValue(item.name))
if value == _undefined {
object.self.deleteStr(item.name, false)
} else {
Expand Down
14 changes: 14 additions & 0 deletions builtin_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1f8a2ff

Please sign in to comment.