lua-users home
lua-l archive

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


On Sun, Mar 23, 2014 at 04:40:24PM -0300, Roberto Ierusalimschy wrote:
> > I'd like to suggest that the timing for changes to coercion is in this
> I am afraid elimination of this conversion is a much deeper disruption
> than it seems; I would love to be proved wrong.

Most common breakage seem to be (%d+) captures. This is reasonably
possible to work around for legacy code by wrapping string.* to
catch (%d+) in patterns and call tonumber() for given result position.

> I think it is really easy to disable this coercion in Lua; all it takes
> is a simple change in lvm.c (see below). It would be very helpful if
> people try that in the wild and report their experiences here in the
> list.

For 5.3, what about luaconf.h compat option enabled by default:

I'm not really sure about semantics of luaV_equalobj, but that
#if should definitely happen only in single place...

diff --git a/src/luaconf.h b/src/luaconf.h
index 3d6390a..dd10ed2 100644
--- a/src/luaconf.h
+++ b/src/luaconf.h
@@ -266,6 +266,11 @@
 #if defined(LUA_COMPAT_ALL)	/* { */
 
 /*
+@@ LUA_COMPAT_NUMBER_COERCION controls the auto-coercion of strings to numbers.
+*/
+//#define LUA_COMPAT_NUMBER_COERCION
+
+/*
 @@ LUA_COMPAT_BITLIB controls the presence of library 'bit32'.
 */
 #define LUA_COMPAT_BITLIB
diff --git a/src/lvm.c b/src/lvm.c
index a99e26b..4430ba9 100644
--- a/src/lvm.c
+++ b/src/lvm.c
@@ -44,7 +44,11 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
     return 1;
   }
   else
+#if defined LUA_COMPAT_NUMBER_COERCION
     return (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, n));
+#else
+    return 0;
+#endif
 }
 
 
@@ -222,7 +226,11 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
     else {  /* two numbers with different variants */
       lua_Number n1, n2;
       lua_assert(ttisnumber(t1) && ttisnumber(t2));
+#if defined LUA_COMPAT_NUMBER_COERCION
       cast_void(tonumber(t1, &n1)); cast_void(tonumber(t2, &n2));
+#else
+      n1 = cast_num(ivalue(t1)); n2 = cast_num(ivalue(t2));
+#endif
       return luai_numeq(n1, n2);
     }
   }
diff --git a/src/luaconf.h b/src/luaconf.h
index 3d6390a..dd10ed2 100644
--- a/src/luaconf.h
+++ b/src/luaconf.h
@@ -266,6 +266,11 @@
 #if defined(LUA_COMPAT_ALL)	/* { */
 
 /*
+@@ LUA_COMPAT_NUMBER_COERCION controls the auto-coercion of strings to numbers.
+*/
+//#define LUA_COMPAT_NUMBER_COERCION
+
+/*
 @@ LUA_COMPAT_BITLIB controls the presence of library 'bit32'.
 */
 #define LUA_COMPAT_BITLIB
diff --git a/src/lvm.c b/src/lvm.c
index a99e26b..4430ba9 100644
--- a/src/lvm.c
+++ b/src/lvm.c
@@ -44,7 +44,11 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
     return 1;
   }
   else
+#if defined LUA_COMPAT_NUMBER_COERCION
     return (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, n));
+#else
+    return 0;
+#endif
 }
 
 
@@ -222,7 +226,11 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
     else {  /* two numbers with different variants */
       lua_Number n1, n2;
       lua_assert(ttisnumber(t1) && ttisnumber(t2));
+#if defined LUA_COMPAT_NUMBER_COERCION
       cast_void(tonumber(t1, &n1)); cast_void(tonumber(t2, &n2));
+#else
+      n1 = cast_num(ivalue(t1)); n2 = cast_num(ivalue(t2));
+#endif
       return luai_numeq(n1, n2);
     }
   }