Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Calling JSON.stringify on arguments fails #11845

Closed
ndhoule opened this issue Dec 18, 2013 · 3 comments
Closed

Calling JSON.stringify on arguments fails #11845

ndhoule opened this issue Dec 18, 2013 · 3 comments
Milestone

Comments

@ndhoule
Copy link

ndhoule commented Dec 18, 2013

Running latest PhantomJS, 1.9.2, on both OS X 10.9 and Ubuntu 12.04. Installed phantomjs both via brew and npm, no difference in behavior.

Here's the code I'm trying to use:

var memoize = function(func) {
  var memos = {};
  return function() {
    var serialization = JSON.stringify(arguments);
    return memos[serialization] = memos[serialization] || func.apply(this, arguments);
  };
};

(Sidenote: Declaring this function at the PhantomJS console errored, told me that it was a cyclic structure. Same problem persisted when not at the console, though.)

And my test cases:

var fib = function(n) {
  if (n < 2) { return n; }
  return fib(n - 1) + fib(n - 2);
};
var fastFib = memoize(fib);

expect(fib(10)).to.equal(55);      // Passes
expect(fastFib(10)).to.equal(55);  // Passes
expect(fastFib(7)).to.equal(13);   // Fails, returns 55

I narrowed this down by throwing a console.log(JSON.stringify(arguments)) just below var serialization, above. It logs {}. I also experienced the same behavior when trying to console.log(arguments), though console.log(arguments[0]); worked fine.

@JamesMGreene
Copy link
Collaborator

This is due to the old version of WebKit that Phantom 1.x runs on. Phantom 2.0 should eliminate this problem.

Until then, here's a workaround that should do the trick:

var args = Array.prototype.slice.call(arguments, 0);
console.log(JSON.stringify(args));

@ndhoule
Copy link
Author

ndhoule commented Dec 19, 2013

Gotcha, thanks a lot!

@ariya
Copy link
Owner

ariya commented Aug 16, 2014

Fixed in Qt5-based master branch.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants