duk_config.h 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284
  1. /*
  2. * Determine platform features, select feature selection defines
  3. * (e.g. _XOPEN_SOURCE), include system headers, and define DUK_USE_xxx
  4. * defines which are (only) checked in Duktape internal code for
  5. * activated features. Duktape feature selection is based on automatic
  6. * feature detection, user supplied DUK_OPT_xxx defines, and optionally
  7. * a "duk_custom.h" user header (if DUK_OPT_HAVE_CUSTOM_H is defined).
  8. *
  9. * When compiling Duktape, DUK_COMPILING_DUKTAPE is set, and this file
  10. * is included before any system headers are included. Feature selection
  11. * defines (e.g. _XOPEN_SOURCE) are defined here before any system headers
  12. * are included (which is a requirement for system headers to work correctly).
  13. * This file is responsible for including all system headers and contains
  14. * all platform dependent cruft in general. When compiling user code,
  15. * DUK_COMPILING_DUKTAPE is not defined, and we must avoid e.g. defining
  16. * unnecessary feature selection defines.
  17. *
  18. * The general order of handling:
  19. * - Compiler feature detection (require no includes)
  20. * - Intermediate platform detection (-> easier platform defines)
  21. * - Platform detection, system includes, byte order detection, etc
  22. * - ANSI C wrappers (e.g. DUK_MEMCMP), wrappers for constants, etc
  23. * - DUK_USE_xxx defines are resolved based on input defines
  24. * - Duktape Date provider settings
  25. * - Final sanity checks
  26. *
  27. * DUK_F_xxx are internal feature detection macros which should not be
  28. * used outside this header.
  29. *
  30. * Useful resources:
  31. *
  32. * http://sourceforge.net/p/predef/wiki/Home/
  33. * http://sourceforge.net/p/predef/wiki/Architectures/
  34. * http://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor
  35. * http://en.wikipedia.org/wiki/C_data_types#Fixed-width_integer_types
  36. *
  37. * Preprocessor defines available in a particular GCC:
  38. *
  39. * gcc -dM -E - </dev/null # http://www.brain-dump.org/blog/entry/107
  40. */
  41. #ifndef DUK_CONFIG_H_INCLUDED
  42. #define DUK_CONFIG_H_INCLUDED
  43. /*
  44. * Compiler features
  45. */
  46. #undef DUK_F_C99
  47. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
  48. #define DUK_F_C99
  49. #endif
  50. #undef DUK_F_CPP
  51. #if defined(__cplusplus)
  52. #define DUK_F_CPP
  53. #endif
  54. #undef DUK_F_CPP11
  55. #if defined(__cplusplus) && (__cplusplus >= 201103L)
  56. #define DUK_F_CPP11
  57. #endif
  58. /*
  59. * Provides the duk_rdtsc() inline function (if available), limited to
  60. * GCC C99.
  61. *
  62. * See: http://www.mcs.anl.gov/~kazutomo/rdtsc.html
  63. */
  64. /* XXX: more accurate detection of what gcc versions work; more inline
  65. * asm versions for other compilers.
  66. */
  67. #if defined(__GNUC__) && defined(__i386__) && defined(DUK_F_C99) && \
  68. !defined(__cplusplus) /* unsigned long long not standard */
  69. static __inline__ unsigned long long duk_rdtsc(void) {
  70. unsigned long long int x;
  71. __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
  72. return x;
  73. }
  74. #define DUK_USE_RDTSC() duk_rdtsc()
  75. #elif defined(__GNUC__) && defined(__x86_64__) && defined(DUK_F_C99) && \
  76. !defined(__cplusplus) /* unsigned long long not standard */
  77. static __inline__ unsigned long long duk_rdtsc(void) {
  78. unsigned hi, lo;
  79. __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
  80. return ((unsigned long long) lo) | (((unsigned long long) hi) << 32);
  81. }
  82. #define DUK_USE_RDTSC() duk_rdtsc()
  83. #else
  84. /* not available */
  85. #undef DUK_USE_RDTSC
  86. #endif
  87. /*
  88. * Intermediate platform, architecture, and compiler detection. These are
  89. * hopelessly intertwined - e.g. architecture defines depend on compiler etc.
  90. *
  91. * Provide easier defines for platforms and compilers which are often tricky
  92. * or verbose to detect. The intent is not to provide intermediate defines for
  93. * all features; only if existing feature defines are inconvenient.
  94. */
  95. /* Intel x86 (32-bit) */
  96. #if defined(i386) || defined(__i386) || defined(__i386__) || \
  97. defined(__i486__) || defined(__i586__) || defined(__i686__) || \
  98. defined(__IA32__) || defined(_M_IX86) || defined(__X86__) || \
  99. defined(_X86_) || defined(__THW_INTEL__) || defined(__I86__)
  100. #define DUK_F_X86
  101. #endif
  102. /* AMD64 (64-bit) */
  103. #if defined(__amd64__) || defined(__amd64) || \
  104. defined(__x86_64__) || defined(__x86_64) || \
  105. defined(_M_X64) || defined(_M_AMD64)
  106. #define DUK_F_X64
  107. #endif
  108. /* X32: 64-bit with 32-bit pointers (allows packed tvals). X32 support is
  109. * not very mature yet.
  110. *
  111. * https://sites.google.com/site/x32abi/
  112. */
  113. #if defined(DUK_F_X64) && \
  114. (defined(_ILP32) || defined(__ILP32__))
  115. #define DUK_F_X32
  116. /* define only one of: DUK_F_X86, DUK_F_X32, or DUK_F_X64 */
  117. #undef DUK_F_X64
  118. #undef DUK_F_X86
  119. #endif
  120. /* ARM */
  121. #if defined(__arm__) || defined(__thumb__) || defined(_ARM) || defined(_M_ARM)
  122. #define DUK_F_ARM
  123. #endif
  124. /* MIPS */
  125. /* Related defines: __MIPSEB__, __MIPSEL__, __mips_isa_rev, __LP64__ */
  126. #if defined(__mips__) || defined(mips) || defined(_MIPS_ISA) || \
  127. defined(_R3000) || defined(_R4000) || defined(_R5900) || \
  128. defined(_MIPS_ISA_MIPS1) || defined(_MIPS_ISA_MIPS2) || \
  129. defined(_MIPS_ISA_MIPS3) || defined(_MIPS_ISA_MIPS4) || \
  130. defined(__mips) || defined(__MIPS__)
  131. #define DUK_F_MIPS
  132. #if defined(__LP64__) || defined(__mips64) || defined(__mips64__) || \
  133. defined(__mips_n64)
  134. #define DUK_F_MIPS64
  135. #else
  136. #define DUK_F_MIPS32
  137. #endif
  138. #endif
  139. /* SuperH */
  140. #if defined(__sh__) || \
  141. defined(__sh1__) || defined(__SH1__) || \
  142. defined(__sh2__) || defined(__SH2__) || \
  143. defined(__sh3__) || defined(__SH3__) || \
  144. defined(__sh4__) || defined(__SH4__) || \
  145. defined(__sh5__) || defined(__SH5__)
  146. #define DUK_F_SUPERH
  147. #endif
  148. /* Motorola 68K. Not defined by VBCC, so user must define one of these
  149. * manually when using VBCC.
  150. */
  151. #if defined(__m68k__) || defined(M68000) || defined(__MC68K__)
  152. #define DUK_F_M68K
  153. #endif
  154. /* PowerPC */
  155. #if defined(__powerpc) || defined(__powerpc__) || defined(__PPC__)
  156. #define DUK_F_PPC
  157. #if defined(__PPC64__)
  158. #define DUK_F_PPC64
  159. #else
  160. #define DUK_F_PPC32
  161. #endif
  162. #endif
  163. /* Linux */
  164. #if defined(__linux) || defined(__linux__) || defined(linux)
  165. #define DUK_F_LINUX
  166. #endif
  167. /* FreeBSD */
  168. #if defined(__FreeBSD__) || defined(__FreeBSD)
  169. #define DUK_F_FREEBSD
  170. #endif
  171. /* NetBSD */
  172. #if defined(__NetBSD__) || defined(__NetBSD)
  173. #define DUK_F_NETBSD
  174. #endif
  175. /* OpenBSD */
  176. #if defined(__OpenBSD__) || defined(__OpenBSD)
  177. #define DUK_F_OPENBSD
  178. #endif
  179. /* BSD variant */
  180. #if defined(DUK_F_FREEBSD) || defined(DUK_F_NETBSD) || defined(DUK_F_OPENBSD) || \
  181. defined(__bsdi__) || defined(__DragonFly__)
  182. #define DUK_F_BSD
  183. #endif
  184. /* Generic Unix (includes Cygwin) */
  185. #if defined(__unix) || defined(__unix__) || defined(unix) || \
  186. defined(DUK_F_LINUX) || defined(DUK_F_BSD)
  187. #define DUK_F_UNIX
  188. #endif
  189. /* Cygwin */
  190. #if defined(__CYGWIN__)
  191. #define DUK_F_CYGWIN
  192. #endif
  193. /* Windows (32-bit or above) */
  194. #if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) || \
  195. defined(__WIN32__) || defined(__TOS_WIN__) || defined(__WINDOWS__)
  196. #define DUK_F_WINDOWS
  197. #endif
  198. #if defined(__APPLE__)
  199. #define DUK_F_APPLE
  200. #endif
  201. /* Atari ST TOS. __TOS__ defined by PureC (which doesn't work as a target now
  202. * because int is 16-bit, to be fixed). No platform define in VBCC apparently,
  203. * so to use with VBCC, user must define '__TOS__' manually.
  204. */
  205. #if defined(__TOS__)
  206. #define DUK_F_TOS
  207. #endif
  208. /* AmigaOS. Neither AMIGA nor __amigaos__ is defined on VBCC, so user must
  209. * define 'AMIGA' manually.
  210. */
  211. #if defined(AMIGA) || defined(__amigaos__)
  212. #define DUK_F_AMIGAOS
  213. #endif
  214. /* Flash player (e.g. Crossbridge) */
  215. #if defined(__FLASHPLAYER__)
  216. #define DUK_F_FLASHPLAYER
  217. #endif
  218. /* Emscripten (provided explicitly by user), improve if possible */
  219. #if defined(EMSCRIPTEN)
  220. #define DUK_F_EMSCRIPTEN
  221. #endif
  222. /* QNX */
  223. #if defined(__QNX__)
  224. #define DUK_F_QNX
  225. #endif
  226. /* TI-Nspire (using Ndless) */
  227. #if defined(_TINSPIRE)
  228. #define DUK_F_TINSPIRE
  229. #endif
  230. /* GCC and GCC version convenience define. */
  231. #if defined(__GNUC__)
  232. #define DUK_F_GCC
  233. #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
  234. /* Convenience, e.g. gcc 4.5.1 == 40501; http://stackoverflow.com/questions/6031819/emulating-gccs-builtin-unreachable */
  235. #define DUK_F_GCC_VERSION (__GNUC__ * 10000L + __GNUC_MINOR__ * 100L + __GNUC_PATCHLEVEL__)
  236. #else
  237. #error cannot figure out gcc version
  238. #endif
  239. #endif
  240. /* Clang */
  241. #if defined(__clang__)
  242. #define DUK_F_CLANG
  243. /* It seems clang also defines __GNUC__, so undo the GCC detection. */
  244. #if defined(DUK_F_GCC)
  245. #undef DUK_F_GCC
  246. #endif
  247. #if defined(DUK_F_GCC_VERSION)
  248. #undef DUK_F_GCC_VERSION
  249. #endif
  250. #endif
  251. /* MSVC */
  252. #if defined(_MSC_VER)
  253. /* MSVC preprocessor defines: http://msdn.microsoft.com/en-us/library/b0084kay.aspx
  254. * _MSC_FULL_VER includes the build number, but it has at least two formats, see e.g.
  255. * BOOST_MSVC_FULL_VER in http://www.boost.org/doc/libs/1_52_0/boost/config/compiler/visualc.hpp
  256. */
  257. #define DUK_F_MSVC
  258. #if defined(_MSC_FULL_VER)
  259. #if (_MSC_FULL_VER > 100000000)
  260. #define DUK_F_MSVC_FULL_VER _MSC_FULL_VER
  261. #else
  262. #define DUK_F_MSCV_FULL_VER (_MSC_FULL_VER * 10)
  263. #endif
  264. #endif
  265. #endif /* _MSC_VER */
  266. /* MinGW */
  267. #if defined(__MINGW32__) || defined(__MINGW64__)
  268. /* NOTE: Also GCC flags are detected (DUK_F_GCC etc). */
  269. #define DUK_F_MINGW
  270. #endif
  271. /* BCC (Bruce's C compiler): this is a "torture target" for compilation */
  272. #if defined(__BCC__) || defined(__BCC_VERSION__)
  273. #define DUK_F_BCC
  274. #endif
  275. #if defined(__VBCC__)
  276. #define DUK_F_VBCC
  277. #endif
  278. /*
  279. * Platform detection, system includes, Date provider selection.
  280. *
  281. * Feature selection (e.g. _XOPEN_SOURCE) must happen before any system
  282. * headers are included. This header should avoid providing any feature
  283. * selection defines when compiling user code (only when compiling Duktape
  284. * itself). If a feature selection option is required for user code to
  285. * compile correctly (e.g. it is needed for type detection), it should
  286. * probably be -checked- here, not defined here.
  287. *
  288. * Date provider selection seems a bit out-of-place here, but since
  289. * the date headers and provider functions are heavily platform
  290. * specific, there's little point in duplicating the platform if-else
  291. * ladder. All platform specific Date provider functions are in
  292. * duk_bi_date.c; here we provide appropriate #defines to enable them,
  293. * and include all the necessary system headers so that duk_bi_date.c
  294. * compiles. Date "providers" are:
  295. *
  296. * NOW = getting current time (required)
  297. * TZO = getting local time offset (required)
  298. * PRS = parse datetime (optional)
  299. * FMT = format datetime (optional)
  300. *
  301. * There's a lot of duplication here, unfortunately, because many
  302. * platforms have similar (but not identical) headers, Date providers,
  303. * etc. The duplication could be removed by more complicated nested
  304. * #ifdefs, but it would then be more difficult to make fixes which
  305. * affect only a specific platform.
  306. *
  307. * XXX: add a way to provide custom functions to provide the critical
  308. * primitives; this would be convenient when porting to unknown platforms
  309. * (rather than muck with Duktape internals).
  310. */
  311. #if defined(DUK_COMPILING_DUKTAPE) && \
  312. (defined(DUK_F_LINUX) || defined(DUK_F_EMSCRIPTEN))
  313. /* A more recent Emscripten (2014-05) seems to lack "linux" environment
  314. * defines, so check for Emscripten explicitly.
  315. */
  316. #ifndef _POSIX_C_SOURCE
  317. #define _POSIX_C_SOURCE 200809L
  318. #endif
  319. #ifndef _GNU_SOURCE
  320. #define _GNU_SOURCE /* e.g. getdate_r */
  321. #endif
  322. #ifndef _XOPEN_SOURCE
  323. #define _XOPEN_SOURCE /* e.g. strptime */
  324. #endif
  325. #endif
  326. #if defined(DUK_F_QNX) && defined(DUK_COMPILING_DUKTAPE)
  327. /* See: /opt/qnx650/target/qnx6/usr/include/sys/platform.h */
  328. #define _XOPEN_SOURCE 600
  329. #define _POSIX_C_SOURCE 200112L
  330. #endif
  331. #undef DUK_F_MSVC_CRT_SECURE
  332. #if defined(DUK_F_WINDOWS) && defined(_MSC_VER)
  333. /* http://msdn.microsoft.com/en-us/library/8ef0s5kh.aspx
  334. * http://msdn.microsoft.com/en-us/library/wd3wzwts.aspx
  335. * Seem to be available since VS2005.
  336. */
  337. #if (_MSC_VER >= 1400)
  338. /* VS2005+, secure CRT functions are preferred. Windows Store applications
  339. * (and probably others) should use these.
  340. */
  341. #define DUK_F_MSVC_CRT_SECURE
  342. #endif
  343. #if (_MSC_VER < 1700)
  344. /* VS2012+ has stdint.h, < VS2012 does not (but it's available for download). */
  345. #define DUK_F_NO_STDINT_H
  346. #endif
  347. /* Initial fix: disable secure CRT related warnings when compiling Duktape
  348. * itself (must be defined before including Windows headers). Don't define
  349. * for user code including duktape.h.
  350. */
  351. #if defined(DUK_COMPILING_DUKTAPE) && !defined(_CRT_SECURE_NO_WARNINGS)
  352. #define _CRT_SECURE_NO_WARNINGS
  353. #endif
  354. #endif /* DUK_F_WINDOWS && _MSC_VER */
  355. #if defined(DUK_F_TOS) || defined(DUK_F_BCC)
  356. #define DUK_F_NO_STDINT_H
  357. #endif
  358. /* Workaround for older C++ compilers before including <inttypes.h>,
  359. * see e.g.: https://sourceware.org/bugzilla/show_bug.cgi?id=15366
  360. */
  361. #if defined(__cplusplus) && !defined(__STDC_LIMIT_MACROS)
  362. #define __STDC_LIMIT_MACROS
  363. #endif
  364. #if defined(__cplusplus) && !defined(__STDC_CONSTANT_MACROS)
  365. #define __STDC_CONSTANT_MACROS
  366. #endif
  367. #if defined(__APPLE__)
  368. /* Mac OSX, iPhone, Darwin */
  369. #define DUK_USE_DATE_NOW_GETTIMEOFDAY
  370. #define DUK_USE_DATE_TZO_GMTIME_R
  371. #define DUK_USE_DATE_PRS_STRPTIME
  372. #define DUK_USE_DATE_FMT_STRFTIME
  373. #include <TargetConditionals.h>
  374. #include <architecture/byte_order.h>
  375. #include <limits.h>
  376. #include <sys/param.h>
  377. #include <sys/time.h>
  378. #include <time.h>
  379. #elif defined(DUK_F_OPENBSD)
  380. /* http://www.monkey.org/openbsd/archive/ports/0401/msg00089.html */
  381. #define DUK_USE_DATE_NOW_GETTIMEOFDAY
  382. #define DUK_USE_DATE_TZO_GMTIME_R
  383. #define DUK_USE_DATE_PRS_STRPTIME
  384. #define DUK_USE_DATE_FMT_STRFTIME
  385. #include <sys/types.h>
  386. #include <sys/endian.h>
  387. #include <limits.h>
  388. #include <sys/param.h>
  389. #include <sys/time.h>
  390. #include <time.h>
  391. #elif defined(DUK_F_BSD)
  392. /* other BSD */
  393. #define DUK_USE_DATE_NOW_GETTIMEOFDAY
  394. #define DUK_USE_DATE_TZO_GMTIME_R
  395. #define DUK_USE_DATE_PRS_STRPTIME
  396. #define DUK_USE_DATE_FMT_STRFTIME
  397. #include <sys/types.h>
  398. #include <sys/endian.h>
  399. #include <limits.h>
  400. #include <sys/param.h>
  401. #include <sys/time.h>
  402. #include <time.h>
  403. #elif defined(DUK_F_TOS)
  404. /* Atari ST TOS */
  405. #define DUK_USE_DATE_NOW_TIME
  406. #define DUK_USE_DATE_TZO_GMTIME
  407. /* no parsing (not an error) */
  408. #define DUK_USE_DATE_FMT_STRFTIME
  409. #include <limits.h>
  410. #include <time.h>
  411. #elif defined(DUK_F_AMIGAOS)
  412. #if defined(DUK_F_M68K)
  413. /* AmigaOS on M68k */
  414. #define DUK_USE_DATE_NOW_TIME
  415. #define DUK_USE_DATE_TZO_GMTIME
  416. /* no parsing (not an error) */
  417. #define DUK_USE_DATE_FMT_STRFTIME
  418. #include <limits.h>
  419. #include <time.h>
  420. #elif defined(DUK_F_PPC)
  421. #define DUK_USE_DATE_NOW_GETTIMEOFDAY
  422. #define DUK_USE_DATE_TZO_GMTIME_R
  423. #define DUK_USE_DATE_PRS_STRPTIME
  424. #define DUK_USE_DATE_FMT_STRFTIME
  425. #include <limits.h>
  426. #include <time.h>
  427. #ifndef UINTPTR_MAX
  428. #define UINTPTR_MAX UINT_MAX
  429. #endif
  430. #else
  431. #error AmigaOS but not M68K/PPC, not supported now
  432. #endif
  433. #elif defined(DUK_F_WINDOWS)
  434. /* Windows 32-bit and 64-bit are currently the same. */
  435. /* MSVC does not have sys/param.h */
  436. #define DUK_USE_DATE_NOW_WINDOWS
  437. #define DUK_USE_DATE_TZO_WINDOWS
  438. /* Note: PRS and FMT are intentionally left undefined for now. This means
  439. * there is no platform specific date parsing/formatting but there is still
  440. * the ISO 8601 standard format.
  441. */
  442. #if defined(DUK_COMPILING_DUKTAPE)
  443. #include <windows.h>
  444. // ATOMIC BEGIN
  445. #ifdef GetObject
  446. #undef GetObject
  447. #endif
  448. #ifdef MessageBox
  449. #undef MessageBox
  450. #endif
  451. #ifdef GetMessage
  452. #undef GetMessage
  453. #endif
  454. #ifdef FindText
  455. #undef FindText
  456. #endif
  457. // ATOMIC END
  458. #endif
  459. #include <limits.h>
  460. #elif defined(DUK_F_FLASHPLAYER)
  461. /* Crossbridge */
  462. #define DUK_USE_DATE_NOW_GETTIMEOFDAY
  463. #define DUK_USE_DATE_TZO_GMTIME_R
  464. #define DUK_USE_DATE_PRS_STRPTIME
  465. #define DUK_USE_DATE_FMT_STRFTIME
  466. #include <endian.h>
  467. #include <limits.h>
  468. #include <sys/param.h>
  469. #include <sys/time.h>
  470. #include <time.h>
  471. #elif defined(DUK_F_QNX)
  472. #define DUK_USE_DATE_NOW_GETTIMEOFDAY
  473. #define DUK_USE_DATE_TZO_GMTIME_R
  474. #define DUK_USE_DATE_PRS_STRPTIME
  475. #define DUK_USE_DATE_FMT_STRFTIME
  476. #include <sys/types.h>
  477. #include <limits.h>
  478. #include <sys/param.h>
  479. #include <sys/time.h>
  480. #include <time.h>
  481. #elif defined(DUK_F_TINSPIRE)
  482. #define DUK_USE_DATE_NOW_GETTIMEOFDAY
  483. #define DUK_USE_DATE_TZO_GMTIME_R
  484. #define DUK_USE_DATE_PRS_STRPTIME
  485. #define DUK_USE_DATE_FMT_STRFTIME
  486. #include <sys/types.h>
  487. #include <limits.h>
  488. #include <sys/param.h>
  489. #include <sys/time.h>
  490. #include <time.h>
  491. #elif defined(DUK_F_LINUX)
  492. #define DUK_USE_DATE_NOW_GETTIMEOFDAY
  493. #define DUK_USE_DATE_TZO_GMTIME_R
  494. #define DUK_USE_DATE_PRS_STRPTIME
  495. #define DUK_USE_DATE_FMT_STRFTIME
  496. #include <sys/types.h>
  497. #if defined(DUK_F_BCC)
  498. /* no endian.h */
  499. #else
  500. #include <endian.h>
  501. #endif /* DUK_F_BCC */
  502. #include <limits.h>
  503. #include <sys/param.h>
  504. #include <sys/time.h>
  505. #include <time.h>
  506. #elif defined(__posix)
  507. /* POSIX */
  508. #define DUK_USE_DATE_NOW_GETTIMEOFDAY
  509. #define DUK_USE_DATE_TZO_GMTIME_R
  510. #define DUK_USE_DATE_PRS_STRPTIME
  511. #define DUK_USE_DATE_FMT_STRFTIME
  512. #include <sys/types.h>
  513. #include <endian.h>
  514. #include <limits.h>
  515. #include <sys/param.h>
  516. #include <sys/time.h>
  517. #include <time.h>
  518. #elif defined(DUK_F_CYGWIN)
  519. /* Cygwin -- don't use strptime() for now */
  520. #define DUK_USE_DATE_NOW_GETTIMEOFDAY
  521. #define DUK_USE_DATE_TZO_GMTIME_R
  522. #define DUK_USE_DATE_FMT_STRFTIME
  523. #include <sys/types.h>
  524. #include <endian.h>
  525. #include <limits.h>
  526. #include <sys/param.h>
  527. #include <sys/time.h>
  528. #include <time.h>
  529. #else
  530. /* Other UNIX, hopefully others */
  531. #define DUK_USE_DATE_NOW_GETTIMEOFDAY
  532. #define DUK_USE_DATE_TZO_GMTIME_R
  533. #define DUK_USE_DATE_PRS_STRPTIME
  534. #define DUK_USE_DATE_FMT_STRFTIME
  535. #include <sys/types.h>
  536. #if defined(DUK_F_BCC)
  537. /* no endian.h */
  538. #else
  539. #include <endian.h>
  540. #endif /* DUK_F_BCC */
  541. #include <limits.h>
  542. #include <sys/param.h>
  543. #include <sys/time.h>
  544. #include <time.h>
  545. #endif
  546. /* Shared includes */
  547. #include <stdio.h>
  548. #include <stdlib.h>
  549. #include <string.h>
  550. #include <stdarg.h> /* varargs */
  551. #include <setjmp.h>
  552. #include <stddef.h> /* e.g. ptrdiff_t */
  553. #if defined(DUK_F_NO_STDINT_H)
  554. /* stdint.h not available */
  555. #else
  556. /* Technically C99 (C++11) but found in many systems. Note the workaround
  557. * above for some C++ compilers (__STDC_LIMIT_MACROS etc).
  558. */
  559. #include <stdint.h>
  560. #endif
  561. #include <math.h>
  562. #if (defined(DUK_F_C99) || defined(DUK_F_CPP11)) && \
  563. !defined(DUK_F_BCC)
  564. /* ULL / LL preprocessor constants should be avoided because they're not
  565. * always available. With suitable options, some compilers will support
  566. * 64-bit integer types but won't support ULL / LL preprocessor constants.
  567. * Assume C99/C++11 environments have these. However, BCC is nominally
  568. * C99 but doesn't support these constants.
  569. */
  570. #define DUK_F_ULL_CONSTS
  571. #endif
  572. /*
  573. * Detection for specific libc variants (like uclibc) and other libc specific
  574. * features. Potentially depends on the #includes above.
  575. */
  576. #if defined(__UCLIBC__)
  577. #define DUK_F_UCLIBC
  578. #endif
  579. /*
  580. * Wrapper typedefs and constants for integer types, also sanity check types.
  581. *
  582. * C99 typedefs are quite good but not always available, and we want to avoid
  583. * forcibly redefining the C99 typedefs. So, there are Duktape wrappers for
  584. * all C99 typedefs and Duktape code should only use these typedefs. Type
  585. * detection when C99 is not supported is best effort and may end up detecting
  586. * some types incorrectly.
  587. *
  588. * Pointer sizes are a portability problem: pointers to different types may
  589. * have a different size and function pointers are very difficult to manage
  590. * portably.
  591. *
  592. * http://en.wikipedia.org/wiki/C_data_types#Fixed-width_integer_types
  593. *
  594. * Note: there's an interesting corner case when trying to define minimum
  595. * signed integer value constants which leads to the current workaround of
  596. * defining e.g. -0x80000000 as (-0x7fffffffL - 1L). See doc/code-issues.txt
  597. * for a longer discussion.
  598. *
  599. * Note: avoid typecasts and computations in macro integer constants as they
  600. * can then no longer be used in macro relational expressions (such as
  601. * #if DUK_SIZE_MAX < 0xffffffffUL). There is internal code which relies on
  602. * being able to compare DUK_SIZE_MAX against a limit.
  603. */
  604. /* XXX: add feature options to force basic types from outside? */
  605. #if !defined(INT_MAX)
  606. #error INT_MAX not defined
  607. #endif
  608. /* Check that architecture is two's complement, standard C allows e.g.
  609. * INT_MIN to be -2**31+1 (instead of -2**31).
  610. */
  611. #if defined(INT_MAX) && defined(INT_MIN)
  612. #if INT_MAX != -(INT_MIN + 1)
  613. #error platform does not seem complement of two
  614. #endif
  615. #else
  616. #error cannot check complement of two
  617. #endif
  618. /* Pointer size determination based on architecture.
  619. * XXX: unsure about BCC correctness.
  620. */
  621. #if defined(DUK_F_X86) || defined(DUK_F_X32) || \
  622. defined(DUK_F_BCC) || defined(ANDROID) || \
  623. (defined(__WORDSIZE) && (__WORDSIZE == 32))
  624. #define DUK_F_32BIT_PTRS
  625. #elif defined(DUK_F_X64) || \
  626. (defined(__WORDSIZE) && (__WORDSIZE == 64))
  627. #define DUK_F_64BIT_PTRS
  628. #else
  629. /* not sure, not needed with C99 anyway */
  630. #endif
  631. /* Intermediate define for 'have inttypes.h' */
  632. #undef DUK_F_HAVE_INTTYPES
  633. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
  634. !(defined(DUK_F_AMIGAOS) && defined(DUK_F_VBCC))
  635. /* vbcc + AmigaOS has C99 but no inttypes.h */
  636. #define DUK_F_HAVE_INTTYPES
  637. #elif defined(__cplusplus) && (__cplusplus >= 201103L)
  638. /* C++11 apparently ratified stdint.h */
  639. #define DUK_F_HAVE_INTTYPES
  640. #endif
  641. /* Basic integer typedefs and limits, preferably from inttypes.h, otherwise
  642. * through automatic detection.
  643. */
  644. #if defined(DUK_F_HAVE_INTTYPES)
  645. /* C99 or compatible */
  646. #define DUK_F_HAVE_64BIT
  647. #include <inttypes.h>
  648. typedef uint8_t duk_uint8_t;
  649. typedef int8_t duk_int8_t;
  650. typedef uint16_t duk_uint16_t;
  651. typedef int16_t duk_int16_t;
  652. typedef uint32_t duk_uint32_t;
  653. typedef int32_t duk_int32_t;
  654. typedef uint64_t duk_uint64_t;
  655. typedef int64_t duk_int64_t;
  656. typedef uint_least8_t duk_uint_least8_t;
  657. typedef int_least8_t duk_int_least8_t;
  658. typedef uint_least16_t duk_uint_least16_t;
  659. typedef int_least16_t duk_int_least16_t;
  660. typedef uint_least32_t duk_uint_least32_t;
  661. typedef int_least32_t duk_int_least32_t;
  662. typedef uint_least64_t duk_uint_least64_t;
  663. typedef int_least64_t duk_int_least64_t;
  664. typedef uint_fast8_t duk_uint_fast8_t;
  665. typedef int_fast8_t duk_int_fast8_t;
  666. typedef uint_fast16_t duk_uint_fast16_t;
  667. typedef int_fast16_t duk_int_fast16_t;
  668. typedef uint_fast32_t duk_uint_fast32_t;
  669. typedef int_fast32_t duk_int_fast32_t;
  670. typedef uint_fast64_t duk_uint_fast64_t;
  671. typedef int_fast64_t duk_int_fast64_t;
  672. typedef uintptr_t duk_uintptr_t;
  673. typedef intptr_t duk_intptr_t;
  674. typedef uintmax_t duk_uintmax_t;
  675. typedef intmax_t duk_intmax_t;
  676. #define DUK_UINT8_MIN 0
  677. #define DUK_UINT8_MAX UINT8_MAX
  678. #define DUK_INT8_MIN INT8_MIN
  679. #define DUK_INT8_MAX INT8_MAX
  680. #define DUK_UINT_LEAST8_MIN 0
  681. #define DUK_UINT_LEAST8_MAX UINT_LEAST8_MAX
  682. #define DUK_INT_LEAST8_MIN INT_LEAST8_MIN
  683. #define DUK_INT_LEAST8_MAX INT_LEAST8_MAX
  684. #define DUK_UINT_FAST8_MIN 0
  685. #define DUK_UINT_FAST8_MAX UINT_FAST8_MAX
  686. #define DUK_INT_FAST8_MIN INT_FAST8_MIN
  687. #define DUK_INT_FAST8_MAX INT_FAST8_MAX
  688. #define DUK_UINT16_MIN 0
  689. #define DUK_UINT16_MAX UINT16_MAX
  690. #define DUK_INT16_MIN INT16_MIN
  691. #define DUK_INT16_MAX INT16_MAX
  692. #define DUK_UINT_LEAST16_MIN 0
  693. #define DUK_UINT_LEAST16_MAX UINT_LEAST16_MAX
  694. #define DUK_INT_LEAST16_MIN INT_LEAST16_MIN
  695. #define DUK_INT_LEAST16_MAX INT_LEAST16_MAX
  696. #define DUK_UINT_FAST16_MIN 0
  697. #define DUK_UINT_FAST16_MAX UINT_FAST16_MAX
  698. #define DUK_INT_FAST16_MIN INT_FAST16_MIN
  699. #define DUK_INT_FAST16_MAX INT_FAST16_MAX
  700. #define DUK_UINT32_MIN 0
  701. #define DUK_UINT32_MAX UINT32_MAX
  702. #define DUK_INT32_MIN INT32_MIN
  703. #define DUK_INT32_MAX INT32_MAX
  704. #define DUK_UINT_LEAST32_MIN 0
  705. #define DUK_UINT_LEAST32_MAX UINT_LEAST32_MAX
  706. #define DUK_INT_LEAST32_MIN INT_LEAST32_MIN
  707. #define DUK_INT_LEAST32_MAX INT_LEAST32_MAX
  708. #define DUK_UINT_FAST32_MIN 0
  709. #define DUK_UINT_FAST32_MAX UINT_FAST32_MAX
  710. #define DUK_INT_FAST32_MIN INT_FAST32_MIN
  711. #define DUK_INT_FAST32_MAX INT_FAST32_MAX
  712. #define DUK_UINT64_MIN 0
  713. #define DUK_UINT64_MAX UINT64_MAX
  714. #define DUK_INT64_MIN INT64_MIN
  715. #define DUK_INT64_MAX INT64_MAX
  716. #define DUK_UINT_LEAST64_MIN 0
  717. #define DUK_UINT_LEAST64_MAX UINT_LEAST64_MAX
  718. #define DUK_INT_LEAST64_MIN INT_LEAST64_MIN
  719. #define DUK_INT_LEAST64_MAX INT_LEAST64_MAX
  720. #define DUK_UINT_FAST64_MIN 0
  721. #define DUK_UINT_FAST64_MAX UINT_FAST64_MAX
  722. #define DUK_INT_FAST64_MIN INT_FAST64_MIN
  723. #define DUK_INT_FAST64_MAX INT_FAST64_MAX
  724. #define DUK_UINTPTR_MIN 0
  725. #define DUK_UINTPTR_MAX UINTPTR_MAX
  726. #define DUK_INTPTR_MIN INTPTR_MIN
  727. #define DUK_INTPTR_MAX INTPTR_MAX
  728. #define DUK_UINTMAX_MIN 0
  729. #define DUK_UINTMAX_MAX UINTMAX_MAX
  730. #define DUK_INTMAX_MIN INTMAX_MIN
  731. #define DUK_INTMAX_MAX INTMAX_MAX
  732. #define DUK_SIZE_MIN 0
  733. #define DUK_SIZE_MAX SIZE_MAX
  734. #undef DUK_SIZE_MAX_COMPUTED
  735. #else /* C99 types */
  736. /* When C99 types are not available, we use heuristic detection to get
  737. * the basic 8, 16, 32, and (possibly) 64 bit types. The fast/least
  738. * types are then assumed to be exactly the same for now: these could
  739. * be improved per platform but C99 types are very often now available.
  740. * 64-bit types are not available on all platforms; this is OK at least
  741. * on 32-bit platforms.
  742. *
  743. * This detection code is necessarily a bit hacky and can provide typedefs
  744. * and defines that won't work correctly on some exotic platform.
  745. */
  746. #if (defined(CHAR_BIT) && (CHAR_BIT == 8)) || \
  747. (defined(UCHAR_MAX) && (UCHAR_MAX == 255))
  748. typedef unsigned char duk_uint8_t;
  749. typedef signed char duk_int8_t;
  750. #else
  751. #error cannot detect 8-bit type
  752. #endif
  753. #if defined(USHRT_MAX) && (USHRT_MAX == 65535UL)
  754. typedef unsigned short duk_uint16_t;
  755. typedef signed short duk_int16_t;
  756. #elif defined(UINT_MAX) && (UINT_MAX == 65535UL)
  757. /* On some platforms int is 16-bit but long is 32-bit (e.g. PureC) */
  758. typedef unsigned int duk_uint16_t;
  759. typedef signed int duk_int16_t;
  760. #else
  761. #error cannot detect 16-bit type
  762. #endif
  763. #if defined(UINT_MAX) && (UINT_MAX == 4294967295UL)
  764. typedef unsigned int duk_uint32_t;
  765. typedef signed int duk_int32_t;
  766. #elif defined(ULONG_MAX) && (ULONG_MAX == 4294967295UL)
  767. /* On some platforms int is 16-bit but long is 32-bit (e.g. PureC) */
  768. typedef unsigned long duk_uint32_t;
  769. typedef signed long duk_int32_t;
  770. #else
  771. #error cannot detect 32-bit type
  772. #endif
  773. /* 64-bit type detection is a bit tricky.
  774. *
  775. * ULLONG_MAX is a standard define. __LONG_LONG_MAX__ and __ULONG_LONG_MAX__
  776. * are used by at least GCC (even if system headers don't provide ULLONG_MAX).
  777. * Some GCC variants may provide __LONG_LONG_MAX__ but not __ULONG_LONG_MAX__.
  778. *
  779. * ULL / LL constants are rejected / warned about by some compilers, even if
  780. * the compiler has a 64-bit type and the compiler/system headers provide an
  781. * unsupported constant (ULL/LL)! Try to avoid using ULL / LL constants.
  782. * As a side effect we can only check that e.g. ULONG_MAX is larger than 32
  783. * bits but can't be sure it is exactly 64 bits. Self tests will catch such
  784. * cases.
  785. */
  786. #undef DUK_F_HAVE_64BIT
  787. #if !defined(DUK_F_HAVE_64BIT) && defined(ULONG_MAX)
  788. #if (ULONG_MAX > 4294967295UL)
  789. #define DUK_F_HAVE_64BIT
  790. typedef unsigned long duk_uint64_t;
  791. typedef signed long duk_int64_t;
  792. #endif
  793. #endif
  794. #if !defined(DUK_F_HAVE_64BIT) && defined(ULLONG_MAX)
  795. #if (ULLONG_MAX > 4294967295UL)
  796. #define DUK_F_HAVE_64BIT
  797. typedef unsigned long long duk_uint64_t;
  798. typedef signed long long duk_int64_t;
  799. #endif
  800. #endif
  801. #if !defined(DUK_F_HAVE_64BIT) && defined(__ULONG_LONG_MAX__)
  802. #if (__ULONG_LONG_MAX__ > 4294967295UL)
  803. #define DUK_F_HAVE_64BIT
  804. typedef unsigned long long duk_uint64_t;
  805. typedef signed long long duk_int64_t;
  806. #endif
  807. #endif
  808. #if !defined(DUK_F_HAVE_64BIT) && defined(__LONG_LONG_MAX__)
  809. #if (__LONG_LONG_MAX__ > 2147483647L)
  810. #define DUK_F_HAVE_64BIT
  811. typedef unsigned long long duk_uint64_t;
  812. typedef signed long long duk_int64_t;
  813. #endif
  814. #endif
  815. #if !defined(DUK_F_HAVE_64BIT) && \
  816. (defined(DUK_F_MINGW) || defined(DUK_F_MSVC))
  817. /* Both MinGW and MSVC have a 64-bit type. */
  818. #define DUK_F_HAVE_64BIT
  819. typedef unsigned long duk_uint64_t;
  820. typedef signed long duk_int64_t;
  821. #endif
  822. #if !defined(DUK_F_HAVE_64BIT)
  823. /* cannot detect 64-bit type, not always needed so don't error */
  824. #endif
  825. typedef duk_uint8_t duk_uint_least8_t;
  826. typedef duk_int8_t duk_int_least8_t;
  827. typedef duk_uint16_t duk_uint_least16_t;
  828. typedef duk_int16_t duk_int_least16_t;
  829. typedef duk_uint32_t duk_uint_least32_t;
  830. typedef duk_int32_t duk_int_least32_t;
  831. typedef duk_uint8_t duk_uint_fast8_t;
  832. typedef duk_int8_t duk_int_fast8_t;
  833. typedef duk_uint16_t duk_uint_fast16_t;
  834. typedef duk_int16_t duk_int_fast16_t;
  835. typedef duk_uint32_t duk_uint_fast32_t;
  836. typedef duk_int32_t duk_int_fast32_t;
  837. #if defined(DUK_F_HAVE_64BIT)
  838. typedef duk_uint64_t duk_uint_least64_t;
  839. typedef duk_int64_t duk_int_least64_t;
  840. typedef duk_uint64_t duk_uint_fast64_t;
  841. typedef duk_int64_t duk_int_fast64_t;
  842. #endif
  843. #if defined(DUK_F_HAVE_64BIT)
  844. typedef duk_uint64_t duk_uintmax_t;
  845. typedef duk_int64_t duk_intmax_t;
  846. #else
  847. typedef duk_uint32_t duk_uintmax_t;
  848. typedef duk_int32_t duk_intmax_t;
  849. #endif
  850. /* Note: the funny looking computations for signed minimum 16-bit, 32-bit, and
  851. * 64-bit values are intentional as the obvious forms (e.g. -0x80000000L) are
  852. * -not- portable. See code-issues.txt for a detailed discussion.
  853. */
  854. #define DUK_UINT8_MIN 0UL
  855. #define DUK_UINT8_MAX 0xffUL
  856. #define DUK_INT8_MIN (-0x80L)
  857. #define DUK_INT8_MAX 0x7fL
  858. #define DUK_UINT_LEAST8_MIN 0UL
  859. #define DUK_UINT_LEAST8_MAX 0xffUL
  860. #define DUK_INT_LEAST8_MIN (-0x80L)
  861. #define DUK_INT_LEAST8_MAX 0x7fL
  862. #define DUK_UINT_FAST8_MIN 0UL
  863. #define DUK_UINT_FAST8_MAX 0xffUL
  864. #define DUK_INT_FAST8_MIN (-0x80L)
  865. #define DUK_INT_FAST8_MAX 0x7fL
  866. #define DUK_UINT16_MIN 0UL
  867. #define DUK_UINT16_MAX 0xffffUL
  868. #define DUK_INT16_MIN (-0x7fffL - 1L)
  869. #define DUK_INT16_MAX 0x7fffL
  870. #define DUK_UINT_LEAST16_MIN 0UL
  871. #define DUK_UINT_LEAST16_MAX 0xffffUL
  872. #define DUK_INT_LEAST16_MIN (-0x7fffL - 1L)
  873. #define DUK_INT_LEAST16_MAX 0x7fffL
  874. #define DUK_UINT_FAST16_MIN 0UL
  875. #define DUK_UINT_FAST16_MAX 0xffffUL
  876. #define DUK_INT_FAST16_MIN (-0x7fffL - 1L)
  877. #define DUK_INT_FAST16_MAX 0x7fffL
  878. #define DUK_UINT32_MIN 0UL
  879. #define DUK_UINT32_MAX 0xffffffffUL
  880. #define DUK_INT32_MIN (-0x7fffffffL - 1L)
  881. #define DUK_INT32_MAX 0x7fffffffL
  882. #define DUK_UINT_LEAST32_MIN 0UL
  883. #define DUK_UINT_LEAST32_MAX 0xffffffffUL
  884. #define DUK_INT_LEAST32_MIN (-0x7fffffffL - 1L)
  885. #define DUK_INT_LEAST32_MAX 0x7fffffffL
  886. #define DUK_UINT_FAST32_MIN 0UL
  887. #define DUK_UINT_FAST32_MAX 0xffffffffUL
  888. #define DUK_INT_FAST32_MIN (-0x7fffffffL - 1L)
  889. #define DUK_INT_FAST32_MAX 0x7fffffffL
  890. /* 64-bit constants. Since LL / ULL constants are not always available,
  891. * use computed values. These values can't be used in preprocessor
  892. * comparisons; flag them as such.
  893. */
  894. #if defined(DUK_F_HAVE_64BIT)
  895. #define DUK_UINT64_MIN ((duk_uint64_t) 0)
  896. #define DUK_UINT64_MAX ((duk_uint64_t) -1)
  897. #define DUK_INT64_MIN ((duk_int64_t) (~(DUK_UINT64_MAX >> 1)))
  898. #define DUK_INT64_MAX ((duk_int64_t) (DUK_UINT64_MAX >> 1))
  899. #define DUK_UINT_LEAST64_MIN DUK_UINT64_MIN
  900. #define DUK_UINT_LEAST64_MAX DUK_UINT64_MAX
  901. #define DUK_INT_LEAST64_MIN DUK_INT64_MIN
  902. #define DUK_INT_LEAST64_MAX DUK_INT64_MAX
  903. #define DUK_UINT_FAST64_MIN DUK_UINT64_MIN
  904. #define DUK_UINT_FAST64_MAX DUK_UINT64_MAX
  905. #define DUK_INT_FAST64_MIN DUK_INT64_MIN
  906. #define DUK_INT_FAST64_MAX DUK_INT64_MAX
  907. #define DUK_UINT64_MIN_COMPUTED
  908. #define DUK_UINT64_MAX_COMPUTED
  909. #define DUK_INT64_MIN_COMPUTED
  910. #define DUK_INT64_MAX_COMPUTED
  911. #define DUK_UINT_LEAST64_MIN_COMPUTED
  912. #define DUK_UINT_LEAST64_MAX_COMPUTED
  913. #define DUK_INT_LEAST64_MIN_COMPUTED
  914. #define DUK_INT_LEAST64_MAX_COMPUTED
  915. #define DUK_UINT_FAST64_MIN_COMPUTED
  916. #define DUK_UINT_FAST64_MAX_COMPUTED
  917. #define DUK_INT_FAST64_MIN_COMPUTED
  918. #define DUK_INT_FAST64_MAX_COMPUTED
  919. #endif
  920. #if defined(DUK_F_HAVE_64BIT)
  921. #define DUK_UINTMAX_MIN DUK_UINT64_MIN
  922. #define DUK_UINTMAX_MAX DUK_UINT64_MAX
  923. #define DUK_INTMAX_MIN DUK_INT64_MIN
  924. #define DUK_INTMAX_MAX DUK_INT64_MAX
  925. #define DUK_UINTMAX_MIN_COMPUTED
  926. #define DUK_UINTMAX_MAX_COMPUTED
  927. #define DUK_INTMAX_MIN_COMPUTED
  928. #define DUK_INTMAX_MAX_COMPUTED
  929. #else
  930. #define DUK_UINTMAX_MIN 0UL
  931. #define DUK_UINTMAX_MAX 0xffffffffUL
  932. #define DUK_INTMAX_MIN (-0x7fffffffL - 1L)
  933. #define DUK_INTMAX_MAX 0x7fffffffL
  934. #endif
  935. /* This detection is not very reliable. */
  936. #if defined(DUK_F_32BIT_PTRS)
  937. typedef duk_int32_t duk_intptr_t;
  938. typedef duk_uint32_t duk_uintptr_t;
  939. #define DUK_UINTPTR_MIN DUK_UINT32_MIN
  940. #define DUK_UINTPTR_MAX DUK_UINT32_MAX
  941. #define DUK_INTPTR_MIN DUK_INT32_MIN
  942. #define DUK_INTPTR_MAX DUK_INT32_MAX
  943. #elif defined(DUK_F_64BIT_PTRS) && defined(DUK_F_HAVE_64BIT)
  944. typedef duk_int64_t duk_intptr_t;
  945. typedef duk_uint64_t duk_uintptr_t;
  946. #define DUK_UINTPTR_MIN DUK_UINT64_MIN
  947. #define DUK_UINTPTR_MAX DUK_UINT64_MAX
  948. #define DUK_INTPTR_MIN DUK_INT64_MIN
  949. #define DUK_INTPTR_MAX DUK_INT64_MAX
  950. #define DUK_UINTPTR_MIN_COMPUTED
  951. #define DUK_UINTPTR_MAX_COMPUTED
  952. #define DUK_INTPTR_MIN_COMPUTED
  953. #define DUK_INTPTR_MAX_COMPUTED
  954. #else
  955. #error cannot determine intptr type
  956. #endif
  957. /* SIZE_MAX may be missing so use an approximate value for it. */
  958. #undef DUK_SIZE_MAX_COMPUTED
  959. #if !defined(SIZE_MAX)
  960. #define DUK_SIZE_MAX_COMPUTED
  961. #define SIZE_MAX ((size_t) (-1))
  962. #endif
  963. #define DUK_SIZE_MIN 0
  964. #define DUK_SIZE_MAX SIZE_MAX
  965. #endif /* C99 types */
  966. /* A few types are assumed to always exist. */
  967. typedef size_t duk_size_t;
  968. typedef ptrdiff_t duk_ptrdiff_t;
  969. /* The best type for an "all around int" in Duktape internals is "at least
  970. * 32 bit signed integer" which is most convenient. Same for unsigned type.
  971. * Prefer 'int' when large enough, as it is almost always a convenient type.
  972. */
  973. #if defined(UINT_MAX) && (UINT_MAX >= 0xffffffffUL)
  974. typedef int duk_int_t;
  975. typedef unsigned int duk_uint_t;
  976. #define DUK_INT_MIN INT_MIN
  977. #define DUK_INT_MAX INT_MAX
  978. #define DUK_UINT_MIN 0
  979. #define DUK_UINT_MAX UINT_MAX
  980. #else
  981. typedef duk_int_fast32_t duk_int_t;
  982. typedef duk_uint_fast32_t duk_uint_t;
  983. #define DUK_INT_MIN DUK_INT_FAST32_MIN
  984. #define DUK_INT_MAX DUK_INT_FAST32_MAX
  985. #define DUK_UINT_MIN DUK_UINT_FAST32_MIN
  986. #define DUK_UINT_MAX DUK_UINT_FAST32_MAX
  987. #endif
  988. /* Same as 'duk_int_t' but guaranteed to be a 'fast' variant if this
  989. * distinction matters for the CPU. These types are used mainly in the
  990. * executor where it might really matter.
  991. */
  992. typedef duk_int_fast32_t duk_int_fast_t;
  993. typedef duk_uint_fast32_t duk_uint_fast_t;
  994. #define DUK_INT_FAST_MIN DUK_INT_FAST32_MIN
  995. #define DUK_INT_FAST_MAX DUK_INT_FAST32_MAX
  996. #define DUK_UINT_FAST_MIN DUK_UINT_FAST32_MIN
  997. #define DUK_UINT_FAST_MAX DUK_UINT_FAST32_MAX
  998. /* Small integers (16 bits or more) can fall back to the 'int' type, but
  999. * have a typedef so they are marked "small" explicitly.
  1000. */
  1001. typedef int duk_small_int_t;
  1002. typedef unsigned int duk_small_uint_t;
  1003. #define DUK_SMALL_INT_MIN INT_MIN
  1004. #define DUK_SMALL_INT_MAX INT_MAX
  1005. #define DUK_SMALL_UINT_MIN 0
  1006. #define DUK_SMALL_UINT_MAX UINT_MAX
  1007. /* Fast variants of small integers, again for really fast paths like the
  1008. * executor.
  1009. */
  1010. typedef duk_int_fast16_t duk_small_int_fast_t;
  1011. typedef duk_uint_fast16_t duk_small_uint_fast_t;
  1012. #define DUK_SMALL_INT_FAST_MIN DUK_INT_FAST16_MIN
  1013. #define DUK_SMALL_INT_FAST_MAX DUK_INT_FAST16_MAX
  1014. #define DUK_SMALL_UINT_FAST_MIN DUK_UINT_FAST16_MIN
  1015. #define DUK_SMALL_UINT_FAST_MAX DUK_UINT_FAST16_MAX
  1016. /* Boolean values are represented with the platform 'int'. */
  1017. typedef duk_small_int_t duk_bool_t;
  1018. #define DUK_BOOL_MIN DUK_SMALL_INT_MIN
  1019. #define DUK_BOOL_MAX DUK_SMALL_INT_MAX
  1020. /* Index values must have at least 32-bit signed range. */
  1021. typedef duk_int_t duk_idx_t;
  1022. #define DUK_IDX_MIN DUK_INT_MIN
  1023. #define DUK_IDX_MAX DUK_INT_MAX
  1024. /* Unsigned index variant. */
  1025. typedef duk_uint_t duk_uidx_t;
  1026. #define DUK_UIDX_MIN DUK_UINT_MIN
  1027. #define DUK_UIDX_MAX DUK_UINT_MAX
  1028. /* Array index values, could be exact 32 bits.
  1029. * Currently no need for signed duk_arridx_t.
  1030. */
  1031. typedef duk_uint_t duk_uarridx_t;
  1032. #define DUK_UARRIDX_MIN DUK_UINT_MIN
  1033. #define DUK_UARRIDX_MAX DUK_UINT_MAX
  1034. /* Duktape/C function return value, platform int is enough for now to
  1035. * represent 0, 1, or negative error code. Must be compatible with
  1036. * assigning truth values (e.g. duk_ret_t rc = (foo == bar);).
  1037. */
  1038. typedef duk_small_int_t duk_ret_t;
  1039. #define DUK_RET_MIN DUK_SMALL_INT_MIN
  1040. #define DUK_RET_MAX DUK_SMALL_INT_MAX
  1041. /* Error codes are represented with platform int. High bits are used
  1042. * for flags and such, so 32 bits are needed.
  1043. */
  1044. typedef duk_int_t duk_errcode_t;
  1045. #define DUK_ERRCODE_MIN DUK_INT_MIN
  1046. #define DUK_ERRCODE_MAX DUK_INT_MAX
  1047. /* Codepoint type. Must be 32 bits or more because it is used also for
  1048. * internal codepoints. The type is signed because negative codepoints
  1049. * are used as internal markers (e.g. to mark EOF or missing argument).
  1050. * (X)UTF-8/CESU-8 encode/decode take and return an unsigned variant to
  1051. * ensure duk_uint32_t casts back and forth nicely. Almost everything
  1052. * else uses the signed one.
  1053. */
  1054. typedef duk_int_t duk_codepoint_t;
  1055. typedef duk_uint_t duk_ucodepoint_t;
  1056. #define DUK_CODEPOINT_MIN DUK_INT_MIN
  1057. #define DUK_CODEPOINT_MAX DUK_INT_MAX
  1058. #define DUK_UCODEPOINT_MIN DUK_UINT_MIN
  1059. #define DUK_UCODEPOINT_MAX DUK_UINT_MAX
  1060. /* IEEE float/double typedef. */
  1061. typedef float duk_float_t;
  1062. typedef double duk_double_t;
  1063. /* We're generally assuming that we're working on a platform with a 32-bit
  1064. * address space. If DUK_SIZE_MAX is a typecast value (which is necessary
  1065. * if SIZE_MAX is missing), the check must be avoided because the
  1066. * preprocessor can't do a comparison.
  1067. */
  1068. #if !defined(DUK_SIZE_MAX)
  1069. #error DUK_SIZE_MAX is undefined, probably missing SIZE_MAX
  1070. #elif !defined(DUK_SIZE_MAX_COMPUTED)
  1071. #if DUK_SIZE_MAX < 0xffffffffUL
  1072. /* On some systems SIZE_MAX can be smaller than max unsigned 32-bit value
  1073. * which seems incorrect if size_t is (at least) an unsigned 32-bit type.
  1074. * However, it doesn't seem useful to error out compilation if this is the
  1075. * case.
  1076. */
  1077. #endif
  1078. #endif
  1079. /* Type for public API calls. */
  1080. typedef struct duk_hthread duk_context;
  1081. /*
  1082. * Check whether we should use 64-bit integers
  1083. */
  1084. /* Quite incomplete now. Use 64-bit types if detected (C99 or other detection)
  1085. * unless they are known to be unreliable. For instance, 64-bit types are
  1086. * available on VBCC but seem to misbehave.
  1087. */
  1088. #if defined(DUK_F_HAVE_64BIT) && !defined(DUK_F_VBCC)
  1089. #define DUK_USE_64BIT_OPS
  1090. #else
  1091. #undef DUK_USE_64BIT_OPS
  1092. #endif
  1093. /*
  1094. * Alignment requirement and support for unaligned accesses
  1095. *
  1096. * Assume unaligned accesses are not supported unless specifically allowed
  1097. * in the target platform. Some platforms may support unaligned accesses
  1098. * but alignment to 4 or 8 may still be desirable.
  1099. */
  1100. #undef DUK_USE_UNALIGNED_ACCESSES_POSSIBLE
  1101. #undef DUK_USE_ALIGN_BY
  1102. #if defined(DUK_F_EMSCRIPTEN)
  1103. /* Required on at least some targets, so use whenever Emscripten used,
  1104. * regardless of compilation target.
  1105. */
  1106. #define DUK_USE_ALIGN_BY 8
  1107. #elif defined(DUK_F_ARM)
  1108. #define DUK_USE_ALIGN_BY 4
  1109. #elif defined(DUK_F_MIPS32)
  1110. /* Based on 'make checkalign' there are no alignment requirements on
  1111. * Linux MIPS except for doubles, which need align by 4. Alignment
  1112. * requirements vary based on target though.
  1113. */
  1114. #define DUK_USE_ALIGN_BY 4
  1115. #elif defined(DUK_F_MIPS64)
  1116. /* Good default is a bit arbitrary because alignment requirements
  1117. * depend on target. See https://github.com/svaarala/duktape/issues/102.
  1118. */
  1119. #define DUK_USE_ALIGN_BY 8
  1120. #elif defined(DUK_F_SUPERH)
  1121. /* Based on 'make checkalign' there are no alignment requirements on
  1122. * Linux SH4, but align by 4 is probably a good basic default.
  1123. */
  1124. #define DUK_USE_ALIGN_BY 4
  1125. #elif defined(DUK_F_X86) || defined(DUK_F_X32) || defined(DUK_F_X64) || \
  1126. defined(DUK_F_BCC)
  1127. /* XXX: This is technically not guaranteed because it's possible to configure
  1128. * an x86 to require aligned accesses with Alignment Check (AC) flag.
  1129. */
  1130. #define DUK_USE_ALIGN_BY 1
  1131. #define DUK_USE_UNALIGNED_ACCESSES_POSSIBLE
  1132. #else
  1133. /* Unknown, use safe default */
  1134. #define DUK_USE_ALIGN_BY 8
  1135. #endif
  1136. /* User forced alignment to 4 or 8. */
  1137. #if defined(DUK_OPT_FORCE_ALIGN)
  1138. #undef DUK_USE_ALIGN_BY
  1139. #undef DUK_USE_UNALIGNED_ACCESSES_POSSIBLE
  1140. #if (DUK_OPT_FORCE_ALIGN == 4)
  1141. #define DUK_USE_ALIGN_BY 4
  1142. #elif (DUK_OPT_FORCE_ALIGN == 8)
  1143. #define DUK_USE_ALIGN_BY 8
  1144. #else
  1145. #error invalid DUK_OPT_FORCE_ALIGN value
  1146. #endif
  1147. #endif
  1148. /* Compiler specific hackery needed to force struct size to match aligment,
  1149. * see e.g. duk_hbuffer.h.
  1150. *
  1151. * http://stackoverflow.com/questions/11130109/c-struct-size-alignment
  1152. * http://stackoverflow.com/questions/10951039/specifying-64-bit-alignment
  1153. */
  1154. #if defined(DUK_F_MSVC)
  1155. #define DUK_USE_PACK_MSVC_PRAGMA
  1156. #elif defined(DUK_F_GCC)
  1157. #define DUK_USE_PACK_GCC_ATTR
  1158. #elif defined(DUK_F_CLANG)
  1159. #define DUK_USE_PACK_CLANG_ATTR
  1160. #else
  1161. #define DUK_USE_PACK_DUMMY_MEMBER
  1162. #endif
  1163. #ifdef DUK_USE_UNALIGNED_ACCESSES_POSSIBLE
  1164. #define DUK_USE_HASHBYTES_UNALIGNED_U32_ACCESS
  1165. #else
  1166. #undef DUK_USE_HASHBYTES_UNALIGNED_U32_ACCESS
  1167. #endif
  1168. /* Object property allocation layout has implications for memory and code
  1169. * footprint and generated code size/speed. The best layout also depends
  1170. * on whether the platform has alignment requirements or benefits from
  1171. * having mostly aligned accesses.
  1172. */
  1173. #undef DUK_USE_HOBJECT_LAYOUT_1
  1174. #undef DUK_USE_HOBJECT_LAYOUT_2
  1175. #undef DUK_USE_HOBJECT_LAYOUT_3
  1176. #if defined(DUK_USE_UNALIGNED_ACCESSES_POSSIBLE) && (DUK_USE_ALIGN_BY == 1)
  1177. /* On platforms without any alignment issues, layout 1 is preferable
  1178. * because it compiles to slightly less code and provides direct access
  1179. * to property keys.
  1180. */
  1181. #define DUK_USE_HOBJECT_LAYOUT_1
  1182. #else
  1183. /* On other platforms use layout 2, which requires some padding but
  1184. * is a bit more natural than layout 3 in ordering the entries. Layout
  1185. * 3 is currently not used.
  1186. */
  1187. #define DUK_USE_HOBJECT_LAYOUT_2
  1188. #endif
  1189. /*
  1190. * Byte order and double memory layout detection
  1191. *
  1192. * Endianness detection is a major portability hassle because the macros
  1193. * and headers are not standardized. There's even variance across UNIX
  1194. * platforms. Even with "standard" headers, details like underscore count
  1195. * varies between platforms, e.g. both __BYTE_ORDER and _BYTE_ORDER are used
  1196. * (Crossbridge has a single underscore, for instance).
  1197. *
  1198. * The checks below are structured with this in mind: several approaches are
  1199. * used, and at the end we check if any of them worked. This allows generic
  1200. * approaches to be tried first, and platform/compiler specific hacks tried
  1201. * last. As a last resort, the user can force a specific endianness, as it's
  1202. * not likely that automatic detection will work on the most exotic platforms.
  1203. *
  1204. * Duktape supports little and big endian machines. There's also support
  1205. * for a hybrid used by some ARM machines where integers are little endian
  1206. * but IEEE double values use a mixed order (12345678 -> 43218765). This
  1207. * byte order for doubles is referred to as "mixed endian".
  1208. */
  1209. #undef DUK_F_BYTEORDER
  1210. #undef DUK_USE_BYTEORDER_FORCED
  1211. /* DUK_F_BYTEORDER is set as an intermediate value when detection
  1212. * succeeds, to one of:
  1213. * 1 = little endian
  1214. * 2 = mixed (arm hybrid) endian
  1215. * 3 = big endian
  1216. */
  1217. /* For custom platforms allow user to define byteorder explicitly.
  1218. * Since endianness headers are not standardized, this is a useful
  1219. * workaround for custom platforms for which endianness detection
  1220. * is not directly supported. Perhaps custom hardware is used and
  1221. * user cannot submit upstream patches.
  1222. */
  1223. #if defined(DUK_OPT_FORCE_BYTEORDER)
  1224. #if (DUK_OPT_FORCE_BYTEORDER == 1)
  1225. #define DUK_F_BYTEORDER 1
  1226. #elif (DUK_OPT_FORCE_BYTEORDER == 2)
  1227. #define DUK_F_BYTEORDER 2
  1228. #elif (DUK_OPT_FORCE_BYTEORDER == 3)
  1229. #define DUK_F_BYTEORDER 3
  1230. #else
  1231. #error invalid DUK_OPT_FORCE_BYTEORDER value
  1232. #endif
  1233. #define DUK_USE_BYTEORDER_FORCED
  1234. #endif /* DUK_OPT_FORCE_BYTEORDER */
  1235. /* More or less standard endianness predefines provided by header files.
  1236. * The ARM hybrid case is detected by assuming that __FLOAT_WORD_ORDER
  1237. * will be big endian, see: http://lists.mysql.com/internals/443.
  1238. */
  1239. #if !defined(DUK_F_BYTEORDER)
  1240. #if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN) || \
  1241. defined(_BYTE_ORDER) && defined(_LITTLE_ENDIAN) && (_BYTE_ORDER == _LITTLE_ENDIAN) || \
  1242. defined(__LITTLE_ENDIAN__)
  1243. /* Integer little endian */
  1244. #if defined(__FLOAT_WORD_ORDER) && defined(__LITTLE_ENDIAN) && (__FLOAT_WORD_ORDER == __LITTLE_ENDIAN) || \
  1245. defined(_FLOAT_WORD_ORDER) && defined(_LITTLE_ENDIAN) && (_FLOAT_WORD_ORDER == _LITTLE_ENDIAN)
  1246. #define DUK_F_BYTEORDER 1
  1247. #elif defined(__FLOAT_WORD_ORDER) && defined(__BIG_ENDIAN) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN) || \
  1248. defined(_FLOAT_WORD_ORDER) && defined(_BIG_ENDIAN) && (_FLOAT_WORD_ORDER == _BIG_ENDIAN)
  1249. #define DUK_F_BYTEORDER 2
  1250. #elif !defined(__FLOAT_WORD_ORDER) && !defined(_FLOAT_WORD_ORDER)
  1251. /* Float word order not known, assume not a hybrid. */
  1252. #define DUK_F_BYTEORDER 1
  1253. #else
  1254. /* byte order is little endian but cannot determine IEEE double word order */
  1255. #endif /* float word order */
  1256. #elif defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN) || \
  1257. defined(_BYTE_ORDER) && defined(_BIG_ENDIAN) && (_BYTE_ORDER == _BIG_ENDIAN) || \
  1258. defined(__BIG_ENDIAN__)
  1259. /* Integer big endian */
  1260. #if defined(__FLOAT_WORD_ORDER) && defined(__BIG_ENDIAN) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN) || \
  1261. defined(_FLOAT_WORD_ORDER) && defined(_BIG_ENDIAN) && (_FLOAT_WORD_ORDER == _BIG_ENDIAN)
  1262. #define DUK_F_BYTEORDER 3
  1263. #elif !defined(__FLOAT_WORD_ORDER) && !defined(_FLOAT_WORD_ORDER)
  1264. /* Float word order not known, assume not a hybrid. */
  1265. #define DUK_F_BYTEORDER 3
  1266. #else
  1267. /* byte order is big endian but cannot determine IEEE double word order */
  1268. #endif /* float word order */
  1269. #else
  1270. /* cannot determine byte order */
  1271. #endif /* integer byte order */
  1272. #endif /* !defined(DUK_F_BYTEORDER) */
  1273. /* GCC and Clang provide endianness defines as built-in predefines, with
  1274. * leading and trailing double underscores (e.g. __BYTE_ORDER__). See
  1275. * output of "make gccpredefs" and "make clangpredefs". Clang doesn't
  1276. * seem to provide __FLOAT_WORD_ORDER__.
  1277. * http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
  1278. */
  1279. #if !defined(DUK_F_BYTEORDER) && defined(__BYTE_ORDER__)
  1280. #if defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  1281. /* Integer little endian */
  1282. #if defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  1283. (__FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  1284. #define DUK_F_BYTEORDER 1
  1285. #elif defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
  1286. (__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__)
  1287. #define DUK_F_BYTEORDER 2
  1288. #elif !defined(__FLOAT_WORD_ORDER__)
  1289. /* Float word order not known, assume not a hybrid. */
  1290. #define DUK_F_BYTEORDER 1
  1291. #else
  1292. /* byte order is little endian but cannot determine IEEE double word order */
  1293. #endif /* float word order */
  1294. #elif defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  1295. /* Integer big endian */
  1296. #if defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
  1297. (__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__)
  1298. #define DUK_F_BYTEORDER 3
  1299. #elif !defined(__FLOAT_WORD_ORDER__)
  1300. /* Float word order not known, assume not a hybrid. */
  1301. #define DUK_F_BYTEORDER 3
  1302. #else
  1303. /* byte order is big endian but cannot determine IEEE double word order */
  1304. #endif /* float word order */
  1305. #else
  1306. /* cannot determine byte order; __ORDER_PDP_ENDIAN__ is related to 32-bit
  1307. * integer ordering and is not relevant
  1308. */
  1309. #endif /* integer byte order */
  1310. #endif /* !defined(DUK_F_BYTEORDER) && defined(__BYTE_ORDER__) */
  1311. /* Atari ST TOS */
  1312. #if !defined(DUK_F_BYTEORDER) && defined(DUK_F_TOS)
  1313. #define DUK_F_BYTEORDER 3
  1314. #endif
  1315. /* AmigaOS on M68K or PPC */
  1316. #if !defined(DUK_F_BYTEORDER) && defined(DUK_F_AMIGAOS)
  1317. #if defined(DUK_F_M68K) || defined(DUK_F_PPC)
  1318. #define DUK_F_BYTEORDER 3
  1319. #endif
  1320. #endif
  1321. /* On Windows, assume we're little endian. Even Itanium which has a
  1322. * configurable endianness runs little endian in Windows.
  1323. */
  1324. #if !defined(DUK_F_BYTEORDER) && defined(DUK_F_WINDOWS)
  1325. /* XXX: verify that Windows on ARM is little endian for floating point
  1326. * values too.
  1327. */
  1328. #define DUK_F_BYTEORDER 1
  1329. #endif /* Windows */
  1330. /* Crossbridge should work with the standard byteorder #ifdefs. It doesn't
  1331. * provide _FLOAT_WORD_ORDER but the standard approach now covers that case
  1332. * too. This has been left here just in case.
  1333. */
  1334. #if !defined(DUK_F_BYTEORDER) && defined(DUK_F_FLASHPLAYER)
  1335. #define DUK_F_BYTEORDER 1
  1336. #endif
  1337. /* QNX gcc cross compiler seems to define e.g. __LITTLEENDIAN__ or __BIGENDIAN__:
  1338. * $ /opt/qnx650/host/linux/x86/usr/bin/i486-pc-nto-qnx6.5.0-gcc -dM -E - </dev/null | grep -ni endian
  1339. * 67:#define __LITTLEENDIAN__ 1
  1340. * $ /opt/qnx650/host/linux/x86/usr/bin/mips-unknown-nto-qnx6.5.0-gcc -dM -E - </dev/null | grep -ni endian
  1341. * 81:#define __BIGENDIAN__ 1
  1342. * $ /opt/qnx650/host/linux/x86/usr/bin/arm-unknown-nto-qnx6.5.0-gcc -dM -E - </dev/null | grep -ni endian
  1343. * 70:#define __LITTLEENDIAN__ 1
  1344. */
  1345. #if !defined(DUK_F_BYTEORDER) && defined(DUK_F_QNX)
  1346. /* XXX: ARM hybrid? */
  1347. #if defined(__LITTLEENDIAN__)
  1348. #define DUK_F_BYTEORDER 1
  1349. #elif defined(__BIGENDIAN__)
  1350. #define DUK_F_BYTEORDER 3
  1351. #endif
  1352. #endif
  1353. /* Bruce's C Compiler (BCC), assume we're on x86. */
  1354. #if !defined(DUK_F_BYTEORDER) && defined(DUK_F_BCC)
  1355. #define DUK_F_BYTEORDER 1
  1356. #endif
  1357. /* Check whether or not byte order detection worked based on the intermediate
  1358. * define, and define final values. If detection failed, #error out.
  1359. */
  1360. #if defined(DUK_F_BYTEORDER)
  1361. #if (DUK_F_BYTEORDER == 1)
  1362. #define DUK_USE_INTEGER_LE
  1363. #define DUK_USE_DOUBLE_LE
  1364. #elif (DUK_F_BYTEORDER == 2)
  1365. #define DUK_USE_INTEGER_LE /* integer endianness is little on purpose */
  1366. #define DUK_USE_DOUBLE_ME
  1367. #elif (DUK_F_BYTEORDER == 3)
  1368. #define DUK_USE_INTEGER_BE
  1369. #define DUK_USE_DOUBLE_BE
  1370. #else
  1371. #error unsupported: byte order detection failed (internal error, should not happen)
  1372. #endif /* byte order */
  1373. #else
  1374. #error unsupported: byte order detection failed
  1375. #endif /* defined(DUK_F_BYTEORDER) */
  1376. /*
  1377. * Check whether or not a packed duk_tval representation is possible.
  1378. * What's basically required is that pointers are 32-bit values
  1379. * (sizeof(void *) == 4). Best effort check, not always accurate.
  1380. * If guess goes wrong, crashes may result; self tests also verify
  1381. * the guess.
  1382. */
  1383. #undef DUK_USE_PACKED_TVAL_POSSIBLE
  1384. /* Strict C99 case: DUK_UINTPTR_MAX (= UINTPTR_MAX) should be very reliable */
  1385. #if !defined(DUK_USE_PACKED_TVAL_POSSIBLE) && defined(DUK_F_HAVE_INTTYPES) && defined(DUK_UINTPTR_MAX)
  1386. #if (DUK_UINTPTR_MAX <= 0xffffffffUL)
  1387. #define DUK_USE_PACKED_TVAL_POSSIBLE
  1388. #endif
  1389. #endif
  1390. /* Non-C99 case, still relying on DUK_UINTPTR_MAX, as long as it is not a computed value */
  1391. #if !defined(DUK_USE_PACKED_TVAL_POSSIBLE) && defined(DUK_UINTPTR_MAX) && !defined(DUK_UINTPTR_MAX_COMPUTED)
  1392. #if (DUK_UINTPTR_MAX <= 0xffffffffUL)
  1393. #define DUK_USE_PACKED_TVAL_POSSIBLE
  1394. #endif
  1395. #endif
  1396. /* DUK_SIZE_MAX (= SIZE_MAX) is often reliable */
  1397. #if !defined(DUK_USE_PACKED_TVAL_POSSIBLE) && defined(DUK_SIZE_MAX) && !defined(DUK_SIZE_MAX_COMPUTED)
  1398. #if DUK_SIZE_MAX <= 0xffffffffUL
  1399. #define DUK_USE_PACKED_TVAL_POSSIBLE
  1400. #endif
  1401. #endif
  1402. /* M68K: packed always possible */
  1403. #if !defined(DUK_USE_PACKED_TVAL_POSSIBLE) && defined(DUK_F_M68K)
  1404. #define DUK_USE_PACKED_TVAL_POSSIBLE
  1405. #endif
  1406. /* PPC32: packed always possible */
  1407. #if !defined(DUK_USE_PACKED_TVAL_POSSIBLE) && defined(DUK_F_PPC32)
  1408. #define DUK_USE_PACKED_TVAL_POSSIBLE
  1409. #endif
  1410. /* With Emscripten, force unpacked duk_tval just to be safe, as it seems to
  1411. * break at least on Firefox (probably IEEE double arithmetic is not 100%
  1412. * supported, especially for NaNs).
  1413. */
  1414. #if defined(DUK_USE_PACKED_TVAL_POSSIBLE) && defined(DUK_F_EMSCRIPTEN)
  1415. #undef DUK_USE_PACKED_TVAL_POSSIBLE
  1416. #endif
  1417. /* Microsoft Visual Studio 2010 on x64 fails the above rules and tries to
  1418. * use a packed type. Force unpacked on x64 in general.
  1419. */
  1420. #if defined(DUK_USE_PACKED_TVAL_POSSIBLE) && defined(DUK_F_X64)
  1421. #undef DUK_USE_PACKED_TVAL_POSSIBLE
  1422. #endif
  1423. /* GCC/clang inaccurate math would break compliance and probably duk_tval,
  1424. * so refuse to compile. Relax this if -ffast-math is tested to work.
  1425. */
  1426. #if defined(__FAST_MATH__)
  1427. #error __FAST_MATH__ defined, refusing to compile
  1428. #endif
  1429. /*
  1430. * Detection of double constants and math related functions. Availability
  1431. * of constants and math functions is a significant porting concern.
  1432. *
  1433. * INFINITY/HUGE_VAL is problematic on GCC-3.3: it causes an overflow warning
  1434. * and there is no pragma in GCC-3.3 to disable it. Using __builtin_inf()
  1435. * avoids this problem for some reason.
  1436. */
  1437. #define DUK_DOUBLE_2TO32 4294967296.0
  1438. #define DUK_DOUBLE_2TO31 2147483648.0
  1439. #undef DUK_USE_COMPUTED_INFINITY
  1440. #if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION < 40600)
  1441. /* GCC older than 4.6: avoid overflow warnings related to using INFINITY */
  1442. #define DUK_DOUBLE_INFINITY (__builtin_inf())
  1443. #elif defined(INFINITY)
  1444. #define DUK_DOUBLE_INFINITY ((double) INFINITY)
  1445. #elif !defined(DUK_F_VBCC) && !defined(DUK_F_MSVC) && !defined(DUK_F_BCC)
  1446. #define DUK_DOUBLE_INFINITY (1.0 / 0.0)
  1447. #else
  1448. /* In VBCC (1.0 / 0.0) results in a warning and 0.0 instead of infinity.
  1449. * Use a computed infinity (initialized when a heap is created at the
  1450. * latest).
  1451. */
  1452. extern double duk_computed_infinity;
  1453. #define DUK_USE_COMPUTED_INFINITY
  1454. #define DUK_DOUBLE_INFINITY duk_computed_infinity
  1455. #endif
  1456. #undef DUK_USE_COMPUTED_NAN
  1457. #if defined(NAN)
  1458. #define DUK_DOUBLE_NAN NAN
  1459. #elif !defined(DUK_F_VBCC) && !defined(DUK_F_MSVC) && !defined(DUK_F_BCC)
  1460. #define DUK_DOUBLE_NAN (0.0 / 0.0)
  1461. #else
  1462. /* In VBCC (0.0 / 0.0) results in a warning and 0.0 instead of NaN.
  1463. * In MSVC (VS2010 Express) (0.0 / 0.0) results in a compile error.
  1464. * Use a computed NaN (initialized when a heap is created at the
  1465. * latest).
  1466. */
  1467. extern double duk_computed_nan;
  1468. #define DUK_USE_COMPUTED_NAN
  1469. #define DUK_DOUBLE_NAN duk_computed_nan
  1470. #endif
  1471. /* Many platforms are missing fpclassify() and friends, so use replacements
  1472. * if necessary. The replacement constants (FP_NAN etc) can be anything but
  1473. * match Linux constants now.
  1474. */
  1475. #undef DUK_USE_REPL_FPCLASSIFY
  1476. #undef DUK_USE_REPL_SIGNBIT
  1477. #undef DUK_USE_REPL_ISFINITE
  1478. #undef DUK_USE_REPL_ISNAN
  1479. #undef DUK_USE_REPL_ISINF
  1480. /* Complex condition broken into separate parts. */
  1481. #undef DUK_F_USE_REPL_ALL
  1482. #if !(defined(FP_NAN) && defined(FP_INFINITE) && defined(FP_ZERO) && \
  1483. defined(FP_SUBNORMAL) && defined(FP_NORMAL))
  1484. /* Missing some obvious constants. */
  1485. #define DUK_F_USE_REPL_ALL
  1486. #elif defined(DUK_F_AMIGAOS) && defined(DUK_F_VBCC)
  1487. /* VBCC is missing the built-ins even in C99 mode (perhaps a header issue) */
  1488. #define DUK_F_USE_REPL_ALL
  1489. #elif defined(DUK_F_FREEBSD) && defined(DUK_F_CLANG)
  1490. /* Placeholder fix for (detection is wider than necessary):
  1491. * http://llvm.org/bugs/show_bug.cgi?id=17788
  1492. */
  1493. #define DUK_F_USE_REPL_ALL
  1494. #elif defined(DUK_F_UCLIBC)
  1495. /* At least some uclibc versions have broken floating point math. For
  1496. * example, fpclassify() can incorrectly classify certain NaN formats.
  1497. * To be safe, use replacements.
  1498. */
  1499. #define DUK_F_USE_REPL_ALL
  1500. #endif
  1501. #if defined(DUK_F_USE_REPL_ALL)
  1502. #define DUK_USE_REPL_FPCLASSIFY
  1503. #define DUK_USE_REPL_SIGNBIT
  1504. #define DUK_USE_REPL_ISFINITE
  1505. #define DUK_USE_REPL_ISNAN
  1506. #define DUK_USE_REPL_ISINF
  1507. #define DUK_FPCLASSIFY duk_repl_fpclassify
  1508. #define DUK_SIGNBIT duk_repl_signbit
  1509. #define DUK_ISFINITE duk_repl_isfinite
  1510. #define DUK_ISNAN duk_repl_isnan
  1511. #define DUK_ISINF duk_repl_isinf
  1512. #define DUK_FP_NAN 0
  1513. #define DUK_FP_INFINITE 1
  1514. #define DUK_FP_ZERO 2
  1515. #define DUK_FP_SUBNORMAL 3
  1516. #define DUK_FP_NORMAL 4
  1517. #else
  1518. #define DUK_FPCLASSIFY fpclassify
  1519. #define DUK_SIGNBIT signbit
  1520. #define DUK_ISFINITE isfinite
  1521. #define DUK_ISNAN isnan
  1522. #define DUK_ISINF isinf
  1523. #define DUK_FP_NAN FP_NAN
  1524. #define DUK_FP_INFINITE FP_INFINITE
  1525. #define DUK_FP_ZERO FP_ZERO
  1526. #define DUK_FP_SUBNORMAL FP_SUBNORMAL
  1527. #define DUK_FP_NORMAL FP_NORMAL
  1528. #endif
  1529. #if defined(DUK_F_USE_REPL_ALL)
  1530. #undef DUK_F_USE_REPL_ALL
  1531. #endif
  1532. /* Some math functions are C99 only. This is also an issue with some
  1533. * embedded environments using uclibc where uclibc has been configured
  1534. * not to provide some functions. For now, use replacements whenever
  1535. * using uclibc.
  1536. */
  1537. #undef DUK_USE_MATH_FMIN
  1538. #undef DUK_USE_MATH_FMAX
  1539. #undef DUK_USE_MATH_ROUND
  1540. #if defined(DUK_F_UCLIBC)
  1541. /* uclibc may be missing these */
  1542. #elif defined(DUK_F_AMIGAOS) && defined(DUK_F_VBCC)
  1543. /* vbcc + AmigaOS may be missing these */
  1544. #elif !defined(DUK_F_C99) && !defined(DUK_F_CPP11)
  1545. /* build is not C99 or C++11, play it safe */
  1546. #else
  1547. /* C99 or C++11, no known issues */
  1548. #define DUK_USE_MATH_FMIN
  1549. #define DUK_USE_MATH_FMAX
  1550. #define DUK_USE_MATH_ROUND
  1551. #endif
  1552. /* These functions don't currently need replacement but are wrapped for
  1553. * completeness. Because these are used as function pointers, they need
  1554. * to be defined as concrete C functions (not macros).
  1555. */
  1556. #define DUK_FABS fabs
  1557. #define DUK_FMIN fmin
  1558. #define DUK_FMAX fmax
  1559. #define DUK_FLOOR floor
  1560. #define DUK_CEIL ceil
  1561. #define DUK_FMOD fmod
  1562. #define DUK_POW pow
  1563. #define DUK_ACOS acos
  1564. #define DUK_ASIN asin
  1565. #define DUK_ATAN atan
  1566. #define DUK_ATAN2 atan2
  1567. #define DUK_SIN sin
  1568. #define DUK_COS cos
  1569. #define DUK_TAN tan
  1570. #define DUK_EXP exp
  1571. #define DUK_LOG log
  1572. #define DUK_SQRT sqrt
  1573. /* NetBSD 6.0 x86 (at least) has a few problems with pow() semantics,
  1574. * see test-bug-netbsd-math-pow.js. Use NetBSD specific workaround.
  1575. * (This might be a wider problem; if so, generalize the define name.)
  1576. */
  1577. #undef DUK_USE_POW_NETBSD_WORKAROUND
  1578. #if defined(DUK_F_NETBSD)
  1579. #define DUK_USE_POW_NETBSD_WORKAROUND
  1580. #endif
  1581. /* Rely as little as possible on compiler behavior for NaN comparison,
  1582. * signed zero handling, etc. Currently never activated but may be needed
  1583. * for broken compilers.
  1584. */
  1585. #undef DUK_USE_PARANOID_MATH
  1586. /* There was a curious bug where test-bi-date-canceling.js would fail e.g.
  1587. * on 64-bit Ubuntu, gcc-4.8.1, -m32, and no -std=c99. Some date computations
  1588. * using doubles would be optimized which then broke some corner case tests.
  1589. * The problem goes away by adding 'volatile' to the datetime computations.
  1590. * Not sure what the actual triggering conditions are, but using this on
  1591. * non-C99 systems solves the known issues and has relatively little cost
  1592. * on other platforms. See bugs/issue-2e9d9c2d761dabaf8136c0897b91a270d1a47147.yaml.
  1593. */
  1594. #undef DUK_USE_PARANOID_DATE_COMPUTATION
  1595. #if !defined(DUK_F_C99)
  1596. #define DUK_USE_PARANOID_DATE_COMPUTATION
  1597. #endif
  1598. /*
  1599. * ANSI C string/memory function wrapper defines to allow easier workarounds.
  1600. * Also convenience macros like DUK_MEMZERO which may be mapped to existing
  1601. * platform function to zero memory (like the deprecated bzero).
  1602. *
  1603. * For instance, some platforms don't support zero-size memcpy correctly,
  1604. * some arcane uclibc versions have a buggy memcpy (but working memmove)
  1605. * and so on. Such broken platforms can be dealt with here.
  1606. *
  1607. * NOTE: ANSI C (various versions) and some implementations require that the
  1608. * pointer arguments to memset(), memcpy(), and memmove() be valid values
  1609. * even when byte size is 0 (even a NULL pointer is considered invalid in
  1610. * this context). Zero-size operations as such are allowed, as long as their
  1611. * pointer arguments point to a valid memory area. The DUK_MEMSET(),
  1612. * DUK_MEMCPY(), and DUK_MEMMOVE() macros require this same behavior, i.e.:
  1613. * (1) pointers must be valid and non-NULL, (2) zero size must otherwise be
  1614. * allowed. If these are not fulfilled, a macro wrapper is needed.
  1615. *
  1616. * http://stackoverflow.com/questions/5243012/is-it-guaranteed-to-be-safe-to-perform-memcpy0-0-0
  1617. * http://lists.cs.uiuc.edu/pipermail/llvmdev/2007-October/011065.html
  1618. *
  1619. * Not sure what's the required behavior when a pointer points just past the
  1620. * end of a buffer, which often happens in practice (e.g. zero size memmoves).
  1621. * For example, if allocation size is 3, the following pointer would not
  1622. * technically point to a valid memory byte:
  1623. *
  1624. * <-- alloc -->
  1625. * | 0 | 1 | 2 | .....
  1626. * ^-- p=3, points after last valid byte (2)
  1627. *
  1628. * If this is a practical issue, wrappers are again needed.
  1629. */
  1630. typedef FILE duk_file;
  1631. #define DUK_STDIN stdin
  1632. #define DUK_STDOUT stdout
  1633. #define DUK_STDERR stderr
  1634. /* Special naming to avoid conflict with e.g. DUK_FREE() in duk_heap.h
  1635. * (which is unfortunately named).
  1636. */
  1637. #define DUK_ANSI_MALLOC malloc
  1638. #define DUK_ANSI_REALLOC realloc
  1639. #define DUK_ANSI_CALLOC calloc
  1640. #define DUK_ANSI_FREE free
  1641. /* Old uclibcs have a broken memcpy so use memmove instead (this is overly
  1642. * wide now on purpose):
  1643. * http://lists.uclibc.org/pipermail/uclibc-cvs/2008-October/025511.html
  1644. */
  1645. #if defined(DUK_F_UCLIBC)
  1646. #define DUK_MEMCPY memmove
  1647. #else
  1648. #define DUK_MEMCPY memcpy
  1649. #endif
  1650. #define DUK_MEMMOVE memmove
  1651. #define DUK_MEMCMP memcmp
  1652. #define DUK_MEMSET memset
  1653. #define DUK_STRLEN strlen
  1654. #define DUK_STRCMP strcmp
  1655. #define DUK_STRNCMP strncmp
  1656. #define DUK_PRINTF printf
  1657. #define DUK_FPRINTF fprintf
  1658. #define DUK_SPRINTF sprintf
  1659. #if defined(DUK_F_MSVC)
  1660. /* _snprintf() does NOT NUL terminate on truncation, but Duktape code never
  1661. * assumes that.
  1662. * http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010
  1663. */
  1664. #define DUK_SNPRINTF _snprintf
  1665. #else
  1666. #define DUK_SNPRINTF snprintf
  1667. #endif
  1668. #define DUK_VSPRINTF vsprintf
  1669. #if defined(DUK_F_MSVC)
  1670. #if (_MSC_VER < 1600)
  1671. /* Older MSVC version are missing vsnprintf() but have _vsnprintf(). */
  1672. #define DUK_VSNPRINTF _vsnprintf
  1673. #else
  1674. #define DUK_VSNPRINTF vsnprintf
  1675. #endif
  1676. #else
  1677. #define DUK_VSNPRINTF vsnprintf
  1678. #endif /* DUK_F_MSVC */
  1679. #define DUK_SSCANF sscanf
  1680. #define DUK_VSSCANF vsscanf
  1681. #define DUK_FOPEN fopen
  1682. #define DUK_FCLOSE fclose
  1683. #define DUK_FREAD fread
  1684. #define DUK_FWRITE fwrite
  1685. #define DUK_FSEEK fseek
  1686. #define DUK_FTELL ftell
  1687. #define DUK_FFLUSH fflush
  1688. #define DUK_FPUTC fputc
  1689. #define DUK_MEMZERO(p,n) \
  1690. DUK_MEMSET((p), 0, (n))
  1691. /*
  1692. * Vararg macro wrappers. We need va_copy() which is defined in C99 / C++11,
  1693. * so an awkward replacement is needed for pre-C99 / pre-C++11 environments.
  1694. * This will quite likely need portability hacks for some non-C99 environments.
  1695. */
  1696. #if defined(DUK_F_C99) || defined(DUK_F_CPP11)
  1697. /* C99 / C++11 and above: rely on va_copy() which is required.
  1698. * Omit parenthesis on macro right side on purpose to minimize differences
  1699. * to direct use.
  1700. */
  1701. #define DUK_VA_COPY(dest,src) va_copy(dest,src)
  1702. #elif defined(DUK_F_GCC) || defined(DUK_F_CLANG)
  1703. /* GCC: assume we have __va_copy() in non-C99 mode, which should be correct
  1704. * for even quite old GCC versions. Clang matches GCC behavior.
  1705. */
  1706. #define DUK_VA_COPY(dest,src) __va_copy(dest,src)
  1707. #else
  1708. /* Pre-C99: va_list type is implementation dependent. This replacement
  1709. * assumes it is a plain value so that a simple assignment will work.
  1710. * This is not the case on all platforms (it may be a single-array element,
  1711. * for instance).
  1712. */
  1713. #define DUK_VA_COPY(dest,src) do { (dest) = (src); } while (0)
  1714. #endif
  1715. /*
  1716. * Miscellaneous ANSI C or other platform wrappers.
  1717. */
  1718. #define DUK_ABORT abort
  1719. #define DUK_EXIT exit
  1720. /*
  1721. * Avoiding platform function pointers.
  1722. *
  1723. * On some platforms built-in functions may be implemented as macros or
  1724. * inline functions, so they can't be necessarily addressed by function
  1725. * pointers. This is certainly the case with some platform "polyfills"
  1726. * which provide missing C99/C++11 functions through macros, and may be
  1727. * the case with VS2013 (see GH-17).
  1728. */
  1729. /* This is now the default: the cost in footprint is negligible. */
  1730. #define DUK_USE_AVOID_PLATFORM_FUNCPTRS
  1731. /*
  1732. * Macro hackery to convert e.g. __LINE__ to a string without formatting,
  1733. * see: http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
  1734. */
  1735. #define DUK_F_STRINGIFY_HELPER(x) #x
  1736. #define DUK_MACRO_STRINGIFY(x) DUK_F_STRINGIFY_HELPER(x)
  1737. /*
  1738. * Cause segfault macro.
  1739. *
  1740. * This is optionally used by panic handling to cause the program to segfault
  1741. * (instead of e.g. abort()) on panic. Valgrind will then indicate the C
  1742. * call stack leading to the panic.
  1743. */
  1744. #define DUK_CAUSE_SEGFAULT() do { \
  1745. *((volatile duk_uint32_t *) NULL) = (duk_uint32_t) 0xdeadbeefUL; \
  1746. } while (0)
  1747. /*
  1748. * Macro for suppressing warnings for potentially unreferenced variables.
  1749. * The variables can be actually unreferenced or unreferenced in some
  1750. * specific cases only; for instance, if a variable is only debug printed,
  1751. * it is unreferenced when debug printing is disabled.
  1752. *
  1753. * (Introduced here because it's potentially compiler specific.)
  1754. */
  1755. #define DUK_UNREF(x) do { \
  1756. (void) (x); \
  1757. } while (0)
  1758. /* Convert any input pointer into a "void *", losing a const qualifier.
  1759. * This is not fully portable because casting through duk_uintptr_t may
  1760. * not work on all architectures (e.g. those with long, segmented pointers).
  1761. */
  1762. #define DUK_LOSE_CONST(src) ((void *) (duk_uintptr_t) (src))
  1763. /*
  1764. * DUK_NORETURN: macro for declaring a 'noreturn' function.
  1765. * Unfortunately the noreturn declaration may appear in various
  1766. * places of a function declaration, so the solution is to wrap
  1767. * the entire declaration inside the macro. Compiler support
  1768. * for using a noreturn declaration on function pointers varies;
  1769. * this macro must only be used for actual function declarations.
  1770. *
  1771. * http://gcc.gnu.org/onlinedocs/gcc-4.3.2//gcc/Function-Attributes.html
  1772. * http://clang.llvm.org/docs/LanguageExtensions.html
  1773. */
  1774. #if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 20500L)
  1775. /* since gcc-2.5 */
  1776. #define DUK_NORETURN(decl) decl __attribute__((noreturn))
  1777. #elif defined(__clang__)
  1778. /* syntax same as gcc */
  1779. #define DUK_NORETURN(decl) decl __attribute__((noreturn))
  1780. #elif defined(DUK_F_MSVC)
  1781. /* http://msdn.microsoft.com/en-us/library/aa235362(VS.60).aspx */
  1782. #define DUK_NORETURN(decl) __declspec(noreturn) decl
  1783. #else
  1784. /* Don't know how to declare a noreturn function, so don't do it; this
  1785. * may cause some spurious compilation warnings (e.g. "variable used
  1786. * uninitialized").
  1787. */
  1788. #define DUK_NORETURN(decl) decl
  1789. #endif
  1790. /*
  1791. * Macro for stating that a certain line cannot be reached.
  1792. *
  1793. * http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Other-Builtins.html#Other-Builtins
  1794. * http://clang.llvm.org/docs/LanguageExtensions.html
  1795. */
  1796. #if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 40500L)
  1797. /* since gcc-4.5 */
  1798. #define DUK_UNREACHABLE() do { __builtin_unreachable(); } while(0)
  1799. #elif defined(__clang__) && defined(__has_builtin)
  1800. #if __has_builtin(__builtin_unreachable)
  1801. /* same as gcc */
  1802. #define DUK_UNREACHABLE() do { __builtin_unreachable(); } while(0)
  1803. #endif
  1804. #else
  1805. /* unknown */
  1806. #endif
  1807. #if !defined(DUK_UNREACHABLE)
  1808. /* Don't know how to declare unreachable point, so don't do it; this
  1809. * may cause some spurious compilation warnings (e.g. "variable used
  1810. * uninitialized").
  1811. */
  1812. #define DUK_UNREACHABLE() /* unreachable */
  1813. #endif
  1814. /*
  1815. * Likely and unlikely branches. Using these is not at all a clear cut case,
  1816. * so the selection is a two-step process: (1) DUK_USE_BRANCH_HINTS is set
  1817. * if the architecture, compiler etc make it useful to use the hints, and (2)
  1818. * a separate check determines how to do them.
  1819. *
  1820. * These macros expect the argument to be a relational expression with an
  1821. * integer value. If used with pointers, you should use an explicit check
  1822. * like:
  1823. *
  1824. * if (DUK_LIKELY(ptr != NULL)) { ... }
  1825. *
  1826. * instead of:
  1827. *
  1828. * if (DUK_LIKELY(ptr)) { ... }
  1829. *
  1830. * http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html (__builtin_expect)
  1831. */
  1832. /* pretty much a placeholder now */
  1833. #if defined(DUK_F_GCC)
  1834. #define DUK_USE_BRANCH_HINTS
  1835. #elif defined(DUK_F_CLANG)
  1836. #define DUK_USE_BRANCH_HINTS
  1837. #else
  1838. #undef DUK_USE_BRANCH_HINTS
  1839. #endif
  1840. #if defined(DUK_USE_BRANCH_HINTS)
  1841. #if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 40500L)
  1842. /* GCC: test not very accurate; enable only in relatively recent builds
  1843. * because of bugs in gcc-4.4 (http://lists.debian.org/debian-gcc/2010/04/msg00000.html)
  1844. */
  1845. #define DUK_LIKELY(x) __builtin_expect((x), 1)
  1846. #define DUK_UNLIKELY(x) __builtin_expect((x), 0)
  1847. #elif defined(DUK_F_CLANG)
  1848. #define DUK_LIKELY(x) __builtin_expect((x), 1)
  1849. #define DUK_UNLIKELY(x) __builtin_expect((x), 0)
  1850. #endif
  1851. #endif /* DUK_USE_BRANCH_HINTS */
  1852. #if !defined(DUK_LIKELY)
  1853. #define DUK_LIKELY(x) (x)
  1854. #endif
  1855. #if !defined(DUK_UNLIKELY)
  1856. #define DUK_UNLIKELY(x) (x)
  1857. #endif
  1858. /*
  1859. * Function inlining control
  1860. *
  1861. * DUK_NOINLINE: avoid inlining a function.
  1862. * DUK_INLINE: suggest inlining a function.
  1863. * DUK_ALWAYS_INLINE: force inlining for critical functions.
  1864. *
  1865. * Apply to function definition only (not declaration).
  1866. */
  1867. #if defined(DUK_F_CLANG) && (defined(DUK_F_C99) || defined(DUK_F_CPP11))
  1868. #define DUK_NOINLINE __attribute__((noinline))
  1869. #define DUK_INLINE inline
  1870. #define DUK_ALWAYS_INLINE inline __attribute__((always_inline))
  1871. #elif defined(DUK_F_GCC) && defined(DUK_F_GCC_VERSION) && (defined(DUK_F_C99) || defined(DUK_F_CPP11))
  1872. #if (DUK_F_GCC_VERSION >= 30101)
  1873. #define DUK_NOINLINE __attribute__((noinline))
  1874. #define DUK_INLINE inline
  1875. #define DUK_ALWAYS_INLINE inline __attribute__((always_inline))
  1876. #endif
  1877. #endif
  1878. #if !defined(DUK_NOINLINE)
  1879. #define DUK_NOINLINE /*nop*/
  1880. #define DUK_INLINE /*nop*/
  1881. #define DUK_ALWAYS_INLINE /*nop*/
  1882. #endif
  1883. /* Temporary workaround for GH-323: avoid inlining control when
  1884. * compiling from multiple sources, as it causes compiler trouble.
  1885. */
  1886. #if !defined(DUK_SINGLE_FILE)
  1887. #undef DUK_NOINLINE
  1888. #undef DUK_INLINE
  1889. #undef DUK_ALWAYS_INLINE
  1890. #define DUK_NOINLINE /*nop*/
  1891. #define DUK_INLINE /*nop*/
  1892. #define DUK_ALWAYS_INLINE /*nop*/
  1893. #endif
  1894. /*
  1895. * Symbol visibility macros
  1896. *
  1897. * To avoid C++ declaration issues (see GH-63):
  1898. *
  1899. * - Don't use DUK_LOCAL_DECL for local -data symbols- so that you don't
  1900. * end up with both a "static" declaration and a definition.
  1901. *
  1902. * - Wrap any DUK_INTERNAL_DECL with a '#if !defined(DUK_SINGLE_FILE)'
  1903. * so that the internal declarations (which would map to "static" in
  1904. * a single file distribution) get dropped.
  1905. */
  1906. /* XXX: user override for these? user override for just using the default visibility macros? */
  1907. /* XXX: separate macros for function and data may be necessary at some point. */
  1908. #if defined(DUK_F_GCC_VERSION)
  1909. #if (DUK_F_GCC_VERSION >= 40000) && !(defined(DUK_F_MINGW) || defined(DUK_F_CYGWIN))
  1910. /* Might work on earlier versions too but limit to GCC 4+.
  1911. * MinGW should use Windows specific __declspec or no visibility attributes at all,
  1912. * otherwise: "warning: visibility attribute not supported in this configuration; ignored".
  1913. * Same applies to Cygwin GCC.
  1914. */
  1915. #define DUK_F_GCC_SYMBOL_VISIBILITY
  1916. #endif
  1917. #endif
  1918. #if defined(DUK_F_CLANG) && !defined(DUK_F_GCC_SYMBOL_VISIBILITY)
  1919. #define DUK_F_GCC_SYMBOL_VISIBILITY
  1920. #endif
  1921. #if defined(DUK_OPT_DLL_BUILD) && defined(_WIN32) && (defined(_MSC_VER) || defined(__GNUC__))
  1922. /* __declspec(dllexport) and __declspec(dllimport) only for Windows DLL build.
  1923. * MSVC: any minimum version?
  1924. * MinGW: no minimum version, even gcc-2.95.3 supported dllimport/dllexport.
  1925. */
  1926. #define DUK_F_MSVC_DLL_SYMBOL_VISIBILITY
  1927. #endif
  1928. #if defined(DUK_F_GCC_SYMBOL_VISIBILITY)
  1929. /* GCC 4+ visibility attributes. */
  1930. #define DUK_EXTERNAL_DECL __attribute__ ((visibility("default"))) extern
  1931. #define DUK_EXTERNAL __attribute__ ((visibility("default")))
  1932. #if defined(DUK_SINGLE_FILE)
  1933. #define DUK_INTERNAL_DECL static
  1934. #define DUK_INTERNAL static
  1935. #else
  1936. #define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) extern
  1937. #define DUK_INTERNAL __attribute__ ((visibility("hidden")))
  1938. #endif
  1939. #elif defined(DUK_F_MSVC_DLL_SYMBOL_VISIBILITY)
  1940. /* MSVC dllexport/dllimport: appropriate __declspec depends on whether we're
  1941. * compiling Duktape or the application.
  1942. */
  1943. #if defined(DUK_COMPILING_DUKTAPE)
  1944. #define DUK_EXTERNAL_DECL extern __declspec(dllexport)
  1945. #define DUK_EXTERNAL __declspec(dllexport)
  1946. #else
  1947. #define DUK_EXTERNAL_DECL extern __declspec(dllimport)
  1948. #define DUK_EXTERNAL should_not_happen
  1949. #endif
  1950. #if defined(DUK_SINGLE_FILE)
  1951. #define DUK_INTERNAL_DECL static
  1952. #define DUK_INTERNAL static
  1953. #else
  1954. #define DUK_INTERNAL_DECL extern
  1955. #define DUK_INTERNAL /*empty*/
  1956. #endif
  1957. #else
  1958. /* Default visibility. */
  1959. #define DUK_EXTERNAL_DECL extern
  1960. #define DUK_EXTERNAL /*empty*/
  1961. #if defined(DUK_SINGLE_FILE)
  1962. #define DUK_INTERNAL_DECL static
  1963. #define DUK_INTERNAL static
  1964. #else /* DUK_SINGLE_FILE */
  1965. #define DUK_INTERNAL_DECL extern
  1966. #define DUK_INTERNAL /*empty*/
  1967. #endif
  1968. #endif
  1969. /* For now, these are shared. */
  1970. #define DUK_LOCAL_DECL static
  1971. #define DUK_LOCAL static
  1972. /*
  1973. * __FILE__, __LINE__, __func__ are wrapped. Especially __func__ is a
  1974. * problem because it is not available even in some compilers which try
  1975. * to be C99 compatible (e.g. VBCC with -c99 option).
  1976. */
  1977. #define DUK_FILE_MACRO __FILE__
  1978. #define DUK_LINE_MACRO __LINE__
  1979. #if !defined(DUK_F_VBCC) && !defined(DUK_F_MSVC)
  1980. #define DUK_FUNC_MACRO __func__
  1981. #else
  1982. #define DUK_FUNC_MACRO "unknown"
  1983. #endif
  1984. /*
  1985. * Byteswap macros
  1986. *
  1987. * These are here so that inline assembly or other platform functions can be
  1988. * used if available.
  1989. */
  1990. #define DUK_BSWAP32(x) \
  1991. ((((duk_uint32_t) (x)) >> 24) | \
  1992. ((((duk_uint32_t) (x)) >> 8) & 0xff00UL) | \
  1993. ((((duk_uint32_t) (x)) << 8) & 0xff0000UL) | \
  1994. (((duk_uint32_t) (x)) << 24))
  1995. #define DUK_BSWAP16(x) \
  1996. ((duk_uint16_t) (x) >> 8) | \
  1997. ((duk_uint16_t) (x) << 8)
  1998. /*
  1999. * Architecture string, human readable value exposed in Duktape.env
  2000. */
  2001. #if defined(DUK_F_X86)
  2002. #define DUK_USE_ARCH_STRING "x86"
  2003. #elif defined(DUK_F_X32)
  2004. #define DUK_USE_ARCH_STRING "x32"
  2005. #elif defined(DUK_F_X64)
  2006. #define DUK_USE_ARCH_STRING "x64"
  2007. #elif defined(DUK_F_ARM)
  2008. #define DUK_USE_ARCH_STRING "arm"
  2009. #elif defined(DUK_F_MIPS32)
  2010. #define DUK_USE_ARCH_STRING "mips32"
  2011. #elif defined(DUK_F_MIPS64)
  2012. #define DUK_USE_ARCH_STRING "mips64"
  2013. #elif defined(DUK_F_SUPERH)
  2014. #define DUK_USE_ARCH_STRING "sh"
  2015. #elif defined(DUK_F_PPC)
  2016. #define DUK_USE_ARCH_STRING "ppc"
  2017. #elif defined(DUK_F_M68K)
  2018. #define DUK_USE_ARCH_STRING "m68k"
  2019. #elif defined(DUK_F_FLASHPLAYER)
  2020. #define DUK_USE_ARCH_STRING "flashplayer"
  2021. #elif defined(DUK_F_EMSCRIPTEN)
  2022. #define DUK_USE_ARCH_STRING "emscripten"
  2023. #else
  2024. #define DUK_USE_ARCH_STRING "unknown"
  2025. #endif
  2026. /*
  2027. * OS string, human readable value exposed in Duktape.env
  2028. */
  2029. #if defined(DUK_F_LINUX)
  2030. #define DUK_USE_OS_STRING "linux"
  2031. #elif defined(__APPLE__)
  2032. /* http://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor */
  2033. #if TARGET_IPHONE_SIMULATOR
  2034. #define DUK_USE_OS_STRING "iphone-sim"
  2035. #elif TARGET_OS_IPHONE
  2036. #define DUK_USE_OS_STRING "iphone"
  2037. #elif TARGET_OS_MAC
  2038. #define DUK_USE_OS_STRING "ios"
  2039. #else
  2040. #define DUK_USE_OS_STRING "ios-unknown"
  2041. #endif
  2042. #elif defined(DUK_F_FREEBSD)
  2043. #define DUK_USE_OS_STRING "freebsd"
  2044. #elif defined(DUK_F_OPENBSD)
  2045. #define DUK_USE_OS_STRING "openbsd"
  2046. #elif defined(DUK_F_NETBSD)
  2047. #define DUK_USE_OS_STRING "netbsd"
  2048. #elif defined(DUK_F_BSD)
  2049. #define DUK_USE_OS_STRING "bsd"
  2050. #elif defined(DUK_F_UNIX)
  2051. #define DUK_USE_OS_STRING "unix"
  2052. #elif defined(DUK_F_WINDOWS)
  2053. #define DUK_USE_OS_STRING "windows"
  2054. #elif defined(DUK_F_TOS)
  2055. #define DUK_USE_OS_STRING "tos"
  2056. #elif defined(DUK_F_AMIGAOS)
  2057. #define DUK_USE_OS_STRING "amigaos"
  2058. #elif defined(DUK_F_QNX)
  2059. #define DUK_USE_OS_STRING "qnx"
  2060. #elif defined(DUK_F_TINSPIRE)
  2061. #define DUK_USE_OS_STRING "tinspire"
  2062. #else
  2063. #define DUK_USE_OS_STRING "unknown"
  2064. #endif
  2065. /*
  2066. * Compiler string, human readable value exposed in Duktape.env
  2067. */
  2068. #if defined(DUK_F_MINGW)
  2069. #define DUK_USE_COMPILER_STRING "mingw"
  2070. #elif defined(DUK_F_GCC)
  2071. #if defined(DUK_F_CPP)
  2072. #define DUK_USE_COMPILER_STRING "g++"
  2073. #else
  2074. #define DUK_USE_COMPILER_STRING "gcc"
  2075. #endif
  2076. #elif defined(DUK_F_CLANG)
  2077. #define DUK_USE_COMPILER_STRING "clang"
  2078. #elif defined(DUK_F_MSVC)
  2079. #define DUK_USE_COMPILER_STRING "msvc"
  2080. #elif defined(DUK_F_VBCC)
  2081. #define DUK_USE_COMPILER_STRING "vbcc"
  2082. #else
  2083. #define DUK_USE_COMPILER_STRING "unknown"
  2084. #endif
  2085. /*
  2086. * Target info string
  2087. */
  2088. #if defined(DUK_OPT_TARGET_INFO)
  2089. #define DUK_USE_TARGET_INFO DUK_OPT_TARGET_INFO
  2090. #else
  2091. #define DUK_USE_TARGET_INFO "unknown"
  2092. #endif
  2093. /*
  2094. * Long control transfer, setjmp/longjmp or alternatives
  2095. *
  2096. * Signal mask is not saved (when that can be communicated to the platform)
  2097. */
  2098. /* dummy non-zero value to be used as an argument for longjmp(), see man longjmp */
  2099. #define DUK_LONGJMP_DUMMY_VALUE 1
  2100. #if defined(DUK_OPT_SETJMP)
  2101. #define DUK_USE_SETJMP
  2102. #elif defined(DUK_OPT_UNDERSCORE_SETJMP)
  2103. #define DUK_USE_UNDERSCORE_SETJMP
  2104. #elif defined(DUK_OPT_SIGSETJMP)
  2105. #define DUK_USE_SIGSETJMP
  2106. #elif defined(__APPLE__)
  2107. /* Use _setjmp() on Apple by default, see GH-55. */
  2108. #define DUK_USE_UNDERSCORE_SETJMP
  2109. #else
  2110. /* The most portable default is setjmp(). */
  2111. #define DUK_USE_SETJMP
  2112. #endif
  2113. #if defined(DUK_USE_UNDERSCORE_SETJMP)
  2114. #define DUK_SETJMP(jb) _setjmp((jb))
  2115. #define DUK_LONGJMP(jb) _longjmp((jb), DUK_LONGJMP_DUMMY_VALUE)
  2116. #elif defined(DUK_USE_SIGSETJMP)
  2117. #define DUK_SETJMP(jb) sigsetjmp((jb), 0 /*savesigs*/)
  2118. #define DUK_LONGJMP(jb) siglongjmp((jb), DUK_LONGJMP_DUMMY_VALUE)
  2119. #elif defined(DUK_USE_SETJMP)
  2120. #define DUK_SETJMP(jb) setjmp((jb))
  2121. #define DUK_LONGJMP(jb) longjmp((jb), DUK_LONGJMP_DUMMY_VALUE)
  2122. #else
  2123. #error internal error
  2124. #endif
  2125. /*
  2126. * Speed/size and other performance options
  2127. */
  2128. /* Use fast ("inline") refcount operations instead of calling out to helpers
  2129. * by default. The difference in binary size is small (~1kB on x64).
  2130. */
  2131. #define DUK_USE_FAST_REFCOUNT_DEFAULT
  2132. /* Assert for valstack space but don't check for it in non-assert build.
  2133. * Valstack overruns (writing beyond checked space) is memory unsafe and
  2134. * potentially a segfault. Produces a smaller and faster binary.
  2135. * (In practice the speed difference is small with -O3 so default to
  2136. * safer behavior for now.)
  2137. */
  2138. #undef DUK_USE_VALSTACK_UNSAFE
  2139. /* Catch-all flag which can be used to choose between variant algorithms
  2140. * where a speed-size tradeoff exists (e.g. lookup tables). When it really
  2141. * matters, specific use flags may be appropriate.
  2142. */
  2143. #undef DUK_USE_PREFER_SIZE
  2144. /* Use a sliding window for lexer; slightly larger footprint, slightly faster. */
  2145. #define DUK_USE_LEXER_SLIDING_WINDOW
  2146. /* Transparent JSON.stringify() fastpath. */
  2147. #undef DUK_USE_JSON_STRINGIFY_FASTPATH
  2148. #if defined(DUK_OPT_JSON_STRINGIFY_FASTPATH)
  2149. #define DUK_USE_JSON_STRINGIFY_FASTPATH
  2150. #endif
  2151. /*
  2152. * Tagged type representation (duk_tval)
  2153. */
  2154. #undef DUK_USE_PACKED_TVAL
  2155. #undef DUK_USE_FULL_TVAL
  2156. #if defined(DUK_USE_PACKED_TVAL_POSSIBLE) && !defined(DUK_OPT_NO_PACKED_TVAL)
  2157. #define DUK_USE_PACKED_TVAL
  2158. #endif
  2159. /* Support for 48-bit signed integer duk_tval with transparent semantics. */
  2160. #undef DUK_USE_FASTINT
  2161. #if defined(DUK_OPT_FASTINT)
  2162. #if !defined(DUK_F_HAVE_64BIT)
  2163. #error DUK_OPT_FASTINT requires 64-bit integer type support at the moment
  2164. #endif
  2165. #define DUK_USE_FASTINT
  2166. #endif
  2167. /*
  2168. * Memory management options
  2169. */
  2170. #define DUK_USE_REFERENCE_COUNTING
  2171. #define DUK_USE_DOUBLE_LINKED_HEAP
  2172. #define DUK_USE_MARK_AND_SWEEP
  2173. #define DUK_USE_MS_STRINGTABLE_RESIZE
  2174. #if defined(DUK_OPT_NO_REFERENCE_COUNTING)
  2175. #undef DUK_USE_REFERENCE_COUNTING
  2176. #undef DUK_USE_DOUBLE_LINKED_HEAP
  2177. /* XXX: undef DUK_USE_MS_STRINGTABLE_RESIZE as it is more expensive
  2178. * with more frequent mark-and-sweeps?
  2179. */
  2180. #endif
  2181. #if defined(DUK_OPT_NO_MARK_AND_SWEEP)
  2182. #undef DUK_USE_MARK_AND_SWEEP
  2183. #endif
  2184. #if defined(DUK_USE_MARK_AND_SWEEP)
  2185. #define DUK_USE_VOLUNTARY_GC
  2186. #if defined(DUK_OPT_NO_VOLUNTARY_GC)
  2187. #undef DUK_USE_VOLUNTARY_GC
  2188. #endif
  2189. #endif
  2190. #if !defined(DUK_USE_MARK_AND_SWEEP) && !defined(DUK_USE_REFERENCE_COUNTING)
  2191. #error must have either mark-and-sweep or reference counting enabled
  2192. #endif
  2193. #if defined(DUK_OPT_NO_MS_STRINGTABLE_RESIZE)
  2194. #undef DUK_USE_MS_STRINGTABLE_RESIZE
  2195. #endif
  2196. #undef DUK_USE_GC_TORTURE
  2197. #if defined(DUK_OPT_GC_TORTURE)
  2198. #define DUK_USE_GC_TORTURE
  2199. #endif
  2200. /*
  2201. * String table options
  2202. */
  2203. #if defined(DUK_OPT_STRTAB_CHAIN) && defined(DUK_OPT_STRTAB_CHAIN_SIZE)
  2204. /* Low memory algorithm: separate chaining using arrays, fixed size hash */
  2205. #define DUK_USE_STRTAB_CHAIN
  2206. #define DUK_USE_STRTAB_CHAIN_SIZE DUK_OPT_STRTAB_CHAIN_SIZE
  2207. #else
  2208. /* Default algorithm: open addressing (probing) */
  2209. #define DUK_USE_STRTAB_PROBE
  2210. #endif
  2211. /*
  2212. * Error handling options
  2213. */
  2214. #define DUK_USE_AUGMENT_ERROR_CREATE
  2215. #define DUK_USE_AUGMENT_ERROR_THROW
  2216. #define DUK_USE_TRACEBACKS
  2217. #define DUK_USE_ERRCREATE
  2218. #define DUK_USE_ERRTHROW
  2219. #define DUK_USE_VERBOSE_ERRORS
  2220. #if defined(DUK_OPT_NO_AUGMENT_ERRORS)
  2221. #undef DUK_USE_AUGMENT_ERROR_CREATE
  2222. #undef DUK_USE_AUGMENT_ERROR_THROW
  2223. #undef DUK_USE_TRACEBACKS
  2224. #undef DUK_USE_ERRCREATE
  2225. #undef DUK_USE_ERRTHROW
  2226. #elif defined(DUK_OPT_NO_TRACEBACKS)
  2227. #undef DUK_USE_TRACEBACKS
  2228. #endif
  2229. #if defined(DUK_OPT_NO_VERBOSE_ERRORS)
  2230. #undef DUK_USE_VERBOSE_ERRORS
  2231. #endif
  2232. #if defined(DUK_USE_TRACEBACKS)
  2233. #if defined(DUK_OPT_TRACEBACK_DEPTH)
  2234. #define DUK_USE_TRACEBACK_DEPTH DUK_OPT_TRACEBACK_DEPTH
  2235. #else
  2236. #define DUK_USE_TRACEBACK_DEPTH 10
  2237. #endif
  2238. #endif
  2239. /* Include messages in executor internal errors. */
  2240. #define DUK_USE_VERBOSE_EXECUTOR_ERRORS
  2241. /*
  2242. * Execution and debugger options
  2243. */
  2244. #undef DUK_USE_INTERRUPT_COUNTER
  2245. #if defined(DUK_OPT_INTERRUPT_COUNTER)
  2246. #define DUK_USE_INTERRUPT_COUNTER
  2247. #endif
  2248. #undef DUK_USE_EXEC_TIMEOUT_CHECK
  2249. #if defined(DUK_OPT_EXEC_TIMEOUT_CHECK)
  2250. #define DUK_USE_EXEC_TIMEOUT_CHECK(udata) DUK_OPT_EXEC_TIMEOUT_CHECK((udata))
  2251. #endif
  2252. #undef DUK_USE_DEBUGGER_SUPPORT
  2253. #if defined(DUK_OPT_DEBUGGER_SUPPORT)
  2254. #define DUK_USE_DEBUGGER_SUPPORT
  2255. #endif
  2256. #undef DUK_USE_DEBUGGER_FWD_PRINTALERT
  2257. #if defined(DUK_OPT_DEBUGGER_SUPPORT) && defined(DUK_OPT_DEBUGGER_FWD_PRINTALERT)
  2258. #define DUK_USE_DEBUGGER_FWD_PRINTALERT
  2259. #endif
  2260. #undef DUK_USE_DEBUGGER_FWD_LOGGING
  2261. #if defined(DUK_OPT_DEBUGGER_SUPPORT) && defined(DUK_OPT_DEBUGGER_FWD_LOGGING)
  2262. #define DUK_USE_DEBUGGER_FWD_LOGGING
  2263. #endif
  2264. /* DumpHeap is optional because it's not always needed and has a relatively
  2265. * large footprint.
  2266. */
  2267. #undef DUK_USE_DEBUGGER_DUMPHEAP
  2268. #if defined(DUK_OPT_DEBUGGER_DUMPHEAP)
  2269. #define DUK_USE_DEBUGGER_DUMPHEAP
  2270. #endif
  2271. #define DUK_USE_DEBUGGER_THROW_NOTIFY
  2272. #if defined(DUK_OPT_NO_DEBUGGER_THROW_NOTIFY)
  2273. #undef DUK_USE_DEBUGGER_THROW_NOTIFY
  2274. #endif
  2275. #undef DUK_USE_DEBUGGER_PAUSE_UNCAUGHT
  2276. #if defined(DUK_OPT_DEBUGGER_PAUSE_UNCAUGHT)
  2277. #define DUK_USE_DEBUGGER_PAUSE_UNCAUGHT
  2278. #endif
  2279. /* Debugger transport read/write torture. */
  2280. #undef DUK_USE_DEBUGGER_TRANSPORT_TORTURE
  2281. #if defined(DUK_OPT_DEBUGGER_TRANSPORT_TORTURE)
  2282. #define DUK_USE_DEBUGGER_TRANSPORT_TORTURE
  2283. #endif
  2284. /* For opcodes with indirect indices, check final index against stack size.
  2285. * This should not be necessary because the compiler is trusted, and we don't
  2286. * bound check non-indirect indices either.
  2287. */
  2288. #undef DUK_USE_EXEC_INDIRECT_BOUND_CHECK
  2289. #if defined(DUK_OPT_DEBUG) || defined(DUK_OPT_ASSERTIONS)
  2290. /* Enabled with debug/assertions just so that any issues can be caught. */
  2291. #define DUK_USE_EXEC_INDIRECT_BOUND_CHECK
  2292. #endif
  2293. /*
  2294. * Debug printing and assertion options
  2295. */
  2296. #undef DUK_USE_DEBUG
  2297. #undef DUK_USE_DPRINT
  2298. #undef DUK_USE_DDPRINT
  2299. #undef DUK_USE_DDDPRINT
  2300. #undef DUK_USE_DPRINT_RDTSC
  2301. #undef DUK_USE_ASSERTIONS
  2302. /* Global debug enable. Compile must be clean on C99 regardless of whether or
  2303. * not debugging is enabled. On non-C99 platforms compile should be clean with
  2304. * debugging disabled but may produce warnings with debugging enabled (related
  2305. * to debug macro hackery and such).
  2306. */
  2307. #if defined(DUK_OPT_DEBUG)
  2308. #define DUK_USE_DEBUG
  2309. #endif
  2310. #if defined(DUK_OPT_DEBUG) && defined(DUK_OPT_DPRINT)
  2311. #define DUK_USE_DPRINT
  2312. #endif
  2313. #if defined(DUK_OPT_DEBUG) && defined(DUK_OPT_DDPRINT)
  2314. #define DUK_USE_DDPRINT
  2315. #endif
  2316. #if defined(DUK_OPT_DEBUG) && defined(DUK_OPT_DDDPRINT)
  2317. #define DUK_USE_DDDPRINT
  2318. #endif
  2319. #undef DUK_USE_DPRINT_COLORS
  2320. #if defined(DUK_OPT_DPRINT_COLORS)
  2321. #define DUK_USE_DPRINT_COLORS
  2322. #endif
  2323. #if defined(DUK_USE_RDTSC) && defined(DUK_OPT_DPRINT_RDTSC)
  2324. #define DUK_USE_DPRINT_RDTSC
  2325. #else
  2326. #undef DUK_USE_DPRINT_RDTSC
  2327. #endif
  2328. #if defined(DUK_OPT_ASSERTIONS)
  2329. #define DUK_USE_ASSERTIONS
  2330. #endif
  2331. /* The static buffer for debug printing is quite large by default, so there
  2332. * is an option to shrink it manually for constrained builds.
  2333. */
  2334. #if defined(DUK_OPT_DEBUG_BUFSIZE)
  2335. #define DUK_USE_DEBUG_BUFSIZE DUK_OPT_DEBUG_BUFSIZE
  2336. #else
  2337. #define DUK_USE_DEBUG_BUFSIZE 65536L
  2338. #endif
  2339. /*
  2340. * Ecmascript features / compliance options
  2341. */
  2342. #if defined(DUK_F_BCC)
  2343. /* Math built-in is stubbed out on BCC to allow compiler torture testing. */
  2344. #else
  2345. #define DUK_USE_MATH_BUILTIN
  2346. #endif
  2347. #define DUK_USE_STRICT_DECL
  2348. #if defined(DUK_OPT_NO_STRICT_DECL)
  2349. #undef DUK_USE_STRICT_DECL
  2350. #endif
  2351. #define DUK_USE_REGEXP_SUPPORT
  2352. #if defined(DUK_OPT_NO_REGEXP_SUPPORT)
  2353. #undef DUK_USE_REGEXP_SUPPORT
  2354. #endif
  2355. #undef DUK_USE_STRICT_UTF8_SOURCE
  2356. #if defined(DUK_OPT_STRICT_UTF8_SOURCE)
  2357. #define DUK_USE_STRICT_UTF8_SOURCE
  2358. #endif
  2359. #define DUK_USE_OCTAL_SUPPORT
  2360. #if defined(DUK_OPT_NO_OCTAL_SUPPORT)
  2361. #undef DUK_USE_OCTAL_SUPPORT
  2362. #endif
  2363. #define DUK_USE_SOURCE_NONBMP
  2364. #if defined(DUK_OPT_NO_SOURCE_NONBMP)
  2365. #undef DUK_USE_SOURCE_NONBMP
  2366. #endif
  2367. #define DUK_USE_BROWSER_LIKE
  2368. #if defined(DUK_OPT_NO_BROWSER_LIKE)
  2369. #undef DUK_USE_BROWSER_LIKE
  2370. #endif
  2371. /* E5/E5.1 Section B features. */
  2372. #define DUK_USE_SECTION_B
  2373. #if defined(DUK_OPT_NO_SECTION_B)
  2374. #undef DUK_USE_SECTION_B
  2375. #endif
  2376. /* Non-standard regexp parsing features. */
  2377. #define DUK_USE_NONSTD_REGEXP_DOLLAR_ESCAPE
  2378. /* Treat function statements (function declarations outside top level of
  2379. * Program or FunctionBody) same as normal function declarations. This is
  2380. * also V8 behavior. See test-dev-func-decl-outside-top.js.
  2381. */
  2382. #define DUK_USE_NONSTD_FUNC_STMT
  2383. #if defined(DUK_OPT_NO_NONSTD_FUNC_STMT)
  2384. #undef DUK_USE_NONSTD_FUNC_STMT
  2385. #endif
  2386. /* Array.prototype.splice() non-standard but real world compatible behavior
  2387. * when deleteCount is omitted.
  2388. */
  2389. #define DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT
  2390. #if defined(DUK_OPT_NO_NONSTD_ARRAY_SPLICE_DELCOUNT)
  2391. #undef DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT
  2392. #endif
  2393. /* Array.prototype.concat() non-standard but real world compatible behavior
  2394. * for non-existent trailing elements.
  2395. */
  2396. #define DUK_USE_NONSTD_ARRAY_CONCAT_TRAILER
  2397. #if defined(DUK_OPT_NO_NONSTD_ARRAY_CONCAT_TRAILER)
  2398. #undef DUK_USE_NONSTD_ARRAY_CONCAT_TRAILER
  2399. #endif
  2400. /* Array.prototype.map() non-standard but real world compatible behavior
  2401. * for non-existent trailing elements.
  2402. */
  2403. #define DUK_USE_NONSTD_ARRAY_MAP_TRAILER
  2404. #if defined(DUK_OPT_NO_NONSTD_ARRAY_MAP_TRAILER)
  2405. #undef DUK_USE_NONSTD_ARRAY_MAP_TRAILER
  2406. #endif
  2407. /* Non-standard 'caller' property for function instances, see
  2408. * test-bi-function-nonstd-caller-prop.js.
  2409. */
  2410. #undef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  2411. #if defined(DUK_OPT_NONSTD_FUNC_CALLER_PROPERTY)
  2412. #define DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  2413. #endif
  2414. /* Non-standard Object.prototype.__proto__ (ES6), see
  2415. * test-bi-object-proto-__proto__.js.
  2416. */
  2417. #define DUK_USE_ES6_OBJECT_PROTO_PROPERTY
  2418. #if defined(DUK_OPT_NO_ES6_OBJECT_PROTO_PROPERTY)
  2419. #undef DUK_USE_ES6_OBJECT_PROTO_PROPERTY
  2420. #endif
  2421. /* Non-standard Object.setPrototypeOf (ES6), see
  2422. * test-bi-object-setprototypeof.js.
  2423. */
  2424. #define DUK_USE_ES6_OBJECT_SETPROTOTYPEOF
  2425. #if defined(DUK_OPT_NO_ES6_OBJECT_SETPROTOTYPEOF)
  2426. #undef DUK_USE_ES6_OBJECT_SETPROTOTYPEOF
  2427. #endif
  2428. /* ES6 Proxy object (subset for now). */
  2429. #define DUK_USE_ES6_PROXY
  2430. #if defined(DUK_OPT_NO_ES6_PROXY)
  2431. #undef DUK_USE_ES6_PROXY
  2432. #endif
  2433. /* Record pc-to-line information. */
  2434. #define DUK_USE_PC2LINE
  2435. #if defined(DUK_OPT_NO_PC2LINE)
  2436. #undef DUK_USE_PC2LINE
  2437. #endif
  2438. /* Non-standard function 'source' property. */
  2439. #undef DUK_USE_NONSTD_FUNC_SOURCE_PROPERTY
  2440. #if defined(DUK_OPT_NONSTD_FUNC_SOURCE_PROPERTY)
  2441. #define DUK_USE_NONSTD_FUNC_SOURCE_PROPERTY
  2442. #endif
  2443. /* CommonJS modules */
  2444. #define DUK_USE_COMMONJS_MODULES
  2445. #if defined(DUK_OPT_NO_COMMONJS_MODULES)
  2446. #undef DUK_USE_COMMONJS_MODULES
  2447. #endif
  2448. /* Additional key argument to setter/getter calls when triggered by property
  2449. * accesses.
  2450. */
  2451. #define DUK_USE_NONSTD_GETTER_KEY_ARGUMENT
  2452. #define DUK_USE_NONSTD_SETTER_KEY_ARGUMENT
  2453. #if defined(DUK_OPT_NO_NONSTD_ACCESSOR_KEY_ARGUMENT)
  2454. #undef DUK_USE_NONSTD_GETTER_KEY_ARGUMENT
  2455. #undef DUK_USE_NONSTD_SETTER_KEY_ARGUMENT
  2456. #endif
  2457. /* JSON escaping of U+2028 and U+2029.
  2458. */
  2459. #define DUK_USE_NONSTD_JSON_ESC_U2028_U2029
  2460. #if defined(DUK_OPT_NO_NONSTD_JSON_ESC_U2028_U2029)
  2461. #undef DUK_USE_NONSTD_JSON_ESC_U2028_U2029
  2462. #endif
  2463. /* Allow 32-bit codepoints in String.fromCharCode. */
  2464. #define DUK_USE_NONSTD_STRING_FROMCHARCODE_32BIT
  2465. #if defined(DUK_OPT_NO_NONSTD_STRING_FROMCHARCODE_32BIT)
  2466. #undef DUK_USE_NONSTD_STRING_FROMCHARCODE_32BIT
  2467. #endif
  2468. /* Non-standard array fast path write behavior: when writing to numeric
  2469. * indexes of an Array instance, assume Array.prototype doesn't have
  2470. * conflicting properties (e.g. a non-writable property "7").
  2471. */
  2472. #define DUK_USE_NONSTD_ARRAY_WRITE
  2473. #if defined(DUK_OPT_NO_NONSTD_ARRAY_WRITE)
  2474. #undef DUK_USE_NONSTD_ARRAY_WRITE
  2475. #endif
  2476. /* Node.js Buffer and Khronos/ES6 typed array support. */
  2477. #define DUK_USE_BUFFEROBJECT_SUPPORT
  2478. #if defined(DUK_OPT_NO_BUFFEROBJECT_SUPPORT)
  2479. #undef DUK_USE_BUFFEROBJECT_SUPPORT
  2480. #endif
  2481. /*
  2482. * Optional C API options
  2483. */
  2484. #define DUK_USE_BYTECODE_DUMP_SUPPORT
  2485. #if defined(DUK_OPT_NO_BYTECODE_DUMP_SUPPORT)
  2486. #undef DUK_USE_BYTECODE_DUMP_SUPPORT
  2487. #endif
  2488. /*
  2489. * Tailcalls
  2490. */
  2491. /* Tailcalls are enabled by default. The non-standard function 'caller'
  2492. * property feature conflicts with tailcalls quite severely so tailcalls
  2493. * are disabled if the 'caller' property is enabled.
  2494. */
  2495. #define DUK_USE_TAILCALL
  2496. #if defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY)
  2497. #undef DUK_USE_TAILCALL
  2498. #endif
  2499. /*
  2500. * Ecmascript compiler
  2501. */
  2502. /* Ensure final bytecode never exceeds a certain byte size and never uses
  2503. * line numbers above a certain limit. This ensures that there is no need
  2504. * to deal with unbounded ranges in e.g. pc2line data structures. For now,
  2505. * limits are set so that signed 32-bit values can represent line number
  2506. * and byte offset with room to spare.
  2507. */
  2508. #define DUK_USE_ESBC_LIMITS
  2509. #define DUK_USE_ESBC_MAX_LINENUMBER 0x7fff0000L
  2510. #define DUK_USE_ESBC_MAX_BYTES 0x7fff0000L
  2511. #undef DUK_USE_SHUFFLE_TORTURE
  2512. #if defined(DUK_OPT_SHUFFLE_TORTURE)
  2513. #define DUK_USE_SHUFFLE_TORTURE
  2514. #endif
  2515. /*
  2516. * User panic handler, panic exit behavior for default panic handler
  2517. */
  2518. #undef DUK_USE_PANIC_HANDLER
  2519. #if defined(DUK_OPT_PANIC_HANDLER)
  2520. #define DUK_USE_PANIC_HANDLER(code,msg) DUK_OPT_PANIC_HANDLER((code),(msg))
  2521. #endif
  2522. #undef DUK_USE_PANIC_ABORT
  2523. #undef DUK_USE_PANIC_EXIT
  2524. #undef DUK_USE_PANIC_SEGFAULT
  2525. #if defined(DUK_OPT_SEGFAULT_ON_PANIC)
  2526. #define DUK_USE_PANIC_SEGFAULT
  2527. #else
  2528. #define DUK_USE_PANIC_ABORT
  2529. #endif
  2530. /*
  2531. * File I/O support. This is now used in a few API calls to e.g. push
  2532. * a string from file contents or eval a file. For portability it must
  2533. * be possible to disable I/O altogether.
  2534. */
  2535. #undef DUK_USE_FILE_IO
  2536. #if !defined(DUK_OPT_NO_FILE_IO)
  2537. #define DUK_USE_FILE_IO
  2538. #endif
  2539. /*
  2540. * Optional run-time self tests executed when a heap is created. Some
  2541. * platform/compiler issues cannot be determined at compile time. One
  2542. * particular example is the bug described in misc/clang_aliasing.c.
  2543. */
  2544. #undef DUK_USE_SELF_TESTS
  2545. #if defined(DUK_OPT_SELF_TESTS)
  2546. #define DUK_USE_SELF_TESTS
  2547. #endif
  2548. /* Double aliasing testcase fails when Emscripten-generated code is run
  2549. * on Firefox. This is not fatal because it only affects packed duk_tval
  2550. * which we avoid with Emscripten.
  2551. */
  2552. #undef DUK_USE_NO_DOUBLE_ALIASING_SELFTEST
  2553. #if defined(DUK_F_EMSCRIPTEN)
  2554. #define DUK_USE_NO_DOUBLE_ALIASING_SELFTEST
  2555. #endif
  2556. /*
  2557. * Codecs
  2558. */
  2559. #define DUK_USE_JX
  2560. #if defined(DUK_OPT_NO_JX)
  2561. #undef DUK_USE_JX
  2562. #endif
  2563. #define DUK_USE_JC
  2564. #if defined(DUK_OPT_NO_JC)
  2565. #undef DUK_USE_JC
  2566. #endif
  2567. /*
  2568. * InitJS code
  2569. */
  2570. /* Always use the built-in InitJS code for now. */
  2571. #define DUK_USE_BUILTIN_INITJS
  2572. /* User provided InitJS. */
  2573. #undef DUK_USE_USER_INITJS
  2574. #if defined(DUK_OPT_USER_INITJS)
  2575. #define DUK_USE_USER_INITJS (DUK_OPT_USER_INITJS)
  2576. #endif
  2577. /*
  2578. * External string data support
  2579. *
  2580. * Allow duk_hstrings to store data also behind an external pointer (see
  2581. * duk_hstring_external). This increases code size slightly but is useful
  2582. * in low memory environments where memory is more limited than flash.
  2583. */
  2584. #undef DUK_USE_HSTRING_EXTDATA
  2585. #if defined(DUK_OPT_EXTERNAL_STRINGS)
  2586. #define DUK_USE_HSTRING_EXTDATA
  2587. #endif
  2588. #undef DUK_USE_EXTSTR_INTERN_CHECK
  2589. #if defined(DUK_OPT_EXTERNAL_STRINGS) && defined(DUK_OPT_EXTSTR_INTERN_CHECK)
  2590. #define DUK_USE_EXTSTR_INTERN_CHECK(udata,ptr,len) DUK_OPT_EXTSTR_INTERN_CHECK((udata), (ptr), (len))
  2591. #endif
  2592. #undef DUK_USE_EXTSTR_FREE
  2593. #if defined(DUK_OPT_EXTERNAL_STRINGS) && defined(DUK_OPT_EXTSTR_FREE)
  2594. #define DUK_USE_EXTSTR_FREE(udata,ptr) DUK_OPT_EXTSTR_FREE((udata), (ptr))
  2595. #endif
  2596. /*
  2597. * Lightweight functions
  2598. */
  2599. /* Force built-ins to use lightfunc function pointers when possible. This
  2600. * makes the built-in functions non-compliant with respect to their property
  2601. * values and such, but is very useful in low memory environments (can save
  2602. * around 14kB of initial RAM footprint).
  2603. */
  2604. #undef DUK_USE_LIGHTFUNC_BUILTINS
  2605. #if defined(DUK_OPT_LIGHTFUNC_BUILTINS)
  2606. #define DUK_USE_LIGHTFUNC_BUILTINS
  2607. #endif
  2608. /*
  2609. * Pointer compression and 16-bit header fields for low memory environments
  2610. */
  2611. #undef DUK_USE_HEAPPTR16
  2612. #undef DUK_USE_HEAPPTR_ENC16
  2613. #undef DUK_USE_HEAPPTR_DEC16
  2614. #if defined(DUK_OPT_HEAPPTR16) && defined(DUK_OPT_HEAPPTR_ENC16) && defined(DUK_OPT_HEAPPTR_DEC16)
  2615. #define DUK_USE_HEAPPTR16
  2616. #define DUK_USE_HEAPPTR_ENC16(udata,ptr) DUK_OPT_HEAPPTR_ENC16((udata),(ptr))
  2617. #define DUK_USE_HEAPPTR_DEC16(udata,ptr) DUK_OPT_HEAPPTR_DEC16((udata),(ptr))
  2618. #endif
  2619. #undef DUK_USE_DATAPTR16
  2620. #undef DUK_USE_DATAPTR_ENC16
  2621. #undef DUK_USE_DATAPTR_DEC16
  2622. #if defined(DUK_OPT_DATAPTR16) && defined(DUK_OPT_DATAPTR_ENC16) && defined(DUK_OPT_DATAPTR_DEC16)
  2623. #define DUK_USE_DATAPTR16
  2624. #define DUK_USE_DATAPTR_ENC16(udata,ptr) DUK_OPT_DATAPTR_ENC16((udata),(ptr))
  2625. #define DUK_USE_DATAPTR_DEC16(udata,ptr) DUK_OPT_DATAPTR_DEC16((udata),(ptr))
  2626. #endif
  2627. #undef DUK_USE_FUNCPTR16
  2628. #undef DUK_USE_FUNCPTR_ENC16
  2629. #undef DUK_USE_FUNCPTR_DEC16
  2630. #if defined(DUK_OPT_FUNCPTR16) && defined(DUK_OPT_FUNCPTR_ENC16) && defined(DUK_OPT_FUNCPTR_DEC16)
  2631. #define DUK_USE_FUNCPTR16
  2632. #define DUK_USE_FUNCPTR_ENC16(udata,ptr) DUK_OPT_FUNCPTR_ENC16((udata),(ptr))
  2633. #define DUK_USE_FUNCPTR_DEC16(udata,ptr) DUK_OPT_FUNCPTR_DEC16((udata),(ptr))
  2634. #endif
  2635. #undef DUK_USE_REFCOUNT16
  2636. #if defined(DUK_OPT_REFCOUNT16)
  2637. #define DUK_USE_REFCOUNT16
  2638. #endif
  2639. #undef DUK_USE_STRHASH16
  2640. #if defined(DUK_OPT_STRHASH16)
  2641. #define DUK_USE_STRHASH16
  2642. #endif
  2643. #undef DUK_USE_STRLEN16
  2644. #if defined(DUK_OPT_STRLEN16)
  2645. #define DUK_USE_STRLEN16
  2646. #endif
  2647. #undef DUK_USE_BUFLEN16
  2648. #if defined(DUK_OPT_BUFLEN16)
  2649. #define DUK_USE_BUFLEN16
  2650. #endif
  2651. #undef DUK_USE_OBJSIZES16
  2652. #if defined(DUK_OPT_OBJSIZES16)
  2653. #define DUK_USE_OBJSIZES16
  2654. #endif
  2655. /* For now, hash part is dropped if and only if 16-bit object fields are used. */
  2656. #define DUK_USE_HOBJECT_HASH_PART
  2657. #if defined(DUK_USE_OBJSIZES16)
  2658. #undef DUK_USE_HOBJECT_HASH_PART
  2659. #endif
  2660. /*
  2661. * Miscellaneous
  2662. */
  2663. /* Convenience define: 32-bit pointers. 32-bit platforms are an important
  2664. * footprint optimization target, and this define allows e.g. struct sizes
  2665. * to be organized for compactness.
  2666. */
  2667. #undef DUK_USE_32BIT_PTRS
  2668. #if defined(DUK_UINTPTR_MAX) && !defined(DUK_UINTPTR_MAX_COMPUTED)
  2669. #if DUK_UINTPTR_MAX <= 0xffffffffUL
  2670. #define DUK_USE_32BIT_PTRS
  2671. #endif
  2672. #endif
  2673. #define DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS
  2674. #undef DUK_USE_EXPLICIT_NULL_INIT
  2675. #define DUK_USE_ZERO_BUFFER_DATA
  2676. #if defined(DUK_OPT_NO_ZERO_BUFFER_DATA)
  2677. #undef DUK_USE_ZERO_BUFFER_DATA
  2678. #endif
  2679. #undef DUK_USE_VARIADIC_MACROS
  2680. #if defined(DUK_F_C99) || (defined(DUK_F_CPP11) && defined(__GNUC__))
  2681. #define DUK_USE_VARIADIC_MACROS
  2682. #endif
  2683. #if defined(_MSC_VER) && !defined(DUK_USE_VARIADIC_MACROS)
  2684. #if (_MSC_VER >= 1400)
  2685. /* VS2005+ should have variadic macros even when they're not C99. */
  2686. #define DUK_USE_VARIADIC_MACROS
  2687. #endif
  2688. #endif
  2689. /*
  2690. * Variable size array initialization.
  2691. *
  2692. * Variable size array at the end of a structure is nonportable.
  2693. * There are three alternatives:
  2694. *
  2695. * 1) C99 (flexible array member): char buf[]
  2696. * 2) Compiler specific (e.g. GCC): char buf[0]
  2697. * 3) Portable but wastes memory / complicates allocation: char buf[1]
  2698. */
  2699. /* XXX: Currently unused, only hbuffer.h needed this at some point. */
  2700. #undef DUK_USE_FLEX_C99
  2701. #undef DUK_USE_FLEX_ZEROSIZE
  2702. #undef DUK_USE_FLEX_ONESIZE
  2703. #if defined(DUK_F_C99)
  2704. #define DUK_USE_FLEX_C99
  2705. #elif defined(__GNUC__)
  2706. #define DUK_USE_FLEX_ZEROSIZE
  2707. #else
  2708. #define DUK_USE_FLEX_ONESIZE
  2709. #endif
  2710. /*
  2711. * GCC pragmas
  2712. */
  2713. /* XXX: GCC pragma inside a function fails in some earlier GCC versions (e.g. gcc 4.5).
  2714. * This is very approximate but allows clean builds for development right now.
  2715. */
  2716. /* http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html */
  2717. #if defined(__GNUC__) && defined(__GNUC_MINOR__) && (__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)
  2718. #define DUK_USE_GCC_PRAGMAS
  2719. #else
  2720. #undef DUK_USE_GCC_PRAGMAS
  2721. #endif
  2722. /*
  2723. * User declarations
  2724. */
  2725. #if defined(DUK_OPT_DECLARE)
  2726. #define DUK_USE_USER_DECLARE() DUK_OPT_DECLARE
  2727. #else
  2728. #define DUK_USE_USER_DECLARE() /* no user declarations */
  2729. #endif
  2730. /*
  2731. * Forced options
  2732. */
  2733. #undef DUK_USE_DEBUGGER_SUPPORT
  2734. #undef DUK_USE_FASTINT
  2735. #define DUK_USE_FASTINT
  2736. #undef DUK_USE_FAST_REFCOUNT_DEFAULT
  2737. #define DUK_USE_FAST_REFCOUNT_DEFAULT
  2738. #undef DUK_USE_INTERRUPT_COUNTER
  2739. #define DUK_USE_JSON_DECNUMBER_FASTPATH
  2740. #define DUK_USE_JSON_DECSTRING_FASTPATH
  2741. #define DUK_USE_JSON_EATWHITE_FASTPATH
  2742. #define DUK_USE_JSON_QUOTESTRING_FASTPATH
  2743. #undef DUK_USE_JSON_STRINGIFY_FASTPATH
  2744. #define DUK_USE_JSON_STRINGIFY_FASTPATH
  2745. #undef DUK_USE_MARK_AND_SWEEP
  2746. #define DUK_USE_MARK_AND_SWEEP
  2747. #undef DUK_USE_PACKED_TVAL
  2748. #undef DUK_USE_PREFER_SIZE
  2749. #undef DUK_USE_REFERENCE_COUNTING
  2750. #define DUK_USE_REFERENCE_COUNTING
  2751. #undef DUK_USE_VALSTACK_UNSAFE
  2752. #define DUK_USE_VALSTACK_UNSAFE
  2753. #undef DUK_USE_VOLUNTARY_GC
  2754. /*
  2755. * Autogenerated defaults
  2756. */
  2757. #define DUK_USE_COMPILER_RECLIMIT 2500
  2758. #undef DUK_USE_DATE_FORMAT_STRING
  2759. #undef DUK_USE_DATE_GET_LOCAL_TZOFFSET
  2760. #undef DUK_USE_DATE_GET_NOW
  2761. #undef DUK_USE_DATE_PARSE_STRING
  2762. #undef DUK_USE_DATE_PRS_GETDATE
  2763. #undef DUK_USE_EXEC_FUN_LOCAL
  2764. #undef DUK_USE_INTEGER_ME
  2765. #undef DUK_USE_INTERRUPT_DEBUG_FIXUP
  2766. #define DUK_USE_JSON_DEC_RECLIMIT 1000
  2767. #define DUK_USE_JSON_ENC_RECLIMIT 1000
  2768. #undef DUK_USE_MARKANDSWEEP_FINALIZER_TORTURE
  2769. #define DUK_USE_MARK_AND_SWEEP_RECLIMIT 256
  2770. #define DUK_USE_NATIVE_CALL_RECLIMIT 1000
  2771. #undef DUK_USE_REFZERO_FINALIZER_TORTURE
  2772. #define DUK_USE_REGEXP_COMPILER_RECLIMIT 10000
  2773. #define DUK_USE_REGEXP_EXECUTOR_RECLIMIT 10000
  2774. #define DUK_USE_VERBOSE_PROP_ERRORS
  2775. /*
  2776. * Alternative customization header
  2777. *
  2778. * If you want to modify the final DUK_USE_xxx flags directly (without
  2779. * using the available DUK_OPT_xxx flags), define DUK_OPT_HAVE_CUSTOM_H
  2780. * and tweak the final flags there.
  2781. */
  2782. #if defined(DUK_OPT_HAVE_CUSTOM_H)
  2783. #include "duk_custom.h"
  2784. #endif
  2785. /*
  2786. * You may add overriding #define/#undef directives below for
  2787. * customization. You of course cannot un-#include or un-typedef
  2788. * anything; these require direct changes above.
  2789. */
  2790. /* __OVERRIDE_DEFINES__ */
  2791. /*
  2792. * Date provider selection
  2793. *
  2794. * User may define DUK_USE_DATE_GET_NOW() etc directly, in which case we'll
  2795. * rely on an external provider. If this is not done, revert to previous
  2796. * behavior and use Unix/Windows built-in provider.
  2797. */
  2798. #if defined(DUK_COMPILING_DUKTAPE)
  2799. #if defined(DUK_USE_DATE_GET_NOW)
  2800. /* External provider already defined. */
  2801. #elif defined(DUK_USE_DATE_NOW_GETTIMEOFDAY)
  2802. DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now_gettimeofday(duk_context *ctx);
  2803. #define DUK_USE_DATE_GET_NOW(ctx) duk_bi_date_get_now_gettimeofday((ctx))
  2804. #elif defined(DUK_USE_DATE_NOW_TIME)
  2805. DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now_time(duk_context *ctx);
  2806. #define DUK_USE_DATE_GET_NOW(ctx) duk_bi_date_get_now_time((ctx))
  2807. #elif defined(DUK_USE_DATE_NOW_WINDOWS)
  2808. DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now_windows(duk_context *ctx);
  2809. #define DUK_USE_DATE_GET_NOW(ctx) duk_bi_date_get_now_windows((ctx))
  2810. #else
  2811. #error no provider for DUK_USE_DATE_GET_NOW()
  2812. #endif
  2813. #if defined(DUK_USE_DATE_GET_LOCAL_TZOFFSET)
  2814. /* External provider already defined. */
  2815. #elif defined(DUK_USE_DATE_TZO_GMTIME_R) || defined(DUK_USE_DATE_TZO_GMTIME)
  2816. DUK_INTERNAL_DECL duk_int_t duk_bi_date_get_local_tzoffset_gmtime(duk_double_t d);
  2817. #define DUK_USE_DATE_GET_LOCAL_TZOFFSET(d) duk_bi_date_get_local_tzoffset_gmtime((d))
  2818. #elif defined(DUK_USE_DATE_TZO_WINDOWS)
  2819. DUK_INTERNAL_DECL duk_int_t duk_bi_date_get_local_tzoffset_windows(duk_double_t d);
  2820. #define DUK_USE_DATE_GET_LOCAL_TZOFFSET(d) duk_bi_date_get_local_tzoffset_windows((d))
  2821. #else
  2822. #error no provider for DUK_USE_DATE_GET_LOCAL_TZOFFSET()
  2823. #endif
  2824. #if defined(DUK_USE_DATE_PARSE_STRING)
  2825. /* External provider already defined. */
  2826. #elif defined(DUK_USE_DATE_PRS_STRPTIME)
  2827. DUK_INTERNAL_DECL duk_bool_t duk_bi_date_parse_string_strptime(duk_context *ctx, const char *str);
  2828. #define DUK_USE_DATE_PARSE_STRING(ctx,str) duk_bi_date_parse_string_strptime((ctx), (str))
  2829. #elif defined(DUK_USE_DATE_PRS_GETDATE)
  2830. DUK_INTERNAL_DECL duk_bool_t duk_bi_date_parse_string_getdate(duk_context *ctx, const char *str);
  2831. #define DUK_USE_DATE_PARSE_STRING(ctx,str) duk_bi_date_parse_string_getdate((ctx), (str))
  2832. #else
  2833. /* No provider for DUK_USE_DATE_PARSE_STRING(), fall back to ISO 8601 only. */
  2834. #endif
  2835. #if defined(DUK_USE_DATE_FORMAT_STRING)
  2836. /* External provider already defined. */
  2837. #elif defined(DUK_USE_DATE_FMT_STRFTIME)
  2838. DUK_INTERNAL_DECL duk_bool_t duk_bi_date_format_parts_strftime(duk_context *ctx, duk_int_t *parts, duk_int_t tzoffset, duk_small_uint_t flags);
  2839. #define DUK_USE_DATE_FORMAT_STRING(ctx,parts,tzoffset,flags) \
  2840. duk_bi_date_format_parts_strftime((ctx), (parts), (tzoffset), (flags))
  2841. #else
  2842. /* No provider for DUK_USE_DATE_FORMAT_STRING(), fall back to ISO 8601 only. */
  2843. #endif
  2844. #endif /* DUK_COMPILING_DUKTAPE */
  2845. /*
  2846. * Sanity check for the final effective internal defines. Also
  2847. * double checks user tweaks made by an optional duk_custom.h header.
  2848. */
  2849. /*
  2850. * Deprecated feature options.
  2851. *
  2852. * Catch so that user more easily notices and updates build.
  2853. */
  2854. #if defined(DUK_OPT_NO_FUNC_STMT)
  2855. #error DUK_OPT_NO_FUNC_STMT is deprecated, use DUK_OPT_NO_NONSTD_FUNC_STMT
  2856. #endif
  2857. #if defined(DUK_OPT_FUNC_NONSTD_CALLER_PROPERTY)
  2858. #error DUK_OPT_FUNC_NONSTD_CALLER_PROPERTY is deprecated, use DUK_OPT_NONSTD_FUNC_CALLER_PROPERTY
  2859. #endif
  2860. #if defined(DUK_OPT_FUNC_NONSTD_SOURCE_PROPERTY)
  2861. #error DUK_OPT_FUNC_NONSTD_SOURCE_PROPERTY is deprecated, use DUK_OPT_NONSTD_FUNC_SOURCE_PROPERTY
  2862. #endif
  2863. #if defined(DUK_OPT_NO_ARRAY_SPLICE_NONSTD_DELCOUNT)
  2864. #error DUK_OPT_NO_ARRAY_SPLICE_NONSTD_DELCOUNT is deprecated, use DUK_OPT_NO_NONSTD_ARRAY_SPLICE_DELCOUNT
  2865. #endif
  2866. #if defined(DUK_OPT_NO_OBJECT_ES6_PROTO_PROPERTY)
  2867. #error DUK_OPT_NO_OBJECT_ES6_PROTO_PROPERTY is deprecated, use DUK_OPT_NO_ES6_OBJECT_PROTO_PROPERTY
  2868. #endif
  2869. #if defined(DUK_OPT_NO_OBJECT_ES6_SETPROTOTYPEOF)
  2870. #error DUK_OPT_NO_OBJECT_ES6_SETPROTOTYPEOF is deprecated, use DUK_OPT_NO_ES6_OBJECT_SETPROTOTYPEOF
  2871. #endif
  2872. #if defined(DUK_OPT_NO_JSONX)
  2873. #error DUK_OPT_NO_JSONX is deprecated, use DUK_OPT_NO_JX
  2874. #endif
  2875. #if defined(DUK_OPT_NO_JSONC)
  2876. #error DUK_OPT_NO_JSONC is deprecated, use DUK_OPT_NO_JC
  2877. #endif
  2878. /*
  2879. * Debug print consistency
  2880. */
  2881. #if defined(DUK_USE_DPRINT) && !defined(DUK_USE_DEBUG)
  2882. #error DUK_USE_DPRINT without DUK_USE_DEBUG
  2883. #endif
  2884. #if defined(DUK_USE_DDPRINT) && !defined(DUK_USE_DEBUG)
  2885. #error DUK_USE_DDPRINT without DUK_USE_DEBUG
  2886. #endif
  2887. #if defined(DUK_USE_DDDPRINT) && !defined(DUK_USE_DEBUG)
  2888. #error DUK_USE_DDDPRINT without DUK_USE_DEBUG
  2889. #endif
  2890. #if defined(DUK_USE_HEAPPTR16) && defined(DUK_USE_DEBUG)
  2891. /* Debug code doesn't have access to 'heap' so it cannot decode pointers. */
  2892. #error debug printing cannot currently be used with heap pointer compression
  2893. #endif
  2894. /*
  2895. * Debugger consistency
  2896. */
  2897. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  2898. #if !defined(DUK_USE_INTERRUPT_COUNTER)
  2899. #error DUK_USE_INTERRUPT_COUNTER is needed when debugger support is enabled
  2900. #endif
  2901. #if !defined(DUK_USE_PC2LINE)
  2902. #error DUK_USE_PC2LINE is needed when debugger support is enabled
  2903. #endif
  2904. #endif
  2905. /*
  2906. * Garbage collection consistency
  2907. */
  2908. #if defined(DUK_USE_REFERENCE_COUNTING) && !defined(DUK_USE_DOUBLE_LINKED_HEAP)
  2909. #error DUK_USE_REFERENCE_COUNTING defined without DUK_USE_DOUBLE_LINKED_HEAP
  2910. #endif
  2911. #if defined(DUK_USE_GC_TORTURE) && !defined(DUK_USE_MARK_AND_SWEEP)
  2912. #error DUK_USE_GC_TORTURE defined without DUK_USE_MARK_AND_SWEEP
  2913. #endif
  2914. /*
  2915. * Low memory feature consistency
  2916. */
  2917. #if defined(DUK_USE_OBJSIZES16)
  2918. #if defined(DUK_USE_HOBJECT_HASH_PART)
  2919. #error DUK_USE_OBJSIZES16 assumes DUK_USE_HOBJECT_HASH_PART is not defined
  2920. #endif
  2921. #endif
  2922. #if defined(DUK_USE_STRTAB_CHAIN) && defined(DUK_USE_STRTAB_PROBE)
  2923. #error both DUK_USE_STRTAB_CHAIN and DUK_USE_STRTAB_PROBE defined
  2924. #endif
  2925. #if !defined(DUK_USE_STRTAB_CHAIN) && !defined(DUK_USE_STRTAB_PROBE)
  2926. #error neither DUK_USE_STRTAB_CHAIN nor DUK_USE_STRTAB_PROBE is defined
  2927. #endif
  2928. #endif /* DUK_CONFIG_H_INCLUDED */