Permalink
Browse files

sandbox, refactor: implement virtual compile function.

  • Loading branch information...
xicilion committed Aug 14, 2018
1 parent 56806cd commit 926286d582b4be46369d65a67f782cdd79977081
View
@@ -129,12 +129,15 @@ class SandBox : public SandBox_base {
std::vector<arg>& extarg);
public:
virtual result_t run(Context* ctx, Buffer_base* src, exlib::string name,
exlib::string arg_names, std::vector<v8::Local<v8::Value>>& args)
virtual result_t compile(Context* ctx, Buffer_base* src, exlib::string name,
exlib::string arg_names, v8::Local<v8::Script>& script)
{
return CHECK_ERROR(Runtime::setError("SandBox: Invalid file format."));
}
virtual result_t run(Context* ctx, Buffer_base* src, exlib::string name,
exlib::string arg_names, std::vector<v8::Local<v8::Value>>& args);
public:
exlib::string m_ext;
};
@@ -8,6 +8,8 @@
#include "object.h"
#include "SandBox.h"
#include "ifs/global.h"
#include "ifs/util.h"
#include "path.h"
namespace fibjs {
@@ -63,6 +65,38 @@ result_t SandBox::ExtLoader::run_module(Context* ctx, Buffer_base* src, exlib::s
arg_names.append("){", 2);
return run(ctx, src, name, arg_names.c_str(), args);
return run(ctx, src, name, arg_names, args);
}
result_t SandBox::ExtLoader::run(Context* ctx, Buffer_base* src, exlib::string name,
exlib::string arg_names, std::vector<v8::Local<v8::Value>>& args)
{
result_t hr;
v8::Local<v8::Script> script;
hr = compile(ctx, src, name, arg_names, script);
if (hr < 0)
return hr;
v8::Local<v8::Value> v = script->Run();
if (v.IsEmpty())
return CALL_E_JAVASCRIPT;
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(v);
util_base::sync(func, true, func);
Isolate* isolate = ctx->m_sb->holder();
exlib::string pname;
path_base::dirname(name, pname);
args[0] = isolate->NewString(name);
args[1] = isolate->NewString(pname);
v8::Local<v8::Object> glob = isolate->context()->Global();
v = func->Call(glob, (int32_t)args.size(), args.data());
if (v.IsEmpty())
return CALL_E_JAVASCRIPT;
return 0;
}
}
@@ -13,64 +13,42 @@
namespace fibjs {
result_t JsLoader::run(SandBox::Context* ctx, Buffer_base* src, exlib::string name,
exlib::string arg_names, std::vector<v8::Local<v8::Value>>& args)
result_t JsLoader::compile(SandBox::Context* ctx, Buffer_base* src, exlib::string name,
exlib::string arg_names, v8::Local<v8::Script>& script)
{
Isolate* isolate = ctx->m_sb->holder();
v8::Local<v8::String> soname = isolate->NewString(name);
exlib::string pname;
path_base::dirname(name, pname);
v8::Local<v8::Script> script;
{
TryCatch try_catch;
exlib::string strScript;
src->toString(strScript);
const char* c_str = strScript.c_str();
if (strScript.length() > 2 && c_str[0] == '#' && c_str[1] == '!') {
strScript[0] = '/';
strScript[1] = '/';
}
exlib::string strScript;
src->toString(strScript);
const char* c_str = strScript.c_str();
exlib::string src1 = arg_names + strScript + "\n});";
if (strScript.length() > 2 && c_str[0] == '#' && c_str[1] == '!') {
strScript[0] = '/';
strScript[1] = '/';
}
script = v8::Script::Compile(
isolate->NewString(src1), soname);
if (script.IsEmpty()) {
TryCatch try_catch1;
exlib::string src1 = arg_names + strScript + "\n});";
v8::ScriptCompiler::Source script_source(
isolate->NewString(strScript),
v8::ScriptOrigin(soname));
TryCatch try_catch;
script = v8::Script::Compile(
isolate->NewString(src1), soname);
if (script.IsEmpty()) {
TryCatch try_catch1;
if (v8::ScriptCompiler::CompileUnboundScript(
isolate->m_isolate, &script_source)
.IsEmpty()) {
try_catch.Reset();
return throwSyntaxError(try_catch1);
}
v8::ScriptCompiler::Source script_source(
isolate->NewString(strScript),
v8::ScriptOrigin(soname));
throwSyntaxError(try_catch);
if (v8::ScriptCompiler::CompileUnboundScript(
isolate->m_isolate, &script_source)
.IsEmpty()) {
try_catch.Reset();
return throwSyntaxError(try_catch1);
}
}
v8::Local<v8::Value> v = script->Run();
if (v.IsEmpty())
return CALL_E_JAVASCRIPT;
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(v);
util_base::sync(func, true, func);
args[0] = soname;
args[1] = isolate->NewString(pname);
v8::Local<v8::Object> glob = isolate->context()->Global();
v = func->Call(glob, (int32_t)args.size(), args.data());
if (v.IsEmpty())
return CALL_E_JAVASCRIPT;
throwSyntaxError(try_catch);
}
return 0;
}
@@ -15,8 +15,8 @@
namespace fibjs {
result_t JscLoader::run(SandBox::Context* ctx, Buffer_base* src, exlib::string name,
exlib::string arg_names, std::vector<v8::Local<v8::Value>>& args)
result_t JscLoader::compile(SandBox::Context* ctx, Buffer_base* src, exlib::string name,
exlib::string arg_names, v8::Local<v8::Script>& script)
{
result_t hr;
@@ -66,7 +66,7 @@ result_t JscLoader::run(SandBox::Context* ctx, Buffer_base* src, exlib::string n
pos++;
}
v8::MaybeLocal<v8::Script> script;
v8::MaybeLocal<v8::Script> mayscript;
{
TryCatch try_catch;
@@ -76,26 +76,14 @@ result_t JscLoader::run(SandBox::Context* ctx, Buffer_base* src, exlib::string n
v8::ScriptCompiler::Source source(isolate->NewString(s_temp_source),
v8::ScriptOrigin(soname), cache);
script = v8::ScriptCompiler::Compile(isolate->context(), &source,
mayscript = v8::ScriptCompiler::Compile(isolate->context(), &source,
v8::ScriptCompiler::kConsumeCodeCache);
if (script.IsEmpty())
if (mayscript.IsEmpty())
return throwSyntaxError(try_catch);
}
v8::Local<v8::Value> v = script.ToLocalChecked()->Run();
if (v.IsEmpty())
return CALL_E_JAVASCRIPT;
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(v);
util_base::sync(func, true, func);
args[0] = soname;
args[1] = isolate->NewString(pname);
v8::Local<v8::Object> glob = isolate->context()->Global();
v = func->Call(glob, (int32_t)args.size(), args.data());
if (v.IsEmpty())
return CALL_E_JAVASCRIPT;
script = mayscript.ToLocalChecked();
return 0;
}
@@ -20,8 +20,8 @@ class JsLoader : public SandBox::ExtLoader {
}
public:
virtual result_t run(SandBox::Context* ctx, Buffer_base* src, exlib::string name,
exlib::string arg_names, std::vector<v8::Local<v8::Value>>& args);
virtual result_t compile(SandBox::Context* ctx, Buffer_base* src, exlib::string name,
exlib::string arg_names, v8::Local<v8::Script>& script);
};
class JscLoader : public SandBox::ExtLoader {
@@ -32,8 +32,8 @@ class JscLoader : public SandBox::ExtLoader {
}
public:
virtual result_t run(SandBox::Context* ctx, Buffer_base* src, exlib::string name,
exlib::string arg_names, std::vector<v8::Local<v8::Value>>& args);
virtual result_t compile(SandBox::Context* ctx, Buffer_base* src, exlib::string name,
exlib::string arg_names, v8::Local<v8::Script>& script);
};
class JsonLoader : public SandBox::ExtLoader {

0 comments on commit 926286d

Please sign in to comment.