@harmoniclabs/plu-ts-onchain
Version:
An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript
137 lines (136 loc) • 5.25 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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IRApp = void 0;
var concatUint8Arr_1 = require("../utils/concatUint8Arr.js");
var isIRTerm_1 = require("../utils/isIRTerm.js");
var isIRParentTerm_1 = require("../utils/isIRParentTerm.js");
var IRHash_1 = require("../IRHash.js");
var obj_utils_1 = require("@harmoniclabs/obj-utils");
var equalIRTerm_1 = require("../utils/equalIRTerm.js");
var IRNodeKind_1 = require("../IRNodeKind.js");
var IRApp = /** @class */ (function () {
function IRApp(_fn_, _arg_, meta, _unsafeHash) {
if (meta === void 0) { meta = undefined; }
if (!(0, isIRTerm_1.isIRTerm)(_fn_))
throw new Error("invalid function node for `IRApp`");
if (!(0, isIRTerm_1.isIRTerm)(_arg_))
throw new Error("invalid argument node for `IRApp`");
this._meta = meta;
this._fn = _fn_;
this._arg = _arg_;
this._fn.parent = this;
this._arg.parent = this;
this._hash = (0, IRHash_1.isIRHash)(_unsafeHash) ? _unsafeHash : undefined;
}
Object.defineProperty(IRApp, "kind", {
get: function () { return IRNodeKind_1.IRNodeKind.App; },
enumerable: false,
configurable: true
});
Object.defineProperty(IRApp.prototype, "kind", {
get: function () { return IRApp.kind; },
enumerable: false,
configurable: true
});
Object.defineProperty(IRApp, "tag", {
get: function () { return new Uint8Array([IRApp.kind]); },
enumerable: false,
configurable: true
});
Object.defineProperty(IRApp.prototype, "fn", {
get: function () { return this._fn; },
set: function (newFn) {
if (!(0, isIRTerm_1.isIRTerm)(newFn))
return;
if (!(0, equalIRTerm_1.shallowEqualIRTermHash)(this._fn, newFn))
this.markHashAsInvalid();
// keep the parent reference in the old child, useful for compilation
// fn.parent = undefined;
this._fn = newFn;
this._fn.parent = this;
},
enumerable: false,
configurable: true
});
Object.defineProperty(IRApp.prototype, "arg", {
get: function () { return this._arg; },
set: function (newArg) {
if (!(0, isIRTerm_1.isIRTerm)(newArg))
return;
if (!(0, equalIRTerm_1.shallowEqualIRTermHash)(this._arg, newArg))
this.markHashAsInvalid();
// keep the parent reference in the old child, useful for compilation
// arg.parent = undefined;
this._arg = newArg;
this._arg.parent = this;
},
enumerable: false,
configurable: true
});
Object.defineProperty(IRApp.prototype, "hash", {
get: function () {
if (!(0, IRHash_1.isIRHash)(this._hash)) {
// basically a merkle tree
this._hash = (0, IRHash_1.hashIrData)((0, concatUint8Arr_1.concatUint8Arr)(IRApp.tag, this._fn.hash, this._arg.hash));
}
return this._hash;
},
enumerable: false,
configurable: true
});
IRApp.prototype.markHashAsInvalid = function () {
var _a;
this._hash = undefined;
(_a = this.parent) === null || _a === void 0 ? void 0 : _a.markHashAsInvalid();
};
IRApp.prototype.isHashPresent = function () { return (0, IRHash_1.isIRHash)(this._hash); };
Object.defineProperty(IRApp.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
});
Object.defineProperty(IRApp.prototype, "meta", {
get: function () {
if (!(0, obj_utils_1.isObject)(this._meta))
this._meta = {};
return this._meta;
},
enumerable: false,
configurable: true
});
IRApp.prototype.clone = function () {
return new IRApp(this.fn.clone(), this.arg.clone(), (0, obj_utils_1.isObject)(this._meta) ? __assign({}, this._meta) : undefined, this.isHashPresent() ? this.hash : undefined);
};
IRApp.prototype.toJSON = function () { return this.toJson(); };
IRApp.prototype.toJson = function () {
return {
type: "IRApp",
fn: this.fn.toJson(),
arg: this.arg.toJson()
};
};
return IRApp;
}());
exports.IRApp = IRApp;