Skip to content

Commit

Permalink
Merge pull request #680 from nicolo-ribaudo/no-neg-1-numericliteral
Browse files Browse the repository at this point in the history
Do not use `-1` to mark uninitialized location numeric literals
  • Loading branch information
benjamn committed Aug 4, 2023
2 parents c24b7b7 + 82331fb commit 01365c4
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/transform/src/emit.js
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.
const PENDING_LOCATION = Number.MAX_VALUE;
Ep.loc = function() {
const l = util.getTypes().numericLiteral(-1)
const l = util.getTypes().numericLiteral(PENDING_LOCATION)
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 === PENDING_LOCATION) {
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 === PENDING_LOCATION) {
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 === PENDING_LOCATION) {
// 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 01365c4

Please sign in to comment.