@harmoniclabs/plu-ts-onchain
Version:
An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript
155 lines (154 loc) • 6.32 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IRCase = void 0;
var concatUint8Arr_1 = require("../utils/concatUint8Arr.js");
var isIRParentTerm_1 = require("../utils/isIRParentTerm.js");
var mapArrayLike_1 = require("./utils/mapArrayLike.js");
var utils_1 = require("../utils/index.js");
var makeArrayLikeProxy_1 = require("./utils/makeArrayLikeProxy.js");
var IRHash_1 = require("../IRHash.js");
var equalIRTerm_1 = require("../utils/equalIRTerm.js");
var IRNodeKind_1 = require("../IRNodeKind.js");
var IRCase = /** @class */ (function () {
function IRCase(constrTerm, continuations, meta, _unsafeHash) {
if (meta === void 0) { meta = {}; }
var self = this;
this.meta = meta;
this._constrTerm = constrTerm;
this._constrTerm.parent = self;
Object.defineProperty(this, "continuations", {
value: (0, makeArrayLikeProxy_1.makeArrayLikeProxy)(continuations, utils_1.isIRTerm,
// initModifyElem
// function called once for each element in the array
// only at element definition
function (newElem) {
newElem.parent = self;
return newElem;
},
// modifyElem
function (newElem, oldElem) {
// keep the parent reference in the old child, useful for compilation
// oldElem.parent = undefined;
newElem.parent = self;
if (!(0, equalIRTerm_1.shallowEqualIRTermHash)(oldElem, newElem))
self.markHashAsInvalid();
return newElem;
}),
writable: false,
enumerable: true,
configurable: false
});
this._hash = (0, IRHash_1.isIRHash)(_unsafeHash) ? _unsafeHash : undefined;
}
Object.defineProperty(IRCase, "kind", {
get: function () { return IRNodeKind_1.IRNodeKind.Case; },
enumerable: false,
configurable: true
});
Object.defineProperty(IRCase.prototype, "kind", {
get: function () { return IRCase.kind; },
enumerable: false,
configurable: true
});
Object.defineProperty(IRCase, "tag", {
get: function () { return new Uint8Array([IRCase.kind]); },
enumerable: false,
configurable: true
});
Object.defineProperty(IRCase.prototype, "hash", {
get: function () {
if (!(0, IRHash_1.isIRHash)(this._hash)) {
// basically a merkle tree
this._hash = (0, IRHash_1.hashIrData)(concatUint8Arr_1.concatUint8Arr.apply(void 0, __spreadArray([IRCase.tag,
this._constrTerm.hash], __read((0, mapArrayLike_1.mapArrayLike)(this.continuations, function (f) { return f.hash; })), false)));
}
return this._hash;
},
enumerable: false,
configurable: true
});
IRCase.prototype.markHashAsInvalid = function () {
var _a;
this._hash = undefined;
(_a = this.parent) === null || _a === void 0 ? void 0 : _a.markHashAsInvalid();
};
IRCase.prototype.isHashPresent = function () { return (0, IRHash_1.isIRHash)(this._hash); };
Object.defineProperty(IRCase.prototype, "constrTerm", {
get: function () { return this._constrTerm; },
set: function (newConstrTerm) {
if (!(0, utils_1.isIRTerm)(newConstrTerm))
return;
if (!(0, equalIRTerm_1.shallowEqualIRTermHash)(this._constrTerm, newConstrTerm))
this.markHashAsInvalid();
// keep the parent reference in the old child, useful for compilation
// constrTerm.parent = undefined;
this._constrTerm = newConstrTerm;
this._constrTerm.parent = this;
},
enumerable: false,
configurable: true
});
Object.defineProperty(IRCase.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
});
IRCase.prototype.clone = function () {
return new IRCase(this.constrTerm, (0, mapArrayLike_1.mapArrayLike)(this.continuations, function (f) { return f.clone(); }), __assign({}, this.meta), this.isHashPresent() ? this.hash : undefined);
};
IRCase.prototype.toJSON = function () { return this.toJson(); };
IRCase.prototype.toJson = function () {
return {
constrTerm: this.constrTerm.toString(),
continuations: (0, mapArrayLike_1.mapArrayLike)(this.continuations, function (f) { return f.toJson(); })
};
};
return IRCase;
}());
exports.IRCase = IRCase;