Skip to content

Commit

Permalink
add 'has' method in SandBox Module. (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
tapir-dream authored and xicilion committed Jun 27, 2018
1 parent a8ad136 commit c56f754
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions fibjs/include/SandBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SandBox : public SandBox_base {
virtual result_t add(v8::Local<v8::Object> mods);
virtual result_t addScript(exlib::string srcname, Buffer_base* script, v8::Local<v8::Value>& retVal);
virtual result_t remove(exlib::string id);
virtual result_t has(exlib::string id, bool& retVal);
virtual result_t clone(obj_ptr<SandBox_base>& retVal);
virtual result_t run(exlib::string fname, v8::Local<v8::Array> argv);
virtual result_t resolve(exlib::string id, exlib::string base, exlib::string& retVal);
Expand Down
20 changes: 20 additions & 0 deletions fibjs/include/ifs/SandBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class SandBox_base : public object_base {
virtual result_t add(v8::Local<v8::Object> mods) = 0;
virtual result_t addScript(exlib::string srcname, Buffer_base* script, v8::Local<v8::Value>& retVal) = 0;
virtual result_t remove(exlib::string id) = 0;
virtual result_t has(exlib::string id, bool& retVal) = 0;
virtual result_t clone(obj_ptr<SandBox_base>& retVal) = 0;
virtual result_t run(exlib::string fname, v8::Local<v8::Array> argv) = 0;
virtual result_t resolve(exlib::string id, exlib::string base, exlib::string& retVal) = 0;
Expand All @@ -46,6 +47,7 @@ class SandBox_base : public object_base {
static void s_add(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_addScript(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_remove(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_has(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_clone(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_run(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_resolve(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand All @@ -63,6 +65,7 @@ inline ClassInfo& SandBox_base::class_info()
{ "add", s_add, false },
{ "addScript", s_addScript, false },
{ "remove", s_remove, false },
{ "has", s_has, false },
{ "clone", s_clone, false },
{ "run", s_run, false },
{ "resolve", s_resolve, false },
Expand Down Expand Up @@ -183,6 +186,23 @@ inline void SandBox_base::s_remove(const v8::FunctionCallbackInfo<v8::Value>& ar
METHOD_VOID();
}

inline void SandBox_base::s_has(const v8::FunctionCallbackInfo<v8::Value>& args)
{
bool vr;

METHOD_NAME("SandBox.has");
METHOD_INSTANCE(SandBox_base);
METHOD_ENTER();

METHOD_OVER(1, 1);

ARG(exlib::string, 0);

hr = pInst->has(v0, vr);

METHOD_RETURN();
}

inline void SandBox_base::s_clone(const v8::FunctionCallbackInfo<v8::Value>& args)
{
obj_ptr<SandBox_base> vr;
Expand Down
11 changes: 11 additions & 0 deletions fibjs/src/sandbox/SandBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ result_t SandBox::remove(exlib::string id)
return 0;
}

result_t SandBox::has(exlib::string id, bool& retVal)
{
path_base::normalize(id, id);
retVal = mods()->Has(
Isolate::current()->context(),
holder()->NewString(id))
.ToChecked();

return 0;
}

result_t SandBox::clone(obj_ptr<SandBox_base>& retVal)
{
obj_ptr<SandBox> sbox = new SandBox();
Expand Down
6 changes: 6 additions & 0 deletions idl/us-en/SandBox.idl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ interface SandBox : object
*/
remove(String id);

/*! @brief Check a module in the sandbox exists
@param id Module name
@return bool
*/
Boolean has(String id);

/*! @brief clone current sandbox, the cloned sandbox contains all current sandbox module, same name and same require function.
@return cloned sanbox
*/
Expand Down
6 changes: 6 additions & 0 deletions idl/zh-cn/SandBox.idl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ interface SandBox : object
*/
remove(String id);

/*! @brief 从沙箱中检测基础模块是否存在
@param id 指定要检测的模块名称,此路径与当前运行脚本无关,必须为绝对路径或者模块名
@return 是否存在
*/
Boolean has(String id);

/*! @brief 复制当前沙箱,新沙箱包含当前沙箱的模块,以及相同的名称和 require 函数
@return 复制的新沙箱
*/
Expand Down
20 changes: 20 additions & 0 deletions test/vm_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ describe("vm", () => {
sbox.require('./vm_test/t1', __dirname).fun();
assert.equal(1000, b.a);
});
it("has", () => {
var b = {
a: 1000
}

sbox = new vm.SandBox({
a: 100,
assert: assert,
b: b
}, (name) => {
if (name == 'c')
return 300;
});

sbox.add('a', new Number(100));
sbox.add('coroutine', require('coroutine'));
assert.equal(true, sbox.has('a'));
assert.equal(true, sbox.has('coroutine'));
assert.equal(false, sbox.has('foo'));
});

it("addScript", () => {
var a = sbox.addScript("t1.js", "module.exports = {a : 100};");
Expand Down

0 comments on commit c56f754

Please sign in to comment.