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: 54 104 51.9 %
Date: 2019-02-01 22:03:38 Branches: 15 49 30.6 %

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
static bool AllowWasmCodeGenerationCallback(Local<Context> context,
26
                                            Local<String>) {
27
  Local<Value> wasm_code_gen =
28
      context->GetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration);
29
  return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue();
30
}
31
32
static bool ShouldAbortOnUncaughtException(Isolate* isolate) {
33
  HandleScope scope(isolate);
34
  Environment* env = Environment::GetCurrent(isolate);
35
  return env != nullptr && env->should_abort_on_uncaught_toggle()[0] &&
36
         !env->inside_should_not_abort_on_uncaught_scope();
37
}
38
39
4
static void OnMessage(Local<Message> message, Local<Value> error) {
40
4
  Isolate* isolate = message->GetIsolate();
41
4
  switch (message->ErrorLevel()) {
42
    case Isolate::MessageErrorLevel::kMessageWarning: {
43
      Environment* env = Environment::GetCurrent(isolate);
44
      if (!env) {
45
        break;
46
      }
47
      Utf8Value filename(isolate, message->GetScriptOrigin().ResourceName());
48
      // (filename):(line) (message)
49
      std::stringstream warning;
50
      warning << *filename;
51
      warning << ":";
52
      warning << message->GetLineNumber(env->context()).FromMaybe(-1);
53
      warning << " ";
54
      v8::String::Utf8Value msg(isolate, message->Get());
55
      warning << *msg;
56
      USE(ProcessEmitWarningGeneric(env, warning.str().c_str(), "V8"));
57
      break;
58
    }
59
    case Isolate::MessageErrorLevel::kMessageError:
60
4
      FatalException(isolate, error, message);
61
4
      break;
62
  }
63
4
}
64
65
11675
void* ArrayBufferAllocator::Allocate(size_t size) {
66

11675
  if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers)
67
1863
    return UncheckedCalloc(size);
68
  else
69
9812
    return UncheckedMalloc(size);
70
}
71
72
165
ArrayBufferAllocator* CreateArrayBufferAllocator() {
73
165
  return new ArrayBufferAllocator();
74
}
75
76
154
void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator) {
77
154
  delete allocator;
78
154
}
79
80
165
Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
81
165
  Isolate::CreateParams params;
82
165
  params.array_buffer_allocator = allocator;
83
84
165
  double total_memory = uv_get_total_memory();
85
165
  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
165
    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
165
  Isolate* isolate = Isolate::Allocate();
97
165
  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
165
  per_process::v8_platform.Platform()->RegisterIsolate(isolate, event_loop);
102
165
  Isolate::Initialize(isolate, params);
103
104
  isolate->AddMessageListenerWithErrorLevel(
105
      OnMessage,
106
      Isolate::MessageErrorLevel::kMessageError |
107
165
          Isolate::MessageErrorLevel::kMessageWarning);
108
165
  isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException);
109
165
  isolate->SetMicrotasksPolicy(MicrotasksPolicy::kExplicit);
110
165
  isolate->SetFatalErrorHandler(OnFatalError);
111
165
  isolate->SetAllowWasmCodeGenerationCallback(AllowWasmCodeGenerationCallback);
112
165
  v8::CpuProfiler::UseDetailedSourcePositionsForProfiling(isolate);
113
114
165
  return isolate;
115
}
116
117
165
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
165
      allocator != nullptr ? allocator->zero_fill_field() : nullptr);
126
}
127
128
154
void FreeIsolateData(IsolateData* isolate_data) {
129
154
  delete isolate_data;
130
154
}
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 =
147
      new Environment(isolate_data, context, Environment::kIsMainThread);
148
  env->Start(per_process::v8_is_profiling);
149
  env->ProcessCliArgs(args, exec_args);
150
  return env;
151
}
152
153
void FreeEnvironment(Environment* env) {
154
  env->RunCleanup();
155
  delete env;
156
}
157
158
Environment* GetCurrentEnvironment(Local<Context> context) {
159
  return Environment::GetCurrent(context);
160
}
161
162
MultiIsolatePlatform* GetMainThreadMultiIsolatePlatform() {
163
  return per_process::v8_platform.Platform();
164
}
165
166
MultiIsolatePlatform* CreatePlatform(
167
    int thread_pool_size,
168
    node::tracing::TracingController* tracing_controller) {
169
  return new NodePlatform(thread_pool_size, tracing_controller);
170
}
171
172
165
MultiIsolatePlatform* InitializeV8Platform(int thread_pool_size) {
173
165
  per_process::v8_platform.Initialize(thread_pool_size);
174
165
  return per_process::v8_platform.Platform();
175
}
176
177
void FreePlatform(MultiIsolatePlatform* platform) {
178
  delete platform;
179
}
180
181
167
Local<Context> NewContext(Isolate* isolate,
182
                          Local<ObjectTemplate> object_template) {
183
334
  auto context = Context::New(isolate, nullptr, object_template);
184
167
  if (context.IsEmpty()) return context;
185
167
  HandleScope handle_scope(isolate);
186
187
  context->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration,
188
167
                           True(isolate));
189
190
  {
191
    // Run lib/internal/per_context.js
192
    Context::Scope context_scope(context);
193
194
    std::vector<Local<String>> parameters = {
195
334
        FIXED_ONE_BYTE_STRING(isolate, "global")};
196
501
    Local<Value> arguments[] = {context->Global()};
197
    MaybeLocal<Function> maybe_fn =
198
        per_process::native_module_loader.LookupAndCompile(
199
167
            context, "internal/per_context", &parameters, nullptr);
200
167
    if (maybe_fn.IsEmpty()) {
201
      return Local<Context>();
202
    }
203
167
    Local<Function> fn = maybe_fn.ToLocalChecked();
204
    MaybeLocal<Value> result =
205
334
        fn->Call(context, Undefined(isolate), arraysize(arguments), arguments);
206
    // Execution failed during context creation.
207
    // TODO(joyeecheung): deprecate this signature and return a MaybeLocal.
208
167
    if (result.IsEmpty()) {
209
      return Local<Context>();
210
167
    }
211
  }
212
213
167
  return context;
214
}
215
216
uv_loop_t* GetCurrentEventLoop(Isolate* isolate) {
217
  HandleScope handle_scope(isolate);
218
  Local<Context> context = isolate->GetCurrentContext();
219
  if (context.IsEmpty()) return nullptr;
220
  Environment* env = Environment::GetCurrent(context);
221
  if (env == nullptr) return nullptr;
222
  return env->event_loop();
223
}
224
225
}  // namespace node