v8 ** Snapshot ** help !

282 views
Skip to first unread message

Hugh Fitzpatrick

unread,
Jan 6, 2016, 3:22:38 PM1/6/16
to v8-users

Folks,

I'm trying to understand how to use the 'snapshot' feature in v8.

I am currently using node-v0.12.7 ... a little old - but should be ok.

I have modified 'node.gyp' to enable snapshots.

I can convert my sample 'foo.js' to 'snapshot.cc' via *mksnapshot* and 
see that v8::NewContextFromSnapshot() is being called. 

So it seems that my snapshot is compiled-in, and being deserialized when I run .node'.

'foo.js'  contained a function called "sayHello()".

I cannot call this function by v8-script-compile-run means or using the REPL from node.

Question - What am I doing wrong ? 

Is there a simple step-by-step example anywhere ?

Thanks in advance.

- Hugh


Jochen Eisinger

unread,
Jan 7, 2016, 5:37:29 AM1/7/16
to v8-users
The snapshot used by V8 in this case is only for builtin objects such as Math or Date. The current version of V8 has something called "V8 extras" that can be used to add additional scripts to the snapshot, but I'm not aware of anybody using that for node.

--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hugh Fitzpatrick

unread,
Jan 7, 2016, 9:49:40 AM1/7/16
to v8-users

So it turns out that I was using mksnapshot incorrectly.

My procedure is now:-

1.  Create snapshot ...

       ./mksnapshot --extra_code my code.js my_snapshot.cc

2.  Copy snapshot ...

     cp my_snapshot.cc ./obj.target/v8_snapshot/geni/snapshot.cc
     
3.  Rebuild Node

Starting node results in availability of code from my_code.js.

Thus far - the code that I can snapshot needs to be really simple, and not
dependent on other packages.

For instance - I cannot access  console.log(..)  or use require.

 ... Any thoughts on getting these to work ?   Can I combine *many* JS files in a snapshot ?

Thanks in advance.

- Hugh


Ben Noordhuis

unread,
Jan 7, 2016, 10:11:30 AM1/7/16
to v8-u...@googlegroups.com
On Thu, Jan 7, 2016 at 3:49 PM, Hugh Fitzpatrick
<hugh.fit...@gmail.com> wrote:
> For instance - I cannot access console.log(..) or use require.
>
> ... Any thoughts on getting these to work ? Can I combine *many* JS files
> in a snapshot ?

You can't use node.js specific code in snapshot creation.

I'm not sure about deserialization; certainly not at deserialization
time itself because any node.js specific logic won't have been
installed yet.

Functions exported by the snapshot in theory should be able to call
`console.log` (`console` is a global object that can be lazily
resolved) but IIRC, there are some restrictions on scope lookup in
serialized code that may prohibit that.

require() won't work, it's not a global and snapshot code doesn't run
in module context.

Hugh Fitzpatrick

unread,
Jan 7, 2016, 10:47:52 AM1/7/16
to v8-users
Ben,

Thanks for the reply. 

I got console.log(..) to work.... but I guess more complex stuff like setInterval(..) is dependent on Node.

I'm trying to use snapshots with an embedded Node/V8 application - they may help with startup times.

I'll let you know how I get on.

- Hugh

Yang Guo

unread,
Jan 20, 2016, 5:13:24 AM1/20/16
to v8-users
As long as console.log or setInterval is not called when you create the snapshot, it's fine. The variable resolution for global variables happens at runtime, after the snapshot has been deserialized. I'd suggest simply putting everything you need into one .js file to create the snapshot from.
Reply all
Reply to author
Forward
0 new messages