Skip to content

Commit

Permalink
SandBox, refactor: set resource_line_offset to -1 to enable v8 to rep…
Browse files Browse the repository at this point in the history
…ort the correct line number.
  • Loading branch information
xicilion committed Jun 8, 2023
1 parent 8f363b3 commit 61ccbeb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 23 deletions.
24 changes: 6 additions & 18 deletions fibjs/src/sandbox/loaders/js_loader.cpp
Expand Up @@ -29,30 +29,18 @@ result_t JsLoader::compile(SandBox::Context* ctx, Buffer_base* src, exlib::strin
_strScript[1] = '/';
}

exlib::string src1 = arg_names + strScript + "\n});";
exlib::string src1 = arg_names + "\n" + strScript + "\n});";

TryCatch try_catch;
v8::ScriptOrigin so_origin(isolate->m_isolate, soname);
script = v8::Script::Compile(
isolate->m_isolate->GetCurrentContext(),
isolate->NewString(src1), &so_origin).FromMaybe(v8::Local<v8::Script>());

if (script.IsEmpty()) {
TryCatch try_catch1;
v8::ScriptOrigin so_origin(isolate->m_isolate, soname, -1);

v8::ScriptCompiler::Source script_source(
isolate->NewString(strScript),
so_origin);

if (v8::ScriptCompiler::CompileUnboundScript(
isolate->m_isolate, &script_source)
.IsEmpty()) {
try_catch.Reset();
return throwSyntaxError(try_catch1);
}
script = v8::Script::Compile(isolate->m_isolate->GetCurrentContext(),
isolate->NewString(src1), &so_origin)
.FromMaybe(v8::Local<v8::Script>());

if (script.IsEmpty())
return throwSyntaxError(try_catch);
}

return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion fibjs/src/sandbox/loaders/jsc_loader.cpp
Expand Up @@ -73,8 +73,10 @@ result_t JscLoader::compile(SandBox::Context* ctx, Buffer_base* src, exlib::stri
v8::ScriptCompiler::CachedData* cache;
cache = new v8::ScriptCompiler::CachedData(code->data(), code_len);

v8::ScriptOrigin so_origin(isolate->m_isolate, soname, -1);

v8::ScriptCompiler::Source source(isolate->NewString(s_temp_source),
v8::ScriptOrigin(isolate->m_isolate, soname), cache);
so_origin, cache);

script = v8::ScriptCompiler::Compile(isolate->context(), &source,
v8::ScriptCompiler::kConsumeCodeCache)
Expand Down
6 changes: 3 additions & 3 deletions fibjs/src/util/util_compile.cpp
Expand Up @@ -43,7 +43,7 @@ result_t util_base::compile(exlib::string srcname, exlib::string script,
return throwSyntaxError(try_catch);
}

const char* args;
exlib::string args;

switch (mode) {
case 2:
Expand All @@ -53,14 +53,14 @@ result_t util_base::compile(exlib::string srcname, exlib::string script,
args = SandBox::module_args;
break;
}
script = args + script + "\n});";
script = args + "\n" + script + "\n});";

exlib::wstring wscript(utf8to16String(script));

v8::Local<v8::String> v8src = v8::String::NewFromTwoByte(isolate->m_isolate, (const uint16_t*)wscript.c_str(),
v8::NewStringType::kNormal, (int32_t)wscript.length())
.FromMaybe(v8::Local<v8::String>());
v8::ScriptCompiler::Source script_source(v8src, v8::ScriptOrigin(isolate->m_isolate, soname));
v8::ScriptCompiler::Source script_source(v8src, v8::ScriptOrigin(isolate->m_isolate, soname, -1));

v8::Local<v8::UnboundScript> ubs = v8::ScriptCompiler::CompileUnboundScript(
isolate->m_isolate, &script_source,
Expand Down
2 changes: 1 addition & 1 deletion test/promise_test.js
Expand Up @@ -31,7 +31,7 @@ describe('promise', () => {
});

it("error in promise", () => {
t('promise3.js', 'Error: 1000\n at /promise/promise3.js:2:11\n at new Promise (<anonymous>)\n at /promise/promise3.js:1:73\n');
t('promise3.js', 'Error: 1000\n at /promise/promise3.js:2:11\n at new Promise (<anonymous>)\n at /promise/promise3.js:1:1\n');
});

it("error in promise with catch", () => {
Expand Down

0 comments on commit 61ccbeb

Please sign in to comment.