Update with current status
[gnash.git] / testsuite / actionscript.all / ASnative.as
blob207c72716c6c09e9f8fccb01eb47df4ec50421cd
1 //
2 // Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program 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
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 // This counts calls to toString() and valueOf() regularly to check that native
19 // methods are correctly applied. So it saves much effort if new tests are added
20 // after the end of the present ones.
22 rcsid="ASnative.as";
23 #include "check.as"
25 /// Global
27 // Old ASnew
28 a = ASnative(2, 0);
30 var result = "";
31 function func() {
32 if (a()) { result = "true"; }
33 else { result = "false"; }
36 func();
37 check_equals (result, "false");
38 discard = new func();
39 xcheck_equals (result, "true");
41 a = ASnative (100, 0); // escape
42 check_equals(a(" "), "%20");
43 a = ASnative (100, 1); // unescape
44 check_equals(a("%20"), " ");
45 a = ASnative (100, 2); // parseInt
46 check_equals(a(" 566765s"), "566765");
47 a = ASnative (100, 3); // parseFloat
48 check_equals(a("8.4e6"), 8.4e6);
50 // Do this first to make sure ASnative is
51 // registered before Date class itself is loaded (Gnash loads
52 // on demand).
53 a = ASnative(103, 257);
54 check_equals (a(65, 1, 1, 1, 1, 1, 1), Date.UTC(65, 1, 1, 1, 1, 1, 1));
56 e = ASnative(103, 256); // _global.Date
57 xcheck_equals(e().valueOf(), Date().valueOf());
58 g = e;
59 xcheck_equals(typeOf(g().valueOf()), 'string');
61 xcheck_equals(e(100000).valueOf(), Date().valueOf());
63 g = e();
64 check_equals(typeOf(g.getMilliseconds), 'undefined');
66 f = new e(100000000); // not instantiatable
67 xcheck_equals(typeof(f), 'object');
68 check_equals(typeof(f.getMilliseconds()), 'undefined');
69 check_equals(typeof(f().getMilliseconds()), 'undefined');
71 d = new Date (123456789);
73 d.a = ASnative(103, 0);
74 check_equals (d.a(), d.getFullYear());
75 d.a = ASnative(103, 1);
76 check_equals (d.a(), d.getYear());
77 d.a = ASnative(103, 2);
78 check_equals (d.a(), d.getMonth());
79 d.a = ASnative(103, 3);
80 check_equals (d.a(), d.getDate());
81 d.a = ASnative(103, 4);
82 check_equals (d.a(), d.getDay());
83 d.a = ASnative(103, 5);
84 check_equals (d.a(), d.getHours());
85 d.a = ASnative(103, 6);
86 check_equals (d.a(), d.getMinutes());
87 d.a = ASnative(103, 7);
88 check_equals (d.a(), d.getSeconds());
89 d.a = ASnative(103, 8);
90 check_equals (d.a(), d.getMilliseconds());
91 d.a = ASnative(103, 16);
92 check_equals (d.a(), d.getTime());
93 d.a = ASnative(103, 18);
94 check_equals (d.a(), d.getTimezoneOffset());
95 d.a = ASnative(103, 19);
96 check_equals (d.a(), d.toString());
97 d.a = ASnative(103, 128);
98 check_equals (d.a(), d.getUTCFullYear());
99 d.a = ASnative(103, 129);
100 check_equals (d.a(), d.getUTCYear());
101 d.a = ASnative(103, 130);
102 check_equals (d.a(), d.getUTCMonth());
103 d.a = ASnative(103, 131);
104 check_equals (d.a(), d.getUTCDate());
105 d.a = ASnative(103, 132);
106 check_equals (d.a(), d.getUTCDay());
107 d.a = ASnative(103, 133);
108 check_equals (d.a(), d.getUTCHours());
109 d.a = ASnative(103, 134);
110 check_equals (d.a(), d.getUTCMinutes());
111 d.a = ASnative(103, 135);
112 check_equals (d.a(), d.getUTCSeconds());
113 d.a = ASnative(103, 136);
114 check_equals (d.a(), d.getUTCMilliseconds());
115 d.a = ASnative(103, 1);
116 check_equals (d.a(), d.getYear());
118 countVO = 0;
119 countTS = 0;
121 // ASNative Math (call valueOf)
123 func = {};
124 func.valueOf = function () {
125 //trace ("valueOf()");
126 countVO++;
127 return 0.3;
130 func.toString = function () {
131 //trace ("toString()");
132 countTS++;
133 return "gNaSh mUsT woRK! öÜäÄ€€";
136 a = ASnative(200, 0);
137 check_equals(a(func), 0.3); // abs
138 check_equals(a(0.3), 0.3); // abs
140 a = ASnative(200, 1);
141 check_equals(a(func, func + 1), 0.3); // min
142 check_equals(a(0.3, 1.3), 0.3); // min
144 a = ASnative(200, 2);
145 check_equals(a(func, func + 1), 1.3); // max
146 check_equals(a(0.3, 1.3), 1.3); // max
148 a = ASnative(200, 3);
149 check_equals(a(func).toString(), "0.29552020666134"); // sin
150 check_equals(a(0.3).toString(), "0.29552020666134"); // sin
152 a = ASnative(200, 4);
153 check_equals(a(func).toString(), "0.955336489125606"); // cos
154 check_equals(a(0.3).toString(), "0.955336489125606"); // cos
156 a = ASnative(200, 5);
157 check_equals(a(func, func + 1).toString(), "0.226798848053886"); // atan2
158 check_equals(a(0.3, 1.3).toString(), "0.226798848053886"); // atan2
160 a = ASnative(200, 6);
161 check_equals(a(func).toString(), "0.309336249609623"); // tan
162 a = ASnative(200, 7);
163 check_equals(a(func).toString(), "1.349858807576"); // exp
164 a = ASnative(200, 8);
165 check_equals(a(func).toString(), "-1.20397280432594"); // log
166 a = ASnative(200, 9);
167 check_equals(a(func).toString(), "0.547722557505166"); // sqrt
168 a = ASnative(200, 10);
169 check_equals(a(func), 0); // round
170 a = ASnative(200, 11);
171 check(a(func) >= 0 && a(func < 1)); // random
172 a = ASnative(200, 12);
173 check_equals(a(func), 0); // floor
174 a = ASnative(200, 13);
175 check_equals(a(func), 1); // ceil
176 a = ASnative(200, 14);
177 check_equals(a(func).toString(), "0.291456794477867"); // atan
178 a = ASnative(200, 15);
179 check_equals(a(func).toString(), "0.304692654015398"); // asin
180 a = ASnative(200, 16);
181 check_equals(a(func).toString(), "1.2661036727795"); // acos
182 a = ASnative(200, 17);
183 check_equals(a(func, func + 1).toString(), "0.209053590580785"); // pow
184 a = ASnative(200, 18);
185 check_equals(a(func), false); // isNan
186 a = ASnative(200, 19);
187 check_equals(a(func), true); // isFinite
189 check_equals (countVO, 25); // calls to valueOf.
190 check_equals (countTS, 0); // calls to toString.
192 // String functions (call toString)
194 a = ASnative(251, 3); // String.toUpperCase
195 check_equals(a("Hello World"), "_LEVEL0");
196 a = ASnative(102, 0); // SWF5 to upper
197 check_equals(a("Hello World"), "_LEVEL0");
200 // SWF5 has problems with UTF-8, tested in String.as.
201 // No need to test here as well.
203 check_equals (countTS, 0); // calls to toString.
205 #if OUTPUT_VERSION > 5
206 func.a = ASnative(251, 3); // String.toUpperCase
207 check_equals(func.a(), "GNASH MUST WORK! ÖÜÄÄ€€");
209 func.a = ASnative(251, 4); // String.toLowerCase
210 check_equals(func.a(), "gnash must work! öüä䀀");
212 // Check calls to toString.
213 check_equals (countTS, 2);
214 #endif
216 func.a = ASnative(102, 0); // SWF5 to upper
217 check_equals(func.a(), "GNASH MUST WORK! öÜäÄ€€");
219 func.a = ASnative(102, 1); // SWF5 to lower
220 check_equals(func.a(), "gnash must work! öÜäÄ€€");
222 // Check calls to toString.
223 #if OUTPUT_VERSION > 5
224 check_equals (countTS, 4);
225 #else
226 check_equals (countTS, 2);
227 #endif
229 // Stage
230 st = ASnative(666, 2);
231 st("exactFit");
232 st = ASnative(666, 1);
233 check_equals (st(), "exactFit");
235 st = ASnative(666, 4);
236 st("BRL");
238 st = ASnative(666, 3);
239 check_equals (st(), "LRB");
241 //Stage.width - read only!
242 st = ASnative(666, 6);
243 st(402);
244 st = ASnative(666, 5);
245 check_equals (st(), 640);
247 // Stage.height - read only!
248 st = ASnative(666, 8);
249 st(402);
250 st = ASnative(666, 7);
251 check_equals (st(), 480);
253 // Stage.showMenu
254 st = ASnative(666, 10);
255 st = ASnative(666, 9);
257 #if OUTPUT_VERSION > 5
258 check_equals (countTS, 4);
259 #else
260 check_equals (countTS, 2);
261 #endif
263 check_equals (countVO, 25);
265 /// SharedObject undocumented functions.
267 a = ASnative(2106, 202);
268 f = a("level1/level2/settings", "/", undefined);
269 xcheck_equals(typeof(f), "null");
271 a = ASnative(2106, 204);
272 f = new SharedObject;
273 check_equals (typeof(f.data), "undefined");
274 ret = a(f, "level1/level2/settings", "/", undefined);
275 xcheck_equals(ret, true);
276 xcheck_equals (typeof(f.data), "object");
278 // Check that ASnative returns a new function, not the same one.
279 a = ASnative(2106, 204);
280 b = ASnative(2106, 204);
281 #if OUTPUT_VERSION < 6
282 check(a == b);
283 #else
284 check(a != b);
285 #endif
287 /// Test ASconstructor
289 /// The important things seem to be:
290 /// 1. a new prototype is created every time; it's not the same object.
291 /// 2. native functions may only work with an appropriate prototype.
293 // This is _global.Number
294 f = ASconstructor(106, 2);
295 check_equals(typeof(f), "function");
296 check_equals(typeof(f.prototype), "object");
298 // Attach number natives and it works.
299 ASSetNative(f.prototype, 106, "valueOf,toString");
301 obj = new f(6);
302 check_equals(obj.__proto__, f.prototype);
303 #if OUTPUT_VERSION > 5
304 check(obj.__proto__ != Number.prototype);
305 #else
306 check_equals(obj.__proto__, undefined);
307 #endif
309 check_equals(typeof(obj), "object");
310 check_equals(obj.toString(), "6");
312 // Attach boolean natives and it fails.
313 ASSetNative(f.prototype, 107, "valueOf,toString");
314 check_equals(typeof(obj), "object");
315 check_equals(obj.toString(), undefined);
317 // Attach number natives to prototype again and it works.
318 ASSetNative(f.prototype, 106, "valueOf,toString");
319 check_equals(obj.toString(), "6");
321 g = ASnative(106, 2);
322 check_equals(typeof(g), "function");
323 check_equals(typeof(g.prototype), "undefined");
325 // This is the ASnative function Number.toString. It does not attach
326 // Number.prototype, so the Number.toString function fails.
327 f = ASconstructor(106, 1);
328 ASSetNative(f.prototype, 106, "valueOf,toString");
329 check_equals(typeof(f), "function");
330 check_equals(typeof(f.prototype), "object");
331 obj = new f(6);
332 xcheck_equals(typeof(obj), "object");
333 check_equals(obj.toString(), undefined);
335 obj = new f();
336 check_equals(obj.toString(), undefined);
338 ba = ASnative;
339 ASnative = 78;
341 // ASconstructor doesn't rely on ASnative.
342 f = ASconstructor(106, 2);
343 check_equals(typeof(f), "function");
344 check_equals(typeof(f.prototype), "object");
346 g = ASnative(106, 2);
347 check_equals(typeof(g), "undefined");
348 check_equals(typeof(g.prototype), "undefined");
350 ASnative = ba;
352 #if OUTPUT_VERSION > 5
353 check_totals(104);
354 #else
355 check_totals(101);
356 #endif