From ad5e73fb85fbd34e082cb2903b29027de0f3b14c Mon Sep 17 00:00:00 2001 From: "ager@chromium.org" Date: Tue, 13 Jul 2010 10:29:31 +0000 Subject: [PATCH] Fix preparsing from a source string that is not external. This fixes issue 775. Review URL: http://codereview.chromium.org/2959007 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@5050 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/compiler.cc | 7 +------ src/scanner.cc | 15 ++++++++++----- test/cctest/test-api.cc | 19 +++++++++++++++---- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/compiler.cc b/src/compiler.cc index ebb9743..8218921 100755 --- a/src/compiler.cc +++ b/src/compiler.cc @@ -275,9 +275,6 @@ static Handle MakeFunctionInfo(bool is_global, } -static StaticResource safe_string_input_buffer; - - Handle Compiler::Compile(Handle source, Handle script_name, int line_offset, @@ -306,9 +303,7 @@ Handle Compiler::Compile(Handle source, // No cache entry found. Do pre-parsing and compile the script. ScriptDataImpl* pre_data = input_pre_data; if (pre_data == NULL && source_length >= FLAG_min_preparse_length) { - Access buf(&safe_string_input_buffer); - buf->Reset(source.location()); - pre_data = PreParse(source, buf.value(), extension); + pre_data = PreParse(source, NULL, extension); } // Create a script object describing the script to be compiled. diff --git a/src/scanner.cc b/src/scanner.cc index 286f515..ca0e2d8 100755 --- a/src/scanner.cc +++ b/src/scanner.cc @@ -341,8 +341,7 @@ Scanner::Scanner(ParserMode pre) void Scanner::Initialize(Handle source, ParserLanguage language) { - safe_string_input_buffer_.Reset(source.location()); - Init(source, &safe_string_input_buffer_, 0, source->length(), language); + Init(source, NULL, 0, source->length(), language); } @@ -357,9 +356,7 @@ void Scanner::Initialize(Handle source, int start_position, int end_position, ParserLanguage language) { - safe_string_input_buffer_.Reset(source.location()); - Init(source, &safe_string_input_buffer_, - start_position, end_position, language); + Init(source, NULL, start_position, end_position, language); } @@ -368,6 +365,10 @@ void Scanner::Init(Handle source, int start_position, int end_position, ParserLanguage language) { + // Either initialize the scanner from a character stream or from a + // string. + ASSERT(source.is_null() || stream == NULL); + // Initialize the source buffer. if (!source.is_null() && StringShape(*source).IsExternalTwoByte()) { two_byte_string_buffer_.Initialize( @@ -382,6 +383,10 @@ void Scanner::Init(Handle source, end_position); source_ = &ascii_string_buffer_; } else { + if (!source.is_null()) { + safe_string_input_buffer_.Reset(source.location()); + stream = &safe_string_input_buffer_; + } char_stream_buffer_.Initialize(source, stream, start_position, diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index 20f8eae..7005873 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -8612,20 +8612,31 @@ TEST(PreCompileAPIVariationsAreSame) { v8::HandleScope scope; const char* cstring = "function foo(a) { return a+1; }"; + v8::ScriptData* sd_from_cstring = v8::ScriptData::PreCompile(cstring, i::StrLength(cstring)); TestAsciiResource* resource = new TestAsciiResource(cstring); - v8::ScriptData* sd_from_istring = v8::ScriptData::PreCompile( + v8::ScriptData* sd_from_external_string = v8::ScriptData::PreCompile( v8::String::NewExternal(resource)); - CHECK_EQ(sd_from_cstring->Length(), sd_from_istring->Length()); + v8::ScriptData* sd_from_string = v8::ScriptData::PreCompile( + v8::String::New(cstring)); + + CHECK_EQ(sd_from_cstring->Length(), sd_from_external_string->Length()); + CHECK_EQ(0, memcmp(sd_from_cstring->Data(), + sd_from_external_string->Data(), + sd_from_cstring->Length())); + + CHECK_EQ(sd_from_cstring->Length(), sd_from_string->Length()); CHECK_EQ(0, memcmp(sd_from_cstring->Data(), - sd_from_istring->Data(), + sd_from_string->Data(), sd_from_cstring->Length())); + delete sd_from_cstring; - delete sd_from_istring; + delete sd_from_external_string; + delete sd_from_string; } -- 2.7.4