/src/node/deps/v8/include/v8-primitive.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2021 the V8 project authors. All rights reserved. |
2 | | // Use of this source code is governed by a BSD-style license that can be |
3 | | // found in the LICENSE file. |
4 | | |
5 | | #ifndef INCLUDE_V8_PRIMITIVE_H_ |
6 | | #define INCLUDE_V8_PRIMITIVE_H_ |
7 | | |
8 | | #include "v8-data.h" // NOLINT(build/include_directory) |
9 | | #include "v8-internal.h" // NOLINT(build/include_directory) |
10 | | #include "v8-local-handle.h" // NOLINT(build/include_directory) |
11 | | #include "v8-value.h" // NOLINT(build/include_directory) |
12 | | #include "v8config.h" // NOLINT(build/include_directory) |
13 | | |
14 | | namespace v8 { |
15 | | |
16 | | class Context; |
17 | | class Isolate; |
18 | | class String; |
19 | | |
20 | | namespace internal { |
21 | | class ExternalString; |
22 | | class ScopedExternalStringLock; |
23 | | class StringForwardingTable; |
24 | | } // namespace internal |
25 | | |
26 | | /** |
27 | | * The superclass of primitive values. See ECMA-262 4.3.2. |
28 | | */ |
29 | | class V8_EXPORT Primitive : public Value {}; |
30 | | |
31 | | /** |
32 | | * A primitive boolean value (ECMA-262, 4.3.14). Either the true |
33 | | * or false value. |
34 | | */ |
35 | | class V8_EXPORT Boolean : public Primitive { |
36 | | public: |
37 | | bool Value() const; |
38 | 0 | V8_INLINE static Boolean* Cast(v8::Data* data) { |
39 | 0 | #ifdef V8_ENABLE_CHECKS |
40 | 0 | CheckCast(data); |
41 | 0 | #endif |
42 | 0 | return static_cast<Boolean*>(data); |
43 | 0 | } |
44 | | |
45 | | V8_INLINE static Local<Boolean> New(Isolate* isolate, bool value); |
46 | | |
47 | | private: |
48 | | static void CheckCast(v8::Data* that); |
49 | | }; |
50 | | |
51 | | /** |
52 | | * An array to hold Primitive values. This is used by the embedder to |
53 | | * pass host defined options to the ScriptOptions during compilation. |
54 | | * |
55 | | * This is passed back to the embedder as part of |
56 | | * HostImportModuleDynamicallyCallback for module loading. |
57 | | */ |
58 | | class V8_EXPORT PrimitiveArray : public Data { |
59 | | public: |
60 | | static Local<PrimitiveArray> New(Isolate* isolate, int length); |
61 | | int Length() const; |
62 | | void Set(Isolate* isolate, int index, Local<Primitive> item); |
63 | | Local<Primitive> Get(Isolate* isolate, int index); |
64 | | |
65 | 0 | V8_INLINE static PrimitiveArray* Cast(Data* data) { |
66 | 0 | #ifdef V8_ENABLE_CHECKS |
67 | 0 | CheckCast(data); |
68 | 0 | #endif |
69 | 0 | return reinterpret_cast<PrimitiveArray*>(data); |
70 | 0 | } |
71 | | |
72 | | private: |
73 | | static void CheckCast(Data* obj); |
74 | | }; |
75 | | |
76 | | /** |
77 | | * A superclass for symbols and strings. |
78 | | */ |
79 | | class V8_EXPORT Name : public Primitive { |
80 | | public: |
81 | | /** |
82 | | * Returns the identity hash for this object. The current implementation |
83 | | * uses an inline property on the object to store the identity hash. |
84 | | * |
85 | | * The return value will never be 0. Also, it is not guaranteed to be |
86 | | * unique. |
87 | | */ |
88 | | int GetIdentityHash(); |
89 | | |
90 | 0 | V8_INLINE static Name* Cast(Data* data) { |
91 | 0 | #ifdef V8_ENABLE_CHECKS |
92 | 0 | CheckCast(data); |
93 | 0 | #endif |
94 | 0 | return static_cast<Name*>(data); |
95 | 0 | } |
96 | | |
97 | | private: |
98 | | static void CheckCast(Data* that); |
99 | | }; |
100 | | |
101 | | /** |
102 | | * A flag describing different modes of string creation. |
103 | | * |
104 | | * Aside from performance implications there are no differences between the two |
105 | | * creation modes. |
106 | | */ |
107 | | enum class NewStringType { |
108 | | /** |
109 | | * Create a new string, always allocating new storage memory. |
110 | | */ |
111 | | kNormal, |
112 | | |
113 | | /** |
114 | | * Acts as a hint that the string should be created in the |
115 | | * old generation heap space and be deduplicated if an identical string |
116 | | * already exists. |
117 | | */ |
118 | | kInternalized |
119 | | }; |
120 | | |
121 | | /** |
122 | | * A JavaScript string value (ECMA-262, 4.3.17). |
123 | | */ |
124 | | class V8_EXPORT String : public Name { |
125 | | public: |
126 | | static constexpr int kMaxLength = |
127 | | internal::kApiSystemPointerSize == 4 ? (1 << 28) - 16 : (1 << 29) - 24; |
128 | | |
129 | | enum Encoding { |
130 | | UNKNOWN_ENCODING = 0x1, |
131 | | TWO_BYTE_ENCODING = 0x0, |
132 | | ONE_BYTE_ENCODING = 0x8 |
133 | | }; |
134 | | /** |
135 | | * Returns the number of characters (UTF-16 code units) in this string. |
136 | | */ |
137 | | int Length() const; |
138 | | |
139 | | /** |
140 | | * Returns the number of bytes in the UTF-8 encoded |
141 | | * representation of this string. |
142 | | */ |
143 | | int Utf8Length(Isolate* isolate) const; |
144 | | |
145 | | /** |
146 | | * Returns whether this string is known to contain only one byte data, |
147 | | * i.e. ISO-8859-1 code points. |
148 | | * Does not read the string. |
149 | | * False negatives are possible. |
150 | | */ |
151 | | bool IsOneByte() const; |
152 | | |
153 | | /** |
154 | | * Returns whether this string contain only one byte data, |
155 | | * i.e. ISO-8859-1 code points. |
156 | | * Will read the entire string in some cases. |
157 | | */ |
158 | | bool ContainsOnlyOneByte() const; |
159 | | |
160 | | /** |
161 | | * Write the contents of the string to an external buffer. |
162 | | * If no arguments are given, expects the buffer to be large |
163 | | * enough to hold the entire string and NULL terminator. Copies |
164 | | * the contents of the string and the NULL terminator into the |
165 | | * buffer. |
166 | | * |
167 | | * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop |
168 | | * before the end of the buffer. |
169 | | * |
170 | | * Copies up to length characters into the output buffer. |
171 | | * Only null-terminates if there is enough space in the buffer. |
172 | | * |
173 | | * \param buffer The buffer into which the string will be copied. |
174 | | * \param start The starting position within the string at which |
175 | | * copying begins. |
176 | | * \param length The number of characters to copy from the string. For |
177 | | * WriteUtf8 the number of bytes in the buffer. |
178 | | * \param nchars_ref The number of characters written, can be NULL. |
179 | | * \param options Various options that might affect performance of this or |
180 | | * subsequent operations. |
181 | | * \return The number of characters copied to the buffer excluding the null |
182 | | * terminator. For WriteUtf8: The number of bytes copied to the buffer |
183 | | * including the null terminator (if written). |
184 | | */ |
185 | | enum WriteOptions { |
186 | | NO_OPTIONS = 0, |
187 | | HINT_MANY_WRITES_EXPECTED = 1, |
188 | | NO_NULL_TERMINATION = 2, |
189 | | PRESERVE_ONE_BYTE_NULL = 4, |
190 | | // Used by WriteUtf8 to replace orphan surrogate code units with the |
191 | | // unicode replacement character. Needs to be set to guarantee valid UTF-8 |
192 | | // output. |
193 | | REPLACE_INVALID_UTF8 = 8 |
194 | | }; |
195 | | |
196 | | // 16-bit character codes. |
197 | | int Write(Isolate* isolate, uint16_t* buffer, int start = 0, int length = -1, |
198 | | int options = NO_OPTIONS) const; |
199 | | // One byte characters. |
200 | | int WriteOneByte(Isolate* isolate, uint8_t* buffer, int start = 0, |
201 | | int length = -1, int options = NO_OPTIONS) const; |
202 | | // UTF-8 encoded characters. |
203 | | int WriteUtf8(Isolate* isolate, char* buffer, int length = -1, |
204 | | int* nchars_ref = nullptr, int options = NO_OPTIONS) const; |
205 | | |
206 | | /** |
207 | | * A zero length string. |
208 | | */ |
209 | | V8_INLINE static Local<String> Empty(Isolate* isolate); |
210 | | |
211 | | /** |
212 | | * Returns true if the string is external. |
213 | | */ |
214 | | bool IsExternal() const; |
215 | | |
216 | | /** |
217 | | * Returns true if the string is both external and two-byte. |
218 | | */ |
219 | | bool IsExternalTwoByte() const; |
220 | | |
221 | | /** |
222 | | * Returns true if the string is both external and one-byte. |
223 | | */ |
224 | | bool IsExternalOneByte() const; |
225 | | |
226 | | class V8_EXPORT ExternalStringResourceBase { |
227 | | public: |
228 | 0 | virtual ~ExternalStringResourceBase() = default; |
229 | | |
230 | | /** |
231 | | * If a string is cacheable, the value returned by |
232 | | * ExternalStringResource::data() may be cached, otherwise it is not |
233 | | * expected to be stable beyond the current top-level task. |
234 | | */ |
235 | 11.4M | virtual bool IsCacheable() const { return true; } |
236 | | |
237 | | // Disallow copying and assigning. |
238 | | ExternalStringResourceBase(const ExternalStringResourceBase&) = delete; |
239 | | void operator=(const ExternalStringResourceBase&) = delete; |
240 | | |
241 | | protected: |
242 | 40.5M | ExternalStringResourceBase() = default; |
243 | | |
244 | | /** |
245 | | * Internally V8 will call this Dispose method when the external string |
246 | | * resource is no longer needed. The default implementation will use the |
247 | | * delete operator. This method can be overridden in subclasses to |
248 | | * control how allocated external string resources are disposed. |
249 | | */ |
250 | 0 | virtual void Dispose() { delete this; } |
251 | | |
252 | | /** |
253 | | * For a non-cacheable string, the value returned by |
254 | | * |ExternalStringResource::data()| has to be stable between |Lock()| and |
255 | | * |Unlock()|, that is the string must behave as is |IsCacheable()| returned |
256 | | * true. |
257 | | * |
258 | | * These two functions must be thread-safe, and can be called from anywhere. |
259 | | * They also must handle lock depth, in the sense that each can be called |
260 | | * several times, from different threads, and unlocking should only happen |
261 | | * when the balance of Lock() and Unlock() calls is 0. |
262 | | */ |
263 | 35.7M | virtual void Lock() const {} |
264 | | |
265 | | /** |
266 | | * Unlocks the string. |
267 | | */ |
268 | 35.7M | virtual void Unlock() const {} |
269 | | |
270 | | private: |
271 | | friend class internal::ExternalString; |
272 | | friend class v8::String; |
273 | | friend class internal::StringForwardingTable; |
274 | | friend class internal::ScopedExternalStringLock; |
275 | | }; |
276 | | |
277 | | /** |
278 | | * An ExternalStringResource is a wrapper around a two-byte string |
279 | | * buffer that resides outside V8's heap. Implement an |
280 | | * ExternalStringResource to manage the life cycle of the underlying |
281 | | * buffer. Note that the string data must be immutable. |
282 | | */ |
283 | | class V8_EXPORT ExternalStringResource : public ExternalStringResourceBase { |
284 | | public: |
285 | | /** |
286 | | * Override the destructor to manage the life cycle of the underlying |
287 | | * buffer. |
288 | | */ |
289 | | ~ExternalStringResource() override = default; |
290 | | |
291 | | /** |
292 | | * The string data from the underlying buffer. If the resource is cacheable |
293 | | * then data() must return the same value for all invocations. |
294 | | */ |
295 | | virtual const uint16_t* data() const = 0; |
296 | | |
297 | | /** |
298 | | * The length of the string. That is, the number of two-byte characters. |
299 | | */ |
300 | | virtual size_t length() const = 0; |
301 | | |
302 | | /** |
303 | | * Returns the cached data from the underlying buffer. This method can be |
304 | | * called only for cacheable resources (i.e. IsCacheable() == true) and only |
305 | | * after UpdateDataCache() was called. |
306 | | */ |
307 | 0 | const uint16_t* cached_data() const { |
308 | 0 | CheckCachedDataInvariants(); |
309 | 0 | return cached_data_; |
310 | 0 | } |
311 | | |
312 | | /** |
313 | | * Update {cached_data_} with the data from the underlying buffer. This can |
314 | | * be called only for cacheable resources. |
315 | | */ |
316 | | void UpdateDataCache(); |
317 | | |
318 | | protected: |
319 | 488k | ExternalStringResource() = default; |
320 | | |
321 | | private: |
322 | | void CheckCachedDataInvariants() const; |
323 | | |
324 | | const uint16_t* cached_data_ = nullptr; |
325 | | }; |
326 | | |
327 | | /** |
328 | | * An ExternalOneByteStringResource is a wrapper around an one-byte |
329 | | * string buffer that resides outside V8's heap. Implement an |
330 | | * ExternalOneByteStringResource to manage the life cycle of the |
331 | | * underlying buffer. Note that the string data must be immutable |
332 | | * and that the data must be Latin-1 and not UTF-8, which would require |
333 | | * special treatment internally in the engine and do not allow efficient |
334 | | * indexing. Use String::New or convert to 16 bit data for non-Latin1. |
335 | | */ |
336 | | |
337 | | class V8_EXPORT ExternalOneByteStringResource |
338 | | : public ExternalStringResourceBase { |
339 | | public: |
340 | | /** |
341 | | * Override the destructor to manage the life cycle of the underlying |
342 | | * buffer. |
343 | | */ |
344 | | ~ExternalOneByteStringResource() override = default; |
345 | | |
346 | | /** |
347 | | * The string data from the underlying buffer. If the resource is cacheable |
348 | | * then data() must return the same value for all invocations. |
349 | | */ |
350 | | virtual const char* data() const = 0; |
351 | | |
352 | | /** The number of Latin-1 characters in the string.*/ |
353 | | virtual size_t length() const = 0; |
354 | | |
355 | | /** |
356 | | * Returns the cached data from the underlying buffer. If the resource is |
357 | | * uncacheable or if UpdateDataCache() was not called before, it has |
358 | | * undefined behaviour. |
359 | | */ |
360 | 0 | const char* cached_data() const { |
361 | 0 | CheckCachedDataInvariants(); |
362 | 0 | return cached_data_; |
363 | 0 | } |
364 | | |
365 | | /** |
366 | | * Update {cached_data_} with the data from the underlying buffer. This can |
367 | | * be called only for cacheable resources. |
368 | | */ |
369 | | void UpdateDataCache(); |
370 | | |
371 | | protected: |
372 | 40.0M | ExternalOneByteStringResource() = default; |
373 | | |
374 | | private: |
375 | | void CheckCachedDataInvariants() const; |
376 | | |
377 | | const char* cached_data_ = nullptr; |
378 | | }; |
379 | | |
380 | | /** |
381 | | * If the string is an external string, return the ExternalStringResourceBase |
382 | | * regardless of the encoding, otherwise return NULL. The encoding of the |
383 | | * string is returned in encoding_out. |
384 | | */ |
385 | | V8_INLINE ExternalStringResourceBase* GetExternalStringResourceBase( |
386 | | Encoding* encoding_out) const; |
387 | | |
388 | | /** |
389 | | * Get the ExternalStringResource for an external string. Returns |
390 | | * NULL if IsExternal() doesn't return true. |
391 | | */ |
392 | | V8_INLINE ExternalStringResource* GetExternalStringResource() const; |
393 | | |
394 | | /** |
395 | | * Get the ExternalOneByteStringResource for an external one-byte string. |
396 | | * Returns NULL if IsExternalOneByte() doesn't return true. |
397 | | */ |
398 | | const ExternalOneByteStringResource* GetExternalOneByteStringResource() const; |
399 | | |
400 | 0 | V8_INLINE static String* Cast(v8::Data* data) { |
401 | | #ifdef V8_ENABLE_CHECKS |
402 | | CheckCast(data); |
403 | | #endif |
404 | 0 | return static_cast<String*>(data); |
405 | 0 | } |
406 | | |
407 | | /** |
408 | | * Allocates a new string from a UTF-8 literal. This is equivalent to calling |
409 | | * String::NewFromUtf(isolate, "...").ToLocalChecked(), but without the check |
410 | | * overhead. |
411 | | * |
412 | | * When called on a string literal containing '\0', the inferred length is the |
413 | | * length of the input array minus 1 (for the final '\0') and not the value |
414 | | * returned by strlen. |
415 | | **/ |
416 | | template <int N> |
417 | | static V8_WARN_UNUSED_RESULT Local<String> NewFromUtf8Literal( |
418 | | Isolate* isolate, const char (&literal)[N], |
419 | 0 | NewStringType type = NewStringType::kNormal) { |
420 | 0 | static_assert(N <= kMaxLength, "String is too long"); |
421 | 0 | return NewFromUtf8Literal(isolate, literal, type, N - 1); |
422 | 0 | } |
423 | | |
424 | | /** Allocates a new string from UTF-8 data. Only returns an empty value when |
425 | | * length > kMaxLength. **/ |
426 | | static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromUtf8( |
427 | | Isolate* isolate, const char* data, |
428 | | NewStringType type = NewStringType::kNormal, int length = -1); |
429 | | |
430 | | /** Allocates a new string from Latin-1 data. Only returns an empty value |
431 | | * when length > kMaxLength. **/ |
432 | | static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromOneByte( |
433 | | Isolate* isolate, const uint8_t* data, |
434 | | NewStringType type = NewStringType::kNormal, int length = -1); |
435 | | |
436 | | /** Allocates a new string from UTF-16 data. Only returns an empty value when |
437 | | * length > kMaxLength. **/ |
438 | | static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromTwoByte( |
439 | | Isolate* isolate, const uint16_t* data, |
440 | | NewStringType type = NewStringType::kNormal, int length = -1); |
441 | | |
442 | | /** |
443 | | * Creates a new string by concatenating the left and the right strings |
444 | | * passed in as parameters. |
445 | | */ |
446 | | static Local<String> Concat(Isolate* isolate, Local<String> left, |
447 | | Local<String> right); |
448 | | |
449 | | /** |
450 | | * Creates a new external string using the data defined in the given |
451 | | * resource. When the external string is no longer live on V8's heap the |
452 | | * resource will be disposed by calling its Dispose method. The caller of |
453 | | * this function should not otherwise delete or modify the resource. Neither |
454 | | * should the underlying buffer be deallocated or modified except through the |
455 | | * destructor of the external string resource. |
456 | | */ |
457 | | static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewExternalTwoByte( |
458 | | Isolate* isolate, ExternalStringResource* resource); |
459 | | |
460 | | /** |
461 | | * Associate an external string resource with this string by transforming it |
462 | | * in place so that existing references to this string in the JavaScript heap |
463 | | * will use the external string resource. The external string resource's |
464 | | * character contents need to be equivalent to this string. |
465 | | * Returns true if the string has been changed to be an external string. |
466 | | * The string is not modified if the operation fails. See NewExternal for |
467 | | * information on the lifetime of the resource. |
468 | | */ |
469 | | bool MakeExternal(ExternalStringResource* resource); |
470 | | |
471 | | /** |
472 | | * Creates a new external string using the one-byte data defined in the given |
473 | | * resource. When the external string is no longer live on V8's heap the |
474 | | * resource will be disposed by calling its Dispose method. The caller of |
475 | | * this function should not otherwise delete or modify the resource. Neither |
476 | | * should the underlying buffer be deallocated or modified except through the |
477 | | * destructor of the external string resource. |
478 | | */ |
479 | | static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewExternalOneByte( |
480 | | Isolate* isolate, ExternalOneByteStringResource* resource); |
481 | | |
482 | | /** |
483 | | * Associate an external string resource with this string by transforming it |
484 | | * in place so that existing references to this string in the JavaScript heap |
485 | | * will use the external string resource. The external string resource's |
486 | | * character contents need to be equivalent to this string. |
487 | | * Returns true if the string has been changed to be an external string. |
488 | | * The string is not modified if the operation fails. See NewExternal for |
489 | | * information on the lifetime of the resource. |
490 | | */ |
491 | | bool MakeExternal(ExternalOneByteStringResource* resource); |
492 | | |
493 | | /** |
494 | | * Returns true if this string can be made external, given the encoding for |
495 | | * the external string resource. |
496 | | */ |
497 | | bool CanMakeExternal(Encoding encoding) const; |
498 | | |
499 | | /** |
500 | | * Returns true if the strings values are equal. Same as JS ==/===. |
501 | | */ |
502 | | bool StringEquals(Local<String> str) const; |
503 | | |
504 | | /** |
505 | | * Converts an object to a UTF-8-encoded character array. Useful if |
506 | | * you want to print the object. If conversion to a string fails |
507 | | * (e.g. due to an exception in the toString() method of the object) |
508 | | * then the length() method returns 0 and the * operator returns |
509 | | * NULL. |
510 | | */ |
511 | | class V8_EXPORT Utf8Value { |
512 | | public: |
513 | | Utf8Value(Isolate* isolate, Local<v8::Value> obj); |
514 | | ~Utf8Value(); |
515 | 400 | char* operator*() { return str_; } |
516 | 0 | const char* operator*() const { return str_; } |
517 | 0 | int length() const { return length_; } |
518 | | |
519 | | // Disallow copying and assigning. |
520 | | Utf8Value(const Utf8Value&) = delete; |
521 | | void operator=(const Utf8Value&) = delete; |
522 | | |
523 | | private: |
524 | | char* str_; |
525 | | int length_; |
526 | | }; |
527 | | |
528 | | /** |
529 | | * Converts an object to a two-byte (UTF-16-encoded) string. |
530 | | * If conversion to a string fails (eg. due to an exception in the toString() |
531 | | * method of the object) then the length() method returns 0 and the * operator |
532 | | * returns NULL. |
533 | | */ |
534 | | class V8_EXPORT Value { |
535 | | public: |
536 | | Value(Isolate* isolate, Local<v8::Value> obj); |
537 | | ~Value(); |
538 | 26 | uint16_t* operator*() { return str_; } |
539 | 0 | const uint16_t* operator*() const { return str_; } |
540 | 26 | int length() const { return length_; } |
541 | | |
542 | | // Disallow copying and assigning. |
543 | | Value(const Value&) = delete; |
544 | | void operator=(const Value&) = delete; |
545 | | |
546 | | private: |
547 | | uint16_t* str_; |
548 | | int length_; |
549 | | }; |
550 | | |
551 | | private: |
552 | | void VerifyExternalStringResourceBase(ExternalStringResourceBase* v, |
553 | | Encoding encoding) const; |
554 | | void VerifyExternalStringResource(ExternalStringResource* val) const; |
555 | | ExternalStringResource* GetExternalStringResourceSlow() const; |
556 | | ExternalStringResourceBase* GetExternalStringResourceBaseSlow( |
557 | | String::Encoding* encoding_out) const; |
558 | | |
559 | | static Local<v8::String> NewFromUtf8Literal(Isolate* isolate, |
560 | | const char* literal, |
561 | | NewStringType type, int length); |
562 | | |
563 | | static void CheckCast(v8::Data* that); |
564 | | }; |
565 | | |
566 | | // Zero-length string specialization (templated string size includes |
567 | | // terminator). |
568 | | template <> |
569 | | inline V8_WARN_UNUSED_RESULT Local<String> String::NewFromUtf8Literal( |
570 | 0 | Isolate* isolate, const char (&literal)[1], NewStringType type) { |
571 | 0 | return String::Empty(isolate); |
572 | 0 | } |
573 | | |
574 | | /** |
575 | | * Interface for iterating through all external resources in the heap. |
576 | | */ |
577 | | class V8_EXPORT ExternalResourceVisitor { |
578 | | public: |
579 | | virtual ~ExternalResourceVisitor() = default; |
580 | 0 | virtual void VisitExternalString(Local<String> string) {} |
581 | | }; |
582 | | |
583 | | /** |
584 | | * A JavaScript symbol (ECMA-262 edition 6) |
585 | | */ |
586 | | class V8_EXPORT Symbol : public Name { |
587 | | public: |
588 | | /** |
589 | | * Returns the description string of the symbol, or undefined if none. |
590 | | */ |
591 | | Local<Value> Description(Isolate* isolate) const; |
592 | | |
593 | | /** |
594 | | * Create a symbol. If description is not empty, it will be used as the |
595 | | * description. |
596 | | */ |
597 | | static Local<Symbol> New(Isolate* isolate, |
598 | | Local<String> description = Local<String>()); |
599 | | |
600 | | /** |
601 | | * Access global symbol registry. |
602 | | * Note that symbols created this way are never collected, so |
603 | | * they should only be used for statically fixed properties. |
604 | | * Also, there is only one global name space for the descriptions used as |
605 | | * keys. |
606 | | * To minimize the potential for clashes, use qualified names as keys. |
607 | | */ |
608 | | static Local<Symbol> For(Isolate* isolate, Local<String> description); |
609 | | |
610 | | /** |
611 | | * Retrieve a global symbol. Similar to |For|, but using a separate |
612 | | * registry that is not accessible by (and cannot clash with) JavaScript code. |
613 | | */ |
614 | | static Local<Symbol> ForApi(Isolate* isolate, Local<String> description); |
615 | | |
616 | | // Well-known symbols |
617 | | static Local<Symbol> GetAsyncIterator(Isolate* isolate); |
618 | | static Local<Symbol> GetHasInstance(Isolate* isolate); |
619 | | static Local<Symbol> GetIsConcatSpreadable(Isolate* isolate); |
620 | | static Local<Symbol> GetIterator(Isolate* isolate); |
621 | | static Local<Symbol> GetMatch(Isolate* isolate); |
622 | | static Local<Symbol> GetReplace(Isolate* isolate); |
623 | | static Local<Symbol> GetSearch(Isolate* isolate); |
624 | | static Local<Symbol> GetSplit(Isolate* isolate); |
625 | | static Local<Symbol> GetToPrimitive(Isolate* isolate); |
626 | | static Local<Symbol> GetToStringTag(Isolate* isolate); |
627 | | static Local<Symbol> GetUnscopables(Isolate* isolate); |
628 | | |
629 | 0 | V8_INLINE static Symbol* Cast(Data* data) { |
630 | | #ifdef V8_ENABLE_CHECKS |
631 | | CheckCast(data); |
632 | | #endif |
633 | 0 | return static_cast<Symbol*>(data); |
634 | 0 | } |
635 | | |
636 | | private: |
637 | | Symbol(); |
638 | | static void CheckCast(Data* that); |
639 | | }; |
640 | | |
641 | | /** |
642 | | * A JavaScript numeric value (either Number or BigInt). |
643 | | * https://tc39.es/ecma262/#sec-numeric-types |
644 | | */ |
645 | | class V8_EXPORT Numeric : public Primitive { |
646 | | private: |
647 | | Numeric(); |
648 | | static void CheckCast(v8::Data* that); |
649 | | }; |
650 | | |
651 | | /** |
652 | | * A JavaScript number value (ECMA-262, 4.3.20) |
653 | | */ |
654 | | class V8_EXPORT Number : public Numeric { |
655 | | public: |
656 | | double Value() const; |
657 | | static Local<Number> New(Isolate* isolate, double value); |
658 | 0 | V8_INLINE static Number* Cast(v8::Data* data) { |
659 | 0 | #ifdef V8_ENABLE_CHECKS |
660 | 0 | CheckCast(data); |
661 | 0 | #endif |
662 | 0 | return static_cast<Number*>(data); |
663 | 0 | } |
664 | | |
665 | | private: |
666 | | Number(); |
667 | | static void CheckCast(v8::Data* that); |
668 | | }; |
669 | | |
670 | | /** |
671 | | * A JavaScript value representing a signed integer. |
672 | | */ |
673 | | class V8_EXPORT Integer : public Number { |
674 | | public: |
675 | | static Local<Integer> New(Isolate* isolate, int32_t value); |
676 | | static Local<Integer> NewFromUnsigned(Isolate* isolate, uint32_t value); |
677 | | int64_t Value() const; |
678 | 0 | V8_INLINE static Integer* Cast(v8::Data* data) { |
679 | 0 | #ifdef V8_ENABLE_CHECKS |
680 | 0 | CheckCast(data); |
681 | 0 | #endif |
682 | 0 | return static_cast<Integer*>(data); |
683 | 0 | } |
684 | | |
685 | | private: |
686 | | Integer(); |
687 | | static void CheckCast(v8::Data* that); |
688 | | }; |
689 | | |
690 | | /** |
691 | | * A JavaScript value representing a 32-bit signed integer. |
692 | | */ |
693 | | class V8_EXPORT Int32 : public Integer { |
694 | | public: |
695 | | int32_t Value() const; |
696 | 0 | V8_INLINE static Int32* Cast(v8::Data* data) { |
697 | 0 | #ifdef V8_ENABLE_CHECKS |
698 | 0 | CheckCast(data); |
699 | 0 | #endif |
700 | 0 | return static_cast<Int32*>(data); |
701 | 0 | } |
702 | | |
703 | | private: |
704 | | Int32(); |
705 | | static void CheckCast(v8::Data* that); |
706 | | }; |
707 | | |
708 | | /** |
709 | | * A JavaScript value representing a 32-bit unsigned integer. |
710 | | */ |
711 | | class V8_EXPORT Uint32 : public Integer { |
712 | | public: |
713 | | uint32_t Value() const; |
714 | 0 | V8_INLINE static Uint32* Cast(v8::Data* data) { |
715 | 0 | #ifdef V8_ENABLE_CHECKS |
716 | 0 | CheckCast(data); |
717 | 0 | #endif |
718 | 0 | return static_cast<Uint32*>(data); |
719 | 0 | } |
720 | | |
721 | | private: |
722 | | Uint32(); |
723 | | static void CheckCast(v8::Data* that); |
724 | | }; |
725 | | |
726 | | /** |
727 | | * A JavaScript BigInt value (https://tc39.github.io/proposal-bigint) |
728 | | */ |
729 | | class V8_EXPORT BigInt : public Numeric { |
730 | | public: |
731 | | static Local<BigInt> New(Isolate* isolate, int64_t value); |
732 | | static Local<BigInt> NewFromUnsigned(Isolate* isolate, uint64_t value); |
733 | | /** |
734 | | * Creates a new BigInt object using a specified sign bit and a |
735 | | * specified list of digits/words. |
736 | | * The resulting number is calculated as: |
737 | | * |
738 | | * (-1)^sign_bit * (words[0] * (2^64)^0 + words[1] * (2^64)^1 + ...) |
739 | | */ |
740 | | static MaybeLocal<BigInt> NewFromWords(Local<Context> context, int sign_bit, |
741 | | int word_count, const uint64_t* words); |
742 | | |
743 | | /** |
744 | | * Returns the value of this BigInt as an unsigned 64-bit integer. |
745 | | * If `lossless` is provided, it will reflect whether the return value was |
746 | | * truncated or wrapped around. In particular, it is set to `false` if this |
747 | | * BigInt is negative. |
748 | | */ |
749 | | uint64_t Uint64Value(bool* lossless = nullptr) const; |
750 | | |
751 | | /** |
752 | | * Returns the value of this BigInt as a signed 64-bit integer. |
753 | | * If `lossless` is provided, it will reflect whether this BigInt was |
754 | | * truncated or not. |
755 | | */ |
756 | | int64_t Int64Value(bool* lossless = nullptr) const; |
757 | | |
758 | | /** |
759 | | * Returns the number of 64-bit words needed to store the result of |
760 | | * ToWordsArray(). |
761 | | */ |
762 | | int WordCount() const; |
763 | | |
764 | | /** |
765 | | * Writes the contents of this BigInt to a specified memory location. |
766 | | * `sign_bit` must be provided and will be set to 1 if this BigInt is |
767 | | * negative. |
768 | | * `*word_count` has to be initialized to the length of the `words` array. |
769 | | * Upon return, it will be set to the actual number of words that would |
770 | | * be needed to store this BigInt (i.e. the return value of `WordCount()`). |
771 | | */ |
772 | | void ToWordsArray(int* sign_bit, int* word_count, uint64_t* words) const; |
773 | | |
774 | 0 | V8_INLINE static BigInt* Cast(v8::Data* data) { |
775 | 0 | #ifdef V8_ENABLE_CHECKS |
776 | 0 | CheckCast(data); |
777 | 0 | #endif |
778 | 0 | return static_cast<BigInt*>(data); |
779 | 0 | } |
780 | | |
781 | | private: |
782 | | BigInt(); |
783 | | static void CheckCast(v8::Data* that); |
784 | | }; |
785 | | |
786 | 72.3k | Local<String> String::Empty(Isolate* isolate) { |
787 | 72.3k | using S = internal::Address; |
788 | 72.3k | using I = internal::Internals; |
789 | 72.3k | I::CheckInitialized(isolate); |
790 | 72.3k | S* slot = I::GetRootSlot(isolate, I::kEmptyStringRootIndex); |
791 | 72.3k | return Local<String>::FromSlot(slot); |
792 | 72.3k | } |
793 | | |
794 | 0 | String::ExternalStringResource* String::GetExternalStringResource() const { |
795 | 0 | using A = internal::Address; |
796 | 0 | using I = internal::Internals; |
797 | 0 | A obj = internal::ValueHelper::ValueAsAddress(this); |
798 | |
|
799 | 0 | ExternalStringResource* result; |
800 | 0 | if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) { |
801 | 0 | Isolate* isolate = I::GetIsolateForSandbox(obj); |
802 | 0 | A value = I::ReadExternalPointerField<internal::kExternalStringResourceTag>( |
803 | 0 | isolate, obj, I::kStringResourceOffset); |
804 | 0 | result = reinterpret_cast<String::ExternalStringResource*>(value); |
805 | 0 | } else { |
806 | 0 | result = GetExternalStringResourceSlow(); |
807 | 0 | } |
808 | | #ifdef V8_ENABLE_CHECKS |
809 | | VerifyExternalStringResource(result); |
810 | | #endif |
811 | 0 | return result; |
812 | 0 | } |
813 | | |
814 | | String::ExternalStringResourceBase* String::GetExternalStringResourceBase( |
815 | 0 | String::Encoding* encoding_out) const { |
816 | 0 | using A = internal::Address; |
817 | 0 | using I = internal::Internals; |
818 | 0 | A obj = internal::ValueHelper::ValueAsAddress(this); |
819 | 0 | int type = I::GetInstanceType(obj) & I::kStringRepresentationAndEncodingMask; |
820 | 0 | *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask); |
821 | 0 | ExternalStringResourceBase* resource; |
822 | 0 | if (type == I::kExternalOneByteRepresentationTag || |
823 | 0 | type == I::kExternalTwoByteRepresentationTag) { |
824 | 0 | Isolate* isolate = I::GetIsolateForSandbox(obj); |
825 | 0 | A value = I::ReadExternalPointerField<internal::kExternalStringResourceTag>( |
826 | 0 | isolate, obj, I::kStringResourceOffset); |
827 | 0 | resource = reinterpret_cast<ExternalStringResourceBase*>(value); |
828 | 0 | } else { |
829 | 0 | resource = GetExternalStringResourceBaseSlow(encoding_out); |
830 | 0 | } |
831 | 0 | #ifdef V8_ENABLE_CHECKS |
832 | 0 | VerifyExternalStringResourceBase(resource, *encoding_out); |
833 | 0 | #endif |
834 | 0 | return resource; |
835 | 0 | } |
836 | | |
837 | | // --- Statics --- |
838 | | |
839 | 9.23M | V8_INLINE Local<Primitive> Undefined(Isolate* isolate) { |
840 | 9.23M | using S = internal::Address; |
841 | 9.23M | using I = internal::Internals; |
842 | 9.23M | I::CheckInitialized(isolate); |
843 | 9.23M | S* slot = I::GetRootSlot(isolate, I::kUndefinedValueRootIndex); |
844 | 9.23M | return Local<Primitive>::FromSlot(slot); |
845 | 9.23M | } |
846 | | |
847 | 2.05M | V8_INLINE Local<Primitive> Null(Isolate* isolate) { |
848 | 2.05M | using S = internal::Address; |
849 | 2.05M | using I = internal::Internals; |
850 | 2.05M | I::CheckInitialized(isolate); |
851 | 2.05M | S* slot = I::GetRootSlot(isolate, I::kNullValueRootIndex); |
852 | 2.05M | return Local<Primitive>::FromSlot(slot); |
853 | 2.05M | } |
854 | | |
855 | 5.47M | V8_INLINE Local<Boolean> True(Isolate* isolate) { |
856 | 5.47M | using S = internal::Address; |
857 | 5.47M | using I = internal::Internals; |
858 | 5.47M | I::CheckInitialized(isolate); |
859 | 5.47M | S* slot = I::GetRootSlot(isolate, I::kTrueValueRootIndex); |
860 | 5.47M | return Local<Boolean>::FromSlot(slot); |
861 | 5.47M | } |
862 | | |
863 | 30.2M | V8_INLINE Local<Boolean> False(Isolate* isolate) { |
864 | 30.2M | using S = internal::Address; |
865 | 30.2M | using I = internal::Internals; |
866 | 30.2M | I::CheckInitialized(isolate); |
867 | 30.2M | S* slot = I::GetRootSlot(isolate, I::kFalseValueRootIndex); |
868 | 30.2M | return Local<Boolean>::FromSlot(slot); |
869 | 30.2M | } |
870 | | |
871 | 33.6M | Local<Boolean> Boolean::New(Isolate* isolate, bool value) { |
872 | 33.6M | return value ? True(isolate) : False(isolate); |
873 | 33.6M | } |
874 | | |
875 | | } // namespace v8 |
876 | | |
877 | | #endif // INCLUDE_V8_PRIMITIVE_H_ |