2008-09-09 20:08:45 +00:00
|
|
|
// Copyright 2006-2008 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-06-19 18:38:03 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2011-04-29 12:08:33 +00:00
|
|
|
#ifdef COMPRESS_STARTUP_DATA_BZ2
|
|
|
|
#include <bzlib.h>
|
|
|
|
#endif
|
2008-07-03 15:10:15 +00:00
|
|
|
#include <signal.h>
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-07-03 07:37:27 +00:00
|
|
|
#include "include/libplatform/libplatform.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/assembler.h"
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/platform/platform.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/bootstrapper.h"
|
|
|
|
#include "src/flags.h"
|
2014-06-20 08:40:11 +00:00
|
|
|
#include "src/list.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/natives.h"
|
|
|
|
#include "src/serialize.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-04-17 14:45:06 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
using namespace v8;
|
|
|
|
|
|
|
|
|
2011-04-29 12:08:33 +00:00
|
|
|
class Compressor {
|
2009-10-30 10:23:12 +00:00
|
|
|
public:
|
2011-04-29 12:08:33 +00:00
|
|
|
virtual ~Compressor() {}
|
2014-07-10 10:28:05 +00:00
|
|
|
virtual bool Compress(i::Vector<i::byte> input) = 0;
|
|
|
|
virtual i::Vector<i::byte>* output() = 0;
|
2011-04-29 12:08:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-04-24 12:31:10 +00:00
|
|
|
class SnapshotWriter {
|
|
|
|
public:
|
|
|
|
explicit SnapshotWriter(const char* snapshot_file)
|
|
|
|
: fp_(GetFileDescriptorOrDie(snapshot_file))
|
|
|
|
, raw_file_(NULL)
|
|
|
|
, raw_context_file_(NULL)
|
2014-06-23 13:52:17 +00:00
|
|
|
, startup_blob_file_(NULL)
|
|
|
|
, compressor_(NULL) {
|
2011-04-29 12:08:33 +00:00
|
|
|
}
|
2014-04-24 12:31:10 +00:00
|
|
|
|
|
|
|
~SnapshotWriter() {
|
|
|
|
fclose(fp_);
|
|
|
|
if (raw_file_) fclose(raw_file_);
|
|
|
|
if (raw_context_file_) fclose(raw_context_file_);
|
2014-06-23 13:52:17 +00:00
|
|
|
if (startup_blob_file_) fclose(startup_blob_file_);
|
2011-04-29 12:08:33 +00:00
|
|
|
}
|
2014-04-24 12:31:10 +00:00
|
|
|
|
|
|
|
void SetCompressor(Compressor* compressor) {
|
|
|
|
compressor_ = compressor;
|
2011-04-29 12:08:33 +00:00
|
|
|
}
|
2011-06-06 20:47:30 +00:00
|
|
|
|
2014-04-24 12:31:10 +00:00
|
|
|
void SetRawFiles(const char* raw_file, const char* raw_context_file) {
|
|
|
|
raw_file_ = GetFileDescriptorOrDie(raw_file);
|
|
|
|
raw_context_file_ = GetFileDescriptorOrDie(raw_context_file);
|
|
|
|
}
|
2011-04-29 12:08:33 +00:00
|
|
|
|
2014-06-23 13:52:17 +00:00
|
|
|
void SetStartupBlobFile(const char* startup_blob_file) {
|
|
|
|
if (startup_blob_file != NULL)
|
|
|
|
startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file);
|
|
|
|
}
|
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
void WriteSnapshot(const i::List<i::byte>& snapshot_data,
|
2014-04-24 12:31:10 +00:00
|
|
|
const i::Serializer& serializer,
|
2014-07-10 10:28:05 +00:00
|
|
|
const i::List<i::byte>& context_snapshot_data,
|
2014-04-24 12:31:10 +00:00
|
|
|
const i::Serializer& context_serializer) const {
|
2014-06-23 13:52:17 +00:00
|
|
|
WriteSnapshotFile(snapshot_data, serializer,
|
|
|
|
context_snapshot_data, context_serializer);
|
|
|
|
MaybeWriteStartupBlob(snapshot_data, serializer,
|
|
|
|
context_snapshot_data, context_serializer);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-07-10 10:28:05 +00:00
|
|
|
void MaybeWriteStartupBlob(const i::List<i::byte>& snapshot_data,
|
2014-06-23 13:52:17 +00:00
|
|
|
const i::Serializer& serializer,
|
2014-07-10 10:28:05 +00:00
|
|
|
const i::List<i::byte>& context_snapshot_data,
|
2014-06-23 13:52:17 +00:00
|
|
|
const i::Serializer& context_serializer) const {
|
|
|
|
if (!startup_blob_file_)
|
|
|
|
return;
|
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
i::List<i::byte> startup_blob;
|
2014-07-08 09:04:08 +00:00
|
|
|
i::ListSnapshotSink sink(&startup_blob);
|
2014-06-23 13:52:17 +00:00
|
|
|
|
2014-09-25 07:32:13 +00:00
|
|
|
int spaces[] = {i::NEW_SPACE, i::OLD_POINTER_SPACE,
|
|
|
|
i::OLD_DATA_SPACE, i::CODE_SPACE,
|
|
|
|
i::MAP_SPACE, i::CELL_SPACE,
|
|
|
|
i::PROPERTY_CELL_SPACE, i::LO_SPACE};
|
2014-06-23 13:52:17 +00:00
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
i::byte* snapshot_bytes = snapshot_data.begin();
|
2014-06-23 13:52:17 +00:00
|
|
|
sink.PutBlob(snapshot_bytes, snapshot_data.length(), "snapshot");
|
2014-10-15 14:04:53 +00:00
|
|
|
for (size_t i = 0; i < arraysize(spaces); ++i) {
|
|
|
|
i::Vector<const uint32_t> chunks =
|
|
|
|
serializer.FinalAllocationChunks(spaces[i]);
|
|
|
|
// For the start-up snapshot, none of the reservations has more than
|
|
|
|
// one chunk (reservation for each space fits onto a single page).
|
|
|
|
CHECK_EQ(1, chunks.length());
|
|
|
|
sink.PutInt(chunks[0], "spaces");
|
|
|
|
}
|
2014-06-23 13:52:17 +00:00
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
i::byte* context_bytes = context_snapshot_data.begin();
|
2014-06-23 13:52:17 +00:00
|
|
|
sink.PutBlob(context_bytes, context_snapshot_data.length(), "context");
|
2014-10-15 14:04:53 +00:00
|
|
|
for (size_t i = 0; i < arraysize(spaces); ++i) {
|
|
|
|
i::Vector<const uint32_t> chunks =
|
|
|
|
context_serializer.FinalAllocationChunks(spaces[i]);
|
|
|
|
// For the context snapshot, none of the reservations has more than
|
|
|
|
// one chunk (reservation for each space fits onto a single page).
|
|
|
|
CHECK_EQ(1, chunks.length());
|
|
|
|
sink.PutInt(chunks[0], "spaces");
|
|
|
|
}
|
2014-06-23 13:52:17 +00:00
|
|
|
|
|
|
|
size_t written = fwrite(startup_blob.begin(), 1, startup_blob.length(),
|
|
|
|
startup_blob_file_);
|
|
|
|
if (written != (size_t)startup_blob.length()) {
|
|
|
|
i::PrintF("Writing snapshot file failed.. Aborting.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
void WriteSnapshotFile(const i::List<i::byte>& snapshot_data,
|
2014-06-23 13:52:17 +00:00
|
|
|
const i::Serializer& serializer,
|
2014-07-10 10:28:05 +00:00
|
|
|
const i::List<i::byte>& context_snapshot_data,
|
2014-06-23 13:52:17 +00:00
|
|
|
const i::Serializer& context_serializer) const {
|
2014-04-24 12:31:10 +00:00
|
|
|
WriteFilePrefix();
|
|
|
|
WriteData("", snapshot_data, raw_file_);
|
|
|
|
WriteData("context_", context_snapshot_data, raw_context_file_);
|
|
|
|
WriteMeta("context_", context_serializer);
|
|
|
|
WriteMeta("", serializer);
|
|
|
|
WriteFileSuffix();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteFilePrefix() const {
|
2009-10-30 10:23:12 +00:00
|
|
|
fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n");
|
2014-06-03 08:12:43 +00:00
|
|
|
fprintf(fp_, "#include \"src/v8.h\"\n");
|
2014-06-30 13:25:46 +00:00
|
|
|
fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n");
|
2014-06-03 08:12:43 +00:00
|
|
|
fprintf(fp_, "#include \"src/snapshot.h\"\n\n");
|
2014-04-24 12:31:10 +00:00
|
|
|
fprintf(fp_, "namespace v8 {\n");
|
|
|
|
fprintf(fp_, "namespace internal {\n\n");
|
2009-10-30 10:23:12 +00:00
|
|
|
}
|
|
|
|
|
2014-04-24 12:31:10 +00:00
|
|
|
void WriteFileSuffix() const {
|
|
|
|
fprintf(fp_, "} // namespace internal\n");
|
|
|
|
fprintf(fp_, "} // namespace v8\n");
|
|
|
|
}
|
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
void WriteData(const char* prefix, const i::List<i::byte>& source_data,
|
2014-04-24 12:31:10 +00:00
|
|
|
FILE* raw_file) const {
|
2014-07-10 10:28:05 +00:00
|
|
|
const i::List<i::byte>* data_to_be_written = NULL;
|
|
|
|
i::List<i::byte> compressed_data;
|
2014-04-24 12:31:10 +00:00
|
|
|
if (!compressor_) {
|
|
|
|
data_to_be_written = &source_data;
|
|
|
|
} else if (compressor_->Compress(source_data.ToVector())) {
|
|
|
|
compressed_data.AddAll(*compressor_->output());
|
|
|
|
data_to_be_written = &compressed_data;
|
|
|
|
} else {
|
|
|
|
i::PrintF("Compression failed. Aborting.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(data_to_be_written);
|
2014-04-24 12:31:10 +00:00
|
|
|
MaybeWriteRawFile(data_to_be_written, raw_file);
|
|
|
|
WriteData(prefix, source_data, data_to_be_written);
|
2010-03-23 11:40:38 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
void MaybeWriteRawFile(const i::List<i::byte>* data, FILE* raw_file) const {
|
2014-04-24 12:31:10 +00:00
|
|
|
if (!data || !raw_file)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Sanity check, whether i::List iterators truly return pointers to an
|
|
|
|
// internal array.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(data->end() - data->begin() == data->length());
|
2014-04-24 12:31:10 +00:00
|
|
|
|
|
|
|
size_t written = fwrite(data->begin(), 1, data->length(), raw_file);
|
|
|
|
if (written != (size_t)data->length()) {
|
|
|
|
i::PrintF("Writing raw file failed.. Aborting.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-03-23 11:40:38 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
void WriteData(const char* prefix, const i::List<i::byte>& source_data,
|
|
|
|
const i::List<i::byte>* data_to_be_written) const {
|
2014-04-24 12:31:10 +00:00
|
|
|
fprintf(fp_, "const byte Snapshot::%sdata_[] = {\n", prefix);
|
2014-06-23 13:52:17 +00:00
|
|
|
WriteSnapshotData(data_to_be_written);
|
2014-04-24 12:31:10 +00:00
|
|
|
fprintf(fp_, "};\n");
|
|
|
|
fprintf(fp_, "const int Snapshot::%ssize_ = %d;\n", prefix,
|
|
|
|
data_to_be_written->length());
|
|
|
|
|
2014-06-23 13:52:17 +00:00
|
|
|
if (data_to_be_written == &source_data) {
|
2014-04-24 12:31:10 +00:00
|
|
|
fprintf(fp_, "const byte* Snapshot::%sraw_data_ = Snapshot::%sdata_;\n",
|
|
|
|
prefix, prefix);
|
|
|
|
fprintf(fp_, "const int Snapshot::%sraw_size_ = Snapshot::%ssize_;\n",
|
|
|
|
prefix, prefix);
|
|
|
|
} else {
|
|
|
|
fprintf(fp_, "const byte* Snapshot::%sraw_data_ = NULL;\n", prefix);
|
|
|
|
fprintf(fp_, "const int Snapshot::%sraw_size_ = %d;\n",
|
|
|
|
prefix, source_data.length());
|
|
|
|
}
|
|
|
|
fprintf(fp_, "\n");
|
2009-10-30 10:23:12 +00:00
|
|
|
}
|
|
|
|
|
2014-04-24 12:31:10 +00:00
|
|
|
void WriteMeta(const char* prefix, const i::Serializer& ser) const {
|
|
|
|
WriteSizeVar(ser, prefix, "new", i::NEW_SPACE);
|
|
|
|
WriteSizeVar(ser, prefix, "pointer", i::OLD_POINTER_SPACE);
|
|
|
|
WriteSizeVar(ser, prefix, "data", i::OLD_DATA_SPACE);
|
|
|
|
WriteSizeVar(ser, prefix, "code", i::CODE_SPACE);
|
|
|
|
WriteSizeVar(ser, prefix, "map", i::MAP_SPACE);
|
|
|
|
WriteSizeVar(ser, prefix, "cell", i::CELL_SPACE);
|
|
|
|
WriteSizeVar(ser, prefix, "property_cell", i::PROPERTY_CELL_SPACE);
|
2014-09-25 07:32:13 +00:00
|
|
|
WriteSizeVar(ser, prefix, "lo", i::LO_SPACE);
|
2014-04-24 12:31:10 +00:00
|
|
|
fprintf(fp_, "\n");
|
2009-10-30 10:23:12 +00:00
|
|
|
}
|
|
|
|
|
2014-04-24 12:31:10 +00:00
|
|
|
void WriteSizeVar(const i::Serializer& ser, const char* prefix,
|
|
|
|
const char* name, int space) const {
|
2014-10-15 14:04:53 +00:00
|
|
|
i::Vector<const uint32_t> chunks = ser.FinalAllocationChunks(space);
|
|
|
|
// For the start-up snapshot, none of the reservations has more than
|
|
|
|
// one chunk (total reservation fits into a single page).
|
|
|
|
CHECK_EQ(1, chunks.length());
|
|
|
|
fprintf(fp_, "const int Snapshot::%s%s_space_used_ = %d;\n", prefix, name,
|
|
|
|
chunks[0]);
|
2014-04-24 12:31:10 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
void WriteSnapshotData(const i::List<i::byte>* data) const {
|
2014-04-24 12:31:10 +00:00
|
|
|
for (int i = 0; i < data->length(); i++) {
|
|
|
|
if ((i & 0x1f) == 0x1f)
|
|
|
|
fprintf(fp_, "\n");
|
|
|
|
if (i > 0)
|
|
|
|
fprintf(fp_, ",");
|
|
|
|
fprintf(fp_, "%u", static_cast<unsigned char>(data->at(i)));
|
|
|
|
}
|
|
|
|
fprintf(fp_, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* GetFileDescriptorOrDie(const char* filename) {
|
2014-06-30 13:25:46 +00:00
|
|
|
FILE* fp = base::OS::FOpen(filename, "wb");
|
2014-04-24 12:31:10 +00:00
|
|
|
if (fp == NULL) {
|
|
|
|
i::PrintF("Unable to open file \"%s\" for writing.\n", filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return fp;
|
|
|
|
}
|
2011-04-29 12:08:33 +00:00
|
|
|
|
|
|
|
FILE* fp_;
|
2014-04-24 12:31:10 +00:00
|
|
|
FILE* raw_file_;
|
|
|
|
FILE* raw_context_file_;
|
2014-06-23 13:52:17 +00:00
|
|
|
FILE* startup_blob_file_;
|
2014-04-24 12:31:10 +00:00
|
|
|
Compressor* compressor_;
|
2011-04-29 12:08:33 +00:00
|
|
|
};
|
2010-01-18 16:04:25 +00:00
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
|
2011-04-29 12:08:33 +00:00
|
|
|
#ifdef COMPRESS_STARTUP_DATA_BZ2
|
|
|
|
class BZip2Compressor : public Compressor {
|
|
|
|
public:
|
|
|
|
BZip2Compressor() : output_(NULL) {}
|
|
|
|
virtual ~BZip2Compressor() {
|
|
|
|
delete output_;
|
|
|
|
}
|
|
|
|
virtual bool Compress(i::Vector<char> input) {
|
|
|
|
delete output_;
|
|
|
|
output_ = new i::ScopedVector<char>((input.length() * 101) / 100 + 1000);
|
|
|
|
unsigned int output_length_ = output_->length();
|
|
|
|
int result = BZ2_bzBuffToBuffCompress(output_->start(), &output_length_,
|
|
|
|
input.start(), input.length(),
|
|
|
|
9, 1, 0);
|
|
|
|
if (result == BZ_OK) {
|
|
|
|
output_->Truncate(output_length_);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "bzlib error code: %d\n", result);
|
|
|
|
return false;
|
2010-03-23 11:40:38 +00:00
|
|
|
}
|
2011-04-29 12:08:33 +00:00
|
|
|
}
|
|
|
|
virtual i::Vector<char>* output() { return output_; }
|
2010-03-23 11:40:38 +00:00
|
|
|
|
2009-10-30 10:23:12 +00:00
|
|
|
private:
|
2011-04-29 12:08:33 +00:00
|
|
|
i::ScopedVector<char>* output_;
|
2009-10-30 10:23:12 +00:00
|
|
|
};
|
2011-06-06 20:47:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BZip2Decompressor : public StartupDataDecompressor {
|
|
|
|
public:
|
|
|
|
virtual ~BZip2Decompressor() { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual int DecompressData(char* raw_data,
|
|
|
|
int* raw_data_size,
|
|
|
|
const char* compressed_data,
|
|
|
|
int compressed_data_size) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ(StartupData::kBZip2,
|
2011-06-06 20:47:30 +00:00
|
|
|
V8::GetCompressedStartupDataAlgorithm());
|
|
|
|
unsigned int decompressed_size = *raw_data_size;
|
|
|
|
int result =
|
|
|
|
BZ2_bzBuffToBuffDecompress(raw_data,
|
|
|
|
&decompressed_size,
|
|
|
|
const_cast<char*>(compressed_data),
|
|
|
|
compressed_data_size,
|
|
|
|
0, 1);
|
|
|
|
if (result == BZ_OK) {
|
|
|
|
*raw_data_size = decompressed_size;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
2011-04-29 12:08:33 +00:00
|
|
|
#endif
|
2009-10-30 10:23:12 +00:00
|
|
|
|
|
|
|
|
2013-04-29 11:49:24 +00:00
|
|
|
void DumpException(Handle<Message> message) {
|
|
|
|
String::Utf8Value message_string(message->Get());
|
|
|
|
String::Utf8Value message_line(message->GetSourceLine());
|
|
|
|
fprintf(stderr, "%s at line %d\n", *message_string, message->GetLineNumber());
|
|
|
|
fprintf(stderr, "%s\n", *message_line);
|
|
|
|
for (int i = 0; i <= message->GetEndColumn(); ++i) {
|
|
|
|
fprintf(stderr, "%c", i < message->GetStartColumn() ? ' ' : '^');
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
// By default, log code create information in the snapshot.
|
|
|
|
i::FLAG_log_code = true;
|
2011-07-13 09:09:04 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Print the usage if an error occurs when parsing the command line
|
|
|
|
// flags or if the help flag is set.
|
2014-05-16 15:18:24 +00:00
|
|
|
int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
|
2008-11-06 10:43:15 +00:00
|
|
|
if (result > 0 || argc != 2 || i::FLAG_help) {
|
2008-07-03 15:10:15 +00:00
|
|
|
::printf("Usage: %s [flag] ... outfile\n", argv[0]);
|
2008-09-12 10:31:37 +00:00
|
|
|
i::FlagList::PrintHelp();
|
2008-11-06 10:43:15 +00:00
|
|
|
return !i::FLAG_help;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2014-09-19 08:01:35 +00:00
|
|
|
|
|
|
|
i::CpuFeatures::Probe(true);
|
|
|
|
V8::InitializeICU();
|
|
|
|
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
|
|
|
|
v8::V8::InitializePlatform(platform);
|
|
|
|
v8::V8::Initialize();
|
|
|
|
|
2011-06-06 20:47:30 +00:00
|
|
|
#ifdef COMPRESS_STARTUP_DATA_BZ2
|
|
|
|
BZip2Decompressor natives_decompressor;
|
|
|
|
int bz2_result = natives_decompressor.Decompress();
|
|
|
|
if (bz2_result != BZ_OK) {
|
|
|
|
fprintf(stderr, "bzip error code: %d\n", bz2_result);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
2013-09-20 13:19:40 +00:00
|
|
|
i::FLAG_logfile_per_isolate = false;
|
|
|
|
|
2014-09-19 08:01:35 +00:00
|
|
|
Isolate::CreateParams params;
|
|
|
|
params.enable_serializer = true;
|
|
|
|
Isolate* isolate = v8::Isolate::New(params);
|
2014-05-22 09:36:20 +00:00
|
|
|
{ Isolate::Scope isolate_scope(isolate);
|
|
|
|
i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
|
|
|
|
|
|
|
|
Persistent<Context> context;
|
|
|
|
{
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
context.Reset(isolate, Context::New(isolate));
|
|
|
|
}
|
2013-05-08 07:45:16 +00:00
|
|
|
|
2014-05-22 09:36:20 +00:00
|
|
|
if (context.IsEmpty()) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"\nException thrown while compiling natives - see above.\n\n");
|
2012-06-19 18:38:03 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2014-05-22 09:36:20 +00:00
|
|
|
if (i::FLAG_extra_code != NULL) {
|
|
|
|
// Capture 100 frames if anything happens.
|
|
|
|
V8::SetCaptureStackTraceForUncaughtExceptions(true, 100);
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
v8::Context::Scope cscope(v8::Local<v8::Context>::New(isolate, context));
|
|
|
|
const char* name = i::FLAG_extra_code;
|
2014-06-30 13:25:46 +00:00
|
|
|
FILE* file = base::OS::FOpen(name, "rb");
|
2014-05-22 09:36:20 +00:00
|
|
|
if (file == NULL) {
|
|
|
|
fprintf(stderr, "Failed to open '%s': errno %d\n", name, errno);
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-06-19 18:38:03 +00:00
|
|
|
|
2014-05-22 09:36:20 +00:00
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
int size = ftell(file);
|
|
|
|
rewind(file);
|
|
|
|
|
|
|
|
char* chars = new char[size + 1];
|
|
|
|
chars[size] = '\0';
|
|
|
|
for (int i = 0; i < size;) {
|
|
|
|
int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
|
|
|
|
if (read < 0) {
|
|
|
|
fprintf(stderr, "Failed to read '%s': errno %d\n", name, errno);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
i += read;
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
Local<String> source = String::NewFromUtf8(isolate, chars);
|
|
|
|
TryCatch try_catch;
|
|
|
|
Local<Script> script = Script::Compile(source);
|
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
fprintf(stderr, "Failure compiling '%s'\n", name);
|
|
|
|
DumpException(try_catch.Message());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
script->Run();
|
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
fprintf(stderr, "Failure running '%s'\n", name);
|
|
|
|
DumpException(try_catch.Message());
|
2012-06-19 18:38:03 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2014-05-22 09:36:20 +00:00
|
|
|
// Make sure all builtin scripts are cached.
|
|
|
|
{ HandleScope scope(isolate);
|
|
|
|
for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) {
|
|
|
|
internal_isolate->bootstrapper()->NativesSourceLookup(i);
|
|
|
|
}
|
2012-06-19 18:38:03 +00:00
|
|
|
}
|
2014-05-22 09:36:20 +00:00
|
|
|
// If we don't do this then we end up with a stray root pointing at the
|
|
|
|
// context even after we have disposed of the context.
|
2014-10-14 12:21:18 +00:00
|
|
|
internal_isolate->heap()->CollectAllAvailableGarbage("mksnapshot");
|
2014-05-22 09:36:20 +00:00
|
|
|
i::Object* raw_context = *v8::Utils::OpenPersistent(context);
|
|
|
|
context.Reset();
|
|
|
|
|
|
|
|
// This results in a somewhat smaller snapshot, probably because it gets
|
|
|
|
// rid of some things that are cached between garbage collections.
|
2014-07-10 10:28:05 +00:00
|
|
|
i::List<i::byte> snapshot_data;
|
2014-07-08 09:04:08 +00:00
|
|
|
i::ListSnapshotSink snapshot_sink(&snapshot_data);
|
2014-05-22 09:36:20 +00:00
|
|
|
i::StartupSerializer ser(internal_isolate, &snapshot_sink);
|
|
|
|
ser.SerializeStrongReferences();
|
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
i::List<i::byte> context_data;
|
2014-07-08 09:04:08 +00:00
|
|
|
i::ListSnapshotSink contex_sink(&context_data);
|
2014-05-22 09:36:20 +00:00
|
|
|
i::PartialSerializer context_ser(internal_isolate, &ser, &contex_sink);
|
|
|
|
context_ser.Serialize(&raw_context);
|
|
|
|
ser.SerializeWeakReferences();
|
|
|
|
|
2014-10-15 14:04:53 +00:00
|
|
|
context_ser.FinalizeAllocation();
|
|
|
|
ser.FinalizeAllocation();
|
|
|
|
|
2014-05-22 09:36:20 +00:00
|
|
|
{
|
|
|
|
SnapshotWriter writer(argv[1]);
|
|
|
|
if (i::FLAG_raw_file && i::FLAG_raw_context_file)
|
|
|
|
writer.SetRawFiles(i::FLAG_raw_file, i::FLAG_raw_context_file);
|
2014-06-23 13:52:17 +00:00
|
|
|
if (i::FLAG_startup_blob)
|
|
|
|
writer.SetStartupBlobFile(i::FLAG_startup_blob);
|
2014-05-22 09:36:20 +00:00
|
|
|
#ifdef COMPRESS_STARTUP_DATA_BZ2
|
|
|
|
BZip2Compressor bzip2;
|
|
|
|
writer.SetCompressor(&bzip2);
|
|
|
|
#endif
|
|
|
|
writer.WriteSnapshot(snapshot_data, ser, context_data, context_ser);
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-24 12:31:10 +00:00
|
|
|
|
2014-01-29 13:30:38 +00:00
|
|
|
isolate->Dispose();
|
2014-05-22 09:36:20 +00:00
|
|
|
V8::Dispose();
|
2014-07-03 07:37:27 +00:00
|
|
|
V8::ShutdownPlatform();
|
|
|
|
delete platform;
|
2008-07-03 15:10:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|