[Swfdec] Branch 'as' - 4 commits - libswfdec/swfdec_as_interpret.c libswfdec/swfdec_as_types.c libswfdec/swfdec_as_types.h

Benjamin Otte company at kemper.freedesktop.org
Wed Apr 11 12:10:02 PDT 2007


 libswfdec/swfdec_as_interpret.c |  129 +++++++++++++++++-----------------------
 libswfdec/swfdec_as_types.c     |   52 ++++++++++++++++
 libswfdec/swfdec_as_types.h     |    8 ++
 3 files changed, 116 insertions(+), 73 deletions(-)

New commits:
diff-tree bf5b82f30cd63edfe3e508e785583e6e4356cde9 (from 01beffe72234bcdfb4d3b33b5f30996413f3b933)
Author: Benjamin Otte <otte at gnome.org>
Date:   Wed Apr 11 21:09:52 2007 +0200

    implement And and Or actions

diff --git a/libswfdec/swfdec_as_interpret.c b/libswfdec/swfdec_as_interpret.c
index 249f9cc..3989186 100644
--- a/libswfdec/swfdec_as_interpret.c
+++ b/libswfdec/swfdec_as_interpret.c
@@ -1831,41 +1831,24 @@ swfdec_action_enumerate2 (SwfdecAsContex
   JS_DestroyIdArray (cx, array);
   return JS_TRUE;
 }
+#endif
 
 static void
-swfdec_action_logical_5 (SwfdecAsContext *cx, guint action, const guint8 *data, guint len)
-{
-  void l, r;
-
-  l = swfdec_value_to_boolean_5 (cx, cx->fp->sp[-1]);
-  r = swfdec_value_to_boolean_5 (cx, cx->fp->sp[-2]);
-
-  cx->fp->sp--;
-  if (action == 0x10)
-    cx->fp->sp[-1] = l && r ? JSVAL_TRUE : JSVAL_FALSE;
-  else
-    cx->fp->sp[-1] = l || r ? JSVAL_TRUE : JSVAL_FALSE;
-  return JS_TRUE;
-}
-
-static void
-swfdec_action_logical_7 (SwfdecAsContext *cx, guint action, const guint8 *data, guint len)
+swfdec_action_logical (SwfdecAsContext *cx, guint action, const guint8 *data, guint len)
 {
-  void l, r;
+  SwfdecAsValue *val;
+  gboolean l, r;
 
-  l = swfdec_value_to_boolean_7 (cx, cx->fp->sp[-1]);
-  r = swfdec_value_to_boolean_7 (cx, cx->fp->sp[-2]);
+  l = swfdec_as_value_to_boolean (cx, swfdec_as_stack_pop (cx->frame->stack));
+  val = swfdec_as_stack_peek (cx->frame->stack, 1);
+  r = swfdec_as_value_to_boolean (cx, val);
 
-  cx->fp->sp--;
-  if (action == 0x10)
-    cx->fp->sp[-1] = l && r ? JSVAL_TRUE : JSVAL_FALSE;
-  else
-    cx->fp->sp[-1] = l || r ? JSVAL_TRUE : JSVAL_FALSE;
-  return JS_TRUE;
+  SWFDEC_AS_VALUE_SET_BOOLEAN (val, (action == 0x10) ? (l && r) : (l || r));
 }
 
 /*** PRINT FUNCTIONS ***/
 
+#if 0
 static char *
 swfdec_action_print_store_register (guint action, const guint8 *data, guint len)
 {
@@ -2168,9 +2151,9 @@ const SwfdecActionSpec swfdec_as_actions
 #if 0
   [0x0e] = { "Equals", NULL, 2, 1, { NULL, swfdec_action_old_compare, swfdec_action_old_compare, swfdec_action_old_compare, swfdec_action_old_compare } },
   [0x0f] = { "Less", NULL, 2, 1, { NULL, swfdec_action_old_compare, swfdec_action_old_compare, swfdec_action_old_compare, swfdec_action_old_compare } },
-  [0x10] = { "And", NULL, 2, 1, { NULL, /* FIXME */NULL, swfdec_action_logical_5, swfdec_action_logical_5, swfdec_action_logical_7 } },
-  [0x11] = { "Or", NULL, 2, 1, { NULL, /* FIXME */NULL, swfdec_action_logical_5, swfdec_action_logical_5, swfdec_action_logical_7 } },
 #endif
+  [SWFDEC_AS_ACTION_AND] = { "And", NULL, 2, 1, { NULL, /* FIXME */NULL, swfdec_action_logical, swfdec_action_logical, swfdec_action_logical } },
+  [SWFDEC_AS_ACTION_OR] = { "Or", NULL, 2, 1, { NULL, /* FIXME */NULL, swfdec_action_logical, swfdec_action_logical, swfdec_action_logical } },
   [SWFDEC_AS_ACTION_NOT] = { "Not", NULL, 1, 1, { NULL, swfdec_action_not_4, swfdec_action_not_5, swfdec_action_not_5, swfdec_action_not_5 } },
 #if 0
   [0x13] = { "StringEquals", NULL },
diff-tree 01beffe72234bcdfb4d3b33b5f30996413f3b933 (from a8742b1e3de562504c5980a9477d89aed83819ab)
Author: Benjamin Otte <otte at gnome.org>
Date:   Wed Apr 11 21:09:20 2007 +0200

    new function swfdec_as_value_to_boolean

diff --git a/libswfdec/swfdec_as_types.c b/libswfdec/swfdec_as_types.c
index 2446681..ee8c403 100644
--- a/libswfdec/swfdec_as_types.c
+++ b/libswfdec/swfdec_as_types.c
@@ -227,6 +227,9 @@ swfdec_as_value_to_integer (SwfdecAsCont
 SwfdecAsObject *
 swfdec_as_value_to_object (SwfdecAsContext *context, const SwfdecAsValue *value)
 {
+  g_return_val_if_fail (SWFDEC_IS_AS_CONTEXT (context), NULL);
+  g_return_val_if_fail (SWFDEC_IS_AS_VALUE (value), NULL);
+
   switch (value->type) {
     case SWFDEC_TYPE_AS_UNDEFINED:
     case SWFDEC_TYPE_AS_NULL:
@@ -244,3 +247,46 @@ swfdec_as_value_to_object (SwfdecAsConte
   }
 }
 
+/**
+ * swfdec_as_value_to_boolean:
+ * @context: a #SwfdecAsContext
+ * @value: value to convert
+ *
+ * Converts the given value to a boolean according to Flash's rules. Note that
+ * these rules changed significantly for strings between Flash 6 and 7.
+ *
+ * Returns: either %TRUE or %FALSE.
+ **/
+gboolean
+swfdec_as_value_to_boolean (SwfdecAsContext *context, const SwfdecAsValue *value)
+{
+  g_return_val_if_fail (SWFDEC_IS_AS_CONTEXT (context), FALSE);
+  g_return_val_if_fail (SWFDEC_IS_AS_VALUE (value), FALSE);
+
+  /* FIXME: what do we do when called in flash 4? */
+  switch (value->type) {
+    case SWFDEC_TYPE_AS_UNDEFINED:
+    case SWFDEC_TYPE_AS_NULL:
+      return FALSE;
+    case SWFDEC_TYPE_AS_BOOLEAN:
+      return SWFDEC_AS_VALUE_GET_BOOLEAN (value);
+    case SWFDEC_TYPE_AS_NUMBER:
+      {
+	double d = SWFDEC_AS_VALUE_GET_NUMBER (value);
+	return d != 0.0 && !isnan (d);
+      }
+    case SWFDEC_TYPE_AS_STRING:
+      if (context->version <= 6) {
+	double d = swfdec_as_value_to_number (context, value);
+	return d != 0.0 && !isnan (d);
+      } else {
+	return SWFDEC_AS_VALUE_GET_STRING (value) != SWFDEC_AS_STR_EMPTY;
+      }
+    case SWFDEC_TYPE_AS_ASOBJECT:
+      return TRUE;
+    default:
+      g_assert_not_reached ();
+      return FALSE;
+  }
+}
+
diff --git a/libswfdec/swfdec_as_types.h b/libswfdec/swfdec_as_types.h
index dc390cd..10f3a9c 100644
--- a/libswfdec/swfdec_as_types.h
+++ b/libswfdec/swfdec_as_types.h
@@ -265,6 +265,8 @@ typedef enum {
   SWFDEC_AS_ACTION_GOTO_FRAME2 = 0x9F
 } SwfdecAsAction;
 
+gboolean	swfdec_as_value_to_boolean	(SwfdecAsContext *	context,
+						 const SwfdecAsValue *	value);
 int		swfdec_as_value_to_integer	(SwfdecAsContext *	context,
 						 const SwfdecAsValue *	value);
 double		swfdec_as_value_to_number	(SwfdecAsContext *	context,
diff-tree a8742b1e3de562504c5980a9477d89aed83819ab (from b2d93e32881e59cee6f5cb189b10014a0f2b9ee5)
Author: Benjamin Otte <otte at gnome.org>
Date:   Wed Apr 11 20:50:52 2007 +0200

    reenabled TypeOf action
    
    untested

diff --git a/libswfdec/swfdec_as_interpret.c b/libswfdec/swfdec_as_interpret.c
index 2421fbd..249f9cc 100644
--- a/libswfdec/swfdec_as_interpret.c
+++ b/libswfdec/swfdec_as_interpret.c
@@ -1719,46 +1719,50 @@ swfdec_action_to_string (SwfdecAsContext
       swfdec_as_value_to_string (cx, swfdec_as_stack_peek (cx->frame->stack, 1)));
 }
 
-#if 0
 static void
 swfdec_action_type_of (SwfdecAsContext *cx, guint action, const guint8 *data, guint len)
 {
-  jsval val;
+  SwfdecAsValue *val;
   const char *type;
-  JSString *string;
 
-  val = cx->fp->sp[-1];
-  if (JSVAL_IS_NUMBER (val)) {
-    type = "number";
-  } else if (JSVAL_IS_BOOLEAN (val)) {
-    type = "boolean";
-  } else if (JSVAL_IS_STRING (val)) {
-    type = "string";
-  } else if (JSVAL_IS_VOID (val)) {
-    type = "undefined";
-  } else if (JSVAL_IS_NULL (val)) {
-    type = "null";
-  } else if (JSVAL_IS_OBJECT (val)) {
-    JSObject *obj = JSVAL_TO_OBJECT (val);
-    if (swfdec_js_is_movieclip (cx, obj)) {
-      type = "movieclip";
-    } else if (JS_ObjectIsFunction (cx, obj)) {
-      type = "function";
-    } else {
-      type = "object";
-    }
-  } else {
-    g_assert_not_reached ();
-    return JS_FALSE;
+  val = swfdec_as_stack_peek (cx->frame->stack, 1);
+  switch (val->type) {
+    case SWFDEC_TYPE_AS_NUMBER:
+      type = SWFDEC_AS_STR_NUMBER;
+      break;
+    case SWFDEC_TYPE_AS_BOOLEAN:
+      type = SWFDEC_AS_STR_BOOLEAN;
+      break;
+    case SWFDEC_TYPE_AS_STRING:
+      type = SWFDEC_AS_STR_STRING;
+      break;
+    case SWFDEC_TYPE_AS_UNDEFINED:
+      type = SWFDEC_AS_STR_UNDEFINED;
+      break;
+    case SWFDEC_TYPE_AS_NULL:
+      type = SWFDEC_AS_STR_NULL;
+      break;
+    case SWFDEC_TYPE_AS_ASOBJECT:
+      {
+	SwfdecAsObject *obj = SWFDEC_AS_VALUE_GET_OBJECT (val);
+	if (SWFDEC_IS_MOVIE (obj)) {
+	  type = SWFDEC_AS_STR_MOVIECLIP;
+	} else if (SWFDEC_IS_AS_FUNCTION (obj)) {
+	  type = SWFDEC_AS_STR_FUNCTION;
+	} else {
+	  type = SWFDEC_AS_STR_OBJECT;
+	}
+      }
+      break;
+    default:
+      g_assert_not_reached ();
+      type = SWFDEC_AS_STR_EMPTY;
+      break;
   }
-  /* can't use InternString here because of case sensitivity issues */
-  string = JS_NewStringCopyZ (cx, type);
-  if (string == NULL)
-    return JS_FALSE;
-  cx->fp->sp[-1] = STRING_TO_JSVAL (string);
-  return JS_TRUE;
+  SWFDEC_AS_VALUE_SET_STRING (val, type);
 }
 
+#if 0
 static void
 swfdec_action_get_time (SwfdecAsContext *cx, guint action, const guint8 *data, guint len)
 {
@@ -2218,7 +2222,9 @@ const SwfdecActionSpec swfdec_as_actions
   [0x41] = { "DefineLocal2", NULL, 1, 0, { NULL, NULL, swfdec_action_define_local2, swfdec_action_define_local2, swfdec_action_define_local2 } },
   [0x42] = { "InitArray", NULL, -1, 1, { NULL, NULL, swfdec_action_init_array, swfdec_action_init_array, swfdec_action_init_array } },
   [0x43] = { "InitObject", NULL, -1, 1, { NULL, NULL, swfdec_action_init_object, swfdec_action_init_object, swfdec_action_init_object } },
-  [0x44] = { "TypeOf", NULL, 1, 1, { NULL, NULL, swfdec_action_type_of, swfdec_action_type_of, swfdec_action_type_of } },
+#endif
+  [SWFDEC_AS_ACTION_TYPE_OF] = { "TypeOf", NULL, 1, 1, { NULL, NULL, swfdec_action_type_of, swfdec_action_type_of, swfdec_action_type_of } },
+#if 0
   [0x45] = { "TargetPath", NULL, 1, 1, { NULL, NULL, swfdec_action_target_path, swfdec_action_target_path, swfdec_action_target_path } },
   [0x46] = { "Enumerate", NULL },
 #endif
diff --git a/libswfdec/swfdec_as_types.c b/libswfdec/swfdec_as_types.c
index b39f592..2446681 100644
--- a/libswfdec/swfdec_as_types.c
+++ b/libswfdec/swfdec_as_types.c
@@ -92,6 +92,12 @@ const char *swfdec_as_strings[] = {
   SWFDEC_AS_CONSTANT_STRING ("_xmouse"), 
   SWFDEC_AS_CONSTANT_STRING ("_ymouse"),
   SWFDEC_AS_CONSTANT_STRING ("#ERROR#"),
+  SWFDEC_AS_CONSTANT_STRING ("number"),
+  SWFDEC_AS_CONSTANT_STRING ("boolean"),
+  SWFDEC_AS_CONSTANT_STRING ("string"),
+  SWFDEC_AS_CONSTANT_STRING ("movieclip"),
+  SWFDEC_AS_CONSTANT_STRING ("function"),
+  SWFDEC_AS_CONSTANT_STRING ("object"),
   /* add more here */
   NULL
 };
diff --git a/libswfdec/swfdec_as_types.h b/libswfdec/swfdec_as_types.h
index 22b6bde..dc390cd 100644
--- a/libswfdec/swfdec_as_types.h
+++ b/libswfdec/swfdec_as_types.h
@@ -159,6 +159,12 @@ extern const char *swfdec_as_strings[];
 #define SWFDEC_AS_STR__XMOUSE		SWFDEC_AS_STR_CONSTANT(59)
 #define SWFDEC_AS_STR__YMOUSE		SWFDEC_AS_STR_CONSTANT(60)
 #define SWFDEC_AS_STR_HASH_ERROR	SWFDEC_AS_STR_CONSTANT(61)
+#define SWFDEC_AS_STR_NUMBER		SWFDEC_AS_STR_CONSTANT(62)
+#define SWFDEC_AS_STR_BOOLEAN		SWFDEC_AS_STR_CONSTANT(63)
+#define SWFDEC_AS_STR_STRING		SWFDEC_AS_STR_CONSTANT(64)
+#define SWFDEC_AS_STR_MOVIECLIP		SWFDEC_AS_STR_CONSTANT(65)
+#define SWFDEC_AS_STR_FUNCTION		SWFDEC_AS_STR_CONSTANT(66)
+#define SWFDEC_AS_STR_OBJECT		SWFDEC_AS_STR_CONSTANT(67)
 
 /* all existing actions */
 typedef enum {
diff-tree b2d93e32881e59cee6f5cb189b10014a0f2b9ee5 (from 7f254e5e30a3907a6e8418159d1f62914cd988e9)
Author: Benjamin Otte <otte at gnome.org>
Date:   Wed Apr 11 19:58:44 2007 +0200

    reenable ToNumber and ToString code
    
    untested...

diff --git a/libswfdec/swfdec_as_interpret.c b/libswfdec/swfdec_as_interpret.c
index a679c0a..2421fbd 100644
--- a/libswfdec/swfdec_as_interpret.c
+++ b/libswfdec/swfdec_as_interpret.c
@@ -1705,27 +1705,21 @@ swfdec_action_swap (SwfdecAsContext *cx,
   *swfdec_as_stack_peek (cx->frame->stack, 2) = val;
 }
 
-#if 0
 static void
 swfdec_action_to_number (SwfdecAsContext *cx, guint action, const guint8 *data, guint len)
 {
-  double d;
-  if (!JS_ValueToNumber (cx, cx->fp->sp[-1], &d))
-    return JS_FALSE;
-  return JS_NewNumberValue (cx, d, &cx->fp->sp[-1]);
+  SWFDEC_AS_VALUE_SET_NUMBER (swfdec_as_stack_peek (cx->frame->stack, 1),
+      swfdec_as_value_to_number (cx, swfdec_as_stack_peek (cx->frame->stack, 1)));
 }
 
 static void
 swfdec_action_to_string (SwfdecAsContext *cx, guint action, const guint8 *data, guint len)
 {
-  JSString *s;
-  s = JS_ValueToString(cx, cx->fp->sp[-1]);
-  if (!s)
-    return JS_FALSE;
-  cx->fp->sp[-1] = STRING_TO_JSVAL (s);
-  return JS_TRUE;
+  SWFDEC_AS_VALUE_SET_STRING (swfdec_as_stack_peek (cx->frame->stack, 1),
+      swfdec_as_value_to_string (cx, swfdec_as_stack_peek (cx->frame->stack, 1)));
 }
 
+#if 0
 static void
 swfdec_action_type_of (SwfdecAsContext *cx, guint action, const guint8 *data, guint len)
 {
@@ -2232,9 +2226,9 @@ const SwfdecActionSpec swfdec_as_actions
 #if 0
   [0x48] = { "Less2", NULL, 2, 1, { NULL, NULL, swfdec_action_new_comparison_6, swfdec_action_new_comparison_6, swfdec_action_new_comparison_7 } },
   [0x49] = { "Equals2", NULL, 2, 1, { NULL, NULL, swfdec_action_equals2, swfdec_action_equals2, swfdec_action_equals2 } },
-  [0x4a] = { "ToNumber", NULL, 1, 1, { NULL, NULL, swfdec_action_to_number, swfdec_action_to_number, swfdec_action_to_number } },
-  [0x4b] = { "ToString", NULL, 1, 1, { NULL, NULL, swfdec_action_to_string, swfdec_action_to_string, swfdec_action_to_string } },
 #endif
+  [SWFDEC_AS_ACTION_TO_NUMBER] = { "ToNumber", NULL, 1, 1, { NULL, NULL, swfdec_action_to_number, swfdec_action_to_number, swfdec_action_to_number } },
+  [SWFDEC_AS_ACTION_TO_STRING] = { "ToString", NULL, 1, 1, { NULL, NULL, swfdec_action_to_string, swfdec_action_to_string, swfdec_action_to_string } },
   [SWFDEC_AS_ACTION_PUSH_DUPLICATE] = { "PushDuplicate", NULL, 1, 2, { NULL, NULL, swfdec_action_push_duplicate, swfdec_action_push_duplicate, swfdec_action_push_duplicate } },
   [SWFDEC_AS_ACTION_SWAP] = { "Swap", NULL, 2, 2, { NULL, NULL, swfdec_action_swap, swfdec_action_swap, swfdec_action_swap } },
   [SWFDEC_AS_ACTION_GET_MEMBER] = { "GetMember", NULL, 2, 1, { NULL, swfdec_action_get_member, swfdec_action_get_member, swfdec_action_get_member, swfdec_action_get_member } },


More information about the Swfdec mailing list