OLD | NEW |
(Empty) | |
| 1 // Copyright 2011 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 |
| 28 #include "datetime-format.h" |
| 29 |
| 30 #include "i18n-utils.h" |
| 31 #include "unicode/dtfmtsym.h" |
| 32 #include "unicode/dtptngen.h" |
| 33 #include "unicode/locid.h" |
| 34 #include "unicode/smpdtfmt.h" |
| 35 |
| 36 namespace v8 { |
| 37 namespace internal { |
| 38 |
| 39 v8::Persistent<v8::FunctionTemplate> DateTimeFormat::datetime_format_template_; |
| 40 |
| 41 static icu::SimpleDateFormat* CreateDateTimeFormat(v8::Handle<v8::String>, |
| 42 v8::Handle<v8::String>); |
| 43 static v8::Handle<v8::Value> GetSymbols( |
| 44 const v8::Arguments&, const icu::UnicodeString*, int32_t, |
| 45 const icu::UnicodeString*, int32_t); |
| 46 static v8::Handle<v8::Value> ThrowUnexpectedObjectError(); |
| 47 |
| 48 icu::SimpleDateFormat* DateTimeFormat::UnpackDateTimeFormat( |
| 49 v8::Handle<v8::Object> obj) { |
| 50 if (datetime_format_template_->HasInstance(obj)) { |
| 51 return static_cast<icu::SimpleDateFormat*>( |
| 52 obj->GetPointerFromInternalField(0)); |
| 53 } |
| 54 |
| 55 return NULL; |
| 56 } |
| 57 |
| 58 void DateTimeFormat::DeleteDateTimeFormat(v8::Persistent<v8::Value> object, |
| 59 void* param) { |
| 60 v8::Persistent<v8::Object> persistent_object = |
| 61 v8::Persistent<v8::Object>::Cast(object); |
| 62 |
| 63 // First delete the hidden C++ object. |
| 64 // Unpacking should never return NULL here. That would only happen if |
| 65 // this method is used as the weak callback for persistent handles not |
| 66 // pointing to a date time formatter. |
| 67 delete UnpackDateTimeFormat(persistent_object); |
| 68 |
| 69 // Then dispose of the persistent handle to JS object. |
| 70 persistent_object.Dispose(); |
| 71 } |
| 72 |
| 73 v8::Handle<v8::Value> DateTimeFormat::Format(const v8::Arguments& args) { |
| 74 v8::HandleScope handle_scope; |
| 75 |
| 76 double millis = 0.0; |
| 77 if (args.Length() != 1 || !args[0]->IsDate()) { |
| 78 // Create a new date. |
| 79 millis = v8::Script::Compile(v8::String::New("eval('new Date()')"))->Run() |
| 80 ->NumberValue(); |
| 81 } else { |
| 82 millis = v8::Date::Cast(*args[0])->NumberValue(); |
| 83 } |
| 84 |
| 85 icu::SimpleDateFormat* date_format = UnpackDateTimeFormat(args.Holder()); |
| 86 if (!date_format) { |
| 87 return ThrowUnexpectedObjectError(); |
| 88 } |
| 89 |
| 90 icu::UnicodeString result; |
| 91 date_format->format(millis, result); |
| 92 |
| 93 return v8::String::New( |
| 94 reinterpret_cast<const uint16_t*>(result.getBuffer()), result.length()); |
| 95 } |
| 96 |
| 97 v8::Handle<v8::Value> DateTimeFormat::GetMonths(const v8::Arguments& args) { |
| 98 icu::SimpleDateFormat* date_format = UnpackDateTimeFormat(args.Holder()); |
| 99 if (!date_format) { |
| 100 return ThrowUnexpectedObjectError(); |
| 101 } |
| 102 |
| 103 const icu::DateFormatSymbols* symbols = date_format->getDateFormatSymbols(); |
| 104 |
| 105 int32_t narrow_count; |
| 106 const icu::UnicodeString* narrow = symbols->getShortMonths(narrow_count); |
| 107 int32_t wide_count; |
| 108 const icu::UnicodeString* wide = symbols->getMonths(wide_count); |
| 109 |
| 110 return GetSymbols(args, narrow, narrow_count, wide, wide_count); |
| 111 } |
| 112 |
| 113 v8::Handle<v8::Value> DateTimeFormat::GetWeekdays(const v8::Arguments& args) { |
| 114 icu::SimpleDateFormat* date_format = UnpackDateTimeFormat(args.Holder()); |
| 115 if (!date_format) { |
| 116 ThrowUnexpectedObjectError(); |
| 117 } |
| 118 |
| 119 const icu::DateFormatSymbols* symbols = date_format->getDateFormatSymbols(); |
| 120 |
| 121 int32_t narrow_count; |
| 122 const icu::UnicodeString* narrow = symbols->getShortWeekdays(narrow_count); |
| 123 int32_t wide_count; |
| 124 const icu::UnicodeString* wide = symbols->getWeekdays(wide_count); |
| 125 |
| 126 // ICU documentation says we should ignore element 0 of the returned array. |
| 127 // getXXXWeekdays always returns 8 elements. |
| 128 CHECK(wide_count == 8); |
| 129 return GetSymbols( |
| 130 args, narrow + 1, narrow_count -1 , wide + 1, wide_count - 1); |
| 131 } |
| 132 |
| 133 v8::Handle<v8::Value> DateTimeFormat::GetEras(const v8::Arguments& args) { |
| 134 icu::SimpleDateFormat* date_format = UnpackDateTimeFormat(args.Holder()); |
| 135 if (!date_format) { |
| 136 return ThrowUnexpectedObjectError(); |
| 137 } |
| 138 |
| 139 const icu::DateFormatSymbols* symbols = date_format->getDateFormatSymbols(); |
| 140 |
| 141 int32_t narrow_count; |
| 142 const icu::UnicodeString* narrow = symbols->getNarrowEras(narrow_count); |
| 143 int32_t wide_count; |
| 144 const icu::UnicodeString* wide = symbols->getEraNames(wide_count); |
| 145 |
| 146 return GetSymbols(args, narrow, narrow_count, wide, wide_count); |
| 147 } |
| 148 |
| 149 v8::Handle<v8::Value> DateTimeFormat::GetAmPm(const v8::Arguments& args) { |
| 150 icu::SimpleDateFormat* date_format = UnpackDateTimeFormat(args.Holder()); |
| 151 if (!date_format) { |
| 152 return ThrowUnexpectedObjectError(); |
| 153 } |
| 154 |
| 155 const icu::DateFormatSymbols* symbols = date_format->getDateFormatSymbols(); |
| 156 |
| 157 int32_t narrow_count; |
| 158 const icu::UnicodeString* narrow = symbols->getAmPmStrings(narrow_count); |
| 159 int32_t wide_count = narrow_count; |
| 160 const icu::UnicodeString* wide = narrow; |
| 161 |
| 162 return GetSymbols(args, narrow, narrow_count, wide, wide_count); |
| 163 } |
| 164 |
| 165 v8::Handle<v8::Value> DateTimeFormat::JSDateTimeFormat( |
| 166 const v8::Arguments& args) { |
| 167 v8::HandleScope handle_scope; |
| 168 |
| 169 icu::SimpleDateFormat* date_format = |
| 170 CreateDateTimeFormat(args[0]->ToString(), args[1]->ToString()); |
| 171 |
| 172 if (datetime_format_template_.IsEmpty()) { |
| 173 v8::Local<v8::FunctionTemplate> raw_template(v8::FunctionTemplate::New()); |
| 174 |
| 175 raw_template->SetClassName(v8::String::New("v8Locale.DateTimeFormat")); |
| 176 |
| 177 // Define internal field count on instance template. |
| 178 v8::Local<v8::ObjectTemplate> object_template = |
| 179 raw_template->InstanceTemplate(); |
| 180 |
| 181 // Set aside internal field for icu date time formatter. |
| 182 object_template->SetInternalFieldCount(1); |
| 183 |
| 184 // Define all of the prototype methods on prototype template. |
| 185 v8::Local<v8::ObjectTemplate> proto = raw_template->PrototypeTemplate(); |
| 186 proto->Set(v8::String::New("format"), |
| 187 v8::FunctionTemplate::New(Format)); |
| 188 proto->Set(v8::String::New("getMonths"), |
| 189 v8::FunctionTemplate::New(GetMonths)); |
| 190 proto->Set(v8::String::New("getWeekdays"), |
| 191 v8::FunctionTemplate::New(GetWeekdays)); |
| 192 proto->Set(v8::String::New("getEras"), |
| 193 v8::FunctionTemplate::New(GetEras)); |
| 194 proto->Set(v8::String::New("getAmPm"), |
| 195 v8::FunctionTemplate::New(GetAmPm)); |
| 196 |
| 197 datetime_format_template_ = |
| 198 v8::Persistent<v8::FunctionTemplate>::New(raw_template); |
| 199 } |
| 200 |
| 201 // Create an empty object wrapper. |
| 202 v8::Local<v8::Object> local_object = |
| 203 datetime_format_template_->GetFunction()->NewInstance(); |
| 204 v8::Persistent<v8::Object> wrapper = |
| 205 v8::Persistent<v8::Object>::New(local_object); |
| 206 |
| 207 // Set date time formatter as internal field of the resulting JS object. |
| 208 wrapper->SetPointerInInternalField(0, date_format); |
| 209 |
| 210 // Set resolved pattern in options.pattern. |
| 211 icu::UnicodeString pattern; |
| 212 date_format->toPattern(pattern); |
| 213 v8::Local<v8::Object> options = v8::Object::New(); |
| 214 options->Set(v8::String::New("pattern"), |
| 215 v8::String::New(reinterpret_cast<const uint16_t*>( |
| 216 pattern.getBuffer()), pattern.length())); |
| 217 wrapper->Set(v8::String::New("options"), options); |
| 218 |
| 219 // Make object handle weak so we can delete iterator once GC kicks in. |
| 220 wrapper.MakeWeak(NULL, DeleteDateTimeFormat); |
| 221 |
| 222 return wrapper; |
| 223 } |
| 224 |
| 225 // Returns SimpleDateFormat. |
| 226 static icu::SimpleDateFormat* CreateDateTimeFormat( |
| 227 v8::Handle<v8::String> locale, v8::Handle<v8::String> skeleton) { |
| 228 v8::HandleScope handle_scope; |
| 229 |
| 230 v8::String::AsciiValue ascii_locale(locale); |
| 231 icu::Locale icu_locale(*ascii_locale); |
| 232 |
| 233 v8::String::Value text_value(skeleton); |
| 234 icu::UnicodeString skeleton_string( |
| 235 reinterpret_cast<const UChar*>(*text_value), text_value.length()); |
| 236 |
| 237 UErrorCode status = U_ZERO_ERROR; |
| 238 v8::Local<icu::DateTimePatternGenerator> generator( |
| 239 icu::DateTimePatternGenerator::createInstance(icu_locale, status)); |
| 240 icu::UnicodeString pattern = |
| 241 generator->getBestPattern(skeleton_string, status); |
| 242 |
| 243 icu::SimpleDateFormat* date_format = |
| 244 new icu::SimpleDateFormat(pattern, icu_locale, status); |
| 245 if (U_SUCCESS(status)) { |
| 246 return date_format; |
| 247 } else { |
| 248 delete date_format; |
| 249 status = U_ZERO_ERROR; |
| 250 return new icu::SimpleDateFormat(status); |
| 251 } |
| 252 } |
| 253 |
| 254 // Creates a v8::Array of narrow or wide symbols. |
| 255 static v8::Handle<v8::Value> GetSymbols(const v8::Arguments& args, |
| 256 const icu::UnicodeString* narrow, |
| 257 int32_t narrow_count, |
| 258 const icu::UnicodeString* wide, |
| 259 int32_t wide_count) { |
| 260 v8::HandleScope handle_scope; |
| 261 |
| 262 const icu::UnicodeString* result = wide; |
| 263 int32_t count = wide_count; |
| 264 |
| 265 if (args.Length() == 1 && args[0]->IsString()) { |
| 266 v8::String::AsciiValue ascii_value(args[0]); |
| 267 if (strcmp(*ascii_value, "narrow") == 0) { |
| 268 result = narrow; |
| 269 count = narrow_count; |
| 270 } |
| 271 } |
| 272 |
| 273 v8::Handle<v8::Array> symbols = v8::Array::New(); |
| 274 for (int32_t i = 0; i < count; ++i) { |
| 275 symbols->Set(i, v8::String::New( |
| 276 reinterpret_cast<const uint16_t*>(result[i].getBuffer()), |
| 277 result[i].length())); |
| 278 } |
| 279 |
| 280 return handle_scope.Close(symbols); |
| 281 } |
| 282 |
| 283 // Throws a JavaScript exception. |
| 284 static v8::Handle<v8::Value> ThrowUnexpectedObjectError() { |
| 285 // Returns undefined, and schedules an exception to be thrown. |
| 286 return v8::ThrowException(v8::Exception::Error( |
| 287 v8::String::New("DateTimeFormat method called on an object " |
| 288 "that is not a DateTimeFormat."))); |
| 289 } |
| 290 |
| 291 } } // namespace v8::internal |
OLD | NEW |