jed / lave

eval in reverse: stringifying all the stuff that JSON.stringify won't

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Example from README gives different result

mathiasrw opened this issue · comments

Hi. I really like the idea with lave. Im testing out how to use it to let AlaSQL store itself to a string (so we can restore the state again).

First thing I did was to try the example in on the README. But I get a different result from what is described in the README.

First I installed the modules:

npm install lave 
npm install escodegen

then I copy/paste the example from the README and run:

node << EOF          
  var generate = require('escodegen').generate
  var lave = require('lave')

  var a = [function(){}, new Date, new Buffer('A'), global]
  a.splice(2, 0, a)

  var js = lave(a, {generate, format: 'module'})
  console.log(js)
EOF

The result I get is

export default a;

but the expected result in the README is described as

var a = [
    function (){},
    new Date(1456522677247),
    null,
    Buffer('QQ==', 'base64'),
    (0, eval)('this')
];
a[2] = a;
export default a;

escodegen@1.8.0 + lave@1.1.5 on node@5.3.0 + nvm@3.3.12

Any inputs to why I am not getting same results as described in the README?

Yes, I'm sorry about that! It's my fault. Basically, when lave does it's traversal of the global object, it's slurping up your variable binding. I should pare back the scale of the global crawl to prevent this.

For now, you can run your lave code in an IIFE to prevent this.

IIFE?

Immediately invoked function expression (more specific... I had a similar question)

For now, you can run your lave code in an IIFE to prevent this.

Ok - ill try it out.

Any plans on updating the README example?

you shouldn't need an IIFE anymore, thanks to @fgnass: #23