js/src/jit-test/tests/debug/Object-apply-02.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jit-test/tests/debug/Object-apply-02.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,58 @@
     1.4 +// tests calling native functions via Debugger.Object.prototype.apply/call
     1.5 +
     1.6 +load(libdir + "asserts.js");
     1.7 +
     1.8 +var g = newGlobal();
     1.9 +g.eval("function f() { debugger; }");
    1.10 +var dbg = new Debugger(g);
    1.11 +
    1.12 +function test(usingApply) {
    1.13 +    dbg.onDebuggerStatement = function (frame) {
    1.14 +        var max = frame.arguments[0];
    1.15 +        var cv = usingApply ? max.apply(null, [9, 16]) : max.call(null, 9, 16);
    1.16 +        assertEq(cv.return, 16);
    1.17 +
    1.18 +        cv = usingApply ? max.apply() : max.call();
    1.19 +        assertEq(cv.return, -1/0);
    1.20 +
    1.21 +        cv = usingApply ? max.apply(null, [2, 5, 3, 8, 1, 9, 4, 6, 7])
    1.22 +                        : max.call(null, 2, 5, 3, 8, 1, 9, 4, 6, 7);
    1.23 +        assertEq(cv.return, 9);
    1.24 +
    1.25 +        // second argument to apply must be an array
    1.26 +        assertThrowsInstanceOf(function () { max.apply(null, 12); }, TypeError);
    1.27 +    };
    1.28 +    g.eval("f(Math.max);");
    1.29 +
    1.30 +    dbg.onDebuggerStatement = function (frame) {
    1.31 +        var push = frame.arguments[0];
    1.32 +        var arr = frame.arguments[1];
    1.33 +        var cv;
    1.34 +
    1.35 +        cv = usingApply ? push.apply(arr, [0, 1, 2]) : push.call(arr, 0, 1, 2);
    1.36 +        assertEq(cv.return, 3);
    1.37 +
    1.38 +        cv = usingApply ? push.apply(arr, [arr]) : push.call(arr, arr);
    1.39 +        assertEq(cv.return, 4);
    1.40 +
    1.41 +        cv = usingApply ? push.apply(arr) : push.call(arr);
    1.42 +        assertEq(cv.return, 4);
    1.43 +
    1.44 +        // You can apply Array.prototype.push to a string; it does ToObject on
    1.45 +        // it.  But as the length property on String objects is non-writable,
    1.46 +        // attempting to increase the length will throw a TypeError.
    1.47 +        cv = usingApply
    1.48 +             ? push.apply("hello", ["world"])
    1.49 +             : push.call("hello", "world");
    1.50 +        assertEq("throw" in cv, true);
    1.51 +        var ex = cv.throw;
    1.52 +        assertEq(frame.evalWithBindings("ex instanceof TypeError", { ex: ex }).return, true);
    1.53 +    };
    1.54 +    g.eval("var a = []; f(Array.prototype.push, a);");
    1.55 +    assertEq(g.a.length, 4);
    1.56 +    assertEq(g.a.slice(0, 3).join(","), "0,1,2");
    1.57 +    assertEq(g.a[3], g.a);
    1.58 +}
    1.59 +
    1.60 +test(true);
    1.61 +test(false);

mercurial