Skip to content

Commit 4ae204a

Browse files
committed
Add binding for GHCJS
implement javascript versions under the assumption that engines where we run the code will have V8 compatibile implementation. NodeJS looks fine.
1 parent e4b7956 commit 4ae204a

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

double-conversion.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ library
7070
double-conversion/src/fast-dtoa.cc
7171
double-conversion/src/fixed-dtoa.cc
7272
double-conversion/src/strtod.cc
73+
js-sources:
74+
jsbits/hs-double-conversion.js
7375

7476
if os(windows)
7577
if arch(x86_64)

jsbits/hs-double-conversion.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
2+
var kToFixedLength =
3+
1 + /* DoubleToStringConverter::kMaxFixedDigitsBeforePoint*/ 60 +
4+
1 + /* DoubleToStringConverter::kMaxFixedDigitsAfterPoint*/ 60;
5+
6+
7+
var kToShortestLength = 26;
8+
var kToExponentialLength = /*DoubleToStringConverter::kMaxExponentialDigits*/ 120 + 8;
9+
var kToPrecisionLength = /*DoubleToStringConverter::kMaxPrecisionDigits*/ 120 + 7;
10+
11+
function h$_hs_ToShortestLength() {
12+
return kToShortestLength;
13+
}
14+
15+
function h$_hs_ToFixedLength()
16+
{
17+
return kToFixedLength;
18+
}
19+
20+
function h$_hs_ToExponentialLength()
21+
{
22+
return kToExponentialLength;
23+
}
24+
25+
function h$_hs_ToPrecisionLength()
26+
{
27+
return kToPrecisionLength;
28+
}
29+
30+
//--------------------
31+
32+
function h$_hs_Text_ToShortest(value, buf)
33+
{
34+
var conv = value.toString().replace("e+","e");
35+
Array.prototype.map.call(conv,function(c,i){ buf.u1[i]=c.charCodeAt(0)});
36+
return buf.len = conv.length;
37+
}
38+
39+
function h$_hs_ToShortest(value, buf)
40+
{
41+
var conv = value.toString().replace("e+","e");
42+
Array.prototype.map.call(conv,function(c,i){ buf.u8[i]=c.charCodeAt(0)});
43+
return buf.len = conv.length;
44+
}
45+
46+
//--------------------
47+
48+
function h$_hs_Text_ToFixed(value, buf, ndigits)
49+
{
50+
// negative digits differ for small values
51+
if (ndigits > 60) return -1;
52+
if (ndigits > 0)
53+
{
54+
var conv = value.toFixed(Math.min(20,ndigits))+ "0".repeat(Math.max(0,ndigits - 20));
55+
Array.prototype.map.call(conv,function(c,i){ buf.u1[i]=c.charCodeAt(0)});
56+
return buf.len = conv.length;
57+
}
58+
else
59+
{
60+
var conv = value.toFixed(0).slice(0,ndigits) + "0".repeat(-ndigits);
61+
Array.prototype.map.call(conv,function(c,i){ buf.u1[i]=c.charCodeAt(0)});
62+
return buf.len = conv.length;
63+
}
64+
}
65+
66+
function h$_hs_ToFixed(value, buf, unused, ndigits)
67+
{
68+
// negative digits differ for small values
69+
if (ndigits > 60) return -1;
70+
if (ndigits > 0)
71+
{
72+
var conv = value.toFixed(Math.min(20,ndigits))+ "0".repeat(Math.max(0,ndigits - 20));
73+
Array.prototype.map.call(conv,function(c,i){ buf.u8[i]=c.charCodeAt(0)});
74+
return buf.len = conv.length;
75+
}
76+
else
77+
{
78+
var conv = value.toFixed(0).slice(0,ndigits) + "0".repeat(-ndigits);
79+
Array.prototype.map.call(conv,function(c,i){ buf.u8[i]=c.charCodeAt(0)});
80+
return buf.len = conv.length;
81+
}
82+
}
83+
84+
//--------------------
85+
86+
function h$_hs_Text_ToExponential(value, buf, ndigits)
87+
{
88+
89+
if (ndigits > 120 || ndigits < -1) return -1;
90+
var conv = (ndigits == -1 ? value.toExponential() : value.toExponential(Math.min(20,ndigits)).replace("e",("0".repeat(Math.max(0,ndigits - 20))+"e"))).replace("e+","e");
91+
Array.prototype.map.call(conv,function(c,i){ buf.u1[i]=c.charCodeAt(0)});
92+
return buf.len = conv.length;
93+
}
94+
95+
function h$_hs_ToExponential(value, buf, unused, ndigits)
96+
{
97+
if (ndigits > 120 || ndigits < -1) return -1;
98+
var conv = (ndigits == -1 ? value.toExponential() : value.toExponential(Math.min(20,ndigits)).replace("e",("0".repeat(Math.max(0,ndigits - 20))+"e"))).replace("e+","e");
99+
Array.prototype.map.call(conv,function(c,i){ buf.u8[i]=c.charCodeAt(0)});
100+
return buf.len = conv.length;
101+
}
102+
103+
//--------------------
104+
105+
function h$_hs_Text_ToPrecision(value, buf, ndigits)
106+
{
107+
if (ndigits > 120 || ndigits < 1) return -1;
108+
var conv = (value.toPrecision(Math.min(21,ndigits))+ "0".repeat(Math.max(0,ndigits - 21))).replace("e+","e");
109+
Array.prototype.map.call(conv,function(c,i){ buf.u1[i]=c.charCodeAt(0)});
110+
return buf.len = conv.length;
111+
}
112+
113+
function h$_hs_ToPrecision(value, buf, unused, ndigits)
114+
{
115+
if (ndigits > 120 || ndigits < 1) return -1;
116+
var conv = (value.toPrecision(Math.min(21,ndigits))+ "0".repeat(Math.max(0,ndigits - 21))).replace("e+","e");
117+
Array.prototype.map.call(conv,function(c,i){ buf.u8[i]=c.charCodeAt(0)});
118+
return buf.len = conv.length;
119+
}

0 commit comments

Comments
 (0)