| 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> |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 DCHECK(strcmp(*v8::String::Utf8Value(str), "isOneByteString") == 0); | 47 DCHECK(strcmp(*v8::String::Utf8Value(str), "isOneByteString") == 0); |
| 48 return v8::FunctionTemplate::New(isolate, | 48 return v8::FunctionTemplate::New(isolate, |
| 49 ExternalizeStringExtension::IsOneByte); | 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( |
| 58 args.GetIsolate(), | 58 v8::String::NewFromUtf8( |
| 59 "First parameter to externalizeString() must be a string.")); | 59 args.GetIsolate(), |
| 60 "First parameter to externalizeString() must be a string.", |
| 61 NewStringType::kNormal).ToLocalChecked()); |
| 60 return; | 62 return; |
| 61 } | 63 } |
| 62 bool force_two_byte = false; | 64 bool force_two_byte = false; |
| 63 if (args.Length() >= 2) { | 65 if (args.Length() >= 2) { |
| 64 if (args[1]->IsBoolean()) { | 66 if (args[1]->IsBoolean()) { |
| 65 force_two_byte = args[1]->BooleanValue(); | 67 force_two_byte = |
| 68 args[1] |
| 69 ->BooleanValue(args.GetIsolate()->GetCurrentContext()) |
| 70 .FromJust(); |
| 66 } else { | 71 } else { |
| 67 args.GetIsolate()->ThrowException(v8::String::NewFromUtf8( | 72 args.GetIsolate()->ThrowException( |
| 68 args.GetIsolate(), | 73 v8::String::NewFromUtf8( |
| 69 "Second parameter to externalizeString() must be a boolean.")); | 74 args.GetIsolate(), |
| 75 "Second parameter to externalizeString() must be a boolean.", |
| 76 NewStringType::kNormal).ToLocalChecked()); |
| 70 return; | 77 return; |
| 71 } | 78 } |
| 72 } | 79 } |
| 73 bool result = false; | 80 bool result = false; |
| 74 Handle<String> string = Utils::OpenHandle(*args[0].As<v8::String>()); | 81 Handle<String> string = Utils::OpenHandle(*args[0].As<v8::String>()); |
| 75 if (string->IsExternalString()) { | 82 if (string->IsExternalString()) { |
| 76 args.GetIsolate()->ThrowException(v8::String::NewFromUtf8( | 83 args.GetIsolate()->ThrowException( |
| 77 args.GetIsolate(), | 84 v8::String::NewFromUtf8(args.GetIsolate(), |
| 78 "externalizeString() can't externalize twice.")); | 85 "externalizeString() can't externalize twice.", |
| 86 NewStringType::kNormal).ToLocalChecked()); |
| 79 return; | 87 return; |
| 80 } | 88 } |
| 81 if (string->IsOneByteRepresentation() && !force_two_byte) { | 89 if (string->IsOneByteRepresentation() && !force_two_byte) { |
| 82 uint8_t* data = new uint8_t[string->length()]; | 90 uint8_t* data = new uint8_t[string->length()]; |
| 83 String::WriteToFlat(*string, data, 0, string->length()); | 91 String::WriteToFlat(*string, data, 0, string->length()); |
| 84 SimpleOneByteStringResource* resource = new SimpleOneByteStringResource( | 92 SimpleOneByteStringResource* resource = new SimpleOneByteStringResource( |
| 85 reinterpret_cast<char*>(data), string->length()); | 93 reinterpret_cast<char*>(data), string->length()); |
| 86 result = string->MakeExternal(resource); | 94 result = string->MakeExternal(resource); |
| 87 if (result) { | 95 if (result) { |
| 88 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); | 96 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); |
| 89 isolate->heap()->external_string_table()->AddString(*string); | 97 isolate->heap()->external_string_table()->AddString(*string); |
| 90 } | 98 } |
| 91 if (!result) delete resource; | 99 if (!result) delete resource; |
| 92 } else { | 100 } else { |
| 93 uc16* data = new uc16[string->length()]; | 101 uc16* data = new uc16[string->length()]; |
| 94 String::WriteToFlat(*string, data, 0, string->length()); | 102 String::WriteToFlat(*string, data, 0, string->length()); |
| 95 SimpleTwoByteStringResource* resource = new SimpleTwoByteStringResource( | 103 SimpleTwoByteStringResource* resource = new SimpleTwoByteStringResource( |
| 96 data, string->length()); | 104 data, string->length()); |
| 97 result = string->MakeExternal(resource); | 105 result = string->MakeExternal(resource); |
| 98 if (result) { | 106 if (result) { |
| 99 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); | 107 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); |
| 100 isolate->heap()->external_string_table()->AddString(*string); | 108 isolate->heap()->external_string_table()->AddString(*string); |
| 101 } | 109 } |
| 102 if (!result) delete resource; | 110 if (!result) delete resource; |
| 103 } | 111 } |
| 104 if (!result) { | 112 if (!result) { |
| 105 args.GetIsolate()->ThrowException(v8::String::NewFromUtf8( | 113 args.GetIsolate()->ThrowException( |
| 106 args.GetIsolate(), "externalizeString() failed.")); | 114 v8::String::NewFromUtf8(args.GetIsolate(), |
| 115 "externalizeString() failed.", |
| 116 NewStringType::kNormal).ToLocalChecked()); |
| 107 return; | 117 return; |
| 108 } | 118 } |
| 109 } | 119 } |
| 110 | 120 |
| 111 | 121 |
| 112 void ExternalizeStringExtension::IsOneByte( | 122 void ExternalizeStringExtension::IsOneByte( |
| 113 const v8::FunctionCallbackInfo<v8::Value>& args) { | 123 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 114 if (args.Length() != 1 || !args[0]->IsString()) { | 124 if (args.Length() != 1 || !args[0]->IsString()) { |
| 115 args.GetIsolate()->ThrowException(v8::String::NewFromUtf8( | 125 args.GetIsolate()->ThrowException( |
| 116 args.GetIsolate(), | 126 v8::String::NewFromUtf8( |
| 117 "isOneByteString() requires a single string argument.")); | 127 args.GetIsolate(), |
| 128 "isOneByteString() requires a single string argument.", |
| 129 NewStringType::kNormal).ToLocalChecked()); |
| 118 return; | 130 return; |
| 119 } | 131 } |
| 120 bool is_one_byte = | 132 bool is_one_byte = |
| 121 Utils::OpenHandle(*args[0].As<v8::String>())->IsOneByteRepresentation(); | 133 Utils::OpenHandle(*args[0].As<v8::String>())->IsOneByteRepresentation(); |
| 122 args.GetReturnValue().Set(is_one_byte); | 134 args.GetReturnValue().Set(is_one_byte); |
| 123 } | 135 } |
| 124 | 136 |
| 125 } // namespace internal | 137 } // namespace internal |
| 126 } // namespace v8 | 138 } // namespace v8 |
| OLD | NEW |