higgsjs / Higgs

Higgs JavaScript Virtual Machine
877 stars 62 forks source link

JSON.stringify output is truncated. #192

Open sbstp opened 9 years ago

sbstp commented 9 years ago

I was debugging some code using JSON.stringifyand it seems that some objects get truncated. That's the code in question.

print(JSON.stringify(members, null, 2));

members actually has 3 items. I've checked the length property. However, the output only has one element.

[
  {
    "wrapper": "CPtr",
    "to": {
      "wrapper": "CStruct",
      "members": [
        {
          "wrapper": "CType",
          "name": "double",
          "base_type": "f64",
          "size": 8,
          "load_fun": "$ir_load_f64",
          "store_fun": "$ir_store_f64",
          "wrapper_fun": null
        },

      ],
      "name": "s{}",
      "size": 16
    },
    "base_type": "*",
    "name": "*s{}",
    "size": 8,
    "load_fun": "$ir_load_rawptr",
    "store_fun": "$ir_store_rawptr"
  },
  ,

]

If I print the 3 items manually, like below, it displays all of them.

for (var i = 0; i < members.length; i++) {
    print(JSON.stringify(members[i], null, 2));
}

Thinking it maybe was a print bug, I checked the length of the generated strings. They should be about the same, ± a few characters. There's a huge gap! Doesn't look like a bug with print.

print(names.length, members.length);
var x = 0;
for (var i = 0; i < members.length; i++) {
    x += JSON.stringify(members[i], null, 2).length;
}
var y = JSON.stringify(members, null, 2).length;
print(x, y);
// ~1400, ~500
sbstp commented 9 years ago

I removed the null & 2 arguments from stringify and the result is the same. It's not a pretty-printing error.

EDIT: Haven't been able to replicate with something else than the FFI code atm.

sbstp commented 9 years ago

After investigation, it seems that it's a cycle detection issue.

var a = {qux: 'baz'};
var b = [a, a];
print(JSON.stringify(b, null, 2));

Outputs

[
  {
    "qux": "baz"
  },

]

EDIT: Objects are also affected.

var a = {qux: 'baz'};
var b = {a: a, a: a};
print(JSON.stringify(b, null, 2));

Output

{
  "a": {
    "qux": "baz"
  }
}

The output is valid JSON however.