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

Side by Side Diff: src/api.cc

Issue 1509533003: Rewrite Object.prototype.toString in C++ (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebase, add back experimental flag Created 5 years 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
« no previous file with comments | « no previous file | src/bootstrapper.cc » ('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 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 3763 matching lines...) Expand 10 before | Expand all | Expand 10 after
3774 } 3774 }
3775 3775
3776 3776
3777 Local<Array> v8::Object::GetOwnPropertyNames() { 3777 Local<Array> v8::Object::GetOwnPropertyNames() {
3778 auto context = ContextFromHeapObject(Utils::OpenHandle(this)); 3778 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3779 RETURN_TO_LOCAL_UNCHECKED(GetOwnPropertyNames(context), Array); 3779 RETURN_TO_LOCAL_UNCHECKED(GetOwnPropertyNames(context), Array);
3780 } 3780 }
3781 3781
3782 3782
3783 MaybeLocal<String> v8::Object::ObjectProtoToString(Local<Context> context) { 3783 MaybeLocal<String> v8::Object::ObjectProtoToString(Local<Context> context) {
3784 auto self = Utils::OpenHandle(this); 3784 PREPARE_FOR_EXECUTION(context, "v8::Object::ObjectProtoToString", String);
3785 auto isolate = self->GetIsolate(); 3785 auto obj = Utils::OpenHandle(this);
3786 auto v8_isolate = reinterpret_cast<v8::Isolate*>(isolate); 3786 Local<String> result;
3787 i::Handle<i::Object> name(self->class_name(), isolate); 3787 has_pending_exception =
3788 i::Handle<i::Object> tag; 3788 !ToLocal<String>(i::JSObject::ObjectProtoToString(isolate, obj), &result);
3789 3789 RETURN_ON_FAILED_EXECUTION(String);
3790 // Native implementation of Object.prototype.toString (v8natives.js): 3790 RETURN_ESCAPED(result);
3791 // var c = %_ClassOf(this);
3792 // if (c === 'Arguments') c = 'Object';
3793 // return "[object " + c + "]";
3794
3795 if (!name->IsString()) {
3796 return v8::String::NewFromUtf8(v8_isolate, "[object ]",
3797 NewStringType::kNormal);
3798 }
3799 auto class_name = i::Handle<i::String>::cast(name);
3800 if (i::String::Equals(class_name, isolate->factory()->Arguments_string())) {
3801 return v8::String::NewFromUtf8(v8_isolate, "[object Object]",
3802 NewStringType::kNormal);
3803 }
3804 if (internal::FLAG_harmony_tostring) {
3805 PREPARE_FOR_EXECUTION(context, "v8::Object::ObjectProtoToString()", String);
3806 auto toStringTag = isolate->factory()->to_string_tag_symbol();
3807 has_pending_exception = !i::Runtime::GetObjectProperty(
3808 isolate, self, toStringTag).ToHandle(&tag);
3809 RETURN_ON_FAILED_EXECUTION(String);
3810 if (tag->IsString()) {
3811 class_name = Utils::OpenHandle(*handle_scope.Escape(
3812 Utils::ToLocal(i::Handle<i::String>::cast(tag))));
3813 }
3814 }
3815 const char* prefix = "[object ";
3816 Local<String> str = Utils::ToLocal(class_name);
3817 const char* postfix = "]";
3818
3819 int prefix_len = i::StrLength(prefix);
3820 int str_len = str->Utf8Length();
3821 int postfix_len = i::StrLength(postfix);
3822
3823 int buf_len = prefix_len + str_len + postfix_len;
3824 i::ScopedVector<char> buf(buf_len);
3825
3826 // Write prefix.
3827 char* ptr = buf.start();
3828 i::MemCopy(ptr, prefix, prefix_len * v8::internal::kCharSize);
3829 ptr += prefix_len;
3830
3831 // Write real content.
3832 str->WriteUtf8(ptr, str_len);
3833 ptr += str_len;
3834
3835 // Write postfix.
3836 i::MemCopy(ptr, postfix, postfix_len * v8::internal::kCharSize);
3837
3838 // Copy the buffer into a heap-allocated string and return it.
3839 return v8::String::NewFromUtf8(v8_isolate, buf.start(),
3840 NewStringType::kNormal, buf_len);
3841 } 3791 }
3842 3792
3843 3793
3844 Local<String> v8::Object::ObjectProtoToString() { 3794 Local<String> v8::Object::ObjectProtoToString() {
3845 auto context = ContextFromHeapObject(Utils::OpenHandle(this)); 3795 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3846 RETURN_TO_LOCAL_UNCHECKED(ObjectProtoToString(context), String); 3796 RETURN_TO_LOCAL_UNCHECKED(ObjectProtoToString(context), String);
3847 } 3797 }
3848 3798
3849 3799
3850 Local<String> v8::Object::GetConstructorName() { 3800 Local<String> v8::Object::GetConstructorName() {
(...skipping 4682 matching lines...) Expand 10 before | Expand all | Expand 10 after
8533 Address callback_address = 8483 Address callback_address =
8534 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); 8484 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
8535 VMState<EXTERNAL> state(isolate); 8485 VMState<EXTERNAL> state(isolate);
8536 ExternalCallbackScope call_scope(isolate, callback_address); 8486 ExternalCallbackScope call_scope(isolate, callback_address);
8537 callback(info); 8487 callback(info);
8538 } 8488 }
8539 8489
8540 8490
8541 } // namespace internal 8491 } // namespace internal
8542 } // namespace v8 8492 } // namespace v8
OLDNEW
« no previous file with comments | « no previous file | src/bootstrapper.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698