JSValueRef.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (C) 2006 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef JSValueRef_h
  26. #define JSValueRef_h
  27. #include <JavaScriptCore/JSBase.h>
  28. #include <JavaScriptCore/WebKitAvailability.h>
  29. #ifndef __cplusplus
  30. #include <stdbool.h>
  31. #endif
  32. /*!
  33. @enum JSType
  34. @abstract A constant identifying the type of a JSValue.
  35. @constant kJSTypeUndefined The unique undefined value.
  36. @constant kJSTypeNull The unique null value.
  37. @constant kJSTypeBoolean A primitive boolean value, one of true or false.
  38. @constant kJSTypeNumber A primitive number value.
  39. @constant kJSTypeString A primitive string value.
  40. @constant kJSTypeObject An object value (meaning that this JSValueRef is a JSObjectRef).
  41. @constant kJSTypeSymbol A primitive symbol value.
  42. */
  43. typedef enum {
  44. kJSTypeUndefined,
  45. kJSTypeNull,
  46. kJSTypeBoolean,
  47. kJSTypeNumber,
  48. kJSTypeString,
  49. kJSTypeObject,
  50. kJSTypeSymbol JSC_API_AVAILABLE(macosx(JSC_MAC_TBA), ios(JSC_IOS_TBA))
  51. } JSType;
  52. /*!
  53. @enum JSTypedArrayType
  54. @abstract A constant identifying the Typed Array type of a JSObjectRef.
  55. @constant kJSTypedArrayTypeInt8Array Int8Array
  56. @constant kJSTypedArrayTypeInt16Array Int16Array
  57. @constant kJSTypedArrayTypeInt32Array Int32Array
  58. @constant kJSTypedArrayTypeUint8Array Uint8Array
  59. @constant kJSTypedArrayTypeUint8ClampedArray Uint8ClampedArray
  60. @constant kJSTypedArrayTypeUint16Array Uint16Array
  61. @constant kJSTypedArrayTypeUint32Array Uint32Array
  62. @constant kJSTypedArrayTypeFloat32Array Float32Array
  63. @constant kJSTypedArrayTypeFloat64Array Float64Array
  64. @constant kJSTypedArrayTypeArrayBuffer ArrayBuffer
  65. @constant kJSTypedArrayTypeNone Not a Typed Array
  66. */
  67. typedef enum {
  68. kJSTypedArrayTypeInt8Array,
  69. kJSTypedArrayTypeInt16Array,
  70. kJSTypedArrayTypeInt32Array,
  71. kJSTypedArrayTypeUint8Array,
  72. kJSTypedArrayTypeUint8ClampedArray,
  73. kJSTypedArrayTypeUint16Array,
  74. kJSTypedArrayTypeUint32Array,
  75. kJSTypedArrayTypeFloat32Array,
  76. kJSTypedArrayTypeFloat64Array,
  77. kJSTypedArrayTypeArrayBuffer,
  78. kJSTypedArrayTypeNone,
  79. } JSTypedArrayType JSC_API_AVAILABLE(macosx(10.12), ios(10.0));
  80. #ifdef __cplusplus
  81. extern "C" {
  82. #endif
  83. /*!
  84. @function
  85. @abstract Returns a JavaScript value's type.
  86. @param ctx The execution context to use.
  87. @param value The JSValue whose type you want to obtain.
  88. @result A value of type JSType that identifies value's type.
  89. */
  90. JS_EXPORT JSType JSValueGetType(JSContextRef ctx, JSValueRef value);
  91. /*!
  92. @function
  93. @abstract Tests whether a JavaScript value's type is the undefined type.
  94. @param ctx The execution context to use.
  95. @param value The JSValue to test.
  96. @result true if value's type is the undefined type, otherwise false.
  97. */
  98. JS_EXPORT bool JSValueIsUndefined(JSContextRef ctx, JSValueRef value);
  99. /*!
  100. @function
  101. @abstract Tests whether a JavaScript value's type is the null type.
  102. @param ctx The execution context to use.
  103. @param value The JSValue to test.
  104. @result true if value's type is the null type, otherwise false.
  105. */
  106. JS_EXPORT bool JSValueIsNull(JSContextRef ctx, JSValueRef value);
  107. /*!
  108. @function
  109. @abstract Tests whether a JavaScript value's type is the boolean type.
  110. @param ctx The execution context to use.
  111. @param value The JSValue to test.
  112. @result true if value's type is the boolean type, otherwise false.
  113. */
  114. JS_EXPORT bool JSValueIsBoolean(JSContextRef ctx, JSValueRef value);
  115. /*!
  116. @function
  117. @abstract Tests whether a JavaScript value's type is the number type.
  118. @param ctx The execution context to use.
  119. @param value The JSValue to test.
  120. @result true if value's type is the number type, otherwise false.
  121. */
  122. JS_EXPORT bool JSValueIsNumber(JSContextRef ctx, JSValueRef value);
  123. /*!
  124. @function
  125. @abstract Tests whether a JavaScript value's type is the string type.
  126. @param ctx The execution context to use.
  127. @param value The JSValue to test.
  128. @result true if value's type is the string type, otherwise false.
  129. */
  130. JS_EXPORT bool JSValueIsString(JSContextRef ctx, JSValueRef value);
  131. /*!
  132. @function
  133. @abstract Tests whether a JavaScript value's type is the object type.
  134. @param ctx The execution context to use.
  135. @param value The JSValue to test.
  136. @result true if value's type is the object type, otherwise false.
  137. */
  138. JS_EXPORT bool JSValueIsObject(JSContextRef ctx, JSValueRef value);
  139. /*!
  140. @function
  141. @abstract Tests whether a JavaScript value is an object with a given class in its class chain.
  142. @param ctx The execution context to use.
  143. @param value The JSValue to test.
  144. @param jsClass The JSClass to test against.
  145. @result true if value is an object and has jsClass in its class chain, otherwise false.
  146. */
  147. JS_EXPORT bool JSValueIsObjectOfClass(JSContextRef ctx, JSValueRef value, JSClassRef jsClass);
  148. /*!
  149. @function
  150. @abstract Tests whether a JavaScript value is an array.
  151. @param ctx The execution context to use.
  152. @param value The JSValue to test.
  153. @result true if value is an array, otherwise false.
  154. */
  155. JS_EXPORT bool JSValueIsArray(JSContextRef ctx, JSValueRef value) JSC_API_AVAILABLE(macosx(10.11), ios(9.0));
  156. /*!
  157. @function
  158. @abstract Tests whether a JavaScript value is a date.
  159. @param ctx The execution context to use.
  160. @param value The JSValue to test.
  161. @result true if value is a date, otherwise false.
  162. */
  163. JS_EXPORT bool JSValueIsDate(JSContextRef ctx, JSValueRef value) JSC_API_AVAILABLE(macosx(10.11), ios(9.0));
  164. /*!
  165. @function
  166. @abstract Returns a JavaScript value's Typed Array type.
  167. @param ctx The execution context to use.
  168. @param value The JSValue whose Typed Array type to return.
  169. @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
  170. @result A value of type JSTypedArrayType that identifies value's Typed Array type, or kJSTypedArrayTypeNone if the value is not a Typed Array object.
  171. */
  172. JS_EXPORT JSTypedArrayType JSValueGetTypedArrayType(JSContextRef ctx, JSValueRef value, JSValueRef* exception) JSC_API_AVAILABLE(macosx(10.12), ios(10.0));
  173. /* Comparing values */
  174. /*!
  175. @function
  176. @abstract Tests whether two JavaScript values are equal, as compared by the JS == operator.
  177. @param ctx The execution context to use.
  178. @param a The first value to test.
  179. @param b The second value to test.
  180. @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
  181. @result true if the two values are equal, false if they are not equal or an exception is thrown.
  182. */
  183. JS_EXPORT bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception);
  184. /*!
  185. @function
  186. @abstract Tests whether two JavaScript values are strict equal, as compared by the JS === operator.
  187. @param ctx The execution context to use.
  188. @param a The first value to test.
  189. @param b The second value to test.
  190. @result true if the two values are strict equal, otherwise false.
  191. */
  192. JS_EXPORT bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b);
  193. /*!
  194. @function
  195. @abstract Tests whether a JavaScript value is an object constructed by a given constructor, as compared by the JS instanceof operator.
  196. @param ctx The execution context to use.
  197. @param value The JSValue to test.
  198. @param constructor The constructor to test against.
  199. @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
  200. @result true if value is an object constructed by constructor, as compared by the JS instanceof operator, otherwise false.
  201. */
  202. JS_EXPORT bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception);
  203. /* Creating values */
  204. /*!
  205. @function
  206. @abstract Creates a JavaScript value of the undefined type.
  207. @param ctx The execution context to use.
  208. @result The unique undefined value.
  209. */
  210. JS_EXPORT JSValueRef JSValueMakeUndefined(JSContextRef ctx);
  211. /*!
  212. @function
  213. @abstract Creates a JavaScript value of the null type.
  214. @param ctx The execution context to use.
  215. @result The unique null value.
  216. */
  217. JS_EXPORT JSValueRef JSValueMakeNull(JSContextRef ctx);
  218. /*!
  219. @function
  220. @abstract Creates a JavaScript value of the boolean type.
  221. @param ctx The execution context to use.
  222. @param boolean The bool to assign to the newly created JSValue.
  223. @result A JSValue of the boolean type, representing the value of boolean.
  224. */
  225. JS_EXPORT JSValueRef JSValueMakeBoolean(JSContextRef ctx, bool boolean);
  226. /*!
  227. @function
  228. @abstract Creates a JavaScript value of the number type.
  229. @param ctx The execution context to use.
  230. @param number The double to assign to the newly created JSValue.
  231. @result A JSValue of the number type, representing the value of number.
  232. */
  233. JS_EXPORT JSValueRef JSValueMakeNumber(JSContextRef ctx, double number);
  234. /*!
  235. @function
  236. @abstract Creates a JavaScript value of the string type.
  237. @param ctx The execution context to use.
  238. @param string The JSString to assign to the newly created JSValue. The
  239. newly created JSValue retains string, and releases it upon garbage collection.
  240. @result A JSValue of the string type, representing the value of string.
  241. */
  242. JS_EXPORT JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string);
  243. /* Converting to and from JSON formatted strings */
  244. /*!
  245. @function
  246. @abstract Creates a JavaScript value from a JSON formatted string.
  247. @param ctx The execution context to use.
  248. @param string The JSString containing the JSON string to be parsed.
  249. @result A JSValue containing the parsed value, or NULL if the input is invalid.
  250. */
  251. JS_EXPORT JSValueRef JSValueMakeFromJSONString(JSContextRef ctx, JSStringRef string) JSC_API_AVAILABLE(macosx(10.7), ios(7.0));
  252. /*!
  253. @function
  254. @abstract Creates a JavaScript string containing the JSON serialized representation of a JS value.
  255. @param ctx The execution context to use.
  256. @param value The value to serialize.
  257. @param indent The number of spaces to indent when nesting. If 0, the resulting JSON will not contains newlines. The size of the indent is clamped to 10 spaces.
  258. @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
  259. @result A JSString with the result of serialization, or NULL if an exception is thrown.
  260. */
  261. JS_EXPORT JSStringRef JSValueCreateJSONString(JSContextRef ctx, JSValueRef value, unsigned indent, JSValueRef* exception) JSC_API_AVAILABLE(macosx(10.7), ios(7.0));
  262. /* Converting to primitive values */
  263. /*!
  264. @function
  265. @abstract Converts a JavaScript value to boolean and returns the resulting boolean.
  266. @param ctx The execution context to use.
  267. @param value The JSValue to convert.
  268. @result The boolean result of conversion.
  269. */
  270. JS_EXPORT bool JSValueToBoolean(JSContextRef ctx, JSValueRef value);
  271. /*!
  272. @function
  273. @abstract Converts a JavaScript value to number and returns the resulting number.
  274. @param ctx The execution context to use.
  275. @param value The JSValue to convert.
  276. @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
  277. @result The numeric result of conversion, or NaN if an exception is thrown.
  278. */
  279. JS_EXPORT double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception);
  280. /*!
  281. @function
  282. @abstract Converts a JavaScript value to string and copies the result into a JavaScript string.
  283. @param ctx The execution context to use.
  284. @param value The JSValue to convert.
  285. @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
  286. @result A JSString with the result of conversion, or NULL if an exception is thrown. Ownership follows the Create Rule.
  287. */
  288. JS_EXPORT JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception);
  289. /*!
  290. @function
  291. @abstract Converts a JavaScript value to object and returns the resulting object.
  292. @param ctx The execution context to use.
  293. @param value The JSValue to convert.
  294. @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
  295. @result The JSObject result of conversion, or NULL if an exception is thrown.
  296. */
  297. JS_EXPORT JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception);
  298. /* Garbage collection */
  299. /*!
  300. @function
  301. @abstract Protects a JavaScript value from garbage collection.
  302. @param ctx The execution context to use.
  303. @param value The JSValue to protect.
  304. @discussion Use this method when you want to store a JSValue in a global or on the heap, where the garbage collector will not be able to discover your reference to it.
  305. A value may be protected multiple times and must be unprotected an equal number of times before becoming eligible for garbage collection.
  306. */
  307. JS_EXPORT void JSValueProtect(JSContextRef ctx, JSValueRef value);
  308. /*!
  309. @function
  310. @abstract Unprotects a JavaScript value from garbage collection.
  311. @param ctx The execution context to use.
  312. @param value The JSValue to unprotect.
  313. @discussion A value may be protected multiple times and must be unprotected an
  314. equal number of times before becoming eligible for garbage collection.
  315. */
  316. JS_EXPORT void JSValueUnprotect(JSContextRef ctx, JSValueRef value);
  317. #ifdef __cplusplus
  318. }
  319. #endif
  320. #endif /* JSValueRef_h */