Say what?
V8Engine is nothing more than a simple abstraction that wraps the internal Node.js V8 engine located in module vm to an instantiatable class that provides a number of methods that behave similar to the implementation of V8ScriptEngine in Microsoft.ClearScript.V8.
Install
npm install V8Engine
Example | evaluate some code
/* initialize */const V8Engine = /* create a new v8 engine */var ctx = /* evaluate (run) code inside it */ctxctx
Example | understand the following
It is important to realize that your context does not have a function require or even console.log because those are context specific implementations and not part of the EcmaScript language itself.
/* this does not work */const V8Engine = var ctx = ctx /* ReferenceError: console is not defined. */
Example || expose the console
const V8Engine = var ctx = /* Creates a variable 'log' and assigns console.log from current process */ctx /* You can also just expose the entire console */ctx
Example | the difference between addHostObject and addHostType
Similar to Microsoft.ClearScript.V8 as mentioned earlier, V8Engine distinguishes between an host object (any type of variable in your process) and a host type (callable). This simply means that a type is an object with a constructor like a function or class in C# or any other Object-Oriented language and a host Object is an instance of something. In particular case, any variable in your Node application can be assigned as a host object because we are talking Javascript and everything is an Object of some sorts. Yes. This could be considered slightly inconsistent of me because you can assign a function using addHostType because it is callable but in the real world outside Javascript, a type (class) returns an instance of itself.
/* this works fine */ctx ctxctx /* this fails */ctx // exception: target is not a constructor (number)ctx // exception: unexpected object, expecting string identifier
Example | get the context
The method getContext() returns the internal v8 context as Object.
var ctx = ctxctx/* returns{ a: 1, b: 'hello'}*/
Note: if you are using NW.js or Electron, the vm module of Node.js behaves differently. Variables set using let will not be properties of the context object. This is not my fault but a minor inconsistency in their product. I have not reported this 'bug' because I am lazy.
Example || initialize V8Engine with a context
var ctx = require: require console: console foo: 'bar'/* ctx will already have a variable require, console and foo */
Example | clone
Creates a new V8Engine with current context.
let ctx = ctx var ctx2 = ctx /* ctx2 is instance of V8Engine with all the variables of ctx but does not affect ctx when updated */
That was all!
Module V8Engine is properly documented and commented so if you want to learn how it works internally, open index.js and read all my awesome explanations.
Until next time.
Jochem Stoel
Involuntary public figure.
- https://www.npmjs.com/~jochemstoel
- http://jochemstoel.github.io/
- https://www.quora.com/profile/Jochem-Stoel/
- https://jochemstoel.tumblr.com/
- https://jochemstoel.nl/
- https://www.facebook.com/Jochem-Stoel-271292656217087/