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 |
(...skipping 1633 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1644 return Local<Script>()); | 1644 return Local<Script>()); |
1645 LOG_API(isolate, "ScriptCompiler::CompileModule()"); | 1645 LOG_API(isolate, "ScriptCompiler::CompileModule()"); |
1646 ENTER_V8(isolate); | 1646 ENTER_V8(isolate); |
1647 Local<UnboundScript> generic = | 1647 Local<UnboundScript> generic = |
1648 CompileUnboundInternal(v8_isolate, source, options, true); | 1648 CompileUnboundInternal(v8_isolate, source, options, true); |
1649 if (generic.IsEmpty()) return Local<Script>(); | 1649 if (generic.IsEmpty()) return Local<Script>(); |
1650 return generic->BindToCurrentContext(); | 1650 return generic->BindToCurrentContext(); |
1651 } | 1651 } |
1652 | 1652 |
1653 | 1653 |
| 1654 class IsIdentifierHelper { |
| 1655 public: |
| 1656 IsIdentifierHelper() : is_identifier_(false), first_char_(true) {} |
| 1657 |
| 1658 bool Check(i::String* string) { |
| 1659 i::ConsString* cons_string = i::String::VisitFlat(this, string, 0); |
| 1660 if (cons_string == NULL) return is_identifier_; |
| 1661 // We don't support cons strings here. |
| 1662 return false; |
| 1663 } |
| 1664 void VisitOneByteString(const uint8_t* chars, int length) { |
| 1665 for (int i = 0; i < length; ++i) { |
| 1666 if (first_char_) { |
| 1667 first_char_ = false; |
| 1668 is_identifier_ = unicode_cache_.IsIdentifierStart(chars[0]); |
| 1669 } else { |
| 1670 is_identifier_ &= unicode_cache_.IsIdentifierPart(chars[i]); |
| 1671 } |
| 1672 } |
| 1673 } |
| 1674 void VisitTwoByteString(const uint16_t* chars, int length) { |
| 1675 for (int i = 0; i < length; ++i) { |
| 1676 if (first_char_) { |
| 1677 first_char_ = false; |
| 1678 is_identifier_ = unicode_cache_.IsIdentifierStart(chars[0]); |
| 1679 } else { |
| 1680 is_identifier_ &= unicode_cache_.IsIdentifierPart(chars[i]); |
| 1681 } |
| 1682 } |
| 1683 } |
| 1684 |
| 1685 private: |
| 1686 bool is_identifier_; |
| 1687 bool first_char_; |
| 1688 i::UnicodeCache unicode_cache_; |
| 1689 DISALLOW_COPY_AND_ASSIGN(IsIdentifierHelper); |
| 1690 }; |
| 1691 |
| 1692 |
1654 Local<Function> ScriptCompiler::CompileFunctionInContext( | 1693 Local<Function> ScriptCompiler::CompileFunctionInContext( |
1655 Isolate* v8_isolate, Source* source, Local<Context> v8_context, | 1694 Isolate* v8_isolate, Source* source, Local<Context> v8_context, |
| 1695 size_t arguments_count, Local<String> arguments[], |
1656 size_t context_extension_count, Local<Object> context_extensions[]) { | 1696 size_t context_extension_count, Local<Object> context_extensions[]) { |
1657 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); | 1697 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); |
1658 ON_BAILOUT(isolate, "v8::ScriptCompiler::CompileFunctionInContext()", | 1698 ON_BAILOUT(isolate, "v8::ScriptCompiler::CompileFunctionInContext()", |
1659 return Local<Function>()); | 1699 return Local<Function>()); |
1660 LOG_API(isolate, "ScriptCompiler::CompileFunctionInContext()"); | 1700 LOG_API(isolate, "ScriptCompiler::CompileFunctionInContext()"); |
1661 ENTER_V8(isolate); | 1701 ENTER_V8(isolate); |
| 1702 |
| 1703 i::Handle<i::String> source_string; |
| 1704 if (arguments_count) { |
| 1705 source_string = |
| 1706 Utils::OpenHandle(*v8::String::NewFromUtf8(v8_isolate, "(function(")); |
| 1707 for (size_t i = 0; i < arguments_count; ++i) { |
| 1708 IsIdentifierHelper helper; |
| 1709 if (!helper.Check(*Utils::OpenHandle(*arguments[i]))) { |
| 1710 return Local<Function>(); |
| 1711 } |
| 1712 i::MaybeHandle<i::String> maybe_source = |
| 1713 isolate->factory()->NewConsString(source_string, |
| 1714 Utils::OpenHandle(*arguments[i])); |
| 1715 if (!maybe_source.ToHandle(&source_string)) { |
| 1716 return Local<Function>(); |
| 1717 } |
| 1718 if (i + 1 == arguments_count) continue; |
| 1719 maybe_source = isolate->factory()->NewConsString( |
| 1720 source_string, |
| 1721 isolate->factory()->LookupSingleCharacterStringFromCode(',')); |
| 1722 if (!maybe_source.ToHandle(&source_string)) { |
| 1723 return Local<Function>(); |
| 1724 } |
| 1725 } |
| 1726 i::Handle<i::String> brackets = |
| 1727 Utils::OpenHandle(*v8::String::NewFromUtf8(v8_isolate, "){")); |
| 1728 i::MaybeHandle<i::String> maybe_source = |
| 1729 isolate->factory()->NewConsString(source_string, brackets); |
| 1730 if (!maybe_source.ToHandle(&source_string)) { |
| 1731 return Local<Function>(); |
| 1732 } |
| 1733 } else { |
| 1734 source_string = |
| 1735 Utils::OpenHandle(*v8::String::NewFromUtf8(v8_isolate, "(function(){")); |
| 1736 } |
| 1737 |
| 1738 int scope_position = source_string->length(); |
| 1739 i::MaybeHandle<i::String> maybe_source = isolate->factory()->NewConsString( |
| 1740 source_string, Utils::OpenHandle(*source->source_string)); |
| 1741 if (!maybe_source.ToHandle(&source_string)) { |
| 1742 return Local<Function>(); |
| 1743 } |
| 1744 // Include \n in case the source contains a line end comment. |
| 1745 i::Handle<i::String> brackets = |
| 1746 Utils::OpenHandle(*v8::String::NewFromUtf8(v8_isolate, "\n})")); |
| 1747 maybe_source = isolate->factory()->NewConsString(source_string, brackets); |
| 1748 if (!maybe_source.ToHandle(&source_string)) { |
| 1749 return Local<Function>(); |
| 1750 } |
| 1751 |
1662 i::Handle<i::Context> context = Utils::OpenHandle(*v8_context); | 1752 i::Handle<i::Context> context = Utils::OpenHandle(*v8_context); |
1663 i::Handle<i::SharedFunctionInfo> outer_info(context->closure()->shared(), | 1753 i::Handle<i::SharedFunctionInfo> outer_info(context->closure()->shared(), |
1664 isolate); | 1754 isolate); |
1665 for (size_t i = 0; i < context_extension_count; ++i) { | 1755 for (size_t i = 0; i < context_extension_count; ++i) { |
1666 i::Handle<i::JSObject> extension = | 1756 i::Handle<i::JSObject> extension = |
1667 Utils::OpenHandle(*context_extensions[i]); | 1757 Utils::OpenHandle(*context_extensions[i]); |
1668 i::Handle<i::JSFunction> closure(context->closure(), isolate); | 1758 i::Handle<i::JSFunction> closure(context->closure(), isolate); |
1669 context = isolate->factory()->NewWithContext(closure, context, extension); | 1759 context = isolate->factory()->NewWithContext(closure, context, extension); |
1670 } | 1760 } |
| 1761 |
1671 EXCEPTION_PREAMBLE(isolate); | 1762 EXCEPTION_PREAMBLE(isolate); |
1672 i::MaybeHandle<i::JSFunction> result = i::Compiler::GetFunctionFromEval( | 1763 i::MaybeHandle<i::JSFunction> maybe_fun = i::Compiler::GetFunctionFromEval( |
1673 Utils::OpenHandle(*source->source_string), outer_info, context, i::SLOPPY, | 1764 source_string, outer_info, context, i::SLOPPY, |
1674 i::NO_PARSE_RESTRICTION, 0 /* scope_position */); | 1765 i::ONLY_SINGLE_FUNCTION_LITERAL, scope_position); |
1675 has_pending_exception = result.is_null(); | 1766 i::Handle<i::JSFunction> fun; |
| 1767 has_pending_exception = !maybe_fun.ToHandle(&fun); |
1676 EXCEPTION_BAILOUT_CHECK(isolate, Local<Function>()); | 1768 EXCEPTION_BAILOUT_CHECK(isolate, Local<Function>()); |
1677 return Utils::ToLocal(result.ToHandleChecked()); | 1769 |
| 1770 i::MaybeHandle<i::Object> result = i::Execution::Call( |
| 1771 isolate, fun, Utils::OpenHandle(*v8_context->Global()), 0, NULL); |
| 1772 i::Handle<i::Object> final_result; |
| 1773 has_pending_exception = !result.ToHandle(&final_result); |
| 1774 EXCEPTION_BAILOUT_CHECK(isolate, Local<Function>()); |
| 1775 return Utils::ToLocal(i::Handle<i::JSFunction>::cast(final_result)); |
1678 } | 1776 } |
1679 | 1777 |
1680 | 1778 |
1681 ScriptCompiler::ScriptStreamingTask* ScriptCompiler::StartStreamingScript( | 1779 ScriptCompiler::ScriptStreamingTask* ScriptCompiler::StartStreamingScript( |
1682 Isolate* v8_isolate, StreamedSource* source, CompileOptions options) { | 1780 Isolate* v8_isolate, StreamedSource* source, CompileOptions options) { |
1683 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); | 1781 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); |
1684 return new i::BackgroundParsingTask(source->impl(), options, | 1782 return new i::BackgroundParsingTask(source->impl(), options, |
1685 i::FLAG_stack_size, isolate); | 1783 i::FLAG_stack_size, isolate); |
1686 } | 1784 } |
1687 | 1785 |
(...skipping 6051 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7739 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate()); | 7837 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate()); |
7740 Address callback_address = | 7838 Address callback_address = |
7741 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); | 7839 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); |
7742 VMState<EXTERNAL> state(isolate); | 7840 VMState<EXTERNAL> state(isolate); |
7743 ExternalCallbackScope call_scope(isolate, callback_address); | 7841 ExternalCallbackScope call_scope(isolate, callback_address); |
7744 callback(info); | 7842 callback(info); |
7745 } | 7843 } |
7746 | 7844 |
7747 | 7845 |
7748 } } // namespace v8::internal | 7846 } } // namespace v8::internal |
OLD | NEW |