test-extra.js 3.96 KB
Newer Older
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
(function (global, binding, v8) {
6
  'use strict';
7
  binding.testExtraShouldReturnFive = function() {
8 9
    return 5;
  };
10

11 12
  binding.testExtraShouldCallToRuntime = function() {
    return binding.runtime(3);
13
  };
14

15 16 17 18 19 20 21 22 23
  binding.testFunctionToString = function() {
    function foo() { return 1; }
    return foo.toString();
  };

  binding.testStackTrace = function(f) {
    return f();
  }

24 25
  // Exercise all of the extras utils:
  // - v8.createPrivateSymbol
26
  // - v8.uncurryThis
27 28 29 30 31 32 33 34 35 36 37
  // - v8.InternalPackedArray
  // - v8.createPromise, v8.resolvePromise, v8.rejectPromise

  const Object = global.Object;
  const hasOwn = v8.uncurryThis(Object.prototype.hasOwnProperty);

  const Function = global.Function;
  const call = v8.uncurryThis(Function.prototype.call);
  const apply = v8.uncurryThis(Function.prototype.apply);

  const Promise = global.Promise;
38
  const Promise_resolve = Promise.resolve.bind(Promise);
39

40 41 42 43 44 45 46 47 48 49 50 51
  const arrayToTest = new v8.InternalPackedArray();
  arrayToTest.push(1);
  arrayToTest.push(2);
  arrayToTest.pop();
  arrayToTest.unshift("a", "b", "c");
  arrayToTest.shift();
  arrayToTest.splice(0, 1);
  const slicedArray = arrayToTest.slice();
  const arraysOK = arrayToTest.length === 2 && arrayToTest[0] === "c" &&
      arrayToTest[1] === 1 && slicedArray.length === 2 &&
      slicedArray[0] === "c" && slicedArray[1] === 1;

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  binding.testCreatePromise = function() {
    return v8.createPromise();
  }

  binding.testCreatePromiseWithParent = function(parent) {
    return v8.createPromise(parent);
  }

  binding.testRejectPromise = function(promise, reason) {
    return v8.rejectPromise(promise, reason);
  }

  binding.testResolvePromise = function(promise, resolution) {
    return v8.resolvePromise(promise, resolution);
  }

68 69 70 71
  binding.testExtraCanUseUtils = function() {
    const fulfilledPromise = v8.createPromise();
    v8.resolvePromise(
      fulfilledPromise,
72 73
      hasOwn({ test: 'test' }, 'test') ? 1 : -1,
      undefined  // pass an extra arg to test arguments adapter frame
74 75
    );

76 77 78
    const fulfilledPromise2 = Promise_resolve(call(function (arg1, arg2) {
      return (this.prop === arg1 && arg1 === 'value' && arg2) ? 2 : -1;
    }, { prop: 'value' }, 'value', arraysOK));
79 80 81 82

    const rejectedPromise = v8.createPromise();
    v8.rejectPromise(rejectedPromise, apply(function (arg1, arg2) {
      return (arg1 === arg2 && arg2 === 'x') ? 3 : -1;
83
    }, null, ['x', 'x']));
84

85 86 87 88
    const rejectedButHandledPromise = v8.createPromise();
    v8.rejectPromise(rejectedButHandledPromise, 4);
    v8.markPromiseAsHandled(rejectedButHandledPromise);

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    function promiseStateToString(promise) {
      switch (v8.promiseState(promise)) {
        case v8.kPROMISE_PENDING:
          return "pending";
        case v8.kPROMISE_FULFILLED:
          return "fulfilled";
        case v8.kPROMISE_REJECTED:
          return "rejected";
        default:
          throw new Error("Unexpected value for promiseState");
      }
    }

    let promiseStates = promiseStateToString(new Promise(() => {})) + ' ' +
                        promiseStateToString(fulfilledPromise) + ' ' +
                        promiseStateToString(rejectedPromise);

106 107 108 109
    const uncurryThis = v8.uncurryThis(function (a, b, c, d, e) {
      return (this + a + b + c + d + e) === 21;
    })(1, 2, 3, 4, 5, 6);

110 111 112 113
    return {
      privateSymbol: v8.createPrivateSymbol('sym'),
      fulfilledPromise, // should be fulfilled with 1
      fulfilledPromise2, // should be fulfilled with 2
114
      rejectedPromise, // should be rejected with 3
115
      rejectedButHandledPromise, // should be rejected but have a handler
domenic's avatar
domenic committed
116 117
      promiseStates, // should be the string "pending fulfilled rejected"
      promiseIsPromise: v8.isPromise(fulfilledPromise), // should be true
118 119
      thenableIsPromise: v8.isPromise({ then() { } }),  // should be false
      uncurryThis // should be true
120 121
    };
  };
122
})