|
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() | |
Mads Ager (chromium)
2011/05/16 07:20:52
Since we don't have a Date::New() method not takin
Nebojša Ćirić
2011/05/17 20:40:37
Done.
| |
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 // getXXXWeekdays always returns 8 elements. | |
127 CHECK_EQ(8, narrow_count); | |
128 CHECK_EQ(8, wide_count); | |
129 | |
130 // ICU documentation says we should ignore element 0 of the returned array. | |
131 return GetSymbols( | |
132 args, narrow + 1, narrow_count -1 , wide + 1, wide_count - 1); | |
133 } | |
134 | |
135 v8::Handle<v8::Value> DateTimeFormat::GetEras(const v8::Arguments& args) { | |
136 icu::SimpleDateFormat* date_format = UnpackDateTimeFormat(args.Holder()); | |
137 if (!date_format) { | |
138 return ThrowUnexpectedObjectError(); | |
139 } | |
140 | |
141 const icu::DateFormatSymbols* symbols = date_format->getDateFormatSymbols(); | |
142 | |
143 int32_t narrow_count; | |
144 const icu::UnicodeString* narrow = symbols->getNarrowEras(narrow_count); | |
145 int32_t wide_count; | |
146 const icu::UnicodeString* wide = symbols->getEraNames(wide_count); | |
147 | |
148 return GetSymbols(args, narrow, narrow_count, wide, wide_count); | |
149 } | |
150 | |
151 v8::Handle<v8::Value> DateTimeFormat::GetAmPm(const v8::Arguments& args) { | |
152 icu::SimpleDateFormat* date_format = UnpackDateTimeFormat(args.Holder()); | |
153 if (!date_format) { | |
154 return ThrowUnexpectedObjectError(); | |
155 } | |
156 | |
157 const icu::DateFormatSymbols* symbols = date_format->getDateFormatSymbols(); | |
158 | |
159 int32_t narrow_count; | |
160 const icu::UnicodeString* narrow = symbols->getAmPmStrings(narrow_count); | |
161 int32_t wide_count = narrow_count; | |
162 const icu::UnicodeString* wide = narrow; | |
163 | |
164 return GetSymbols(args, narrow, narrow_count, wide, wide_count); | |
165 } | |
166 | |
167 v8::Handle<v8::Value> DateTimeFormat::JSDateTimeFormat( | |
168 const v8::Arguments& args) { | |
169 v8::HandleScope handle_scope; | |
170 | |
171 icu::SimpleDateFormat* date_format = | |
172 CreateDateTimeFormat(args[0]->ToString(), args[1]->ToString()); | |
Mads Ager (chromium)
2011/05/16 07:20:52
String conversions can throw exceptions. If you ex
| |
173 | |
174 if (datetime_format_template_.IsEmpty()) { | |
175 v8::Local<v8::FunctionTemplate> raw_template(v8::FunctionTemplate::New()); | |
176 | |
177 raw_template->SetClassName(v8::String::New("v8Locale.DateTimeFormat")); | |
178 | |
179 // Define internal field count on instance template. | |
180 v8::Local<v8::ObjectTemplate> object_template = | |
181 raw_template->InstanceTemplate(); | |
182 | |
183 // Set aside internal field for icu date time formatter. | |
184 object_template->SetInternalFieldCount(1); | |
185 | |
186 // Define all of the prototype methods on prototype template. | |
187 v8::Local<v8::ObjectTemplate> proto = raw_template->PrototypeTemplate(); | |
188 proto->Set(v8::String::New("format"), | |
189 v8::FunctionTemplate::New(Format)); | |
190 proto->Set(v8::String::New("getMonths"), | |
191 v8::FunctionTemplate::New(GetMonths)); | |
192 proto->Set(v8::String::New("getWeekdays"), | |
193 v8::FunctionTemplate::New(GetWeekdays)); | |
194 proto->Set(v8::String::New("getEras"), | |
195 v8::FunctionTemplate::New(GetEras)); | |
196 proto->Set(v8::String::New("getAmPm"), | |
197 v8::FunctionTemplate::New(GetAmPm)); | |
198 | |
199 datetime_format_template_ = | |
200 v8::Persistent<v8::FunctionTemplate>::New(raw_template); | |
201 } | |
202 | |
203 // Create an empty object wrapper. | |
204 v8::Local<v8::Object> local_object = | |
205 datetime_format_template_->GetFunction()->NewInstance(); | |
206 v8::Persistent<v8::Object> wrapper = | |
207 v8::Persistent<v8::Object>::New(local_object); | |
208 | |
209 // Set date time formatter as internal field of the resulting JS object. | |
210 wrapper->SetPointerInInternalField(0, date_format); | |
211 | |
212 // Set resolved pattern in options.pattern. | |
213 icu::UnicodeString pattern; | |
214 date_format->toPattern(pattern); | |
215 v8::Local<v8::Object> options = v8::Object::New(); | |
216 options->Set(v8::String::New("pattern"), | |
217 v8::String::New(reinterpret_cast<const uint16_t*>( | |
218 pattern.getBuffer()), pattern.length())); | |
219 wrapper->Set(v8::String::New("options"), options); | |
220 | |
221 // Make object handle weak so we can delete iterator once GC kicks in. | |
222 wrapper.MakeWeak(NULL, DeleteDateTimeFormat); | |
223 | |
224 return wrapper; | |
225 } | |
226 | |
227 // Returns SimpleDateFormat. | |
228 static icu::SimpleDateFormat* CreateDateTimeFormat( | |
229 v8::Handle<v8::String> locale, v8::Handle<v8::String> skeleton) { | |
230 v8::HandleScope handle_scope; | |
231 | |
232 v8::String::AsciiValue ascii_locale(locale); | |
233 icu::Locale icu_locale(*ascii_locale); | |
234 | |
235 v8::String::Value text_value(skeleton); | |
236 icu::UnicodeString skeleton_string( | |
237 reinterpret_cast<const UChar*>(*text_value), text_value.length()); | |
238 | |
239 UErrorCode status = U_ZERO_ERROR; | |
240 v8::Local<icu::DateTimePatternGenerator> generator( | |
241 icu::DateTimePatternGenerator::createInstance(icu_locale, status)); | |
242 icu::UnicodeString pattern = | |
243 generator->getBestPattern(skeleton_string, status); | |
244 | |
245 icu::SimpleDateFormat* date_format = | |
246 new icu::SimpleDateFormat(pattern, icu_locale, status); | |
247 if (U_SUCCESS(status)) { | |
248 return date_format; | |
249 } else { | |
250 delete date_format; | |
251 status = U_ZERO_ERROR; | |
252 return new icu::SimpleDateFormat(status); | |
253 } | |
254 } | |
255 | |
256 // Creates a v8::Array of narrow or wide symbols. | |
257 static v8::Handle<v8::Value> GetSymbols(const v8::Arguments& args, | |
258 const icu::UnicodeString* narrow, | |
259 int32_t narrow_count, | |
260 const icu::UnicodeString* wide, | |
261 int32_t wide_count) { | |
262 v8::HandleScope handle_scope; | |
263 | |
264 const icu::UnicodeString* result = wide; | |
265 int32_t count = wide_count; | |
266 | |
267 if (args.Length() == 1 && args[0]->IsString()) { | |
268 v8::String::AsciiValue ascii_value(args[0]); | |
269 if (strcmp(*ascii_value, "narrow") == 0) { | |
270 result = narrow; | |
271 count = narrow_count; | |
272 } | |
273 } | |
274 | |
275 v8::Handle<v8::Array> symbols = v8::Array::New(); | |
276 for (int32_t i = 0; i < count; ++i) { | |
277 symbols->Set(i, v8::String::New( | |
278 reinterpret_cast<const uint16_t*>(result[i].getBuffer()), | |
279 result[i].length())); | |
280 } | |
281 | |
282 return handle_scope.Close(symbols); | |
283 } | |
284 | |
285 // Throws a JavaScript exception. | |
286 static v8::Handle<v8::Value> ThrowUnexpectedObjectError() { | |
287 // Returns undefined, and schedules an exception to be thrown. | |
288 return v8::ThrowException(v8::Exception::Error( | |
289 v8::String::New("DateTimeFormat method called on an object " | |
290 "that is not a DateTimeFormat."))); | |
291 } | |
292 | |
293 } } // namespace v8::internal | |
OLD | NEW |