COMPOSITE => OBJECT in swfdec_as_relay_call()
[swfdec.git] / swfdec / swfdec_as_relay.c
blob2123aa1f29afd81f36c4b8b9ef3699f85e865300
1 /* Swfdec
2 * Copyright (C) 2008 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include <string.h>
26 #include "swfdec_as_relay.h"
27 #include "swfdec_as_context.h"
28 #include "swfdec_as_object.h"
29 #include "swfdec_as_function.h"
31 G_DEFINE_ABSTRACT_TYPE (SwfdecAsRelay, swfdec_as_relay, SWFDEC_TYPE_GC_OBJECT)
33 static void
34 swfdec_as_relay_mark (SwfdecGcObject *object)
36 SwfdecAsRelay *relay = SWFDEC_AS_RELAY (object);
38 if (relay->relay)
39 swfdec_gc_object_mark (relay->relay);
41 SWFDEC_GC_OBJECT_CLASS (swfdec_as_relay_parent_class)->mark (object);
44 static void
45 swfdec_as_relay_class_init (SwfdecAsRelayClass *klass)
47 SwfdecGcObjectClass *gc_class = SWFDEC_GC_OBJECT_CLASS (klass);
49 gc_class->mark = swfdec_as_relay_mark;
52 static void
53 swfdec_as_relay_init (SwfdecAsRelay *object)
57 /**
58 * swfdec_as_relay_get_as_object:
59 * @object: a #SwfdecAsRelay.
61 * Gets the Actionscript object associated with this object.
63 * Returns: The #SwfdecAsObject associated with this relay.
64 **/
65 SwfdecAsObject *
66 swfdec_as_relay_get_as_object (SwfdecAsRelay *relay)
68 g_return_val_if_fail (SWFDEC_IS_AS_RELAY (relay), NULL);
69 g_return_val_if_fail (relay->relay != NULL, NULL);
71 return relay->relay;
74 /**
75 * swfdec_as_relay_call:
76 * @object: a #SwfdecAsRelay
77 * @name: garbage-collected string naming the function to call.
78 * @argc: number of arguments to provide to function
79 * @argv: arguments or %NULL when @argc is 0
80 * @return_value: location to take the return value of the call or %NULL to
81 * ignore the return value.
83 * Calls the function named @name on the given object. This function is
84 * essentially equal to the folloeing Actionscript code:
85 * <informalexample><programlisting>
86 * @return_value = @object.@name (@argv[0], ..., @argv[argc-1]);
87 * </programlisting></informalexample>
89 * Returns: %TRUE if @object had a function with the given name, %FALSE otherwise
90 **/
91 gboolean
92 swfdec_as_relay_call (SwfdecAsRelay *relay, const char *name, guint argc,
93 SwfdecAsValue *argv, SwfdecAsValue *return_value)
95 SwfdecAsValue tmp;
96 SwfdecAsFunction *fun;
98 g_return_val_if_fail (SWFDEC_IS_AS_RELAY (relay), TRUE);
99 g_return_val_if_fail (name != NULL, TRUE);
100 g_return_val_if_fail (argc == 0 || argv != NULL, TRUE);
101 g_return_val_if_fail (swfdec_gc_object_get_context (relay)->global != NULL, TRUE); /* for SwfdecPlayer */
103 /* If this doesn't hold, we need to use swfdec_as_relay_get_as_object()
104 * and have that function create the relay on demand. */
105 g_assert (relay->relay);
107 if (return_value)
108 SWFDEC_AS_VALUE_SET_UNDEFINED (return_value);
109 swfdec_as_object_get_variable (relay->relay, name, &tmp);
110 if (!SWFDEC_AS_VALUE_IS_OBJECT (&tmp))
111 return FALSE;
112 fun = (SwfdecAsFunction *) (SWFDEC_AS_VALUE_GET_OBJECT (&tmp)->relay);
113 if (!SWFDEC_IS_AS_FUNCTION (fun))
114 return FALSE;
115 swfdec_as_function_call (fun, relay->relay, argc, argv, return_value ? return_value : &tmp);
117 return TRUE;