SLua functions to replicate lsl's (integer) and (float) casts
planned
Tapple Gao
LSL has these casting functions. They are not exposed directly in SLua. Here's what I've been replacing them with:
- (integer)x->math.modf(tonumber(x) or 0)
- (float)x->tonumber(x) or 0
- (string)x->tostring(x)
- (vector)x->tovector(x) or ZERO_VECTOR
- (rotation)x->toquaternion(x) or ZERO_ROTATION
- (key)x->touuid(x) or NULL_KEY
My
(string)
, (vector)
, and (rotation)
replacements seem to behave identically in all cases I've thought of. My (key)
replacement behaves identically in all cases I actually care about. It's behavior doesn't match when the string is not a valid uuid, but, I actually prefer the new behavior, so I'm happy with the replacement.However, the
(integer)
replacement is a bit cumbersome. Additionally, (integer)
and (float)
have some very convenient behavior when you're parsing real notecards that aren't trivial to replicate using the luau standard library. My replacements get close, but don't behave the same in all cases:default { state_entry() {
llOwnerSay((string)((integer)"0xf")); // 15
llOwnerSay((string)((integer)" 99.5 red baloons")); // 99
llOwnerSay((string)((float)" 99.5 red baloons")); // 99.500000
llOwnerSay((string)((float)" -99e2 red baloons")); // -9900.000000
}}
In SLua, we still have indirect access to this behavior via
ll.List2Integer
and ll.List2Float
:print(ll.List2Integer({"0xf"}, 1)) -- 15
print(ll.List2Integer({" 99.5 red baloons"}, 1)) -- 99
print(ll.List2Float({" 99.5 red baloons"}, 1)) -- 99.5
print(ll.List2Float({" -99.5e2 red baloons"}, 1)) -- -9950
print(tonumber("0xf")) -- 15
print(tonumber(" 99.5 red baloons")) -- nil
print(tonumber(" -99.5e2 red baloons")) -- nil
print(tonumber("-99.5e2")) -- -9950
But it would be nice to have a direct conversion, or even better, one that replicated all the oddities of the lsl casts, but returned
nil
rather than 0
on failure. Also, such functions might be necessary for the future LSL -> SLua transpilerLog In
Tapple Gao
was just looking thru old announcements. It looks like some behavior like this was intentionally removed from tovector and toquaternion: https://feedback.secondlife.com/slua-announcements/p/slua-alpha-release-notes-may-1-2025
I'm not sure how good an idea this proposal actually is. I think I mostly just wish there was a
string.trim
function in addition to ll.StringTrim
I'm currently leaning toward making my notecard parsing stricter now that error messages are easier thanks to
error
and pcall
H
Harold Linden
marked this post as
planned
Tapple Gao
This seems to fully replicate all the behavior of
(integer)x
I know about:math.modf(tonumber(if typeof(x) == "string" then string.match(x, "[-+]?[x%x]+") else x) or 0)
(float)x
replacement:tonumber(if typeof(x) == "string" then string.match(x, "[-+]?[.e%d]+") else x) or 0