Permalink
Browse files

sandbox, feat: passout callbackArgs in 'SandBox::CustomExtLoader::run' (

  • Loading branch information...
richardo2016 authored and xicilion committed Aug 13, 2018
1 parent 4ec046d commit 8a1dc8cf3337d1bfb3eb8268e822acd7b7ec3aac
Showing with 40 additions and 12 deletions.
  1. +14 −4 fibjs/src/sandbox/loaders/custom_loader.cpp
  2. +26 −8 test/vm_test.js
@@ -22,15 +22,25 @@ result_t SandBox::CustomExtLoader::run(SandBox::Context* ctx, Buffer_base* src,
{
Isolate* isolate = ctx->m_sb->holder();
v8::Local<v8::String> soname = isolate->NewString(name);
v8::Local<v8::String> filename = isolate->NewString(name);
exlib::string pname;
path_base::dirname(name, pname);
v8::Local<v8::Value> require_fn = ctx->m_sb->GetPrivate(SandBox::_get_extloader_pname(m_ext));
// transpile with customFile first :start
// read filecontent :start
std::vector<v8::Local<v8::Value>> transpileArgs;
transpileArgs.resize(1);
transpileArgs.resize(2);
src->valueOf(transpileArgs[0]);
obj_ptr<NObject> requireInfo = new NObject();
requireInfo->add("dirname", pname);
requireInfo->add("filename", name);
v8::Local<v8::Value> callbackObj;
requireInfo->valueOf(callbackObj);
transpileArgs[1] = callbackObj;
src->valueOf(transpileArgs[0]);
@@ -47,7 +57,7 @@ result_t SandBox::CustomExtLoader::run(SandBox::Context* ctx, Buffer_base* src,
} else
return CHECK_ERROR(Runtime::setError(GetException(try_catch, 0)));
}
// transpile with customFile first :end
// read filecontent :end
v8::Local<v8::Object> module = v8::Local<v8::Object>::Cast(args[5]);
module->Set(isolate->NewString("exports"), fileContent);
View
@@ -170,7 +170,7 @@ describe("vm", () => {
})
sbox.require('./vm_test/custom_ext.abc', __dirname);
sbox.setModuleLoader('.abc', function (content) {
sbox.setModuleLoader('.abc', function (buf) {
});
sbox.require('./vm_test/custom_ext', __dirname);
})
@@ -180,29 +180,47 @@ describe("vm", () => {
var testVarValue = 'lalala'
sbox.setModuleLoader('.abc', function (content) {
sbox.setModuleLoader('.abc', function (buf) {
return testVarValue;
});
var testVar = sbox.require('./vm_test/custom_ext', __dirname);
assert.equal(testVar, 'lalala');
assert.throws(() => {
(new SandBox({})).setModuleLoader('^abc', function (content) {
(new SandBox({})).setModuleLoader('^abc', function (buf) {
return testVarValue;
});
(new SandBox({})).setModuleLoader('-abc', function (content) {
});
assert.throws(() => {
(new SandBox({})).setModuleLoader('-abc', function (buf) {
return testVarValue;
});
})
})
it("setModuleLoader: requireInfo", () => {
var sbox = new vm.SandBox({});
sbox.setModuleLoader('.abc', function (buf, requireInfo) {
return requireInfo;
});
var expectedRequireInfo = {
dirname: path.resolve(__dirname, './vm_test'),
filename: path.resolve(__dirname, './vm_test/custom_ext.abc')
}
var testArgs = sbox.require('./vm_test/custom_ext', __dirname);
assert.deepEqual(testArgs, expectedRequireInfo);
})
it("setModuleLoader: cached require", () => {
var sbox = new vm.SandBox({});
var testVarValue = 'I am not increasing:'
var initialCnt = cnt = 0;
sbox.setModuleLoader('.abc', function (content) {
sbox.setModuleLoader('.abc', function (buf) {
var _return = testVarValue + cnt;
cnt++;
@@ -224,7 +242,7 @@ describe("vm", () => {
var testVarValue = 'miaomiaomiao'
var t0 = Date.now();
sbox1.setModuleLoader('.abc', function (content) {
sbox1.setModuleLoader('.abc', function (buf) {
return { test: testVarValue + t0 };
});
@@ -249,7 +267,7 @@ describe("vm", () => {
it's better to delete the sandbox and build new one, then re-require all modules.
*/
sbox1 = new vm.SandBox({})
sbox1.setModuleLoader('.abc', function (content) {
sbox1.setModuleLoader('.abc', function (buf) {
return testVarValue;
});
testVar1 = sbox1.require('./vm_test/custom_ext', __dirname);
@@ -258,7 +276,7 @@ describe("vm", () => {
var t = Date.now();
var testVar2;
sbox2.setModuleLoader('.abc', function (content) {
sbox2.setModuleLoader('.abc', function (buf) {
return testVarValue + t;
});
testVar2 = sbox2.require('./vm_test/custom_ext.abc', __dirname);

0 comments on commit 8a1dc8c

Please sign in to comment.