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: 81 83 97.6 %
Date: 2020-05-27 22:15:15 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
417
  for (auto const& x : in) {
30
1239
    out->Add(context, OneByteString(isolate, x.c_str(), x.size()))
31
        .ToLocalChecked();
32
  }
33
4
  return out;
34
}
35
36
9
bool NativeModuleEnv::Add(const char* id, const UnionBytes& source) {
37
9
  return NativeModuleLoader::GetInstance()->Add(id, source);
38
}
39
40
4558
bool NativeModuleEnv::Exists(const char* id) {
41
4558
  return NativeModuleLoader::GetInstance()->Exists(id);
42
}
43
44
7
Local<Object> NativeModuleEnv::GetSourceObject(Local<Context> context) {
45
7
  return NativeModuleLoader::GetInstance()->GetSourceObject(context);
46
}
47
48
4601
Local<String> NativeModuleEnv::GetConfigString(Isolate* isolate) {
49
4601
  return NativeModuleLoader::GetInstance()->GetConfigString(isolate);
50
}
51
52
1
void NativeModuleEnv::GetModuleCategories(
53
    Local<Name> property, const PropertyCallbackInfo<Value>& info) {
54
1
  Environment* env = Environment::GetCurrent(info);
55
1
  Isolate* isolate = env->isolate();
56
1
  Local<Context> context = env->context();
57
1
  Local<Object> result = Object::New(isolate);
58
59
  // Copy from the per-process categories
60
  std::set<std::string> cannot_be_required =
61
2
      NativeModuleLoader::GetInstance()->GetCannotBeRequired();
62
  std::set<std::string> can_be_required =
63
2
      NativeModuleLoader::GetInstance()->GetCanBeRequired();
64
65
1
  if (!env->owns_process_state()) {
66
    can_be_required.erase("trace_events");
67
    cannot_be_required.insert("trace_events");
68
  }
69
70
  result
71
2
      ->Set(context,
72
            OneByteString(isolate, "cannotBeRequired"),
73
4
            ToJsSet(context, cannot_be_required))
74
      .FromJust();
75
  result
76
2
      ->Set(context,
77
            OneByteString(isolate, "canBeRequired"),
78
4
            ToJsSet(context, can_be_required))
79
      .FromJust();
80
2
  info.GetReturnValue().Set(result);
81
1
}
82
83
1
void NativeModuleEnv::GetCacheUsage(const FunctionCallbackInfo<Value>& args) {
84
1
  Environment* env = Environment::GetCurrent(args);
85
1
  Isolate* isolate = env->isolate();
86
1
  Local<Context> context = env->context();
87
1
  Local<Object> result = Object::New(isolate);
88
  result
89
2
      ->Set(env->context(),
90
            OneByteString(isolate, "compiledWithCache"),
91
5
            ToJsSet(context, env->native_modules_with_cache))
92
      .FromJust();
93
  result
94
2
      ->Set(env->context(),
95
            OneByteString(isolate, "compiledWithoutCache"),
96
5
            ToJsSet(context, env->native_modules_without_cache))
97
      .FromJust();
98
2
  args.GetReturnValue().Set(result);
99
1
}
100
101
4594
void NativeModuleEnv::ModuleIdsGetter(Local<Name> property,
102
                                      const PropertyCallbackInfo<Value>& info) {
103
4594
  Isolate* isolate = info.GetIsolate();
104
105
  std::vector<std::string> ids =
106
9188
      NativeModuleLoader::GetInstance()->GetModuleIds();
107
9188
  info.GetReturnValue().Set(
108
9188
      ToV8Value(isolate->GetCurrentContext(), ids).ToLocalChecked());
109
4594
}
110
111
4594
void NativeModuleEnv::ConfigStringGetter(
112
    Local<Name> property, const PropertyCallbackInfo<Value>& info) {
113
13782
  info.GetReturnValue().Set(GetConfigString(info.GetIsolate()));
114
4594
}
115
116
456895
void NativeModuleEnv::RecordResult(const char* id,
117
                                   NativeModuleLoader::Result result,
118
                                   Environment* env) {
119
456895
  if (result == NativeModuleLoader::Result::kWithCache) {
120
417043
    env->native_modules_with_cache.insert(id);
121
  } else {
122
39852
    env->native_modules_without_cache.insert(id);
123
  }
124
456895
}
125
433938
void NativeModuleEnv::CompileFunction(const FunctionCallbackInfo<Value>& args) {
126
433938
  Environment* env = Environment::GetCurrent(args);
127
1301814
  CHECK(args[0]->IsString());
128
1301816
  node::Utf8Value id_v(env->isolate(), args[0].As<String>());
129
433940
  const char* id = *id_v;
130
  NativeModuleLoader::Result result;
131
  MaybeLocal<Function> maybe =
132
      NativeModuleLoader::GetInstance()->CompileAsModule(
133
433940
          env->context(), id, &result);
134
433940
  RecordResult(id, result, env);
135
433940
  if (!maybe.IsEmpty()) {
136
867880
    args.GetReturnValue().Set(maybe.ToLocalChecked());
137
  }
138
433940
}
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
24986
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
24986
          context, id, parameters, &result);
152
24990
  if (optional_env != nullptr) {
153
22955
    RecordResult(id, result, optional_env);
154
  }
155
24990
  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
4594
void NativeModuleEnv::Initialize(Local<Object> target,
162
                                 Local<Value> unused,
163
                                 Local<Context> context,
164
                                 void* priv) {
165
4594
  Environment* env = Environment::GetCurrent(context);
166
167
  target
168
9188
      ->SetAccessor(env->context(),
169
                    env->config_string(),
170
                    ConfigStringGetter,
171
                    nullptr,
172
                    MaybeLocal<Value>(),
173
                    DEFAULT,
174
                    None,
175
18376
                    SideEffectType::kHasNoSideEffect)
176
      .Check();
177
  target
178
9188
      ->SetAccessor(env->context(),
179
                    FIXED_ONE_BYTE_STRING(env->isolate(), "moduleIds"),
180
                    ModuleIdsGetter,
181
                    nullptr,
182
                    MaybeLocal<Value>(),
183
                    DEFAULT,
184
                    None,
185
18376
                    SideEffectType::kHasNoSideEffect)
186
      .Check();
187
188
  target
189
9188
      ->SetAccessor(env->context(),
190
                    FIXED_ONE_BYTE_STRING(env->isolate(), "moduleCategories"),
191
                    GetModuleCategories,
192
                    nullptr,
193
                    Local<Value>(),
194
                    DEFAULT,
195
                    None,
196
18376
                    SideEffectType::kHasNoSideEffect)
197
      .Check();
198
199
4594
  env->SetMethod(target, "getCacheUsage", NativeModuleEnv::GetCacheUsage);
200
4594
  env->SetMethod(target, "compileFunction", NativeModuleEnv::CompileFunction);
201
  // internalBinding('native_module') should be frozen
202
9188
  target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
203
4594
}
204
205
}  // namespace native_module
206
}  // namespace node
207
208
4325
NODE_MODULE_CONTEXT_AWARE_INTERNAL(
209
    native_module, node::native_module::NativeModuleEnv::Initialize)