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

Side by Side Diff: src/api.cc

Issue 546803003: Update ObjectToString to Harmony-draft algorithm (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Add lots more tests, make it safer Created 6 years, 2 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 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 3352 matching lines...) Expand 10 before | Expand all | Expand 10 after
3363 // Because we use caching to speed up enumeration it is important 3363 // Because we use caching to speed up enumeration it is important
3364 // to never change the result of the basic enumeration function so 3364 // to never change the result of the basic enumeration function so
3365 // we clone the result. 3365 // we clone the result.
3366 i::Handle<i::FixedArray> elms = isolate->factory()->CopyFixedArray(value); 3366 i::Handle<i::FixedArray> elms = isolate->factory()->CopyFixedArray(value);
3367 i::Handle<i::JSArray> result = 3367 i::Handle<i::JSArray> result =
3368 isolate->factory()->NewJSArrayWithElements(elms); 3368 isolate->factory()->NewJSArrayWithElements(elms);
3369 return Utils::ToLocal(scope.CloseAndEscape(result)); 3369 return Utils::ToLocal(scope.CloseAndEscape(result));
3370 } 3370 }
3371 3371
3372 3372
3373 static bool GetPredefinedToString(
3374 i::Handle<i::String> tag, Local<String>* result) {
3375 i::Isolate* i_isolate = tag->GetIsolate();
3376 Isolate* isolate = reinterpret_cast<Isolate*>(i_isolate);
3377 i::Factory* factory = i_isolate->factory();
3378
3379 if (i::String::Equals(tag, factory->Arguments_string())) {
3380 *result = v8::String::NewFromUtf8(isolate, "[object ~Arguments]");
3381 } else if (i::String::Equals(tag, factory->Array_string())) {
3382 *result = v8::String::NewFromUtf8(isolate, "[object ~Array]");
3383 } else if (i::String::Equals(tag, factory->Boolean_string())) {
3384 *result = v8::String::NewFromUtf8(isolate, "[object ~Boolean]");
3385 } else if (i::String::Equals(tag, factory->Date_string())) {
3386 *result = v8::String::NewFromUtf8(isolate, "[object ~Date]");
3387 } else if (i::String::Equals(tag, factory->Error_string())) {
3388 *result = v8::String::NewFromUtf8(isolate, "[object ~Error]");
3389 } else if (i::String::Equals(tag, factory->Function_string())) {
3390 *result = v8::String::NewFromUtf8(isolate, "[object ~Function]");
3391 } else if (i::String::Equals(tag, factory->Number_string())) {
3392 *result = v8::String::NewFromUtf8(isolate, "[object ~Number]");
3393 } else if (i::String::Equals(tag, factory->RegExp_string())) {
3394 *result = v8::String::NewFromUtf8(isolate, "[object ~RegExp]");
3395 } else if (i::String::Equals(tag, factory->String_string())) {
3396 *result = v8::String::NewFromUtf8(isolate, "[object ~String]");
3397 } else {
3398 return false;
3399 }
3400 return true;
3401 }
3402
3403
3373 Local<String> v8::Object::ObjectProtoToString() { 3404 Local<String> v8::Object::ObjectProtoToString() {
3374 i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate(); 3405 i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate();
3375 Isolate* isolate = reinterpret_cast<Isolate*>(i_isolate); 3406 Isolate* isolate = reinterpret_cast<Isolate*>(i_isolate);
3376 ON_BAILOUT(i_isolate, "v8::Object::ObjectProtoToString()", 3407 ON_BAILOUT(i_isolate, "v8::Object::ObjectProtoToString()",
3377 return Local<v8::String>()); 3408 return Local<v8::String>());
3378 ENTER_V8(i_isolate); 3409 ENTER_V8(i_isolate);
3379 i::Handle<i::JSObject> self = Utils::OpenHandle(this); 3410 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3380 3411
3381 i::Handle<i::Object> name(self->class_name(), i_isolate); 3412 i::Handle<i::Object> name(self->class_name(), i_isolate);
3413 i::Handle<i::Object> tag;
3382 3414
3383 // Native implementation of Object.prototype.toString (v8natives.js): 3415 // Native implementation of Object.prototype.toString (v8natives.js):
3384 // var c = %_ClassOf(this); 3416 // var c = %_ClassOf(this);
3385 // if (c === 'Arguments') c = 'Object'; 3417 // if (c === 'Arguments') c = 'Object';
3386 // return "[object " + c + "]"; 3418 // return "[object " + c + "]";
3387 3419
3388 if (!name->IsString()) { 3420 if (!name->IsString()) {
3389 return v8::String::NewFromUtf8(isolate, "[object ]"); 3421 return v8::String::NewFromUtf8(isolate, "[object ]");
3390 } else { 3422 } else {
3391 i::Handle<i::String> class_name = i::Handle<i::String>::cast(name); 3423 i::Handle<i::String> class_name = i::Handle<i::String>::cast(name);
3392 if (i::String::Equals(class_name, 3424 if (i::String::Equals(class_name,
3393 i_isolate->factory()->Arguments_string())) { 3425 i_isolate->factory()->Arguments_string())) {
3394 return v8::String::NewFromUtf8(isolate, "[object Object]"); 3426 return v8::String::NewFromUtf8(isolate, "[object Object]");
3395 } else { 3427 } else {
3428 if (internal::FLAG_harmony_tostring) {
3429 i::Handle<i::Symbol> toStringTag = Utils::OpenHandle(
3430 *Symbol::GetToStringTag(isolate));
3431 EXCEPTION_PREAMBLE(i_isolate);
3432 has_pending_exception = !i::Runtime::GetObjectProperty(
3433 i_isolate, self, toStringTag).ToHandle(&tag);
3434 EXCEPTION_BAILOUT_CHECK(i_isolate, Local<v8::String>());
3435
3436 if (!tag->IsUndefined()) {
3437 if (!tag->IsString()) {
3438 return v8::String::NewFromUtf8(isolate, "[object ???]");
3439 } else {
3440 i::Handle<i::String> tag_name = i::Handle<i::String>::cast(tag);
3441 if (!i::String::Equals(class_name, tag_name)) {
3442 Local<String> result;
3443 if (GetPredefinedToString(tag_name, &result)) {
Dmitry Lomov (no reviews) 2014/10/18 21:24:19 Nits: Better formatting: if (GetPredefinedToS
3444 return result;
3445 } else {
3446 class_name = tag_name;
3447 }
3448 }
3449 }
3450 }
3451 }
3396 const char* prefix = "[object "; 3452 const char* prefix = "[object ";
3397 Local<String> str = Utils::ToLocal(class_name); 3453 Local<String> str = Utils::ToLocal(class_name);
3398 const char* postfix = "]"; 3454 const char* postfix = "]";
3399 3455
3400 int prefix_len = i::StrLength(prefix); 3456 int prefix_len = i::StrLength(prefix);
3401 int str_len = str->Utf8Length(); 3457 int str_len = str->Utf8Length();
3402 int postfix_len = i::StrLength(postfix); 3458 int postfix_len = i::StrLength(postfix);
3403 3459
3404 int buf_len = prefix_len + str_len + postfix_len; 3460 int buf_len = prefix_len + str_len + postfix_len;
3405 i::ScopedVector<char> buf(buf_len); 3461 i::ScopedVector<char> buf(buf_len);
(...skipping 2786 matching lines...) Expand 10 before | Expand all | Expand 10 after
6192 Local<Symbol> v8::Symbol::GetIterator(Isolate* isolate) { 6248 Local<Symbol> v8::Symbol::GetIterator(Isolate* isolate) {
6193 return GetWellKnownSymbol(isolate, "Symbol.iterator"); 6249 return GetWellKnownSymbol(isolate, "Symbol.iterator");
6194 } 6250 }
6195 6251
6196 6252
6197 Local<Symbol> v8::Symbol::GetUnscopables(Isolate* isolate) { 6253 Local<Symbol> v8::Symbol::GetUnscopables(Isolate* isolate) {
6198 return GetWellKnownSymbol(isolate, "Symbol.unscopables"); 6254 return GetWellKnownSymbol(isolate, "Symbol.unscopables");
6199 } 6255 }
6200 6256
6201 6257
6258 Local<Symbol> v8::Symbol::GetToStringTag(Isolate* isolate) {
6259 return GetWellKnownSymbol(isolate, "Symbol.toStringTag");
6260 }
6261
6262
6202 Local<Private> v8::Private::New(Isolate* isolate, Local<String> name) { 6263 Local<Private> v8::Private::New(Isolate* isolate, Local<String> name) {
6203 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); 6264 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6204 LOG_API(i_isolate, "Private::New()"); 6265 LOG_API(i_isolate, "Private::New()");
6205 ENTER_V8(i_isolate); 6266 ENTER_V8(i_isolate);
6206 i::Handle<i::Symbol> symbol = i_isolate->factory()->NewPrivateSymbol(); 6267 i::Handle<i::Symbol> symbol = i_isolate->factory()->NewPrivateSymbol();
6207 if (!name.IsEmpty()) symbol->set_name(*Utils::OpenHandle(*name)); 6268 if (!name.IsEmpty()) symbol->set_name(*Utils::OpenHandle(*name));
6208 Local<Symbol> result = Utils::ToLocal(symbol); 6269 Local<Symbol> result = Utils::ToLocal(symbol);
6209 return v8::Handle<Private>(reinterpret_cast<Private*>(*result)); 6270 return v8::Handle<Private>(reinterpret_cast<Private*>(*result));
6210 } 6271 }
6211 6272
(...skipping 1449 matching lines...) Expand 10 before | Expand all | Expand 10 after
7661 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate()); 7722 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
7662 Address callback_address = 7723 Address callback_address =
7663 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); 7724 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
7664 VMState<EXTERNAL> state(isolate); 7725 VMState<EXTERNAL> state(isolate);
7665 ExternalCallbackScope call_scope(isolate, callback_address); 7726 ExternalCallbackScope call_scope(isolate, callback_address);
7666 callback(info); 7727 callback(info);
7667 } 7728 }
7668 7729
7669 7730
7670 } } // namespace v8::internal 7731 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « include/v8.h ('k') | src/arraybuffer.js » ('j') | test/cctest/test-api.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698