Skip to content

Commit 1f17f88

Browse files
bnoordhuisindutny
authored andcommitted
src, test: fix up ObjectWrap, make test-addons
V8 was upgraded from 3.22 to 3.24 in commit 1c7bf24. Upgrade source files in test/addons/ and automatically generated tests from doc/api/addons.markdown to the new V8 API. This coincidentally fixes a bug in src/node_object_wrap.h where it was still using the old V8 weak persistent handle interface, which is gone in 3.24.
1 parent d0ff900 commit 1f17f88

4 files changed

Lines changed: 30 additions & 22 deletions

File tree

doc/api/addons.markdown

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ function calls and return a result. This is the main and only needed source
160160
return;
161161
}
162162

163-
Local<Number> num = Number::New(args[0]->NumberValue() +
164-
args[1]->NumberValue());
163+
double value = args[0]->NumberValue() + args[1]->NumberValue();
164+
Local<Number> num = Number::New(isolate, value);
165165

166166
args.GetReturnValue().Set(num);
167167
}
@@ -197,7 +197,7 @@ there. Here's `addon.cc`:
197197
Local<Function> cb = Local<Function>::Cast(args[0]);
198198
const unsigned argc = 1;
199199
Local<Value> argv[argc] = { String::NewFromUtf8(isolate, "hello world") };
200-
cb->Call(Context::GetCurrent()->Global(), argc, argv);
200+
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
201201
}
202202

203203
void Init(Handle<Object> exports, Handle<Object> module) {
@@ -236,7 +236,7 @@ the string passed to `createObject()`:
236236
Isolate* isolate = Isolate::GetCurrent();
237237
HandleScope scope(isolate);
238238

239-
Local<Object> obj = Object::New();
239+
Local<Object> obj = Object::New(isolate);
240240
obj->Set(String::NewFromUtf8(isolate, "msg"), args[0]->ToString());
241241

242242
args.GetReturnValue().Set(obj);
@@ -278,7 +278,7 @@ wraps a C++ function:
278278
Isolate* isolate = Isolate::GetCurrent();
279279
HandleScope scope(isolate);
280280

281-
Local<FunctionTemplate> tpl = FunctionTemplate::New(MyFunction);
281+
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, MyFunction);
282282
Local<Function> fn = tpl->GetFunction();
283283

284284
// omit this to make it anonymous
@@ -366,7 +366,7 @@ prototype:
366366
Isolate* isolate = Isolate::GetCurrent();
367367

368368
// Prepare constructor template
369-
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
369+
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
370370
tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
371371
tpl->InstanceTemplate()->SetInternalFieldCount(1);
372372

@@ -404,7 +404,7 @@ prototype:
404404
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.This());
405405
obj->value_ += 1;
406406

407-
args.GetReturnValue().Set(Number::New(obj->value_));
407+
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
408408
}
409409

410410
Test it with:
@@ -494,7 +494,7 @@ The implementation is similar to the above in `myobject.cc`:
494494
void MyObject::Init() {
495495
Isolate* isolate = Isolate::GetCurrent();
496496
// Prepare constructor template
497-
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
497+
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
498498
tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
499499
tpl->InstanceTemplate()->SetInternalFieldCount(1);
500500

@@ -542,7 +542,7 @@ The implementation is similar to the above in `myobject.cc`:
542542
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.This());
543543
obj->value_ += 1;
544544

545-
args.GetReturnValue().Set(Number::New(obj->value_));
545+
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
546546
}
547547

548548
Test it with:
@@ -591,7 +591,7 @@ In the following `addon.cc` we introduce a function `add()` that can take on two
591591
args[1]->ToObject());
592592

593593
double sum = obj1->value() + obj2->value();
594-
args.GetReturnValue().Set(Number::New(sum));
594+
args.GetReturnValue().Set(Number::New(isolate, sum));
595595
}
596596

597597
void InitAll(Handle<Object> exports) {
@@ -650,7 +650,7 @@ The implementation of `myobject.cc` is similar as before:
650650
Isolate* isolate = Isolate::GetCurrent();
651651

652652
// Prepare constructor template
653-
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
653+
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
654654
tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
655655
tpl->InstanceTemplate()->SetInternalFieldCount(1);
656656

src/node_object_wrap.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class ObjectWrap {
8282

8383

8484
inline void MakeWeak(void) {
85-
persistent().MakeWeak(this, WeakCallback);
85+
persistent().SetWeak(this, WeakCallback);
8686
persistent().MarkIndependent();
8787
}
8888

@@ -116,13 +116,16 @@ class ObjectWrap {
116116
int refs_; // ro
117117

118118
private:
119-
static void WeakCallback(v8::Isolate* isolate,
120-
v8::Persistent<v8::Object>* pobj,
121-
ObjectWrap* wrap) {
119+
static void WeakCallback(
120+
const v8::WeakCallbackData<v8::Object, ObjectWrap>& data) {
121+
v8::Isolate* isolate = data.GetIsolate();
122122
v8::HandleScope scope(isolate);
123+
ObjectWrap* wrap = data.GetParameter();
123124
assert(wrap->refs_ == 0);
124-
assert(*pobj == wrap->persistent());
125-
assert((*pobj).IsNearDeath());
125+
assert(wrap->handle_.IsNearDeath());
126+
assert(
127+
data.GetValue() == v8::Local<v8::Object>::New(isolate, wrap->handle_));
128+
wrap->handle_.Reset();
126129
delete wrap;
127130
}
128131

test/addons/async-hello-world/binding.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ void AfterAsync(uv_work_t* r) {
2424
HandleScope scope(isolate);
2525
async_req* req = reinterpret_cast<async_req*>(r->data);
2626

27-
Handle<Value> argv[2] = { Null(), Integer::New(req->output) };
27+
Handle<Value> argv[2] = {
28+
Null(isolate),
29+
Integer::New(isolate, req->output)
30+
};
2831

2932
TryCatch try_catch;
3033

3134
Local<Function> callback = Local<Function>::New(isolate, req->callback);
32-
callback->Call(Context::GetCurrent()->Global(), 2, argv);
35+
callback->Call(isolate->GetCurrentContext()->Global(), 2, argv);
3336

3437
// cleanup
35-
req->callback.Dispose();
38+
req->callback.Reset();
3639
delete req;
3740

3841
if (try_catch.HasCaught()) {

test/addons/at-exit/binding.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ static int at_exit_cb1_called = 0;
1616
static int at_exit_cb2_called = 0;
1717

1818
static void at_exit_cb1(void* arg) {
19-
HandleScope scope(Isolate::GetCurrent());
19+
// FIXME(bnoordhuis) Isolate::GetCurrent() is on its way out.
20+
Isolate* isolate = Isolate::GetCurrent();
21+
HandleScope handle_scope(isolate);
2022
assert(arg == 0);
21-
Local<Object> obj = Object::New();
23+
Local<Object> obj = Object::New(isolate);
2224
assert(!obj.IsEmpty()); // assert VM is still alive
2325
assert(obj->IsObject());
2426
at_exit_cb1_called++;

0 commit comments

Comments
 (0)