OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/api.h" | 5 #include "src/api.h" |
6 | 6 |
7 #include <string.h> // For memcpy, strlen. | 7 #include <string.h> // For memcpy, strlen. |
8 #ifdef V8_USE_ADDRESS_SANITIZER | 8 #ifdef V8_USE_ADDRESS_SANITIZER |
9 #include <sanitizer/asan_interface.h> | 9 #include <sanitizer/asan_interface.h> |
10 #endif // V8_USE_ADDRESS_SANITIZER | 10 #endif // V8_USE_ADDRESS_SANITIZER |
11 #include <cmath> // For isnan. | 11 #include <cmath> // For isnan. |
12 #include "include/v8-debug.h" | 12 #include "include/v8-debug.h" |
13 #include "include/v8-profiler.h" | 13 #include "include/v8-profiler.h" |
14 #include "include/v8-testing.h" | 14 #include "include/v8-testing.h" |
15 #include "src/assert-scope.h" | 15 #include "src/assert-scope.h" |
| 16 #include "src/background-parsing-task.h" |
16 #include "src/base/platform/platform.h" | 17 #include "src/base/platform/platform.h" |
17 #include "src/base/platform/time.h" | 18 #include "src/base/platform/time.h" |
18 #include "src/base/utils/random-number-generator.h" | 19 #include "src/base/utils/random-number-generator.h" |
19 #include "src/bootstrapper.h" | 20 #include "src/bootstrapper.h" |
20 #include "src/code-stubs.h" | 21 #include "src/code-stubs.h" |
21 #include "src/compiler.h" | 22 #include "src/compiler.h" |
22 #include "src/conversions-inl.h" | 23 #include "src/conversions-inl.h" |
23 #include "src/counters.h" | 24 #include "src/counters.h" |
24 #include "src/cpu-profiler.h" | 25 #include "src/cpu-profiler.h" |
25 #include "src/debug.h" | 26 #include "src/debug.h" |
(...skipping 1567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1593 : data(data_), length(length_), buffer_policy(buffer_policy_) {} | 1594 : data(data_), length(length_), buffer_policy(buffer_policy_) {} |
1594 | 1595 |
1595 | 1596 |
1596 ScriptCompiler::CachedData::~CachedData() { | 1597 ScriptCompiler::CachedData::~CachedData() { |
1597 if (buffer_policy == BufferOwned) { | 1598 if (buffer_policy == BufferOwned) { |
1598 delete[] data; | 1599 delete[] data; |
1599 } | 1600 } |
1600 } | 1601 } |
1601 | 1602 |
1602 | 1603 |
| 1604 ScriptCompiler::StreamedSource::~StreamedSource() { |
| 1605 delete source_stream; |
| 1606 delete cached_data; |
| 1607 delete streaming_data; |
| 1608 } |
| 1609 |
| 1610 |
1603 Local<Script> UnboundScript::BindToCurrentContext() { | 1611 Local<Script> UnboundScript::BindToCurrentContext() { |
1604 i::Handle<i::HeapObject> obj = | 1612 i::Handle<i::HeapObject> obj = |
1605 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this)); | 1613 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this)); |
1606 i::Handle<i::SharedFunctionInfo> | 1614 i::Handle<i::SharedFunctionInfo> |
1607 function_info(i::SharedFunctionInfo::cast(*obj), obj->GetIsolate()); | 1615 function_info(i::SharedFunctionInfo::cast(*obj), obj->GetIsolate()); |
1608 i::Handle<i::JSFunction> function = | 1616 i::Handle<i::JSFunction> function = |
1609 obj->GetIsolate()->factory()->NewFunctionFromSharedFunctionInfo( | 1617 obj->GetIsolate()->factory()->NewFunctionFromSharedFunctionInfo( |
1610 function_info, obj->GetIsolate()->global_context()); | 1618 function_info, obj->GetIsolate()->global_context()); |
1611 return ToApiHandle<Script>(function); | 1619 return ToApiHandle<Script>(function); |
1612 } | 1620 } |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1807 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); | 1815 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); |
1808 ON_BAILOUT(isolate, "v8::ScriptCompiler::Compile()", return Local<Script>()); | 1816 ON_BAILOUT(isolate, "v8::ScriptCompiler::Compile()", return Local<Script>()); |
1809 LOG_API(isolate, "ScriptCompiler::CompiletBound()"); | 1817 LOG_API(isolate, "ScriptCompiler::CompiletBound()"); |
1810 ENTER_V8(isolate); | 1818 ENTER_V8(isolate); |
1811 Local<UnboundScript> generic = CompileUnbound(v8_isolate, source, options); | 1819 Local<UnboundScript> generic = CompileUnbound(v8_isolate, source, options); |
1812 if (generic.IsEmpty()) return Local<Script>(); | 1820 if (generic.IsEmpty()) return Local<Script>(); |
1813 return generic->BindToCurrentContext(); | 1821 return generic->BindToCurrentContext(); |
1814 } | 1822 } |
1815 | 1823 |
1816 | 1824 |
| 1825 ScriptCompiler::ScriptStreamingTask* ScriptCompiler::StartStreamingScript( |
| 1826 Isolate* v8_isolate, StreamedSource* source, CompileOptions options) { |
| 1827 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); |
| 1828 if (!isolate->global_context().is_null() && |
| 1829 !isolate->global_context()->IsNativeContext()) { |
| 1830 // The context chain is non-trivial, and constructing the corresponding |
| 1831 // non-trivial Scope chain outside the V8 heap is not implemented. Don't |
| 1832 // stream the script. This will only occur if Harmony scoping is enabled and |
| 1833 // a previous script has introduced "let" or "const" variables. TODO(marja): |
| 1834 // Implement externalizing ScopeInfos and constructing non-trivial Scope |
| 1835 // chains independent of the V8 heap so that we can stream also in this |
| 1836 // case. |
| 1837 return NULL; |
| 1838 } |
| 1839 |
| 1840 // Prepare the data for the internalization phase and compilation phase, which |
| 1841 // will happen in the main thread after parsing. |
| 1842 i::CompilationInfo* info = |
| 1843 new i::CompilationInfoWithZone(source->source_stream, isolate); |
| 1844 info->MarkAsGlobal(); |
| 1845 |
| 1846 // We don't set the context to the CompilationInfo yet, because the background |
| 1847 // thread cannot do anything with it anyway. We set it just before compilation |
| 1848 // on the foreground thread. |
| 1849 DCHECK(options == kProduceParserCache || options == kProduceCodeCache || |
| 1850 options == kNoCompileOptions); |
| 1851 bool allow_lazy = !i::Compiler::DebuggerWantsEagerCompilation(info); |
| 1852 source->streaming_data = new i::StreamingData( |
| 1853 info, isolate->heap()->HashSeed(), new i::UnicodeCache(), allow_lazy); |
| 1854 i::BackgroundParsingTask* task = new i::BackgroundParsingTask(source); |
| 1855 if (options == kProduceParserCache || options == kProduceCodeCache) { |
| 1856 info->SetCachedData(&(task->script_data_), options); |
| 1857 } |
| 1858 return task; |
| 1859 } |
| 1860 |
| 1861 |
| 1862 Local<Script> ScriptCompiler::Compile(Isolate* v8_isolate, |
| 1863 StreamedSource* source, |
| 1864 Handle<String> full_source_string, |
| 1865 const ScriptOrigin& origin) { |
| 1866 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); |
| 1867 ON_BAILOUT(isolate, "v8::ScriptCompiler::Compile()", return Local<Script>()); |
| 1868 LOG_API(isolate, "ScriptCompiler::Compile()"); |
| 1869 ENTER_V8(isolate); |
| 1870 i::SharedFunctionInfo* raw_result = NULL; |
| 1871 |
| 1872 { |
| 1873 i::HandleScope scope(isolate); |
| 1874 i::Handle<i::String> str = Utils::OpenHandle(*(full_source_string)); |
| 1875 i::Handle<i::Script> script = isolate->factory()->NewScript(str); |
| 1876 if (!origin.ResourceName().IsEmpty()) { |
| 1877 script->set_name(*Utils::OpenHandle(*(origin.ResourceName()))); |
| 1878 } |
| 1879 if (!origin.ResourceLineOffset().IsEmpty()) { |
| 1880 script->set_line_offset(i::Smi::FromInt( |
| 1881 static_cast<int>(origin.ResourceLineOffset()->Value()))); |
| 1882 } |
| 1883 if (!origin.ResourceColumnOffset().IsEmpty()) { |
| 1884 script->set_column_offset(i::Smi::FromInt( |
| 1885 static_cast<int>(origin.ResourceColumnOffset()->Value()))); |
| 1886 } |
| 1887 if (!origin.ResourceIsSharedCrossOrigin().IsEmpty()) { |
| 1888 script->set_is_shared_cross_origin(origin.ResourceIsSharedCrossOrigin() == |
| 1889 v8::True(v8_isolate)); |
| 1890 } |
| 1891 source->streaming_data->info->set_script(script); |
| 1892 source->streaming_data->info->SetContext(isolate->global_context()); |
| 1893 |
| 1894 EXCEPTION_PREAMBLE(isolate); |
| 1895 |
| 1896 // Do the parsing tasks which need to be done on the main thread. This will |
| 1897 // also handle parse errors. |
| 1898 source->streaming_data->parser->Internalize(); |
| 1899 |
| 1900 i::Handle<i::SharedFunctionInfo> result = |
| 1901 i::Handle<i::SharedFunctionInfo>::null(); |
| 1902 if (source->streaming_data->info->function() != NULL) { |
| 1903 // Parsing has succeeded. |
| 1904 result = i::Compiler::CompileStreamedScript(source->streaming_data->info, |
| 1905 str->length()); |
| 1906 } |
| 1907 has_pending_exception = result.is_null(); |
| 1908 if (has_pending_exception) isolate->ReportPendingMessages(); |
| 1909 EXCEPTION_BAILOUT_CHECK(isolate, Local<Script>()); |
| 1910 |
| 1911 raw_result = *result; |
| 1912 // The Handle<Script> will go out of scope soon; make sure CompilationInfo |
| 1913 // doesn't point to it. |
| 1914 source->streaming_data->info->set_script(i::Handle<i::Script>()); |
| 1915 } // HandleScope goes out of scope. |
| 1916 i::Handle<i::SharedFunctionInfo> result(raw_result, isolate); |
| 1917 Local<UnboundScript> generic = ToApiHandle<UnboundScript>(result); |
| 1918 if (generic.IsEmpty()) { |
| 1919 return Local<Script>(); |
| 1920 } |
| 1921 return generic->BindToCurrentContext(); |
| 1922 } |
| 1923 |
| 1924 |
1817 Local<Script> Script::Compile(v8::Handle<String> source, | 1925 Local<Script> Script::Compile(v8::Handle<String> source, |
1818 v8::ScriptOrigin* origin) { | 1926 v8::ScriptOrigin* origin) { |
1819 i::Handle<i::String> str = Utils::OpenHandle(*source); | 1927 i::Handle<i::String> str = Utils::OpenHandle(*source); |
1820 if (origin) { | 1928 if (origin) { |
1821 ScriptCompiler::Source script_source(source, *origin); | 1929 ScriptCompiler::Source script_source(source, *origin); |
1822 return ScriptCompiler::Compile( | 1930 return ScriptCompiler::Compile( |
1823 reinterpret_cast<v8::Isolate*>(str->GetIsolate()), | 1931 reinterpret_cast<v8::Isolate*>(str->GetIsolate()), |
1824 &script_source); | 1932 &script_source); |
1825 } | 1933 } |
1826 ScriptCompiler::Source script_source(source); | 1934 ScriptCompiler::Source script_source(source); |
(...skipping 5799 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7626 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate()); | 7734 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate()); |
7627 Address callback_address = | 7735 Address callback_address = |
7628 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); | 7736 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); |
7629 VMState<EXTERNAL> state(isolate); | 7737 VMState<EXTERNAL> state(isolate); |
7630 ExternalCallbackScope call_scope(isolate, callback_address); | 7738 ExternalCallbackScope call_scope(isolate, callback_address); |
7631 callback(info); | 7739 callback(info); |
7632 } | 7740 } |
7633 | 7741 |
7634 | 7742 |
7635 } } // namespace v8::internal | 7743 } } // namespace v8::internal |
OLD | NEW |