Author: fperrad
Date: Thu Mar  8 23:49:04 2007
New Revision: 17399

Modified:
   trunk/languages/lua/lib/luaos.pir
   trunk/languages/lua/pmc/lua.pmc
   trunk/languages/lua/t/os.t
   trunk/src/call_list.txt

Log:
[Lua]
- add Lua.strftime needed by os.date
- and test

Modified: trunk/languages/lua/lib/luaos.pir
==============================================================================
--- trunk/languages/lua/lib/luaos.pir   (original)
+++ trunk/languages/lua/lib/luaos.pir   Thu Mar  8 23:49:04 2007
@@ -122,8 +122,6 @@
 representation that depends on the host system and on the current locale
 (that is, C<os.date()> is equivalent to C<os.date("%c")>).
 
-STILL INCOMPLETE (see strftime).
-
 =cut
 
 .include 'tm.pasm'
@@ -193,18 +191,11 @@
     ret[$P1] = $P2
     .return (ret)
 L4:
-    $S0 = strftime($S1, $P0)
-    new ret, .LuaString
-    set ret, $P0
+    new $P1, .Lua
+    ret = $P1.'strftime'($S1, $P0)
     .return (ret)
 .end
 
-.sub 'strftime' :anon
-    .param string format
-    .param pmc t
-    not_implemented()
-.end
-
 
 =item C<os.difftime (t2, t1)>
 

Modified: trunk/languages/lua/pmc/lua.pmc
==============================================================================
--- trunk/languages/lua/pmc/lua.pmc     (original)
+++ trunk/languages/lua/pmc/lua.pmc     Thu Mar  8 23:49:04 2007
@@ -20,6 +20,10 @@
 
 #include "parrot/parrot.h"
 
+extern INTVAL dynpmc_LuaNil;
+extern INTVAL dynpmc_LuaNumber;
+extern INTVAL dynpmc_LuaString;
+
 static PMC * Lua_PMC;
 
 
@@ -61,12 +65,10 @@
         PMC *retval;
         int e;
 
-        y = pmc_new(INTERP, pmc_type(INTERP,
-              const_string(INTERP, "LuaNumber")));
-        expn = pmc_new(INTERP, pmc_type(INTERP,
-              const_string(INTERP, "LuaNumber")));
+        y = pmc_new(INTERP, dynpmc_LuaNumber);
+        expn = pmc_new(INTERP, dynpmc_LuaNumber);
         VTABLE_set_number_native(INTERP, y,
-              frexp(VTABLE_get_number(INTERP, x), &e));
+                                 frexp(VTABLE_get_number(INTERP, x), &e));
         VTABLE_set_integer_native(INTERP, expn, e);
         retval = pmc_new(INTERP, enum_class_Array);
         VTABLE_set_integer_native(INTERP, retval, 2);
@@ -83,11 +85,10 @@
 
 */
     METHOD PMC* ldexp(PMC* x, PMC* expn) {
-        PMC *retval = pmc_new(INTERP, pmc_type(INTERP,
-              const_string(INTERP, "LuaNumber")));
+        PMC *retval = pmc_new(INTERP, dynpmc_LuaNumber);
         VTABLE_set_number_native(INTERP, retval,
-              ldexp(VTABLE_get_number(INTERP, x),
-              VTABLE_get_integer(INTERP, expn)));
+                                 ldexp(VTABLE_get_number(INTERP, x),
+                                       VTABLE_get_integer(INTERP, expn)));
         return retval;
     }
 
@@ -104,12 +105,10 @@
         PMC *retval;
         FLOATVAL _d;
 
-        y = pmc_new(INTERP, pmc_type(INTERP,
-              const_string(INTERP, "LuaNumber")));
-        d = pmc_new(INTERP, pmc_type(INTERP,
-              const_string(INTERP, "LuaNumber")));
+        y = pmc_new(INTERP, dynpmc_LuaNumber);
+        d = pmc_new(INTERP, dynpmc_LuaNumber);
         VTABLE_set_number_native(INTERP, y,
-              modf(VTABLE_get_number(INTERP, x), &_d));
+                                 modf(VTABLE_get_number(INTERP, x), &_d));
         VTABLE_set_number_native(INTERP, d, _d);
         retval = pmc_new(INTERP, enum_class_Array);
         VTABLE_set_integer_native(INTERP, retval, 2);
@@ -118,6 +117,43 @@
         return retval;
     }
 
+/*
+
+=item C<PMC* strftime(STRING* fmt, PMC* tm)>
+
+=cut
+
+*/
+    METHOD PMC* strftime(STRING* fmt, PMC* tm) {
+        PMC *retval = NULL;
+        struct tm stm;
+        char b[256];
+        const char *s = string_to_cstring(INTERP, fmt);
+
+        stm.tm_sec   = VTABLE_get_integer_keyed_int(INTERP, tm, 0);
+        stm.tm_min   = VTABLE_get_integer_keyed_int(INTERP, tm, 1);
+        stm.tm_hour  = VTABLE_get_integer_keyed_int(INTERP, tm, 2);
+        stm.tm_mday  = VTABLE_get_integer_keyed_int(INTERP, tm, 3);
+        stm.tm_mon   = VTABLE_get_integer_keyed_int(INTERP, tm, 4) - 1;
+        stm.tm_year  = VTABLE_get_integer_keyed_int(INTERP, tm, 5) - 1900;
+        stm.tm_wday  = VTABLE_get_integer_keyed_int(INTERP, tm, 6);
+        stm.tm_yday  = VTABLE_get_integer_keyed_int(INTERP, tm, 7);
+        stm.tm_isdst = VTABLE_get_integer_keyed_int(INTERP, tm, 8);
+
+        if (strftime(b, sizeof b, s, &stm)) {
+            retval = pmc_new(INTERP, dynpmc_LuaString);
+            VTABLE_set_string_native(INTERP, retval,
+                                     string_from_cstring(INTERP, b, 0));
+        }
+        else {
+            PMC* func = Parrot_find_global_cur(INTERP,
+                                               const_string(INTERP, "error"));
+            STRING* msg = const_string(INTERP, "'date' format too long");
+            (void)Parrot_runops_fromc_args(INTERP, func, "vS", msg);
+        }
+        return retval;
+    }
+
 }
 
 /*

Modified: trunk/languages/lua/t/os.t
==============================================================================
--- trunk/languages/lua/t/os.t  (original)
+++ trunk/languages/lua/t/os.t  Thu Mar  8 23:49:04 2007
@@ -27,7 +27,7 @@
 use FindBin;
 use lib "$FindBin::Bin";
 
-use Parrot::Test tests => 17;
+use Parrot::Test tests => 18;
 use Test::More;
 
 language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'function os.date' );
@@ -39,6 +39,12 @@
 5      1       false
 OUTPUT
 
+language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'function os.date' );
+print(os.date("!%c", 0))
+CODE
+01/01/70 00:00:00
+OUTPUT
+
 language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'function os.difftime' );
 print(os.difftime(1234, 1200))
 print(os.difftime(1234))

Modified: trunk/src/call_list.txt
==============================================================================
--- trunk/src/call_list.txt     (original)
+++ trunk/src/call_list.txt     Thu Mar  8 23:49:04 2007
@@ -338,6 +338,7 @@
 
 # Make lua stop panic'ing.
 P      JOI
+P      JOSP
 
 # ParrotThread creation
 i      JOP@

Reply via email to