lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


 On Wed, Mar 5, 2014 at 1:59 PM, Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:
>> I meant raise runtime errors. That is, if the division by zero is
>> eventually executed, an error will be raised. For that, those macros
>> should be enough. Doesn't it? (As far as I could test, it worked
>> well).
>
> But I meant compile-time errors. For that, the macros are not enough. We
> need extra code to avoid calling the macros before they raise an error.

I think the following patch should be enough for that:

--- src/lcode.c
+++ src/lcode.c
@@ -754,7 +754,11 @@ static int constfolding (FuncState *fs, OpCode
op, expdesc *e1, expdesc *e2) {
   lua_State *L = fs->ls->L;
   if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2))
     return 0;
+#if !defined(LUA_NO_FLOAT)
   if (op == OP_IDIV &&
+#else
+  if ((op == OP_IDIV || op == OP_DIV) &&
+#endif
         (!tointeger(&v1, &i) || !tointeger(&v2, &i) || i == 0))
     return 0;  /* avoid division by 0 and conversion errors */
   if (op == OP_MOD && ttisinteger(&v1) && ttisinteger(&v2) && ivalue(&v2) == 0)

I don't see how to raise a compile-time error only by disabling
lua_Number because luaO_arith() will eventually be called by
constfolding() anyway and proceed the regular computation, based on
the opcode.

I also prevented lua_Number to be created, as follows, but it wasn't
enough (I think because luai_num* can be called based on opcodes):

--- src/lobject.h
+++ src/lobject.h
@@ -188,12 +188,16 @@ typedef struct lua_TValue TValue;
 /* Macros to set values */
 #define settt_(o,t)    ((o)->tt_=(t))

-#define setnvalue(obj,x) \
-  { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
-
 #define setivalue(obj,x) \
   { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); }

+#if !defined(LUA_NO_FLOAT)
+#define setnvalue(obj,x) \
+  { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
+#else
+#define setnvalue      setivalue
+#endif
+
 #define setnilvalue(obj) settt_(obj, LUA_TNIL)

 #define setfvalue(obj,x) \

I've attached a more complete (and invasive) patch where I introduced
a macro (LUA_NO_FLOAT) to disable the dependency of floating-point
numbers, math.h and -lm. Don't know if it is a naive solution, but it
seems to work fine.

Regards,
-- 
Lourival Vieira Neto

Attachment: lua_no_float.patch
Description: Binary data