Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/api.h" | 5 #include "src/api.h" |
| 6 | 6 |
| 7 #include <string.h> // For memcpy, strlen. | 7 #include <string.h> // For memcpy, strlen. |
| 8 #ifdef V8_USE_ADDRESS_SANITIZER | 8 #ifdef V8_USE_ADDRESS_SANITIZER |
| 9 #include <sanitizer/asan_interface.h> | 9 #include <sanitizer/asan_interface.h> |
| 10 #endif // V8_USE_ADDRESS_SANITIZER | 10 #endif // V8_USE_ADDRESS_SANITIZER |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 302 void V8::SetNativesDataBlob(StartupData* natives_blob) { | 302 void V8::SetNativesDataBlob(StartupData* natives_blob) { |
| 303 i::V8::SetNativesBlob(natives_blob); | 303 i::V8::SetNativesBlob(natives_blob); |
| 304 } | 304 } |
| 305 | 305 |
| 306 | 306 |
| 307 void V8::SetSnapshotDataBlob(StartupData* snapshot_blob) { | 307 void V8::SetSnapshotDataBlob(StartupData* snapshot_blob) { |
| 308 i::V8::SetSnapshotBlob(snapshot_blob); | 308 i::V8::SetSnapshotBlob(snapshot_blob); |
| 309 } | 309 } |
| 310 | 310 |
| 311 | 311 |
| 312 bool RunExtraCode(Isolate* isolate, const char* utf8_source) { | 312 bool RunExtraCode(Isolate* isolate, Local<Context> context, |
| 313 const char* utf8_source) { | |
| 313 // Run custom script if provided. | 314 // Run custom script if provided. |
| 314 base::ElapsedTimer timer; | 315 base::ElapsedTimer timer; |
| 315 timer.Start(); | 316 timer.Start(); |
| 316 TryCatch try_catch(isolate); | 317 TryCatch try_catch(isolate); |
| 317 Local<String> source_string = String::NewFromUtf8(isolate, utf8_source); | 318 Local<String> source_string; |
| 318 if (try_catch.HasCaught()) return false; | 319 if (!String::NewFromUtf8(isolate, utf8_source, NewStringType::kNormal) |
| 319 ScriptOrigin origin(String::NewFromUtf8(isolate, "<embedded script>")); | 320 .ToLocal(&source_string)) { |
| 321 return false; | |
| 322 } | |
| 323 Local<String> resource_name; | |
| 324 if (!String::NewFromUtf8(isolate, "<embedded script>", NewStringType::kNormal) | |
| 325 .ToLocal(&resource_name)) { | |
|
vogelheim
2015/06/03 08:23:13
How could this NewFromUtf8 call fail?
jochen (gone - plz use gerrit)
2015/06/03 09:43:57
it can't. Made it ToLocalChecked()
| |
| 326 return false; | |
| 327 } | |
| 328 ScriptOrigin origin(resource_name); | |
| 320 ScriptCompiler::Source source(source_string, origin); | 329 ScriptCompiler::Source source(source_string, origin); |
| 321 Local<Script> script = ScriptCompiler::Compile(isolate, &source); | 330 Local<Script> script; |
| 322 if (try_catch.HasCaught()) return false; | 331 if (!ScriptCompiler::Compile(context, &source).ToLocal(&script)) return false; |
| 323 script->Run(); | 332 if (script->Run(context).IsEmpty()) return false; |
| 324 if (i::FLAG_profile_deserialization) { | 333 if (i::FLAG_profile_deserialization) { |
| 325 i::PrintF("Executing custom snapshot script took %0.3f ms\n", | 334 i::PrintF("Executing custom snapshot script took %0.3f ms\n", |
| 326 timer.Elapsed().InMillisecondsF()); | 335 timer.Elapsed().InMillisecondsF()); |
| 327 } | 336 } |
| 328 timer.Stop(); | 337 timer.Stop(); |
| 329 return !try_catch.HasCaught(); | 338 CHECK(!try_catch.HasCaught()); |
| 339 return true; | |
| 330 } | 340 } |
| 331 | 341 |
| 332 | 342 |
| 333 namespace { | 343 namespace { |
| 334 | 344 |
| 335 class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | 345 class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { |
| 336 public: | 346 public: |
| 337 virtual void* Allocate(size_t length) { | 347 virtual void* Allocate(size_t length) { |
| 338 void* data = AllocateUninitialized(length); | 348 void* data = AllocateUninitialized(length); |
| 339 return data == NULL ? data : memset(data, 0, length); | 349 return data == NULL ? data : memset(data, 0, length); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 360 Persistent<Context> context; | 370 Persistent<Context> context; |
| 361 i::Snapshot::Metadata metadata; | 371 i::Snapshot::Metadata metadata; |
| 362 { | 372 { |
| 363 HandleScope handle_scope(isolate); | 373 HandleScope handle_scope(isolate); |
| 364 Handle<Context> new_context = Context::New(isolate); | 374 Handle<Context> new_context = Context::New(isolate); |
| 365 internal_isolate->set_creating_default_snapshot(false); | 375 internal_isolate->set_creating_default_snapshot(false); |
| 366 context.Reset(isolate, new_context); | 376 context.Reset(isolate, new_context); |
| 367 if (custom_source != NULL) { | 377 if (custom_source != NULL) { |
| 368 metadata.set_embeds_script(true); | 378 metadata.set_embeds_script(true); |
| 369 Context::Scope context_scope(new_context); | 379 Context::Scope context_scope(new_context); |
| 370 if (!RunExtraCode(isolate, custom_source)) context.Reset(); | 380 if (!RunExtraCode(isolate, new_context, custom_source)) context.Reset(); |
| 371 } | 381 } |
| 372 } | 382 } |
| 373 if (!context.IsEmpty()) { | 383 if (!context.IsEmpty()) { |
| 374 // Make sure all builtin scripts are cached. | 384 // Make sure all builtin scripts are cached. |
| 375 { | 385 { |
| 376 HandleScope scope(isolate); | 386 HandleScope scope(isolate); |
| 377 for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) { | 387 for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) { |
| 378 internal_isolate->bootstrapper()->SourceLookup<i::Natives>(i); | 388 internal_isolate->bootstrapper()->SourceLookup<i::Natives>(i); |
| 379 } | 389 } |
| 380 } | 390 } |
| (...skipping 3479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3860 auto v8_isolate = reinterpret_cast<v8::Isolate*>(isolate); | 3870 auto v8_isolate = reinterpret_cast<v8::Isolate*>(isolate); |
| 3861 i::Handle<i::Object> name(self->class_name(), isolate); | 3871 i::Handle<i::Object> name(self->class_name(), isolate); |
| 3862 i::Handle<i::Object> tag; | 3872 i::Handle<i::Object> tag; |
| 3863 | 3873 |
| 3864 // Native implementation of Object.prototype.toString (v8natives.js): | 3874 // Native implementation of Object.prototype.toString (v8natives.js): |
| 3865 // var c = %_ClassOf(this); | 3875 // var c = %_ClassOf(this); |
| 3866 // if (c === 'Arguments') c = 'Object'; | 3876 // if (c === 'Arguments') c = 'Object'; |
| 3867 // return "[object " + c + "]"; | 3877 // return "[object " + c + "]"; |
| 3868 | 3878 |
| 3869 if (!name->IsString()) { | 3879 if (!name->IsString()) { |
| 3870 return v8::String::NewFromUtf8(v8_isolate, "[object ]"); | 3880 return v8::String::NewFromUtf8(v8_isolate, "[object ]", |
| 3881 NewStringType::kNormal); | |
| 3871 } | 3882 } |
| 3872 auto class_name = i::Handle<i::String>::cast(name); | 3883 auto class_name = i::Handle<i::String>::cast(name); |
| 3873 if (i::String::Equals(class_name, isolate->factory()->Arguments_string())) { | 3884 if (i::String::Equals(class_name, isolate->factory()->Arguments_string())) { |
| 3874 return v8::String::NewFromUtf8(v8_isolate, "[object Object]"); | 3885 return v8::String::NewFromUtf8(v8_isolate, "[object Object]", |
| 3886 NewStringType::kNormal); | |
| 3875 } | 3887 } |
| 3876 if (internal::FLAG_harmony_tostring) { | 3888 if (internal::FLAG_harmony_tostring) { |
| 3877 PREPARE_FOR_EXECUTION(context, "v8::Object::ObjectProtoToString()", String); | 3889 PREPARE_FOR_EXECUTION(context, "v8::Object::ObjectProtoToString()", String); |
| 3878 auto toStringTag = isolate->factory()->to_string_tag_symbol(); | 3890 auto toStringTag = isolate->factory()->to_string_tag_symbol(); |
| 3879 has_pending_exception = !i::Runtime::GetObjectProperty( | 3891 has_pending_exception = !i::Runtime::GetObjectProperty( |
| 3880 isolate, self, toStringTag).ToHandle(&tag); | 3892 isolate, self, toStringTag).ToHandle(&tag); |
| 3881 RETURN_ON_FAILED_EXECUTION(String); | 3893 RETURN_ON_FAILED_EXECUTION(String); |
| 3882 if (tag->IsString()) { | 3894 if (tag->IsString()) { |
| 3883 class_name = i::Handle<i::String>::cast(tag).EscapeFrom(&handle_scope); | 3895 class_name = i::Handle<i::String>::cast(tag).EscapeFrom(&handle_scope); |
| 3884 } | 3896 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 3900 ptr += prefix_len; | 3912 ptr += prefix_len; |
| 3901 | 3913 |
| 3902 // Write real content. | 3914 // Write real content. |
| 3903 str->WriteUtf8(ptr, str_len); | 3915 str->WriteUtf8(ptr, str_len); |
| 3904 ptr += str_len; | 3916 ptr += str_len; |
| 3905 | 3917 |
| 3906 // Write postfix. | 3918 // Write postfix. |
| 3907 i::MemCopy(ptr, postfix, postfix_len * v8::internal::kCharSize); | 3919 i::MemCopy(ptr, postfix, postfix_len * v8::internal::kCharSize); |
| 3908 | 3920 |
| 3909 // Copy the buffer into a heap-allocated string and return it. | 3921 // Copy the buffer into a heap-allocated string and return it. |
| 3910 return v8::String::NewFromUtf8(v8_isolate, buf.start(), String::kNormalString, | 3922 return v8::String::NewFromUtf8(v8_isolate, buf.start(), |
| 3911 buf_len); | 3923 NewStringType::kNormal, buf_len); |
| 3912 } | 3924 } |
| 3913 | 3925 |
| 3914 | 3926 |
| 3915 Local<String> v8::Object::ObjectProtoToString() { | 3927 Local<String> v8::Object::ObjectProtoToString() { |
| 3916 auto context = ContextFromHeapObject(Utils::OpenHandle(this)); | 3928 auto context = ContextFromHeapObject(Utils::OpenHandle(this)); |
| 3917 RETURN_TO_LOCAL_UNCHECKED(ObjectProtoToString(context), String); | 3929 RETURN_TO_LOCAL_UNCHECKED(ObjectProtoToString(context), String); |
| 3918 } | 3930 } |
| 3919 | 3931 |
| 3920 | 3932 |
| 3921 Local<String> v8::Object::GetConstructorName() { | 3933 Local<String> v8::Object::GetConstructorName() { |
| (...skipping 1473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 5395 return Utils::ToLocal(value); | 5407 return Utils::ToLocal(value); |
| 5396 } | 5408 } |
| 5397 | 5409 |
| 5398 | 5410 |
| 5399 void v8::Object::SetInternalField(int index, v8::Handle<Value> value) { | 5411 void v8::Object::SetInternalField(int index, v8::Handle<Value> value) { |
| 5400 i::Handle<i::JSObject> obj = Utils::OpenHandle(this); | 5412 i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
| 5401 const char* location = "v8::Object::SetInternalField()"; | 5413 const char* location = "v8::Object::SetInternalField()"; |
| 5402 if (!InternalFieldOK(obj, index, location)) return; | 5414 if (!InternalFieldOK(obj, index, location)) return; |
| 5403 i::Handle<i::Object> val = Utils::OpenHandle(*value); | 5415 i::Handle<i::Object> val = Utils::OpenHandle(*value); |
| 5404 obj->SetInternalField(index, *val); | 5416 obj->SetInternalField(index, *val); |
| 5405 DCHECK(value->Equals(GetInternalField(index))); | |
| 5406 } | 5417 } |
| 5407 | 5418 |
| 5408 | 5419 |
| 5409 void* v8::Object::SlowGetAlignedPointerFromInternalField(int index) { | 5420 void* v8::Object::SlowGetAlignedPointerFromInternalField(int index) { |
| 5410 i::Handle<i::JSObject> obj = Utils::OpenHandle(this); | 5421 i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
| 5411 const char* location = "v8::Object::GetAlignedPointerFromInternalField()"; | 5422 const char* location = "v8::Object::GetAlignedPointerFromInternalField()"; |
| 5412 if (!InternalFieldOK(obj, index, location)) return NULL; | 5423 if (!InternalFieldOK(obj, index, location)) return NULL; |
| 5413 return DecodeSmiToAligned(obj->GetInternalField(index), location); | 5424 return DecodeSmiToAligned(obj->GetInternalField(index), location); |
| 5414 } | 5425 } |
| 5415 | 5426 |
| (...skipping 2083 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 7499 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); | 7510 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 7500 i::DisallowHeapAllocation no_allocation; | 7511 i::DisallowHeapAllocation no_allocation; |
| 7501 VisitorAdapter visitor_adapter(visitor); | 7512 VisitorAdapter visitor_adapter(visitor); |
| 7502 isolate->global_handles()->IterateAllRootsInNewSpaceWithClassIds( | 7513 isolate->global_handles()->IterateAllRootsInNewSpaceWithClassIds( |
| 7503 &visitor_adapter); | 7514 &visitor_adapter); |
| 7504 } | 7515 } |
| 7505 | 7516 |
| 7506 | 7517 |
| 7507 String::Utf8Value::Utf8Value(v8::Handle<v8::Value> obj) | 7518 String::Utf8Value::Utf8Value(v8::Handle<v8::Value> obj) |
| 7508 : str_(NULL), length_(0) { | 7519 : str_(NULL), length_(0) { |
| 7520 if (obj.IsEmpty()) return; | |
| 7509 i::Isolate* isolate = i::Isolate::Current(); | 7521 i::Isolate* isolate = i::Isolate::Current(); |
| 7510 Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate); | 7522 Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate); |
| 7511 if (obj.IsEmpty()) return; | |
| 7512 ENTER_V8(isolate); | 7523 ENTER_V8(isolate); |
| 7513 i::HandleScope scope(isolate); | 7524 i::HandleScope scope(isolate); |
| 7525 Local<Context> context = v8_isolate->GetCurrentContext(); | |
| 7514 TryCatch try_catch(v8_isolate); | 7526 TryCatch try_catch(v8_isolate); |
| 7515 Handle<String> str = obj->ToString(v8_isolate); | 7527 Handle<String> str; |
| 7516 if (str.IsEmpty()) return; | 7528 if (!obj->ToString(context).ToLocal(&str)) return; |
| 7517 i::Handle<i::String> i_str = Utils::OpenHandle(*str); | 7529 i::Handle<i::String> i_str = Utils::OpenHandle(*str); |
| 7518 length_ = v8::Utf8Length(*i_str, isolate); | 7530 length_ = v8::Utf8Length(*i_str, isolate); |
| 7519 str_ = i::NewArray<char>(length_ + 1); | 7531 str_ = i::NewArray<char>(length_ + 1); |
| 7520 str->WriteUtf8(str_); | 7532 str->WriteUtf8(str_); |
| 7521 } | 7533 } |
| 7522 | 7534 |
| 7523 | 7535 |
| 7524 String::Utf8Value::~Utf8Value() { | 7536 String::Utf8Value::~Utf8Value() { |
| 7525 i::DeleteArray(str_); | 7537 i::DeleteArray(str_); |
| 7526 } | 7538 } |
| 7527 | 7539 |
| 7528 | 7540 |
| 7529 String::Value::Value(v8::Handle<v8::Value> obj) | 7541 String::Value::Value(v8::Handle<v8::Value> obj) |
| 7530 : str_(NULL), length_(0) { | 7542 : str_(NULL), length_(0) { |
| 7543 if (obj.IsEmpty()) return; | |
| 7531 i::Isolate* isolate = i::Isolate::Current(); | 7544 i::Isolate* isolate = i::Isolate::Current(); |
| 7532 Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate); | 7545 Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate); |
| 7533 if (obj.IsEmpty()) return; | |
| 7534 ENTER_V8(isolate); | 7546 ENTER_V8(isolate); |
| 7535 i::HandleScope scope(isolate); | 7547 i::HandleScope scope(isolate); |
| 7548 Local<Context> context = v8_isolate->GetCurrentContext(); | |
| 7536 TryCatch try_catch(v8_isolate); | 7549 TryCatch try_catch(v8_isolate); |
| 7537 Handle<String> str = obj->ToString(v8_isolate); | 7550 Handle<String> str; |
| 7538 if (str.IsEmpty()) return; | 7551 if (!obj->ToString(context).ToLocal(&str)) return; |
| 7539 length_ = str->Length(); | 7552 length_ = str->Length(); |
| 7540 str_ = i::NewArray<uint16_t>(length_ + 1); | 7553 str_ = i::NewArray<uint16_t>(length_ + 1); |
| 7541 str->Write(str_); | 7554 str->Write(str_); |
| 7542 } | 7555 } |
| 7543 | 7556 |
| 7544 | 7557 |
| 7545 String::Value::~Value() { | 7558 String::Value::~Value() { |
| 7546 i::DeleteArray(str_); | 7559 i::DeleteArray(str_); |
| 7547 } | 7560 } |
| 7548 | 7561 |
| (...skipping 813 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 8362 Address callback_address = | 8375 Address callback_address = |
| 8363 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); | 8376 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); |
| 8364 VMState<EXTERNAL> state(isolate); | 8377 VMState<EXTERNAL> state(isolate); |
| 8365 ExternalCallbackScope call_scope(isolate, callback_address); | 8378 ExternalCallbackScope call_scope(isolate, callback_address); |
| 8366 callback(info); | 8379 callback(info); |
| 8367 } | 8380 } |
| 8368 | 8381 |
| 8369 | 8382 |
| 8370 } // namespace internal | 8383 } // namespace internal |
| 8371 } // namespace v8 | 8384 } // namespace v8 |
| OLD | NEW |