[seed] libseed: Improve integral type conversions; mostly portability issues



commit 251a556dc841a8165d865771d9f30de94428d901
Author: Iain Nicol <iain thenicols net>
Date:   Thu Nov 19 13:03:35 2009 +0000

    libseed: Improve integral type conversions; mostly portability issues
    
    Partial fix for BGO #602404.

 libseed/seed-closure.c |   34 +++++++++++----
 libseed/seed-types.c   |  113 +++++++++++++++++++++++++++++++++++++++++++++---
 libseed/seed-types.h   |   10 ++++
 3 files changed, 142 insertions(+), 15 deletions(-)
---
diff --git a/libseed/seed-closure.c b/libseed/seed-closure.c
index 2f3c3fd..766de5a 100644
--- a/libseed/seed-closure.c
+++ b/libseed/seed-closure.c
@@ -98,20 +98,28 @@ seed_handle_closure (ffi_cif * cif, void *result, void **args, void *userdata)
 	  arg->v_uint32 = *(guint32 *) args[i];
 	  break;
 	case GI_TYPE_TAG_LONG:
+	  arg->v_long = *(glong *) args[i];
+	  break;
 	case GI_TYPE_TAG_INT64:
-	  arg->v_int64 = *(glong *) args[i];
+	  arg->v_int64 = *(gint64 *) args[i];
 	  break;
 	case GI_TYPE_TAG_ULONG:
+	  arg->v_ulong = *(gulong *) args[i];
+	  break;
 	case GI_TYPE_TAG_UINT64:
-	  arg->v_uint64 = *(glong *) args[i];
+	  arg->v_uint64 = *(guint64 *) args[i];
 	  break;
 	case GI_TYPE_TAG_INT:
+	  arg->v_int = *(gint *) args[i];
+	  break;
 	case GI_TYPE_TAG_SSIZE:
+	  arg->v_ssize = *(gssize *) args[i];
+	  break;
 	case GI_TYPE_TAG_SIZE:
-	  arg->v_int32 = *(gint *) args[i];
+	  arg->v_size = *(gsize *) args[i];
 	  break;
 	case GI_TYPE_TAG_UINT:
-	  arg->v_uint32 = *(guint *) args[i];
+	  arg->v_uint = *(guint *) args[i];
 	  break;
 	case GI_TYPE_TAG_FLOAT:
 	  arg->v_float = *(gfloat *) args[i];
@@ -199,7 +207,7 @@ seed_handle_closure (ffi_cif * cif, void *result, void **args, void *userdata)
       *(gint16 *) result = return_arg.v_int16;
       break;
     case GI_TYPE_TAG_UINT16:
-      return_arg.v_uint16 = *(guint16 *) args[i];
+      *(guint16 *) result = return_arg.v_uint16;
       break;
     case GI_TYPE_TAG_INT32:
       *(gint32 *) result = return_arg.v_int32;
@@ -208,20 +216,28 @@ seed_handle_closure (ffi_cif * cif, void *result, void **args, void *userdata)
       *(guint32 *) result = return_arg.v_uint32;
       break;
     case GI_TYPE_TAG_LONG:
+      *(glong *) result = return_arg.v_long;
+      break;
     case GI_TYPE_TAG_INT64:
-      *(glong *) result = return_arg.v_int64;
+      *(gint64 *) result = return_arg.v_int64;
       break;
     case GI_TYPE_TAG_ULONG:
+      *(gulong *) result = return_arg.v_ulong;
+      break;
     case GI_TYPE_TAG_UINT64:
-      *(glong *) result = return_arg.v_uint64;
+      *(guint64 *) result = return_arg.v_uint64;
       break;
     case GI_TYPE_TAG_INT:
+      *(gint *) result = return_arg.v_int;
+      break;
     case GI_TYPE_TAG_SSIZE:
+      *(gssize *) result = return_arg.v_ssize;
+      break;
     case GI_TYPE_TAG_SIZE:
-      *(gint *) result = return_arg.v_int32;
+      *(gsize *) result = return_arg.v_size;
       break;
     case GI_TYPE_TAG_UINT:
-      *(guint *) result = return_arg.v_uint32;
+      *(guint *) result = return_arg.v_uint;
       break;
     case GI_TYPE_TAG_FLOAT:
       *(gfloat *) result = return_arg.v_float;
diff --git a/libseed/seed-types.c b/libseed/seed-types.c
index d5a9ea2..689e4cc 100644
--- a/libseed/seed-types.c
+++ b/libseed/seed-types.c
@@ -485,12 +485,16 @@ seed_gi_make_argument (JSContextRef ctx,
       arg->v_uint32 = seed_value_to_uint (ctx, value, exception);
       break;
     case GI_TYPE_TAG_LONG:
+      arg->v_long = seed_value_to_long (ctx, value, exception);
+      break;
     case GI_TYPE_TAG_INT64:
-      arg->v_int64 = seed_value_to_long (ctx, value, exception);
+      arg->v_int64 = seed_value_to_int64 (ctx, value, exception);
       break;
     case GI_TYPE_TAG_ULONG:
+      arg->v_ulong = seed_value_to_ulong (ctx, value, exception);
+      break;
     case GI_TYPE_TAG_UINT64:
-      arg->v_uint64 = seed_value_to_ulong (ctx, value, exception);
+      arg->v_uint64 = seed_value_to_uint64 (ctx, value, exception);
       break;
     case GI_TYPE_TAG_INT:
       arg->v_int = seed_value_to_int (ctx, value, exception);
@@ -499,8 +503,10 @@ seed_gi_make_argument (JSContextRef ctx,
       arg->v_uint = seed_value_to_uint (ctx, value, exception);
       break;
     case GI_TYPE_TAG_SIZE:
+      arg->v_size = seed_value_to_size (ctx, value, exception);
+      break;
     case GI_TYPE_TAG_SSIZE:
-      arg->v_int = seed_value_to_int (ctx, value, exception);
+      arg->v_ssize = seed_value_to_ssize (ctx, value, exception);
       break;
     case GI_TYPE_TAG_FLOAT:
       arg->v_float = seed_value_to_float (ctx, value, exception);
@@ -758,18 +764,21 @@ seed_gi_argument_make_js (JSContextRef ctx,
     case GI_TYPE_TAG_UINT32:
       return seed_value_from_uint (ctx, arg->v_uint32, exception);
     case GI_TYPE_TAG_LONG:
+      return seed_value_from_long (ctx, arg->v_long, exception);
     case GI_TYPE_TAG_INT64:
-      return seed_value_from_long (ctx, arg->v_int64, exception);
+      return seed_value_from_int64 (ctx, arg->v_int64, exception);
     case GI_TYPE_TAG_ULONG:
+      return seed_value_from_ulong (ctx, arg->v_ulong, exception);
     case GI_TYPE_TAG_UINT64:
-      return seed_value_from_ulong (ctx, arg->v_uint64, exception);
+      return seed_value_from_uint64 (ctx, arg->v_uint64, exception);
     case GI_TYPE_TAG_INT:
       return seed_value_from_int (ctx, arg->v_int32, exception);
     case GI_TYPE_TAG_UINT:
       return seed_value_from_uint (ctx, arg->v_uint32, exception);
     case GI_TYPE_TAG_SSIZE:
+      return seed_value_from_ssize (ctx, arg->v_ssize, exception);
     case GI_TYPE_TAG_SIZE:
-      return seed_value_from_int (ctx, arg->v_int, exception);
+      return seed_value_from_size (ctx, arg->v_size, exception);
     case GI_TYPE_TAG_FLOAT:
       return seed_value_from_float (ctx, arg->v_float, exception);
     case GI_TYPE_TAG_DOUBLE:
@@ -1928,6 +1937,98 @@ seed_value_from_double (JSContextRef ctx, gdouble val, JSValueRef * exception)
 }
 
 /**
+ * seed_value_to_size:
+ * @ctx: A #SeedContext.
+ * @val: The #SeedValue to convert.
+ * @exception: A reference to a #SeedValue in which to store any exceptions.
+ *             Pass %NULL to ignore exceptions.
+ *
+ * Converts the given #SeedValue into a #gsize.
+ *
+ * Return value: The #gsize represented by @val, or %NULL if an exception
+ *               is raised during the conversion.
+ *
+ */
+gsize
+seed_value_to_size (JSContextRef ctx, JSValueRef val, JSValueRef * exception)
+{
+  if (!JSValueIsNumber (ctx, val) && !JSValueIsBoolean (ctx, val))
+    {
+      if (!JSValueIsNull (ctx, val))
+	seed_make_exception (ctx, exception, "ConversionError",
+			     "Can not convert Javascript value to" " gsize");
+      return 0;
+    }
+
+  return (gsize) JSValueToNumber (ctx, val, NULL);
+}
+
+/**
+ * seed_value_from_size:
+ * @ctx: A #SeedContext.
+ * @val: The #gsize to represent.
+ * @exception: A reference to a #SeedValue in which to store any exceptions.
+ *             Pass %NULL to ignore exceptions.
+ *
+ * Converts the given #gsize into a #SeedValue.
+ *
+ * Return value: A #SeedValue which represents @val, or %NULL if an exception
+ *               is raised during the conversion.
+ *
+ */
+JSValueRef
+seed_value_from_size (JSContextRef ctx, gsize val, JSValueRef * exception)
+{
+  return JSValueMakeNumber (ctx, (gdouble) val);
+}
+
+/**
+ * seed_value_to_ssize:
+ * @ctx: A #SeedContext.
+ * @val: The #SeedValue to convert.
+ * @exception: A reference to a #SeedValue in which to store any exceptions.
+ *             Pass %NULL to ignore exceptions.
+ *
+ * Converts the given #SeedValue into a #gssize.
+ *
+ * Return value: The #gssize represented by @val, or %NULL if an exception
+ *               is raised during the conversion.
+ *
+ */
+gssize
+seed_value_to_ssize (JSContextRef ctx, JSValueRef val, JSValueRef * exception)
+{
+  if (!JSValueIsNumber (ctx, val) && !JSValueIsBoolean (ctx, val))
+    {
+      if (!JSValueIsNull (ctx, val))
+	seed_make_exception (ctx, exception, "ConversionError",
+			     "Can not convert Javascript value to" " gssize");
+      return 0;
+    }
+
+  return (gssize) JSValueToNumber (ctx, val, NULL);
+}
+
+/**
+ * seed_value_from_ssize:
+ * @ctx: A #SeedContext.
+ * @val: The #gssize to represent.
+ * @exception: A reference to a #SeedValue in which to store any exceptions.
+ *             Pass %NULL to ignore exceptions.
+ *
+ * Converts the given #gssize into a #SeedValue.
+ *
+ * Return value: A #SeedValue which represents @val, or %NULL if an exception
+ *               is raised during the conversion.
+ *
+ */
+JSValueRef
+seed_value_from_ssize (JSContextRef ctx, gssize val, JSValueRef * exception)
+{
+  return JSValueMakeNumber (ctx, (gdouble) val);
+}
+
+/**
  * seed_value_to_string:
  * @ctx: A #SeedContext.
  * @val: The #SeedValue to convert.
diff --git a/libseed/seed-types.h b/libseed/seed-types.h
index 55ceb80..eda08a4 100644
--- a/libseed/seed-types.h
+++ b/libseed/seed-types.h
@@ -109,6 +109,16 @@ gdouble seed_value_to_double (JSContextRef ctx,
 JSValueRef seed_value_from_double (JSContextRef ctx,
 				   gdouble val, JSValueRef * exception);
 
+gsize seed_value_to_size (JSContextRef ctx,
+			  JSValueRef val, JSValueRef * exception);
+JSValueRef seed_value_from_size (JSContextRef ctx,
+				 gsize val, JSValueRef * exception);
+
+gssize seed_value_to_ssize (JSContextRef ctx,
+			    JSValueRef val, JSValueRef * exception);
+JSValueRef seed_value_from_ssize (JSContextRef ctx,
+				  gssize val, JSValueRef * exception);
+
 gchar *seed_value_to_filename (JSContextRef ctx,
 			       JSValueRef val, JSValueRef * exception);
 JSValueRef seed_value_from_filename (JSContextRef ctx,



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