Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 35 #include "src/compiler.h" | 35 #include "src/compiler.h" |
| 36 #include "src/execution.h" | 36 #include "src/execution.h" |
| 37 #include "src/isolate.h" | 37 #include "src/isolate.h" |
| 38 #include "src/objects.h" | 38 #include "src/objects.h" |
| 39 #include "src/parser.h" | 39 #include "src/parser.h" |
| 40 #include "src/preparser.h" | 40 #include "src/preparser.h" |
| 41 #include "src/rewriter.h" | 41 #include "src/rewriter.h" |
| 42 #include "src/scanner-character-streams.h" | 42 #include "src/scanner-character-streams.h" |
| 43 #include "src/token.h" | 43 #include "src/token.h" |
| 44 #include "src/utils.h" | 44 #include "src/utils.h" |
| 45 | |
| 45 #include "test/cctest/cctest.h" | 46 #include "test/cctest/cctest.h" |
| 46 | 47 |
| 47 TEST(ScanKeywords) { | 48 TEST(ScanKeywords) { |
| 48 struct KeywordToken { | 49 struct KeywordToken { |
| 49 const char* keyword; | 50 const char* keyword; |
| 50 i::Token::Value token; | 51 i::Token::Value token; |
| 51 }; | 52 }; |
| 52 | 53 |
| 53 static const KeywordToken keywords[] = { | 54 static const KeywordToken keywords[] = { |
| 54 #define KEYWORD(t, s, d) { s, i::Token::t }, | 55 #define KEYWORD(t, s, d) { s, i::Token::t }, |
| (...skipping 2790 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2845 LocalContext env; | 2846 LocalContext env; |
| 2846 i::FLAG_lazy = true; | 2847 i::FLAG_lazy = true; |
| 2847 i::FLAG_min_preparse_length = 0; | 2848 i::FLAG_min_preparse_length = 0; |
| 2848 CompileRun("function this_is_lazy() {\n" | 2849 CompileRun("function this_is_lazy() {\n" |
| 2849 " break p;\n" | 2850 " break p;\n" |
| 2850 "}\n" | 2851 "}\n" |
| 2851 "this_is_lazy();\n"); | 2852 "this_is_lazy();\n"); |
| 2852 } | 2853 } |
| 2853 | 2854 |
| 2854 | 2855 |
| 2856 TEST(SerializationOfMaybeAssignmentFlag) { | |
| 2857 i::Isolate* isolate = CcTest::i_isolate(); | |
| 2858 i::Factory* factory = isolate->factory(); | |
| 2859 i::HandleScope scope(isolate); | |
| 2860 LocalContext env; | |
| 2861 | |
| 2862 const char* src = | |
| 2863 "function h() {" | |
| 2864 " var result = [];" | |
| 2865 " function f() {" | |
| 2866 " result.push(2);" | |
| 2867 " }" | |
| 2868 " function assertResult(r) {" | |
| 2869 " f();" | |
| 2870 " result = [];" | |
| 2871 " }" | |
| 2872 " assertResult([2]);" | |
| 2873 " assertResult([2]);" | |
| 2874 " return f;" | |
| 2875 "};" | |
| 2876 "h();"; | |
| 2877 | |
| 2878 i::ScopedVector<char> program(Utf8LengthHelper(src) + 1); | |
| 2879 i::SNPrintF(program, "%s", src); | |
| 2880 i::Handle<i::String> source = factory->InternalizeUtf8String(program.start()); | |
| 2881 source->PrintOn(stdout); | |
| 2882 printf("\n"); | |
| 2883 i::Zone zone(isolate); | |
| 2884 v8::Local<v8::Value> v = CompileRun(src); | |
| 2885 i::Handle<i::Object> o = v8::Utils::OpenHandle(*v); | |
| 2886 i::Handle<i::JSFunction> f = i::Handle<i::JSFunction>::cast(o); | |
| 2887 i::Context* context = f->context(); | |
| 2888 i::AstValueFactory avf(&zone, isolate->heap()->HashSeed()); | |
| 2889 avf.Internalize(isolate); | |
| 2890 const i::AstRawString* name = avf.GetOneByteString("result"); | |
| 2891 i::Handle<i::String> str = name->string(); | |
| 2892 CHECK(str->IsInternalizedString()); | |
| 2893 i::Scope* global_scope = | |
| 2894 new (&zone) i::Scope(NULL, i::GLOBAL_SCOPE, &avf, &zone); | |
| 2895 global_scope->Initialize(); | |
| 2896 i::Scope* s = i::Scope::DeserializeScopeChain(context, global_scope, &zone); | |
| 2897 ASSERT(s != global_scope); | |
| 2898 ASSERT(name != NULL); | |
| 2899 | |
| 2900 // Get result from h's function context (that is f's context) | |
| 2901 i::Variable* var = s->Lookup(name); | |
| 2902 | |
| 2903 CHECK(var != NULL); | |
| 2904 // Maybe assigned should survive deserialization | |
| 2905 CHECK(var->maybe_assigned() == i::kMaybeAssigned); | |
| 2906 // TODO(sigurds) Figure out if is_used should survive context serialization. | |
| 2907 } | |
| 2908 | |
| 2909 | |
| 2910 TEST(IfArgumentsArrayAccessedThenParametersMaybeAssigned) { | |
| 2911 i::Isolate* isolate = CcTest::i_isolate(); | |
| 2912 i::Factory* factory = isolate->factory(); | |
| 2913 i::HandleScope scope(isolate); | |
| 2914 LocalContext env; | |
| 2915 | |
| 2916 | |
| 2917 const char* src = | |
| 2918 "function f(x) {" | |
| 2919 " var a = arguments;" | |
| 2920 " function g(i) {" | |
| 2921 " ++a[0];" | |
| 2922 " };" | |
| 2923 " return g;" | |
| 2924 " }" | |
| 2925 "f(0);"; | |
| 2926 | |
| 2927 i::ScopedVector<char> program(Utf8LengthHelper(src) + 1); | |
| 2928 i::SNPrintF(program, "%s", src); | |
| 2929 i::Handle<i::String> source = factory->InternalizeUtf8String(program.start()); | |
| 2930 source->PrintOn(stdout); | |
| 2931 printf("\n"); | |
| 2932 i::Zone zone(isolate); | |
| 2933 v8::Local<v8::Value> v = CompileRun(src); | |
| 2934 i::Handle<i::Object> o = v8::Utils::OpenHandle(*v); | |
| 2935 i::Handle<i::JSFunction> f = i::Handle<i::JSFunction>::cast(o); | |
| 2936 i::Context* context = f->context(); | |
| 2937 i::AstValueFactory avf(&zone, isolate->heap()->HashSeed()); | |
| 2938 avf.Internalize(isolate); | |
| 2939 | |
| 2940 i::Scope* global_scope = | |
| 2941 new (&zone) i::Scope(NULL, i::GLOBAL_SCOPE, &avf, &zone); | |
| 2942 global_scope->Initialize(); | |
| 2943 i::Scope* s = i::Scope::DeserializeScopeChain(context, global_scope, &zone); | |
| 2944 ASSERT(s != global_scope); | |
| 2945 const i::AstRawString* name_x = avf.GetOneByteString("x"); | |
| 2946 | |
| 2947 // Get result from f's function context (that is g's outer context) | |
| 2948 i::Variable* var_x = s->Lookup(name_x); | |
| 2949 CHECK(var_x != NULL); | |
| 2950 CHECK(var_x->maybe_assigned() == i::kMaybeAssigned); | |
| 2951 } | |
| 2952 | |
| 2953 | |
| 2954 TEST(ExportsMaybeAssigned) { | |
| 2955 i::FLAG_use_strict = true; | |
| 2956 i::FLAG_harmony_scoping = true; | |
| 2957 i::FLAG_harmony_modules = true; | |
| 2958 | |
| 2959 i::Isolate* isolate = CcTest::i_isolate(); | |
| 2960 i::Factory* factory = isolate->factory(); | |
| 2961 i::HandleScope scope(isolate); | |
| 2962 LocalContext env; | |
| 2963 | |
| 2964 const char* src = | |
| 2965 "module A {" | |
| 2966 " export var x = 1;" | |
| 2967 " export function f() { return x };" | |
| 2968 " export const y = 2;" | |
| 2969 " module B {}" | |
| 2970 " export module C {}" | |
| 2971 "};" | |
| 2972 "A.f"; | |
| 2973 | |
| 2974 i::ScopedVector<char> program(Utf8LengthHelper(src) + 1); | |
| 2975 i::SNPrintF(program, "%s", src); | |
| 2976 i::Handle<i::String> source = factory->InternalizeUtf8String(program.start()); | |
| 2977 source->PrintOn(stdout); | |
| 2978 printf("\n"); | |
| 2979 i::Zone zone(isolate); | |
| 2980 v8::Local<v8::Value> v = CompileRun(src); | |
| 2981 i::Handle<i::Object> o = v8::Utils::OpenHandle(*v); | |
| 2982 i::Handle<i::JSFunction> f = i::Handle<i::JSFunction>::cast(o); | |
| 2983 i::Context* context = f->context(); | |
| 2984 i::AstValueFactory avf(&zone, isolate->heap()->HashSeed()); | |
| 2985 avf.Internalize(isolate); | |
| 2986 | |
| 2987 i::Scope* global_scope = | |
| 2988 new (&zone) i::Scope(NULL, i::GLOBAL_SCOPE, &avf, &zone); | |
| 2989 global_scope->Initialize(); | |
| 2990 i::Scope* s = i::Scope::DeserializeScopeChain(context, global_scope, &zone); | |
| 2991 ASSERT(s != global_scope); | |
| 2992 const i::AstRawString* name_x = avf.GetOneByteString("x"); | |
| 2993 const i::AstRawString* name_f = avf.GetOneByteString("f"); | |
| 2994 const i::AstRawString* name_y = avf.GetOneByteString("y"); | |
| 2995 const i::AstRawString* name_B = avf.GetOneByteString("B"); | |
| 2996 const i::AstRawString* name_C = avf.GetOneByteString("C"); | |
| 2997 | |
| 2998 // Get result from h's function context (that is f's context) | |
| 2999 i::Variable* var_x = s->Lookup(name_x); | |
| 3000 CHECK(var_x != NULL); | |
| 3001 CHECK(var_x->maybe_assigned() == i::kMaybeAssigned); | |
| 3002 i::Variable* var_f = s->Lookup(name_f); | |
| 3003 CHECK(var_f != NULL); | |
| 3004 CHECK(var_f->maybe_assigned() == i::kMaybeAssigned); | |
| 3005 i::Variable* var_y = s->Lookup(name_y); | |
| 3006 CHECK(var_y != NULL); | |
| 3007 CHECK(var_y->maybe_assigned() == i::kNotAssigned); | |
| 3008 i::Variable* var_B = s->Lookup(name_B); | |
| 3009 CHECK(var_B != NULL); | |
| 3010 CHECK(var_B->maybe_assigned() == i::kNotAssigned); | |
| 3011 i::Variable* var_C = s->Lookup(name_C); | |
| 3012 CHECK(var_C != NULL); | |
| 3013 CHECK(var_C->maybe_assigned() == i::kNotAssigned); | |
| 3014 } | |
| 3015 | |
| 3016 | |
| 2855 TEST(InnerAssignment) { | 3017 TEST(InnerAssignment) { |
| 2856 i::Isolate* isolate = CcTest::i_isolate(); | 3018 i::Isolate* isolate = CcTest::i_isolate(); |
| 2857 i::Factory* factory = isolate->factory(); | 3019 i::Factory* factory = isolate->factory(); |
| 2858 i::HandleScope scope(isolate); | 3020 i::HandleScope scope(isolate); |
| 2859 LocalContext env; | 3021 LocalContext env; |
| 2860 | 3022 |
| 3023 // Used to trigger lazy compilation of function | |
|
Michael Starzinger
2014/07/30 13:03:03
This comment should got to line 3108 below I think
| |
| 3024 | |
| 3025 | |
| 2861 const char* prefix = "function f() {"; | 3026 const char* prefix = "function f() {"; |
| 2862 const char* midfix = " function g() {"; | 3027 const char* midfix = " function g() {"; |
| 2863 const char* suffix = "}}"; | 3028 const char* suffix = "}}"; |
| 2864 struct { const char* source; bool assigned; bool strict; } outers[] = { | 3029 struct { const char* source; bool assigned; bool strict; } outers[] = { |
| 2865 // Actual assignments. | 3030 // Actual assignments. |
| 2866 { "var x; var x = 5;", true, false }, | 3031 { "var x; var x = 5;", true, false }, |
| 2867 { "var x; { var x = 5; }", true, false }, | 3032 { "var x; { var x = 5; }", true, false }, |
| 2868 { "'use strict'; let x; x = 6;", true, true }, | 3033 { "'use strict'; let x; x = 6;", true, true }, |
| 2869 { "var x = 5; function x() {}", true, false }, | 3034 { "var x = 5; function x() {}", true, false }, |
| 2870 // Actual non-assignments. | 3035 // Actual non-assignments. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2933 { "function h() { eval(''); }", true, false }, | 3098 { "function h() { eval(''); }", true, false }, |
| 2934 { "(function() { eval(''); })", true, false }, | 3099 { "(function() { eval(''); })", true, false }, |
| 2935 // Shadowing not recognized because of eval approximation. | 3100 // Shadowing not recognized because of eval approximation. |
| 2936 { "var x; eval('');", true, false }, | 3101 { "var x; eval('');", true, false }, |
| 2937 { "'use strict'; let x; eval('');", true, false }, | 3102 { "'use strict'; let x; eval('');", true, false }, |
| 2938 { "try {} catch(x) { eval(''); }", true, false }, | 3103 { "try {} catch(x) { eval(''); }", true, false }, |
| 2939 { "function x() { eval(''); }", true, false }, | 3104 { "function x() { eval(''); }", true, false }, |
| 2940 { "(function(x) { eval(''); })", true, false }, | 3105 { "(function(x) { eval(''); })", true, false }, |
| 2941 }; | 3106 }; |
| 2942 | 3107 |
| 3108 int comment_len = 2048; | |
| 3109 i::ScopedVector<char> comment(comment_len + 1); | |
| 3110 i::SNPrintF(comment, "/*%0*d*/", comment_len - 4, 0); | |
| 2943 int prefix_len = Utf8LengthHelper(prefix); | 3111 int prefix_len = Utf8LengthHelper(prefix); |
| 2944 int midfix_len = Utf8LengthHelper(midfix); | 3112 int midfix_len = Utf8LengthHelper(midfix); |
| 2945 int suffix_len = Utf8LengthHelper(suffix); | 3113 int suffix_len = Utf8LengthHelper(suffix); |
| 2946 for (unsigned i = 0; i < ARRAY_SIZE(outers); ++i) { | 3114 for (unsigned i = 0; i < ARRAY_SIZE(outers); ++i) { |
| 2947 const char* outer = outers[i].source; | 3115 const char* outer = outers[i].source; |
| 2948 int outer_len = Utf8LengthHelper(outer); | 3116 int outer_len = Utf8LengthHelper(outer); |
| 2949 for (unsigned j = 0; j < ARRAY_SIZE(inners); ++j) { | 3117 for (unsigned j = 0; j < ARRAY_SIZE(inners); ++j) { |
| 2950 if (outers[i].strict && inners[j].with) continue; | 3118 for (unsigned outer_lazy = 0; outer_lazy < 2; ++outer_lazy) { |
| 2951 const char* inner = inners[j].source; | 3119 for (unsigned inner_lazy = 0; inner_lazy < 2; ++inner_lazy) { |
| 2952 int inner_len = Utf8LengthHelper(inner); | 3120 if (outers[i].strict && inners[j].with) continue; |
| 2953 int len = prefix_len + outer_len + midfix_len + inner_len + suffix_len; | 3121 const char* inner = inners[j].source; |
| 2954 i::ScopedVector<char> program(len + 1); | 3122 int inner_len = Utf8LengthHelper(inner); |
| 2955 i::SNPrintF(program, "%s%s%s%s%s", prefix, outer, midfix, inner, suffix); | |
| 2956 i::Handle<i::String> source = | |
| 2957 factory->InternalizeUtf8String(program.start()); | |
| 2958 source->PrintOn(stdout); | |
| 2959 printf("\n"); | |
| 2960 | 3123 |
| 2961 i::Handle<i::Script> script = factory->NewScript(source); | 3124 int outer_comment_len = outer_lazy ? comment_len : 0; |
| 2962 i::CompilationInfoWithZone info(script); | 3125 int inner_comment_len = inner_lazy ? comment_len : 0; |
| 2963 i::Parser parser(&info); | 3126 const char* outer_comment = outer_lazy ? comment.start() : ""; |
| 2964 parser.set_allow_harmony_scoping(true); | 3127 const char* inner_comment = inner_lazy ? comment.start() : ""; |
| 2965 CHECK(parser.Parse()); | 3128 int len = prefix_len + outer_comment_len + outer_len + midfix_len + |
| 2966 CHECK(i::Rewriter::Rewrite(&info)); | 3129 inner_comment_len + inner_len + suffix_len; |
| 2967 CHECK(i::Scope::Analyze(&info)); | 3130 i::ScopedVector<char> program(len + 1); |
| 2968 CHECK(info.function() != NULL); | |
| 2969 | 3131 |
| 2970 i::Scope* scope = info.function()->scope(); | 3132 i::SNPrintF(program, "%s%s%s%s%s%s%s", prefix, outer_comment, outer, |
| 2971 CHECK_EQ(scope->inner_scopes()->length(), 1); | 3133 midfix, inner_comment, inner, suffix); |
| 2972 i::Scope* inner_scope = scope->inner_scopes()->at(0); | 3134 i::Handle<i::String> source = |
| 2973 const i::AstRawString* var_name = | 3135 factory->InternalizeUtf8String(program.start()); |
| 2974 info.ast_value_factory()->GetOneByteString("x"); | 3136 source->PrintOn(stdout); |
| 2975 i::Variable* var = inner_scope->Lookup(var_name); | 3137 printf("\n"); |
| 2976 bool expected = outers[i].assigned || inners[j].assigned; | 3138 |
| 2977 CHECK(var != NULL); | 3139 i::Handle<i::Script> script = factory->NewScript(source); |
| 2978 CHECK(var->is_used() || !expected); | 3140 i::CompilationInfoWithZone info(script); |
| 2979 CHECK(var->maybe_assigned() == expected); | 3141 i::Parser parser(&info); |
| 3142 parser.set_allow_harmony_scoping(true); | |
| 3143 CHECK(parser.Parse()); | |
| 3144 CHECK(i::Rewriter::Rewrite(&info)); | |
| 3145 CHECK(i::Scope::Analyze(&info)); | |
| 3146 CHECK(info.function() != NULL); | |
| 3147 | |
| 3148 i::Scope* scope = info.function()->scope(); | |
| 3149 CHECK_EQ(scope->inner_scopes()->length(), 1); | |
| 3150 i::Scope* inner_scope = scope->inner_scopes()->at(0); | |
| 3151 const i::AstRawString* var_name = | |
| 3152 info.ast_value_factory()->GetOneByteString("x"); | |
| 3153 i::Variable* var = inner_scope->Lookup(var_name); | |
| 3154 bool expected = outers[i].assigned || inners[j].assigned; | |
| 3155 CHECK(var != NULL); | |
| 3156 CHECK(var->is_used() || !expected); | |
| 3157 CHECK((var->maybe_assigned() == i::kMaybeAssigned) == expected); | |
| 3158 } | |
| 3159 } | |
| 2980 } | 3160 } |
| 2981 } | 3161 } |
| 2982 } | 3162 } |
| 2983 | 3163 |
| 2984 namespace { | 3164 namespace { |
| 2985 | 3165 |
| 2986 int* global_use_counts = NULL; | 3166 int* global_use_counts = NULL; |
| 2987 | 3167 |
| 2988 void MockUseCounterCallback(v8::Isolate* isolate, | 3168 void MockUseCounterCallback(v8::Isolate* isolate, |
| 2989 v8::Isolate::UseCounterFeature feature) { | 3169 v8::Isolate::UseCounterFeature feature) { |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3162 | 3342 |
| 3163 // Arrow has more precedence, this is the same as: foo ? bar : (baz = {}) | 3343 // Arrow has more precedence, this is the same as: foo ? bar : (baz = {}) |
| 3164 "foo ? bar : baz => {}", | 3344 "foo ? bar : baz => {}", |
| 3165 NULL | 3345 NULL |
| 3166 }; | 3346 }; |
| 3167 | 3347 |
| 3168 static const ParserFlag always_flags[] = {kAllowArrowFunctions}; | 3348 static const ParserFlag always_flags[] = {kAllowArrowFunctions}; |
| 3169 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0, | 3349 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0, |
| 3170 always_flags, ARRAY_SIZE(always_flags)); | 3350 always_flags, ARRAY_SIZE(always_flags)); |
| 3171 } | 3351 } |
| OLD | NEW |