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