Permalink
Browse files

SandBox, refactor: rewrite some of ExtLoader.

  • Loading branch information...
xicilion committed Jul 3, 2018
1 parent 0399450 commit bd3a4b8d44f12414985296e87fb1ec5e9565aaf8
View
@@ -116,15 +116,15 @@ class SandBox : public SandBox_base {
{
}
public:
typedef std::pair<exlib::string, v8::Local<v8::Value>> arg;
public:
result_t run_script(Context* ctx, Buffer_base* src, exlib::string name,
v8::Local<v8::Array> argv);
result_t run_main(Context* ctx, Buffer_base* src, exlib::string name,
v8::Local<v8::Array> argv);
result_t run_worker(Context* ctx, Buffer_base* src, exlib::string name,
Worker_base* master);
std::vector<arg>& extarg, bool is_main);
result_t run_module(Context* ctx, Buffer_base* src, exlib::string name,
v8::Local<v8::Object> module, v8::Local<v8::Object> exports);
v8::Local<v8::Object> module, v8::Local<v8::Object> exports,
std::vector<arg>& extarg);
public:
virtual result_t run(Context* ctx, Buffer_base* src, exlib::string name,
@@ -212,6 +212,7 @@ class SandBox : public SandBox_base {
static const char* script_args;
static const char* worker_args;
static const char* module_args;
static const char* base_args;
public:
std::vector<obj_ptr<ExtLoader>> m_loaders;
@@ -42,7 +42,8 @@ result_t SandBox::installScript(exlib::string srcname, Buffer_base* script,
InstallModule(srcname, exports, mod);
hr = l->run_module(&context, script, srcname, mod, exports);
std::vector<ExtLoader::arg> extarg;
hr = l->run_module(&context, script, srcname, mod, exports, extarg);
if (hr < 0) {
// delete from modules
remove(srcname);
@@ -10,6 +10,7 @@
#include "Buffer.h"
#include "path.h"
#include "options.h"
#include "ifs/global.h"
namespace fibjs {
@@ -45,7 +46,14 @@ result_t SandBox::run_main(exlib::string fname, v8::Local<v8::Array> argv)
return hr;
Context context(this, fname);
return l->run_main(&context, bin, fname, argv);
Isolate* isolate = holder();
std::vector<ExtLoader::arg> extarg(2);
extarg[0] = ExtLoader::arg("argv", argv);
extarg[1] = ExtLoader::arg("repl",
global_base::class_info().getModule(isolate)->Get(isolate->NewString("repl")));
return l->run_script(&context, bin, fname, extarg, true);
}
result_t SandBox::run_worker(exlib::string fname, Worker_base* master)
@@ -69,7 +77,11 @@ result_t SandBox::run_worker(exlib::string fname, Worker_base* master)
return hr;
Context context(this, fname);
return l->run_worker(&context, bin, fname, master);
std::vector<ExtLoader::arg> extarg(1);
extarg[0] = ExtLoader::arg("Master", master->wrap());
return l->run_script(&context, bin, fname, extarg, false);
}
result_t SandBox::run(exlib::string fname, v8::Local<v8::Array> argv)
@@ -95,6 +107,10 @@ result_t SandBox::run(exlib::string fname, v8::Local<v8::Array> argv)
return hr;
Context context(this, fname);
return l->run_script(&context, bin, fname, argv);
std::vector<ExtLoader::arg> extarg(2);
extarg[0] = ExtLoader::arg("argv", argv);
extarg[1] = ExtLoader::arg("repl", v8::Undefined(holder()->m_isolate));
return l->run_script(&context, bin, fname, extarg, false);
}
}
@@ -12,16 +12,12 @@
namespace fibjs {
const char* SandBox::script_args = "(async function(__filename,__dirname,require,run,exports,module,argv,repl){";
static const int32_t script_args_count = 8;
const char* SandBox::worker_args = "(async function(__filename,__dirname,require,run,exports,module,Master){";
static const int32_t worker_args_count = 7;
const char* SandBox::module_args = "(async function(__filename,__dirname,require,run,exports,module){";
static const int32_t module_args_count = 6;
const char* SandBox::base_args = "(async function(__filename,__dirname,require,run,exports,module";
result_t SandBox::ExtLoader::run_script(Context* ctx, Buffer_base* src, exlib::string name,
v8::Local<v8::Array> argv)
std::vector<arg>& extarg, bool is_main)
{
Isolate* isolate = ctx->m_sb->holder();
@@ -34,86 +30,39 @@ result_t SandBox::ExtLoader::run_script(Context* ctx, Buffer_base* src, exlib::s
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)
{
Isolate* isolate = ctx->m_sb->holder();
v8::Local<v8::String> strRequire = isolate->NewString("require");
v8::Local<v8::String> strExports = isolate->NewString("exports");
if (is_main)
ctx->m_fnRequest->Set(isolate->NewString("main"), module);
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);
ctx->m_fnRequest->Set(isolate->NewString("main"), module);
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);
return run_module(ctx, src, name, module, exports, extarg);
}
result_t SandBox::ExtLoader::run_worker(Context* ctx, Buffer_base* src, exlib::string name,
Worker_base* master)
result_t SandBox::ExtLoader::run_module(Context* ctx, Buffer_base* src, exlib::string name,
v8::Local<v8::Object> module, v8::Local<v8::Object> exports,
std::vector<arg>& extarg)
{
Isolate* isolate = ctx->m_sb->holder();
std::vector<v8::Local<v8::Value>> args;
v8::Local<v8::String> strRequire = isolate->NewString("require");
v8::Local<v8::String> strExports = isolate->NewString("exports");
args.resize(extarg.size() + 6);
v8::Local<v8::Object> module = v8::Object::New(isolate->m_isolate);
v8::Local<v8::Object> exports = v8::Object::New(isolate->m_isolate);
args[0] = v8::Local<v8::Value>();
args[1] = v8::Local<v8::Value>();
args[2] = ctx->m_fnRequest;
args[3] = ctx->m_fnRun;
args[4] = exports;
args[5] = module;
module->Set(strExports, exports);
module->Set(strRequire, ctx->m_fnRequest);
int32_t i;
exlib::string arg_names(base_args);
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);
}
for (i = 0; i < (int32_t)extarg.size(); i++) {
arg& a = extarg[i];
arg_names.append(1, ',');
arg_names.append(a.first);
args[i + 6] = a.second;
}
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>(),
ctx->m_fnRequest,
ctx->m_fnRun,
exports,
module
};
return run(ctx, src, name, module_args, args, module_args_count);
arg_names.append("){", 2);
return run(ctx, src, name, arg_names.c_str(), args.data(), (int32_t)args.size());
}
}

0 comments on commit bd3a4b8

Please sign in to comment.