11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#ifndef INCLUDE_V8_PRIMITIVE_H_ 61cb0ef41Sopenharmony_ci#define INCLUDE_V8_PRIMITIVE_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include "v8-data.h" // NOLINT(build/include_directory) 91cb0ef41Sopenharmony_ci#include "v8-internal.h" // NOLINT(build/include_directory) 101cb0ef41Sopenharmony_ci#include "v8-local-handle.h" // NOLINT(build/include_directory) 111cb0ef41Sopenharmony_ci#include "v8-value.h" // NOLINT(build/include_directory) 121cb0ef41Sopenharmony_ci#include "v8config.h" // NOLINT(build/include_directory) 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_cinamespace v8 { 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciclass Context; 171cb0ef41Sopenharmony_ciclass Isolate; 181cb0ef41Sopenharmony_ciclass String; 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_cinamespace internal { 211cb0ef41Sopenharmony_ciclass ExternalString; 221cb0ef41Sopenharmony_ciclass ScopedExternalStringLock; 231cb0ef41Sopenharmony_ciclass StringForwardingTable; 241cb0ef41Sopenharmony_ci} // namespace internal 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci/** 271cb0ef41Sopenharmony_ci * The superclass of primitive values. See ECMA-262 4.3.2. 281cb0ef41Sopenharmony_ci */ 291cb0ef41Sopenharmony_ciclass V8_EXPORT Primitive : public Value {}; 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci/** 321cb0ef41Sopenharmony_ci * A primitive boolean value (ECMA-262, 4.3.14). Either the true 331cb0ef41Sopenharmony_ci * or false value. 341cb0ef41Sopenharmony_ci */ 351cb0ef41Sopenharmony_ciclass V8_EXPORT Boolean : public Primitive { 361cb0ef41Sopenharmony_ci public: 371cb0ef41Sopenharmony_ci bool Value() const; 381cb0ef41Sopenharmony_ci V8_INLINE static Boolean* Cast(v8::Data* data) { 391cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS 401cb0ef41Sopenharmony_ci CheckCast(data); 411cb0ef41Sopenharmony_ci#endif 421cb0ef41Sopenharmony_ci return static_cast<Boolean*>(data); 431cb0ef41Sopenharmony_ci } 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci V8_INLINE static Local<Boolean> New(Isolate* isolate, bool value); 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci private: 481cb0ef41Sopenharmony_ci static void CheckCast(v8::Data* that); 491cb0ef41Sopenharmony_ci}; 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci/** 521cb0ef41Sopenharmony_ci * An array to hold Primitive values. This is used by the embedder to 531cb0ef41Sopenharmony_ci * pass host defined options to the ScriptOptions during compilation. 541cb0ef41Sopenharmony_ci * 551cb0ef41Sopenharmony_ci * This is passed back to the embedder as part of 561cb0ef41Sopenharmony_ci * HostImportModuleDynamicallyCallback for module loading. 571cb0ef41Sopenharmony_ci */ 581cb0ef41Sopenharmony_ciclass V8_EXPORT PrimitiveArray : public Data { 591cb0ef41Sopenharmony_ci public: 601cb0ef41Sopenharmony_ci static Local<PrimitiveArray> New(Isolate* isolate, int length); 611cb0ef41Sopenharmony_ci int Length() const; 621cb0ef41Sopenharmony_ci void Set(Isolate* isolate, int index, Local<Primitive> item); 631cb0ef41Sopenharmony_ci Local<Primitive> Get(Isolate* isolate, int index); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci V8_INLINE static PrimitiveArray* Cast(Data* data) { 661cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS 671cb0ef41Sopenharmony_ci CheckCast(data); 681cb0ef41Sopenharmony_ci#endif 691cb0ef41Sopenharmony_ci return reinterpret_cast<PrimitiveArray*>(data); 701cb0ef41Sopenharmony_ci } 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci private: 731cb0ef41Sopenharmony_ci static void CheckCast(Data* obj); 741cb0ef41Sopenharmony_ci}; 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci/** 771cb0ef41Sopenharmony_ci * A superclass for symbols and strings. 781cb0ef41Sopenharmony_ci */ 791cb0ef41Sopenharmony_ciclass V8_EXPORT Name : public Primitive { 801cb0ef41Sopenharmony_ci public: 811cb0ef41Sopenharmony_ci /** 821cb0ef41Sopenharmony_ci * Returns the identity hash for this object. The current implementation 831cb0ef41Sopenharmony_ci * uses an inline property on the object to store the identity hash. 841cb0ef41Sopenharmony_ci * 851cb0ef41Sopenharmony_ci * The return value will never be 0. Also, it is not guaranteed to be 861cb0ef41Sopenharmony_ci * unique. 871cb0ef41Sopenharmony_ci */ 881cb0ef41Sopenharmony_ci int GetIdentityHash(); 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci V8_INLINE static Name* Cast(Data* data) { 911cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS 921cb0ef41Sopenharmony_ci CheckCast(data); 931cb0ef41Sopenharmony_ci#endif 941cb0ef41Sopenharmony_ci return static_cast<Name*>(data); 951cb0ef41Sopenharmony_ci } 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci private: 981cb0ef41Sopenharmony_ci static void CheckCast(Data* that); 991cb0ef41Sopenharmony_ci}; 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci/** 1021cb0ef41Sopenharmony_ci * A flag describing different modes of string creation. 1031cb0ef41Sopenharmony_ci * 1041cb0ef41Sopenharmony_ci * Aside from performance implications there are no differences between the two 1051cb0ef41Sopenharmony_ci * creation modes. 1061cb0ef41Sopenharmony_ci */ 1071cb0ef41Sopenharmony_cienum class NewStringType { 1081cb0ef41Sopenharmony_ci /** 1091cb0ef41Sopenharmony_ci * Create a new string, always allocating new storage memory. 1101cb0ef41Sopenharmony_ci */ 1111cb0ef41Sopenharmony_ci kNormal, 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci /** 1141cb0ef41Sopenharmony_ci * Acts as a hint that the string should be created in the 1151cb0ef41Sopenharmony_ci * old generation heap space and be deduplicated if an identical string 1161cb0ef41Sopenharmony_ci * already exists. 1171cb0ef41Sopenharmony_ci */ 1181cb0ef41Sopenharmony_ci kInternalized 1191cb0ef41Sopenharmony_ci}; 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ci/** 1221cb0ef41Sopenharmony_ci * A JavaScript string value (ECMA-262, 4.3.17). 1231cb0ef41Sopenharmony_ci */ 1241cb0ef41Sopenharmony_ciclass V8_EXPORT String : public Name { 1251cb0ef41Sopenharmony_ci public: 1261cb0ef41Sopenharmony_ci static constexpr int kMaxLength = 1271cb0ef41Sopenharmony_ci internal::kApiSystemPointerSize == 4 ? (1 << 28) - 16 : (1 << 29) - 24; 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci enum Encoding { 1301cb0ef41Sopenharmony_ci UNKNOWN_ENCODING = 0x1, 1311cb0ef41Sopenharmony_ci TWO_BYTE_ENCODING = 0x0, 1321cb0ef41Sopenharmony_ci ONE_BYTE_ENCODING = 0x8 1331cb0ef41Sopenharmony_ci }; 1341cb0ef41Sopenharmony_ci /** 1351cb0ef41Sopenharmony_ci * Returns the number of characters (UTF-16 code units) in this string. 1361cb0ef41Sopenharmony_ci */ 1371cb0ef41Sopenharmony_ci int Length() const; 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ci /** 1401cb0ef41Sopenharmony_ci * Returns the number of bytes in the UTF-8 encoded 1411cb0ef41Sopenharmony_ci * representation of this string. 1421cb0ef41Sopenharmony_ci */ 1431cb0ef41Sopenharmony_ci int Utf8Length(Isolate* isolate) const; 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ci /** 1461cb0ef41Sopenharmony_ci * Returns whether this string is known to contain only one byte data, 1471cb0ef41Sopenharmony_ci * i.e. ISO-8859-1 code points. 1481cb0ef41Sopenharmony_ci * Does not read the string. 1491cb0ef41Sopenharmony_ci * False negatives are possible. 1501cb0ef41Sopenharmony_ci */ 1511cb0ef41Sopenharmony_ci bool IsOneByte() const; 1521cb0ef41Sopenharmony_ci 1531cb0ef41Sopenharmony_ci /** 1541cb0ef41Sopenharmony_ci * Returns whether this string contain only one byte data, 1551cb0ef41Sopenharmony_ci * i.e. ISO-8859-1 code points. 1561cb0ef41Sopenharmony_ci * Will read the entire string in some cases. 1571cb0ef41Sopenharmony_ci */ 1581cb0ef41Sopenharmony_ci bool ContainsOnlyOneByte() const; 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ci /** 1611cb0ef41Sopenharmony_ci * Write the contents of the string to an external buffer. 1621cb0ef41Sopenharmony_ci * If no arguments are given, expects the buffer to be large 1631cb0ef41Sopenharmony_ci * enough to hold the entire string and NULL terminator. Copies 1641cb0ef41Sopenharmony_ci * the contents of the string and the NULL terminator into the 1651cb0ef41Sopenharmony_ci * buffer. 1661cb0ef41Sopenharmony_ci * 1671cb0ef41Sopenharmony_ci * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop 1681cb0ef41Sopenharmony_ci * before the end of the buffer. 1691cb0ef41Sopenharmony_ci * 1701cb0ef41Sopenharmony_ci * Copies up to length characters into the output buffer. 1711cb0ef41Sopenharmony_ci * Only null-terminates if there is enough space in the buffer. 1721cb0ef41Sopenharmony_ci * 1731cb0ef41Sopenharmony_ci * \param buffer The buffer into which the string will be copied. 1741cb0ef41Sopenharmony_ci * \param start The starting position within the string at which 1751cb0ef41Sopenharmony_ci * copying begins. 1761cb0ef41Sopenharmony_ci * \param length The number of characters to copy from the string. For 1771cb0ef41Sopenharmony_ci * WriteUtf8 the number of bytes in the buffer. 1781cb0ef41Sopenharmony_ci * \param nchars_ref The number of characters written, can be NULL. 1791cb0ef41Sopenharmony_ci * \param options Various options that might affect performance of this or 1801cb0ef41Sopenharmony_ci * subsequent operations. 1811cb0ef41Sopenharmony_ci * \return The number of characters copied to the buffer excluding the null 1821cb0ef41Sopenharmony_ci * terminator. For WriteUtf8: The number of bytes copied to the buffer 1831cb0ef41Sopenharmony_ci * including the null terminator (if written). 1841cb0ef41Sopenharmony_ci */ 1851cb0ef41Sopenharmony_ci enum WriteOptions { 1861cb0ef41Sopenharmony_ci NO_OPTIONS = 0, 1871cb0ef41Sopenharmony_ci HINT_MANY_WRITES_EXPECTED = 1, 1881cb0ef41Sopenharmony_ci NO_NULL_TERMINATION = 2, 1891cb0ef41Sopenharmony_ci PRESERVE_ONE_BYTE_NULL = 4, 1901cb0ef41Sopenharmony_ci // Used by WriteUtf8 to replace orphan surrogate code units with the 1911cb0ef41Sopenharmony_ci // unicode replacement character. Needs to be set to guarantee valid UTF-8 1921cb0ef41Sopenharmony_ci // output. 1931cb0ef41Sopenharmony_ci REPLACE_INVALID_UTF8 = 8 1941cb0ef41Sopenharmony_ci }; 1951cb0ef41Sopenharmony_ci 1961cb0ef41Sopenharmony_ci // 16-bit character codes. 1971cb0ef41Sopenharmony_ci int Write(Isolate* isolate, uint16_t* buffer, int start = 0, int length = -1, 1981cb0ef41Sopenharmony_ci int options = NO_OPTIONS) const; 1991cb0ef41Sopenharmony_ci // One byte characters. 2001cb0ef41Sopenharmony_ci int WriteOneByte(Isolate* isolate, uint8_t* buffer, int start = 0, 2011cb0ef41Sopenharmony_ci int length = -1, int options = NO_OPTIONS) const; 2021cb0ef41Sopenharmony_ci // UTF-8 encoded characters. 2031cb0ef41Sopenharmony_ci int WriteUtf8(Isolate* isolate, char* buffer, int length = -1, 2041cb0ef41Sopenharmony_ci int* nchars_ref = nullptr, int options = NO_OPTIONS) const; 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ci /** 2071cb0ef41Sopenharmony_ci * A zero length string. 2081cb0ef41Sopenharmony_ci */ 2091cb0ef41Sopenharmony_ci V8_INLINE static Local<String> Empty(Isolate* isolate); 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ci /** 2121cb0ef41Sopenharmony_ci * Returns true if the string is external. 2131cb0ef41Sopenharmony_ci */ 2141cb0ef41Sopenharmony_ci bool IsExternal() const; 2151cb0ef41Sopenharmony_ci 2161cb0ef41Sopenharmony_ci /** 2171cb0ef41Sopenharmony_ci * Returns true if the string is both external and two-byte. 2181cb0ef41Sopenharmony_ci */ 2191cb0ef41Sopenharmony_ci bool IsExternalTwoByte() const; 2201cb0ef41Sopenharmony_ci 2211cb0ef41Sopenharmony_ci /** 2221cb0ef41Sopenharmony_ci * Returns true if the string is both external and one-byte. 2231cb0ef41Sopenharmony_ci */ 2241cb0ef41Sopenharmony_ci bool IsExternalOneByte() const; 2251cb0ef41Sopenharmony_ci 2261cb0ef41Sopenharmony_ci class V8_EXPORT ExternalStringResourceBase { 2271cb0ef41Sopenharmony_ci public: 2281cb0ef41Sopenharmony_ci virtual ~ExternalStringResourceBase() = default; 2291cb0ef41Sopenharmony_ci 2301cb0ef41Sopenharmony_ci /** 2311cb0ef41Sopenharmony_ci * If a string is cacheable, the value returned by 2321cb0ef41Sopenharmony_ci * ExternalStringResource::data() may be cached, otherwise it is not 2331cb0ef41Sopenharmony_ci * expected to be stable beyond the current top-level task. 2341cb0ef41Sopenharmony_ci */ 2351cb0ef41Sopenharmony_ci virtual bool IsCacheable() const { return true; } 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ci // Disallow copying and assigning. 2381cb0ef41Sopenharmony_ci ExternalStringResourceBase(const ExternalStringResourceBase&) = delete; 2391cb0ef41Sopenharmony_ci void operator=(const ExternalStringResourceBase&) = delete; 2401cb0ef41Sopenharmony_ci 2411cb0ef41Sopenharmony_ci protected: 2421cb0ef41Sopenharmony_ci ExternalStringResourceBase() = default; 2431cb0ef41Sopenharmony_ci 2441cb0ef41Sopenharmony_ci /** 2451cb0ef41Sopenharmony_ci * Internally V8 will call this Dispose method when the external string 2461cb0ef41Sopenharmony_ci * resource is no longer needed. The default implementation will use the 2471cb0ef41Sopenharmony_ci * delete operator. This method can be overridden in subclasses to 2481cb0ef41Sopenharmony_ci * control how allocated external string resources are disposed. 2491cb0ef41Sopenharmony_ci */ 2501cb0ef41Sopenharmony_ci virtual void Dispose() { delete this; } 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ci /** 2531cb0ef41Sopenharmony_ci * For a non-cacheable string, the value returned by 2541cb0ef41Sopenharmony_ci * |ExternalStringResource::data()| has to be stable between |Lock()| and 2551cb0ef41Sopenharmony_ci * |Unlock()|, that is the string must behave as is |IsCacheable()| returned 2561cb0ef41Sopenharmony_ci * true. 2571cb0ef41Sopenharmony_ci * 2581cb0ef41Sopenharmony_ci * These two functions must be thread-safe, and can be called from anywhere. 2591cb0ef41Sopenharmony_ci * They also must handle lock depth, in the sense that each can be called 2601cb0ef41Sopenharmony_ci * several times, from different threads, and unlocking should only happen 2611cb0ef41Sopenharmony_ci * when the balance of Lock() and Unlock() calls is 0. 2621cb0ef41Sopenharmony_ci */ 2631cb0ef41Sopenharmony_ci virtual void Lock() const {} 2641cb0ef41Sopenharmony_ci 2651cb0ef41Sopenharmony_ci /** 2661cb0ef41Sopenharmony_ci * Unlocks the string. 2671cb0ef41Sopenharmony_ci */ 2681cb0ef41Sopenharmony_ci virtual void Unlock() const {} 2691cb0ef41Sopenharmony_ci 2701cb0ef41Sopenharmony_ci private: 2711cb0ef41Sopenharmony_ci friend class internal::ExternalString; 2721cb0ef41Sopenharmony_ci friend class v8::String; 2731cb0ef41Sopenharmony_ci friend class internal::StringForwardingTable; 2741cb0ef41Sopenharmony_ci friend class internal::ScopedExternalStringLock; 2751cb0ef41Sopenharmony_ci }; 2761cb0ef41Sopenharmony_ci 2771cb0ef41Sopenharmony_ci /** 2781cb0ef41Sopenharmony_ci * An ExternalStringResource is a wrapper around a two-byte string 2791cb0ef41Sopenharmony_ci * buffer that resides outside V8's heap. Implement an 2801cb0ef41Sopenharmony_ci * ExternalStringResource to manage the life cycle of the underlying 2811cb0ef41Sopenharmony_ci * buffer. Note that the string data must be immutable. 2821cb0ef41Sopenharmony_ci */ 2831cb0ef41Sopenharmony_ci class V8_EXPORT ExternalStringResource : public ExternalStringResourceBase { 2841cb0ef41Sopenharmony_ci public: 2851cb0ef41Sopenharmony_ci /** 2861cb0ef41Sopenharmony_ci * Override the destructor to manage the life cycle of the underlying 2871cb0ef41Sopenharmony_ci * buffer. 2881cb0ef41Sopenharmony_ci */ 2891cb0ef41Sopenharmony_ci ~ExternalStringResource() override = default; 2901cb0ef41Sopenharmony_ci 2911cb0ef41Sopenharmony_ci /** 2921cb0ef41Sopenharmony_ci * The string data from the underlying buffer. If the resource is cacheable 2931cb0ef41Sopenharmony_ci * then data() must return the same value for all invocations. 2941cb0ef41Sopenharmony_ci */ 2951cb0ef41Sopenharmony_ci virtual const uint16_t* data() const = 0; 2961cb0ef41Sopenharmony_ci 2971cb0ef41Sopenharmony_ci /** 2981cb0ef41Sopenharmony_ci * The length of the string. That is, the number of two-byte characters. 2991cb0ef41Sopenharmony_ci */ 3001cb0ef41Sopenharmony_ci virtual size_t length() const = 0; 3011cb0ef41Sopenharmony_ci 3021cb0ef41Sopenharmony_ci /** 3031cb0ef41Sopenharmony_ci * Returns the cached data from the underlying buffer. This method can be 3041cb0ef41Sopenharmony_ci * called only for cacheable resources (i.e. IsCacheable() == true) and only 3051cb0ef41Sopenharmony_ci * after UpdateDataCache() was called. 3061cb0ef41Sopenharmony_ci */ 3071cb0ef41Sopenharmony_ci const uint16_t* cached_data() const { 3081cb0ef41Sopenharmony_ci CheckCachedDataInvariants(); 3091cb0ef41Sopenharmony_ci return cached_data_; 3101cb0ef41Sopenharmony_ci } 3111cb0ef41Sopenharmony_ci 3121cb0ef41Sopenharmony_ci /** 3131cb0ef41Sopenharmony_ci * Update {cached_data_} with the data from the underlying buffer. This can 3141cb0ef41Sopenharmony_ci * be called only for cacheable resources. 3151cb0ef41Sopenharmony_ci */ 3161cb0ef41Sopenharmony_ci void UpdateDataCache(); 3171cb0ef41Sopenharmony_ci 3181cb0ef41Sopenharmony_ci protected: 3191cb0ef41Sopenharmony_ci ExternalStringResource() = default; 3201cb0ef41Sopenharmony_ci 3211cb0ef41Sopenharmony_ci private: 3221cb0ef41Sopenharmony_ci void CheckCachedDataInvariants() const; 3231cb0ef41Sopenharmony_ci 3241cb0ef41Sopenharmony_ci const uint16_t* cached_data_ = nullptr; 3251cb0ef41Sopenharmony_ci }; 3261cb0ef41Sopenharmony_ci 3271cb0ef41Sopenharmony_ci /** 3281cb0ef41Sopenharmony_ci * An ExternalOneByteStringResource is a wrapper around an one-byte 3291cb0ef41Sopenharmony_ci * string buffer that resides outside V8's heap. Implement an 3301cb0ef41Sopenharmony_ci * ExternalOneByteStringResource to manage the life cycle of the 3311cb0ef41Sopenharmony_ci * underlying buffer. Note that the string data must be immutable 3321cb0ef41Sopenharmony_ci * and that the data must be Latin-1 and not UTF-8, which would require 3331cb0ef41Sopenharmony_ci * special treatment internally in the engine and do not allow efficient 3341cb0ef41Sopenharmony_ci * indexing. Use String::New or convert to 16 bit data for non-Latin1. 3351cb0ef41Sopenharmony_ci */ 3361cb0ef41Sopenharmony_ci 3371cb0ef41Sopenharmony_ci class V8_EXPORT ExternalOneByteStringResource 3381cb0ef41Sopenharmony_ci : public ExternalStringResourceBase { 3391cb0ef41Sopenharmony_ci public: 3401cb0ef41Sopenharmony_ci /** 3411cb0ef41Sopenharmony_ci * Override the destructor to manage the life cycle of the underlying 3421cb0ef41Sopenharmony_ci * buffer. 3431cb0ef41Sopenharmony_ci */ 3441cb0ef41Sopenharmony_ci ~ExternalOneByteStringResource() override = default; 3451cb0ef41Sopenharmony_ci 3461cb0ef41Sopenharmony_ci /** 3471cb0ef41Sopenharmony_ci * The string data from the underlying buffer. If the resource is cacheable 3481cb0ef41Sopenharmony_ci * then data() must return the same value for all invocations. 3491cb0ef41Sopenharmony_ci */ 3501cb0ef41Sopenharmony_ci virtual const char* data() const = 0; 3511cb0ef41Sopenharmony_ci 3521cb0ef41Sopenharmony_ci /** The number of Latin-1 characters in the string.*/ 3531cb0ef41Sopenharmony_ci virtual size_t length() const = 0; 3541cb0ef41Sopenharmony_ci 3551cb0ef41Sopenharmony_ci /** 3561cb0ef41Sopenharmony_ci * Returns the cached data from the underlying buffer. If the resource is 3571cb0ef41Sopenharmony_ci * uncacheable or if UpdateDataCache() was not called before, it has 3581cb0ef41Sopenharmony_ci * undefined behaviour. 3591cb0ef41Sopenharmony_ci */ 3601cb0ef41Sopenharmony_ci const char* cached_data() const { 3611cb0ef41Sopenharmony_ci CheckCachedDataInvariants(); 3621cb0ef41Sopenharmony_ci return cached_data_; 3631cb0ef41Sopenharmony_ci } 3641cb0ef41Sopenharmony_ci 3651cb0ef41Sopenharmony_ci /** 3661cb0ef41Sopenharmony_ci * Update {cached_data_} with the data from the underlying buffer. This can 3671cb0ef41Sopenharmony_ci * be called only for cacheable resources. 3681cb0ef41Sopenharmony_ci */ 3691cb0ef41Sopenharmony_ci void UpdateDataCache(); 3701cb0ef41Sopenharmony_ci 3711cb0ef41Sopenharmony_ci protected: 3721cb0ef41Sopenharmony_ci ExternalOneByteStringResource() = default; 3731cb0ef41Sopenharmony_ci 3741cb0ef41Sopenharmony_ci private: 3751cb0ef41Sopenharmony_ci void CheckCachedDataInvariants() const; 3761cb0ef41Sopenharmony_ci 3771cb0ef41Sopenharmony_ci const char* cached_data_ = nullptr; 3781cb0ef41Sopenharmony_ci }; 3791cb0ef41Sopenharmony_ci 3801cb0ef41Sopenharmony_ci /** 3811cb0ef41Sopenharmony_ci * If the string is an external string, return the ExternalStringResourceBase 3821cb0ef41Sopenharmony_ci * regardless of the encoding, otherwise return NULL. The encoding of the 3831cb0ef41Sopenharmony_ci * string is returned in encoding_out. 3841cb0ef41Sopenharmony_ci */ 3851cb0ef41Sopenharmony_ci V8_INLINE ExternalStringResourceBase* GetExternalStringResourceBase( 3861cb0ef41Sopenharmony_ci Encoding* encoding_out) const; 3871cb0ef41Sopenharmony_ci 3881cb0ef41Sopenharmony_ci /** 3891cb0ef41Sopenharmony_ci * Get the ExternalStringResource for an external string. Returns 3901cb0ef41Sopenharmony_ci * NULL if IsExternal() doesn't return true. 3911cb0ef41Sopenharmony_ci */ 3921cb0ef41Sopenharmony_ci V8_INLINE ExternalStringResource* GetExternalStringResource() const; 3931cb0ef41Sopenharmony_ci 3941cb0ef41Sopenharmony_ci /** 3951cb0ef41Sopenharmony_ci * Get the ExternalOneByteStringResource for an external one-byte string. 3961cb0ef41Sopenharmony_ci * Returns NULL if IsExternalOneByte() doesn't return true. 3971cb0ef41Sopenharmony_ci */ 3981cb0ef41Sopenharmony_ci const ExternalOneByteStringResource* GetExternalOneByteStringResource() const; 3991cb0ef41Sopenharmony_ci 4001cb0ef41Sopenharmony_ci V8_INLINE static String* Cast(v8::Data* data) { 4011cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS 4021cb0ef41Sopenharmony_ci CheckCast(data); 4031cb0ef41Sopenharmony_ci#endif 4041cb0ef41Sopenharmony_ci return static_cast<String*>(data); 4051cb0ef41Sopenharmony_ci } 4061cb0ef41Sopenharmony_ci 4071cb0ef41Sopenharmony_ci /** 4081cb0ef41Sopenharmony_ci * Allocates a new string from a UTF-8 literal. This is equivalent to calling 4091cb0ef41Sopenharmony_ci * String::NewFromUtf(isolate, "...").ToLocalChecked(), but without the check 4101cb0ef41Sopenharmony_ci * overhead. 4111cb0ef41Sopenharmony_ci * 4121cb0ef41Sopenharmony_ci * When called on a string literal containing '\0', the inferred length is the 4131cb0ef41Sopenharmony_ci * length of the input array minus 1 (for the final '\0') and not the value 4141cb0ef41Sopenharmony_ci * returned by strlen. 4151cb0ef41Sopenharmony_ci **/ 4161cb0ef41Sopenharmony_ci template <int N> 4171cb0ef41Sopenharmony_ci static V8_WARN_UNUSED_RESULT Local<String> NewFromUtf8Literal( 4181cb0ef41Sopenharmony_ci Isolate* isolate, const char (&literal)[N], 4191cb0ef41Sopenharmony_ci NewStringType type = NewStringType::kNormal) { 4201cb0ef41Sopenharmony_ci static_assert(N <= kMaxLength, "String is too long"); 4211cb0ef41Sopenharmony_ci return NewFromUtf8Literal(isolate, literal, type, N - 1); 4221cb0ef41Sopenharmony_ci } 4231cb0ef41Sopenharmony_ci 4241cb0ef41Sopenharmony_ci /** Allocates a new string from UTF-8 data. Only returns an empty value when 4251cb0ef41Sopenharmony_ci * length > kMaxLength. **/ 4261cb0ef41Sopenharmony_ci static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromUtf8( 4271cb0ef41Sopenharmony_ci Isolate* isolate, const char* data, 4281cb0ef41Sopenharmony_ci NewStringType type = NewStringType::kNormal, int length = -1); 4291cb0ef41Sopenharmony_ci 4301cb0ef41Sopenharmony_ci /** Allocates a new string from Latin-1 data. Only returns an empty value 4311cb0ef41Sopenharmony_ci * when length > kMaxLength. **/ 4321cb0ef41Sopenharmony_ci static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromOneByte( 4331cb0ef41Sopenharmony_ci Isolate* isolate, const uint8_t* data, 4341cb0ef41Sopenharmony_ci NewStringType type = NewStringType::kNormal, int length = -1); 4351cb0ef41Sopenharmony_ci 4361cb0ef41Sopenharmony_ci /** Allocates a new string from UTF-16 data. Only returns an empty value when 4371cb0ef41Sopenharmony_ci * length > kMaxLength. **/ 4381cb0ef41Sopenharmony_ci static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromTwoByte( 4391cb0ef41Sopenharmony_ci Isolate* isolate, const uint16_t* data, 4401cb0ef41Sopenharmony_ci NewStringType type = NewStringType::kNormal, int length = -1); 4411cb0ef41Sopenharmony_ci 4421cb0ef41Sopenharmony_ci /** 4431cb0ef41Sopenharmony_ci * Creates a new string by concatenating the left and the right strings 4441cb0ef41Sopenharmony_ci * passed in as parameters. 4451cb0ef41Sopenharmony_ci */ 4461cb0ef41Sopenharmony_ci static Local<String> Concat(Isolate* isolate, Local<String> left, 4471cb0ef41Sopenharmony_ci Local<String> right); 4481cb0ef41Sopenharmony_ci 4491cb0ef41Sopenharmony_ci /** 4501cb0ef41Sopenharmony_ci * Creates a new external string using the data defined in the given 4511cb0ef41Sopenharmony_ci * resource. When the external string is no longer live on V8's heap the 4521cb0ef41Sopenharmony_ci * resource will be disposed by calling its Dispose method. The caller of 4531cb0ef41Sopenharmony_ci * this function should not otherwise delete or modify the resource. Neither 4541cb0ef41Sopenharmony_ci * should the underlying buffer be deallocated or modified except through the 4551cb0ef41Sopenharmony_ci * destructor of the external string resource. 4561cb0ef41Sopenharmony_ci */ 4571cb0ef41Sopenharmony_ci static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewExternalTwoByte( 4581cb0ef41Sopenharmony_ci Isolate* isolate, ExternalStringResource* resource); 4591cb0ef41Sopenharmony_ci 4601cb0ef41Sopenharmony_ci /** 4611cb0ef41Sopenharmony_ci * Associate an external string resource with this string by transforming it 4621cb0ef41Sopenharmony_ci * in place so that existing references to this string in the JavaScript heap 4631cb0ef41Sopenharmony_ci * will use the external string resource. The external string resource's 4641cb0ef41Sopenharmony_ci * character contents need to be equivalent to this string. 4651cb0ef41Sopenharmony_ci * Returns true if the string has been changed to be an external string. 4661cb0ef41Sopenharmony_ci * The string is not modified if the operation fails. See NewExternal for 4671cb0ef41Sopenharmony_ci * information on the lifetime of the resource. 4681cb0ef41Sopenharmony_ci */ 4691cb0ef41Sopenharmony_ci bool MakeExternal(ExternalStringResource* resource); 4701cb0ef41Sopenharmony_ci 4711cb0ef41Sopenharmony_ci /** 4721cb0ef41Sopenharmony_ci * Creates a new external string using the one-byte data defined in the given 4731cb0ef41Sopenharmony_ci * resource. When the external string is no longer live on V8's heap the 4741cb0ef41Sopenharmony_ci * resource will be disposed by calling its Dispose method. The caller of 4751cb0ef41Sopenharmony_ci * this function should not otherwise delete or modify the resource. Neither 4761cb0ef41Sopenharmony_ci * should the underlying buffer be deallocated or modified except through the 4771cb0ef41Sopenharmony_ci * destructor of the external string resource. 4781cb0ef41Sopenharmony_ci */ 4791cb0ef41Sopenharmony_ci static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewExternalOneByte( 4801cb0ef41Sopenharmony_ci Isolate* isolate, ExternalOneByteStringResource* resource); 4811cb0ef41Sopenharmony_ci 4821cb0ef41Sopenharmony_ci /** 4831cb0ef41Sopenharmony_ci * Associate an external string resource with this string by transforming it 4841cb0ef41Sopenharmony_ci * in place so that existing references to this string in the JavaScript heap 4851cb0ef41Sopenharmony_ci * will use the external string resource. The external string resource's 4861cb0ef41Sopenharmony_ci * character contents need to be equivalent to this string. 4871cb0ef41Sopenharmony_ci * Returns true if the string has been changed to be an external string. 4881cb0ef41Sopenharmony_ci * The string is not modified if the operation fails. See NewExternal for 4891cb0ef41Sopenharmony_ci * information on the lifetime of the resource. 4901cb0ef41Sopenharmony_ci */ 4911cb0ef41Sopenharmony_ci bool MakeExternal(ExternalOneByteStringResource* resource); 4921cb0ef41Sopenharmony_ci 4931cb0ef41Sopenharmony_ci /** 4941cb0ef41Sopenharmony_ci * Returns true if this string can be made external. 4951cb0ef41Sopenharmony_ci */ 4961cb0ef41Sopenharmony_ci V8_DEPRECATED("Use the version that takes an encoding as argument.") 4971cb0ef41Sopenharmony_ci bool CanMakeExternal() const; 4981cb0ef41Sopenharmony_ci 4991cb0ef41Sopenharmony_ci /** 5001cb0ef41Sopenharmony_ci * Returns true if this string can be made external, given the encoding for 5011cb0ef41Sopenharmony_ci * the external string resource. 5021cb0ef41Sopenharmony_ci */ 5031cb0ef41Sopenharmony_ci bool CanMakeExternal(Encoding encoding) const; 5041cb0ef41Sopenharmony_ci 5051cb0ef41Sopenharmony_ci /** 5061cb0ef41Sopenharmony_ci * Returns true if the strings values are equal. Same as JS ==/===. 5071cb0ef41Sopenharmony_ci */ 5081cb0ef41Sopenharmony_ci bool StringEquals(Local<String> str) const; 5091cb0ef41Sopenharmony_ci 5101cb0ef41Sopenharmony_ci /** 5111cb0ef41Sopenharmony_ci * Converts an object to a UTF-8-encoded character array. Useful if 5121cb0ef41Sopenharmony_ci * you want to print the object. If conversion to a string fails 5131cb0ef41Sopenharmony_ci * (e.g. due to an exception in the toString() method of the object) 5141cb0ef41Sopenharmony_ci * then the length() method returns 0 and the * operator returns 5151cb0ef41Sopenharmony_ci * NULL. 5161cb0ef41Sopenharmony_ci */ 5171cb0ef41Sopenharmony_ci class V8_EXPORT Utf8Value { 5181cb0ef41Sopenharmony_ci public: 5191cb0ef41Sopenharmony_ci Utf8Value(Isolate* isolate, Local<v8::Value> obj); 5201cb0ef41Sopenharmony_ci ~Utf8Value(); 5211cb0ef41Sopenharmony_ci char* operator*() { return str_; } 5221cb0ef41Sopenharmony_ci const char* operator*() const { return str_; } 5231cb0ef41Sopenharmony_ci int length() const { return length_; } 5241cb0ef41Sopenharmony_ci 5251cb0ef41Sopenharmony_ci // Disallow copying and assigning. 5261cb0ef41Sopenharmony_ci Utf8Value(const Utf8Value&) = delete; 5271cb0ef41Sopenharmony_ci void operator=(const Utf8Value&) = delete; 5281cb0ef41Sopenharmony_ci 5291cb0ef41Sopenharmony_ci private: 5301cb0ef41Sopenharmony_ci char* str_; 5311cb0ef41Sopenharmony_ci int length_; 5321cb0ef41Sopenharmony_ci }; 5331cb0ef41Sopenharmony_ci 5341cb0ef41Sopenharmony_ci /** 5351cb0ef41Sopenharmony_ci * Converts an object to a two-byte (UTF-16-encoded) string. 5361cb0ef41Sopenharmony_ci * If conversion to a string fails (eg. due to an exception in the toString() 5371cb0ef41Sopenharmony_ci * method of the object) then the length() method returns 0 and the * operator 5381cb0ef41Sopenharmony_ci * returns NULL. 5391cb0ef41Sopenharmony_ci */ 5401cb0ef41Sopenharmony_ci class V8_EXPORT Value { 5411cb0ef41Sopenharmony_ci public: 5421cb0ef41Sopenharmony_ci Value(Isolate* isolate, Local<v8::Value> obj); 5431cb0ef41Sopenharmony_ci ~Value(); 5441cb0ef41Sopenharmony_ci uint16_t* operator*() { return str_; } 5451cb0ef41Sopenharmony_ci const uint16_t* operator*() const { return str_; } 5461cb0ef41Sopenharmony_ci int length() const { return length_; } 5471cb0ef41Sopenharmony_ci 5481cb0ef41Sopenharmony_ci // Disallow copying and assigning. 5491cb0ef41Sopenharmony_ci Value(const Value&) = delete; 5501cb0ef41Sopenharmony_ci void operator=(const Value&) = delete; 5511cb0ef41Sopenharmony_ci 5521cb0ef41Sopenharmony_ci private: 5531cb0ef41Sopenharmony_ci uint16_t* str_; 5541cb0ef41Sopenharmony_ci int length_; 5551cb0ef41Sopenharmony_ci }; 5561cb0ef41Sopenharmony_ci 5571cb0ef41Sopenharmony_ci private: 5581cb0ef41Sopenharmony_ci void VerifyExternalStringResourceBase(ExternalStringResourceBase* v, 5591cb0ef41Sopenharmony_ci Encoding encoding) const; 5601cb0ef41Sopenharmony_ci void VerifyExternalStringResource(ExternalStringResource* val) const; 5611cb0ef41Sopenharmony_ci ExternalStringResource* GetExternalStringResourceSlow() const; 5621cb0ef41Sopenharmony_ci ExternalStringResourceBase* GetExternalStringResourceBaseSlow( 5631cb0ef41Sopenharmony_ci String::Encoding* encoding_out) const; 5641cb0ef41Sopenharmony_ci 5651cb0ef41Sopenharmony_ci static Local<v8::String> NewFromUtf8Literal(Isolate* isolate, 5661cb0ef41Sopenharmony_ci const char* literal, 5671cb0ef41Sopenharmony_ci NewStringType type, int length); 5681cb0ef41Sopenharmony_ci 5691cb0ef41Sopenharmony_ci static void CheckCast(v8::Data* that); 5701cb0ef41Sopenharmony_ci}; 5711cb0ef41Sopenharmony_ci 5721cb0ef41Sopenharmony_ci// Zero-length string specialization (templated string size includes 5731cb0ef41Sopenharmony_ci// terminator). 5741cb0ef41Sopenharmony_citemplate <> 5751cb0ef41Sopenharmony_ciinline V8_WARN_UNUSED_RESULT Local<String> String::NewFromUtf8Literal( 5761cb0ef41Sopenharmony_ci Isolate* isolate, const char (&literal)[1], NewStringType type) { 5771cb0ef41Sopenharmony_ci return String::Empty(isolate); 5781cb0ef41Sopenharmony_ci} 5791cb0ef41Sopenharmony_ci 5801cb0ef41Sopenharmony_ci/** 5811cb0ef41Sopenharmony_ci * Interface for iterating through all external resources in the heap. 5821cb0ef41Sopenharmony_ci */ 5831cb0ef41Sopenharmony_ciclass V8_EXPORT ExternalResourceVisitor { 5841cb0ef41Sopenharmony_ci public: 5851cb0ef41Sopenharmony_ci virtual ~ExternalResourceVisitor() = default; 5861cb0ef41Sopenharmony_ci virtual void VisitExternalString(Local<String> string) {} 5871cb0ef41Sopenharmony_ci}; 5881cb0ef41Sopenharmony_ci 5891cb0ef41Sopenharmony_ci/** 5901cb0ef41Sopenharmony_ci * A JavaScript symbol (ECMA-262 edition 6) 5911cb0ef41Sopenharmony_ci */ 5921cb0ef41Sopenharmony_ciclass V8_EXPORT Symbol : public Name { 5931cb0ef41Sopenharmony_ci public: 5941cb0ef41Sopenharmony_ci /** 5951cb0ef41Sopenharmony_ci * Returns the description string of the symbol, or undefined if none. 5961cb0ef41Sopenharmony_ci */ 5971cb0ef41Sopenharmony_ci Local<Value> Description(Isolate* isolate) const; 5981cb0ef41Sopenharmony_ci 5991cb0ef41Sopenharmony_ci /** 6001cb0ef41Sopenharmony_ci * Create a symbol. If description is not empty, it will be used as the 6011cb0ef41Sopenharmony_ci * description. 6021cb0ef41Sopenharmony_ci */ 6031cb0ef41Sopenharmony_ci static Local<Symbol> New(Isolate* isolate, 6041cb0ef41Sopenharmony_ci Local<String> description = Local<String>()); 6051cb0ef41Sopenharmony_ci 6061cb0ef41Sopenharmony_ci /** 6071cb0ef41Sopenharmony_ci * Access global symbol registry. 6081cb0ef41Sopenharmony_ci * Note that symbols created this way are never collected, so 6091cb0ef41Sopenharmony_ci * they should only be used for statically fixed properties. 6101cb0ef41Sopenharmony_ci * Also, there is only one global name space for the descriptions used as 6111cb0ef41Sopenharmony_ci * keys. 6121cb0ef41Sopenharmony_ci * To minimize the potential for clashes, use qualified names as keys. 6131cb0ef41Sopenharmony_ci */ 6141cb0ef41Sopenharmony_ci static Local<Symbol> For(Isolate* isolate, Local<String> description); 6151cb0ef41Sopenharmony_ci 6161cb0ef41Sopenharmony_ci /** 6171cb0ef41Sopenharmony_ci * Retrieve a global symbol. Similar to |For|, but using a separate 6181cb0ef41Sopenharmony_ci * registry that is not accessible by (and cannot clash with) JavaScript code. 6191cb0ef41Sopenharmony_ci */ 6201cb0ef41Sopenharmony_ci static Local<Symbol> ForApi(Isolate* isolate, Local<String> description); 6211cb0ef41Sopenharmony_ci 6221cb0ef41Sopenharmony_ci // Well-known symbols 6231cb0ef41Sopenharmony_ci static Local<Symbol> GetAsyncIterator(Isolate* isolate); 6241cb0ef41Sopenharmony_ci static Local<Symbol> GetHasInstance(Isolate* isolate); 6251cb0ef41Sopenharmony_ci static Local<Symbol> GetIsConcatSpreadable(Isolate* isolate); 6261cb0ef41Sopenharmony_ci static Local<Symbol> GetIterator(Isolate* isolate); 6271cb0ef41Sopenharmony_ci static Local<Symbol> GetMatch(Isolate* isolate); 6281cb0ef41Sopenharmony_ci static Local<Symbol> GetReplace(Isolate* isolate); 6291cb0ef41Sopenharmony_ci static Local<Symbol> GetSearch(Isolate* isolate); 6301cb0ef41Sopenharmony_ci static Local<Symbol> GetSplit(Isolate* isolate); 6311cb0ef41Sopenharmony_ci static Local<Symbol> GetToPrimitive(Isolate* isolate); 6321cb0ef41Sopenharmony_ci static Local<Symbol> GetToStringTag(Isolate* isolate); 6331cb0ef41Sopenharmony_ci static Local<Symbol> GetUnscopables(Isolate* isolate); 6341cb0ef41Sopenharmony_ci 6351cb0ef41Sopenharmony_ci V8_INLINE static Symbol* Cast(Data* data) { 6361cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS 6371cb0ef41Sopenharmony_ci CheckCast(data); 6381cb0ef41Sopenharmony_ci#endif 6391cb0ef41Sopenharmony_ci return static_cast<Symbol*>(data); 6401cb0ef41Sopenharmony_ci } 6411cb0ef41Sopenharmony_ci 6421cb0ef41Sopenharmony_ci private: 6431cb0ef41Sopenharmony_ci Symbol(); 6441cb0ef41Sopenharmony_ci static void CheckCast(Data* that); 6451cb0ef41Sopenharmony_ci}; 6461cb0ef41Sopenharmony_ci 6471cb0ef41Sopenharmony_ci/** 6481cb0ef41Sopenharmony_ci * A JavaScript number value (ECMA-262, 4.3.20) 6491cb0ef41Sopenharmony_ci */ 6501cb0ef41Sopenharmony_ciclass V8_EXPORT Number : public Primitive { 6511cb0ef41Sopenharmony_ci public: 6521cb0ef41Sopenharmony_ci double Value() const; 6531cb0ef41Sopenharmony_ci static Local<Number> New(Isolate* isolate, double value); 6541cb0ef41Sopenharmony_ci V8_INLINE static Number* Cast(v8::Data* data) { 6551cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS 6561cb0ef41Sopenharmony_ci CheckCast(data); 6571cb0ef41Sopenharmony_ci#endif 6581cb0ef41Sopenharmony_ci return static_cast<Number*>(data); 6591cb0ef41Sopenharmony_ci } 6601cb0ef41Sopenharmony_ci 6611cb0ef41Sopenharmony_ci private: 6621cb0ef41Sopenharmony_ci Number(); 6631cb0ef41Sopenharmony_ci static void CheckCast(v8::Data* that); 6641cb0ef41Sopenharmony_ci}; 6651cb0ef41Sopenharmony_ci 6661cb0ef41Sopenharmony_ci/** 6671cb0ef41Sopenharmony_ci * A JavaScript value representing a signed integer. 6681cb0ef41Sopenharmony_ci */ 6691cb0ef41Sopenharmony_ciclass V8_EXPORT Integer : public Number { 6701cb0ef41Sopenharmony_ci public: 6711cb0ef41Sopenharmony_ci static Local<Integer> New(Isolate* isolate, int32_t value); 6721cb0ef41Sopenharmony_ci static Local<Integer> NewFromUnsigned(Isolate* isolate, uint32_t value); 6731cb0ef41Sopenharmony_ci int64_t Value() const; 6741cb0ef41Sopenharmony_ci V8_INLINE static Integer* Cast(v8::Data* data) { 6751cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS 6761cb0ef41Sopenharmony_ci CheckCast(data); 6771cb0ef41Sopenharmony_ci#endif 6781cb0ef41Sopenharmony_ci return static_cast<Integer*>(data); 6791cb0ef41Sopenharmony_ci } 6801cb0ef41Sopenharmony_ci 6811cb0ef41Sopenharmony_ci private: 6821cb0ef41Sopenharmony_ci Integer(); 6831cb0ef41Sopenharmony_ci static void CheckCast(v8::Data* that); 6841cb0ef41Sopenharmony_ci}; 6851cb0ef41Sopenharmony_ci 6861cb0ef41Sopenharmony_ci/** 6871cb0ef41Sopenharmony_ci * A JavaScript value representing a 32-bit signed integer. 6881cb0ef41Sopenharmony_ci */ 6891cb0ef41Sopenharmony_ciclass V8_EXPORT Int32 : public Integer { 6901cb0ef41Sopenharmony_ci public: 6911cb0ef41Sopenharmony_ci int32_t Value() const; 6921cb0ef41Sopenharmony_ci V8_INLINE static Int32* Cast(v8::Data* data) { 6931cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS 6941cb0ef41Sopenharmony_ci CheckCast(data); 6951cb0ef41Sopenharmony_ci#endif 6961cb0ef41Sopenharmony_ci return static_cast<Int32*>(data); 6971cb0ef41Sopenharmony_ci } 6981cb0ef41Sopenharmony_ci 6991cb0ef41Sopenharmony_ci private: 7001cb0ef41Sopenharmony_ci Int32(); 7011cb0ef41Sopenharmony_ci static void CheckCast(v8::Data* that); 7021cb0ef41Sopenharmony_ci}; 7031cb0ef41Sopenharmony_ci 7041cb0ef41Sopenharmony_ci/** 7051cb0ef41Sopenharmony_ci * A JavaScript value representing a 32-bit unsigned integer. 7061cb0ef41Sopenharmony_ci */ 7071cb0ef41Sopenharmony_ciclass V8_EXPORT Uint32 : public Integer { 7081cb0ef41Sopenharmony_ci public: 7091cb0ef41Sopenharmony_ci uint32_t Value() const; 7101cb0ef41Sopenharmony_ci V8_INLINE static Uint32* Cast(v8::Data* data) { 7111cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS 7121cb0ef41Sopenharmony_ci CheckCast(data); 7131cb0ef41Sopenharmony_ci#endif 7141cb0ef41Sopenharmony_ci return static_cast<Uint32*>(data); 7151cb0ef41Sopenharmony_ci } 7161cb0ef41Sopenharmony_ci 7171cb0ef41Sopenharmony_ci private: 7181cb0ef41Sopenharmony_ci Uint32(); 7191cb0ef41Sopenharmony_ci static void CheckCast(v8::Data* that); 7201cb0ef41Sopenharmony_ci}; 7211cb0ef41Sopenharmony_ci 7221cb0ef41Sopenharmony_ci/** 7231cb0ef41Sopenharmony_ci * A JavaScript BigInt value (https://tc39.github.io/proposal-bigint) 7241cb0ef41Sopenharmony_ci */ 7251cb0ef41Sopenharmony_ciclass V8_EXPORT BigInt : public Primitive { 7261cb0ef41Sopenharmony_ci public: 7271cb0ef41Sopenharmony_ci static Local<BigInt> New(Isolate* isolate, int64_t value); 7281cb0ef41Sopenharmony_ci static Local<BigInt> NewFromUnsigned(Isolate* isolate, uint64_t value); 7291cb0ef41Sopenharmony_ci /** 7301cb0ef41Sopenharmony_ci * Creates a new BigInt object using a specified sign bit and a 7311cb0ef41Sopenharmony_ci * specified list of digits/words. 7321cb0ef41Sopenharmony_ci * The resulting number is calculated as: 7331cb0ef41Sopenharmony_ci * 7341cb0ef41Sopenharmony_ci * (-1)^sign_bit * (words[0] * (2^64)^0 + words[1] * (2^64)^1 + ...) 7351cb0ef41Sopenharmony_ci */ 7361cb0ef41Sopenharmony_ci static MaybeLocal<BigInt> NewFromWords(Local<Context> context, int sign_bit, 7371cb0ef41Sopenharmony_ci int word_count, const uint64_t* words); 7381cb0ef41Sopenharmony_ci 7391cb0ef41Sopenharmony_ci /** 7401cb0ef41Sopenharmony_ci * Returns the value of this BigInt as an unsigned 64-bit integer. 7411cb0ef41Sopenharmony_ci * If `lossless` is provided, it will reflect whether the return value was 7421cb0ef41Sopenharmony_ci * truncated or wrapped around. In particular, it is set to `false` if this 7431cb0ef41Sopenharmony_ci * BigInt is negative. 7441cb0ef41Sopenharmony_ci */ 7451cb0ef41Sopenharmony_ci uint64_t Uint64Value(bool* lossless = nullptr) const; 7461cb0ef41Sopenharmony_ci 7471cb0ef41Sopenharmony_ci /** 7481cb0ef41Sopenharmony_ci * Returns the value of this BigInt as a signed 64-bit integer. 7491cb0ef41Sopenharmony_ci * If `lossless` is provided, it will reflect whether this BigInt was 7501cb0ef41Sopenharmony_ci * truncated or not. 7511cb0ef41Sopenharmony_ci */ 7521cb0ef41Sopenharmony_ci int64_t Int64Value(bool* lossless = nullptr) const; 7531cb0ef41Sopenharmony_ci 7541cb0ef41Sopenharmony_ci /** 7551cb0ef41Sopenharmony_ci * Returns the number of 64-bit words needed to store the result of 7561cb0ef41Sopenharmony_ci * ToWordsArray(). 7571cb0ef41Sopenharmony_ci */ 7581cb0ef41Sopenharmony_ci int WordCount() const; 7591cb0ef41Sopenharmony_ci 7601cb0ef41Sopenharmony_ci /** 7611cb0ef41Sopenharmony_ci * Writes the contents of this BigInt to a specified memory location. 7621cb0ef41Sopenharmony_ci * `sign_bit` must be provided and will be set to 1 if this BigInt is 7631cb0ef41Sopenharmony_ci * negative. 7641cb0ef41Sopenharmony_ci * `*word_count` has to be initialized to the length of the `words` array. 7651cb0ef41Sopenharmony_ci * Upon return, it will be set to the actual number of words that would 7661cb0ef41Sopenharmony_ci * be needed to store this BigInt (i.e. the return value of `WordCount()`). 7671cb0ef41Sopenharmony_ci */ 7681cb0ef41Sopenharmony_ci void ToWordsArray(int* sign_bit, int* word_count, uint64_t* words) const; 7691cb0ef41Sopenharmony_ci 7701cb0ef41Sopenharmony_ci V8_INLINE static BigInt* Cast(v8::Data* data) { 7711cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS 7721cb0ef41Sopenharmony_ci CheckCast(data); 7731cb0ef41Sopenharmony_ci#endif 7741cb0ef41Sopenharmony_ci return static_cast<BigInt*>(data); 7751cb0ef41Sopenharmony_ci } 7761cb0ef41Sopenharmony_ci 7771cb0ef41Sopenharmony_ci private: 7781cb0ef41Sopenharmony_ci BigInt(); 7791cb0ef41Sopenharmony_ci static void CheckCast(v8::Data* that); 7801cb0ef41Sopenharmony_ci}; 7811cb0ef41Sopenharmony_ci 7821cb0ef41Sopenharmony_ciLocal<String> String::Empty(Isolate* isolate) { 7831cb0ef41Sopenharmony_ci using S = internal::Address; 7841cb0ef41Sopenharmony_ci using I = internal::Internals; 7851cb0ef41Sopenharmony_ci I::CheckInitialized(isolate); 7861cb0ef41Sopenharmony_ci S* slot = I::GetRootSlot(isolate, I::kEmptyStringRootIndex); 7871cb0ef41Sopenharmony_ci return Local<String>::FromSlot(slot); 7881cb0ef41Sopenharmony_ci} 7891cb0ef41Sopenharmony_ci 7901cb0ef41Sopenharmony_ciString::ExternalStringResource* String::GetExternalStringResource() const { 7911cb0ef41Sopenharmony_ci using A = internal::Address; 7921cb0ef41Sopenharmony_ci using I = internal::Internals; 7931cb0ef41Sopenharmony_ci A obj = internal::ValueHelper::ValueAsAddress(this); 7941cb0ef41Sopenharmony_ci 7951cb0ef41Sopenharmony_ci ExternalStringResource* result; 7961cb0ef41Sopenharmony_ci if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) { 7971cb0ef41Sopenharmony_ci Isolate* isolate = I::GetIsolateForSandbox(obj); 7981cb0ef41Sopenharmony_ci A value = I::ReadExternalPointerField<internal::kExternalStringResourceTag>( 7991cb0ef41Sopenharmony_ci isolate, obj, I::kStringResourceOffset); 8001cb0ef41Sopenharmony_ci result = reinterpret_cast<String::ExternalStringResource*>(value); 8011cb0ef41Sopenharmony_ci } else { 8021cb0ef41Sopenharmony_ci result = GetExternalStringResourceSlow(); 8031cb0ef41Sopenharmony_ci } 8041cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS 8051cb0ef41Sopenharmony_ci VerifyExternalStringResource(result); 8061cb0ef41Sopenharmony_ci#endif 8071cb0ef41Sopenharmony_ci return result; 8081cb0ef41Sopenharmony_ci} 8091cb0ef41Sopenharmony_ci 8101cb0ef41Sopenharmony_ciString::ExternalStringResourceBase* String::GetExternalStringResourceBase( 8111cb0ef41Sopenharmony_ci String::Encoding* encoding_out) const { 8121cb0ef41Sopenharmony_ci using A = internal::Address; 8131cb0ef41Sopenharmony_ci using I = internal::Internals; 8141cb0ef41Sopenharmony_ci A obj = internal::ValueHelper::ValueAsAddress(this); 8151cb0ef41Sopenharmony_ci int type = I::GetInstanceType(obj) & I::kStringRepresentationAndEncodingMask; 8161cb0ef41Sopenharmony_ci *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask); 8171cb0ef41Sopenharmony_ci ExternalStringResourceBase* resource; 8181cb0ef41Sopenharmony_ci if (type == I::kExternalOneByteRepresentationTag || 8191cb0ef41Sopenharmony_ci type == I::kExternalTwoByteRepresentationTag) { 8201cb0ef41Sopenharmony_ci Isolate* isolate = I::GetIsolateForSandbox(obj); 8211cb0ef41Sopenharmony_ci A value = I::ReadExternalPointerField<internal::kExternalStringResourceTag>( 8221cb0ef41Sopenharmony_ci isolate, obj, I::kStringResourceOffset); 8231cb0ef41Sopenharmony_ci resource = reinterpret_cast<ExternalStringResourceBase*>(value); 8241cb0ef41Sopenharmony_ci } else { 8251cb0ef41Sopenharmony_ci resource = GetExternalStringResourceBaseSlow(encoding_out); 8261cb0ef41Sopenharmony_ci } 8271cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS 8281cb0ef41Sopenharmony_ci VerifyExternalStringResourceBase(resource, *encoding_out); 8291cb0ef41Sopenharmony_ci#endif 8301cb0ef41Sopenharmony_ci return resource; 8311cb0ef41Sopenharmony_ci} 8321cb0ef41Sopenharmony_ci 8331cb0ef41Sopenharmony_ci// --- Statics --- 8341cb0ef41Sopenharmony_ci 8351cb0ef41Sopenharmony_ciV8_INLINE Local<Primitive> Undefined(Isolate* isolate) { 8361cb0ef41Sopenharmony_ci using S = internal::Address; 8371cb0ef41Sopenharmony_ci using I = internal::Internals; 8381cb0ef41Sopenharmony_ci I::CheckInitialized(isolate); 8391cb0ef41Sopenharmony_ci S* slot = I::GetRootSlot(isolate, I::kUndefinedValueRootIndex); 8401cb0ef41Sopenharmony_ci return Local<Primitive>::FromSlot(slot); 8411cb0ef41Sopenharmony_ci} 8421cb0ef41Sopenharmony_ci 8431cb0ef41Sopenharmony_ciV8_INLINE Local<Primitive> Null(Isolate* isolate) { 8441cb0ef41Sopenharmony_ci using S = internal::Address; 8451cb0ef41Sopenharmony_ci using I = internal::Internals; 8461cb0ef41Sopenharmony_ci I::CheckInitialized(isolate); 8471cb0ef41Sopenharmony_ci S* slot = I::GetRootSlot(isolate, I::kNullValueRootIndex); 8481cb0ef41Sopenharmony_ci return Local<Primitive>::FromSlot(slot); 8491cb0ef41Sopenharmony_ci} 8501cb0ef41Sopenharmony_ci 8511cb0ef41Sopenharmony_ciV8_INLINE Local<Boolean> True(Isolate* isolate) { 8521cb0ef41Sopenharmony_ci using S = internal::Address; 8531cb0ef41Sopenharmony_ci using I = internal::Internals; 8541cb0ef41Sopenharmony_ci I::CheckInitialized(isolate); 8551cb0ef41Sopenharmony_ci S* slot = I::GetRootSlot(isolate, I::kTrueValueRootIndex); 8561cb0ef41Sopenharmony_ci return Local<Boolean>::FromSlot(slot); 8571cb0ef41Sopenharmony_ci} 8581cb0ef41Sopenharmony_ci 8591cb0ef41Sopenharmony_ciV8_INLINE Local<Boolean> False(Isolate* isolate) { 8601cb0ef41Sopenharmony_ci using S = internal::Address; 8611cb0ef41Sopenharmony_ci using I = internal::Internals; 8621cb0ef41Sopenharmony_ci I::CheckInitialized(isolate); 8631cb0ef41Sopenharmony_ci S* slot = I::GetRootSlot(isolate, I::kFalseValueRootIndex); 8641cb0ef41Sopenharmony_ci return Local<Boolean>::FromSlot(slot); 8651cb0ef41Sopenharmony_ci} 8661cb0ef41Sopenharmony_ci 8671cb0ef41Sopenharmony_ciLocal<Boolean> Boolean::New(Isolate* isolate, bool value) { 8681cb0ef41Sopenharmony_ci return value ? True(isolate) : False(isolate); 8691cb0ef41Sopenharmony_ci} 8701cb0ef41Sopenharmony_ci 8711cb0ef41Sopenharmony_ci} // namespace v8 8721cb0ef41Sopenharmony_ci 8731cb0ef41Sopenharmony_ci#endif // INCLUDE_V8_PRIMITIVE_H_ 874