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

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