Permalink
Browse files

add 'has' method in SandBox Module. (#442)

  • Loading branch information...
tapir-dream authored and xicilion committed Jun 27, 2018
1 parent a8ad136 commit c56f754d3739168153388b1a07c0de8fe4517b94
Showing with 64 additions and 0 deletions.
  1. +1 −0 fibjs/include/SandBox.h
  2. +20 −0 fibjs/include/ifs/SandBox.h
  3. +11 −0 fibjs/src/sandbox/SandBox.cpp
  4. +6 −0 idl/us-en/SandBox.idl
  5. +6 −0 idl/zh-cn/SandBox.idl
  6. +20 −0 test/vm_test.js
View
@@ -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);
@@ -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;
@@ -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);
@@ -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 },
@@ -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;
@@ -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();
View
@@ -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
*/
View
@@ -61,6 +61,12 @@ interface SandBox : object
*/
remove(String id);
/*! @brief 从沙箱中检测基础模块是否存在
@param id 指定要检测的模块名称,此路径与当前运行脚本无关,必须为绝对路径或者模块名
@return 是否存在
*/
Boolean has(String id);
/*! @brief 复制当前沙箱,新沙箱包含当前沙箱的模块,以及相同的名称和 require 函数
@return 复制的新沙箱
*/
View
@@ -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};");

0 comments on commit c56f754

Please sign in to comment.