2010-01-15 14:20:31 +00:00
|
|
|
// Copyright 2007-2010 the V8 project authors. All rights reserved.
|
2008-08-22 13:33:59 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include <sys/stat.h>
|
2013-05-02 20:18:42 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/bootstrapper.h"
|
2014-07-16 06:59:14 +00:00
|
|
|
#include "src/compilation-cache.h"
|
2016-08-23 12:35:20 +00:00
|
|
|
#include "src/compiler.h"
|
2015-07-31 11:07:50 +00:00
|
|
|
#include "src/debug/debug.h"
|
2014-08-05 08:18:22 +00:00
|
|
|
#include "src/heap/spaces.h"
|
2016-05-19 13:09:06 +00:00
|
|
|
#include "src/macro-assembler.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/objects.h"
|
2015-11-26 16:22:34 +00:00
|
|
|
#include "src/parsing/parser.h"
|
2014-09-25 07:16:15 +00:00
|
|
|
#include "src/runtime/runtime.h"
|
2016-03-01 14:42:57 +00:00
|
|
|
#include "src/snapshot/code-serializer.h"
|
|
|
|
#include "src/snapshot/deserializer.h"
|
2015-03-27 15:28:55 +00:00
|
|
|
#include "src/snapshot/natives.h"
|
2016-03-01 14:42:57 +00:00
|
|
|
#include "src/snapshot/partial-serializer.h"
|
2015-03-27 15:28:55 +00:00
|
|
|
#include "src/snapshot/snapshot.h"
|
2016-03-01 14:42:57 +00:00
|
|
|
#include "src/snapshot/startup-serializer.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
2016-05-20 13:30:22 +00:00
|
|
|
#include "test/cctest/heap/heap-utils.h"
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
using namespace v8::internal;
|
|
|
|
|
2015-03-05 14:46:39 +00:00
|
|
|
void DisableTurbofan() {
|
|
|
|
const char* flag = "--turbo-filter=\"\"";
|
|
|
|
FlagList::SetFlagsFromString(flag, StrLength(flag));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
// TestIsolate is used for testing isolate serialization.
|
|
|
|
class TestIsolate : public Isolate {
|
|
|
|
public:
|
|
|
|
static v8::Isolate* NewInitialized(bool enable_serializer) {
|
|
|
|
i::Isolate* isolate = new TestIsolate(enable_serializer);
|
|
|
|
v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
|
|
|
|
v8::Isolate::Scope isolate_scope(v8_isolate);
|
|
|
|
isolate->Init(NULL);
|
|
|
|
return v8_isolate;
|
|
|
|
}
|
2015-04-29 09:54:34 +00:00
|
|
|
explicit TestIsolate(bool enable_serializer) : Isolate(enable_serializer) {
|
|
|
|
set_array_buffer_allocator(CcTest::array_buffer_allocator());
|
|
|
|
}
|
2015-02-25 11:14:40 +00:00
|
|
|
};
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
static Vector<const byte> WritePayload(const Vector<const byte>& payload) {
|
|
|
|
int length = payload.length();
|
|
|
|
byte* blob = NewArray<byte>(length);
|
|
|
|
memcpy(blob, payload.begin(), length);
|
|
|
|
return Vector<const byte>(const_cast<const byte*>(blob), length);
|
2014-10-23 11:23:57 +00:00
|
|
|
}
|
2010-01-27 08:25:48 +00:00
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
static Vector<const byte> Serialize(v8::Isolate* isolate) {
|
2009-10-27 11:54:01 +00:00
|
|
|
// We have to create one context. One reason for this is so that the builtins
|
|
|
|
// can be loaded from v8natives.js and their addresses can be processed. This
|
|
|
|
// will clear the pending fixups array, which would otherwise contain GC roots
|
|
|
|
// that would confuse the serialization/deserialization process.
|
2014-09-19 08:01:35 +00:00
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
2013-05-08 07:45:16 +00:00
|
|
|
{
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Context::New(isolate);
|
|
|
|
}
|
2013-09-19 12:06:27 +00:00
|
|
|
|
2014-09-19 08:01:35 +00:00
|
|
|
Isolate* internal_isolate = reinterpret_cast<Isolate*>(isolate);
|
2014-10-14 12:21:18 +00:00
|
|
|
internal_isolate->heap()->CollectAllAvailableGarbage("serialize");
|
2016-06-10 11:26:28 +00:00
|
|
|
StartupSerializer ser(internal_isolate,
|
2016-06-10 05:44:23 +00:00
|
|
|
v8::SnapshotCreator::FunctionCodeHandling::kClear);
|
2016-03-10 09:57:47 +00:00
|
|
|
ser.SerializeStrongReferences();
|
|
|
|
ser.SerializeWeakReferencesAndDeferred();
|
2016-06-10 10:58:10 +00:00
|
|
|
SnapshotData snapshot_data(&ser);
|
2016-03-10 09:57:47 +00:00
|
|
|
return WritePayload(snapshot_data.RawData());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-27 14:56:51 +00:00
|
|
|
Vector<const uint8_t> ConstructSource(Vector<const uint8_t> head,
|
|
|
|
Vector<const uint8_t> body,
|
|
|
|
Vector<const uint8_t> tail, int repeats) {
|
|
|
|
int source_length = head.length() + body.length() * repeats + tail.length();
|
|
|
|
uint8_t* source = NewArray<uint8_t>(static_cast<size_t>(source_length));
|
|
|
|
CopyChars(source, head.start(), head.length());
|
|
|
|
for (int i = 0; i < repeats; i++) {
|
|
|
|
CopyChars(source + head.length() + i * body.length(), body.start(),
|
|
|
|
body.length());
|
|
|
|
}
|
|
|
|
CopyChars(source + head.length() + repeats * body.length(), tail.start(),
|
|
|
|
tail.length());
|
|
|
|
return Vector<const uint8_t>(const_cast<const uint8_t*>(source),
|
|
|
|
source_length);
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
v8::Isolate* InitializeFromBlob(Vector<const byte> blob) {
|
2014-09-19 08:01:35 +00:00
|
|
|
v8::Isolate* v8_isolate = NULL;
|
2014-06-23 13:52:17 +00:00
|
|
|
{
|
2016-03-10 09:57:47 +00:00
|
|
|
SnapshotData snapshot_data(blob);
|
2014-12-05 13:03:10 +00:00
|
|
|
Deserializer deserializer(&snapshot_data);
|
2015-02-25 11:14:40 +00:00
|
|
|
Isolate* isolate = new TestIsolate(false);
|
2014-09-19 08:01:35 +00:00
|
|
|
v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
|
|
|
|
v8::Isolate::Scope isolate_scope(v8_isolate);
|
|
|
|
isolate->Init(&deserializer);
|
2014-06-23 13:52:17 +00:00
|
|
|
}
|
2014-09-19 08:01:35 +00:00
|
|
|
return v8_isolate;
|
2014-06-23 13:52:17 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
static v8::Isolate* Deserialize(Vector<const byte> blob) {
|
|
|
|
v8::Isolate* isolate = InitializeFromBlob(blob);
|
2014-09-19 08:01:35 +00:00
|
|
|
CHECK(isolate);
|
|
|
|
return isolate;
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-19 08:01:35 +00:00
|
|
|
static void SanityCheck(v8::Isolate* v8_isolate) {
|
|
|
|
Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
|
|
|
|
v8::HandleScope scope(v8_isolate);
|
2012-10-15 06:34:22 +00:00
|
|
|
#ifdef VERIFY_HEAP
|
2014-09-19 08:01:35 +00:00
|
|
|
isolate->heap()->Verify();
|
2008-08-22 13:33:59 +00:00
|
|
|
#endif
|
2013-06-04 10:30:05 +00:00
|
|
|
CHECK(isolate->global_object()->IsJSObject());
|
|
|
|
CHECK(isolate->native_context()->IsContext());
|
2014-09-19 08:01:35 +00:00
|
|
|
CHECK(isolate->heap()->string_table()->IsStringTable());
|
2014-09-10 12:38:12 +00:00
|
|
|
isolate->factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("Empty"));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
UNINITIALIZED_TEST(StartupSerializerOnce) {
|
2010-03-23 11:40:38 +00:00
|
|
|
// The serialize-deserialize tests only work if the VM is built without
|
|
|
|
// serialization. That doesn't matter. We don't need to be able to
|
|
|
|
// serialize a snapshot in a VM that is booted from a snapshot.
|
2015-03-05 14:46:39 +00:00
|
|
|
DisableTurbofan();
|
2016-03-02 13:12:59 +00:00
|
|
|
v8::Isolate* isolate = TestIsolate::NewInitialized(true);
|
2016-03-10 09:57:47 +00:00
|
|
|
Vector<const byte> blob = Serialize(isolate);
|
|
|
|
isolate = Deserialize(blob);
|
|
|
|
blob.Dispose();
|
2015-02-25 11:14:40 +00:00
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
v8::Local<v8::Context> env = v8::Context::New(isolate);
|
|
|
|
env->Enter();
|
2009-11-25 12:55:33 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
SanityCheck(isolate);
|
2010-03-23 11:40:38 +00:00
|
|
|
}
|
2015-02-25 11:14:40 +00:00
|
|
|
isolate->Dispose();
|
2009-11-25 12:55:33 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
UNINITIALIZED_TEST(StartupSerializerTwice) {
|
2015-03-05 14:46:39 +00:00
|
|
|
DisableTurbofan();
|
2016-03-02 13:12:59 +00:00
|
|
|
v8::Isolate* isolate = TestIsolate::NewInitialized(true);
|
2016-03-10 09:57:47 +00:00
|
|
|
Vector<const byte> blob1 = Serialize(isolate);
|
|
|
|
Vector<const byte> blob2 = Serialize(isolate);
|
|
|
|
blob1.Dispose();
|
|
|
|
isolate = Deserialize(blob2);
|
|
|
|
blob2.Dispose();
|
2015-02-25 11:14:40 +00:00
|
|
|
{
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2009-11-25 12:55:33 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
v8::Local<v8::Context> env = v8::Context::New(isolate);
|
|
|
|
env->Enter();
|
2009-10-27 11:54:01 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
SanityCheck(isolate);
|
2010-03-23 11:40:38 +00:00
|
|
|
}
|
2015-02-25 11:14:40 +00:00
|
|
|
isolate->Dispose();
|
2009-10-27 11:54:01 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
UNINITIALIZED_TEST(StartupSerializerOnceRunScript) {
|
2015-03-05 14:46:39 +00:00
|
|
|
DisableTurbofan();
|
2016-03-02 13:12:59 +00:00
|
|
|
v8::Isolate* isolate = TestIsolate::NewInitialized(true);
|
2016-03-10 09:57:47 +00:00
|
|
|
Vector<const byte> blob = Serialize(isolate);
|
|
|
|
isolate = Deserialize(blob);
|
|
|
|
blob.Dispose();
|
2015-02-25 11:14:40 +00:00
|
|
|
{
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2014-09-19 08:01:35 +00:00
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
v8::Local<v8::Context> env = v8::Context::New(isolate);
|
|
|
|
env->Enter();
|
2009-10-27 11:54:01 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
const char* c_source = "\"1234\".length";
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::Script> script = v8_compile(c_source);
|
|
|
|
v8::Maybe<int32_t> result = script->Run(isolate->GetCurrentContext())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Int32Value(isolate->GetCurrentContext());
|
|
|
|
CHECK_EQ(4, result.FromJust());
|
2010-03-23 11:40:38 +00:00
|
|
|
}
|
2015-02-25 11:14:40 +00:00
|
|
|
isolate->Dispose();
|
2009-10-27 11:54:01 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
UNINITIALIZED_TEST(StartupSerializerTwiceRunScript) {
|
2015-03-05 14:46:39 +00:00
|
|
|
DisableTurbofan();
|
2016-03-02 13:12:59 +00:00
|
|
|
v8::Isolate* isolate = TestIsolate::NewInitialized(true);
|
2016-03-10 09:57:47 +00:00
|
|
|
Vector<const byte> blob1 = Serialize(isolate);
|
|
|
|
Vector<const byte> blob2 = Serialize(isolate);
|
|
|
|
blob1.Dispose();
|
|
|
|
isolate = Deserialize(blob2);
|
|
|
|
blob2.Dispose();
|
2015-02-25 11:14:40 +00:00
|
|
|
{
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2009-11-25 12:55:33 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
v8::Local<v8::Context> env = v8::Context::New(isolate);
|
|
|
|
env->Enter();
|
2009-11-25 12:55:33 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
const char* c_source = "\"1234\".length";
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::Script> script = v8_compile(c_source);
|
|
|
|
v8::Maybe<int32_t> result = script->Run(isolate->GetCurrentContext())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Int32Value(isolate->GetCurrentContext());
|
|
|
|
CHECK_EQ(4, result.FromJust());
|
2010-03-23 11:40:38 +00:00
|
|
|
}
|
2015-02-25 11:14:40 +00:00
|
|
|
isolate->Dispose();
|
2009-11-25 12:55:33 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
static void PartiallySerializeObject(Vector<const byte>* startup_blob_out,
|
|
|
|
Vector<const byte>* partial_blob_out) {
|
2015-02-25 11:14:40 +00:00
|
|
|
v8::Isolate* v8_isolate = TestIsolate::NewInitialized(true);
|
|
|
|
Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
|
|
|
|
v8_isolate->Enter();
|
|
|
|
{
|
|
|
|
Heap* heap = isolate->heap();
|
2010-01-14 14:46:31 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
v8::Persistent<v8::Context> env;
|
|
|
|
{
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
env.Reset(v8_isolate, v8::Context::New(v8_isolate));
|
|
|
|
}
|
2015-12-07 05:36:41 +00:00
|
|
|
CHECK(!env.IsEmpty());
|
2015-02-25 11:14:40 +00:00
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(v8_isolate);
|
|
|
|
v8::Local<v8::Context>::New(v8_isolate, env)->Enter();
|
|
|
|
}
|
|
|
|
// Make sure all builtin scripts are cached.
|
|
|
|
{
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
|
2015-04-30 15:10:43 +00:00
|
|
|
isolate->bootstrapper()->SourceLookup<Natives>(i);
|
2014-09-19 08:01:35 +00:00
|
|
|
}
|
2015-02-25 11:14:40 +00:00
|
|
|
}
|
2015-04-23 08:37:24 +00:00
|
|
|
heap->CollectAllGarbage();
|
|
|
|
heap->CollectAllGarbage();
|
2010-01-14 14:46:31 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
Object* raw_foo;
|
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(v8_isolate);
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::String> foo = v8_str("foo");
|
2015-12-07 05:36:41 +00:00
|
|
|
CHECK(!foo.IsEmpty());
|
2015-02-25 11:14:40 +00:00
|
|
|
raw_foo = *(v8::Utils::OpenHandle(*foo));
|
|
|
|
}
|
2014-09-18 21:57:10 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(v8_isolate);
|
|
|
|
v8::Local<v8::Context>::New(v8_isolate, env)->Exit();
|
|
|
|
}
|
|
|
|
env.Reset();
|
2014-09-19 08:01:35 +00:00
|
|
|
|
2016-06-10 05:44:23 +00:00
|
|
|
StartupSerializer startup_serializer(
|
2016-06-10 11:26:28 +00:00
|
|
|
isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear);
|
2015-02-25 11:14:40 +00:00
|
|
|
startup_serializer.SerializeStrongReferences();
|
2014-10-23 11:23:57 +00:00
|
|
|
|
2016-06-10 11:26:28 +00:00
|
|
|
PartialSerializer partial_serializer(isolate, &startup_serializer);
|
2015-02-25 11:14:40 +00:00
|
|
|
partial_serializer.Serialize(&raw_foo);
|
2014-09-19 08:01:35 +00:00
|
|
|
|
2015-05-15 07:42:40 +00:00
|
|
|
startup_serializer.SerializeWeakReferencesAndDeferred();
|
2014-10-23 11:23:57 +00:00
|
|
|
|
2016-06-10 10:58:10 +00:00
|
|
|
SnapshotData startup_snapshot(&startup_serializer);
|
|
|
|
SnapshotData partial_snapshot(&partial_serializer);
|
2014-10-15 14:04:53 +00:00
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
*partial_blob_out = WritePayload(partial_snapshot.RawData());
|
|
|
|
*startup_blob_out = WritePayload(startup_snapshot.RawData());
|
2012-06-19 18:38:03 +00:00
|
|
|
}
|
2015-02-25 11:14:40 +00:00
|
|
|
v8_isolate->Exit();
|
|
|
|
v8_isolate->Dispose();
|
2010-01-15 14:20:31 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
UNINITIALIZED_TEST(PartialSerializerObject) {
|
2015-03-05 14:46:39 +00:00
|
|
|
DisableTurbofan();
|
2016-03-10 09:57:47 +00:00
|
|
|
Vector<const byte> startup_blob;
|
|
|
|
Vector<const byte> partial_blob;
|
|
|
|
PartiallySerializeObject(&startup_blob, &partial_blob);
|
2015-02-25 11:14:40 +00:00
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
v8::Isolate* v8_isolate = InitializeFromBlob(startup_blob);
|
|
|
|
startup_blob.Dispose();
|
2015-02-25 11:14:40 +00:00
|
|
|
CHECK(v8_isolate);
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope isolate_scope(v8_isolate);
|
2010-03-23 11:40:38 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
Handle<Object> root;
|
|
|
|
// Intentionally empty handle. The deserializer should not come across
|
|
|
|
// any references to the global proxy in this test.
|
|
|
|
Handle<JSGlobalProxy> global_proxy = Handle<JSGlobalProxy>::null();
|
2014-09-18 21:57:10 +00:00
|
|
|
{
|
2016-03-10 09:57:47 +00:00
|
|
|
SnapshotData snapshot_data(partial_blob);
|
2015-02-25 11:14:40 +00:00
|
|
|
Deserializer deserializer(&snapshot_data);
|
2015-12-01 23:35:57 +00:00
|
|
|
root = deserializer.DeserializePartial(isolate, global_proxy)
|
|
|
|
.ToHandleChecked();
|
2015-02-25 11:14:40 +00:00
|
|
|
CHECK(root->IsString());
|
|
|
|
}
|
2014-09-19 08:01:35 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
Handle<Object> root2;
|
|
|
|
{
|
2016-03-10 09:57:47 +00:00
|
|
|
SnapshotData snapshot_data(partial_blob);
|
2015-02-25 11:14:40 +00:00
|
|
|
Deserializer deserializer(&snapshot_data);
|
2015-12-01 23:35:57 +00:00
|
|
|
root2 = deserializer.DeserializePartial(isolate, global_proxy)
|
|
|
|
.ToHandleChecked();
|
2015-02-25 11:14:40 +00:00
|
|
|
CHECK(root2->IsString());
|
|
|
|
CHECK(root.is_identical_to(root2));
|
2010-03-23 11:40:38 +00:00
|
|
|
}
|
2016-03-10 09:57:47 +00:00
|
|
|
partial_blob.Dispose();
|
2010-01-27 08:25:48 +00:00
|
|
|
}
|
2015-02-25 11:14:40 +00:00
|
|
|
v8_isolate->Dispose();
|
2010-03-23 11:40:38 +00:00
|
|
|
}
|
2010-01-27 08:25:48 +00:00
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
static void PartiallySerializeContext(Vector<const byte>* startup_blob_out,
|
|
|
|
Vector<const byte>* partial_blob_out) {
|
2015-02-25 11:14:40 +00:00
|
|
|
v8::Isolate* v8_isolate = TestIsolate::NewInitialized(true);
|
|
|
|
Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope isolate_scope(v8_isolate);
|
2014-09-19 08:01:35 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
v8::Persistent<v8::Context> env;
|
|
|
|
{
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
env.Reset(v8_isolate, v8::Context::New(v8_isolate));
|
|
|
|
}
|
2015-12-07 05:36:41 +00:00
|
|
|
CHECK(!env.IsEmpty());
|
2015-02-25 11:14:40 +00:00
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(v8_isolate);
|
|
|
|
v8::Local<v8::Context>::New(v8_isolate, env)->Enter();
|
|
|
|
}
|
|
|
|
// Make sure all builtin scripts are cached.
|
|
|
|
{
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
|
2015-04-30 15:10:43 +00:00
|
|
|
isolate->bootstrapper()->SourceLookup<Natives>(i);
|
2014-09-19 08:01:35 +00:00
|
|
|
}
|
2015-02-25 11:14:40 +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 env.
|
2015-04-23 08:37:24 +00:00
|
|
|
heap->CollectAllGarbage();
|
2010-03-23 11:40:38 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(v8_isolate);
|
|
|
|
v8::Local<v8::Context>::New(v8_isolate, env)->Exit();
|
|
|
|
}
|
2014-09-18 21:57:10 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
i::Object* raw_context = *v8::Utils::OpenPersistent(env);
|
2014-09-19 08:01:35 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
env.Reset();
|
2014-09-19 08:01:35 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
SnapshotByteSink startup_sink;
|
2016-06-10 05:44:23 +00:00
|
|
|
StartupSerializer startup_serializer(
|
2016-06-10 11:26:28 +00:00
|
|
|
isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear);
|
2015-02-25 11:14:40 +00:00
|
|
|
startup_serializer.SerializeStrongReferences();
|
2014-09-19 08:01:35 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
SnapshotByteSink partial_sink;
|
2016-06-10 11:26:28 +00:00
|
|
|
PartialSerializer partial_serializer(isolate, &startup_serializer);
|
2015-02-25 11:14:40 +00:00
|
|
|
partial_serializer.Serialize(&raw_context);
|
2015-05-15 07:42:40 +00:00
|
|
|
startup_serializer.SerializeWeakReferencesAndDeferred();
|
2014-09-19 08:01:35 +00:00
|
|
|
|
2016-06-10 10:58:10 +00:00
|
|
|
SnapshotData startup_snapshot(&startup_serializer);
|
|
|
|
SnapshotData partial_snapshot(&partial_serializer);
|
2014-10-23 11:23:57 +00:00
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
*partial_blob_out = WritePayload(partial_snapshot.RawData());
|
|
|
|
*startup_blob_out = WritePayload(startup_snapshot.RawData());
|
2012-06-19 18:38:03 +00:00
|
|
|
}
|
2015-02-25 11:14:40 +00:00
|
|
|
v8_isolate->Dispose();
|
2010-03-23 11:40:38 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
UNINITIALIZED_TEST(PartialSerializerContext) {
|
2016-03-02 13:12:59 +00:00
|
|
|
DisableTurbofan();
|
2016-03-10 09:57:47 +00:00
|
|
|
Vector<const byte> startup_blob;
|
|
|
|
Vector<const byte> partial_blob;
|
|
|
|
PartiallySerializeContext(&startup_blob, &partial_blob);
|
2015-02-25 11:14:40 +00:00
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
v8::Isolate* v8_isolate = InitializeFromBlob(startup_blob);
|
2015-02-25 11:14:40 +00:00
|
|
|
CHECK(v8_isolate);
|
2016-03-10 09:57:47 +00:00
|
|
|
startup_blob.Dispose();
|
2015-02-25 11:14:40 +00:00
|
|
|
{
|
|
|
|
v8::Isolate::Scope isolate_scope(v8_isolate);
|
2010-03-23 11:40:38 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
Handle<Object> root;
|
|
|
|
Handle<JSGlobalProxy> global_proxy =
|
|
|
|
isolate->factory()->NewUninitializedJSGlobalProxy();
|
2014-09-18 21:57:10 +00:00
|
|
|
{
|
2016-03-10 09:57:47 +00:00
|
|
|
SnapshotData snapshot_data(partial_blob);
|
2015-02-25 11:14:40 +00:00
|
|
|
Deserializer deserializer(&snapshot_data);
|
2015-12-01 23:35:57 +00:00
|
|
|
root = deserializer.DeserializePartial(isolate, global_proxy)
|
|
|
|
.ToHandleChecked();
|
2015-02-25 11:14:40 +00:00
|
|
|
CHECK(root->IsContext());
|
|
|
|
CHECK(Handle<Context>::cast(root)->global_proxy() == *global_proxy);
|
|
|
|
}
|
2014-09-19 08:01:35 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
Handle<Object> root2;
|
|
|
|
{
|
2016-03-10 09:57:47 +00:00
|
|
|
SnapshotData snapshot_data(partial_blob);
|
2015-02-25 11:14:40 +00:00
|
|
|
Deserializer deserializer(&snapshot_data);
|
2015-12-01 23:35:57 +00:00
|
|
|
root2 = deserializer.DeserializePartial(isolate, global_proxy)
|
|
|
|
.ToHandleChecked();
|
2015-02-25 11:14:40 +00:00
|
|
|
CHECK(root2->IsContext());
|
|
|
|
CHECK(!root.is_identical_to(root2));
|
2015-01-13 08:48:00 +00:00
|
|
|
}
|
2016-03-10 09:57:47 +00:00
|
|
|
partial_blob.Dispose();
|
2015-01-13 08:48:00 +00:00
|
|
|
}
|
2015-02-25 11:14:40 +00:00
|
|
|
v8_isolate->Dispose();
|
2015-01-13 08:48:00 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
static void PartiallySerializeCustomContext(
|
|
|
|
Vector<const byte>* startup_blob_out,
|
|
|
|
Vector<const byte>* partial_blob_out) {
|
2015-02-25 11:14:40 +00:00
|
|
|
v8::Isolate* v8_isolate = TestIsolate::NewInitialized(true);
|
|
|
|
Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope isolate_scope(v8_isolate);
|
2015-01-13 08:48:00 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
v8::Persistent<v8::Context> env;
|
|
|
|
{
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
env.Reset(v8_isolate, v8::Context::New(v8_isolate));
|
|
|
|
}
|
2015-12-07 05:36:41 +00:00
|
|
|
CHECK(!env.IsEmpty());
|
2015-02-25 11:14:40 +00:00
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(v8_isolate);
|
|
|
|
v8::Local<v8::Context>::New(v8_isolate, env)->Enter();
|
|
|
|
// After execution, e's function context refers to the global object.
|
|
|
|
CompileRun(
|
|
|
|
"var e;"
|
|
|
|
"(function() {"
|
|
|
|
" e = function(s) { return eval (s); }"
|
|
|
|
"})();"
|
|
|
|
"var o = this;"
|
2016-03-16 15:02:41 +00:00
|
|
|
"var r = Math.random();"
|
|
|
|
"var c = Math.sin(0) + Math.cos(0);"
|
2015-02-25 11:14:40 +00:00
|
|
|
"var f = (function(a, b) { return a + b; }).bind(1, 2, 3);"
|
|
|
|
"var s = parseInt('12345');");
|
|
|
|
|
|
|
|
Vector<const uint8_t> source = ConstructSource(
|
|
|
|
STATIC_CHAR_VECTOR("function g() { return [,"),
|
|
|
|
STATIC_CHAR_VECTOR("1,"),
|
|
|
|
STATIC_CHAR_VECTOR("];} a = g(); b = g(); b.push(1);"), 100000);
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::MaybeLocal<v8::String> source_str = v8::String::NewFromOneByte(
|
|
|
|
v8_isolate, source.start(), v8::NewStringType::kNormal,
|
2015-02-25 11:14:40 +00:00
|
|
|
source.length());
|
2015-10-08 09:48:05 +00:00
|
|
|
CompileRun(source_str.ToLocalChecked());
|
2015-02-25 11:14:40 +00:00
|
|
|
source.Dispose();
|
|
|
|
}
|
|
|
|
// Make sure all builtin scripts are cached.
|
|
|
|
{
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
|
2015-04-30 15:10:43 +00:00
|
|
|
isolate->bootstrapper()->SourceLookup<Natives>(i);
|
2015-01-13 08:48:00 +00:00
|
|
|
}
|
2015-02-25 11:14:40 +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 env.
|
|
|
|
isolate->heap()->CollectAllAvailableGarbage("snapshotting");
|
2015-01-13 08:48:00 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(v8_isolate);
|
|
|
|
v8::Local<v8::Context>::New(v8_isolate, env)->Exit();
|
|
|
|
}
|
2015-01-13 08:48:00 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
i::Object* raw_context = *v8::Utils::OpenPersistent(env);
|
2015-01-13 08:48:00 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
env.Reset();
|
2015-01-13 08:48:00 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
SnapshotByteSink startup_sink;
|
2016-06-10 05:44:23 +00:00
|
|
|
StartupSerializer startup_serializer(
|
2016-06-10 11:26:28 +00:00
|
|
|
isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear);
|
2015-02-25 11:14:40 +00:00
|
|
|
startup_serializer.SerializeStrongReferences();
|
2015-01-13 08:48:00 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
SnapshotByteSink partial_sink;
|
2016-06-10 11:26:28 +00:00
|
|
|
PartialSerializer partial_serializer(isolate, &startup_serializer);
|
2015-02-25 11:14:40 +00:00
|
|
|
partial_serializer.Serialize(&raw_context);
|
2015-05-15 07:42:40 +00:00
|
|
|
startup_serializer.SerializeWeakReferencesAndDeferred();
|
2015-01-13 08:48:00 +00:00
|
|
|
|
2016-06-10 10:58:10 +00:00
|
|
|
SnapshotData startup_snapshot(&startup_serializer);
|
|
|
|
SnapshotData partial_snapshot(&partial_serializer);
|
2015-01-13 08:48:00 +00:00
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
*partial_blob_out = WritePayload(partial_snapshot.RawData());
|
|
|
|
*startup_blob_out = WritePayload(startup_snapshot.RawData());
|
2015-01-13 08:48:00 +00:00
|
|
|
}
|
2015-02-25 11:14:40 +00:00
|
|
|
v8_isolate->Dispose();
|
2015-01-13 08:48:00 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
UNINITIALIZED_TEST(PartialSerializerCustomContext) {
|
2015-03-05 14:46:39 +00:00
|
|
|
DisableTurbofan();
|
2016-03-10 09:57:47 +00:00
|
|
|
Vector<const byte> startup_blob;
|
|
|
|
Vector<const byte> partial_blob;
|
|
|
|
PartiallySerializeCustomContext(&startup_blob, &partial_blob);
|
2015-02-25 11:14:40 +00:00
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
v8::Isolate* v8_isolate = InitializeFromBlob(startup_blob);
|
2015-02-25 11:14:40 +00:00
|
|
|
CHECK(v8_isolate);
|
2016-03-10 09:57:47 +00:00
|
|
|
startup_blob.Dispose();
|
2015-02-25 11:14:40 +00:00
|
|
|
{
|
|
|
|
v8::Isolate::Scope isolate_scope(v8_isolate);
|
2015-01-13 08:48:00 +00:00
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
Handle<Object> root;
|
|
|
|
Handle<JSGlobalProxy> global_proxy =
|
|
|
|
isolate->factory()->NewUninitializedJSGlobalProxy();
|
2015-01-13 08:48:00 +00:00
|
|
|
{
|
2016-03-10 09:57:47 +00:00
|
|
|
SnapshotData snapshot_data(partial_blob);
|
2015-02-25 11:14:40 +00:00
|
|
|
Deserializer deserializer(&snapshot_data);
|
2015-12-01 23:35:57 +00:00
|
|
|
root = deserializer.DeserializePartial(isolate, global_proxy)
|
|
|
|
.ToHandleChecked();
|
2015-02-25 11:14:40 +00:00
|
|
|
CHECK(root->IsContext());
|
|
|
|
Handle<Context> context = Handle<Context>::cast(root);
|
2016-03-10 11:18:13 +00:00
|
|
|
|
|
|
|
// Add context to the weak native context list
|
|
|
|
context->set(Context::NEXT_CONTEXT_LINK,
|
|
|
|
isolate->heap()->native_contexts_list(),
|
|
|
|
UPDATE_WEAK_WRITE_BARRIER);
|
|
|
|
isolate->heap()->set_native_contexts_list(*context);
|
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
CHECK(context->global_proxy() == *global_proxy);
|
|
|
|
Handle<String> o = isolate->factory()->NewStringFromAsciiChecked("o");
|
|
|
|
Handle<JSObject> global_object(context->global_object(), isolate);
|
2015-05-12 13:52:26 +00:00
|
|
|
Handle<Object> property = JSReceiver::GetDataProperty(global_object, o);
|
2015-02-25 11:14:40 +00:00
|
|
|
CHECK(property.is_identical_to(global_proxy));
|
|
|
|
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::Context> v8_context = v8::Utils::ToLocal(context);
|
2015-02-25 11:14:40 +00:00
|
|
|
v8::Context::Scope context_scope(v8_context);
|
2015-10-08 09:48:05 +00:00
|
|
|
double r = CompileRun("r")
|
|
|
|
->ToNumber(v8_isolate->GetCurrentContext())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Value();
|
2016-03-16 15:02:41 +00:00
|
|
|
CHECK(0.0 <= r && r < 1.0);
|
|
|
|
// Math.random still works.
|
|
|
|
double random = CompileRun("Math.random()")
|
|
|
|
->ToNumber(v8_isolate->GetCurrentContext())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Value();
|
|
|
|
CHECK(0.0 <= random && random < 1.0);
|
|
|
|
double c = CompileRun("c")
|
|
|
|
->ToNumber(v8_isolate->GetCurrentContext())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Value();
|
|
|
|
CHECK_EQ(1, c);
|
2015-10-08 09:48:05 +00:00
|
|
|
int f = CompileRun("f()")
|
|
|
|
->ToNumber(v8_isolate->GetCurrentContext())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Int32Value(v8_isolate->GetCurrentContext())
|
|
|
|
.FromJust();
|
2015-02-25 11:14:40 +00:00
|
|
|
CHECK_EQ(5, f);
|
2015-10-08 09:48:05 +00:00
|
|
|
f = CompileRun("e('f()')")
|
|
|
|
->ToNumber(v8_isolate->GetCurrentContext())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Int32Value(v8_isolate->GetCurrentContext())
|
|
|
|
.FromJust();
|
2015-02-25 11:14:40 +00:00
|
|
|
CHECK_EQ(5, f);
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::String> s = CompileRun("s")
|
|
|
|
->ToString(v8_isolate->GetCurrentContext())
|
|
|
|
.ToLocalChecked();
|
|
|
|
CHECK(s->Equals(v8_isolate->GetCurrentContext(), v8_str("12345"))
|
|
|
|
.FromJust());
|
|
|
|
int a = CompileRun("a.length")
|
|
|
|
->ToNumber(v8_isolate->GetCurrentContext())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Int32Value(v8_isolate->GetCurrentContext())
|
|
|
|
.FromJust();
|
2015-02-25 11:14:40 +00:00
|
|
|
CHECK_EQ(100001, a);
|
2015-10-08 09:48:05 +00:00
|
|
|
int b = CompileRun("b.length")
|
|
|
|
->ToNumber(v8_isolate->GetCurrentContext())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Int32Value(v8_isolate->GetCurrentContext())
|
|
|
|
.FromJust();
|
2015-02-25 11:14:40 +00:00
|
|
|
CHECK_EQ(100002, b);
|
2010-03-23 11:40:38 +00:00
|
|
|
}
|
2016-03-10 09:57:47 +00:00
|
|
|
partial_blob.Dispose();
|
2015-02-25 11:14:40 +00:00
|
|
|
}
|
|
|
|
v8_isolate->Dispose();
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CustomSnapshotDataBlob) {
|
2015-03-05 14:46:39 +00:00
|
|
|
DisableTurbofan();
|
2015-02-25 11:14:40 +00:00
|
|
|
const char* source1 = "function f() { return 42; }";
|
|
|
|
const char* source2 =
|
|
|
|
"function f() { return g() * 2; }"
|
2015-03-04 15:01:55 +00:00
|
|
|
"function g() { return 43; }"
|
|
|
|
"/./.test('a')";
|
2015-02-25 11:14:40 +00:00
|
|
|
|
|
|
|
v8::StartupData data1 = v8::V8::CreateSnapshotDataBlob(source1);
|
|
|
|
v8::StartupData data2 = v8::V8::CreateSnapshotDataBlob(source2);
|
|
|
|
|
|
|
|
v8::Isolate::CreateParams params1;
|
|
|
|
params1.snapshot_blob = &data1;
|
2015-04-29 09:54:34 +00:00
|
|
|
params1.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
|
2015-02-25 11:14:40 +00:00
|
|
|
v8::Isolate* isolate1 = v8::Isolate::New(params1);
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope i_scope(isolate1);
|
|
|
|
v8::HandleScope h_scope(isolate1);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate1);
|
|
|
|
delete[] data1.data; // We can dispose of the snapshot blob now.
|
|
|
|
v8::Context::Scope c_scope(context);
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Maybe<int32_t> result =
|
|
|
|
CompileRun("f()")->Int32Value(isolate1->GetCurrentContext());
|
|
|
|
CHECK_EQ(42, result.FromJust());
|
2015-02-25 11:14:40 +00:00
|
|
|
CHECK(CompileRun("this.g")->IsUndefined());
|
|
|
|
}
|
|
|
|
isolate1->Dispose();
|
|
|
|
|
|
|
|
v8::Isolate::CreateParams params2;
|
|
|
|
params2.snapshot_blob = &data2;
|
2015-04-29 09:54:34 +00:00
|
|
|
params2.array_buffer_allocator = CcTest::array_buffer_allocator();
|
2015-02-25 11:14:40 +00:00
|
|
|
v8::Isolate* isolate2 = v8::Isolate::New(params2);
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope i_scope(isolate2);
|
|
|
|
v8::HandleScope h_scope(isolate2);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate2);
|
|
|
|
delete[] data2.data; // We can dispose of the snapshot blob now.
|
|
|
|
v8::Context::Scope c_scope(context);
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Maybe<int32_t> result =
|
|
|
|
CompileRun("f()")->Int32Value(isolate2->GetCurrentContext());
|
|
|
|
CHECK_EQ(86, result.FromJust());
|
|
|
|
result = CompileRun("g()")->Int32Value(isolate2->GetCurrentContext());
|
|
|
|
CHECK_EQ(43, result.FromJust());
|
2010-01-27 08:25:48 +00:00
|
|
|
}
|
2015-02-25 11:14:40 +00:00
|
|
|
isolate2->Dispose();
|
2010-01-14 14:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-19 09:07:23 +00:00
|
|
|
static void SerializationFunctionTemplate(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
args.GetReturnValue().Set(args[0]);
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CustomSnapshotDataBlobOutdatedContextWithOverflow) {
|
2015-06-19 09:07:23 +00:00
|
|
|
DisableTurbofan();
|
|
|
|
|
|
|
|
const char* source1 =
|
|
|
|
"var o = {};"
|
|
|
|
"(function() {"
|
|
|
|
" function f1(x) { return f2(x) instanceof Array; }"
|
|
|
|
" function f2(x) { return foo.bar(x); }"
|
|
|
|
" o.a = f2.bind(null);"
|
|
|
|
" o.b = 1;"
|
|
|
|
" o.c = 2;"
|
|
|
|
" o.d = 3;"
|
|
|
|
" o.e = 4;"
|
|
|
|
"})();\n";
|
|
|
|
|
|
|
|
const char* source2 = "o.a(42)";
|
|
|
|
|
|
|
|
v8::StartupData data = v8::V8::CreateSnapshotDataBlob(source1);
|
|
|
|
|
|
|
|
v8::Isolate::CreateParams params;
|
|
|
|
params.snapshot_blob = &data;
|
|
|
|
params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
|
|
|
|
v8::Isolate* isolate = v8::Isolate::New(params);
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope i_scope(isolate);
|
|
|
|
v8::HandleScope h_scope(isolate);
|
|
|
|
|
|
|
|
v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
|
|
|
|
v8::Local<v8::ObjectTemplate> property = v8::ObjectTemplate::New(isolate);
|
|
|
|
v8::Local<v8::FunctionTemplate> function =
|
|
|
|
v8::FunctionTemplate::New(isolate, SerializationFunctionTemplate);
|
|
|
|
property->Set(isolate, "bar", function);
|
|
|
|
global->Set(isolate, "foo", property);
|
|
|
|
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global);
|
|
|
|
delete[] data.data; // We can dispose of the snapshot blob now.
|
|
|
|
v8::Context::Scope c_scope(context);
|
|
|
|
v8::Local<v8::Value> result = CompileRun(source2);
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Maybe<bool> compare = v8_str("42")->Equals(
|
|
|
|
v8::Isolate::GetCurrent()->GetCurrentContext(), result);
|
|
|
|
CHECK(compare.FromJust());
|
2015-06-19 09:07:23 +00:00
|
|
|
}
|
|
|
|
isolate->Dispose();
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CustomSnapshotDataBlobWithLocker) {
|
2015-03-05 14:46:39 +00:00
|
|
|
DisableTurbofan();
|
2015-04-29 09:54:34 +00:00
|
|
|
v8::Isolate::CreateParams create_params;
|
|
|
|
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
v8::Isolate* isolate0 = v8::Isolate::New(create_params);
|
2015-03-03 16:14:04 +00:00
|
|
|
{
|
|
|
|
v8::Locker locker(isolate0);
|
|
|
|
v8::Isolate::Scope i_scope(isolate0);
|
|
|
|
v8::HandleScope h_scope(isolate0);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate0);
|
|
|
|
v8::Context::Scope c_scope(context);
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Maybe<int32_t> result =
|
|
|
|
CompileRun("Math.cos(0)")->Int32Value(isolate0->GetCurrentContext());
|
|
|
|
CHECK_EQ(1, result.FromJust());
|
2015-03-03 16:14:04 +00:00
|
|
|
}
|
|
|
|
isolate0->Dispose();
|
|
|
|
|
|
|
|
const char* source1 = "function f() { return 42; }";
|
|
|
|
|
|
|
|
v8::StartupData data1 = v8::V8::CreateSnapshotDataBlob(source1);
|
|
|
|
|
|
|
|
v8::Isolate::CreateParams params1;
|
|
|
|
params1.snapshot_blob = &data1;
|
2015-04-29 09:54:34 +00:00
|
|
|
params1.array_buffer_allocator = CcTest::array_buffer_allocator();
|
2015-03-03 16:14:04 +00:00
|
|
|
v8::Isolate* isolate1 = v8::Isolate::New(params1);
|
|
|
|
{
|
|
|
|
v8::Locker locker(isolate1);
|
|
|
|
v8::Isolate::Scope i_scope(isolate1);
|
|
|
|
v8::HandleScope h_scope(isolate1);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate1);
|
|
|
|
delete[] data1.data; // We can dispose of the snapshot blob now.
|
|
|
|
v8::Context::Scope c_scope(context);
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Maybe<int32_t> result = CompileRun("f()")->Int32Value(context);
|
|
|
|
CHECK_EQ(42, result.FromJust());
|
2015-03-03 16:14:04 +00:00
|
|
|
}
|
|
|
|
isolate1->Dispose();
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CustomSnapshotDataBlobStackOverflow) {
|
2015-05-15 07:42:40 +00:00
|
|
|
DisableTurbofan();
|
|
|
|
const char* source =
|
|
|
|
"var a = [0];"
|
|
|
|
"var b = a;"
|
|
|
|
"for (var i = 0; i < 10000; i++) {"
|
|
|
|
" var c = [i];"
|
|
|
|
" b.push(c);"
|
|
|
|
" b.push(c);"
|
|
|
|
" b = c;"
|
|
|
|
"}";
|
|
|
|
|
|
|
|
v8::StartupData data = v8::V8::CreateSnapshotDataBlob(source);
|
|
|
|
|
|
|
|
v8::Isolate::CreateParams params;
|
|
|
|
params.snapshot_blob = &data;
|
|
|
|
params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
|
|
|
|
v8::Isolate* isolate = v8::Isolate::New(params);
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope i_scope(isolate);
|
|
|
|
v8::HandleScope h_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
delete[] data.data; // We can dispose of the snapshot blob now.
|
|
|
|
v8::Context::Scope c_scope(context);
|
|
|
|
const char* test =
|
|
|
|
"var sum = 0;"
|
|
|
|
"while (a) {"
|
|
|
|
" sum += a[0];"
|
|
|
|
" a = a[1];"
|
|
|
|
"}"
|
|
|
|
"sum";
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Maybe<int32_t> result =
|
|
|
|
CompileRun(test)->Int32Value(isolate->GetCurrentContext());
|
|
|
|
CHECK_EQ(9999 * 5000, result.FromJust());
|
2015-05-15 07:42:40 +00:00
|
|
|
}
|
|
|
|
isolate->Dispose();
|
|
|
|
}
|
|
|
|
|
2016-03-17 10:32:36 +00:00
|
|
|
bool IsCompiled(const char* name) {
|
|
|
|
return i::Handle<i::JSFunction>::cast(
|
|
|
|
v8::Utils::OpenHandle(*CompileRun(name)))
|
|
|
|
->shared()
|
|
|
|
->is_compiled();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SnapshotDataBlobWithWarmup) {
|
|
|
|
DisableTurbofan();
|
2016-06-30 08:41:05 +00:00
|
|
|
const char* warmup = "Math.abs(1); Math.random = 1;";
|
2016-03-17 10:32:36 +00:00
|
|
|
|
|
|
|
v8::StartupData cold = v8::V8::CreateSnapshotDataBlob();
|
|
|
|
v8::StartupData warm = v8::V8::WarmUpSnapshotDataBlob(cold, warmup);
|
|
|
|
delete[] cold.data;
|
|
|
|
|
|
|
|
v8::Isolate::CreateParams params;
|
|
|
|
params.snapshot_blob = &warm;
|
|
|
|
params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
|
|
|
|
v8::Isolate* isolate = v8::Isolate::New(params);
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope i_scope(isolate);
|
|
|
|
v8::HandleScope h_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
delete[] warm.data;
|
|
|
|
v8::Context::Scope c_scope(context);
|
|
|
|
// Running the warmup script has effect on whether functions are
|
|
|
|
// pre-compiled, but does not pollute the context.
|
2016-06-30 08:41:05 +00:00
|
|
|
CHECK(IsCompiled("Math.abs"));
|
[builtins] Unify most of the remaining Math builtins.
Import fdlibm versions of acos, acosh, asin and asinh, which are more
precise and produce the same result across platforms (we were using
libm versions for asin and acos so far, where both speed and precision
depended on the operating system so far). Introduce appropriate TurboFan
operators for these functions and use them both for inlining and for the
generic builtin.
Also migrate the Math.imul and Math.fround builtins to TurboFan builtins
to ensure that their behavior is always exactly the same as the inlined
TurboFan version (i.e. C++ truncation semantics for double to float
don't necessarily meet the JavaScript semantics).
For completeness, also migrate Math.sign, which can even get some nice
love in TurboFan.
Drive-by-fix: Some alpha-sorting on the Math related functions, and
cleanup the list of Math intrinsics that we have to export via the
native context currently.
BUG=v8:3266,v8:3496,v8:3509,v8:3952,v8:5169,v8:5170,v8:5171,v8:5172
TBR=rossberg@chromium.org
R=franzih@chromium.org
Review-Url: https://codereview.chromium.org/2116753002
Cr-Commit-Position: refs/heads/master@{#37476}
2016-07-01 11:11:33 +00:00
|
|
|
CHECK(!IsCompiled("Number.isFinite"));
|
2016-06-30 08:41:05 +00:00
|
|
|
CHECK(CompileRun("Math.random")->IsFunction());
|
2016-03-17 10:32:36 +00:00
|
|
|
}
|
|
|
|
isolate->Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CustomSnapshotDataBlobWithWarmup) {
|
|
|
|
DisableTurbofan();
|
|
|
|
const char* source =
|
2016-06-30 08:41:05 +00:00
|
|
|
"function f() { return Math.abs(1); }\n"
|
[builtins] Unify most of the remaining Math builtins.
Import fdlibm versions of acos, acosh, asin and asinh, which are more
precise and produce the same result across platforms (we were using
libm versions for asin and acos so far, where both speed and precision
depended on the operating system so far). Introduce appropriate TurboFan
operators for these functions and use them both for inlining and for the
generic builtin.
Also migrate the Math.imul and Math.fround builtins to TurboFan builtins
to ensure that their behavior is always exactly the same as the inlined
TurboFan version (i.e. C++ truncation semantics for double to float
don't necessarily meet the JavaScript semantics).
For completeness, also migrate Math.sign, which can even get some nice
love in TurboFan.
Drive-by-fix: Some alpha-sorting on the Math related functions, and
cleanup the list of Math intrinsics that we have to export via the
native context currently.
BUG=v8:3266,v8:3496,v8:3509,v8:3952,v8:5169,v8:5170,v8:5171,v8:5172
TBR=rossberg@chromium.org
R=franzih@chromium.org
Review-Url: https://codereview.chromium.org/2116753002
Cr-Commit-Position: refs/heads/master@{#37476}
2016-07-01 11:11:33 +00:00
|
|
|
"function g() { return Number.isFinite(1); }\n"
|
|
|
|
"Number.isNaN(1);"
|
2016-03-17 10:32:36 +00:00
|
|
|
"var a = 5";
|
|
|
|
const char* warmup = "a = f()";
|
|
|
|
|
|
|
|
v8::StartupData cold = v8::V8::CreateSnapshotDataBlob(source);
|
|
|
|
v8::StartupData warm = v8::V8::WarmUpSnapshotDataBlob(cold, warmup);
|
|
|
|
delete[] cold.data;
|
|
|
|
|
|
|
|
v8::Isolate::CreateParams params;
|
|
|
|
params.snapshot_blob = &warm;
|
|
|
|
params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
|
|
|
|
v8::Isolate* isolate = v8::Isolate::New(params);
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope i_scope(isolate);
|
|
|
|
v8::HandleScope h_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
delete[] warm.data;
|
|
|
|
v8::Context::Scope c_scope(context);
|
|
|
|
// Running the warmup script has effect on whether functions are
|
|
|
|
// pre-compiled, but does not pollute the context.
|
|
|
|
CHECK(IsCompiled("f"));
|
2016-06-30 08:41:05 +00:00
|
|
|
CHECK(IsCompiled("Math.abs"));
|
2016-03-17 10:32:36 +00:00
|
|
|
CHECK(!IsCompiled("g"));
|
[builtins] Unify most of the remaining Math builtins.
Import fdlibm versions of acos, acosh, asin and asinh, which are more
precise and produce the same result across platforms (we were using
libm versions for asin and acos so far, where both speed and precision
depended on the operating system so far). Introduce appropriate TurboFan
operators for these functions and use them both for inlining and for the
generic builtin.
Also migrate the Math.imul and Math.fround builtins to TurboFan builtins
to ensure that their behavior is always exactly the same as the inlined
TurboFan version (i.e. C++ truncation semantics for double to float
don't necessarily meet the JavaScript semantics).
For completeness, also migrate Math.sign, which can even get some nice
love in TurboFan.
Drive-by-fix: Some alpha-sorting on the Math related functions, and
cleanup the list of Math intrinsics that we have to export via the
native context currently.
BUG=v8:3266,v8:3496,v8:3509,v8:3952,v8:5169,v8:5170,v8:5171,v8:5172
TBR=rossberg@chromium.org
R=franzih@chromium.org
Review-Url: https://codereview.chromium.org/2116753002
Cr-Commit-Position: refs/heads/master@{#37476}
2016-07-01 11:11:33 +00:00
|
|
|
CHECK(!IsCompiled("Number.isFinite"));
|
|
|
|
CHECK(!IsCompiled("Number.isNaN"));
|
2016-03-17 10:32:36 +00:00
|
|
|
CHECK_EQ(5, CompileRun("a")->Int32Value(context).FromJust());
|
|
|
|
}
|
|
|
|
isolate->Dispose();
|
|
|
|
}
|
2015-05-15 07:42:40 +00:00
|
|
|
|
2016-03-17 13:15:56 +00:00
|
|
|
TEST(CustomSnapshotDataBlobImmortalImmovableRoots) {
|
|
|
|
DisableTurbofan();
|
|
|
|
// Flood the startup snapshot with shared function infos. If they are
|
|
|
|
// serialized before the immortal immovable root, the root will no longer end
|
|
|
|
// up on the first page.
|
|
|
|
Vector<const uint8_t> source =
|
|
|
|
ConstructSource(STATIC_CHAR_VECTOR("var a = [];"),
|
|
|
|
STATIC_CHAR_VECTOR("a.push(function() {return 7});"),
|
|
|
|
STATIC_CHAR_VECTOR("\0"), 10000);
|
|
|
|
|
|
|
|
v8::StartupData data = v8::V8::CreateSnapshotDataBlob(
|
|
|
|
reinterpret_cast<const char*>(source.start()));
|
|
|
|
|
|
|
|
v8::Isolate::CreateParams params;
|
|
|
|
params.snapshot_blob = &data;
|
|
|
|
params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
|
|
|
|
v8::Isolate* isolate = v8::Isolate::New(params);
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope i_scope(isolate);
|
|
|
|
v8::HandleScope h_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
delete[] data.data; // We can dispose of the snapshot blob now.
|
|
|
|
v8::Context::Scope c_scope(context);
|
|
|
|
CHECK_EQ(7, CompileRun("a[0]()")->Int32Value(context).FromJust());
|
|
|
|
}
|
|
|
|
isolate->Dispose();
|
|
|
|
source.Dispose();
|
|
|
|
}
|
|
|
|
|
2009-10-22 19:09:09 +00:00
|
|
|
TEST(TestThatAlwaysSucceeds) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(TestThatAlwaysFails) {
|
|
|
|
bool ArtificialFailure = false;
|
|
|
|
CHECK(ArtificialFailure);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-14 11:22:03 +00:00
|
|
|
int CountBuiltins() {
|
|
|
|
// Check that we have not deserialized any additional builtin.
|
|
|
|
HeapIterator iterator(CcTest::heap());
|
|
|
|
DisallowHeapAllocation no_allocation;
|
|
|
|
int counter = 0;
|
|
|
|
for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
|
|
|
|
if (obj->IsCode() && Code::cast(obj)->kind() == Code::BUILTIN) counter++;
|
|
|
|
}
|
|
|
|
return counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-06 17:52:20 +00:00
|
|
|
static Handle<SharedFunctionInfo> CompileScript(
|
|
|
|
Isolate* isolate, Handle<String> source, Handle<String> name,
|
|
|
|
ScriptData** cached_data, v8::ScriptCompiler::CompileOptions options) {
|
2016-03-08 12:07:27 +00:00
|
|
|
return Compiler::GetSharedFunctionInfoForScript(
|
2015-05-19 03:11:33 +00:00
|
|
|
source, name, 0, 0, v8::ScriptOriginOptions(), Handle<Object>(),
|
2015-03-05 13:03:42 +00:00
|
|
|
Handle<Context>(isolate->native_context()), NULL, cached_data, options,
|
|
|
|
NOT_NATIVES_CODE, false);
|
2015-02-06 17:52:20 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CodeSerializerOnePlusOne) {
|
2014-07-14 11:22:03 +00:00
|
|
|
FLAG_serialize_toplevel = true;
|
2014-07-15 08:46:47 +00:00
|
|
|
LocalContext context;
|
2014-07-16 06:59:14 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
|
2014-07-14 11:22:03 +00:00
|
|
|
|
2014-07-16 06:59:14 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2014-07-14 11:22:03 +00:00
|
|
|
|
2014-07-16 06:59:14 +00:00
|
|
|
const char* source = "1 + 1";
|
2014-07-14 11:22:03 +00:00
|
|
|
|
2014-07-16 06:59:14 +00:00
|
|
|
Handle<String> orig_source = isolate->factory()
|
|
|
|
->NewStringFromUtf8(CStrVector(source))
|
|
|
|
.ToHandleChecked();
|
|
|
|
Handle<String> copy_source = isolate->factory()
|
|
|
|
->NewStringFromUtf8(CStrVector(source))
|
|
|
|
.ToHandleChecked();
|
|
|
|
CHECK(!orig_source.is_identical_to(copy_source));
|
|
|
|
CHECK(orig_source->Equals(*copy_source));
|
2014-07-14 11:22:03 +00:00
|
|
|
|
|
|
|
ScriptData* cache = NULL;
|
|
|
|
|
2015-02-06 17:52:20 +00:00
|
|
|
Handle<SharedFunctionInfo> orig =
|
|
|
|
CompileScript(isolate, orig_source, Handle<String>(), &cache,
|
|
|
|
v8::ScriptCompiler::kProduceCodeCache);
|
2014-07-14 11:22:03 +00:00
|
|
|
|
|
|
|
int builtins_count = CountBuiltins();
|
|
|
|
|
2014-07-16 06:59:14 +00:00
|
|
|
Handle<SharedFunctionInfo> copy;
|
|
|
|
{
|
|
|
|
DisallowCompilation no_compile_expected(isolate);
|
2015-02-06 17:52:20 +00:00
|
|
|
copy = CompileScript(isolate, copy_source, Handle<String>(), &cache,
|
|
|
|
v8::ScriptCompiler::kConsumeCodeCache);
|
2014-07-16 06:59:14 +00:00
|
|
|
}
|
Change ScriptCompiler::CompileOptions to allow for two 'cache' modes
(parser or code) and to be explicit about cache consumption or production
(rather than making presence of cached_data imply one or the other.)
Also add a --cache flag to d8, to allow testing the functionality.
-----------------------------
API change
Reason: Currently, V8 supports a 'parser cache' for repeatedly executing the same script. We'd like to add a 2nd mode that would cache code, and would like to let the embedder decide which mode they chose (if any).
Note: Previously, the 'use cached data' property was implied by the presence of the cached data itself. (That is, kNoCompileOptions and source->cached_data != NULL.) That is no longer sufficient, since the presence of data is no longer sufficient to determine /which kind/ of data is present.
Changes from old behaviour:
- If you previously didn't use caching, nothing changes.
Example:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
- If you previously used caching, it worked like this:
- 1st run:
v8::CompileUnbound(isolate, source, kProduceToCache);
Then, source->cached_data would contain the
data-to-be cached. This remains the same, except you
need to tell V8 which type of data you want.
v8::CompileUnbound(isolate, source, kProduceParserCache);
- 2nd run:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
with source->cached_data set to the data you received in
the first run. This will now ignore the cached data, and
you need to explicitly tell V8 to use it:
v8::CompileUnbound(isolate, source, kConsumeParserCache);
-----------------------------
BUG=
R=marja@chromium.org, yangguo@chromium.org
Review URL: https://codereview.chromium.org/389573006
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22431 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-07-16 12:18:33 +00:00
|
|
|
|
2014-07-15 08:46:47 +00:00
|
|
|
CHECK_NE(*orig, *copy);
|
2014-07-16 06:59:14 +00:00
|
|
|
CHECK(Script::cast(copy->script())->source() == *copy_source);
|
2014-07-15 10:17:22 +00:00
|
|
|
|
2014-07-15 08:46:47 +00:00
|
|
|
Handle<JSFunction> copy_fun =
|
2014-07-14 11:22:03 +00:00
|
|
|
isolate->factory()->NewFunctionFromSharedFunctionInfo(
|
2014-07-15 08:46:47 +00:00
|
|
|
copy, isolate->native_context());
|
2014-07-14 11:22:03 +00:00
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
2014-07-15 08:46:47 +00:00
|
|
|
Handle<Object> copy_result =
|
|
|
|
Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked();
|
|
|
|
CHECK_EQ(2, Handle<Smi>::cast(copy_result)->value());
|
2014-07-14 11:22:03 +00:00
|
|
|
|
|
|
|
CHECK_EQ(builtins_count, CountBuiltins());
|
|
|
|
|
|
|
|
delete cache;
|
|
|
|
}
|
2014-07-15 08:46:47 +00:00
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CodeSerializerPromotedToCompilationCache) {
|
2015-03-16 13:19:10 +00:00
|
|
|
FLAG_serialize_toplevel = true;
|
|
|
|
LocalContext context;
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
|
|
|
const char* source = "1 + 1";
|
|
|
|
|
|
|
|
Handle<String> src = isolate->factory()
|
|
|
|
->NewStringFromUtf8(CStrVector(source))
|
|
|
|
.ToHandleChecked();
|
|
|
|
ScriptData* cache = NULL;
|
|
|
|
|
|
|
|
CompileScript(isolate, src, src, &cache,
|
|
|
|
v8::ScriptCompiler::kProduceCodeCache);
|
|
|
|
|
|
|
|
DisallowCompilation no_compile_expected(isolate);
|
|
|
|
Handle<SharedFunctionInfo> copy = CompileScript(
|
|
|
|
isolate, src, src, &cache, v8::ScriptCompiler::kConsumeCodeCache);
|
|
|
|
|
|
|
|
CHECK(isolate->compilation_cache()
|
2015-05-19 03:11:33 +00:00
|
|
|
->LookupScript(src, src, 0, 0, v8::ScriptOriginOptions(),
|
2015-03-16 13:19:10 +00:00
|
|
|
isolate->native_context(), SLOPPY)
|
|
|
|
.ToHandleChecked()
|
|
|
|
.is_identical_to(copy));
|
|
|
|
|
|
|
|
delete cache;
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CodeSerializerInternalizedString) {
|
2014-07-15 08:46:47 +00:00
|
|
|
FLAG_serialize_toplevel = true;
|
|
|
|
LocalContext context;
|
2014-07-16 06:59:14 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
|
|
|
|
|
2014-07-15 08:46:47 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
2014-07-16 06:59:14 +00:00
|
|
|
const char* source = "'string1'";
|
2014-07-15 08:46:47 +00:00
|
|
|
|
2014-07-16 06:59:14 +00:00
|
|
|
Handle<String> orig_source = isolate->factory()
|
|
|
|
->NewStringFromUtf8(CStrVector(source))
|
|
|
|
.ToHandleChecked();
|
|
|
|
Handle<String> copy_source = isolate->factory()
|
|
|
|
->NewStringFromUtf8(CStrVector(source))
|
|
|
|
.ToHandleChecked();
|
|
|
|
CHECK(!orig_source.is_identical_to(copy_source));
|
|
|
|
CHECK(orig_source->Equals(*copy_source));
|
2014-07-15 08:46:47 +00:00
|
|
|
|
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
|
|
|
ScriptData* cache = NULL;
|
|
|
|
|
2015-02-06 17:52:20 +00:00
|
|
|
Handle<SharedFunctionInfo> orig =
|
|
|
|
CompileScript(isolate, orig_source, Handle<String>(), &cache,
|
|
|
|
v8::ScriptCompiler::kProduceCodeCache);
|
2014-07-15 08:46:47 +00:00
|
|
|
Handle<JSFunction> orig_fun =
|
|
|
|
isolate->factory()->NewFunctionFromSharedFunctionInfo(
|
|
|
|
orig, isolate->native_context());
|
|
|
|
Handle<Object> orig_result =
|
|
|
|
Execution::Call(isolate, orig_fun, global, 0, NULL).ToHandleChecked();
|
|
|
|
CHECK(orig_result->IsInternalizedString());
|
|
|
|
|
|
|
|
int builtins_count = CountBuiltins();
|
|
|
|
|
2014-07-16 06:59:14 +00:00
|
|
|
Handle<SharedFunctionInfo> copy;
|
|
|
|
{
|
|
|
|
DisallowCompilation no_compile_expected(isolate);
|
2015-02-06 17:52:20 +00:00
|
|
|
copy = CompileScript(isolate, copy_source, Handle<String>(), &cache,
|
|
|
|
v8::ScriptCompiler::kConsumeCodeCache);
|
2014-07-16 06:59:14 +00:00
|
|
|
}
|
2014-07-15 08:46:47 +00:00
|
|
|
CHECK_NE(*orig, *copy);
|
2014-07-16 06:59:14 +00:00
|
|
|
CHECK(Script::cast(copy->script())->source() == *copy_source);
|
2014-07-15 10:17:22 +00:00
|
|
|
|
2014-07-15 08:46:47 +00:00
|
|
|
Handle<JSFunction> copy_fun =
|
|
|
|
isolate->factory()->NewFunctionFromSharedFunctionInfo(
|
|
|
|
copy, isolate->native_context());
|
|
|
|
CHECK_NE(*orig_fun, *copy_fun);
|
|
|
|
Handle<Object> copy_result =
|
|
|
|
Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked();
|
|
|
|
CHECK(orig_result.is_identical_to(copy_result));
|
|
|
|
Handle<String> expected =
|
|
|
|
isolate->factory()->NewStringFromAsciiChecked("string1");
|
|
|
|
|
|
|
|
CHECK(Handle<String>::cast(copy_result)->Equals(*expected));
|
|
|
|
CHECK_EQ(builtins_count, CountBuiltins());
|
|
|
|
|
|
|
|
delete cache;
|
|
|
|
}
|
2014-07-23 07:16:32 +00:00
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CodeSerializerLargeCodeObject) {
|
2014-09-25 07:32:13 +00:00
|
|
|
FLAG_serialize_toplevel = true;
|
|
|
|
LocalContext context;
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
|
|
|
|
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
2016-08-09 12:16:48 +00:00
|
|
|
// The serializer only tests the shared code, which is always the unoptimized
|
|
|
|
// code. Don't even bother generating optimized code to avoid timeouts.
|
|
|
|
FLAG_always_opt = false;
|
|
|
|
|
2014-09-25 07:32:13 +00:00
|
|
|
Vector<const uint8_t> source =
|
2016-08-09 12:16:48 +00:00
|
|
|
ConstructSource(STATIC_CHAR_VECTOR("var j=1; if (!j) {"),
|
|
|
|
STATIC_CHAR_VECTOR("for (let i of Object.prototype);"),
|
|
|
|
STATIC_CHAR_VECTOR("} j=7; j"), 1500);
|
2014-09-25 07:32:13 +00:00
|
|
|
Handle<String> source_str =
|
|
|
|
isolate->factory()->NewStringFromOneByte(source).ToHandleChecked();
|
|
|
|
|
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
|
|
|
ScriptData* cache = NULL;
|
|
|
|
|
2015-02-06 17:52:20 +00:00
|
|
|
Handle<SharedFunctionInfo> orig =
|
|
|
|
CompileScript(isolate, source_str, Handle<String>(), &cache,
|
|
|
|
v8::ScriptCompiler::kProduceCodeCache);
|
2014-09-25 07:32:13 +00:00
|
|
|
|
2016-08-09 12:16:48 +00:00
|
|
|
CHECK(isolate->heap()->InSpace(orig->abstract_code(), LO_SPACE));
|
2014-09-25 07:32:13 +00:00
|
|
|
|
|
|
|
Handle<SharedFunctionInfo> copy;
|
|
|
|
{
|
|
|
|
DisallowCompilation no_compile_expected(isolate);
|
2015-02-06 17:52:20 +00:00
|
|
|
copy = CompileScript(isolate, source_str, Handle<String>(), &cache,
|
|
|
|
v8::ScriptCompiler::kConsumeCodeCache);
|
2014-09-25 07:32:13 +00:00
|
|
|
}
|
|
|
|
CHECK_NE(*orig, *copy);
|
|
|
|
|
|
|
|
Handle<JSFunction> copy_fun =
|
|
|
|
isolate->factory()->NewFunctionFromSharedFunctionInfo(
|
|
|
|
copy, isolate->native_context());
|
|
|
|
|
|
|
|
Handle<Object> copy_result =
|
|
|
|
Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked();
|
|
|
|
|
|
|
|
int result_int;
|
|
|
|
CHECK(copy_result->ToInt32(&result_int));
|
|
|
|
CHECK_EQ(7, result_int);
|
|
|
|
|
|
|
|
delete cache;
|
|
|
|
source.Dispose();
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CodeSerializerLargeStrings) {
|
2014-09-25 07:32:13 +00:00
|
|
|
FLAG_serialize_toplevel = true;
|
|
|
|
LocalContext context;
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2014-10-27 16:40:04 +00:00
|
|
|
Factory* f = isolate->factory();
|
2014-09-25 07:32:13 +00:00
|
|
|
isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
|
|
|
|
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
2014-10-27 16:40:04 +00:00
|
|
|
Vector<const uint8_t> source_s = ConstructSource(
|
2014-09-25 07:32:13 +00:00
|
|
|
STATIC_CHAR_VECTOR("var s = \""), STATIC_CHAR_VECTOR("abcdef"),
|
2014-10-27 16:40:04 +00:00
|
|
|
STATIC_CHAR_VECTOR("\";"), 1000000);
|
|
|
|
Vector<const uint8_t> source_t = ConstructSource(
|
|
|
|
STATIC_CHAR_VECTOR("var t = \""), STATIC_CHAR_VECTOR("uvwxyz"),
|
|
|
|
STATIC_CHAR_VECTOR("\"; s + t"), 999999);
|
2014-09-25 07:32:13 +00:00
|
|
|
Handle<String> source_str =
|
2014-10-27 16:40:04 +00:00
|
|
|
f->NewConsString(f->NewStringFromOneByte(source_s).ToHandleChecked(),
|
|
|
|
f->NewStringFromOneByte(source_t).ToHandleChecked())
|
|
|
|
.ToHandleChecked();
|
2014-09-25 07:32:13 +00:00
|
|
|
|
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
|
|
|
ScriptData* cache = NULL;
|
|
|
|
|
2015-02-06 17:52:20 +00:00
|
|
|
Handle<SharedFunctionInfo> orig =
|
|
|
|
CompileScript(isolate, source_str, Handle<String>(), &cache,
|
|
|
|
v8::ScriptCompiler::kProduceCodeCache);
|
2014-09-25 07:32:13 +00:00
|
|
|
|
|
|
|
Handle<SharedFunctionInfo> copy;
|
|
|
|
{
|
|
|
|
DisallowCompilation no_compile_expected(isolate);
|
2015-02-06 17:52:20 +00:00
|
|
|
copy = CompileScript(isolate, source_str, Handle<String>(), &cache,
|
|
|
|
v8::ScriptCompiler::kConsumeCodeCache);
|
2014-09-25 07:32:13 +00:00
|
|
|
}
|
|
|
|
CHECK_NE(*orig, *copy);
|
|
|
|
|
|
|
|
Handle<JSFunction> copy_fun =
|
|
|
|
isolate->factory()->NewFunctionFromSharedFunctionInfo(
|
|
|
|
copy, isolate->native_context());
|
|
|
|
|
|
|
|
Handle<Object> copy_result =
|
|
|
|
Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked();
|
|
|
|
|
2014-10-27 16:40:04 +00:00
|
|
|
CHECK_EQ(6 * 1999999, Handle<String>::cast(copy_result)->length());
|
2015-05-12 13:52:26 +00:00
|
|
|
Handle<Object> property = JSReceiver::GetDataProperty(
|
2014-10-27 16:40:04 +00:00
|
|
|
isolate->global_object(), f->NewStringFromAsciiChecked("s"));
|
|
|
|
CHECK(isolate->heap()->InSpace(HeapObject::cast(*property), LO_SPACE));
|
2015-05-12 13:52:26 +00:00
|
|
|
property = JSReceiver::GetDataProperty(isolate->global_object(),
|
|
|
|
f->NewStringFromAsciiChecked("t"));
|
2014-10-27 16:40:04 +00:00
|
|
|
CHECK(isolate->heap()->InSpace(HeapObject::cast(*property), LO_SPACE));
|
2014-10-24 12:40:05 +00:00
|
|
|
// Make sure we do not serialize too much, e.g. include the source string.
|
2014-10-27 16:40:04 +00:00
|
|
|
CHECK_LT(cache->length(), 13000000);
|
2014-09-25 07:32:13 +00:00
|
|
|
|
|
|
|
delete cache;
|
2014-10-27 16:40:04 +00:00
|
|
|
source_s.Dispose();
|
|
|
|
source_t.Dispose();
|
2014-09-25 07:32:13 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CodeSerializerThreeBigStrings) {
|
2014-10-15 14:04:53 +00:00
|
|
|
FLAG_serialize_toplevel = true;
|
|
|
|
LocalContext context;
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* f = isolate->factory();
|
|
|
|
isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
|
|
|
|
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
|
|
|
Vector<const uint8_t> source_a =
|
|
|
|
ConstructSource(STATIC_CHAR_VECTOR("var a = \""), STATIC_CHAR_VECTOR("a"),
|
|
|
|
STATIC_CHAR_VECTOR("\";"), 700000);
|
|
|
|
Handle<String> source_a_str =
|
|
|
|
f->NewStringFromOneByte(source_a).ToHandleChecked();
|
|
|
|
|
|
|
|
Vector<const uint8_t> source_b =
|
|
|
|
ConstructSource(STATIC_CHAR_VECTOR("var b = \""), STATIC_CHAR_VECTOR("b"),
|
2016-08-28 07:28:14 +00:00
|
|
|
STATIC_CHAR_VECTOR("\";"), 600000);
|
2014-10-15 14:04:53 +00:00
|
|
|
Handle<String> source_b_str =
|
|
|
|
f->NewStringFromOneByte(source_b).ToHandleChecked();
|
|
|
|
|
|
|
|
Vector<const uint8_t> source_c =
|
|
|
|
ConstructSource(STATIC_CHAR_VECTOR("var c = \""), STATIC_CHAR_VECTOR("c"),
|
2016-08-28 07:28:14 +00:00
|
|
|
STATIC_CHAR_VECTOR("\";"), 500000);
|
2014-10-15 14:04:53 +00:00
|
|
|
Handle<String> source_c_str =
|
|
|
|
f->NewStringFromOneByte(source_c).ToHandleChecked();
|
|
|
|
|
|
|
|
Handle<String> source_str =
|
|
|
|
f->NewConsString(
|
|
|
|
f->NewConsString(source_a_str, source_b_str).ToHandleChecked(),
|
|
|
|
source_c_str).ToHandleChecked();
|
|
|
|
|
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
|
|
|
ScriptData* cache = NULL;
|
|
|
|
|
2015-02-06 17:52:20 +00:00
|
|
|
Handle<SharedFunctionInfo> orig =
|
|
|
|
CompileScript(isolate, source_str, Handle<String>(), &cache,
|
|
|
|
v8::ScriptCompiler::kProduceCodeCache);
|
2014-10-15 14:04:53 +00:00
|
|
|
|
|
|
|
Handle<SharedFunctionInfo> copy;
|
|
|
|
{
|
|
|
|
DisallowCompilation no_compile_expected(isolate);
|
2015-02-06 17:52:20 +00:00
|
|
|
copy = CompileScript(isolate, source_str, Handle<String>(), &cache,
|
|
|
|
v8::ScriptCompiler::kConsumeCodeCache);
|
2014-10-15 14:04:53 +00:00
|
|
|
}
|
|
|
|
CHECK_NE(*orig, *copy);
|
|
|
|
|
|
|
|
Handle<JSFunction> copy_fun =
|
|
|
|
isolate->factory()->NewFunctionFromSharedFunctionInfo(
|
|
|
|
copy, isolate->native_context());
|
|
|
|
|
2014-10-15 14:42:32 +00:00
|
|
|
USE(Execution::Call(isolate, copy_fun, global, 0, NULL));
|
2014-10-15 14:04:53 +00:00
|
|
|
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Maybe<int32_t> result =
|
|
|
|
CompileRun("(a + b).length")
|
|
|
|
->Int32Value(v8::Isolate::GetCurrent()->GetCurrentContext());
|
2016-08-28 07:28:14 +00:00
|
|
|
CHECK_EQ(600000 + 700000, result.FromJust());
|
2015-10-08 09:48:05 +00:00
|
|
|
result = CompileRun("(b + c).length")
|
|
|
|
->Int32Value(v8::Isolate::GetCurrent()->GetCurrentContext());
|
2016-08-28 07:28:14 +00:00
|
|
|
CHECK_EQ(500000 + 600000, result.FromJust());
|
2014-10-15 14:04:53 +00:00
|
|
|
Heap* heap = isolate->heap();
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::String> result_str =
|
|
|
|
CompileRun("a")
|
|
|
|
->ToString(CcTest::isolate()->GetCurrentContext())
|
|
|
|
.ToLocalChecked();
|
2015-10-15 10:55:12 +00:00
|
|
|
CHECK(heap->InSpace(*v8::Utils::OpenHandle(*result_str), LO_SPACE));
|
2015-10-08 09:48:05 +00:00
|
|
|
result_str = CompileRun("b")
|
|
|
|
->ToString(CcTest::isolate()->GetCurrentContext())
|
|
|
|
.ToLocalChecked();
|
2015-10-13 13:47:14 +00:00
|
|
|
CHECK(heap->InSpace(*v8::Utils::OpenHandle(*result_str), OLD_SPACE));
|
2015-10-08 09:48:05 +00:00
|
|
|
result_str = CompileRun("c")
|
|
|
|
->ToString(CcTest::isolate()->GetCurrentContext())
|
|
|
|
.ToLocalChecked();
|
2015-10-13 13:47:14 +00:00
|
|
|
CHECK(heap->InSpace(*v8::Utils::OpenHandle(*result_str), OLD_SPACE));
|
2014-10-15 14:04:53 +00:00
|
|
|
|
|
|
|
delete cache;
|
|
|
|
source_a.Dispose();
|
|
|
|
source_b.Dispose();
|
2014-10-16 13:26:28 +00:00
|
|
|
source_c.Dispose();
|
2014-10-15 14:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-02 08:18:03 +00:00
|
|
|
class SerializerOneByteResource
|
|
|
|
: public v8::String::ExternalOneByteStringResource {
|
2014-10-02 07:12:46 +00:00
|
|
|
public:
|
2014-10-02 08:18:03 +00:00
|
|
|
SerializerOneByteResource(const char* data, size_t length)
|
2014-10-02 07:12:46 +00:00
|
|
|
: data_(data), length_(length) {}
|
|
|
|
virtual const char* data() const { return data_; }
|
|
|
|
virtual size_t length() const { return length_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char* data_;
|
|
|
|
size_t length_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-10-02 08:18:03 +00:00
|
|
|
class SerializerTwoByteResource : public v8::String::ExternalStringResource {
|
2014-10-02 07:12:46 +00:00
|
|
|
public:
|
2014-10-02 08:18:03 +00:00
|
|
|
SerializerTwoByteResource(const char* data, size_t length)
|
2014-10-02 07:12:46 +00:00
|
|
|
: data_(AsciiToTwoByteString(data)), length_(length) {}
|
2014-10-02 08:18:03 +00:00
|
|
|
~SerializerTwoByteResource() { DeleteArray<const uint16_t>(data_); }
|
2014-10-02 07:12:46 +00:00
|
|
|
|
|
|
|
virtual const uint16_t* data() const { return data_; }
|
|
|
|
virtual size_t length() const { return length_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const uint16_t* data_;
|
|
|
|
size_t length_;
|
|
|
|
};
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CodeSerializerExternalString) {
|
2014-10-02 07:12:46 +00:00
|
|
|
FLAG_serialize_toplevel = true;
|
|
|
|
LocalContext context;
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
|
|
|
|
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
|
|
|
// Obtain external internalized one-byte string.
|
2014-10-02 08:18:03 +00:00
|
|
|
SerializerOneByteResource one_byte_resource("one_byte", 8);
|
2014-10-02 07:12:46 +00:00
|
|
|
Handle<String> one_byte_string =
|
|
|
|
isolate->factory()->NewStringFromAsciiChecked("one_byte");
|
|
|
|
one_byte_string = isolate->factory()->InternalizeString(one_byte_string);
|
|
|
|
one_byte_string->MakeExternal(&one_byte_resource);
|
|
|
|
CHECK(one_byte_string->IsExternalOneByteString());
|
|
|
|
CHECK(one_byte_string->IsInternalizedString());
|
|
|
|
|
|
|
|
// Obtain external internalized two-byte string.
|
2014-10-02 08:18:03 +00:00
|
|
|
SerializerTwoByteResource two_byte_resource("two_byte", 8);
|
2014-10-02 07:12:46 +00:00
|
|
|
Handle<String> two_byte_string =
|
|
|
|
isolate->factory()->NewStringFromAsciiChecked("two_byte");
|
|
|
|
two_byte_string = isolate->factory()->InternalizeString(two_byte_string);
|
|
|
|
two_byte_string->MakeExternal(&two_byte_resource);
|
|
|
|
CHECK(two_byte_string->IsExternalTwoByteString());
|
|
|
|
CHECK(two_byte_string->IsInternalizedString());
|
|
|
|
|
|
|
|
const char* source =
|
|
|
|
"var o = {} \n"
|
|
|
|
"o.one_byte = 7; \n"
|
|
|
|
"o.two_byte = 8; \n"
|
|
|
|
"o.one_byte + o.two_byte; \n";
|
|
|
|
Handle<String> source_string = isolate->factory()
|
|
|
|
->NewStringFromUtf8(CStrVector(source))
|
|
|
|
.ToHandleChecked();
|
|
|
|
|
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
|
|
|
ScriptData* cache = NULL;
|
|
|
|
|
2015-02-06 17:52:20 +00:00
|
|
|
Handle<SharedFunctionInfo> orig =
|
|
|
|
CompileScript(isolate, source_string, Handle<String>(), &cache,
|
|
|
|
v8::ScriptCompiler::kProduceCodeCache);
|
2014-10-02 07:12:46 +00:00
|
|
|
|
|
|
|
Handle<SharedFunctionInfo> copy;
|
|
|
|
{
|
|
|
|
DisallowCompilation no_compile_expected(isolate);
|
2015-02-06 17:52:20 +00:00
|
|
|
copy = CompileScript(isolate, source_string, Handle<String>(), &cache,
|
|
|
|
v8::ScriptCompiler::kConsumeCodeCache);
|
2014-10-02 07:12:46 +00:00
|
|
|
}
|
|
|
|
CHECK_NE(*orig, *copy);
|
|
|
|
|
|
|
|
Handle<JSFunction> copy_fun =
|
|
|
|
isolate->factory()->NewFunctionFromSharedFunctionInfo(
|
|
|
|
copy, isolate->native_context());
|
|
|
|
|
|
|
|
Handle<Object> copy_result =
|
|
|
|
Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked();
|
|
|
|
|
2015-10-06 14:50:29 +00:00
|
|
|
CHECK_EQ(15.0, copy_result->Number());
|
2014-10-02 07:12:46 +00:00
|
|
|
|
|
|
|
delete cache;
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CodeSerializerLargeExternalString) {
|
2014-10-02 07:12:46 +00:00
|
|
|
FLAG_serialize_toplevel = true;
|
|
|
|
LocalContext context;
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
|
|
|
|
|
|
|
|
Factory* f = isolate->factory();
|
|
|
|
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
|
|
|
// Create a huge external internalized string to use as variable name.
|
|
|
|
Vector<const uint8_t> string =
|
|
|
|
ConstructSource(STATIC_CHAR_VECTOR(""), STATIC_CHAR_VECTOR("abcdef"),
|
2014-10-24 07:29:39 +00:00
|
|
|
STATIC_CHAR_VECTOR(""), 999999);
|
2014-10-02 07:12:46 +00:00
|
|
|
Handle<String> name = f->NewStringFromOneByte(string).ToHandleChecked();
|
2014-10-02 08:18:03 +00:00
|
|
|
SerializerOneByteResource one_byte_resource(
|
2014-10-02 07:12:46 +00:00
|
|
|
reinterpret_cast<const char*>(string.start()), string.length());
|
|
|
|
name = f->InternalizeString(name);
|
|
|
|
name->MakeExternal(&one_byte_resource);
|
|
|
|
CHECK(name->IsExternalOneByteString());
|
|
|
|
CHECK(name->IsInternalizedString());
|
|
|
|
CHECK(isolate->heap()->InSpace(*name, LO_SPACE));
|
|
|
|
|
|
|
|
// Create the source, which is "var <literal> = 42; <literal>".
|
|
|
|
Handle<String> source_str =
|
|
|
|
f->NewConsString(
|
|
|
|
f->NewConsString(f->NewStringFromAsciiChecked("var "), name)
|
|
|
|
.ToHandleChecked(),
|
|
|
|
f->NewConsString(f->NewStringFromAsciiChecked(" = 42; "), name)
|
|
|
|
.ToHandleChecked()).ToHandleChecked();
|
|
|
|
|
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
|
|
|
ScriptData* cache = NULL;
|
|
|
|
|
2015-02-06 17:52:20 +00:00
|
|
|
Handle<SharedFunctionInfo> orig =
|
|
|
|
CompileScript(isolate, source_str, Handle<String>(), &cache,
|
|
|
|
v8::ScriptCompiler::kProduceCodeCache);
|
2014-10-02 07:12:46 +00:00
|
|
|
|
|
|
|
Handle<SharedFunctionInfo> copy;
|
|
|
|
{
|
|
|
|
DisallowCompilation no_compile_expected(isolate);
|
2015-02-06 17:52:20 +00:00
|
|
|
copy = CompileScript(isolate, source_str, Handle<String>(), &cache,
|
|
|
|
v8::ScriptCompiler::kConsumeCodeCache);
|
2014-10-02 07:12:46 +00:00
|
|
|
}
|
|
|
|
CHECK_NE(*orig, *copy);
|
|
|
|
|
|
|
|
Handle<JSFunction> copy_fun =
|
2014-10-02 09:39:13 +00:00
|
|
|
f->NewFunctionFromSharedFunctionInfo(copy, isolate->native_context());
|
2014-10-02 07:12:46 +00:00
|
|
|
|
|
|
|
Handle<Object> copy_result =
|
|
|
|
Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked();
|
|
|
|
|
2015-10-06 14:50:29 +00:00
|
|
|
CHECK_EQ(42.0, copy_result->Number());
|
2014-10-02 07:12:46 +00:00
|
|
|
|
|
|
|
delete cache;
|
|
|
|
string.Dispose();
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CodeSerializerExternalScriptName) {
|
2014-10-02 09:39:13 +00:00
|
|
|
FLAG_serialize_toplevel = true;
|
|
|
|
LocalContext context;
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
|
|
|
|
|
|
|
|
Factory* f = isolate->factory();
|
|
|
|
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
|
|
|
const char* source =
|
|
|
|
"var a = [1, 2, 3, 4];"
|
|
|
|
"a.reduce(function(x, y) { return x + y }, 0)";
|
|
|
|
|
|
|
|
Handle<String> source_string =
|
|
|
|
f->NewStringFromUtf8(CStrVector(source)).ToHandleChecked();
|
|
|
|
|
|
|
|
const SerializerOneByteResource one_byte_resource("one_byte", 8);
|
|
|
|
Handle<String> name =
|
|
|
|
f->NewExternalStringFromOneByte(&one_byte_resource).ToHandleChecked();
|
|
|
|
CHECK(name->IsExternalOneByteString());
|
|
|
|
CHECK(!name->IsInternalizedString());
|
|
|
|
|
|
|
|
Handle<JSObject> global(isolate->context()->global_object());
|
|
|
|
ScriptData* cache = NULL;
|
|
|
|
|
2015-02-06 17:52:20 +00:00
|
|
|
Handle<SharedFunctionInfo> orig =
|
|
|
|
CompileScript(isolate, source_string, name, &cache,
|
|
|
|
v8::ScriptCompiler::kProduceCodeCache);
|
2014-10-02 09:39:13 +00:00
|
|
|
|
|
|
|
Handle<SharedFunctionInfo> copy;
|
|
|
|
{
|
|
|
|
DisallowCompilation no_compile_expected(isolate);
|
2015-02-06 17:52:20 +00:00
|
|
|
copy = CompileScript(isolate, source_string, name, &cache,
|
|
|
|
v8::ScriptCompiler::kConsumeCodeCache);
|
2014-10-02 09:39:13 +00:00
|
|
|
}
|
|
|
|
CHECK_NE(*orig, *copy);
|
|
|
|
|
|
|
|
Handle<JSFunction> copy_fun =
|
|
|
|
f->NewFunctionFromSharedFunctionInfo(copy, isolate->native_context());
|
|
|
|
|
|
|
|
Handle<Object> copy_result =
|
|
|
|
Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked();
|
|
|
|
|
2015-10-06 14:50:29 +00:00
|
|
|
CHECK_EQ(10.0, copy_result->Number());
|
2014-10-02 09:39:13 +00:00
|
|
|
|
|
|
|
delete cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-24 08:37:03 +00:00
|
|
|
static bool toplevel_test_code_event_found = false;
|
|
|
|
|
|
|
|
|
|
|
|
static void SerializerCodeEventListener(const v8::JitCodeEvent* event) {
|
|
|
|
if (event->type == v8::JitCodeEvent::CODE_ADDED &&
|
|
|
|
memcmp(event->name.str, "Script:~test", 12) == 0) {
|
|
|
|
toplevel_test_code_event_found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-09 14:49:35 +00:00
|
|
|
v8::ScriptCompiler::CachedData* ProduceCache(const char* source) {
|
2014-07-23 07:16:32 +00:00
|
|
|
v8::ScriptCompiler::CachedData* cache;
|
2015-04-29 09:54:34 +00:00
|
|
|
v8::Isolate::CreateParams create_params;
|
|
|
|
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
v8::Isolate* isolate1 = v8::Isolate::New(create_params);
|
2014-07-23 07:16:32 +00:00
|
|
|
{
|
2014-07-24 12:11:30 +00:00
|
|
|
v8::Isolate::Scope iscope(isolate1);
|
|
|
|
v8::HandleScope scope(isolate1);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate1);
|
2014-07-23 07:16:32 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
v8::Local<v8::String> source_str = v8_str(source);
|
|
|
|
v8::ScriptOrigin origin(v8_str("test"));
|
|
|
|
v8::ScriptCompiler::Source source(source_str, origin);
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::UnboundScript> script =
|
|
|
|
v8::ScriptCompiler::CompileUnboundScript(
|
|
|
|
isolate1, &source, v8::ScriptCompiler::kProduceCodeCache)
|
|
|
|
.ToLocalChecked();
|
2014-07-23 07:16:32 +00:00
|
|
|
const v8::ScriptCompiler::CachedData* data = source.GetCachedData();
|
2014-10-17 09:54:48 +00:00
|
|
|
CHECK(data);
|
2014-07-23 07:16:32 +00:00
|
|
|
// Persist cached data.
|
|
|
|
uint8_t* buffer = NewArray<uint8_t>(data->length);
|
|
|
|
MemCopy(buffer, data->data, data->length);
|
|
|
|
cache = new v8::ScriptCompiler::CachedData(
|
|
|
|
buffer, data->length, v8::ScriptCompiler::CachedData::BufferOwned);
|
|
|
|
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::Value> result = script->BindToCurrentContext()
|
|
|
|
->Run(isolate1->GetCurrentContext())
|
|
|
|
.ToLocalChecked();
|
|
|
|
v8::Local<v8::String> result_string =
|
|
|
|
result->ToString(isolate1->GetCurrentContext()).ToLocalChecked();
|
|
|
|
CHECK(result_string->Equals(isolate1->GetCurrentContext(), v8_str("abcdef"))
|
|
|
|
.FromJust());
|
2014-07-23 07:16:32 +00:00
|
|
|
}
|
2014-07-24 12:11:30 +00:00
|
|
|
isolate1->Dispose();
|
2015-02-09 14:49:35 +00:00
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CodeSerializerIsolates) {
|
2015-02-09 14:49:35 +00:00
|
|
|
FLAG_serialize_toplevel = true;
|
|
|
|
|
|
|
|
const char* source = "function f() { return 'abc'; }; f() + 'def'";
|
|
|
|
v8::ScriptCompiler::CachedData* cache = ProduceCache(source);
|
2014-07-23 07:16:32 +00:00
|
|
|
|
2015-04-29 09:54:34 +00:00
|
|
|
v8::Isolate::CreateParams create_params;
|
|
|
|
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
v8::Isolate* isolate2 = v8::Isolate::New(create_params);
|
2014-10-24 08:37:03 +00:00
|
|
|
isolate2->SetJitCodeEventHandler(v8::kJitCodeEventDefault,
|
|
|
|
SerializerCodeEventListener);
|
|
|
|
toplevel_test_code_event_found = false;
|
2014-07-23 07:16:32 +00:00
|
|
|
{
|
2014-07-24 12:11:30 +00:00
|
|
|
v8::Isolate::Scope iscope(isolate2);
|
|
|
|
v8::HandleScope scope(isolate2);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate2);
|
2014-07-23 07:16:32 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
v8::Local<v8::String> source_str = v8_str(source);
|
|
|
|
v8::ScriptOrigin origin(v8_str("test"));
|
|
|
|
v8::ScriptCompiler::Source source(source_str, origin, cache);
|
|
|
|
v8::Local<v8::UnboundScript> script;
|
|
|
|
{
|
2014-07-24 12:11:30 +00:00
|
|
|
DisallowCompilation no_compile(reinterpret_cast<Isolate*>(isolate2));
|
2015-10-08 09:48:05 +00:00
|
|
|
script = v8::ScriptCompiler::CompileUnboundScript(
|
|
|
|
isolate2, &source, v8::ScriptCompiler::kConsumeCodeCache)
|
|
|
|
.ToLocalChecked();
|
2014-07-23 07:16:32 +00:00
|
|
|
}
|
2014-11-17 12:16:27 +00:00
|
|
|
CHECK(!cache->rejected);
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::Value> result = script->BindToCurrentContext()
|
|
|
|
->Run(isolate2->GetCurrentContext())
|
|
|
|
.ToLocalChecked();
|
|
|
|
CHECK(result->ToString(isolate2->GetCurrentContext())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Equals(isolate2->GetCurrentContext(), v8_str("abcdef"))
|
|
|
|
.FromJust());
|
2014-07-23 07:16:32 +00:00
|
|
|
}
|
2015-12-07 05:36:41 +00:00
|
|
|
CHECK(toplevel_test_code_event_found);
|
2014-07-24 12:11:30 +00:00
|
|
|
isolate2->Dispose();
|
2014-07-23 07:16:32 +00:00
|
|
|
}
|
2014-10-17 09:54:48 +00:00
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CodeSerializerFlagChange) {
|
2015-02-06 15:20:40 +00:00
|
|
|
FLAG_serialize_toplevel = true;
|
|
|
|
|
|
|
|
const char* source = "function f() { return 'abc'; }; f() + 'def'";
|
2015-02-09 14:49:35 +00:00
|
|
|
v8::ScriptCompiler::CachedData* cache = ProduceCache(source);
|
2015-02-06 15:20:40 +00:00
|
|
|
|
2015-04-29 09:54:34 +00:00
|
|
|
v8::Isolate::CreateParams create_params;
|
|
|
|
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
v8::Isolate* isolate2 = v8::Isolate::New(create_params);
|
2015-03-17 11:04:12 +00:00
|
|
|
|
2015-02-09 14:49:35 +00:00
|
|
|
FLAG_allow_natives_syntax = true; // Flag change should trigger cache reject.
|
2015-03-17 11:04:12 +00:00
|
|
|
FlagList::EnforceFlagImplications();
|
2015-02-06 15:20:40 +00:00
|
|
|
{
|
2015-02-09 14:49:35 +00:00
|
|
|
v8::Isolate::Scope iscope(isolate2);
|
|
|
|
v8::HandleScope scope(isolate2);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate2);
|
2015-02-06 15:20:40 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
v8::Local<v8::String> source_str = v8_str(source);
|
|
|
|
v8::ScriptOrigin origin(v8_str("test"));
|
2015-02-09 14:49:35 +00:00
|
|
|
v8::ScriptCompiler::Source source(source_str, origin, cache);
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::ScriptCompiler::CompileUnboundScript(
|
|
|
|
isolate2, &source, v8::ScriptCompiler::kConsumeCodeCache)
|
|
|
|
.ToLocalChecked();
|
2015-02-09 14:49:35 +00:00
|
|
|
CHECK(cache->rejected);
|
2015-02-06 15:20:40 +00:00
|
|
|
}
|
2015-02-09 14:49:35 +00:00
|
|
|
isolate2->Dispose();
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CodeSerializerBitFlip) {
|
2015-02-09 14:49:35 +00:00
|
|
|
FLAG_serialize_toplevel = true;
|
|
|
|
|
|
|
|
const char* source = "function f() { return 'abc'; }; f() + 'def'";
|
|
|
|
v8::ScriptCompiler::CachedData* cache = ProduceCache(source);
|
|
|
|
|
|
|
|
// Random bit flip.
|
|
|
|
const_cast<uint8_t*>(cache->data)[337] ^= 0x40;
|
2015-02-06 15:20:40 +00:00
|
|
|
|
2015-04-29 09:54:34 +00:00
|
|
|
v8::Isolate::CreateParams create_params;
|
|
|
|
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
v8::Isolate* isolate2 = v8::Isolate::New(create_params);
|
2015-02-06 15:20:40 +00:00
|
|
|
{
|
|
|
|
v8::Isolate::Scope iscope(isolate2);
|
|
|
|
v8::HandleScope scope(isolate2);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate2);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
v8::Local<v8::String> source_str = v8_str(source);
|
|
|
|
v8::ScriptOrigin origin(v8_str("test"));
|
|
|
|
v8::ScriptCompiler::Source source(source_str, origin, cache);
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::ScriptCompiler::CompileUnboundScript(
|
|
|
|
isolate2, &source, v8::ScriptCompiler::kConsumeCodeCache)
|
|
|
|
.ToLocalChecked();
|
2015-02-06 15:20:40 +00:00
|
|
|
CHECK(cache->rejected);
|
|
|
|
}
|
|
|
|
isolate2->Dispose();
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CodeSerializerWithHarmonyScoping) {
|
2014-10-17 09:54:48 +00:00
|
|
|
FLAG_serialize_toplevel = true;
|
|
|
|
|
|
|
|
const char* source1 = "'use strict'; let x = 'X'";
|
|
|
|
const char* source2 = "'use strict'; let y = 'Y'";
|
|
|
|
const char* source3 = "'use strict'; x + y";
|
|
|
|
|
|
|
|
v8::ScriptCompiler::CachedData* cache;
|
|
|
|
|
2015-04-29 09:54:34 +00:00
|
|
|
v8::Isolate::CreateParams create_params;
|
|
|
|
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
v8::Isolate* isolate1 = v8::Isolate::New(create_params);
|
2014-10-17 09:54:48 +00:00
|
|
|
{
|
|
|
|
v8::Isolate::Scope iscope(isolate1);
|
|
|
|
v8::HandleScope scope(isolate1);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate1);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
CompileRun(source1);
|
|
|
|
CompileRun(source2);
|
|
|
|
|
|
|
|
v8::Local<v8::String> source_str = v8_str(source3);
|
|
|
|
v8::ScriptOrigin origin(v8_str("test"));
|
|
|
|
v8::ScriptCompiler::Source source(source_str, origin);
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::UnboundScript> script =
|
|
|
|
v8::ScriptCompiler::CompileUnboundScript(
|
|
|
|
isolate1, &source, v8::ScriptCompiler::kProduceCodeCache)
|
|
|
|
.ToLocalChecked();
|
2014-10-17 09:54:48 +00:00
|
|
|
const v8::ScriptCompiler::CachedData* data = source.GetCachedData();
|
2014-10-17 10:22:47 +00:00
|
|
|
CHECK(data);
|
2014-10-17 09:54:48 +00:00
|
|
|
// Persist cached data.
|
|
|
|
uint8_t* buffer = NewArray<uint8_t>(data->length);
|
|
|
|
MemCopy(buffer, data->data, data->length);
|
|
|
|
cache = new v8::ScriptCompiler::CachedData(
|
|
|
|
buffer, data->length, v8::ScriptCompiler::CachedData::BufferOwned);
|
|
|
|
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::Value> result = script->BindToCurrentContext()
|
|
|
|
->Run(isolate1->GetCurrentContext())
|
|
|
|
.ToLocalChecked();
|
|
|
|
v8::Local<v8::String> result_str =
|
|
|
|
result->ToString(isolate1->GetCurrentContext()).ToLocalChecked();
|
|
|
|
CHECK(result_str->Equals(isolate1->GetCurrentContext(), v8_str("XY"))
|
|
|
|
.FromJust());
|
2014-10-17 09:54:48 +00:00
|
|
|
}
|
|
|
|
isolate1->Dispose();
|
|
|
|
|
2015-04-29 09:54:34 +00:00
|
|
|
v8::Isolate* isolate2 = v8::Isolate::New(create_params);
|
2014-10-17 09:54:48 +00:00
|
|
|
{
|
|
|
|
v8::Isolate::Scope iscope(isolate2);
|
|
|
|
v8::HandleScope scope(isolate2);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate2);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
// Reverse order of prior running scripts.
|
|
|
|
CompileRun(source2);
|
|
|
|
CompileRun(source1);
|
|
|
|
|
|
|
|
v8::Local<v8::String> source_str = v8_str(source3);
|
|
|
|
v8::ScriptOrigin origin(v8_str("test"));
|
|
|
|
v8::ScriptCompiler::Source source(source_str, origin, cache);
|
|
|
|
v8::Local<v8::UnboundScript> script;
|
|
|
|
{
|
|
|
|
DisallowCompilation no_compile(reinterpret_cast<Isolate*>(isolate2));
|
2015-10-08 09:48:05 +00:00
|
|
|
script = v8::ScriptCompiler::CompileUnboundScript(
|
|
|
|
isolate2, &source, v8::ScriptCompiler::kConsumeCodeCache)
|
|
|
|
.ToLocalChecked();
|
2014-10-17 09:54:48 +00:00
|
|
|
}
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::Value> result = script->BindToCurrentContext()
|
|
|
|
->Run(isolate2->GetCurrentContext())
|
|
|
|
.ToLocalChecked();
|
|
|
|
v8::Local<v8::String> result_str =
|
|
|
|
result->ToString(isolate2->GetCurrentContext()).ToLocalChecked();
|
|
|
|
CHECK(result_str->Equals(isolate2->GetCurrentContext(), v8_str("XY"))
|
|
|
|
.FromJust());
|
2014-10-17 09:54:48 +00:00
|
|
|
}
|
|
|
|
isolate2->Dispose();
|
|
|
|
}
|
2015-03-05 13:46:31 +00:00
|
|
|
|
2016-03-10 09:57:47 +00:00
|
|
|
TEST(CodeSerializerInternalReference) {
|
2015-04-08 06:50:10 +00:00
|
|
|
#if V8_TARGET_ARCH_ARM || V8_TARGET_ARCH_ARM64
|
2015-03-18 13:38:32 +00:00
|
|
|
return;
|
2015-04-08 06:50:10 +00:00
|
|
|
#endif
|
2016-03-11 13:35:40 +00:00
|
|
|
// In ignition there are only relative jumps, so the following code
|
|
|
|
// would not have any internal references. This test is not relevant
|
|
|
|
// for ignition.
|
|
|
|
if (FLAG_ignition) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-05 13:46:31 +00:00
|
|
|
// Disable experimental natives that are loaded after deserialization.
|
2015-09-25 11:33:28 +00:00
|
|
|
FLAG_function_context_specialization = false;
|
2015-03-05 13:46:31 +00:00
|
|
|
FLAG_always_opt = true;
|
|
|
|
const char* flag = "--turbo-filter=foo";
|
|
|
|
FlagList::SetFlagsFromString(flag, StrLength(flag));
|
|
|
|
|
|
|
|
const char* source =
|
|
|
|
"var foo = (function(stdlib, foreign, heap) {"
|
|
|
|
" function foo(i) {"
|
|
|
|
" i = i|0;"
|
|
|
|
" var j = 0;"
|
|
|
|
" switch (i) {"
|
|
|
|
" case 0:"
|
|
|
|
" case 1: j = 1; break;"
|
|
|
|
" case 2:"
|
|
|
|
" case 3: j = 2; break;"
|
|
|
|
" case 4:"
|
2015-03-18 13:38:32 +00:00
|
|
|
" case 5: j = foo(3) + 1; break;"
|
2015-03-05 13:46:31 +00:00
|
|
|
" default: j = 0; break;"
|
|
|
|
" }"
|
2015-03-18 13:38:32 +00:00
|
|
|
" return j + 10;"
|
2015-03-05 13:46:31 +00:00
|
|
|
" }"
|
|
|
|
" return { foo: foo };"
|
|
|
|
"})(this, {}, undefined).foo;"
|
|
|
|
"foo(1);";
|
|
|
|
|
|
|
|
v8::StartupData data = v8::V8::CreateSnapshotDataBlob(source);
|
|
|
|
CHECK(data.data);
|
|
|
|
|
|
|
|
v8::Isolate::CreateParams params;
|
|
|
|
params.snapshot_blob = &data;
|
2015-04-29 09:54:34 +00:00
|
|
|
params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
2015-03-05 13:46:31 +00:00
|
|
|
v8::Isolate* isolate = v8::Isolate::New(params);
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope i_scope(isolate);
|
|
|
|
v8::HandleScope h_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
delete[] data.data; // We can dispose of the snapshot blob now.
|
|
|
|
v8::Context::Scope c_scope(context);
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::Function> foo =
|
|
|
|
v8::Local<v8::Function>::Cast(CompileRun("foo"));
|
2015-03-18 13:38:32 +00:00
|
|
|
|
|
|
|
// There are at least 6 internal references.
|
|
|
|
int mask = RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE) |
|
|
|
|
RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE_ENCODED);
|
2015-10-23 12:26:49 +00:00
|
|
|
RelocIterator it(
|
|
|
|
Handle<JSFunction>::cast(v8::Utils::OpenHandle(*foo))->code(), mask);
|
2015-03-18 13:38:32 +00:00
|
|
|
for (int i = 0; i < 6; ++i) {
|
|
|
|
CHECK(!it.done());
|
|
|
|
it.next();
|
|
|
|
}
|
|
|
|
|
2015-10-23 12:26:49 +00:00
|
|
|
CHECK(Handle<JSFunction>::cast(v8::Utils::OpenHandle(*foo))
|
|
|
|
->code()
|
|
|
|
->is_turbofanned());
|
2015-10-08 09:48:05 +00:00
|
|
|
CHECK_EQ(11, CompileRun("foo(0)")
|
|
|
|
->Int32Value(isolate->GetCurrentContext())
|
|
|
|
.FromJust());
|
|
|
|
CHECK_EQ(11, CompileRun("foo(1)")
|
|
|
|
->Int32Value(isolate->GetCurrentContext())
|
|
|
|
.FromJust());
|
|
|
|
CHECK_EQ(12, CompileRun("foo(2)")
|
|
|
|
->Int32Value(isolate->GetCurrentContext())
|
|
|
|
.FromJust());
|
|
|
|
CHECK_EQ(12, CompileRun("foo(3)")
|
|
|
|
->Int32Value(isolate->GetCurrentContext())
|
|
|
|
.FromJust());
|
|
|
|
CHECK_EQ(23, CompileRun("foo(4)")
|
|
|
|
->Int32Value(isolate->GetCurrentContext())
|
|
|
|
.FromJust());
|
|
|
|
CHECK_EQ(23, CompileRun("foo(5)")
|
|
|
|
->Int32Value(isolate->GetCurrentContext())
|
|
|
|
.FromJust());
|
|
|
|
CHECK_EQ(10, CompileRun("foo(6)")
|
|
|
|
->Int32Value(isolate->GetCurrentContext())
|
|
|
|
.FromJust());
|
2015-03-05 13:46:31 +00:00
|
|
|
}
|
|
|
|
isolate->Dispose();
|
|
|
|
}
|
2015-04-10 14:13:17 +00:00
|
|
|
|
2016-03-14 18:55:41 +00:00
|
|
|
TEST(CodeSerializerEagerCompilationAndPreAge) {
|
|
|
|
if (FLAG_ignition) return;
|
|
|
|
|
|
|
|
FLAG_lazy = true;
|
|
|
|
FLAG_serialize_toplevel = true;
|
|
|
|
FLAG_serialize_age_code = true;
|
|
|
|
FLAG_serialize_eager = true;
|
|
|
|
FLAG_min_preparse_length = 1;
|
|
|
|
|
|
|
|
static const char* source =
|
|
|
|
"function f() {"
|
|
|
|
" function g() {"
|
|
|
|
" return 1;"
|
|
|
|
" }"
|
|
|
|
" return g();"
|
|
|
|
"}"
|
|
|
|
"'abcdef';";
|
|
|
|
|
|
|
|
v8::ScriptCompiler::CachedData* cache = ProduceCache(source);
|
|
|
|
|
|
|
|
v8::Isolate::CreateParams create_params;
|
|
|
|
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
v8::Isolate* isolate2 = v8::Isolate::New(create_params);
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope iscope(isolate2);
|
|
|
|
v8::HandleScope scope(isolate2);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate2);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
v8::Local<v8::String> source_str = v8_str(source);
|
|
|
|
v8::ScriptOrigin origin(v8_str("test"));
|
|
|
|
v8::ScriptCompiler::Source source(source_str, origin, cache);
|
|
|
|
v8::Local<v8::UnboundScript> unbound =
|
|
|
|
v8::ScriptCompiler::CompileUnboundScript(
|
|
|
|
isolate2, &source, v8::ScriptCompiler::kConsumeCodeCache)
|
|
|
|
.ToLocalChecked();
|
|
|
|
|
|
|
|
CHECK(!cache->rejected);
|
|
|
|
|
|
|
|
Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate2);
|
|
|
|
HandleScope i_scope(i_isolate);
|
|
|
|
Handle<SharedFunctionInfo> toplevel = v8::Utils::OpenHandle(*unbound);
|
|
|
|
Handle<Script> script(Script::cast(toplevel->script()));
|
|
|
|
WeakFixedArray::Iterator iterator(script->shared_function_infos());
|
|
|
|
// Every function has been pre-compiled from the code cache.
|
|
|
|
int count = 0;
|
|
|
|
while (SharedFunctionInfo* shared = iterator.Next<SharedFunctionInfo>()) {
|
|
|
|
CHECK(shared->is_compiled());
|
|
|
|
CHECK_EQ(Code::kPreAgedCodeAge, shared->code()->GetAge());
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
CHECK_EQ(3, count);
|
|
|
|
}
|
|
|
|
isolate2->Dispose();
|
|
|
|
}
|
2015-04-10 14:13:17 +00:00
|
|
|
|
2015-06-24 14:26:31 +00:00
|
|
|
TEST(Regress503552) {
|
|
|
|
// Test that the code serializer can deal with weak cells that form a linked
|
|
|
|
// list during incremental marking.
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
Handle<String> source = isolate->factory()->NewStringFromAsciiChecked(
|
|
|
|
"function f() {} function g() {}");
|
|
|
|
ScriptData* script_data = NULL;
|
2016-03-08 12:07:27 +00:00
|
|
|
Handle<SharedFunctionInfo> shared = Compiler::GetSharedFunctionInfoForScript(
|
2015-06-24 14:26:31 +00:00
|
|
|
source, Handle<String>(), 0, 0, v8::ScriptOriginOptions(),
|
|
|
|
Handle<Object>(), Handle<Context>(isolate->native_context()), NULL,
|
|
|
|
&script_data, v8::ScriptCompiler::kProduceCodeCache, NOT_NATIVES_CODE,
|
|
|
|
false);
|
|
|
|
delete script_data;
|
|
|
|
|
2016-05-20 13:30:22 +00:00
|
|
|
heap::SimulateIncrementalMarking(isolate->heap());
|
2015-06-24 14:26:31 +00:00
|
|
|
|
|
|
|
script_data = CodeSerializer::Serialize(isolate, shared, source);
|
|
|
|
delete script_data;
|
|
|
|
}
|
|
|
|
|
2016-05-19 13:09:06 +00:00
|
|
|
#if V8_TARGET_ARCH_X64
|
|
|
|
TEST(CodeSerializerCell) {
|
|
|
|
FLAG_serialize_toplevel = true;
|
|
|
|
LocalContext context;
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
|
|
|
|
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
|
|
|
size_t actual_size;
|
|
|
|
byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
|
|
|
|
Assembler::kMinimalBufferSize, &actual_size, true));
|
|
|
|
CHECK(buffer);
|
|
|
|
HandleScope handles(isolate);
|
|
|
|
|
|
|
|
MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
|
|
|
|
v8::internal::CodeObjectRequired::kYes);
|
|
|
|
assembler.enable_serializer();
|
|
|
|
Handle<HeapNumber> number = isolate->factory()->NewHeapNumber(0.3);
|
|
|
|
CHECK(isolate->heap()->InNewSpace(*number));
|
2016-06-28 12:34:24 +00:00
|
|
|
Handle<Code> code;
|
|
|
|
{
|
|
|
|
MacroAssembler* masm = &assembler;
|
|
|
|
Handle<Cell> cell = isolate->factory()->NewCell(number);
|
|
|
|
masm->Move(rax, cell, RelocInfo::CELL);
|
|
|
|
masm->movp(rax, Operand(rax, 0));
|
|
|
|
masm->ret(0);
|
|
|
|
CodeDesc desc;
|
|
|
|
masm->GetCode(&desc);
|
|
|
|
code = isolate->factory()->NewCode(desc, Code::ComputeFlags(Code::FUNCTION),
|
|
|
|
masm->CodeObject());
|
|
|
|
code->set_has_reloc_info_for_serialization(true);
|
|
|
|
}
|
2016-05-19 13:09:06 +00:00
|
|
|
RelocIterator rit1(*code, 1 << RelocInfo::CELL);
|
|
|
|
CHECK_EQ(*number, rit1.rinfo()->target_cell()->value());
|
|
|
|
|
|
|
|
Handle<String> source = isolate->factory()->empty_string();
|
|
|
|
Handle<SharedFunctionInfo> sfi =
|
|
|
|
isolate->factory()->NewSharedFunctionInfo(source, code, false);
|
|
|
|
ScriptData* script_data = CodeSerializer::Serialize(isolate, sfi, source);
|
|
|
|
|
|
|
|
Handle<SharedFunctionInfo> copy =
|
|
|
|
CodeSerializer::Deserialize(isolate, script_data, source)
|
|
|
|
.ToHandleChecked();
|
|
|
|
RelocIterator rit2(copy->code(), 1 << RelocInfo::CELL);
|
|
|
|
CHECK(rit2.rinfo()->target_cell()->IsCell());
|
|
|
|
Handle<Cell> cell(rit2.rinfo()->target_cell());
|
|
|
|
CHECK(cell->value()->IsHeapNumber());
|
|
|
|
CHECK_EQ(0.3, HeapNumber::cast(cell->value())->value());
|
|
|
|
|
|
|
|
delete script_data;
|
|
|
|
}
|
|
|
|
#endif // V8_TARGET_ARCH_X64
|
2015-06-24 14:26:31 +00:00
|
|
|
|
2016-08-04 09:36:33 +00:00
|
|
|
TEST(CodeSerializerEmbeddedObject) {
|
|
|
|
FLAG_serialize_toplevel = true;
|
|
|
|
LocalContext context;
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
|
|
|
size_t actual_size;
|
|
|
|
byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
|
|
|
|
Assembler::kMinimalBufferSize, &actual_size, true));
|
|
|
|
CHECK(buffer);
|
|
|
|
HandleScope handles(isolate);
|
|
|
|
|
|
|
|
MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
|
|
|
|
v8::internal::CodeObjectRequired::kYes);
|
|
|
|
assembler.enable_serializer();
|
|
|
|
Handle<Object> number = isolate->factory()->NewHeapNumber(0.3);
|
|
|
|
CHECK(isolate->heap()->InNewSpace(*number));
|
|
|
|
Handle<Code> code;
|
|
|
|
{
|
|
|
|
MacroAssembler* masm = &assembler;
|
|
|
|
masm->Push(number);
|
|
|
|
CodeDesc desc;
|
|
|
|
masm->GetCode(&desc);
|
|
|
|
code = isolate->factory()->NewCode(desc, Code::ComputeFlags(Code::FUNCTION),
|
|
|
|
masm->CodeObject());
|
|
|
|
code->set_has_reloc_info_for_serialization(true);
|
|
|
|
}
|
|
|
|
RelocIterator rit1(*code, RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT));
|
|
|
|
CHECK_EQ(*number, rit1.rinfo()->target_object());
|
|
|
|
|
|
|
|
Handle<String> source = isolate->factory()->empty_string();
|
|
|
|
Handle<SharedFunctionInfo> sfi =
|
|
|
|
isolate->factory()->NewSharedFunctionInfo(source, code, false);
|
|
|
|
ScriptData* script_data = CodeSerializer::Serialize(isolate, sfi, source);
|
|
|
|
|
|
|
|
Handle<SharedFunctionInfo> copy =
|
|
|
|
CodeSerializer::Deserialize(isolate, script_data, source)
|
|
|
|
.ToHandleChecked();
|
|
|
|
RelocIterator rit2(copy->code(),
|
|
|
|
RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT));
|
|
|
|
CHECK(rit2.rinfo()->target_object()->IsHeapNumber());
|
|
|
|
CHECK_EQ(0.3, HeapNumber::cast(rit2.rinfo()->target_object())->value());
|
|
|
|
|
|
|
|
heap->CollectAllAvailableGarbage();
|
|
|
|
|
|
|
|
RelocIterator rit3(copy->code(),
|
|
|
|
RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT));
|
|
|
|
CHECK(rit3.rinfo()->target_object()->IsHeapNumber());
|
|
|
|
CHECK_EQ(0.3, HeapNumber::cast(rit3.rinfo()->target_object())->value());
|
|
|
|
|
|
|
|
delete script_data;
|
|
|
|
}
|
|
|
|
|
2016-06-15 15:38:40 +00:00
|
|
|
TEST(SnapshotCreatorMultipleContexts) {
|
|
|
|
DisableTurbofan();
|
|
|
|
v8::StartupData blob;
|
|
|
|
{
|
|
|
|
v8::SnapshotCreator creator;
|
|
|
|
v8::Isolate* isolate = creator.GetIsolate();
|
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
CompileRun("var f = function() { return 1; }");
|
|
|
|
CHECK_EQ(0, creator.AddContext(context));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
CompileRun("var f = function() { return 2; }");
|
|
|
|
CHECK_EQ(1, creator.AddContext(context));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
CHECK_EQ(2, creator.AddContext(context));
|
|
|
|
}
|
|
|
|
blob =
|
|
|
|
creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear);
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Isolate::CreateParams params;
|
|
|
|
params.snapshot_blob = &blob;
|
|
|
|
params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
v8::Isolate* isolate = v8::Isolate::New(params);
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context =
|
2016-06-28 13:47:35 +00:00
|
|
|
v8::Context::FromSnapshot(isolate, 0).ToLocalChecked();
|
2016-06-15 15:38:40 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
ExpectInt32("f()", 1);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context =
|
2016-06-28 13:47:35 +00:00
|
|
|
v8::Context::FromSnapshot(isolate, 1).ToLocalChecked();
|
2016-06-15 15:38:40 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
ExpectInt32("f()", 2);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context =
|
2016-06-28 13:47:35 +00:00
|
|
|
v8::Context::FromSnapshot(isolate, 2).ToLocalChecked();
|
2016-06-15 15:38:40 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
ExpectUndefined("this.f");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isolate->Dispose();
|
|
|
|
delete[] blob.data;
|
|
|
|
}
|
|
|
|
|
2016-06-20 15:28:06 +00:00
|
|
|
static void SerializedCallback(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
args.GetReturnValue().Set(v8_num(42));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SerializedCallbackReplacement(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
args.GetReturnValue().Set(v8_num(1337));
|
|
|
|
}
|
|
|
|
|
|
|
|
intptr_t original_external_references[] = {
|
|
|
|
reinterpret_cast<intptr_t>(SerializedCallback), 0};
|
|
|
|
|
|
|
|
intptr_t replaced_external_references[] = {
|
|
|
|
reinterpret_cast<intptr_t>(SerializedCallbackReplacement), 0};
|
|
|
|
|
|
|
|
TEST(SnapshotCreatorExternalReferences) {
|
|
|
|
DisableTurbofan();
|
|
|
|
v8::StartupData blob;
|
|
|
|
{
|
|
|
|
v8::SnapshotCreator creator(original_external_references);
|
|
|
|
v8::Isolate* isolate = creator.GetIsolate();
|
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
v8::Local<v8::FunctionTemplate> callback =
|
|
|
|
v8::FunctionTemplate::New(isolate, SerializedCallback);
|
|
|
|
v8::Local<v8::Value> function =
|
|
|
|
callback->GetFunction(context).ToLocalChecked();
|
|
|
|
CHECK(context->Global()->Set(context, v8_str("f"), function).FromJust());
|
|
|
|
ExpectInt32("f()", 42);
|
|
|
|
CHECK_EQ(0, creator.AddContext(context));
|
|
|
|
}
|
|
|
|
blob =
|
|
|
|
creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize with the original external reference.
|
|
|
|
{
|
|
|
|
v8::Isolate::CreateParams params;
|
|
|
|
params.snapshot_blob = &blob;
|
|
|
|
params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
params.external_references = original_external_references;
|
|
|
|
v8::Isolate* isolate = v8::Isolate::New(params);
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context =
|
2016-06-28 13:47:35 +00:00
|
|
|
v8::Context::FromSnapshot(isolate, 0).ToLocalChecked();
|
2016-06-20 15:28:06 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
ExpectInt32("f()", 42);
|
|
|
|
}
|
|
|
|
isolate->Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize with the some other external reference.
|
|
|
|
{
|
|
|
|
v8::Isolate::CreateParams params;
|
|
|
|
params.snapshot_blob = &blob;
|
|
|
|
params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
params.external_references = replaced_external_references;
|
|
|
|
v8::Isolate* isolate = v8::Isolate::New(params);
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context =
|
2016-06-28 13:47:35 +00:00
|
|
|
v8::Context::FromSnapshot(isolate, 0).ToLocalChecked();
|
2016-06-20 15:28:06 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
ExpectInt32("f()", 1337);
|
|
|
|
}
|
|
|
|
isolate->Dispose();
|
|
|
|
}
|
|
|
|
delete[] blob.data;
|
|
|
|
}
|
|
|
|
|
2016-06-21 05:08:38 +00:00
|
|
|
TEST(SnapshotCreatorTemplates) {
|
2016-06-20 15:28:06 +00:00
|
|
|
DisableTurbofan();
|
|
|
|
v8::StartupData blob;
|
|
|
|
{
|
|
|
|
v8::SnapshotCreator creator(original_external_references);
|
|
|
|
v8::Isolate* isolate = creator.GetIsolate();
|
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::ExtensionConfiguration* no_extension = nullptr;
|
|
|
|
v8::Local<v8::ObjectTemplate> global_template =
|
|
|
|
v8::ObjectTemplate::New(isolate);
|
2016-06-21 05:08:38 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> callback =
|
|
|
|
v8::FunctionTemplate::New(isolate, SerializedCallback);
|
|
|
|
global_template->Set(v8_str("f"), callback);
|
2016-06-20 15:28:06 +00:00
|
|
|
v8::Local<v8::Context> context =
|
|
|
|
v8::Context::New(isolate, no_extension, global_template);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
ExpectInt32("f()", 42);
|
|
|
|
CHECK_EQ(0, creator.AddContext(context));
|
2016-06-21 05:08:38 +00:00
|
|
|
CHECK_EQ(0, creator.AddTemplate(callback));
|
|
|
|
CHECK_EQ(1, creator.AddTemplate(global_template));
|
2016-06-20 15:28:06 +00:00
|
|
|
}
|
|
|
|
blob =
|
|
|
|
creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
v8::Isolate::CreateParams params;
|
|
|
|
params.snapshot_blob = &blob;
|
|
|
|
params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
params.external_references = original_external_references;
|
|
|
|
v8::Isolate* isolate = v8::Isolate::New(params);
|
|
|
|
{
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
{
|
|
|
|
// Create a new context without a new object template.
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context =
|
2016-06-28 13:47:35 +00:00
|
|
|
v8::Context::FromSnapshot(isolate, 0).ToLocalChecked();
|
2016-06-20 15:28:06 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
ExpectInt32("f()", 42);
|
2016-06-21 05:08:38 +00:00
|
|
|
|
|
|
|
// Retrieve the snapshotted object template.
|
|
|
|
v8::Local<v8::ObjectTemplate> obj_template =
|
2016-06-28 13:47:35 +00:00
|
|
|
v8::ObjectTemplate::FromSnapshot(isolate, 1).ToLocalChecked();
|
2016-06-21 05:08:38 +00:00
|
|
|
CHECK(!obj_template.IsEmpty());
|
|
|
|
v8::Local<v8::Object> object =
|
|
|
|
obj_template->NewInstance(context).ToLocalChecked();
|
|
|
|
CHECK(context->Global()->Set(context, v8_str("o"), object).FromJust());
|
|
|
|
ExpectInt32("o.f()", 42);
|
|
|
|
// Check that it instantiates to the same prototype.
|
|
|
|
ExpectTrue("o.f.prototype === f.prototype");
|
|
|
|
|
|
|
|
// Retrieve the snapshotted function template.
|
|
|
|
v8::Local<v8::FunctionTemplate> fun_template =
|
2016-06-28 13:47:35 +00:00
|
|
|
v8::FunctionTemplate::FromSnapshot(isolate, 0).ToLocalChecked();
|
2016-06-21 05:08:38 +00:00
|
|
|
CHECK(!fun_template.IsEmpty());
|
|
|
|
v8::Local<v8::Function> fun =
|
|
|
|
fun_template->GetFunction(context).ToLocalChecked();
|
|
|
|
CHECK(context->Global()->Set(context, v8_str("g"), fun).FromJust());
|
|
|
|
ExpectInt32("g()", 42);
|
|
|
|
// Check that it instantiates to the same prototype.
|
|
|
|
ExpectTrue("g.prototype === f.prototype");
|
2016-06-28 13:47:35 +00:00
|
|
|
|
|
|
|
// Accessing out of bound returns empty MaybeHandle.
|
|
|
|
CHECK(v8::ObjectTemplate::FromSnapshot(isolate, 2).IsEmpty());
|
|
|
|
CHECK(v8::FunctionTemplate::FromSnapshot(isolate, 2).IsEmpty());
|
|
|
|
CHECK(v8::Context::FromSnapshot(isolate, 2).IsEmpty());
|
2016-06-20 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Create a context with a new object template. It is merged into the
|
|
|
|
// deserialized global object.
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::ExtensionConfiguration* no_extension = nullptr;
|
|
|
|
v8::Local<v8::ObjectTemplate> global_template =
|
|
|
|
v8::ObjectTemplate::New(isolate);
|
|
|
|
global_template->Set(
|
|
|
|
v8_str("g"),
|
|
|
|
v8::FunctionTemplate::New(isolate, SerializedCallbackReplacement));
|
2016-06-28 13:47:35 +00:00
|
|
|
v8::Local<v8::Context> context =
|
|
|
|
v8::Context::FromSnapshot(isolate, 0, no_extension, global_template)
|
|
|
|
.ToLocalChecked();
|
2016-06-20 15:28:06 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
ExpectInt32("g()", 1337);
|
|
|
|
ExpectInt32("f()", 42);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
isolate->Dispose();
|
|
|
|
}
|
|
|
|
delete[] blob.data;
|
|
|
|
}
|
|
|
|
|
2015-04-10 14:13:17 +00:00
|
|
|
TEST(SerializationMemoryStats) {
|
|
|
|
FLAG_profile_deserialization = true;
|
|
|
|
FLAG_always_opt = false;
|
|
|
|
v8::StartupData blob = v8::V8::CreateSnapshotDataBlob();
|
|
|
|
delete[] blob.data;
|
|
|
|
}
|