OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 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 3064 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3075 | 3075 |
3076 | 3076 |
3077 i::Handle<i::String> NewExternalAsciiStringHandle( | 3077 i::Handle<i::String> NewExternalAsciiStringHandle( |
3078 v8::String::ExternalAsciiStringResource* resource) { | 3078 v8::String::ExternalAsciiStringResource* resource) { |
3079 i::Handle<i::String> result = | 3079 i::Handle<i::String> result = |
3080 i::Factory::NewExternalStringFromAscii(resource); | 3080 i::Factory::NewExternalStringFromAscii(resource); |
3081 return result; | 3081 return result; |
3082 } | 3082 } |
3083 | 3083 |
3084 | 3084 |
3085 static void DisposeExternalString(v8::Persistent<v8::Value> obj, | |
3086 void* parameter) { | |
3087 ENTER_V8; | |
3088 i::ExternalTwoByteString* str = | |
3089 i::ExternalTwoByteString::cast(*Utils::OpenHandle(*obj)); | |
3090 | |
3091 // External symbols are deleted when they are pruned out of the symbol | |
3092 // table. Generally external symbols are not registered with the weak handle | |
3093 // callbacks unless they are upgraded to a symbol after being externalized. | |
3094 if (!str->IsSymbol()) { | |
3095 v8::String::ExternalStringResource* resource = | |
3096 reinterpret_cast<v8::String::ExternalStringResource*>(parameter); | |
3097 if (resource != NULL) { | |
3098 const int total_size = | |
3099 static_cast<int>(resource->length() * sizeof(*resource->data())); | |
3100 i::Counters::total_external_string_memory.Decrement(total_size); | |
3101 | |
3102 // The object will continue to live in the JavaScript heap until the | |
3103 // handle is entirely cleaned out by the next GC. For example the | |
3104 // destructor for the resource below could bring it back to life again. | |
3105 // Which is why we make sure to not have a dangling pointer here. | |
3106 str->set_resource(NULL); | |
3107 delete resource; | |
3108 } | |
3109 } | |
3110 | |
3111 // In any case we do not need this handle any longer. | |
3112 obj.Dispose(); | |
3113 } | |
3114 | |
3115 | |
3116 static void DisposeExternalAsciiString(v8::Persistent<v8::Value> obj, | |
3117 void* parameter) { | |
3118 ENTER_V8; | |
3119 i::ExternalAsciiString* str = | |
3120 i::ExternalAsciiString::cast(*Utils::OpenHandle(*obj)); | |
3121 | |
3122 // External symbols are deleted when they are pruned out of the symbol | |
3123 // table. Generally external symbols are not registered with the weak handle | |
3124 // callbacks unless they are upgraded to a symbol after being externalized. | |
3125 if (!str->IsSymbol()) { | |
3126 v8::String::ExternalAsciiStringResource* resource = | |
3127 reinterpret_cast<v8::String::ExternalAsciiStringResource*>(parameter); | |
3128 if (resource != NULL) { | |
3129 const int total_size = | |
3130 static_cast<int>(resource->length() * sizeof(*resource->data())); | |
3131 i::Counters::total_external_string_memory.Decrement(total_size); | |
3132 | |
3133 // The object will continue to live in the JavaScript heap until the | |
3134 // handle is entirely cleaned out by the next GC. For example the | |
3135 // destructor for the resource below could bring it back to life again. | |
3136 // Which is why we make sure to not have a dangling pointer here. | |
3137 str->set_resource(NULL); | |
3138 delete resource; | |
3139 } | |
3140 } | |
3141 | |
3142 // In any case we do not need this handle any longer. | |
3143 obj.Dispose(); | |
3144 } | |
3145 | |
3146 | |
3147 Local<String> v8::String::NewExternal( | 3085 Local<String> v8::String::NewExternal( |
3148 v8::String::ExternalStringResource* resource) { | 3086 v8::String::ExternalStringResource* resource) { |
3149 EnsureInitialized("v8::String::NewExternal()"); | 3087 EnsureInitialized("v8::String::NewExternal()"); |
3150 LOG_API("String::NewExternal"); | 3088 LOG_API("String::NewExternal"); |
3151 ENTER_V8; | 3089 ENTER_V8; |
3152 const int total_size = | |
3153 static_cast<int>(resource->length() * sizeof(*resource->data())); | |
3154 i::Counters::total_external_string_memory.Increment(total_size); | |
3155 i::Handle<i::String> result = NewExternalStringHandle(resource); | 3090 i::Handle<i::String> result = NewExternalStringHandle(resource); |
3156 i::Handle<i::Object> handle = i::GlobalHandles::Create(*result); | 3091 i::ExternalStringTable::AddString(*result); |
3157 i::GlobalHandles::MakeWeak(handle.location(), | |
3158 resource, | |
3159 &DisposeExternalString); | |
3160 return Utils::ToLocal(result); | 3092 return Utils::ToLocal(result); |
3161 } | 3093 } |
3162 | 3094 |
3163 | 3095 |
3164 bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) { | 3096 bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) { |
3165 if (IsDeadCheck("v8::String::MakeExternal()")) return false; | 3097 if (IsDeadCheck("v8::String::MakeExternal()")) return false; |
3166 if (this->IsExternal()) return false; // Already an external string. | 3098 if (this->IsExternal()) return false; // Already an external string. |
3167 ENTER_V8; | 3099 ENTER_V8; |
3168 i::Handle<i::String> obj = Utils::OpenHandle(this); | 3100 i::Handle<i::String> obj = Utils::OpenHandle(this); |
3169 bool result = obj->MakeExternal(resource); | 3101 bool result = obj->MakeExternal(resource); |
3170 if (result && !obj->IsSymbol()) { | 3102 if (result && !obj->IsSymbol()) { |
3171 // Operation was successful and the string is not a symbol. In this case | 3103 i::ExternalStringTable::AddString(*obj); |
3172 // we need to make sure that the we call the destructor for the external | |
3173 // resource when no strong references to the string remain. | |
3174 i::Handle<i::Object> handle = i::GlobalHandles::Create(*obj); | |
3175 i::GlobalHandles::MakeWeak(handle.location(), | |
3176 resource, | |
3177 &DisposeExternalString); | |
3178 } | 3104 } |
3179 return result; | 3105 return result; |
3180 } | 3106 } |
3181 | 3107 |
3182 | 3108 |
3183 Local<String> v8::String::NewExternal( | 3109 Local<String> v8::String::NewExternal( |
3184 v8::String::ExternalAsciiStringResource* resource) { | 3110 v8::String::ExternalAsciiStringResource* resource) { |
3185 EnsureInitialized("v8::String::NewExternal()"); | 3111 EnsureInitialized("v8::String::NewExternal()"); |
3186 LOG_API("String::NewExternal"); | 3112 LOG_API("String::NewExternal"); |
3187 ENTER_V8; | 3113 ENTER_V8; |
3188 const int total_size = | |
3189 static_cast<int>(resource->length() * sizeof(*resource->data())); | |
3190 i::Counters::total_external_string_memory.Increment(total_size); | |
3191 i::Handle<i::String> result = NewExternalAsciiStringHandle(resource); | 3114 i::Handle<i::String> result = NewExternalAsciiStringHandle(resource); |
3192 i::Handle<i::Object> handle = i::GlobalHandles::Create(*result); | 3115 i::ExternalStringTable::AddString(*result); |
3193 i::GlobalHandles::MakeWeak(handle.location(), | |
3194 resource, | |
3195 &DisposeExternalAsciiString); | |
3196 return Utils::ToLocal(result); | 3116 return Utils::ToLocal(result); |
3197 } | 3117 } |
3198 | 3118 |
3199 | 3119 |
3200 bool v8::String::MakeExternal( | 3120 bool v8::String::MakeExternal( |
3201 v8::String::ExternalAsciiStringResource* resource) { | 3121 v8::String::ExternalAsciiStringResource* resource) { |
3202 if (IsDeadCheck("v8::String::MakeExternal()")) return false; | 3122 if (IsDeadCheck("v8::String::MakeExternal()")) return false; |
3203 if (this->IsExternal()) return false; // Already an external string. | 3123 if (this->IsExternal()) return false; // Already an external string. |
3204 ENTER_V8; | 3124 ENTER_V8; |
3205 i::Handle<i::String> obj = Utils::OpenHandle(this); | 3125 i::Handle<i::String> obj = Utils::OpenHandle(this); |
3206 bool result = obj->MakeExternal(resource); | 3126 bool result = obj->MakeExternal(resource); |
3207 if (result && !obj->IsSymbol()) { | 3127 if (result && !obj->IsSymbol()) { |
3208 // Operation was successful and the string is not a symbol. In this case | 3128 i::ExternalStringTable::AddString(*obj); |
3209 // we need to make sure that the we call the destructor for the external | |
3210 // resource when no strong references to the string remain. | |
3211 i::Handle<i::Object> handle = i::GlobalHandles::Create(*obj); | |
3212 i::GlobalHandles::MakeWeak(handle.location(), | |
3213 resource, | |
3214 &DisposeExternalAsciiString); | |
3215 } | 3129 } |
3216 return result; | 3130 return result; |
3217 } | 3131 } |
3218 | 3132 |
3219 | 3133 |
3220 bool v8::String::CanMakeExternal() { | 3134 bool v8::String::CanMakeExternal() { |
3221 if (IsDeadCheck("v8::String::CanMakeExternal()")) return false; | 3135 if (IsDeadCheck("v8::String::CanMakeExternal()")) return false; |
3222 i::Handle<i::String> obj = Utils::OpenHandle(this); | 3136 i::Handle<i::String> obj = Utils::OpenHandle(this); |
3223 int size = obj->Size(); // Byte size of the original string. | 3137 int size = obj->Size(); // Byte size of the original string. |
3224 if (size < i::ExternalString::kSize) | 3138 if (size < i::ExternalString::kSize) |
(...skipping 683 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3908 | 3822 |
3909 | 3823 |
3910 char* HandleScopeImplementer::Iterate(ObjectVisitor* v, char* storage) { | 3824 char* HandleScopeImplementer::Iterate(ObjectVisitor* v, char* storage) { |
3911 HandleScopeImplementer* thread_local = | 3825 HandleScopeImplementer* thread_local = |
3912 reinterpret_cast<HandleScopeImplementer*>(storage); | 3826 reinterpret_cast<HandleScopeImplementer*>(storage); |
3913 thread_local->IterateThis(v); | 3827 thread_local->IterateThis(v); |
3914 return storage + ArchiveSpacePerThread(); | 3828 return storage + ArchiveSpacePerThread(); |
3915 } | 3829 } |
3916 | 3830 |
3917 } } // namespace v8::internal | 3831 } } // namespace v8::internal |
OLD | NEW |