summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lmathlib.c
diff options
context:
space:
mode:
authorRichard Quirk <richard.quirk@gmail.com>2014-03-19 19:31:31 +0100
committerMarcin Bukat <marcin.bukat@gmail.com>2014-04-02 20:31:54 +0200
commit36378988ad4059982742f05f5eb50580b456840a (patch)
treeee70be810d894566c5e351f21a1ebb79be742a85 /apps/plugins/lua/lmathlib.c
parent020f16a1c7d5bc9a302671cef03f56ac735e20c5 (diff)
downloadrockbox-36378988ad4059982742f05f5eb50580b456840a.tar.gz
rockbox-36378988ad4059982742f05f5eb50580b456840a.zip
Update lua plugin to 5.2.3
Prior to this patch the Lua plugin used version 5.1.4. This change reduces the number of modifications in the Lua source using some new defines and because the upstream source is now more flexible. Unless otherwise stated, l*.[ch] files are taken unmodified from the upstream lua-5.2.3. fscanf.c: file descriptors in rockbox are just ints, they are hidden behind a void* now so liolib requires less modifications. fscanf is updated to use void* too. getc.c: this is a new file required for getc implementation in lauxlib.c lauxlib.c: LoadF replaced FILE* with int, the rockbox file descriptor int are cast to FILE* (actually void* due to typedef). getc uses the PREFIX version. stdin is not used, as per 5.1.4. lbaselib.c: now uses strspn in the number parsing. print uses DEBUGF now rather than being commented out. lbitlib.c: use the built-in version from 5.2.3 rather than Reuben Thomas's external library. Backwards compatible and adds some new bit operations. ldo.c: the LUAI_THROW/TRY defines are now in the core lua code, so have been removed from rockconf.h liolib.c: here the implementation has changed to use the LStream from the original source, and cast the FILE* pointers to int. This has reduced the number of modifications from the upstream version. llex.c: the only change from upstream is to remove the locale include. lmathlib.c: updated from the 5.2.3 version and re-applied the changes that were made vs 5.1.4 for random numbers and to remove unsupported float functions. loadlib.c: upstream version, with the 5.1.4 changes for missing functions. lobject.c: upstream version, with ctype.h added and sprintf changed to snprintf. loslib.c: upstream version with locale.h removed and 5.1.4 changes for unsupportable functions. lstrlib.c: sprintf changed to snprintf. ltable.c: upstream with the hashnum function from 5.1.4 to avoid frexp in luai_hashnum. luaconf.h: updated to 5.2.3 version, restored relevant parts from the original 5.1.4 configuration. The COMPAT defines that are no longer available are not included. lundump.c: VERSION macro conflicts with the core Rockbox equivalent. rocklib.c: luaL_reg is no longer available, replaced by luaL_Reg equivalent. Moved checkboolean/optboolean functions to this file and out of core lua files. luaL_getn is no longer available, replaced by luaL_rawlen. luaL_register is deprecated, use the newlib/setfuncs replacements. rli_init has to be called before setting up the newlib to avoid overwriting the rb table. rocklib_aux.pl: use rli_checkboolean from rocklib.c. rocklua.c: new default bits library used, update the library loading code with idiomatic 5.2 code. strcspn.c: no longer needed, but strspn.c is required for strspn in lbaselib.c Change-Id: I0c7945c755f79083afe98ec117e1e8cf13de2651 Reviewed-on: http://gerrit.rockbox.org/774 Tested: Richard Quirk <richard.quirk@gmail.com> Reviewed-by: Marcin Bukat <marcin.bukat@gmail.com>
Diffstat (limited to 'apps/plugins/lua/lmathlib.c')
-rw-r--r--apps/plugins/lua/lmathlib.c92
1 files changed, 53 insertions, 39 deletions
diff --git a/apps/plugins/lua/lmathlib.c b/apps/plugins/lua/lmathlib.c
index 99a104050c..0b288e6a12 100644
--- a/apps/plugins/lua/lmathlib.c
+++ b/apps/plugins/lua/lmathlib.c
@@ -1,14 +1,12 @@
1/* 1/*
2** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $ 2** $Id: lmathlib.c,v 1.83.1.1 2013/04/12 18:48:47 roberto Exp $
3** Standard mathematical library 3** Standard mathematical library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
7 7
8#if 0
9#include <stdlib.h> 8#include <stdlib.h>
10#include <math.h> 9#include <math.h>
11#endif
12 10
13#define lmathlib_c 11#define lmathlib_c
14#define LUA_LIB 12#define LUA_LIB
@@ -20,9 +18,9 @@
20 18
21 19
22#undef PI 20#undef PI
23#define PI (3.14159265358979323846) 21#define PI ((lua_Number)(3.1415926535897932384626433832795))
24#define RADIANS_PER_DEGREE (PI/180.0) 22#define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0))
25#define DEGREES_PER_RADIAN (180.0/PI) 23#define DEGREES_PER_RADIAN ((lua_Number)180.0/PI)
26 24
27 25
28static int math_abs (lua_State *L) { 26static int math_abs (lua_State *L) {
@@ -34,52 +32,53 @@ static int math_abs (lua_State *L) {
34 32
35#if 0 33#if 0
36static int math_sin (lua_State *L) { 34static int math_sin (lua_State *L) {
37 lua_pushnumber(L, sin(luaL_checknumber(L, 1))); 35 lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
38 return 1; 36 return 1;
39} 37}
40 38
41static int math_sinh (lua_State *L) { 39static int math_sinh (lua_State *L) {
42 lua_pushnumber(L, sinh(luaL_checknumber(L, 1))); 40 lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
43 return 1; 41 return 1;
44} 42}
45 43
46static int math_cos (lua_State *L) { 44static int math_cos (lua_State *L) {
47 lua_pushnumber(L, cos(luaL_checknumber(L, 1))); 45 lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
48 return 1; 46 return 1;
49} 47}
50 48
51static int math_cosh (lua_State *L) { 49static int math_cosh (lua_State *L) {
52 lua_pushnumber(L, cosh(luaL_checknumber(L, 1))); 50 lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
53 return 1; 51 return 1;
54} 52}
55 53
56static int math_tan (lua_State *L) { 54static int math_tan (lua_State *L) {
57 lua_pushnumber(L, tan(luaL_checknumber(L, 1))); 55 lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
58 return 1; 56 return 1;
59} 57}
60 58
61static int math_tanh (lua_State *L) { 59static int math_tanh (lua_State *L) {
62 lua_pushnumber(L, tanh(luaL_checknumber(L, 1))); 60 lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
63 return 1; 61 return 1;
64} 62}
65 63
66static int math_asin (lua_State *L) { 64static int math_asin (lua_State *L) {
67 lua_pushnumber(L, asin(luaL_checknumber(L, 1))); 65 lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
68 return 1; 66 return 1;
69} 67}
70 68
71static int math_acos (lua_State *L) { 69static int math_acos (lua_State *L) {
72 lua_pushnumber(L, acos(luaL_checknumber(L, 1))); 70 lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
73 return 1; 71 return 1;
74} 72}
75 73
76static int math_atan (lua_State *L) { 74static int math_atan (lua_State *L) {
77 lua_pushnumber(L, atan(luaL_checknumber(L, 1))); 75 lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1)));
78 return 1; 76 return 1;
79} 77}
80 78
81static int math_atan2 (lua_State *L) { 79static int math_atan2 (lua_State *L) {
82 lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 80 lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1),
81 luaL_checknumber(L, 2)));
83 return 1; 82 return 1;
84} 83}
85#endif 84#endif
@@ -104,35 +103,48 @@ static int math_fmod (lua_State *L) {
104 103
105#if 0 104#if 0
106static int math_modf (lua_State *L) { 105static int math_modf (lua_State *L) {
107 double ip; 106 lua_Number ip;
108 double fp = modf(luaL_checknumber(L, 1), &ip); 107 lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip);
109 lua_pushnumber(L, ip); 108 lua_pushnumber(L, ip);
110 lua_pushnumber(L, fp); 109 lua_pushnumber(L, fp);
111 return 2; 110 return 2;
112} 111}
113 112
114static int math_sqrt (lua_State *L) { 113static int math_sqrt (lua_State *L) {
115 lua_pushnumber(L, sqrt(luaL_checknumber(L, 1))); 114 lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
116 return 1; 115 return 1;
117} 116}
118 117
119static int math_pow (lua_State *L) { 118static int math_pow (lua_State *L) {
120 lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 119 lua_Number x = luaL_checknumber(L, 1);
120 lua_Number y = luaL_checknumber(L, 2);
121 lua_pushnumber(L, l_mathop(pow)(x, y));
121 return 1; 122 return 1;
122} 123}
123 124
124static int math_log (lua_State *L) { 125static int math_log (lua_State *L) {
125 lua_pushnumber(L, log(luaL_checknumber(L, 1))); 126 lua_Number x = luaL_checknumber(L, 1);
127 lua_Number res;
128 if (lua_isnoneornil(L, 2))
129 res = l_mathop(log)(x);
130 else {
131 lua_Number base = luaL_checknumber(L, 2);
132 if (base == (lua_Number)10.0) res = l_mathop(log10)(x);
133 else res = l_mathop(log)(x)/l_mathop(log)(base);
134 }
135 lua_pushnumber(L, res);
126 return 1; 136 return 1;
127} 137}
128 138
139#if defined(LUA_COMPAT_LOG10)
129static int math_log10 (lua_State *L) { 140static int math_log10 (lua_State *L) {
130 lua_pushnumber(L, log10(luaL_checknumber(L, 1))); 141 lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
131 return 1; 142 return 1;
132} 143}
144#endif
133 145
134static int math_exp (lua_State *L) { 146static int math_exp (lua_State *L) {
135 lua_pushnumber(L, exp(luaL_checknumber(L, 1))); 147 lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
136 return 1; 148 return 1;
137} 149}
138#endif 150#endif
@@ -140,6 +152,7 @@ static int math_deg (lua_State *L) {
140 lua_pushnumber(L, luaL_checknumber(L, 1)*DEGREES_PER_RADIAN); 152 lua_pushnumber(L, luaL_checknumber(L, 1)*DEGREES_PER_RADIAN);
141 return 1; 153 return 1;
142} 154}
155
143static int math_rad (lua_State *L) { 156static int math_rad (lua_State *L) {
144 lua_pushnumber(L, (luaL_checknumber(L, 1)*100)/(DEGREES_PER_RADIAN*100)); 157 lua_pushnumber(L, (luaL_checknumber(L, 1)*100)/(DEGREES_PER_RADIAN*100));
145 return 1; 158 return 1;
@@ -148,13 +161,15 @@ static int math_rad (lua_State *L) {
148#if 0 161#if 0
149static int math_frexp (lua_State *L) { 162static int math_frexp (lua_State *L) {
150 int e; 163 int e;
151 lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e)); 164 lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
152 lua_pushinteger(L, e); 165 lua_pushinteger(L, e);
153 return 2; 166 return 2;
154} 167}
155 168
156static int math_ldexp (lua_State *L) { 169static int math_ldexp (lua_State *L) {
157 lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2))); 170 lua_Number x = luaL_checknumber(L, 1);
171 int ep = luaL_checkint(L, 2);
172 lua_pushnumber(L, l_mathop(ldexp)(x, ep));
158 return 1; 173 return 1;
159} 174}
160#endif 175#endif
@@ -197,16 +212,16 @@ static int math_random (lua_State *L) {
197 break; 212 break;
198 } 213 }
199 case 1: { /* only upper limit */ 214 case 1: { /* only upper limit */
200 int u = luaL_checkint(L, 1); 215 lua_Number u = luaL_checknumber(L, 1);
201 luaL_argcheck(L, 1<=u, 1, "interval is empty"); 216 luaL_argcheck(L, 1 <= u, 1, "interval is empty");
202 lua_pushnumber(L, r%u+1); /* int between 1 and `u' */ 217 lua_pushnumber(L, r%u+1); /* int between 1 and `u' */
203 break; 218 break;
204 } 219 }
205 case 2: { /* lower and upper limits */ 220 case 2: { /* lower and upper limits */
206 int l = luaL_checkint(L, 1); 221 lua_Number l = luaL_checknumber(L, 1);
207 int u = luaL_checkint(L, 2); 222 lua_Number u = luaL_checknumber(L, 2);
208 luaL_argcheck(L, l<=u, 2, "interval is empty"); 223 luaL_argcheck(L, l <= u, 2, "interval is empty");
209 lua_pushnumber(L, r%(u-l+1)+l); /* int between `l' and `u' */ 224 lua_pushnumber(L, r%(u-l+1)+l); /* [l, u] */
210 break; 225 break;
211 } 226 }
212 default: return luaL_error(L, "wrong number of arguments"); 227 default: return luaL_error(L, "wrong number of arguments");
@@ -216,7 +231,7 @@ static int math_random (lua_State *L) {
216 231
217 232
218static int math_randomseed (lua_State *L) { 233static int math_randomseed (lua_State *L) {
219 rb->srand(luaL_checkint(L, 1)); 234 rb->srand(luaL_checkunsigned(L, 1));
220 return 0; 235 return 0;
221} 236}
222 237
@@ -243,7 +258,9 @@ static const luaL_Reg mathlib[] = {
243#if 0 258#if 0
244 {"frexp", math_frexp}, 259 {"frexp", math_frexp},
245 {"ldexp", math_ldexp}, 260 {"ldexp", math_ldexp},
261#if defined(LUA_COMPAT_LOG10)
246 {"log10", math_log10}, 262 {"log10", math_log10},
263#endif
247 {"log", math_log}, 264 {"log", math_log},
248#endif 265#endif
249 {"max", math_max}, 266 {"max", math_max},
@@ -259,7 +276,7 @@ static const luaL_Reg mathlib[] = {
259 {"sinh", math_sinh}, 276 {"sinh", math_sinh},
260 {"sin", math_sin}, 277 {"sin", math_sin},
261 {"sqrt", math_sqrt}, 278 {"sqrt", math_sqrt},
262 {"tanh", math_tanh}, 279 {"tanh", math_tanh},
263 {"tan", math_tan}, 280 {"tan", math_tan},
264#endif 281#endif
265 {NULL, NULL} 282 {NULL, NULL}
@@ -269,17 +286,14 @@ static const luaL_Reg mathlib[] = {
269/* 286/*
270** Open math library 287** Open math library
271*/ 288*/
272LUALIB_API int luaopen_math (lua_State *L) { 289LUAMOD_API int luaopen_math (lua_State *L) {
273 luaL_register(L, LUA_MATHLIBNAME, mathlib); 290 luaL_newlib(L, mathlib);
274#if 0 /* No use in adding floating point constants when there's no FP */ 291#if 0 /* No use in adding floating point constants when there's no FP */
275 lua_pushnumber(L, PI); 292 lua_pushnumber(L, PI);
276 lua_setfield(L, -2, "pi"); 293 lua_setfield(L, -2, "pi");
277 lua_pushnumber(L, HUGE_VAL); 294 lua_pushnumber(L, HUGE_VAL);
278 lua_setfield(L, -2, "huge"); 295 lua_setfield(L, -2, "huge");
279#if defined(LUA_COMPAT_MOD)
280 lua_getfield(L, -1, "fmod");
281 lua_setfield(L, -2, "mod");
282#endif
283#endif 296#endif
284 return 1; 297 return 1;
285} 298}
299