From 7b52c8c4a0ed663b6bca730b56633a64c9ce4c26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 20 Jul 2023 14:55:53 +0200 Subject: [PATCH] Do not use `-1` to mark uninitialized location numeric literals --- packages/transform/src/emit.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/transform/src/emit.js b/packages/transform/src/emit.js index 953da3ed1..ca78672c2 100644 --- a/packages/transform/src/emit.js +++ b/packages/transform/src/emit.js @@ -57,8 +57,13 @@ exports.Emitter = Emitter; // the amazingly convenient benefit of allowing the exact value of the // location to be determined at any time, even after generating code that // refers to the location. +// We use 'Number.MAX_VALUE' to mark uninitialized location. We can safely do +// so because no code can realistically have about 1.8e+308 locations before +// hitting memory limit of the machine it's running on. For comparison, the +// estimated number of atoms in the observable universe is around 1e+80. +let uninitializedLocation = Number.MAX_VALUE; Ep.loc = function() { - const l = util.getTypes().numericLiteral(-1) + const l = util.getTypes().numericLiteral(uninitializedLocation) this.insertedLocs.add(l); return l; } @@ -76,7 +81,7 @@ Ep.getContextId = function() { Ep.mark = function(loc) { util.getTypes().assertLiteral(loc); let index = this.listing.length; - if (loc.value === -1) { + if (loc.value === uninitializedLocation) { loc.value = index; } else { // Locations can be marked redundantly, but their values cannot change @@ -644,7 +649,7 @@ Ep.explodeStatement = function(path, labelId) { ); self.mark(after); - if (defaultLoc.value === -1) { + if (defaultLoc.value === uninitializedLocation) { self.mark(defaultLoc); assert.strictEqual(after.value, defaultLoc.value); } @@ -885,7 +890,7 @@ Ep.updateContextPrevLoc = function(loc) { if (loc) { t.assertLiteral(loc); - if (loc.value === -1) { + if (loc.value === uninitializedLocation) { // If an uninitialized location literal was passed in, set its value // to the current this.listing.length. loc.value = this.listing.length;