UNPKG

@harmoniclabs/plu-ts-onchain

Version:

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

174 lines (173 loc) 7.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Term = void 0; var plutus_machine_1 = require("@harmoniclabs/plutus-machine"); var IRConst_1 = require("../../IR/IRNodes/IRConst.js"); var IRError_1 = require("../../IR/IRNodes/IRError.js"); var IRHoisted_1 = require("../../IR/IRNodes/IRHoisted.js"); var compileIRToUPLC_1 = require("../../IR/toUPLC/compileIRToUPLC.js"); var type_system_1 = require("../type_system/index.js"); var cloneTermType_1 = require("../type_system/cloneTermType.js"); var obj_utils_1 = require("@harmoniclabs/obj-utils"); var Cloneable_1 = require("../../utils/Cloneable.js"); var assert_1 = require("../../utils/assert.js"); var IRVar_1 = require("../../IR/IRNodes/IRVar.js"); var IRSelfCall_1 = require("../../IR/IRNodes/IRSelfCall.js"); var CompilerOptions_1 = require("../../IR/toUPLC/CompilerOptions.js"); var Term = /** @class */ (function () { function Term(type, _toIR, isConstant) { if (isConstant === void 0) { isConstant = false; } var _this = this; (0, assert_1.assert)((0, type_system_1.isWellFormedGenericType)(type) || Boolean(void console.log((0, type_system_1.termTypeToString)(type))), "invalid type while constructing Term"); var _type = (0, cloneTermType_1.cloneTermType)(type); Object.defineProperty(this, "type", { get: function () { return (0, cloneTermType_1.cloneTermType)(_type); }, set: function () { }, enumerable: true, configurable: false }); var _toIR_ = _toIR; //.bind( this ); var shouldHoist = false; var _IR_cache = {}; Object.defineProperty(this, "toIR", { value: function (config, deBruijnLevel) { if (config === void 0) { config = CompilerOptions_1.defaultOptions; } if (deBruijnLevel === void 0) { deBruijnLevel = 0; } if (!(0, obj_utils_1.isObject)(config)) config = CompilerOptions_1.defaultOptions; var dbnStr = deBruijnLevel.toString(); var _cacheHit = _IR_cache[shouldHoist ? "hoisted" : dbnStr]; if (_cacheHit) return _cacheHit.clone(); if (typeof deBruijnLevel !== "bigint") deBruijnLevel = BigInt(deBruijnLevel); var ir = _toIR_(config, deBruijnLevel); if (shouldHoist) { var res = new IRHoisted_1.IRHoisted(ir); _IR_cache["hoisted"] = res.clone(); return res; } if (!(ir instanceof IRHoisted_1.IRHoisted) && _this.isConstant && !(ir instanceof IRConst_1.IRConst)) { // console.log( showIR( ir ).text ); // logJson( ir ) // !!! IMPORTANT !!! // `compileIRToUPLC` modifies the `IRTerm` in place ! // as for the current implementation we don't care // because we are going to re-assign the variable `ir` anyway // if this ever changes make sure to call `ir.clone()` var uplc = (0, compileIRToUPLC_1.compileIRToUPLC)(ir, CompilerOptions_1.debugOptions); // console.log( prettyUPLC( uplc ) ) // !!! IMPORTANT !!! // pair creation assumes this evaluation is happening here // if for whatever reason this is removed please adapt the rest of the codebas uplc = plutus_machine_1.Machine.evalSimple(uplc); if (uplc instanceof plutus_machine_1.CEKConst) { ir = new IRConst_1.IRConst(_this.type, uplc.value); } else { ir = new IRError_1.IRError(); } } if (!( // we don't cache `IRVar` since // it is likely they will be accessed at different dbn ir instanceof IRVar_1.IRVar || ir instanceof IRSelfCall_1.IRSelfCall || // same for `IRConst` ir instanceof IRConst_1.IRConst)) { _IR_cache[dbnStr] = ir.clone(); } return ir; }, writable: false, enumerable: true, configurable: false }); var _UPLC_cache = {}; Object.defineProperty(this, "toUPLC", { value: function (deBruijnLevel, config) { if (deBruijnLevel === void 0) { deBruijnLevel = 0; } if (config === void 0) { config = CompilerOptions_1.defaultOptions; } var key = shouldHoist ? "hoisted" : deBruijnLevel.toString(); var _cacheHit = _UPLC_cache[key]; if (_cacheHit) return _cacheHit.clone(); var res = (0, compileIRToUPLC_1.compileIRToUPLC)(_this.toIR(config, deBruijnLevel), config); _UPLC_cache[key] = res.clone(); return res; }, writable: false, enumerable: true, configurable: false }); (0, obj_utils_1.defineReadOnlyHiddenProperty)(this, "hoist", function () { shouldHoist = true; }); var _isConstant = false; Object.defineProperty(this, "isConstant", { get: function () { return _isConstant; }, set: function (isConst) { if (typeof isConst !== "boolean") return; _isConstant = isConst; }, enumerable: false, configurable: false }); // calls the `set` function of the descriptor above; this.isConstant = isConstant; Object.defineProperty(this, "clone", { value: function () { var cloned = new Term(_this.type, _toIR_, Boolean(_this.isConstant) // isConstant ); Object.keys(_this).forEach(function (k) { var _a; if (k === "_type" || k === "toUPLC" || k === "toIR") return; Object.defineProperty(cloned, k, (_a = Object.getOwnPropertyDescriptor(_this, k)) !== null && _a !== void 0 ? _a : {}); }); if (shouldHoist) { cloned.hoist(); } return cloned; }, writable: false, enumerable: false, configurable: false }); } Object.defineProperty(Term.prototype, "pInstance", { get: function () { if (this._pInstance === undefined) return undefined; return (0, Cloneable_1.isCloneable)(this._pInstance) ? this._pInstance.clone() : this._pInstance; }, enumerable: false, configurable: true }); return Term; }()); exports.Term = Term; Object.defineProperty(Term.prototype, "as", /** * here only as fallback * * usually only the utility term "as" is accessed */ { value: function _as(type) { if (!(0, type_system_1.isWellFormedType)(type)) { throw new Error("`punsafeConvertType` called with invalid type"); } return new Term(type, this.toIR, Boolean(this.isConstant) // isConstant ); }, writable: false, enumerable: false, configurable: false } //*/ );