// Copyright 2014 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.
#ifndef V8_BOOTSTRAPPER_H_
#define V8_BOOTSTRAPPER_H_
#include "src/heap/factory.h"
#include "src/objects/shared-function-info.h"
#include "src/objects/slots.h"
#include "src/snapshot/natives.h"
#include "src/visitors.h"
namespace v8 {
namespace internal {
// A SourceCodeCache uses a FixedArray to store pairs of
// (OneByteString*, JSFunction*), mapping names of native code files
// (array.js, etc.) to precompiled functions. Instead of mapping
// names to functions it might make sense to let the JS2C tool
// generate an index for each native JS file.
class SourceCodeCache final {
public:
explicit SourceCodeCache(Script::Type type) : type_(type), cache_(nullptr) {}
void Initialize(Isolate* isolate, bool create_heap_objects);
void Iterate(RootVisitor* v) {
v->VisitRootPointer(Root::kExtensions, nullptr,
ObjectSlot(reinterpret_cast
(&cache_)));
}
bool Lookup(Isolate* isolate, Vector name,
Handle* handle);
void Add(Isolate* isolate, Vector name,
Handle shared);
private:
Script::Type type_;
FixedArray* cache_;
DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
};
enum GlobalContextType { FULL_CONTEXT, DEBUG_CONTEXT };
// The Boostrapper is the public interface for creating a JavaScript global
// context.
class Bootstrapper final {
public:
static void InitializeOncePerProcess();
static void TearDownExtensions();
// Requires: Heap::SetUp has been called.
void Initialize(bool create_heap_objects);
void TearDown();
// Creates a JavaScript Global Context with initial object graph.
// The returned value is a global handle casted to V8Environment*.
Handle CreateEnvironment(
MaybeHandle maybe_global_proxy,
v8::Local global_object_template,
v8::ExtensionConfiguration* extensions, size_t context_snapshot_index,
v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer,
GlobalContextType context_type = FULL_CONTEXT);
Handle NewRemoteContext(
MaybeHandle maybe_global_proxy,
v8::Local global_object_template);
// Detach the environment from its outer global object.
void DetachGlobal(Handle env);
// Traverses the pointers for memory management.
void Iterate(RootVisitor* v);
// Accessor for the native scripts source code.
Handle GetNativeSource(NativeType type, int index);
// Tells whether bootstrapping is active.
bool IsActive() const { return nesting_ != 0; }
// Support for thread preemption.
static int ArchiveSpacePerThread();
char* ArchiveState(char* to);
char* RestoreState(char* from);
void FreeThreadResources();
// Used for new context creation.
bool InstallExtensions(Handle native_context,
v8::ExtensionConfiguration* extensions);
SourceCodeCache* extensions_cache() { return &extensions_cache_; }
static bool CompileNative(Isolate* isolate, Vector name,
Handle source, int argc,
Handle