|
1 // Call a function with no arguments. |
|
2 function a_g() { |
|
3 return 5; |
|
4 } |
|
5 |
|
6 function a_f(g) { |
|
7 return g(); |
|
8 } |
|
9 |
|
10 a_g(); |
|
11 assertEq(a_f(a_g), 5); |
|
12 |
|
13 /////////////////////////////////////////////////////////////////////////////// |
|
14 // Call a function with one argument. |
|
15 function b_g(a) { |
|
16 return a; |
|
17 } |
|
18 |
|
19 function b_f(h,b) { |
|
20 return h(5); |
|
21 } |
|
22 b_g(5); |
|
23 assertEq(b_f(b_g,4), 5); |
|
24 |
|
25 /////////////////////////////////////////////////////////////////////////////// |
|
26 // Try to confuse the register allocator. |
|
27 function c_g(a) { |
|
28 return a; |
|
29 } |
|
30 function c_f(h,b) { |
|
31 var x = h(5); |
|
32 var y = x + 1; |
|
33 var z = h(h(y + x + 2)); |
|
34 var k = 2 + z + 3; |
|
35 return h(h(h(k))); |
|
36 } |
|
37 c_g(2); // prime g(). |
|
38 assertEq(c_f(c_g,7), 18) |
|
39 |
|
40 /////////////////////////////////////////////////////////////////////////////// |
|
41 // Fail during unboxing, get kicked to interpreter. |
|
42 // Interpreter throws an exception; handle it. |
|
43 |
|
44 function d_f(a) { |
|
45 return a(); // Call a known non-object. This fails in unboxing. |
|
46 } |
|
47 var d_x = 0; |
|
48 try { |
|
49 d_f(1); // Don't assert. |
|
50 } catch(e) { |
|
51 d_x = 1; |
|
52 } |
|
53 assertEq(d_x, 1); |
|
54 |
|
55 /////////////////////////////////////////////////////////////////////////////// |
|
56 // Try passing an uncompiled function. |
|
57 |
|
58 function e_uncompiled(a,b,c) { |
|
59 return eval("b"); |
|
60 } |
|
61 function e_f(h) { |
|
62 return h(0,h(2,4,6),1); |
|
63 } |
|
64 assertEq(e_f(e_uncompiled),4); |
|
65 |
|
66 /////////////////////////////////////////////////////////////////////////////// |
|
67 // Try passing a native function. |
|
68 |
|
69 function f_app(f,n) { |
|
70 return f(n); |
|
71 } |
|
72 assertEq(f_app(Math.sqrt, 16), 4); |
|
73 |
|
74 /////////////////////////////////////////////////////////////////////////////// |
|
75 // Handle the case where too few arguments are passed. |
|
76 function g_g(a,b,c,d,e) { |
|
77 return e; |
|
78 } |
|
79 |
|
80 function g_f(g) { |
|
81 return g(2); |
|
82 } |
|
83 |
|
84 g_g(); |
|
85 assertEq(g_f(g_g), undefined); |
|
86 |
|
87 /////////////////////////////////////////////////////////////////////////////// |
|
88 // Don't assert when given a non-function object. |
|
89 function h_f(a) { |
|
90 return a(); |
|
91 } |
|
92 |
|
93 var x = new Object(); |
|
94 var h_ret = 0; |
|
95 try { |
|
96 h_f(x); // don't assert. |
|
97 } catch (e) { |
|
98 h_ret = 1; |
|
99 } |
|
100 assertEq(h_ret, 1); |
|
101 |