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#ifndef ECMASCRIPT_BUILTINS_BUILTINS_GLOBAL_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_BUILTINS_BUILTINS_GLOBAL_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include "ecmascript/base/builtins_base.h" 204514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h" 214514f5e3Sopenharmony_ci 224514f5e3Sopenharmony_ci#define BUILTIN_GLOBAL_CONSTANTS(V) \ 234514f5e3Sopenharmony_ci V("Infinity", INFINITY_VALUE) \ 244514f5e3Sopenharmony_ci V("NaN", NAN_VALUE) \ 254514f5e3Sopenharmony_ci V("undefined", UNDEFINED_VALUE) 264514f5e3Sopenharmony_ci 274514f5e3Sopenharmony_ci// List of functions in the global object. 284514f5e3Sopenharmony_ci// V(name, func, length, stubIndex) 294514f5e3Sopenharmony_ci// where BuiltinsGlobal::func refers to the native implementation of globalThis[name]. 304514f5e3Sopenharmony_ci// kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available. 314514f5e3Sopenharmony_ci// The following global object properties are not implemented yet: 324514f5e3Sopenharmony_ci// - Encode ( string, extraUnescaped ) 334514f5e3Sopenharmony_ci// - Decode ( string, preserveEscapeSet ) 344514f5e3Sopenharmony_ci// - ParseHexOctet ( string, position ) 354514f5e3Sopenharmony_ci// The following global object properties are not listed here: 364514f5e3Sopenharmony_ci// - parseFloat ( string ), listed in builtins_number.h instead. 374514f5e3Sopenharmony_ci// - parseInt ( string ), listed in builtins_number.h instead. 384514f5e3Sopenharmony_ci#define BUILTIN_GLOBAL_FUNCTIONS_COMMON(V) \ 394514f5e3Sopenharmony_ci /* decodeURI ( encodedURI ) */ \ 404514f5e3Sopenharmony_ci V("decodeURI", DecodeURI, 1, INVALID) \ 414514f5e3Sopenharmony_ci /* decodeURIComponent ( encodedURIComponent ) */ \ 424514f5e3Sopenharmony_ci V("decodeURIComponent", DecodeURIComponent, 1, GlobalDecodeURIComponent) \ 434514f5e3Sopenharmony_ci /* encodeURI ( uri ) */ \ 444514f5e3Sopenharmony_ci V("encodeURI", EncodeURI, 1, INVALID) \ 454514f5e3Sopenharmony_ci /* encodeURIComponent ( uriComponent ) */ \ 464514f5e3Sopenharmony_ci V("encodeURIComponent", EncodeURIComponent, 1, INVALID) \ 474514f5e3Sopenharmony_ci /* escape ( string ), defined in B.2.1 */ \ 484514f5e3Sopenharmony_ci V("escape", Escape, 1, INVALID) \ 494514f5e3Sopenharmony_ci /* eval ( x ), which is NOT supported in ArkTS engine */ \ 504514f5e3Sopenharmony_ci V("eval", NotSupportEval, 1, INVALID) \ 514514f5e3Sopenharmony_ci /* isFinite ( number ) */ \ 524514f5e3Sopenharmony_ci V("isFinite", IsFinite, 1, GlobalIsFinite) \ 534514f5e3Sopenharmony_ci /* isNaN ( number ) */ \ 544514f5e3Sopenharmony_ci V("isNaN", IsNaN, 1, GlobalIsNan) \ 554514f5e3Sopenharmony_ci /* unescape ( string )*/ \ 564514f5e3Sopenharmony_ci V("unescape", Unescape, 1, INVALID) \ 574514f5e3Sopenharmony_ci /* The following are ArkTS extensions */ \ 584514f5e3Sopenharmony_ci V("markModuleCollectable", MarkModuleCollectable, 0, INVALID) \ 594514f5e3Sopenharmony_ci V("loadNativeModule", LoadNativeModule, 0, INVALID) \ 604514f5e3Sopenharmony_ci V("print", PrintEntrypoint, 0, INVALID) \ 614514f5e3Sopenharmony_ci V("isSendable", IsSendable, 0, INVALID) \ 624514f5e3Sopenharmony_ci V("__getCurrentModuleName__", GetCurrentModuleName, 0, INVALID) \ 634514f5e3Sopenharmony_ci V("__getCurrentBundleName__", GetCurrentBundleName, 0, INVALID) 644514f5e3Sopenharmony_ci#if ECMASCRIPT_ENABLE_RUNTIME_STAT 654514f5e3Sopenharmony_ci#define BUILTIN_GLOBAL_FUNCTIONS_RUNTIME_STAT(V) \ 664514f5e3Sopenharmony_ci V("startRuntimeStat", StartRuntimeStat, 0, INVALID) \ 674514f5e3Sopenharmony_ci V("stopRuntimeStat", StopRuntimeStat, 0, INVALID) 684514f5e3Sopenharmony_ci#else 694514f5e3Sopenharmony_ci#define BUILTIN_GLOBAL_FUNCTIONS_RUNTIME_STAT(V) // Nothing 704514f5e3Sopenharmony_ci#endif 714514f5e3Sopenharmony_ci 724514f5e3Sopenharmony_ci#if ECMASCRIPT_ENABLE_OPT_CODE_PROFILER 734514f5e3Sopenharmony_ci#define BUILTIN_GLOBAL_FUNCTIONS_OPT_CODE_PROFILER(V) \ 744514f5e3Sopenharmony_ci V("printOptStat", PrintOptStat, 0, INVALID) 754514f5e3Sopenharmony_ci#else 764514f5e3Sopenharmony_ci#define BUILTIN_GLOBAL_FUNCTIONS_OPT_CODE_PROFILER(V) // Nothing 774514f5e3Sopenharmony_ci#endif 784514f5e3Sopenharmony_ci 794514f5e3Sopenharmony_ci#if ECMASCRIPT_ENABLE_FUNCTION_CALL_TIMER 804514f5e3Sopenharmony_ci#define BUILTIN_GLOBAL_FUNCTIONS_FUNCTION_CALL_TIMER(V) \ 814514f5e3Sopenharmony_ci V("printFunctionCallStat", PrintFunctionCallStat, 0, INVALID) 824514f5e3Sopenharmony_ci#else 834514f5e3Sopenharmony_ci#define BUILTIN_GLOBAL_FUNCTIONS_FUNCTION_CALL_TIMER(V) // Nothing 844514f5e3Sopenharmony_ci#endif 854514f5e3Sopenharmony_ci 864514f5e3Sopenharmony_ci#define BUILTIN_GLOBAL_FUNCTIONS(V) \ 874514f5e3Sopenharmony_ci BUILTIN_GLOBAL_FUNCTIONS_COMMON(V) \ 884514f5e3Sopenharmony_ci BUILTIN_GLOBAL_FUNCTIONS_RUNTIME_STAT(V) \ 894514f5e3Sopenharmony_ci BUILTIN_GLOBAL_FUNCTIONS_OPT_CODE_PROFILER(V) \ 904514f5e3Sopenharmony_ci BUILTIN_GLOBAL_FUNCTIONS_FUNCTION_CALL_TIMER(V) 914514f5e3Sopenharmony_ci 924514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins { 934514f5e3Sopenharmony_cistatic constexpr uint8_t BIT_MASK = 0x0F; 944514f5e3Sopenharmony_cistatic constexpr uint8_t BIT_MASK_FF = 0xFF; 954514f5e3Sopenharmony_cistatic constexpr uint16_t BIT_MASK_4F = 0xFFFF; 964514f5e3Sopenharmony_cistatic constexpr uint16_t BIT16_MASK = 0x3FF; 974514f5e3Sopenharmony_cistatic constexpr uint8_t BIT_MASK_ONE = 0x80; 984514f5e3Sopenharmony_cistatic constexpr uint8_t BIT_MASK_TWO = 0xC0; 994514f5e3Sopenharmony_ciusing judgURIFunc = bool (*)(uint16_t); 1004514f5e3Sopenharmony_ci 1014514f5e3Sopenharmony_cienum class Placement { 1024514f5e3Sopenharmony_ci START = 0, 1034514f5e3Sopenharmony_ci END, 1044514f5e3Sopenharmony_ci}; 1054514f5e3Sopenharmony_ci 1064514f5e3Sopenharmony_ciclass BuiltinsGlobal : public base::BuiltinsBase { 1074514f5e3Sopenharmony_cipublic: 1084514f5e3Sopenharmony_ci static const inline JSTaggedValue INFINITY_VALUE = JSTaggedValue(base::POSITIVE_INFINITY); 1094514f5e3Sopenharmony_ci static const inline JSTaggedValue NAN_VALUE = JSTaggedValue(base::NAN_VALUE); 1104514f5e3Sopenharmony_ci static const inline JSTaggedValue UNDEFINED_VALUE = JSTaggedValue::Undefined(); 1114514f5e3Sopenharmony_ci 1124514f5e3Sopenharmony_ci // 18.2.1 1134514f5e3Sopenharmony_ci static JSTaggedValue NotSupportEval(EcmaRuntimeCallInfo *msg); 1144514f5e3Sopenharmony_ci // 18.2.2 1154514f5e3Sopenharmony_ci static JSTaggedValue IsFinite(EcmaRuntimeCallInfo *msg); 1164514f5e3Sopenharmony_ci // 18.2.3 1174514f5e3Sopenharmony_ci static JSTaggedValue IsNaN(EcmaRuntimeCallInfo *msg); 1184514f5e3Sopenharmony_ci // 18.2.6 1194514f5e3Sopenharmony_ci static JSTaggedValue DecodeURI(EcmaRuntimeCallInfo *msg); 1204514f5e3Sopenharmony_ci static JSTaggedValue EncodeURI(EcmaRuntimeCallInfo *msg); 1214514f5e3Sopenharmony_ci static JSTaggedValue DecodeURIComponent(EcmaRuntimeCallInfo *msg); 1224514f5e3Sopenharmony_ci static JSTaggedValue EncodeURIComponent(EcmaRuntimeCallInfo *msg); 1234514f5e3Sopenharmony_ci 1244514f5e3Sopenharmony_ci static JSTaggedValue PrintEntrypoint(EcmaRuntimeCallInfo *msg); 1254514f5e3Sopenharmony_ci static JSTaggedValue MarkModuleCollectable(EcmaRuntimeCallInfo *msg); 1264514f5e3Sopenharmony_ci static JSTaggedValue LoadNativeModule(EcmaRuntimeCallInfo *msg); 1274514f5e3Sopenharmony_ci static JSTaggedValue CallJsBoundFunction(EcmaRuntimeCallInfo *msg); 1284514f5e3Sopenharmony_ci static JSTaggedValue CallJsProxy(EcmaRuntimeCallInfo *msg); 1294514f5e3Sopenharmony_ci static JSTaggedValue IsSendable(EcmaRuntimeCallInfo *msg); 1304514f5e3Sopenharmony_ci 1314514f5e3Sopenharmony_ci static JSTaggedValue GetCurrentModuleName(EcmaRuntimeCallInfo *msg); 1324514f5e3Sopenharmony_ci static JSTaggedValue GetCurrentBundleName(EcmaRuntimeCallInfo *msg); 1334514f5e3Sopenharmony_ci#if ECMASCRIPT_ENABLE_RUNTIME_STAT 1344514f5e3Sopenharmony_ci static JSTaggedValue StartRuntimeStat(EcmaRuntimeCallInfo *msg); 1354514f5e3Sopenharmony_ci static JSTaggedValue StopRuntimeStat(EcmaRuntimeCallInfo *msg); 1364514f5e3Sopenharmony_ci#endif 1374514f5e3Sopenharmony_ci 1384514f5e3Sopenharmony_ci#if ECMASCRIPT_ENABLE_OPT_CODE_PROFILER 1394514f5e3Sopenharmony_ci static JSTaggedValue PrintOptStat(EcmaRuntimeCallInfo *msg); 1404514f5e3Sopenharmony_ci#endif 1414514f5e3Sopenharmony_ci 1424514f5e3Sopenharmony_ci#if ECMASCRIPT_ENABLE_FUNCTION_CALL_TIMER 1434514f5e3Sopenharmony_ci static JSTaggedValue PrintFunctionCallStat(EcmaRuntimeCallInfo *msg); 1444514f5e3Sopenharmony_ci#endif 1454514f5e3Sopenharmony_ci // B.2.1.1 escape ( string ) 1464514f5e3Sopenharmony_ci static JSTaggedValue Escape(EcmaRuntimeCallInfo *msg); 1474514f5e3Sopenharmony_ci // B.2.1.2 unescape ( string ) 1484514f5e3Sopenharmony_ci static JSTaggedValue Unescape(EcmaRuntimeCallInfo *msg); 1494514f5e3Sopenharmony_ci 1504514f5e3Sopenharmony_ci static Span<const base::BuiltinConstantEntry> GetGlobalConstants() 1514514f5e3Sopenharmony_ci { 1524514f5e3Sopenharmony_ci return Span<const base::BuiltinConstantEntry>(GLOBAL_CONSTANTS); 1534514f5e3Sopenharmony_ci } 1544514f5e3Sopenharmony_ci 1554514f5e3Sopenharmony_ci static Span<const base::BuiltinFunctionEntry> GetGlobalFunctions() 1564514f5e3Sopenharmony_ci { 1574514f5e3Sopenharmony_ci return Span<const base::BuiltinFunctionEntry>(GLOBAL_FUNCTIONS); 1584514f5e3Sopenharmony_ci } 1594514f5e3Sopenharmony_ci 1604514f5e3Sopenharmony_ciprivate: 1614514f5e3Sopenharmony_ci#define BUILTIN_GLOBAL_CONSTANT_ENTRY(name, var) \ 1624514f5e3Sopenharmony_ci base::BuiltinConstantEntry::Create(name, BuiltinsGlobal::var), 1634514f5e3Sopenharmony_ci#define BUILTIN_GLOBAL_FUNCTION_ENTRY(name, func, length, id) \ 1644514f5e3Sopenharmony_ci base::BuiltinFunctionEntry::Create(name, BuiltinsGlobal::func, length, kungfu::BuiltinsStubCSigns::id), 1654514f5e3Sopenharmony_ci 1664514f5e3Sopenharmony_ci static inline std::array GLOBAL_CONSTANTS = { 1674514f5e3Sopenharmony_ci BUILTIN_GLOBAL_CONSTANTS(BUILTIN_GLOBAL_CONSTANT_ENTRY) 1684514f5e3Sopenharmony_ci }; 1694514f5e3Sopenharmony_ci static constexpr std::array GLOBAL_FUNCTIONS = { 1704514f5e3Sopenharmony_ci BUILTIN_GLOBAL_FUNCTIONS(BUILTIN_GLOBAL_FUNCTION_ENTRY) 1714514f5e3Sopenharmony_ci }; 1724514f5e3Sopenharmony_ci#undef BUILTIN_GLOBAL_CONSTANT_ENTRY 1734514f5e3Sopenharmony_ci#undef BUILTIN_GLOBAL_FUNCTION_ENTRY 1744514f5e3Sopenharmony_ci 1754514f5e3Sopenharmony_ci static void PrintString(JSThread *thread, EcmaString *string); 1764514f5e3Sopenharmony_ci static void PrintValue(int64_t value, int64_t tag); 1774514f5e3Sopenharmony_ci static JSTaggedValue Encode(JSThread *thread, const JSHandle<EcmaString> &str, judgURIFunc IsInURISet); 1784514f5e3Sopenharmony_ci static JSTaggedValue Decode(JSThread *thread, const JSHandle<EcmaString> &str, judgURIFunc IsInURISet); 1794514f5e3Sopenharmony_ci static JSTaggedValue UTF16EncodeCodePoint(JSThread *thread, judgURIFunc IsInURISet, 1804514f5e3Sopenharmony_ci const std::vector<uint8_t> &oct, const JSHandle<EcmaString> &str, 1814514f5e3Sopenharmony_ci uint32_t &start, int32_t &k, std::u16string &sStr); 1824514f5e3Sopenharmony_ci static void HandleSingleByteCharacter(JSThread *thread, uint8_t &bb, 1834514f5e3Sopenharmony_ci const JSHandle<EcmaString> &str, 1844514f5e3Sopenharmony_ci uint32_t &start, int32_t &k, 1854514f5e3Sopenharmony_ci std::u16string &sStr, judgURIFunc IsInURISet); 1864514f5e3Sopenharmony_ci static JSTaggedValue DecodePercentEncoding(JSThread *thread, int32_t &n, 1874514f5e3Sopenharmony_ci int32_t &k, const JSHandle<EcmaString> &str, 1884514f5e3Sopenharmony_ci uint8_t &bb, std::vector<uint8_t> &oct); 1894514f5e3Sopenharmony_ci static JSTaggedValue DecodePercentEncoding(JSThread *thread, const JSHandle<EcmaString> &str, int32_t &k, 1904514f5e3Sopenharmony_ci judgURIFunc IsInURISet, int32_t strLen, std::u16string &sStr); 1914514f5e3Sopenharmony_ci static bool IsUnescapedURI(uint16_t ch); 1924514f5e3Sopenharmony_ci static bool IsInUnescapedURISet(uint16_t ch); 1934514f5e3Sopenharmony_ci static bool IsInReservedURISet(uint16_t ch); 1944514f5e3Sopenharmony_ci static bool IsReservedURI(uint16_t ch); 1954514f5e3Sopenharmony_ci static bool IsInMarkURISet(uint16_t ch); 1964514f5e3Sopenharmony_ci static bool IsHexDigits(uint16_t ch); 1974514f5e3Sopenharmony_ci static uint8_t GetValueFromTwoHex(uint16_t front, uint16_t behind); 1984514f5e3Sopenharmony_ci static uint16_t GetValueFromHexString(const JSHandle<EcmaString> &string); 1994514f5e3Sopenharmony_ci // 22.1.3.17.2 StringPad ( S, maxLength, fillString, placement ) 2004514f5e3Sopenharmony_ci static EcmaString *StringPad(JSThread *thread, 2014514f5e3Sopenharmony_ci const JSHandle<EcmaString> &string, 2024514f5e3Sopenharmony_ci uint32_t maxLength, 2034514f5e3Sopenharmony_ci const JSHandle<EcmaString> &fillString, 2044514f5e3Sopenharmony_ci Placement placement = Placement::START); 2054514f5e3Sopenharmony_ci static bool IsUTF16HighSurrogate(uint16_t ch) 2064514f5e3Sopenharmony_ci { 2074514f5e3Sopenharmony_ci return base::utf_helper::DECODE_LEAD_LOW <= ch && ch <= base::utf_helper::DECODE_LEAD_HIGH; 2084514f5e3Sopenharmony_ci } 2094514f5e3Sopenharmony_ci 2104514f5e3Sopenharmony_ci static bool IsUTF16LowSurrogate(uint16_t ch) 2114514f5e3Sopenharmony_ci { 2124514f5e3Sopenharmony_ci return base::utf_helper::DECODE_TRAIL_LOW <= ch && ch <= base::utf_helper::DECODE_TRAIL_HIGH; 2134514f5e3Sopenharmony_ci } 2144514f5e3Sopenharmony_ci 2154514f5e3Sopenharmony_ci // 11.1.3 Static Semantics: UTF16SurrogatePairToCodePoint ( lead, trail ) 2164514f5e3Sopenharmony_ci static uint16_t UTF16SurrogatePairToCodePoint(uint16_t lead, uint16_t trail); 2174514f5e3Sopenharmony_ci // 11.1.5 Static Semantics: StringToCodePoints ( string ) 2184514f5e3Sopenharmony_ci static EcmaString *StringToCodePoints(JSThread *thread, const JSHandle<EcmaString> &string); 2194514f5e3Sopenharmony_ci}; 2204514f5e3Sopenharmony_ci} // namespace panda::ecmascript::builtins 2214514f5e3Sopenharmony_ci 2224514f5e3Sopenharmony_ci#endif // ECMASCRIPT_BUILTINS_BUILTINS_ERROR_H 223