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

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