Skip to content

Commit

Permalink
Do not use -1 to mark uninitialized location numeric literals
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jul 20, 2023
1 parent c24b7b7 commit 7b52c8c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/transform/src/emit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7b52c8c

Please sign in to comment.