@@ -396,7 +396,174 @@ static ecma_completion_value_t
396
396
ecma_builtin_number_prototype_object_to_exponential (ecma_value_t this_arg, /* *< this argument */
397
397
ecma_value_t arg) /* *< routine's argument */
398
398
{
399
- ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
399
+ ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
400
+
401
+ /* 1. */
402
+ ECMA_OP_TO_NUMBER_TRY_CATCH (this_num, this_arg, ret_value);
403
+ ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);
404
+
405
+ /* 7. */
406
+ if (arg_num <= -1.0 || arg_num >= 21.0 )
407
+ {
408
+ ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_RANGE));
409
+ }
410
+ else
411
+ {
412
+ /* 3. */
413
+ if (ecma_number_is_nan (this_num))
414
+ {
415
+ ecma_string_t *nan_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_NAN);
416
+ ret_value = ecma_make_normal_completion_value (ecma_make_string_value (nan_str_p));
417
+ }
418
+ else
419
+ {
420
+ bool is_negative = false ;
421
+
422
+ /* 5. */
423
+ if (ecma_number_is_negative (this_num) && !ecma_number_is_zero (this_num))
424
+ {
425
+ is_negative = true ;
426
+ this_num *= -1 ;
427
+ }
428
+
429
+ /* 6. */
430
+ if (ecma_number_is_infinity (this_num))
431
+ {
432
+ ecma_string_t *infinity_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_INFINITY_UL);
433
+
434
+ if (is_negative)
435
+ {
436
+ ecma_string_t *neg_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_MINUS_CHAR);
437
+ ecma_string_t *neg_inf_str_p = ecma_concat_ecma_strings (neg_str_p, infinity_str_p);
438
+ ecma_deref_ecma_string (infinity_str_p);
439
+ ecma_deref_ecma_string (neg_str_p);
440
+ ret_value = ecma_make_normal_completion_value (ecma_make_string_value (neg_inf_str_p));
441
+ }
442
+ else
443
+ {
444
+ ret_value = ecma_make_normal_completion_value (ecma_make_string_value (infinity_str_p));
445
+ }
446
+ }
447
+ else
448
+ {
449
+ uint64_t digits = 0 ;
450
+ int32_t num_digits = 0 ;
451
+ int32_t exponent = 1 ;
452
+
453
+ if (!ecma_number_is_zero (this_num))
454
+ {
455
+ /* Get the parameters of the number if non zero. */
456
+ ecma_number_to_decimal (this_num, &digits, &num_digits, &exponent);
457
+ }
458
+
459
+ int32_t frac_digits;
460
+ if (ecma_is_value_undefined (arg))
461
+ {
462
+ frac_digits = num_digits - 1 ;
463
+ }
464
+ else
465
+ {
466
+ frac_digits = ecma_number_to_int32 (arg_num);
467
+ }
468
+
469
+ digits = ecma_builtin_number_prototype_helper_round (digits, num_digits - frac_digits - 1 );
470
+
471
+ /* frac_digits + 2 characters for number, 5 characters for exponent, 1 for \0. */
472
+ int buffer_size = frac_digits + 2 + 5 + 1 ;
473
+
474
+ if (is_negative)
475
+ {
476
+ /* +1 character for sign. */
477
+ buffer_size++;
478
+ }
479
+
480
+ MEM_DEFINE_LOCAL_ARRAY (buff, buffer_size, lit_utf8_byte_t );
481
+
482
+ int digit = 0 ;
483
+ uint64_t scale = 1 ;
484
+
485
+ /* Calculate the magnitude of the number. This is used to get the digits from left to right. */
486
+ while (scale <= digits)
487
+ {
488
+ scale *= 10 ;
489
+ }
490
+
491
+ lit_utf8_byte_t *actual_char_p = buff;
492
+
493
+ if (is_negative)
494
+ {
495
+ *actual_char_p++ = ' -' ;
496
+ }
497
+
498
+ /* Add significant digits. */
499
+ for (int i = 0 ; i <= frac_digits; i++)
500
+ {
501
+ digit = 0 ;
502
+ scale /= 10 ;
503
+ while (digits >= scale && scale > 0 )
504
+ {
505
+ digits -= scale;
506
+ digit++;
507
+ }
508
+
509
+ *actual_char_p = (lit_utf8_byte_t ) (digit + ' 0' );
510
+ actual_char_p++;
511
+
512
+ if (i == 0 && frac_digits != 0 )
513
+ {
514
+ *actual_char_p++ = ' .' ;
515
+ }
516
+ }
517
+
518
+ *actual_char_p++ = ' e' ;
519
+
520
+ exponent--;
521
+ if (exponent < 0 )
522
+ {
523
+ exponent *= -1 ;
524
+ *actual_char_p++ = ' -' ;
525
+ }
526
+ else
527
+ {
528
+ *actual_char_p++ = ' +' ;
529
+ }
530
+
531
+ /* Get magnitude of exponent. */
532
+ int32_t scale_expt = 1 ;
533
+ while (scale_expt <= exponent)
534
+ {
535
+ scale_expt *= 10 ;
536
+ }
537
+ scale_expt /= 10 ;
538
+
539
+ /* Add exponent digits. */
540
+ if (exponent == 0 )
541
+ {
542
+ *actual_char_p++ = ' 0' ;
543
+ }
544
+ else
545
+ {
546
+ while (scale_expt > 0 )
547
+ {
548
+ digit = exponent / scale_expt;
549
+ exponent %= scale_expt;
550
+ *actual_char_p++ = (lit_utf8_byte_t ) (digit + ' 0' );
551
+ scale_expt /= 10 ;
552
+ }
553
+ }
554
+
555
+ JERRY_ASSERT (actual_char_p - buff < buffer_size);
556
+ *actual_char_p = ' \0 ' ;
557
+ ecma_string_t *str = ecma_new_ecma_string_from_utf8 (buff, (lit_utf8_size_t ) (actual_char_p - buff));
558
+ ret_value = ecma_make_normal_completion_value (ecma_make_string_value (str));
559
+ MEM_FINALIZE_LOCAL_ARRAY (buff);
560
+ }
561
+ }
562
+ }
563
+
564
+ ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
565
+ ECMA_OP_TO_NUMBER_FINALIZE (this_num);
566
+ return ret_value;
400
567
} /* ecma_builtin_number_prototype_object_to_exponential */
401
568
402
569
/* *
0 commit comments