test-run-variables.cc 4.15 KB
Newer Older
1 2 3 4
// Copyright 2014 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
#include "src/api/api-inl.h"
6
#include "src/objects/objects-inl.h"
7 8
#include "test/cctest/compiler/function-tester.h"

9 10 11
namespace v8 {
namespace internal {
namespace compiler {
12

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
static const char* throws = nullptr;

static const char* load_tests[] = {"var x = a; r = x",
                                   "123",
                                   "0",
                                   "var x = (r = x)",
                                   "undefined",
                                   "undefined",
                                   "var x = (a?1:2); r = x",
                                   "1",
                                   "2",
                                   "const x = a; r = x",
                                   "123",
                                   "0",
                                   "const x = (a?3:4); r = x",
                                   "3",
                                   "4",
                                   "'use strict'; const x = a; r = x",
                                   "123",
                                   "0",
                                   "'use strict'; const x = (r = x)",
                                   throws,
                                   throws,
                                   "'use strict'; const x = (a?5:6); r = x",
                                   "5",
                                   "6",
                                   "'use strict'; let x = a; r = x",
                                   "123",
                                   "0",
                                   "'use strict'; let x = (r = x)",
                                   throws,
                                   throws,
                                   "'use strict'; let x = (a?7:8); r = x",
                                   "7",
                                   "8",
                                   nullptr};
49 50

static const char* store_tests[] = {
51 52
    "var x = 1; x = a; r = x", "123", "0", "var x = (a?(x=4,2):3); r = x", "2",
    "3", "var x = (a?4:5); x = a; r = x", "123", "0",
53 54
    // Assignments to 'const' are SyntaxErrors, handled by the parser,
    // hence we cannot test them here because they are early errors.
55 56 57
    "'use strict'; let x = 1; x = a; r = x", "123", "0",
    "'use strict'; let x = (a?(x=4,2):3); r = x", throws, "3",
    "'use strict'; let x = (a?4:5); x = a; r = x", "123", "0", nullptr};
58 59

static void RunVariableTests(const char* source, const char* tests[]) {
60
  base::EmbeddedVector<char, 512> buffer;
61

62
  for (int i = 0; tests[i] != nullptr; i += 3) {
63
    SNPrintF(buffer, source, tests[i]);
64 65
    PrintF("#%d: %s\n", i / 3, buffer.begin());
    FunctionTester T(buffer.begin());
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

    // Check function with non-falsey parameter.
    if (tests[i + 1] != throws) {
      Handle<Object> r = v8::Utils::OpenHandle(*CompileRun(tests[i + 1]));
      T.CheckCall(r, T.Val(123), T.Val("result"));
    } else {
      T.CheckThrows(T.Val(123), T.Val("result"));
    }

    // Check function with falsey parameter.
    if (tests[i + 2] != throws) {
      Handle<Object> r = v8::Utils::OpenHandle(*CompileRun(tests[i + 2]));
      T.CheckCall(r, T.Val(0.0), T.Val("result"));
    } else {
      T.CheckThrows(T.Val(0.0), T.Val("result"));
    }
  }
}


TEST(StackLoadVariables) {
  const char* source = "(function(a,r) { %s; return r; })";
  RunVariableTests(source, load_tests);
}


TEST(ContextLoadVariables) {
  const char* source = "(function(a,r) { %s; function f() {x} return r; })";
  RunVariableTests(source, load_tests);
}


TEST(StackStoreVariables) {
  const char* source = "(function(a,r) { %s; return r; })";
  RunVariableTests(source, store_tests);
}


TEST(ContextStoreVariables) {
  const char* source = "(function(a,r) { %s; function f() {x} return r; })";
  RunVariableTests(source, store_tests);
}


TEST(SelfReferenceVariable) {
  FunctionTester T("(function self() { return self; })");

  T.CheckCall(T.function);
  CompileRun("var self = 'not a function'");
  T.CheckCall(T.function);
}
117 118 119 120

}  // namespace compiler
}  // namespace internal
}  // namespace v8