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 |
| 46 // static |
| 47 bool Utils::V8StringToUnicodeString(const v8::Handle<v8::Value>& input, |
| 48 icu::UnicodeString* output) { |
| 49 v8::String::Utf8Value utf8_value(input); |
| 50 |
| 51 if (*utf8_value == NULL) return false; |
| 52 |
| 53 output->setTo(icu::UnicodeString::fromUTF8(*utf8_value)); |
| 54 |
| 55 return true; |
| 56 } |
| 57 |
| 58 |
| 59 // static |
| 60 bool Utils::ExtractStringSetting(const v8::Handle<v8::Object>& settings, |
| 61 const char* setting, |
| 62 icu::UnicodeString* result) { |
| 63 if (!setting || !result) return false; |
| 64 |
| 65 v8::HandleScope handle_scope; |
| 66 v8::TryCatch try_catch; |
| 67 v8::Handle<v8::Value> value = settings->Get(v8::String::New(setting)); |
| 68 if (try_catch.HasCaught()) { |
| 69 return false; |
| 70 } |
| 71 // No need to check if |value| is empty because it's taken care of |
| 72 // by TryCatch above. |
| 73 if (!value->IsUndefined() && !value->IsNull() && value->IsString()) { |
| 74 return V8StringToUnicodeString(value, result); |
| 75 } |
| 76 return false; |
| 77 } |
| 78 |
| 79 |
| 80 // static |
| 81 bool Utils::ExtractIntegerSetting(const v8::Handle<v8::Object>& settings, |
| 82 const char* setting, |
| 83 int32_t* result) { |
| 84 if (!setting || !result) return false; |
| 85 |
| 86 v8::HandleScope handle_scope; |
| 87 v8::TryCatch try_catch; |
| 88 v8::Handle<v8::Value> value = settings->Get(v8::String::New(setting)); |
| 89 if (try_catch.HasCaught()) { |
| 90 return false; |
| 91 } |
| 92 // No need to check if |value| is empty because it's taken care of |
| 93 // by TryCatch above. |
| 94 if (!value->IsUndefined() && !value->IsNull() && value->IsNumber()) { |
| 95 *result = static_cast<int32_t>(value->Int32Value()); |
| 96 return true; |
| 97 } |
| 98 return false; |
| 99 } |
| 100 |
| 101 |
| 102 // static |
| 103 bool Utils::ExtractBooleanSetting(const v8::Handle<v8::Object>& settings, |
| 104 const char* setting, |
| 105 bool* result) { |
| 106 if (!setting || !result) return false; |
| 107 |
| 108 v8::HandleScope handle_scope; |
| 109 v8::TryCatch try_catch; |
| 110 v8::Handle<v8::Value> value = settings->Get(v8::String::New(setting)); |
| 111 if (try_catch.HasCaught()) { |
| 112 return false; |
| 113 } |
| 114 // No need to check if |value| is empty because it's taken care of |
| 115 // by TryCatch above. |
| 116 if (!value->IsUndefined() && !value->IsNull() && value->IsBoolean()) { |
| 117 *result = static_cast<bool>(value->BooleanValue()); |
| 118 return true; |
| 119 } |
| 120 return false; |
| 121 } |
| 122 |
| 123 |
| 124 // static |
| 125 void Utils::AsciiToUChar(const char* source, |
| 126 int32_t source_length, |
| 127 UChar* target, |
| 128 int32_t target_length) { |
| 129 int32_t length = |
| 130 source_length < target_length ? source_length : target_length; |
| 131 |
| 132 if (length <= 0) { |
| 133 return; |
| 134 } |
| 135 |
| 136 for (int32_t i = 0; i < length - 1; ++i) { |
| 137 target[i] = static_cast<UChar>(source[i]); |
| 138 } |
| 139 |
| 140 target[length - 1] = 0x0u; |
| 141 } |
| 142 |
| 143 |
| 144 // static |
| 145 // Chrome Linux doesn't like static initializers in class, so we create |
| 146 // template on demand. |
| 147 v8::Local<v8::ObjectTemplate> Utils::GetTemplate(v8::Isolate* isolate) { |
| 148 static v8::Persistent<v8::ObjectTemplate> icu_template; |
| 149 |
| 150 if (icu_template.IsEmpty()) { |
| 151 v8::Local<v8::ObjectTemplate> raw_template(v8::ObjectTemplate::New()); |
| 152 |
| 153 // Set aside internal field for ICU class. |
| 154 raw_template->SetInternalFieldCount(1); |
| 155 |
| 156 icu_template.Reset(isolate, raw_template); |
| 157 } |
| 158 |
| 159 return v8::Local<v8::ObjectTemplate>::New(isolate, icu_template); |
| 160 } |
| 161 |
| 162 |
| 163 // static |
| 164 // Chrome Linux doesn't like static initializers in class, so we create |
| 165 // template on demand. This one has 2 internal fields. |
| 166 v8::Local<v8::ObjectTemplate> Utils::GetTemplate2(v8::Isolate* isolate) { |
| 167 static v8::Persistent<v8::ObjectTemplate> icu_template_2; |
| 168 |
| 169 if (icu_template_2.IsEmpty()) { |
| 170 v8::Local<v8::ObjectTemplate> raw_template(v8::ObjectTemplate::New()); |
| 171 |
| 172 // Set aside internal field for ICU class and additional data. |
| 173 raw_template->SetInternalFieldCount(2); |
| 174 |
| 175 icu_template_2.Reset(isolate, raw_template); |
| 176 } |
| 177 |
| 178 return v8::Local<v8::ObjectTemplate>::New(isolate, icu_template_2); |
| 179 } |
| 180 |
| 181 } // namespace v8_i18n |
OLD | NEW |