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