Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: src/api.cc

Issue 186723005: New Compilation API, part 1 (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: . Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« src/api.h ('K') | « src/api.h ('k') | src/d8.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 1593 matching lines...) Expand 10 before | Expand all | Expand 10 after
1604 // Copy the data to align it. 1604 // Copy the data to align it.
1605 unsigned* deserialized_data = i::NewArray<unsigned>(deserialized_data_length); 1605 unsigned* deserialized_data = i::NewArray<unsigned>(deserialized_data_length);
1606 i::CopyBytes(reinterpret_cast<char*>(deserialized_data), 1606 i::CopyBytes(reinterpret_cast<char*>(deserialized_data),
1607 data, static_cast<size_t>(length)); 1607 data, static_cast<size_t>(length));
1608 1608
1609 return new i::ScriptDataImpl( 1609 return new i::ScriptDataImpl(
1610 i::Vector<unsigned>(deserialized_data, deserialized_data_length)); 1610 i::Vector<unsigned>(deserialized_data, deserialized_data_length));
1611 } 1611 }
1612 1612
1613 1613
1614 // --- S c r i p t --- 1614 // --- S c r i p t s ---
1615 1615
1616 1616
1617 Local<Script> Script::New(v8::Handle<String> source, 1617 // Internally, ContextUnboundScript is a SharedFunctionInfo, and Script is a
1618 v8::ScriptOrigin* origin, 1618 // JSFunction.
1619 v8::ScriptData* pre_data) { 1619
1620 i::Handle<i::String> str = Utils::OpenHandle(*source); 1620 ScriptCompiler::Source::Source(Local<String> string, const ScriptOrigin& origin,
1621 i::Isolate* isolate = str->GetIsolate(); 1621 const CachedData& data)
1622 ON_BAILOUT(isolate, "v8::Script::New()", return Local<Script>()); 1622 : source_string(string),
1623 LOG_API(isolate, "Script::New"); 1623 resource_name(origin.ResourceName()),
1624 ENTER_V8(isolate); 1624 resource_line_offset(origin.ResourceLineOffset()),
1625 i::SharedFunctionInfo* raw_result = NULL; 1625 resource_column_offset(origin.ResourceColumnOffset()),
1626 { i::HandleScope scope(isolate); 1626 resource_is_shared_cross_origin(origin.ResourceIsSharedCrossOrigin()),
1627 i::Handle<i::Object> name_obj; 1627 cached_data(data) {}
1628 int line_offset = 0; 1628
1629 int column_offset = 0; 1629
1630 bool is_shared_cross_origin = false; 1630 ScriptCompiler::Source::Source(Local<String> string,
1631 if (origin != NULL) { 1631 const CachedData& data)
1632 if (!origin->ResourceName().IsEmpty()) { 1632 : source_string(string), cached_data(data) {}
1633 name_obj = Utils::OpenHandle(*origin->ResourceName()); 1633
1634 } 1634
1635 if (!origin->ResourceLineOffset().IsEmpty()) { 1635 Local<Script> ContextUnboundScript::BindToGlobalContext() {
1636 line_offset = static_cast<int>(origin->ResourceLineOffset()->Value()); 1636 i::Handle<i::HeapObject> obj =
1637 } 1637 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this));
1638 if (!origin->ResourceColumnOffset().IsEmpty()) { 1638 i::Handle<i::SharedFunctionInfo>
1639 column_offset = 1639 function_info(i::SharedFunctionInfo::cast(*obj), obj->GetIsolate());
1640 static_cast<int>(origin->ResourceColumnOffset()->Value()); 1640 i::Handle<i::JSFunction> function =
1641 } 1641 obj->GetIsolate()->factory()->NewFunctionFromSharedFunctionInfo(
1642 if (!origin->ResourceIsSharedCrossOrigin().IsEmpty()) { 1642 function_info, obj->GetIsolate()->global_context());
1643 v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate); 1643 return ToApiHandle<Script>(function);
1644 is_shared_cross_origin =
1645 origin->ResourceIsSharedCrossOrigin() == v8::True(v8_isolate);
1646 }
1647 }
1648 EXCEPTION_PREAMBLE(isolate);
1649 i::ScriptDataImpl* pre_data_impl =
1650 static_cast<i::ScriptDataImpl*>(pre_data);
1651 // We assert that the pre-data is sane, even though we can actually
1652 // handle it if it turns out not to be in release mode.
1653 ASSERT(pre_data_impl == NULL || pre_data_impl->SanityCheck());
1654 // If the pre-data isn't sane we simply ignore it
1655 if (pre_data_impl != NULL && !pre_data_impl->SanityCheck()) {
1656 pre_data_impl = NULL;
1657 }
1658 i::Handle<i::SharedFunctionInfo> result =
1659 i::Compiler::CompileScript(str,
1660 name_obj,
1661 line_offset,
1662 column_offset,
1663 is_shared_cross_origin,
1664 isolate->global_context(),
1665 NULL,
1666 pre_data_impl,
1667 i::NOT_NATIVES_CODE);
1668 has_pending_exception = result.is_null();
1669 EXCEPTION_BAILOUT_CHECK(isolate, Local<Script>());
1670 raw_result = *result;
1671 }
1672 i::Handle<i::SharedFunctionInfo> result(raw_result, isolate);
1673 return ToApiHandle<Script>(result);
1674 } 1644 }
1675 1645
1676 1646
1677 Local<Script> Script::New(v8::Handle<String> source, 1647 int ContextUnboundScript::GetId() {
1678 v8::Handle<Value> file_name) { 1648 i::Handle<i::HeapObject> obj =
1679 ScriptOrigin origin(file_name); 1649 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this));
1680 return New(source, &origin); 1650 i::Isolate* isolate = obj->GetIsolate();
1651 ON_BAILOUT(isolate, "v8::ContextUnboundScript::GetId()", return -1);
1652 LOG_API(isolate, "v8::ContextUnboundScript::GetId");
1653 {
1654 i::HandleScope scope(isolate);
1655 i::Handle<i::SharedFunctionInfo> function_info(
1656 i::SharedFunctionInfo::cast(*obj));
1657 i::Handle<i::Script> script(i::Script::cast(function_info->script()));
1658 return script->id()->value();
1659 }
1681 } 1660 }
1682 1661
1683 1662
1684 Local<Script> Script::Compile(v8::Handle<String> source, 1663 int ContextUnboundScript::GetLineNumber(int code_pos) {
1685 v8::ScriptOrigin* origin, 1664 i::Handle<i::HeapObject> obj =
1686 v8::ScriptData* pre_data) { 1665 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this));
1687 i::Handle<i::String> str = Utils::OpenHandle(*source); 1666 i::Isolate* isolate = obj->GetIsolate();
1688 i::Isolate* isolate = str->GetIsolate(); 1667 ON_BAILOUT(isolate, "v8::ContextUnboundScript::GetLineNumber()", return -1);
1689 ON_BAILOUT(isolate, "v8::Script::Compile()", return Local<Script>()); 1668 LOG_API(isolate, "ContextUnboundScript::GetLineNumber");
1690 LOG_API(isolate, "Script::Compile"); 1669 if (obj->IsScript()) {
1691 ENTER_V8(isolate); 1670 i::Handle<i::Script> script(i::Script::cast(*obj));
1692 Local<Script> generic = New(source, origin, pre_data); 1671 return i::GetScriptLineNumber(script, code_pos);
1693 if (generic.IsEmpty()) 1672 } else {
1694 return generic; 1673 return -1;
1695 i::Handle<i::Object> obj = Utils::OpenHandle(*generic); 1674 }
1696 i::Handle<i::SharedFunctionInfo> function =
1697 i::Handle<i::SharedFunctionInfo>(i::SharedFunctionInfo::cast(*obj));
1698 i::Handle<i::JSFunction> result =
1699 isolate->factory()->NewFunctionFromSharedFunctionInfo(
1700 function,
1701 isolate->global_context());
1702 return ToApiHandle<Script>(result);
1703 } 1675 }
1704 1676
1705 1677
1706 Local<Script> Script::Compile(v8::Handle<String> source, 1678 Handle<Value> ContextUnboundScript::GetScriptName() {
1707 v8::Handle<Value> file_name) { 1679 i::Handle<i::HeapObject> obj =
1708 ScriptOrigin origin(file_name); 1680 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this));
1709 return Compile(source, &origin); 1681 i::Isolate* isolate = obj->GetIsolate();
1682 ON_BAILOUT(isolate, "v8::ContextUnboundScript::GetName()",
1683 return Handle<String>());
1684 LOG_API(isolate, "ContextUnboundScript::GetName");
1685 if (obj->IsScript()) {
1686 i::Object* name = i::Script::cast(*obj)->name();
1687 return Utils::ToLocal(i::Handle<i::Object>(name, isolate));
1688 } else {
1689 return Handle<String>();
1690 }
1691 }
1692
1693
1694 Local<Value> ContextUnboundScript::Run() {
1695 // If execution is terminating, CompileContextUnbound(..)->Run() requires this
1696 // check.
1697 if (this == NULL) return Local<Value>();
1698 return BindToGlobalContext()->Run();
1710 } 1699 }
1711 1700
1712 1701
1713 Local<Value> Script::Run() { 1702 Local<Value> Script::Run() {
1714 // If execution is terminating, Compile(script)->Run() requires this check. 1703 // If execution is terminating, CompileContextBound(..)->Run() requires this
1704 // check.
1715 if (this == NULL) return Local<Value>(); 1705 if (this == NULL) return Local<Value>();
1716 i::Handle<i::HeapObject> obj = 1706 i::Handle<i::HeapObject> obj =
1717 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this)); 1707 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this));
1718 i::Isolate* isolate = obj->GetIsolate(); 1708 i::Isolate* isolate = obj->GetIsolate();
1719 ON_BAILOUT(isolate, "v8::Script::Run()", return Local<Value>()); 1709 ON_BAILOUT(isolate, "v8::Script::Run()", return Local<Value>());
1720 LOG_API(isolate, "Script::Run"); 1710 LOG_API(isolate, "Script::Run");
1721 ENTER_V8(isolate); 1711 ENTER_V8(isolate);
1722 i::Logger::TimerEventScope timer_scope( 1712 i::Logger::TimerEventScope timer_scope(
1723 isolate, i::Logger::TimerEventScope::v8_execute); 1713 isolate, i::Logger::TimerEventScope::v8_execute);
1724 i::Object* raw_result = NULL; 1714 i::Object* raw_result = NULL;
1725 { 1715 {
1726 i::HandleScope scope(isolate); 1716 i::HandleScope scope(isolate);
1727 i::Handle<i::JSFunction> fun; 1717 i::Handle<i::JSFunction> fun =
1728 if (obj->IsSharedFunctionInfo()) { 1718 i::Handle<i::JSFunction>(i::JSFunction::cast(*obj), isolate);
1729 i::Handle<i::SharedFunctionInfo>
1730 function_info(i::SharedFunctionInfo::cast(*obj), isolate);
1731 fun = isolate->factory()->NewFunctionFromSharedFunctionInfo(
1732 function_info, isolate->global_context());
1733 } else {
1734 fun = i::Handle<i::JSFunction>(i::JSFunction::cast(*obj), isolate);
1735 }
1736 EXCEPTION_PREAMBLE(isolate); 1719 EXCEPTION_PREAMBLE(isolate);
1737 i::Handle<i::Object> receiver( 1720 i::Handle<i::Object> receiver(
1738 isolate->context()->global_proxy(), isolate); 1721 isolate->context()->global_proxy(), isolate);
1739 i::Handle<i::Object> result = i::Execution::Call( 1722 i::Handle<i::Object> result = i::Execution::Call(
1740 isolate, fun, receiver, 0, NULL, &has_pending_exception); 1723 isolate, fun, receiver, 0, NULL, &has_pending_exception);
1741 EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, Local<Value>()); 1724 EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, Local<Value>());
1742 raw_result = *result; 1725 raw_result = *result;
1743 } 1726 }
1744 i::Handle<i::Object> result(raw_result, isolate); 1727 i::Handle<i::Object> result(raw_result, isolate);
1745 return Utils::ToLocal(result); 1728 return Utils::ToLocal(result);
1746 } 1729 }
1747 1730
1748 1731
1749 static i::Handle<i::SharedFunctionInfo> OpenScript(Script* script) { 1732 Local<ContextUnboundScript> Script::GetUnboundScript() {
1750 i::Handle<i::Object> obj = Utils::OpenHandle(script); 1733 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1751 i::Handle<i::SharedFunctionInfo> result; 1734 return ToApiHandle<ContextUnboundScript>(
1752 if (obj->IsSharedFunctionInfo()) { 1735 i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared()));
1753 result =
1754 i::Handle<i::SharedFunctionInfo>(i::SharedFunctionInfo::cast(*obj));
1755 } else {
1756 result =
1757 i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared());
1758 }
1759 return result;
1760 } 1736 }
1761 1737
1762 1738
1763 int Script::GetId() { 1739 Local<ContextUnboundScript> ScriptCompiler::CompileContextUnbound(
1764 i::Handle<i::HeapObject> obj = 1740 Isolate* v8_isolate,
1765 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this)); 1741 const Source& source,
1766 i::Isolate* isolate = obj->GetIsolate(); 1742 CompileOptions options) {
1767 ON_BAILOUT(isolate, "v8::Script::Id()", return -1); 1743 // FIXME(marja): This function cannot yet create cached data (if
1768 LOG_API(isolate, "Script::Id"); 1744 // options.produce_data_to_cache is true), but the PreCompile function is
1769 { 1745 // still there for doing it.
1770 i::HandleScope scope(isolate); 1746 i::Handle<i::String> str = Utils::OpenHandle(*(source.source_string));
1771 i::Handle<i::SharedFunctionInfo> function_info = OpenScript(this); 1747 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
1772 i::Handle<i::Script> script(i::Script::cast(function_info->script())); 1748 ON_BAILOUT(isolate, "v8::ScriptCompiler::CompileContextUnbound()",
1773 return script->id()->value(); 1749 return Local<ContextUnboundScript>());
1750 LOG_API(isolate, "ScriptCompiler::CompileContextUnbound");
1751 ENTER_V8(isolate);
1752 i::SharedFunctionInfo* raw_result = NULL;
1753 { i::HandleScope scope(isolate);
1754 i::Handle<i::Object> name_obj;
1755 int line_offset = 0;
1756 int column_offset = 0;
1757 bool is_shared_cross_origin = false;
1758 if (!source.resource_name.IsEmpty()) {
1759 name_obj = Utils::OpenHandle(*source.resource_name);
1760 }
1761 if (!source.resource_line_offset.IsEmpty()) {
1762 line_offset = static_cast<int>(source.resource_line_offset->Value());
1763 }
1764 if (!source.resource_column_offset.IsEmpty()) {
1765 column_offset =
1766 static_cast<int>(source.resource_column_offset->Value());
1767 }
1768 if (!source.resource_is_shared_cross_origin.IsEmpty()) {
1769 v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
1770 is_shared_cross_origin =
1771 source.resource_is_shared_cross_origin == v8::True(v8_isolate);
1772 }
1773 EXCEPTION_PREAMBLE(isolate);
1774 i::ScriptDataImpl* pre_data_impl = NULL;
1775 if (source.cached_data.data) {
1776 // FIXME(marja): Make compiler use CachedData directly.
1777 pre_data_impl = static_cast<i::ScriptDataImpl*>(ScriptData::New(
1778 reinterpret_cast<const char*>(source.cached_data.data),
1779 source.cached_data.length));
1780 }
1781 // We assert that the pre-data is sane, even though we can actually
1782 // handle it if it turns out not to be in release mode.
1783 ASSERT(pre_data_impl == NULL || pre_data_impl->SanityCheck());
1784 // If the pre-data isn't sane we simply ignore it
1785 if (pre_data_impl != NULL && !pre_data_impl->SanityCheck()) {
1786 delete pre_data_impl;
1787 pre_data_impl = NULL;
1788 }
1789 i::Handle<i::SharedFunctionInfo> result =
1790 i::Compiler::CompileScript(str,
1791 name_obj,
1792 line_offset,
1793 column_offset,
1794 is_shared_cross_origin,
1795 isolate->global_context(),
1796 NULL,
1797 pre_data_impl,
1798 i::NOT_NATIVES_CODE);
1799 has_pending_exception = result.is_null();
1800 EXCEPTION_BAILOUT_CHECK(isolate, Local<ContextUnboundScript>());
1801 raw_result = *result;
1802 delete pre_data_impl;
1774 } 1803 }
1804 i::Handle<i::SharedFunctionInfo> result(raw_result, isolate);
1805 return ToApiHandle<ContextUnboundScript>(result);
1775 } 1806 }
1776 1807
1777 1808
1778 int Script::GetLineNumber(int code_pos) { 1809 Local<Script> ScriptCompiler::CompileContextBound(
1779 i::Handle<i::HeapObject> obj = 1810 Isolate* v8_isolate,
1780 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this)); 1811 const Source& source,
1781 i::Isolate* isolate = obj->GetIsolate(); 1812 CompileOptions options) {
1782 ON_BAILOUT(isolate, "v8::Script::GetLineNumber()", return -1); 1813 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
1783 LOG_API(isolate, "Script::GetLineNumber"); 1814 ON_BAILOUT(isolate, "v8::ScriptCompiler::CompileContextBound()",
1784 if (obj->IsScript()) { 1815 return Local<Script>());
1785 i::Handle<i::Script> script = i::Handle<i::Script>(i::Script::cast(*obj)); 1816 LOG_API(isolate, "ScriptCompiler::CompileContextBound()");
1786 return i::GetScriptLineNumber(script, code_pos); 1817 ENTER_V8(isolate);
1787 } else { 1818 Local<ContextUnboundScript> generic =
1788 return -1; 1819 CompileContextUnbound(v8_isolate, source, options);
1789 } 1820 if (generic.IsEmpty()) return Local<Script>();
1821 return generic->BindToGlobalContext();
1790 } 1822 }
1791 1823
1792 1824
1793 Handle<Value> Script::GetScriptName() { 1825 Local<Script> Script::Compile(v8::Handle<String> source,
1794 i::Handle<i::HeapObject> obj = 1826 v8::ScriptOrigin* origin) {
1795 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this)); 1827 i::Handle<i::String> str = Utils::OpenHandle(*source);
1796 i::Isolate* isolate = obj->GetIsolate(); 1828 if (origin) {
1797 ON_BAILOUT(isolate, "v8::Script::GetName()", return Handle<String>()); 1829 return ScriptCompiler::CompileContextBound(
1798 LOG_API(isolate, "Script::GetName"); 1830 reinterpret_cast<v8::Isolate*>(str->GetIsolate()),
1799 if (obj->IsScript()) { 1831 ScriptCompiler::Source(source, *origin));
1800 i::Object* name = i::Script::cast(*obj)->name();
1801 return Utils::ToLocal(i::Handle<i::Object>(name, isolate));
1802 } else {
1803 return Handle<String>();
1804 } 1832 }
1833 return ScriptCompiler::CompileContextBound(
1834 reinterpret_cast<v8::Isolate*>(str->GetIsolate()),
1835 ScriptCompiler::Source(source));
1805 } 1836 }
1806 1837
1807 1838
1808 // --- E x c e p t i o n s --- 1839 // --- E x c e p t i o n s ---
1809 1840
1810 1841
1811 v8::TryCatch::TryCatch() 1842 v8::TryCatch::TryCatch()
1812 : isolate_(i::Isolate::Current()), 1843 : isolate_(i::Isolate::Current()),
1813 next_(isolate_->try_catch_handler_address()), 1844 next_(isolate_->try_catch_handler_address()),
1814 is_verbose_(false), 1845 is_verbose_(false),
(...skipping 5527 matching lines...) Expand 10 before | Expand all | Expand 10 after
7342 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate()); 7373 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
7343 Address callback_address = 7374 Address callback_address =
7344 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); 7375 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
7345 VMState<EXTERNAL> state(isolate); 7376 VMState<EXTERNAL> state(isolate);
7346 ExternalCallbackScope call_scope(isolate, callback_address); 7377 ExternalCallbackScope call_scope(isolate, callback_address);
7347 callback(info); 7378 callback(info);
7348 } 7379 }
7349 7380
7350 7381
7351 } } // namespace v8::internal 7382 } } // namespace v8::internal
OLDNEW
« src/api.h ('K') | « src/api.h ('k') | src/d8.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698