UNPKG

@harmoniclabs/plu-ts-onchain

Version:

An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript

121 lines (120 loc) 4.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IRFunc = void 0; var BasePlutsError_1 = require("../../utils/BasePlutsError.js"); var concatUint8Arr_1 = require("../utils/concatUint8Arr.js"); var isIRTerm_1 = require("../utils/isIRTerm.js"); var positiveIntAsBytes_1 = require("../utils/positiveIntAsBytes.js"); var isIRParentTerm_1 = require("../utils/isIRParentTerm.js"); var IRHash_1 = require("../IRHash.js"); var equalIRTerm_1 = require("../utils/equalIRTerm.js"); var IRNodeKind_1 = require("../IRNodeKind.js"); var IRFunc = /** @class */ (function () { function IRFunc(arity, body, func_name, _unsafeHash) { if (!Number.isSafeInteger(arity) && arity >= 1) throw new BasePlutsError_1.BasePlutsError("invalid arity for 'IRFunc'"); if (!(0, isIRTerm_1.isIRTerm)(body)) throw new Error("IRFunc body argument was not an IRTerm"); Object.defineProperties(this, { arity: { value: arity, writable: false, enumerable: true, configurable: false }, meta: { value: { name: typeof func_name === "string" ? func_name : (void 0) }, writable: true, enumerable: true, configurable: false } }); this._body = body; this._body.parent = this; this._hash = (0, IRHash_1.isIRHash)(_unsafeHash) ? _unsafeHash : undefined; this._parent = undefined; } Object.defineProperty(IRFunc, "kind", { get: function () { return IRNodeKind_1.IRNodeKind.Func; }, enumerable: false, configurable: true }); Object.defineProperty(IRFunc.prototype, "kind", { get: function () { return IRFunc.kind; }, enumerable: false, configurable: true }); Object.defineProperty(IRFunc, "tag", { get: function () { return new Uint8Array([IRFunc.kind]); }, enumerable: false, configurable: true }); Object.defineProperty(IRFunc.prototype, "body", { get: function () { return this._body; }, set: function (newBody) { if (!(0, isIRTerm_1.isIRTerm)(newBody)) { throw new BasePlutsError_1.BasePlutsError("invalid IRTerm to be a function body"); } if (!(0, equalIRTerm_1.shallowEqualIRTermHash)(this._body, newBody)) this.markHashAsInvalid(); // keep the parent reference in the old child, useful for compilation // _body.parent = undefined; this._body = newBody; this._body.parent = this; }, enumerable: false, configurable: true }); Object.defineProperty(IRFunc.prototype, "hash", { get: function () { if (!(0, IRHash_1.isIRHash)(this._hash)) { this._hash = (0, IRHash_1.hashIrData)((0, concatUint8Arr_1.concatUint8Arr)(IRFunc.tag, (0, positiveIntAsBytes_1.positiveIntAsBytes)(this.arity), this._body.hash)); } return this._hash; }, enumerable: false, configurable: true }); IRFunc.prototype.isHashPresent = function () { return (0, IRHash_1.isIRHash)(this._hash); }; IRFunc.prototype.markHashAsInvalid = function () { var _a; this._hash = undefined; (_a = this.parent) === null || _a === void 0 ? void 0 : _a.markHashAsInvalid(); }; Object.defineProperty(IRFunc.prototype, "name", { get: function () { return this.meta.name; }, enumerable: false, configurable: true }); ; Object.defineProperty(IRFunc.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; this._parent = newParent; }, enumerable: false, configurable: true }); IRFunc.prototype.clone = function () { return new IRFunc(this.arity, this._body.clone(), this.meta.name, this.isHashPresent() ? this.hash : undefined); }; IRFunc.prototype.toJSON = function () { return this.toJson(); }; IRFunc.prototype.toJson = function () { return { type: "IRFunc", arity: this.arity, body: this._body.toJson() }; }; return IRFunc; }()); exports.IRFunc = IRFunc;