mirror of
https://github.com/luau-lang/luau.git
synced 2025-05-04 10:33:46 +01:00
Implement built-in for bit32.byteswap
This commit is contained in:
parent
e34b0160f1
commit
f6c07e2032
3 changed files with 30 additions and 0 deletions
|
@ -560,6 +560,9 @@ enum LuauBuiltinFunction
|
|||
// tonumber/tostring
|
||||
LBF_TONUMBER,
|
||||
LBF_TOSTRING,
|
||||
|
||||
// bit32.byteswap(n)
|
||||
LBF_BIT32_BYTESWAP,
|
||||
};
|
||||
|
||||
// Capture type, used in LOP_CAPTURE
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
#include "Luau/Bytecode.h"
|
||||
#include "Luau/Compiler.h"
|
||||
|
||||
LUAU_FASTFLAG(LuauBit32ByteswapBuiltin)
|
||||
|
||||
namespace Luau
|
||||
{
|
||||
namespace Compile
|
||||
|
@ -166,6 +168,11 @@ static int getBuiltinFunctionId(const Builtin& builtin, const CompileOptions& op
|
|||
return LBF_BIT32_COUNTLZ;
|
||||
if (builtin.method == "countrz")
|
||||
return LBF_BIT32_COUNTRZ;
|
||||
if (builtin.method == "byteswap")
|
||||
{
|
||||
LUAU_ASSERT(FFlag::LuauBit32ByteswapBuiltin);
|
||||
return LBF_BIT32_BYTESWAP;
|
||||
}
|
||||
}
|
||||
|
||||
if (builtin.object == "string")
|
||||
|
@ -402,6 +409,9 @@ BuiltinInfo getBuiltinInfo(int bfid)
|
|||
|
||||
case LBF_TOSTRING:
|
||||
return {1, 1};
|
||||
|
||||
case LBF_BIT32_BYTESWAP:
|
||||
return {1, 1, BuiltinInfo::Flag_NoneSafe};
|
||||
};
|
||||
|
||||
LUAU_UNREACHABLE();
|
||||
|
|
|
@ -1319,6 +1319,21 @@ static int luauF_tostring(lua_State* L, StkId res, TValue* arg0, int nresults, S
|
|||
return -1;
|
||||
}
|
||||
|
||||
static int luauF_byteswap(lua_State* L, StkId res, TValue* arg0, int nresults, StkId args, int nparams)
|
||||
{
|
||||
if (nparams >= 1 && nresults <= 1 && ttisnumber(arg0))
|
||||
{
|
||||
double a1 = nvalue(arg0);
|
||||
unsigned n;
|
||||
luai_num2unsigned(n, a1);
|
||||
|
||||
setnvalue(res, double(((n >> 24) & 0xff) | ((n << 8) & 0xff0000) | ((n >> 8) & 0xff00) | ((n << 24) & 0xff000000)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int luauF_missing(lua_State* L, StkId res, TValue* arg0, int nresults, StkId args, int nparams)
|
||||
{
|
||||
return -1;
|
||||
|
@ -1486,6 +1501,8 @@ const luau_FastFunction luauF_table[256] = {
|
|||
luauF_tonumber,
|
||||
luauF_tostring,
|
||||
|
||||
luauF_byteswap,
|
||||
|
||||
// When adding builtins, add them above this line; what follows is 64 "dummy" entries with luauF_missing fallback.
|
||||
// This is important so that older versions of the runtime that don't support newer builtins automatically fall back via luauF_missing.
|
||||
// Given the builtin addition velocity this should always provide a larger compatibility window than bytecode versions suggest.
|
||||
|
|
Loading…
Add table
Reference in a new issue