Skip to content

Commit 8b8e3b6

Browse files
committed
async-wrap: integrate with WeakObject
Making WeakObject inherit from AsyncWrap allows us to peak into almost all the MakeCallback calls in Node internals.
1 parent efa62fd commit 8b8e3b6

14 files changed

Lines changed: 102 additions & 176 deletions

src/env.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,15 @@ namespace node {
9696
V(onclienthello_string, "onclienthello") \
9797
V(oncomplete_string, "oncomplete") \
9898
V(onconnection_string, "onconnection") \
99+
V(ondone_string, "ondone") \
99100
V(onerror_string, "onerror") \
100101
V(onexit_string, "onexit") \
101102
V(onhandshakedone_string, "onhandshakedone") \
102103
V(onhandshakestart_string, "onhandshakestart") \
103104
V(onmessage_string, "onmessage") \
104105
V(onnewsession_string, "onnewsession") \
105106
V(onread_string, "onread") \
107+
V(onselect_string, "onselect") \
106108
V(onsignal_string, "onsignal") \
107109
V(onstop_string, "onstop") \
108110
V(path_string, "path") \

src/node_contextify.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,9 @@ class ContextifyScript : public WeakObject {
412412
return ThrowError("Must call vm.Script as a constructor.");
413413
}
414414

415+
Environment* env = Environment::GetCurrent(args.GetIsolate());
415416
ContextifyScript* contextify_script =
416-
new ContextifyScript(args.GetIsolate(), args.This());
417+
new ContextifyScript(env, args.This());
417418

418419
TryCatch try_catch;
419420
Local<String> code = args[0]->ToString();
@@ -605,8 +606,8 @@ class ContextifyScript : public WeakObject {
605606
}
606607

607608

608-
ContextifyScript(Isolate* isolate, Local<Object> object)
609-
: WeakObject(isolate, object) {
609+
ContextifyScript(Environment* env, Local<Object> object)
610+
: WeakObject(env, object) {
610611
}
611612

612613

src/node_crypto.cc

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ int SSLWrap<Base>::NewSessionCallback(SSL* s, SSL_SESSION* sess) {
845845
HandleScope scope(node_isolate);
846846

847847
Base* w = static_cast<Base*>(SSL_get_app_data(s));
848-
Environment* env = w->env();
848+
Environment* env = w->ssl_env();
849849

850850
if (!w->session_callbacks_)
851851
return 0;
@@ -866,11 +866,7 @@ int SSLWrap<Base>::NewSessionCallback(SSL* s, SSL_SESSION* sess) {
866866
reinterpret_cast<char*>(sess->session_id),
867867
sess->session_id_length);
868868
Local<Value> argv[] = { session, buff };
869-
MakeCallback(env,
870-
w->weak_object(node_isolate),
871-
env->onnewsession_string(),
872-
ARRAY_SIZE(argv),
873-
argv);
869+
w->MakeCallback(env->onnewsession_string(), ARRAY_SIZE(argv), argv);
874870

875871
return 0;
876872
}
@@ -882,7 +878,7 @@ void SSLWrap<Base>::OnClientHello(void* arg,
882878
HandleScope scope(node_isolate);
883879

884880
Base* w = static_cast<Base*>(arg);
885-
Environment* env = w->env();
881+
Environment* env = w->ssl_env();
886882

887883
Local<Object> hello_obj = Object::New();
888884
Local<Object> buff = Buffer::New(
@@ -901,11 +897,7 @@ void SSLWrap<Base>::OnClientHello(void* arg,
901897
hello_obj->Set(env->tls_ticket_string(), Boolean::New(hello.has_ticket()));
902898

903899
Local<Value> argv[] = { hello_obj };
904-
MakeCallback(env,
905-
w->weak_object(node_isolate),
906-
env->onclienthello_string(),
907-
ARRAY_SIZE(argv),
908-
argv);
900+
w->MakeCallback(env->onclienthello_string(), ARRAY_SIZE(argv), argv);
909901
}
910902

911903

@@ -916,7 +908,7 @@ void SSLWrap<Base>::GetPeerCertificate(
916908
HandleScope scope(node_isolate);
917909

918910
Base* w = Unwrap<Base>(args.This());
919-
Environment* env = w->env();
911+
Environment* env = w->ssl_env();
920912

921913
Local<Object> info = Object::New();
922914
X509* peer_cert = SSL_get_peer_certificate(w->ssl_);
@@ -1109,7 +1101,7 @@ void SSLWrap<Base>::LoadSession(const FunctionCallbackInfo<Value>& args) {
11091101
HandleScope scope(node_isolate);
11101102

11111103
Base* w = Unwrap<Base>(args.This());
1112-
Environment* env = w->env();
1104+
Environment* env = w->ssl_env();
11131105

11141106
if (args.Length() >= 1 && Buffer::HasInstance(args[0])) {
11151107
ssize_t slen = Buffer::Length(args[0]);
@@ -1258,7 +1250,7 @@ void SSLWrap<Base>::GetCurrentCipher(const FunctionCallbackInfo<Value>& args) {
12581250
HandleScope scope(node_isolate);
12591251

12601252
Base* w = Unwrap<Base>(args.This());
1261-
Environment* env = w->env();
1253+
Environment* env = w->ssl_env();
12621254

12631255
OPENSSL_CONST SSL_CIPHER* c = SSL_get_current_cipher(w->ssl_);
12641256
if (c == NULL)
@@ -1432,8 +1424,7 @@ int Connection::HandleBIOError(BIO *bio, const char* func, int rv) {
14321424
HandleScope scope(node_isolate);
14331425
Local<Value> exception =
14341426
Exception::Error(OneByteString(node_isolate, ssl_error_buf));
1435-
weak_object(node_isolate)->Set(
1436-
FIXED_ONE_BYTE_STRING(node_isolate, "error"), exception);
1427+
object()->Set(FIXED_ONE_BYTE_STRING(node_isolate, "error"), exception);
14371428

14381429
DEBUG_PRINT("[%p] BIO: %s failed: (%d) %s\n",
14391430
ssl_,
@@ -1476,8 +1467,7 @@ int Connection::HandleSSLError(const char* func,
14761467
} else if (err == SSL_ERROR_ZERO_RETURN) {
14771468
Local<Value> exception =
14781469
Exception::Error(FIXED_ONE_BYTE_STRING(node_isolate, "ZERO_RETURN"));
1479-
weak_object(node_isolate)->Set(
1480-
FIXED_ONE_BYTE_STRING(node_isolate, "error"), exception);
1470+
object()->Set(FIXED_ONE_BYTE_STRING(node_isolate, "error"), exception);
14811471
return rv;
14821472

14831473
} else if (err == SSL_ERROR_SYSCALL && ss == kIgnoreSyscall) {
@@ -1501,8 +1491,7 @@ int Connection::HandleSSLError(const char* func,
15011491
BIO_get_mem_ptr(bio, &mem);
15021492
Local<Value> exception =
15031493
Exception::Error(OneByteString(node_isolate, mem->data, mem->length));
1504-
weak_object(node_isolate)->Set(
1505-
FIXED_ONE_BYTE_STRING(node_isolate, "error"), exception);
1494+
object()->Set(FIXED_ONE_BYTE_STRING(node_isolate, "error"), exception);
15061495
BIO_free_all(bio);
15071496
}
15081497

@@ -1519,7 +1508,7 @@ void Connection::ClearError() {
15191508

15201509
// We should clear the error in JS-land
15211510
Local<String> error_key = FIXED_ONE_BYTE_STRING(node_isolate, "error");
1522-
Local<Value> error = weak_object(node_isolate)->Get(error_key);
1511+
Local<Value> error = object()->Get(error_key);
15231512
assert(error->BooleanValue() == false);
15241513
#endif // NDEBUG
15251514
}
@@ -1533,13 +1522,13 @@ void Connection::SetShutdownFlags() {
15331522
if (flags & SSL_SENT_SHUTDOWN) {
15341523
Local<String> sent_shutdown_key =
15351524
FIXED_ONE_BYTE_STRING(node_isolate, "sentShutdown");
1536-
weak_object(node_isolate)->Set(sent_shutdown_key, True(node_isolate));
1525+
object()->Set(sent_shutdown_key, True(node_isolate));
15371526
}
15381527

15391528
if (flags & SSL_RECEIVED_SHUTDOWN) {
15401529
Local<String> received_shutdown_key =
15411530
FIXED_ONE_BYTE_STRING(node_isolate, "receivedShutdown");
1542-
weak_object(node_isolate)->Set(received_shutdown_key, True(node_isolate));
1531+
object()->Set(received_shutdown_key, True(node_isolate));
15431532
}
15441533
}
15451534

@@ -1644,10 +1633,8 @@ int Connection::SelectSNIContextCallback_(SSL *s, int *ad, void* arg) {
16441633
if (!conn->sniObject_.IsEmpty()) {
16451634
conn->sniContext_.Dispose();
16461635

1647-
Local<Object> sni_object =
1648-
PersistentToLocal(node_isolate, conn->sniObject_);
16491636
Local<Value> arg = PersistentToLocal(node_isolate, conn->servername_);
1650-
Local<Value> ret = MakeCallback(env, sni_object, "onselect", 1, &arg);
1637+
Local<Value> ret = conn->MakeCallback(env->onselect_string(), 1, &arg);
16511638

16521639
// If ret is SecureContext
16531640
Local<FunctionTemplate> secure_context_constructor_template =
@@ -1766,15 +1753,11 @@ void Connection::SSLInfoCallback(const SSL *ssl_, int where, int ret) {
17661753
HandleScope handle_scope(env->isolate());
17671754

17681755
if (where & SSL_CB_HANDSHAKE_START) {
1769-
MakeCallback(env,
1770-
conn->weak_object(node_isolate),
1771-
env->onhandshakestart_string());
1756+
conn->MakeCallback(env->onhandshakestart_string(), 0, NULL);
17721757
}
17731758

17741759
if (where & SSL_CB_HANDSHAKE_DONE) {
1775-
MakeCallback(env,
1776-
conn->weak_object(node_isolate),
1777-
env->onhandshakedone_string());
1760+
conn->MakeCallback(env->onhandshakedone_string(), 0, NULL);
17781761
}
17791762
}
17801763

@@ -2088,7 +2071,8 @@ void CipherBase::Initialize(Environment* env, Handle<Object> target) {
20882071
void CipherBase::New(const FunctionCallbackInfo<Value>& args) {
20892072
assert(args.IsConstructCall() == true);
20902073
CipherKind kind = args[0]->IsTrue() ? kCipher : kDecipher;
2091-
new CipherBase(args.GetIsolate(), args.This(), kind);
2074+
Environment* env = Environment::GetCurrent(args.GetIsolate());
2075+
new CipherBase(env, args.This(), kind);
20922076
}
20932077

20942078

@@ -2329,7 +2313,8 @@ void Hmac::Initialize(Environment* env, v8::Handle<v8::Object> target) {
23292313

23302314

23312315
void Hmac::New(const FunctionCallbackInfo<Value>& args) {
2332-
new Hmac(args.GetIsolate(), args.This());
2316+
Environment* env = Environment::GetCurrent(args.GetIsolate());
2317+
new Hmac(env, args.This());
23332318
}
23342319

23352320

@@ -2466,7 +2451,8 @@ void Hash::New(const FunctionCallbackInfo<Value>& args) {
24662451

24672452
const String::Utf8Value hash_type(args[0]);
24682453

2469-
Hash* hash = new Hash(args.GetIsolate(), args.This());
2454+
Environment* env = Environment::GetCurrent(args.GetIsolate());
2455+
Hash* hash = new Hash(env, args.This());
24702456
if (!hash->HashInit(*hash_type)) {
24712457
return ThrowError("Digest method not supported");
24722458
}
@@ -2565,7 +2551,8 @@ void Sign::Initialize(Environment* env, v8::Handle<v8::Object> target) {
25652551

25662552

25672553
void Sign::New(const FunctionCallbackInfo<Value>& args) {
2568-
new Sign(args.GetIsolate(), args.This());
2554+
Environment* env = Environment::GetCurrent(args.GetIsolate());
2555+
new Sign(env, args.This());
25692556
}
25702557

25712558

@@ -2746,7 +2733,8 @@ void Verify::Initialize(Environment* env, v8::Handle<v8::Object> target) {
27462733

27472734

27482735
void Verify::New(const FunctionCallbackInfo<Value>& args) {
2749-
new Verify(args.GetIsolate(), args.This());
2736+
Environment* env = Environment::GetCurrent(args.GetIsolate());
2737+
new Verify(env, args.This());
27502738
}
27512739

27522740

@@ -3006,8 +2994,8 @@ void DiffieHellman::DiffieHellmanGroup(
30062994
const FunctionCallbackInfo<Value>& args) {
30072995
HandleScope scope(node_isolate);
30082996

3009-
DiffieHellman* diffieHellman =
3010-
new DiffieHellman(args.GetIsolate(), args.This());
2997+
Environment* env = Environment::GetCurrent(args.GetIsolate());
2998+
DiffieHellman* diffieHellman = new DiffieHellman(env, args.This());
30112999

30123000
if (args.Length() != 1 || !args[0]->IsString()) {
30133001
return ThrowError("No group name given");
@@ -3034,8 +3022,9 @@ void DiffieHellman::DiffieHellmanGroup(
30343022
void DiffieHellman::New(const FunctionCallbackInfo<Value>& args) {
30353023
HandleScope scope(node_isolate);
30363024

3025+
Environment* env = Environment::GetCurrent(args.GetIsolate());
30373026
DiffieHellman* diffieHellman =
3038-
new DiffieHellman(args.GetIsolate(), args.This());
3027+
new DiffieHellman(env, args.This());
30393028
bool initialized = false;
30403029

30413030
if (args.Length() > 0) {
@@ -3353,7 +3342,7 @@ void EIO_PBKDF2After(uv_work_t* work_req, int status) {
33533342
req->obj.Dispose();
33543343
Local<Value> argv[2];
33553344
EIO_PBKDF2After(req, argv);
3356-
MakeCallback(env, obj, "ondone", ARRAY_SIZE(argv), argv);
3345+
MakeCallback(env, obj, env->ondone_string(), ARRAY_SIZE(argv), argv);
33573346
}
33583347

33593348

@@ -3532,7 +3521,7 @@ void RandomBytesAfter(uv_work_t* work_req, int status) {
35323521
Local<Value> argv[2];
35333522
RandomBytesCheck(req, argv);
35343523
Local<Object> obj = PersistentToLocal(node_isolate, req->obj_);
3535-
MakeCallback(env, obj, "ondone", ARRAY_SIZE(argv), argv);
3524+
MakeCallback(env, obj, env->ondone_string(), ARRAY_SIZE(argv), argv);
35363525
delete req;
35373526
}
35383527

@@ -3664,7 +3653,8 @@ void Certificate::Initialize(Handle<Object> target) {
36643653

36653654

36663655
void Certificate::New(const FunctionCallbackInfo<Value>& args) {
3667-
new Certificate(args.GetIsolate(), args.This());
3656+
Environment* env = Environment::GetCurrent(args.GetIsolate());
3657+
new Certificate(env, args.This());
36683658
}
36693659

36703660

0 commit comments

Comments
 (0)