Permalink
Browse files

sandbox, refactor: add virtual custom resolve function.

  • Loading branch information...
xicilion committed Jun 23, 2018
1 parent 637a9ef commit 172c4ead9d5c5ce5d66ebb0072b938039c0b9bc7
Showing with 27 additions and 18 deletions.
  1. +4 −1 fibjs/include/SandBox.h
  2. +23 −17 fibjs/src/sandbox/SandBox_resolve.cpp
View
@@ -136,6 +136,9 @@ class SandBox : public SandBox_base {
exlib::string m_ext;
};
public:
virtual result_t custom_resolveId(exlib::string& id, v8::Local<v8::Value>& retVal);
public:
void initRoot();
void initRequire(v8::Local<v8::Function> func)
@@ -156,7 +159,7 @@ class SandBox : public SandBox_base {
result_t resolveFile(exlib::string& fname, obj_ptr<Buffer_base>& data,
v8::Local<v8::Value>* retVal);
result_t resolveId(exlib::string& id, obj_ptr<Buffer_base>& data, v8::Local<v8::Value>& retVal);
result_t resolveId(exlib::string& id, v8::Local<v8::Value>& retVal);
result_t resolveModule(exlib::string base, exlib::string& id, obj_ptr<Buffer_base>& data,
v8::Local<v8::Value>& retVal);
result_t resolve(exlib::string base, exlib::string& id, obj_ptr<Buffer_base>& data,
@@ -186,8 +186,27 @@ result_t SandBox::resolveFile(exlib::string& fname, obj_ptr<Buffer_base>& data,
return CALL_E_FILE_NOT_FOUND;
}
result_t SandBox::resolveId(exlib::string& id, obj_ptr<Buffer_base>& data,
v8::Local<v8::Value>& retVal)
result_t SandBox::custom_resolveId(exlib::string& id, v8::Local<v8::Value>& retVal)
{
Isolate* isolate = holder();
v8::Local<v8::Value> func = GetPrivate("require");
if (!func->IsUndefined()) {
v8::Local<v8::Value> arg = isolate->NewString(id);
retVal = v8::Local<v8::Function>::Cast(func)->Call(wrap(), 1, &arg);
if (retVal.IsEmpty())
return CALL_E_JAVASCRIPT;
if (!IsEmpty(retVal)) {
InstallModule(id, retVal);
return 0;
}
}
return CALL_E_FILE_NOT_FOUND;
}
result_t SandBox::resolveId(exlib::string& id, v8::Local<v8::Value>& retVal)
{
Isolate* isolate = holder();
v8::Local<v8::Object> _mods = mods();
@@ -205,20 +224,7 @@ result_t SandBox::resolveId(exlib::string& id, obj_ptr<Buffer_base>& data,
return 0;
}
v8::Local<v8::Value> func = GetPrivate("require");
if (!func->IsUndefined()) {
v8::Local<v8::Value> arg = isolate->NewString(id);
retVal = v8::Local<v8::Function>::Cast(func)->Call(wrap(), 1, &arg);
if (retVal.IsEmpty())
return CALL_E_JAVASCRIPT;
if (!IsEmpty(retVal)) {
InstallModule(id, retVal);
return 0;
}
}
return CALL_E_FILE_NOT_FOUND;
return custom_resolveId(id, retVal);
}
result_t SandBox::resolveModule(exlib::string base, exlib::string& id, obj_ptr<Buffer_base>& data,
@@ -295,7 +301,7 @@ result_t SandBox::resolve(exlib::string base, exlib::string& id, obj_ptr<Buffer_
if (!isAbs) {
result_t hr;
hr = resolveId(id, data, retVal);
hr = resolveId(id, retVal);
if (hr != CALL_E_FILE_NOT_FOUND && hr != CALL_E_PATH_NOT_FOUND)
return hr;
return resolveModule(base, id, data, retVal);

0 comments on commit 172c4ea

Please sign in to comment.