2
0

010-lua-5.1.3-lnum-full-260308.patch 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748
  1. --- a/Makefile
  2. +++ b/Makefile
  3. @@ -42,7 +42,7 @@ PLATS= aix ansi bsd freebsd generic linu
  4. # What to install.
  5. TO_BIN= lua$V luac$V
  6. -TO_INC= lua.h luaconf.h lualib.h lauxlib.h ../etc/lua.hpp
  7. +TO_INC= lua.h luaconf.h lualib.h lauxlib.h lnum_config.h ../etc/lua.hpp
  8. TO_LIB= liblua.a
  9. TO_MAN= lua$V.1 luac$V.1
  10. --- a/src/Makefile
  11. +++ b/src/Makefile
  12. @@ -25,7 +25,7 @@ PLATS= aix ansi bsd freebsd generic linu
  13. LUA_A= liblua.a
  14. CORE_O= lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \
  15. lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \
  16. - lundump.o lvm.o lzio.o
  17. + lundump.o lvm.o lzio.o lnum.o
  18. LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \
  19. lstrlib.o loadlib.o linit.o
  20. @@ -148,6 +148,7 @@ llex.o: llex.c lua.h luaconf.h ldo.h lob
  21. lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h
  22. lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
  23. ltm.h lzio.h lmem.h ldo.h
  24. +lnum.o: lnum.c lua.h llex.h lnum.h
  25. loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h
  26. lobject.o: lobject.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h \
  27. ltm.h lzio.h lmem.h lstring.h lgc.h lvm.h
  28. @@ -179,4 +180,18 @@ lzio.o: lzio.c lua.h luaconf.h llimits.h
  29. print.o: print.c ldebug.h lstate.h lua.h luaconf.h lobject.h llimits.h \
  30. ltm.h lzio.h lmem.h lopcodes.h lundump.h
  31. +luaconf.h: lnum_config.h
  32. +lapi.c: lnum.h
  33. +lauxlib.c: llimits.h
  34. +lbaselib.c: llimits.h lobject.h lapi.h
  35. +lcode.c: lnum.h
  36. +liolib.c: lnum.h llex.h
  37. +llex.c: lnum.h
  38. +lnum.h: lobject.h
  39. +lobject.c: llex.h lnum.h
  40. +ltable.c: lnum.h
  41. +lua.c: llimits.h
  42. +lvm.c: llex.h lnum.h
  43. +print.c: lnum.h
  44. +
  45. # (end of Makefile)
  46. --- a/src/lapi.c
  47. +++ b/src/lapi.c
  48. @@ -28,7 +28,7 @@
  49. #include "ltm.h"
  50. #include "lundump.h"
  51. #include "lvm.h"
  52. -
  53. +#include "lnum.h"
  54. const char lua_ident[] =
  55. @@ -241,12 +241,13 @@ LUA_API void lua_pushvalue (lua_State *L
  56. LUA_API int lua_type (lua_State *L, int idx) {
  57. StkId o = index2adr(L, idx);
  58. - return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
  59. + return (o == luaO_nilobject) ? LUA_TNONE : ttype_ext(o);
  60. }
  61. LUA_API const char *lua_typename (lua_State *L, int t) {
  62. UNUSED(L);
  63. + lua_assert( t!= LUA_TINT );
  64. return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
  65. }
  66. @@ -264,6 +265,14 @@ LUA_API int lua_isnumber (lua_State *L,
  67. }
  68. +LUA_API int lua_isinteger (lua_State *L, int idx) {
  69. + TValue tmp;
  70. + lua_Integer dum;
  71. + const TValue *o = index2adr(L, idx);
  72. + return tonumber(o,&tmp) && (ttisint(o) || tt_integer_valued(o,&dum));
  73. +}
  74. +
  75. +
  76. LUA_API int lua_isstring (lua_State *L, int idx) {
  77. int t = lua_type(L, idx);
  78. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  79. @@ -309,31 +318,66 @@ LUA_API int lua_lessthan (lua_State *L,
  80. }
  81. -
  82. LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
  83. TValue n;
  84. const TValue *o = index2adr(L, idx);
  85. - if (tonumber(o, &n))
  86. + if (tonumber(o, &n)) {
  87. +#ifdef LNUM_COMPLEX
  88. + if (nvalue_img(o) != 0)
  89. + luaG_runerror(L, "expecting a real number");
  90. +#endif
  91. return nvalue(o);
  92. - else
  93. - return 0;
  94. + }
  95. + return 0;
  96. }
  97. LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
  98. TValue n;
  99. + /* Lua 5.1 documented behaviour is to return nonzero for non-integer:
  100. + * "If the number is not an integer, it is truncated in some non-specified way."
  101. + * I would suggest to change this, to return 0 for anything that would
  102. + * not fit in 'lua_Integer'.
  103. + */
  104. +#ifdef LUA_COMPAT_TOINTEGER
  105. + /* Lua 5.1 compatible */
  106. const TValue *o = index2adr(L, idx);
  107. if (tonumber(o, &n)) {
  108. - lua_Integer res;
  109. - lua_Number num = nvalue(o);
  110. - lua_number2integer(res, num);
  111. - return res;
  112. + lua_Integer i;
  113. + lua_Number d;
  114. + if (ttisint(o)) return ivalue(o);
  115. + d= nvalue_fast(o);
  116. +# ifdef LNUM_COMPLEX
  117. + if (nvalue_img_fast(o) != 0)
  118. + luaG_runerror(L, "expecting a real number");
  119. +# endif
  120. + lua_number2integer(i, d);
  121. + return i;
  122. }
  123. - else
  124. - return 0;
  125. +#else
  126. + /* New suggestion */
  127. + const TValue *o = index2adr(L, idx);
  128. + if (tonumber(o, &n)) {
  129. + lua_Integer i;
  130. + if (ttisint(o)) return ivalue(o);
  131. + if (tt_integer_valued(o,&i)) return i;
  132. + }
  133. +#endif
  134. + return 0;
  135. }
  136. +#ifdef LNUM_COMPLEX
  137. +LUA_API lua_Complex lua_tocomplex (lua_State *L, int idx) {
  138. + TValue tmp;
  139. + const TValue *o = index2adr(L, idx);
  140. + if (tonumber(o, &tmp))
  141. + return nvalue_complex(o);
  142. + return 0;
  143. +}
  144. +#endif
  145. +
  146. +
  147. LUA_API int lua_toboolean (lua_State *L, int idx) {
  148. const TValue *o = index2adr(L, idx);
  149. return !l_isfalse(o);
  150. @@ -364,6 +408,7 @@ LUA_API size_t lua_objlen (lua_State *L,
  151. case LUA_TSTRING: return tsvalue(o)->len;
  152. case LUA_TUSERDATA: return uvalue(o)->len;
  153. case LUA_TTABLE: return luaH_getn(hvalue(o));
  154. + case LUA_TINT:
  155. case LUA_TNUMBER: {
  156. size_t l;
  157. lua_lock(L); /* `luaV_tostring' may create a new string */
  158. @@ -426,6 +471,8 @@ LUA_API void lua_pushnil (lua_State *L)
  159. }
  160. +/* 'lua_pushnumber()' may lose accuracy on integers, 'lua_pushinteger' will not.
  161. + */
  162. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  163. lua_lock(L);
  164. setnvalue(L->top, n);
  165. @@ -434,12 +481,22 @@ LUA_API void lua_pushnumber (lua_State *
  166. }
  167. -LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  168. +LUA_API void lua_pushinteger (lua_State *L, lua_Integer i) {
  169. + lua_lock(L);
  170. + setivalue(L->top, i);
  171. + api_incr_top(L);
  172. + lua_unlock(L);
  173. +}
  174. +
  175. +
  176. +#ifdef LNUM_COMPLEX
  177. +LUA_API void lua_pushcomplex (lua_State *L, lua_Complex v) {
  178. lua_lock(L);
  179. - setnvalue(L->top, cast_num(n));
  180. + setnvalue_complex( L->top, v );
  181. api_incr_top(L);
  182. lua_unlock(L);
  183. }
  184. +#endif
  185. LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  186. @@ -569,7 +626,7 @@ LUA_API void lua_rawgeti (lua_State *L,
  187. lua_lock(L);
  188. o = index2adr(L, idx);
  189. api_check(L, ttistable(o));
  190. - setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
  191. + setobj2s(L, L->top, luaH_getint(hvalue(o), n));
  192. api_incr_top(L);
  193. lua_unlock(L);
  194. }
  195. @@ -597,6 +654,9 @@ LUA_API int lua_getmetatable (lua_State
  196. case LUA_TUSERDATA:
  197. mt = uvalue(obj)->metatable;
  198. break;
  199. + case LUA_TINT:
  200. + mt = G(L)->mt[LUA_TNUMBER];
  201. + break;
  202. default:
  203. mt = G(L)->mt[ttype(obj)];
  204. break;
  205. @@ -687,7 +747,7 @@ LUA_API void lua_rawseti (lua_State *L,
  206. api_checknelems(L, 1);
  207. o = index2adr(L, idx);
  208. api_check(L, ttistable(o));
  209. - setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
  210. + setobj2t(L, luaH_setint(L, hvalue(o), n), L->top-1);
  211. luaC_barriert(L, hvalue(o), L->top-1);
  212. L->top--;
  213. lua_unlock(L);
  214. @@ -721,7 +781,7 @@ LUA_API int lua_setmetatable (lua_State
  215. break;
  216. }
  217. default: {
  218. - G(L)->mt[ttype(obj)] = mt;
  219. + G(L)->mt[ttype_ext(obj)] = mt;
  220. break;
  221. }
  222. }
  223. @@ -1085,3 +1145,32 @@ LUA_API const char *lua_setupvalue (lua_
  224. return name;
  225. }
  226. +
  227. +/* Help function for 'luaB_tonumber()', avoids multiple str->number
  228. + * conversions for Lua "tonumber()".
  229. + *
  230. + * Also pushes floating point numbers with integer value as integer, which
  231. + * can be used by 'tonumber()' in scripts to bring values back to integer
  232. + * realm.
  233. + *
  234. + * Note: The 'back to integer realm' is _not_ to affect string conversions:
  235. + * 'tonumber("4294967295.1")' should give a floating point value, although
  236. + * the value would be 4294967296 (and storable in int64 realm).
  237. + */
  238. +int lua_pushvalue_as_number (lua_State *L, int idx)
  239. +{
  240. + const TValue *o = index2adr(L, idx);
  241. + TValue tmp;
  242. + lua_Integer i;
  243. + if (ttisnumber(o)) {
  244. + if ( (!ttisint(o)) && tt_integer_valued(o,&i)) {
  245. + lua_pushinteger( L, i );
  246. + return 1;
  247. + }
  248. + } else if (!tonumber(o, &tmp)) {
  249. + return 0;
  250. + }
  251. + if (ttisint(o)) lua_pushinteger( L, ivalue(o) );
  252. + else lua_pushnumber( L, nvalue_fast(o) );
  253. + return 1;
  254. +}
  255. --- a/src/lapi.h
  256. +++ b/src/lapi.h
  257. @@ -13,4 +13,6 @@
  258. LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o);
  259. +int lua_pushvalue_as_number (lua_State *L, int idx);
  260. +
  261. #endif
  262. --- a/src/lauxlib.c
  263. +++ b/src/lauxlib.c
  264. @@ -23,7 +23,7 @@
  265. #include "lua.h"
  266. #include "lauxlib.h"
  267. -
  268. +#include "llimits.h"
  269. #define FREELIST_REF 0 /* free list of references */
  270. @@ -66,7 +66,7 @@ LUALIB_API int luaL_typerror (lua_State
  271. static void tag_error (lua_State *L, int narg, int tag) {
  272. - luaL_typerror(L, narg, lua_typename(L, tag));
  273. + luaL_typerror(L, narg, tag==LUA_TINT ? "integer" : lua_typename(L, tag));
  274. }
  275. @@ -188,8 +188,8 @@ LUALIB_API lua_Number luaL_optnumber (lu
  276. LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
  277. lua_Integer d = lua_tointeger(L, narg);
  278. - if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
  279. - tag_error(L, narg, LUA_TNUMBER);
  280. + if (d == 0 && !lua_isinteger(L, narg)) /* avoid extra test when d is not 0 */
  281. + tag_error(L, narg, LUA_TINT);
  282. return d;
  283. }
  284. @@ -200,6 +200,16 @@ LUALIB_API lua_Integer luaL_optinteger (
  285. }
  286. +#ifdef LNUM_COMPLEX
  287. +LUALIB_API lua_Complex luaL_checkcomplex (lua_State *L, int narg) {
  288. + lua_Complex c = lua_tocomplex(L, narg);
  289. + if (c == 0 && !lua_isnumber(L, narg)) /* avoid extra test when c is not 0 */
  290. + tag_error(L, narg, LUA_TNUMBER);
  291. + return c;
  292. +}
  293. +#endif
  294. +
  295. +
  296. LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
  297. if (!lua_getmetatable(L, obj)) /* no metatable? */
  298. return 0;
  299. --- a/src/lauxlib.h
  300. +++ b/src/lauxlib.h
  301. @@ -57,6 +57,12 @@ LUALIB_API lua_Number (luaL_optnumber) (
  302. LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);
  303. LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,
  304. lua_Integer def);
  305. +#define luaL_checkint32(L,narg) ((int)luaL_checkinteger(L,narg))
  306. +#define luaL_optint32(L,narg,def) ((int)luaL_optinteger(L,narg,def))
  307. +
  308. +#ifdef LNUM_COMPLEX
  309. + LUALIB_API lua_Complex (luaL_checkcomplex) (lua_State *L, int narg);
  310. +#endif
  311. LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
  312. LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);
  313. --- a/src/lbaselib.c
  314. +++ b/src/lbaselib.c
  315. @@ -18,7 +18,9 @@
  316. #include "lauxlib.h"
  317. #include "lualib.h"
  318. -
  319. +#include "llimits.h"
  320. +#include "lobject.h"
  321. +#include "lapi.h"
  322. @@ -54,20 +56,25 @@ static int luaB_tonumber (lua_State *L)
  323. int base = luaL_optint(L, 2, 10);
  324. if (base == 10) { /* standard conversion */
  325. luaL_checkany(L, 1);
  326. - if (lua_isnumber(L, 1)) {
  327. - lua_pushnumber(L, lua_tonumber(L, 1));
  328. + if (lua_isnumber(L, 1)) { /* numeric string, or a number */
  329. + lua_pushvalue_as_number(L,1); /* API extension (not to lose accuracy here) */
  330. return 1;
  331. - }
  332. + }
  333. }
  334. else {
  335. const char *s1 = luaL_checkstring(L, 1);
  336. char *s2;
  337. - unsigned long n;
  338. + unsigned LUA_INTEGER n;
  339. luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
  340. - n = strtoul(s1, &s2, base);
  341. + n = lua_str2ul(s1, &s2, base);
  342. if (s1 != s2) { /* at least one valid digit? */
  343. while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
  344. if (*s2 == '\0') { /* no invalid trailing characters? */
  345. +
  346. + /* Push as number, there needs to be separate 'luaB_tointeger' for
  347. + * when the caller wants to preserve the bits (matters if unsigned
  348. + * values are used).
  349. + */
  350. lua_pushnumber(L, (lua_Number)n);
  351. return 1;
  352. }
  353. @@ -144,7 +151,7 @@ static int luaB_setfenv (lua_State *L) {
  354. luaL_checktype(L, 2, LUA_TTABLE);
  355. getfunc(L, 0);
  356. lua_pushvalue(L, 2);
  357. - if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
  358. + if (lua_isnumber(L, 1) && lua_tointeger(L, 1) == 0) {
  359. /* change environment of current thread */
  360. lua_pushthread(L);
  361. lua_insert(L, -2);
  362. @@ -209,7 +216,7 @@ static int luaB_collectgarbage (lua_Stat
  363. return 1;
  364. }
  365. default: {
  366. - lua_pushnumber(L, res);
  367. + lua_pushinteger(L, res);
  368. return 1;
  369. }
  370. }
  371. @@ -631,6 +638,8 @@ static void base_open (lua_State *L) {
  372. luaL_register(L, "_G", base_funcs);
  373. lua_pushliteral(L, LUA_VERSION);
  374. lua_setglobal(L, "_VERSION"); /* set global _VERSION */
  375. + lua_pushliteral(L, LUA_LNUM);
  376. + lua_setglobal(L, "_LNUM"); /* "[complex] double|float|ldouble int32|int64" */
  377. /* `ipairs' and `pairs' need auxiliary functions as upvalues */
  378. auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
  379. auxopen(L, "pairs", luaB_pairs, luaB_next);
  380. --- a/src/lcode.c
  381. +++ b/src/lcode.c
  382. @@ -22,13 +22,18 @@
  383. #include "lopcodes.h"
  384. #include "lparser.h"
  385. #include "ltable.h"
  386. +#include "lnum.h"
  387. #define hasjumps(e) ((e)->t != (e)->f)
  388. -
  389. static int isnumeral(expdesc *e) {
  390. - return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
  391. + int ek=
  392. +#ifdef LNUM_COMPLEX
  393. + (e->k == VKNUM2) ||
  394. +#endif
  395. + (e->k == VKINT) || (e->k == VKNUM);
  396. + return (ek && e->t == NO_JUMP && e->f == NO_JUMP);
  397. }
  398. @@ -231,12 +236,16 @@ static int addk (FuncState *fs, TValue *
  399. TValue *idx = luaH_set(L, fs->h, k);
  400. Proto *f = fs->f;
  401. int oldsize = f->sizek;
  402. - if (ttisnumber(idx)) {
  403. - lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v));
  404. - return cast_int(nvalue(idx));
  405. + if (ttype(idx)==LUA_TNUMBER) {
  406. + luai_normalize(idx);
  407. + lua_assert( ttype(idx)==LUA_TINT ); /* had no fraction */
  408. + }
  409. + if (ttisint(idx)) {
  410. + lua_assert(luaO_rawequalObj(&fs->f->k[ivalue(idx)], v));
  411. + return cast_int(ivalue(idx));
  412. }
  413. else { /* constant not found; create a new entry */
  414. - setnvalue(idx, cast_num(fs->nk));
  415. + setivalue(idx, fs->nk);
  416. luaM_growvector(L, f->k, fs->nk, f->sizek, TValue,
  417. MAXARG_Bx, "constant table overflow");
  418. while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
  419. @@ -261,6 +270,21 @@ int luaK_numberK (FuncState *fs, lua_Num
  420. }
  421. +int luaK_integerK (FuncState *fs, lua_Integer r) {
  422. + TValue o;
  423. + setivalue(&o, r);
  424. + return addk(fs, &o, &o);
  425. +}
  426. +
  427. +
  428. +#ifdef LNUM_COMPLEX
  429. +static int luaK_imagK (FuncState *fs, lua_Number r) {
  430. + TValue o;
  431. + setnvalue_complex(&o, r*I);
  432. + return addk(fs, &o, &o);
  433. +}
  434. +#endif
  435. +
  436. static int boolK (FuncState *fs, int b) {
  437. TValue o;
  438. setbvalue(&o, b);
  439. @@ -359,6 +383,16 @@ static void discharge2reg (FuncState *fs
  440. luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval));
  441. break;
  442. }
  443. + case VKINT: {
  444. + luaK_codeABx(fs, OP_LOADK, reg, luaK_integerK(fs, e->u.ival));
  445. + break;
  446. + }
  447. +#ifdef LNUM_COMPLEX
  448. + case VKNUM2: {
  449. + luaK_codeABx(fs, OP_LOADK, reg, luaK_imagK(fs, e->u.nval));
  450. + break;
  451. + }
  452. +#endif
  453. case VRELOCABLE: {
  454. Instruction *pc = &getcode(fs, e);
  455. SETARG_A(*pc, reg);
  456. @@ -444,6 +478,10 @@ void luaK_exp2val (FuncState *fs, expdes
  457. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  458. luaK_exp2val(fs, e);
  459. switch (e->k) {
  460. +#ifdef LNUM_COMPLEX
  461. + case VKNUM2:
  462. +#endif
  463. + case VKINT:
  464. case VKNUM:
  465. case VTRUE:
  466. case VFALSE:
  467. @@ -451,6 +489,10 @@ int luaK_exp2RK (FuncState *fs, expdesc
  468. if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */
  469. e->u.s.info = (e->k == VNIL) ? nilK(fs) :
  470. (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) :
  471. + (e->k == VKINT) ? luaK_integerK(fs, e->u.ival) :
  472. +#ifdef LNUM_COMPLEX
  473. + (e->k == VKNUM2) ? luaK_imagK(fs, e->u.nval) :
  474. +#endif
  475. boolK(fs, (e->k == VTRUE));
  476. e->k = VK;
  477. return RKASK(e->u.s.info);
  478. @@ -540,7 +582,10 @@ void luaK_goiftrue (FuncState *fs, expde
  479. int pc; /* pc of last jump */
  480. luaK_dischargevars(fs, e);
  481. switch (e->k) {
  482. - case VK: case VKNUM: case VTRUE: {
  483. +#ifdef LNUM_COMPLEX
  484. + case VKNUM2:
  485. +#endif
  486. + case VKINT: case VK: case VKNUM: case VTRUE: {
  487. pc = NO_JUMP; /* always true; do nothing */
  488. break;
  489. }
  490. @@ -590,7 +635,10 @@ static void codenot (FuncState *fs, expd
  491. e->k = VTRUE;
  492. break;
  493. }
  494. - case VK: case VKNUM: case VTRUE: {
  495. +#ifdef LNUM_COMPLEX
  496. + case VKNUM2:
  497. +#endif
  498. + case VKINT: case VK: case VKNUM: case VTRUE: {
  499. e->k = VFALSE;
  500. break;
  501. }
  502. @@ -626,25 +674,70 @@ void luaK_indexed (FuncState *fs, expdes
  503. static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
  504. lua_Number v1, v2, r;
  505. + int vkres= VKNUM;
  506. if (!isnumeral(e1) || !isnumeral(e2)) return 0;
  507. - v1 = e1->u.nval;
  508. - v2 = e2->u.nval;
  509. +
  510. + /* real and imaginary parts don't mix. */
  511. +#ifdef LNUM_COMPLEX
  512. + if (e1->k == VKNUM2) {
  513. + if ((op != OP_UNM) && (e2->k != VKNUM2)) return 0;
  514. + vkres= VKNUM2; }
  515. + else if (e2->k == VKNUM2) { return 0; }
  516. +#endif
  517. + if ((e1->k == VKINT) && (e2->k == VKINT)) {
  518. + lua_Integer i1= e1->u.ival, i2= e2->u.ival;
  519. + lua_Integer rr;
  520. + int done= 0;
  521. + /* Integer/integer calculations (may end up producing floating point) */
  522. + switch (op) {
  523. + case OP_ADD: done= try_addint( &rr, i1, i2 ); break;
  524. + case OP_SUB: done= try_subint( &rr, i1, i2 ); break;
  525. + case OP_MUL: done= try_mulint( &rr, i1, i2 ); break;
  526. + case OP_DIV: done= try_divint( &rr, i1, i2 ); break;
  527. + case OP_MOD: done= try_modint( &rr, i1, i2 ); break;
  528. + case OP_POW: done= try_powint( &rr, i1, i2 ); break;
  529. + case OP_UNM: done= try_unmint( &rr, i1 ); break;
  530. + default: done= 0; break;
  531. + }
  532. + if (done) {
  533. + e1->u.ival = rr; /* remained within integer range */
  534. + return 1;
  535. + }
  536. + }
  537. + v1 = (e1->k == VKINT) ? ((lua_Number)e1->u.ival) : e1->u.nval;
  538. + v2 = (e2->k == VKINT) ? ((lua_Number)e2->u.ival) : e2->u.nval;
  539. +
  540. switch (op) {
  541. case OP_ADD: r = luai_numadd(v1, v2); break;
  542. case OP_SUB: r = luai_numsub(v1, v2); break;
  543. - case OP_MUL: r = luai_nummul(v1, v2); break;
  544. + case OP_MUL:
  545. +#ifdef LNUM_COMPLEX
  546. + if (vkres==VKNUM2) return 0; /* leave to runtime (could do here, but not worth it?) */
  547. +#endif
  548. + r = luai_nummul(v1, v2); break;
  549. case OP_DIV:
  550. if (v2 == 0) return 0; /* do not attempt to divide by 0 */
  551. - r = luai_numdiv(v1, v2); break;
  552. +#ifdef LNUM_COMPLEX
  553. + if (vkres==VKNUM2) return 0; /* leave to runtime */
  554. +#endif
  555. + r = luai_numdiv(v1, v2); break;
  556. case OP_MOD:
  557. if (v2 == 0) return 0; /* do not attempt to divide by 0 */
  558. +#ifdef LNUM_COMPLEX
  559. + if (vkres==VKNUM2) return 0; /* leave to runtime */
  560. +#endif
  561. r = luai_nummod(v1, v2); break;
  562. - case OP_POW: r = luai_numpow(v1, v2); break;
  563. + case OP_POW:
  564. +#ifdef LNUM_COMPLEX
  565. + if (vkres==VKNUM2) return 0; /* leave to runtime */
  566. +#endif
  567. + r = luai_numpow(v1, v2); break;
  568. case OP_UNM: r = luai_numunm(v1); break;
  569. case OP_LEN: return 0; /* no constant folding for 'len' */
  570. default: lua_assert(0); r = 0; break;
  571. }
  572. if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */
  573. + e1->k = cast(expkind,vkres);
  574. e1->u.nval = r;
  575. return 1;
  576. }
  577. @@ -688,7 +781,8 @@ static void codecomp (FuncState *fs, OpC
  578. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
  579. expdesc e2;
  580. - e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
  581. + e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0;
  582. +
  583. switch (op) {
  584. case OPR_MINUS: {
  585. if (!isnumeral(e))
  586. --- a/src/lcode.h
  587. +++ b/src/lcode.h
  588. @@ -71,6 +71,6 @@ LUAI_FUNC void luaK_prefix (FuncState *f
  589. LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
  590. LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2);
  591. LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);
  592. -
  593. +LUAI_FUNC int luaK_integerK (FuncState *fs, lua_Integer r);
  594. #endif
  595. --- a/src/ldebug.c
  596. +++ b/src/ldebug.c
  597. @@ -183,7 +183,7 @@ static void collectvalidlines (lua_State
  598. int *lineinfo = f->l.p->lineinfo;
  599. int i;
  600. for (i=0; i<f->l.p->sizelineinfo; i++)
  601. - setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
  602. + setbvalue(luaH_setint(L, t, lineinfo[i]), 1);
  603. sethvalue(L, L->top, t);
  604. }
  605. incr_top(L);
  606. @@ -566,7 +566,7 @@ static int isinstack (CallInfo *ci, cons
  607. void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
  608. const char *name = NULL;
  609. - const char *t = luaT_typenames[ttype(o)];
  610. + const char *t = luaT_typenames[ttype_ext(o)];
  611. const char *kind = (isinstack(L->ci, o)) ?
  612. getobjname(L, L->ci, cast_int(o - L->base), &name) :
  613. NULL;
  614. @@ -594,8 +594,8 @@ void luaG_aritherror (lua_State *L, cons
  615. int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
  616. - const char *t1 = luaT_typenames[ttype(p1)];
  617. - const char *t2 = luaT_typenames[ttype(p2)];
  618. + const char *t1 = luaT_typenames[ttype_ext(p1)];
  619. + const char *t2 = luaT_typenames[ttype_ext(p2)];
  620. if (t1[2] == t2[2])
  621. luaG_runerror(L, "attempt to compare two %s values", t1);
  622. else
  623. --- a/src/ldo.c
  624. +++ b/src/ldo.c
  625. @@ -220,9 +220,9 @@ static StkId adjust_varargs (lua_State *
  626. luaD_checkstack(L, p->maxstacksize);
  627. htab = luaH_new(L, nvar, 1); /* create `arg' table */
  628. for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */
  629. - setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i);
  630. + setobj2n(L, luaH_setint(L, htab, i+1), L->top - nvar + i);
  631. /* store counter in field `n' */
  632. - setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
  633. + setivalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), nvar);
  634. }
  635. #endif
  636. /* move fixed parameters to final position */
  637. --- a/src/ldump.c
  638. +++ b/src/ldump.c
  639. @@ -52,6 +52,11 @@ static void DumpNumber(lua_Number x, Dum
  640. DumpVar(x,D);
  641. }
  642. +static void DumpInteger(lua_Integer x, DumpState* D)
  643. +{
  644. + DumpVar(x,D);
  645. +}
  646. +
  647. static void DumpVector(const void* b, int n, size_t size, DumpState* D)
  648. {
  649. DumpInt(n,D);
  650. @@ -93,8 +98,11 @@ static void DumpConstants(const Proto* f
  651. DumpChar(bvalue(o),D);
  652. break;
  653. case LUA_TNUMBER:
  654. - DumpNumber(nvalue(o),D);
  655. + DumpNumber(nvalue_fast(o),D);
  656. break;
  657. + case LUA_TINT:
  658. + DumpInteger(ivalue(o),D);
  659. + break;
  660. case LUA_TSTRING:
  661. DumpString(rawtsvalue(o),D);
  662. break;
  663. --- a/src/liolib.c
  664. +++ b/src/liolib.c
  665. @@ -9,6 +9,7 @@
  666. #include <stdio.h>
  667. #include <stdlib.h>
  668. #include <string.h>
  669. +#include <ctype.h>
  670. #define liolib_c
  671. #define LUA_LIB
  672. @@ -18,7 +19,8 @@
  673. #include "lauxlib.h"
  674. #include "lualib.h"
  675. -
  676. +#include "lnum.h"
  677. +#include "llex.h"
  678. #define IO_INPUT 1
  679. #define IO_OUTPUT 2
  680. @@ -269,6 +271,13 @@ static int io_lines (lua_State *L) {
  681. ** =======================================================
  682. */
  683. +/*
  684. +* Many problems if we intend the same 'n' format specifier (see 'file:read()')
  685. +* to work for both FP and integer numbers, without losing their accuracy. So
  686. +* we don't. 'n' reads numbers as floating points, 'i' as integers. Old code
  687. +* remains valid, but won't provide full integer accuracy (this only matters
  688. +* with float FP and/or 64-bit integers).
  689. +*/
  690. static int read_number (lua_State *L, FILE *f) {
  691. lua_Number d;
  692. @@ -282,6 +291,43 @@ static int read_number (lua_State *L, FI
  693. }
  694. }
  695. +static int read_integer (lua_State *L, FILE *f) {
  696. + lua_Integer i;
  697. + if (fscanf(f, LUA_INTEGER_SCAN, &i) == 1) {
  698. + lua_pushinteger(L, i);
  699. + return 1;
  700. + }
  701. + else return 0; /* read fails */
  702. +}
  703. +
  704. +#ifdef LNUM_COMPLEX
  705. +static int read_complex (lua_State *L, FILE *f) {
  706. + /* NNN / NNNi / NNN+MMMi / NNN-MMMi */
  707. + lua_Number a,b;
  708. + if (fscanf(f, LUA_NUMBER_SCAN, &a) == 1) {
  709. + int c=fgetc(f);
  710. + switch(c) {
  711. + case 'i':
  712. + lua_pushcomplex(L, a*I);
  713. + return 1;
  714. + case '+':
  715. + case '-':
  716. + /* "i" is consumed if at the end; just 'NNN+MMM' will most likely
  717. + * behave as if "i" was there? (TBD: test)
  718. + */
  719. + if (fscanf(f, LUA_NUMBER_SCAN "i", &b) == 1) {
  720. + lua_pushcomplex(L, a+ (c=='+' ? b:-b)*I);
  721. + return 1;
  722. + }
  723. + }
  724. + ungetc( c,f );
  725. + lua_pushnumber(L,a); /*real part only*/
  726. + return 1;
  727. + }
  728. + return 0; /* read fails */
  729. +}
  730. +#endif
  731. +
  732. static int test_eof (lua_State *L, FILE *f) {
  733. int c = getc(f);
  734. @@ -355,6 +401,14 @@ static int g_read (lua_State *L, FILE *f
  735. case 'n': /* number */
  736. success = read_number(L, f);
  737. break;
  738. + case 'i': /* integer (full accuracy) */
  739. + success = read_integer(L, f);
  740. + break;
  741. +#ifdef LNUM_COMPLEX
  742. + case 'c': /* complex */
  743. + success = read_complex(L, f);
  744. + break;
  745. +#endif
  746. case 'l': /* line */
  747. success = read_line(L, f);
  748. break;
  749. @@ -415,9 +469,10 @@ static int g_write (lua_State *L, FILE *
  750. int status = 1;
  751. for (; nargs--; arg++) {
  752. if (lua_type(L, arg) == LUA_TNUMBER) {
  753. - /* optimization: could be done exactly as for strings */
  754. - status = status &&
  755. - fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
  756. + if (lua_isinteger(L,arg))
  757. + status = status && fprintf(f, LUA_INTEGER_FMT, lua_tointeger(L, arg)) > 0;
  758. + else
  759. + status = status && fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
  760. }
  761. else {
  762. size_t l;
  763. @@ -460,7 +515,7 @@ static int f_setvbuf (lua_State *L) {
  764. static const char *const modenames[] = {"no", "full", "line", NULL};
  765. FILE *f = tofile(L);
  766. int op = luaL_checkoption(L, 2, NULL, modenames);
  767. - lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
  768. + size_t sz = luaL_optint32(L, 3, LUAL_BUFFERSIZE);
  769. int res = setvbuf(f, NULL, mode[op], sz);
  770. return pushresult(L, res == 0, NULL);
  771. }
  772. --- a/src/llex.c
  773. +++ b/src/llex.c
  774. @@ -22,6 +22,7 @@
  775. #include "lstring.h"
  776. #include "ltable.h"
  777. #include "lzio.h"
  778. +#include "lnum.h"
  779. @@ -34,13 +35,17 @@
  780. /* ORDER RESERVED */
  781. -const char *const luaX_tokens [] = {
  782. +static const char *const luaX_tokens [] = {
  783. "and", "break", "do", "else", "elseif",
  784. "end", "false", "for", "function", "if",
  785. "in", "local", "nil", "not", "or", "repeat",
  786. "return", "then", "true", "until", "while",
  787. "..", "...", "==", ">=", "<=", "~=",
  788. "<number>", "<name>", "<string>", "<eof>",
  789. + "<integer>",
  790. +#ifdef LNUM_COMPLEX
  791. + "<number2>",
  792. +#endif
  793. NULL
  794. };
  795. @@ -90,7 +95,11 @@ static const char *txtToken (LexState *l
  796. switch (token) {
  797. case TK_NAME:
  798. case TK_STRING:
  799. + case TK_INT:
  800. case TK_NUMBER:
  801. +#ifdef LNUM_COMPLEX
  802. + case TK_NUMBER2:
  803. +#endif
  804. save(ls, '\0');
  805. return luaZ_buffer(ls->buff);
  806. default:
  807. @@ -175,23 +184,27 @@ static void buffreplace (LexState *ls, c
  808. if (p[n] == from) p[n] = to;
  809. }
  810. -
  811. -static void trydecpoint (LexState *ls, SemInfo *seminfo) {
  812. +/* TK_NUMBER (/ TK_NUMBER2) */
  813. +static int trydecpoint (LexState *ls, SemInfo *seminfo) {
  814. /* format error: try to update decimal point separator */
  815. struct lconv *cv = localeconv();
  816. char old = ls->decpoint;
  817. + int ret;
  818. ls->decpoint = (cv ? cv->decimal_point[0] : '.');
  819. buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */
  820. - if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
  821. + ret= luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r, NULL);
  822. + if (!ret) {
  823. /* format error with correct decimal point: no more options */
  824. buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
  825. luaX_lexerror(ls, "malformed number", TK_NUMBER);
  826. }
  827. + return ret;
  828. }
  829. -/* LUA_NUMBER */
  830. -static void read_numeral (LexState *ls, SemInfo *seminfo) {
  831. +/* TK_NUMBER / TK_INT (/TK_NUMBER2) */
  832. +static int read_numeral (LexState *ls, SemInfo *seminfo) {
  833. + int ret;
  834. lua_assert(isdigit(ls->current));
  835. do {
  836. save_and_next(ls);
  837. @@ -202,8 +215,9 @@ static void read_numeral (LexState *ls,
  838. save_and_next(ls);
  839. save(ls, '\0');
  840. buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
  841. - if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
  842. - trydecpoint(ls, seminfo); /* try to update decimal point separator */
  843. + ret= luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r, &seminfo->i );
  844. + if (!ret) return trydecpoint(ls, seminfo); /* try to update decimal point separator */
  845. + return ret;
  846. }
  847. @@ -331,6 +345,7 @@ static void read_string (LexState *ls, i
  848. }
  849. +/* char / TK_* */
  850. static int llex (LexState *ls, SemInfo *seminfo) {
  851. luaZ_resetbuffer(ls->buff);
  852. for (;;) {
  853. @@ -402,8 +417,7 @@ static int llex (LexState *ls, SemInfo *
  854. }
  855. else if (!isdigit(ls->current)) return '.';
  856. else {
  857. - read_numeral(ls, seminfo);
  858. - return TK_NUMBER;
  859. + return read_numeral(ls, seminfo);
  860. }
  861. }
  862. case EOZ: {
  863. @@ -416,8 +430,7 @@ static int llex (LexState *ls, SemInfo *
  864. continue;
  865. }
  866. else if (isdigit(ls->current)) {
  867. - read_numeral(ls, seminfo);
  868. - return TK_NUMBER;
  869. + return read_numeral(ls, seminfo);
  870. }
  871. else if (isalpha(ls->current) || ls->current == '_') {
  872. /* identifier or reserved word */
  873. --- a/src/llex.h
  874. +++ b/src/llex.h
  875. @@ -29,19 +29,22 @@ enum RESERVED {
  876. TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
  877. /* other terminal symbols */
  878. TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER,
  879. - TK_NAME, TK_STRING, TK_EOS
  880. + TK_NAME, TK_STRING, TK_EOS, TK_INT
  881. +#ifdef LNUM_COMPLEX
  882. + , TK_NUMBER2 /* imaginary constants: Ni */
  883. +#endif
  884. };
  885. /* number of reserved words */
  886. #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1))
  887. -/* array with token `names' */
  888. -LUAI_DATA const char *const luaX_tokens [];
  889. -
  890. -
  891. +/* SemInfo is a local data structure of 'llex.c', used for carrying a string
  892. + * or a number. A separate token (TK_*) will tell, how to interpret the data.
  893. + */
  894. typedef union {
  895. lua_Number r;
  896. + lua_Integer i;
  897. TString *ts;
  898. } SemInfo; /* semantics information */
  899. --- a/src/llimits.h
  900. +++ b/src/llimits.h
  901. @@ -49,6 +49,7 @@ typedef LUAI_USER_ALIGNMENT_T L_Umaxalig
  902. /* result of a `usual argument conversion' over lua_Number */
  903. typedef LUAI_UACNUMBER l_uacNumber;
  904. +typedef LUAI_UACINTEGER l_uacInteger;
  905. /* internal assertions for in-house debugging */
  906. @@ -80,7 +81,6 @@ typedef LUAI_UACNUMBER l_uacNumber;
  907. #define cast_int(i) cast(int, (i))
  908. -
  909. /*
  910. ** type for virtual-machine instructions
  911. ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
  912. --- a/src/lmathlib.c
  913. +++ b/src/lmathlib.c
  914. @@ -4,7 +4,6 @@
  915. ** See Copyright Notice in lua.h
  916. */
  917. -
  918. #include <stdlib.h>
  919. #include <math.h>
  920. @@ -16,113 +15,210 @@
  921. #include "lauxlib.h"
  922. #include "lualib.h"
  923. +/* 'luai_vectpow()' as a replacement for 'cpow()'. Defined in the header; we
  924. + * don't intrude the code libs internal functions.
  925. + */
  926. +#ifdef LNUM_COMPLEX
  927. +# include "lnum.h"
  928. +#endif
  929. #undef PI
  930. -#define PI (3.14159265358979323846)
  931. -#define RADIANS_PER_DEGREE (PI/180.0)
  932. -
  933. -
  934. +#ifdef LNUM_FLOAT
  935. +# define PI (3.14159265358979323846F)
  936. +#elif defined(M_PI)
  937. +# define PI M_PI
  938. +#else
  939. +# define PI (3.14159265358979323846264338327950288)
  940. +#endif
  941. +#define RADIANS_PER_DEGREE (PI/180)
  942. +
  943. +#undef HUGE
  944. +#ifdef LNUM_FLOAT
  945. +# define HUGE HUGE_VALF
  946. +#elif defined(LNUM_LDOUBLE)
  947. +# define HUGE HUGE_VALL
  948. +#else
  949. +# define HUGE HUGE_VAL
  950. +#endif
  951. static int math_abs (lua_State *L) {
  952. - lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
  953. +#ifdef LNUM_COMPLEX
  954. + lua_pushnumber(L, _LF(cabs) (luaL_checkcomplex(L,1)));
  955. +#else
  956. + lua_pushnumber(L, _LF(fabs) (luaL_checknumber(L, 1)));
  957. +#endif
  958. return 1;
  959. }
  960. static int math_sin (lua_State *L) {
  961. - lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
  962. +#ifdef LNUM_COMPLEX
  963. + lua_pushcomplex(L, _LF(csin) (luaL_checkcomplex(L,1)));
  964. +#else
  965. + lua_pushnumber(L, _LF(sin) (luaL_checknumber(L, 1)));
  966. +#endif
  967. return 1;
  968. }
  969. static int math_sinh (lua_State *L) {
  970. - lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
  971. +#ifdef LNUM_COMPLEX
  972. + lua_pushcomplex(L, _LF(csinh) (luaL_checkcomplex(L,1)));
  973. +#else
  974. + lua_pushnumber(L, _LF(sinh) (luaL_checknumber(L, 1)));
  975. +#endif
  976. return 1;
  977. }
  978. static int math_cos (lua_State *L) {
  979. - lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
  980. +#ifdef LNUM_COMPLEX
  981. + lua_pushcomplex(L, _LF(ccos) (luaL_checkcomplex(L,1)));
  982. +#else
  983. + lua_pushnumber(L, _LF(cos) (luaL_checknumber(L, 1)));
  984. +#endif
  985. return 1;
  986. }
  987. static int math_cosh (lua_State *L) {
  988. - lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
  989. +#ifdef LNUM_COMPLEX
  990. + lua_pushcomplex(L, _LF(ccosh) (luaL_checkcomplex(L,1)));
  991. +#else
  992. + lua_pushnumber(L, _LF(cosh) (luaL_checknumber(L, 1)));
  993. +#endif
  994. return 1;
  995. }
  996. static int math_tan (lua_State *L) {
  997. - lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
  998. +#ifdef LNUM_COMPLEX
  999. + lua_pushcomplex(L, _LF(ctan) (luaL_checkcomplex(L,1)));
  1000. +#else
  1001. + lua_pushnumber(L, _LF(tan) (luaL_checknumber(L, 1)));
  1002. +#endif
  1003. return 1;
  1004. }
  1005. static int math_tanh (lua_State *L) {
  1006. - lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
  1007. +#ifdef LNUM_COMPLEX
  1008. + lua_pushcomplex(L, _LF(ctanh) (luaL_checkcomplex(L,1)));
  1009. +#else
  1010. + lua_pushnumber(L, _LF(tanh) (luaL_checknumber(L, 1)));
  1011. +#endif
  1012. return 1;
  1013. }
  1014. static int math_asin (lua_State *L) {
  1015. - lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
  1016. +#ifdef LNUM_COMPLEX
  1017. + lua_pushcomplex(L, _LF(casin) (luaL_checkcomplex(L,1)));
  1018. +#else
  1019. + lua_pushnumber(L, _LF(asin) (luaL_checknumber(L, 1)));
  1020. +#endif
  1021. return 1;
  1022. }
  1023. static int math_acos (lua_State *L) {
  1024. - lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
  1025. +#ifdef LNUM_COMPLEX
  1026. + lua_pushcomplex(L, _LF(cacos) (luaL_checkcomplex(L,1)));
  1027. +#else
  1028. + lua_pushnumber(L, _LF(acos) (luaL_checknumber(L, 1)));
  1029. +#endif
  1030. return 1;
  1031. }
  1032. static int math_atan (lua_State *L) {
  1033. - lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
  1034. +#ifdef LNUM_COMPLEX
  1035. + lua_pushcomplex(L, _LF(catan) (luaL_checkcomplex(L,1)));
  1036. +#else
  1037. + lua_pushnumber(L, _LF(atan) (luaL_checknumber(L, 1)));
  1038. +#endif
  1039. return 1;
  1040. }
  1041. static int math_atan2 (lua_State *L) {
  1042. - lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  1043. + /* scalars only */
  1044. + lua_pushnumber(L, _LF(atan2) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  1045. return 1;
  1046. }
  1047. static int math_ceil (lua_State *L) {
  1048. - lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
  1049. +#ifdef LNUM_COMPLEX
  1050. + lua_Complex v= luaL_checkcomplex(L, 1);
  1051. + lua_pushcomplex(L, _LF(ceil) (_LF(creal)(v)) + _LF(ceil) (_LF(cimag)(v))*I);
  1052. +#else
  1053. + lua_pushnumber(L, _LF(ceil) (luaL_checknumber(L, 1)));
  1054. +#endif
  1055. return 1;
  1056. }
  1057. static int math_floor (lua_State *L) {
  1058. - lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
  1059. +#ifdef LNUM_COMPLEX
  1060. + lua_Complex v= luaL_checkcomplex(L, 1);
  1061. + lua_pushcomplex(L, _LF(floor) (_LF(creal)(v)) + _LF(floor) (_LF(cimag)(v))*I);
  1062. +#else
  1063. + lua_pushnumber(L, _LF(floor) (luaL_checknumber(L, 1)));
  1064. +#endif
  1065. return 1;
  1066. }
  1067. -static int math_fmod (lua_State *L) {
  1068. - lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  1069. +static int math_fmod (lua_State *L) {
  1070. + /* scalars only */
  1071. + lua_pushnumber(L, _LF(fmod) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  1072. return 1;
  1073. }
  1074. static int math_modf (lua_State *L) {
  1075. - double ip;
  1076. - double fp = modf(luaL_checknumber(L, 1), &ip);
  1077. + /* scalars only */
  1078. + lua_Number ip;
  1079. + lua_Number fp = _LF(modf) (luaL_checknumber(L, 1), &ip);
  1080. lua_pushnumber(L, ip);
  1081. lua_pushnumber(L, fp);
  1082. return 2;
  1083. }
  1084. static int math_sqrt (lua_State *L) {
  1085. - lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
  1086. +#ifdef LNUM_COMPLEX
  1087. + lua_pushcomplex(L, _LF(csqrt) (luaL_checkcomplex(L,1)));
  1088. +#else
  1089. + lua_pushnumber(L, _LF(sqrt) (luaL_checknumber(L, 1)));
  1090. +#endif
  1091. return 1;
  1092. }
  1093. static int math_pow (lua_State *L) {
  1094. - lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  1095. +#ifdef LNUM_COMPLEX
  1096. + /* C99 'cpow' gives somewhat inaccurate results (i.e. (-1)^2 = -1+1.2246467991474e-16i).
  1097. + * 'luai_vectpow' smoothens such, reusing it is the reason we need to #include "lnum.h".
  1098. + */
  1099. + lua_pushcomplex(L, luai_vectpow(luaL_checkcomplex(L,1), luaL_checkcomplex(L,2)));
  1100. +#else
  1101. + lua_pushnumber(L, _LF(pow) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  1102. +#endif
  1103. return 1;
  1104. }
  1105. static int math_log (lua_State *L) {
  1106. - lua_pushnumber(L, log(luaL_checknumber(L, 1)));
  1107. +#ifdef LNUM_COMPLEX
  1108. + lua_pushcomplex(L, _LF(clog) (luaL_checkcomplex(L,1)));
  1109. +#else
  1110. + lua_pushnumber(L, _LF(log) (luaL_checknumber(L, 1)));
  1111. +#endif
  1112. return 1;
  1113. }
  1114. static int math_log10 (lua_State *L) {
  1115. - lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
  1116. +#ifdef LNUM_COMPLEX
  1117. + /* Not in standard <complex.h> , but easy to calculate: log_a(x) = log_b(x) / log_b(a)
  1118. + */
  1119. + lua_pushcomplex(L, _LF(clog) (luaL_checkcomplex(L,1)) / _LF(log) (10));
  1120. +#else
  1121. + lua_pushnumber(L, _LF(log10) (luaL_checknumber(L, 1)));
  1122. +#endif
  1123. return 1;
  1124. }
  1125. static int math_exp (lua_State *L) {
  1126. - lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
  1127. +#ifdef LNUM_COMPLEX
  1128. + lua_pushcomplex(L, _LF(cexp) (luaL_checkcomplex(L,1)));
  1129. +#else
  1130. + lua_pushnumber(L, _LF(exp) (luaL_checknumber(L, 1)));
  1131. +#endif
  1132. return 1;
  1133. }
  1134. @@ -138,19 +234,20 @@ static int math_rad (lua_State *L) {
  1135. static int math_frexp (lua_State *L) {
  1136. int e;
  1137. - lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
  1138. + lua_pushnumber(L, _LF(frexp) (luaL_checknumber(L, 1), &e));
  1139. lua_pushinteger(L, e);
  1140. return 2;
  1141. }
  1142. static int math_ldexp (lua_State *L) {
  1143. - lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
  1144. + lua_pushnumber(L, _LF(ldexp) (luaL_checknumber(L, 1), luaL_checkint(L, 2)));
  1145. return 1;
  1146. }
  1147. static int math_min (lua_State *L) {
  1148. + /* scalars only */
  1149. int n = lua_gettop(L); /* number of arguments */
  1150. lua_Number dmin = luaL_checknumber(L, 1);
  1151. int i;
  1152. @@ -165,6 +262,7 @@ static int math_min (lua_State *L) {
  1153. static int math_max (lua_State *L) {
  1154. + /* scalars only */
  1155. int n = lua_gettop(L); /* number of arguments */
  1156. lua_Number dmax = luaL_checknumber(L, 1);
  1157. int i;
  1158. @@ -182,25 +280,20 @@ static int math_random (lua_State *L) {
  1159. /* the `%' avoids the (rare) case of r==1, and is needed also because on
  1160. some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
  1161. lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
  1162. - switch (lua_gettop(L)) { /* check number of arguments */
  1163. - case 0: { /* no arguments */
  1164. - lua_pushnumber(L, r); /* Number between 0 and 1 */
  1165. - break;
  1166. - }
  1167. - case 1: { /* only upper limit */
  1168. - int u = luaL_checkint(L, 1);
  1169. - luaL_argcheck(L, 1<=u, 1, "interval is empty");
  1170. - lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */
  1171. - break;
  1172. - }
  1173. - case 2: { /* lower and upper limits */
  1174. - int l = luaL_checkint(L, 1);
  1175. - int u = luaL_checkint(L, 2);
  1176. - luaL_argcheck(L, l<=u, 2, "interval is empty");
  1177. - lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */
  1178. - break;
  1179. - }
  1180. - default: return luaL_error(L, "wrong number of arguments");
  1181. + int n= lua_gettop(L); /* number of arguments */
  1182. + if (n==0) { /* no arguments: range [0,1) */
  1183. + lua_pushnumber(L, r);
  1184. + } else if (n<=2) { /* int range [1,u] or [l,u] */
  1185. + int l= n==1 ? 1 : luaL_checkint(L, 1);
  1186. + int u = luaL_checkint(L, n);
  1187. + int tmp;
  1188. + lua_Number d;
  1189. + luaL_argcheck(L, l<=u, n, "interval is empty");
  1190. + d= _LF(floor)(r*(u-l+1));
  1191. + lua_number2int(tmp,d);
  1192. + lua_pushinteger(L, l+tmp);
  1193. + } else {
  1194. + return luaL_error(L, "wrong number of arguments");
  1195. }
  1196. return 1;
  1197. }
  1198. @@ -211,6 +304,66 @@ static int math_randomseed (lua_State *L
  1199. return 0;
  1200. }
  1201. +/*
  1202. +* Lua 5.1 does not have acosh, asinh, atanh for scalars (not ANSI C)
  1203. +*/
  1204. +#if __STDC_VERSION__ >= 199901L
  1205. +static int math_acosh (lua_State *L) {
  1206. +# ifdef LNUM_COMPLEX
  1207. + lua_pushcomplex(L, _LF(cacosh) (luaL_checkcomplex(L,1)));
  1208. +# else
  1209. + lua_pushnumber(L, _LF(acosh) (luaL_checknumber(L,1)));
  1210. +# endif
  1211. + return 1;
  1212. +}
  1213. +static int math_asinh (lua_State *L) {
  1214. +# ifdef LNUM_COMPLEX
  1215. + lua_pushcomplex(L, _LF(casinh) (luaL_checkcomplex(L,1)));
  1216. +# else
  1217. + lua_pushnumber(L, _LF(asinh) (luaL_checknumber(L,1)));
  1218. +# endif
  1219. + return 1;
  1220. +}
  1221. +static int math_atanh (lua_State *L) {
  1222. +# ifdef LNUM_COMPLEX
  1223. + lua_pushcomplex(L, _LF(catanh) (luaL_checkcomplex(L,1)));
  1224. +# else
  1225. + lua_pushnumber(L, _LF(atanh) (luaL_checknumber(L,1)));
  1226. +# endif
  1227. + return 1;
  1228. +}
  1229. +#endif
  1230. +
  1231. +/*
  1232. + * C99 complex functions, not covered above.
  1233. +*/
  1234. +#ifdef LNUM_COMPLEX
  1235. +static int math_arg (lua_State *L) {
  1236. + lua_pushnumber(L, _LF(carg) (luaL_checkcomplex(L,1)));
  1237. + return 1;
  1238. +}
  1239. +
  1240. +static int math_imag (lua_State *L) {
  1241. + lua_pushnumber(L, _LF(cimag) (luaL_checkcomplex(L,1)));
  1242. + return 1;
  1243. +}
  1244. +
  1245. +static int math_real (lua_State *L) {
  1246. + lua_pushnumber(L, _LF(creal) (luaL_checkcomplex(L,1)));
  1247. + return 1;
  1248. +}
  1249. +
  1250. +static int math_conj (lua_State *L) {
  1251. + lua_pushcomplex(L, _LF(conj) (luaL_checkcomplex(L,1)));
  1252. + return 1;
  1253. +}
  1254. +
  1255. +static int math_proj (lua_State *L) {
  1256. + lua_pushcomplex(L, _LF(cproj) (luaL_checkcomplex(L,1)));
  1257. + return 1;
  1258. +}
  1259. +#endif
  1260. +
  1261. static const luaL_Reg mathlib[] = {
  1262. {"abs", math_abs},
  1263. @@ -241,6 +394,18 @@ static const luaL_Reg mathlib[] = {
  1264. {"sqrt", math_sqrt},
  1265. {"tanh", math_tanh},
  1266. {"tan", math_tan},
  1267. +#if __STDC_VERSION__ >= 199901L
  1268. + {"acosh", math_acosh},
  1269. + {"asinh", math_asinh},
  1270. + {"atanh", math_atanh},
  1271. +#endif
  1272. +#ifdef LNUM_COMPLEX
  1273. + {"arg", math_arg},
  1274. + {"imag", math_imag},
  1275. + {"real", math_real},
  1276. + {"conj", math_conj},
  1277. + {"proj", math_proj},
  1278. +#endif
  1279. {NULL, NULL}
  1280. };
  1281. @@ -252,8 +417,10 @@ LUALIB_API int luaopen_math (lua_State *
  1282. luaL_register(L, LUA_MATHLIBNAME, mathlib);
  1283. lua_pushnumber(L, PI);
  1284. lua_setfield(L, -2, "pi");
  1285. - lua_pushnumber(L, HUGE_VAL);
  1286. + lua_pushnumber(L, HUGE);
  1287. lua_setfield(L, -2, "huge");
  1288. + lua_pushinteger(L, LUA_INTEGER_MAX );
  1289. + lua_setfield(L, -2, "hugeint");
  1290. #if defined(LUA_COMPAT_MOD)
  1291. lua_getfield(L, -1, "fmod");
  1292. lua_setfield(L, -2, "mod");
  1293. --- /dev/null
  1294. +++ b/src/lnum.c
  1295. @@ -0,0 +1,312 @@
  1296. +/*
  1297. +** $Id: lnum.c,v ... $
  1298. +** Internal number model
  1299. +** See Copyright Notice in lua.h
  1300. +*/
  1301. +
  1302. +#include <stdlib.h>
  1303. +#include <math.h>
  1304. +#include <ctype.h>
  1305. +#include <string.h>
  1306. +#include <stdio.h>
  1307. +#include <errno.h>
  1308. +
  1309. +#define lnum_c
  1310. +#define LUA_CORE
  1311. +
  1312. +#include "lua.h"
  1313. +#include "llex.h"
  1314. +#include "lnum.h"
  1315. +
  1316. +/*
  1317. +** lua_real2str converts a (non-complex) number to a string.
  1318. +** lua_str2real converts a string to a (non-complex) number.
  1319. +*/
  1320. +#define lua_real2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
  1321. +
  1322. +/*
  1323. +* Note: Only 'strtod()' is part of ANSI C; others are C99 and
  1324. +* may need '--std=c99' compiler setting (at least on Ubuntu 7.10).
  1325. +*
  1326. +* Visual C++ 2008 Express does not have 'strtof()', nor 'strtold()'.
  1327. +* References to '_strtold()' exist but don't compile. It seems best
  1328. +* to leave Windows users with DOUBLE only (or compile with MinGW).
  1329. +*
  1330. +* In practise, using '(long double)strtod' is a risky thing, since
  1331. +* it will cause accuracy loss in reading in numbers, and such losses
  1332. +* will pile up in later processing. Get a real 'strtold()' or don't
  1333. +* use that mode at all.
  1334. +*/
  1335. +#ifdef LNUM_DOUBLE
  1336. +# define lua_str2real strtod
  1337. +#elif defined(LNUM_FLOAT)
  1338. +# define lua_str2real strtof
  1339. +#elif defined(LNUM_LDOUBLE)
  1340. +# define lua_str2real strtold
  1341. +#endif
  1342. +
  1343. +#define lua_integer2str(s,v) sprintf((s), LUA_INTEGER_FMT, (v))
  1344. +
  1345. +/* 's' is expected to be LUAI_MAXNUMBER2STR long (enough for any number)
  1346. +*/
  1347. +void luaO_num2buf( char *s, const TValue *o )
  1348. +{
  1349. + lua_Number n;
  1350. + lua_assert( ttisnumber(o) );
  1351. +
  1352. + /* Reason to handle integers differently is not only speed, but accuracy as
  1353. + * well. We want to make any integer tostring() without roundings, at all.
  1354. + */
  1355. + if (ttisint(o)) {
  1356. + lua_integer2str( s, ivalue(o) );
  1357. + return;
  1358. + }
  1359. + n= nvalue_fast(o);
  1360. + lua_real2str(s, n);
  1361. +
  1362. +#ifdef LNUM_COMPLEX
  1363. + lua_Number n2= nvalue_img_fast(o);
  1364. + if (n2!=0) { /* Postfix with +-Ni */
  1365. + int re0= (n == 0);
  1366. + char *s2= re0 ? s : strchr(s,'\0');
  1367. + if ((!re0) && (n2>0)) *s2++= '+';
  1368. + lua_real2str( s2, n2 );
  1369. + strcat(s2,"i");
  1370. + }
  1371. +#endif
  1372. +}
  1373. +
  1374. +/*
  1375. +* If a LUA_TNUMBER has integer value, give it.
  1376. +*/
  1377. +int /*bool*/ tt_integer_valued( const TValue *o, lua_Integer *ref ) {
  1378. + lua_Number d;
  1379. + lua_Integer i;
  1380. +
  1381. + lua_assert( ttype(o)==LUA_TNUMBER );
  1382. + lua_assert( ref );
  1383. +#ifdef LNUM_COMPLEX
  1384. + if (nvalue_img_fast(o)!=0) return 0;
  1385. +#endif
  1386. + d= nvalue_fast(o);
  1387. + lua_number2integer(i, d);
  1388. + if (cast_num(i) == d) {
  1389. + *ref= i; return 1;
  1390. + }
  1391. + return 0;
  1392. +}
  1393. +
  1394. +/*
  1395. + * Lua 5.1.3 (using 'strtod()') allows 0x+hex but not 0+octal. This is good,
  1396. + * and we should NOT use 'autobase' 0 with 'strtoul[l]()' for this reason.
  1397. + *
  1398. + * Lua 5.1.3 allows '0x...' numbers to overflow and lose precision; this is not
  1399. + * good. On Visual C++ 2008, 'strtod()' does not even take them in. Better to
  1400. + * require hex values to fit 'lua_Integer' or give an error that they don't?
  1401. + *
  1402. + * Full hex range (0 .. 0xff..ff) is stored as integers, not to lose any bits.
  1403. + * Numerical value of 0xff..ff will be -1, if used in calculations.
  1404. + *
  1405. + * Returns: TK_INT for a valid integer, '*endptr_ref' updated
  1406. + * TK_NUMBER for seemingly numeric, to be parsed as floating point
  1407. + * 0 for bad characters, not a number (or '0x' out of range)
  1408. + */
  1409. +static int luaO_str2i (const char *s, lua_Integer *res, char **endptr_ref) {
  1410. + char *endptr;
  1411. + /* 'v' gets ULONG_MAX on possible overflow (which is > LUA_INTEGER_MAX);
  1412. + * we don't have to check 'errno' here.
  1413. + */
  1414. + unsigned LUA_INTEGER v= lua_str2ul(s, &endptr, 10);
  1415. + if (endptr == s) return 0; /* nothing numeric */
  1416. + if (v==0 && *endptr=='x') {
  1417. + errno= 0; /* needs to be set, 'strtoul[l]' does not clear it */
  1418. + v= lua_str2ul(endptr+1, &endptr, 16); /* retry as hex, unsigned range */
  1419. + if (errno==ERANGE) { /* clamped to 0xff..ff */
  1420. +#if (defined(LNUM_INT32) && !defined(LNUM_FLOAT)) || defined(LNUM_LDOUBLE)
  1421. + return TK_NUMBER; /* Allow to be read as floating point (has more integer range) */
  1422. +#else
  1423. + return 0; /* Reject the number */
  1424. +#endif
  1425. + }
  1426. + } else if ((v > LUA_INTEGER_MAX) || (*endptr && (!isspace(*endptr)))) {
  1427. + return TK_NUMBER; /* not in signed range, or has '.', 'e' etc. trailing */
  1428. + }
  1429. + *res= (lua_Integer)v;
  1430. + *endptr_ref= endptr;
  1431. + return TK_INT;
  1432. +}
  1433. +
  1434. +/* 0 / TK_NUMBER / TK_INT (/ TK_NUMBER2) */
  1435. +int luaO_str2d (const char *s, lua_Number *res_n, lua_Integer *res_i) {
  1436. + char *endptr;
  1437. + int ret= TK_NUMBER;
  1438. + /* Check integers first, if caller is allowing.
  1439. + * If 'res2'==NULL, they're only looking for floating point.
  1440. + */
  1441. + if (res_i) {
  1442. + ret= luaO_str2i(s,res_i,&endptr);
  1443. + if (ret==0) return 0;
  1444. + }
  1445. + if (ret==TK_NUMBER) {
  1446. + lua_assert(res_n);
  1447. + /* Note: Visual C++ 2008 Express 'strtod()' does not read in "0x..."
  1448. + * numbers; it will read '0' and spit 'x' as endptr.
  1449. + * This means hex constants not fitting in 'lua_Integer' won't
  1450. + * be read in at all. What to do?
  1451. + */
  1452. + *res_n = lua_str2real(s, &endptr);
  1453. + if (endptr == s) return 0; /* conversion failed */
  1454. + /* Visual C++ 2008 'strtod()' does not allow "0x..." input. */
  1455. +#if defined(_MSC_VER) && !defined(LNUM_FLOAT) && !defined(LNUM_INT64)
  1456. + if (*res_n==0 && *endptr=='x') {
  1457. + /* Hex constant too big for 'lua_Integer' but that could fit in 'lua_Number'
  1458. + * integer bits
  1459. + */
  1460. + unsigned __int64 v= _strtoui64( s, &endptr, 16 );
  1461. + /* We just let > 64 bit values be clamped to _UI64_MAX (MSDN does not say 'errno'==ERANGE would be set) */
  1462. + *res_n= cast_num(v);
  1463. + if (*res_n != v) return 0; /* Would have lost accuracy */
  1464. + }
  1465. +#endif
  1466. +#ifdef LNUM_COMPLEX
  1467. + if (*endptr == 'i') { endptr++; ret= TK_NUMBER2; }
  1468. +#endif
  1469. + }
  1470. + if (*endptr) {
  1471. + while (isspace(cast(unsigned char, *endptr))) endptr++;
  1472. + if (*endptr) return 0; /* invalid trail */
  1473. + }
  1474. + return ret;
  1475. +}
  1476. +
  1477. +
  1478. +/* Functions for finding out, when integer operations remain in range
  1479. + * (and doing them).
  1480. + */
  1481. +int try_addint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
  1482. + /* Signed int overflow is undefined behavior, so catch it without causing it. */
  1483. + if (ic>0) { if (ib > LUA_INTEGER_MAX - ic) return 0; /*overflow, use floats*/ }
  1484. + else { if (ib < LUA_INTEGER_MIN - ic) return 0; }
  1485. + *r = ib + ic;
  1486. + return 1;
  1487. +}
  1488. +
  1489. +int try_subint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
  1490. + /* Signed int overflow is undefined behavior, so catch it without causing it. */
  1491. + if (ic>0) { if (ib < LUA_INTEGER_MIN + ic) return 0; /*overflow, use floats*/ }
  1492. + else { if (ib > LUA_INTEGER_MAX + ic) return 0; }
  1493. + *r = ib - ic;
  1494. + return 1;
  1495. +}
  1496. +
  1497. +int try_mulint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
  1498. + if (ib!=LUA_INTEGER_MIN && ic!=LUA_INTEGER_MIN) {
  1499. + lua_Integer b= luai_abs(ib), c= luai_abs(ic);
  1500. + if ( (ib==0) || (LUA_INTEGER_MAX/b >= c) ) {
  1501. + *r= ib*ic; /* no overflow */
  1502. + return 1;
  1503. + }
  1504. + } else if (ib==0 || ic==0) {
  1505. + *r= 0; return 1;
  1506. + }
  1507. +
  1508. + /* Result can be LUA_INTEGER_MIN; if it is, calculating it using floating
  1509. + * point will not cause accuracy loss.
  1510. + */
  1511. + if ( luai_nummul( cast_num(ib), cast_num(ic) ) == LUA_INTEGER_MIN ) {
  1512. + *r= LUA_INTEGER_MIN;
  1513. + return 1;
  1514. + }
  1515. + return 0;
  1516. +}
  1517. +
  1518. +int try_divint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
  1519. + /* N/0: leave to float side, to give an error
  1520. + */
  1521. + if (ic==0) return 0;
  1522. +
  1523. + /* N/LUA_INTEGER_MIN: always non-integer results, or 0 or +1
  1524. + */
  1525. + if (ic==LUA_INTEGER_MIN) {
  1526. + if (ib==LUA_INTEGER_MIN) { *r=1; return 1; }
  1527. + if (ib==0) { *r=0; return 1; }
  1528. +
  1529. + /* LUA_INTEGER_MIN (-2^31|63)/N: calculate using float side (either the division
  1530. + * causes non-integer results, or there is no accuracy loss in int->fp->int
  1531. + * conversions (N=2,4,8,..,256 and N=2^30,2^29,..2^23).
  1532. + */
  1533. + } else if (ib==LUA_INTEGER_MIN) {
  1534. + lua_Number d= luai_numdiv( cast_num(LUA_INTEGER_MIN), cast_num(ic) );
  1535. + lua_Integer i; lua_number2integer(i,d);
  1536. + if (cast_num(i)==d) { *r= i; return 1; }
  1537. +
  1538. + } else {
  1539. + /* Note: We _can_ use ANSI C mod here, even on negative values, since
  1540. + * we only test for == 0 (the sign would be implementation dependent).
  1541. + */
  1542. + if (ib%ic == 0) { *r= ib/ic; return 1; }
  1543. + }
  1544. +
  1545. + return 0;
  1546. +}
  1547. +
  1548. +int try_modint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
  1549. + if (ic!=0) {
  1550. + /* ANSI C can be trusted when b%c==0, or when values are non-negative.
  1551. + * b - (floor(b/c) * c)
  1552. + * -->
  1553. + * + +: b - (b/c) * c (b % c can be used)
  1554. + * - -: b - (b/c) * c (b % c could work, but not defined by ANSI C)
  1555. + * 0 -: b - (b/c) * c (=0, b % c could work, but not defined by ANSI C)
  1556. + * - +: b - (b/c-1) * c (when b!=-c)
  1557. + * + -: b - (b/c-1) * c (when b!=-c)
  1558. + *
  1559. + * o MIN%MIN ends up 0, via overflow in calcs but that does not matter.
  1560. + * o MIN%MAX ends up MAX-1 (and other such numbers), also after overflow,
  1561. + * but that does not matter, results do.
  1562. + */
  1563. + lua_Integer v= ib % ic;
  1564. + if ( v!=0 && (ib<0 || ic<0) ) {
  1565. + v= ib - ((ib/ic) - ((ib<=0 && ic<0) ? 0:1)) * ic;
  1566. + }
  1567. + /* Result should always have same sign as 2nd argument. (PIL2) */
  1568. + lua_assert( (v<0) ? (ic<0) : (v>0) ? (ic>0) : 1 );
  1569. + *r= v;
  1570. + return 1;
  1571. + }
  1572. + return 0; /* let float side return NaN */
  1573. +}
  1574. +
  1575. +int try_powint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
  1576. +
  1577. + /* In FLOAT/INT32 or FLOAT|DOUBLE/INT64 modes, calculating integer powers
  1578. + * via FP realm may lose accuracy (i.e. 7^11 = 1977326743, which fits int32
  1579. + * but not 23-bit float mantissa).
  1580. + *
  1581. + * The current solution is dumb, but it works and uses little code. Use of
  1582. + * integer powers is not anticipated to be very frequent (apart from 2^x,
  1583. + * which is separately optimized).
  1584. + */
  1585. + if (ib==0) *r=0;
  1586. + else if (ic<0) return 0; /* FP realm */
  1587. + else if (ib==2 && ic < (int)sizeof(lua_Integer)*8-1) *r= ((lua_Integer)1)<<ic; /* 1,2,4,...2^30 | 2^62 optimization */
  1588. + else if (ic==0) *r=1;
  1589. + else if (luai_abs(ib)==1) *r= (ic%2) ? ib:1;
  1590. + else {
  1591. + lua_Integer x= ib;
  1592. + while( --ic ) {
  1593. + if (!try_mulint( &x, x, ib ))
  1594. + return 0; /* FP realm */
  1595. + }
  1596. + *r= x;
  1597. + }
  1598. + return 1;
  1599. +}
  1600. +
  1601. +int try_unmint( lua_Integer *r, lua_Integer ib ) {
  1602. + /* Negating LUA_INTEGER_MIN leaves the range. */
  1603. + if ( ib != LUA_INTEGER_MIN )
  1604. + { *r= -ib; return 1; }
  1605. + return 0;
  1606. +}
  1607. +
  1608. --- /dev/null
  1609. +++ b/src/lnum.h
  1610. @@ -0,0 +1,116 @@
  1611. +/*
  1612. +** $Id: lnum.h,v ... $
  1613. +** Internal Number model
  1614. +** See Copyright Notice in lua.h
  1615. +*/
  1616. +
  1617. +#ifndef lnum_h
  1618. +#define lnum_h
  1619. +
  1620. +#include <math.h>
  1621. +
  1622. +#include "lobject.h"
  1623. +
  1624. +/*
  1625. +** The luai_num* macros define the primitive operations over 'lua_Number's
  1626. +** (not 'lua_Integer's, not 'lua_Complex').
  1627. +*/
  1628. +#define luai_numadd(a,b) ((a)+(b))
  1629. +#define luai_numsub(a,b) ((a)-(b))
  1630. +#define luai_nummul(a,b) ((a)*(b))
  1631. +#define luai_numdiv(a,b) ((a)/(b))
  1632. +#define luai_nummod(a,b) ((a) - _LF(floor)((a)/(b))*(b))
  1633. +#define luai_numpow(a,b) (_LF(pow)(a,b))
  1634. +#define luai_numunm(a) (-(a))
  1635. +#define luai_numeq(a,b) ((a)==(b))
  1636. +#define luai_numlt(a,b) ((a)<(b))
  1637. +#define luai_numle(a,b) ((a)<=(b))
  1638. +#define luai_numisnan(a) (!luai_numeq((a), (a)))
  1639. +
  1640. +int try_addint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
  1641. +int try_subint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
  1642. +int try_mulint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
  1643. +int try_divint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
  1644. +int try_modint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
  1645. +int try_powint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
  1646. +int try_unmint( lua_Integer *r, lua_Integer ib );
  1647. +
  1648. +#ifdef LNUM_COMPLEX
  1649. + static inline lua_Complex luai_vectunm( lua_Complex a ) { return -a; }
  1650. + static inline lua_Complex luai_vectadd( lua_Complex a, lua_Complex b ) { return a+b; }
  1651. + static inline lua_Complex luai_vectsub( lua_Complex a, lua_Complex b ) { return a-b; }
  1652. + static inline lua_Complex luai_vectmul( lua_Complex a, lua_Complex b ) { return a*b; }
  1653. + static inline lua_Complex luai_vectdiv( lua_Complex a, lua_Complex b ) { return a/b; }
  1654. +
  1655. +/*
  1656. + * C99 does not provide modulus for complex numbers. It most likely is not
  1657. + * meaningful at all.
  1658. + */
  1659. +
  1660. +/*
  1661. + * Complex power
  1662. + *
  1663. + * C99 'cpow' gives inaccurate results for many common cases s.a. (1i)^2 ->
  1664. + * -1+1.2246467991474e-16i (OS X 10.4, gcc 4.0.1 build 5367)
  1665. + *
  1666. + * [(a+bi)^(c+di)] = (r^c) * exp(-d*t) * cos(c*t + d*ln(r)) +
  1667. + * = (r^c) * exp(-d*t) * sin(c*t + d*ln(r)) *i
  1668. + * r = sqrt(a^2+b^2), t = arctan( b/a )
  1669. + *
  1670. + * Reference: <http://home.att.net/~srschmitt/complexnumbers.html>
  1671. + * Could also be calculated using: x^y = exp(ln(x)*y)
  1672. + *
  1673. + * Note: Defined here (and not in .c) so 'lmathlib.c' can share the
  1674. + * implementation.
  1675. + */
  1676. + static inline
  1677. + lua_Complex luai_vectpow( lua_Complex a, lua_Complex b )
  1678. + {
  1679. +# if 1
  1680. + lua_Number ar= _LF(creal)(a), ai= _LF(cimag)(a);
  1681. + lua_Number br= _LF(creal)(b), bi= _LF(cimag)(b);
  1682. +
  1683. + if (ai==0 && bi==0) { /* a^c (real) */
  1684. + return luai_numpow( ar, br );
  1685. + }
  1686. +
  1687. + int br_int= (int)br;
  1688. +
  1689. + if ( ai!=0 && bi==0 && br_int==br && br_int!=0 && br_int!=INT_MIN ) {
  1690. + /* (a+bi)^N, N = { +-1,+-2, ... +-INT_MAX }
  1691. + */
  1692. + lua_Number k= luai_numpow( _LF(sqrt) (ar*ar + ai*ai), br );
  1693. + lua_Number cos_z, sin_z;
  1694. +
  1695. + /* Situation depends upon c (N) in the following manner:
  1696. + *
  1697. + * N%4==0 => cos(c*t)=1, sin(c*t)=0
  1698. + * (N*sign(b))%4==1 or (N*sign(b))%4==-3 => cos(c*t)=0, sin(c*t)=1
  1699. + * N%4==2 or N%4==-2 => cos(c*t)=-1, sin(c*t)=0
  1700. + * (N*sign(b))%4==-1 or (N*sign(b))%4==3 => cos(c*t)=0, sin(c*t)=-1
  1701. + */
  1702. + int br_int_abs = br_int<0 ? -br_int:br_int;
  1703. +
  1704. + switch( (br_int_abs%4) * (br_int<0 ? -1:1) * (ai<0 ? -1:1) ) {
  1705. + case 0: cos_z=1, sin_z=0; break;
  1706. + case 2: case -2: cos_z=-1, sin_z=0; break;
  1707. + case 1: case -3: cos_z=0, sin_z=1; break;
  1708. + case 3: case -1: cos_z=0, sin_z=-1; break;
  1709. + default: lua_assert(0); return 0;
  1710. + }
  1711. + return k*cos_z + (k*sin_z)*I;
  1712. + }
  1713. +# endif
  1714. + return _LF(cpow) ( a, b );
  1715. + }
  1716. +#endif
  1717. +
  1718. +LUAI_FUNC int luaO_str2d (const char *s, lua_Number *res1, lua_Integer *res2);
  1719. +LUAI_FUNC void luaO_num2buf( char *s, const TValue *o );
  1720. +
  1721. +LUAI_FUNC int /*bool*/ tt_integer_valued( const TValue *o, lua_Integer *ref );
  1722. +
  1723. +#define luai_normalize(o) \
  1724. +{ lua_Integer _i; if (tt_integer_valued(o,&_i)) setivalue(o,_i); }
  1725. +
  1726. +#endif
  1727. --- /dev/null
  1728. +++ b/src/lnum_config.h
  1729. @@ -0,0 +1,221 @@
  1730. +/*
  1731. +** $Id: lnum_config.h,v ... $
  1732. +** Internal Number model
  1733. +** See Copyright Notice in lua.h
  1734. +*/
  1735. +
  1736. +#ifndef lnum_config_h
  1737. +#define lnum_config_h
  1738. +
  1739. +/*
  1740. +** Default number modes
  1741. +*/
  1742. +#if (!defined LNUM_DOUBLE) && (!defined LNUM_FLOAT) && (!defined LNUM_LDOUBLE)
  1743. +# define LNUM_FLOAT
  1744. +#endif
  1745. +#if (!defined LNUM_INT16) && (!defined LNUM_INT32) && (!defined LNUM_INT64)
  1746. +# define LNUM_INT32
  1747. +#endif
  1748. +
  1749. +/*
  1750. +** Require C99 mode for COMPLEX, FLOAT and LDOUBLE (only DOUBLE is ANSI C).
  1751. +*/
  1752. +#if defined(LNUM_COMPLEX) && (__STDC_VERSION__ < 199901L)
  1753. +# error "Need C99 for complex (use '--std=c99' or similar)"
  1754. +#elif defined(LNUM_LDOUBLE) && (__STDC_VERSION__ < 199901L) && !defined(_MSC_VER)
  1755. +# error "Need C99 for 'long double' (use '--std=c99' or similar)"
  1756. +#elif defined(LNUM_FLOAT) && (__STDC_VERSION__ < 199901L)
  1757. +/* LNUM_FLOAT not supported on Windows */
  1758. +# error "Need C99 for 'float' (use '--std=c99' or similar)"
  1759. +#endif
  1760. +
  1761. +/*
  1762. +** Number mode identifier to accompany the version string.
  1763. +*/
  1764. +#ifdef LNUM_COMPLEX
  1765. +# define _LNUM1 "complex "
  1766. +#else
  1767. +# define _LNUM1 ""
  1768. +#endif
  1769. +#ifdef LNUM_DOUBLE
  1770. +# define _LNUM2 "double"
  1771. +#elif defined(LNUM_FLOAT)
  1772. +# define _LNUM2 "float"
  1773. +#elif defined(LNUM_LDOUBLE)
  1774. +# define _LNUM2 "ldouble"
  1775. +#endif
  1776. +#ifdef LNUM_INT32
  1777. +# define _LNUM3 "int32"
  1778. +#elif defined(LNUM_INT64)
  1779. +# define _LNUM3 "int64"
  1780. +#elif defined(LNUM_INT16)
  1781. +# define _LNUM3 "int16"
  1782. +#endif
  1783. +#define LUA_LNUM _LNUM1 _LNUM2 " " _LNUM3
  1784. +
  1785. +/*
  1786. +** LUA_NUMBER is the type of floating point number in Lua
  1787. +** LUA_NUMBER_SCAN is the format for reading numbers.
  1788. +** LUA_NUMBER_FMT is the format for writing numbers.
  1789. +*/
  1790. +#ifdef LNUM_FLOAT
  1791. +# define LUA_NUMBER float
  1792. +# define LUA_NUMBER_SCAN "%f"
  1793. +# define LUA_NUMBER_FMT "%g"
  1794. +#elif (defined LNUM_DOUBLE)
  1795. +# define LUA_NUMBER double
  1796. +# define LUA_NUMBER_SCAN "%lf"
  1797. +# define LUA_NUMBER_FMT "%.14g"
  1798. +#elif (defined LNUM_LDOUBLE)
  1799. +# define LUA_NUMBER long double
  1800. +# define LUA_NUMBER_SCAN "%Lg"
  1801. +# define LUA_NUMBER_FMT "%.20Lg"
  1802. +#endif
  1803. +
  1804. +
  1805. +/*
  1806. +** LUAI_MAXNUMBER2STR: size of a buffer fitting any number->string result.
  1807. +**
  1808. +** double: 24 (sign, x.xxxxxxxxxxxxxxe+nnnn, and \0)
  1809. +** int64: 21 (19 digits, sign, and \0)
  1810. +** long double: 43 for 128-bit (sign, x.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxe+nnnn, and \0)
  1811. +** 30 for 80-bit (sign, x.xxxxxxxxxxxxxxxxxxxxe+nnnn, and \0)
  1812. +*/
  1813. +#ifdef LNUM_LDOUBLE
  1814. +# define _LUAI_MN2S 44
  1815. +#else
  1816. +# define _LUAI_MN2S 24
  1817. +#endif
  1818. +
  1819. +#ifdef LNUM_COMPLEX
  1820. +# define LUAI_MAXNUMBER2STR (2*_LUAI_MN2S)
  1821. +#else
  1822. +# define LUAI_MAXNUMBER2STR _LUAI_MN2S
  1823. +#endif
  1824. +
  1825. +/*
  1826. +** LUA_INTEGER is the integer type used by lua_pushinteger/lua_tointeger/lua_isinteger.
  1827. +** LUA_INTEGER_SCAN is the format for reading integers
  1828. +** LUA_INTEGER_FMT is the format for writing integers
  1829. +**
  1830. +** Note: Visual C++ 2005 does not have 'strtoull()', use '_strtoui64()' instead.
  1831. +*/
  1832. +#ifdef LNUM_INT32
  1833. +# if LUAI_BITSINT > 16
  1834. +# define LUA_INTEGER int
  1835. +# define LUA_INTEGER_SCAN "%d"
  1836. +# define LUA_INTEGER_FMT "%d"
  1837. +# else
  1838. +/* Note: 'LUA_INTEGER' being 'ptrdiff_t' (as in Lua 5.1) causes problems with
  1839. + * 'printf()' operations. Also 'unsigned ptrdiff_t' is invalid.
  1840. + */
  1841. +# define LUA_INTEGER long
  1842. +# define LUA_INTEGER_SCAN "%ld"
  1843. +# define LUA_INTEGER_FMT "%ld"
  1844. +# endif
  1845. +# define LUA_INTEGER_MAX 0x7FFFFFFF /* 2^31-1 */
  1846. +/* */
  1847. +#elif defined(LNUM_INT64)
  1848. +# define LUA_INTEGER long long
  1849. +# ifdef _MSC_VER
  1850. +# define lua_str2ul _strtoui64
  1851. +# else
  1852. +# define lua_str2ul strtoull
  1853. +# endif
  1854. +# define LUA_INTEGER_SCAN "%lld"
  1855. +# define LUA_INTEGER_FMT "%lld"
  1856. +# define LUA_INTEGER_MAX 0x7fffffffffffffffLL /* 2^63-1 */
  1857. +# define LUA_INTEGER_MIN (-LUA_INTEGER_MAX - 1LL) /* -2^63 */
  1858. +/* */
  1859. +#elif defined(LNUM_INT16)
  1860. +# if LUAI_BITSINT > 16
  1861. +# define LUA_INTEGER short
  1862. +# define LUA_INTEGER_SCAN "%hd"
  1863. +# define LUA_INTEGER_FMT "%hd"
  1864. +# else
  1865. +# define LUA_INTEGER int
  1866. +# define LUA_INTEGER_SCAN "%d"
  1867. +# define LUA_INTEGER_FMT "%d"
  1868. +# endif
  1869. +# define LUA_INTEGER_MAX 0x7FFF /* 2^16-1 */
  1870. +#endif
  1871. +
  1872. +#ifndef lua_str2ul
  1873. +# define lua_str2ul (unsigned LUA_INTEGER)strtoul
  1874. +#endif
  1875. +#ifndef LUA_INTEGER_MIN
  1876. +# define LUA_INTEGER_MIN (-LUA_INTEGER_MAX -1) /* -2^16|32 */
  1877. +#endif
  1878. +
  1879. +/*
  1880. +@@ lua_number2int is a macro to convert lua_Number to int.
  1881. +@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
  1882. +** CHANGE them if you know a faster way to convert a lua_Number to
  1883. +** int (with any rounding method and without throwing errors) in your
  1884. +** system. In Pentium machines, a naive typecast from double to int
  1885. +** in C is extremely slow, so any alternative is worth trying.
  1886. +*/
  1887. +
  1888. +/* On a Pentium, resort to a trick */
  1889. +#if defined(LNUM_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
  1890. + (defined(__i386) || defined (_M_IX86) || defined(__i386__))
  1891. +
  1892. +/* On a Microsoft compiler, use assembler */
  1893. +# if defined(_MSC_VER)
  1894. +# define lua_number2int(i,d) __asm fld d __asm fistp i
  1895. +# else
  1896. +
  1897. +/* the next trick should work on any Pentium, but sometimes clashes
  1898. + with a DirectX idiosyncrasy */
  1899. +union luai_Cast { double l_d; long l_l; };
  1900. +# define lua_number2int(i,d) \
  1901. + { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
  1902. +# endif
  1903. +
  1904. +# ifndef LNUM_INT64
  1905. +# define lua_number2integer lua_number2int
  1906. +# endif
  1907. +
  1908. +/* this option always works, but may be slow */
  1909. +#else
  1910. +# define lua_number2int(i,d) ((i)=(int)(d))
  1911. +#endif
  1912. +
  1913. +/* Note: Some compilers (OS X gcc 4.0?) may choke on double->long long conversion
  1914. + * since it can lose precision. Others do require 'long long' there.
  1915. + */
  1916. +#ifndef lua_number2integer
  1917. +# define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
  1918. +#endif
  1919. +
  1920. +/*
  1921. +** 'luai_abs()' to give absolute value of 'lua_Integer'
  1922. +*/
  1923. +#ifdef LNUM_INT32
  1924. +# define luai_abs abs
  1925. +#elif defined(LNUM_INT64) && (__STDC_VERSION__ >= 199901L)
  1926. +# define luai_abs llabs
  1927. +#else
  1928. +# define luai_abs(v) ((v) >= 0 ? (v) : -(v))
  1929. +#endif
  1930. +
  1931. +/*
  1932. +** LUAI_UACNUMBER is the result of an 'usual argument conversion' over a number.
  1933. +** LUAI_UACINTEGER the same, over an integer.
  1934. +*/
  1935. +#define LUAI_UACNUMBER double
  1936. +#define LUAI_UACINTEGER long
  1937. +
  1938. +/* ANSI C only has math funcs for 'double. C99 required for float and long double
  1939. + * variants.
  1940. + */
  1941. +#ifdef LNUM_DOUBLE
  1942. +# define _LF(name) name
  1943. +#elif defined(LNUM_FLOAT)
  1944. +# define _LF(name) name ## f
  1945. +#elif defined(LNUM_LDOUBLE)
  1946. +# define _LF(name) name ## l
  1947. +#endif
  1948. +
  1949. +#endif
  1950. +
  1951. --- a/src/lobject.c
  1952. +++ b/src/lobject.c
  1953. @@ -21,7 +21,8 @@
  1954. #include "lstate.h"
  1955. #include "lstring.h"
  1956. #include "lvm.h"
  1957. -
  1958. +#include "llex.h"
  1959. +#include "lnum.h"
  1960. const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};
  1961. @@ -70,12 +71,31 @@ int luaO_log2 (unsigned int x) {
  1962. int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
  1963. - if (ttype(t1) != ttype(t2)) return 0;
  1964. + if (!ttype_ext_same(t1,t2)) return 0;
  1965. else switch (ttype(t1)) {
  1966. case LUA_TNIL:
  1967. return 1;
  1968. + case LUA_TINT:
  1969. + if (ttype(t2)==LUA_TINT)
  1970. + return ivalue(t1) == ivalue(t2);
  1971. + else { /* t1:int, t2:num */
  1972. +#ifdef LNUM_COMPLEX
  1973. + if (nvalue_img_fast(t2) != 0) return 0;
  1974. +#endif
  1975. + /* Avoid doing accuracy losing cast, if possible. */
  1976. + lua_Integer tmp;
  1977. + if (tt_integer_valued(t2,&tmp))
  1978. + return ivalue(t1) == tmp;
  1979. + else
  1980. + return luai_numeq( cast_num(ivalue(t1)), nvalue_fast(t2) );
  1981. + }
  1982. case LUA_TNUMBER:
  1983. - return luai_numeq(nvalue(t1), nvalue(t2));
  1984. + if (ttype(t2)==LUA_TINT)
  1985. + return luaO_rawequalObj(t2, t1); /* swap LUA_TINT to left */
  1986. +#ifdef LNUM_COMPLEX
  1987. + if (!luai_numeq(nvalue_img_fast(t1), nvalue_img_fast(t2))) return 0;
  1988. +#endif
  1989. + return luai_numeq(nvalue_fast(t1), nvalue_fast(t2));
  1990. case LUA_TBOOLEAN:
  1991. return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
  1992. case LUA_TLIGHTUSERDATA:
  1993. @@ -86,21 +106,6 @@ int luaO_rawequalObj (const TValue *t1,
  1994. }
  1995. }
  1996. -
  1997. -int luaO_str2d (const char *s, lua_Number *result) {
  1998. - char *endptr;
  1999. - *result = lua_str2number(s, &endptr);
  2000. - if (endptr == s) return 0; /* conversion failed */
  2001. - if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
  2002. - *result = cast_num(strtoul(s, &endptr, 16));
  2003. - if (*endptr == '\0') return 1; /* most common case */
  2004. - while (isspace(cast(unsigned char, *endptr))) endptr++;
  2005. - if (*endptr != '\0') return 0; /* invalid trailing characters? */
  2006. - return 1;
  2007. -}
  2008. -
  2009. -
  2010. -
  2011. static void pushstr (lua_State *L, const char *str) {
  2012. setsvalue2s(L, L->top, luaS_new(L, str));
  2013. incr_top(L);
  2014. @@ -131,7 +136,11 @@ const char *luaO_pushvfstring (lua_State
  2015. break;
  2016. }
  2017. case 'd': {
  2018. - setnvalue(L->top, cast_num(va_arg(argp, int)));
  2019. + /* This is tricky for 64-bit integers; maybe they even cannot be
  2020. + * supported on all compilers; depends on the conversions applied to
  2021. + * variable argument lists. TBD: test!
  2022. + */
  2023. + setivalue(L->top, (lua_Integer) va_arg(argp, l_uacInteger));
  2024. incr_top(L);
  2025. break;
  2026. }
  2027. @@ -212,3 +221,4 @@ void luaO_chunkid (char *out, const char
  2028. }
  2029. }
  2030. }
  2031. +
  2032. --- a/src/lobject.h
  2033. +++ b/src/lobject.h
  2034. @@ -17,7 +17,11 @@
  2035. /* tags for values visible from Lua */
  2036. -#define LAST_TAG LUA_TTHREAD
  2037. +#if LUA_TINT > LUA_TTHREAD
  2038. +# define LAST_TAG LUA_TINT
  2039. +#else
  2040. +# define LAST_TAG LUA_TTHREAD
  2041. +#endif
  2042. #define NUM_TAGS (LAST_TAG+1)
  2043. @@ -59,7 +63,12 @@ typedef struct GCheader {
  2044. typedef union {
  2045. GCObject *gc;
  2046. void *p;
  2047. +#ifdef LNUM_COMPLEX
  2048. + lua_Complex n;
  2049. +#else
  2050. lua_Number n;
  2051. +#endif
  2052. + lua_Integer i;
  2053. int b;
  2054. } Value;
  2055. @@ -77,7 +86,11 @@ typedef struct lua_TValue {
  2056. /* Macros to test type */
  2057. #define ttisnil(o) (ttype(o) == LUA_TNIL)
  2058. -#define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
  2059. +#define ttisint(o) (ttype(o) == LUA_TINT)
  2060. +#define ttisnumber(o) ((ttype(o) == LUA_TINT) || (ttype(o) == LUA_TNUMBER))
  2061. +#ifdef LNUM_COMPLEX
  2062. +# define ttiscomplex(o) ((ttype(o) == LUA_TNUMBER) && (nvalue_img_fast(o)!=0))
  2063. +#endif
  2064. #define ttisstring(o) (ttype(o) == LUA_TSTRING)
  2065. #define ttistable(o) (ttype(o) == LUA_TTABLE)
  2066. #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
  2067. @@ -90,7 +103,25 @@ typedef struct lua_TValue {
  2068. #define ttype(o) ((o)->tt)
  2069. #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
  2070. #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
  2071. -#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
  2072. +
  2073. +#define ttype_ext(o) ( ttype(o) == LUA_TINT ? LUA_TNUMBER : ttype(o) )
  2074. +#define ttype_ext_same(o1,o2) ( (ttype(o1)==ttype(o2)) || (ttisnumber(o1) && ttisnumber(o2)) )
  2075. +
  2076. +/* '_fast' variants are for cases where 'ttype(o)' is known to be LUA_TNUMBER.
  2077. + */
  2078. +#ifdef LNUM_COMPLEX
  2079. +# define nvalue_complex_fast(o) check_exp( ttype(o)==LUA_TNUMBER, (o)->value.n )
  2080. +# define nvalue_fast(o) ( _LF(creal) ( nvalue_complex_fast(o) ) )
  2081. +# define nvalue_img_fast(o) ( _LF(cimag) ( nvalue_complex_fast(o) ) )
  2082. +# define nvalue_complex(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? (o)->value.i : (o)->value.n )
  2083. +# define nvalue_img(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? 0 : _LF(cimag)( (o)->value.n ) )
  2084. +# define nvalue(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? cast_num((o)->value.i) : _LF(creal)((o)->value.n) )
  2085. +#else
  2086. +# define nvalue(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? cast_num((o)->value.i) : (o)->value.n )
  2087. +# define nvalue_fast(o) check_exp( ttype(o)==LUA_TNUMBER, (o)->value.n )
  2088. +#endif
  2089. +#define ivalue(o) check_exp( ttype(o)==LUA_TINT, (o)->value.i )
  2090. +
  2091. #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
  2092. #define tsvalue(o) (&rawtsvalue(o)->tsv)
  2093. #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
  2094. @@ -116,8 +147,27 @@ typedef struct lua_TValue {
  2095. /* Macros to set values */
  2096. #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
  2097. -#define setnvalue(obj,x) \
  2098. - { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
  2099. +/* Must not have side effects, 'x' may be expression.
  2100. +*/
  2101. +#define setivalue(obj,x) \
  2102. + { TValue *i_o=(obj); i_o->value.i=(x); i_o->tt=LUA_TINT; }
  2103. +
  2104. +# define setnvalue(obj,x) \
  2105. + { TValue *i_o=(obj); i_o->value.n= (x); i_o->tt=LUA_TNUMBER; }
  2106. +
  2107. +/* Note: Complex always has "inline", both are C99.
  2108. +*/
  2109. +#ifdef LNUM_COMPLEX
  2110. + static inline void setnvalue_complex_fast( TValue *obj, lua_Complex x ) {
  2111. + lua_assert( _LF(cimag)(x) != 0 );
  2112. + obj->value.n= x; obj->tt= LUA_TNUMBER;
  2113. + }
  2114. + static inline void setnvalue_complex( TValue *obj, lua_Complex x ) {
  2115. + if (_LF(cimag)(x) == 0) { setnvalue(obj, _LF(creal)(x)); }
  2116. + else { obj->value.n= x; obj->tt= LUA_TNUMBER; }
  2117. + }
  2118. +#endif
  2119. +
  2120. #define setpvalue(obj,x) \
  2121. { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
  2122. @@ -155,9 +205,6 @@ typedef struct lua_TValue {
  2123. i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
  2124. checkliveness(G(L),i_o); }
  2125. -
  2126. -
  2127. -
  2128. #define setobj(L,obj1,obj2) \
  2129. { const TValue *o2=(obj2); TValue *o1=(obj1); \
  2130. o1->value = o2->value; o1->tt=o2->tt; \
  2131. @@ -185,8 +232,11 @@ typedef struct lua_TValue {
  2132. #define setttype(obj, tt) (ttype(obj) = (tt))
  2133. -
  2134. -#define iscollectable(o) (ttype(o) >= LUA_TSTRING)
  2135. +#if LUA_TINT >= LUA_TSTRING
  2136. +# define iscollectable(o) ((ttype(o) >= LUA_TSTRING) && (ttype(o) != LUA_TINT))
  2137. +#else
  2138. +# define iscollectable(o) (ttype(o) >= LUA_TSTRING)
  2139. +#endif
  2140. @@ -370,12 +420,10 @@ LUAI_FUNC int luaO_log2 (unsigned int x)
  2141. LUAI_FUNC int luaO_int2fb (unsigned int x);
  2142. LUAI_FUNC int luaO_fb2int (int x);
  2143. LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
  2144. -LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
  2145. LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
  2146. va_list argp);
  2147. LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
  2148. LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
  2149. -
  2150. #endif
  2151. --- a/src/loslib.c
  2152. +++ b/src/loslib.c
  2153. @@ -186,15 +186,30 @@ static int os_time (lua_State *L) {
  2154. }
  2155. if (t == (time_t)(-1))
  2156. lua_pushnil(L);
  2157. - else
  2158. - lua_pushnumber(L, (lua_Number)t);
  2159. + else {
  2160. + /* On float systems the pushed value must be an integer, NOT a number.
  2161. + * Otherwise, accuracy is lost in the time_t->float conversion.
  2162. + */
  2163. +#ifdef LNUM_FLOAT
  2164. + lua_pushinteger(L, (lua_Integer) t);
  2165. +#else
  2166. + lua_pushnumber(L, (lua_Number) t);
  2167. +#endif
  2168. + }
  2169. return 1;
  2170. }
  2171. static int os_difftime (lua_State *L) {
  2172. +#ifdef LNUM_FLOAT
  2173. + lua_Integer i= (lua_Integer)
  2174. + difftime( (time_t)(luaL_checkinteger(L, 1)),
  2175. + (time_t)(luaL_optinteger(L, 2, 0)));
  2176. + lua_pushinteger(L, i);
  2177. +#else
  2178. lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
  2179. (time_t)(luaL_optnumber(L, 2, 0))));
  2180. +#endif
  2181. return 1;
  2182. }
  2183. --- a/src/lparser.c
  2184. +++ b/src/lparser.c
  2185. @@ -33,7 +33,6 @@
  2186. #define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m)
  2187. -
  2188. /*
  2189. ** nodes for block list (list of active blocks)
  2190. */
  2191. @@ -72,7 +71,7 @@ static void errorlimit (FuncState *fs, i
  2192. const char *msg = (fs->f->linedefined == 0) ?
  2193. luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) :
  2194. luaO_pushfstring(fs->L, "function at line %d has more than %d %s",
  2195. - fs->f->linedefined, limit, what);
  2196. + (fs->f->linedefined), limit, what);
  2197. luaX_lexerror(fs->ls, msg, 0);
  2198. }
  2199. @@ -733,6 +732,18 @@ static void simpleexp (LexState *ls, exp
  2200. v->u.nval = ls->t.seminfo.r;
  2201. break;
  2202. }
  2203. + case TK_INT: {
  2204. + init_exp(v, VKINT, 0);
  2205. + v->u.ival = ls->t.seminfo.i;
  2206. + break;
  2207. + }
  2208. +#ifdef LNUM_COMPLEX
  2209. + case TK_NUMBER2: {
  2210. + init_exp(v, VKNUM2, 0);
  2211. + v->u.nval = ls->t.seminfo.r;
  2212. + break;
  2213. + }
  2214. +#endif
  2215. case TK_STRING: {
  2216. codestring(ls, v, ls->t.seminfo.ts);
  2217. break;
  2218. @@ -1079,7 +1090,7 @@ static void fornum (LexState *ls, TStrin
  2219. if (testnext(ls, ','))
  2220. exp1(ls); /* optional step */
  2221. else { /* default step = 1 */
  2222. - luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
  2223. + luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_integerK(fs, 1));
  2224. luaK_reserveregs(fs, 1);
  2225. }
  2226. forbody(ls, base, line, 1, 1);
  2227. --- a/src/lparser.h
  2228. +++ b/src/lparser.h
  2229. @@ -31,7 +31,11 @@ typedef enum {
  2230. VRELOCABLE, /* info = instruction pc */
  2231. VNONRELOC, /* info = result register */
  2232. VCALL, /* info = instruction pc */
  2233. - VVARARG /* info = instruction pc */
  2234. + VVARARG, /* info = instruction pc */
  2235. + VKINT /* ival = integer value */
  2236. +#ifdef LNUM_COMPLEX
  2237. + ,VKNUM2 /* nval = imaginary value */
  2238. +#endif
  2239. } expkind;
  2240. typedef struct expdesc {
  2241. @@ -39,6 +43,7 @@ typedef struct expdesc {
  2242. union {
  2243. struct { int info, aux; } s;
  2244. lua_Number nval;
  2245. + lua_Integer ival;
  2246. } u;
  2247. int t; /* patch list of `exit when true' */
  2248. int f; /* patch list of `exit when false' */
  2249. --- a/src/lstrlib.c
  2250. +++ b/src/lstrlib.c
  2251. @@ -43,8 +43,8 @@ static ptrdiff_t posrelat (ptrdiff_t pos
  2252. static int str_sub (lua_State *L) {
  2253. size_t l;
  2254. const char *s = luaL_checklstring(L, 1, &l);
  2255. - ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);
  2256. - ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);
  2257. + ptrdiff_t start = posrelat(luaL_checkint32(L, 2), l);
  2258. + ptrdiff_t end = posrelat(luaL_optint32(L, 3, -1), l);
  2259. if (start < 1) start = 1;
  2260. if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
  2261. if (start <= end)
  2262. @@ -106,8 +106,8 @@ static int str_rep (lua_State *L) {
  2263. static int str_byte (lua_State *L) {
  2264. size_t l;
  2265. const char *s = luaL_checklstring(L, 1, &l);
  2266. - ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
  2267. - ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
  2268. + ptrdiff_t posi = posrelat(luaL_optint32(L, 2, 1), l);
  2269. + ptrdiff_t pose = posrelat(luaL_optint32(L, 3, posi), l);
  2270. int n, i;
  2271. if (posi <= 0) posi = 1;
  2272. if ((size_t)pose > l) pose = l;
  2273. @@ -496,7 +496,7 @@ static int str_find_aux (lua_State *L, i
  2274. size_t l1, l2;
  2275. const char *s = luaL_checklstring(L, 1, &l1);
  2276. const char *p = luaL_checklstring(L, 2, &l2);
  2277. - ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
  2278. + ptrdiff_t init = posrelat(luaL_optint32(L, 3, 1), l1) - 1;
  2279. if (init < 0) init = 0;
  2280. else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;
  2281. if (find && (lua_toboolean(L, 4) || /* explicit request? */
  2282. @@ -690,7 +690,7 @@ static int str_gsub (lua_State *L) {
  2283. ** maximum size of each format specification (such as '%-099.99d')
  2284. ** (+10 accounts for %99.99x plus margin of error)
  2285. */
  2286. -#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
  2287. +#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTEGER_FMT)-2 + 10)
  2288. static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
  2289. @@ -747,9 +747,9 @@ static const char *scanformat (lua_State
  2290. static void addintlen (char *form) {
  2291. size_t l = strlen(form);
  2292. char spec = form[l - 1];
  2293. - strcpy(form + l - 1, LUA_INTFRMLEN);
  2294. - form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;
  2295. - form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0';
  2296. + const char *tmp= LUA_INTEGER_FMT; /* "%lld" or "%ld" */
  2297. + strcpy(form + l - 1, tmp+1);
  2298. + form[l + sizeof(LUA_INTEGER_FMT)-4] = spec;
  2299. }
  2300. @@ -779,12 +779,12 @@ static int str_format (lua_State *L) {
  2301. }
  2302. case 'd': case 'i': {
  2303. addintlen(form);
  2304. - sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg));
  2305. + sprintf(buff, form, luaL_checkinteger(L, arg));
  2306. break;
  2307. }
  2308. case 'o': case 'u': case 'x': case 'X': {
  2309. addintlen(form);
  2310. - sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg));
  2311. + sprintf(buff, form, (unsigned LUA_INTEGER)luaL_checkinteger(L, arg));
  2312. break;
  2313. }
  2314. case 'e': case 'E': case 'f':
  2315. --- a/src/ltable.c
  2316. +++ b/src/ltable.c
  2317. @@ -33,6 +33,7 @@
  2318. #include "lobject.h"
  2319. #include "lstate.h"
  2320. #include "ltable.h"
  2321. +#include "lnum.h"
  2322. /*
  2323. @@ -51,25 +52,15 @@
  2324. #define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
  2325. #define hashboolean(t,p) hashpow2(t, p)
  2326. -
  2327. +#define hashint(t,i) hashpow2(t,i)
  2328. /*
  2329. ** for some types, it is better to avoid modulus by power of 2, as
  2330. ** they tend to have many 2 factors.
  2331. */
  2332. #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
  2333. -
  2334. -
  2335. #define hashpointer(t,p) hashmod(t, IntPoint(p))
  2336. -
  2337. -/*
  2338. -** number of ints inside a lua_Number
  2339. -*/
  2340. -#define numints cast_int(sizeof(lua_Number)/sizeof(int))
  2341. -
  2342. -
  2343. -
  2344. #define dummynode (&dummynode_)
  2345. static const Node dummynode_ = {
  2346. @@ -80,27 +71,46 @@ static const Node dummynode_ = {
  2347. /*
  2348. ** hash for lua_Numbers
  2349. +**
  2350. +** for non-complex modes, never called with 'lua_Integer' value range (s.a. 0)
  2351. */
  2352. static Node *hashnum (const Table *t, lua_Number n) {
  2353. - unsigned int a[numints];
  2354. - int i;
  2355. - if (luai_numeq(n, 0)) /* avoid problems with -0 */
  2356. - return gnode(t, 0);
  2357. - memcpy(a, &n, sizeof(a));
  2358. - for (i = 1; i < numints; i++) a[0] += a[i];
  2359. - return hashmod(t, a[0]);
  2360. + const unsigned int *p= cast(const unsigned int *,&n);
  2361. + unsigned int sum= *p;
  2362. + unsigned int m= sizeof(lua_Number)/sizeof(int);
  2363. + unsigned int i;
  2364. + /* OS X Intel has 'm'==4 and gives "Bus error" if the last integer of
  2365. + * 'n' is read; the actual size of long double is only 80 bits = 10 bytes.
  2366. + * Linux x86 has 'm'==3, and does not require reduction.
  2367. + */
  2368. +#if defined(LNUM_LDOUBLE) && defined(__i386__)
  2369. + if (m>3) m--;
  2370. +#endif
  2371. + for (i = 1; i < m; i++) sum += p[i];
  2372. + return hashmod(t, sum);
  2373. }
  2374. -
  2375. /*
  2376. ** returns the `main' position of an element in a table (that is, the index
  2377. ** of its hash value)
  2378. +**
  2379. +** Floating point numbers with integer value give the hash position of the
  2380. +** integer (so they use the same table position).
  2381. */
  2382. static Node *mainposition (const Table *t, const TValue *key) {
  2383. + lua_Integer i;
  2384. switch (ttype(key)) {
  2385. case LUA_TNUMBER:
  2386. - return hashnum(t, nvalue(key));
  2387. + if (tt_integer_valued(key,&i))
  2388. + return hashint(t, i);
  2389. +#ifdef LNUM_COMPLEX
  2390. + if (nvalue_img_fast(key)!=0 && luai_numeq(nvalue_fast(key),0))
  2391. + return gnode(t, 0); /* 0 and -0 to give same hash */
  2392. +#endif
  2393. + return hashnum(t, nvalue_fast(key));
  2394. + case LUA_TINT:
  2395. + return hashint(t, ivalue(key));
  2396. case LUA_TSTRING:
  2397. return hashstr(t, rawtsvalue(key));
  2398. case LUA_TBOOLEAN:
  2399. @@ -116,16 +126,20 @@ static Node *mainposition (const Table *
  2400. /*
  2401. ** returns the index for `key' if `key' is an appropriate key to live in
  2402. ** the array part of the table, -1 otherwise.
  2403. +**
  2404. +** Anything <=0 is taken as not being in the array part.
  2405. */
  2406. -static int arrayindex (const TValue *key) {
  2407. - if (ttisnumber(key)) {
  2408. - lua_Number n = nvalue(key);
  2409. - int k;
  2410. - lua_number2int(k, n);
  2411. - if (luai_numeq(cast_num(k), n))
  2412. - return k;
  2413. +static int arrayindex (const TValue *key, int max) {
  2414. + lua_Integer k;
  2415. + switch( ttype(key) ) {
  2416. + case LUA_TINT:
  2417. + k= ivalue(key); break;
  2418. + case LUA_TNUMBER:
  2419. + if (tt_integer_valued(key,&k)) break;
  2420. + default:
  2421. + return -1; /* not to be used as array index */
  2422. }
  2423. - return -1; /* `key' did not match some condition */
  2424. + return ((k>0) && (k <= max)) ? cast_int(k) : -1;
  2425. }
  2426. @@ -137,8 +151,8 @@ static int arrayindex (const TValue *key
  2427. static int findindex (lua_State *L, Table *t, StkId key) {
  2428. int i;
  2429. if (ttisnil(key)) return -1; /* first iteration */
  2430. - i = arrayindex(key);
  2431. - if (0 < i && i <= t->sizearray) /* is `key' inside array part? */
  2432. + i = arrayindex(key, t->sizearray);
  2433. + if (i>0) /* inside array part? */
  2434. return i-1; /* yes; that's the index (corrected to C) */
  2435. else {
  2436. Node *n = mainposition(t, key);
  2437. @@ -163,7 +177,7 @@ int luaH_next (lua_State *L, Table *t, S
  2438. int i = findindex(L, t, key); /* find original element */
  2439. for (i++; i < t->sizearray; i++) { /* try first array part */
  2440. if (!ttisnil(&t->array[i])) { /* a non-nil value? */
  2441. - setnvalue(key, cast_num(i+1));
  2442. + setivalue(key, i+1);
  2443. setobj2s(L, key+1, &t->array[i]);
  2444. return 1;
  2445. }
  2446. @@ -209,8 +223,8 @@ static int computesizes (int nums[], int
  2447. static int countint (const TValue *key, int *nums) {
  2448. - int k = arrayindex(key);
  2449. - if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
  2450. + int k = arrayindex(key,MAXASIZE);
  2451. + if (k>0) { /* appropriate array index? */
  2452. nums[ceillog2(k)]++; /* count as such */
  2453. return 1;
  2454. }
  2455. @@ -308,7 +322,7 @@ static void resize (lua_State *L, Table
  2456. /* re-insert elements from vanishing slice */
  2457. for (i=nasize; i<oldasize; i++) {
  2458. if (!ttisnil(&t->array[i]))
  2459. - setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);
  2460. + setobjt2t(L, luaH_setint(L, t, i+1), &t->array[i]);
  2461. }
  2462. /* shrink array */
  2463. luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
  2464. @@ -409,7 +423,9 @@ static TValue *newkey (lua_State *L, Tab
  2465. othern = mainposition(t, key2tval(mp));
  2466. if (othern != mp) { /* is colliding node out of its main position? */
  2467. /* yes; move colliding node into free position */
  2468. - while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
  2469. + while (gnext(othern) != mp) {
  2470. + othern = gnext(othern); /* find previous */
  2471. + }
  2472. gnext(othern) = n; /* redo the chain with `n' in place of `mp' */
  2473. *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
  2474. gnext(mp) = NULL; /* now `mp' is free */
  2475. @@ -432,17 +448,18 @@ static TValue *newkey (lua_State *L, Tab
  2476. /*
  2477. ** search function for integers
  2478. */
  2479. -const TValue *luaH_getnum (Table *t, int key) {
  2480. +const TValue *luaH_getint (Table *t, lua_Integer key) {
  2481. /* (1 <= key && key <= t->sizearray) */
  2482. if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))
  2483. return &t->array[key-1];
  2484. else {
  2485. - lua_Number nk = cast_num(key);
  2486. - Node *n = hashnum(t, nk);
  2487. + Node *n = hashint(t, key);
  2488. do { /* check whether `key' is somewhere in the chain */
  2489. - if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))
  2490. + if (ttisint(gkey(n)) && (ivalue(gkey(n)) == key)) {
  2491. return gval(n); /* that's it */
  2492. - else n = gnext(n);
  2493. + } else {
  2494. + n = gnext(n);
  2495. + }
  2496. } while (n);
  2497. return luaO_nilobject;
  2498. }
  2499. @@ -470,14 +487,12 @@ const TValue *luaH_get (Table *t, const
  2500. switch (ttype(key)) {
  2501. case LUA_TNIL: return luaO_nilobject;
  2502. case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
  2503. + case LUA_TINT: return luaH_getint(t, ivalue(key));
  2504. case LUA_TNUMBER: {
  2505. - int k;
  2506. - lua_Number n = nvalue(key);
  2507. - lua_number2int(k, n);
  2508. - if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
  2509. - return luaH_getnum(t, k); /* use specialized version */
  2510. - /* else go through */
  2511. - }
  2512. + lua_Integer i;
  2513. + if (tt_integer_valued(key,&i))
  2514. + return luaH_getint(t,i);
  2515. + } /* pass through */
  2516. default: {
  2517. Node *n = mainposition(t, key);
  2518. do { /* check whether `key' is somewhere in the chain */
  2519. @@ -498,20 +513,25 @@ TValue *luaH_set (lua_State *L, Table *t
  2520. return cast(TValue *, p);
  2521. else {
  2522. if (ttisnil(key)) luaG_runerror(L, "table index is nil");
  2523. - else if (ttisnumber(key) && luai_numisnan(nvalue(key)))
  2524. - luaG_runerror(L, "table index is NaN");
  2525. + else if (ttype(key)==LUA_TNUMBER) {
  2526. + lua_Integer k;
  2527. + if (luai_numisnan(nvalue_fast(key)))
  2528. + luaG_runerror(L, "table index is NaN");
  2529. + if (tt_integer_valued(key,&k))
  2530. + return luaH_setint(L, t, k);
  2531. + }
  2532. return newkey(L, t, key);
  2533. }
  2534. }
  2535. -TValue *luaH_setnum (lua_State *L, Table *t, int key) {
  2536. - const TValue *p = luaH_getnum(t, key);
  2537. +TValue *luaH_setint (lua_State *L, Table *t, lua_Integer key) {
  2538. + const TValue *p = luaH_getint(t, key);
  2539. if (p != luaO_nilobject)
  2540. return cast(TValue *, p);
  2541. else {
  2542. TValue k;
  2543. - setnvalue(&k, cast_num(key));
  2544. + setivalue(&k, key);
  2545. return newkey(L, t, &k);
  2546. }
  2547. }
  2548. @@ -533,20 +553,21 @@ static int unbound_search (Table *t, uns
  2549. unsigned int i = j; /* i is zero or a present index */
  2550. j++;
  2551. /* find `i' and `j' such that i is present and j is not */
  2552. - while (!ttisnil(luaH_getnum(t, j))) {
  2553. + while (!ttisnil(luaH_getint(t, j))) {
  2554. i = j;
  2555. j *= 2;
  2556. if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
  2557. /* table was built with bad purposes: resort to linear search */
  2558. - i = 1;
  2559. - while (!ttisnil(luaH_getnum(t, i))) i++;
  2560. - return i - 1;
  2561. + for( i = 1; i<MAX_INT+1; i++ ) {
  2562. + if (ttisnil(luaH_getint(t, i))) break;
  2563. + }
  2564. + return i - 1; /* up to MAX_INT */
  2565. }
  2566. }
  2567. /* now do a binary search between them */
  2568. while (j - i > 1) {
  2569. unsigned int m = (i+j)/2;
  2570. - if (ttisnil(luaH_getnum(t, m))) j = m;
  2571. + if (ttisnil(luaH_getint(t, m))) j = m;
  2572. else i = m;
  2573. }
  2574. return i;
  2575. --- a/src/ltable.h
  2576. +++ b/src/ltable.h
  2577. @@ -18,8 +18,8 @@
  2578. #define key2tval(n) (&(n)->i_key.tvk)
  2579. -LUAI_FUNC const TValue *luaH_getnum (Table *t, int key);
  2580. -LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key);
  2581. +LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
  2582. +LUAI_FUNC TValue *luaH_setint (lua_State *L, Table *t, lua_Integer key);
  2583. LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
  2584. LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key);
  2585. LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
  2586. --- a/src/ltm.c
  2587. +++ b/src/ltm.c
  2588. @@ -19,7 +19,6 @@
  2589. #include "ltm.h"
  2590. -
  2591. const char *const luaT_typenames[] = {
  2592. "nil", "boolean", "userdata", "number",
  2593. "string", "table", "function", "userdata", "thread",
  2594. @@ -67,6 +66,9 @@ const TValue *luaT_gettmbyobj (lua_State
  2595. case LUA_TUSERDATA:
  2596. mt = uvalue(o)->metatable;
  2597. break;
  2598. + case LUA_TINT:
  2599. + mt = G(L)->mt[LUA_TNUMBER];
  2600. + break;
  2601. default:
  2602. mt = G(L)->mt[ttype(o)];
  2603. }
  2604. --- a/src/lua.c
  2605. +++ b/src/lua.c
  2606. @@ -16,7 +16,7 @@
  2607. #include "lauxlib.h"
  2608. #include "lualib.h"
  2609. -
  2610. +#include "llimits.h"
  2611. static lua_State *globalL = NULL;
  2612. @@ -382,6 +382,15 @@ int main (int argc, char **argv) {
  2613. l_message(argv[0], "cannot create state: not enough memory");
  2614. return EXIT_FAILURE;
  2615. }
  2616. + /* Checking 'sizeof(lua_Integer)' cannot be made in preprocessor on all compilers.
  2617. + */
  2618. +#ifdef LNUM_INT16
  2619. + lua_assert( sizeof(lua_Integer) == 2 );
  2620. +#elif defined(LNUM_INT32)
  2621. + lua_assert( sizeof(lua_Integer) == 4 );
  2622. +#elif defined(LNUM_INT64)
  2623. + lua_assert( sizeof(lua_Integer) == 8 );
  2624. +#endif
  2625. s.argc = argc;
  2626. s.argv = argv;
  2627. status = lua_cpcall(L, &pmain, &s);
  2628. --- a/src/lua.h
  2629. +++ b/src/lua.h
  2630. @@ -19,7 +19,7 @@
  2631. #define LUA_VERSION "Lua 5.1"
  2632. #define LUA_RELEASE "Lua 5.1.5"
  2633. #define LUA_VERSION_NUM 501
  2634. -#define LUA_COPYRIGHT "Copyright (C) 1994-2012 Lua.org, PUC-Rio"
  2635. +#define LUA_COPYRIGHT "Copyright (C) 1994-2012 Lua.org, PUC-Rio" " (" LUA_LNUM ")"
  2636. #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
  2637. @@ -71,6 +71,16 @@ typedef void * (*lua_Alloc) (void *ud, v
  2638. */
  2639. #define LUA_TNONE (-1)
  2640. +/* LUA_TINT is an internal type, not visible to applications. There are three
  2641. + * potential values where it can be tweaked to (code autoadjusts to these):
  2642. + *
  2643. + * -2: not 'usual' type value; good since 'LUA_TINT' is not part of the API
  2644. + * LUA_TNUMBER+1: shifts other type values upwards, breaking binary compatibility
  2645. + * not acceptable for 5.1, maybe 5.2 onwards?
  2646. + * 9: greater than existing (5.1) type values.
  2647. +*/
  2648. +#define LUA_TINT (-2)
  2649. +
  2650. #define LUA_TNIL 0
  2651. #define LUA_TBOOLEAN 1
  2652. #define LUA_TLIGHTUSERDATA 2
  2653. @@ -139,6 +149,8 @@ LUA_API int (lua_isuserdata)
  2654. LUA_API int (lua_type) (lua_State *L, int idx);
  2655. LUA_API const char *(lua_typename) (lua_State *L, int tp);
  2656. +LUA_API int (lua_isinteger) (lua_State *L, int idx);
  2657. +
  2658. LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2);
  2659. LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
  2660. LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2);
  2661. @@ -244,6 +256,19 @@ LUA_API lua_Alloc (lua_getallocf) (lua_S
  2662. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
  2663. +/*
  2664. +* It is unnecessary to break Lua C API 'lua_tonumber()' compatibility, just
  2665. +* because the Lua number type is complex. Most C modules would use scalars
  2666. +* only. We'll introduce new 'lua_tocomplex' and 'lua_pushcomplex' for when
  2667. +* the module really wants to use them.
  2668. +*/
  2669. +#ifdef LNUM_COMPLEX
  2670. + #include <complex.h>
  2671. + typedef LUA_NUMBER complex lua_Complex;
  2672. + LUA_API lua_Complex (lua_tocomplex) (lua_State *L, int idx);
  2673. + LUA_API void (lua_pushcomplex) (lua_State *L, lua_Complex v);
  2674. +#endif
  2675. +
  2676. /*
  2677. ** ===============================================================
  2678. @@ -268,7 +293,12 @@ LUA_API void lua_setallocf (lua_State *L
  2679. #define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN)
  2680. #define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
  2681. #define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
  2682. -#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
  2683. +
  2684. +#if LUA_TINT < 0
  2685. +# define lua_isnoneornil(L, n) ( (lua_type(L,(n)) <= 0) && (lua_type(L,(n)) != LUA_TINT) )
  2686. +#else
  2687. +# define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
  2688. +#endif
  2689. #define lua_pushliteral(L, s) \
  2690. lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
  2691. @@ -386,3 +416,4 @@ struct lua_Debug {
  2692. #endif
  2693. +
  2694. --- a/src/luaconf.h
  2695. +++ b/src/luaconf.h
  2696. @@ -10,7 +10,9 @@
  2697. #include <limits.h>
  2698. #include <stddef.h>
  2699. -
  2700. +#ifdef lua_assert
  2701. +# include <assert.h>
  2702. +#endif
  2703. /*
  2704. ** ==================================================================
  2705. @@ -136,14 +138,38 @@
  2706. /*
  2707. -@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger.
  2708. -** CHANGE that if ptrdiff_t is not adequate on your machine. (On most
  2709. -** machines, ptrdiff_t gives a good choice between int or long.)
  2710. +@@ LUAI_BITSINT defines the number of bits in an int.
  2711. +** CHANGE here if Lua cannot automatically detect the number of bits of
  2712. +** your machine. Probably you do not need to change this.
  2713. */
  2714. -#define LUA_INTEGER ptrdiff_t
  2715. +/* avoid overflows in comparison */
  2716. +#if INT_MAX-20 < 32760
  2717. +#define LUAI_BITSINT 16
  2718. +#elif INT_MAX > 2147483640L
  2719. +/* int has at least 32 bits */
  2720. +#define LUAI_BITSINT 32
  2721. +#else
  2722. +#error "you must define LUA_BITSINT with number of bits in an integer"
  2723. +#endif
  2724. /*
  2725. +@@ LNUM_DOUBLE | LNUM_FLOAT | LNUM_LDOUBLE: Generic Lua number mode
  2726. +@@ LNUM_INT32 | LNUM_INT64: Integer type
  2727. +@@ LNUM_COMPLEX: Define for using 'a+bi' numbers
  2728. +@@
  2729. +@@ You can combine LNUM_xxx but only one of each group. I.e. '-DLNUM_FLOAT
  2730. +@@ -DLNUM_INT32 -DLNUM_COMPLEX' gives float range complex numbers, with
  2731. +@@ 32-bit scalar integer range optimized.
  2732. +**
  2733. +** These are kept in a separate configuration file mainly for ease of patching
  2734. +** (can be changed if integerated to Lua proper).
  2735. +*/
  2736. +/*#define LNUM_DOUBLE*/
  2737. +/*#define LNUM_INT32*/
  2738. +#include "lnum_config.h"
  2739. +
  2740. +/*
  2741. @@ LUA_API is a mark for all core API functions.
  2742. @@ LUALIB_API is a mark for all standard library functions.
  2743. ** CHANGE them if you need to define those functions in some special way.
  2744. @@ -383,22 +409,6 @@
  2745. /*
  2746. -@@ LUAI_BITSINT defines the number of bits in an int.
  2747. -** CHANGE here if Lua cannot automatically detect the number of bits of
  2748. -** your machine. Probably you do not need to change this.
  2749. -*/
  2750. -/* avoid overflows in comparison */
  2751. -#if INT_MAX-20 < 32760
  2752. -#define LUAI_BITSINT 16
  2753. -#elif INT_MAX > 2147483640L
  2754. -/* int has at least 32 bits */
  2755. -#define LUAI_BITSINT 32
  2756. -#else
  2757. -#error "you must define LUA_BITSINT with number of bits in an integer"
  2758. -#endif
  2759. -
  2760. -
  2761. -/*
  2762. @@ LUAI_UINT32 is an unsigned integer with at least 32 bits.
  2763. @@ LUAI_INT32 is an signed integer with at least 32 bits.
  2764. @@ LUAI_UMEM is an unsigned integer big enough to count the total
  2765. @@ -425,6 +435,15 @@
  2766. #define LUAI_MEM long
  2767. #endif
  2768. +/*
  2769. +@@ LUAI_BOOL carries 0 and nonzero (normally 1). It may be defined as 'char'
  2770. +** (to save memory), 'int' (for speed), 'bool' (for C++) or '_Bool' (C99)
  2771. +*/
  2772. +#ifdef __cplusplus
  2773. +# define LUAI_BOOL bool
  2774. +#else
  2775. +# define LUAI_BOOL int
  2776. +#endif
  2777. /*
  2778. @@ LUAI_MAXCALLS limits the number of nested calls.
  2779. @@ -490,101 +509,6 @@
  2780. /* }================================================================== */
  2781. -
  2782. -
  2783. -/*
  2784. -** {==================================================================
  2785. -@@ LUA_NUMBER is the type of numbers in Lua.
  2786. -** CHANGE the following definitions only if you want to build Lua
  2787. -** with a number type different from double. You may also need to
  2788. -** change lua_number2int & lua_number2integer.
  2789. -** ===================================================================
  2790. -*/
  2791. -
  2792. -#define LUA_NUMBER_DOUBLE
  2793. -#define LUA_NUMBER double
  2794. -
  2795. -/*
  2796. -@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
  2797. -@* over a number.
  2798. -*/
  2799. -#define LUAI_UACNUMBER double
  2800. -
  2801. -
  2802. -/*
  2803. -@@ LUA_NUMBER_SCAN is the format for reading numbers.
  2804. -@@ LUA_NUMBER_FMT is the format for writing numbers.
  2805. -@@ lua_number2str converts a number to a string.
  2806. -@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
  2807. -@@ lua_str2number converts a string to a number.
  2808. -*/
  2809. -#define LUA_NUMBER_SCAN "%lf"
  2810. -#define LUA_NUMBER_FMT "%.14g"
  2811. -#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
  2812. -#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
  2813. -#define lua_str2number(s,p) strtod((s), (p))
  2814. -
  2815. -
  2816. -/*
  2817. -@@ The luai_num* macros define the primitive operations over numbers.
  2818. -*/
  2819. -#if defined(LUA_CORE)
  2820. -#include <math.h>
  2821. -#define luai_numadd(a,b) ((a)+(b))
  2822. -#define luai_numsub(a,b) ((a)-(b))
  2823. -#define luai_nummul(a,b) ((a)*(b))
  2824. -#define luai_numdiv(a,b) ((a)/(b))
  2825. -#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b))
  2826. -#define luai_numpow(a,b) (pow(a,b))
  2827. -#define luai_numunm(a) (-(a))
  2828. -#define luai_numeq(a,b) ((a)==(b))
  2829. -#define luai_numlt(a,b) ((a)<(b))
  2830. -#define luai_numle(a,b) ((a)<=(b))
  2831. -#define luai_numisnan(a) (!luai_numeq((a), (a)))
  2832. -#endif
  2833. -
  2834. -
  2835. -/*
  2836. -@@ lua_number2int is a macro to convert lua_Number to int.
  2837. -@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
  2838. -** CHANGE them if you know a faster way to convert a lua_Number to
  2839. -** int (with any rounding method and without throwing errors) in your
  2840. -** system. In Pentium machines, a naive typecast from double to int
  2841. -** in C is extremely slow, so any alternative is worth trying.
  2842. -*/
  2843. -
  2844. -/* On a Pentium, resort to a trick */
  2845. -#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
  2846. - (defined(__i386) || defined (_M_IX86) || defined(__i386__))
  2847. -
  2848. -/* On a Microsoft compiler, use assembler */
  2849. -#if defined(_MSC_VER)
  2850. -
  2851. -#define lua_number2int(i,d) __asm fld d __asm fistp i
  2852. -#define lua_number2integer(i,n) lua_number2int(i, n)
  2853. -
  2854. -/* the next trick should work on any Pentium, but sometimes clashes
  2855. - with a DirectX idiosyncrasy */
  2856. -#else
  2857. -
  2858. -union luai_Cast { double l_d; long l_l; };
  2859. -#define lua_number2int(i,d) \
  2860. - { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
  2861. -#define lua_number2integer(i,n) lua_number2int(i, n)
  2862. -
  2863. -#endif
  2864. -
  2865. -
  2866. -/* this option always works, but may be slow */
  2867. -#else
  2868. -#define lua_number2int(i,d) ((i)=(int)(d))
  2869. -#define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
  2870. -
  2871. -#endif
  2872. -
  2873. -/* }================================================================== */
  2874. -
  2875. -
  2876. /*
  2877. @@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment.
  2878. ** CHANGE it if your system requires alignments larger than double. (For
  2879. @@ -728,28 +652,6 @@ union luai_Cast { double l_d; long l_l;
  2880. #define luai_userstateyield(L,n) ((void)L)
  2881. -/*
  2882. -@@ LUA_INTFRMLEN is the length modifier for integer conversions
  2883. -@* in 'string.format'.
  2884. -@@ LUA_INTFRM_T is the integer type correspoding to the previous length
  2885. -@* modifier.
  2886. -** CHANGE them if your system supports long long or does not support long.
  2887. -*/
  2888. -
  2889. -#if defined(LUA_USELONGLONG)
  2890. -
  2891. -#define LUA_INTFRMLEN "ll"
  2892. -#define LUA_INTFRM_T long long
  2893. -
  2894. -#else
  2895. -
  2896. -#define LUA_INTFRMLEN "l"
  2897. -#define LUA_INTFRM_T long
  2898. -
  2899. -#endif
  2900. -
  2901. -
  2902. -
  2903. /* =================================================================== */
  2904. /*
  2905. --- a/src/lundump.c
  2906. +++ b/src/lundump.c
  2907. @@ -73,6 +73,13 @@ static lua_Number LoadNumber(LoadState*
  2908. return x;
  2909. }
  2910. +static lua_Integer LoadInteger(LoadState* S)
  2911. +{
  2912. + lua_Integer x;
  2913. + LoadVar(S,x);
  2914. + return x;
  2915. +}
  2916. +
  2917. static TString* LoadString(LoadState* S)
  2918. {
  2919. size_t size;
  2920. @@ -119,6 +126,9 @@ static void LoadConstants(LoadState* S,
  2921. case LUA_TNUMBER:
  2922. setnvalue(o,LoadNumber(S));
  2923. break;
  2924. + case LUA_TINT: /* Integer type saved in bytecode (see lcode.c) */
  2925. + setivalue(o,LoadInteger(S));
  2926. + break;
  2927. case LUA_TSTRING:
  2928. setsvalue2n(S->L,o,LoadString(S));
  2929. break;
  2930. @@ -223,5 +233,22 @@ void luaU_header (char* h)
  2931. *h++=(char)sizeof(size_t);
  2932. *h++=(char)sizeof(Instruction);
  2933. *h++=(char)sizeof(lua_Number);
  2934. - *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
  2935. +
  2936. + /*
  2937. + * Last byte of header (0/1 in unpatched Lua 5.1.3):
  2938. + *
  2939. + * 0: lua_Number is float or double, lua_Integer not used. (nonpatched only)
  2940. + * 1: lua_Number is integer (nonpatched only)
  2941. + *
  2942. + * +2: LNUM_INT16: sizeof(lua_Integer)
  2943. + * +4: LNUM_INT32: sizeof(lua_Integer)
  2944. + * +8: LNUM_INT64: sizeof(lua_Integer)
  2945. + *
  2946. + * +0x80: LNUM_COMPLEX
  2947. + */
  2948. + *h++ = (char)(sizeof(lua_Integer)
  2949. +#ifdef LNUM_COMPLEX
  2950. + | 0x80
  2951. +#endif
  2952. + );
  2953. }
  2954. --- a/src/lvm.c
  2955. +++ b/src/lvm.c
  2956. @@ -25,22 +25,35 @@
  2957. #include "ltable.h"
  2958. #include "ltm.h"
  2959. #include "lvm.h"
  2960. -
  2961. -
  2962. +#include "llex.h"
  2963. +#include "lnum.h"
  2964. /* limit for table tag-method chains (to avoid loops) */
  2965. #define MAXTAGLOOP 100
  2966. -const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
  2967. - lua_Number num;
  2968. +/*
  2969. + * If 'obj' is a string, it is tried to be interpreted as a number.
  2970. + */
  2971. +const TValue *luaV_tonumber ( const TValue *obj, TValue *n) {
  2972. + lua_Number d;
  2973. + lua_Integer i;
  2974. +
  2975. if (ttisnumber(obj)) return obj;
  2976. - if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
  2977. - setnvalue(n, num);
  2978. - return n;
  2979. - }
  2980. - else
  2981. - return NULL;
  2982. +
  2983. + if (ttisstring(obj)) {
  2984. + switch( luaO_str2d( svalue(obj), &d, &i ) ) {
  2985. + case TK_INT:
  2986. + setivalue(n,i); return n;
  2987. + case TK_NUMBER:
  2988. + setnvalue(n,d); return n;
  2989. +#ifdef LNUM_COMPLEX
  2990. + case TK_NUMBER2: /* "N.NNNi", != 0 */
  2991. + setnvalue_complex_fast(n, d*I); return n;
  2992. +#endif
  2993. + }
  2994. + }
  2995. + return NULL;
  2996. }
  2997. @@ -49,8 +62,7 @@ int luaV_tostring (lua_State *L, StkId o
  2998. return 0;
  2999. else {
  3000. char s[LUAI_MAXNUMBER2STR];
  3001. - lua_Number n = nvalue(obj);
  3002. - lua_number2str(s, n);
  3003. + luaO_num2buf(s,obj);
  3004. setsvalue2s(L, obj, luaS_new(L, s));
  3005. return 1;
  3006. }
  3007. @@ -222,59 +234,127 @@ static int l_strcmp (const TString *ls,
  3008. }
  3009. +#ifdef LNUM_COMPLEX
  3010. +void error_complex( lua_State *L, const TValue *l, const TValue *r )
  3011. +{
  3012. + char buf1[ LUAI_MAXNUMBER2STR ];
  3013. + char buf2[ LUAI_MAXNUMBER2STR ];
  3014. + luaO_num2buf( buf1, l );
  3015. + luaO_num2buf( buf2, r );
  3016. + luaG_runerror( L, "unable to compare: %s with %s", buf1, buf2 );
  3017. + /* no return */
  3018. +}
  3019. +#endif
  3020. +
  3021. +
  3022. int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
  3023. int res;
  3024. - if (ttype(l) != ttype(r))
  3025. + int tl,tr;
  3026. + lua_Integer tmp;
  3027. +
  3028. + if (!ttype_ext_same(l,r))
  3029. return luaG_ordererror(L, l, r);
  3030. - else if (ttisnumber(l))
  3031. - return luai_numlt(nvalue(l), nvalue(r));
  3032. - else if (ttisstring(l))
  3033. - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
  3034. - else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
  3035. +#ifdef LNUM_COMPLEX
  3036. + if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) )
  3037. + error_complex( L, l, r );
  3038. +#endif
  3039. + tl= ttype(l); tr= ttype(r);
  3040. + if (tl==tr) { /* clear arithmetics */
  3041. + switch(tl) {
  3042. + case LUA_TINT: return ivalue(l) < ivalue(r);
  3043. + case LUA_TNUMBER: return luai_numlt(nvalue_fast(l), nvalue_fast(r));
  3044. + case LUA_TSTRING: return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
  3045. + }
  3046. + } else if (tl==LUA_TINT) { /* l:int, r:num */
  3047. + /* Avoid accuracy losing casts: if 'r' is integer by value, do comparisons
  3048. + * in integer realm. Only otherwise cast 'l' to FP (which might change its
  3049. + * value).
  3050. + */
  3051. + if (tt_integer_valued(r,&tmp))
  3052. + return ivalue(l) < tmp;
  3053. + else
  3054. + return luai_numlt( cast_num(ivalue(l)), nvalue_fast(r) );
  3055. +
  3056. + } else if (tl==LUA_TNUMBER) { /* l:num, r:int */
  3057. + if (tt_integer_valued(l,&tmp))
  3058. + return tmp < ivalue(r);
  3059. + else
  3060. + return luai_numlt( nvalue_fast(l), cast_num(ivalue(r)) );
  3061. +
  3062. + } else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
  3063. return res;
  3064. +
  3065. return luaG_ordererror(L, l, r);
  3066. }
  3067. static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
  3068. int res;
  3069. - if (ttype(l) != ttype(r))
  3070. + int tl, tr;
  3071. + lua_Integer tmp;
  3072. +
  3073. + if (!ttype_ext_same(l,r))
  3074. return luaG_ordererror(L, l, r);
  3075. - else if (ttisnumber(l))
  3076. - return luai_numle(nvalue(l), nvalue(r));
  3077. - else if (ttisstring(l))
  3078. - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
  3079. - else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
  3080. +#ifdef LNUM_COMPLEX
  3081. + if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) )
  3082. + error_complex( L, l, r );
  3083. +#endif
  3084. + tl= ttype(l); tr= ttype(r);
  3085. + if (tl==tr) { /* clear arithmetics */
  3086. + switch(tl) {
  3087. + case LUA_TINT: return ivalue(l) <= ivalue(r);
  3088. + case LUA_TNUMBER: return luai_numle(nvalue_fast(l), nvalue_fast(r));
  3089. + case LUA_TSTRING: return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
  3090. + }
  3091. + }
  3092. + if (tl==LUA_TINT) { /* l:int, r:num */
  3093. + if (tt_integer_valued(r,&tmp))
  3094. + return ivalue(l) <= tmp;
  3095. + else
  3096. + return luai_numle( cast_num(ivalue(l)), nvalue_fast(r) );
  3097. +
  3098. + } else if (tl==LUA_TNUMBER) { /* l:num, r:int */
  3099. + if (tt_integer_valued(l,&tmp))
  3100. + return tmp <= ivalue(r);
  3101. + else
  3102. + return luai_numle( nvalue_fast(l), cast_num(ivalue(r)) );
  3103. +
  3104. + } else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
  3105. return res;
  3106. else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
  3107. return !res;
  3108. +
  3109. return luaG_ordererror(L, l, r);
  3110. }
  3111. -int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
  3112. +/* Note: 'luaV_equalval()' and 'luaO_rawequalObj()' have largely overlapping
  3113. + * implementation. LUA_TNIL..LUA_TLIGHTUSERDATA cases could be handled
  3114. + * simply by the 'default' case here.
  3115. + */
  3116. +int luaV_equalval (lua_State *L, const TValue *l, const TValue *r) {
  3117. const TValue *tm;
  3118. - lua_assert(ttype(t1) == ttype(t2));
  3119. - switch (ttype(t1)) {
  3120. + lua_assert(ttype_ext_same(l,r));
  3121. + switch (ttype(l)) {
  3122. case LUA_TNIL: return 1;
  3123. - case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
  3124. - case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
  3125. - case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
  3126. + case LUA_TINT:
  3127. + case LUA_TNUMBER: return luaO_rawequalObj(l,r);
  3128. + case LUA_TBOOLEAN: return bvalue(l) == bvalue(r); /* true must be 1 !! */
  3129. + case LUA_TLIGHTUSERDATA: return pvalue(l) == pvalue(r);
  3130. case LUA_TUSERDATA: {
  3131. - if (uvalue(t1) == uvalue(t2)) return 1;
  3132. - tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
  3133. - TM_EQ);
  3134. + if (uvalue(l) == uvalue(r)) return 1;
  3135. + tm = get_compTM(L, uvalue(l)->metatable, uvalue(r)->metatable, TM_EQ);
  3136. break; /* will try TM */
  3137. }
  3138. case LUA_TTABLE: {
  3139. - if (hvalue(t1) == hvalue(t2)) return 1;
  3140. - tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
  3141. + if (hvalue(l) == hvalue(r)) return 1;
  3142. + tm = get_compTM(L, hvalue(l)->metatable, hvalue(r)->metatable, TM_EQ);
  3143. break; /* will try TM */
  3144. }
  3145. - default: return gcvalue(t1) == gcvalue(t2);
  3146. + default: return gcvalue(l) == gcvalue(r);
  3147. }
  3148. if (tm == NULL) return 0; /* no TM? */
  3149. - callTMres(L, L->top, tm, t1, t2); /* call TM */
  3150. + callTMres(L, L->top, tm, l, r); /* call TM */
  3151. return !l_isfalse(L->top);
  3152. }
  3153. @@ -314,30 +394,6 @@ void luaV_concat (lua_State *L, int tota
  3154. }
  3155. -static void Arith (lua_State *L, StkId ra, const TValue *rb,
  3156. - const TValue *rc, TMS op) {
  3157. - TValue tempb, tempc;
  3158. - const TValue *b, *c;
  3159. - if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
  3160. - (c = luaV_tonumber(rc, &tempc)) != NULL) {
  3161. - lua_Number nb = nvalue(b), nc = nvalue(c);
  3162. - switch (op) {
  3163. - case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
  3164. - case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
  3165. - case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
  3166. - case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
  3167. - case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
  3168. - case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
  3169. - case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
  3170. - default: lua_assert(0); break;
  3171. - }
  3172. - }
  3173. - else if (!call_binTM(L, rb, rc, ra, op))
  3174. - luaG_aritherror(L, rb, rc);
  3175. -}
  3176. -
  3177. -
  3178. -
  3179. /*
  3180. ** some macros for common tasks in `luaV_execute'
  3181. */
  3182. @@ -361,17 +417,154 @@ static void Arith (lua_State *L, StkId r
  3183. #define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
  3184. -#define arith_op(op,tm) { \
  3185. - TValue *rb = RKB(i); \
  3186. - TValue *rc = RKC(i); \
  3187. - if (ttisnumber(rb) && ttisnumber(rc)) { \
  3188. - lua_Number nb = nvalue(rb), nc = nvalue(rc); \
  3189. - setnvalue(ra, op(nb, nc)); \
  3190. - } \
  3191. - else \
  3192. - Protect(Arith(L, ra, rb, rc, tm)); \
  3193. +/* Note: if called for unary operations, 'rc'=='rb'.
  3194. + */
  3195. +static void Arith (lua_State *L, StkId ra, const TValue *rb,
  3196. + const TValue *rc, TMS op) {
  3197. + TValue tempb, tempc;
  3198. + const TValue *b, *c;
  3199. + lua_Number nb,nc;
  3200. +
  3201. + if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
  3202. + (c = luaV_tonumber(rc, &tempc)) != NULL) {
  3203. +
  3204. + /* Keep integer arithmetics in the integer realm, if possible.
  3205. + */
  3206. + if (ttisint(b) && ttisint(c)) {
  3207. + lua_Integer ib = ivalue(b), ic = ivalue(c);
  3208. + lua_Integer *ri = &ra->value.i;
  3209. + ra->tt= LUA_TINT; /* part of 'setivalue(ra)' */
  3210. + switch (op) {
  3211. + case TM_ADD: if (try_addint( ri, ib, ic)) return; break;
  3212. + case TM_SUB: if (try_subint( ri, ib, ic)) return; break;
  3213. + case TM_MUL: if (try_mulint( ri, ib, ic)) return; break;
  3214. + case TM_DIV: if (try_divint( ri, ib, ic)) return; break;
  3215. + case TM_MOD: if (try_modint( ri, ib, ic)) return; break;
  3216. + case TM_POW: if (try_powint( ri, ib, ic)) return; break;
  3217. + case TM_UNM: if (try_unmint( ri, ib)) return; break;
  3218. + default: lua_assert(0);
  3219. }
  3220. + }
  3221. + /* Fallback to floating point, when leaving range. */
  3222. +#ifdef LNUM_COMPLEX
  3223. + if ((nvalue_img(b)!=0) || (nvalue_img(c)!=0)) {
  3224. + lua_Complex r;
  3225. + if (op==TM_UNM) {
  3226. + r= -nvalue_complex_fast(b); /* never an integer (or scalar) */
  3227. + setnvalue_complex_fast( ra, r );
  3228. + } else {
  3229. + lua_Complex bb= nvalue_complex(b), cc= nvalue_complex(c);
  3230. + switch (op) {
  3231. + case TM_ADD: r= bb + cc; break;
  3232. + case TM_SUB: r= bb - cc; break;
  3233. + case TM_MUL: r= bb * cc; break;
  3234. + case TM_DIV: r= bb / cc; break;
  3235. + case TM_MOD:
  3236. + luaG_runerror(L, "attempt to use %% on complex numbers"); /* no return */
  3237. + case TM_POW: r= luai_vectpow( bb, cc ); break;
  3238. + default: lua_assert(0); r=0;
  3239. + }
  3240. + setnvalue_complex( ra, r );
  3241. + }
  3242. + return;
  3243. + }
  3244. +#endif
  3245. + nb = nvalue(b); nc = nvalue(c);
  3246. + switch (op) {
  3247. + case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); return;
  3248. + case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); return;
  3249. + case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); return;
  3250. + case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); return;
  3251. + case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); return;
  3252. + case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); return;
  3253. + case TM_UNM: setnvalue(ra, luai_numunm(nb)); return;
  3254. + default: lua_assert(0);
  3255. + }
  3256. + }
  3257. +
  3258. + /* Either operand not a number */
  3259. + if (!call_binTM(L, rb, rc, ra, op))
  3260. + luaG_aritherror(L, rb, rc);
  3261. +}
  3262. +
  3263. +/* Helper macro to sort arithmetic operations into four categories:
  3264. + * TK_INT: integer - integer operands
  3265. + * TK_NUMBER: number - number (non complex, either may be integer)
  3266. + * TK_NUMBER2: complex numbers (at least the other)
  3267. + * 0: non-numeric (at least the other)
  3268. +*/
  3269. +#ifdef LNUM_COMPLEX
  3270. +static inline int arith_mode( const TValue *rb, const TValue *rc ) {
  3271. + if (ttisint(rb) && ttisint(rc)) return TK_INT;
  3272. + if (ttiscomplex(rb) || ttiscomplex(rc)) return TK_NUMBER2;
  3273. + if (ttisnumber(rb) && ttisnumber(rc)) return TK_NUMBER;
  3274. + return 0;
  3275. +}
  3276. +#else
  3277. +# define arith_mode(rb,rc) \
  3278. + ( (ttisint(rb) && ttisint(rc)) ? TK_INT : \
  3279. + (ttisnumber(rb) && ttisnumber(rc)) ? TK_NUMBER : 0 )
  3280. +#endif
  3281. +
  3282. +/* arith_op macro for two operators:
  3283. + * automatically chooses, which function (number, integer, complex) to use
  3284. + */
  3285. +#define ARITH_OP2_START( op_num, op_int ) \
  3286. + int failed= 0; \
  3287. + switch( arith_mode(rb,rc) ) { \
  3288. + case TK_INT: \
  3289. + if (op_int ( &(ra)->value.i, ivalue(rb), ivalue(rc) )) \
  3290. + { ra->tt= LUA_TINT; break; } /* else flow through */ \
  3291. + case TK_NUMBER: \
  3292. + setnvalue(ra, op_num ( nvalue(rb), nvalue(rc) )); break;
  3293. +
  3294. +#define ARITH_OP2_END \
  3295. + default: \
  3296. + failed= 1; break; \
  3297. + } if (!failed) continue;
  3298. +
  3299. +#define arith_op_continue_scalar( op_num, op_int ) \
  3300. + ARITH_OP2_START( op_num, op_int ) \
  3301. + ARITH_OP2_END
  3302. +
  3303. +#ifdef LNUM_COMPLEX
  3304. +# define arith_op_continue( op_num, op_int, op_complex ) \
  3305. + ARITH_OP2_START( op_num, op_int ) \
  3306. + case TK_NUMBER2: \
  3307. + setnvalue_complex( ra, op_complex ( nvalue_complex(rb), nvalue_complex(rc) ) ); break; \
  3308. + ARITH_OP2_END
  3309. +#else
  3310. +# define arith_op_continue(op_num,op_int,_) arith_op_continue_scalar(op_num,op_int)
  3311. +#endif
  3312. +
  3313. +/* arith_op macro for one operator:
  3314. + */
  3315. +#define ARITH_OP1_START( op_num, op_int ) \
  3316. + int failed= 0; \
  3317. + switch( arith_mode(rb,rb) ) { \
  3318. + case TK_INT: \
  3319. + if (op_int ( &(ra)->value.i, ivalue(rb) )) \
  3320. + { ra->tt= LUA_TINT; break; } /* else flow through */ \
  3321. + case TK_NUMBER: \
  3322. + setnvalue(ra, op_num (nvalue(rb))); break; \
  3323. +
  3324. +#define ARITH_OP1_END \
  3325. + default: \
  3326. + failed= 1; break; \
  3327. + } if (!failed) continue;
  3328. +
  3329. +#ifdef LNUM_COMPLEX
  3330. +# define arith_op1_continue( op_num, op_int, op_complex ) \
  3331. + ARITH_OP1_START( op_num, op_int ) \
  3332. + case TK_NUMBER2: \
  3333. + setnvalue_complex( ra, op_complex ( nvalue_complex_fast(rb) )); break; \
  3334. + ARITH_OP1_END
  3335. +#else
  3336. +# define arith_op1_continue( op_num, op_int, _ ) \
  3337. + ARITH_OP1_START( op_num, op_int ) \
  3338. + ARITH_OP1_END
  3339. +#endif
  3340. void luaV_execute (lua_State *L, int nexeccalls) {
  3341. @@ -472,38 +665,45 @@ void luaV_execute (lua_State *L, int nex
  3342. continue;
  3343. }
  3344. case OP_ADD: {
  3345. - arith_op(luai_numadd, TM_ADD);
  3346. + TValue *rb = RKB(i), *rc= RKC(i);
  3347. + arith_op_continue( luai_numadd, try_addint, luai_vectadd );
  3348. + Protect(Arith(L, ra, rb, rc, TM_ADD)); \
  3349. continue;
  3350. }
  3351. case OP_SUB: {
  3352. - arith_op(luai_numsub, TM_SUB);
  3353. + TValue *rb = RKB(i), *rc= RKC(i);
  3354. + arith_op_continue( luai_numsub, try_subint, luai_vectsub );
  3355. + Protect(Arith(L, ra, rb, rc, TM_SUB));
  3356. continue;
  3357. }
  3358. case OP_MUL: {
  3359. - arith_op(luai_nummul, TM_MUL);
  3360. + TValue *rb = RKB(i), *rc= RKC(i);
  3361. + arith_op_continue(luai_nummul, try_mulint, luai_vectmul);
  3362. + Protect(Arith(L, ra, rb, rc, TM_MUL));
  3363. continue;
  3364. }
  3365. case OP_DIV: {
  3366. - arith_op(luai_numdiv, TM_DIV);
  3367. + TValue *rb = RKB(i), *rc= RKC(i);
  3368. + arith_op_continue(luai_numdiv, try_divint, luai_vectdiv);
  3369. + Protect(Arith(L, ra, rb, rc, TM_DIV));
  3370. continue;
  3371. }
  3372. case OP_MOD: {
  3373. - arith_op(luai_nummod, TM_MOD);
  3374. + TValue *rb = RKB(i), *rc= RKC(i);
  3375. + arith_op_continue_scalar(luai_nummod, try_modint); /* scalars only */
  3376. + Protect(Arith(L, ra, rb, rc, TM_MOD));
  3377. continue;
  3378. }
  3379. case OP_POW: {
  3380. - arith_op(luai_numpow, TM_POW);
  3381. + TValue *rb = RKB(i), *rc= RKC(i);
  3382. + arith_op_continue(luai_numpow, try_powint, luai_vectpow);
  3383. + Protect(Arith(L, ra, rb, rc, TM_POW));
  3384. continue;
  3385. }
  3386. case OP_UNM: {
  3387. TValue *rb = RB(i);
  3388. - if (ttisnumber(rb)) {
  3389. - lua_Number nb = nvalue(rb);
  3390. - setnvalue(ra, luai_numunm(nb));
  3391. - }
  3392. - else {
  3393. - Protect(Arith(L, ra, rb, rb, TM_UNM));
  3394. - }
  3395. + arith_op1_continue(luai_numunm, try_unmint, luai_vectunm);
  3396. + Protect(Arith(L, ra, rb, rb, TM_UNM));
  3397. continue;
  3398. }
  3399. case OP_NOT: {
  3400. @@ -515,11 +715,11 @@ void luaV_execute (lua_State *L, int nex
  3401. const TValue *rb = RB(i);
  3402. switch (ttype(rb)) {
  3403. case LUA_TTABLE: {
  3404. - setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
  3405. + setivalue(ra, luaH_getn(hvalue(rb)));
  3406. break;
  3407. }
  3408. case LUA_TSTRING: {
  3409. - setnvalue(ra, cast_num(tsvalue(rb)->len));
  3410. + setivalue(ra, tsvalue(rb)->len);
  3411. break;
  3412. }
  3413. default: { /* try metamethod */
  3414. @@ -652,14 +852,30 @@ void luaV_execute (lua_State *L, int nex
  3415. }
  3416. }
  3417. case OP_FORLOOP: {
  3418. - lua_Number step = nvalue(ra+2);
  3419. - lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
  3420. - lua_Number limit = nvalue(ra+1);
  3421. - if (luai_numlt(0, step) ? luai_numle(idx, limit)
  3422. - : luai_numle(limit, idx)) {
  3423. - dojump(L, pc, GETARG_sBx(i)); /* jump back */
  3424. - setnvalue(ra, idx); /* update internal index... */
  3425. - setnvalue(ra+3, idx); /* ...and external index */
  3426. + /* If start,step and limit are all integers, we don't need to check
  3427. + * against overflow in the looping.
  3428. + */
  3429. + if (ttisint(ra) && ttisint(ra+1) && ttisint(ra+2)) {
  3430. + lua_Integer step = ivalue(ra+2);
  3431. + lua_Integer idx = ivalue(ra) + step; /* increment index */
  3432. + lua_Integer limit = ivalue(ra+1);
  3433. + if (step > 0 ? (idx <= limit) : (limit <= idx)) {
  3434. + dojump(L, pc, GETARG_sBx(i)); /* jump back */
  3435. + setivalue(ra, idx); /* update internal index... */
  3436. + setivalue(ra+3, idx); /* ...and external index */
  3437. + }
  3438. + } else {
  3439. + /* non-integer looping (don't use 'nvalue_fast', some may be integer!)
  3440. + */
  3441. + lua_Number step = nvalue(ra+2);
  3442. + lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
  3443. + lua_Number limit = nvalue(ra+1);
  3444. + if (luai_numlt(0, step) ? luai_numle(idx, limit)
  3445. + : luai_numle(limit, idx)) {
  3446. + dojump(L, pc, GETARG_sBx(i)); /* jump back */
  3447. + setnvalue(ra, idx); /* update internal index... */
  3448. + setnvalue(ra+3, idx); /* ...and external index */
  3449. + }
  3450. }
  3451. continue;
  3452. }
  3453. @@ -668,13 +884,21 @@ void luaV_execute (lua_State *L, int nex
  3454. const TValue *plimit = ra+1;
  3455. const TValue *pstep = ra+2;
  3456. L->savedpc = pc; /* next steps may throw errors */
  3457. + /* Using same location for tonumber's both arguments, effectively does
  3458. + * in-place modification (string->number). */
  3459. if (!tonumber(init, ra))
  3460. luaG_runerror(L, LUA_QL("for") " initial value must be a number");
  3461. else if (!tonumber(plimit, ra+1))
  3462. luaG_runerror(L, LUA_QL("for") " limit must be a number");
  3463. else if (!tonumber(pstep, ra+2))
  3464. luaG_runerror(L, LUA_QL("for") " step must be a number");
  3465. - setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
  3466. + /* Step back one value (keep within integers if we can)
  3467. + */
  3468. + if (!( ttisint(ra) && ttisint(pstep) &&
  3469. + try_subint( &ra->value.i, ivalue(ra), ivalue(pstep) ) )) {
  3470. + /* don't use 'nvalue_fast()', values may be integer */
  3471. + setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
  3472. + }
  3473. dojump(L, pc, GETARG_sBx(i));
  3474. continue;
  3475. }
  3476. @@ -711,7 +935,7 @@ void luaV_execute (lua_State *L, int nex
  3477. luaH_resizearray(L, h, last); /* pre-alloc it at once */
  3478. for (; n > 0; n--) {
  3479. TValue *val = ra+n;
  3480. - setobj2t(L, luaH_setnum(L, h, last--), val);
  3481. + setobj2t(L, luaH_setint(L, h, last--), val);
  3482. luaC_barriert(L, h, val);
  3483. }
  3484. continue;
  3485. --- a/src/lvm.h
  3486. +++ b/src/lvm.h
  3487. @@ -15,11 +15,9 @@
  3488. #define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o)))
  3489. -#define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \
  3490. - (((o) = luaV_tonumber(o,n)) != NULL))
  3491. +#define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL))
  3492. -#define equalobj(L,o1,o2) \
  3493. - (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2))
  3494. +#define equalobj(L,o1,o2) (ttype_ext_same(o1,o2) && luaV_equalval(L, o1, o2))
  3495. LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
  3496. --- a/src/print.c
  3497. +++ b/src/print.c
  3498. @@ -14,6 +14,7 @@
  3499. #include "lobject.h"
  3500. #include "lopcodes.h"
  3501. #include "lundump.h"
  3502. +#include "lnum.h"
  3503. #define PrintFunction luaU_print
  3504. @@ -59,8 +60,16 @@ static void PrintConstant(const Proto* f
  3505. case LUA_TBOOLEAN:
  3506. printf(bvalue(o) ? "true" : "false");
  3507. break;
  3508. + case LUA_TINT:
  3509. + printf(LUA_INTEGER_FMT,ivalue(o));
  3510. + break;
  3511. case LUA_TNUMBER:
  3512. - printf(LUA_NUMBER_FMT,nvalue(o));
  3513. +#ifdef LNUM_COMPLEX
  3514. + // TBD: Do we get complex values here?
  3515. + { lua_Number b= nvalue_img_fast(o);
  3516. + printf( LUA_NUMBER_FMT "%s" LUA_NUMBER_FMT "i", nvalue_fast(o), b>=0 ? "+":"", b ); }
  3517. +#endif
  3518. + printf(LUA_NUMBER_FMT,nvalue_fast(o));
  3519. break;
  3520. case LUA_TSTRING:
  3521. PrintString(rawtsvalue(o));