bootstrapper.h 4.49 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/heap/factory.h"
9
#include "src/objects/fixed-array.h"
10
#include "src/objects/shared-function-info.h"
11
#include "src/objects/slots.h"
12
#include "src/snapshot/natives.h"
13
#include "src/visitors.h"
14

15 16
namespace v8 {
namespace internal {
17

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

27
  void Initialize(Isolate* isolate, bool create_heap_objects);
28

29
  void Iterate(RootVisitor* v);
30

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

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

 private:
38
  Script::Type type_;
39
  FixedArray cache_;
40
  DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
41 42 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

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

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

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

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

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

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

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

  SourceCodeCache* extensions_cache() { return &extensions_cache_; }

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

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

100
 private:
101 102 103
  // Log newly created Map objects if no snapshot was used.
  void LogAllMaps();

104
  Isolate* isolate_;
105 106 107 108 109 110 111 112
  typedef int NestingCounterType;
  NestingCounterType nesting_;
  SourceCodeCache extensions_cache_;

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

113
  explicit Bootstrapper(Isolate* isolate);
114

115 116 117 118 119
  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_;
120
  static v8::Extension* ignition_statistics_extension_;
121

122 123 124
  DISALLOW_COPY_AND_ASSIGN(Bootstrapper);
};

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

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

 private:
137 138
  Bootstrapper* bootstrapper_;

139
  DISALLOW_COPY_AND_ASSIGN(BootstrapperActive);
140 141
};

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

#endif  // V8_BOOTSTRAPPER_H_