Skip to content

Commit 60a3e69

Browse files
committed
src: don't use WeakObject::Unwrap
Switch out to use UnwrapObject from util.h.
1 parent 93f75a8 commit 60a3e69

File tree

9 files changed

+68
-80
lines changed

9 files changed

+68
-80
lines changed

src/node_contextify.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ class ContextifyScript : public WeakObject {
575575
}
576576

577577
ContextifyScript* wrapped_script =
578-
WeakObject::Unwrap<ContextifyScript>(args.This());
578+
UnwrapObject<ContextifyScript>(args.This());
579579
Local<Script> script = PersistentToLocal(node_isolate,
580580
wrapped_script->script_);
581581

src/node_crypto.cc

Lines changed: 41 additions & 49 deletions
Large diffs are not rendered by default.

src/node_crypto.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ class Connection : public SSLWrap<Connection>, public WeakObject {
275275
void SetShutdownFlags();
276276

277277
static Connection* Unwrap(v8::Local<v8::Object> object) {
278-
Connection* conn = WeakObject::Unwrap<Connection>(object);
278+
Connection* conn = UnwrapObject<Connection>(object);
279279
conn->ClearError();
280280
return conn;
281281
}

src/node_http_parser.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#include "env.h"
2727
#include "env-inl.h"
28+
#include "util.h"
29+
#include "util-inl.h"
2830
#include "weak-object.h"
2931
#include "weak-object-inl.h"
3032
#include "v8.h"
@@ -362,7 +364,7 @@ class Parser : public WeakObject {
362364
static void Execute(const FunctionCallbackInfo<Value>& args) {
363365
HandleScope scope(node_isolate);
364366

365-
Parser* parser = WeakObject::Unwrap<Parser>(args.This());
367+
Parser* parser = UnwrapObject<Parser>(args.This());
366368
assert(parser->current_buffer_.IsEmpty());
367369
assert(parser->current_buffer_len_ == 0);
368370
assert(parser->current_buffer_data_ == NULL);
@@ -417,7 +419,7 @@ class Parser : public WeakObject {
417419
static void Finish(const FunctionCallbackInfo<Value>& args) {
418420
HandleScope scope(node_isolate);
419421

420-
Parser* parser = WeakObject::Unwrap<Parser>(args.This());
422+
Parser* parser = UnwrapObject<Parser>(args.This());
421423

422424
assert(parser->current_buffer_.IsEmpty());
423425
parser->got_exception_ = false;
@@ -451,7 +453,7 @@ class Parser : public WeakObject {
451453
static_cast<http_parser_type>(args[0]->Int32Value());
452454

453455
assert(type == HTTP_REQUEST || type == HTTP_RESPONSE);
454-
Parser* parser = WeakObject::Unwrap<Parser>(args.This());
456+
Parser* parser = UnwrapObject<Parser>(args.This());
455457
// Should always be called from the same context.
456458
assert(env == parser->env());
457459
parser->Init(type);
@@ -462,7 +464,7 @@ class Parser : public WeakObject {
462464
static void Pause(const FunctionCallbackInfo<Value>& args) {
463465
Environment* env = Environment::GetCurrent(args.GetIsolate());
464466
HandleScope handle_scope(args.GetIsolate());
465-
Parser* parser = WeakObject::Unwrap<Parser>(args.This());
467+
Parser* parser = UnwrapObject<Parser>(args.This());
466468
// Should always be called from the same context.
467469
assert(env == parser->env());
468470
http_parser_pause(&parser->parser_, should_pause);

src/node_stat_watcher.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "node_stat_watcher.h"
2323
#include "env.h"
2424
#include "env-inl.h"
25+
#include "util.h"
26+
#include "util-inl.h"
2527
#include "weak-object.h"
2628
#include "weak-object-inl.h"
2729

@@ -112,7 +114,7 @@ void StatWatcher::Start(const FunctionCallbackInfo<Value>& args) {
112114
assert(args.Length() == 3);
113115
HandleScope scope(node_isolate);
114116

115-
StatWatcher* wrap = WeakObject::Unwrap<StatWatcher>(args.This());
117+
StatWatcher* wrap = UnwrapObject<StatWatcher>(args.This());
116118
String::Utf8Value path(args[0]);
117119
const bool persistent = args[1]->BooleanValue();
118120
const uint32_t interval = args[2]->Uint32Value();
@@ -125,7 +127,7 @@ void StatWatcher::Start(const FunctionCallbackInfo<Value>& args) {
125127

126128

127129
void StatWatcher::Stop(const FunctionCallbackInfo<Value>& args) {
128-
StatWatcher* wrap = WeakObject::Unwrap<StatWatcher>(args.This());
130+
StatWatcher* wrap = UnwrapObject<StatWatcher>(args.This());
129131
Environment* env = wrap->env();
130132
Context::Scope context_scope(env->context());
131133
HandleScope handle_scope(env->isolate());

src/node_zlib.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
#include "env.h"
2626
#include "env-inl.h"
27+
#include "util.h"
28+
#include "util-inl.h"
2729
#include "weak-object.h"
2830
#include "weak-object-inl.h"
2931

@@ -122,7 +124,7 @@ class ZCtx : public WeakObject {
122124

123125
static void Close(const FunctionCallbackInfo<Value>& args) {
124126
HandleScope scope(node_isolate);
125-
ZCtx* ctx = WeakObject::Unwrap<ZCtx>(args.This());
127+
ZCtx* ctx = UnwrapObject<ZCtx>(args.This());
126128
ctx->Close();
127129
}
128130

@@ -132,7 +134,7 @@ class ZCtx : public WeakObject {
132134
HandleScope scope(node_isolate);
133135
assert(args.Length() == 7);
134136

135-
ZCtx* ctx = WeakObject::Unwrap<ZCtx>(args.This());
137+
ZCtx* ctx = UnwrapObject<ZCtx>(args.This());
136138
assert(ctx->init_done_ && "write before init");
137139
assert(ctx->mode_ != NONE && "already finalized");
138140

@@ -341,7 +343,7 @@ class ZCtx : public WeakObject {
341343
assert((args.Length() == 4 || args.Length() == 5) &&
342344
"init(windowBits, level, memLevel, strategy, [dictionary])");
343345

344-
ZCtx* ctx = WeakObject::Unwrap<ZCtx>(args.This());
346+
ZCtx* ctx = UnwrapObject<ZCtx>(args.This());
345347

346348
int windowBits = args[0]->Uint32Value();
347349
assert((windowBits >= 8 && windowBits <= 15) && "invalid windowBits");
@@ -380,15 +382,15 @@ class ZCtx : public WeakObject {
380382

381383
assert(args.Length() == 2 && "params(level, strategy)");
382384

383-
ZCtx* ctx = WeakObject::Unwrap<ZCtx>(args.This());
385+
ZCtx* ctx = UnwrapObject<ZCtx>(args.This());
384386

385387
Params(ctx, args[0]->Int32Value(), args[1]->Int32Value());
386388
}
387389

388390
static void Reset(const FunctionCallbackInfo<Value> &args) {
389391
HandleScope scope(node_isolate);
390392

391-
ZCtx* ctx = WeakObject::Unwrap<ZCtx>(args.This());
393+
ZCtx* ctx = UnwrapObject<ZCtx>(args.This());
392394

393395
Reset(ctx);
394396
SetDictionary(ctx);

src/tls_wrap.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ TLSCallbacks::TLSCallbacks(Environment* env,
6060
Kind kind,
6161
Handle<Object> sc,
6262
StreamWrapCallbacks* old)
63-
: SSLWrap<TLSCallbacks>(env, WeakObject::Unwrap<SecureContext>(sc), kind),
63+
: SSLWrap<TLSCallbacks>(env, UnwrapObject<SecureContext>(sc), kind),
6464
StreamWrapCallbacks(old),
6565
enc_in_(NULL),
6666
enc_out_(NULL),
@@ -72,7 +72,7 @@ TLSCallbacks::TLSCallbacks(Environment* env,
7272
shutdown_(false) {
7373

7474
// Persist SecureContext
75-
sc_ = WeakObject::Unwrap<SecureContext>(sc);
75+
sc_ = UnwrapObject<SecureContext>(sc);
7676
sc_handle_.Reset(node_isolate, sc);
7777

7878
Local<Object> object = env->tls_wrap_constructor_function()->NewInstance();
@@ -694,7 +694,7 @@ int TLSCallbacks::SelectSNIContextCallback(SSL* s, int* ad, void* arg) {
694694
p->sni_context_.Dispose();
695695
p->sni_context_.Reset(node_isolate, ctx);
696696

697-
SecureContext* sc = WeakObject::Unwrap<SecureContext>(ctx.As<Object>());
697+
SecureContext* sc = UnwrapObject<SecureContext>(ctx.As<Object>());
698698
SSL_set_SSL_CTX(s, sc->ctx_);
699699
}
700700

src/weak-object-inl.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@
2323
#define SRC_WEAK_OBJECT_INL_H_
2424

2525
#include "weak-object.h"
26+
#include "util.h"
27+
#include "util-inl.h"
2628

2729
namespace node {
2830

2931
WeakObject::WeakObject(v8::Isolate* isolate, v8::Local<v8::Object> object)
3032
: weak_object_(isolate, object) {
3133
weak_object_.MarkIndependent();
32-
object->SetAlignedPointerInInternalField(kInternalFieldIndex, this);
34+
35+
// The pointer is resolved as void*.
36+
WrapObject<WeakObject>(object, this);
3337
MakeWeak();
3438
}
3539

@@ -48,15 +52,6 @@ void WeakObject::ClearWeak() {
4852
weak_object_.ClearWeak();
4953
}
5054

51-
template <typename TypeName>
52-
TypeName* WeakObject::Unwrap(v8::Local<v8::Object> object) {
53-
// Cast to WeakObject* before casting to TypeName* avoids issues with classes
54-
// that have multiple base classes.
55-
void* p = object->GetAlignedPointerFromInternalField(kInternalFieldIndex);
56-
WeakObject* w = static_cast<WeakObject*>(p);
57-
return static_cast<TypeName*>(w);
58-
}
59-
6055
void WeakObject::WeakCallback(v8::Isolate* isolate,
6156
v8::Persistent<v8::Object>* persistent,
6257
WeakObject* self) {

src/weak-object.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ namespace node {
2828

2929
class WeakObject {
3030
public:
31-
// FIXME(bnoordhuis) These methods are public only because the code base
32-
// plays fast and loose with encapsulation.
33-
template <typename TypeName>
34-
inline static TypeName* Unwrap(v8::Local<v8::Object> object);
3531
// Returns the wrapped object. Illegal to call in your destructor.
3632
inline v8::Local<v8::Object> weak_object(v8::Isolate* isolate) const;
3733
protected:
@@ -45,7 +41,6 @@ class WeakObject {
4541
inline static void WeakCallback(v8::Isolate* isolate,
4642
v8::Persistent<v8::Object>* persistent,
4743
WeakObject* self);
48-
static const int kInternalFieldIndex = 0;
4944
v8::Persistent<v8::Object> weak_object_;
5045
};
5146

0 commit comments

Comments
 (0)