blob: c01baad79a2c2d85bf865a7a35e9ba173b988e8f [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// The common functionality when building with or without snapshots.
29
30#include "v8.h"
31
32#include "api.h"
33#include "serialize.h"
34#include "snapshot.h"
Steve Blockd0582a62009-12-15 09:54:21 +000035#include "platform.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036
37namespace v8 {
38namespace internal {
39
40bool Snapshot::Deserialize(const byte* content, int len) {
Steve Blockd0582a62009-12-15 09:54:21 +000041 SnapshotByteSource source(content, len);
42 Deserializer deserializer(&source);
43 return V8::Initialize(&deserializer);
Steve Blocka7e24c12009-10-30 11:49:00 +000044}
45
46
47bool Snapshot::Initialize(const char* snapshot_file) {
48 if (snapshot_file) {
49 int len;
50 byte* str = ReadBytes(snapshot_file, &len);
51 if (!str) return false;
Steve Blockd0582a62009-12-15 09:54:21 +000052 Deserialize(str, len);
Steve Blocka7e24c12009-10-30 11:49:00 +000053 DeleteArray(str);
Steve Blockd0582a62009-12-15 09:54:21 +000054 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +000055 } else if (size_ > 0) {
Steve Blockd0582a62009-12-15 09:54:21 +000056 Deserialize(data_, size_);
57 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +000058 }
59 return false;
60}
61
62
Steve Blockd0582a62009-12-15 09:54:21 +000063class FileByteSink : public SnapshotByteSink {
64 public:
65 explicit FileByteSink(const char* snapshot_file) {
66 fp_ = OS::FOpen(snapshot_file, "wb");
67 if (fp_ == NULL) {
68 PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file);
69 exit(1);
70 }
71 }
72 virtual ~FileByteSink() {
73 if (fp_ != NULL) {
74 fclose(fp_);
75 }
76 }
77 virtual void Put(int byte, const char* description) {
78 if (fp_ != NULL) {
79 fputc(byte, fp_);
80 }
81 }
82
83 private:
84 FILE* fp_;
85};
86
87
Steve Blocka7e24c12009-10-30 11:49:00 +000088bool Snapshot::WriteToFile(const char* snapshot_file) {
Steve Blockd0582a62009-12-15 09:54:21 +000089 FileByteSink file(snapshot_file);
90 Serializer ser(&file);
Steve Blocka7e24c12009-10-30 11:49:00 +000091 ser.Serialize();
Steve Blockd0582a62009-12-15 09:54:21 +000092 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +000093}
94
95
Steve Blockd0582a62009-12-15 09:54:21 +000096
Steve Blocka7e24c12009-10-30 11:49:00 +000097} } // namespace v8::internal