NodeJS vm documentation issue

50 views
Skip to first unread message

Sebi

unread,
Jun 29, 2012, 8:46:51 PM6/29/12
to nod...@googlegroups.com
Hello,
I think you've a error in your docs belonging to the vm Module.
It says:
JavaScript code can be compiled and run immediately or compiled, saved, and run later. 
http://nodejs.org/docs/v0.8.1/api/vm.html#vm_executing_javascript
So if I would follow this sentence, i could do the following:

vm = require('vm');
var script = vm.createScript("hello = 'Hello World!'");
// save the script???
... JSON.stringify(script);
OUTPUT: '{}'

So how to save compiled Javascript? There is no entry in the API Docs, that describe saving compiled code.
I think until today it's an error, so the sentence should be:

Javascript can be compiled and run immediatley or compiled and run later.

mscdex

unread,
Jun 29, 2012, 9:41:38 PM6/29/12
to nodejs
On Jun 29, 8:46 pm, Sebi <sebastian.tild...@googlemail.com> wrote:
> Hello,
> I think you've a error in your docs belonging to the vm Module.

What the documentation means is that you can save the actual compiled
code (into a variable) and use it again later, unlike say eval(),
which compiles code every time it's called.

mscdex

unread,
Jun 29, 2012, 9:43:24 PM6/29/12
to nodejs
Additionally, the value being returned by the VM functions is a v8
context and not some magical thing that you can .toString() or similar
to get the original code back out of it.

mscdex

unread,
Jun 29, 2012, 9:45:37 PM6/29/12
to nodejs
On Jun 29, 9:43 pm, mscdex <msc...@gmail.com> wrote:
> Additionally, the value being returned by the VM functions is a v8
> context and not some magical thing that you can .toString() or similar
> to get the original code back out of it.

Oops, mistook createScript() for createContext() here.

Brandon Benvie

unread,
Jun 30, 2012, 2:28:26 AM6/30/12
to nod...@googlegroups.com
It's not wrong but it is an "eats shoots and leaves" ambiguous wording that, to me at least, has the more obvious interpretation as the wrong one. You can "save" a vm script or context like you save any other value in the js heap: keep the engine running and keep a reference to it. But you cannot usefully serialize it for storage for loading later in a separate instance of the vm, aside from saving the raw input source code.
Reply all
Reply to author
Forward
0 new messages