duktape.h 67 KB

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