duk_config.h 102 KB

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