Skip to content

Commit

Permalink
sandbox, refactor: add virtual custom resolve function.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Jun 23, 2018
1 parent 637a9ef commit 172c4ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
5 changes: 4 additions & 1 deletion fibjs/include/SandBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
40 changes: 23 additions & 17 deletions fibjs/src/sandbox/SandBox_resolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 172c4ea

Please sign in to comment.