Skip to content
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

Implement examples for node 4.x. #49

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions 1_hello_world/node_4.x/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "hello",
"sources": [ "hello.cc" ]
}
]
}
24 changes: 24 additions & 0 deletions 1_hello_world/node_4.x/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
3 changes: 3 additions & 0 deletions 1_hello_world/node_4.x/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const addon = require('bindings')('hello');

console.log(addon.hello()); // 'world'
14 changes: 14 additions & 0 deletions 1_hello_world/node_4.x/package.json
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"
}
}
51 changes: 51 additions & 0 deletions 2_function_arguments/node_4.x/addon.cc
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
3 changes: 3 additions & 0 deletions 2_function_arguments/node_4.x/addon.js
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));
8 changes: 8 additions & 0 deletions 2_function_arguments/node_4.x/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "addon",
"sources": [ "addon.cc" ]
}
]
}
15 changes: 15 additions & 0 deletions 2_function_arguments/node_4.x/package.json
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
}
32 changes: 32 additions & 0 deletions 3_callbacks/node_4.x/addon.cc
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]);
Copy link
Member

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.

Copy link
Author

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?

if (!args[0]->IsFunction()) {
  return args.GetReturnValue().SetUndefined();
}

Copy link

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 [email protected] wrote:

@@ -0,0 +1,18 @@
+#include <node.h>
+
+using namespace v8;
+
+void RunCallback(const FunctionCallbackInfo& args) {

  • Isolate* isolate = args.GetIsolate();
  • Local cb = Local::Cast(args[0]);

Would the following make sense?

if (!args[0]->IsFunction()) {
 return args.GetReturnValue().SetUndefined();
}

Reply to this email directly or view it on GitHub:
https://github.com/nodejs/node-addon-examples/pull/49/files#r51998512

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
6 changes: 6 additions & 0 deletions 3_callbacks/node_4.x/addon.js
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
8 changes: 8 additions & 0 deletions 3_callbacks/node_4.x/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "addon",
"sources": [ "addon.cc" ]
}
]
}
11 changes: 11 additions & 0 deletions 3_callbacks/node_4.x/package.json
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"
}
}
37 changes: 37 additions & 0 deletions 4_object_factory/node_4.x/addon.cc
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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's perhaps overwrought for an example but ToString() can fail and return an empty handle, e.g. when you call it with addon({ toString() { throw 'boom' } }).


args.GetReturnValue().Set(obj);
}

void Init(Local<Object> exports, Local<Object> module) {
NODE_SET_METHOD(module, "exports", CreateObject);
}

NODE_MODULE(addon, Init)

} // namespace demo
5 changes: 5 additions & 0 deletions 4_object_factory/node_4.x/addon.js
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'
8 changes: 8 additions & 0 deletions 4_object_factory/node_4.x/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "addon",
"sources": [ "addon.cc" ]
}
]
}
11 changes: 11 additions & 0 deletions 4_object_factory/node_4.x/package.json
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"
}
}
37 changes: 37 additions & 0 deletions 5_function_factory/node_4.x/addon.cc
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
4 changes: 4 additions & 0 deletions 5_function_factory/node_4.x/addon.js
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'
8 changes: 8 additions & 0 deletions 5_function_factory/node_4.x/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "addon",
"sources": [ "addon.cc" ]
}
]
}
11 changes: 11 additions & 0 deletions 5_function_factory/node_4.x/package.json
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"
}
}
15 changes: 15 additions & 0 deletions 6_object_wrap/node_4.x/addon.cc
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
6 changes: 6 additions & 0 deletions 6_object_wrap/node_4.x/addon.js
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
8 changes: 8 additions & 0 deletions 6_object_wrap/node_4.x/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "addon",
"sources": [ "addon.cc", "myobject.cc" ]
}
]
}
Loading