Skip to content

Instantly share code, notes, and snippets.

@bodokaiser
Created June 7, 2013 19:15
Show Gist options
  • Save bodokaiser/5731656 to your computer and use it in GitHub Desktop.
Save bodokaiser/5731656 to your computer and use it in GitHub Desktop.
Example of a v8 object exported with node-gyp.
#include "v8.h"
#include "node.h"
using namespace v8;
Handle<Value> Get(const Arguments &args);
Handle<Value> Set(const Arguments &args);
void
Initialize(Handle<Object> exports) {
exports->Set(String::New("value"),
Object::New());
exports->Set(String::New("get"),
FunctionTemplate::New(Get)->GetFunction());
exports->Set(String::New("set"),
FunctionTemplate::New(Set)->GetFunction());
}
Handle<Value>
Get(const Arguments &args) {
HandleScope Scope;
if (!args[0]->IsObject() || !args[1]->IsString())
return Scope.Close(Undefined());
Local<Object> ObjectArg = args[0]->ToObject();
Local<String> StringArg = args[1]->ToString();
if (!ObjectArg->Has(StringArg))
return Scope.Close(Undefined());
return Scope.Close(ObjectArg->Get(StringArg));
}
Handle<Value>
Set(const Arguments &args) {
HandleScope Scope;
if (!args[0]->IsObject() || !args[1]->IsString())
return Scope.Close(Undefined());
Local<Object> ObjectArg = args[0]->ToObject();
Local<String> StringArg = args[1]->ToString();
ObjectArg->Set(StringArg, args[2]);
return Scope.Close(ObjectArg);
}
NODE_MODULE(object, Initialize)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment