// Copyright 2021 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "include/v8-context.h" #include "include/v8-function.h" #include "include/v8-isolate.h" #include "include/v8-local-handle.h" #include "include/v8-platform.h" #include "include/v8-primitive.h" #include "include/v8-script.h" #include "src/codegen/compilation-cache.h" #include "test/unittests/test-utils.h" #include "testing/gtest/include/gtest/gtest.h" namespace v8 { class DeserializeTest : public TestWithPlatform { public: class IsolateAndContextScope { public: explicit IsolateAndContextScope(DeserializeTest* test) : test_(test), isolate_wrapper_(kNoCounters), isolate_scope_(isolate_wrapper_.isolate()), handle_scope_(isolate_wrapper_.isolate()), context_(Context::New(isolate_wrapper_.isolate())), context_scope_(context_) { CHECK_NULL(test->isolate_); CHECK(test->context_.IsEmpty()); test->isolate_ = isolate_wrapper_.isolate(); test->context_ = context_; } ~IsolateAndContextScope() { test_->isolate_ = nullptr; test_->context_ = {}; } private: DeserializeTest* test_; v8::IsolateWrapper isolate_wrapper_; v8::Isolate::Scope isolate_scope_; v8::HandleScope handle_scope_; v8::Local context_; v8::Context::Scope context_scope_; }; Local NewString(const char* val) { return String::NewFromUtf8(isolate(), val).ToLocalChecked(); } Local RunGlobalFunc(const char* name) { Local func_val = context()->Global()->Get(context(), NewString(name)).ToLocalChecked(); CHECK(func_val->IsFunction()); Local func = Local::Cast(func_val); return func->Call(context(), Undefined(isolate()), 0, nullptr) .ToLocalChecked(); } Isolate* isolate() { return isolate_; } v8::Local context() { return context_.ToLocalChecked(); } private: Isolate* isolate_ = nullptr; v8::MaybeLocal context_; }; // Check that deserialization works. TEST_F(DeserializeTest, Deserialize) { std::unique_ptr cached_data; { IsolateAndContextScope scope(this); Local source_code = NewString("function foo() { return 42; }"); Local