Skip to content

Commit

Permalink
rewrite sanbox.run.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Jun 2, 2017
1 parent ec616ea commit dd0f99f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 165 deletions.
15 changes: 4 additions & 11 deletions fibjs/include/SandBox.h
Expand Up @@ -67,17 +67,10 @@ class SandBox : public fibjs::SandBox_base {
result_t run(Buffer_base* src, exlib::string name, exlib::string arg_names, v8::Local<v8::Value>* args, int32_t args_count);
result_t run(exlib::string src, exlib::string name, exlib::string arg_names, v8::Local<v8::Value>* args, int32_t args_count);

template <typename T>
result_t run_script(T src, exlib::string name, v8::Local<v8::Array> argv);

template <typename T>
result_t run_main(T src, exlib::string name, v8::Local<v8::Array> argv);

template <typename T>
result_t run_worker(T src, exlib::string name, Worker_base* worker);

template <typename T>
result_t run_module(T src, exlib::string name, v8::Local<v8::Object> module,
result_t run_script(Buffer_base* src, exlib::string name, v8::Local<v8::Array> argv);
result_t run_main(Buffer_base* src, exlib::string name, v8::Local<v8::Array> argv);
result_t run_worker(Buffer_base* src, exlib::string name, Worker_base* master);
result_t run_module(Buffer_base* src, exlib::string name, v8::Local<v8::Object> module,
v8::Local<v8::Object> exports);

static result_t repl(v8::Local<v8::Array> cmds, Stream_base* out);
Expand Down
214 changes: 60 additions & 154 deletions fibjs/src/sandbox/SandBox_run.cpp
Expand Up @@ -227,6 +227,15 @@ result_t SandBox::Context::run(exlib::string src, exlib::string name, exlib::str

result_t SandBox::Context::run(Buffer_base* src, exlib::string name, exlib::string arg_names, v8::Local<v8::Value>* args, int32_t args_count)
{
if (name.length() <= 4 || qstrcmp(name.c_str() + name.length() - 4, ".jsc")) {
exlib::string strScript;

if (src)
src->toString(strScript);

return run(strScript, name, arg_names, args, args_count);
}

result_t hr;

obj_ptr<Buffer_base> unz;
Expand Down Expand Up @@ -288,8 +297,7 @@ result_t SandBox::Context::run(Buffer_base* src, exlib::string name, exlib::stri
const char* script_args = "(function(__filename,__dirname,require,run,argv){";
static const int32_t script_args_count = 5;

template <typename T>
result_t SandBox::Context::run_script(T src, exlib::string name, v8::Local<v8::Array> argv)
result_t SandBox::Context::run_script(Buffer_base* src, exlib::string name, v8::Local<v8::Array> argv)
{
v8::Local<v8::Value> args[10] = {
v8::Local<v8::Value>(), v8::Local<v8::Value>(),
Expand All @@ -304,8 +312,7 @@ result_t SandBox::Context::run_script(T src, exlib::string name, v8::Local<v8::A
const char* main_args = "(function(__filename,__dirname,require,run,argv,repl){";
static const int32_t main_args_count = 6;

template <typename T>
result_t SandBox::Context::run_main(T src, exlib::string name, v8::Local<v8::Array> argv)
result_t SandBox::Context::run_main(Buffer_base* src, exlib::string name, v8::Local<v8::Array> argv)
{
Isolate* isolate = m_sb->holder();

Expand All @@ -325,23 +332,21 @@ result_t SandBox::Context::run_main(T src, exlib::string name, v8::Local<v8::Arr
const char* worker_args = "(function(__filename,__dirname,require,run,Master){";
static const int32_t worker_args_count = 5;

template <typename T>
result_t SandBox::Context::run_worker(T src, exlib::string name, Worker_base* worker)
result_t SandBox::Context::run_worker(Buffer_base* src, exlib::string name, Worker_base* master)
{
v8::Local<v8::Value> args[10] = {
v8::Local<v8::Value>(), v8::Local<v8::Value>(),
m_fnRequest,
m_fnRun,
worker->wrap()
master->wrap()
};
return run(src, name, worker_args, args, worker_args_count);
}

const char* module_args = "(function(__filename,__dirname,require,run,exports,module){";
static const int32_t module_args_count = 6;

template <typename T>
result_t SandBox::Context::run_module(T src, exlib::string name, v8::Local<v8::Object> module,
result_t SandBox::Context::run_module(Buffer_base* src, exlib::string name, v8::Local<v8::Object> module,
v8::Local<v8::Object> exports)
{
v8::Local<v8::Value> args[10] = {
Expand Down Expand Up @@ -380,89 +385,49 @@ result_t SandBox::addScript(exlib::string srcname, Buffer_base* script,

retVal = v;
return 0;
} else if (id.length() > 3 && !qstrcmp(id.c_str() + id.length() - 3, ".js")) {
Isolate* isolate = holder();
Context context(this, srcname);
}

if (id.length() > 3 && !qstrcmp(id.c_str() + id.length() - 3, ".js"))
id.resize(id.length() - 3);

exlib::string strScript;
if (script)
script->toString(strScript);

v8::Local<v8::Object> mod;
v8::Local<v8::Object> exports;

// cache string
v8::Local<v8::String> strRequire = isolate->NewFromUtf8("require");
v8::Local<v8::String> strExports = isolate->NewFromUtf8("exports");

exports = v8::Object::New(isolate->m_isolate);

// module and exports object
mod = v8::Object::New(isolate->m_isolate);

// init module
mod->Set(strExports, exports);
mod->Set(strRequire, context.m_fnRequest);

InstallModule(id, exports);

hr = context.run_module(strScript, srcname, mod, exports);
if (hr < 0) {
// delete from modules
remove(id);
return hr;
}

// use module.exports as result value
v8::Local<v8::Value> v = mod->Get(strExports);
if (!exports->Equals(v))
InstallModule(id, v);

retVal = v;
return 0;
} else if (id.length() > 4 && !qstrcmp(id.c_str() + id.length() - 4, ".jsc")) {
Isolate* isolate = holder();
Context context(this, srcname);

else if (id.length() > 4 && !qstrcmp(id.c_str() + id.length() - 4, ".jsc"))
id.resize(id.length() - 4);
else
return CHECK_ERROR(Runtime::setError("SandBox: Invalid file format."));

v8::Local<v8::Object> mod;
v8::Local<v8::Object> exports;
Isolate* isolate = holder();
Context context(this, srcname);

// cache string
v8::Local<v8::String> strRequire = isolate->NewFromUtf8("require");
v8::Local<v8::String> strExports = isolate->NewFromUtf8("exports");
v8::Local<v8::Object> mod;
v8::Local<v8::Object> exports;

exports = v8::Object::New(isolate->m_isolate);
// cache string
v8::Local<v8::String> strRequire = isolate->NewFromUtf8("require");
v8::Local<v8::String> strExports = isolate->NewFromUtf8("exports");

// module and exports object
mod = v8::Object::New(isolate->m_isolate);
exports = v8::Object::New(isolate->m_isolate);

// init module
mod->Set(strExports, exports);
mod->Set(strRequire, context.m_fnRequest);
// module and exports object
mod = v8::Object::New(isolate->m_isolate);

InstallModule(id, exports);
// init module
mod->Set(strExports, exports);
mod->Set(strRequire, context.m_fnRequest);

hr = context.run_module(script, srcname, mod, exports);
if (hr < 0) {
// delete from modules
remove(id);
return hr;
}
InstallModule(id, exports);

// use module.exports as result value
v8::Local<v8::Value> v = mod->Get(strExports);
if (!exports->Equals(v))
InstallModule(id, v);
hr = context.run_module(script, srcname, mod, exports);
if (hr < 0) {
// delete from modules
remove(id);
return hr;
}

retVal = v;
return 0;
} else
return CHECK_ERROR(Runtime::setError("SandBox: Invalid file format."));
// use module.exports as result value
v8::Local<v8::Value> v = mod->Get(strExports);
if (!exports->Equals(v))
InstallModule(id, v);

retVal = v;
return 0;
}

Expand Down Expand Up @@ -673,38 +638,17 @@ result_t SandBox::run_main(exlib::string fname, v8::Local<v8::Array> argv)
pathAdd(sfname, fname);
path_base::normalize(sfname, sfname);

bool is_jsc = false;

if (sfname.length() > 4 && !qstrcmp(sfname.c_str() + sfname.length() - 4, ".jsc"))
is_jsc = true;
else if (sfname.length() > 3 && !qstrcmp(sfname.c_str() + sfname.length() - 3, ".js"))
;
else
return CHECK_ERROR(Runtime::setError("SandBox: Invalid file format."));

const char* pname = sfname.c_str();

obj_ptr<Buffer_base> bin;
exlib::string buf;

if (is_jsc) {
hr = fs_base::cc_readFile(pname, bin);
if (hr < 0)
return hr;

Context context(this, pname);
return context.run_main(bin, pname, argv);
} else {
hr = fs_base::cc_readTextFile(pname, buf);
if (hr < 0)
return hr;
hr = fs_base::cc_readFile(sfname, bin);
if (hr < 0)
return hr;

Context context(this, pname);
return context.run_main(buf, pname, argv);
}
Context context(this, sfname);
return context.run_main(bin, sfname, argv);
}

result_t SandBox::run_worker(exlib::string fname, Worker_base* worker)
result_t SandBox::run_worker(exlib::string fname, Worker_base* master)
{
result_t hr;

Expand All @@ -713,33 +657,14 @@ result_t SandBox::run_worker(exlib::string fname, Worker_base* worker)
pathAdd(sfname, fname);
path_base::normalize(sfname, sfname);

bool is_jsc = false;

if (sfname.length() > 4 && !qstrcmp(sfname.c_str() + sfname.length() - 4, ".jsc"))
is_jsc = true;
else if (!(sfname.length() > 3 && !qstrcmp(sfname.c_str() + sfname.length() - 3, ".js")))
return CHECK_ERROR(Runtime::setError("SandBox: Invalid file format."));

const char* pname = sfname.c_str();

obj_ptr<Buffer_base> bin;
exlib::string buf;

if (is_jsc) {
hr = fs_base::cc_readFile(pname, bin);
if (hr < 0)
return hr;

Context context(this, pname);
return context.run_worker(bin, pname, worker);
} else {
hr = fs_base::cc_readTextFile(pname, buf);
if (hr < 0)
return hr;
hr = fs_base::cc_readFile(sfname, bin);
if (hr < 0)
return hr;

Context context(this, pname);
return context.run_worker(buf, pname, worker);
}
Context context(this, sfname);
return context.run_worker(bin, sfname, master);
}

result_t SandBox::run(exlib::string fname, v8::Local<v8::Array> argv)
Expand All @@ -751,32 +676,13 @@ result_t SandBox::run(exlib::string fname, v8::Local<v8::Array> argv)
pathAdd(sfname, fname);
path_base::normalize(sfname, sfname);

bool is_jsc = false;

if (sfname.length() > 4 && !qstrcmp(sfname.c_str() + sfname.length() - 4, ".jsc"))
is_jsc = true;
else if (!(sfname.length() > 3 && !qstrcmp(sfname.c_str() + sfname.length() - 3, ".js")))
return CHECK_ERROR(Runtime::setError("SandBox: Invalid file format."));

const char* pname = sfname.c_str();

obj_ptr<Buffer_base> bin;
exlib::string buf;

if (is_jsc) {
hr = fs_base::cc_readFile(pname, bin);
if (hr < 0)
return hr;

Context context(this, pname);
return context.run_script(bin, pname, argv);
} else {
hr = fs_base::cc_readTextFile(pname, buf);
if (hr < 0)
return hr;
hr = fs_base::cc_readFile(sfname, bin);
if (hr < 0)
return hr;

Context context(this, pname);
return context.run_script(buf, pname, argv);
}
Context context(this, sfname);
return context.run_script(bin, sfname, argv);
}
}

0 comments on commit dd0f99f

Please sign in to comment.