bootstrapper.h 4.53 KB
Newer Older
1 2 3
// 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.
4 5 6 7

#ifndef V8_BOOTSTRAPPER_H_
#define V8_BOOTSTRAPPER_H_

8
#include "src/factory.h"
9
#include "src/objects/shared-function-info.h"
10
#include "src/snapshot/natives.h"
11
#include "src/visitors.h"
12

13 14
namespace v8 {
namespace internal {
15

16
// A SourceCodeCache uses a FixedArray to store pairs of
17
// (OneByteString*, JSFunction*), mapping names of native code files
18
// (array.js, etc.) to precompiled functions. Instead of mapping
19 20
// names to functions it might make sense to let the JS2C tool
// generate an index for each native JS file.
21
class SourceCodeCache final BASE_EMBEDDED {
22
 public:
23
  explicit SourceCodeCache(Script::Type type) : type_(type), cache_(nullptr) {}
24

25
  void Initialize(Isolate* isolate, bool create_heap_objects);
26

27 28 29
  void Iterate(RootVisitor* v) {
    v->VisitRootPointer(Root::kExtensions,
                        bit_cast<Object**, FixedArray**>(&cache_));
30 31
  }

32
  bool Lookup(Vector<const char> name, Handle<SharedFunctionInfo>* handle);
33

34
  void Add(Vector<const char> name, Handle<SharedFunctionInfo> shared);
35 36

 private:
37 38 39
  Script::Type type_;
  FixedArray* cache_;
  DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
40 41
};

42
enum GlobalContextType { FULL_CONTEXT, DEBUG_CONTEXT };
43

44 45
// The Boostrapper is the public interface for creating a JavaScript global
// context.
46
class Bootstrapper final {
47
 public:
48
  static void InitializeOncePerProcess();
49
  static void TearDownExtensions();
50

51
  // Requires: Heap::SetUp has been called.
52 53
  void Initialize(bool create_heap_objects);
  void TearDown();
54 55 56

  // Creates a JavaScript Global Context with initial object graph.
  // The returned value is a global handle casted to V8Environment*.
57
  Handle<Context> CreateEnvironment(
58
      MaybeHandle<JSGlobalProxy> maybe_global_proxy,
59
      v8::Local<v8::ObjectTemplate> global_object_template,
60
      v8::ExtensionConfiguration* extensions, size_t context_snapshot_index,
61
      v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer,
62
      GlobalContextType context_type = FULL_CONTEXT);
63

64 65 66 67
  Handle<JSGlobalProxy> NewRemoteContext(
      MaybeHandle<JSGlobalProxy> maybe_global_proxy,
      v8::Local<v8::ObjectTemplate> global_object_template);

68
  // Detach the environment from its outer global object.
69
  void DetachGlobal(Handle<Context> env);
70

71
  // Traverses the pointers for memory management.
72
  void Iterate(RootVisitor* v);
73

74
  // Accessor for the native scripts source code.
75
  Handle<String> GetNativeSource(NativeType type, int index);
76

77
  // Tells whether bootstrapping is active.
78
  bool IsActive() const { return nesting_ != 0; }
79

80
  // Support for thread preemption.
81
  static int ArchiveSpacePerThread();
82 83 84
  char* ArchiveState(char* to);
  char* RestoreState(char* from);
  void FreeThreadResources();
85

86
  // Used for new context creation.
87
  bool InstallExtensions(Handle<Context> native_context,
88 89 90 91
                         v8::ExtensionConfiguration* extensions);

  SourceCodeCache* extensions_cache() { return &extensions_cache_; }

92
  static bool CompileNative(Isolate* isolate, Vector<const char> name,
yangguo's avatar
yangguo committed
93
                            Handle<String> source, int argc,
94
                            Handle<Object> argv[], NativesFlag natives_flag);
95 96
  static bool CompileBuiltin(Isolate* isolate, int index);
  static bool CompileExtraBuiltin(Isolate* isolate, int index);
97
  static bool CompileExperimentalExtraBuiltin(Isolate* isolate, int index);
98

99
  static void ExportFromRuntime(Isolate* isolate, Handle<JSObject> container);
100

101
 private:
102
  Isolate* isolate_;
103 104 105 106 107 108 109 110
  typedef int NestingCounterType;
  NestingCounterType nesting_;
  SourceCodeCache extensions_cache_;

  friend class BootstrapperActive;
  friend class Isolate;
  friend class NativesExternalStringResource;

111
  explicit Bootstrapper(Isolate* isolate);
112

113 114 115 116 117
  static v8::Extension* free_buffer_extension_;
  static v8::Extension* gc_extension_;
  static v8::Extension* externalize_string_extension_;
  static v8::Extension* statistics_extension_;
  static v8::Extension* trigger_failure_extension_;
118
  static v8::Extension* ignition_statistics_extension_;
119

120 121 122 123
  DISALLOW_COPY_AND_ASSIGN(Bootstrapper);
};


124
class BootstrapperActive final BASE_EMBEDDED {
125
 public:
126 127 128
  explicit BootstrapperActive(Bootstrapper* bootstrapper)
      : bootstrapper_(bootstrapper) {
    ++bootstrapper_->nesting_;
129 130 131
  }

  ~BootstrapperActive() {
132
    --bootstrapper_->nesting_;
133 134 135
  }

 private:
136 137
  Bootstrapper* bootstrapper_;

138
  DISALLOW_COPY_AND_ASSIGN(BootstrapperActive);
139 140
};

141 142
}  // namespace internal
}  // namespace v8
143 144

#endif  // V8_BOOTSTRAPPER_H_