2
0

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

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