Merge remote-tracking branch 'ry/v0.10'
[platform/upstream/nodejs.git] / doc / api / addons.markdown
index cb44538..370de49 100644 (file)
@@ -41,10 +41,12 @@ First we create a file `hello.cc`:
     #include <node.h>
     #include <v8.h>
 
+    Isolate* isolate = Isolate::GetCurrent();
+
     using namespace v8;
 
     Handle<Value> Method(const Arguments& args) {
-      HandleScope scope;
+      HandleScope scope(isolate);
       return scope.Close(String::New("world"));
     }
 
@@ -140,19 +142,21 @@ function calls and return a result. This is the main and only needed source
     #define BUILDING_NODE_EXTENSION
     #include <node.h>
 
+    Isolate* isolate = Isolate::GetCurrent();
+
     using namespace v8;
 
     Handle<Value> Add(const Arguments& args) {
-      HandleScope scope;
+      HandleScope scope(isolate);
 
       if (args.Length() < 2) {
         ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
-        return scope.Close(Undefined());
+        return scope.Close(Undefined(isolate));
       }
 
       if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
         ThrowException(Exception::TypeError(String::New("Wrong arguments")));
-        return scope.Close(Undefined());
+        return scope.Close(Undefined(isolate));
       }
 
       Local<Number> num = Number::New(args[0]->NumberValue() +
@@ -182,17 +186,19 @@ there. Here's `addon.cc`:
     #define BUILDING_NODE_EXTENSION
     #include <node.h>
 
+    Isolate* isolate = Isolate::GetCurrent();
+
     using namespace v8;
 
     Handle<Value> RunCallback(const Arguments& args) {
-      HandleScope scope;
+      HandleScope scope(isolate);
 
       Local<Function> cb = Local<Function>::Cast(args[0]);
       const unsigned argc = 1;
       Local<Value> argv[argc] = { Local<Value>::New(String::New("hello world")) };
       cb->Call(Context::GetCurrent()->Global(), argc, argv);
 
-      return scope.Close(Undefined());
+      return scope.Close(Undefined(isolate));
     }
 
     void Init(Handle<Object> exports, Handle<Object> module) {
@@ -225,10 +231,12 @@ the string passed to `createObject()`:
     #define BUILDING_NODE_EXTENSION
     #include <node.h>
 
+    Isolate* isolate = Isolate::GetCurrent();
+
     using namespace v8;
 
     Handle<Value> CreateObject(const Arguments& args) {
-      HandleScope scope;
+      HandleScope scope(isolate);
 
       Local<Object> obj = Object::New();
       obj->Set(String::NewSymbol("msg"), args[0]->ToString());
@@ -260,15 +268,17 @@ wraps a C++ function:
     #define BUILDING_NODE_EXTENSION
     #include <node.h>
 
+    Isolate* isolate = Isolate::GetCurrent();
+
     using namespace v8;
 
     Handle<Value> MyFunction(const Arguments& args) {
-      HandleScope scope;
+      HandleScope scope(isolate);
       return scope.Close(String::New("hello world"));
     }
 
     Handle<Value> CreateFunction(const Arguments& args) {
-      HandleScope scope;
+      HandleScope scope(isolate);
 
       Local<FunctionTemplate> tpl = FunctionTemplate::New(MyFunction);
       Local<Function> fn = tpl->GetFunction();
@@ -341,6 +351,8 @@ prototype:
     #include <node.h>
     #include "myobject.h"
 
+    Isolate* isolate = Isolate::GetCurrent();
+
     using namespace v8;
 
     MyObject::MyObject() {};
@@ -355,12 +367,12 @@ prototype:
       tpl->PrototypeTemplate()->Set(String::NewSymbol("plusOne"),
           FunctionTemplate::New(PlusOne)->GetFunction());
 
-      Persistent<Function> constructor = Persistent<Function>::New(tpl->GetFunction());
+      Persistent<Function> constructor = Persistent<Function>::New(isolate, tpl->GetFunction());
       exports->Set(String::NewSymbol("MyObject"), constructor);
     }
 
     Handle<Value> MyObject::New(const Arguments& args) {
-      HandleScope scope;
+      HandleScope scope(isolate);
 
       MyObject* obj = new MyObject();
       obj->counter_ = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
@@ -370,7 +382,7 @@ prototype:
     }
 
     Handle<Value> MyObject::PlusOne(const Arguments& args) {
-      HandleScope scope;
+      HandleScope scope(isolate);
 
       MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.This());
       obj->counter_ += 1;
@@ -403,10 +415,12 @@ Let's register our `createObject` method in `addon.cc`:
     #include <node.h>
     #include "myobject.h"
 
+    Isolate* isolate = Isolate::GetCurrent();
+
     using namespace v8;
 
     Handle<Value> CreateObject(const Arguments& args) {
-      HandleScope scope;
+      HandleScope scope(isolate);
       return scope.Close(MyObject::NewInstance(args));
     }
 
@@ -451,6 +465,8 @@ The implementation is similar to the above in `myobject.cc`:
     #include <node.h>
     #include "myobject.h"
 
+    Isolate* isolate = Isolate::GetCurrent();
+
     using namespace v8;
 
     MyObject::MyObject() {};
@@ -467,11 +483,11 @@ The implementation is similar to the above in `myobject.cc`:
       tpl->PrototypeTemplate()->Set(String::NewSymbol("plusOne"),
           FunctionTemplate::New(PlusOne)->GetFunction());
 
-      constructor = Persistent<Function>::New(tpl->GetFunction());
+      constructor = Persistent<Function>::New(isolate, tpl->GetFunction());
     }
 
     Handle<Value> MyObject::New(const Arguments& args) {
-      HandleScope scope;
+      HandleScope scope(isolate);
 
       MyObject* obj = new MyObject();
       obj->counter_ = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
@@ -481,7 +497,7 @@ The implementation is similar to the above in `myobject.cc`:
     }
 
     Handle<Value> MyObject::NewInstance(const Arguments& args) {
-      HandleScope scope;
+      HandleScope scope(isolate);
 
       const unsigned argc = 1;
       Handle<Value> argv[argc] = { args[0] };
@@ -491,7 +507,7 @@ The implementation is similar to the above in `myobject.cc`:
     }
 
     Handle<Value> MyObject::PlusOne(const Arguments& args) {
-      HandleScope scope;
+      HandleScope scope(isolate);
 
       MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.This());
       obj->counter_ += 1;
@@ -525,15 +541,17 @@ In the following `addon.cc` we introduce a function `add()` that can take on two
     #include <node.h>
     #include "myobject.h"
 
+    Isolate* isolate = Isolate::GetCurrent();
+
     using namespace v8;
 
     Handle<Value> CreateObject(const Arguments& args) {
-      HandleScope scope;
+      HandleScope scope(isolate);
       return scope.Close(MyObject::NewInstance(args));
     }
 
     Handle<Value> Add(const Arguments& args) {
-      HandleScope scope;
+      HandleScope scope(isolate);
 
       MyObject* obj1 = node::ObjectWrap::Unwrap<MyObject>(
           args[0]->ToObject());
@@ -588,6 +606,8 @@ The implementation of `myobject.cc` is similar as before:
     #include <node.h>
     #include "myobject.h"
 
+    Isolate* isolate = Isolate::GetCurrent();
+
     using namespace v8;
 
     MyObject::MyObject() {};
@@ -605,7 +625,7 @@ The implementation of `myobject.cc` is similar as before:
     }
 
     Handle<Value> MyObject::New(const Arguments& args) {
-      HandleScope scope;
+      HandleScope scope(isolate);
 
       MyObject* obj = new MyObject();
       obj->val_ = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
@@ -615,7 +635,7 @@ The implementation of `myobject.cc` is similar as before:
     }
 
     Handle<Value> MyObject::NewInstance(const Arguments& args) {
-      HandleScope scope;
+      HandleScope scope(isolate);
 
       const unsigned argc = 1;
       Handle<Value> argv[argc] = { args[0] };