Index: test/cctest/test-parsing.cc |
diff --git a/test/cctest/test-parsing.cc b/test/cctest/test-parsing.cc |
index 8d51f8caa5f9d2b5b92784f2c92a24c8a477f197..f2effbb76c3235c44270bc7201fa4bc3b677f01d 100755 |
--- a/test/cctest/test-parsing.cc |
+++ b/test/cctest/test-parsing.cc |
@@ -744,3 +744,133 @@ TEST(RegExpScanning) { |
TestScanRegExp("/=/", "="); |
TestScanRegExp("/=?/", "=?"); |
} |
+ |
+ |
+TEST(ScopePositiosn) { |
+ // Test the parser for correctly setting the start and end positions |
+ // of a scope. We check the scope positions of exactly one scope |
+ // nested in the global scope of a program. 'inner source' is the |
+ // source code that determines the part of the source belonging |
+ // to the nested scope. 'outer_prefix' and 'outer_suffix' are |
+ // parts of the source that belong to the global scope. |
+ struct SourceData { |
+ const char* outer_prefix; |
+ const char* inner_source; |
+ const char* outer_suffix; |
+ i::ScopeType scope_type; |
+ }; |
+ |
+ const SourceData source_data[] = { |
+ { " with ({}) ", "{ block; }", " more;", i::WITH_SCOPE }, |
+ { " with ({}) ", "{ block; }", "; more;", i::WITH_SCOPE }, |
+ { " with ({}) ", "{\n" |
+ " block;\n" |
+ " }", "\n" |
+ " more;", i::WITH_SCOPE }, |
+ { " with ({}) ", "statement;", " more;", i::WITH_SCOPE }, |
+ { " with ({}) ", "statement", "\n" |
+ " more;", i::WITH_SCOPE }, |
+ { " with ({})\n" |
+ " ", "statement;", "\n" |
+ " more;", i::WITH_SCOPE }, |
+ { " try {} catch ", "(e) { block; }", " more;", i::CATCH_SCOPE }, |
+ { " try {} catch ", "(e) { block; }", "; more;", i::CATCH_SCOPE }, |
+ { " try {} catch ", "(e) {\n" |
+ " block;\n" |
+ " }", "\n" |
+ " more;", i::CATCH_SCOPE }, |
+ { " try {} catch ", "(e) { block; }", " finally { block; } more;", |
+ i::CATCH_SCOPE }, |
+ { " start;\n" |
+ " ", "{ let block; }", " more;", i::BLOCK_SCOPE }, |
+ { " start;\n" |
+ " ", "{ let block; }", "; more;", i::BLOCK_SCOPE }, |
+ { " start;\n" |
+ " ", "{\n" |
+ " let block;\n" |
+ " }", "\n" |
+ " more;", i::BLOCK_SCOPE }, |
+ { " start;\n" |
+ " function fun", "(a,b) { infunction; }", " more;", |
+ i::FUNCTION_SCOPE }, |
+ { " start;\n" |
+ " function fun", "(a,b) {\n" |
+ " infunction;\n" |
+ " }", "\n" |
+ " more;", i::FUNCTION_SCOPE }, |
+ { " (function fun", "(a,b) { infunction; }", ")();", |
+ i::FUNCTION_SCOPE }, |
+ { " for ", "(let x = 1 ; x < 10; ++ x) { block; }", " more;", |
+ i::BLOCK_SCOPE }, |
+ { " for ", "(let x = 1 ; x < 10; ++ x) { block; }", "; more;", |
+ i::BLOCK_SCOPE }, |
+ { " for ", "(let x = 1 ; x < 10; ++ x) {\n" |
+ " block;\n" |
+ " }", "\n" |
+ " more;", i::BLOCK_SCOPE }, |
+ { " for ", "(let x = 1 ; x < 10; ++ x) statement;", " more;", |
+ i::BLOCK_SCOPE }, |
+ { " for ", "(let x = 1 ; x < 10; ++ x) statement", "\n" |
+ " more;", i::BLOCK_SCOPE }, |
+ { " for ", "(let x = 1 ; x < 10; ++ x)\n" |
+ " statement;", "\n" |
+ " more;", i::BLOCK_SCOPE }, |
+ { " for ", "(let x in {}) { block; }", " more;", i::BLOCK_SCOPE }, |
+ { " for ", "(let x in {}) { block; }", "; more;", i::BLOCK_SCOPE }, |
+ { " for ", "(let x in {}) {\n" |
+ " block;\n" |
+ " }", "\n" |
+ " more;", i::BLOCK_SCOPE }, |
+ { " for ", "(let x in {}) statement;", " more;", i::BLOCK_SCOPE }, |
+ { " for ", "(let x in {}) statement", "\n" |
+ " more;", i::BLOCK_SCOPE }, |
+ { " for ", "(let x in {})\n" |
+ " statement;", "\n" |
+ " more;", i::BLOCK_SCOPE }, |
+ { NULL, NULL, NULL, i::EVAL_SCOPE } |
+ }; |
+ |
+ v8::HandleScope handles; |
+ v8::Persistent<v8::Context> context = v8::Context::New(); |
+ v8::Context::Scope context_scope(context); |
+ |
+ int marker; |
+ i::Isolate::Current()->stack_guard()->SetStackLimit( |
+ reinterpret_cast<uintptr_t>(&marker) - 128 * 1024); |
+ |
+ for (int i = 0; source_data[i].outer_prefix; i++) { |
+ size_t kPrefixLen = strlen(source_data[i].outer_prefix); |
+ size_t kInnerLen = strlen(source_data[i].inner_source); |
+ size_t kSuffixLen = strlen(source_data[i].outer_suffix); |
+ size_t kProgramSize = kPrefixLen + kInnerLen + kSuffixLen; |
+ i::SmartArrayPointer<char> program( |
+ reinterpret_cast<char*>(malloc(kProgramSize + 1))); |
+ snprintf(*program, kProgramSize + 1, "%s%s%s", |
+ source_data[i].outer_prefix, |
+ source_data[i].inner_source, |
+ source_data[i].outer_suffix); |
+ |
+ // Parse program source. |
+ i::Handle<i::String> source( |
+ FACTORY->NewStringFromAscii(i::CStrVector(*program))); |
+ i::Handle<i::Script> script = FACTORY->NewScript(source); |
+ i::Parser parser(script, false, NULL, NULL); |
+ parser.SetHarmonyScoping(true); |
+ i::FunctionLiteral* function = |
+ parser.ParseProgram(source, true, i::kNonStrictMode); |
+ |
+ // Check scope types and positions. |
+ i::Scope* scope = function->scope(); |
+ CHECK(scope->is_global_scope()); |
+ CHECK_EQ(scope->start_position(), 0); |
+ CHECK_EQ(scope->end_position(), kProgramSize); |
+ CHECK_EQ(scope->inner_scopes()->length(), 1); |
+ |
+ i::Scope* inner_scope = scope->inner_scopes()->at(0); |
+ CHECK_EQ(inner_scope->type(), source_data[i].scope_type); |
+ CHECK_EQ(inner_scope->start_position(), kPrefixLen); |
+ // The end position of a token is one position after the last |
+ // character belonging to that token. |
+ CHECK_EQ(inner_scope->end_position(), kPrefixLen + kInnerLen); |
+ } |
+} |