Skip to content

Commit dec7e0f

Browse files
authored
Convert round doubles to fixnums instead of floats (#349)
* Convert round doubles to fixnums instead of floats Fixes: #348 * squash! tweak for large numbers
1 parent a361827 commit dec7e0f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

ext/mini_racer_extension/mini_racer_extension.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include <stdatomic.h>
2+
#include <stdint.h>
23
#include <stdio.h>
34
#include <stdlib.h>
45
#include <string.h>
56
#include <pthread.h>
7+
#include <math.h>
68

79
#include "ruby.h"
810
#include "ruby/encoding.h"
@@ -282,7 +284,11 @@ static void des_int(void *arg, int64_t v)
282284

283285
static void des_num(void *arg, double v)
284286
{
285-
put(arg, DBL2NUM(v));
287+
if (isfinite(v) && v == trunc(v) && v >= INT64_MIN && v <= INT64_MAX) {
288+
put(arg, LONG2FIX(v));
289+
} else {
290+
put(arg, DBL2NUM(v));
291+
}
286292
}
287293

288294
static void des_date(void *arg, double v)

test/mini_racer_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,4 +1144,14 @@ def test_termination_exception
11441144
a.kill
11451145
b.kill
11461146
end
1147+
1148+
def test_large_integer
1149+
[10_000_000_001, -2**63, 2**63-1].each { |big_int|
1150+
context = MiniRacer::Context.new
1151+
context.attach("test", proc { big_int })
1152+
result = context.eval("test()")
1153+
assert_equal(result.class, big_int.class)
1154+
assert_equal(result, big_int)
1155+
}
1156+
end
11471157
end

0 commit comments

Comments
 (0)