From 5725864dfdc951ee7616188776b56b9e0a95e8e7 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 12 Aug 2013 20:34:18 +0200 Subject: [PATCH] src: don't obj->Set(Integer::New(...), val) Don't create an Integer when setting a numeric index on an object or an array, use the version of v8::Object::Set() that takes an uint32_t. Change the types of the variables from int to uint32_t and clean up some code consistency issues while we're here. --- src/cares_wrap.cc | 70 +++++++++++++++++++++++------------------------------- src/node_crypto.cc | 3 +-- src/node_file.cc | 12 +++++----- src/tls_wrap.cc | 3 +-- 4 files changed, 38 insertions(+), 50 deletions(-) diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 914b0f5..438b405 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -203,11 +203,10 @@ static Local HostentToAddresses(struct hostent* host) { Local addresses = Array::New(); char ip[INET6_ADDRSTRLEN]; - for (int i = 0; host->h_addr_list[i]; ++i) { + for (uint32_t i = 0; host->h_addr_list[i] != NULL; ++i) { uv_inet_ntop(host->h_addrtype, host->h_addr_list[i], ip, sizeof(ip)); - Local address = OneByteString(node_isolate, ip); - addresses->Set(Integer::New(i, node_isolate), address); + addresses->Set(i, address); } return scope.Close(addresses); @@ -218,9 +217,9 @@ static Local HostentToNames(struct hostent* host) { HandleScope scope(node_isolate); Local names = Array::New(); - for (int i = 0; host->h_aliases[i]; ++i) { + for (uint32_t i = 0; host->h_aliases[i] != NULL; ++i) { Local address = OneByteString(node_isolate, host->h_aliases[i]); - names->Set(Integer::New(i, node_isolate), address); + names->Set(i, address); } return scope.Close(names); @@ -455,16 +454,14 @@ class QueryMxWrap: public QueryWrap { Local priority_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "priority"); - int i = 0; - for (struct ares_mx_reply* mx_current = mx_start; - mx_current; - mx_current = mx_current->next) { + ares_mx_reply* current = mx_start; + for (uint32_t i = 0; current != NULL; ++i, current = current->next) { Local mx_record = Object::New(); mx_record->Set(exchange_symbol, - OneByteString(node_isolate, mx_current->host)); + OneByteString(node_isolate, current->host)); mx_record->Set(priority_symbol, - Integer::New(mx_current->priority, node_isolate)); - mx_records->Set(Integer::New(i++, node_isolate), mx_record); + Integer::New(current->priority, node_isolate)); + mx_records->Set(i, mx_record); } ares_free_data(mx_start); @@ -524,10 +521,10 @@ class QueryTxtWrap: public QueryWrap { Local txt_records = Array::New(); - struct ares_txt_reply *current = txt_out; - for (int i = 0; current; ++i, current = current->next) { + ares_txt_reply* current = txt_out; + for (uint32_t i = 0; current != NULL; ++i, current = current->next) { Local txt = OneByteString(node_isolate, current->txt); - txt_records->Set(Integer::New(i, node_isolate), txt); + txt_records->Set(i, txt); } ares_free_data(txt_out); @@ -573,20 +570,18 @@ class QuerySrvWrap: public QueryWrap { Local weight_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "weight"); - int i = 0; - for (struct ares_srv_reply* srv_current = srv_start; - srv_current; - srv_current = srv_current->next) { + ares_srv_reply* current = srv_start; + for (uint32_t i = 0; current != NULL; ++i, current = current->next) { Local srv_record = Object::New(); srv_record->Set(name_symbol, - OneByteString(node_isolate, srv_current->host)); + OneByteString(node_isolate, current->host)); srv_record->Set(port_symbol, - Integer::New(srv_current->port, node_isolate)); + Integer::New(current->port, node_isolate)); srv_record->Set(priority_symbol, - Integer::New(srv_current->priority, node_isolate)); + Integer::New(current->priority, node_isolate)); srv_record->Set(weight_symbol, - Integer::New(srv_current->weight, node_isolate)); - srv_records->Set(Integer::New(i++, node_isolate), srv_record); + Integer::New(current->weight, node_isolate)); + srv_records->Set(i, srv_record); } ares_free_data(srv_start); @@ -637,27 +632,22 @@ class QueryNaptrWrap: public QueryWrap { Local preference_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "preference"); - int i = 0; - for (ares_naptr_reply* naptr_current = naptr_start; - naptr_current; - naptr_current = naptr_current->next) { + ares_naptr_reply* current = naptr_start; + for (uint32_t i = 0; current != NULL; ++i, current = current->next) { Local naptr_record = Object::New(); - naptr_record->Set(flags_symbol, - OneByteString(node_isolate, naptr_current->flags)); + OneByteString(node_isolate, current->flags)); naptr_record->Set(service_symbol, - OneByteString(node_isolate, naptr_current->service)); + OneByteString(node_isolate, current->service)); naptr_record->Set(regexp_symbol, - OneByteString(node_isolate, naptr_current->regexp)); + OneByteString(node_isolate, current->regexp)); naptr_record->Set(replacement_symbol, - OneByteString(node_isolate, - naptr_current->replacement)); - naptr_record->Set(order_symbol, Integer::New(naptr_current->order, - node_isolate)); + OneByteString(node_isolate, current->replacement)); + naptr_record->Set(order_symbol, + Integer::New(current->order, node_isolate)); naptr_record->Set(preference_symbol, - Integer::New(naptr_current->preference, node_isolate)); - - naptr_records->Set(Integer::New(i++, node_isolate), naptr_record); + Integer::New(current->preference, node_isolate)); + naptr_records->Set(i, naptr_record); } ares_free_data(naptr_start); @@ -917,7 +907,7 @@ static void GetServers(const FunctionCallbackInfo& args) { ares_addr_node* cur = servers; - for (int i = 0; cur != NULL; ++i, cur = cur->next) { + for (uint32_t i = 0; cur != NULL; ++i, cur = cur->next) { char ip[INET6_ADDRSTRLEN]; const void* caddr = static_cast(&cur->addr); diff --git a/src/node_crypto.cc b/src/node_crypto.cc index a893c01..5a9646d 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -1601,8 +1601,7 @@ void Connection::GetPeerCertificate(const FunctionCallbackInfo& args) { for (int i = 0; i < sk_ASN1_OBJECT_num(eku); i++) { memset(buf, 0, sizeof(buf)); OBJ_obj2txt(buf, sizeof(buf) - 1, sk_ASN1_OBJECT_value(eku, i), 1); - ext_key_usage->Set(Integer::New(i, node_isolate), - OneByteString(node_isolate, buf)); + ext_key_usage->Set(i, OneByteString(node_isolate, buf)); } sk_ASN1_OBJECT_pop_free(eku, ASN1_OBJECT_free); diff --git a/src/node_file.cc b/src/node_file.cc index f09973f..e01c5bd 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -187,7 +187,7 @@ static void After(uv_fs_t *req) { for (int i = 0; i < nnames; i++) { Local name = String::NewFromUtf8(node_isolate, namebuf); - names->Set(Integer::New(i, node_isolate), name); + names->Set(i, name); #ifndef NDEBUG namebuf += strlen(namebuf); assert(*namebuf == '\0'); @@ -618,13 +618,13 @@ static void ReadDir(const FunctionCallbackInfo& args) { } else { SYNC_CALL(readdir, *path, *path, 0 /*flags*/) - char *namebuf = static_cast(SYNC_REQ.ptr); - int nnames = req_wrap.req.result; + assert(SYNC_REQ.result >= 0); + char* namebuf = static_cast(SYNC_REQ.ptr); + uint32_t nnames = SYNC_REQ.result; Local names = Array::New(nnames); - for (int i = 0; i < nnames; i++) { - Local name = String::NewFromUtf8(node_isolate, namebuf); - names->Set(Integer::New(i, node_isolate), name); + for (uint32_t i = 0; i < nnames; ++i) { + names->Set(i, String::NewFromUtf8(node_isolate, namebuf)); #ifndef NDEBUG namebuf += strlen(namebuf); assert(*namebuf == '\0'); diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index 0574a95..c4fc5a6 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -944,8 +944,7 @@ void TLSCallbacks::GetPeerCertificate(const FunctionCallbackInfo& args) { for (int i = 0; i < sk_ASN1_OBJECT_num(eku); i++) { memset(buf, 0, sizeof(buf)); OBJ_obj2txt(buf, sizeof(buf) - 1, sk_ASN1_OBJECT_value(eku, i), 1); - ext_key_usage->Set(Integer::New(i, node_isolate), - OneByteString(node_isolate, buf)); + ext_key_usage->Set(i, OneByteString(node_isolate, buf)); } sk_ASN1_OBJECT_pop_free(eku, ASN1_OBJECT_free); -- 2.7.4