};
-/**
- * Pre-compilation data that can be associated with a script. This
- * data can be calculated for a script in advance of actually
- * compiling it, and can be stored between compilations. When script
- * data is given to the compile method compilation will be faster.
- */
-class V8_EXPORT ScriptData { // NOLINT
- public:
- virtual ~ScriptData() { }
-
- /**
- * Pre-compiles the specified script (context-independent).
- *
- * NOTE: Pre-compilation using this method cannot happen on another thread
- * without using Lockers.
- *
- * \param source Script source code.
- */
- static ScriptData* PreCompile(Handle<String> source);
-
- /**
- * Load previous pre-compilation data.
- *
- * \param data Pointer to data returned by a call to Data() of a previous
- * ScriptData. Ownership is not transferred.
- * \param length Length of data.
- */
- static ScriptData* New(const char* data, int length);
-
- /**
- * Returns the length of Data().
- */
- virtual int Length() = 0;
-
- /**
- * Returns a serialized representation of this ScriptData that can later be
- * passed to New(). NOTE: Serialized data is platform-dependent.
- */
- virtual const char* Data() = 0;
-
- /**
- * Returns true if the source code could not be parsed.
- */
- virtual bool HasError() = 0;
-};
-
-
/**
* The origin, within a file, of a script.
*/
public:
/**
* A shorthand for ScriptCompiler::Compile().
- * The ScriptData parameter will be deprecated; use ScriptCompiler::Compile if
- * you want to pass it.
*/
static Local<Script> Compile(Handle<String> source,
- ScriptOrigin* origin = NULL,
- ScriptData* script_data = NULL);
+ ScriptOrigin* origin = NULL);
// To be decprecated, use the Compile above.
static Local<Script> Compile(Handle<String> source,
}
-// --- S c r i p t D a t a ---
-
-
-ScriptData* ScriptData::PreCompile(v8::Handle<String> source) {
- i::Handle<i::String> str = Utils::OpenHandle(*source);
- i::Isolate* isolate = str->GetIsolate();
- if (str->IsExternalTwoByteString()) {
- i::ExternalTwoByteStringUtf16CharacterStream stream(
- i::Handle<i::ExternalTwoByteString>::cast(str), 0, str->length());
- return i::PreParserApi::PreParse(isolate, &stream);
- } else {
- i::GenericStringUtf16CharacterStream stream(str, 0, str->length());
- return i::PreParserApi::PreParse(isolate, &stream);
- }
-}
-
-
-ScriptData* ScriptData::New(const char* data, int length) {
- // Return an empty ScriptData if the length is obviously invalid.
- if (length % sizeof(unsigned) != 0) {
- return new i::ScriptDataImpl();
- }
-
- // Copy the data to ensure it is properly aligned.
- int deserialized_data_length = length / sizeof(unsigned);
- // If aligned, don't create a copy of the data.
- if (reinterpret_cast<intptr_t>(data) % sizeof(unsigned) == 0) {
- return new i::ScriptDataImpl(data, length);
- }
- // Copy the data to align it.
- unsigned* deserialized_data = i::NewArray<unsigned>(deserialized_data_length);
- i::CopyBytes(reinterpret_cast<char*>(deserialized_data),
- data, static_cast<size_t>(length));
-
- return new i::ScriptDataImpl(
- i::Vector<unsigned>(deserialized_data, deserialized_data_length));
-}
-
-
// --- S c r i p t s ---
Isolate* v8_isolate,
Source* source,
CompileOptions options) {
- i::ScriptDataImpl* script_data_impl = NULL;
+ i::ScriptData* script_data_impl = NULL;
i::CachedDataMode cached_data_mode = i::NO_CACHED_DATA;
if (options & kProduceDataToCache) {
cached_data_mode = i::PRODUCE_CACHED_DATA;
source->cached_data = NULL;
}
} else if (source->cached_data) {
- // FIXME(marja): Make compiler use CachedData directly. Aligning needs to be
- // taken care of.
- script_data_impl = static_cast<i::ScriptDataImpl*>(ScriptData::New(
+ // ScriptData takes care of aligning, in case the data is not aligned
+ // correctly.
+ script_data_impl = i::ScriptData::New(
reinterpret_cast<const char*>(source->cached_data->data),
- source->cached_data->length));
+ source->cached_data->length);
// We assert that the pre-data is sane, even though we can actually
// handle it if it turns out not to be in release mode.
ASSERT(script_data_impl->SanityCheck());
Local<Script> Script::Compile(v8::Handle<String> source,
- v8::ScriptOrigin* origin,
- ScriptData* script_data) {
+ v8::ScriptOrigin* origin) {
i::Handle<i::String> str = Utils::OpenHandle(*source);
- ScriptCompiler::CachedData* cached_data = NULL;
- if (script_data) {
- cached_data = new ScriptCompiler::CachedData(
- reinterpret_cast<const uint8_t*>(script_data->Data()),
- script_data->Length());
- }
if (origin) {
- ScriptCompiler::Source script_source(source, *origin, cached_data);
+ ScriptCompiler::Source script_source(source, *origin);
return ScriptCompiler::Compile(
reinterpret_cast<v8::Isolate*>(str->GetIsolate()),
&script_source);
}
- ScriptCompiler::Source script_source(source, cached_data);
+ ScriptCompiler::Source script_source(source);
return ScriptCompiler::Compile(
reinterpret_cast<v8::Isolate*>(str->GetIsolate()),
&script_source);
bool is_shared_cross_origin,
Handle<Context> context,
v8::Extension* extension,
- ScriptDataImpl** cached_data,
+ ScriptData** cached_data,
CachedDataMode cached_data_mode,
NativesFlag natives) {
if (cached_data_mode == NO_CACHED_DATA) {
namespace v8 {
namespace internal {
-class ScriptDataImpl;
+class ScriptData;
class HydrogenCodeStub;
// ParseRestriction is used to restrict the set of valid statements in a
Handle<Script> script() const { return script_; }
HydrogenCodeStub* code_stub() const {return code_stub_; }
v8::Extension* extension() const { return extension_; }
- ScriptDataImpl** cached_data() const { return cached_data_; }
+ ScriptData** cached_data() const { return cached_data_; }
CachedDataMode cached_data_mode() const {
return cached_data_mode_;
}
ASSERT(!is_lazy());
extension_ = extension;
}
- void SetCachedData(ScriptDataImpl** cached_data,
+ void SetCachedData(ScriptData** cached_data,
CachedDataMode cached_data_mode) {
cached_data_mode_ = cached_data_mode;
if (cached_data_mode == NO_CACHED_DATA) {
// Fields possibly needed for eager compilation, NULL by default.
v8::Extension* extension_;
- ScriptDataImpl** cached_data_;
+ ScriptData** cached_data_;
CachedDataMode cached_data_mode_;
// The context of the caller for eval code, and the global context for a
bool is_shared_cross_origin,
Handle<Context> context,
v8::Extension* extension,
- ScriptDataImpl** cached_data,
+ ScriptData** cached_data,
CachedDataMode cached_data_mode,
NativesFlag is_natives_code);
}
-FunctionEntry ScriptDataImpl::GetFunctionEntry(int start) {
+ScriptData* ScriptData::New(const char* data, int length) {
+ // The length is obviously invalid.
+ if (length % sizeof(unsigned) != 0) {
+ return new ScriptData();
+ }
+
+ int deserialized_data_length = length / sizeof(unsigned);
+ unsigned* deserialized_data;
+ ScriptData* script_data = new ScriptData();
+ script_data->owns_store_ =
+ reinterpret_cast<intptr_t>(data) % sizeof(unsigned) != 0;
+ if (script_data->owns_store_) {
+ // Copy the data to align it.
+ deserialized_data = i::NewArray<unsigned>(deserialized_data_length);
+ i::CopyBytes(reinterpret_cast<char*>(deserialized_data),
+ data, static_cast<size_t>(length));
+ } else {
+ // If aligned, don't create a copy of the data.
+ deserialized_data = reinterpret_cast<unsigned*>(const_cast<char*>(data));
+ }
+ script_data->store_ =
+ Vector<unsigned>(deserialized_data, deserialized_data_length);
+ return script_data;
+}
+
+
+FunctionEntry ScriptData::GetFunctionEntry(int start) {
// The current pre-data entry must be a FunctionEntry with the given
// start position.
if ((function_index_ + FunctionEntry::kSize <= store_.length())
}
-int ScriptDataImpl::GetSymbolIdentifier() {
+int ScriptData::GetSymbolIdentifier() {
return ReadNumber(&symbol_data_);
}
-bool ScriptDataImpl::SanityCheck() {
+bool ScriptData::SanityCheck() {
// Check that the header data is valid and doesn't specify
// point to positions outside the store.
if (store_.length() < PreparseDataConstants::kHeaderSize) return false;
-const char* ScriptDataImpl::ReadString(unsigned* start, int* chars) {
+const char* ScriptData::ReadString(unsigned* start, int* chars) {
int length = start[0];
char* result = NewArray<char>(length + 1);
for (int i = 0; i < length; i++) {
}
-Scanner::Location ScriptDataImpl::MessageLocation() const {
+Scanner::Location ScriptData::MessageLocation() const {
int beg_pos = Read(PreparseDataConstants::kMessageStartPos);
int end_pos = Read(PreparseDataConstants::kMessageEndPos);
return Scanner::Location(beg_pos, end_pos);
}
-bool ScriptDataImpl::IsReferenceError() const {
+bool ScriptData::IsReferenceError() const {
return Read(PreparseDataConstants::kIsReferenceErrorPos);
}
-const char* ScriptDataImpl::BuildMessage() const {
+const char* ScriptData::BuildMessage() const {
unsigned* start = ReadAddress(PreparseDataConstants::kMessageTextPos);
return ReadString(start, NULL);
}
-Vector<const char*> ScriptDataImpl::BuildArgs() const {
+Vector<const char*> ScriptData::BuildArgs() const {
int arg_count = Read(PreparseDataConstants::kMessageArgCountPos);
const char** array = NewArray<const char*>(arg_count);
// Position after text found by skipping past length field and
}
-unsigned ScriptDataImpl::Read(int position) const {
+unsigned ScriptData::Read(int position) const {
return store_[PreparseDataConstants::kHeaderSize + position];
}
-unsigned* ScriptDataImpl::ReadAddress(int position) const {
+unsigned* ScriptData::ReadAddress(int position) const {
return &store_[PreparseDataConstants::kHeaderSize + position];
}
}
if (cached_data_mode_ == PRODUCE_CACHED_DATA) {
Vector<unsigned> store = recorder.ExtractData();
- *cached_data_ = new ScriptDataImpl(store);
+ *cached_data_ = new ScriptData(store);
log_ = NULL;
}
return result;
// ----------------------------------------------------------------------------
// The Parser interface.
-ScriptDataImpl::~ScriptDataImpl() {
+ScriptData::~ScriptData() {
if (owns_store_) store_.Dispose();
}
-int ScriptDataImpl::Length() {
+int ScriptData::Length() {
return store_.length() * sizeof(unsigned);
}
-const char* ScriptDataImpl::Data() {
+const char* ScriptData::Data() {
return reinterpret_cast<const char*>(store_.start());
}
-bool ScriptDataImpl::HasError() {
+bool ScriptData::HasError() {
return has_error();
}
-void ScriptDataImpl::Initialize() {
+void ScriptData::Initialize() {
// Prepares state for use.
if (store_.length() >= PreparseDataConstants::kHeaderSize) {
function_index_ = PreparseDataConstants::kHeaderSize;
}
-int ScriptDataImpl::ReadNumber(byte** source) {
+int ScriptData::ReadNumber(byte** source) {
// Reads a number from symbol_data_ in base 128. The most significant
// bit marks that there are more digits.
// If the first byte is 0x80 (kNumberTerminator), it would normally
}
-// Create a Scanner for the preparser to use as input, and preparse the source.
-ScriptDataImpl* PreParserApi::PreParse(Isolate* isolate,
- Utf16CharacterStream* source) {
- CompleteParserRecorder recorder;
- HistogramTimerScope timer(isolate->counters()->pre_parse());
- Scanner scanner(isolate->unicode_cache());
- intptr_t stack_limit = isolate->stack_guard()->real_climit();
- PreParser preparser(&scanner, &recorder, stack_limit);
- preparser.set_allow_lazy(true);
- preparser.set_allow_generators(FLAG_harmony_generators);
- preparser.set_allow_for_of(FLAG_harmony_iteration);
- preparser.set_allow_harmony_scoping(FLAG_harmony_scoping);
- preparser.set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals);
- scanner.Initialize(source);
- PreParser::PreParseResult result = preparser.PreParseProgram();
- if (result == PreParser::kPreParseStackOverflow) {
- isolate->StackOverflow();
- return NULL;
- }
-
- // Extract the accumulated data from the recorder as a single
- // contiguous vector that we are responsible for disposing.
- Vector<unsigned> store = recorder.ExtractData();
- return new ScriptDataImpl(store);
-}
-
-
bool RegExpParser::ParseRegExp(FlatStringReader* input,
bool multiline,
RegExpCompileData* result,
SetCachedData(info()->cached_data(), info()->cached_data_mode());
if (info()->cached_data_mode() == CONSUME_CACHED_DATA &&
(*info()->cached_data())->has_error()) {
- ScriptDataImpl* cached_data = *(info()->cached_data());
+ ScriptData* cached_data = *(info()->cached_data());
Scanner::Location loc = cached_data->MessageLocation();
const char* message = cached_data->BuildMessage();
Vector<const char*> args = cached_data->BuildArgs();
};
-class ScriptDataImpl : public ScriptData {
+class ScriptData {
public:
- explicit ScriptDataImpl(Vector<unsigned> store)
+ explicit ScriptData(Vector<unsigned> store)
: store_(store),
owns_store_(true) { }
- // Create an empty ScriptDataImpl that is guaranteed to not satisfy
+ // Create an empty ScriptData that is guaranteed to not satisfy
// a SanityCheck.
- ScriptDataImpl() : owns_store_(false) { }
+ ScriptData() : owns_store_(false) { }
- virtual ~ScriptDataImpl();
+ // The created ScriptData won't take ownership of the data. If the alignment
+ // is not correct, this will copy the data (and the created ScriptData will
+ // take ownership of the copy).
+ static ScriptData* New(const char* data, int length);
+
+ virtual ~ScriptData();
virtual int Length();
virtual const char* Data();
virtual bool HasError();
unsigned version() { return store_[PreparseDataConstants::kVersionOffset]; }
private:
+ // Disable copying and assigning; because of owns_store they won't be correct.
+ ScriptData(const ScriptData&);
+ ScriptData& operator=(const ScriptData&);
+
friend class v8::ScriptCompiler;
Vector<unsigned> store_;
unsigned char* symbol_data_;
// Reads a number from the current symbols
int ReadNumber(byte** source);
- ScriptDataImpl(const char* backing_store, int length)
- : store_(reinterpret_cast<unsigned*>(const_cast<char*>(backing_store)),
- length / static_cast<int>(sizeof(unsigned))),
- owns_store_(false) {
- ASSERT_EQ(0, static_cast<int>(
- reinterpret_cast<intptr_t>(backing_store) % sizeof(unsigned)));
- }
-
// Read strings written by ParserRecorder::WriteString.
static const char* ReadString(unsigned* start, int* chars);
-
- friend class ScriptData;
-};
-
-
-class PreParserApi {
- public:
- // Pre-parse a character stream and return full preparse data.
- //
- // This interface is here instead of in preparser.h because it instantiates a
- // preparser recorder object that is suited to the parser's purposes. Also,
- // the preparser doesn't know about ScriptDataImpl.
- static ScriptDataImpl* PreParse(Isolate* isolate,
- Utf16CharacterStream* source);
};
// Report syntax error
void ReportInvalidPreparseData(Handle<String> name, bool* ok);
- void SetCachedData(ScriptDataImpl** data,
+ void SetCachedData(ScriptData** data,
CachedDataMode cached_data_mode) {
cached_data_mode_ = cached_data_mode;
if (cached_data_mode == NO_CACHED_DATA) {
}
bool inside_with() const { return scope_->inside_with(); }
- ScriptDataImpl** cached_data() const { return cached_data_; }
+ ScriptData** cached_data() const { return cached_data_; }
CachedDataMode cached_data_mode() const { return cached_data_mode_; }
Scope* DeclarationScope(VariableMode mode) {
return IsLexicalVariableMode(mode)
PreParser* reusable_preparser_;
Scope* original_scope_; // for ES5 function declarations in sloppy eval
Target* target_stack_; // for break, continue statements
- ScriptDataImpl** cached_data_;
+ ScriptData** cached_data_;
CachedDataMode cached_data_mode_;
CompilationInfo* info_;
i::OS::MemCopy(serialized_data, cd->data, cd->length);
// Deserialize.
- v8::ScriptData* deserialized =
- v8::ScriptData::New(serialized_data, cd->length);
+ i::ScriptData* deserialized = i::ScriptData::New(serialized_data, cd->length);
// Verify that the original is the same as the deserialized.
CHECK_EQ(cd->length, deserialized->Length());
v8::V8::Initialize();
const char* data = "DONT CARE";
int invalid_size = 3;
- v8::ScriptData* sd = v8::ScriptData::New(data, invalid_size);
+ i::ScriptData* sd = i::ScriptData::New(data, invalid_size);
CHECK_EQ(0, sd->Length());
// want to modify the data before passing it back.
const v8::ScriptCompiler::CachedData* cd = source.GetCachedData();
// ScriptData does not take ownership of the buffers passed to it.
- v8::ScriptData* sd =
- v8::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length);
+ i::ScriptData* sd =
+ i::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length);
CHECK(!sd->HasError());
- // ScriptDataImpl private implementation details
+ // ScriptData private implementation details
const int kHeaderSize = i::PreparseDataConstants::kHeaderSize;
const int kFunctionEntrySize = i::FunctionEntry::kSize;
const int kFunctionEntryStartOffset = 0;
// will not be found when searching for it by position and we should fall
// back on eager compilation.
// ScriptData does not take ownership of the buffers passed to it.
- sd = v8::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length);
+ sd = i::ScriptData::New(reinterpret_cast<const char*>(cd->data), cd->length);
sd_data = reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data()));
sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryStartOffset] =
200;
preparser.set_allow_lazy(true);
i::PreParser::PreParseResult result = preparser.PreParseProgram();
CHECK_EQ(i::PreParser::kPreParseSuccess, result);
- i::ScriptDataImpl data(log.ExtractData());
+ i::ScriptData data(log.ExtractData());
CHECK(!data.has_error());
}
i::PreParser::PreParseResult result = preparser.PreParseProgram();
// Even in the case of a syntax error, kPreParseSuccess is returned.
CHECK_EQ(i::PreParser::kPreParseSuccess, result);
- i::ScriptDataImpl data(log.ExtractData());
+ i::ScriptData data(log.ExtractData());
CHECK(data.has_error());
}
}
preparser.set_allow_natives_syntax(true);
i::PreParser::PreParseResult result = preparser.PreParseProgram();
CHECK_EQ(i::PreParser::kPreParseSuccess, result);
- i::ScriptDataImpl data(log.ExtractData());
+ i::ScriptData data(log.ExtractData());
CHECK(!data.has_error());
}
}
preparser.set_allow_lazy(true);
i::PreParser::PreParseResult result = preparser.PreParseProgram();
CHECK_EQ(i::PreParser::kPreParseSuccess, result);
- i::ScriptDataImpl data(log.ExtractData());
+ i::ScriptData data(log.ExtractData());
// Data contains syntax error.
CHECK(data.has_error());
}
F::FakeWritingSymbolIdInPreParseData(&log, (3 << i) + (5 << (i - 6)));
}
i::Vector<unsigned> store = log.ExtractData();
- i::ScriptDataImpl script_data(store);
+ i::ScriptData script_data(store);
script_data.Initialize();
// Check that we get the same symbols back.
for (int i = 0; i < 18; ++i) {
i::PreParser::PreParseResult result = preparser.PreParseProgram();
// Even in the case of a syntax error, kPreParseSuccess is returned.
CHECK_EQ(i::PreParser::kPreParseSuccess, result);
- i::ScriptDataImpl data(log.ExtractData());
+ i::ScriptData data(log.ExtractData());
CHECK(data.has_error());
}
preparser.set_allow_lazy(true);
i::PreParser::PreParseResult result = preparser.PreParseProgram();
CHECK_EQ(i::PreParser::kPreParseSuccess, result);
- i::ScriptDataImpl data(log.ExtractData());
+ i::ScriptData data(log.ExtractData());
CHECK(!data.has_error());
data.Initialize();
}
-i::Handle<i::String> FormatMessage(i::ScriptDataImpl* data) {
+i::Handle<i::String> FormatMessage(i::ScriptData* data) {
i::Isolate* isolate = CcTest::i_isolate();
i::Factory* factory = isolate->factory();
const char* message = data->BuildMessage();
i::PreParser::PreParseResult result = preparser.PreParseProgram();
CHECK_EQ(i::PreParser::kPreParseSuccess, result);
}
- i::ScriptDataImpl data(log.ExtractData());
+ i::ScriptData data(log.ExtractData());
// Parse the data
i::FunctionLiteral* function;
factory->NewStringFromUtf8(i::CStrVector(program)));
i::Handle<i::Script> script = factory->NewScript(source);
i::CompilationInfoWithZone info(script);
- i::ScriptDataImpl* data = NULL;
+ i::ScriptData* data = NULL;
info.SetCachedData(&data, i::PRODUCE_CACHED_DATA);
i::Parser::Parse(&info, true);
CHECK(data);
TimeDelta parse_time1, parse_time2;
Handle<Script> script = Isolate::Current()->factory()->NewScript(
v8::Utils::OpenHandle(*source_handle));
- i::ScriptDataImpl* cached_data_impl = NULL;
+ i::ScriptData* cached_data_impl = NULL;
// First round of parsing (produce data to cache).
{
CompilationInfoWithZone info(script);