2
0

duktape.h 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. /*
  2. * Duktape public API for Duktape 1.4.0.
  3. * See the API reference for documentation on call semantics.
  4. * The exposed API is inside the DUK_API_PUBLIC_H_INCLUDED
  5. * include guard. Other parts of the header are Duktape
  6. * internal and related to platform/compiler/feature detection.
  7. *
  8. * Git commit cad6f595382a0cc1a7e4207794ade5be11b3e397 (v1.4.0).
  9. * Git branch master.
  10. *
  11. * See Duktape AUTHORS.rst and LICENSE.txt for copyright and
  12. * licensing information.
  13. */
  14. /* LICENSE.txt */
  15. /*
  16. * ===============
  17. * Duktape license
  18. * ===============
  19. *
  20. * (http://opensource.org/licenses/MIT)
  21. *
  22. * Copyright (c) 2013-2016 by Duktape authors (see AUTHORS.rst)
  23. *
  24. * Permission is hereby granted, free of charge, to any person obtaining a copy
  25. * of this software and associated documentation files (the "Software"), to deal
  26. * in the Software without restriction, including without limitation the rights
  27. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  28. * copies of the Software, and to permit persons to whom the Software is
  29. * furnished to do so, subject to the following conditions:
  30. *
  31. * The above copyright notice and this permission notice shall be included in
  32. * all copies or substantial portions of the Software.
  33. *
  34. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  35. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  36. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  37. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  38. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  39. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  40. * THE SOFTWARE.
  41. */
  42. /* AUTHORS.rst */
  43. /*
  44. * ===============
  45. * Duktape authors
  46. * ===============
  47. *
  48. * Copyright
  49. * =========
  50. *
  51. * Duktape copyrights are held by its authors. Each author has a copyright
  52. * to their contribution, and agrees to irrevocably license the contribution
  53. * under the Duktape ``LICENSE.txt``.
  54. *
  55. * Authors
  56. * =======
  57. *
  58. * Please include an e-mail address, a link to your GitHub profile, or something
  59. * similar to allow your contribution to be identified accurately.
  60. *
  61. * The following people have contributed code, website contents, or Wiki contents,
  62. * and agreed to irrevocably license their contributions under the Duktape
  63. * ``LICENSE.txt`` (in order of appearance):
  64. *
  65. * * Sami Vaarala <[email protected]>
  66. * * Niki Dobrev
  67. * * Andreas \u00d6man <[email protected]>
  68. * * L\u00e1szl\u00f3 Lang\u00f3 <[email protected]>
  69. * * Legimet <[email protected]>
  70. * * Karl Skomski <[email protected]>
  71. * * Bruce Pascoe <[email protected]>
  72. * * Ren\u00e9 Hollander <[email protected]>
  73. *
  74. * Other contributions
  75. * ===================
  76. *
  77. * The following people have contributed something other than code (e.g. reported
  78. * bugs, provided ideas, etc; roughly in order of appearance):
  79. *
  80. * * Greg Burns
  81. * * Anthony Rabine
  82. * * Carlos Costa
  83. * * Aur\u00e9lien Bouilland
  84. * * Preet Desai (Pris Matic)
  85. * * judofyr (http://www.reddit.com/user/judofyr)
  86. * * Jason Woofenden
  87. * * Micha\u0142 Przyby\u015b
  88. * * Anthony Howe
  89. * * Conrad Pankoff
  90. * * Jim Schimpf
  91. * * Rajaran Gaunker (https://github.com/zimbabao)
  92. * * Andreas \u00d6man
  93. * * Doug Sanden
  94. * * Josh Engebretson (https://github.com/JoshEngebretson)
  95. * * Remo Eichenberger (https://github.com/remoe)
  96. * * Mamod Mehyar (https://github.com/mamod)
  97. * * David Demelier (https://github.com/markand)
  98. * * Tim Caswell (https://github.com/creationix)
  99. * * Mitchell Blank Jr (https://github.com/mitchblank)
  100. * * https://github.com/yushli
  101. * * Seo Sanghyeon (https://github.com/sanxiyn)
  102. * * Han ChoongWoo (https://github.com/tunz)
  103. * * Joshua Peek (https://github.com/josh)
  104. * * Bruce E. Pascoe (https://github.com/fatcerberus)
  105. * * https://github.com/Kelledin
  106. * * https://github.com/sstruchtrup
  107. * * Michael Drake (https://github.com/tlsa)
  108. * * https://github.com/chris-y
  109. * * Laurent Zubiaur (https://github.com/lzubiaur)
  110. *
  111. * If you are accidentally missing from this list, send me an e-mail
  112. * (``[email protected]``) and I'll fix the omission.
  113. */
  114. #ifndef DUKTAPE_H_INCLUDED
  115. #define DUKTAPE_H_INCLUDED
  116. #define DUK_SINGLE_FILE
  117. /* External duk_config.h provides platform/compiler/OS dependent
  118. * typedefs and macros, and DUK_USE_xxx config options so that
  119. * the rest of Duktape doesn't need to do any feature detection.
  120. */
  121. #include "duk_config.h"
  122. /*
  123. * BEGIN PUBLIC API
  124. */
  125. #ifndef DUK_API_PUBLIC_H_INCLUDED
  126. #define DUK_API_PUBLIC_H_INCLUDED
  127. /*
  128. * Avoid C++ name mangling
  129. */
  130. #ifdef __cplusplus
  131. extern "C" {
  132. #endif
  133. /*
  134. * Some defines forwarded from feature detection
  135. */
  136. #undef DUK_API_VARIADIC_MACROS
  137. #ifdef DUK_USE_VARIADIC_MACROS
  138. #define DUK_API_VARIADIC_MACROS
  139. #endif
  140. #define DUK_API_NORETURN(decl) DUK_NORETURN(decl)
  141. /*
  142. * Public API specific typedefs
  143. *
  144. * Many types are wrapped by Duktape for portability to rare platforms
  145. * where e.g. 'int' is a 16-bit type. See practical typing discussion
  146. * in Duktape web documentation.
  147. */
  148. struct duk_memory_functions;
  149. struct duk_function_list_entry;
  150. struct duk_number_list_entry;
  151. /* duk_context is now defined in duk_config.h because it may also be
  152. * referenced there by prototypes.
  153. */
  154. typedef struct duk_memory_functions duk_memory_functions;
  155. typedef struct duk_function_list_entry duk_function_list_entry;
  156. typedef struct duk_number_list_entry duk_number_list_entry;
  157. typedef duk_ret_t (*duk_c_function)(duk_context *ctx);
  158. typedef void *(*duk_alloc_function) (void *udata, duk_size_t size);
  159. typedef void *(*duk_realloc_function) (void *udata, void *ptr, duk_size_t size);
  160. typedef void (*duk_free_function) (void *udata, void *ptr);
  161. typedef void (*duk_fatal_function) (duk_context *ctx, duk_errcode_t code, const char *msg);
  162. typedef void (*duk_decode_char_function) (void *udata, duk_codepoint_t codepoint);
  163. typedef duk_codepoint_t (*duk_map_char_function) (void *udata, duk_codepoint_t codepoint);
  164. typedef duk_ret_t (*duk_safe_call_function) (duk_context *ctx);
  165. typedef duk_size_t (*duk_debug_read_function) (void *udata, char *buffer, duk_size_t length);
  166. typedef duk_size_t (*duk_debug_write_function) (void *udata, const char *buffer, duk_size_t length);
  167. typedef duk_size_t (*duk_debug_peek_function) (void *udata);
  168. typedef void (*duk_debug_read_flush_function) (void *udata);
  169. typedef void (*duk_debug_write_flush_function) (void *udata);
  170. typedef void (*duk_debug_detached_function) (void *udata);
  171. struct duk_memory_functions {
  172. duk_alloc_function alloc_func;
  173. duk_realloc_function realloc_func;
  174. duk_free_function free_func;
  175. void *udata;
  176. };
  177. struct duk_function_list_entry {
  178. const char *key;
  179. duk_c_function value;
  180. duk_idx_t nargs;
  181. };
  182. struct duk_number_list_entry {
  183. const char *key;
  184. duk_double_t value;
  185. };
  186. /*
  187. * Constants
  188. */
  189. /* Duktape version, (major * 10000) + (minor * 100) + patch. Allows C code
  190. * to #ifdef against Duktape API version. The same value is also available
  191. * to Ecmascript code in Duktape.version. Unofficial development snapshots
  192. * have 99 for patch level (e.g. 0.10.99 would be a development version
  193. * after 0.10.0 but before the next official release).
  194. */
  195. #define DUK_VERSION 10400L
  196. /* Git commit, describe, and branch for Duktape build. Useful for
  197. * non-official snapshot builds so that application code can easily log
  198. * which Duktape snapshot was used. Not available in the Ecmascript
  199. * environment.
  200. */
  201. #define DUK_GIT_COMMIT "cad6f595382a0cc1a7e4207794ade5be11b3e397"
  202. #define DUK_GIT_DESCRIBE "v1.4.0"
  203. #define DUK_GIT_BRANCH "master"
  204. /* Duktape debug protocol version used by this build. */
  205. #define DUK_DEBUG_PROTOCOL_VERSION 1
  206. /* Used to represent invalid index; if caller uses this without checking,
  207. * this index will map to a non-existent stack entry. Also used in some
  208. * API calls as a marker to denote "no value".
  209. */
  210. #define DUK_INVALID_INDEX DUK_IDX_MIN
  211. /* Indicates that a native function does not have a fixed number of args,
  212. * and the argument stack should not be capped/extended at all.
  213. */
  214. #define DUK_VARARGS ((duk_int_t) (-1))
  215. /* Number of value stack entries (in addition to actual call arguments)
  216. * guaranteed to be allocated on entry to a Duktape/C function.
  217. */
  218. #define DUK_API_ENTRY_STACK 64
  219. /* Value types, used by e.g. duk_get_type() */
  220. #define DUK_TYPE_MIN 0
  221. #define DUK_TYPE_NONE 0 /* no value, e.g. invalid index */
  222. #define DUK_TYPE_UNDEFINED 1 /* Ecmascript undefined */
  223. #define DUK_TYPE_NULL 2 /* Ecmascript null */
  224. #define DUK_TYPE_BOOLEAN 3 /* Ecmascript boolean: 0 or 1 */
  225. #define DUK_TYPE_NUMBER 4 /* Ecmascript number: double */
  226. #define DUK_TYPE_STRING 5 /* Ecmascript string: CESU-8 / extended UTF-8 encoded */
  227. #define DUK_TYPE_OBJECT 6 /* Ecmascript object: includes objects, arrays, functions, threads */
  228. #define DUK_TYPE_BUFFER 7 /* fixed or dynamic, garbage collected byte buffer */
  229. #define DUK_TYPE_POINTER 8 /* raw void pointer */
  230. #define DUK_TYPE_LIGHTFUNC 9 /* lightweight function pointer */
  231. #define DUK_TYPE_MAX 9
  232. /* Value mask types, used by e.g. duk_get_type_mask() */
  233. #define DUK_TYPE_MASK_NONE (1 << DUK_TYPE_NONE)
  234. #define DUK_TYPE_MASK_UNDEFINED (1 << DUK_TYPE_UNDEFINED)
  235. #define DUK_TYPE_MASK_NULL (1 << DUK_TYPE_NULL)
  236. #define DUK_TYPE_MASK_BOOLEAN (1 << DUK_TYPE_BOOLEAN)
  237. #define DUK_TYPE_MASK_NUMBER (1 << DUK_TYPE_NUMBER)
  238. #define DUK_TYPE_MASK_STRING (1 << DUK_TYPE_STRING)
  239. #define DUK_TYPE_MASK_OBJECT (1 << DUK_TYPE_OBJECT)
  240. #define DUK_TYPE_MASK_BUFFER (1 << DUK_TYPE_BUFFER)
  241. #define DUK_TYPE_MASK_POINTER (1 << DUK_TYPE_POINTER)
  242. #define DUK_TYPE_MASK_LIGHTFUNC (1 << DUK_TYPE_LIGHTFUNC)
  243. #define DUK_TYPE_MASK_THROW (1 << 10) /* internal flag value: throw if mask doesn't match */
  244. /* Coercion hints */
  245. #define DUK_HINT_NONE 0 /* prefer number, unless input is a Date, in which
  246. * case prefer string (E5 Section 8.12.8)
  247. */
  248. #define DUK_HINT_STRING 1 /* prefer string */
  249. #define DUK_HINT_NUMBER 2 /* prefer number */
  250. /* Enumeration flags for duk_enum() */
  251. #define DUK_ENUM_INCLUDE_NONENUMERABLE (1 << 0) /* enumerate non-numerable properties in addition to enumerable */
  252. #define DUK_ENUM_INCLUDE_INTERNAL (1 << 1) /* enumerate internal properties (regardless of enumerability) */
  253. #define DUK_ENUM_OWN_PROPERTIES_ONLY (1 << 2) /* don't walk prototype chain, only check own properties */
  254. #define DUK_ENUM_ARRAY_INDICES_ONLY (1 << 3) /* only enumerate array indices */
  255. #define DUK_ENUM_SORT_ARRAY_INDICES (1 << 4) /* sort array indices, use with DUK_ENUM_ARRAY_INDICES_ONLY */
  256. #define DUK_ENUM_NO_PROXY_BEHAVIOR (1 << 5) /* enumerate a proxy object itself without invoking proxy behavior */
  257. /* Compilation flags for duk_compile() and duk_eval() */
  258. #define DUK_COMPILE_EVAL (1 << 0) /* compile eval code (instead of global code) */
  259. #define DUK_COMPILE_FUNCTION (1 << 1) /* compile function code (instead of global code) */
  260. #define DUK_COMPILE_STRICT (1 << 2) /* use strict (outer) context for global, eval, or function code */
  261. #define DUK_COMPILE_SAFE (1 << 3) /* (internal) catch compilation errors */
  262. #define DUK_COMPILE_NORESULT (1 << 4) /* (internal) omit eval result */
  263. #define DUK_COMPILE_NOSOURCE (1 << 5) /* (internal) no source string on stack */
  264. #define DUK_COMPILE_STRLEN (1 << 6) /* (internal) take strlen() of src_buffer (avoids double evaluation in macro) */
  265. /* Flags for duk_def_prop() and its variants */
  266. #define DUK_DEFPROP_WRITABLE (1 << 0) /* set writable (effective if DUK_DEFPROP_HAVE_WRITABLE set) */
  267. #define DUK_DEFPROP_ENUMERABLE (1 << 1) /* set enumerable (effective if DUK_DEFPROP_HAVE_ENUMERABLE set) */
  268. #define DUK_DEFPROP_CONFIGURABLE (1 << 2) /* set configurable (effective if DUK_DEFPROP_HAVE_CONFIGURABLE set) */
  269. #define DUK_DEFPROP_HAVE_WRITABLE (1 << 3) /* set/clear writable */
  270. #define DUK_DEFPROP_HAVE_ENUMERABLE (1 << 4) /* set/clear enumerable */
  271. #define DUK_DEFPROP_HAVE_CONFIGURABLE (1 << 5) /* set/clear configurable */
  272. #define DUK_DEFPROP_HAVE_VALUE (1 << 6) /* set value (given on value stack) */
  273. #define DUK_DEFPROP_HAVE_GETTER (1 << 7) /* set getter (given on value stack) */
  274. #define DUK_DEFPROP_HAVE_SETTER (1 << 8) /* set setter (given on value stack) */
  275. #define DUK_DEFPROP_FORCE (1 << 9) /* force change if possible, may still fail for e.g. virtual properties */
  276. #define DUK_DEFPROP_SET_WRITABLE (DUK_DEFPROP_HAVE_WRITABLE | DUK_DEFPROP_WRITABLE)
  277. #define DUK_DEFPROP_CLEAR_WRITABLE DUK_DEFPROP_HAVE_WRITABLE
  278. #define DUK_DEFPROP_SET_ENUMERABLE (DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE)
  279. #define DUK_DEFPROP_CLEAR_ENUMERABLE DUK_DEFPROP_HAVE_ENUMERABLE
  280. #define DUK_DEFPROP_SET_CONFIGURABLE (DUK_DEFPROP_HAVE_CONFIGURABLE | DUK_DEFPROP_CONFIGURABLE)
  281. #define DUK_DEFPROP_CLEAR_CONFIGURABLE DUK_DEFPROP_HAVE_CONFIGURABLE
  282. /* Flags for duk_push_thread_raw() */
  283. #define DUK_THREAD_NEW_GLOBAL_ENV (1 << 0) /* create a new global environment */
  284. /* Flags for duk_push_string_file_raw() */
  285. #define DUK_STRING_PUSH_SAFE (1 << 0) /* no error if file does not exist */
  286. /* Duktape specific error codes */
  287. #define DUK_ERR_NONE 0 /* no error (e.g. from duk_get_error_code()) */
  288. #define DUK_ERR_UNIMPLEMENTED_ERROR 50 /* UnimplementedError */
  289. #define DUK_ERR_UNSUPPORTED_ERROR 51 /* UnsupportedError */
  290. #define DUK_ERR_INTERNAL_ERROR 52 /* InternalError */
  291. #define DUK_ERR_ALLOC_ERROR 53 /* AllocError */
  292. #define DUK_ERR_ASSERTION_ERROR 54 /* AssertionError */
  293. #define DUK_ERR_API_ERROR 55 /* APIError */
  294. #define DUK_ERR_UNCAUGHT_ERROR 56 /* UncaughtError */
  295. /* Ecmascript E5 specification error codes */
  296. #define DUK_ERR_ERROR 100 /* Error */
  297. #define DUK_ERR_EVAL_ERROR 101 /* EvalError */
  298. #define DUK_ERR_RANGE_ERROR 102 /* RangeError */
  299. #define DUK_ERR_REFERENCE_ERROR 103 /* ReferenceError */
  300. #define DUK_ERR_SYNTAX_ERROR 104 /* SyntaxError */
  301. #define DUK_ERR_TYPE_ERROR 105 /* TypeError */
  302. #define DUK_ERR_URI_ERROR 106 /* URIError */
  303. /* Return codes for C functions (shortcut for throwing an error) */
  304. #define DUK_RET_UNIMPLEMENTED_ERROR (-DUK_ERR_UNIMPLEMENTED_ERROR)
  305. #define DUK_RET_UNSUPPORTED_ERROR (-DUK_ERR_UNSUPPORTED_ERROR)
  306. #define DUK_RET_INTERNAL_ERROR (-DUK_ERR_INTERNAL_ERROR)
  307. #define DUK_RET_ALLOC_ERROR (-DUK_ERR_ALLOC_ERROR)
  308. #define DUK_RET_ASSERTION_ERROR (-DUK_ERR_ASSERTION_ERROR)
  309. #define DUK_RET_API_ERROR (-DUK_ERR_API_ERROR)
  310. #define DUK_RET_UNCAUGHT_ERROR (-DUK_ERR_UNCAUGHT_ERROR)
  311. #define DUK_RET_ERROR (-DUK_ERR_ERROR)
  312. #define DUK_RET_EVAL_ERROR (-DUK_ERR_EVAL_ERROR)
  313. #define DUK_RET_RANGE_ERROR (-DUK_ERR_RANGE_ERROR)
  314. #define DUK_RET_REFERENCE_ERROR (-DUK_ERR_REFERENCE_ERROR)
  315. #define DUK_RET_SYNTAX_ERROR (-DUK_ERR_SYNTAX_ERROR)
  316. #define DUK_RET_TYPE_ERROR (-DUK_ERR_TYPE_ERROR)
  317. #define DUK_RET_URI_ERROR (-DUK_ERR_URI_ERROR)
  318. /* Return codes for protected calls (duk_safe_call(), duk_pcall()). */
  319. #define DUK_EXEC_SUCCESS 0
  320. #define DUK_EXEC_ERROR 1
  321. /* Log levels */
  322. #define DUK_LOG_TRACE 0
  323. #define DUK_LOG_DEBUG 1
  324. #define DUK_LOG_INFO 2
  325. #define DUK_LOG_WARN 3
  326. #define DUK_LOG_ERROR 4
  327. #define DUK_LOG_FATAL 5
  328. /*
  329. * If no variadic macros, __FILE__ and __LINE__ are passed through globals
  330. * which is ugly and not thread safe.
  331. */
  332. #ifndef DUK_API_VARIADIC_MACROS
  333. DUK_EXTERNAL_DECL const char *duk_api_global_filename;
  334. DUK_EXTERNAL_DECL duk_int_t duk_api_global_line;
  335. #endif
  336. /*
  337. * Context management
  338. */
  339. DUK_EXTERNAL_DECL
  340. duk_context *duk_create_heap(duk_alloc_function alloc_func,
  341. duk_realloc_function realloc_func,
  342. duk_free_function free_func,
  343. void *heap_udata,
  344. duk_fatal_function fatal_handler);
  345. DUK_EXTERNAL_DECL void duk_destroy_heap(duk_context *ctx);
  346. #define duk_create_heap_default() \
  347. duk_create_heap(NULL, NULL, NULL, NULL, NULL)
  348. /*
  349. * Memory management
  350. *
  351. * Raw functions have no side effects (cannot trigger GC).
  352. */
  353. DUK_EXTERNAL_DECL void *duk_alloc_raw(duk_context *ctx, duk_size_t size);
  354. DUK_EXTERNAL_DECL void duk_free_raw(duk_context *ctx, void *ptr);
  355. DUK_EXTERNAL_DECL void *duk_realloc_raw(duk_context *ctx, void *ptr, duk_size_t size);
  356. DUK_EXTERNAL_DECL void *duk_alloc(duk_context *ctx, duk_size_t size);
  357. DUK_EXTERNAL_DECL void duk_free(duk_context *ctx, void *ptr);
  358. DUK_EXTERNAL_DECL void *duk_realloc(duk_context *ctx, void *ptr, duk_size_t size);
  359. DUK_EXTERNAL_DECL void duk_get_memory_functions(duk_context *ctx, duk_memory_functions *out_funcs);
  360. DUK_EXTERNAL_DECL void duk_gc(duk_context *ctx, duk_uint_t flags);
  361. /*
  362. * Error handling
  363. */
  364. DUK_API_NORETURN(DUK_EXTERNAL_DECL void duk_throw(duk_context *ctx));
  365. DUK_API_NORETURN(DUK_EXTERNAL_DECL void duk_fatal(duk_context *ctx, duk_errcode_t err_code, const char *err_msg));
  366. DUK_API_NORETURN(DUK_EXTERNAL_DECL void duk_error_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, ...));
  367. #ifdef DUK_API_VARIADIC_MACROS
  368. #define duk_error(ctx,err_code,...) \
  369. duk_error_raw((ctx), (duk_errcode_t) (err_code), (const char *) (__FILE__), (duk_int_t) (__LINE__), __VA_ARGS__)
  370. #else
  371. DUK_API_NORETURN(DUK_EXTERNAL_DECL void duk_error_stash(duk_context *ctx, duk_errcode_t err_code, const char *fmt, ...));
  372. /* One problem with this macro is that expressions like the following fail
  373. * to compile: "(void) duk_error(...)". But because duk_error() is noreturn,
  374. * they make little sense anyway.
  375. */
  376. #define duk_error \
  377. (duk_api_global_filename = (const char *) (__FILE__), \
  378. duk_api_global_line = (duk_int_t) (__LINE__), \
  379. duk_error_stash) /* last value is func pointer, arguments follow in parens */
  380. #endif
  381. DUK_API_NORETURN(DUK_EXTERNAL_DECL void duk_error_va_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, va_list ap));
  382. #define duk_error_va(ctx,err_code,fmt,ap) \
  383. duk_error_va_raw((ctx), (duk_errcode_t) (err_code), (const char *) (__FILE__), (duk_int_t) (__LINE__), (fmt), (ap))
  384. /*
  385. * Other state related functions
  386. */
  387. DUK_EXTERNAL_DECL duk_bool_t duk_is_strict_call(duk_context *ctx);
  388. DUK_EXTERNAL_DECL duk_bool_t duk_is_constructor_call(duk_context *ctx);
  389. /*
  390. * Stack management
  391. */
  392. DUK_EXTERNAL_DECL duk_idx_t duk_normalize_index(duk_context *ctx, duk_idx_t index);
  393. DUK_EXTERNAL_DECL duk_idx_t duk_require_normalize_index(duk_context *ctx, duk_idx_t index);
  394. DUK_EXTERNAL_DECL duk_bool_t duk_is_valid_index(duk_context *ctx, duk_idx_t index);
  395. DUK_EXTERNAL_DECL void duk_require_valid_index(duk_context *ctx, duk_idx_t index);
  396. DUK_EXTERNAL_DECL duk_idx_t duk_get_top(duk_context *ctx);
  397. DUK_EXTERNAL_DECL void duk_set_top(duk_context *ctx, duk_idx_t index);
  398. DUK_EXTERNAL_DECL duk_idx_t duk_get_top_index(duk_context *ctx);
  399. DUK_EXTERNAL_DECL duk_idx_t duk_require_top_index(duk_context *ctx);
  400. /* Although extra/top could be an unsigned type here, using a signed type
  401. * makes the API more robust to calling code calculation errors or corner
  402. * cases (where caller might occasionally come up with negative values).
  403. * Negative values are treated as zero, which is better than casting them
  404. * to a large unsigned number. (This principle is used elsewhere in the
  405. * API too.)
  406. */
  407. DUK_EXTERNAL_DECL duk_bool_t duk_check_stack(duk_context *ctx, duk_idx_t extra);
  408. DUK_EXTERNAL_DECL void duk_require_stack(duk_context *ctx, duk_idx_t extra);
  409. DUK_EXTERNAL_DECL duk_bool_t duk_check_stack_top(duk_context *ctx, duk_idx_t top);
  410. DUK_EXTERNAL_DECL void duk_require_stack_top(duk_context *ctx, duk_idx_t top);
  411. /*
  412. * Stack manipulation (other than push/pop)
  413. */
  414. DUK_EXTERNAL_DECL void duk_swap(duk_context *ctx, duk_idx_t index1, duk_idx_t index2);
  415. DUK_EXTERNAL_DECL void duk_swap_top(duk_context *ctx, duk_idx_t index);
  416. DUK_EXTERNAL_DECL void duk_dup(duk_context *ctx, duk_idx_t from_index);
  417. DUK_EXTERNAL_DECL void duk_dup_top(duk_context *ctx);
  418. DUK_EXTERNAL_DECL void duk_insert(duk_context *ctx, duk_idx_t to_index);
  419. DUK_EXTERNAL_DECL void duk_replace(duk_context *ctx, duk_idx_t to_index);
  420. DUK_EXTERNAL_DECL void duk_copy(duk_context *ctx, duk_idx_t from_index, duk_idx_t to_index);
  421. DUK_EXTERNAL_DECL void duk_remove(duk_context *ctx, duk_idx_t index);
  422. DUK_EXTERNAL_DECL void duk_xcopymove_raw(duk_context *to_ctx, duk_context *from_ctx, duk_idx_t count, duk_bool_t is_copy);
  423. #define duk_xmove_top(to_ctx,from_ctx,count) \
  424. duk_xcopymove_raw((to_ctx), (from_ctx), (count), 0 /*is_copy*/)
  425. #define duk_xcopy_top(to_ctx,from_ctx,count) \
  426. duk_xcopymove_raw((to_ctx), (from_ctx), (count), 1 /*is_copy*/)
  427. /*
  428. * Push operations
  429. *
  430. * Push functions return the absolute (relative to bottom of frame)
  431. * position of the pushed value for convenience.
  432. *
  433. * Note: duk_dup() is technically a push.
  434. */
  435. DUK_EXTERNAL_DECL void duk_push_undefined(duk_context *ctx);
  436. DUK_EXTERNAL_DECL void duk_push_null(duk_context *ctx);
  437. DUK_EXTERNAL_DECL void duk_push_boolean(duk_context *ctx, duk_bool_t val);
  438. DUK_EXTERNAL_DECL void duk_push_true(duk_context *ctx);
  439. DUK_EXTERNAL_DECL void duk_push_false(duk_context *ctx);
  440. DUK_EXTERNAL_DECL void duk_push_number(duk_context *ctx, duk_double_t val);
  441. DUK_EXTERNAL_DECL void duk_push_nan(duk_context *ctx);
  442. DUK_EXTERNAL_DECL void duk_push_int(duk_context *ctx, duk_int_t val);
  443. DUK_EXTERNAL_DECL void duk_push_uint(duk_context *ctx, duk_uint_t val);
  444. DUK_EXTERNAL_DECL const char *duk_push_string(duk_context *ctx, const char *str);
  445. DUK_EXTERNAL_DECL const char *duk_push_lstring(duk_context *ctx, const char *str, duk_size_t len);
  446. DUK_EXTERNAL_DECL void duk_push_pointer(duk_context *ctx, void *p);
  447. DUK_EXTERNAL_DECL const char *duk_push_sprintf(duk_context *ctx, const char *fmt, ...);
  448. DUK_EXTERNAL_DECL const char *duk_push_vsprintf(duk_context *ctx, const char *fmt, va_list ap);
  449. DUK_EXTERNAL_DECL const char *duk_push_string_file_raw(duk_context *ctx, const char *path, duk_uint_t flags);
  450. #define duk_push_string_file(ctx,path) \
  451. duk_push_string_file_raw((ctx), (path), 0)
  452. DUK_EXTERNAL_DECL void duk_push_this(duk_context *ctx);
  453. DUK_EXTERNAL_DECL void duk_push_current_function(duk_context *ctx);
  454. DUK_EXTERNAL_DECL void duk_push_current_thread(duk_context *ctx);
  455. DUK_EXTERNAL_DECL void duk_push_global_object(duk_context *ctx);
  456. DUK_EXTERNAL_DECL void duk_push_heap_stash(duk_context *ctx);
  457. DUK_EXTERNAL_DECL void duk_push_global_stash(duk_context *ctx);
  458. DUK_EXTERNAL_DECL void duk_push_thread_stash(duk_context *ctx, duk_context *target_ctx);
  459. DUK_EXTERNAL_DECL duk_idx_t duk_push_object(duk_context *ctx);
  460. DUK_EXTERNAL_DECL duk_idx_t duk_push_array(duk_context *ctx);
  461. DUK_EXTERNAL_DECL duk_idx_t duk_push_c_function(duk_context *ctx, duk_c_function func, duk_idx_t nargs);
  462. DUK_EXTERNAL_DECL duk_idx_t duk_push_c_lightfunc(duk_context *ctx, duk_c_function func, duk_idx_t nargs, duk_idx_t length, duk_int_t magic);
  463. DUK_EXTERNAL_DECL duk_idx_t duk_push_thread_raw(duk_context *ctx, duk_uint_t flags);
  464. #define duk_push_thread(ctx) \
  465. duk_push_thread_raw((ctx), 0 /*flags*/)
  466. #define duk_push_thread_new_globalenv(ctx) \
  467. duk_push_thread_raw((ctx), DUK_THREAD_NEW_GLOBAL_ENV /*flags*/)
  468. DUK_EXTERNAL_DECL duk_idx_t duk_push_error_object_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, ...);
  469. #ifdef DUK_API_VARIADIC_MACROS
  470. #define duk_push_error_object(ctx,err_code,...) \
  471. duk_push_error_object_raw((ctx), (err_code), (const char *) (__FILE__), (duk_int_t) (__LINE__), __VA_ARGS__)
  472. #else
  473. DUK_EXTERNAL_DECL duk_idx_t duk_push_error_object_stash(duk_context *ctx, duk_errcode_t err_code, const char *fmt, ...);
  474. /* Note: parentheses are required so that the comma expression works in assignments. */
  475. #define duk_push_error_object \
  476. (duk_api_global_filename = (const char *) (__FILE__), \
  477. duk_api_global_line = (duk_int_t) (__LINE__), \
  478. duk_push_error_object_stash) /* last value is func pointer, arguments follow in parens */
  479. #endif
  480. DUK_EXTERNAL_DECL duk_idx_t duk_push_error_object_va_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, va_list ap);
  481. #define duk_push_error_object_va(ctx,err_code,fmt,ap) \
  482. duk_push_error_object_va_raw((ctx), (err_code), (const char *) (__FILE__), (duk_int_t) (__LINE__), (fmt), (ap))
  483. #define DUK_BUF_FLAG_DYNAMIC (1 << 0) /* internal flag: dynamic buffer */
  484. #define DUK_BUF_FLAG_EXTERNAL (1 << 1) /* internal flag: external buffer */
  485. #define DUK_BUF_FLAG_NOZERO (1 << 2) /* internal flag: don't zero allocated buffer */
  486. DUK_EXTERNAL_DECL void *duk_push_buffer_raw(duk_context *ctx, duk_size_t size, duk_small_uint_t flags);
  487. #define duk_push_buffer(ctx,size,dynamic) \
  488. duk_push_buffer_raw((ctx), (size), (dynamic) ? DUK_BUF_FLAG_DYNAMIC : 0)
  489. #define duk_push_fixed_buffer(ctx,size) \
  490. duk_push_buffer_raw((ctx), (size), 0 /*flags*/)
  491. #define duk_push_dynamic_buffer(ctx,size) \
  492. duk_push_buffer_raw((ctx), (size), DUK_BUF_FLAG_DYNAMIC /*flags*/)
  493. #define duk_push_external_buffer(ctx) \
  494. ((void) duk_push_buffer_raw((ctx), 0, DUK_BUF_FLAG_DYNAMIC | DUK_BUF_FLAG_EXTERNAL))
  495. #define DUK_BUFOBJ_CREATE_ARRBUF (1 << 4) /* internal flag: create backing ArrayBuffer; keep in one byte */
  496. #define DUK_BUFOBJ_DUKTAPE_BUFFER 0
  497. #define DUK_BUFOBJ_NODEJS_BUFFER 1
  498. #define DUK_BUFOBJ_ARRAYBUFFER 2
  499. #define DUK_BUFOBJ_DATAVIEW (3 | DUK_BUFOBJ_CREATE_ARRBUF)
  500. #define DUK_BUFOBJ_INT8ARRAY (4 | DUK_BUFOBJ_CREATE_ARRBUF)
  501. #define DUK_BUFOBJ_UINT8ARRAY (5 | DUK_BUFOBJ_CREATE_ARRBUF)
  502. #define DUK_BUFOBJ_UINT8CLAMPEDARRAY (6 | DUK_BUFOBJ_CREATE_ARRBUF)
  503. #define DUK_BUFOBJ_INT16ARRAY (7 | DUK_BUFOBJ_CREATE_ARRBUF)
  504. #define DUK_BUFOBJ_UINT16ARRAY (8 | DUK_BUFOBJ_CREATE_ARRBUF)
  505. #define DUK_BUFOBJ_INT32ARRAY (9 | DUK_BUFOBJ_CREATE_ARRBUF)
  506. #define DUK_BUFOBJ_UINT32ARRAY (10 | DUK_BUFOBJ_CREATE_ARRBUF)
  507. #define DUK_BUFOBJ_FLOAT32ARRAY (11 | DUK_BUFOBJ_CREATE_ARRBUF)
  508. #define DUK_BUFOBJ_FLOAT64ARRAY (12 | DUK_BUFOBJ_CREATE_ARRBUF)
  509. DUK_EXTERNAL_DECL void duk_push_buffer_object(duk_context *ctx, duk_idx_t idx_buffer, duk_size_t byte_offset, duk_size_t byte_length, duk_uint_t flags);
  510. DUK_EXTERNAL_DECL duk_idx_t duk_push_heapptr(duk_context *ctx, void *ptr);
  511. /*
  512. * Pop operations
  513. */
  514. DUK_EXTERNAL_DECL void duk_pop(duk_context *ctx);
  515. DUK_EXTERNAL_DECL void duk_pop_n(duk_context *ctx, duk_idx_t count);
  516. DUK_EXTERNAL_DECL void duk_pop_2(duk_context *ctx);
  517. DUK_EXTERNAL_DECL void duk_pop_3(duk_context *ctx);
  518. /*
  519. * Type checks
  520. *
  521. * duk_is_none(), which would indicate whether index it outside of stack,
  522. * is not needed; duk_is_valid_index() gives the same information.
  523. */
  524. DUK_EXTERNAL_DECL duk_int_t duk_get_type(duk_context *ctx, duk_idx_t index);
  525. DUK_EXTERNAL_DECL duk_bool_t duk_check_type(duk_context *ctx, duk_idx_t index, duk_int_t type);
  526. DUK_EXTERNAL_DECL duk_uint_t duk_get_type_mask(duk_context *ctx, duk_idx_t index);
  527. DUK_EXTERNAL_DECL duk_bool_t duk_check_type_mask(duk_context *ctx, duk_idx_t index, duk_uint_t mask);
  528. DUK_EXTERNAL_DECL duk_bool_t duk_is_undefined(duk_context *ctx, duk_idx_t index);
  529. DUK_EXTERNAL_DECL duk_bool_t duk_is_null(duk_context *ctx, duk_idx_t index);
  530. DUK_EXTERNAL_DECL duk_bool_t duk_is_null_or_undefined(duk_context *ctx, duk_idx_t index);
  531. DUK_EXTERNAL_DECL duk_bool_t duk_is_boolean(duk_context *ctx, duk_idx_t index);
  532. DUK_EXTERNAL_DECL duk_bool_t duk_is_number(duk_context *ctx, duk_idx_t index);
  533. DUK_EXTERNAL_DECL duk_bool_t duk_is_nan(duk_context *ctx, duk_idx_t index);
  534. DUK_EXTERNAL_DECL duk_bool_t duk_is_string(duk_context *ctx, duk_idx_t index);
  535. DUK_EXTERNAL_DECL duk_bool_t duk_is_object(duk_context *ctx, duk_idx_t index);
  536. DUK_EXTERNAL_DECL duk_bool_t duk_is_buffer(duk_context *ctx, duk_idx_t index);
  537. DUK_EXTERNAL_DECL duk_bool_t duk_is_pointer(duk_context *ctx, duk_idx_t index);
  538. DUK_EXTERNAL_DECL duk_bool_t duk_is_lightfunc(duk_context *ctx, duk_idx_t index);
  539. DUK_EXTERNAL_DECL duk_bool_t duk_is_array(duk_context *ctx, duk_idx_t index);
  540. DUK_EXTERNAL_DECL duk_bool_t duk_is_function(duk_context *ctx, duk_idx_t index);
  541. DUK_EXTERNAL_DECL duk_bool_t duk_is_c_function(duk_context *ctx, duk_idx_t index);
  542. DUK_EXTERNAL_DECL duk_bool_t duk_is_ecmascript_function(duk_context *ctx, duk_idx_t index);
  543. DUK_EXTERNAL_DECL duk_bool_t duk_is_bound_function(duk_context *ctx, duk_idx_t index);
  544. DUK_EXTERNAL_DECL duk_bool_t duk_is_thread(duk_context *ctx, duk_idx_t index);
  545. #define duk_is_callable(ctx,index) \
  546. duk_is_function((ctx), (index))
  547. DUK_EXTERNAL_DECL duk_bool_t duk_is_dynamic_buffer(duk_context *ctx, duk_idx_t index);
  548. DUK_EXTERNAL_DECL duk_bool_t duk_is_fixed_buffer(duk_context *ctx, duk_idx_t index);
  549. DUK_EXTERNAL_DECL duk_bool_t duk_is_external_buffer(duk_context *ctx, duk_idx_t index);
  550. #define duk_is_primitive(ctx,index) \
  551. duk_check_type_mask((ctx), (index), DUK_TYPE_MASK_UNDEFINED | \
  552. DUK_TYPE_MASK_NULL | \
  553. DUK_TYPE_MASK_BOOLEAN | \
  554. DUK_TYPE_MASK_NUMBER | \
  555. DUK_TYPE_MASK_STRING | \
  556. DUK_TYPE_MASK_BUFFER | \
  557. DUK_TYPE_MASK_POINTER | \
  558. DUK_TYPE_MASK_LIGHTFUNC)
  559. #define duk_is_object_coercible(ctx,index) \
  560. duk_check_type_mask((ctx), (index), DUK_TYPE_MASK_BOOLEAN | \
  561. DUK_TYPE_MASK_NUMBER | \
  562. DUK_TYPE_MASK_STRING | \
  563. DUK_TYPE_MASK_OBJECT | \
  564. DUK_TYPE_MASK_BUFFER | \
  565. DUK_TYPE_MASK_POINTER | \
  566. DUK_TYPE_MASK_LIGHTFUNC)
  567. DUK_EXTERNAL_DECL duk_errcode_t duk_get_error_code(duk_context *ctx, duk_idx_t index);
  568. #define duk_is_error(ctx,index) \
  569. (duk_get_error_code((ctx), (index)) != 0)
  570. #define duk_is_eval_error(ctx,index) \
  571. (duk_get_error_code((ctx), (index)) == DUK_ERR_EVAL_ERROR)
  572. #define duk_is_range_error(ctx,index) \
  573. (duk_get_error_code((ctx), (index)) == DUK_ERR_RANGE_ERROR)
  574. #define duk_is_reference_error(ctx,index) \
  575. (duk_get_error_code((ctx), (index)) == DUK_ERR_REFERENCE_ERROR)
  576. #define duk_is_syntax_error(ctx,index) \
  577. (duk_get_error_code((ctx), (index)) == DUK_ERR_SYNTAX_ERROR)
  578. #define duk_is_type_error(ctx,index) \
  579. (duk_get_error_code((ctx), (index)) == DUK_ERR_TYPE_ERROR)
  580. #define duk_is_uri_error(ctx,index) \
  581. (duk_get_error_code((ctx), (index)) == DUK_ERR_URI_ERROR)
  582. /*
  583. * Get operations: no coercion, returns default value for invalid
  584. * indices and invalid value types.
  585. *
  586. * duk_get_undefined() and duk_get_null() would be pointless and
  587. * are not included.
  588. */
  589. DUK_EXTERNAL_DECL duk_bool_t duk_get_boolean(duk_context *ctx, duk_idx_t index);
  590. DUK_EXTERNAL_DECL duk_double_t duk_get_number(duk_context *ctx, duk_idx_t index);
  591. DUK_EXTERNAL_DECL duk_int_t duk_get_int(duk_context *ctx, duk_idx_t index);
  592. DUK_EXTERNAL_DECL duk_uint_t duk_get_uint(duk_context *ctx, duk_idx_t index);
  593. DUK_EXTERNAL_DECL const char *duk_get_string(duk_context *ctx, duk_idx_t index);
  594. DUK_EXTERNAL_DECL const char *duk_get_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len);
  595. DUK_EXTERNAL_DECL void *duk_get_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size);
  596. DUK_EXTERNAL_DECL void *duk_get_buffer_data(duk_context *ctx, duk_idx_t index, duk_size_t *out_size);
  597. DUK_EXTERNAL_DECL void *duk_get_pointer(duk_context *ctx, duk_idx_t index);
  598. DUK_EXTERNAL_DECL duk_c_function duk_get_c_function(duk_context *ctx, duk_idx_t index);
  599. DUK_EXTERNAL_DECL duk_context *duk_get_context(duk_context *ctx, duk_idx_t index);
  600. DUK_EXTERNAL_DECL void *duk_get_heapptr(duk_context *ctx, duk_idx_t index);
  601. DUK_EXTERNAL_DECL duk_size_t duk_get_length(duk_context *ctx, duk_idx_t index);
  602. /*
  603. * Require operations: no coercion, throw error if index or type
  604. * is incorrect. No defaulting.
  605. */
  606. #define duk_require_type_mask(ctx,index,mask) \
  607. ((void) duk_check_type_mask((ctx), (index), (mask) | DUK_TYPE_MASK_THROW))
  608. DUK_EXTERNAL_DECL void duk_require_undefined(duk_context *ctx, duk_idx_t index);
  609. DUK_EXTERNAL_DECL void duk_require_null(duk_context *ctx, duk_idx_t index);
  610. DUK_EXTERNAL_DECL duk_bool_t duk_require_boolean(duk_context *ctx, duk_idx_t index);
  611. DUK_EXTERNAL_DECL duk_double_t duk_require_number(duk_context *ctx, duk_idx_t index);
  612. DUK_EXTERNAL_DECL duk_int_t duk_require_int(duk_context *ctx, duk_idx_t index);
  613. DUK_EXTERNAL_DECL duk_uint_t duk_require_uint(duk_context *ctx, duk_idx_t index);
  614. DUK_EXTERNAL_DECL const char *duk_require_string(duk_context *ctx, duk_idx_t index);
  615. DUK_EXTERNAL_DECL const char *duk_require_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len);
  616. DUK_EXTERNAL_DECL void *duk_require_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size);
  617. DUK_EXTERNAL_DECL void *duk_require_buffer_data(duk_context *ctx, duk_idx_t index, duk_size_t *out_size);
  618. DUK_EXTERNAL_DECL void *duk_require_pointer(duk_context *ctx, duk_idx_t index);
  619. DUK_EXTERNAL_DECL duk_c_function duk_require_c_function(duk_context *ctx, duk_idx_t index);
  620. DUK_EXTERNAL_DECL duk_context *duk_require_context(duk_context *ctx, duk_idx_t index);
  621. DUK_EXTERNAL_DECL void duk_require_function(duk_context *ctx, duk_idx_t index);
  622. #define duk_require_callable(ctx,index) \
  623. duk_require_function((ctx), (index))
  624. DUK_EXTERNAL_DECL void *duk_require_heapptr(duk_context *ctx, duk_idx_t index);
  625. #define duk_require_object_coercible(ctx,index) \
  626. ((void) duk_check_type_mask((ctx), (index), DUK_TYPE_MASK_BOOLEAN | \
  627. DUK_TYPE_MASK_NUMBER | \
  628. DUK_TYPE_MASK_STRING | \
  629. DUK_TYPE_MASK_OBJECT | \
  630. DUK_TYPE_MASK_BUFFER | \
  631. DUK_TYPE_MASK_POINTER | \
  632. DUK_TYPE_MASK_LIGHTFUNC | \
  633. DUK_TYPE_MASK_THROW))
  634. /*
  635. * Coercion operations: in-place coercion, return coerced value where
  636. * applicable. If index is invalid, throw error. Some coercions may
  637. * throw an expected error (e.g. from a toString() or valueOf() call)
  638. * or an internal error (e.g. from out of memory).
  639. */
  640. DUK_EXTERNAL_DECL void duk_to_undefined(duk_context *ctx, duk_idx_t index);
  641. DUK_EXTERNAL_DECL void duk_to_null(duk_context *ctx, duk_idx_t index);
  642. DUK_EXTERNAL_DECL duk_bool_t duk_to_boolean(duk_context *ctx, duk_idx_t index);
  643. DUK_EXTERNAL_DECL duk_double_t duk_to_number(duk_context *ctx, duk_idx_t index);
  644. DUK_EXTERNAL_DECL duk_int_t duk_to_int(duk_context *ctx, duk_idx_t index);
  645. DUK_EXTERNAL_DECL duk_uint_t duk_to_uint(duk_context *ctx, duk_idx_t index);
  646. DUK_EXTERNAL_DECL duk_int32_t duk_to_int32(duk_context *ctx, duk_idx_t index);
  647. DUK_EXTERNAL_DECL duk_uint32_t duk_to_uint32(duk_context *ctx, duk_idx_t index);
  648. DUK_EXTERNAL_DECL duk_uint16_t duk_to_uint16(duk_context *ctx, duk_idx_t index);
  649. DUK_EXTERNAL_DECL const char *duk_to_string(duk_context *ctx, duk_idx_t index);
  650. DUK_EXTERNAL_DECL const char *duk_to_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len);
  651. DUK_EXTERNAL_DECL void *duk_to_buffer_raw(duk_context *ctx, duk_idx_t index, duk_size_t *out_size, duk_uint_t flags);
  652. DUK_EXTERNAL_DECL void *duk_to_pointer(duk_context *ctx, duk_idx_t index);
  653. DUK_EXTERNAL_DECL void duk_to_object(duk_context *ctx, duk_idx_t index);
  654. DUK_EXTERNAL_DECL void duk_to_defaultvalue(duk_context *ctx, duk_idx_t index, duk_int_t hint);
  655. DUK_EXTERNAL_DECL void duk_to_primitive(duk_context *ctx, duk_idx_t index, duk_int_t hint);
  656. #define DUK_BUF_MODE_FIXED 0 /* internal: request fixed buffer result */
  657. #define DUK_BUF_MODE_DYNAMIC 1 /* internal: request dynamic buffer result */
  658. #define DUK_BUF_MODE_DONTCARE 2 /* internal: don't care about fixed/dynamic nature */
  659. #define duk_to_buffer(ctx,index,out_size) \
  660. duk_to_buffer_raw((ctx), (index), (out_size), DUK_BUF_MODE_DONTCARE)
  661. #define duk_to_fixed_buffer(ctx,index,out_size) \
  662. duk_to_buffer_raw((ctx), (index), (out_size), DUK_BUF_MODE_FIXED)
  663. #define duk_to_dynamic_buffer(ctx,index,out_size) \
  664. duk_to_buffer_raw((ctx), (index), (out_size), DUK_BUF_MODE_DYNAMIC)
  665. /* safe variants of a few coercion operations */
  666. DUK_EXTERNAL_DECL const char *duk_safe_to_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len);
  667. #define duk_safe_to_string(ctx,index) \
  668. duk_safe_to_lstring((ctx), (index), NULL)
  669. /*
  670. * Misc conversion
  671. */
  672. DUK_EXTERNAL_DECL const char *duk_base64_encode(duk_context *ctx, duk_idx_t index);
  673. DUK_EXTERNAL_DECL void duk_base64_decode(duk_context *ctx, duk_idx_t index);
  674. DUK_EXTERNAL_DECL const char *duk_hex_encode(duk_context *ctx, duk_idx_t index);
  675. DUK_EXTERNAL_DECL void duk_hex_decode(duk_context *ctx, duk_idx_t index);
  676. DUK_EXTERNAL_DECL const char *duk_json_encode(duk_context *ctx, duk_idx_t index);
  677. DUK_EXTERNAL_DECL void duk_json_decode(duk_context *ctx, duk_idx_t index);
  678. /*
  679. * Buffer
  680. */
  681. DUK_EXTERNAL_DECL void *duk_resize_buffer(duk_context *ctx, duk_idx_t index, duk_size_t new_size);
  682. DUK_EXTERNAL_DECL void *duk_steal_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size);
  683. DUK_EXTERNAL_DECL void duk_config_buffer(duk_context *ctx, duk_idx_t index, void *ptr, duk_size_t len);
  684. /*
  685. * Property access
  686. *
  687. * The basic function assumes key is on stack. The _string variant takes
  688. * a C string as a property name, while the _index variant takes an array
  689. * index as a property name (e.g. 123 is equivalent to the key "123").
  690. */
  691. DUK_EXTERNAL_DECL duk_bool_t duk_get_prop(duk_context *ctx, duk_idx_t obj_index);
  692. DUK_EXTERNAL_DECL duk_bool_t duk_get_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key);
  693. DUK_EXTERNAL_DECL duk_bool_t duk_get_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index);
  694. DUK_EXTERNAL_DECL duk_bool_t duk_put_prop(duk_context *ctx, duk_idx_t obj_index);
  695. DUK_EXTERNAL_DECL duk_bool_t duk_put_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key);
  696. DUK_EXTERNAL_DECL duk_bool_t duk_put_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index);
  697. DUK_EXTERNAL_DECL duk_bool_t duk_del_prop(duk_context *ctx, duk_idx_t obj_index);
  698. DUK_EXTERNAL_DECL duk_bool_t duk_del_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key);
  699. DUK_EXTERNAL_DECL duk_bool_t duk_del_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index);
  700. DUK_EXTERNAL_DECL duk_bool_t duk_has_prop(duk_context *ctx, duk_idx_t obj_index);
  701. DUK_EXTERNAL_DECL duk_bool_t duk_has_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key);
  702. DUK_EXTERNAL_DECL duk_bool_t duk_has_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index);
  703. DUK_EXTERNAL_DECL void duk_def_prop(duk_context *ctx, duk_idx_t obj_index, duk_uint_t flags);
  704. DUK_EXTERNAL_DECL duk_bool_t duk_get_global_string(duk_context *ctx, const char *key);
  705. DUK_EXTERNAL_DECL duk_bool_t duk_put_global_string(duk_context *ctx, const char *key);
  706. /*
  707. * Object prototype
  708. */
  709. DUK_EXTERNAL_DECL void duk_get_prototype(duk_context *ctx, duk_idx_t index);
  710. DUK_EXTERNAL_DECL void duk_set_prototype(duk_context *ctx, duk_idx_t index);
  711. /*
  712. * Object finalizer
  713. */
  714. DUK_EXTERNAL_DECL void duk_get_finalizer(duk_context *ctx, duk_idx_t index);
  715. DUK_EXTERNAL_DECL void duk_set_finalizer(duk_context *ctx, duk_idx_t index);
  716. /*
  717. * Global object
  718. */
  719. DUK_EXTERNAL_DECL void duk_set_global_object(duk_context *ctx);
  720. /*
  721. * Duktape/C function magic value
  722. */
  723. DUK_EXTERNAL_DECL duk_int_t duk_get_magic(duk_context *ctx, duk_idx_t index);
  724. DUK_EXTERNAL_DECL void duk_set_magic(duk_context *ctx, duk_idx_t index, duk_int_t magic);
  725. DUK_EXTERNAL_DECL duk_int_t duk_get_current_magic(duk_context *ctx);
  726. /*
  727. * Module helpers: put multiple function or constant properties
  728. */
  729. DUK_EXTERNAL_DECL void duk_put_function_list(duk_context *ctx, duk_idx_t obj_index, const duk_function_list_entry *funcs);
  730. DUK_EXTERNAL_DECL void duk_put_number_list(duk_context *ctx, duk_idx_t obj_index, const duk_number_list_entry *numbers);
  731. /*
  732. * Variable access
  733. */
  734. /* XXX: These calls are incomplete and not usable now. They are not (yet)
  735. * part of the public API.
  736. */
  737. DUK_EXTERNAL_DECL void duk_get_var(duk_context *ctx);
  738. DUK_EXTERNAL_DECL void duk_put_var(duk_context *ctx);
  739. DUK_EXTERNAL_DECL duk_bool_t duk_del_var(duk_context *ctx);
  740. DUK_EXTERNAL_DECL duk_bool_t duk_has_var(duk_context *ctx);
  741. /*
  742. * Object operations
  743. */
  744. DUK_EXTERNAL_DECL void duk_compact(duk_context *ctx, duk_idx_t obj_index);
  745. DUK_EXTERNAL_DECL void duk_enum(duk_context *ctx, duk_idx_t obj_index, duk_uint_t enum_flags);
  746. DUK_EXTERNAL_DECL duk_bool_t duk_next(duk_context *ctx, duk_idx_t enum_index, duk_bool_t get_value);
  747. /*
  748. * String manipulation
  749. */
  750. DUK_EXTERNAL_DECL void duk_concat(duk_context *ctx, duk_idx_t count);
  751. DUK_EXTERNAL_DECL void duk_join(duk_context *ctx, duk_idx_t count);
  752. DUK_EXTERNAL_DECL void duk_decode_string(duk_context *ctx, duk_idx_t index, duk_decode_char_function callback, void *udata);
  753. DUK_EXTERNAL_DECL void duk_map_string(duk_context *ctx, duk_idx_t index, duk_map_char_function callback, void *udata);
  754. DUK_EXTERNAL_DECL void duk_substring(duk_context *ctx, duk_idx_t index, duk_size_t start_char_offset, duk_size_t end_char_offset);
  755. DUK_EXTERNAL_DECL void duk_trim(duk_context *ctx, duk_idx_t index);
  756. DUK_EXTERNAL_DECL duk_codepoint_t duk_char_code_at(duk_context *ctx, duk_idx_t index, duk_size_t char_offset);
  757. /*
  758. * Ecmascript operators
  759. */
  760. DUK_EXTERNAL_DECL duk_bool_t duk_equals(duk_context *ctx, duk_idx_t index1, duk_idx_t index2);
  761. DUK_EXTERNAL_DECL duk_bool_t duk_strict_equals(duk_context *ctx, duk_idx_t index1, duk_idx_t index2);
  762. DUK_EXTERNAL_DECL duk_bool_t duk_instanceof(duk_context *ctx, duk_idx_t index1, duk_idx_t index2);
  763. /*
  764. * Function (method) calls
  765. */
  766. DUK_EXTERNAL_DECL void duk_call(duk_context *ctx, duk_idx_t nargs);
  767. DUK_EXTERNAL_DECL void duk_call_method(duk_context *ctx, duk_idx_t nargs);
  768. DUK_EXTERNAL_DECL void duk_call_prop(duk_context *ctx, duk_idx_t obj_index, duk_idx_t nargs);
  769. DUK_EXTERNAL_DECL duk_int_t duk_pcall(duk_context *ctx, duk_idx_t nargs);
  770. DUK_EXTERNAL_DECL duk_int_t duk_pcall_method(duk_context *ctx, duk_idx_t nargs);
  771. DUK_EXTERNAL_DECL duk_int_t duk_pcall_prop(duk_context *ctx, duk_idx_t obj_index, duk_idx_t nargs);
  772. DUK_EXTERNAL_DECL void duk_new(duk_context *ctx, duk_idx_t nargs);
  773. DUK_EXTERNAL_DECL duk_int_t duk_pnew(duk_context *ctx, duk_idx_t nargs);
  774. DUK_EXTERNAL_DECL duk_int_t duk_safe_call(duk_context *ctx, duk_safe_call_function func, duk_idx_t nargs, duk_idx_t nrets);
  775. /*
  776. * Thread management
  777. */
  778. /* There are currently no native functions to yield/resume, due to the internal
  779. * limitations on coroutine handling. These will be added later.
  780. */
  781. /*
  782. * Compilation and evaluation
  783. */
  784. DUK_EXTERNAL_DECL duk_int_t duk_eval_raw(duk_context *ctx, const char *src_buffer, duk_size_t src_length, duk_uint_t flags);
  785. DUK_EXTERNAL_DECL duk_int_t duk_compile_raw(duk_context *ctx, const char *src_buffer, duk_size_t src_length, duk_uint_t flags);
  786. /* plain */
  787. #define duk_eval(ctx) \
  788. ((void) duk_push_string((ctx), (const char *) (__FILE__)), \
  789. (void) duk_eval_raw((ctx), NULL, 0, DUK_COMPILE_EVAL))
  790. #define duk_eval_noresult(ctx) \
  791. ((void) duk_push_string((ctx), (const char *) (__FILE__)), \
  792. (void) duk_eval_raw((ctx), NULL, 0, DUK_COMPILE_EVAL | DUK_COMPILE_NORESULT))
  793. #define duk_peval(ctx) \
  794. ((void) duk_push_string((ctx), (const char *) (__FILE__)), \
  795. duk_eval_raw((ctx), NULL, 0, DUK_COMPILE_EVAL | DUK_COMPILE_SAFE))
  796. #define duk_peval_noresult(ctx) \
  797. ((void) duk_push_string((ctx), (const char *) (__FILE__)), \
  798. duk_eval_raw((ctx), NULL, 0, DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NORESULT))
  799. #define duk_compile(ctx,flags) \
  800. ((void) duk_compile_raw((ctx), NULL, 0, (flags)))
  801. #define duk_pcompile(ctx,flags) \
  802. (duk_compile_raw((ctx), NULL, 0, (flags) | DUK_COMPILE_SAFE))
  803. /* string */
  804. #define duk_eval_string(ctx,src) \
  805. ((void) duk_push_string((ctx), (const char *) (__FILE__)), \
  806. (void) duk_eval_raw((ctx), (src), 0, DUK_COMPILE_EVAL | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN))
  807. #define duk_eval_string_noresult(ctx,src) \
  808. ((void) duk_push_string((ctx), (const char *) (__FILE__)), \
  809. (void) duk_eval_raw((ctx), (src), 0, DUK_COMPILE_EVAL | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN | DUK_COMPILE_NORESULT))
  810. #define duk_peval_string(ctx,src) \
  811. ((void) duk_push_string((ctx), (const char *) (__FILE__)), \
  812. duk_eval_raw((ctx), (src), 0, DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN))
  813. #define duk_peval_string_noresult(ctx,src) \
  814. ((void) duk_push_string((ctx), (const char *) (__FILE__)), \
  815. duk_eval_raw((ctx), (src), 0, DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN | DUK_COMPILE_NORESULT))
  816. #define duk_compile_string(ctx,flags,src) \
  817. ((void) duk_push_string((ctx), (const char *) (__FILE__)), \
  818. (void) duk_compile_raw((ctx), (src), 0, (flags) | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN))
  819. #define duk_compile_string_filename(ctx,flags,src) \
  820. ((void) duk_compile_raw((ctx), (src), 0, (flags) | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN))
  821. #define duk_pcompile_string(ctx,flags,src) \
  822. ((void) duk_push_string((ctx), (const char *) (__FILE__)), \
  823. duk_compile_raw((ctx), (src), 0, (flags) | DUK_COMPILE_SAFE | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN))
  824. #define duk_pcompile_string_filename(ctx,flags,src) \
  825. (duk_compile_raw((ctx), (src), 0, (flags) | DUK_COMPILE_SAFE | DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN))
  826. /* lstring */
  827. #define duk_eval_lstring(ctx,buf,len) \
  828. ((void) duk_push_string((ctx), (const char *) (__FILE__)), \
  829. (void) duk_eval_raw((ctx), buf, len, DUK_COMPILE_EVAL | DUK_COMPILE_NOSOURCE))
  830. #define duk_eval_lstring_noresult(ctx,buf,len) \
  831. ((void) duk_push_string((ctx), (const char *) (__FILE__)), \
  832. (void) duk_eval_raw((ctx), buf, len, DUK_COMPILE_EVAL | DUK_COMPILE_NOSOURCE | DUK_COMPILE_NORESULT))
  833. #define duk_peval_lstring(ctx,buf,len) \
  834. ((void) duk_push_string((ctx), (const char *) (__FILE__)), \
  835. duk_eval_raw((ctx), buf, len, DUK_COMPILE_EVAL | DUK_COMPILE_NOSOURCE | DUK_COMPILE_SAFE))
  836. #define duk_peval_lstring_noresult(ctx,buf,len) \
  837. ((void) duk_push_string((ctx), (const char *) (__FILE__)), \
  838. duk_eval_raw((ctx), buf, len, DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NOSOURCE | DUK_COMPILE_NORESULT))
  839. #define duk_compile_lstring(ctx,flags,buf,len) \
  840. ((void) duk_push_string((ctx), (const char *) (__FILE__)), \
  841. (void) duk_compile_raw((ctx), buf, len, (flags) | DUK_COMPILE_NOSOURCE))
  842. #define duk_compile_lstring_filename(ctx,flags,buf,len) \
  843. ((void) duk_compile_raw((ctx), buf, len, (flags) | DUK_COMPILE_NOSOURCE))
  844. #define duk_pcompile_lstring(ctx,flags,buf,len) \
  845. ((void) duk_push_string((ctx), (const char *) (__FILE__)), \
  846. duk_compile_raw((ctx), buf, len, (flags) | DUK_COMPILE_SAFE | DUK_COMPILE_NOSOURCE))
  847. #define duk_pcompile_lstring_filename(ctx,flags,buf,len) \
  848. (duk_compile_raw((ctx), buf, len, (flags) | DUK_COMPILE_SAFE | DUK_COMPILE_NOSOURCE))
  849. /* file */
  850. #define duk_eval_file(ctx,path) \
  851. ((void) duk_push_string_file_raw((ctx), (path), 0), \
  852. (void) duk_push_string((ctx), (path)), \
  853. (void) duk_eval_raw((ctx), NULL, 0, DUK_COMPILE_EVAL))
  854. #define duk_eval_file_noresult(ctx,path) \
  855. ((void) duk_push_string_file_raw((ctx), (path), 0), \
  856. (void) duk_push_string((ctx), (path)), \
  857. (void) duk_eval_raw((ctx), NULL, 0, DUK_COMPILE_EVAL | DUK_COMPILE_NORESULT))
  858. #define duk_peval_file(ctx,path) \
  859. ((void) duk_push_string_file_raw((ctx), (path), DUK_STRING_PUSH_SAFE), \
  860. (void) duk_push_string((ctx), (path)), \
  861. duk_eval_raw((ctx), NULL, 0, DUK_COMPILE_EVAL | DUK_COMPILE_SAFE))
  862. #define duk_peval_file_noresult(ctx,path) \
  863. ((void) duk_push_string_file_raw((ctx), (path), DUK_STRING_PUSH_SAFE), \
  864. (void) duk_push_string((ctx), (path)), \
  865. duk_eval_raw((ctx), NULL, 0, DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NORESULT))
  866. #define duk_compile_file(ctx,flags,path) \
  867. ((void) duk_push_string_file_raw((ctx), (path), 0), \
  868. (void) duk_push_string((ctx), (path)), \
  869. (void) duk_compile_raw((ctx), NULL, 0, (flags)))
  870. #define duk_pcompile_file(ctx,flags,path) \
  871. ((void) duk_push_string_file_raw((ctx), (path), DUK_STRING_PUSH_SAFE), \
  872. (void) duk_push_string((ctx), (path)), \
  873. duk_compile_raw((ctx), NULL, 0, (flags) | DUK_COMPILE_SAFE))
  874. /*
  875. * Bytecode load/dump
  876. */
  877. DUK_EXTERNAL_DECL void duk_dump_function(duk_context *ctx);
  878. DUK_EXTERNAL_DECL void duk_load_function(duk_context *ctx);
  879. /*
  880. * Logging
  881. */
  882. DUK_EXTERNAL_DECL void duk_log(duk_context *ctx, duk_int_t level, const char *fmt, ...);
  883. DUK_EXTERNAL_DECL void duk_log_va(duk_context *ctx, duk_int_t level, const char *fmt, va_list ap);
  884. /*
  885. * Debugging
  886. */
  887. DUK_EXTERNAL_DECL void duk_push_context_dump(duk_context *ctx);
  888. #if defined(DUK_USE_FILE_IO)
  889. /* internal use */
  890. #define duk_dump_context_filehandle(ctx,fh) \
  891. (duk_push_context_dump((ctx)), \
  892. DUK_FPRINTF((fh), "%s\n", duk_safe_to_string(ctx, -1)), \
  893. duk_pop(ctx))
  894. /* external use */
  895. #define duk_dump_context_stdout(ctx) \
  896. duk_dump_context_filehandle((ctx), DUK_STDOUT)
  897. #define duk_dump_context_stderr(ctx) \
  898. duk_dump_context_filehandle((ctx), DUK_STDERR)
  899. #else /* DUK_USE_FILE_IO */
  900. #define duk_dump_context_stdout(ctx) ((void) 0)
  901. #define duk_dump_context_stderr(ctx) ((void) 0)
  902. #endif /* DUK_USE_FILE_IO */
  903. /*
  904. * Debugger (debug protocol)
  905. */
  906. DUK_EXTERNAL_DECL void duk_debugger_attach(duk_context *ctx,
  907. duk_debug_read_function read_cb,
  908. duk_debug_write_function write_cb,
  909. duk_debug_peek_function peek_cb,
  910. duk_debug_read_flush_function read_flush_cb,
  911. duk_debug_write_flush_function write_flush_cb,
  912. duk_debug_detached_function detached_cb,
  913. void *udata);
  914. DUK_EXTERNAL_DECL void duk_debugger_detach(duk_context *ctx);
  915. DUK_EXTERNAL_DECL void duk_debugger_cooperate(duk_context *ctx);
  916. /*
  917. * Date provider related constants
  918. *
  919. * NOTE: These are "semi public" - you should only use these if you write
  920. * your own platform specific Date provider, see doc/datetime.rst.
  921. */
  922. /* Millisecond count constants. */
  923. #define DUK_DATE_MSEC_SECOND 1000L
  924. #define DUK_DATE_MSEC_MINUTE (60L * 1000L)
  925. #define DUK_DATE_MSEC_HOUR (60L * 60L * 1000L)
  926. #define DUK_DATE_MSEC_DAY (24L * 60L * 60L * 1000L)
  927. /* Ecmascript date range is 100 million days from Epoch:
  928. * > 100e6 * 24 * 60 * 60 * 1000 // 100M days in millisecs
  929. * 8640000000000000
  930. * (= 8.64e15)
  931. */
  932. #define DUK_DATE_MSEC_100M_DAYS (8.64e15)
  933. #define DUK_DATE_MSEC_100M_DAYS_LEEWAY (8.64e15 + 24 * 3600e3)
  934. /* Ecmascript year range:
  935. * > new Date(100e6 * 24 * 3600e3).toISOString()
  936. * '+275760-09-13T00:00:00.000Z'
  937. * > new Date(-100e6 * 24 * 3600e3).toISOString()
  938. * '-271821-04-20T00:00:00.000Z'
  939. */
  940. #define DUK_DATE_MIN_ECMA_YEAR (-271821L)
  941. #define DUK_DATE_MAX_ECMA_YEAR 275760L
  942. /* Part indices for internal breakdowns. Part order from DUK_DATE_IDX_YEAR
  943. * to DUK_DATE_IDX_MILLISECOND matches argument ordering of Ecmascript API
  944. * calls (like Date constructor call). Some functions in duk_bi_date.c
  945. * depend on the specific ordering, so change with care. 16 bits are not
  946. * enough for all parts (year, specifically).
  947. *
  948. * (Must be in-sync with genbuiltins.py.)
  949. */
  950. #define DUK_DATE_IDX_YEAR 0 /* year */
  951. #define DUK_DATE_IDX_MONTH 1 /* month: 0 to 11 */
  952. #define DUK_DATE_IDX_DAY 2 /* day within month: 0 to 30 */
  953. #define DUK_DATE_IDX_HOUR 3
  954. #define DUK_DATE_IDX_MINUTE 4
  955. #define DUK_DATE_IDX_SECOND 5
  956. #define DUK_DATE_IDX_MILLISECOND 6
  957. #define DUK_DATE_IDX_WEEKDAY 7 /* weekday: 0 to 6, 0=sunday, 1=monday, etc */
  958. #define DUK_DATE_IDX_NUM_PARTS 8
  959. /* Internal API call flags, used for various functions in this file.
  960. * Certain flags are used by only certain functions, but since the flags
  961. * don't overlap, a single flags value can be passed around to multiple
  962. * functions.
  963. *
  964. * The unused top bits of the flags field are also used to pass values
  965. * to helpers (duk__get_part_helper() and duk__set_part_helper()).
  966. *
  967. * (Must be in-sync with genbuiltins.py.)
  968. */
  969. /* NOTE: when writing a Date provider you only need a few specific
  970. * flags from here, the rest are internal. Avoid using anything you
  971. * don't need.
  972. */
  973. #define DUK_DATE_FLAG_NAN_TO_ZERO (1 << 0) /* timeval breakdown: internal time value NaN -> zero */
  974. #define DUK_DATE_FLAG_NAN_TO_RANGE_ERROR (1 << 1) /* timeval breakdown: internal time value NaN -> RangeError (toISOString) */
  975. #define DUK_DATE_FLAG_ONEBASED (1 << 2) /* timeval breakdown: convert month and day-of-month parts to one-based (default is zero-based) */
  976. #define DUK_DATE_FLAG_EQUIVYEAR (1 << 3) /* timeval breakdown: replace year with equivalent year in the [1971,2037] range for DST calculations */
  977. #define DUK_DATE_FLAG_LOCALTIME (1 << 4) /* convert time value to local time */
  978. #define DUK_DATE_FLAG_SUB1900 (1 << 5) /* getter: subtract 1900 from year when getting year part */
  979. #define DUK_DATE_FLAG_TOSTRING_DATE (1 << 6) /* include date part in string conversion result */
  980. #define DUK_DATE_FLAG_TOSTRING_TIME (1 << 7) /* include time part in string conversion result */
  981. #define DUK_DATE_FLAG_TOSTRING_LOCALE (1 << 8) /* use locale specific formatting if available */
  982. #define DUK_DATE_FLAG_TIMESETTER (1 << 9) /* setter: call is a time setter (affects hour, min, sec, ms); otherwise date setter (affects year, month, day-in-month) */
  983. #define DUK_DATE_FLAG_YEAR_FIXUP (1 << 10) /* setter: perform 2-digit year fixup (00...99 -> 1900...1999) */
  984. #define DUK_DATE_FLAG_SEP_T (1 << 11) /* string conversion: use 'T' instead of ' ' as a separator */
  985. #define DUK_DATE_FLAG_VALUE_SHIFT 12 /* additional values begin at bit 12 */
  986. /*
  987. * C++ name mangling
  988. */
  989. #ifdef __cplusplus
  990. /* end 'extern "C"' wrapper */
  991. }
  992. #endif
  993. #endif /* DUK_API_PUBLIC_H_INCLUDED */
  994. /*
  995. * END PUBLIC API
  996. */
  997. /*
  998. * Union to access IEEE double memory representation, indexes for double
  999. * memory representation, and some macros for double manipulation.
  1000. *
  1001. * Also used by packed duk_tval. Use a union for bit manipulation to
  1002. * minimize aliasing issues in practice. The C99 standard does not
  1003. * guarantee that this should work, but it's a very widely supported
  1004. * practice for low level manipulation.
  1005. *
  1006. * IEEE double format summary:
  1007. *
  1008. * seeeeeee eeeeffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
  1009. * A B C D E F G H
  1010. *
  1011. * s sign bit
  1012. * eee... exponent field
  1013. * fff... fraction
  1014. *
  1015. * See http://en.wikipedia.org/wiki/Double_precision_floating-point_format.
  1016. *
  1017. * NaNs are represented as exponent 0x7ff and mantissa != 0. The NaN is a
  1018. * signaling NaN when the highest bit of the mantissa is zero, and a quiet
  1019. * NaN when the highest bit is set.
  1020. *
  1021. * At least three memory layouts are relevant here:
  1022. *
  1023. * A B C D E F G H Big endian (e.g. 68k) DUK_USE_DOUBLE_BE
  1024. * H G F E D C B A Little endian (e.g. x86) DUK_USE_DOUBLE_LE
  1025. * D C B A H G F E Mixed/cross endian (e.g. ARM) DUK_USE_DOUBLE_ME
  1026. *
  1027. * ARM is a special case: ARM double values are in mixed/cross endian
  1028. * format while ARM duk_uint64_t values are in standard little endian
  1029. * format (H G F E D C B A). When a double is read as a duk_uint64_t
  1030. * from memory, the register will contain the (logical) value
  1031. * E F G H A B C D. This requires some special handling below.
  1032. *
  1033. * Indexes of various types (8-bit, 16-bit, 32-bit) in memory relative to
  1034. * the logical (big endian) order:
  1035. *
  1036. * byte order duk_uint8_t duk_uint16_t duk_uint32_t
  1037. * BE 01234567 0123 01
  1038. * LE 76543210 3210 10
  1039. * ME (ARM) 32107654 1032 01
  1040. *
  1041. * Some processors may alter NaN values in a floating point load+store.
  1042. * For instance, on X86 a FLD + FSTP may convert a signaling NaN to a
  1043. * quiet one. This is catastrophic when NaN space is used in packed
  1044. * duk_tval values. See: misc/clang_aliasing.c.
  1045. */
  1046. #ifndef DUK_DBLUNION_H_INCLUDED
  1047. #define DUK_DBLUNION_H_INCLUDED
  1048. /*
  1049. * Union for accessing double parts, also serves as packed duk_tval
  1050. */
  1051. union duk_double_union {
  1052. double d;
  1053. float f[2];
  1054. #if defined(DUK_USE_64BIT_OPS)
  1055. duk_uint64_t ull[1];
  1056. #endif
  1057. duk_uint32_t ui[2];
  1058. duk_uint16_t us[4];
  1059. duk_uint8_t uc[8];
  1060. #if defined(DUK_USE_PACKED_TVAL)
  1061. void *vp[2]; /* used by packed duk_tval, assumes sizeof(void *) == 4 */
  1062. #endif
  1063. };
  1064. typedef union duk_double_union duk_double_union;
  1065. /*
  1066. * Indexes of various types with respect to big endian (logical) layout
  1067. */
  1068. #if defined(DUK_USE_DOUBLE_LE)
  1069. #ifdef DUK_USE_64BIT_OPS
  1070. #define DUK_DBL_IDX_ULL0 0
  1071. #endif
  1072. #define DUK_DBL_IDX_UI0 1
  1073. #define DUK_DBL_IDX_UI1 0
  1074. #define DUK_DBL_IDX_US0 3
  1075. #define DUK_DBL_IDX_US1 2
  1076. #define DUK_DBL_IDX_US2 1
  1077. #define DUK_DBL_IDX_US3 0
  1078. #define DUK_DBL_IDX_UC0 7
  1079. #define DUK_DBL_IDX_UC1 6
  1080. #define DUK_DBL_IDX_UC2 5
  1081. #define DUK_DBL_IDX_UC3 4
  1082. #define DUK_DBL_IDX_UC4 3
  1083. #define DUK_DBL_IDX_UC5 2
  1084. #define DUK_DBL_IDX_UC6 1
  1085. #define DUK_DBL_IDX_UC7 0
  1086. #define DUK_DBL_IDX_VP0 DUK_DBL_IDX_UI0 /* packed tval */
  1087. #define DUK_DBL_IDX_VP1 DUK_DBL_IDX_UI1 /* packed tval */
  1088. #elif defined(DUK_USE_DOUBLE_BE)
  1089. #ifdef DUK_USE_64BIT_OPS
  1090. #define DUK_DBL_IDX_ULL0 0
  1091. #endif
  1092. #define DUK_DBL_IDX_UI0 0
  1093. #define DUK_DBL_IDX_UI1 1
  1094. #define DUK_DBL_IDX_US0 0
  1095. #define DUK_DBL_IDX_US1 1
  1096. #define DUK_DBL_IDX_US2 2
  1097. #define DUK_DBL_IDX_US3 3
  1098. #define DUK_DBL_IDX_UC0 0
  1099. #define DUK_DBL_IDX_UC1 1
  1100. #define DUK_DBL_IDX_UC2 2
  1101. #define DUK_DBL_IDX_UC3 3
  1102. #define DUK_DBL_IDX_UC4 4
  1103. #define DUK_DBL_IDX_UC5 5
  1104. #define DUK_DBL_IDX_UC6 6
  1105. #define DUK_DBL_IDX_UC7 7
  1106. #define DUK_DBL_IDX_VP0 DUK_DBL_IDX_UI0 /* packed tval */
  1107. #define DUK_DBL_IDX_VP1 DUK_DBL_IDX_UI1 /* packed tval */
  1108. #elif defined(DUK_USE_DOUBLE_ME)
  1109. #ifdef DUK_USE_64BIT_OPS
  1110. #define DUK_DBL_IDX_ULL0 0 /* not directly applicable, byte order differs from a double */
  1111. #endif
  1112. #define DUK_DBL_IDX_UI0 0
  1113. #define DUK_DBL_IDX_UI1 1
  1114. #define DUK_DBL_IDX_US0 1
  1115. #define DUK_DBL_IDX_US1 0
  1116. #define DUK_DBL_IDX_US2 3
  1117. #define DUK_DBL_IDX_US3 2
  1118. #define DUK_DBL_IDX_UC0 3
  1119. #define DUK_DBL_IDX_UC1 2
  1120. #define DUK_DBL_IDX_UC2 1
  1121. #define DUK_DBL_IDX_UC3 0
  1122. #define DUK_DBL_IDX_UC4 7
  1123. #define DUK_DBL_IDX_UC5 6
  1124. #define DUK_DBL_IDX_UC6 5
  1125. #define DUK_DBL_IDX_UC7 4
  1126. #define DUK_DBL_IDX_VP0 DUK_DBL_IDX_UI0 /* packed tval */
  1127. #define DUK_DBL_IDX_VP1 DUK_DBL_IDX_UI1 /* packed tval */
  1128. #else
  1129. #error internal error
  1130. #endif
  1131. /*
  1132. * Helper macros for reading/writing memory representation parts, used
  1133. * by duk_numconv.c and duk_tval.h.
  1134. */
  1135. #define DUK_DBLUNION_SET_DOUBLE(u,v) do { \
  1136. (u)->d = (v); \
  1137. } while (0)
  1138. #define DUK_DBLUNION_SET_HIGH32(u,v) do { \
  1139. (u)->ui[DUK_DBL_IDX_UI0] = (duk_uint32_t) (v); \
  1140. } while (0)
  1141. #ifdef DUK_USE_64BIT_OPS
  1142. #ifdef DUK_USE_DOUBLE_ME
  1143. #define DUK_DBLUNION_SET_HIGH32_ZERO_LOW32(u,v) do { \
  1144. (u)->ull[DUK_DBL_IDX_ULL0] = (duk_uint64_t) (v); \
  1145. } while (0)
  1146. #else
  1147. #define DUK_DBLUNION_SET_HIGH32_ZERO_LOW32(u,v) do { \
  1148. (u)->ull[DUK_DBL_IDX_ULL0] = ((duk_uint64_t) (v)) << 32; \
  1149. } while (0)
  1150. #endif
  1151. #else /* DUK_USE_64BIT_OPS */
  1152. #define DUK_DBLUNION_SET_HIGH32_ZERO_LOW32(u,v) do { \
  1153. (u)->ui[DUK_DBL_IDX_UI0] = (duk_uint32_t) (v); \
  1154. (u)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) 0; \
  1155. } while (0)
  1156. #endif /* DUK_USE_64BIT_OPS */
  1157. #define DUK_DBLUNION_SET_LOW32(u,v) do { \
  1158. (u)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (v); \
  1159. } while (0)
  1160. #define DUK_DBLUNION_GET_DOUBLE(u) ((u)->d)
  1161. #define DUK_DBLUNION_GET_HIGH32(u) ((u)->ui[DUK_DBL_IDX_UI0])
  1162. #define DUK_DBLUNION_GET_LOW32(u) ((u)->ui[DUK_DBL_IDX_UI1])
  1163. #ifdef DUK_USE_64BIT_OPS
  1164. #ifdef DUK_USE_DOUBLE_ME
  1165. #define DUK_DBLUNION_SET_UINT64(u,v) do { \
  1166. (u)->ui[DUK_DBL_IDX_UI0] = (duk_uint32_t) ((v) >> 32); \
  1167. (u)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (v); \
  1168. } while (0)
  1169. #define DUK_DBLUNION_GET_UINT64(u) \
  1170. ((((duk_uint64_t) (u)->ui[DUK_DBL_IDX_UI0]) << 32) | \
  1171. ((duk_uint64_t) (u)->ui[DUK_DBL_IDX_UI1]))
  1172. #else
  1173. #define DUK_DBLUNION_SET_UINT64(u,v) do { \
  1174. (u)->ull[DUK_DBL_IDX_ULL0] = (duk_uint64_t) (v); \
  1175. } while (0)
  1176. #define DUK_DBLUNION_GET_UINT64(u) ((u)->ull[DUK_DBL_IDX_ULL0])
  1177. #endif
  1178. #define DUK_DBLUNION_SET_INT64(u,v) DUK_DBLUNION_SET_UINT64((u), (duk_uint64_t) (v))
  1179. #define DUK_DBLUNION_GET_INT64(u) ((duk_int64_t) DUK_DBLUNION_GET_UINT64((u)))
  1180. #endif /* DUK_USE_64BIT_OPS */
  1181. /*
  1182. * Double NaN manipulation macros related to NaN normalization needed when
  1183. * using the packed duk_tval representation. NaN normalization is necessary
  1184. * to keep double values compatible with the duk_tval format.
  1185. *
  1186. * When packed duk_tval is used, the NaN space is used to store pointers
  1187. * and other tagged values in addition to NaNs. Actual NaNs are normalized
  1188. * to a specific quiet NaN. The macros below are used by the implementation
  1189. * to check and normalize NaN values when they might be created. The macros
  1190. * are essentially NOPs when the non-packed duk_tval representation is used.
  1191. *
  1192. * A FULL check is exact and checks all bits. A NOTFULL check is used by
  1193. * the packed duk_tval and works correctly for all NaNs except those that
  1194. * begin with 0x7ff0. Since the 'normalized NaN' values used with packed
  1195. * duk_tval begin with 0x7ff8, the partial check is reliable when packed
  1196. * duk_tval is used. The 0x7ff8 prefix means the normalized NaN will be a
  1197. * quiet NaN regardless of its remaining lower bits.
  1198. *
  1199. * The ME variant below is specifically for ARM byte order, which has the
  1200. * feature that while doubles have a mixed byte order (32107654), unsigned
  1201. * long long values has a little endian byte order (76543210). When writing
  1202. * a logical double value through a ULL pointer, the 32-bit words need to be
  1203. * swapped; hence the #ifdefs below for ULL writes with DUK_USE_DOUBLE_ME.
  1204. * This is not full ARM support but suffices for some environments.
  1205. */
  1206. #ifdef DUK_USE_64BIT_OPS
  1207. #ifdef DUK_USE_DOUBLE_ME
  1208. #define DUK__DBLUNION_SET_NAN_FULL(u) do { \
  1209. (u)->ull[DUK_DBL_IDX_ULL0] = 0x000000007ff80000ULL; \
  1210. } while (0)
  1211. #else
  1212. #define DUK__DBLUNION_SET_NAN_FULL(u) do { \
  1213. (u)->ull[DUK_DBL_IDX_ULL0] = 0x7ff8000000000000ULL; \
  1214. } while (0)
  1215. #endif
  1216. #else /* DUK_USE_64BIT_OPS */
  1217. #define DUK__DBLUNION_SET_NAN_FULL(u) do { \
  1218. (u)->ui[DUK_DBL_IDX_UI0] = (duk_uint32_t) 0x7ff80000UL; \
  1219. (u)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) 0x00000000UL; \
  1220. } while (0)
  1221. #endif /* DUK_USE_64BIT_OPS */
  1222. #define DUK__DBLUNION_SET_NAN_NOTFULL(u) do { \
  1223. (u)->us[DUK_DBL_IDX_US0] = 0x7ff8UL; \
  1224. } while (0)
  1225. #ifdef DUK_USE_64BIT_OPS
  1226. #ifdef DUK_USE_DOUBLE_ME
  1227. #define DUK__DBLUNION_IS_NAN_FULL(u) \
  1228. /* E == 0x7ff, F != 0 => NaN */ \
  1229. ((((u)->us[DUK_DBL_IDX_US0] & 0x7ff0UL) == 0x7ff0UL) && \
  1230. ((((u)->ull[DUK_DBL_IDX_ULL0]) & 0xffffffff000fffffULL) != 0))
  1231. #else
  1232. #define DUK__DBLUNION_IS_NAN_FULL(u) \
  1233. /* E == 0x7ff, F != 0 => NaN */ \
  1234. ((((u)->us[DUK_DBL_IDX_US0] & 0x7ff0UL) == 0x7ff0UL) && \
  1235. ((((u)->ull[DUK_DBL_IDX_ULL0]) & 0x000fffffffffffffULL) != 0))
  1236. #endif
  1237. #else /* DUK_USE_64BIT_OPS */
  1238. #define DUK__DBLUNION_IS_NAN_FULL(u) \
  1239. /* E == 0x7ff, F != 0 => NaN */ \
  1240. ((((u)->ui[DUK_DBL_IDX_UI0] & 0x7ff00000UL) == 0x7ff00000UL) && \
  1241. (((u)->ui[DUK_DBL_IDX_UI0] & 0x000fffffUL) != 0 || \
  1242. (u)->ui[DUK_DBL_IDX_UI1] != 0))
  1243. #endif /* DUK_USE_64BIT_OPS */
  1244. #define DUK__DBLUNION_IS_NAN_NOTFULL(u) \
  1245. /* E == 0x7ff, topmost four bits of F != 0 => assume NaN */ \
  1246. ((((u)->us[DUK_DBL_IDX_US0] & 0x7ff0UL) == 0x7ff0UL) && \
  1247. (((u)->us[DUK_DBL_IDX_US0] & 0x000fUL) != 0x0000UL))
  1248. #ifdef DUK_USE_64BIT_OPS
  1249. #ifdef DUK_USE_DOUBLE_ME
  1250. #define DUK__DBLUNION_IS_NORMALIZED_NAN_FULL(u) \
  1251. ((u)->ull[DUK_DBL_IDX_ULL0] == 0x000000007ff80000ULL)
  1252. #else
  1253. #define DUK__DBLUNION_IS_NORMALIZED_NAN_FULL(u) \
  1254. ((u)->ull[DUK_DBL_IDX_ULL0] == 0x7ff8000000000000ULL)
  1255. #endif
  1256. #else /* DUK_USE_64BIT_OPS */
  1257. #define DUK__DBLUNION_IS_NORMALIZED_NAN_FULL(u) \
  1258. (((u)->ui[DUK_DBL_IDX_UI0] == 0x7ff80000UL) && \
  1259. ((u)->ui[DUK_DBL_IDX_UI1] == 0x00000000UL))
  1260. #endif /* DUK_USE_64BIT_OPS */
  1261. #define DUK__DBLUNION_IS_NORMALIZED_NAN_NOTFULL(u) \
  1262. /* E == 0x7ff, F == 8 => normalized NaN */ \
  1263. ((u)->us[DUK_DBL_IDX_US0] == 0x7ff8UL)
  1264. #define DUK__DBLUNION_NORMALIZE_NAN_CHECK_FULL(u) do { \
  1265. if (DUK__DBLUNION_IS_NAN_FULL((u))) { \
  1266. DUK__DBLUNION_SET_NAN_FULL((u)); \
  1267. } \
  1268. } while (0)
  1269. #define DUK__DBLUNION_NORMALIZE_NAN_CHECK_NOTFULL(u) do { \
  1270. if (DUK__DBLUNION_IS_NAN_NOTFULL((u))) { \
  1271. DUK__DBLUNION_SET_NAN_NOTFULL((u)); \
  1272. } \
  1273. } while (0)
  1274. /* Concrete macros for NaN handling used by the implementation internals.
  1275. * Chosen so that they match the duk_tval representation: with a packed
  1276. * duk_tval, ensure NaNs are properly normalized; with a non-packed duk_tval
  1277. * these are essentially NOPs.
  1278. */
  1279. #if defined(DUK_USE_PACKED_TVAL)
  1280. #if defined(DUK_USE_FULL_TVAL)
  1281. #define DUK_DBLUNION_NORMALIZE_NAN_CHECK(u) DUK__DBLUNION_NORMALIZE_NAN_CHECK_FULL((u))
  1282. #define DUK_DBLUNION_IS_NAN(u) DUK__DBLUNION_IS_NAN_FULL((u))
  1283. #define DUK_DBLUNION_IS_NORMALIZED_NAN(u) DUK__DBLUNION_IS_NORMALIZED_NAN_FULL((u))
  1284. #define DUK_DBLUNION_SET_NAN(d) DUK__DBLUNION_SET_NAN_FULL((d))
  1285. #else
  1286. #define DUK_DBLUNION_NORMALIZE_NAN_CHECK(u) DUK__DBLUNION_NORMALIZE_NAN_CHECK_NOTFULL((u))
  1287. #define DUK_DBLUNION_IS_NAN(u) DUK__DBLUNION_IS_NAN_NOTFULL((u))
  1288. #define DUK_DBLUNION_IS_NORMALIZED_NAN(u) DUK__DBLUNION_IS_NORMALIZED_NAN_NOTFULL((u))
  1289. #define DUK_DBLUNION_SET_NAN(d) DUK__DBLUNION_SET_NAN_NOTFULL((d))
  1290. #endif
  1291. #define DUK_DBLUNION_IS_NORMALIZED(u) \
  1292. (!DUK_DBLUNION_IS_NAN((u)) || /* either not a NaN */ \
  1293. DUK_DBLUNION_IS_NORMALIZED_NAN((u))) /* or is a normalized NaN */
  1294. #else /* DUK_USE_PACKED_TVAL */
  1295. #define DUK_DBLUNION_NORMALIZE_NAN_CHECK(u) /* nop: no need to normalize */
  1296. #define DUK_DBLUNION_IS_NAN(u) (DUK_ISNAN((u)->d))
  1297. #define DUK_DBLUNION_IS_NORMALIZED_NAN(u) (DUK_ISNAN((u)->d))
  1298. #define DUK_DBLUNION_IS_NORMALIZED(u) 1 /* all doubles are considered normalized */
  1299. #define DUK_DBLUNION_SET_NAN(u) do { \
  1300. /* in non-packed representation we don't care about which NaN is used */ \
  1301. (u)->d = DUK_DOUBLE_NAN; \
  1302. } while (0)
  1303. #endif /* DUK_USE_PACKED_TVAL */
  1304. /* XXX: native 64-bit byteswaps when available */
  1305. /* 64-bit byteswap, same operation independent of target endianness. */
  1306. #define DUK_DBLUNION_BSWAP64(u) do { \
  1307. duk_uint32_t duk__bswaptmp1, duk__bswaptmp2; \
  1308. duk__bswaptmp1 = (u)->ui[0]; \
  1309. duk__bswaptmp2 = (u)->ui[1]; \
  1310. duk__bswaptmp1 = DUK_BSWAP32(duk__bswaptmp1); \
  1311. duk__bswaptmp2 = DUK_BSWAP32(duk__bswaptmp2); \
  1312. (u)->ui[0] = duk__bswaptmp2; \
  1313. (u)->ui[1] = duk__bswaptmp1; \
  1314. } while (0)
  1315. /* Byteswap an IEEE double in the duk_double_union from host to network
  1316. * order. For a big endian target this is a no-op.
  1317. */
  1318. #if defined(DUK_USE_DOUBLE_LE)
  1319. #define DUK_DBLUNION_DOUBLE_HTON(u) do { \
  1320. duk_uint32_t duk__bswaptmp1, duk__bswaptmp2; \
  1321. duk__bswaptmp1 = (u)->ui[0]; \
  1322. duk__bswaptmp2 = (u)->ui[1]; \
  1323. duk__bswaptmp1 = DUK_BSWAP32(duk__bswaptmp1); \
  1324. duk__bswaptmp2 = DUK_BSWAP32(duk__bswaptmp2); \
  1325. (u)->ui[0] = duk__bswaptmp2; \
  1326. (u)->ui[1] = duk__bswaptmp1; \
  1327. } while (0)
  1328. #elif defined(DUK_USE_DOUBLE_ME)
  1329. #define DUK_DBLUNION_DOUBLE_HTON(u) do { \
  1330. duk_uint32_t duk__bswaptmp1, duk__bswaptmp2; \
  1331. duk__bswaptmp1 = (u)->ui[0]; \
  1332. duk__bswaptmp2 = (u)->ui[1]; \
  1333. duk__bswaptmp1 = DUK_BSWAP32(duk__bswaptmp1); \
  1334. duk__bswaptmp2 = DUK_BSWAP32(duk__bswaptmp2); \
  1335. (u)->ui[0] = duk__bswaptmp1; \
  1336. (u)->ui[1] = duk__bswaptmp2; \
  1337. } while (0)
  1338. #elif defined(DUK_USE_DOUBLE_BE)
  1339. #define DUK_DBLUNION_DOUBLE_HTON(u) do { } while (0)
  1340. #else
  1341. #error internal error, double endianness insane
  1342. #endif
  1343. /* Reverse operation is the same. */
  1344. #define DUK_DBLUNION_DOUBLE_NTOH(u) DUK_DBLUNION_DOUBLE_HTON((u))
  1345. #endif /* DUK_DBLUNION_H_INCLUDED */
  1346. #endif /* DUKTAPE_H_INCLUDED */