Skip to content

Commit

Permalink
Add binding for GHCJS
Browse files Browse the repository at this point in the history
implement javascript versions under the assumption that engines where we run the code
will have V8 compatibile implementation. NodeJS looks fine.
  • Loading branch information
tolysz committed Jan 11, 2017
1 parent e4b7956 commit a481b2b
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions double-conversion.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ library
double-conversion/src/fast-dtoa.cc
double-conversion/src/fixed-dtoa.cc
double-conversion/src/strtod.cc
js-sources:
jsbits/hs-double-conversion.js

if os(windows)
if arch(x86_64)
Expand Down
92 changes: 92 additions & 0 deletions jsbits/hs-double-conversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

var kToFixedLength =
1 + /* DoubleToStringConverter::kMaxFixedDigitsBeforePoint*/ 60 +
1 + /* DoubleToStringConverter::kMaxFixedDigitsAfterPoint*/ 60;


var kToShortestLength = 26;
var kToExponentialLength = /*DoubleToStringConverter::kMaxExponentialDigits*/ 120 + 8;
var kToPrecisionLength = /*DoubleToStringConverter::kMaxPrecisionDigits*/ 120 + 7;

function h$_hs_ToShortestLength() {
return kToShortestLength;
}

function h$_hs_ToFixedLength()
{
return kToFixedLength;
}

function h$_hs_ToExponentialLength()
{
return kToExponentialLength;
}

function h$_hs_ToPrecisionLength()
{
return kToPrecisionLength;
}

//--------------------

function h$_hs_Text_ToShortest(value, buf)
{
var conv = value.toString().replace("e+","e");
Array.prototype.map.call(conv,function(c,i){ buf.u1[i]=c.charCodeAt(0)});
return buf.len = conv.length;
}

function h$_hs_ToShortest(value, buf)
{
var conv = value.toString().replace("e+","e");
Array.prototype.map.call(conv,function(c,i){ buf.u8[i]=c.charCodeAt(0)});
return buf.len = conv.length;
}

//--------------------

function h$_hs_Text_ToFixed(value, buf, ndigits)
{
var conv = value.toFixed(Math.min(20,ndigits))+ "0".repeat(Math.max(0,ndigits - 20));
Array.prototype.map.call(conv,function(c,i){ buf.u1[i]=c.charCodeAt(0)});
return buf.len = conv.length;
}

function h$_hs_ToFixed(value, buf, unused, ndigits)
{
var conv = value.toFixed(Math.min(20,ndigits))+ "0".repeat(Math.max(0,ndigits - 20));
Array.prototype.map.call(conv,function(c,i){ buf.u8[i]=c.charCodeAt(0)});
return buf.len = conv.length;
}

//--------------------

function h$_hs_Text_ToExponential(value, buf, ndigits)
{
var conv = value.toExponential(ndigits).replace("e+","e");
Array.prototype.map.call(conv,function(c,i){ buf.u1[i]=c.charCodeAt(0)});
return buf.len = conv.length;
}

function h$_hs_ToExponential(value, buf, unused, ndigits)
{
var conv = value.toExponential(ndigits).replace("e+","e");
Array.prototype.map.call(conv,function(c,i){ buf.u8[i]=c.charCodeAt(0)});
return buf.len = conv.length;
}

//--------------------

function h$_hs_Text_ToPrecision(value, buf, ndigits)
{
var conv = value.toPrecision(Math.min(21,ndigits)) + "0".repeat(Math.max(0,ndigits - 21));
Array.prototype.map.call(conv,function(c,i){ buf.u1[i]=c.charCodeAt(0)});
return buf.len = conv.length;
}

function h$_hs_ToPrecision(value, buf, unused, ndigits)
{
var conv = value.toPrecision(Math.min(21,ndigits))+ "0".repeat(Math.max(0,ndigits - 21));
Array.prototype.map.call(conv,function(c,i){ buf.u8[i]=c.charCodeAt(0)});
return buf.len = conv.length;
}

0 comments on commit a481b2b

Please sign in to comment.