From e33ae81ce1c13f80533d6c997fd39ab7d6609dfc Mon Sep 17 00:00:00 2001 From: yurys Date: Thu, 5 Mar 2015 05:03:42 -0800 Subject: [PATCH] Allow passing sourceMapUrl when compiling scripts According to Source Map specification [1] source map url can be passed either as a magic comment at the end of script or as SourceMap http header. We already parse the former value and expose it on Script object. This change allows to unify the way we deal with source map urls received in http header by providing api for passing that url into the script being compiled. source_map_url is intentionally not passed into CompilationCacheScript::Lookup. The cache is anyways disabled when debugger is on. [1] https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit LOG=Y BUG=chromium:462572 Review URL: https://codereview.chromium.org/983603003 Cr-Commit-Position: refs/heads/master@{#27017} --- include/v8.h | 13 +++++++-- src/api.cc | 54 +++++++++++++++++++++--------------- src/bootstrapper.cc | 4 +-- src/compiler.cc | 7 +++-- src/compiler.h | 5 ++-- src/debug.cc | 4 +-- test/cctest/compiler/test-linkage.cc | 2 +- test/cctest/test-api.cc | 18 ++++++++---- test/cctest/test-compiler.cc | 2 +- test/cctest/test-serialize.cc | 8 +++--- 10 files changed, 73 insertions(+), 44 deletions(-) diff --git a/include/v8.h b/include/v8.h index 9d60fc6..5f41595 100644 --- a/include/v8.h +++ b/include/v8.h @@ -991,13 +991,15 @@ class ScriptOrigin { Handle resource_column_offset = Handle(), Handle resource_is_shared_cross_origin = Handle(), Handle script_id = Handle(), - Handle resource_is_embedder_debug_script = Handle()) + Handle resource_is_embedder_debug_script = Handle(), + Handle source_map_url = Handle()) : resource_name_(resource_name), resource_line_offset_(resource_line_offset), resource_column_offset_(resource_column_offset), resource_is_embedder_debug_script_(resource_is_embedder_debug_script), resource_is_shared_cross_origin_(resource_is_shared_cross_origin), - script_id_(script_id) {} + script_id_(script_id), + source_map_url_(source_map_url) {} V8_INLINE Handle ResourceName() const; V8_INLINE Handle ResourceLineOffset() const; V8_INLINE Handle ResourceColumnOffset() const; @@ -1007,6 +1009,7 @@ class ScriptOrigin { V8_INLINE Handle ResourceIsEmbedderDebugScript() const; V8_INLINE Handle ResourceIsSharedCrossOrigin() const; V8_INLINE Handle ScriptID() const; + V8_INLINE Handle SourceMapUrl() const; private: Handle resource_name_; @@ -1015,6 +1018,7 @@ class ScriptOrigin { Handle resource_is_embedder_debug_script_; Handle resource_is_shared_cross_origin_; Handle script_id_; + Handle source_map_url_; }; @@ -1164,6 +1168,7 @@ class V8_EXPORT ScriptCompiler { Handle resource_column_offset; Handle resource_is_embedder_debug_script; Handle resource_is_shared_cross_origin; + Handle source_map_url; // Cached data from previous compilation (if a kConsume*Cache flag is // set), or hold newly generated cache data (kProduce*Cache flags) are @@ -7001,6 +7006,9 @@ Handle ScriptOrigin::ScriptID() const { } +Handle ScriptOrigin::SourceMapUrl() const { return source_map_url_; } + + ScriptCompiler::Source::Source(Local string, const ScriptOrigin& origin, CachedData* data) : source_string(string), @@ -7009,6 +7017,7 @@ ScriptCompiler::Source::Source(Local string, const ScriptOrigin& origin, resource_column_offset(origin.ResourceColumnOffset()), resource_is_embedder_debug_script(origin.ResourceIsEmbedderDebugScript()), resource_is_shared_cross_origin(origin.ResourceIsSharedCrossOrigin()), + source_map_url(origin.SourceMapUrl()), cached_data(data) {} diff --git a/src/api.cc b/src/api.cc index ad740d1..7de0d12 100644 --- a/src/api.cc +++ b/src/api.cc @@ -215,6 +215,24 @@ class CallDepthScope { } // namespace +static ScriptOrigin GetScriptOriginForScript(i::Isolate* isolate, + i::Handle script) { + i::Handle scriptName(i::Script::GetNameOrSourceURL(script)); + i::Handle source_map_url(script->source_mapping_url(), isolate); + v8::Isolate* v8_isolate = + reinterpret_cast(script->GetIsolate()); + v8::ScriptOrigin origin( + Utils::ToLocal(scriptName), + v8::Integer::New(v8_isolate, script->line_offset()->value()), + v8::Integer::New(v8_isolate, script->column_offset()->value()), + v8::Boolean::New(v8_isolate, script->is_shared_cross_origin()), + v8::Integer::New(v8_isolate, script->id()->value()), + v8::Boolean::New(v8_isolate, script->is_embedder_debug_script()), + Utils::ToLocal(source_map_url)); + return origin; +} + + // --- E x c e p t i o n B e h a v i o r --- @@ -1630,6 +1648,7 @@ MaybeLocal ScriptCompiler::CompileUnboundInternal( { i::HandleScope scope(isolate); i::HistogramTimerScope total(isolate->counters()->compile_script(), true); i::Handle name_obj; + i::Handle source_map_url; int line_offset = 0; int column_offset = 0; bool is_embedder_debug_script = false; @@ -1652,10 +1671,13 @@ MaybeLocal ScriptCompiler::CompileUnboundInternal( is_embedder_debug_script = source->resource_is_embedder_debug_script->IsTrue(); } + if (!source->source_map_url.IsEmpty()) { + source_map_url = Utils::OpenHandle(*(source->source_map_url)); + } i::Handle result = i::Compiler::CompileScript( str, name_obj, line_offset, column_offset, is_embedder_debug_script, - is_shared_cross_origin, isolate->native_context(), NULL, &script_data, - options, i::NOT_NATIVES_CODE, is_module); + is_shared_cross_origin, source_map_url, isolate->native_context(), NULL, + &script_data, options, i::NOT_NATIVES_CODE, is_module); has_pending_exception = result.is_null(); if (has_pending_exception && script_data != NULL) { // This case won't happen during normal operation; we have compiled @@ -1902,6 +1924,11 @@ MaybeLocal