1 : // Copyright 2006-2008 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 <math.h>
29 :
30 : #include "v8.h"
31 : #include "dtoa.h"
32 :
33 : namespace v8 {
34 : namespace internal {
35 :
36 : // MOZ: The return type was changed from 'const char*' to 'char*' to match the
37 : // usage within SpiderMonkey.
38 : //
39 : // MOZ: The arguments were modified to use a char buffer instead of
40 : // v8::internal::Vector, to save SpiderMonkey from having to know about that
41 : // type.
42 : //
43 : // MOZ: The function was modified to return NULL when it needs to fall back to
44 : // Gay's dtoa, rather than calling Gay's dtoa itself. That's because
45 : // SpiderMonkey already has its own copy of Gay's dtoa.
46 : //
47 279511 : char* DoubleToCString(double v, char* buffer, int buflen) {
48 559022 : StringBuilder builder(buffer, buflen);
49 :
50 279511 : switch (fpclassify(v)) {
51 : case FP_NAN:
52 11272 : builder.AddString("NaN");
53 11272 : break;
54 :
55 : case FP_INFINITE:
56 10813 : if (v < 0.0) {
57 5345 : builder.AddString("-Infinity");
58 : } else {
59 5468 : builder.AddString("Infinity");
60 : }
61 10813 : break;
62 :
63 : case FP_ZERO:
64 91 : builder.AddCharacter('0');
65 91 : break;
66 :
67 : default: {
68 : int decimal_point;
69 : int sign;
70 : char* decimal_rep;
71 : //bool used_gay_dtoa = false; MOZ: see above
72 257335 : const int kV8DtoaBufferCapacity = kBase10MaximalLength + 1;
73 : char v8_dtoa_buffer[kV8DtoaBufferCapacity];
74 : int length;
75 :
76 257335 : if (DoubleToAscii(v, DTOA_SHORTEST, 0,
77 : Vector<char>(v8_dtoa_buffer, kV8DtoaBufferCapacity),
78 257335 : &sign, &length, &decimal_point)) {
79 243881 : decimal_rep = v8_dtoa_buffer;
80 : } else {
81 13454 : return NULL; // MOZ: see above
82 : //decimal_rep = dtoa(v, 0, 0, &decimal_point, &sign, NULL);
83 : //used_gay_dtoa = true;
84 : //length = StrLength(decimal_rep);
85 : }
86 :
87 282388 : if (sign) builder.AddCharacter('-');
88 :
89 243881 : if (length <= decimal_point && decimal_point <= 21) {
90 : // ECMA-262 section 9.8.1 step 6.
91 58393 : builder.AddString(decimal_rep);
92 58393 : builder.AddPadding('0', decimal_point - length);
93 :
94 185488 : } else if (0 < decimal_point && decimal_point <= 21) {
95 : // ECMA-262 section 9.8.1 step 7.
96 39814 : builder.AddSubstring(decimal_rep, decimal_point);
97 39814 : builder.AddCharacter('.');
98 39814 : builder.AddString(decimal_rep + decimal_point);
99 :
100 145674 : } else if (decimal_point <= 0 && decimal_point > -6) {
101 : // ECMA-262 section 9.8.1 step 8.
102 88074 : builder.AddString("0.");
103 88074 : builder.AddPadding('0', -decimal_point);
104 88074 : builder.AddString(decimal_rep);
105 :
106 : } else {
107 : // ECMA-262 section 9.8.1 step 9 and 10 combined.
108 57600 : builder.AddCharacter(decimal_rep[0]);
109 57600 : if (length != 1) {
110 57070 : builder.AddCharacter('.');
111 57070 : builder.AddString(decimal_rep + 1);
112 : }
113 57600 : builder.AddCharacter('e');
114 57600 : builder.AddCharacter((decimal_point >= 0) ? '+' : '-');
115 57600 : int exponent = decimal_point - 1;
116 57600 : if (exponent < 0) exponent = -exponent;
117 : // MOZ: This was a call to 'AddFormatted("%d", exponent)', which
118 : // called onto vsnprintf(). Because this was the only call to
119 : // AddFormatted in the imported code, it was replaced with this call
120 : // to AddInteger, which is faster and doesn't require any
121 : // platform-specific code.
122 57600 : builder.AddInteger(exponent);
123 : }
124 :
125 : //if (used_gay_dtoa) freedtoa(decimal_rep); MOZ: see above
126 : }
127 : }
128 266057 : return builder.Finalize();
129 : }
130 :
131 : } } // namespace v8::internal
|