OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 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/extensions/externalize-string-extension.h" | 5 #include "src/extensions/externalize-string-extension.h" |
6 | 6 |
7 namespace v8 { | 7 namespace v8 { |
8 namespace internal { | 8 namespace internal { |
9 | 9 |
10 template <typename Char, typename Base> | 10 template <typename Char, typename Base> |
11 class SimpleStringResource : public Base { | 11 class SimpleStringResource : public Base { |
12 public: | 12 public: |
13 // Takes ownership of |data|. | 13 // Takes ownership of |data|. |
14 SimpleStringResource(Char* data, size_t length) | 14 SimpleStringResource(Char* data, size_t length) |
15 : data_(data), | 15 : data_(data), |
16 length_(length) {} | 16 length_(length) {} |
17 | 17 |
18 virtual ~SimpleStringResource() { delete[] data_; } | 18 virtual ~SimpleStringResource() { delete[] data_; } |
19 | 19 |
20 virtual const Char* data() const { return data_; } | 20 virtual const Char* data() const { return data_; } |
21 | 21 |
22 virtual size_t length() const { return length_; } | 22 virtual size_t length() const { return length_; } |
23 | 23 |
24 private: | 24 private: |
25 Char* const data_; | 25 Char* const data_; |
26 const size_t length_; | 26 const size_t length_; |
27 }; | 27 }; |
28 | 28 |
29 | 29 |
30 typedef SimpleStringResource<char, v8::String::ExternalAsciiStringResource> | 30 typedef SimpleStringResource<char, v8::String::ExternalOneByteStringResource> |
31 SimpleAsciiStringResource; | 31 SimpleOneByteStringResource; |
32 typedef SimpleStringResource<uc16, v8::String::ExternalStringResource> | 32 typedef SimpleStringResource<uc16, v8::String::ExternalStringResource> |
33 SimpleTwoByteStringResource; | 33 SimpleTwoByteStringResource; |
34 | 34 |
35 | 35 |
36 const char* const ExternalizeStringExtension::kSource = | 36 const char* const ExternalizeStringExtension::kSource = |
37 "native function externalizeString();" | 37 "native function externalizeString();" |
38 "native function isAsciiString();"; | 38 "native function isOneByteString();"; |
39 | 39 |
40 v8::Handle<v8::FunctionTemplate> | 40 v8::Handle<v8::FunctionTemplate> |
41 ExternalizeStringExtension::GetNativeFunctionTemplate( | 41 ExternalizeStringExtension::GetNativeFunctionTemplate( |
42 v8::Isolate* isolate, v8::Handle<v8::String> str) { | 42 v8::Isolate* isolate, v8::Handle<v8::String> str) { |
43 if (strcmp(*v8::String::Utf8Value(str), "externalizeString") == 0) { | 43 if (strcmp(*v8::String::Utf8Value(str), "externalizeString") == 0) { |
44 return v8::FunctionTemplate::New(isolate, | 44 return v8::FunctionTemplate::New(isolate, |
45 ExternalizeStringExtension::Externalize); | 45 ExternalizeStringExtension::Externalize); |
46 } else { | 46 } else { |
47 DCHECK(strcmp(*v8::String::Utf8Value(str), "isAsciiString") == 0); | 47 DCHECK(strcmp(*v8::String::Utf8Value(str), "isOneByteString") == 0); |
48 return v8::FunctionTemplate::New(isolate, | 48 return v8::FunctionTemplate::New(isolate, |
49 ExternalizeStringExtension::IsAscii); | 49 ExternalizeStringExtension::IsOneByte); |
50 } | 50 } |
51 } | 51 } |
52 | 52 |
53 | 53 |
54 void ExternalizeStringExtension::Externalize( | 54 void ExternalizeStringExtension::Externalize( |
55 const v8::FunctionCallbackInfo<v8::Value>& args) { | 55 const v8::FunctionCallbackInfo<v8::Value>& args) { |
56 if (args.Length() < 1 || !args[0]->IsString()) { | 56 if (args.Length() < 1 || !args[0]->IsString()) { |
57 args.GetIsolate()->ThrowException(v8::String::NewFromUtf8( | 57 args.GetIsolate()->ThrowException(v8::String::NewFromUtf8( |
58 args.GetIsolate(), | 58 args.GetIsolate(), |
59 "First parameter to externalizeString() must be a string.")); | 59 "First parameter to externalizeString() must be a string.")); |
(...skipping 14 matching lines...) Expand all Loading... |
74 Handle<String> string = Utils::OpenHandle(*args[0].As<v8::String>()); | 74 Handle<String> string = Utils::OpenHandle(*args[0].As<v8::String>()); |
75 if (string->IsExternalString()) { | 75 if (string->IsExternalString()) { |
76 args.GetIsolate()->ThrowException(v8::String::NewFromUtf8( | 76 args.GetIsolate()->ThrowException(v8::String::NewFromUtf8( |
77 args.GetIsolate(), | 77 args.GetIsolate(), |
78 "externalizeString() can't externalize twice.")); | 78 "externalizeString() can't externalize twice.")); |
79 return; | 79 return; |
80 } | 80 } |
81 if (string->IsOneByteRepresentation() && !force_two_byte) { | 81 if (string->IsOneByteRepresentation() && !force_two_byte) { |
82 uint8_t* data = new uint8_t[string->length()]; | 82 uint8_t* data = new uint8_t[string->length()]; |
83 String::WriteToFlat(*string, data, 0, string->length()); | 83 String::WriteToFlat(*string, data, 0, string->length()); |
84 SimpleAsciiStringResource* resource = new SimpleAsciiStringResource( | 84 SimpleOneByteStringResource* resource = new SimpleOneByteStringResource( |
85 reinterpret_cast<char*>(data), string->length()); | 85 reinterpret_cast<char*>(data), string->length()); |
86 result = string->MakeExternal(resource); | 86 result = string->MakeExternal(resource); |
87 if (result) { | 87 if (result) { |
88 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); | 88 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); |
89 isolate->heap()->external_string_table()->AddString(*string); | 89 isolate->heap()->external_string_table()->AddString(*string); |
90 } | 90 } |
91 if (!result) delete resource; | 91 if (!result) delete resource; |
92 } else { | 92 } else { |
93 uc16* data = new uc16[string->length()]; | 93 uc16* data = new uc16[string->length()]; |
94 String::WriteToFlat(*string, data, 0, string->length()); | 94 String::WriteToFlat(*string, data, 0, string->length()); |
95 SimpleTwoByteStringResource* resource = new SimpleTwoByteStringResource( | 95 SimpleTwoByteStringResource* resource = new SimpleTwoByteStringResource( |
96 data, string->length()); | 96 data, string->length()); |
97 result = string->MakeExternal(resource); | 97 result = string->MakeExternal(resource); |
98 if (result) { | 98 if (result) { |
99 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); | 99 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); |
100 isolate->heap()->external_string_table()->AddString(*string); | 100 isolate->heap()->external_string_table()->AddString(*string); |
101 } | 101 } |
102 if (!result) delete resource; | 102 if (!result) delete resource; |
103 } | 103 } |
104 if (!result) { | 104 if (!result) { |
105 args.GetIsolate()->ThrowException(v8::String::NewFromUtf8( | 105 args.GetIsolate()->ThrowException(v8::String::NewFromUtf8( |
106 args.GetIsolate(), "externalizeString() failed.")); | 106 args.GetIsolate(), "externalizeString() failed.")); |
107 return; | 107 return; |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 | 111 |
112 void ExternalizeStringExtension::IsAscii( | 112 void ExternalizeStringExtension::IsOneByte( |
113 const v8::FunctionCallbackInfo<v8::Value>& args) { | 113 const v8::FunctionCallbackInfo<v8::Value>& args) { |
114 if (args.Length() != 1 || !args[0]->IsString()) { | 114 if (args.Length() != 1 || !args[0]->IsString()) { |
115 args.GetIsolate()->ThrowException(v8::String::NewFromUtf8( | 115 args.GetIsolate()->ThrowException(v8::String::NewFromUtf8( |
116 args.GetIsolate(), | 116 args.GetIsolate(), |
117 "isAsciiString() requires a single string argument.")); | 117 "isOneByteString() requires a single string argument.")); |
118 return; | 118 return; |
119 } | 119 } |
120 bool is_one_byte = | 120 bool is_one_byte = |
121 Utils::OpenHandle(*args[0].As<v8::String>())->IsOneByteRepresentation(); | 121 Utils::OpenHandle(*args[0].As<v8::String>())->IsOneByteRepresentation(); |
122 args.GetReturnValue().Set(is_one_byte); | 122 args.GetReturnValue().Set(is_one_byte); |
123 } | 123 } |
124 | 124 |
125 } } // namespace v8::internal | 125 } } // namespace v8::internal |
OLD | NEW |