Permalink
Browse files

sandbox, feat: add 'Sandbox::modules' to get dict of modules installe…

…d in it. (#457)
  • Loading branch information...
richardo2016 authored and xicilion committed Aug 13, 2018
1 parent d59f2d3 commit ab407ce914c0f101613486c535ef87cd63083241
Showing with 84 additions and 4 deletions.
  1. +1 −0 fibjs/include/SandBox.h
  2. +17 −1 fibjs/include/ifs/SandBox.h
  3. +18 −0 fibjs/src/sandbox/SandBox.cpp
  4. +4 −0 idl/zh-cn/SandBox.idl
  5. +44 −3 test/vm_test.js
View
@@ -34,6 +34,7 @@ class SandBox : public SandBox_base {
virtual result_t require(exlib::string id, exlib::string base, v8::Local<v8::Value>& retVal);
virtual result_t setModuleLoader(exlib::string extname, v8::Local<v8::Function> once_require_func);
virtual result_t get_global(v8::Local<v8::Object>& retVal);
virtual result_t get_modules(v8::Local<v8::Object>& retVal);
public:
v8::Local<v8::Object> mods()
@@ -38,6 +38,7 @@ class SandBox_base : public object_base {
virtual result_t require(exlib::string id, exlib::string base, v8::Local<v8::Value>& retVal) = 0;
virtual result_t setModuleLoader(exlib::string extname, v8::Local<v8::Function> once_require_func) = 0;
virtual result_t get_global(v8::Local<v8::Object>& retVal) = 0;
virtual result_t get_modules(v8::Local<v8::Object>& retVal) = 0;
public:
template <typename T>
@@ -55,6 +56,7 @@ class SandBox_base : public object_base {
static void s_require(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_setModuleLoader(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_get_global(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_get_modules(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
};
}
@@ -76,7 +78,8 @@ inline ClassInfo& SandBox_base::class_info()
};
static ClassData::ClassProperty s_property[] = {
{ "global", s_get_global, block_set, false }
{ "global", s_get_global, block_set, false },
{ "modules", s_get_modules, block_set, false }
};
static ClassData s_cd = {
@@ -301,6 +304,19 @@ inline void SandBox_base::s_get_global(v8::Local<v8::Name> property, const v8::P
METHOD_RETURN();
}
inline void SandBox_base::s_get_modules(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args)
{
v8::Local<v8::Object> vr;
METHOD_NAME("SandBox.modules");
METHOD_INSTANCE(SandBox_base);
PROPERTY_ENTER();
hr = pInst->get_modules(vr);
METHOD_RETURN();
}
}
#endif
@@ -196,4 +196,22 @@ result_t SandBox::get_global(v8::Local<v8::Object>& retVal)
return 0;
}
result_t SandBox::get_modules(v8::Local<v8::Object>& retVal)
{
Isolate* isolate = holder();
retVal = v8::Object::New(isolate->m_isolate);
v8::Local<v8::Object> ms = mods();
v8::Local<v8::Array> ks = ms->GetPropertyNames();
v8::Local<v8::String> mgetter = isolate->NewString("exports");
for (int32_t i = 0, len = ks->Length(); i < len; i++) {
v8::Local<v8::Value> k = ks->Get(i);
retVal->Set(k, ms->Get(k)->ToObject()->Get(mgetter));
}
return 0;
}
} /* namespace fibjs */
View
@@ -100,4 +100,8 @@ interface SandBox : object
/*! @brief 查询沙箱的 global 对象 */
readonly Object global;
/*! @brief 查询沙箱中现存的所有模块的字典对象
*/
readonly Object modules;
};
View
@@ -161,7 +161,6 @@ describe("vm", () => {
assert.deepEqual(b, b1);
});
it("setModuleLoader: ext recognition", () => {
var sbox = new vm.SandBox({});
@@ -260,10 +259,10 @@ describe("vm", () => {
var testVar1;
/*
/*
#4d04262b-d752-4d18-a9ad-a107cbd05682
moduld would be cached via its moduleId. so if you really want to re-setModuleLoader,
module would be cached via its moduleId. so if you really want to re-setModuleLoader,
it's better to delete the sandbox and build new one, then re-require all modules.
*/
sbox1 = new vm.SandBox({})
@@ -396,6 +395,48 @@ describe("vm", () => {
sbox1.addScript("t1.js", "new mq.Routing({});");
});
it("modules(dict)", () => {
function assertSrcWithTarget(mods) {
var sbox = new vm.SandBox(mods);
assert.deepEqual(mods, sbox.modules);
}
const modList = [];
modList[0] = {
b: {
a: 100,
b: 200
}
};
modList[1] = {};
modList[2] = { Function, Object, Array, String, Boolean, _: new.target };
modList[3] = { a: new Map(), b: new Set(), c: new Array(), d: new Object() };
modList[4] = { a: undefined, b: null };
modList[5] = { a: Symbol(Date.now()) };
modList[6] = { a: 1, b: -1, c: Infinity, d: -Infinity, e: 0, d: +0, f: -0 };
modList[7] = { a: true, b: false };
modList[8] = { http: require('http'), util: require('util') };
modList.forEach(mod => assertSrcWithTarget(mod))
var sbox = new vm.SandBox(modList[0])
assert.deepEqual(modList[0], sbox.modules);
sbox.add('123', {})
assert.deepEqual({
123: {},
...modList[0]
}, sbox.modules);
sbox.addScript('123.js', "exports.__456 = {abc: 123}")
assert.deepEqual({
123: {},
'123.js': {
__456: {abc: 123},
},
...modList[0]
}, sbox.modules)
});
it("require.cache", () => {
assert.isObject(require.cache);
});

0 comments on commit ab407ce

Please sign in to comment.