@harmoniclabs/plu-ts-onchain
Version:
An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript
157 lines (156 loc) • 5.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IRSelfCall = 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 IRRecursive_1 = require("./IRRecursive.js");
var _1 = require("./index.js");
var IRSelfCall = /** @class */ (function () {
function IRSelfCall(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(IRSelfCall.prototype, "hash", {
get: function () {
if (!(0, IRHash_1.isIRHash)(this._hash)) {
this._hash = getSelfCallHashAtDbn(this.dbn);
}
return this._hash;
},
enumerable: false,
configurable: true
});
/**
* called inside the dbn setter
*/
IRSelfCall.prototype.markHashAsInvalid = function () {
var _a;
this._hash = undefined;
(_a = this.parent) === null || _a === void 0 ? void 0 : _a.markHashAsInvalid();
};
IRSelfCall.prototype.isHashPresent = function () { return true; };
Object.defineProperty(IRSelfCall.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 `plet`
* 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)) {
throw new BasePlutsError_1.BasePlutsError("invalid index for an `IRSelfCall` instance; new DeBruijn was: " + newDbn);
}
if (newDbn === this._dbn)
return; // everything ok
this.markHashAsInvalid();
this._dbn = newDbn;
},
enumerable: false,
configurable: true
});
Object.defineProperty(IRSelfCall, "kind", {
get: function () { return IRNodeKind_1.IRNodeKind.SelfCall; },
enumerable: false,
configurable: true
});
Object.defineProperty(IRSelfCall.prototype, "kind", {
get: function () { return IRSelfCall.kind; },
enumerable: false,
configurable: true
});
Object.defineProperty(IRSelfCall, "tag", {
get: function () { return new Uint8Array([IRSelfCall.kind]); },
enumerable: false,
configurable: true
});
Object.defineProperty(IRSelfCall.prototype, "definition", {
get: function () {
if (!(this._definition instanceof IRRecursive_1.IRRecursive)) {
this._definition = this._findDefinition();
}
return this._definition;
},
set: function (newDefinition) {
if (newDefinition === undefined) {
this._definition = undefined;
return;
}
else
throw new Error("IRSelfCall definition (IRRecursive parent) can't be changed unless set to undefined");
},
enumerable: false,
configurable: true
});
Object.defineProperty(IRSelfCall.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;
// if we change parent, _definition should be recalculated
this._definition = undefined;
},
enumerable: false,
configurable: true
});
IRSelfCall.prototype._findDefinition = function () {
var node = this;
var dbn = this.dbn;
while (node.parent !== undefined) {
node = node.parent;
if (node instanceof IRRecursive_1.IRRecursive ||
node instanceof _1.IRFunc) {
if (dbn === 0) {
if (node.parent instanceof IRRecursive_1.IRRecursive)
return node.parent;
else
throw new Error("IRSelfCall was pointing to a IRFunc node");
}
dbn -= node.arity;
}
}
throw new Error("IRSelfCall instance was not inside a IRRecursive instance");
};
IRSelfCall.prototype.clone = function () {
return new IRSelfCall(this.dbn);
};
IRSelfCall.prototype.toJSON = function () { return this.toJson(); };
IRSelfCall.prototype.toJson = function () {
return {
type: "IRSelfCall",
dbn: this.dbn
};
};
return IRSelfCall;
}());
exports.IRSelfCall = IRSelfCall;
var dbnSelfCallCache = {};
function getSelfCallHashAtDbn(dbn) {
if (!(0, IRHash_1.isIRHash)(dbnSelfCallCache[dbn])) {
dbnSelfCallCache[dbn] = ((0, IRHash_1.hashIrData)((0, concatUint8Arr_1.concatUint8Arr)(IRSelfCall.tag, (0, positiveIntAsBytes_1.positiveIntAsBytes)(dbn))));
}
return dbnSelfCallCache[dbn];
}