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 SWFDEC_AS_CHECK (0, NULL
, "s", &s
);
98 if (scripting
== NULL
)
101 klass
= SWFDEC_PLAYER_SCRIPTING_GET_CLASS (scripting
);
102 if (klass
->js_call
) {
103 char *t
= klass
->js_call (scripting
, player
, s
);
105 SWFDEC_AS_VALUE_SET_STRING (ret
, swfdec_as_context_give_string (cx
, t
));
110 SWFDEC_AS_NATIVE (14, 4, swfdec_external_interface__callOut
)
112 swfdec_external_interface__callOut (SwfdecAsContext
*cx
,
113 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
116 SwfdecPlayer
*player
= SWFDEC_PLAYER (cx
);
117 SwfdecPlayerScripting
*scripting
= player
->priv
->scripting
;
118 SwfdecPlayerScriptingClass
*klass
;
121 SWFDEC_AS_VALUE_SET_NULL (ret
);
122 SWFDEC_AS_CHECK (0, NULL
, "s", &s
);
123 if (scripting
== NULL
)
125 klass
= SWFDEC_PLAYER_SCRIPTING_GET_CLASS (scripting
);
126 if (klass
->xml_call
) {
127 char *t
= klass
->xml_call (scripting
, player
, s
);
129 SWFDEC_AS_VALUE_SET_STRING (ret
, swfdec_as_context_give_string (cx
, t
));
134 SWFDEC_AS_NATIVE (14, 5, swfdec_external_interface__escapeXML
)
136 swfdec_external_interface__escapeXML (SwfdecAsContext
*cx
,
137 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
142 SWFDEC_AS_VALUE_SET_NULL (ret
);
143 SWFDEC_AS_CHECK (0, NULL
, "s", &s
);
144 if (s
== SWFDEC_AS_STR_EMPTY
)
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
,
158 SWFDEC_AS_VALUE_SET_NULL (ret
);
159 SWFDEC_AS_CHECK (0, NULL
, "s", &s
);
160 if (s
== SWFDEC_AS_STR_EMPTY
)
163 SWFDEC_AS_VALUE_SET_STRING (ret
, swfdec_as_context_give_string (cx
,
164 swfdec_xml_unescape_len (cx
, s
, strlen (s
), FALSE
)));
167 SWFDEC_AS_NATIVE (14, 7, swfdec_external_interface__jsQuoteString
)
169 swfdec_external_interface__jsQuoteString (SwfdecAsContext
*cx
,
170 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
177 SWFDEC_AS_VALUE_SET_NULL (ret
);
178 SWFDEC_AS_CHECK (0, NULL
, "s", &s
);
179 if (s
== SWFDEC_AS_STR_EMPTY
)
182 str
= g_string_new ("");
184 /* Yay, we don't escape backslashes! */
185 len
= strcspn (s
, "\n\r\"");
186 g_string_append_len (str
, s
, len
);
190 g_string_append_c (str
, '\\');
193 g_string_append_c (str
, 'n');
196 g_string_append_c (str
, 'r');
199 g_string_append_c (str
, '"');
202 g_assert_not_reached ();
207 SWFDEC_AS_VALUE_SET_STRING (ret
, swfdec_as_context_give_string (cx
, g_string_free (str
, FALSE
)));
210 SWFDEC_AS_NATIVE (14, 100, swfdec_external_interface_get_available
)
212 swfdec_external_interface_get_available (SwfdecAsContext
*cx
,
213 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
216 SWFDEC_AS_VALUE_SET_BOOLEAN (ret
, SWFDEC_PLAYER (cx
)->priv
->scripting
!= NULL
);
219 SWFDEC_AS_NATIVE (14, 101, swfdec_external_interface_set_available
)
221 swfdec_external_interface_set_available (SwfdecAsContext
*cx
,
222 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
225 /* read-only property */