OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 682 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
693 return Top::TerminateExecution(); | 693 return Top::TerminateExecution(); |
694 } | 694 } |
695 if (StackGuard::IsInterrupted()) { | 695 if (StackGuard::IsInterrupted()) { |
696 // interrupt | 696 // interrupt |
697 StackGuard::Continue(INTERRUPT); | 697 StackGuard::Continue(INTERRUPT); |
698 return Top::StackOverflow(); | 698 return Top::StackOverflow(); |
699 } | 699 } |
700 return Heap::undefined_value(); | 700 return Heap::undefined_value(); |
701 } | 701 } |
702 | 702 |
703 // --- G C E x t e n s i o n --- | |
704 | |
705 const char* const GCExtension::kSource = "native function gc();"; | |
706 | |
707 | |
708 v8::Handle<v8::FunctionTemplate> GCExtension::GetNativeFunction( | |
709 v8::Handle<v8::String> str) { | |
710 return v8::FunctionTemplate::New(GCExtension::GC); | |
711 } | |
712 | |
713 | |
714 v8::Handle<v8::Value> GCExtension::GC(const v8::Arguments& args) { | |
715 // All allocation spaces other than NEW_SPACE have the same effect. | |
716 Heap::CollectAllGarbage(false); | |
717 return v8::Undefined(); | |
718 } | |
719 | |
720 | |
721 static GCExtension gc_extension; | |
722 static v8::DeclareExtension gc_extension_declaration(&gc_extension); | |
723 | |
724 | |
725 // --- E x t e r n a l i z e S t r i n g E x t e n s i o n --- | |
726 | |
727 | |
728 template <typename Char, typename Base> | |
729 class SimpleStringResource : public Base { | |
730 public: | |
731 // Takes ownership of |data|. | |
732 SimpleStringResource(Char* data, size_t length) | |
733 : data_(data), | |
734 length_(length) {} | |
735 | |
736 virtual ~SimpleStringResource() { delete[] data_; } | |
737 | |
738 virtual const Char* data() const { return data_; } | |
739 | |
740 virtual size_t length() const { return length_; } | |
741 | |
742 private: | |
743 Char* const data_; | |
744 const size_t length_; | |
745 }; | |
746 | |
747 | |
748 typedef SimpleStringResource<char, v8::String::ExternalAsciiStringResource> | |
749 SimpleAsciiStringResource; | |
750 typedef SimpleStringResource<uc16, v8::String::ExternalStringResource> | |
751 SimpleTwoByteStringResource; | |
752 | |
753 | |
754 const char* const ExternalizeStringExtension::kSource = | |
755 "native function externalizeString();" | |
756 "native function isAsciiString();"; | |
757 | |
758 | |
759 v8::Handle<v8::FunctionTemplate> ExternalizeStringExtension::GetNativeFunction( | |
760 v8::Handle<v8::String> str) { | |
761 if (strcmp(*v8::String::AsciiValue(str), "externalizeString") == 0) { | |
762 return v8::FunctionTemplate::New(ExternalizeStringExtension::Externalize); | |
763 } else { | |
764 ASSERT(strcmp(*v8::String::AsciiValue(str), "isAsciiString") == 0); | |
765 return v8::FunctionTemplate::New(ExternalizeStringExtension::IsAscii); | |
766 } | |
767 } | |
768 | |
769 | |
770 v8::Handle<v8::Value> ExternalizeStringExtension::Externalize( | |
771 const v8::Arguments& args) { | |
772 if (args.Length() < 1 || !args[0]->IsString()) { | |
773 return v8::ThrowException(v8::String::New( | |
774 "First parameter to externalizeString() must be a string.")); | |
775 } | |
776 bool force_two_byte = false; | |
777 if (args.Length() >= 2) { | |
778 if (args[1]->IsBoolean()) { | |
779 force_two_byte = args[1]->BooleanValue(); | |
780 } else { | |
781 return v8::ThrowException(v8::String::New( | |
782 "Second parameter to externalizeString() must be a boolean.")); | |
783 } | |
784 } | |
785 bool result = false; | |
786 Handle<String> string = Utils::OpenHandle(*args[0].As<v8::String>()); | |
787 if (string->IsExternalString()) { | |
788 return v8::ThrowException(v8::String::New( | |
789 "externalizeString() can't externalize twice.")); | |
790 } | |
791 if (string->IsAsciiRepresentation() && !force_two_byte) { | |
792 char* data = new char[string->length()]; | |
793 String::WriteToFlat(*string, data, 0, string->length()); | |
794 SimpleAsciiStringResource* resource = new SimpleAsciiStringResource( | |
795 data, string->length()); | |
796 result = string->MakeExternal(resource); | |
797 if (result && !string->IsSymbol()) { | |
798 i::ExternalStringTable::AddString(*string); | |
799 } | |
800 if (!result) delete resource; | |
801 } else { | |
802 uc16* data = new uc16[string->length()]; | |
803 String::WriteToFlat(*string, data, 0, string->length()); | |
804 SimpleTwoByteStringResource* resource = new SimpleTwoByteStringResource( | |
805 data, string->length()); | |
806 result = string->MakeExternal(resource); | |
807 if (result && !string->IsSymbol()) { | |
808 i::ExternalStringTable::AddString(*string); | |
809 } | |
810 if (!result) delete resource; | |
811 } | |
812 if (!result) { | |
813 return v8::ThrowException(v8::String::New("externalizeString() failed.")); | |
814 } | |
815 return v8::Undefined(); | |
816 } | |
817 | |
818 | |
819 v8::Handle<v8::Value> ExternalizeStringExtension::IsAscii( | |
820 const v8::Arguments& args) { | |
821 if (args.Length() != 1 || !args[0]->IsString()) { | |
822 return v8::ThrowException(v8::String::New( | |
823 "isAsciiString() requires a single string argument.")); | |
824 } | |
825 return Utils::OpenHandle(*args[0].As<v8::String>())->IsAsciiRepresentation() ? | |
826 v8::True() : v8::False(); | |
827 } | |
828 | |
829 | |
830 static ExternalizeStringExtension externalize_extension; | |
831 static v8::DeclareExtension externalize_extension_declaration( | |
832 &externalize_extension); | |
833 | |
834 } } // namespace v8::internal | 703 } } // namespace v8::internal |
OLD | NEW |