!9817 use constant string for symbol tostring

Merge pull request !9817 from zhouguangyuan0718/master-symbol
This commit is contained in:
openharmony_ci 2024-10-17 22:46:52 +00:00 committed by Gitee
commit f87f2186e1
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 16 additions and 7 deletions

View File

@ -15,6 +15,7 @@
#include "ecmascript/builtins/builtins_symbol.h"
#include "ecmascript/ecma_string_table.h"
#include "ecmascript/global_env.h"
#include "ecmascript/js_primitive_ref.h"
#include "ecmascript/symbol_table.h"
@ -91,18 +92,19 @@ JSTaggedValue BuiltinsSymbol::SymbolDescriptiveString(JSThread *thread, JSTagged
JSHandle<JSTaggedValue> descHandle(thread, symbolObject->GetDescription());
// If desc is undefined, let desc be the empty string.
JSHandle<SingleCharTable> singleCharTable(thread, thread->GetSingleCharTable());
auto constants = thread->GlobalConstants();
if (descHandle->IsUndefined()) {
JSHandle<EcmaString> leftHandle(factory->NewFromASCII("Symbol("));
JSHandle<EcmaString> rightHandle(factory->NewFromASCII(")"));
JSHandle<EcmaString> leftHandle = JSHandle<EcmaString>::Cast(constants->GetHandledSymbolLeftParentheses());
JSHandle<EcmaString> rightHandle(thread, singleCharTable->GetStringFromSingleCharTable(')'));
JSHandle<EcmaString> str = factory->ConcatFromString(leftHandle, rightHandle);
return str.GetTaggedValue();
}
// Assert: Type(desc) is String.
ASSERT(descHandle->IsString());
// Return the result of concatenating the strings "Symbol(", desc, and ")".
JSHandle<EcmaString> leftHandle(factory->NewFromASCII("Symbol("));
JSHandle<EcmaString> rightHandle(factory->NewFromASCII(")"));
JSHandle<EcmaString> leftHandle = JSHandle<EcmaString>::Cast(constants->GetHandledSymbolLeftParentheses());
JSHandle<EcmaString> rightHandle(thread, singleCharTable->GetStringFromSingleCharTable(')'));
JSHandle<EcmaString> stringLeft =
factory->ConcatFromString(leftHandle, JSTaggedValue::ToString(thread, descHandle));
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);

View File

@ -629,7 +629,8 @@ class ObjectFactory;
V(AppSpawnFullGcCause, APP_SPAWN_FULL_GC_CAUSE, "app_spawn_full") \
V(SharedGcCause, SHARED_GC_CAUSE, "shared") \
V(SharedFullGcCause, SHARED_FULL_GC_CAUSE, "shared_full") \
V(AppSpawnSharedFullGcCause, APP_SPAWN_SHARED_FULL_GC_CAUSE, "app_spawn_shared_full")
V(AppSpawnSharedFullGcCause, APP_SPAWN_SHARED_FULL_GC_CAUSE, "app_spawn_shared_full") \
V(SymbolLeftParentheses, SYMBOL_LEFT_PARENTHESES, "Symbol(")
/* GlobalConstant */
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)

View File

@ -13,4 +13,6 @@
test Symbol.prototype.toString successful !!!
test Symbol.prototype.valueOf successful !!!
{"at":true,"copyWithin":true,"entries":true,"fill":true,"find":true,"findIndex":true,"findLast":true,"findLastIndex":true,"flat":true,"flatMap":true,"includes":true,"keys":true,"values":true,"toReversed":true,"toSorted":true,"toSpliced":true}
{"at":true,"copyWithin":true,"entries":true,"fill":true,"find":true,"findIndex":true,"findLast":true,"findLastIndex":true,"flat":true,"flatMap":true,"includes":true,"keys":true,"values":true,"toReversed":true,"toSorted":true,"toSpliced":true}
Symbol(foo)
Symbol(foo)

View File

@ -33,3 +33,7 @@ try {
print("test Symbol.prototype.valueOf successful !!!");
}
print(JSON.stringify(Array.prototype[Symbol.unscopables]));
print(Symbol('foo').toString());
print(Symbol.for("foo").toString());