Skip to content

Commit c32f309

Browse files
committed
Work around lack of support for std::isfinite() on older platforms
1 parent fed2e37 commit c32f309

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

‎VM/src/llsl.cpp‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525
# pragma STDC FP_CONTRACT OFF
2626
#endif
2727

28+
#if defined(_MSC_VER) && _MSC_VER >= 1200 && _MSC_VER < 1800
29+
# include <float.h>
30+
# define isfinite _finite
31+
#elif defined(__sun) && defined(__SVR4) //Solaris
32+
# include <ieeefp.h>
33+
# define isfinite finite
34+
#else
35+
# define isfinite std::isfinite
36+
#endif
37+
2838

2939
constexpr size_t UUID_STR_LENGTH = 37;
3040
constexpr int UUID_BYTES = 16;
@@ -195,22 +205,25 @@ static int _lsl_cast_internal(lua_State* L, bool in_list, bool neg_zero, bool ni
195205
switch(to_lsl_type)
196206
{
197207
case LSLIType::LST_INTEGER:
208+
{
198209
// If this seems weird, that's because it is. Mono truncates to 32-bit float
199210
// before converting to integer. You'll note that we cast to int64_t first before
200211
// truncating to int32_t, which also seems odd. We need that to emulate x86-64
201212
// integer wraparound on AArch64, or `print(((integer)((float)0x7FffFFfe)))`
202213
// will print 2147483647 rather than -2147483648. See
203214
// https://stackoverflow.com/questions/66279679/casting-float-to-int-with-wrap-around-on-aarch64-arm64
204-
if (std::isfinite(nvalue(val)))
215+
float nval = (float)nvalue(val);
216+
if (isfinite(nval))
205217
{
206-
setintvalue(&new_tv, (int32_t)((int64_t)((float)nvalue(val))));
218+
setintvalue(&new_tv, (int32_t)((int64_t)(nval)));
207219
}
208220
else
209221
{
210222
// Mono treats non-finite values as INT32_MIN.
211223
setintvalue(&new_tv, INT32_MIN);
212224
}
213225
break;
226+
}
214227
case LSLIType::LST_STRING:
215228
{
216229
// Again, truncates to float first.

‎VM/src/lvmexecute.cpp‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
#endif
2525
#endif
2626

27+
#if defined(_MSC_VER) && _MSC_VER >= 1200 && _MSC_VER < 1800
28+
# include <float.h>
29+
# define isfinite _finite
30+
#elif defined(__sun) && defined(__SVR4) //Solaris
31+
# include <ieeefp.h>
32+
# define isfinite finite
33+
#else
34+
# define isfinite std::isfinite
35+
#endif
36+
2737
// When working with VM code, pay attention to these rules for correctness:
2838
// 1. Many external Lua functions can fail; for them to fail and be able to generate a proper stack, we need to copy pc to L->ci->savedpc before the
2939
// call
@@ -365,9 +375,10 @@ static void luau_execute(lua_State* L)
365375
{
366376
// float -> int (matches lsl_cast() and avoids AArch64 weirdness)
367377
LUAU_ASSERT(ttisnumber(rb));
368-
if (std::isfinite(nvalue(rb)))
378+
float nval = (float)nvalue(rb);
379+
if (isfinite(nval))
369380
{
370-
setintvalue(ra, (int32_t)((int64_t)((float)nvalue(rb))));
381+
setintvalue(ra, (int32_t)((int64_t)(nval)));
371382
}
372383
else
373384
{

0 commit comments

Comments
 (0)