Reviewers: jochen, Yang,

Description:
Take the ScriptOrigin into account for CompileFunctionInContext

[email protected],[email protected]
LOG=n
BUG=

Please review this at https://codereview.chromium.org/1233563005/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+60, -9 lines):
  M src/api.cc
  M src/compiler.h
  M src/compiler.cc
  M src/messages.js
  M test/cctest/test-compiler.cc


Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index e2abb98ff92dd948e9288e1ba7a188c837122a8d..2697e098c5bc704a1ce73ee9bcbb2c90a8448c5a 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -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;
Index: src/compiler.cc
diff --git a/src/compiler.cc b/src/compiler.cc
index 147adb0511342564bffbe76341409bcf9f7e2741..ab0a620bedc007000931dd8b43529baaec61d2c0 100644
--- a/src/compiler.cc
+++ b/src/compiler.cc
@@ -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());
Index: src/compiler.h
diff --git a/src/compiler.h b/src/compiler.h
index 45863f6b28d2a4537ae8579692b48f0c08963435..1f5a84e8b00a5bc06564ba196c5537546897299c 100644
--- a/src/compiler.h
+++ b/src/compiler.h
@@ -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(
Index: src/messages.js
diff --git a/src/messages.js b/src/messages.js
index d7ca7cd6479bd85bbdecf3647dc941ae4c9460a1..2f3e5edf945dafde755e5fe9eed94660b11815b9 100644
--- a/src/messages.js
+++ b/src/messages.js
@@ -543,7 +543,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;
 }


Index: test/cctest/test-compiler.cc
diff --git a/test/cctest/test-compiler.cc b/test/cctest/test-compiler.cc
index 3b25480a4aa784687daacb44cdc7ea90bbd74d0e..038d4d76e7c9352df33a994980213d9e9a527111 100644
--- a/test/cctest/test-compiler.cc
+++ b/test/cctest/test-compiler.cc
@@ -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) {


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to