Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
docs: show how to use Isolate
Browse files Browse the repository at this point in the history
Part of the 3.17 update is to pass the isolate as an argument. The addon
docs have been updated with this usage.
  • Loading branch information
trevnorris authored and bnoordhuis committed Mar 20, 2013
1 parent 88217ec commit da4d79a
Showing 1 changed file with 41 additions and 21 deletions.
62 changes: 41 additions & 21 deletions doc/api/addons.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand Down Expand Up @@ -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() +
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -341,6 +351,8 @@ prototype:
#include <node.h>
#include "myobject.h"

Isolate* isolate = Isolate::GetCurrent();

using namespace v8;

MyObject::MyObject() {};
Expand All @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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() {};
Expand All @@ -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();
Expand All @@ -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] };
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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() {};
Expand All @@ -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();
Expand All @@ -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] };
Expand Down

0 comments on commit da4d79a

Please sign in to comment.