fix jsut committed crasher by rewriting startDrag action
[swfdec.git] / libswfdec / swfdec_as_script_function.c
blob1abf45cfa4aedf966c79d90795f31e4f5c704789
1 /* Swfdec
2 * Copyright (C) 2007 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 "swfdec_as_script_function.h"
25 #include "swfdec_as_context.h"
26 #include "swfdec_as_frame_internal.h"
27 #include "swfdec_as_internal.h"
28 #include "swfdec_as_stack.h"
29 #include "swfdec_as_strings.h"
30 #include "swfdec_debug.h"
32 G_DEFINE_TYPE (SwfdecAsScriptFunction, swfdec_as_script_function, SWFDEC_TYPE_AS_FUNCTION)
34 static SwfdecAsFrame *
35 swfdec_as_script_function_call (SwfdecAsFunction *function)
37 SwfdecAsScriptFunction *script = SWFDEC_AS_SCRIPT_FUNCTION (function);
38 SwfdecAsFrame *frame;
40 frame = swfdec_as_frame_new (SWFDEC_AS_OBJECT (function)->context, script->script);
41 if (frame == NULL)
42 return NULL;
43 SWFDEC_AS_SCOPE (frame)->next = script->scope;
44 frame->function = function;
45 frame->target = script->target;
46 frame->original_target = script->target;
47 return frame;
50 static void
51 swfdec_as_script_function_dispose (GObject *object)
53 SwfdecAsScriptFunction *script = SWFDEC_AS_SCRIPT_FUNCTION (object);
55 if (script->script) {
56 swfdec_script_unref (script->script);
57 script->script = NULL;
60 G_OBJECT_CLASS (swfdec_as_script_function_parent_class)->dispose (object);
63 static void
64 swfdec_as_script_function_mark (SwfdecAsObject *object)
66 SwfdecAsScriptFunction *script = SWFDEC_AS_SCRIPT_FUNCTION (object);
68 if (script->scope)
69 swfdec_as_object_mark (SWFDEC_AS_OBJECT (script->scope));
71 SWFDEC_AS_OBJECT_CLASS (swfdec_as_script_function_parent_class)->mark (object);
74 static char *
75 swfdec_as_script_function_debug (SwfdecAsObject *object)
77 SwfdecAsScriptFunction *script = SWFDEC_AS_SCRIPT_FUNCTION (object);
78 SwfdecScript *s = script->script;
79 GString *string;
80 guint i;
82 string = g_string_new (s->name);
83 g_string_append (string, " (");
84 for (i = 0; i < s->n_arguments; i++) {
85 if (i > 0)
86 g_string_append (string, ", ");
87 g_string_append (string, s->arguments[i].name);
89 g_string_append (string, ")");
91 return g_string_free (string, FALSE);
94 static void
95 swfdec_as_script_function_class_init (SwfdecAsScriptFunctionClass *klass)
97 GObjectClass *object_class = G_OBJECT_CLASS (klass);
98 SwfdecAsObjectClass *asobject_class = SWFDEC_AS_OBJECT_CLASS (klass);
99 SwfdecAsFunctionClass *function_class = SWFDEC_AS_FUNCTION_CLASS (klass);
101 object_class->dispose = swfdec_as_script_function_dispose;
103 asobject_class->mark = swfdec_as_script_function_mark;
104 asobject_class->debug = swfdec_as_script_function_debug;
106 function_class->call = swfdec_as_script_function_call;
109 static void
110 swfdec_as_script_function_init (SwfdecAsScriptFunction *script_function)
114 SwfdecAsFunction *
115 swfdec_as_script_function_new (SwfdecAsScope *scope, SwfdecAsObject *target, SwfdecScript *script)
117 SwfdecAsValue val;
118 SwfdecAsScriptFunction *fun;
119 SwfdecAsObject *proto;
120 SwfdecAsContext *context;
122 g_return_val_if_fail (SWFDEC_IS_AS_SCOPE (scope), NULL);
123 g_return_val_if_fail (SWFDEC_IS_AS_OBJECT (target), NULL);
124 g_return_val_if_fail (script != NULL, NULL);
126 context = target->context;
127 if (!swfdec_as_context_use_mem (context, sizeof (SwfdecAsScriptFunction)))
128 return NULL;
129 fun = g_object_new (SWFDEC_TYPE_AS_SCRIPT_FUNCTION, NULL);
130 if (fun == NULL)
131 return NULL;
132 fun->scope = scope;
133 fun->script = script;
134 fun->target = target;
135 swfdec_as_object_add (SWFDEC_AS_OBJECT (fun), context, sizeof (SwfdecAsScriptFunction));
136 /* set prototype */
137 proto = swfdec_as_object_new_empty (context);
138 if (proto == NULL)
139 return NULL;
140 SWFDEC_AS_VALUE_SET_OBJECT (&val, proto);
141 swfdec_as_object_set_variable_and_flags (SWFDEC_AS_OBJECT (fun),
142 SWFDEC_AS_STR_prototype, &val,
143 SWFDEC_AS_VARIABLE_HIDDEN | SWFDEC_AS_VARIABLE_PERMANENT);
144 swfdec_as_function_set_constructor (SWFDEC_AS_FUNCTION (fun));
145 SWFDEC_AS_VALUE_SET_OBJECT (&val, SWFDEC_AS_OBJECT (fun));
146 swfdec_as_object_set_variable_and_flags (proto, SWFDEC_AS_STR_constructor,
147 &val, SWFDEC_AS_VARIABLE_HIDDEN | SWFDEC_AS_VARIABLE_PERMANENT);
148 SWFDEC_AS_VALUE_SET_OBJECT (&val, context->Object_prototype);
149 swfdec_as_object_set_variable_and_flags (proto, SWFDEC_AS_STR___proto__,
150 &val, SWFDEC_AS_VARIABLE_HIDDEN | SWFDEC_AS_VARIABLE_PERMANENT);
152 return SWFDEC_AS_FUNCTION (fun);