Skip to content

Commit

Permalink
sandbox, feat: support exports in main script.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Nov 27, 2017
1 parent 9a24097 commit b617bb8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
5 changes: 0 additions & 5 deletions fibjs/include/SandBox.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -206,13 +206,8 @@ class SandBox : public SandBox_base {


public: public:
static const char* script_args; static const char* script_args;
static const int32_t script_args_count;

static const char* worker_args; static const char* worker_args;
static const int32_t worker_args_count;

static const char* module_args; static const char* module_args;
static const int32_t module_args_count;


public: public:
std::vector<obj_ptr<ExtLoader>> m_loaders; std::vector<obj_ptr<ExtLoader>> m_loaders;
Expand Down
63 changes: 50 additions & 13 deletions fibjs/src/sandbox/loaders/base_loader.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,61 +11,98 @@


namespace fibjs { namespace fibjs {


const char* SandBox::script_args = "(function(__filename,__dirname,require,run,argv,repl){"; const char* SandBox::script_args = "(function(__filename,__dirname,require,run,exports,module,argv,repl){";
const int32_t SandBox::script_args_count = 6; static const int32_t script_args_count = 8;


const char* SandBox::worker_args = "(function(__filename,__dirname,require,run,Master){"; const char* SandBox::worker_args = "(function(__filename,__dirname,require,run,exports,module,Master){";
const int32_t SandBox::worker_args_count = 5; static const int32_t worker_args_count = 7;


const char* SandBox::module_args = "(function(__filename,__dirname,require,run,exports,module){"; 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, result_t SandBox::ExtLoader::run_script(Context* ctx, Buffer_base* src, exlib::string name,
exlib::string name, v8::Local<v8::Array> argv) 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> args[10] = {
v8::Local<v8::Value>(), v8::Local<v8::Value>(), v8::Local<v8::Value>(), v8::Local<v8::Value>(),
ctx->m_fnRequest, ctx->m_fnRequest,
ctx->m_fnRun, ctx->m_fnRun,
exports,
module,
argv, argv,
v8::Undefined(ctx->m_sb->holder()->m_isolate) v8::Undefined(ctx->m_sb->holder()->m_isolate)
}; };


return run(ctx, src, name, script_args, args, script_args_count); return run(ctx, src, name, script_args, args, script_args_count);
} }


result_t SandBox::ExtLoader::run_main(Context* ctx, Buffer_base* src, result_t SandBox::ExtLoader::run_main(Context* ctx, Buffer_base* src, exlib::string name,
exlib::string name, v8::Local<v8::Array> argv) v8::Local<v8::Array> argv)
{ {
Isolate* isolate = ctx->m_sb->holder(); 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( v8::Local<v8::Value> replFunc = global_base::class_info().getModule(isolate)->Get(
isolate->NewString("repl")); isolate->NewString("repl"));


v8::Local<v8::Value> args[10] = { v8::Local<v8::Value> args[10] = {
v8::Local<v8::Value>(), v8::Local<v8::Value>(), v8::Local<v8::Value>(), v8::Local<v8::Value>(),
ctx->m_fnRequest, ctx->m_fnRequest,
ctx->m_fnRun, ctx->m_fnRun,
exports,
module,
argv, argv,
replFunc replFunc
}; };
return run(ctx, src, name, script_args, args, script_args_count); return run(ctx, src, name, script_args, args, script_args_count);
} }


result_t SandBox::ExtLoader::run_worker(Context* ctx, Buffer_base* src, result_t SandBox::ExtLoader::run_worker(Context* ctx, Buffer_base* src, exlib::string name,
exlib::string name, Worker_base* master) 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> args[10] = {
v8::Local<v8::Value>(), v8::Local<v8::Value>(), v8::Local<v8::Value>(), v8::Local<v8::Value>(),
ctx->m_fnRequest, ctx->m_fnRequest,
ctx->m_fnRun, ctx->m_fnRun,
exports,
module,
master->wrap() master->wrap()
}; };
return run(ctx, src, name, worker_args, args, worker_args_count); return run(ctx, src, name, worker_args, args, worker_args_count);
} }


result_t SandBox::ExtLoader::run_module(Context* ctx, Buffer_base* src, result_t SandBox::ExtLoader::run_module(Context* ctx, Buffer_base* src, exlib::string name,
exlib::string name, v8::Local<v8::Object> module, v8::Local<v8::Object> exports) v8::Local<v8::Object> module, v8::Local<v8::Object> exports)
{ {
v8::Local<v8::Value> args[10] = { v8::Local<v8::Value> args[10] = {
v8::Local<v8::Value>(), v8::Local<v8::Value>(), v8::Local<v8::Value>(), v8::Local<v8::Value>(),
Expand Down
2 changes: 2 additions & 0 deletions test/module/exec18.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
var assert = require('assert');
assert.equal(module.exports, exports);
4 changes: 4 additions & 0 deletions test/module_test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ describe("module", () => {
it("strack", () => { it("strack", () => {
assert.ok(require("./module/stack").func().match(/module_test/)); assert.ok(require("./module/stack").func().match(/module_test/));
}); });

it("support exports in script", () => {
run('./module/exec18');
});
}); });


repl && test.run(console.DEBUG); repl && test.run(console.DEBUG);

0 comments on commit b617bb8

Please sign in to comment.