Skip to content

Commit 8608bc0

Browse files
committed
Merge pull request nodejs#32 from nodejs/chaining
chaining style APIs
2 parents f605d35 + b945171 commit 8608bc0

File tree

9 files changed

+103
-15
lines changed

9 files changed

+103
-15
lines changed

2_function_arguments/node_0.10/addon.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,24 @@
22

33
using namespace v8;
44

5-
void Add(const v8::FunctionCallbackInfo<Value>& args) {
6-
Isolate* isolate = Isolate::GetCurrent();
7-
HandleScope scope(isolate);
5+
Handle<Value> Add(const Arguments& args) {
6+
HandleScope scope;
87

98
if (args.Length() < 2) {
109
ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
11-
return;
10+
return scope.Close(Undefined());
1211
}
1312

1413
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
1514
ThrowException(Exception::TypeError(String::New("Wrong arguments")));
16-
return;
15+
return scope.Close(Undefined());
1716
}
1817

1918
double arg0 = args[0]->NumberValue();
2019
double arg1 = args[1]->NumberValue();
2120
Local<Number> num = Number::New(arg0 + arg1);
2221

23-
args.GetReturnValue().Set(num);
22+
return scope.Close(num);
2423
}
2524

2625
void Init(Handle<Object> exports) {

6_object_wrap/nan/addon.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,11 @@ var addon = require('bindings')('addon');
33
var obj = new addon.MyObject(10);
44
console.log( obj.plusOne() ); // 11
55
console.log( obj.plusOne() ); // 12
6-
console.log( obj.plusOne() ); // 13
6+
console.log( obj.plusOne() ); // 13
7+
8+
console.log( obj.multiply().value() ); // 13
9+
console.log( obj.multiply(10).value() ); // 130
10+
11+
var newobj = obj.multiply(-1);
12+
console.log( newobj.value() ); // -13
13+
console.log( obj === newobj ); // false

6_object_wrap/nan/myobject.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ void MyObject::Init(Handle<Object> exports) {
1919
tpl->InstanceTemplate()->SetInternalFieldCount(1);
2020

2121
// Prototype
22+
NODE_SET_PROTOTYPE_METHOD(tpl, "value", GetValue);
2223
NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);
24+
NODE_SET_PROTOTYPE_METHOD(tpl, "multiply", Multiply);
2325

2426
NanAssignPersistent(constructor, tpl->GetFunction());
2527
exports->Set(NanNew("MyObject"), tpl->GetFunction());
@@ -43,6 +45,14 @@ NAN_METHOD(MyObject::New) {
4345
}
4446
}
4547

48+
NAN_METHOD(MyObject::GetValue) {
49+
NanScope();
50+
51+
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
52+
53+
NanReturnValue(NanNew(obj->value_));
54+
}
55+
4656
NAN_METHOD(MyObject::PlusOne) {
4757
NanScope();
4858

@@ -51,3 +61,17 @@ NAN_METHOD(MyObject::PlusOne) {
5161

5262
NanReturnValue(NanNew(obj->value_));
5363
}
64+
65+
NAN_METHOD(MyObject::Multiply) {
66+
NanScope();
67+
68+
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
69+
double multiple = args[0]->IsUndefined() ? 1 : args[0]->NumberValue();
70+
71+
Local<Function> cons = NanNew<Function>(constructor);
72+
73+
const int argc = 1;
74+
Local<Value> argv[argc] = { NanNew(obj->value_ * multiple) };
75+
76+
NanReturnValue(cons->NewInstance(argc, argv));
77+
}

6_object_wrap/nan/myobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ class MyObject : public node::ObjectWrap {
1212
~MyObject();
1313

1414
static NAN_METHOD(New);
15+
static NAN_METHOD(GetValue);
1516
static NAN_METHOD(PlusOne);
17+
static NAN_METHOD(Multiply);
1618
static v8::Persistent<v8::Function> constructor;
1719
double value_;
1820
};

6_object_wrap/node_0.10/addon.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,11 @@ var addon = require('bindings')('addon');
33
var obj = new addon.MyObject(10);
44
console.log( obj.plusOne() ); // 11
55
console.log( obj.plusOne() ); // 12
6-
console.log( obj.plusOne() ); // 13
6+
console.log( obj.plusOne() ); // 13
7+
8+
console.log( obj.multiply().value() ); // 13
9+
console.log( obj.multiply(10).value() ); // 130
10+
11+
var newobj = obj.multiply(-1);
12+
console.log( newobj.value() ); // -13
13+
console.log( obj === newobj ); // false

6_object_wrap/node_0.10/myobject.cc

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
using namespace v8;
55

6+
Persistent<Function> MyObject::constructor;
7+
68
MyObject::MyObject() {};
79
MyObject::~MyObject() {};
810

@@ -12,28 +14,49 @@ void MyObject::Init(Handle<Object> target) {
1214
tpl->SetClassName(String::NewSymbol("MyObject"));
1315
tpl->InstanceTemplate()->SetInternalFieldCount(1);
1416
// Prototype
15-
tpl->PrototypeTemplate()->Set(String::NewSymbol("plusOne"),
16-
FunctionTemplate::New(PlusOne)->GetFunction());
17+
NODE_SET_PROTOTYPE_METHOD(tpl, "value", GetValue);
18+
NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);
19+
NODE_SET_PROTOTYPE_METHOD(tpl, "multiply", Multiply);
1720

18-
Persistent<Function> constructor = Persistent<Function>::New(tpl->GetFunction());
21+
constructor = Persistent<Function>::New(tpl->GetFunction());
1922
target->Set(String::NewSymbol("MyObject"), constructor);
2023
}
2124

2225
Handle<Value> MyObject::New(const Arguments& args) {
2326
HandleScope scope;
2427

2528
MyObject* obj = new MyObject();
26-
obj->counter_ = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
29+
obj->value_ = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
2730
obj->Wrap(args.This());
2831

2932
return args.This();
3033
}
3134

35+
Handle<Value> MyObject::GetValue(const Arguments& args) {
36+
HandleScope scope;
37+
38+
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
39+
40+
return scope.Close(Number::New(obj->value_));
41+
}
42+
3243
Handle<Value> MyObject::PlusOne(const Arguments& args) {
3344
HandleScope scope;
3445

3546
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.This());
36-
obj->counter_ += 1;
47+
obj->value_ += 1;
48+
49+
return scope.Close(Number::New(obj->value_));
50+
}
51+
52+
Handle<Value> MyObject::Multiply(const Arguments& args) {
53+
HandleScope scope;
54+
55+
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.This());
56+
double multiple = args[0]->IsUndefined() ? 1 : args[0]->NumberValue();
57+
58+
const int argc = 1;
59+
Local<Value> argv[argc] = { Number::New(obj->value_ * multiple) };
3760

38-
return scope.Close(Number::New(obj->counter_));
61+
return scope.Close(constructor->NewInstance(argc, argv));
3962
}

6_object_wrap/node_0.10/myobject.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ class MyObject : public node::ObjectWrap {
1212
~MyObject();
1313

1414
static v8::Handle<v8::Value> New(const v8::Arguments& args);
15+
static v8::Handle<v8::Value> GetValue(const v8::Arguments& args);
1516
static v8::Handle<v8::Value> PlusOne(const v8::Arguments& args);
16-
double counter_;
17+
static v8::Handle<v8::Value> Multiply(const v8::Arguments& args);
18+
static v8::Persistent<v8::Function> constructor;
19+
double value_;
1720
};
1821

1922
#endif

6_object_wrap/node_0.12/myobject.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ void MyObject::New(const FunctionCallbackInfo<Value>& args) {
4545
}
4646
}
4747

48+
void MyObject::GetValue(const FunctionCallbackInfo<Value>& args) {
49+
Isolate* isolate = Isolate::GetCurrent();
50+
HandleScope scope(isolate);
51+
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
52+
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
53+
}
54+
4855
void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) {
4956
Isolate* isolate = Isolate::GetCurrent();
5057
HandleScope scope(isolate);
@@ -54,3 +61,17 @@ void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) {
5461

5562
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
5663
}
64+
65+
void MyObject::Multiply(const FunctionCallbackInfo<Value>& args) {
66+
Isolate* isolate = Isolate::GetCurrent();
67+
HandleScope scope(isolate);
68+
69+
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
70+
double multiple = args[0]->IsUndefined() ? 1 : args[0]->NumberValue();
71+
72+
const int argc = 1;
73+
Local<Value> argv[argc] = { Number::New(isolate, obj->value_ * multiple) };
74+
75+
Local<Function> cons = Local<Function>::New(isolate, constructor);
76+
args.GetReturnValue().Set(cons->NewInstance(argc, argv));
77+
}

6_object_wrap/node_0.12/myobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ class MyObject : public node::ObjectWrap {
1313
~MyObject();
1414

1515
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
16+
static void GetValue(const v8::FunctionCallbackInfo<v8::Value>& args);
1617
static void PlusOne(const v8::FunctionCallbackInfo<v8::Value>& args);
18+
static void Multiply(const v8::FunctionCallbackInfo<v8::Value>& args);
1719
static v8::Persistent<v8::Function> constructor;
1820
double value_;
1921
};

0 commit comments

Comments
 (0)