use constant string for symbol tostring

Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/IAXL05
Signed-off-by: ZhouGuangyuan <zhouguangyuan1@huawei.com>
Change-Id: Id7bbb290b33ea41f37606d0cd6485617fb000fca
This commit is contained in:
zhouguangyuan 2024-10-16 17:18:28 +08:00
parent 500b7116e1
commit 2855001664
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

@ -14,3 +14,5 @@
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}
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());