Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 602
Implement examples for node 4.x.#49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "targets": [ | ||
| { | ||
| "target_name": "hello", | ||
| "sources": [ "hello.cc" ] | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // hello.cc | ||
| #include <node.h> | ||
| namespace demo { | ||
| using v8::FunctionCallbackInfo; | ||
| using v8::Isolate; | ||
| using v8::Local; | ||
| using v8::Object; | ||
| using v8::String; | ||
| using v8::Value; | ||
| void Method(const v8::FunctionCallbackInfo<Value>& args) { | ||
| Isolate* isolate = args.GetIsolate(); | ||
| args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world")); | ||
| } | ||
| void Init(Local<Object> exports) { | ||
| NODE_SET_METHOD(exports, "hello", Method); | ||
| } | ||
| NODE_MODULE(hello, Init) | ||
| } // namespace demo |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| const addon = require('bindings')('hello'); | ||
| console.log(addon.hello()); // 'world' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "name": "hello_world", | ||
| "version": "0.0.0", | ||
| "description": "Node.js Addons Example #1", | ||
| "main": "hello.js", | ||
| "private": true, | ||
| "scripts": { | ||
| "test": "node hello.js" | ||
| }, | ||
| "gypfile": true, | ||
| "dependencies": { | ||
| "bindings": "~1.2.1" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // addon.cc | ||
| #include <node.h> | ||
| namespace demo { | ||
| using v8::Exception; | ||
| using v8::FunctionCallbackInfo; | ||
| using v8::Isolate; | ||
| using v8::Local; | ||
| using v8::Number; | ||
| using v8::Object; | ||
| using v8::String; | ||
| using v8::Value; | ||
| // This is the implementation of the "add" method | ||
| // Input arguments are passed using the | ||
| // const FunctionCallbackInfo<Value>& args struct | ||
| void Add(const FunctionCallbackInfo<Value>& args) { | ||
| Isolate* isolate = args.GetIsolate(); | ||
| // Check the number of arguments passed. | ||
| if (args.Length() < 2) { | ||
| // Throw an Error that is passed back to JavaScript | ||
| isolate->ThrowException(Exception::TypeError( | ||
| String::NewFromUtf8(isolate, "Wrong number of arguments"))); | ||
| return; | ||
| } | ||
| // Check the argument types | ||
| if (!args[0]->IsNumber() || !args[1]->IsNumber()) { | ||
| isolate->ThrowException(Exception::TypeError( | ||
| String::NewFromUtf8(isolate, "Wrong arguments"))); | ||
| return; | ||
| } | ||
| // Perform the operation | ||
| double value = args[0]->NumberValue() + args[1]->NumberValue(); | ||
| Local<Number> num = Number::New(isolate, value); | ||
| // Set the return value (using the passed in | ||
| // FunctionCallbackInfo<Value>&) | ||
| args.GetReturnValue().Set(num); | ||
| } | ||
| void Init(Local<Object> exports) { | ||
| NODE_SET_METHOD(exports, "add", Add); | ||
| } | ||
| NODE_MODULE(addon, Init) | ||
| } // namespace demo |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| const addon = require('bindings')('addon.node'); | ||
| console.log('This should be eight:', addon.add(3, 5)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "targets": [ | ||
| { | ||
| "target_name": "addon", | ||
| "sources": [ "addon.cc" ] | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "name": "function_arguments", | ||
| "version": "0.0.0", | ||
| "description": "Node.js Addons Example #2", | ||
| "main": "addon.js", | ||
| "private": true, | ||
| "dependencies": { | ||
| "bindings": "~1.2.1", | ||
| "nan": "~1.3.0" | ||
| }, | ||
| "scripts": { | ||
| "test": "node addon.js" | ||
| }, | ||
| "gypfile": true | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // addon.cc | ||
| #include <node.h> | ||
| namespace demo { | ||
| using v8::Function; | ||
| using v8::FunctionCallbackInfo; | ||
| using v8::Isolate; | ||
| using v8::Local; | ||
| using v8::Null; | ||
| using v8::Object; | ||
| using v8::String; | ||
| using v8::Value; | ||
| void RunCallback(const FunctionCallbackInfo<Value>& args) { | ||
| Isolate* isolate = args.GetIsolate(); | ||
| if (!args[0]->IsFunction()) { | ||
| return; | ||
| } | ||
| Local<Function> cb = Local<Function>::Cast(args[0]); | ||
| const unsigned argc = 1; | ||
| Local<Value> argv[argc] = { String::NewFromUtf8(isolate, "hello, world") }; | ||
| cb->Call(Null(isolate), argc, argv); | ||
| } | ||
| void Init(Local<Object> exports, Local<Object> module) { | ||
| NODE_SET_METHOD(module, "exports", RunCallback); | ||
| } | ||
| NODE_MODULE(addon, Init) | ||
| } // namespace demo | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| const addon = require('bindings')('addon'); | ||
| addon((msg) => { | ||
| console.log(msg); // 'hello world' | ||
| }); | ||
| addon(); // nothing |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "targets": [ | ||
| { | ||
| "target_name": "addon", | ||
| "sources": [ "addon.cc" ] | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "name": "callbacks", | ||
| "version": "0.0.0", | ||
| "description": "Node.js Addons Example #3", | ||
| "main": "addon.js", | ||
| "private": true, | ||
| "gypfile": true, | ||
| "dependencies": { | ||
| "bindings": "~1.2.1" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // addon.cc | ||
| #include <node.h> | ||
| namespace demo { | ||
| using v8::Exception; | ||
| using v8::FunctionCallbackInfo; | ||
| using v8::Isolate; | ||
| using v8::Local; | ||
| using v8::Null; | ||
| using v8::Object; | ||
| using v8::String; | ||
| using v8::Value; | ||
| void CreateObject(const FunctionCallbackInfo<Value>& args) { | ||
| Isolate* isolate = args.GetIsolate(); | ||
| if (!args[0]->IsString()) { | ||
| isolate->ThrowException(Exception::TypeError( | ||
| String::NewFromUtf8(isolate, "Argument must be a string")) | ||
| ); | ||
| return; | ||
| } | ||
| Local<Object> obj = Object::New(isolate); | ||
| obj->Set(String::NewFromUtf8(isolate, "msg"), args[0]->ToString()); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's perhaps overwrought for an example but | ||
| args.GetReturnValue().Set(obj); | ||
| } | ||
| void Init(Local<Object> exports, Local<Object> module) { | ||
| NODE_SET_METHOD(module, "exports", CreateObject); | ||
| } | ||
| NODE_MODULE(addon, Init) | ||
| } // namespace demo | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| const addon = require('bindings')('addon'); | ||
| const obj1 = addon('hello'); | ||
| const obj2 = addon('world'); | ||
| console.log(obj1.msg + ', ' + obj2.msg); // 'hello, world' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "targets": [ | ||
| { | ||
| "target_name": "addon", | ||
| "sources": [ "addon.cc" ] | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "name": "object_factory", | ||
| "version": "0.0.0", | ||
| "description": "Node.js Addons Example #4", | ||
| "main": "addon.js", | ||
| "private": true, | ||
| "gypfile": true, | ||
| "dependencies": { | ||
| "bindings": "~1.2.1" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #include <node.h> | ||
| namespace demo { | ||
| using v8::Function; | ||
| using v8::FunctionCallbackInfo; | ||
| using v8::FunctionTemplate; | ||
| using v8::Isolate; | ||
| using v8::Local; | ||
| using v8::Object; | ||
| using v8::String; | ||
| using v8::Value; | ||
| void MyFunction(const FunctionCallbackInfo<Value>& args) { | ||
| Isolate* isolate = args.GetIsolate(); | ||
| args.GetReturnValue().Set(String::NewFromUtf8(isolate, "hello, world")); | ||
| } | ||
| void CreateFunction(const FunctionCallbackInfo<Value>& args) { | ||
| Isolate* isolate = args.GetIsolate(); | ||
| Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, MyFunction); | ||
| Local<Function> fn = tpl->GetFunction(); | ||
| // omit this to make it anonymous | ||
| fn->SetName(String::NewFromUtf8(isolate, "theFunction")); | ||
| args.GetReturnValue().Set(fn); | ||
| } | ||
| void Init(Local<Object> exports, Local<Object> module) { | ||
| NODE_SET_METHOD(module, "exports", CreateFunction); | ||
| } | ||
| NODE_MODULE(addon, Init) | ||
| } // namespace demo |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| const addon = require('bindings')('addon'); | ||
| const fn = addon(); | ||
| console.log(fn()); // 'hello, world' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "targets": [ | ||
| { | ||
| "target_name": "addon", | ||
| "sources": [ "addon.cc" ] | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "name": "function_factory", | ||
| "version": "0.0.0", | ||
| "description": "Node.js Addons Example #5", | ||
| "main": "addon.js", | ||
| "private": true, | ||
| "gypfile": true, | ||
| "dependencies": { | ||
| "bindings": "~1.2.1" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // addon.cc | ||
| #include <node.h> | ||
| #include "myobject.h" | ||
| namespace demo { | ||
| using v8::Local; | ||
| void InitAll(Local<Object> exports) { | ||
| MyObject::Init(exports); | ||
| } | ||
| NODE_MODULE(addon, InitAll) | ||
| } // namespace demo |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| const addon = require('bindings')('addon'); | ||
| const obj = new addon.MyObject(10); | ||
| console.log( obj.plusOne() ); // 11 | ||
| console.log( obj.plusOne() ); // 12 | ||
| console.log( obj.plusOne() ); // 13 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "targets": [ | ||
| { | ||
| "target_name": "addon", | ||
| "sources": [ "addon.cc", "myobject.cc" ] | ||
| } | ||
| ] | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should check that
args[0]->IsFunction()before casting it.EDIT: That should probably be added to the examples for the other node versions as well. The way it's currently written is a ticking time bomb.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would the following make sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without looking at the surrounding context, I'd say yes. However, the default return value is set to Undefined, so a simple return statement would suffice.
On February 5, 2016 12:23:39 PM GMT+02:00, Tom Gallacher notifications@github.com wrote: