This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathNodeTools.cpp
58 lines (43 loc) · 1.61 KB
/
NodeTools.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "NodeTools.h"
template<>
std::vector<std::string> FromJsValue<std::vector<std::string> >(
const v8::Local<v8::Value>& value)
{
using namespace v8;
Isolate* isolate = Isolate::GetCurrent();
Local<Context> context = isolate->GetCurrentContext();
std::vector<std::string> result;
if(value->IsArray()) {
Local<Array> jsArray = Local<Array>::Cast(value);
for(unsigned i = 0 ; i < jsArray->Length(); ++i) {
String::Utf8Value item(isolate, jsArray->Get(context, i).ToLocalChecked());
if(item.length())
result.emplace(result.end(), *item);
}
}
return std::move(result);
}
static v8::Local<v8::Function> RequireFunc(const v8::Local<v8::Object>& thisModule)
{
using namespace v8;
Isolate* isolate = Isolate::GetCurrent();
Local<Context> context = isolate->GetCurrentContext();
Local<String > name =
String::NewFromUtf8(
isolate,
"require",
NewStringType::kInternalized).ToLocalChecked();
return Local<Function>::Cast(thisModule->Get(context, name).ToLocalChecked());
}
v8::Local<v8::Object> Require(
const v8::Local<v8::Object>& thisModule,
const char* module)
{
using namespace v8;
Isolate* isolate = Isolate::GetCurrent();
Local<Context> context = isolate->GetCurrentContext();
Local<Object> global = context->Global();
Local<Value> argv[] =
{ String::NewFromUtf8(isolate, module, NewStringType::kInternalized).ToLocalChecked() };
return Local<Object>::Cast(RequireFunc(thisModule)->Call(context, global, 1, argv).ToLocalChecked());
}