michael@0: // Call a function with no arguments. michael@0: function a_g() { michael@0: return 5; michael@0: } michael@0: michael@0: function a_f(g) { michael@0: return g(); michael@0: } michael@0: michael@0: a_g(); michael@0: assertEq(a_f(a_g), 5); michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // Call a function with one argument. michael@0: function b_g(a) { michael@0: return a; michael@0: } michael@0: michael@0: function b_f(h,b) { michael@0: return h(5); michael@0: } michael@0: b_g(5); michael@0: assertEq(b_f(b_g,4), 5); michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // Try to confuse the register allocator. michael@0: function c_g(a) { michael@0: return a; michael@0: } michael@0: function c_f(h,b) { michael@0: var x = h(5); michael@0: var y = x + 1; michael@0: var z = h(h(y + x + 2)); michael@0: var k = 2 + z + 3; michael@0: return h(h(h(k))); michael@0: } michael@0: c_g(2); // prime g(). michael@0: assertEq(c_f(c_g,7), 18) michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // Fail during unboxing, get kicked to interpreter. michael@0: // Interpreter throws an exception; handle it. michael@0: michael@0: function d_f(a) { michael@0: return a(); // Call a known non-object. This fails in unboxing. michael@0: } michael@0: var d_x = 0; michael@0: try { michael@0: d_f(1); // Don't assert. michael@0: } catch(e) { michael@0: d_x = 1; michael@0: } michael@0: assertEq(d_x, 1); michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // Try passing an uncompiled function. michael@0: michael@0: function e_uncompiled(a,b,c) { michael@0: return eval("b"); michael@0: } michael@0: function e_f(h) { michael@0: return h(0,h(2,4,6),1); michael@0: } michael@0: assertEq(e_f(e_uncompiled),4); michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // Try passing a native function. michael@0: michael@0: function f_app(f,n) { michael@0: return f(n); michael@0: } michael@0: assertEq(f_app(Math.sqrt, 16), 4); michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // Handle the case where too few arguments are passed. michael@0: function g_g(a,b,c,d,e) { michael@0: return e; michael@0: } michael@0: michael@0: function g_f(g) { michael@0: return g(2); michael@0: } michael@0: michael@0: g_g(); michael@0: assertEq(g_f(g_g), undefined); michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // Don't assert when given a non-function object. michael@0: function h_f(a) { michael@0: return a(); michael@0: } michael@0: michael@0: var x = new Object(); michael@0: var h_ret = 0; michael@0: try { michael@0: h_f(x); // don't assert. michael@0: } catch (e) { michael@0: h_ret = 1; michael@0: } michael@0: assertEq(h_ret, 1); michael@0: