Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/LuaLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export enum LuaLibFeature {
NumberIsFinite = "NumberIsFinite",
NumberIsNaN = "NumberIsNaN",
NumberToString = "NumberToString",
NumberToFixed = "NumberToFixed",
ObjectAssign = "ObjectAssign",
ObjectDefineProperty = "ObjectDefineProperty",
ObjectEntries = "ObjectEntries",
Expand Down
14 changes: 14 additions & 0 deletions src/lualib/NumberToFixed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// https://www.ecma-international.org/ecma-262/10.0/index.html#sec-number.prototype.tofixed
export function __TS__NumberToFixed(this: number, fractionDigits?: number): string {
if (Math.abs(this) >= 1e21 || this !== this) {
return this.toString();
}
const f = Math.floor(fractionDigits ?? 0);
// reduced to 99 as string.format only supports 2-digit numbers
if (f < 0 || f > 99) {
throw "toFixed() digits argument must be between 0 and 99";
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should errors be exaclty like in JS (between 0 and 100) or this is fine?

}
// throws "invalid format (width or precision too long)" if strlen > 99
// if (f < 80) return fmt; else try {return fmt} catch(_) { throw "toFixed() digits argument..." }
return string.format(`%.${f}f`, this);
}
2 changes: 2 additions & 0 deletions src/transformation/builtins/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export function transformNumberPrototypeCall(
return params.length === 0
? lua.createCallExpression(lua.createIdentifier("tostring"), [caller], node)
: transformLuaLibFunction(context, LuaLibFeature.NumberToString, node, caller, ...params);
case "toFixed":
return transformLuaLibFunction(context, LuaLibFeature.NumberToFixed, node, caller, ...params);
default:
context.diagnostics.push(unsupportedProperty(calledMethod.name, "number", expressionName));
}
Expand Down
20 changes: 20 additions & 0 deletions test/unit/builtins/numbers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ test.each([
util.testExpressionTemplate`(${value}).toString(2)`.expectToEqual(luaNativeSpecialNumString);
});

const toFixedFractions = [undefined, 0, 1, 2, Math.PI, 5, 99];
// 1.5, 1.25 and 1.125 fails as rounding differ
const toFixedValues = [-1, 0, 1, Math.PI, -1.1234, -9.99e19, 1e22];
const toFixedPairs = toFixedValues.flatMap(value => toFixedFractions.map(frac => [value, frac] as const));
test.each(toFixedPairs)("(%p).toFixed(%p)", (value, frac) => {
util.testExpressionTemplate`(${value}).toFixed(${frac})`.expectToMatchJsResult();
});

test.each([
[NaN, "(0/0)"],
[Infinity, "(1/0)"],
[-Infinity, "(-(1/0))"],
])("%p.toFixed(2)", (value, luaNativeSpecialNum) => {
// Need to get the actual lua tostring version of inf/nan
// this is platform dependent so we can/should not hardcode it
const luaNativeSpecialNumString = util.testExpression`${luaNativeSpecialNum}.toString()`.getLuaExecutionResult();
// Cannot use expectToMatchJsResult because this actually wont be the same in JS in Lua
util.testExpressionTemplate`(${value}).toFixed(2)`.expectToEqual(luaNativeSpecialNumString);
});

test.each(cases)("isNaN(%p)", value => {
util.testExpressionTemplate`isNaN(${value} as any)`.expectToMatchJsResult();
});
Expand Down