OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 // limitations under the License. |
| 28 |
| 29 #include "i18n-utils.h" |
| 30 |
| 31 #include <string.h> |
| 32 |
| 33 #include "unicode/unistr.h" |
| 34 |
| 35 namespace v8_i18n { |
| 36 |
| 37 // static |
| 38 void Utils::StrNCopy(char* dest, int length, const char* src) { |
| 39 if (!dest || !src) return; |
| 40 |
| 41 strncpy(dest, src, length); |
| 42 dest[length - 1] = '\0'; |
| 43 } |
| 44 |
| 45 // static |
| 46 bool Utils::V8StringToUnicodeString(const v8::Handle<v8::Value>& input, |
| 47 icu::UnicodeString* output) { |
| 48 v8::String::Utf8Value utf8_value(input); |
| 49 |
| 50 if (*utf8_value == NULL) return false; |
| 51 |
| 52 output->setTo(icu::UnicodeString::fromUTF8(*utf8_value)); |
| 53 |
| 54 return true; |
| 55 } |
| 56 |
| 57 // static |
| 58 bool Utils::ExtractStringSetting(const v8::Handle<v8::Object>& settings, |
| 59 const char* setting, |
| 60 icu::UnicodeString* result) { |
| 61 if (!setting || !result) return false; |
| 62 |
| 63 v8::HandleScope handle_scope; |
| 64 v8::TryCatch try_catch; |
| 65 v8::Handle<v8::Value> value = settings->Get(v8::String::New(setting)); |
| 66 if (try_catch.HasCaught()) { |
| 67 return false; |
| 68 } |
| 69 // No need to check if |value| is empty because it's taken care of |
| 70 // by TryCatch above. |
| 71 if (!value->IsUndefined() && !value->IsNull() && value->IsString()) { |
| 72 return V8StringToUnicodeString(value, result); |
| 73 } |
| 74 return false; |
| 75 } |
| 76 |
| 77 // static |
| 78 bool Utils::ExtractIntegerSetting(const v8::Handle<v8::Object>& settings, |
| 79 const char* setting, |
| 80 int32_t* result) { |
| 81 if (!setting || !result) return false; |
| 82 |
| 83 v8::HandleScope handle_scope; |
| 84 v8::TryCatch try_catch; |
| 85 v8::Handle<v8::Value> value = settings->Get(v8::String::New(setting)); |
| 86 if (try_catch.HasCaught()) { |
| 87 return false; |
| 88 } |
| 89 // No need to check if |value| is empty because it's taken care of |
| 90 // by TryCatch above. |
| 91 if (!value->IsUndefined() && !value->IsNull() && value->IsNumber()) { |
| 92 *result = static_cast<int32_t>(value->Int32Value()); |
| 93 return true; |
| 94 } |
| 95 return false; |
| 96 } |
| 97 |
| 98 // static |
| 99 bool Utils::ExtractBooleanSetting(const v8::Handle<v8::Object>& settings, |
| 100 const char* setting, |
| 101 bool* result) { |
| 102 if (!setting || !result) return false; |
| 103 |
| 104 v8::HandleScope handle_scope; |
| 105 v8::TryCatch try_catch; |
| 106 v8::Handle<v8::Value> value = settings->Get(v8::String::New(setting)); |
| 107 if (try_catch.HasCaught()) { |
| 108 return false; |
| 109 } |
| 110 // No need to check if |value| is empty because it's taken care of |
| 111 // by TryCatch above. |
| 112 if (!value->IsUndefined() && !value->IsNull() && value->IsBoolean()) { |
| 113 *result = static_cast<bool>(value->BooleanValue()); |
| 114 return true; |
| 115 } |
| 116 return false; |
| 117 } |
| 118 |
| 119 // static |
| 120 void Utils::AsciiToUChar(const char* source, |
| 121 int32_t source_length, |
| 122 UChar* target, |
| 123 int32_t target_length) { |
| 124 int32_t length = |
| 125 source_length < target_length ? source_length : target_length; |
| 126 |
| 127 if (length <= 0) { |
| 128 return; |
| 129 } |
| 130 |
| 131 for (int32_t i = 0; i < length - 1; ++i) { |
| 132 target[i] = static_cast<UChar>(source[i]); |
| 133 } |
| 134 |
| 135 target[length - 1] = 0x0u; |
| 136 } |
| 137 |
| 138 // static |
| 139 // Chrome Linux doesn't like static initializers in class, so we create |
| 140 // template on demand. |
| 141 v8::Local<v8::ObjectTemplate> Utils::GetTemplate(v8::Isolate* isolate) { |
| 142 static v8::Persistent<v8::ObjectTemplate> icu_template; |
| 143 |
| 144 if (icu_template.IsEmpty()) { |
| 145 v8::Local<v8::ObjectTemplate> raw_template(v8::ObjectTemplate::New()); |
| 146 |
| 147 // Set aside internal field for ICU class. |
| 148 raw_template->SetInternalFieldCount(1); |
| 149 |
| 150 icu_template.Reset(isolate, raw_template); |
| 151 } |
| 152 |
| 153 return v8::Local<v8::ObjectTemplate>::New(isolate, icu_template); |
| 154 } |
| 155 |
| 156 // static |
| 157 // Chrome Linux doesn't like static initializers in class, so we create |
| 158 // template on demand. This one has 2 internal fields. |
| 159 v8::Local<v8::ObjectTemplate> Utils::GetTemplate2(v8::Isolate* isolate) { |
| 160 static v8::Persistent<v8::ObjectTemplate> icu_template_2; |
| 161 |
| 162 if (icu_template_2.IsEmpty()) { |
| 163 v8::Local<v8::ObjectTemplate> raw_template(v8::ObjectTemplate::New()); |
| 164 |
| 165 // Set aside internal field for ICU class and additional data. |
| 166 raw_template->SetInternalFieldCount(2); |
| 167 |
| 168 icu_template_2.Reset(isolate, raw_template); |
| 169 } |
| 170 |
| 171 return v8::Local<v8::ObjectTemplate>::New(isolate, icu_template_2); |
| 172 } |
| 173 |
| 174 } // namespace v8_i18n |
OLD | NEW |