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

Side by Side Diff: src/api.cc

Issue 21117: Allow the morphing of strings to external strings to avoid having to... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 11 years, 10 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
« no previous file with comments | « include/v8.h ('k') | src/heap.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2007-2008 the V8 project authors. All rights reserved. 1 // Copyright 2007-2008 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 2422 matching lines...) Expand 10 before | Expand all | Expand 10 after
2433 i::Handle<i::String> NewExternalAsciiStringHandle( 2433 i::Handle<i::String> NewExternalAsciiStringHandle(
2434 v8::String::ExternalAsciiStringResource* resource) { 2434 v8::String::ExternalAsciiStringResource* resource) {
2435 i::Handle<i::String> result = 2435 i::Handle<i::String> result =
2436 i::Factory::NewExternalStringFromAscii(resource); 2436 i::Factory::NewExternalStringFromAscii(resource);
2437 return result; 2437 return result;
2438 } 2438 }
2439 2439
2440 2440
2441 static void DisposeExternalString(v8::Persistent<v8::Value> obj, 2441 static void DisposeExternalString(v8::Persistent<v8::Value> obj,
2442 void* parameter) { 2442 void* parameter) {
2443 v8::String::ExternalStringResource* resource = 2443 i::ExternalTwoByteString* str =
2444 reinterpret_cast<v8::String::ExternalStringResource*>(parameter); 2444 i::ExternalTwoByteString::cast(*Utils::OpenHandle(*obj));
2445 const size_t total_size = resource->length() * sizeof(*resource->data()); 2445
2446 i::Counters::total_external_string_memory.Decrement(total_size); 2446 // External symbols are deleted when they are pruned out of the symbol
2447 delete resource; 2447 // table. Generally external symbols are not registered with the weak handle
2448 // callbacks unless they are upgraded to a symbol after being externalized.
2449 if (!str->IsSymbol()) {
2450 v8::String::ExternalStringResource* resource =
2451 reinterpret_cast<v8::String::ExternalStringResource*>(parameter);
2452 if (resource != NULL) {
2453 const size_t total_size = resource->length() * sizeof(*resource->data());
2454 i::Counters::total_external_string_memory.Decrement(total_size);
2455
2456 // The object will continue to live in the JavaScript heap until the
2457 // handle is entirely cleaned out by the next GC. For example the
2458 // destructor for the resource below could bring it back to life again.
2459 // Which is why we make sure to not have a dangling pointer here.
2460 str->set_resource(NULL);
2461 delete resource;
2462 }
2463 }
2464
2465 // In any case we do not need this handle any longer.
2448 obj.Dispose(); 2466 obj.Dispose();
2449 } 2467 }
2450 2468
2451 2469
2452 static void DisposeExternalAsciiString(v8::Persistent<v8::Value> obj, 2470 static void DisposeExternalAsciiString(v8::Persistent<v8::Value> obj,
2453 void* parameter) { 2471 void* parameter) {
2454 v8::String::ExternalAsciiStringResource* resource = 2472 i::ExternalAsciiString* str =
2455 reinterpret_cast<v8::String::ExternalAsciiStringResource*>(parameter); 2473 i::ExternalAsciiString::cast(*Utils::OpenHandle(*obj));
2456 const size_t total_size = resource->length() * sizeof(*resource->data()); 2474
2457 i::Counters::total_external_string_memory.Decrement(total_size); 2475 // External symbols are deleted when they are pruned out of the symbol
2458 delete resource; 2476 // table. Generally external symbols are not registered with the weak handle
2477 // callbacks unless they are upgraded to a symbol after being externalized.
2478 if (!str->IsSymbol()) {
2479 v8::String::ExternalAsciiStringResource* resource =
2480 reinterpret_cast<v8::String::ExternalAsciiStringResource*>(parameter);
2481 if (resource != NULL) {
2482 const size_t total_size = resource->length() * sizeof(*resource->data());
2483 i::Counters::total_external_string_memory.Decrement(total_size);
2484
2485 // The object will continue to live in the JavaScript heap until the
2486 // handle is entirely cleaned out by the next GC. For example the
2487 // destructor for the resource below could bring it back to life again.
2488 // Which is why we make sure to not have a dangling pointer here.
2489 str->set_resource(NULL);
2490 delete resource;
2491 }
2492 }
2493
2494 // In any case we do not need this handle any longer.
2459 obj.Dispose(); 2495 obj.Dispose();
2460 } 2496 }
2461 2497
2462 2498
2463 Local<String> v8::String::NewExternal( 2499 Local<String> v8::String::NewExternal(
2464 v8::String::ExternalStringResource* resource) { 2500 v8::String::ExternalStringResource* resource) {
2465 EnsureInitialized("v8::String::NewExternal()"); 2501 EnsureInitialized("v8::String::NewExternal()");
2466 LOG_API("String::NewExternal"); 2502 LOG_API("String::NewExternal");
2467 const size_t total_size = resource->length() * sizeof(*resource->data()); 2503 const size_t total_size = resource->length() * sizeof(*resource->data());
2468 i::Counters::total_external_string_memory.Increment(total_size); 2504 i::Counters::total_external_string_memory.Increment(total_size);
2469 i::Handle<i::String> result = NewExternalStringHandle(resource); 2505 i::Handle<i::String> result = NewExternalStringHandle(resource);
2470 i::Handle<i::Object> handle = i::GlobalHandles::Create(*result); 2506 i::Handle<i::Object> handle = i::GlobalHandles::Create(*result);
2471 i::GlobalHandles::MakeWeak(handle.location(), 2507 i::GlobalHandles::MakeWeak(handle.location(),
2472 resource, 2508 resource,
2473 &DisposeExternalString); 2509 &DisposeExternalString);
2474 return Utils::ToLocal(result); 2510 return Utils::ToLocal(result);
2475 } 2511 }
2476 2512
2477 2513
2514 bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) {
2515 if (IsDeadCheck("v8::String::MakeExternal()")) return false;
2516 if (this->IsExternal()) return false; // Already an external string.
2517 i::Handle <i::String> obj = Utils::OpenHandle(this);
2518 bool result = obj->MakeExternal(resource);
2519 if (result && !obj->IsSymbol()) {
2520 // Operation was successful and the string is not a symbol. In this case
2521 // we need to make sure that the we call the destructor for the external
2522 // resource when no strong references to the string remain.
2523 i::Handle<i::Object> handle = i::GlobalHandles::Create(*obj);
2524 i::GlobalHandles::MakeWeak(handle.location(),
2525 resource,
2526 &DisposeExternalString);
2527 }
2528 return result;
2529 }
2530
2531
2478 Local<String> v8::String::NewExternal( 2532 Local<String> v8::String::NewExternal(
2479 v8::String::ExternalAsciiStringResource* resource) { 2533 v8::String::ExternalAsciiStringResource* resource) {
2480 EnsureInitialized("v8::String::NewExternal()"); 2534 EnsureInitialized("v8::String::NewExternal()");
2481 LOG_API("String::NewExternal"); 2535 LOG_API("String::NewExternal");
2482 const size_t total_size = resource->length() * sizeof(*resource->data()); 2536 const size_t total_size = resource->length() * sizeof(*resource->data());
2483 i::Counters::total_external_string_memory.Increment(total_size); 2537 i::Counters::total_external_string_memory.Increment(total_size);
2484 i::Handle<i::String> result = NewExternalAsciiStringHandle(resource); 2538 i::Handle<i::String> result = NewExternalAsciiStringHandle(resource);
2485 i::Handle<i::Object> handle = i::GlobalHandles::Create(*result); 2539 i::Handle<i::Object> handle = i::GlobalHandles::Create(*result);
2486 i::GlobalHandles::MakeWeak(handle.location(), 2540 i::GlobalHandles::MakeWeak(handle.location(),
2487 resource, 2541 resource,
2488 &DisposeExternalAsciiString); 2542 &DisposeExternalAsciiString);
2489 return Utils::ToLocal(result); 2543 return Utils::ToLocal(result);
2490 } 2544 }
2491 2545
2492 2546
2547 bool v8::String::MakeExternal(
2548 v8::String::ExternalAsciiStringResource* resource) {
2549 if (IsDeadCheck("v8::String::MakeExternal()")) return false;
2550 if (this->IsExternal()) return false; // Already an external string.
2551 i::Handle <i::String> obj = Utils::OpenHandle(this);
2552 bool result = obj->MakeExternal(resource);
2553 if (result && !obj->IsSymbol()) {
2554 // Operation was successful and the string is not a symbol. In this case
2555 // we need to make sure that the we call the destructor for the external
2556 // resource when no strong references to the string remain.
2557 i::Handle<i::Object> handle = i::GlobalHandles::Create(*obj);
2558 i::GlobalHandles::MakeWeak(handle.location(),
2559 resource,
2560 &DisposeExternalAsciiString);
2561 }
2562 return result;
2563 }
2564
2565
2493 Local<v8::Object> v8::Object::New() { 2566 Local<v8::Object> v8::Object::New() {
2494 EnsureInitialized("v8::Object::New()"); 2567 EnsureInitialized("v8::Object::New()");
2495 LOG_API("Object::New"); 2568 LOG_API("Object::New");
2496 i::Handle<i::JSObject> obj = 2569 i::Handle<i::JSObject> obj =
2497 i::Factory::NewJSObject(i::Top::object_function()); 2570 i::Factory::NewJSObject(i::Top::object_function());
2498 return Utils::ToLocal(obj); 2571 return Utils::ToLocal(obj);
2499 } 2572 }
2500 2573
2501 2574
2502 Local<v8::Value> v8::Date::New(double time) { 2575 Local<v8::Value> v8::Date::New(double time) {
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after
2947 reinterpret_cast<HandleScopeImplementer*>(storage); 3020 reinterpret_cast<HandleScopeImplementer*>(storage);
2948 List<void**>* blocks_of_archived_thread = thread_local->Blocks(); 3021 List<void**>* blocks_of_archived_thread = thread_local->Blocks();
2949 v8::ImplementationUtilities::HandleScopeData* handle_data_of_archived_thread = 3022 v8::ImplementationUtilities::HandleScopeData* handle_data_of_archived_thread =
2950 &thread_local->handle_scope_data_; 3023 &thread_local->handle_scope_data_;
2951 Iterate(v, blocks_of_archived_thread, handle_data_of_archived_thread); 3024 Iterate(v, blocks_of_archived_thread, handle_data_of_archived_thread);
2952 3025
2953 return storage + ArchiveSpacePerThread(); 3026 return storage + ArchiveSpacePerThread();
2954 } 3027 }
2955 3028
2956 } } // namespace v8::internal 3029 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « include/v8.h ('k') | src/heap.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698