gjs r123 - in trunk: gi gjs test/js



Author: walters
Date: Mon Dec  1 18:19:18 2008
New Revision: 123
URL: http://svn.gnome.org/viewvc/gjs?rev=123&view=rev

Log:
Support time_t

This patch assumes that the host system uses "long" for time_t, which
is true for GNU systems.  Right now gobject-introspection also assumes
this.

http://bugzilla.gnome.org/show_bug.cgi?id=561812

Modified:
   trunk/gi/arg.c
   trunk/gjs/jsapi-util.c
   trunk/gjs/jsapi-util.h
   trunk/test/js/testEverythingBasic.js

Modified: trunk/gi/arg.c
==============================================================================
--- trunk/gi/arg.c	(original)
+++ trunk/gi/arg.c	Mon Dec  1 18:19:18 2008
@@ -359,6 +359,14 @@
     }
         break;
 
+    case GI_TYPE_TAG_TIME_T: {
+        double v;
+        if (!JS_ValueToNumber(context, value, &v))
+            wrong = TRUE;
+        arg->v_ulong = (unsigned long) (v/1000);
+    }
+        break;
+
     case GI_TYPE_TAG_BOOLEAN:
         if (!JS_ValueToBoolean(context, value, &arg->v_boolean))
             wrong = TRUE;
@@ -828,6 +836,11 @@
     case GI_TYPE_TAG_DOUBLE:
         return JS_NewDoubleValue(context, arg->v_double, value_p);
 
+    case GI_TYPE_TAG_TIME_T:
+        *value_p = gjs_date_from_time_t(context,
+                                        (time_t) arg->v_long);
+        return JS_TRUE;
+
     case GI_TYPE_TAG_FILENAME:
         if (arg->v_pointer)
             return gjs_string_from_filename(context, arg->v_pointer, -1, value_p);

Modified: trunk/gjs/jsapi-util.c
==============================================================================
--- trunk/gjs/jsapi-util.c	(original)
+++ trunk/gjs/jsapi-util.c	Mon Dec  1 18:19:18 2008
@@ -961,3 +961,34 @@
         return "<unknown>";
     }
 }
+
+jsval
+gjs_date_from_time_t (JSContext *context, time_t time)
+{
+    JSObject *date;
+    JSClass *date_class;
+    JSObject *date_constructor;
+    jsval date_prototype;
+    jsval args[1];
+
+    if (!JS_EnterLocalRootScope(context))
+        return JSVAL_VOID;
+
+    if (!JS_GetClassObject(context, JS_GetGlobalObject(context), JSProto_Date,
+                           &date_constructor))
+        gjs_fatal("Failed to lookup Date prototype");
+
+    if (!JS_GetProperty(context, date_constructor, "prototype", &date_prototype))
+        gjs_fatal("Failed to get prototype from Date constructor");
+
+    date_class = JS_GetClass(context, JSVAL_TO_OBJECT (date_prototype));
+
+    if (!JS_NewNumberValue(context, ((double) time) * 1000, &(args[0])))
+        gjs_fatal("Failed to convert time_t to number");
+
+    date = JS_ConstructObjectWithArguments(context, date_class,
+                                           NULL, NULL, 1, args);
+
+    JS_LeaveLocalRootScope(context);
+    return OBJECT_TO_JSVAL(date);
+}

Modified: trunk/gjs/jsapi-util.h
==============================================================================
--- trunk/gjs/jsapi-util.h	(original)
+++ trunk/gjs/jsapi-util.h	Mon Dec  1 18:19:18 2008
@@ -215,6 +215,8 @@
                                               const char     **name_p);
 const char* gjs_get_type_name                (jsval            value);
 
+jsval       gjs_date_from_time_t             (JSContext *context, time_t time);
+
 GjsRootedArray*   gjs_rooted_array_new        (void);
 void              gjs_rooted_array_append     (JSContext        *context,
                                                GjsRootedArray *array,

Modified: trunk/test/js/testEverythingBasic.js
==============================================================================
--- trunk/test/js/testEverythingBasic.js	(original)
+++ trunk/test/js/testEverythingBasic.js	Mon Dec  1 18:19:18 2008
@@ -38,6 +38,14 @@
     assertEquals(42, Everything.test_size(42));
     assertEquals(42, Everything.test_float(42));
     assertEquals(42, Everything.test_double(42));
+
+    let now = new Date();
+    let bounced = Everything.test_timet(now);
+    assertEquals(now.getFullYear(), bounced.getFullYear());
+    assertEquals(now.getMonth(), bounced.getMonth());
+    assertEquals(now.getDay(), bounced.getDay());
+    assertEquals(now.getHours(), bounced.getHours());
+    assertEquals(now.getSeconds(), bounced.getSeconds());
 }
 
 function testLimits() {



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