GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/api/environment.cc Lines: 86 105 81.9 %
Date: 2019-02-13 22:28:58 Branches: 31 49 63.3 %

Line Branch Exec Source
1
#include "env.h"
2
#include "node.h"
3
#include "node_context_data.h"
4
#include "node_errors.h"
5
#include "node_internals.h"
6
#include "node_native_module.h"
7
#include "node_platform.h"
8
#include "node_process.h"
9
#include "node_v8_platform-inl.h"
10
#include "uv.h"
11
12
namespace node {
13
using v8::Context;
14
using v8::Function;
15
using v8::HandleScope;
16
using v8::Isolate;
17
using v8::Local;
18
using v8::MaybeLocal;
19
using v8::Message;
20
using v8::MicrotasksPolicy;
21
using v8::ObjectTemplate;
22
using v8::String;
23
using v8::Value;
24
25
33
static bool AllowWasmCodeGenerationCallback(Local<Context> context,
26
                                            Local<String>) {
27
  Local<Value> wasm_code_gen =
28
66
      context->GetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration);
29

99
  return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue();
30
}
31
32
31
static bool ShouldAbortOnUncaughtException(Isolate* isolate) {
33
31
  HandleScope scope(isolate);
34
31
  Environment* env = Environment::GetCurrent(isolate);
35


94
  return env != nullptr && env->should_abort_on_uncaught_toggle()[0] &&
36
63
         !env->inside_should_not_abort_on_uncaught_scope();
37
}
38
39
1315
static void OnMessage(Local<Message> message, Local<Value> error) {
40
1315
  Isolate* isolate = message->GetIsolate();
41
1315
  switch (message->ErrorLevel()) {
42
    case Isolate::MessageErrorLevel::kMessageWarning: {
43
1
      Environment* env = Environment::GetCurrent(isolate);
44
1
      if (!env) {
45
        break;
46
      }
47
2
      Utf8Value filename(isolate, message->GetScriptOrigin().ResourceName());
48
      // (filename):(line) (message)
49
2
      std::stringstream warning;
50
1
      warning << *filename;
51
1
      warning << ":";
52
3
      warning << message->GetLineNumber(env->context()).FromMaybe(-1);
53
1
      warning << " ";
54
3
      v8::String::Utf8Value msg(isolate, message->Get());
55
1
      warning << *msg;
56
1
      USE(ProcessEmitWarningGeneric(env, warning.str().c_str(), "V8"));
57
2
      break;
58
    }
59
    case Isolate::MessageErrorLevel::kMessageError:
60
1314
      FatalException(isolate, error, message);
61
1173
      break;
62
  }
63
1174
}
64
65
917886
void* ArrayBufferAllocator::Allocate(size_t size) {
66

917886
  if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers)
67
45856
    return UncheckedCalloc(size);
68
  else
69
872030
    return UncheckedMalloc(size);
70
}
71
72
4424
ArrayBufferAllocator* CreateArrayBufferAllocator() {
73
4424
  return new ArrayBufferAllocator();
74
}
75
76
4054
void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator) {
77
4054
  delete allocator;
78
4054
}
79
80
4424
Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
81
4424
  Isolate::CreateParams params;
82
4424
  params.array_buffer_allocator = allocator;
83
84
4424
  double total_memory = uv_get_total_memory();
85
4424
  if (total_memory > 0) {
86
    // V8 defaults to 700MB or 1.4GB on 32 and 64 bit platforms respectively.
87
    // This default is based on browser use-cases. Tell V8 to configure the
88
    // heap based on the actual physical memory.
89
4424
    params.constraints.ConfigureDefaults(total_memory, 0);
90
  }
91
92
#ifdef NODE_ENABLE_VTUNE_PROFILING
93
  params.code_event_handler = vTune::GetVtuneCodeEventHandler();
94
#endif
95
96
4424
  Isolate* isolate = Isolate::Allocate();
97
4424
  if (isolate == nullptr) return nullptr;
98
99
  // Register the isolate on the platform before the isolate gets initialized,
100
  // so that the isolate can access the platform during initialization.
101
4424
  per_process::v8_platform.Platform()->RegisterIsolate(isolate, event_loop);
102
4424
  Isolate::Initialize(isolate, params);
103
104
  isolate->AddMessageListenerWithErrorLevel(
105
      OnMessage,
106
      Isolate::MessageErrorLevel::kMessageError |
107
4424
          Isolate::MessageErrorLevel::kMessageWarning);
108
4424
  isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException);
109
4424
  isolate->SetMicrotasksPolicy(MicrotasksPolicy::kExplicit);
110
4424
  isolate->SetFatalErrorHandler(OnFatalError);
111
4424
  isolate->SetAllowWasmCodeGenerationCallback(AllowWasmCodeGenerationCallback);
112
4424
  v8::CpuProfiler::UseDetailedSourcePositionsForProfiling(isolate);
113
114
4424
  return isolate;
115
}
116
117
4421
IsolateData* CreateIsolateData(Isolate* isolate,
118
                               uv_loop_t* loop,
119
                               MultiIsolatePlatform* platform,
120
                               ArrayBufferAllocator* allocator) {
121
  return new IsolateData(
122
      isolate,
123
      loop,
124
      platform,
125
4421
      allocator != nullptr ? allocator->zero_fill_field() : nullptr);
126
}
127
128
4052
void FreeIsolateData(IsolateData* isolate_data) {
129
4052
  delete isolate_data;
130
4052
}
131
132
Environment* CreateEnvironment(IsolateData* isolate_data,
133
                               Local<Context> context,
134
                               int argc,
135
                               const char* const* argv,
136
                               int exec_argc,
137
                               const char* const* exec_argv) {
138
  Isolate* isolate = context->GetIsolate();
139
  HandleScope handle_scope(isolate);
140
  Context::Scope context_scope(context);
141
  // TODO(addaleax): This is a much better place for parsing per-Environment
142
  // options than the global parse call.
143
  std::vector<std::string> args(argv, argv + argc);
144
  std::vector<std::string> exec_args(exec_argv, exec_argv + exec_argc);
145
  // TODO(addaleax): Provide more sensible flags, in an embedder-accessible way.
146
  Environment* env = new Environment(
147
      isolate_data,
148
      context,
149
      static_cast<Environment::Flags>(Environment::kIsMainThread |
150
                                      Environment::kOwnsProcessState |
151
                                      Environment::kOwnsInspector));
152
  env->Start(per_process::v8_is_profiling);
153
  env->ProcessCliArgs(args, exec_args);
154
  return env;
155
}
156
157
151
void FreeEnvironment(Environment* env) {
158
151
  env->RunCleanup();
159
151
  delete env;
160
151
}
161
162
Environment* GetCurrentEnvironment(Local<Context> context) {
163
  return Environment::GetCurrent(context);
164
}
165
166
MultiIsolatePlatform* GetMainThreadMultiIsolatePlatform() {
167
  return per_process::v8_platform.Platform();
168
}
169
170
MultiIsolatePlatform* CreatePlatform(
171
    int thread_pool_size,
172
    node::tracing::TracingController* tracing_controller) {
173
  return new NodePlatform(thread_pool_size, tracing_controller);
174
}
175
176
4270
MultiIsolatePlatform* InitializeV8Platform(int thread_pool_size) {
177
4270
  per_process::v8_platform.Initialize(thread_pool_size);
178
4270
  return per_process::v8_platform.Platform();
179
}
180
181
void FreePlatform(MultiIsolatePlatform* platform) {
182
  delete platform;
183
}
184
185
8944
Local<Context> NewContext(Isolate* isolate,
186
                          Local<ObjectTemplate> object_template) {
187
17888
  auto context = Context::New(isolate, nullptr, object_template);
188
8944
  if (context.IsEmpty()) return context;
189
8944
  HandleScope handle_scope(isolate);
190
191
  context->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration,
192
8944
                           True(isolate));
193
194
  {
195
    // Run lib/internal/bootstrap/context.js
196
    Context::Scope context_scope(context);
197
198
    std::vector<Local<String>> parameters = {
199
17887
        FIXED_ONE_BYTE_STRING(isolate, "global")};
200
26832
    Local<Value> arguments[] = {context->Global()};
201
    MaybeLocal<Function> maybe_fn =
202
        per_process::native_module_loader.LookupAndCompile(
203
8944
            context, "internal/bootstrap/context", &parameters, nullptr);
204
8944
    if (maybe_fn.IsEmpty()) {
205
      return Local<Context>();
206
    }
207
8944
    Local<Function> fn = maybe_fn.ToLocalChecked();
208
    MaybeLocal<Value> result =
209
17888
        fn->Call(context, Undefined(isolate), arraysize(arguments), arguments);
210
    // Execution failed during context creation.
211
    // TODO(joyeecheung): deprecate this signature and return a MaybeLocal.
212
8944
    if (result.IsEmpty()) {
213
1
      return Local<Context>();
214
8943
    }
215
  }
216
217
8943
  return context;
218
}
219
220
8
uv_loop_t* GetCurrentEventLoop(Isolate* isolate) {
221
8
  HandleScope handle_scope(isolate);
222
8
  Local<Context> context = isolate->GetCurrentContext();
223
8
  if (context.IsEmpty()) return nullptr;
224
8
  Environment* env = Environment::GetCurrent(context);
225
8
  if (env == nullptr) return nullptr;
226
8
  return env->event_loop();
227
}
228
229
}  // namespace node