v8-value.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. #ifndef INCLUDE_V8_VALUE_H_
  5. #define INCLUDE_V8_VALUE_H_
  6. #include "v8-data.h" // NOLINT(build/include_directory)
  7. #include "v8-internal.h" // NOLINT(build/include_directory)
  8. #include "v8-local-handle.h" // NOLINT(build/include_directory)
  9. #include "v8-maybe.h" // NOLINT(build/include_directory)
  10. #include "v8config.h" // NOLINT(build/include_directory)
  11. /**
  12. * The v8 JavaScript engine.
  13. */
  14. namespace v8 {
  15. class BigInt;
  16. class Int32;
  17. class Integer;
  18. class Number;
  19. class Object;
  20. class String;
  21. class Uint32;
  22. /**
  23. * The superclass of all JavaScript values and objects.
  24. */
  25. class V8_EXPORT Value : public Data {
  26. public:
  27. /**
  28. * Returns true if this value is the undefined value. See ECMA-262
  29. * 4.3.10.
  30. *
  31. * This is equivalent to `value === undefined` in JS.
  32. */
  33. V8_INLINE bool IsUndefined() const;
  34. /**
  35. * Returns true if this value is the null value. See ECMA-262
  36. * 4.3.11.
  37. *
  38. * This is equivalent to `value === null` in JS.
  39. */
  40. V8_INLINE bool IsNull() const;
  41. /**
  42. * Returns true if this value is either the null or the undefined value.
  43. * See ECMA-262
  44. * 4.3.11. and 4.3.12
  45. *
  46. * This is equivalent to `value == null` in JS.
  47. */
  48. V8_INLINE bool IsNullOrUndefined() const;
  49. /**
  50. * Returns true if this value is true.
  51. *
  52. * This is not the same as `BooleanValue()`. The latter performs a
  53. * conversion to boolean, i.e. the result of `Boolean(value)` in JS, whereas
  54. * this checks `value === true`.
  55. */
  56. bool IsTrue() const;
  57. /**
  58. * Returns true if this value is false.
  59. *
  60. * This is not the same as `!BooleanValue()`. The latter performs a
  61. * conversion to boolean, i.e. the result of `!Boolean(value)` in JS, whereas
  62. * this checks `value === false`.
  63. */
  64. bool IsFalse() const;
  65. /**
  66. * Returns true if this value is a symbol or a string.
  67. *
  68. * This is equivalent to
  69. * `typeof value === 'string' || typeof value === 'symbol'` in JS.
  70. */
  71. bool IsName() const;
  72. /**
  73. * Returns true if this value is an instance of the String type.
  74. * See ECMA-262 8.4.
  75. *
  76. * This is equivalent to `typeof value === 'string'` in JS.
  77. */
  78. V8_INLINE bool IsString() const;
  79. /**
  80. * Returns true if this value is a symbol.
  81. *
  82. * This is equivalent to `typeof value === 'symbol'` in JS.
  83. */
  84. bool IsSymbol() const;
  85. /**
  86. * Returns true if this value is a function.
  87. *
  88. * This is equivalent to `typeof value === 'function'` in JS.
  89. */
  90. bool IsFunction() const;
  91. /**
  92. * Returns true if this value is an array. Note that it will return false for
  93. * an Proxy for an array.
  94. */
  95. bool IsArray() const;
  96. /**
  97. * Returns true if this value is an object.
  98. */
  99. bool IsObject() const;
  100. /**
  101. * Returns true if this value is a bigint.
  102. *
  103. * This is equivalent to `typeof value === 'bigint'` in JS.
  104. */
  105. bool IsBigInt() const;
  106. /**
  107. * Returns true if this value is boolean.
  108. *
  109. * This is equivalent to `typeof value === 'boolean'` in JS.
  110. */
  111. bool IsBoolean() const;
  112. /**
  113. * Returns true if this value is a number.
  114. *
  115. * This is equivalent to `typeof value === 'number'` in JS.
  116. */
  117. bool IsNumber() const;
  118. /**
  119. * Returns true if this value is an `External` object.
  120. */
  121. bool IsExternal() const;
  122. /**
  123. * Returns true if this value is a 32-bit signed integer.
  124. */
  125. bool IsInt32() const;
  126. /**
  127. * Returns true if this value is a 32-bit unsigned integer.
  128. */
  129. bool IsUint32() const;
  130. /**
  131. * Returns true if this value is a Date.
  132. */
  133. bool IsDate() const;
  134. /**
  135. * Returns true if this value is an Arguments object.
  136. */
  137. bool IsArgumentsObject() const;
  138. /**
  139. * Returns true if this value is a BigInt object.
  140. */
  141. bool IsBigIntObject() const;
  142. /**
  143. * Returns true if this value is a Boolean object.
  144. */
  145. bool IsBooleanObject() const;
  146. /**
  147. * Returns true if this value is a Number object.
  148. */
  149. bool IsNumberObject() const;
  150. /**
  151. * Returns true if this value is a String object.
  152. */
  153. bool IsStringObject() const;
  154. /**
  155. * Returns true if this value is a Symbol object.
  156. */
  157. bool IsSymbolObject() const;
  158. /**
  159. * Returns true if this value is a NativeError.
  160. */
  161. bool IsNativeError() const;
  162. /**
  163. * Returns true if this value is a RegExp.
  164. */
  165. bool IsRegExp() const;
  166. /**
  167. * Returns true if this value is an async function.
  168. */
  169. bool IsAsyncFunction() const;
  170. /**
  171. * Returns true if this value is a Generator function.
  172. */
  173. bool IsGeneratorFunction() const;
  174. /**
  175. * Returns true if this value is a Generator object (iterator).
  176. */
  177. bool IsGeneratorObject() const;
  178. /**
  179. * Returns true if this value is a Promise.
  180. */
  181. bool IsPromise() const;
  182. /**
  183. * Returns true if this value is a Map.
  184. */
  185. bool IsMap() const;
  186. /**
  187. * Returns true if this value is a Set.
  188. */
  189. bool IsSet() const;
  190. /**
  191. * Returns true if this value is a Map Iterator.
  192. */
  193. bool IsMapIterator() const;
  194. /**
  195. * Returns true if this value is a Set Iterator.
  196. */
  197. bool IsSetIterator() const;
  198. /**
  199. * Returns true if this value is a WeakMap.
  200. */
  201. bool IsWeakMap() const;
  202. /**
  203. * Returns true if this value is a WeakSet.
  204. */
  205. bool IsWeakSet() const;
  206. /**
  207. * Returns true if this value is an ArrayBuffer.
  208. */
  209. bool IsArrayBuffer() const;
  210. /**
  211. * Returns true if this value is an ArrayBufferView.
  212. */
  213. bool IsArrayBufferView() const;
  214. /**
  215. * Returns true if this value is one of TypedArrays.
  216. */
  217. bool IsTypedArray() const;
  218. /**
  219. * Returns true if this value is an Uint8Array.
  220. */
  221. bool IsUint8Array() const;
  222. /**
  223. * Returns true if this value is an Uint8ClampedArray.
  224. */
  225. bool IsUint8ClampedArray() const;
  226. /**
  227. * Returns true if this value is an Int8Array.
  228. */
  229. bool IsInt8Array() const;
  230. /**
  231. * Returns true if this value is an Uint16Array.
  232. */
  233. bool IsUint16Array() const;
  234. /**
  235. * Returns true if this value is an Int16Array.
  236. */
  237. bool IsInt16Array() const;
  238. /**
  239. * Returns true if this value is an Uint32Array.
  240. */
  241. bool IsUint32Array() const;
  242. /**
  243. * Returns true if this value is an Int32Array.
  244. */
  245. bool IsInt32Array() const;
  246. /**
  247. * Returns true if this value is a Float32Array.
  248. */
  249. bool IsFloat32Array() const;
  250. /**
  251. * Returns true if this value is a Float64Array.
  252. */
  253. bool IsFloat64Array() const;
  254. /**
  255. * Returns true if this value is a BigInt64Array.
  256. */
  257. bool IsBigInt64Array() const;
  258. /**
  259. * Returns true if this value is a BigUint64Array.
  260. */
  261. bool IsBigUint64Array() const;
  262. /**
  263. * Returns true if this value is a DataView.
  264. */
  265. bool IsDataView() const;
  266. /**
  267. * Returns true if this value is a SharedArrayBuffer.
  268. */
  269. bool IsSharedArrayBuffer() const;
  270. /**
  271. * Returns true if this value is a JavaScript Proxy.
  272. */
  273. bool IsProxy() const;
  274. /**
  275. * Returns true if this value is a WasmMemoryObject.
  276. */
  277. bool IsWasmMemoryObject() const;
  278. /**
  279. * Returns true if this value is a WasmModuleObject.
  280. */
  281. bool IsWasmModuleObject() const;
  282. /**
  283. * Returns true if the value is a Module Namespace Object.
  284. */
  285. bool IsModuleNamespaceObject() const;
  286. /**
  287. * Perform the equivalent of `BigInt(value)` in JS.
  288. */
  289. V8_WARN_UNUSED_RESULT MaybeLocal<BigInt> ToBigInt(
  290. Local<Context> context) const;
  291. /**
  292. * Perform the equivalent of `Number(value)` in JS.
  293. */
  294. V8_WARN_UNUSED_RESULT MaybeLocal<Number> ToNumber(
  295. Local<Context> context) const;
  296. /**
  297. * Perform the equivalent of `String(value)` in JS.
  298. */
  299. V8_WARN_UNUSED_RESULT MaybeLocal<String> ToString(
  300. Local<Context> context) const;
  301. /**
  302. * Provide a string representation of this value usable for debugging.
  303. * This operation has no observable side effects and will succeed
  304. * unless e.g. execution is being terminated.
  305. */
  306. V8_WARN_UNUSED_RESULT MaybeLocal<String> ToDetailString(
  307. Local<Context> context) const;
  308. /**
  309. * Perform the equivalent of `Object(value)` in JS.
  310. */
  311. V8_WARN_UNUSED_RESULT MaybeLocal<Object> ToObject(
  312. Local<Context> context) const;
  313. /**
  314. * Perform the equivalent of `Number(value)` in JS and convert the result
  315. * to an integer. Negative values are rounded up, positive values are rounded
  316. * down. NaN is converted to 0. Infinite values yield undefined results.
  317. */
  318. V8_WARN_UNUSED_RESULT MaybeLocal<Integer> ToInteger(
  319. Local<Context> context) const;
  320. /**
  321. * Perform the equivalent of `Number(value)` in JS and convert the result
  322. * to an unsigned 32-bit integer by performing the steps in
  323. * https://tc39.es/ecma262/#sec-touint32.
  324. */
  325. V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToUint32(
  326. Local<Context> context) const;
  327. /**
  328. * Perform the equivalent of `Number(value)` in JS and convert the result
  329. * to a signed 32-bit integer by performing the steps in
  330. * https://tc39.es/ecma262/#sec-toint32.
  331. */
  332. V8_WARN_UNUSED_RESULT MaybeLocal<Int32> ToInt32(Local<Context> context) const;
  333. /**
  334. * Perform the equivalent of `Boolean(value)` in JS. This can never fail.
  335. */
  336. Local<Boolean> ToBoolean(Isolate* isolate) const;
  337. /**
  338. * Attempts to convert a string to an array index.
  339. * Returns an empty handle if the conversion fails.
  340. */
  341. V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToArrayIndex(
  342. Local<Context> context) const;
  343. /** Returns the equivalent of `ToBoolean()->Value()`. */
  344. bool BooleanValue(Isolate* isolate) const;
  345. /** Returns the equivalent of `ToNumber()->Value()`. */
  346. V8_WARN_UNUSED_RESULT Maybe<double> NumberValue(Local<Context> context) const;
  347. /** Returns the equivalent of `ToInteger()->Value()`. */
  348. V8_WARN_UNUSED_RESULT Maybe<int64_t> IntegerValue(
  349. Local<Context> context) const;
  350. /** Returns the equivalent of `ToUint32()->Value()`. */
  351. V8_WARN_UNUSED_RESULT Maybe<uint32_t> Uint32Value(
  352. Local<Context> context) const;
  353. /** Returns the equivalent of `ToInt32()->Value()`. */
  354. V8_WARN_UNUSED_RESULT Maybe<int32_t> Int32Value(Local<Context> context) const;
  355. /** JS == */
  356. V8_WARN_UNUSED_RESULT Maybe<bool> Equals(Local<Context> context,
  357. Local<Value> that) const;
  358. bool StrictEquals(Local<Value> that) const;
  359. bool SameValue(Local<Value> that) const;
  360. template <class T>
  361. V8_INLINE static Value* Cast(T* value) {
  362. return static_cast<Value*>(value);
  363. }
  364. Local<String> TypeOf(Isolate*);
  365. Maybe<bool> InstanceOf(Local<Context> context, Local<Object> object);
  366. private:
  367. V8_INLINE bool QuickIsUndefined() const;
  368. V8_INLINE bool QuickIsNull() const;
  369. V8_INLINE bool QuickIsNullOrUndefined() const;
  370. V8_INLINE bool QuickIsString() const;
  371. bool FullIsUndefined() const;
  372. bool FullIsNull() const;
  373. bool FullIsString() const;
  374. static void CheckCast(Data* that);
  375. };
  376. template <>
  377. V8_INLINE Value* Value::Cast(Data* value) {
  378. #ifdef V8_ENABLE_CHECKS
  379. CheckCast(value);
  380. #endif
  381. return static_cast<Value*>(value);
  382. }
  383. bool Value::IsUndefined() const {
  384. #ifdef V8_ENABLE_CHECKS
  385. return FullIsUndefined();
  386. #else
  387. return QuickIsUndefined();
  388. #endif
  389. }
  390. bool Value::QuickIsUndefined() const {
  391. using A = internal::Address;
  392. using I = internal::Internals;
  393. A obj = *reinterpret_cast<const A*>(this);
  394. if (!I::HasHeapObjectTag(obj)) return false;
  395. if (I::GetInstanceType(obj) != I::kOddballType) return false;
  396. return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
  397. }
  398. bool Value::IsNull() const {
  399. #ifdef V8_ENABLE_CHECKS
  400. return FullIsNull();
  401. #else
  402. return QuickIsNull();
  403. #endif
  404. }
  405. bool Value::QuickIsNull() const {
  406. using A = internal::Address;
  407. using I = internal::Internals;
  408. A obj = *reinterpret_cast<const A*>(this);
  409. if (!I::HasHeapObjectTag(obj)) return false;
  410. if (I::GetInstanceType(obj) != I::kOddballType) return false;
  411. return (I::GetOddballKind(obj) == I::kNullOddballKind);
  412. }
  413. bool Value::IsNullOrUndefined() const {
  414. #ifdef V8_ENABLE_CHECKS
  415. return FullIsNull() || FullIsUndefined();
  416. #else
  417. return QuickIsNullOrUndefined();
  418. #endif
  419. }
  420. bool Value::QuickIsNullOrUndefined() const {
  421. using A = internal::Address;
  422. using I = internal::Internals;
  423. A obj = *reinterpret_cast<const A*>(this);
  424. if (!I::HasHeapObjectTag(obj)) return false;
  425. if (I::GetInstanceType(obj) != I::kOddballType) return false;
  426. int kind = I::GetOddballKind(obj);
  427. return kind == I::kNullOddballKind || kind == I::kUndefinedOddballKind;
  428. }
  429. bool Value::IsString() const {
  430. #ifdef V8_ENABLE_CHECKS
  431. return FullIsString();
  432. #else
  433. return QuickIsString();
  434. #endif
  435. }
  436. bool Value::QuickIsString() const {
  437. using A = internal::Address;
  438. using I = internal::Internals;
  439. A obj = *reinterpret_cast<const A*>(this);
  440. if (!I::HasHeapObjectTag(obj)) return false;
  441. return (I::GetInstanceType(obj) < I::kFirstNonstringType);
  442. }
  443. } // namespace v8
  444. #endif // INCLUDE_V8_VALUE_H_