Search code examples
c++node.jsv8

Hello World and V8


I am working on windows. I am trying to run hello world in V8 by using V8 from underscorediscovery. This could not compile at line

// Get the default Isolate created at startup.
  Isolate* isolate = Isolate::GetCurrent();

I looked at the underscorediscovery headers and it did had old lib and headers when this class was not in the headers. That was fine. I removed this line and replace first four lines with

  // Create a stack-allocated handle scope.
  HandleScope handle_scope;

  // Create a new context.
  Handle<Context> context = Context::New();

  // Here's how you could create a Persistent handle to the context, if needed.
  Persistent<Context> persistent_context(context);

and it worked. So this Isolate was added new to V8.

I then installed node.js and it also has v8 in its dependencies deps folder. I build node.js and v8 also got build. I followed instrutions on addon development of nodejs. Its "hello world nodejs" was successful. I thought that the actual google code should also work as class Isolate is in the headers. But it is not compiling with errors :

error C2664: 'v8::HandleScope::HandleScope(const v8::HandleSc
ope &)' : cannot convert parameter 1 from 'v8::Isolate *' to 'const v8::HandleS
cope &' [C:\Users\asnegi\company\nodejs project\node-v0.10.15\src\my_files\buil
d\v8code.vcxproj]
          Reason: cannot convert from 'v8::Isolate *' to 'const v8::HandleScope
  '
          No constructor could take the source type, or constructor overload re
  solution was ambiguous

Looking in the headers at C:\Users\abc.node-gyp\0.10.15\deps\v8\include\v8.h

It has class Isolate defined. Also,

template <class T> class Handle {
 public:
  /**
   * Creates an empty handle.
   */
  inline Handle() : val_(0) {}

  /**
   * Creates a new handle for the specified value.
   */
  inline explicit Handle(T* val) : val_(val) {}
  ...........
  ...........

and

 class HandleScope {
    public:
  inline HandleScope();
  explicit inline HandleScope(Isolate* isolate);
  .....

Therefore, this part of Google's Hello world should have worked :

// Get the default Isolate created at startup.
  Isolate* isolate = Isolate::GetCurrent();

  // Create a stack-allocated handle scope.
  HandleScope handle_scope(isolate);

  // Create a new context.
  Handle<Context> context = Context::New(isolate);

What is the problem ?


Solution

  • Stable Node v0.10.15 use Google V8 version 3.14.5 (2012-10-22) C:\Documents\github\node\deps\v8\include\v8.h

    class V8EXPORT HandleScope {
     private:
      HandleScope(const HandleScope&);
    

    Unstable Node v0.11.5 use Google V8 version 3.20.14 (2013-08-07) C:\Documents\github\node\deps\v8\include\v8.h

    class V8_EXPORT HandleScope {
     public:
      // TODO(svenpanne) Deprecate me when Chrome is fixed!
      HandleScope();
      HandleScope(Isolate* isolate);
      ~HandleScope();
    

    From the node\deps\v8\ChangeLog file:

    2013-03-15: Version 3.17.11

    Added a version of the v8::HandleScope constructor with an v8::Isolate parameter and made AdjustAmountOfExternalAllocatedMemory an instance method of v8::Isolate. (issue 2487)