Commit f24ebb32 authored by epertoso's avatar epertoso Committed by Commit bot

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}
parent f22a6cfc
...@@ -1915,11 +1915,27 @@ MaybeLocal<Function> ScriptCompiler::CompileFunctionInContext( ...@@ -1915,11 +1915,27 @@ MaybeLocal<Function> ScriptCompiler::CompileFunctionInContext(
context = factory->NewWithContext(closure, context, extension); 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; i::Handle<i::JSFunction> fun;
has_pending_exception = has_pending_exception = !i::Compiler::GetFunctionFromEval(
!i::Compiler::GetFunctionFromEval( source_string, outer_info, context, i::SLOPPY,
source_string, outer_info, context, i::SLOPPY, i::ONLY_SINGLE_FUNCTION_LITERAL, line_offset,
i::ONLY_SINGLE_FUNCTION_LITERAL, scope_position).ToHandle(&fun); column_offset - scope_position, name_obj,
source->resource_options).ToHandle(&fun);
if (has_pending_exception) {
isolate->ReportPendingMessages();
}
RETURN_ON_FAILED_EXECUTION(Function); RETURN_ON_FAILED_EXECUTION(Function);
i::Handle<i::Object> result; i::Handle<i::Object> result;
......
...@@ -1150,7 +1150,8 @@ static Handle<SharedFunctionInfo> CompileToplevel(CompilationInfo* info) { ...@@ -1150,7 +1150,8 @@ static Handle<SharedFunctionInfo> CompileToplevel(CompilationInfo* info) {
MaybeHandle<JSFunction> Compiler::GetFunctionFromEval( MaybeHandle<JSFunction> Compiler::GetFunctionFromEval(
Handle<String> source, Handle<SharedFunctionInfo> outer_info, Handle<String> source, Handle<SharedFunctionInfo> outer_info,
Handle<Context> context, LanguageMode language_mode, 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(); Isolate* isolate = source->GetIsolate();
int source_length = source->length(); int source_length = source->length();
isolate->counters()->total_eval_size()->Increment(source_length); isolate->counters()->total_eval_size()->Increment(source_length);
...@@ -1159,11 +1160,17 @@ MaybeHandle<JSFunction> Compiler::GetFunctionFromEval( ...@@ -1159,11 +1160,17 @@ MaybeHandle<JSFunction> Compiler::GetFunctionFromEval(
CompilationCache* compilation_cache = isolate->compilation_cache(); CompilationCache* compilation_cache = isolate->compilation_cache();
MaybeHandle<SharedFunctionInfo> maybe_shared_info = MaybeHandle<SharedFunctionInfo> maybe_shared_info =
compilation_cache->LookupEval(source, outer_info, context, language_mode, compilation_cache->LookupEval(source, outer_info, context, language_mode,
scope_position); line_offset);
Handle<SharedFunctionInfo> shared_info; Handle<SharedFunctionInfo> shared_info;
if (!maybe_shared_info.ToHandle(&shared_info)) { if (!maybe_shared_info.ToHandle(&shared_info)) {
Handle<Script> script = isolate->factory()->NewScript(source); 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; Zone zone;
ParseInfo parse_info(&zone, script); ParseInfo parse_info(&zone, script);
CompilationInfo info(&parse_info); CompilationInfo info(&parse_info);
...@@ -1190,7 +1197,7 @@ MaybeHandle<JSFunction> Compiler::GetFunctionFromEval( ...@@ -1190,7 +1197,7 @@ MaybeHandle<JSFunction> Compiler::GetFunctionFromEval(
DCHECK(is_sloppy(language_mode) || DCHECK(is_sloppy(language_mode) ||
is_strict(shared_info->language_mode())); is_strict(shared_info->language_mode()));
compilation_cache->PutEval(source, outer_info, context, shared_info, compilation_cache->PutEval(source, outer_info, context, shared_info,
scope_position); line_offset);
} }
} else if (shared_info->ic_age() != isolate->heap()->global_ic_age()) { } else if (shared_info->ic_age() != isolate->heap()->global_ic_age()) {
shared_info->ResetForNewContext(isolate->heap()->global_ic_age()); shared_info->ResetForNewContext(isolate->heap()->global_ic_age());
......
...@@ -648,7 +648,9 @@ class Compiler : public AllStatic { ...@@ -648,7 +648,9 @@ class Compiler : public AllStatic {
MUST_USE_RESULT static MaybeHandle<JSFunction> GetFunctionFromEval( MUST_USE_RESULT static MaybeHandle<JSFunction> GetFunctionFromEval(
Handle<String> source, Handle<SharedFunctionInfo> outer_info, Handle<String> source, Handle<SharedFunctionInfo> outer_info,
Handle<Context> context, LanguageMode language_mode, 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. // Compile a String source within a context.
static Handle<SharedFunctionInfo> CompileScript( static Handle<SharedFunctionInfo> CompileScript(
......
...@@ -544,7 +544,7 @@ function GetPositionInLine(message) { ...@@ -544,7 +544,7 @@ function GetPositionInLine(message) {
var start_position = %MessageGetStartPosition(message); var start_position = %MessageGetStartPosition(message);
var location = script.locationFromPosition(start_position, false); var location = script.locationFromPosition(start_position, false);
if (location == null) return -1; if (location == null) return -1;
return start_position - location.start; return location.column;
} }
......
...@@ -566,6 +566,32 @@ TEST(CompileFunctionInContextNonIdentifierArgs) { ...@@ -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 #ifdef ENABLE_DISASSEMBLER
static Handle<JSFunction> GetJSFunction(v8::Handle<v8::Object> obj, static Handle<JSFunction> GetJSFunction(v8::Handle<v8::Object> obj,
const char* property_name) { const char* property_name) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment