Patashu / break_eternity.js

A Javascript numerical library to represent numbers as large as 10^^1e308 and as small as 10^-10^^1e308. Sequel to break_infinity.js, designed for incremental games.
MIT License
120 stars 43 forks source link

Throws a TypeError with standard EcmaScript class fields #169

Open jakub791 opened 3 months ago

jakub791 commented 3 months ago

What is the issue?

Break eternity uses static class fields to initialize the Decimal constants like:

public static readonly dZero = FC_NN(0, 0, 0);

FC_NN` is defined as:

let FC_NN = function FC_NN(sign: number, layer: number, mag: number) {
    return Decimal.fromComponents_noNormalize(sign, layer, mag);
};

Decimal.fromComponents_noNormalize invokes the Decimal class constructor (new Decimal()). But apparently static class fields initializers run before the class is ready to be used as a constructor which causes it to throw a TypeError.

How to reproduce?

Clone this repository, in tsconfig.json change target to "esnext", transpile the library source code using tsc and run the output file in literally any JS runtime that supports ES2022 and you shouldsee an error like Uncaught ReferenceError: can't access lexical declaration 'Decimal' before initialization

note: I feel like this isn't a very great explantion of all this, if you need further info just ask me.