14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_symbol.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/ecma_string_table.h" 194514f5e3Sopenharmony_ci#include "ecmascript/global_env.h" 204514f5e3Sopenharmony_ci#include "ecmascript/js_primitive_ref.h" 214514f5e3Sopenharmony_ci#include "ecmascript/symbol_table.h" 224514f5e3Sopenharmony_ci 234514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins { 244514f5e3Sopenharmony_ci// prototype 254514f5e3Sopenharmony_ci// 19.4.3.1 264514f5e3Sopenharmony_ci// constructor 274514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSymbol::SymbolConstructor(EcmaRuntimeCallInfo *argv) 284514f5e3Sopenharmony_ci{ 294514f5e3Sopenharmony_ci ASSERT(argv); 304514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), Symbol, Constructor); 314514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 324514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 334514f5e3Sopenharmony_ci // 1.If NewTarget is not undefined, throw a TypeError exception. 344514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key = GetCallArg(argv, 0); 354514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv); 364514f5e3Sopenharmony_ci if (!newTarget->IsUndefined()) { 374514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "SymbolConstructor: NewTarget is not undefined", 384514f5e3Sopenharmony_ci JSTaggedValue::Exception()); 394514f5e3Sopenharmony_ci } 404514f5e3Sopenharmony_ci // 2.If description is undefined, let descString be undefined. 414514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 424514f5e3Sopenharmony_ci if (key->IsUndefined()) { 434514f5e3Sopenharmony_ci JSHandle<JSSymbol> jsSymbol = factory->NewJSSymbol(); 444514f5e3Sopenharmony_ci return jsSymbol.GetTaggedValue(); 454514f5e3Sopenharmony_ci } 464514f5e3Sopenharmony_ci // 3.Else, let descString be ToString(description). 474514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> descHandle = JSHandle<JSTaggedValue>::Cast(JSTaggedValue::ToString(thread, key)); 484514f5e3Sopenharmony_ci // 4.ReturnIfAbrupt(descString). 494514f5e3Sopenharmony_ci RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::Exception()); 504514f5e3Sopenharmony_ci // 5.Return a new unique Symbol value whose [[Description]] value is descString. 514514f5e3Sopenharmony_ci JSHandle<JSSymbol> jsSymbol = factory->NewPublicSymbol(descHandle); 524514f5e3Sopenharmony_ci return jsSymbol.GetTaggedValue(); 534514f5e3Sopenharmony_ci} 544514f5e3Sopenharmony_ci 554514f5e3Sopenharmony_ci// 19.4.3.2 Symbol.prototype.toString() 564514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSymbol::ToString(EcmaRuntimeCallInfo *argv) 574514f5e3Sopenharmony_ci{ 584514f5e3Sopenharmony_ci ASSERT(argv); 594514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), Symbol, ToString); 604514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 614514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 624514f5e3Sopenharmony_ci // Let s be the this value. 634514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> valueHandle = GetThis(argv); 644514f5e3Sopenharmony_ci // 1.If value is a Symbol, return value. 654514f5e3Sopenharmony_ci if (valueHandle->IsSymbol()) { 664514f5e3Sopenharmony_ci // Return SymbolDescriptiveString(sym). 674514f5e3Sopenharmony_ci return SymbolDescriptiveString(thread, valueHandle.GetTaggedValue()); 684514f5e3Sopenharmony_ci } 694514f5e3Sopenharmony_ci 704514f5e3Sopenharmony_ci // 2.If value is an Object and value has a [[SymbolData]] internal slot, then 714514f5e3Sopenharmony_ci if (valueHandle->IsJSPrimitiveRef()) { 724514f5e3Sopenharmony_ci // Let sym be the value of s's [[SymbolData]] internal slot. 734514f5e3Sopenharmony_ci JSTaggedValue primitive = JSPrimitiveRef::Cast(valueHandle->GetTaggedObject())->GetValue(); 744514f5e3Sopenharmony_ci if (primitive.IsSymbol()) { 754514f5e3Sopenharmony_ci return SymbolDescriptiveString(thread, primitive); 764514f5e3Sopenharmony_ci } 774514f5e3Sopenharmony_ci } 784514f5e3Sopenharmony_ci 794514f5e3Sopenharmony_ci // 3.If s does not have a [[SymbolData]] internal slot, throw a TypeError exception. 804514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "ToString: no [[SymbolData]]", JSTaggedValue::Exception()); 814514f5e3Sopenharmony_ci} 824514f5e3Sopenharmony_ci 834514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSymbol::SymbolDescriptiveString(JSThread *thread, JSTaggedValue sym) 844514f5e3Sopenharmony_ci{ 854514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, Symbol, SymbolDescriptiveString); 864514f5e3Sopenharmony_ci // Assert: Type(sym) is Symbol. 874514f5e3Sopenharmony_ci ASSERT(sym.IsSymbol()); 884514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 894514f5e3Sopenharmony_ci 904514f5e3Sopenharmony_ci // Let desc be sym’s [[Description]] value. 914514f5e3Sopenharmony_ci auto symbolObject = reinterpret_cast<JSSymbol *>(sym.GetTaggedObject()); 924514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> descHandle(thread, symbolObject->GetDescription()); 934514f5e3Sopenharmony_ci 944514f5e3Sopenharmony_ci // If desc is undefined, let desc be the empty string. 954514f5e3Sopenharmony_ci JSHandle<SingleCharTable> singleCharTable(thread, thread->GetSingleCharTable()); 964514f5e3Sopenharmony_ci auto constants = thread->GlobalConstants(); 974514f5e3Sopenharmony_ci if (descHandle->IsUndefined()) { 984514f5e3Sopenharmony_ci JSHandle<EcmaString> leftHandle = JSHandle<EcmaString>::Cast(constants->GetHandledSymbolLeftParentheses()); 994514f5e3Sopenharmony_ci JSHandle<EcmaString> rightHandle(thread, singleCharTable->GetStringFromSingleCharTable(')')); 1004514f5e3Sopenharmony_ci JSHandle<EcmaString> str = factory->ConcatFromString(leftHandle, rightHandle); 1014514f5e3Sopenharmony_ci return str.GetTaggedValue(); 1024514f5e3Sopenharmony_ci } 1034514f5e3Sopenharmony_ci // Assert: Type(desc) is String. 1044514f5e3Sopenharmony_ci ASSERT(descHandle->IsString()); 1054514f5e3Sopenharmony_ci // Return the result of concatenating the strings "Symbol(", desc, and ")". 1064514f5e3Sopenharmony_ci JSHandle<EcmaString> leftHandle = JSHandle<EcmaString>::Cast(constants->GetHandledSymbolLeftParentheses()); 1074514f5e3Sopenharmony_ci JSHandle<EcmaString> rightHandle(thread, singleCharTable->GetStringFromSingleCharTable(')')); 1084514f5e3Sopenharmony_ci JSHandle<EcmaString> stringLeft = 1094514f5e3Sopenharmony_ci factory->ConcatFromString(leftHandle, JSTaggedValue::ToString(thread, descHandle)); 1104514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1114514f5e3Sopenharmony_ci JSHandle<EcmaString> str = factory->ConcatFromString(stringLeft, rightHandle); 1124514f5e3Sopenharmony_ci return str.GetTaggedValue(); 1134514f5e3Sopenharmony_ci} 1144514f5e3Sopenharmony_ci 1154514f5e3Sopenharmony_ci// 19.4.3.3 1164514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSymbol::ValueOf(EcmaRuntimeCallInfo *argv) 1174514f5e3Sopenharmony_ci{ 1184514f5e3Sopenharmony_ci ASSERT(argv); 1194514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), Symbol, ValueOf); 1204514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1214514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1224514f5e3Sopenharmony_ci // Let s be the this value. 1234514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> valueHandle = GetThis(argv); 1244514f5e3Sopenharmony_ci // 1.If value is a Symbol, return value. 1254514f5e3Sopenharmony_ci if (valueHandle->IsSymbol()) { 1264514f5e3Sopenharmony_ci return valueHandle.GetTaggedValue(); 1274514f5e3Sopenharmony_ci } 1284514f5e3Sopenharmony_ci 1294514f5e3Sopenharmony_ci // 2.If value is an Object and value has a [[SymbolData]] internal slot, then 1304514f5e3Sopenharmony_ci if (valueHandle->IsJSPrimitiveRef()) { 1314514f5e3Sopenharmony_ci // Let sym be the value of s's [[SymbolData]] internal slot. 1324514f5e3Sopenharmony_ci JSTaggedValue primitive = JSPrimitiveRef::Cast(valueHandle->GetTaggedObject())->GetValue(); 1334514f5e3Sopenharmony_ci if (primitive.IsSymbol()) { 1344514f5e3Sopenharmony_ci return primitive; 1354514f5e3Sopenharmony_ci } 1364514f5e3Sopenharmony_ci } 1374514f5e3Sopenharmony_ci 1384514f5e3Sopenharmony_ci // 3.If s does not have a [[SymbolData]] internal slot, throw a TypeError exception. 1394514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "ValueOf: no [[SymbolData]]", JSTaggedValue::Exception()); 1404514f5e3Sopenharmony_ci} 1414514f5e3Sopenharmony_ci 1424514f5e3Sopenharmony_ci// 19.4.2.1 Symbol.for (key) 1434514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSymbol::For(EcmaRuntimeCallInfo *argv) 1444514f5e3Sopenharmony_ci{ 1454514f5e3Sopenharmony_ci ASSERT(argv); 1464514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), Symbol, For); 1474514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1484514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1494514f5e3Sopenharmony_ci // 1.Let stringKey be ToString(key). 1504514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key = BuiltinsSymbol::GetCallArg(argv, 0); 1514514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> stringHandle = JSHandle<JSTaggedValue>::Cast(JSTaggedValue::ToString(thread, key)); 1524514f5e3Sopenharmony_ci // 2.ReturnIfAbrupt 1534514f5e3Sopenharmony_ci RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::Exception()); 1544514f5e3Sopenharmony_ci 1554514f5e3Sopenharmony_ci // 3.For each element e of the GlobalSymbolRegistry List, 1564514f5e3Sopenharmony_ci // If SameValue(e.[[key]], stringKey) is true, return e.[[symbol]]. 1574514f5e3Sopenharmony_ci // 4.Assert: GlobalSymbolRegistry does not currently contain an entry for stringKey. 1584514f5e3Sopenharmony_ci // 5.Let newSymbol be a new unique Symbol value whose [[Description]] value is stringKey. 1594514f5e3Sopenharmony_ci // 6.Append the record { [[key]]: stringKey, [[symbol]]: newSymbol } to the GlobalSymbolRegistry List. 1604514f5e3Sopenharmony_ci // 7.Return newSymbol. 1614514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1624514f5e3Sopenharmony_ci JSHandle<JSSymbol> symbol = factory->NewSymbolWithTable(stringHandle); 1634514f5e3Sopenharmony_ci return symbol.GetTaggedValue(); 1644514f5e3Sopenharmony_ci} 1654514f5e3Sopenharmony_ci 1664514f5e3Sopenharmony_ci// 19.4.2.5 Symbol.keyFor (sym) 1674514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSymbol::KeyFor(EcmaRuntimeCallInfo *argv) 1684514f5e3Sopenharmony_ci{ 1694514f5e3Sopenharmony_ci ASSERT(argv); 1704514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), Symbol, KeyFor); 1714514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1724514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1734514f5e3Sopenharmony_ci // 1.If Type(sym) is not Symbol, throw a TypeError exception. 1744514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> sym = BuiltinsSymbol::GetCallArg(argv, 0); 1754514f5e3Sopenharmony_ci if (!sym->IsSymbol()) { 1764514f5e3Sopenharmony_ci // return typeError 1774514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "KeyFor: sym is not Symbol", JSTaggedValue::Exception()); 1784514f5e3Sopenharmony_ci } 1794514f5e3Sopenharmony_ci // 2.For each element e of the GlobalSymbolRegistry List, 1804514f5e3Sopenharmony_ci // If SameValue(e.[[symbol]], sym) is true, return e.[[key]]. 1814514f5e3Sopenharmony_ci // 3.Assert: GlobalSymbolRegistry does not currently contain an entry for sym. 1824514f5e3Sopenharmony_ci // 4.Return undefined. 1834514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 1844514f5e3Sopenharmony_ci auto *table = env->GetRegisterSymbols().GetObject<SymbolTable>(); 1854514f5e3Sopenharmony_ci JSTaggedValue key = table->FindSymbol(sym.GetTaggedValue()); 1864514f5e3Sopenharmony_ci if (key.IsUndefined()) { 1874514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 1884514f5e3Sopenharmony_ci } 1894514f5e3Sopenharmony_ci return JSTaggedValue::ToString(thread, JSHandle<JSTaggedValue>(thread, key)).GetTaggedValue(); 1904514f5e3Sopenharmony_ci} 1914514f5e3Sopenharmony_ci 1924514f5e3Sopenharmony_ci// 19.4.3.4 Symbol.prototype [ @@toPrimitive ] ( hint ) 1934514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSymbol::ToPrimitive(EcmaRuntimeCallInfo *argv) 1944514f5e3Sopenharmony_ci{ 1954514f5e3Sopenharmony_ci ASSERT(argv); 1964514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), Symbol, ToPrimitive); 1974514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1984514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1994514f5e3Sopenharmony_ci // Let s be the this value. 2004514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> sym = GetThis(argv); 2014514f5e3Sopenharmony_ci // 1.If value is a Symbol, return value. 2024514f5e3Sopenharmony_ci if (sym->IsSymbol()) { 2034514f5e3Sopenharmony_ci return sym.GetTaggedValue(); 2044514f5e3Sopenharmony_ci } 2054514f5e3Sopenharmony_ci 2064514f5e3Sopenharmony_ci // 2.If value is an Object and value has a [[SymbolData]] internal slot, then 2074514f5e3Sopenharmony_ci if (sym->IsJSPrimitiveRef()) { 2084514f5e3Sopenharmony_ci // Let sym be the value of s's [[SymbolData]] internal slot. 2094514f5e3Sopenharmony_ci JSTaggedValue primitive = JSPrimitiveRef::Cast(sym->GetTaggedObject())->GetValue(); 2104514f5e3Sopenharmony_ci if (primitive.IsSymbol()) { 2114514f5e3Sopenharmony_ci return primitive; 2124514f5e3Sopenharmony_ci } 2134514f5e3Sopenharmony_ci } 2144514f5e3Sopenharmony_ci 2154514f5e3Sopenharmony_ci // 3.If s does not have a [[SymbolData]] internal slot, throw a TypeError exception. 2164514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "ToPrimitive: s is not Object", JSTaggedValue::Exception()); 2174514f5e3Sopenharmony_ci} 2184514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSymbol::DescriptionGetter(EcmaRuntimeCallInfo *argv) 2194514f5e3Sopenharmony_ci{ 2204514f5e3Sopenharmony_ci ASSERT(argv); 2214514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), Symbol, DescriptionGetter); 2224514f5e3Sopenharmony_ci // 1.Let s be the this value. 2234514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2244514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 2254514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> s = GetThis(argv); 2264514f5e3Sopenharmony_ci // 2.Let sym be ? thisSymbolValue(s). 2274514f5e3Sopenharmony_ci // 3.Return sym.[[Description]]. 2284514f5e3Sopenharmony_ci return ThisSymbolValue(thread, s); 2294514f5e3Sopenharmony_ci} 2304514f5e3Sopenharmony_ci 2314514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSymbol::ThisSymbolValue(JSThread *thread, const JSHandle<JSTaggedValue> &value) 2324514f5e3Sopenharmony_ci{ 2334514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, Symbol, ThisSymbolValue); 2344514f5e3Sopenharmony_ci if (value->IsSymbol()) { 2354514f5e3Sopenharmony_ci JSTaggedValue desValue = JSSymbol::Cast(value->GetTaggedObject())->GetDescription(); 2364514f5e3Sopenharmony_ci return desValue; 2374514f5e3Sopenharmony_ci } 2384514f5e3Sopenharmony_ci 2394514f5e3Sopenharmony_ci // If s does not have a [[SymbolData]] internal slot, throw a TypeError exception. 2404514f5e3Sopenharmony_ci if (value->IsJSPrimitiveRef()) { 2414514f5e3Sopenharmony_ci JSTaggedValue primitive = JSPrimitiveRef::Cast(value->GetTaggedObject())->GetValue(); 2424514f5e3Sopenharmony_ci if (primitive.IsSymbol()) { 2434514f5e3Sopenharmony_ci // Return the value of s's [[SymbolData]] internal slot. 2444514f5e3Sopenharmony_ci JSTaggedValue primitiveDesValue = JSSymbol::Cast(primitive.GetTaggedObject())->GetDescription(); 2454514f5e3Sopenharmony_ci return primitiveDesValue; 2464514f5e3Sopenharmony_ci } 2474514f5e3Sopenharmony_ci } 2484514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "can not convert to Symbol", JSTaggedValue::Exception()); 2494514f5e3Sopenharmony_ci} 2504514f5e3Sopenharmony_ci} // namespace panda::ecmascript::builtins 251