5

From http://nodejs.org/api/vm.html:

JavaScript code can be compiled and run immediately or compiled, saved, and run later. [...] The returned script is not bound to any global object. It is bound before each run, just for that run.

And then in the API, no method that returns any bytes, nothing. Just a "Script" object.

So, before I politely tear down this desk beneath my arms, is there any way that I can actually SAVE the compiled script, to disk? I figure it's just ordinary raw binary data, maybe a syntax tree or whatever.

8
  • 2
    I was actually planning to sell the binary result as a close-source nodejs app. Commented Jun 16, 2012 at 5:40
  • 1
    Look, I'm just looking for a simple technique; maybe get a pointer to the allocated address space, write the data to a file, reload the data back into memory... something in that manner; I have some basic "hack-through" skills, I'm okay with compiling DLLs, but since it requires some effort I really had to ask first. Maybe there's a different approach or some existing ongoing effort, I really don't know. The badge is unintentional. Why are you upset over? Commented Jun 16, 2012 at 5:51
  • 1
    That somebody with your level of experience on the network doesn't understand the concept of "put more information into the problem so that people can give you a right answer". Commented Jun 16, 2012 at 5:55
  • 1
    Well, you're a programmer. Means you guys are smart enough to intuit reasons, scenarios, etc. I actually do that myself on several occasions, sometimes it's obvious, sometimes I have to fill gaps. But anyway, shave the beard, drop the haircut and get more girls man. Commented Jun 16, 2012 at 6:12
  • 2
    Got the girls, did shave the beard, donated 9 inches to Pantene Beautiful Lengths. Knowing that you want it for a for-sale binary-only product tells me what you want to do with the file you're outputting. Like how you would prefer it be obfuscated, if possible, etc. Somebody is likely already releasing a package for node that helps to compile stuff just like you're describing you need. I wouldn't infer that anyone was going to use any code for any particular use, aside from what they put in the problem. Commented Jun 16, 2012 at 6:14

1 Answer 1

4

The functions you reference are for javascript being run by javascript in a new context (so it can be secure, have new features, etc)... not so much saving a pre-compiled binary...

If you want details on how to actually reload a precompiled script, you can look at the node.js source. The 'node.js' file itself, is precompiled and loaded as a binary (if you build it with this option). In doing so, it makes node start faster.

What you should bear in mind, however, is there is little advantage to this, unless you conceive a particular process (such as node.js) using the V8 library that will run/stop/run/stop... etc. Reason being, the V8 library will only compile your script once - and will then execute it as machine code each time thereafter, or as long as the V8 library is running.

Precompiling, and loading as a binary, will bring some significant disadvantages, including making your program architecture dependent (even across x86 32-bit versus x86_64) and so forth. So - this may not be the best design decision.

Sign up to request clarification or add additional context in comments.

1 Comment

I, like you, certainly hope this is what OP was asking. Now I'm sad panda I didn't type this answer first

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.