Home | History | Annotate | Download | only in compiler
      1 // Copyright 2014 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 #include "src/v8.h"
      6 
      7 #include "test/cctest/compiler/function-tester.h"
      8 
      9 using namespace v8::internal;
     10 using namespace v8::internal::compiler;
     11 
     12 static const char* throws = NULL;
     13 
     14 static const char* load_tests[] = {
     15     "var x = a; r = x",                       "123",       "0",
     16     "var x = (r = x)",                        "undefined", "undefined",
     17     "var x = (a?1:2); r = x",                 "1",         "2",
     18     "const x = a; r = x",                     "123",       "0",
     19     "const x = (r = x)",                      "undefined", "undefined",
     20     "const x = (a?3:4); r = x",               "3",         "4",
     21     "'use strict'; const x = a; r = x",       "123",       "0",
     22     "'use strict'; const x = (r = x)",        throws,      throws,
     23     "'use strict'; const x = (a?5:6); r = x", "5",         "6",
     24     "'use strict'; let x = a; r = x",         "123",       "0",
     25     "'use strict'; let x = (r = x)",          throws,      throws,
     26     "'use strict'; let x = (a?7:8); r = x",   "7",         "8",
     27     NULL};
     28 
     29 static const char* store_tests[] = {
     30     "var x = 1; x = a; r = x",                     "123",  "0",
     31     "var x = (a?(x=4,2):3); r = x",                "2",    "3",
     32     "var x = (a?4:5); x = a; r = x",               "123",  "0",
     33     "const x = 1; x = a; r = x",                   "1",    "1",
     34     "const x = (a?(x=4,2):3); r = x",              "2",    "3",
     35     "const x = (a?4:5); x = a; r = x",             "4",    "5",
     36     // Assignments to 'const' are SyntaxErrors, handled by the parser,
     37     // hence we cannot test them here because they are early errors.
     38     "'use strict'; let x = 1; x = a; r = x",       "123",  "0",
     39     "'use strict'; let x = (a?(x=4,2):3); r = x",  throws, "3",
     40     "'use strict'; let x = (a?4:5); x = a; r = x", "123",  "0",
     41     NULL};
     42 
     43 static const char* bind_tests[] = {
     44     "if (a) { const x = a }; r = x;",            "123", "undefined",
     45     "for (; a > 0; a--) { const x = a }; r = x", "123", "undefined",
     46     // Re-initialization of variables other than legacy 'const' is not
     47     // possible due to sane variable scoping, hence no tests here.
     48     NULL};
     49 
     50 
     51 static void RunVariableTests(const char* source, const char* tests[]) {
     52   FLAG_harmony_scoping = true;
     53   EmbeddedVector<char, 512> buffer;
     54 
     55   for (int i = 0; tests[i] != NULL; i += 3) {
     56     SNPrintF(buffer, source, tests[i]);
     57     PrintF("#%d: %s\n", i / 3, buffer.start());
     58     FunctionTester T(buffer.start());
     59 
     60     // Check function with non-falsey parameter.
     61     if (tests[i + 1] != throws) {
     62       Handle<Object> r = v8::Utils::OpenHandle(*CompileRun(tests[i + 1]));
     63       T.CheckCall(r, T.Val(123), T.Val("result"));
     64     } else {
     65       T.CheckThrows(T.Val(123), T.Val("result"));
     66     }
     67 
     68     // Check function with falsey parameter.
     69     if (tests[i + 2] != throws) {
     70       Handle<Object> r = v8::Utils::OpenHandle(*CompileRun(tests[i + 2]));
     71       T.CheckCall(r, T.Val(0.0), T.Val("result"));
     72     } else {
     73       T.CheckThrows(T.Val(0.0), T.Val("result"));
     74     }
     75   }
     76 }
     77 
     78 
     79 TEST(StackLoadVariables) {
     80   const char* source = "(function(a,r) { %s; return r; })";
     81   RunVariableTests(source, load_tests);
     82 }
     83 
     84 
     85 TEST(ContextLoadVariables) {
     86   const char* source = "(function(a,r) { %s; function f() {x} return r; })";
     87   RunVariableTests(source, load_tests);
     88 }
     89 
     90 
     91 TEST(StackStoreVariables) {
     92   const char* source = "(function(a,r) { %s; return r; })";
     93   RunVariableTests(source, store_tests);
     94 }
     95 
     96 
     97 TEST(ContextStoreVariables) {
     98   const char* source = "(function(a,r) { %s; function f() {x} return r; })";
     99   RunVariableTests(source, store_tests);
    100 }
    101 
    102 
    103 TEST(StackInitializeVariables) {
    104   const char* source = "(function(a,r) { %s; return r; })";
    105   RunVariableTests(source, bind_tests);
    106 }
    107 
    108 
    109 TEST(ContextInitializeVariables) {
    110   const char* source = "(function(a,r) { %s; function f() {x} return r; })";
    111   RunVariableTests(source, bind_tests);
    112 }
    113 
    114 
    115 TEST(SelfReferenceVariable) {
    116   FunctionTester T("(function self() { return self; })");
    117 
    118   T.CheckCall(T.function);
    119   CompileRun("var self = 'not a function'");
    120   T.CheckCall(T.function);
    121 }
    122