OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/api.h" | 7 #include "src/api.h" |
8 #include "src/execution.h" | 8 #include "src/execution.h" |
9 #include "src/messages.h" | 9 #include "src/messages.h" |
10 #include "src/string-builder.h" | 10 #include "src/string-builder.h" |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 } | 315 } |
316 // A string that has been obtained from JS code in this way is | 316 // A string that has been obtained from JS code in this way is |
317 // likely to be a complicated ConsString of some sort. We flatten it | 317 // likely to be a complicated ConsString of some sort. We flatten it |
318 // here to improve the efficiency of converting it to a C string and | 318 // here to improve the efficiency of converting it to a C string and |
319 // other operations that are likely to take place (see GetLocalizedMessage | 319 // other operations that are likely to take place (see GetLocalizedMessage |
320 // for example). | 320 // for example). |
321 return String::Flatten(result_string); | 321 return String::Flatten(result_string); |
322 } | 322 } |
323 | 323 |
324 | 324 |
| 325 const char* MessageTemplate::TemplateString(int template_index) { |
| 326 switch (template_index) { |
| 327 #define CASE(NAME, STRING) \ |
| 328 case k##NAME: \ |
| 329 return STRING; |
| 330 MESSAGE_TEMPLATES(CASE) |
| 331 #undef CASE |
| 332 case kLastMessage: |
| 333 default: |
| 334 return NULL; |
| 335 } |
| 336 } |
| 337 |
| 338 |
325 MaybeHandle<String> MessageTemplate::FormatMessage(int template_index, | 339 MaybeHandle<String> MessageTemplate::FormatMessage(int template_index, |
326 Handle<String> arg0, | 340 Handle<String> arg0, |
327 Handle<String> arg1, | 341 Handle<String> arg1, |
328 Handle<String> arg2) { | 342 Handle<String> arg2) { |
329 Isolate* isolate = arg0->GetIsolate(); | 343 Isolate* isolate = arg0->GetIsolate(); |
330 const char* template_string; | 344 const char* template_string = TemplateString(template_index); |
331 switch (template_index) { | 345 if (template_string == NULL) { |
332 #define CASE(NAME, STRING) \ | 346 isolate->ThrowIllegalOperation(); |
333 case k##NAME: \ | 347 return MaybeHandle<String>(); |
334 template_string = STRING; \ | |
335 break; | |
336 MESSAGE_TEMPLATES(CASE) | |
337 #undef CASE | |
338 case kLastMessage: | |
339 default: | |
340 isolate->ThrowIllegalOperation(); | |
341 return MaybeHandle<String>(); | |
342 } | 348 } |
343 | 349 |
344 IncrementalStringBuilder builder(isolate); | 350 IncrementalStringBuilder builder(isolate); |
345 | 351 |
346 unsigned int i = 0; | 352 unsigned int i = 0; |
347 Handle<String> args[] = {arg0, arg1, arg2}; | 353 Handle<String> args[] = {arg0, arg1, arg2}; |
348 for (const char* c = template_string; *c != '\0'; c++) { | 354 for (const char* c = template_string; *c != '\0'; c++) { |
349 if (*c == '%') { | 355 if (*c == '%') { |
350 // %% results in verbatim %. | 356 // %% results in verbatim %. |
351 if (*(c + 1) == '%') { | 357 if (*(c + 1) == '%') { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 if (obj->IsUndefined()) return default_value; | 456 if (obj->IsUndefined()) return default_value; |
451 if (!obj->IsString()) { | 457 if (!obj->IsString()) { |
452 ASSIGN_RETURN_ON_EXCEPTION(isolate, obj, Execution::ToString(isolate, obj), | 458 ASSIGN_RETURN_ON_EXCEPTION(isolate, obj, Execution::ToString(isolate, obj), |
453 String); | 459 String); |
454 } | 460 } |
455 return Handle<String>::cast(obj); | 461 return Handle<String>::cast(obj); |
456 } | 462 } |
457 | 463 |
458 } // namespace internal | 464 } // namespace internal |
459 } // namespace v8 | 465 } // namespace v8 |
OLD | NEW |