OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <utility> | 5 #include <utility> |
6 | 6 |
7 #include "src/compiler/pipeline.h" | 7 #include "src/compiler/pipeline.h" |
8 #include "src/execution.h" | 8 #include "src/execution.h" |
9 #include "src/handles.h" | 9 #include "src/handles.h" |
10 #include "src/interpreter/bytecode-array-builder.h" | 10 #include "src/interpreter/bytecode-array-builder.h" |
(...skipping 2839 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2850 for (size_t i = 0; i < arraysize(const_decl); i++) { | 2850 for (size_t i = 0; i < arraysize(const_decl); i++) { |
2851 ScopedVector<char> script(1024); | 2851 ScopedVector<char> script(1024); |
2852 SNPrintF(script, "%s %s %s", prologue, const_decl[i].code_snippet, | 2852 SNPrintF(script, "%s %s %s", prologue, const_decl[i].code_snippet, |
2853 epilogue); | 2853 epilogue); |
2854 | 2854 |
2855 BytecodeGraphTester tester(isolate, zone, script.start(), "*"); | 2855 BytecodeGraphTester tester(isolate, zone, script.start(), "*"); |
2856 auto callable = tester.GetCallable<>(); | 2856 auto callable = tester.GetCallable<>(); |
2857 Handle<Object> return_value = callable().ToHandleChecked(); | 2857 Handle<Object> return_value = callable().ToHandleChecked(); |
2858 CHECK(return_value->SameValue(*const_decl[i].return_value())); | 2858 CHECK(return_value->SameValue(*const_decl[i].return_value())); |
2859 } | 2859 } |
2860 | |
2861 // Tests for Legacy constant. | |
2862 bool old_flag_legacy_const = FLAG_legacy_const; | |
2863 FLAG_legacy_const = true; | |
2864 | |
2865 ExpectedSnippet<0> legacy_const_decl[] = { | |
2866 {"return outerConst = 23;", {handle(Smi::FromInt(23), isolate)}}, | |
2867 {"outerConst = 30; return outerConst;", | |
2868 {handle(Smi::FromInt(10), isolate)}}, | |
2869 }; | |
2870 | |
2871 for (size_t i = 0; i < arraysize(legacy_const_decl); i++) { | |
2872 ScopedVector<char> script(1024); | |
2873 SNPrintF(script, "%s %s %s", prologue, legacy_const_decl[i].code_snippet, | |
2874 epilogue); | |
2875 | |
2876 BytecodeGraphTester tester(isolate, zone, script.start(), "*"); | |
2877 auto callable = tester.GetCallable<>(); | |
2878 Handle<Object> return_value = callable().ToHandleChecked(); | |
2879 CHECK(return_value->SameValue(*legacy_const_decl[i].return_value())); | |
2880 } | |
2881 | |
2882 FLAG_legacy_const = old_flag_legacy_const; | |
2883 } | 2860 } |
2884 | 2861 |
2885 TEST(BytecodeGraphBuilderIllegalConstDeclaration) { | 2862 TEST(BytecodeGraphBuilderIllegalConstDeclaration) { |
2886 HandleAndZoneScope scope; | 2863 HandleAndZoneScope scope; |
2887 Isolate* isolate = scope.main_isolate(); | 2864 Isolate* isolate = scope.main_isolate(); |
2888 Zone* zone = scope.main_zone(); | 2865 Zone* zone = scope.main_zone(); |
2889 | 2866 |
2890 ExpectedSnippet<0, const char*> illegal_const_decl[] = { | 2867 ExpectedSnippet<0, const char*> illegal_const_decl[] = { |
2891 {"const x = x = 10 + 3; return x;", | 2868 {"const x = x = 10 + 3; return x;", |
2892 {"Uncaught ReferenceError: x is not defined"}}, | 2869 {"Uncaught ReferenceError: x is not defined"}}, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2926 BytecodeGraphTester tester(isolate, zone, script.start()); | 2903 BytecodeGraphTester tester(isolate, zone, script.start()); |
2927 v8::Local<v8::String> message = tester.CheckThrowsReturnMessage()->Get(); | 2904 v8::Local<v8::String> message = tester.CheckThrowsReturnMessage()->Get(); |
2928 v8::Local<v8::String> expected_string = | 2905 v8::Local<v8::String> expected_string = |
2929 v8_str(illegal_const_decl[i].return_value()); | 2906 v8_str(illegal_const_decl[i].return_value()); |
2930 CHECK( | 2907 CHECK( |
2931 message->Equals(CcTest::isolate()->GetCurrentContext(), expected_string) | 2908 message->Equals(CcTest::isolate()->GetCurrentContext(), expected_string) |
2932 .FromJust()); | 2909 .FromJust()); |
2933 } | 2910 } |
2934 } | 2911 } |
2935 | 2912 |
2936 TEST(BytecodeGraphBuilderLegacyConstDeclaration) { | |
2937 bool old_flag_legacy_const = FLAG_legacy_const; | |
2938 FLAG_legacy_const = true; | |
2939 | |
2940 HandleAndZoneScope scope; | |
2941 Isolate* isolate = scope.main_isolate(); | |
2942 Zone* zone = scope.main_zone(); | |
2943 | |
2944 ExpectedSnippet<0> snippets[] = { | |
2945 {"const x = (x = 10) + 3; return x;", | |
2946 {handle(Smi::FromInt(13), isolate)}}, | |
2947 {"const x = 10; x = 20; return x;", {handle(Smi::FromInt(10), isolate)}}, | |
2948 {"var a = 10;\n" | |
2949 "for (var i = 0; i < 10; ++i) {\n" | |
2950 " const x = i;\n" // Legacy constants are not block scoped. | |
2951 " a = a + x;\n" | |
2952 "}\n" | |
2953 "return a;\n", | |
2954 {handle(Smi::FromInt(10), isolate)}}, | |
2955 {"const x = 20; eval('x = 10;'); return x;", | |
2956 {handle(Smi::FromInt(20), isolate)}}, | |
2957 }; | |
2958 | |
2959 for (size_t i = 0; i < arraysize(snippets); i++) { | |
2960 ScopedVector<char> script(1024); | |
2961 SNPrintF(script, "function %s() { %s }\n%s();", kFunctionName, | |
2962 snippets[i].code_snippet, kFunctionName); | |
2963 | |
2964 BytecodeGraphTester tester(isolate, zone, script.start()); | |
2965 auto callable = tester.GetCallable<>(); | |
2966 Handle<Object> return_value = callable().ToHandleChecked(); | |
2967 CHECK(return_value->SameValue(*snippets[i].return_value())); | |
2968 } | |
2969 | |
2970 FLAG_legacy_const = old_flag_legacy_const; | |
2971 } | |
2972 | |
2973 TEST(BytecodeGraphBuilderDebuggerStatement) { | 2913 TEST(BytecodeGraphBuilderDebuggerStatement) { |
2974 FLAG_expose_debug_as = "debug"; | 2914 FLAG_expose_debug_as = "debug"; |
2975 HandleAndZoneScope scope; | 2915 HandleAndZoneScope scope; |
2976 Isolate* isolate = scope.main_isolate(); | 2916 Isolate* isolate = scope.main_isolate(); |
2977 Zone* zone = scope.main_zone(); | 2917 Zone* zone = scope.main_zone(); |
2978 | 2918 |
2979 ExpectedSnippet<0> snippet = { | 2919 ExpectedSnippet<0> snippet = { |
2980 "var Debug = debug.Debug;" | 2920 "var Debug = debug.Debug;" |
2981 "var count = 0;" | 2921 "var count = 0;" |
2982 "function f() {" | 2922 "function f() {" |
(...skipping 14 matching lines...) Expand all Loading... |
2997 | 2937 |
2998 BytecodeGraphTester tester(isolate, zone, script.start()); | 2938 BytecodeGraphTester tester(isolate, zone, script.start()); |
2999 auto callable = tester.GetCallable<>(); | 2939 auto callable = tester.GetCallable<>(); |
3000 Handle<Object> return_value = callable().ToHandleChecked(); | 2940 Handle<Object> return_value = callable().ToHandleChecked(); |
3001 CHECK(return_value->SameValue(*snippet.return_value())); | 2941 CHECK(return_value->SameValue(*snippet.return_value())); |
3002 } | 2942 } |
3003 | 2943 |
3004 } // namespace compiler | 2944 } // namespace compiler |
3005 } // namespace internal | 2945 } // namespace internal |
3006 } // namespace v8 | 2946 } // namespace v8 |
OLD | NEW |