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