blob: b3752d97b28d96b38ca97b71739d0e139bee8eef [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5(function (global, binding, v8) {
6 'use strict';
7 binding.testExtraShouldReturnFive = function() {
8 return 5;
9 };
10
11 binding.testExtraShouldCallToRuntime = function() {
12 return binding.runtime(3);
13 };
14
Ben Murdoch097c5b22016-05-18 11:27:45 +010015 binding.testFunctionToString = function() {
16 function foo() { return 1; }
17 return foo.toString();
18 };
19
20 binding.testStackTrace = function(f) {
21 return f();
22 }
23
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000024 // Exercise all of the extras utils:
25 // - v8.createPrivateSymbol
26 // - v8.simpleBind, v8.uncurryThis
27 // - v8.InternalPackedArray
28 // - v8.createPromise, v8.resolvePromise, v8.rejectPromise
29
30 const Object = global.Object;
31 const hasOwn = v8.uncurryThis(Object.prototype.hasOwnProperty);
32
33 const Function = global.Function;
34 const call = v8.uncurryThis(Function.prototype.call);
35 const apply = v8.uncurryThis(Function.prototype.apply);
36
37 const Promise = global.Promise;
38 const Promise_resolve = v8.simpleBind(Promise.resolve, Promise);
39
40 const arrayToTest = new v8.InternalPackedArray();
41 arrayToTest.push(1);
42 arrayToTest.push(2);
43 arrayToTest.pop();
44 arrayToTest.unshift("a", "b", "c");
45 arrayToTest.shift();
46 arrayToTest.splice(0, 1);
47 const slicedArray = arrayToTest.slice();
48 const arraysOK = arrayToTest.length === 2 && arrayToTest[0] === "c" &&
49 arrayToTest[1] === 1 && slicedArray.length === 2 &&
50 slicedArray[0] === "c" && slicedArray[1] === 1;
51
52 binding.testExtraCanUseUtils = function() {
53 const fulfilledPromise = v8.createPromise();
54 v8.resolvePromise(
55 fulfilledPromise,
56 hasOwn({ test: 'test' }, 'test') ? 1 : -1
57 );
58
59 const fulfilledPromise2 = Promise_resolve(call(function (arg1, arg2) {
60 return (this.prop === arg1 && arg1 === 'value' && arg2) ? 2 : -1;
61 }, { prop: 'value' }, 'value', arraysOK));
62
63 const rejectedPromise = v8.createPromise();
64 v8.rejectPromise(rejectedPromise, apply(function (arg1, arg2) {
65 return (arg1 === arg2 && arg2 === 'x') ? 3 : -1;
66 }, null, new v8.InternalPackedArray('x', 'x')));
67
68 return {
69 privateSymbol: v8.createPrivateSymbol('sym'),
70 fulfilledPromise, // should be fulfilled with 1
71 fulfilledPromise2, // should be fulfilled with 2
72 rejectedPromise // should be rejected with 3
73 };
74 };
75})