remove SWFDEC_IS_AS_VALUE check
[swfdec.git] / swfdec / swfdec_external_interface.c
blobb760df88e230ec84bdae53a1307f0429700ade17
1 /* Swfdec
2 * Copyright (C) 2007 Pekka Lampila <pekka.lampila@iki.fi>
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_internal.h"
27 #include "swfdec_as_native_function.h"
28 #include "swfdec_as_strings.h"
29 #include "swfdec_debug.h"
30 #include "swfdec_player_internal.h"
31 #include "swfdec_player_scripting.h"
32 #include "swfdec_xml.h"
34 SWFDEC_AS_NATIVE (14, 0, swfdec_external_interface__initJS)
35 void
36 swfdec_external_interface__initJS (SwfdecAsContext *cx, SwfdecAsObject *object,
37 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
39 /* FIXME: call an init vfunc here? */
42 SWFDEC_AS_NATIVE (14, 1, swfdec_external_interface__objectID)
43 void
44 swfdec_external_interface__objectID (SwfdecAsContext *cx,
45 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
46 SwfdecAsValue *ret)
48 SwfdecPlayer *player = SWFDEC_PLAYER (cx);
49 SwfdecPlayerScripting *scripting = player->priv->scripting;
50 SwfdecPlayerScriptingClass *klass;
52 if (scripting == NULL) {
53 SWFDEC_AS_VALUE_SET_NULL (ret);
54 return;
56 klass = SWFDEC_PLAYER_SCRIPTING_GET_CLASS (scripting);
57 if (klass->js_get_id) {
58 char *s = klass->js_get_id (scripting, player);
59 SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_give_string (cx, s));
60 } else {
61 SWFDEC_AS_VALUE_SET_NULL (ret);
65 SWFDEC_AS_NATIVE (14, 2, swfdec_external_interface__addCallback)
66 void
67 swfdec_external_interface__addCallback (SwfdecAsContext *cx,
68 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
69 SwfdecAsValue *ret)
71 SwfdecPlayerPrivate *priv = SWFDEC_PLAYER (cx)->priv;
72 SwfdecAsObject *fun;
73 const char *name;
75 SWFDEC_AS_VALUE_SET_BOOLEAN (ret, FALSE);
76 SWFDEC_AS_CHECK (0, NULL, "so", &name, &fun);
78 /* FIXME: do we allow setting if scripting is unsupported? */
79 if (!SWFDEC_IS_AS_FUNCTION (fun->relay))
80 return;
82 g_hash_table_insert (priv->scripting_callbacks, (gpointer) name, fun);
83 SWFDEC_AS_VALUE_SET_BOOLEAN (ret, TRUE);
86 SWFDEC_AS_NATIVE (14, 3, swfdec_external_interface__evalJS)
87 void
88 swfdec_external_interface__evalJS (SwfdecAsContext *cx, SwfdecAsObject *object,
89 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
91 SwfdecPlayer *player = SWFDEC_PLAYER (cx);
92 SwfdecPlayerScripting *scripting = player->priv->scripting;
93 SwfdecPlayerScriptingClass *klass;
94 const char *s;
96 SWFDEC_AS_VALUE_SET_NULL (ret);
97 if (scripting == NULL || argc == 0)
98 return;
99 s = swfdec_as_value_to_string (cx, &argv[0]);
100 klass = SWFDEC_PLAYER_SCRIPTING_GET_CLASS (scripting);
101 if (klass->js_call) {
102 char *t = klass->js_call (scripting, player, s);
103 if (t != NULL) {
104 SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_give_string (cx, t));
109 SWFDEC_AS_NATIVE (14, 4, swfdec_external_interface__callOut)
110 void
111 swfdec_external_interface__callOut (SwfdecAsContext *cx,
112 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
113 SwfdecAsValue *ret)
115 SwfdecPlayer *player = SWFDEC_PLAYER (cx);
116 SwfdecPlayerScripting *scripting = player->priv->scripting;
117 SwfdecPlayerScriptingClass *klass;
118 const char *s;
120 SWFDEC_AS_VALUE_SET_NULL (ret);
121 if (scripting == NULL || argc == 0)
122 return;
123 s = swfdec_as_value_to_string (cx, &argv[0]);
124 klass = SWFDEC_PLAYER_SCRIPTING_GET_CLASS (scripting);
125 if (klass->xml_call) {
126 char *t = klass->xml_call (scripting, player, s);
127 if (t != NULL) {
128 SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_give_string (cx, t));
133 SWFDEC_AS_NATIVE (14, 5, swfdec_external_interface__escapeXML)
134 void
135 swfdec_external_interface__escapeXML (SwfdecAsContext *cx,
136 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
137 SwfdecAsValue *ret)
139 const char *s;
141 if (argc == 0 ||
142 (s = swfdec_as_value_to_string (cx, &argv[0])) == SWFDEC_AS_STR_EMPTY) {
143 SWFDEC_AS_VALUE_SET_NULL (ret);
144 return;
147 SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_give_string (cx, swfdec_xml_escape (s)));
150 SWFDEC_AS_NATIVE (14, 6, swfdec_external_interface__unescapeXML)
151 void
152 swfdec_external_interface__unescapeXML (SwfdecAsContext *cx,
153 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
154 SwfdecAsValue *ret)
156 const char *s;
158 if (argc == 0 ||
159 (s = swfdec_as_value_to_string (cx, &argv[0])) == SWFDEC_AS_STR_EMPTY) {
160 SWFDEC_AS_VALUE_SET_NULL (ret);
161 return;
164 SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_give_string (cx,
165 swfdec_xml_unescape_len (cx, s, strlen (s), FALSE)));
168 SWFDEC_AS_NATIVE (14, 7, swfdec_external_interface__jsQuoteString)
169 void
170 swfdec_external_interface__jsQuoteString (SwfdecAsContext *cx,
171 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
172 SwfdecAsValue *ret)
174 const char *s;
175 GString *str;
176 size_t len;
178 if (argc == 0 ||
179 (s = swfdec_as_value_to_string (cx, &argv[0])) == SWFDEC_AS_STR_EMPTY) {
180 SWFDEC_AS_VALUE_SET_NULL (ret);
181 return;
184 str = g_string_new ("");
185 do {
186 /* Yay, we don't escape backslashes! */
187 len = strcspn (s, "\n\r\"");
188 g_string_append_len (str, s, len);
189 s += len;
190 if (*s == '\0')
191 break;
192 g_string_append_c (str, '\\');
193 switch (*s) {
194 case '\n':
195 g_string_append_c (str, 'n');
196 break;
197 case '\r':
198 g_string_append_c (str, 'r');
199 break;
200 case '"':
201 g_string_append_c (str, '"');
202 break;
203 default:
204 g_assert_not_reached ();
205 break;
207 s++;
208 } while (TRUE);
209 SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_give_string (cx, g_string_free (str, FALSE)));
212 SWFDEC_AS_NATIVE (14, 100, swfdec_external_interface_get_available)
213 void
214 swfdec_external_interface_get_available (SwfdecAsContext *cx,
215 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
216 SwfdecAsValue *ret)
218 SWFDEC_AS_VALUE_SET_BOOLEAN (ret, SWFDEC_PLAYER (cx)->priv->scripting != NULL);
221 SWFDEC_AS_NATIVE (14, 101, swfdec_external_interface_set_available)
222 void
223 swfdec_external_interface_set_available (SwfdecAsContext *cx,
224 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
225 SwfdecAsValue *ret)
227 /* read-only property */