Take the ScriptOrigin into account for CompileFunctionInContext

R=jochen@chromium.org,yangguo@chromium.org
LOG=n
BUG=

Review URL: https://codereview.chromium.org/1233563005

Cr-Commit-Position: refs/heads/master@{#29700}
This commit is contained in:
epertoso 2015-07-16 05:08:01 -07:00 committed by Commit bot
parent f22a6cfca2
commit f24ebb324a
5 changed files with 60 additions and 9 deletions

View File

@ -1915,11 +1915,27 @@ MaybeLocal<Function> ScriptCompiler::CompileFunctionInContext(
context = factory->NewWithContext(closure, context, extension);
}
i::Handle<i::Object> name_obj;
int line_offset = 0;
int column_offset = 0;
if (!source->resource_name.IsEmpty()) {
name_obj = Utils::OpenHandle(*(source->resource_name));
}
if (!source->resource_line_offset.IsEmpty()) {
line_offset = static_cast<int>(source->resource_line_offset->Value());
}
if (!source->resource_column_offset.IsEmpty()) {
column_offset = static_cast<int>(source->resource_column_offset->Value());
}
i::Handle<i::JSFunction> fun;
has_pending_exception =
!i::Compiler::GetFunctionFromEval(
source_string, outer_info, context, i::SLOPPY,
i::ONLY_SINGLE_FUNCTION_LITERAL, scope_position).ToHandle(&fun);
has_pending_exception = !i::Compiler::GetFunctionFromEval(
source_string, outer_info, context, i::SLOPPY,
i::ONLY_SINGLE_FUNCTION_LITERAL, line_offset,
column_offset - scope_position, name_obj,
source->resource_options).ToHandle(&fun);
if (has_pending_exception) {
isolate->ReportPendingMessages();
}
RETURN_ON_FAILED_EXECUTION(Function);
i::Handle<i::Object> result;

View File

@ -1150,7 +1150,8 @@ static Handle<SharedFunctionInfo> CompileToplevel(CompilationInfo* info) {
MaybeHandle<JSFunction> Compiler::GetFunctionFromEval(
Handle<String> source, Handle<SharedFunctionInfo> outer_info,
Handle<Context> context, LanguageMode language_mode,
ParseRestriction restriction, int scope_position) {
ParseRestriction restriction, int line_offset, int column_offset,
Handle<Object> script_name, ScriptOriginOptions options) {
Isolate* isolate = source->GetIsolate();
int source_length = source->length();
isolate->counters()->total_eval_size()->Increment(source_length);
@ -1159,11 +1160,17 @@ MaybeHandle<JSFunction> Compiler::GetFunctionFromEval(
CompilationCache* compilation_cache = isolate->compilation_cache();
MaybeHandle<SharedFunctionInfo> maybe_shared_info =
compilation_cache->LookupEval(source, outer_info, context, language_mode,
scope_position);
line_offset);
Handle<SharedFunctionInfo> shared_info;
if (!maybe_shared_info.ToHandle(&shared_info)) {
Handle<Script> script = isolate->factory()->NewScript(source);
if (!script_name.is_null()) {
script->set_name(*script_name);
script->set_line_offset(Smi::FromInt(line_offset));
script->set_column_offset(Smi::FromInt(column_offset));
}
script->set_origin_options(options);
Zone zone;
ParseInfo parse_info(&zone, script);
CompilationInfo info(&parse_info);
@ -1190,7 +1197,7 @@ MaybeHandle<JSFunction> Compiler::GetFunctionFromEval(
DCHECK(is_sloppy(language_mode) ||
is_strict(shared_info->language_mode()));
compilation_cache->PutEval(source, outer_info, context, shared_info,
scope_position);
line_offset);
}
} else if (shared_info->ic_age() != isolate->heap()->global_ic_age()) {
shared_info->ResetForNewContext(isolate->heap()->global_ic_age());

View File

@ -648,7 +648,9 @@ class Compiler : public AllStatic {
MUST_USE_RESULT static MaybeHandle<JSFunction> GetFunctionFromEval(
Handle<String> source, Handle<SharedFunctionInfo> outer_info,
Handle<Context> context, LanguageMode language_mode,
ParseRestriction restriction, int scope_position);
ParseRestriction restriction, int line_offset, int column_offset = 0,
Handle<Object> script_name = Handle<Object>(),
ScriptOriginOptions options = ScriptOriginOptions());
// Compile a String source within a context.
static Handle<SharedFunctionInfo> CompileScript(

View File

@ -544,7 +544,7 @@ function GetPositionInLine(message) {
var start_position = %MessageGetStartPosition(message);
var location = script.locationFromPosition(start_position, false);
if (location == null) return -1;
return start_position - location.start;
return location.column;
}

View File

@ -566,6 +566,32 @@ TEST(CompileFunctionInContextNonIdentifierArgs) {
}
TEST(CompileFunctionInContextScriptOrigin) {
CcTest::InitializeVM();
v8::HandleScope scope(CcTest::isolate());
LocalContext env;
v8::ScriptOrigin origin(v8_str("test"),
v8::Integer::New(CcTest::isolate(), 22),
v8::Integer::New(CcTest::isolate(), 41));
v8::ScriptCompiler::Source script_source(v8_str("throw new Error()"), origin);
v8::Local<v8::Function> fun = v8::ScriptCompiler::CompileFunctionInContext(
CcTest::isolate(), &script_source, env.local(), 0, NULL, 0, NULL);
CHECK(!fun.IsEmpty());
v8::TryCatch try_catch;
CcTest::isolate()->SetCaptureStackTraceForUncaughtExceptions(true);
fun->Call(env->Global(), 0, NULL);
CHECK(try_catch.HasCaught());
CHECK(!try_catch.Exception().IsEmpty());
v8::Local<v8::StackTrace> stack =
v8::Exception::GetStackTrace(try_catch.Exception());
CHECK(!stack.IsEmpty());
CHECK(stack->GetFrameCount() > 0);
v8::Local<v8::StackFrame> frame = stack->GetFrame(0);
CHECK_EQ(23, frame->GetLineNumber());
CHECK_EQ(42 + strlen("throw "), static_cast<unsigned>(frame->GetColumn()));
}
#ifdef ENABLE_DISASSEMBLER
static Handle<JSFunction> GetJSFunction(v8::Handle<v8::Object> obj,
const char* property_name) {