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

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