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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737
  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. -
  925. +#ifdef LNUM_FLOAT
  926. +# define PI (3.14159265358979323846F)
  927. +#elif defined(M_PI)
  928. +# define PI M_PI
  929. +#else
  930. +# define PI (3.14159265358979323846264338327950288)
  931. +#endif
  932. +#define RADIANS_PER_DEGREE (PI/180)
  933. +
  934. +#undef HUGE
  935. +#ifdef LNUM_FLOAT
  936. +# define HUGE HUGE_VALF
  937. +#elif defined(LNUM_LDOUBLE)
  938. +# define HUGE HUGE_VALL
  939. +#else
  940. +# define HUGE HUGE_VAL
  941. +#endif
  942. static int math_abs (lua_State *L) {
  943. - lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
  944. +#ifdef LNUM_COMPLEX
  945. + lua_pushnumber(L, _LF(cabs) (luaL_checkcomplex(L,1)));
  946. +#else
  947. + lua_pushnumber(L, _LF(fabs) (luaL_checknumber(L, 1)));
  948. +#endif
  949. return 1;
  950. }
  951. static int math_sin (lua_State *L) {
  952. - lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
  953. +#ifdef LNUM_COMPLEX
  954. + lua_pushcomplex(L, _LF(csin) (luaL_checkcomplex(L,1)));
  955. +#else
  956. + lua_pushnumber(L, _LF(sin) (luaL_checknumber(L, 1)));
  957. +#endif
  958. return 1;
  959. }
  960. static int math_sinh (lua_State *L) {
  961. - lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
  962. +#ifdef LNUM_COMPLEX
  963. + lua_pushcomplex(L, _LF(csinh) (luaL_checkcomplex(L,1)));
  964. +#else
  965. + lua_pushnumber(L, _LF(sinh) (luaL_checknumber(L, 1)));
  966. +#endif
  967. return 1;
  968. }
  969. static int math_cos (lua_State *L) {
  970. - lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
  971. +#ifdef LNUM_COMPLEX
  972. + lua_pushcomplex(L, _LF(ccos) (luaL_checkcomplex(L,1)));
  973. +#else
  974. + lua_pushnumber(L, _LF(cos) (luaL_checknumber(L, 1)));
  975. +#endif
  976. return 1;
  977. }
  978. static int math_cosh (lua_State *L) {
  979. - lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
  980. +#ifdef LNUM_COMPLEX
  981. + lua_pushcomplex(L, _LF(ccosh) (luaL_checkcomplex(L,1)));
  982. +#else
  983. + lua_pushnumber(L, _LF(cosh) (luaL_checknumber(L, 1)));
  984. +#endif
  985. return 1;
  986. }
  987. static int math_tan (lua_State *L) {
  988. - lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
  989. +#ifdef LNUM_COMPLEX
  990. + lua_pushcomplex(L, _LF(ctan) (luaL_checkcomplex(L,1)));
  991. +#else
  992. + lua_pushnumber(L, _LF(tan) (luaL_checknumber(L, 1)));
  993. +#endif
  994. return 1;
  995. }
  996. static int math_tanh (lua_State *L) {
  997. - lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
  998. +#ifdef LNUM_COMPLEX
  999. + lua_pushcomplex(L, _LF(ctanh) (luaL_checkcomplex(L,1)));
  1000. +#else
  1001. + lua_pushnumber(L, _LF(tanh) (luaL_checknumber(L, 1)));
  1002. +#endif
  1003. return 1;
  1004. }
  1005. static int math_asin (lua_State *L) {
  1006. - lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
  1007. +#ifdef LNUM_COMPLEX
  1008. + lua_pushcomplex(L, _LF(casin) (luaL_checkcomplex(L,1)));
  1009. +#else
  1010. + lua_pushnumber(L, _LF(asin) (luaL_checknumber(L, 1)));
  1011. +#endif
  1012. return 1;
  1013. }
  1014. static int math_acos (lua_State *L) {
  1015. - lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
  1016. +#ifdef LNUM_COMPLEX
  1017. + lua_pushcomplex(L, _LF(cacos) (luaL_checkcomplex(L,1)));
  1018. +#else
  1019. + lua_pushnumber(L, _LF(acos) (luaL_checknumber(L, 1)));
  1020. +#endif
  1021. return 1;
  1022. }
  1023. static int math_atan (lua_State *L) {
  1024. - lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
  1025. +#ifdef LNUM_COMPLEX
  1026. + lua_pushcomplex(L, _LF(catan) (luaL_checkcomplex(L,1)));
  1027. +#else
  1028. + lua_pushnumber(L, _LF(atan) (luaL_checknumber(L, 1)));
  1029. +#endif
  1030. return 1;
  1031. }
  1032. static int math_atan2 (lua_State *L) {
  1033. - lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  1034. + /* scalars only */
  1035. + lua_pushnumber(L, _LF(atan2) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  1036. return 1;
  1037. }
  1038. static int math_ceil (lua_State *L) {
  1039. - lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
  1040. +#ifdef LNUM_COMPLEX
  1041. + lua_Complex v= luaL_checkcomplex(L, 1);
  1042. + lua_pushcomplex(L, _LF(ceil) (_LF(creal)(v)) + _LF(ceil) (_LF(cimag)(v))*I);
  1043. +#else
  1044. + lua_pushnumber(L, _LF(ceil) (luaL_checknumber(L, 1)));
  1045. +#endif
  1046. return 1;
  1047. }
  1048. static int math_floor (lua_State *L) {
  1049. - lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
  1050. +#ifdef LNUM_COMPLEX
  1051. + lua_Complex v= luaL_checkcomplex(L, 1);
  1052. + lua_pushcomplex(L, _LF(floor) (_LF(creal)(v)) + _LF(floor) (_LF(cimag)(v))*I);
  1053. +#else
  1054. + lua_pushnumber(L, _LF(floor) (luaL_checknumber(L, 1)));
  1055. +#endif
  1056. return 1;
  1057. }
  1058. -static int math_fmod (lua_State *L) {
  1059. - lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  1060. +static int math_fmod (lua_State *L) {
  1061. + /* scalars only */
  1062. + lua_pushnumber(L, _LF(fmod) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  1063. return 1;
  1064. }
  1065. static int math_modf (lua_State *L) {
  1066. - double ip;
  1067. - double fp = modf(luaL_checknumber(L, 1), &ip);
  1068. + /* scalars only */
  1069. + lua_Number ip;
  1070. + lua_Number fp = _LF(modf) (luaL_checknumber(L, 1), &ip);
  1071. lua_pushnumber(L, ip);
  1072. lua_pushnumber(L, fp);
  1073. return 2;
  1074. }
  1075. static int math_sqrt (lua_State *L) {
  1076. - lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
  1077. +#ifdef LNUM_COMPLEX
  1078. + lua_pushcomplex(L, _LF(csqrt) (luaL_checkcomplex(L,1)));
  1079. +#else
  1080. + lua_pushnumber(L, _LF(sqrt) (luaL_checknumber(L, 1)));
  1081. +#endif
  1082. return 1;
  1083. }
  1084. static int math_pow (lua_State *L) {
  1085. - lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  1086. +#ifdef LNUM_COMPLEX
  1087. + /* C99 'cpow' gives somewhat inaccurate results (i.e. (-1)^2 = -1+1.2246467991474e-16i).
  1088. + * 'luai_vectpow' smoothens such, reusing it is the reason we need to #include "lnum.h".
  1089. + */
  1090. + lua_pushcomplex(L, luai_vectpow(luaL_checkcomplex(L,1), luaL_checkcomplex(L,2)));
  1091. +#else
  1092. + lua_pushnumber(L, _LF(pow) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  1093. +#endif
  1094. return 1;
  1095. }
  1096. static int math_log (lua_State *L) {
  1097. - lua_pushnumber(L, log(luaL_checknumber(L, 1)));
  1098. +#ifdef LNUM_COMPLEX
  1099. + lua_pushcomplex(L, _LF(clog) (luaL_checkcomplex(L,1)));
  1100. +#else
  1101. + lua_pushnumber(L, _LF(log) (luaL_checknumber(L, 1)));
  1102. +#endif
  1103. return 1;
  1104. }
  1105. static int math_log10 (lua_State *L) {
  1106. - lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
  1107. +#ifdef LNUM_COMPLEX
  1108. + /* Not in standard <complex.h> , but easy to calculate: log_a(x) = log_b(x) / log_b(a)
  1109. + */
  1110. + lua_pushcomplex(L, _LF(clog) (luaL_checkcomplex(L,1)) / _LF(log) (10));
  1111. +#else
  1112. + lua_pushnumber(L, _LF(log10) (luaL_checknumber(L, 1)));
  1113. +#endif
  1114. return 1;
  1115. }
  1116. static int math_exp (lua_State *L) {
  1117. - lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
  1118. +#ifdef LNUM_COMPLEX
  1119. + lua_pushcomplex(L, _LF(cexp) (luaL_checkcomplex(L,1)));
  1120. +#else
  1121. + lua_pushnumber(L, _LF(exp) (luaL_checknumber(L, 1)));
  1122. +#endif
  1123. return 1;
  1124. }
  1125. @@ -138,19 +234,20 @@ static int math_rad (lua_State *L) {
  1126. static int math_frexp (lua_State *L) {
  1127. int e;
  1128. - lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
  1129. + lua_pushnumber(L, _LF(frexp) (luaL_checknumber(L, 1), &e));
  1130. lua_pushinteger(L, e);
  1131. return 2;
  1132. }
  1133. static int math_ldexp (lua_State *L) {
  1134. - lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
  1135. + lua_pushnumber(L, _LF(ldexp) (luaL_checknumber(L, 1), luaL_checkint(L, 2)));
  1136. return 1;
  1137. }
  1138. static int math_min (lua_State *L) {
  1139. + /* scalars only */
  1140. int n = lua_gettop(L); /* number of arguments */
  1141. lua_Number dmin = luaL_checknumber(L, 1);
  1142. int i;
  1143. @@ -165,6 +262,7 @@ static int math_min (lua_State *L) {
  1144. static int math_max (lua_State *L) {
  1145. + /* scalars only */
  1146. int n = lua_gettop(L); /* number of arguments */
  1147. lua_Number dmax = luaL_checknumber(L, 1);
  1148. int i;
  1149. @@ -182,25 +280,20 @@ static int math_random (lua_State *L) {
  1150. /* the `%' avoids the (rare) case of r==1, and is needed also because on
  1151. some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
  1152. lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
  1153. - switch (lua_gettop(L)) { /* check number of arguments */
  1154. - case 0: { /* no arguments */
  1155. - lua_pushnumber(L, r); /* Number between 0 and 1 */
  1156. - break;
  1157. - }
  1158. - case 1: { /* only upper limit */
  1159. - int u = luaL_checkint(L, 1);
  1160. - luaL_argcheck(L, 1<=u, 1, "interval is empty");
  1161. - lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */
  1162. - break;
  1163. - }
  1164. - case 2: { /* lower and upper limits */
  1165. - int l = luaL_checkint(L, 1);
  1166. - int u = luaL_checkint(L, 2);
  1167. - luaL_argcheck(L, l<=u, 2, "interval is empty");
  1168. - lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */
  1169. - break;
  1170. - }
  1171. - default: return luaL_error(L, "wrong number of arguments");
  1172. + int n= lua_gettop(L); /* number of arguments */
  1173. + if (n==0) { /* no arguments: range [0,1) */
  1174. + lua_pushnumber(L, r);
  1175. + } else if (n<=2) { /* int range [1,u] or [l,u] */
  1176. + int l= n==1 ? 1 : luaL_checkint(L, 1);
  1177. + int u = luaL_checkint(L, n);
  1178. + int tmp;
  1179. + lua_Number d;
  1180. + luaL_argcheck(L, l<=u, n, "interval is empty");
  1181. + d= _LF(floor)(r*(u-l+1));
  1182. + lua_number2int(tmp,d);
  1183. + lua_pushinteger(L, l+tmp);
  1184. + } else {
  1185. + return luaL_error(L, "wrong number of arguments");
  1186. }
  1187. return 1;
  1188. }
  1189. @@ -211,6 +304,66 @@ static int math_randomseed (lua_State *L
  1190. return 0;
  1191. }
  1192. +/*
  1193. +* Lua 5.1 does not have acosh, asinh, atanh for scalars (not ANSI C)
  1194. +*/
  1195. +#if __STDC_VERSION__ >= 199901L
  1196. +static int math_acosh (lua_State *L) {
  1197. +# ifdef LNUM_COMPLEX
  1198. + lua_pushcomplex(L, _LF(cacosh) (luaL_checkcomplex(L,1)));
  1199. +# else
  1200. + lua_pushnumber(L, _LF(acosh) (luaL_checknumber(L,1)));
  1201. +# endif
  1202. + return 1;
  1203. +}
  1204. +static int math_asinh (lua_State *L) {
  1205. +# ifdef LNUM_COMPLEX
  1206. + lua_pushcomplex(L, _LF(casinh) (luaL_checkcomplex(L,1)));
  1207. +# else
  1208. + lua_pushnumber(L, _LF(asinh) (luaL_checknumber(L,1)));
  1209. +# endif
  1210. + return 1;
  1211. +}
  1212. +static int math_atanh (lua_State *L) {
  1213. +# ifdef LNUM_COMPLEX
  1214. + lua_pushcomplex(L, _LF(catanh) (luaL_checkcomplex(L,1)));
  1215. +# else
  1216. + lua_pushnumber(L, _LF(atanh) (luaL_checknumber(L,1)));
  1217. +# endif
  1218. + return 1;
  1219. +}
  1220. +#endif
  1221. +
  1222. +/*
  1223. + * C99 complex functions, not covered above.
  1224. +*/
  1225. +#ifdef LNUM_COMPLEX
  1226. +static int math_arg (lua_State *L) {
  1227. + lua_pushnumber(L, _LF(carg) (luaL_checkcomplex(L,1)));
  1228. + return 1;
  1229. +}
  1230. +
  1231. +static int math_imag (lua_State *L) {
  1232. + lua_pushnumber(L, _LF(cimag) (luaL_checkcomplex(L,1)));
  1233. + return 1;
  1234. +}
  1235. +
  1236. +static int math_real (lua_State *L) {
  1237. + lua_pushnumber(L, _LF(creal) (luaL_checkcomplex(L,1)));
  1238. + return 1;
  1239. +}
  1240. +
  1241. +static int math_conj (lua_State *L) {
  1242. + lua_pushcomplex(L, _LF(conj) (luaL_checkcomplex(L,1)));
  1243. + return 1;
  1244. +}
  1245. +
  1246. +static int math_proj (lua_State *L) {
  1247. + lua_pushcomplex(L, _LF(cproj) (luaL_checkcomplex(L,1)));
  1248. + return 1;
  1249. +}
  1250. +#endif
  1251. +
  1252. static const luaL_Reg mathlib[] = {
  1253. {"abs", math_abs},
  1254. @@ -241,6 +394,18 @@ static const luaL_Reg mathlib[] = {
  1255. {"sqrt", math_sqrt},
  1256. {"tanh", math_tanh},
  1257. {"tan", math_tan},
  1258. +#if __STDC_VERSION__ >= 199901L
  1259. + {"acosh", math_acosh},
  1260. + {"asinh", math_asinh},
  1261. + {"atanh", math_atanh},
  1262. +#endif
  1263. +#ifdef LNUM_COMPLEX
  1264. + {"arg", math_arg},
  1265. + {"imag", math_imag},
  1266. + {"real", math_real},
  1267. + {"conj", math_conj},
  1268. + {"proj", math_proj},
  1269. +#endif
  1270. {NULL, NULL}
  1271. };
  1272. @@ -252,8 +417,10 @@ LUALIB_API int luaopen_math (lua_State *
  1273. luaL_register(L, LUA_MATHLIBNAME, mathlib);
  1274. lua_pushnumber(L, PI);
  1275. lua_setfield(L, -2, "pi");
  1276. - lua_pushnumber(L, HUGE_VAL);
  1277. + lua_pushnumber(L, HUGE);
  1278. lua_setfield(L, -2, "huge");
  1279. + lua_pushinteger(L, LUA_INTEGER_MAX );
  1280. + lua_setfield(L, -2, "hugeint");
  1281. #if defined(LUA_COMPAT_MOD)
  1282. lua_getfield(L, -1, "fmod");
  1283. lua_setfield(L, -2, "mod");
  1284. --- /dev/null
  1285. +++ b/src/lnum.c
  1286. @@ -0,0 +1,312 @@
  1287. +/*
  1288. +** $Id: lnum.c,v ... $
  1289. +** Internal number model
  1290. +** See Copyright Notice in lua.h
  1291. +*/
  1292. +
  1293. +#include <stdlib.h>
  1294. +#include <math.h>
  1295. +#include <ctype.h>
  1296. +#include <string.h>
  1297. +#include <stdio.h>
  1298. +#include <errno.h>
  1299. +
  1300. +#define lnum_c
  1301. +#define LUA_CORE
  1302. +
  1303. +#include "lua.h"
  1304. +#include "llex.h"
  1305. +#include "lnum.h"
  1306. +
  1307. +/*
  1308. +** lua_real2str converts a (non-complex) number to a string.
  1309. +** lua_str2real converts a string to a (non-complex) number.
  1310. +*/
  1311. +#define lua_real2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
  1312. +
  1313. +/*
  1314. +* Note: Only 'strtod()' is part of ANSI C; others are C99 and
  1315. +* may need '--std=c99' compiler setting (at least on Ubuntu 7.10).
  1316. +*
  1317. +* Visual C++ 2008 Express does not have 'strtof()', nor 'strtold()'.
  1318. +* References to '_strtold()' exist but don't compile. It seems best
  1319. +* to leave Windows users with DOUBLE only (or compile with MinGW).
  1320. +*
  1321. +* In practise, using '(long double)strtod' is a risky thing, since
  1322. +* it will cause accuracy loss in reading in numbers, and such losses
  1323. +* will pile up in later processing. Get a real 'strtold()' or don't
  1324. +* use that mode at all.
  1325. +*/
  1326. +#ifdef LNUM_DOUBLE
  1327. +# define lua_str2real strtod
  1328. +#elif defined(LNUM_FLOAT)
  1329. +# define lua_str2real strtof
  1330. +#elif defined(LNUM_LDOUBLE)
  1331. +# define lua_str2real strtold
  1332. +#endif
  1333. +
  1334. +#define lua_integer2str(s,v) sprintf((s), LUA_INTEGER_FMT, (v))
  1335. +
  1336. +/* 's' is expected to be LUAI_MAXNUMBER2STR long (enough for any number)
  1337. +*/
  1338. +void luaO_num2buf( char *s, const TValue *o )
  1339. +{
  1340. + lua_Number n;
  1341. + lua_assert( ttisnumber(o) );
  1342. +
  1343. + /* Reason to handle integers differently is not only speed, but accuracy as
  1344. + * well. We want to make any integer tostring() without roundings, at all.
  1345. + */
  1346. + if (ttisint(o)) {
  1347. + lua_integer2str( s, ivalue(o) );
  1348. + return;
  1349. + }
  1350. + n= nvalue_fast(o);
  1351. + lua_real2str(s, n);
  1352. +
  1353. +#ifdef LNUM_COMPLEX
  1354. + lua_Number n2= nvalue_img_fast(o);
  1355. + if (n2!=0) { /* Postfix with +-Ni */
  1356. + int re0= (n == 0);
  1357. + char *s2= re0 ? s : strchr(s,'\0');
  1358. + if ((!re0) && (n2>0)) *s2++= '+';
  1359. + lua_real2str( s2, n2 );
  1360. + strcat(s2,"i");
  1361. + }
  1362. +#endif
  1363. +}
  1364. +
  1365. +/*
  1366. +* If a LUA_TNUMBER has integer value, give it.
  1367. +*/
  1368. +int /*bool*/ tt_integer_valued( const TValue *o, lua_Integer *ref ) {
  1369. + lua_Number d;
  1370. + lua_Integer i;
  1371. +
  1372. + lua_assert( ttype(o)==LUA_TNUMBER );
  1373. + lua_assert( ref );
  1374. +#ifdef LNUM_COMPLEX
  1375. + if (nvalue_img_fast(o)!=0) return 0;
  1376. +#endif
  1377. + d= nvalue_fast(o);
  1378. + lua_number2integer(i, d);
  1379. + if (cast_num(i) == d) {
  1380. + *ref= i; return 1;
  1381. + }
  1382. + return 0;
  1383. +}
  1384. +
  1385. +/*
  1386. + * Lua 5.1.3 (using 'strtod()') allows 0x+hex but not 0+octal. This is good,
  1387. + * and we should NOT use 'autobase' 0 with 'strtoul[l]()' for this reason.
  1388. + *
  1389. + * Lua 5.1.3 allows '0x...' numbers to overflow and lose precision; this is not
  1390. + * good. On Visual C++ 2008, 'strtod()' does not even take them in. Better to
  1391. + * require hex values to fit 'lua_Integer' or give an error that they don't?
  1392. + *
  1393. + * Full hex range (0 .. 0xff..ff) is stored as integers, not to lose any bits.
  1394. + * Numerical value of 0xff..ff will be -1, if used in calculations.
  1395. + *
  1396. + * Returns: TK_INT for a valid integer, '*endptr_ref' updated
  1397. + * TK_NUMBER for seemingly numeric, to be parsed as floating point
  1398. + * 0 for bad characters, not a number (or '0x' out of range)
  1399. + */
  1400. +static int luaO_str2i (const char *s, lua_Integer *res, char **endptr_ref) {
  1401. + char *endptr;
  1402. + /* 'v' gets ULONG_MAX on possible overflow (which is > LUA_INTEGER_MAX);
  1403. + * we don't have to check 'errno' here.
  1404. + */
  1405. + unsigned LUA_INTEGER v= lua_str2ul(s, &endptr, 10);
  1406. + if (endptr == s) return 0; /* nothing numeric */
  1407. + if (v==0 && *endptr=='x') {
  1408. + errno= 0; /* needs to be set, 'strtoul[l]' does not clear it */
  1409. + v= lua_str2ul(endptr+1, &endptr, 16); /* retry as hex, unsigned range */
  1410. + if (errno==ERANGE) { /* clamped to 0xff..ff */
  1411. +#if (defined(LNUM_INT32) && !defined(LNUM_FLOAT)) || defined(LNUM_LDOUBLE)
  1412. + return TK_NUMBER; /* Allow to be read as floating point (has more integer range) */
  1413. +#else
  1414. + return 0; /* Reject the number */
  1415. +#endif
  1416. + }
  1417. + } else if ((v > LUA_INTEGER_MAX) || (*endptr && (!isspace(*endptr)))) {
  1418. + return TK_NUMBER; /* not in signed range, or has '.', 'e' etc. trailing */
  1419. + }
  1420. + *res= (lua_Integer)v;
  1421. + *endptr_ref= endptr;
  1422. + return TK_INT;
  1423. +}
  1424. +
  1425. +/* 0 / TK_NUMBER / TK_INT (/ TK_NUMBER2) */
  1426. +int luaO_str2d (const char *s, lua_Number *res_n, lua_Integer *res_i) {
  1427. + char *endptr;
  1428. + int ret= TK_NUMBER;
  1429. + /* Check integers first, if caller is allowing.
  1430. + * If 'res2'==NULL, they're only looking for floating point.
  1431. + */
  1432. + if (res_i) {
  1433. + ret= luaO_str2i(s,res_i,&endptr);
  1434. + if (ret==0) return 0;
  1435. + }
  1436. + if (ret==TK_NUMBER) {
  1437. + lua_assert(res_n);
  1438. + /* Note: Visual C++ 2008 Express 'strtod()' does not read in "0x..."
  1439. + * numbers; it will read '0' and spit 'x' as endptr.
  1440. + * This means hex constants not fitting in 'lua_Integer' won't
  1441. + * be read in at all. What to do?
  1442. + */
  1443. + *res_n = lua_str2real(s, &endptr);
  1444. + if (endptr == s) return 0; /* conversion failed */
  1445. + /* Visual C++ 2008 'strtod()' does not allow "0x..." input. */
  1446. +#if defined(_MSC_VER) && !defined(LNUM_FLOAT) && !defined(LNUM_INT64)
  1447. + if (*res_n==0 && *endptr=='x') {
  1448. + /* Hex constant too big for 'lua_Integer' but that could fit in 'lua_Number'
  1449. + * integer bits
  1450. + */
  1451. + unsigned __int64 v= _strtoui64( s, &endptr, 16 );
  1452. + /* We just let > 64 bit values be clamped to _UI64_MAX (MSDN does not say 'errno'==ERANGE would be set) */
  1453. + *res_n= cast_num(v);
  1454. + if (*res_n != v) return 0; /* Would have lost accuracy */
  1455. + }
  1456. +#endif
  1457. +#ifdef LNUM_COMPLEX
  1458. + if (*endptr == 'i') { endptr++; ret= TK_NUMBER2; }
  1459. +#endif
  1460. + }
  1461. + if (*endptr) {
  1462. + while (isspace(cast(unsigned char, *endptr))) endptr++;
  1463. + if (*endptr) return 0; /* invalid trail */
  1464. + }
  1465. + return ret;
  1466. +}
  1467. +
  1468. +
  1469. +/* Functions for finding out, when integer operations remain in range
  1470. + * (and doing them).
  1471. + */
  1472. +int try_addint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
  1473. + lua_Integer v= ib+ic; /* may overflow */
  1474. + if (ib>0 && ic>0) { if (v < 0) return 0; /*overflow, use floats*/ }
  1475. + else if (ib<0 && ic<0) { if (v >= 0) return 0; }
  1476. + *r= v;
  1477. + return 1;
  1478. +}
  1479. +
  1480. +int try_subint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
  1481. + lua_Integer v= ib-ic; /* may overflow */
  1482. + if (ib>=0 && ic<0) { if (v < 0) return 0; /*overflow, use floats*/ }
  1483. + else if (ib<0 && ic>0) { if (v >= 0) return 0; }
  1484. + *r= v;
  1485. + return 1;
  1486. +}
  1487. +
  1488. +int try_mulint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
  1489. + if (ib!=LUA_INTEGER_MIN && ic!=LUA_INTEGER_MIN) {
  1490. + lua_Integer b= luai_abs(ib), c= luai_abs(ic);
  1491. + if ( (ib==0) || (LUA_INTEGER_MAX/b >= c) ) {
  1492. + *r= ib*ic; /* no overflow */
  1493. + return 1;
  1494. + }
  1495. + } else if (ib==0 || ic==0) {
  1496. + *r= 0; return 1;
  1497. + }
  1498. +
  1499. + /* Result can be LUA_INTEGER_MIN; if it is, calculating it using floating
  1500. + * point will not cause accuracy loss.
  1501. + */
  1502. + if ( luai_nummul( cast_num(ib), cast_num(ic) ) == LUA_INTEGER_MIN ) {
  1503. + *r= LUA_INTEGER_MIN;
  1504. + return 1;
  1505. + }
  1506. + return 0;
  1507. +}
  1508. +
  1509. +int try_divint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
  1510. + /* N/0: leave to float side, to give an error
  1511. + */
  1512. + if (ic==0) return 0;
  1513. +
  1514. + /* N/LUA_INTEGER_MIN: always non-integer results, or 0 or +1
  1515. + */
  1516. + if (ic==LUA_INTEGER_MIN) {
  1517. + if (ib==LUA_INTEGER_MIN) { *r=1; return 1; }
  1518. + if (ib==0) { *r=0; return 1; }
  1519. +
  1520. + /* LUA_INTEGER_MIN (-2^31|63)/N: calculate using float side (either the division
  1521. + * causes non-integer results, or there is no accuracy loss in int->fp->int
  1522. + * conversions (N=2,4,8,..,256 and N=2^30,2^29,..2^23).
  1523. + */
  1524. + } else if (ib==LUA_INTEGER_MIN) {
  1525. + lua_Number d= luai_numdiv( cast_num(LUA_INTEGER_MIN), cast_num(ic) );
  1526. + lua_Integer i; lua_number2integer(i,d);
  1527. + if (cast_num(i)==d) { *r= i; return 1; }
  1528. +
  1529. + } else {
  1530. + /* Note: We _can_ use ANSI C mod here, even on negative values, since
  1531. + * we only test for == 0 (the sign would be implementation dependent).
  1532. + */
  1533. + if (ib%ic == 0) { *r= ib/ic; return 1; }
  1534. + }
  1535. +
  1536. + return 0;
  1537. +}
  1538. +
  1539. +int try_modint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
  1540. + if (ic!=0) {
  1541. + /* ANSI C can be trusted when b%c==0, or when values are non-negative.
  1542. + * b - (floor(b/c) * c)
  1543. + * -->
  1544. + * + +: b - (b/c) * c (b % c can be used)
  1545. + * - -: b - (b/c) * c (b % c could work, but not defined by ANSI C)
  1546. + * 0 -: b - (b/c) * c (=0, b % c could work, but not defined by ANSI C)
  1547. + * - +: b - (b/c-1) * c (when b!=-c)
  1548. + * + -: b - (b/c-1) * c (when b!=-c)
  1549. + *
  1550. + * o MIN%MIN ends up 0, via overflow in calcs but that does not matter.
  1551. + * o MIN%MAX ends up MAX-1 (and other such numbers), also after overflow,
  1552. + * but that does not matter, results do.
  1553. + */
  1554. + lua_Integer v= ib % ic;
  1555. + if ( v!=0 && (ib<0 || ic<0) ) {
  1556. + v= ib - ((ib/ic) - ((ib<=0 && ic<0) ? 0:1)) * ic;
  1557. + }
  1558. + /* Result should always have same sign as 2nd argument. (PIL2) */
  1559. + lua_assert( (v<0) ? (ic<0) : (v>0) ? (ic>0) : 1 );
  1560. + *r= v;
  1561. + return 1;
  1562. + }
  1563. + return 0; /* let float side return NaN */
  1564. +}
  1565. +
  1566. +int try_powint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
  1567. +
  1568. + /* In FLOAT/INT32 or FLOAT|DOUBLE/INT64 modes, calculating integer powers
  1569. + * via FP realm may lose accuracy (i.e. 7^11 = 1977326743, which fits int32
  1570. + * but not 23-bit float mantissa).
  1571. + *
  1572. + * The current solution is dumb, but it works and uses little code. Use of
  1573. + * integer powers is not anticipated to be very frequent (apart from 2^x,
  1574. + * which is separately optimized).
  1575. + */
  1576. + if (ib==0) *r=0;
  1577. + else if (ic<0) return 0; /* FP realm */
  1578. + else if (ib==2 && ic < (int)sizeof(lua_Integer)*8-1) *r= ((lua_Integer)1)<<ic; /* 1,2,4,...2^30 | 2^62 optimization */
  1579. + else if (ic==0) *r=1;
  1580. + else if (luai_abs(ib)==1) *r= (ic%2) ? ib:1;
  1581. + else {
  1582. + lua_Integer x= ib;
  1583. + while( --ic ) {
  1584. + if (!try_mulint( &x, x, ib ))
  1585. + return 0; /* FP realm */
  1586. + }
  1587. + *r= x;
  1588. + }
  1589. + return 1;
  1590. +}
  1591. +
  1592. +int try_unmint( lua_Integer *r, lua_Integer ib ) {
  1593. + /* Negating LUA_INTEGER_MIN leaves the range. */
  1594. + if ( ib != LUA_INTEGER_MIN )
  1595. + { *r= -ib; return 1; }
  1596. + return 0;
  1597. +}
  1598. +
  1599. --- /dev/null
  1600. +++ b/src/lnum.h
  1601. @@ -0,0 +1,116 @@
  1602. +/*
  1603. +** $Id: lnum.h,v ... $
  1604. +** Internal Number model
  1605. +** See Copyright Notice in lua.h
  1606. +*/
  1607. +
  1608. +#ifndef lnum_h
  1609. +#define lnum_h
  1610. +
  1611. +#include <math.h>
  1612. +
  1613. +#include "lobject.h"
  1614. +
  1615. +/*
  1616. +** The luai_num* macros define the primitive operations over 'lua_Number's
  1617. +** (not 'lua_Integer's, not 'lua_Complex').
  1618. +*/
  1619. +#define luai_numadd(a,b) ((a)+(b))
  1620. +#define luai_numsub(a,b) ((a)-(b))
  1621. +#define luai_nummul(a,b) ((a)*(b))
  1622. +#define luai_numdiv(a,b) ((a)/(b))
  1623. +#define luai_nummod(a,b) ((a) - _LF(floor)((a)/(b))*(b))
  1624. +#define luai_numpow(a,b) (_LF(pow)(a,b))
  1625. +#define luai_numunm(a) (-(a))
  1626. +#define luai_numeq(a,b) ((a)==(b))
  1627. +#define luai_numlt(a,b) ((a)<(b))
  1628. +#define luai_numle(a,b) ((a)<=(b))
  1629. +#define luai_numisnan(a) (!luai_numeq((a), (a)))
  1630. +
  1631. +int try_addint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
  1632. +int try_subint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
  1633. +int try_mulint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
  1634. +int try_divint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
  1635. +int try_modint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
  1636. +int try_powint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
  1637. +int try_unmint( lua_Integer *r, lua_Integer ib );
  1638. +
  1639. +#ifdef LNUM_COMPLEX
  1640. + static inline lua_Complex luai_vectunm( lua_Complex a ) { return -a; }
  1641. + static inline lua_Complex luai_vectadd( lua_Complex a, lua_Complex b ) { return a+b; }
  1642. + static inline lua_Complex luai_vectsub( lua_Complex a, lua_Complex b ) { return a-b; }
  1643. + static inline lua_Complex luai_vectmul( lua_Complex a, lua_Complex b ) { return a*b; }
  1644. + static inline lua_Complex luai_vectdiv( lua_Complex a, lua_Complex b ) { return a/b; }
  1645. +
  1646. +/*
  1647. + * C99 does not provide modulus for complex numbers. It most likely is not
  1648. + * meaningful at all.
  1649. + */
  1650. +
  1651. +/*
  1652. + * Complex power
  1653. + *
  1654. + * C99 'cpow' gives inaccurate results for many common cases s.a. (1i)^2 ->
  1655. + * -1+1.2246467991474e-16i (OS X 10.4, gcc 4.0.1 build 5367)
  1656. + *
  1657. + * [(a+bi)^(c+di)] = (r^c) * exp(-d*t) * cos(c*t + d*ln(r)) +
  1658. + * = (r^c) * exp(-d*t) * sin(c*t + d*ln(r)) *i
  1659. + * r = sqrt(a^2+b^2), t = arctan( b/a )
  1660. + *
  1661. + * Reference: <http://home.att.net/~srschmitt/complexnumbers.html>
  1662. + * Could also be calculated using: x^y = exp(ln(x)*y)
  1663. + *
  1664. + * Note: Defined here (and not in .c) so 'lmathlib.c' can share the
  1665. + * implementation.
  1666. + */
  1667. + static inline
  1668. + lua_Complex luai_vectpow( lua_Complex a, lua_Complex b )
  1669. + {
  1670. +# if 1
  1671. + lua_Number ar= _LF(creal)(a), ai= _LF(cimag)(a);
  1672. + lua_Number br= _LF(creal)(b), bi= _LF(cimag)(b);
  1673. +
  1674. + if (ai==0 && bi==0) { /* a^c (real) */
  1675. + return luai_numpow( ar, br );
  1676. + }
  1677. +
  1678. + int br_int= (int)br;
  1679. +
  1680. + if ( ai!=0 && bi==0 && br_int==br && br_int!=0 && br_int!=INT_MIN ) {
  1681. + /* (a+bi)^N, N = { +-1,+-2, ... +-INT_MAX }
  1682. + */
  1683. + lua_Number k= luai_numpow( _LF(sqrt) (ar*ar + ai*ai), br );
  1684. + lua_Number cos_z, sin_z;
  1685. +
  1686. + /* Situation depends upon c (N) in the following manner:
  1687. + *
  1688. + * N%4==0 => 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. + * N%4==2 or N%4==-2 => cos(c*t)=-1, sin(c*t)=0
  1691. + * (N*sign(b))%4==-1 or (N*sign(b))%4==3 => cos(c*t)=0, sin(c*t)=-1
  1692. + */
  1693. + int br_int_abs = br_int<0 ? -br_int:br_int;
  1694. +
  1695. + switch( (br_int_abs%4) * (br_int<0 ? -1:1) * (ai<0 ? -1:1) ) {
  1696. + case 0: cos_z=1, sin_z=0; break;
  1697. + case 2: case -2: cos_z=-1, sin_z=0; break;
  1698. + case 1: case -3: cos_z=0, sin_z=1; break;
  1699. + case 3: case -1: cos_z=0, sin_z=-1; break;
  1700. + default: lua_assert(0); return 0;
  1701. + }
  1702. + return k*cos_z + (k*sin_z)*I;
  1703. + }
  1704. +# endif
  1705. + return _LF(cpow) ( a, b );
  1706. + }
  1707. +#endif
  1708. +
  1709. +LUAI_FUNC int luaO_str2d (const char *s, lua_Number *res1, lua_Integer *res2);
  1710. +LUAI_FUNC void luaO_num2buf( char *s, const TValue *o );
  1711. +
  1712. +LUAI_FUNC int /*bool*/ tt_integer_valued( const TValue *o, lua_Integer *ref );
  1713. +
  1714. +#define luai_normalize(o) \
  1715. +{ lua_Integer _i; if (tt_integer_valued(o,&_i)) setivalue(o,_i); }
  1716. +
  1717. +#endif
  1718. --- /dev/null
  1719. +++ b/src/lnum_config.h
  1720. @@ -0,0 +1,221 @@
  1721. +/*
  1722. +** $Id: lnum_config.h,v ... $
  1723. +** Internal Number model
  1724. +** See Copyright Notice in lua.h
  1725. +*/
  1726. +
  1727. +#ifndef lnum_config_h
  1728. +#define lnum_config_h
  1729. +
  1730. +/*
  1731. +** Default number modes
  1732. +*/
  1733. +#if (!defined LNUM_DOUBLE) && (!defined LNUM_FLOAT) && (!defined LNUM_LDOUBLE)
  1734. +# define LNUM_FLOAT
  1735. +#endif
  1736. +#if (!defined LNUM_INT16) && (!defined LNUM_INT32) && (!defined LNUM_INT64)
  1737. +# define LNUM_INT32
  1738. +#endif
  1739. +
  1740. +/*
  1741. +** Require C99 mode for COMPLEX, FLOAT and LDOUBLE (only DOUBLE is ANSI C).
  1742. +*/
  1743. +#if defined(LNUM_COMPLEX) && (__STDC_VERSION__ < 199901L)
  1744. +# error "Need C99 for complex (use '--std=c99' or similar)"
  1745. +#elif defined(LNUM_LDOUBLE) && (__STDC_VERSION__ < 199901L) && !defined(_MSC_VER)
  1746. +# error "Need C99 for 'long double' (use '--std=c99' or similar)"
  1747. +#elif defined(LNUM_FLOAT) && (__STDC_VERSION__ < 199901L)
  1748. +/* LNUM_FLOAT not supported on Windows */
  1749. +# error "Need C99 for 'float' (use '--std=c99' or similar)"
  1750. +#endif
  1751. +
  1752. +/*
  1753. +** Number mode identifier to accompany the version string.
  1754. +*/
  1755. +#ifdef LNUM_COMPLEX
  1756. +# define _LNUM1 "complex "
  1757. +#else
  1758. +# define _LNUM1 ""
  1759. +#endif
  1760. +#ifdef LNUM_DOUBLE
  1761. +# define _LNUM2 "double"
  1762. +#elif defined(LNUM_FLOAT)
  1763. +# define _LNUM2 "float"
  1764. +#elif defined(LNUM_LDOUBLE)
  1765. +# define _LNUM2 "ldouble"
  1766. +#endif
  1767. +#ifdef LNUM_INT32
  1768. +# define _LNUM3 "int32"
  1769. +#elif defined(LNUM_INT64)
  1770. +# define _LNUM3 "int64"
  1771. +#elif defined(LNUM_INT16)
  1772. +# define _LNUM3 "int16"
  1773. +#endif
  1774. +#define LUA_LNUM _LNUM1 _LNUM2 " " _LNUM3
  1775. +
  1776. +/*
  1777. +** LUA_NUMBER is the type of floating point number in Lua
  1778. +** LUA_NUMBER_SCAN is the format for reading numbers.
  1779. +** LUA_NUMBER_FMT is the format for writing numbers.
  1780. +*/
  1781. +#ifdef LNUM_FLOAT
  1782. +# define LUA_NUMBER float
  1783. +# define LUA_NUMBER_SCAN "%f"
  1784. +# define LUA_NUMBER_FMT "%g"
  1785. +#elif (defined LNUM_DOUBLE)
  1786. +# define LUA_NUMBER double
  1787. +# define LUA_NUMBER_SCAN "%lf"
  1788. +# define LUA_NUMBER_FMT "%.14g"
  1789. +#elif (defined LNUM_LDOUBLE)
  1790. +# define LUA_NUMBER long double
  1791. +# define LUA_NUMBER_SCAN "%Lg"
  1792. +# define LUA_NUMBER_FMT "%.20Lg"
  1793. +#endif
  1794. +
  1795. +
  1796. +/*
  1797. +** LUAI_MAXNUMBER2STR: size of a buffer fitting any number->string result.
  1798. +**
  1799. +** double: 24 (sign, x.xxxxxxxxxxxxxxe+nnnn, and \0)
  1800. +** int64: 21 (19 digits, sign, and \0)
  1801. +** long double: 43 for 128-bit (sign, x.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxe+nnnn, and \0)
  1802. +** 30 for 80-bit (sign, x.xxxxxxxxxxxxxxxxxxxxe+nnnn, and \0)
  1803. +*/
  1804. +#ifdef LNUM_LDOUBLE
  1805. +# define _LUAI_MN2S 44
  1806. +#else
  1807. +# define _LUAI_MN2S 24
  1808. +#endif
  1809. +
  1810. +#ifdef LNUM_COMPLEX
  1811. +# define LUAI_MAXNUMBER2STR (2*_LUAI_MN2S)
  1812. +#else
  1813. +# define LUAI_MAXNUMBER2STR _LUAI_MN2S
  1814. +#endif
  1815. +
  1816. +/*
  1817. +** LUA_INTEGER is the integer type used by lua_pushinteger/lua_tointeger/lua_isinteger.
  1818. +** LUA_INTEGER_SCAN is the format for reading integers
  1819. +** LUA_INTEGER_FMT is the format for writing integers
  1820. +**
  1821. +** Note: Visual C++ 2005 does not have 'strtoull()', use '_strtoui64()' instead.
  1822. +*/
  1823. +#ifdef LNUM_INT32
  1824. +# if LUAI_BITSINT > 16
  1825. +# define LUA_INTEGER int
  1826. +# define LUA_INTEGER_SCAN "%d"
  1827. +# define LUA_INTEGER_FMT "%d"
  1828. +# else
  1829. +/* Note: 'LUA_INTEGER' being 'ptrdiff_t' (as in Lua 5.1) causes problems with
  1830. + * 'printf()' operations. Also 'unsigned ptrdiff_t' is invalid.
  1831. + */
  1832. +# define LUA_INTEGER long
  1833. +# define LUA_INTEGER_SCAN "%ld"
  1834. +# define LUA_INTEGER_FMT "%ld"
  1835. +# endif
  1836. +# define LUA_INTEGER_MAX 0x7FFFFFFF /* 2^31-1 */
  1837. +/* */
  1838. +#elif defined(LNUM_INT64)
  1839. +# define LUA_INTEGER long long
  1840. +# ifdef _MSC_VER
  1841. +# define lua_str2ul _strtoui64
  1842. +# else
  1843. +# define lua_str2ul strtoull
  1844. +# endif
  1845. +# define LUA_INTEGER_SCAN "%lld"
  1846. +# define LUA_INTEGER_FMT "%lld"
  1847. +# define LUA_INTEGER_MAX 0x7fffffffffffffffLL /* 2^63-1 */
  1848. +# define LUA_INTEGER_MIN (-LUA_INTEGER_MAX - 1LL) /* -2^63 */
  1849. +/* */
  1850. +#elif defined(LNUM_INT16)
  1851. +# if LUAI_BITSINT > 16
  1852. +# define LUA_INTEGER short
  1853. +# define LUA_INTEGER_SCAN "%hd"
  1854. +# define LUA_INTEGER_FMT "%hd"
  1855. +# else
  1856. +# define LUA_INTEGER int
  1857. +# define LUA_INTEGER_SCAN "%d"
  1858. +# define LUA_INTEGER_FMT "%d"
  1859. +# endif
  1860. +# define LUA_INTEGER_MAX 0x7FFF /* 2^16-1 */
  1861. +#endif
  1862. +
  1863. +#ifndef lua_str2ul
  1864. +# define lua_str2ul (unsigned LUA_INTEGER)strtoul
  1865. +#endif
  1866. +#ifndef LUA_INTEGER_MIN
  1867. +# define LUA_INTEGER_MIN (-LUA_INTEGER_MAX -1) /* -2^16|32 */
  1868. +#endif
  1869. +
  1870. +/*
  1871. +@@ lua_number2int is a macro to convert lua_Number to int.
  1872. +@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
  1873. +** CHANGE them if you know a faster way to convert a lua_Number to
  1874. +** int (with any rounding method and without throwing errors) in your
  1875. +** system. In Pentium machines, a naive typecast from double to int
  1876. +** in C is extremely slow, so any alternative is worth trying.
  1877. +*/
  1878. +
  1879. +/* On a Pentium, resort to a trick */
  1880. +#if defined(LNUM_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
  1881. + (defined(__i386) || defined (_M_IX86) || defined(__i386__))
  1882. +
  1883. +/* On a Microsoft compiler, use assembler */
  1884. +# if defined(_MSC_VER)
  1885. +# define lua_number2int(i,d) __asm fld d __asm fistp i
  1886. +# else
  1887. +
  1888. +/* the next trick should work on any Pentium, but sometimes clashes
  1889. + with a DirectX idiosyncrasy */
  1890. +union luai_Cast { double l_d; long l_l; };
  1891. +# define lua_number2int(i,d) \
  1892. + { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
  1893. +# endif
  1894. +
  1895. +# ifndef LNUM_INT64
  1896. +# define lua_number2integer lua_number2int
  1897. +# endif
  1898. +
  1899. +/* this option always works, but may be slow */
  1900. +#else
  1901. +# define lua_number2int(i,d) ((i)=(int)(d))
  1902. +#endif
  1903. +
  1904. +/* Note: Some compilers (OS X gcc 4.0?) may choke on double->long long conversion
  1905. + * since it can lose precision. Others do require 'long long' there.
  1906. + */
  1907. +#ifndef lua_number2integer
  1908. +# define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
  1909. +#endif
  1910. +
  1911. +/*
  1912. +** 'luai_abs()' to give absolute value of 'lua_Integer'
  1913. +*/
  1914. +#ifdef LNUM_INT32
  1915. +# define luai_abs abs
  1916. +#elif defined(LNUM_INT64) && (__STDC_VERSION__ >= 199901L)
  1917. +# define luai_abs llabs
  1918. +#else
  1919. +# define luai_abs(v) ((v) >= 0 ? (v) : -(v))
  1920. +#endif
  1921. +
  1922. +/*
  1923. +** LUAI_UACNUMBER is the result of an 'usual argument conversion' over a number.
  1924. +** LUAI_UACINTEGER the same, over an integer.
  1925. +*/
  1926. +#define LUAI_UACNUMBER double
  1927. +#define LUAI_UACINTEGER long
  1928. +
  1929. +/* ANSI C only has math funcs for 'double. C99 required for float and long double
  1930. + * variants.
  1931. + */
  1932. +#ifdef LNUM_DOUBLE
  1933. +# define _LF(name) name
  1934. +#elif defined(LNUM_FLOAT)
  1935. +# define _LF(name) name ## f
  1936. +#elif defined(LNUM_LDOUBLE)
  1937. +# define _LF(name) name ## l
  1938. +#endif
  1939. +
  1940. +#endif
  1941. +
  1942. --- a/src/lobject.c
  1943. +++ b/src/lobject.c
  1944. @@ -21,7 +21,8 @@
  1945. #include "lstate.h"
  1946. #include "lstring.h"
  1947. #include "lvm.h"
  1948. -
  1949. +#include "llex.h"
  1950. +#include "lnum.h"
  1951. const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};
  1952. @@ -70,12 +71,31 @@ int luaO_log2 (unsigned int x) {
  1953. int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
  1954. - if (ttype(t1) != ttype(t2)) return 0;
  1955. + if (!ttype_ext_same(t1,t2)) return 0;
  1956. else switch (ttype(t1)) {
  1957. case LUA_TNIL:
  1958. return 1;
  1959. + case LUA_TINT:
  1960. + if (ttype(t2)==LUA_TINT)
  1961. + return ivalue(t1) == ivalue(t2);
  1962. + else { /* t1:int, t2:num */
  1963. +#ifdef LNUM_COMPLEX
  1964. + if (nvalue_img_fast(t2) != 0) return 0;
  1965. +#endif
  1966. + /* Avoid doing accuracy losing cast, if possible. */
  1967. + lua_Integer tmp;
  1968. + if (tt_integer_valued(t2,&tmp))
  1969. + return ivalue(t1) == tmp;
  1970. + else
  1971. + return luai_numeq( cast_num(ivalue(t1)), nvalue_fast(t2) );
  1972. + }
  1973. case LUA_TNUMBER:
  1974. - return luai_numeq(nvalue(t1), nvalue(t2));
  1975. + if (ttype(t2)==LUA_TINT)
  1976. + return luaO_rawequalObj(t2, t1); /* swap LUA_TINT to left */
  1977. +#ifdef LNUM_COMPLEX
  1978. + if (!luai_numeq(nvalue_img_fast(t1), nvalue_img_fast(t2))) return 0;
  1979. +#endif
  1980. + return luai_numeq(nvalue_fast(t1), nvalue_fast(t2));
  1981. case LUA_TBOOLEAN:
  1982. return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
  1983. case LUA_TLIGHTUSERDATA:
  1984. @@ -86,21 +106,6 @@ int luaO_rawequalObj (const TValue *t1,
  1985. }
  1986. }
  1987. -
  1988. -int luaO_str2d (const char *s, lua_Number *result) {
  1989. - char *endptr;
  1990. - *result = lua_str2number(s, &endptr);
  1991. - if (endptr == s) return 0; /* conversion failed */
  1992. - if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
  1993. - *result = cast_num(strtoul(s, &endptr, 16));
  1994. - if (*endptr == '\0') return 1; /* most common case */
  1995. - while (isspace(cast(unsigned char, *endptr))) endptr++;
  1996. - if (*endptr != '\0') return 0; /* invalid trailing characters? */
  1997. - return 1;
  1998. -}
  1999. -
  2000. -
  2001. -
  2002. static void pushstr (lua_State *L, const char *str) {
  2003. setsvalue2s(L, L->top, luaS_new(L, str));
  2004. incr_top(L);
  2005. @@ -131,7 +136,11 @@ const char *luaO_pushvfstring (lua_State
  2006. break;
  2007. }
  2008. case 'd': {
  2009. - setnvalue(L->top, cast_num(va_arg(argp, int)));
  2010. + /* This is tricky for 64-bit integers; maybe they even cannot be
  2011. + * supported on all compilers; depends on the conversions applied to
  2012. + * variable argument lists. TBD: test!
  2013. + */
  2014. + setivalue(L->top, (lua_Integer) va_arg(argp, l_uacInteger));
  2015. incr_top(L);
  2016. break;
  2017. }
  2018. @@ -212,3 +221,4 @@ void luaO_chunkid (char *out, const char
  2019. }
  2020. }
  2021. }
  2022. +
  2023. --- a/src/lobject.h
  2024. +++ b/src/lobject.h
  2025. @@ -17,7 +17,11 @@
  2026. /* tags for values visible from Lua */
  2027. -#define LAST_TAG LUA_TTHREAD
  2028. +#if LUA_TINT > LUA_TTHREAD
  2029. +# define LAST_TAG LUA_TINT
  2030. +#else
  2031. +# define LAST_TAG LUA_TTHREAD
  2032. +#endif
  2033. #define NUM_TAGS (LAST_TAG+1)
  2034. @@ -59,7 +63,12 @@ typedef struct GCheader {
  2035. typedef union {
  2036. GCObject *gc;
  2037. void *p;
  2038. +#ifdef LNUM_COMPLEX
  2039. + lua_Complex n;
  2040. +#else
  2041. lua_Number n;
  2042. +#endif
  2043. + lua_Integer i;
  2044. int b;
  2045. } Value;
  2046. @@ -77,7 +86,11 @@ typedef struct lua_TValue {
  2047. /* Macros to test type */
  2048. #define ttisnil(o) (ttype(o) == LUA_TNIL)
  2049. -#define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
  2050. +#define ttisint(o) (ttype(o) == LUA_TINT)
  2051. +#define ttisnumber(o) ((ttype(o) == LUA_TINT) || (ttype(o) == LUA_TNUMBER))
  2052. +#ifdef LNUM_COMPLEX
  2053. +# define ttiscomplex(o) ((ttype(o) == LUA_TNUMBER) && (nvalue_img_fast(o)!=0))
  2054. +#endif
  2055. #define ttisstring(o) (ttype(o) == LUA_TSTRING)
  2056. #define ttistable(o) (ttype(o) == LUA_TTABLE)
  2057. #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
  2058. @@ -90,7 +103,25 @@ typedef struct lua_TValue {
  2059. #define ttype(o) ((o)->tt)
  2060. #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
  2061. #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
  2062. -#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
  2063. +
  2064. +#define ttype_ext(o) ( ttype(o) == LUA_TINT ? LUA_TNUMBER : ttype(o) )
  2065. +#define ttype_ext_same(o1,o2) ( (ttype(o1)==ttype(o2)) || (ttisnumber(o1) && ttisnumber(o2)) )
  2066. +
  2067. +/* '_fast' variants are for cases where 'ttype(o)' is known to be LUA_TNUMBER.
  2068. + */
  2069. +#ifdef LNUM_COMPLEX
  2070. +# define nvalue_complex_fast(o) check_exp( ttype(o)==LUA_TNUMBER, (o)->value.n )
  2071. +# define nvalue_fast(o) ( _LF(creal) ( nvalue_complex_fast(o) ) )
  2072. +# define nvalue_img_fast(o) ( _LF(cimag) ( nvalue_complex_fast(o) ) )
  2073. +# define nvalue_complex(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? (o)->value.i : (o)->value.n )
  2074. +# define nvalue_img(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? 0 : _LF(cimag)( (o)->value.n ) )
  2075. +# define nvalue(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? cast_num((o)->value.i) : _LF(creal)((o)->value.n) )
  2076. +#else
  2077. +# define nvalue(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? cast_num((o)->value.i) : (o)->value.n )
  2078. +# define nvalue_fast(o) check_exp( ttype(o)==LUA_TNUMBER, (o)->value.n )
  2079. +#endif
  2080. +#define ivalue(o) check_exp( ttype(o)==LUA_TINT, (o)->value.i )
  2081. +
  2082. #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
  2083. #define tsvalue(o) (&rawtsvalue(o)->tsv)
  2084. #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
  2085. @@ -116,8 +147,27 @@ typedef struct lua_TValue {
  2086. /* Macros to set values */
  2087. #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
  2088. -#define setnvalue(obj,x) \
  2089. - { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
  2090. +/* Must not have side effects, 'x' may be expression.
  2091. +*/
  2092. +#define setivalue(obj,x) \
  2093. + { TValue *i_o=(obj); i_o->value.i=(x); i_o->tt=LUA_TINT; }
  2094. +
  2095. +# define setnvalue(obj,x) \
  2096. + { TValue *i_o=(obj); i_o->value.n= (x); i_o->tt=LUA_TNUMBER; }
  2097. +
  2098. +/* Note: Complex always has "inline", both are C99.
  2099. +*/
  2100. +#ifdef LNUM_COMPLEX
  2101. + static inline void setnvalue_complex_fast( TValue *obj, lua_Complex x ) {
  2102. + lua_assert( _LF(cimag)(x) != 0 );
  2103. + obj->value.n= x; obj->tt= LUA_TNUMBER;
  2104. + }
  2105. + static inline void setnvalue_complex( TValue *obj, lua_Complex x ) {
  2106. + if (_LF(cimag)(x) == 0) { setnvalue(obj, _LF(creal)(x)); }
  2107. + else { obj->value.n= x; obj->tt= LUA_TNUMBER; }
  2108. + }
  2109. +#endif
  2110. +
  2111. #define setpvalue(obj,x) \
  2112. { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
  2113. @@ -155,9 +205,6 @@ typedef struct lua_TValue {
  2114. i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
  2115. checkliveness(G(L),i_o); }
  2116. -
  2117. -
  2118. -
  2119. #define setobj(L,obj1,obj2) \
  2120. { const TValue *o2=(obj2); TValue *o1=(obj1); \
  2121. o1->value = o2->value; o1->tt=o2->tt; \
  2122. @@ -185,8 +232,11 @@ typedef struct lua_TValue {
  2123. #define setttype(obj, tt) (ttype(obj) = (tt))
  2124. -
  2125. -#define iscollectable(o) (ttype(o) >= LUA_TSTRING)
  2126. +#if LUA_TINT >= LUA_TSTRING
  2127. +# define iscollectable(o) ((ttype(o) >= LUA_TSTRING) && (ttype(o) != LUA_TINT))
  2128. +#else
  2129. +# define iscollectable(o) (ttype(o) >= LUA_TSTRING)
  2130. +#endif
  2131. @@ -370,12 +420,10 @@ LUAI_FUNC int luaO_log2 (unsigned int x)
  2132. LUAI_FUNC int luaO_int2fb (unsigned int x);
  2133. LUAI_FUNC int luaO_fb2int (int x);
  2134. LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
  2135. -LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
  2136. LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
  2137. va_list argp);
  2138. LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
  2139. LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
  2140. -
  2141. #endif
  2142. --- a/src/loslib.c
  2143. +++ b/src/loslib.c
  2144. @@ -186,15 +186,30 @@ static int os_time (lua_State *L) {
  2145. }
  2146. if (t == (time_t)(-1))
  2147. lua_pushnil(L);
  2148. - else
  2149. - lua_pushnumber(L, (lua_Number)t);
  2150. + else {
  2151. + /* On float systems the pushed value must be an integer, NOT a number.
  2152. + * Otherwise, accuracy is lost in the time_t->float conversion.
  2153. + */
  2154. +#ifdef LNUM_FLOAT
  2155. + lua_pushinteger(L, (lua_Integer) t);
  2156. +#else
  2157. + lua_pushnumber(L, (lua_Number) t);
  2158. +#endif
  2159. + }
  2160. return 1;
  2161. }
  2162. static int os_difftime (lua_State *L) {
  2163. +#ifdef LNUM_FLOAT
  2164. + lua_Integer i= (lua_Integer)
  2165. + difftime( (time_t)(luaL_checkinteger(L, 1)),
  2166. + (time_t)(luaL_optinteger(L, 2, 0)));
  2167. + lua_pushinteger(L, i);
  2168. +#else
  2169. lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
  2170. (time_t)(luaL_optnumber(L, 2, 0))));
  2171. +#endif
  2172. return 1;
  2173. }
  2174. --- a/src/lparser.c
  2175. +++ b/src/lparser.c
  2176. @@ -33,7 +33,6 @@
  2177. #define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m)
  2178. -
  2179. /*
  2180. ** nodes for block list (list of active blocks)
  2181. */
  2182. @@ -72,7 +71,7 @@ static void errorlimit (FuncState *fs, i
  2183. const char *msg = (fs->f->linedefined == 0) ?
  2184. luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) :
  2185. luaO_pushfstring(fs->L, "function at line %d has more than %d %s",
  2186. - fs->f->linedefined, limit, what);
  2187. + (fs->f->linedefined), limit, what);
  2188. luaX_lexerror(fs->ls, msg, 0);
  2189. }
  2190. @@ -733,6 +732,18 @@ static void simpleexp (LexState *ls, exp
  2191. v->u.nval = ls->t.seminfo.r;
  2192. break;
  2193. }
  2194. + case TK_INT: {
  2195. + init_exp(v, VKINT, 0);
  2196. + v->u.ival = ls->t.seminfo.i;
  2197. + break;
  2198. + }
  2199. +#ifdef LNUM_COMPLEX
  2200. + case TK_NUMBER2: {
  2201. + init_exp(v, VKNUM2, 0);
  2202. + v->u.nval = ls->t.seminfo.r;
  2203. + break;
  2204. + }
  2205. +#endif
  2206. case TK_STRING: {
  2207. codestring(ls, v, ls->t.seminfo.ts);
  2208. break;
  2209. @@ -1079,7 +1090,7 @@ static void fornum (LexState *ls, TStrin
  2210. if (testnext(ls, ','))
  2211. exp1(ls); /* optional step */
  2212. else { /* default step = 1 */
  2213. - luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
  2214. + luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_integerK(fs, 1));
  2215. luaK_reserveregs(fs, 1);
  2216. }
  2217. forbody(ls, base, line, 1, 1);
  2218. --- a/src/lparser.h
  2219. +++ b/src/lparser.h
  2220. @@ -31,7 +31,11 @@ typedef enum {
  2221. VRELOCABLE, /* info = instruction pc */
  2222. VNONRELOC, /* info = result register */
  2223. VCALL, /* info = instruction pc */
  2224. - VVARARG /* info = instruction pc */
  2225. + VVARARG, /* info = instruction pc */
  2226. + VKINT /* ival = integer value */
  2227. +#ifdef LNUM_COMPLEX
  2228. + ,VKNUM2 /* nval = imaginary value */
  2229. +#endif
  2230. } expkind;
  2231. typedef struct expdesc {
  2232. @@ -39,6 +43,7 @@ typedef struct expdesc {
  2233. union {
  2234. struct { int info, aux; } s;
  2235. lua_Number nval;
  2236. + lua_Integer ival;
  2237. } u;
  2238. int t; /* patch list of `exit when true' */
  2239. int f; /* patch list of `exit when false' */
  2240. --- a/src/lstrlib.c
  2241. +++ b/src/lstrlib.c
  2242. @@ -43,8 +43,8 @@ static ptrdiff_t posrelat (ptrdiff_t pos
  2243. static int str_sub (lua_State *L) {
  2244. size_t l;
  2245. const char *s = luaL_checklstring(L, 1, &l);
  2246. - ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);
  2247. - ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);
  2248. + ptrdiff_t start = posrelat(luaL_checkint32(L, 2), l);
  2249. + ptrdiff_t end = posrelat(luaL_optint32(L, 3, -1), l);
  2250. if (start < 1) start = 1;
  2251. if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
  2252. if (start <= end)
  2253. @@ -106,8 +106,8 @@ static int str_rep (lua_State *L) {
  2254. static int str_byte (lua_State *L) {
  2255. size_t l;
  2256. const char *s = luaL_checklstring(L, 1, &l);
  2257. - ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
  2258. - ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
  2259. + ptrdiff_t posi = posrelat(luaL_optint32(L, 2, 1), l);
  2260. + ptrdiff_t pose = posrelat(luaL_optint32(L, 3, posi), l);
  2261. int n, i;
  2262. if (posi <= 0) posi = 1;
  2263. if ((size_t)pose > l) pose = l;
  2264. @@ -496,7 +496,7 @@ static int str_find_aux (lua_State *L, i
  2265. size_t l1, l2;
  2266. const char *s = luaL_checklstring(L, 1, &l1);
  2267. const char *p = luaL_checklstring(L, 2, &l2);
  2268. - ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
  2269. + ptrdiff_t init = posrelat(luaL_optint32(L, 3, 1), l1) - 1;
  2270. if (init < 0) init = 0;
  2271. else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;
  2272. if (find && (lua_toboolean(L, 4) || /* explicit request? */
  2273. @@ -690,7 +690,7 @@ static int str_gsub (lua_State *L) {
  2274. ** maximum size of each format specification (such as '%-099.99d')
  2275. ** (+10 accounts for %99.99x plus margin of error)
  2276. */
  2277. -#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
  2278. +#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTEGER_FMT)-2 + 10)
  2279. static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
  2280. @@ -747,9 +747,9 @@ static const char *scanformat (lua_State
  2281. static void addintlen (char *form) {
  2282. size_t l = strlen(form);
  2283. char spec = form[l - 1];
  2284. - strcpy(form + l - 1, LUA_INTFRMLEN);
  2285. - form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;
  2286. - form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0';
  2287. + const char *tmp= LUA_INTEGER_FMT; /* "%lld" or "%ld" */
  2288. + strcpy(form + l - 1, tmp+1);
  2289. + form[l + sizeof(LUA_INTEGER_FMT)-4] = spec;
  2290. }
  2291. @@ -779,12 +779,12 @@ static int str_format (lua_State *L) {
  2292. }
  2293. case 'd': case 'i': {
  2294. addintlen(form);
  2295. - sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg));
  2296. + sprintf(buff, form, luaL_checkinteger(L, arg));
  2297. break;
  2298. }
  2299. case 'o': case 'u': case 'x': case 'X': {
  2300. addintlen(form);
  2301. - sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg));
  2302. + sprintf(buff, form, (unsigned LUA_INTEGER)luaL_checkinteger(L, arg));
  2303. break;
  2304. }
  2305. case 'e': case 'E': case 'f':
  2306. --- a/src/ltable.c
  2307. +++ b/src/ltable.c
  2308. @@ -33,6 +33,7 @@
  2309. #include "lobject.h"
  2310. #include "lstate.h"
  2311. #include "ltable.h"
  2312. +#include "lnum.h"
  2313. /*
  2314. @@ -51,25 +52,15 @@
  2315. #define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
  2316. #define hashboolean(t,p) hashpow2(t, p)
  2317. -
  2318. +#define hashint(t,i) hashpow2(t,i)
  2319. /*
  2320. ** for some types, it is better to avoid modulus by power of 2, as
  2321. ** they tend to have many 2 factors.
  2322. */
  2323. #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
  2324. -
  2325. -
  2326. #define hashpointer(t,p) hashmod(t, IntPoint(p))
  2327. -
  2328. -/*
  2329. -** number of ints inside a lua_Number
  2330. -*/
  2331. -#define numints cast_int(sizeof(lua_Number)/sizeof(int))
  2332. -
  2333. -
  2334. -
  2335. #define dummynode (&dummynode_)
  2336. static const Node dummynode_ = {
  2337. @@ -80,27 +71,46 @@ static const Node dummynode_ = {
  2338. /*
  2339. ** hash for lua_Numbers
  2340. +**
  2341. +** for non-complex modes, never called with 'lua_Integer' value range (s.a. 0)
  2342. */
  2343. static Node *hashnum (const Table *t, lua_Number n) {
  2344. - unsigned int a[numints];
  2345. - int i;
  2346. - if (luai_numeq(n, 0)) /* avoid problems with -0 */
  2347. - return gnode(t, 0);
  2348. - memcpy(a, &n, sizeof(a));
  2349. - for (i = 1; i < numints; i++) a[0] += a[i];
  2350. - return hashmod(t, a[0]);
  2351. + const unsigned int *p= cast(const unsigned int *,&n);
  2352. + unsigned int sum= *p;
  2353. + unsigned int m= sizeof(lua_Number)/sizeof(int);
  2354. + unsigned int i;
  2355. + /* OS X Intel has 'm'==4 and gives "Bus error" if the last integer of
  2356. + * 'n' is read; the actual size of long double is only 80 bits = 10 bytes.
  2357. + * Linux x86 has 'm'==3, and does not require reduction.
  2358. + */
  2359. +#if defined(LNUM_LDOUBLE) && defined(__i386__)
  2360. + if (m>3) m--;
  2361. +#endif
  2362. + for (i = 1; i < m; i++) sum += p[i];
  2363. + return hashmod(t, sum);
  2364. }
  2365. -
  2366. /*
  2367. ** returns the `main' position of an element in a table (that is, the index
  2368. ** of its hash value)
  2369. +**
  2370. +** Floating point numbers with integer value give the hash position of the
  2371. +** integer (so they use the same table position).
  2372. */
  2373. static Node *mainposition (const Table *t, const TValue *key) {
  2374. + lua_Integer i;
  2375. switch (ttype(key)) {
  2376. case LUA_TNUMBER:
  2377. - return hashnum(t, nvalue(key));
  2378. + if (tt_integer_valued(key,&i))
  2379. + return hashint(t, i);
  2380. +#ifdef LNUM_COMPLEX
  2381. + if (nvalue_img_fast(key)!=0 && luai_numeq(nvalue_fast(key),0))
  2382. + return gnode(t, 0); /* 0 and -0 to give same hash */
  2383. +#endif
  2384. + return hashnum(t, nvalue_fast(key));
  2385. + case LUA_TINT:
  2386. + return hashint(t, ivalue(key));
  2387. case LUA_TSTRING:
  2388. return hashstr(t, rawtsvalue(key));
  2389. case LUA_TBOOLEAN:
  2390. @@ -116,16 +126,20 @@ static Node *mainposition (const Table *
  2391. /*
  2392. ** returns the index for `key' if `key' is an appropriate key to live in
  2393. ** the array part of the table, -1 otherwise.
  2394. +**
  2395. +** Anything <=0 is taken as not being in the array part.
  2396. */
  2397. -static int arrayindex (const TValue *key) {
  2398. - if (ttisnumber(key)) {
  2399. - lua_Number n = nvalue(key);
  2400. - int k;
  2401. - lua_number2int(k, n);
  2402. - if (luai_numeq(cast_num(k), n))
  2403. - return k;
  2404. +static int arrayindex (const TValue *key, int max) {
  2405. + lua_Integer k;
  2406. + switch( ttype(key) ) {
  2407. + case LUA_TINT:
  2408. + k= ivalue(key); break;
  2409. + case LUA_TNUMBER:
  2410. + if (tt_integer_valued(key,&k)) break;
  2411. + default:
  2412. + return -1; /* not to be used as array index */
  2413. }
  2414. - return -1; /* `key' did not match some condition */
  2415. + return ((k>0) && (k <= max)) ? cast_int(k) : -1;
  2416. }
  2417. @@ -137,8 +151,8 @@ static int arrayindex (const TValue *key
  2418. static int findindex (lua_State *L, Table *t, StkId key) {
  2419. int i;
  2420. if (ttisnil(key)) return -1; /* first iteration */
  2421. - i = arrayindex(key);
  2422. - if (0 < i && i <= t->sizearray) /* is `key' inside array part? */
  2423. + i = arrayindex(key, t->sizearray);
  2424. + if (i>0) /* inside array part? */
  2425. return i-1; /* yes; that's the index (corrected to C) */
  2426. else {
  2427. Node *n = mainposition(t, key);
  2428. @@ -163,7 +177,7 @@ int luaH_next (lua_State *L, Table *t, S
  2429. int i = findindex(L, t, key); /* find original element */
  2430. for (i++; i < t->sizearray; i++) { /* try first array part */
  2431. if (!ttisnil(&t->array[i])) { /* a non-nil value? */
  2432. - setnvalue(key, cast_num(i+1));
  2433. + setivalue(key, i+1);
  2434. setobj2s(L, key+1, &t->array[i]);
  2435. return 1;
  2436. }
  2437. @@ -209,8 +223,8 @@ static int computesizes (int nums[], int
  2438. static int countint (const TValue *key, int *nums) {
  2439. - int k = arrayindex(key);
  2440. - if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
  2441. + int k = arrayindex(key,MAXASIZE);
  2442. + if (k>0) { /* appropriate array index? */
  2443. nums[ceillog2(k)]++; /* count as such */
  2444. return 1;
  2445. }
  2446. @@ -308,7 +322,7 @@ static void resize (lua_State *L, Table
  2447. /* re-insert elements from vanishing slice */
  2448. for (i=nasize; i<oldasize; i++) {
  2449. if (!ttisnil(&t->array[i]))
  2450. - setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);
  2451. + setobjt2t(L, luaH_setint(L, t, i+1), &t->array[i]);
  2452. }
  2453. /* shrink array */
  2454. luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
  2455. @@ -409,7 +423,9 @@ static TValue *newkey (lua_State *L, Tab
  2456. othern = mainposition(t, key2tval(mp));
  2457. if (othern != mp) { /* is colliding node out of its main position? */
  2458. /* yes; move colliding node into free position */
  2459. - while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
  2460. + while (gnext(othern) != mp) {
  2461. + othern = gnext(othern); /* find previous */
  2462. + }
  2463. gnext(othern) = n; /* redo the chain with `n' in place of `mp' */
  2464. *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
  2465. gnext(mp) = NULL; /* now `mp' is free */
  2466. @@ -432,17 +448,18 @@ static TValue *newkey (lua_State *L, Tab
  2467. /*
  2468. ** search function for integers
  2469. */
  2470. -const TValue *luaH_getnum (Table *t, int key) {
  2471. +const TValue *luaH_getint (Table *t, lua_Integer key) {
  2472. /* (1 <= key && key <= t->sizearray) */
  2473. if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))
  2474. return &t->array[key-1];
  2475. else {
  2476. - lua_Number nk = cast_num(key);
  2477. - Node *n = hashnum(t, nk);
  2478. + Node *n = hashint(t, key);
  2479. do { /* check whether `key' is somewhere in the chain */
  2480. - if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))
  2481. + if (ttisint(gkey(n)) && (ivalue(gkey(n)) == key)) {
  2482. return gval(n); /* that's it */
  2483. - else n = gnext(n);
  2484. + } else {
  2485. + n = gnext(n);
  2486. + }
  2487. } while (n);
  2488. return luaO_nilobject;
  2489. }
  2490. @@ -470,14 +487,12 @@ const TValue *luaH_get (Table *t, const
  2491. switch (ttype(key)) {
  2492. case LUA_TNIL: return luaO_nilobject;
  2493. case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
  2494. + case LUA_TINT: return luaH_getint(t, ivalue(key));
  2495. case LUA_TNUMBER: {
  2496. - int k;
  2497. - lua_Number n = nvalue(key);
  2498. - lua_number2int(k, n);
  2499. - if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
  2500. - return luaH_getnum(t, k); /* use specialized version */
  2501. - /* else go through */
  2502. - }
  2503. + lua_Integer i;
  2504. + if (tt_integer_valued(key,&i))
  2505. + return luaH_getint(t,i);
  2506. + } /* pass through */
  2507. default: {
  2508. Node *n = mainposition(t, key);
  2509. do { /* check whether `key' is somewhere in the chain */
  2510. @@ -498,20 +513,25 @@ TValue *luaH_set (lua_State *L, Table *t
  2511. return cast(TValue *, p);
  2512. else {
  2513. if (ttisnil(key)) luaG_runerror(L, "table index is nil");
  2514. - else if (ttisnumber(key) && luai_numisnan(nvalue(key)))
  2515. - luaG_runerror(L, "table index is NaN");
  2516. + else if (ttype(key)==LUA_TNUMBER) {
  2517. + lua_Integer k;
  2518. + if (luai_numisnan(nvalue_fast(key)))
  2519. + luaG_runerror(L, "table index is NaN");
  2520. + if (tt_integer_valued(key,&k))
  2521. + return luaH_setint(L, t, k);
  2522. + }
  2523. return newkey(L, t, key);
  2524. }
  2525. }
  2526. -TValue *luaH_setnum (lua_State *L, Table *t, int key) {
  2527. - const TValue *p = luaH_getnum(t, key);
  2528. +TValue *luaH_setint (lua_State *L, Table *t, lua_Integer key) {
  2529. + const TValue *p = luaH_getint(t, key);
  2530. if (p != luaO_nilobject)
  2531. return cast(TValue *, p);
  2532. else {
  2533. TValue k;
  2534. - setnvalue(&k, cast_num(key));
  2535. + setivalue(&k, key);
  2536. return newkey(L, t, &k);
  2537. }
  2538. }
  2539. @@ -533,20 +553,21 @@ static int unbound_search (Table *t, uns
  2540. unsigned int i = j; /* i is zero or a present index */
  2541. j++;
  2542. /* find `i' and `j' such that i is present and j is not */
  2543. - while (!ttisnil(luaH_getnum(t, j))) {
  2544. + while (!ttisnil(luaH_getint(t, j))) {
  2545. i = j;
  2546. j *= 2;
  2547. if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
  2548. /* table was built with bad purposes: resort to linear search */
  2549. - i = 1;
  2550. - while (!ttisnil(luaH_getnum(t, i))) i++;
  2551. - return i - 1;
  2552. + for( i = 1; i<MAX_INT+1; i++ ) {
  2553. + if (ttisnil(luaH_getint(t, i))) break;
  2554. + }
  2555. + return i - 1; /* up to MAX_INT */
  2556. }
  2557. }
  2558. /* now do a binary search between them */
  2559. while (j - i > 1) {
  2560. unsigned int m = (i+j)/2;
  2561. - if (ttisnil(luaH_getnum(t, m))) j = m;
  2562. + if (ttisnil(luaH_getint(t, m))) j = m;
  2563. else i = m;
  2564. }
  2565. return i;
  2566. --- a/src/ltable.h
  2567. +++ b/src/ltable.h
  2568. @@ -18,8 +18,8 @@
  2569. #define key2tval(n) (&(n)->i_key.tvk)
  2570. -LUAI_FUNC const TValue *luaH_getnum (Table *t, int key);
  2571. -LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key);
  2572. +LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
  2573. +LUAI_FUNC TValue *luaH_setint (lua_State *L, Table *t, lua_Integer key);
  2574. LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
  2575. LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key);
  2576. LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
  2577. --- a/src/ltm.c
  2578. +++ b/src/ltm.c
  2579. @@ -19,7 +19,6 @@
  2580. #include "ltm.h"
  2581. -
  2582. const char *const luaT_typenames[] = {
  2583. "nil", "boolean", "userdata", "number",
  2584. "string", "table", "function", "userdata", "thread",
  2585. @@ -67,6 +66,9 @@ const TValue *luaT_gettmbyobj (lua_State
  2586. case LUA_TUSERDATA:
  2587. mt = uvalue(o)->metatable;
  2588. break;
  2589. + case LUA_TINT:
  2590. + mt = G(L)->mt[LUA_TNUMBER];
  2591. + break;
  2592. default:
  2593. mt = G(L)->mt[ttype(o)];
  2594. }
  2595. --- a/src/lua.c
  2596. +++ b/src/lua.c
  2597. @@ -16,7 +16,7 @@
  2598. #include "lauxlib.h"
  2599. #include "lualib.h"
  2600. -
  2601. +#include "llimits.h"
  2602. static lua_State *globalL = NULL;
  2603. @@ -382,6 +382,15 @@ int main (int argc, char **argv) {
  2604. l_message(argv[0], "cannot create state: not enough memory");
  2605. return EXIT_FAILURE;
  2606. }
  2607. + /* Checking 'sizeof(lua_Integer)' cannot be made in preprocessor on all compilers.
  2608. + */
  2609. +#ifdef LNUM_INT16
  2610. + lua_assert( sizeof(lua_Integer) == 2 );
  2611. +#elif defined(LNUM_INT32)
  2612. + lua_assert( sizeof(lua_Integer) == 4 );
  2613. +#elif defined(LNUM_INT64)
  2614. + lua_assert( sizeof(lua_Integer) == 8 );
  2615. +#endif
  2616. s.argc = argc;
  2617. s.argv = argv;
  2618. status = lua_cpcall(L, &pmain, &s);
  2619. --- a/src/lua.h
  2620. +++ b/src/lua.h
  2621. @@ -19,7 +19,7 @@
  2622. #define LUA_VERSION "Lua 5.1"
  2623. #define LUA_RELEASE "Lua 5.1.5"
  2624. #define LUA_VERSION_NUM 501
  2625. -#define LUA_COPYRIGHT "Copyright (C) 1994-2012 Lua.org, PUC-Rio"
  2626. +#define LUA_COPYRIGHT "Copyright (C) 1994-2012 Lua.org, PUC-Rio" " (" LUA_LNUM ")"
  2627. #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
  2628. @@ -71,6 +71,16 @@ typedef void * (*lua_Alloc) (void *ud, v
  2629. */
  2630. #define LUA_TNONE (-1)
  2631. +/* LUA_TINT is an internal type, not visible to applications. There are three
  2632. + * potential values where it can be tweaked to (code autoadjusts to these):
  2633. + *
  2634. + * -2: not 'usual' type value; good since 'LUA_TINT' is not part of the API
  2635. + * LUA_TNUMBER+1: shifts other type values upwards, breaking binary compatibility
  2636. + * not acceptable for 5.1, maybe 5.2 onwards?
  2637. + * 9: greater than existing (5.1) type values.
  2638. +*/
  2639. +#define LUA_TINT (-2)
  2640. +
  2641. #define LUA_TNIL 0
  2642. #define LUA_TBOOLEAN 1
  2643. #define LUA_TLIGHTUSERDATA 2
  2644. @@ -139,6 +149,8 @@ LUA_API int (lua_isuserdata)
  2645. LUA_API int (lua_type) (lua_State *L, int idx);
  2646. LUA_API const char *(lua_typename) (lua_State *L, int tp);
  2647. +LUA_API int (lua_isinteger) (lua_State *L, int idx);
  2648. +
  2649. LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2);
  2650. LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
  2651. LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2);
  2652. @@ -244,6 +256,19 @@ LUA_API lua_Alloc (lua_getallocf) (lua_S
  2653. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
  2654. +/*
  2655. +* It is unnecessary to break Lua C API 'lua_tonumber()' compatibility, just
  2656. +* because the Lua number type is complex. Most C modules would use scalars
  2657. +* only. We'll introduce new 'lua_tocomplex' and 'lua_pushcomplex' for when
  2658. +* the module really wants to use them.
  2659. +*/
  2660. +#ifdef LNUM_COMPLEX
  2661. + #include <complex.h>
  2662. + typedef LUA_NUMBER complex lua_Complex;
  2663. + LUA_API lua_Complex (lua_tocomplex) (lua_State *L, int idx);
  2664. + LUA_API void (lua_pushcomplex) (lua_State *L, lua_Complex v);
  2665. +#endif
  2666. +
  2667. /*
  2668. ** ===============================================================
  2669. @@ -268,7 +293,12 @@ LUA_API void lua_setallocf (lua_State *L
  2670. #define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN)
  2671. #define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
  2672. #define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
  2673. -#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
  2674. +
  2675. +#if LUA_TINT < 0
  2676. +# define lua_isnoneornil(L, n) ( (lua_type(L,(n)) <= 0) && (lua_type(L,(n)) != LUA_TINT) )
  2677. +#else
  2678. +# define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
  2679. +#endif
  2680. #define lua_pushliteral(L, s) \
  2681. lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
  2682. @@ -386,3 +416,4 @@ struct lua_Debug {
  2683. #endif
  2684. +
  2685. --- a/src/luaconf.h
  2686. +++ b/src/luaconf.h
  2687. @@ -10,7 +10,9 @@
  2688. #include <limits.h>
  2689. #include <stddef.h>
  2690. -
  2691. +#ifdef lua_assert
  2692. +# include <assert.h>
  2693. +#endif
  2694. /*
  2695. ** ==================================================================
  2696. @@ -136,14 +138,38 @@
  2697. /*
  2698. -@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger.
  2699. -** CHANGE that if ptrdiff_t is not adequate on your machine. (On most
  2700. -** machines, ptrdiff_t gives a good choice between int or long.)
  2701. +@@ LUAI_BITSINT defines the number of bits in an int.
  2702. +** CHANGE here if Lua cannot automatically detect the number of bits of
  2703. +** your machine. Probably you do not need to change this.
  2704. */
  2705. -#define LUA_INTEGER ptrdiff_t
  2706. +/* avoid overflows in comparison */
  2707. +#if INT_MAX-20 < 32760
  2708. +#define LUAI_BITSINT 16
  2709. +#elif INT_MAX > 2147483640L
  2710. +/* int has at least 32 bits */
  2711. +#define LUAI_BITSINT 32
  2712. +#else
  2713. +#error "you must define LUA_BITSINT with number of bits in an integer"
  2714. +#endif
  2715. /*
  2716. +@@ LNUM_DOUBLE | LNUM_FLOAT | LNUM_LDOUBLE: Generic Lua number mode
  2717. +@@ LNUM_INT32 | LNUM_INT64: Integer type
  2718. +@@ LNUM_COMPLEX: Define for using 'a+bi' numbers
  2719. +@@
  2720. +@@ You can combine LNUM_xxx but only one of each group. I.e. '-DLNUM_FLOAT
  2721. +@@ -DLNUM_INT32 -DLNUM_COMPLEX' gives float range complex numbers, with
  2722. +@@ 32-bit scalar integer range optimized.
  2723. +**
  2724. +** These are kept in a separate configuration file mainly for ease of patching
  2725. +** (can be changed if integerated to Lua proper).
  2726. +*/
  2727. +/*#define LNUM_DOUBLE*/
  2728. +/*#define LNUM_INT32*/
  2729. +#include "lnum_config.h"
  2730. +
  2731. +/*
  2732. @@ LUA_API is a mark for all core API functions.
  2733. @@ LUALIB_API is a mark for all standard library functions.
  2734. ** CHANGE them if you need to define those functions in some special way.
  2735. @@ -383,22 +409,6 @@
  2736. /*
  2737. -@@ LUAI_BITSINT defines the number of bits in an int.
  2738. -** CHANGE here if Lua cannot automatically detect the number of bits of
  2739. -** your machine. Probably you do not need to change this.
  2740. -*/
  2741. -/* avoid overflows in comparison */
  2742. -#if INT_MAX-20 < 32760
  2743. -#define LUAI_BITSINT 16
  2744. -#elif INT_MAX > 2147483640L
  2745. -/* int has at least 32 bits */
  2746. -#define LUAI_BITSINT 32
  2747. -#else
  2748. -#error "you must define LUA_BITSINT with number of bits in an integer"
  2749. -#endif
  2750. -
  2751. -
  2752. -/*
  2753. @@ LUAI_UINT32 is an unsigned integer with at least 32 bits.
  2754. @@ LUAI_INT32 is an signed integer with at least 32 bits.
  2755. @@ LUAI_UMEM is an unsigned integer big enough to count the total
  2756. @@ -425,6 +435,15 @@
  2757. #define LUAI_MEM long
  2758. #endif
  2759. +/*
  2760. +@@ LUAI_BOOL carries 0 and nonzero (normally 1). It may be defined as 'char'
  2761. +** (to save memory), 'int' (for speed), 'bool' (for C++) or '_Bool' (C99)
  2762. +*/
  2763. +#ifdef __cplusplus
  2764. +# define LUAI_BOOL bool
  2765. +#else
  2766. +# define LUAI_BOOL int
  2767. +#endif
  2768. /*
  2769. @@ LUAI_MAXCALLS limits the number of nested calls.
  2770. @@ -490,101 +509,6 @@
  2771. /* }================================================================== */
  2772. -
  2773. -
  2774. -/*
  2775. -** {==================================================================
  2776. -@@ LUA_NUMBER is the type of numbers in Lua.
  2777. -** CHANGE the following definitions only if you want to build Lua
  2778. -** with a number type different from double. You may also need to
  2779. -** change lua_number2int & lua_number2integer.
  2780. -** ===================================================================
  2781. -*/
  2782. -
  2783. -#define LUA_NUMBER_DOUBLE
  2784. -#define LUA_NUMBER double
  2785. -
  2786. -/*
  2787. -@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
  2788. -@* over a number.
  2789. -*/
  2790. -#define LUAI_UACNUMBER double
  2791. -
  2792. -
  2793. -/*
  2794. -@@ LUA_NUMBER_SCAN is the format for reading numbers.
  2795. -@@ LUA_NUMBER_FMT is the format for writing numbers.
  2796. -@@ lua_number2str converts a number to a string.
  2797. -@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
  2798. -@@ lua_str2number converts a string to a number.
  2799. -*/
  2800. -#define LUA_NUMBER_SCAN "%lf"
  2801. -#define LUA_NUMBER_FMT "%.14g"
  2802. -#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
  2803. -#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
  2804. -#define lua_str2number(s,p) strtod((s), (p))
  2805. -
  2806. -
  2807. -/*
  2808. -@@ The luai_num* macros define the primitive operations over numbers.
  2809. -*/
  2810. -#if defined(LUA_CORE)
  2811. -#include <math.h>
  2812. -#define luai_numadd(a,b) ((a)+(b))
  2813. -#define luai_numsub(a,b) ((a)-(b))
  2814. -#define luai_nummul(a,b) ((a)*(b))
  2815. -#define luai_numdiv(a,b) ((a)/(b))
  2816. -#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b))
  2817. -#define luai_numpow(a,b) (pow(a,b))
  2818. -#define luai_numunm(a) (-(a))
  2819. -#define luai_numeq(a,b) ((a)==(b))
  2820. -#define luai_numlt(a,b) ((a)<(b))
  2821. -#define luai_numle(a,b) ((a)<=(b))
  2822. -#define luai_numisnan(a) (!luai_numeq((a), (a)))
  2823. -#endif
  2824. -
  2825. -
  2826. -/*
  2827. -@@ lua_number2int is a macro to convert lua_Number to int.
  2828. -@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
  2829. -** CHANGE them if you know a faster way to convert a lua_Number to
  2830. -** int (with any rounding method and without throwing errors) in your
  2831. -** system. In Pentium machines, a naive typecast from double to int
  2832. -** in C is extremely slow, so any alternative is worth trying.
  2833. -*/
  2834. -
  2835. -/* On a Pentium, resort to a trick */
  2836. -#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
  2837. - (defined(__i386) || defined (_M_IX86) || defined(__i386__))
  2838. -
  2839. -/* On a Microsoft compiler, use assembler */
  2840. -#if defined(_MSC_VER)
  2841. -
  2842. -#define lua_number2int(i,d) __asm fld d __asm fistp i
  2843. -#define lua_number2integer(i,n) lua_number2int(i, n)
  2844. -
  2845. -/* the next trick should work on any Pentium, but sometimes clashes
  2846. - with a DirectX idiosyncrasy */
  2847. -#else
  2848. -
  2849. -union luai_Cast { double l_d; long l_l; };
  2850. -#define lua_number2int(i,d) \
  2851. - { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
  2852. -#define lua_number2integer(i,n) lua_number2int(i, n)
  2853. -
  2854. -#endif
  2855. -
  2856. -
  2857. -/* this option always works, but may be slow */
  2858. -#else
  2859. -#define lua_number2int(i,d) ((i)=(int)(d))
  2860. -#define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
  2861. -
  2862. -#endif
  2863. -
  2864. -/* }================================================================== */
  2865. -
  2866. -
  2867. /*
  2868. @@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment.
  2869. ** CHANGE it if your system requires alignments larger than double. (For
  2870. @@ -728,28 +652,6 @@ union luai_Cast { double l_d; long l_l;
  2871. #define luai_userstateyield(L,n) ((void)L)
  2872. -/*
  2873. -@@ LUA_INTFRMLEN is the length modifier for integer conversions
  2874. -@* in 'string.format'.
  2875. -@@ LUA_INTFRM_T is the integer type correspoding to the previous length
  2876. -@* modifier.
  2877. -** CHANGE them if your system supports long long or does not support long.
  2878. -*/
  2879. -
  2880. -#if defined(LUA_USELONGLONG)
  2881. -
  2882. -#define LUA_INTFRMLEN "ll"
  2883. -#define LUA_INTFRM_T long long
  2884. -
  2885. -#else
  2886. -
  2887. -#define LUA_INTFRMLEN "l"
  2888. -#define LUA_INTFRM_T long
  2889. -
  2890. -#endif
  2891. -
  2892. -
  2893. -
  2894. /* =================================================================== */
  2895. /*
  2896. --- a/src/lundump.c
  2897. +++ b/src/lundump.c
  2898. @@ -73,6 +73,13 @@ static lua_Number LoadNumber(LoadState*
  2899. return x;
  2900. }
  2901. +static lua_Integer LoadInteger(LoadState* S)
  2902. +{
  2903. + lua_Integer x;
  2904. + LoadVar(S,x);
  2905. + return x;
  2906. +}
  2907. +
  2908. static TString* LoadString(LoadState* S)
  2909. {
  2910. size_t size;
  2911. @@ -119,6 +126,9 @@ static void LoadConstants(LoadState* S,
  2912. case LUA_TNUMBER:
  2913. setnvalue(o,LoadNumber(S));
  2914. break;
  2915. + case LUA_TINT: /* Integer type saved in bytecode (see lcode.c) */
  2916. + setivalue(o,LoadInteger(S));
  2917. + break;
  2918. case LUA_TSTRING:
  2919. setsvalue2n(S->L,o,LoadString(S));
  2920. break;
  2921. @@ -223,5 +233,22 @@ void luaU_header (char* h)
  2922. *h++=(char)sizeof(size_t);
  2923. *h++=(char)sizeof(Instruction);
  2924. *h++=(char)sizeof(lua_Number);
  2925. - *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
  2926. +
  2927. + /*
  2928. + * Last byte of header (0/1 in unpatched Lua 5.1.3):
  2929. + *
  2930. + * 0: lua_Number is float or double, lua_Integer not used. (nonpatched only)
  2931. + * 1: lua_Number is integer (nonpatched only)
  2932. + *
  2933. + * +2: LNUM_INT16: sizeof(lua_Integer)
  2934. + * +4: LNUM_INT32: sizeof(lua_Integer)
  2935. + * +8: LNUM_INT64: sizeof(lua_Integer)
  2936. + *
  2937. + * +0x80: LNUM_COMPLEX
  2938. + */
  2939. + *h++ = (char)(sizeof(lua_Integer)
  2940. +#ifdef LNUM_COMPLEX
  2941. + | 0x80
  2942. +#endif
  2943. + );
  2944. }
  2945. --- a/src/lvm.c
  2946. +++ b/src/lvm.c
  2947. @@ -25,22 +25,35 @@
  2948. #include "ltable.h"
  2949. #include "ltm.h"
  2950. #include "lvm.h"
  2951. -
  2952. -
  2953. +#include "llex.h"
  2954. +#include "lnum.h"
  2955. /* limit for table tag-method chains (to avoid loops) */
  2956. #define MAXTAGLOOP 100
  2957. -const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
  2958. - lua_Number num;
  2959. +/*
  2960. + * If 'obj' is a string, it is tried to be interpreted as a number.
  2961. + */
  2962. +const TValue *luaV_tonumber ( const TValue *obj, TValue *n) {
  2963. + lua_Number d;
  2964. + lua_Integer i;
  2965. +
  2966. if (ttisnumber(obj)) return obj;
  2967. - if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
  2968. - setnvalue(n, num);
  2969. - return n;
  2970. - }
  2971. - else
  2972. - return NULL;
  2973. +
  2974. + if (ttisstring(obj)) {
  2975. + switch( luaO_str2d( svalue(obj), &d, &i ) ) {
  2976. + case TK_INT:
  2977. + setivalue(n,i); return n;
  2978. + case TK_NUMBER:
  2979. + setnvalue(n,d); return n;
  2980. +#ifdef LNUM_COMPLEX
  2981. + case TK_NUMBER2: /* "N.NNNi", != 0 */
  2982. + setnvalue_complex_fast(n, d*I); return n;
  2983. +#endif
  2984. + }
  2985. + }
  2986. + return NULL;
  2987. }
  2988. @@ -49,8 +62,7 @@ int luaV_tostring (lua_State *L, StkId o
  2989. return 0;
  2990. else {
  2991. char s[LUAI_MAXNUMBER2STR];
  2992. - lua_Number n = nvalue(obj);
  2993. - lua_number2str(s, n);
  2994. + luaO_num2buf(s,obj);
  2995. setsvalue2s(L, obj, luaS_new(L, s));
  2996. return 1;
  2997. }
  2998. @@ -222,59 +234,127 @@ static int l_strcmp (const TString *ls,
  2999. }
  3000. +#ifdef LNUM_COMPLEX
  3001. +void error_complex( lua_State *L, const TValue *l, const TValue *r )
  3002. +{
  3003. + char buf1[ LUAI_MAXNUMBER2STR ];
  3004. + char buf2[ LUAI_MAXNUMBER2STR ];
  3005. + luaO_num2buf( buf1, l );
  3006. + luaO_num2buf( buf2, r );
  3007. + luaG_runerror( L, "unable to compare: %s with %s", buf1, buf2 );
  3008. + /* no return */
  3009. +}
  3010. +#endif
  3011. +
  3012. +
  3013. int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
  3014. int res;
  3015. - if (ttype(l) != ttype(r))
  3016. + int tl,tr;
  3017. + lua_Integer tmp;
  3018. +
  3019. + if (!ttype_ext_same(l,r))
  3020. return luaG_ordererror(L, l, r);
  3021. - else if (ttisnumber(l))
  3022. - return luai_numlt(nvalue(l), nvalue(r));
  3023. - else if (ttisstring(l))
  3024. - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
  3025. - else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
  3026. +#ifdef LNUM_COMPLEX
  3027. + if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) )
  3028. + error_complex( L, l, r );
  3029. +#endif
  3030. + tl= ttype(l); tr= ttype(r);
  3031. + if (tl==tr) { /* clear arithmetics */
  3032. + switch(tl) {
  3033. + case LUA_TINT: return ivalue(l) < ivalue(r);
  3034. + case LUA_TNUMBER: return luai_numlt(nvalue_fast(l), nvalue_fast(r));
  3035. + case LUA_TSTRING: return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
  3036. + }
  3037. + } else if (tl==LUA_TINT) { /* l:int, r:num */
  3038. + /* Avoid accuracy losing casts: if 'r' is integer by value, do comparisons
  3039. + * in integer realm. Only otherwise cast 'l' to FP (which might change its
  3040. + * value).
  3041. + */
  3042. + if (tt_integer_valued(r,&tmp))
  3043. + return ivalue(l) < tmp;
  3044. + else
  3045. + return luai_numlt( cast_num(ivalue(l)), nvalue_fast(r) );
  3046. +
  3047. + } else if (tl==LUA_TNUMBER) { /* l:num, r:int */
  3048. + if (tt_integer_valued(l,&tmp))
  3049. + return tmp < ivalue(r);
  3050. + else
  3051. + return luai_numlt( nvalue_fast(l), cast_num(ivalue(r)) );
  3052. +
  3053. + } else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
  3054. return res;
  3055. +
  3056. return luaG_ordererror(L, l, r);
  3057. }
  3058. static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
  3059. int res;
  3060. - if (ttype(l) != ttype(r))
  3061. + int tl, tr;
  3062. + lua_Integer tmp;
  3063. +
  3064. + if (!ttype_ext_same(l,r))
  3065. return luaG_ordererror(L, l, r);
  3066. - else if (ttisnumber(l))
  3067. - return luai_numle(nvalue(l), nvalue(r));
  3068. - else if (ttisstring(l))
  3069. - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
  3070. - else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
  3071. +#ifdef LNUM_COMPLEX
  3072. + if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) )
  3073. + error_complex( L, l, r );
  3074. +#endif
  3075. + tl= ttype(l); tr= ttype(r);
  3076. + if (tl==tr) { /* clear arithmetics */
  3077. + switch(tl) {
  3078. + case LUA_TINT: return ivalue(l) <= ivalue(r);
  3079. + case LUA_TNUMBER: return luai_numle(nvalue_fast(l), nvalue_fast(r));
  3080. + case LUA_TSTRING: return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
  3081. + }
  3082. + }
  3083. + if (tl==LUA_TINT) { /* l:int, r:num */
  3084. + if (tt_integer_valued(r,&tmp))
  3085. + return ivalue(l) <= tmp;
  3086. + else
  3087. + return luai_numle( cast_num(ivalue(l)), nvalue_fast(r) );
  3088. +
  3089. + } else if (tl==LUA_TNUMBER) { /* l:num, r:int */
  3090. + if (tt_integer_valued(l,&tmp))
  3091. + return tmp <= ivalue(r);
  3092. + else
  3093. + return luai_numle( nvalue_fast(l), cast_num(ivalue(r)) );
  3094. +
  3095. + } else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
  3096. return res;
  3097. else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
  3098. return !res;
  3099. +
  3100. return luaG_ordererror(L, l, r);
  3101. }
  3102. -int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
  3103. +/* Note: 'luaV_equalval()' and 'luaO_rawequalObj()' have largely overlapping
  3104. + * implementation. LUA_TNIL..LUA_TLIGHTUSERDATA cases could be handled
  3105. + * simply by the 'default' case here.
  3106. + */
  3107. +int luaV_equalval (lua_State *L, const TValue *l, const TValue *r) {
  3108. const TValue *tm;
  3109. - lua_assert(ttype(t1) == ttype(t2));
  3110. - switch (ttype(t1)) {
  3111. + lua_assert(ttype_ext_same(l,r));
  3112. + switch (ttype(l)) {
  3113. case LUA_TNIL: return 1;
  3114. - case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
  3115. - case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
  3116. - case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
  3117. + case LUA_TINT:
  3118. + case LUA_TNUMBER: return luaO_rawequalObj(l,r);
  3119. + case LUA_TBOOLEAN: return bvalue(l) == bvalue(r); /* true must be 1 !! */
  3120. + case LUA_TLIGHTUSERDATA: return pvalue(l) == pvalue(r);
  3121. case LUA_TUSERDATA: {
  3122. - if (uvalue(t1) == uvalue(t2)) return 1;
  3123. - tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
  3124. - TM_EQ);
  3125. + if (uvalue(l) == uvalue(r)) return 1;
  3126. + tm = get_compTM(L, uvalue(l)->metatable, uvalue(r)->metatable, TM_EQ);
  3127. break; /* will try TM */
  3128. }
  3129. case LUA_TTABLE: {
  3130. - if (hvalue(t1) == hvalue(t2)) return 1;
  3131. - tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
  3132. + if (hvalue(l) == hvalue(r)) return 1;
  3133. + tm = get_compTM(L, hvalue(l)->metatable, hvalue(r)->metatable, TM_EQ);
  3134. break; /* will try TM */
  3135. }
  3136. - default: return gcvalue(t1) == gcvalue(t2);
  3137. + default: return gcvalue(l) == gcvalue(r);
  3138. }
  3139. if (tm == NULL) return 0; /* no TM? */
  3140. - callTMres(L, L->top, tm, t1, t2); /* call TM */
  3141. + callTMres(L, L->top, tm, l, r); /* call TM */
  3142. return !l_isfalse(L->top);
  3143. }
  3144. @@ -314,30 +394,6 @@ void luaV_concat (lua_State *L, int tota
  3145. }
  3146. -static void Arith (lua_State *L, StkId ra, const TValue *rb,
  3147. - const TValue *rc, TMS op) {
  3148. - TValue tempb, tempc;
  3149. - const TValue *b, *c;
  3150. - if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
  3151. - (c = luaV_tonumber(rc, &tempc)) != NULL) {
  3152. - lua_Number nb = nvalue(b), nc = nvalue(c);
  3153. - switch (op) {
  3154. - case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
  3155. - case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
  3156. - case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
  3157. - case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
  3158. - case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
  3159. - case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
  3160. - case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
  3161. - default: lua_assert(0); break;
  3162. - }
  3163. - }
  3164. - else if (!call_binTM(L, rb, rc, ra, op))
  3165. - luaG_aritherror(L, rb, rc);
  3166. -}
  3167. -
  3168. -
  3169. -
  3170. /*
  3171. ** some macros for common tasks in `luaV_execute'
  3172. */
  3173. @@ -361,17 +417,154 @@ static void Arith (lua_State *L, StkId r
  3174. #define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
  3175. -#define arith_op(op,tm) { \
  3176. - TValue *rb = RKB(i); \
  3177. - TValue *rc = RKC(i); \
  3178. - if (ttisnumber(rb) && ttisnumber(rc)) { \
  3179. - lua_Number nb = nvalue(rb), nc = nvalue(rc); \
  3180. - setnvalue(ra, op(nb, nc)); \
  3181. - } \
  3182. - else \
  3183. - Protect(Arith(L, ra, rb, rc, tm)); \
  3184. +/* Note: if called for unary operations, 'rc'=='rb'.
  3185. + */
  3186. +static void Arith (lua_State *L, StkId ra, const TValue *rb,
  3187. + const TValue *rc, TMS op) {
  3188. + TValue tempb, tempc;
  3189. + const TValue *b, *c;
  3190. + lua_Number nb,nc;
  3191. +
  3192. + if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
  3193. + (c = luaV_tonumber(rc, &tempc)) != NULL) {
  3194. +
  3195. + /* Keep integer arithmetics in the integer realm, if possible.
  3196. + */
  3197. + if (ttisint(b) && ttisint(c)) {
  3198. + lua_Integer ib = ivalue(b), ic = ivalue(c);
  3199. + lua_Integer *ri = &ra->value.i;
  3200. + ra->tt= LUA_TINT; /* part of 'setivalue(ra)' */
  3201. + switch (op) {
  3202. + case TM_ADD: if (try_addint( ri, ib, ic)) return; break;
  3203. + case TM_SUB: if (try_subint( ri, ib, ic)) return; break;
  3204. + case TM_MUL: if (try_mulint( ri, ib, ic)) return; break;
  3205. + case TM_DIV: if (try_divint( ri, ib, ic)) return; break;
  3206. + case TM_MOD: if (try_modint( ri, ib, ic)) return; break;
  3207. + case TM_POW: if (try_powint( ri, ib, ic)) return; break;
  3208. + case TM_UNM: if (try_unmint( ri, ib)) return; break;
  3209. + default: lua_assert(0);
  3210. }
  3211. + }
  3212. + /* Fallback to floating point, when leaving range. */
  3213. +#ifdef LNUM_COMPLEX
  3214. + if ((nvalue_img(b)!=0) || (nvalue_img(c)!=0)) {
  3215. + lua_Complex r;
  3216. + if (op==TM_UNM) {
  3217. + r= -nvalue_complex_fast(b); /* never an integer (or scalar) */
  3218. + setnvalue_complex_fast( ra, r );
  3219. + } else {
  3220. + lua_Complex bb= nvalue_complex(b), cc= nvalue_complex(c);
  3221. + switch (op) {
  3222. + case TM_ADD: r= bb + cc; break;
  3223. + case TM_SUB: r= bb - cc; break;
  3224. + case TM_MUL: r= bb * cc; break;
  3225. + case TM_DIV: r= bb / cc; break;
  3226. + case TM_MOD:
  3227. + luaG_runerror(L, "attempt to use %% on complex numbers"); /* no return */
  3228. + case TM_POW: r= luai_vectpow( bb, cc ); break;
  3229. + default: lua_assert(0); r=0;
  3230. + }
  3231. + setnvalue_complex( ra, r );
  3232. + }
  3233. + return;
  3234. + }
  3235. +#endif
  3236. + nb = nvalue(b); nc = nvalue(c);
  3237. + switch (op) {
  3238. + case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); return;
  3239. + case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); return;
  3240. + case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); return;
  3241. + case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); return;
  3242. + case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); return;
  3243. + case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); return;
  3244. + case TM_UNM: setnvalue(ra, luai_numunm(nb)); return;
  3245. + default: lua_assert(0);
  3246. + }
  3247. + }
  3248. +
  3249. + /* Either operand not a number */
  3250. + if (!call_binTM(L, rb, rc, ra, op))
  3251. + luaG_aritherror(L, rb, rc);
  3252. +}
  3253. +
  3254. +/* Helper macro to sort arithmetic operations into four categories:
  3255. + * TK_INT: integer - integer operands
  3256. + * TK_NUMBER: number - number (non complex, either may be integer)
  3257. + * TK_NUMBER2: complex numbers (at least the other)
  3258. + * 0: non-numeric (at least the other)
  3259. +*/
  3260. +#ifdef LNUM_COMPLEX
  3261. +static inline int arith_mode( const TValue *rb, const TValue *rc ) {
  3262. + if (ttisint(rb) && ttisint(rc)) return TK_INT;
  3263. + if (ttiscomplex(rb) || ttiscomplex(rc)) return TK_NUMBER2;
  3264. + if (ttisnumber(rb) && ttisnumber(rc)) return TK_NUMBER;
  3265. + return 0;
  3266. +}
  3267. +#else
  3268. +# define arith_mode(rb,rc) \
  3269. + ( (ttisint(rb) && ttisint(rc)) ? TK_INT : \
  3270. + (ttisnumber(rb) && ttisnumber(rc)) ? TK_NUMBER : 0 )
  3271. +#endif
  3272. +
  3273. +/* arith_op macro for two operators:
  3274. + * automatically chooses, which function (number, integer, complex) to use
  3275. + */
  3276. +#define ARITH_OP2_START( op_num, op_int ) \
  3277. + int failed= 0; \
  3278. + switch( arith_mode(rb,rc) ) { \
  3279. + case TK_INT: \
  3280. + if (op_int ( &(ra)->value.i, ivalue(rb), ivalue(rc) )) \
  3281. + { ra->tt= LUA_TINT; break; } /* else flow through */ \
  3282. + case TK_NUMBER: \
  3283. + setnvalue(ra, op_num ( nvalue(rb), nvalue(rc) )); break;
  3284. +
  3285. +#define ARITH_OP2_END \
  3286. + default: \
  3287. + failed= 1; break; \
  3288. + } if (!failed) continue;
  3289. +
  3290. +#define arith_op_continue_scalar( op_num, op_int ) \
  3291. + ARITH_OP2_START( op_num, op_int ) \
  3292. + ARITH_OP2_END
  3293. +
  3294. +#ifdef LNUM_COMPLEX
  3295. +# define arith_op_continue( op_num, op_int, op_complex ) \
  3296. + ARITH_OP2_START( op_num, op_int ) \
  3297. + case TK_NUMBER2: \
  3298. + setnvalue_complex( ra, op_complex ( nvalue_complex(rb), nvalue_complex(rc) ) ); break; \
  3299. + ARITH_OP2_END
  3300. +#else
  3301. +# define arith_op_continue(op_num,op_int,_) arith_op_continue_scalar(op_num,op_int)
  3302. +#endif
  3303. +
  3304. +/* arith_op macro for one operator:
  3305. + */
  3306. +#define ARITH_OP1_START( op_num, op_int ) \
  3307. + int failed= 0; \
  3308. + switch( arith_mode(rb,rb) ) { \
  3309. + case TK_INT: \
  3310. + if (op_int ( &(ra)->value.i, ivalue(rb) )) \
  3311. + { ra->tt= LUA_TINT; break; } /* else flow through */ \
  3312. + case TK_NUMBER: \
  3313. + setnvalue(ra, op_num (nvalue(rb))); break; \
  3314. +
  3315. +#define ARITH_OP1_END \
  3316. + default: \
  3317. + failed= 1; break; \
  3318. + } if (!failed) continue;
  3319. +
  3320. +#ifdef LNUM_COMPLEX
  3321. +# define arith_op1_continue( op_num, op_int, op_complex ) \
  3322. + ARITH_OP1_START( op_num, op_int ) \
  3323. + case TK_NUMBER2: \
  3324. + setnvalue_complex( ra, op_complex ( nvalue_complex_fast(rb) )); break; \
  3325. + ARITH_OP1_END
  3326. +#else
  3327. +# define arith_op1_continue( op_num, op_int, _ ) \
  3328. + ARITH_OP1_START( op_num, op_int ) \
  3329. + ARITH_OP1_END
  3330. +#endif
  3331. void luaV_execute (lua_State *L, int nexeccalls) {
  3332. @@ -472,38 +665,45 @@ void luaV_execute (lua_State *L, int nex
  3333. continue;
  3334. }
  3335. case OP_ADD: {
  3336. - arith_op(luai_numadd, TM_ADD);
  3337. + TValue *rb = RKB(i), *rc= RKC(i);
  3338. + arith_op_continue( luai_numadd, try_addint, luai_vectadd );
  3339. + Protect(Arith(L, ra, rb, rc, TM_ADD)); \
  3340. continue;
  3341. }
  3342. case OP_SUB: {
  3343. - arith_op(luai_numsub, TM_SUB);
  3344. + TValue *rb = RKB(i), *rc= RKC(i);
  3345. + arith_op_continue( luai_numsub, try_subint, luai_vectsub );
  3346. + Protect(Arith(L, ra, rb, rc, TM_SUB));
  3347. continue;
  3348. }
  3349. case OP_MUL: {
  3350. - arith_op(luai_nummul, TM_MUL);
  3351. + TValue *rb = RKB(i), *rc= RKC(i);
  3352. + arith_op_continue(luai_nummul, try_mulint, luai_vectmul);
  3353. + Protect(Arith(L, ra, rb, rc, TM_MUL));
  3354. continue;
  3355. }
  3356. case OP_DIV: {
  3357. - arith_op(luai_numdiv, TM_DIV);
  3358. + TValue *rb = RKB(i), *rc= RKC(i);
  3359. + arith_op_continue(luai_numdiv, try_divint, luai_vectdiv);
  3360. + Protect(Arith(L, ra, rb, rc, TM_DIV));
  3361. continue;
  3362. }
  3363. case OP_MOD: {
  3364. - arith_op(luai_nummod, TM_MOD);
  3365. + TValue *rb = RKB(i), *rc= RKC(i);
  3366. + arith_op_continue_scalar(luai_nummod, try_modint); /* scalars only */
  3367. + Protect(Arith(L, ra, rb, rc, TM_MOD));
  3368. continue;
  3369. }
  3370. case OP_POW: {
  3371. - arith_op(luai_numpow, TM_POW);
  3372. + TValue *rb = RKB(i), *rc= RKC(i);
  3373. + arith_op_continue(luai_numpow, try_powint, luai_vectpow);
  3374. + Protect(Arith(L, ra, rb, rc, TM_POW));
  3375. continue;
  3376. }
  3377. case OP_UNM: {
  3378. TValue *rb = RB(i);
  3379. - if (ttisnumber(rb)) {
  3380. - lua_Number nb = nvalue(rb);
  3381. - setnvalue(ra, luai_numunm(nb));
  3382. - }
  3383. - else {
  3384. - Protect(Arith(L, ra, rb, rb, TM_UNM));
  3385. - }
  3386. + arith_op1_continue(luai_numunm, try_unmint, luai_vectunm);
  3387. + Protect(Arith(L, ra, rb, rb, TM_UNM));
  3388. continue;
  3389. }
  3390. case OP_NOT: {
  3391. @@ -515,11 +715,11 @@ void luaV_execute (lua_State *L, int nex
  3392. const TValue *rb = RB(i);
  3393. switch (ttype(rb)) {
  3394. case LUA_TTABLE: {
  3395. - setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
  3396. + setivalue(ra, luaH_getn(hvalue(rb)));
  3397. break;
  3398. }
  3399. case LUA_TSTRING: {
  3400. - setnvalue(ra, cast_num(tsvalue(rb)->len));
  3401. + setivalue(ra, tsvalue(rb)->len);
  3402. break;
  3403. }
  3404. default: { /* try metamethod */
  3405. @@ -652,14 +852,30 @@ void luaV_execute (lua_State *L, int nex
  3406. }
  3407. }
  3408. case OP_FORLOOP: {
  3409. - lua_Number step = nvalue(ra+2);
  3410. - lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
  3411. - lua_Number limit = nvalue(ra+1);
  3412. - if (luai_numlt(0, step) ? luai_numle(idx, limit)
  3413. - : luai_numle(limit, idx)) {
  3414. - dojump(L, pc, GETARG_sBx(i)); /* jump back */
  3415. - setnvalue(ra, idx); /* update internal index... */
  3416. - setnvalue(ra+3, idx); /* ...and external index */
  3417. + /* If start,step and limit are all integers, we don't need to check
  3418. + * against overflow in the looping.
  3419. + */
  3420. + if (ttisint(ra) && ttisint(ra+1) && ttisint(ra+2)) {
  3421. + lua_Integer step = ivalue(ra+2);
  3422. + lua_Integer idx = ivalue(ra) + step; /* increment index */
  3423. + lua_Integer limit = ivalue(ra+1);
  3424. + if (step > 0 ? (idx <= limit) : (limit <= idx)) {
  3425. + dojump(L, pc, GETARG_sBx(i)); /* jump back */
  3426. + setivalue(ra, idx); /* update internal index... */
  3427. + setivalue(ra+3, idx); /* ...and external index */
  3428. + }
  3429. + } else {
  3430. + /* non-integer looping (don't use 'nvalue_fast', some may be integer!)
  3431. + */
  3432. + lua_Number step = nvalue(ra+2);
  3433. + lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
  3434. + lua_Number limit = nvalue(ra+1);
  3435. + if (luai_numlt(0, step) ? luai_numle(idx, limit)
  3436. + : luai_numle(limit, idx)) {
  3437. + dojump(L, pc, GETARG_sBx(i)); /* jump back */
  3438. + setnvalue(ra, idx); /* update internal index... */
  3439. + setnvalue(ra+3, idx); /* ...and external index */
  3440. + }
  3441. }
  3442. continue;
  3443. }
  3444. @@ -668,13 +884,21 @@ void luaV_execute (lua_State *L, int nex
  3445. const TValue *plimit = ra+1;
  3446. const TValue *pstep = ra+2;
  3447. L->savedpc = pc; /* next steps may throw errors */
  3448. + /* Using same location for tonumber's both arguments, effectively does
  3449. + * in-place modification (string->number). */
  3450. if (!tonumber(init, ra))
  3451. luaG_runerror(L, LUA_QL("for") " initial value must be a number");
  3452. else if (!tonumber(plimit, ra+1))
  3453. luaG_runerror(L, LUA_QL("for") " limit must be a number");
  3454. else if (!tonumber(pstep, ra+2))
  3455. luaG_runerror(L, LUA_QL("for") " step must be a number");
  3456. - setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
  3457. + /* Step back one value (keep within integers if we can)
  3458. + */
  3459. + if (!( ttisint(ra) && ttisint(pstep) &&
  3460. + try_subint( &ra->value.i, ivalue(ra), ivalue(pstep) ) )) {
  3461. + /* don't use 'nvalue_fast()', values may be integer */
  3462. + setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
  3463. + }
  3464. dojump(L, pc, GETARG_sBx(i));
  3465. continue;
  3466. }
  3467. @@ -711,7 +935,7 @@ void luaV_execute (lua_State *L, int nex
  3468. luaH_resizearray(L, h, last); /* pre-alloc it at once */
  3469. for (; n > 0; n--) {
  3470. TValue *val = ra+n;
  3471. - setobj2t(L, luaH_setnum(L, h, last--), val);
  3472. + setobj2t(L, luaH_setint(L, h, last--), val);
  3473. luaC_barriert(L, h, val);
  3474. }
  3475. continue;
  3476. --- a/src/lvm.h
  3477. +++ b/src/lvm.h
  3478. @@ -15,11 +15,9 @@
  3479. #define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o)))
  3480. -#define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \
  3481. - (((o) = luaV_tonumber(o,n)) != NULL))
  3482. +#define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL))
  3483. -#define equalobj(L,o1,o2) \
  3484. - (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2))
  3485. +#define equalobj(L,o1,o2) (ttype_ext_same(o1,o2) && luaV_equalval(L, o1, o2))
  3486. LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
  3487. --- a/src/print.c
  3488. +++ b/src/print.c
  3489. @@ -14,6 +14,7 @@
  3490. #include "lobject.h"
  3491. #include "lopcodes.h"
  3492. #include "lundump.h"
  3493. +#include "lnum.h"
  3494. #define PrintFunction luaU_print
  3495. @@ -59,8 +60,16 @@ static void PrintConstant(const Proto* f
  3496. case LUA_TBOOLEAN:
  3497. printf(bvalue(o) ? "true" : "false");
  3498. break;
  3499. + case LUA_TINT:
  3500. + printf(LUA_INTEGER_FMT,ivalue(o));
  3501. + break;
  3502. case LUA_TNUMBER:
  3503. - printf(LUA_NUMBER_FMT,nvalue(o));
  3504. +#ifdef LNUM_COMPLEX
  3505. + // TBD: Do we get complex values here?
  3506. + { lua_Number b= nvalue_img_fast(o);
  3507. + printf( LUA_NUMBER_FMT "%s" LUA_NUMBER_FMT "i", nvalue_fast(o), b>=0 ? "+":"", b ); }
  3508. +#endif
  3509. + printf(LUA_NUMBER_FMT,nvalue_fast(o));
  3510. break;
  3511. case LUA_TSTRING:
  3512. PrintString(rawtsvalue(o));