GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_native_module_env.cc Lines: 80 82 97.6 %
Date: 2019-10-05 22:32:21 Branches: 9 12 75.0 %

Line Branch Exec Source
1
#include "node_native_module_env.h"
2
#include "env-inl.h"
3
4
namespace node {
5
namespace native_module {
6
7
using v8::Context;
8
using v8::DEFAULT;
9
using v8::Function;
10
using v8::FunctionCallbackInfo;
11
using v8::IntegrityLevel;
12
using v8::Isolate;
13
using v8::Local;
14
using v8::Maybe;
15
using v8::MaybeLocal;
16
using v8::Name;
17
using v8::None;
18
using v8::Object;
19
using v8::PropertyCallbackInfo;
20
using v8::Set;
21
using v8::SideEffectType;
22
using v8::String;
23
using v8::Value;
24
25
// TODO(joyeecheung): make these more general and put them into util.h
26
4
Local<Set> ToJsSet(Local<Context> context, const std::set<std::string>& in) {
27
4
  Isolate* isolate = context->GetIsolate();
28
4
  Local<Set> out = Set::New(isolate);
29
382
  for (auto const& x : in) {
30
1134
    out->Add(context, OneByteString(isolate, x.c_str(), x.size()))
31
756
        .ToLocalChecked();
32
  }
33
4
  return out;
34
}
35
36
4889
bool NativeModuleEnv::Exists(const char* id) {
37
4889
  return NativeModuleLoader::GetInstance()->Exists(id);
38
}
39
40
277
Local<Object> NativeModuleEnv::GetSourceObject(Local<Context> context) {
41
277
  return NativeModuleLoader::GetInstance()->GetSourceObject(context);
42
}
43
44
5369
Local<String> NativeModuleEnv::GetConfigString(Isolate* isolate) {
45
5369
  return NativeModuleLoader::GetInstance()->GetConfigString(isolate);
46
}
47
48
1
void NativeModuleEnv::GetModuleCategories(
49
    Local<Name> property, const PropertyCallbackInfo<Value>& info) {
50
1
  Environment* env = Environment::GetCurrent(info);
51
1
  Isolate* isolate = env->isolate();
52
1
  Local<Context> context = env->context();
53
1
  Local<Object> result = Object::New(isolate);
54
55
  // Copy from the per-process categories
56
  std::set<std::string> cannot_be_required =
57
1
      NativeModuleLoader::GetInstance()->GetCannotBeRequired();
58
  std::set<std::string> can_be_required =
59
2
      NativeModuleLoader::GetInstance()->GetCanBeRequired();
60
61
1
  if (!env->owns_process_state()) {
62
    can_be_required.erase("trace_events");
63
    cannot_be_required.insert("trace_events");
64
  }
65
66
  result
67
      ->Set(context,
68
            OneByteString(isolate, "cannotBeRequired"),
69
4
            ToJsSet(context, cannot_be_required))
70
2
      .FromJust();
71
  result
72
      ->Set(context,
73
            OneByteString(isolate, "canBeRequired"),
74
4
            ToJsSet(context, can_be_required))
75
2
      .FromJust();
76
3
  info.GetReturnValue().Set(result);
77
1
}
78
79
1
void NativeModuleEnv::GetCacheUsage(const FunctionCallbackInfo<Value>& args) {
80
1
  Environment* env = Environment::GetCurrent(args);
81
1
  Isolate* isolate = env->isolate();
82
1
  Local<Context> context = env->context();
83
1
  Local<Object> result = Object::New(isolate);
84
  result
85
      ->Set(env->context(),
86
            OneByteString(isolate, "compiledWithCache"),
87
5
            ToJsSet(context, env->native_modules_with_cache))
88
2
      .FromJust();
89
  result
90
      ->Set(env->context(),
91
            OneByteString(isolate, "compiledWithoutCache"),
92
5
            ToJsSet(context, env->native_modules_without_cache))
93
2
      .FromJust();
94
2
  args.GetReturnValue().Set(result);
95
1
}
96
97
5095
void NativeModuleEnv::ModuleIdsGetter(Local<Name> property,
98
                                      const PropertyCallbackInfo<Value>& info) {
99
5095
  Isolate* isolate = info.GetIsolate();
100
101
  std::vector<std::string> ids =
102
5095
      NativeModuleLoader::GetInstance()->GetModuleIds();
103
  info.GetReturnValue().Set(
104
15285
      ToV8Value(isolate->GetCurrentContext(), ids).ToLocalChecked());
105
5095
}
106
107
5092
void NativeModuleEnv::ConfigStringGetter(
108
    Local<Name> property, const PropertyCallbackInfo<Value>& info) {
109
15276
  info.GetReturnValue().Set(GetConfigString(info.GetIsolate()));
110
5092
}
111
112
408097
void NativeModuleEnv::RecordResult(const char* id,
113
                                   NativeModuleLoader::Result result,
114
                                   Environment* env) {
115
408097
  if (result == NativeModuleLoader::Result::kWithCache) {
116
379326
    env->native_modules_with_cache.insert(id);
117
  } else {
118
28771
    env->native_modules_without_cache.insert(id);
119
  }
120
408097
}
121
392815
void NativeModuleEnv::CompileFunction(const FunctionCallbackInfo<Value>& args) {
122
392815
  Environment* env = Environment::GetCurrent(args);
123
1178445
  CHECK(args[0]->IsString());
124
785630
  node::Utf8Value id_v(env->isolate(), args[0].As<String>());
125
392814
  const char* id = *id_v;
126
  NativeModuleLoader::Result result;
127
  MaybeLocal<Function> maybe =
128
      NativeModuleLoader::GetInstance()->CompileAsModule(
129
392814
          env->context(), id, &result);
130
392815
  RecordResult(id, result, env);
131
392815
  if (!maybe.IsEmpty()) {
132
785630
    args.GetReturnValue().Set(maybe.ToLocalChecked());
133
392815
  }
134
392815
}
135
136
// Returns Local<Function> of the compiled module if return_code_cache
137
// is false (we are only compiling the function).
138
// Otherwise return a Local<Object> containing the cache.
139
16705
MaybeLocal<Function> NativeModuleEnv::LookupAndCompile(
140
    Local<Context> context,
141
    const char* id,
142
    std::vector<Local<String>>* parameters,
143
    Environment* optional_env) {
144
  NativeModuleLoader::Result result;
145
  MaybeLocal<Function> maybe =
146
      NativeModuleLoader::GetInstance()->LookupAndCompile(
147
16705
          context, id, parameters, &result);
148
16706
  if (optional_env != nullptr) {
149
15282
    RecordResult(id, result, optional_env);
150
  }
151
16706
  return maybe;
152
}
153
154
// TODO(joyeecheung): It is somewhat confusing that Class::Initialize
155
// is used to initialize to the binding, but it is the current convention.
156
// Rename this across the code base to something that makes more sense.
157
5095
void NativeModuleEnv::Initialize(Local<Object> target,
158
                                 Local<Value> unused,
159
                                 Local<Context> context,
160
                                 void* priv) {
161
5095
  Environment* env = Environment::GetCurrent(context);
162
163
  target
164
      ->SetAccessor(env->context(),
165
                    env->config_string(),
166
                    ConfigStringGetter,
167
                    nullptr,
168
                    MaybeLocal<Value>(),
169
                    DEFAULT,
170
                    None,
171
20380
                    SideEffectType::kHasNoSideEffect)
172
10190
      .Check();
173
  target
174
      ->SetAccessor(env->context(),
175
                    FIXED_ONE_BYTE_STRING(env->isolate(), "moduleIds"),
176
                    ModuleIdsGetter,
177
                    nullptr,
178
                    MaybeLocal<Value>(),
179
                    DEFAULT,
180
                    None,
181
20380
                    SideEffectType::kHasNoSideEffect)
182
10190
      .Check();
183
184
  target
185
      ->SetAccessor(env->context(),
186
                    FIXED_ONE_BYTE_STRING(env->isolate(), "moduleCategories"),
187
                    GetModuleCategories,
188
                    nullptr,
189
                    env->as_callback_data(),
190
                    DEFAULT,
191
                    None,
192
25475
                    SideEffectType::kHasNoSideEffect)
193
10190
      .Check();
194
195
5095
  env->SetMethod(target, "getCacheUsage", NativeModuleEnv::GetCacheUsage);
196
5095
  env->SetMethod(target, "compileFunction", NativeModuleEnv::CompileFunction);
197
  // internalBinding('native_module') should be frozen
198
10190
  target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
199
5095
}
200
201
}  // namespace native_module
202
}  // namespace node
203
204
4943
NODE_MODULE_CONTEXT_AWARE_INTERNAL(
205
    native_module, node::native_module::NativeModuleEnv::Initialize)