summaryrefslogtreecommitdiff
path: root/src/lstrlib.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-03-28 08:57:21 +0200
committerBenoit Giannangeli <giann008@gmail.com>2017-03-28 10:13:07 +0200
commitca46ad67f398f219cf838feb10127cd0cecc72d8 (patch)
tree54b836e0c4427896e1a004b56a45f7ea967ab26c /src/lstrlib.js
parentc34c0ad9ea78d0a40b0aa0716d9096f961681a97 (diff)
downloadfengari-ca46ad67f398f219cf838feb10127cd0cecc72d8.tar.gz
fengari-ca46ad67f398f219cf838feb10127cd0cecc72d8.tar.bz2
fengari-ca46ad67f398f219cf838feb10127cd0cecc72d8.zip
lua_strx2number
Diffstat (limited to 'src/lstrlib.js')
-rw-r--r--src/lstrlib.js28
1 files changed, 2 insertions, 26 deletions
diff --git a/src/lstrlib.js b/src/lstrlib.js
index 6e54d4d..471187b 100644
--- a/src/lstrlib.js
+++ b/src/lstrlib.js
@@ -85,29 +85,6 @@ const SIZELENMOD = luaconf.LUA_NUMBER_FRMLEN.length + 1;
const L_NBFD = 1;
-// See: http://croquetweak.blogspot.fr/2014/08/deconstructing-floats-frexp-and-ldexp.html
-const frexp = function(value) {
- if (value === 0) return [value, 0];
- var data = new DataView(new ArrayBuffer(8));
- data.setFloat64(0, value);
- var bits = (data.getUint32(0) >>> 20) & 0x7FF;
- if (bits === 0) { // denormal
- data.setFloat64(0, value * Math.pow(2, 64)); // exp + 64
- bits = ((data.getUint32(0) >>> 20) & 0x7FF) - 64;
- }
- var exponent = bits - 1022;
- var mantissa = ldexp(value, -exponent);
- return [mantissa, exponent];
-};
-
-const ldexp = function(mantissa, exponent) {
- var steps = Math.min(3, Math.ceil(Math.abs(exponent) / 1023));
- var result = mantissa;
- for (var i = 0; i < steps; i++)
- result *= Math.pow(2, Math.floor((exponent + i) / steps));
- return result;
-};
-
/*
** Add integer part of 'x' to buffer and return new 'x'
*/
@@ -126,7 +103,7 @@ const num2straux = function(x) {
/* create "0" or "-0" followed by exponent */
return sprintf(luaconf.LUA_NUMBER_FMT + "x0p+0", x).split('').map(e => e.charCodeAt(0));
} else {
- let fe = frexp(x); /* 'x' fraction and exponent */
+ let fe = lobject.frexp(x); /* 'x' fraction and exponent */
let m = fe[0];
let e = fe[1];
let n = 0; /* character count */
@@ -241,8 +218,7 @@ const addliteral = function(L, b, arg) {
checkdp(b); /* ensure it uses a dot */
} else { /* integers */
let n = lapi.lua_tointeger(L, arg);
- let format = (n === Number.MIN_SAFE_INTEGER) ? `0x%${luaconf.LUA_INTEGER_FRMLEN}x` : luaconf.LUA_INTEGER_FMT;
- concat(b, lua.to_luastring(sprintf(format, n)));
+ concat(b, lua.to_luastring(sprintf("%d", n)));
}
break;
}