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.
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
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
)
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
)
44 swfdec_external_interface__objectID (SwfdecAsContext
*cx
,
45 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
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
);
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
));
61 SWFDEC_AS_VALUE_SET_NULL (ret
);
65 SWFDEC_AS_NATIVE (14, 2, swfdec_external_interface__addCallback
)
67 swfdec_external_interface__addCallback (SwfdecAsContext
*cx
,
68 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
71 SwfdecPlayerPrivate
*priv
= SWFDEC_PLAYER (cx
)->priv
;
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
))
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
)
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
;
96 SWFDEC_AS_VALUE_SET_NULL (ret
);
97 if (scripting
== NULL
|| argc
== 0)
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
);
104 SWFDEC_AS_VALUE_SET_STRING (ret
, swfdec_as_context_give_string (cx
, t
));
109 SWFDEC_AS_NATIVE (14, 4, swfdec_external_interface__callOut
)
111 swfdec_external_interface__callOut (SwfdecAsContext
*cx
,
112 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
115 SwfdecPlayer
*player
= SWFDEC_PLAYER (cx
);
116 SwfdecPlayerScripting
*scripting
= player
->priv
->scripting
;
117 SwfdecPlayerScriptingClass
*klass
;
120 SWFDEC_AS_VALUE_SET_NULL (ret
);
121 if (scripting
== NULL
|| argc
== 0)
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
);
128 SWFDEC_AS_VALUE_SET_STRING (ret
, swfdec_as_context_give_string (cx
, t
));
133 SWFDEC_AS_NATIVE (14, 5, swfdec_external_interface__escapeXML
)
135 swfdec_external_interface__escapeXML (SwfdecAsContext
*cx
,
136 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
142 (s
= swfdec_as_value_to_string (cx
, &argv
[0])) == SWFDEC_AS_STR_EMPTY
) {
143 SWFDEC_AS_VALUE_SET_NULL (ret
);
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
)
152 swfdec_external_interface__unescapeXML (SwfdecAsContext
*cx
,
153 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
159 (s
= swfdec_as_value_to_string (cx
, &argv
[0])) == SWFDEC_AS_STR_EMPTY
) {
160 SWFDEC_AS_VALUE_SET_NULL (ret
);
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
)
170 swfdec_external_interface__jsQuoteString (SwfdecAsContext
*cx
,
171 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
179 (s
= swfdec_as_value_to_string (cx
, &argv
[0])) == SWFDEC_AS_STR_EMPTY
) {
180 SWFDEC_AS_VALUE_SET_NULL (ret
);
184 str
= g_string_new ("");
186 /* Yay, we don't escape backslashes! */
187 len
= strcspn (s
, "\n\r\"");
188 g_string_append_len (str
, s
, len
);
192 g_string_append_c (str
, '\\');
195 g_string_append_c (str
, 'n');
198 g_string_append_c (str
, 'r');
201 g_string_append_c (str
, '"');
204 g_assert_not_reached ();
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
)
214 swfdec_external_interface_get_available (SwfdecAsContext
*cx
,
215 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
218 SWFDEC_AS_VALUE_SET_BOOLEAN (ret
, SWFDEC_PLAYER (cx
)->priv
->scripting
!= NULL
);
221 SWFDEC_AS_NATIVE (14, 101, swfdec_external_interface_set_available
)
223 swfdec_external_interface_set_available (SwfdecAsContext
*cx
,
224 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
227 /* read-only property */