duktape.h 77 KB

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