Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(195)

Side by Side Diff: src/api.cc

Issue 6119009: Wrap external pointers more carefully. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 3248 matching lines...) Expand 10 before | Expand all | Expand 10 after
3259 "v8::Object::SetInternalField()", 3259 "v8::Object::SetInternalField()",
3260 "Writing internal field out of bounds")) { 3260 "Writing internal field out of bounds")) {
3261 return; 3261 return;
3262 } 3262 }
3263 ENTER_V8; 3263 ENTER_V8;
3264 i::Handle<i::Object> val = Utils::OpenHandle(*value); 3264 i::Handle<i::Object> val = Utils::OpenHandle(*value);
3265 obj->SetInternalField(index, *val); 3265 obj->SetInternalField(index, *val);
3266 } 3266 }
3267 3267
3268 3268
3269 static bool CanBeEncodedAsSmi(void* ptr) {
3270 const intptr_t address = reinterpret_cast<intptr_t>(ptr);
3271 const intptr_t mask = (intptr_t(1) << i::kSmiTagSize) - 1;
Lasse Reichstein 2011/01/13 08:35:18 Just use kSmiTagMask.
3272 return ((address & mask) == 0) && i::Smi::IsValid(address >> i::kSmiTagSize);
Lasse Reichstein 2011/01/13 08:35:18 Almost no addresses will be valid in 64-bit mode.
3273 }
3274
3275
3276 static i::Smi* EncodeAsSmi(void* ptr) {
3277 ASSERT(CanBeEncodedAsSmi(ptr));
3278 i::Smi* result = i::Smi::FromIntptr(
3279 reinterpret_cast<intptr_t>(ptr) >> i::kSmiTagSize);
3280 ASSERT_EQ(ptr, i::Internals::GetExternalPointerFromSmi(result));
3281 return result;
3282 }
3283
3284
3269 void v8::Object::SetPointerInInternalField(int index, void* value) { 3285 void v8::Object::SetPointerInInternalField(int index, void* value) {
3270 ENTER_V8; 3286 ENTER_V8;
3271 i::Object* as_object = reinterpret_cast<i::Object*>(value); 3287 if (CanBeEncodedAsSmi(value)) {
3272 if (as_object->IsSmi()) { 3288 Utils::OpenHandle(this)->SetInternalField(index, EncodeAsSmi(value));
3273 Utils::OpenHandle(this)->SetInternalField(index, as_object); 3289 } else {
3274 return; 3290 HandleScope scope;
3291 i::Handle<i::Proxy> proxy =
3292 i::Factory::NewProxy(reinterpret_cast<i::Address>(value), i::TENURED);
3293 if (!proxy.is_null())
3294 Utils::OpenHandle(this)->SetInternalField(index, *proxy);
3275 } 3295 }
3276 HandleScope scope; 3296 ASSERT_EQ(value, GetPointerFromInternalField(index));
3277 i::Handle<i::Proxy> proxy =
3278 i::Factory::NewProxy(reinterpret_cast<i::Address>(value), i::TENURED);
3279 if (!proxy.is_null())
3280 Utils::OpenHandle(this)->SetInternalField(index, *proxy);
3281 } 3297 }
3282 3298
3283 3299
3284 // --- E n v i r o n m e n t --- 3300 // --- E n v i r o n m e n t ---
3285 3301
3286 bool v8::V8::Initialize() { 3302 bool v8::V8::Initialize() {
3287 if (i::V8::IsRunning()) return true; 3303 if (i::V8::IsRunning()) return true;
3288 HandleScope scope; 3304 HandleScope scope;
3289 if (i::Snapshot::Initialize()) return true; 3305 if (i::Snapshot::Initialize()) return true;
3290 return i::V8::Initialize(NULL); 3306 return i::V8::Initialize(NULL);
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
3553 static void* ExternalValueImpl(i::Handle<i::Object> obj) { 3569 static void* ExternalValueImpl(i::Handle<i::Object> obj) {
3554 return reinterpret_cast<void*>(i::Proxy::cast(*obj)->proxy()); 3570 return reinterpret_cast<void*>(i::Proxy::cast(*obj)->proxy());
3555 } 3571 }
3556 3572
3557 3573
3558 Local<Value> v8::External::Wrap(void* data) { 3574 Local<Value> v8::External::Wrap(void* data) {
3559 STATIC_ASSERT(sizeof(data) == sizeof(i::Address)); 3575 STATIC_ASSERT(sizeof(data) == sizeof(i::Address));
3560 LOG_API("External::Wrap"); 3576 LOG_API("External::Wrap");
3561 EnsureInitialized("v8::External::Wrap()"); 3577 EnsureInitialized("v8::External::Wrap()");
3562 ENTER_V8; 3578 ENTER_V8;
3563 i::Object* as_object = reinterpret_cast<i::Object*>(data); 3579
3564 if (as_object->IsSmi()) { 3580 v8::Local<v8::Value> result = CanBeEncodedAsSmi(data)
3565 return Utils::ToLocal(i::Handle<i::Object>(as_object)); 3581 ? Utils::ToLocal(i::Handle<i::Object>(EncodeAsSmi(data)))
3566 } 3582 : v8::Local<v8::Value>(ExternalNewImpl(data));
3567 return ExternalNewImpl(data); 3583
3584 ASSERT_EQ(data, Unwrap(result));
3585 return result;
3568 } 3586 }
3569 3587
3570 3588
3571 void* v8::Object::SlowGetPointerFromInternalField(int index) { 3589 void* v8::Object::SlowGetPointerFromInternalField(int index) {
3572 i::Handle<i::JSObject> obj = Utils::OpenHandle(this); 3590 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3573 i::Object* value = obj->GetInternalField(index); 3591 i::Object* value = obj->GetInternalField(index);
3574 if (value->IsSmi()) { 3592 if (value->IsSmi()) {
3575 return value; 3593 return i::Internals::GetExternalPointerFromSmi(value);
3576 } else if (value->IsProxy()) { 3594 } else if (value->IsProxy()) {
3577 return reinterpret_cast<void*>(i::Proxy::cast(value)->proxy()); 3595 return reinterpret_cast<void*>(i::Proxy::cast(value)->proxy());
3578 } else { 3596 } else {
3579 return NULL; 3597 return NULL;
3580 } 3598 }
3581 } 3599 }
3582 3600
3583 3601
3584 void* v8::External::FullUnwrap(v8::Handle<v8::Value> wrapper) { 3602 void* v8::External::FullUnwrap(v8::Handle<v8::Value> wrapper) {
3585 if (IsDeadCheck("v8::External::Unwrap()")) return 0; 3603 if (IsDeadCheck("v8::External::Unwrap()")) return 0;
3586 i::Handle<i::Object> obj = Utils::OpenHandle(*wrapper); 3604 i::Handle<i::Object> obj = Utils::OpenHandle(*wrapper);
3587 void* result; 3605 void* result;
3588 if (obj->IsSmi()) { 3606 if (obj->IsSmi()) {
3589 // The external value was an aligned pointer. 3607 result = i::Internals::GetExternalPointerFromSmi(*obj);
3590 result = *obj;
3591 } else if (obj->IsProxy()) { 3608 } else if (obj->IsProxy()) {
3592 result = ExternalValueImpl(obj); 3609 result = ExternalValueImpl(obj);
3593 } else { 3610 } else {
3594 result = NULL; 3611 result = NULL;
3595 } 3612 }
3596 ASSERT_EQ(result, QuickUnwrap(wrapper)); 3613 ASSERT_EQ(result, QuickUnwrap(wrapper));
3597 return result; 3614 return result;
3598 } 3615 }
3599 3616
3600 3617
(...skipping 1535 matching lines...) Expand 10 before | Expand all | Expand 10 after
5136 5153
5137 5154
5138 char* HandleScopeImplementer::Iterate(ObjectVisitor* v, char* storage) { 5155 char* HandleScopeImplementer::Iterate(ObjectVisitor* v, char* storage) {
5139 HandleScopeImplementer* thread_local = 5156 HandleScopeImplementer* thread_local =
5140 reinterpret_cast<HandleScopeImplementer*>(storage); 5157 reinterpret_cast<HandleScopeImplementer*>(storage);
5141 thread_local->IterateThis(v); 5158 thread_local->IterateThis(v);
5142 return storage + ArchiveSpacePerThread(); 5159 return storage + ArchiveSpacePerThread();
5143 } 5160 }
5144 5161
5145 } } // namespace v8::internal 5162 } } // namespace v8::internal
OLDNEW
« include/v8.h ('K') | « include/v8.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698