diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-03-21 15:37:28 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-03-21 15:37:28 -0300 |
commit | 6fef372fb8b14be806070d6c2e96fd2b9cb2e41a (patch) | |
tree | b47572e1d79829b1871e89a86559e35510a6635a | |
parent | 052a1cc46c3b1a2e589bd061be0e1bd153c9e47a (diff) | |
download | lua-6fef372fb8b14be806070d6c2e96fd2b9cb2e41a.tar.gz lua-6fef372fb8b14be806070d6c2e96fd2b9cb2e41a.tar.bz2 lua-6fef372fb8b14be806070d6c2e96fd2b9cb2e41a.zip |
mathlib now uses i.m. for "pow" operator.
-rw-r--r-- | fallback.c | 4 | ||||
-rw-r--r-- | mathlib.c | 26 |
2 files changed, 7 insertions, 23 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_fallback="$Id: fallback.c,v 1.30 1997/03/20 19:20:43 roberto Exp roberto $"; | 6 | char *rcs_fallback="$Id: fallback.c,v 1.31 1997/03/20 20:36:19 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <string.h> | 9 | #include <string.h> |
@@ -351,7 +351,7 @@ void luaI_setfallback (void) | |||
351 | } | 351 | } |
352 | else if (strcmp(name, "arith") == 0) { /* old arith fallback */ | 352 | else if (strcmp(name, "arith") == 0) { /* old arith fallback */ |
353 | int i; | 353 | int i; |
354 | oldfunc = luaI_IMtable[LUA_T_USERDATA].int_method[IM_ADD]; | 354 | oldfunc = luaI_IMtable[LUA_T_USERDATA].int_method[IM_POW]; |
355 | for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */ | 355 | for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */ |
356 | fillvalids(i, luaI_Address(func)); | 356 | fillvalids(i, luaI_Address(func)); |
357 | replace = typeFB; | 357 | replace = typeFB; |
@@ -3,7 +3,7 @@ | |||
3 | ** Mathematics library to LUA | 3 | ** Mathematics library to LUA |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_mathlib="$Id: mathlib.c,v 1.19 1997/03/17 17:01:10 roberto Exp roberto $"; | 6 | char *rcs_mathlib="$Id: mathlib.c,v 1.20 1997/03/18 15:30:50 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <stdlib.h> | 8 | #include <stdlib.h> |
9 | #include <math.h> | 9 | #include <math.h> |
@@ -105,28 +105,12 @@ static void math_sqrt (void) | |||
105 | lua_pushnumber (sqrt(d)); | 105 | lua_pushnumber (sqrt(d)); |
106 | } | 106 | } |
107 | 107 | ||
108 | static int old_pow; | ||
109 | 108 | ||
110 | static void math_pow (void) | 109 | static void math_pow (void) |
111 | { | 110 | { |
112 | lua_Object o1 = lua_getparam (1); | 111 | double d1 = luaL_check_number(1, "exp"); |
113 | lua_Object o2 = lua_getparam (2); | 112 | double d2 = luaL_check_number(2, "exp"); |
114 | lua_Object op = lua_getparam(3); | 113 | lua_pushnumber(pow(d1,d2)); |
115 | if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p') | ||
116 | { | ||
117 | lua_Object old = lua_getref(old_pow); | ||
118 | lua_pushobject(o1); | ||
119 | lua_pushobject(o2); | ||
120 | lua_pushobject(op); | ||
121 | if (lua_callfunction(old) != 0) | ||
122 | lua_error(NULL); | ||
123 | } | ||
124 | else | ||
125 | { | ||
126 | double d1 = lua_getnumber(o1); | ||
127 | double d2 = lua_getnumber(o2); | ||
128 | lua_pushnumber (pow(d1,d2)); | ||
129 | } | ||
130 | } | 114 | } |
131 | 115 | ||
132 | static void math_min (void) | 116 | static void math_min (void) |
@@ -226,6 +210,6 @@ static struct luaL_reg mathlib[] = { | |||
226 | void mathlib_open (void) | 210 | void mathlib_open (void) |
227 | { | 211 | { |
228 | luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0]))); | 212 | luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0]))); |
229 | old_pow = lua_refobject(lua_setfallback("arith", math_pow), 1); | 213 | lua_setintmethod(0, "pow", math_pow); |
230 | } | 214 | } |
231 | 215 | ||