summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/plugins/lua/SOURCES1
-rw-r--r--apps/plugins/lua/lmathlib.c285
-rw-r--r--apps/plugins/lua/rocklua.c1
3 files changed, 287 insertions, 0 deletions
diff --git a/apps/plugins/lua/SOURCES b/apps/plugins/lua/SOURCES
index 56c029921d..d475783c7a 100644
--- a/apps/plugins/lua/SOURCES
+++ b/apps/plugins/lua/SOURCES
@@ -10,6 +10,7 @@ lfunc.c
10lgc.c 10lgc.c
11liolib.c 11liolib.c
12llex.c 12llex.c
13lmathlib.c
13lmem.c 14lmem.c
14loadlib.c 15loadlib.c
15lobject.c 16lobject.c
diff --git a/apps/plugins/lua/lmathlib.c b/apps/plugins/lua/lmathlib.c
new file mode 100644
index 0000000000..99a104050c
--- /dev/null
+++ b/apps/plugins/lua/lmathlib.c
@@ -0,0 +1,285 @@
1/*
2** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $
3** Standard mathematical library
4** See Copyright Notice in lua.h
5*/
6
7
8#if 0
9#include <stdlib.h>
10#include <math.h>
11#endif
12
13#define lmathlib_c
14#define LUA_LIB
15
16#include "lua.h"
17
18#include "lauxlib.h"
19#include "lualib.h"
20
21
22#undef PI
23#define PI (3.14159265358979323846)
24#define RADIANS_PER_DEGREE (PI/180.0)
25#define DEGREES_PER_RADIAN (180.0/PI)
26
27
28static int math_abs (lua_State *L) {
29 /* Was: lua_pushnumber(L, fabs(luaL_checknumber(L, 1))); */
30 lua_Number n = luaL_checknumber(L, 1);
31 lua_pushnumber(L, n < 0 ? -n : n);
32 return 1;
33}
34
35#if 0
36static int math_sin (lua_State *L) {
37 lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
38 return 1;
39}
40
41static int math_sinh (lua_State *L) {
42 lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
43 return 1;
44}
45
46static int math_cos (lua_State *L) {
47 lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
48 return 1;
49}
50
51static int math_cosh (lua_State *L) {
52 lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
53 return 1;
54}
55
56static int math_tan (lua_State *L) {
57 lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
58 return 1;
59}
60
61static int math_tanh (lua_State *L) {
62 lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
63 return 1;
64}
65
66static int math_asin (lua_State *L) {
67 lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
68 return 1;
69}
70
71static int math_acos (lua_State *L) {
72 lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
73 return 1;
74}
75
76static int math_atan (lua_State *L) {
77 lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
78 return 1;
79}
80
81static int math_atan2 (lua_State *L) {
82 lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
83 return 1;
84}
85#endif
86
87static int math_ceil (lua_State *L) {
88 /* Doesn't change anything in fixed point arithmetic */
89 lua_pushnumber(L, luaL_checknumber(L, 1));
90 return 1;
91}
92
93static int math_floor (lua_State *L) {
94 /* Doesn't change anything in fixed point arithmetic */
95 lua_pushnumber(L, luaL_checknumber(L, 1));
96 return 1;
97}
98
99static int math_fmod (lua_State *L) {
100 /* Was: lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); */
101 lua_pushnumber(L, luaL_checknumber(L, 1) % luaL_checknumber(L, 2));
102 return 1;
103}
104
105#if 0
106static int math_modf (lua_State *L) {
107 double ip;
108 double fp = modf(luaL_checknumber(L, 1), &ip);
109 lua_pushnumber(L, ip);
110 lua_pushnumber(L, fp);
111 return 2;
112}
113
114static int math_sqrt (lua_State *L) {
115 lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
116 return 1;
117}
118
119static int math_pow (lua_State *L) {
120 lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
121 return 1;
122}
123
124static int math_log (lua_State *L) {
125 lua_pushnumber(L, log(luaL_checknumber(L, 1)));
126 return 1;
127}
128
129static int math_log10 (lua_State *L) {
130 lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
131 return 1;
132}
133
134static int math_exp (lua_State *L) {
135 lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
136 return 1;
137}
138#endif
139static int math_deg (lua_State *L) {
140 lua_pushnumber(L, luaL_checknumber(L, 1)*DEGREES_PER_RADIAN);
141 return 1;
142}
143static int math_rad (lua_State *L) {
144 lua_pushnumber(L, (luaL_checknumber(L, 1)*100)/(DEGREES_PER_RADIAN*100));
145 return 1;
146}
147
148#if 0
149static int math_frexp (lua_State *L) {
150 int e;
151 lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
152 lua_pushinteger(L, e);
153 return 2;
154}
155
156static int math_ldexp (lua_State *L) {
157 lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
158 return 1;
159}
160#endif
161
162
163static int math_min (lua_State *L) {
164 int n = lua_gettop(L); /* number of arguments */
165 lua_Number dmin = luaL_checknumber(L, 1);
166 int i;
167 for (i=2; i<=n; i++) {
168 lua_Number d = luaL_checknumber(L, i);
169 if (d < dmin)
170 dmin = d;
171 }
172 lua_pushnumber(L, dmin);
173 return 1;
174}
175
176
177static int math_max (lua_State *L) {
178 int n = lua_gettop(L); /* number of arguments */
179 lua_Number dmax = luaL_checknumber(L, 1);
180 int i;
181 for (i=2; i<=n; i++) {
182 lua_Number d = luaL_checknumber(L, i);
183 if (d > dmax)
184 dmax = d;
185 }
186 lua_pushnumber(L, dmax);
187 return 1;
188}
189
190
191static int math_random (lua_State *L) {
192 /* We're not SunOS */
193 lua_Number r = (lua_Number)(rb->rand());
194 switch (lua_gettop(L)) { /* check number of arguments */
195 case 0: { /* no arguments */
196 lua_pushnumber(L, r); /* Number between 0 and RAND_MAX */
197 break;
198 }
199 case 1: { /* only upper limit */
200 int u = luaL_checkint(L, 1);
201 luaL_argcheck(L, 1<=u, 1, "interval is empty");
202 lua_pushnumber(L, r%u+1); /* int between 1 and `u' */
203 break;
204 }
205 case 2: { /* lower and upper limits */
206 int l = luaL_checkint(L, 1);
207 int u = luaL_checkint(L, 2);
208 luaL_argcheck(L, l<=u, 2, "interval is empty");
209 lua_pushnumber(L, r%(u-l+1)+l); /* int between `l' and `u' */
210 break;
211 }
212 default: return luaL_error(L, "wrong number of arguments");
213 }
214 return 1;
215}
216
217
218static int math_randomseed (lua_State *L) {
219 rb->srand(luaL_checkint(L, 1));
220 return 0;
221}
222
223
224static const luaL_Reg mathlib[] = {
225 {"abs", math_abs},
226#if 0
227 {"acos", math_acos},
228 {"asin", math_asin},
229 {"atan2", math_atan2},
230 {"atan", math_atan},
231#endif
232 {"ceil", math_ceil},
233#if 0
234 {"cosh", math_cosh},
235 {"cos", math_cos},
236#endif
237 {"deg", math_deg},
238#if 0
239 {"exp", math_exp},
240#endif
241 {"floor", math_floor},
242 {"fmod", math_fmod},
243#if 0
244 {"frexp", math_frexp},
245 {"ldexp", math_ldexp},
246 {"log10", math_log10},
247 {"log", math_log},
248#endif
249 {"max", math_max},
250 {"min", math_min},
251#if 0
252 {"modf", math_modf},
253 {"pow", math_pow},
254#endif
255 {"rad", math_rad},
256 {"random", math_random},
257 {"randomseed", math_randomseed},
258#if 0
259 {"sinh", math_sinh},
260 {"sin", math_sin},
261 {"sqrt", math_sqrt},
262 {"tanh", math_tanh},
263 {"tan", math_tan},
264#endif
265 {NULL, NULL}
266};
267
268
269/*
270** Open math library
271*/
272LUALIB_API int luaopen_math (lua_State *L) {
273 luaL_register(L, LUA_MATHLIBNAME, mathlib);
274#if 0 /* No use in adding floating point constants when there's no FP */
275 lua_pushnumber(L, PI);
276 lua_setfield(L, -2, "pi");
277 lua_pushnumber(L, HUGE_VAL);
278 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
284 return 1;
285}
diff --git a/apps/plugins/lua/rocklua.c b/apps/plugins/lua/rocklua.c
index 266fa4305e..395cde8d9d 100644
--- a/apps/plugins/lua/rocklua.c
+++ b/apps/plugins/lua/rocklua.c
@@ -38,6 +38,7 @@ static const luaL_Reg lualibs[] = {
38 {LUA_BITLIBNAME, luaopen_bit}, 38 {LUA_BITLIBNAME, luaopen_bit},
39 {LUA_IOLIBNAME, luaopen_io}, 39 {LUA_IOLIBNAME, luaopen_io},
40 {LUA_LOADLIBNAME, luaopen_package}, 40 {LUA_LOADLIBNAME, luaopen_package},
41 {LUA_MATHLIBNAME, luaopen_math},
41 {NULL, NULL} 42 {NULL, NULL}
42}; 43};
43 44