Permalink
Browse files

sandbox, feat: support exports in main script.

  • Loading branch information...
xicilion committed Nov 27, 2017
1 parent 9a24097 commit b617bb877a0d27148c15125891cc6322c2412a5e
Showing with 56 additions and 18 deletions.
  1. +0 −5 fibjs/include/SandBox.h
  2. +50 −13 fibjs/src/sandbox/loaders/base_loader.cpp
  3. +2 −0 test/module/exec18.js
  4. +4 −0 test/module_test.js
View
@@ -206,13 +206,8 @@ class SandBox : public SandBox_base {
public:
static const char* script_args;
static const int32_t script_args_count;
static const char* worker_args;
static const int32_t worker_args_count;
static const char* module_args;
static const int32_t module_args_count;
public:
std::vector<obj_ptr<ExtLoader>> m_loaders;
@@ -11,61 +11,98 @@
namespace fibjs {
const char* SandBox::script_args = "(function(__filename,__dirname,require,run,argv,repl){";
const int32_t SandBox::script_args_count = 6;
const char* SandBox::script_args = "(function(__filename,__dirname,require,run,exports,module,argv,repl){";
static const int32_t script_args_count = 8;
const char* SandBox::worker_args = "(function(__filename,__dirname,require,run,Master){";
const int32_t SandBox::worker_args_count = 5;
const char* SandBox::worker_args = "(function(__filename,__dirname,require,run,exports,module,Master){";
static const int32_t worker_args_count = 7;
const char* SandBox::module_args = "(function(__filename,__dirname,require,run,exports,module){";
const int32_t SandBox::module_args_count = 6;
static const int32_t module_args_count = 6;
result_t SandBox::ExtLoader::run_script(Context* ctx, Buffer_base* src,
exlib::string name, v8::Local<v8::Array> argv)
result_t SandBox::ExtLoader::run_script(Context* ctx, Buffer_base* src, exlib::string name,
v8::Local<v8::Array> argv)
{
Isolate* isolate = ctx->m_sb->holder();
v8::Local<v8::String> strRequire = isolate->NewString("require");
v8::Local<v8::String> strExports = isolate->NewString("exports");
v8::Local<v8::Object> module = v8::Object::New(isolate->m_isolate);
v8::Local<v8::Object> exports = v8::Object::New(isolate->m_isolate);
module->Set(strExports, exports);
module->Set(strRequire, ctx->m_fnRequest);
v8::Local<v8::Value> args[10] = {
v8::Local<v8::Value>(), v8::Local<v8::Value>(),
ctx->m_fnRequest,
ctx->m_fnRun,
exports,
module,
argv,
v8::Undefined(ctx->m_sb->holder()->m_isolate)
};
return run(ctx, src, name, script_args, args, script_args_count);
}
result_t SandBox::ExtLoader::run_main(Context* ctx, Buffer_base* src,
exlib::string name, v8::Local<v8::Array> argv)
result_t SandBox::ExtLoader::run_main(Context* ctx, Buffer_base* src, exlib::string name,
v8::Local<v8::Array> argv)
{
Isolate* isolate = ctx->m_sb->holder();
v8::Local<v8::String> strRequire = isolate->NewString("require");
v8::Local<v8::String> strExports = isolate->NewString("exports");
v8::Local<v8::Object> module = v8::Object::New(isolate->m_isolate);
v8::Local<v8::Object> exports = v8::Object::New(isolate->m_isolate);
module->Set(strExports, exports);
module->Set(strRequire, ctx->m_fnRequest);
v8::Local<v8::Value> replFunc = global_base::class_info().getModule(isolate)->Get(
isolate->NewString("repl"));
v8::Local<v8::Value> args[10] = {
v8::Local<v8::Value>(), v8::Local<v8::Value>(),
ctx->m_fnRequest,
ctx->m_fnRun,
exports,
module,
argv,
replFunc
};
return run(ctx, src, name, script_args, args, script_args_count);
}
result_t SandBox::ExtLoader::run_worker(Context* ctx, Buffer_base* src,
exlib::string name, Worker_base* master)
result_t SandBox::ExtLoader::run_worker(Context* ctx, Buffer_base* src, exlib::string name,
Worker_base* master)
{
Isolate* isolate = ctx->m_sb->holder();
v8::Local<v8::String> strRequire = isolate->NewString("require");
v8::Local<v8::String> strExports = isolate->NewString("exports");
v8::Local<v8::Object> module = v8::Object::New(isolate->m_isolate);
v8::Local<v8::Object> exports = v8::Object::New(isolate->m_isolate);
module->Set(strExports, exports);
module->Set(strRequire, ctx->m_fnRequest);
v8::Local<v8::Value> args[10] = {
v8::Local<v8::Value>(), v8::Local<v8::Value>(),
ctx->m_fnRequest,
ctx->m_fnRun,
exports,
module,
master->wrap()
};
return run(ctx, src, name, worker_args, args, worker_args_count);
}
result_t SandBox::ExtLoader::run_module(Context* ctx, Buffer_base* src,
exlib::string name, v8::Local<v8::Object> module, v8::Local<v8::Object> exports)
result_t SandBox::ExtLoader::run_module(Context* ctx, Buffer_base* src, exlib::string name,
v8::Local<v8::Object> module, v8::Local<v8::Object> exports)
{
v8::Local<v8::Value> args[10] = {
v8::Local<v8::Value>(), v8::Local<v8::Value>(),
View
@@ -0,0 +1,2 @@
var assert = require('assert');
assert.equal(module.exports, exports);
View
@@ -184,6 +184,10 @@ describe("module", () => {
it("strack", () => {
assert.ok(require("./module/stack").func().match(/module_test/));
});
it("support exports in script", () => {
run('./module/exec18');
});
});
repl && test.run(console.DEBUG);

0 comments on commit b617bb8

Please sign in to comment.