@harmoniclabs/plu-ts-onchain
Version:
An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript
118 lines (117 loc) • 4.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IRVar = void 0;
var BasePlutsError_1 = require("../../utils/BasePlutsError.js");
var concatUint8Arr_1 = require("../utils/concatUint8Arr.js");
var positiveIntAsBytes_1 = require("../utils/positiveIntAsBytes.js");
var isIRParentTerm_1 = require("../utils/isIRParentTerm.js");
var IRHash_1 = require("../IRHash.js");
var IRNodeKind_1 = require("../IRNodeKind.js");
var IRVar = /** @class */ (function () {
function IRVar(DeBruijn) {
Object.defineProperty(this, "meta", {
value: {},
writable: false,
enumerable: true,
configurable: false
});
this._hash = undefined;
DeBruijn = typeof DeBruijn === "number" ? DeBruijn : Number(DeBruijn);
this._dbn = DeBruijn;
// call setter to check for validity
this.dbn = DeBruijn;
this._parent = undefined;
}
Object.defineProperty(IRVar.prototype, "hash", {
get: function () {
if (!(0, IRHash_1.isIRHash)(this._hash)) {
this._hash = getVarHashAtDbn(this.dbn);
}
return this._hash;
},
enumerable: false,
configurable: true
});
/**
* called inside the dbn setter
*/
IRVar.prototype.markHashAsInvalid = function () {
var _a;
this._hash = undefined;
(_a = this.parent) === null || _a === void 0 ? void 0 : _a.markHashAsInvalid();
};
IRVar.prototype.isHashPresent = function () { return true; };
Object.defineProperty(IRVar.prototype, "dbn", {
/**
* the IR DeBruijn index is not necessarly the same of the UPLC
* ( more ofthen than not it won't be the same )
*
* this is because in the IR things like `IRLetted` and `IRHoisted`
* are skipping some DeBruijin levels that are instead present
* in the final UPLC
**/
get: function () { return this._dbn; },
set: function (newDbn) {
if (!(Number.isSafeInteger(newDbn) && newDbn >= 0)) {
// console.log( e.stack );
throw new BasePlutsError_1.BasePlutsError("invalid index for an `IRVar` instance; new DeBruijn was: " + newDbn);
}
if (newDbn === this._dbn)
return; // everything ok
this.markHashAsInvalid();
this._dbn = newDbn;
},
enumerable: false,
configurable: true
});
Object.defineProperty(IRVar, "kind", {
get: function () { return IRNodeKind_1.IRNodeKind.Var; },
enumerable: false,
configurable: true
});
Object.defineProperty(IRVar.prototype, "kind", {
get: function () { return IRVar.kind; },
enumerable: false,
configurable: true
});
Object.defineProperty(IRVar, "tag", {
get: function () { return new Uint8Array([IRVar.kind]); },
enumerable: false,
configurable: true
});
Object.defineProperty(IRVar.prototype, "parent", {
get: function () { return this._parent; },
set: function (newParent) {
if (!( // assert
// new parent value is different than current
this._parent !== newParent && (
// and the new parent value is valid
newParent === undefined ||
(0, isIRParentTerm_1.isIRParentTerm)(newParent))))
return;
// change parent
this._parent = newParent;
},
enumerable: false,
configurable: true
});
IRVar.prototype.clone = function () {
return new IRVar(this.dbn);
};
IRVar.prototype.toJSON = function () { return this.toJson(); };
IRVar.prototype.toJson = function () {
return {
type: "IRVar",
dbn: this.dbn
};
};
return IRVar;
}());
exports.IRVar = IRVar;
var bdnVarHashCache = [];
function getVarHashAtDbn(dbn) {
while ((bdnVarHashCache.length - 1) < dbn) {
bdnVarHashCache.push((0, IRHash_1.hashIrData)((0, concatUint8Arr_1.concatUint8Arr)(IRVar.tag, (0, positiveIntAsBytes_1.positiveIntAsBytes)(bdnVarHashCache.length))));
}
return bdnVarHashCache[dbn];
}