UNPKG

@harmoniclabs/plu-ts-onchain

Version:

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

133 lines (131 loc) 5.67 kB
"use strict"; 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.typeofGenericStruct = exports.pgenericStruct = void 0; var obj_utils_1 = require("@harmoniclabs/obj-utils"); var pair_1 = require("@harmoniclabs/pair"); var PDataRepresentable_1 = require("../../PType/PDataRepresentable.js"); var cloneStructDef_1 = require("./cloneStructDef.js"); var pstruct_1 = require("./pstruct.js"); var types_1 = require("../../type_system/types.js"); var utils_1 = require("../../type_system/utils.js"); var isWellFormedType_1 = require("../../type_system/kinds/isWellFormedType.js"); var isTaggedAsAlias_1 = require("../../type_system/kinds/isTaggedAsAlias.js"); /** * @param getDescriptor * @returns * * @deprecated * * use a function that reutrns a struct based on the specfied types instead */ function pgenericStruct(getDescriptor) { console.warn([ "you are using 'pgenericStruct' to create a paramterized sctruct;", "this method is deprecated since v0.2.0 and might behave incorrectly", "consider updating your code by defining your parametrized struct as a function that reutrns a determined struct" ].join(" ")); /* lambda called immediately needed to allow the creation of a **new** cache per each generic struct cannot create a cache directly in the ```pgenericStruct``` function because that would be global for **every** generic structure; */ return (function () { var tyArgsCache = []; return (0, obj_utils_1.defineReadOnlyProperty)(function () { var tyArgs = []; for (var _i = 0; _i < arguments.length; _i++) { tyArgs[_i] = arguments[_i]; } var thisTyArgsKey = tyArgs.map(function (t) { return (0, utils_1.termTypeToString)(t); }).join('|'); var keys = tyArgsCache.map(function (pair) { return pair.fst; }); if (keys.includes(thisTyArgsKey)) { var cachedResult = tyArgsCache.find(function (pair) { return pair.fst === thisTyArgsKey; }); if (cachedResult !== undefined) return cachedResult.snd; } var result = getDescriptor.apply(void 0, __spreadArray([ /* Argument of type '[TermType, ...TermType[]]' is not assignable to parameter of type 'TypeArgs'. '[TermType, ...TermType[]]' is assignable to the constraint of type 'TypeArgs', but 'TypeArgs' could be instantiated with a different subtype of constraint '[TermType, ...TermType[]]'.ts(2345) */ //@ts-ignore tyArgs[0]], __read(tyArgs.slice(1)), false)); if (!(result instanceof PDataRepresentable_1.PDataRepresentable)) result = (0, pstruct_1.pstruct)(result); tyArgsCache.push(new pair_1.Pair(thisTyArgsKey, result)); return result; }, "type", typeofGenericStruct(getDescriptor)); })(); } exports.pgenericStruct = pgenericStruct; ; function typeofGenericStruct(genStruct) { var nArgs = genStruct.length; var aliases = Array(nArgs); var replacements = Array(nArgs); for (var i = 0; i < nArgs; i++) { aliases[i] = (0, types_1.alias)(types_1.int); replacements[i] = (0, types_1.tyVar)(); } ; var PStruct_ = genStruct.apply(void 0, __spreadArray([], __read(aliases), false)); var sDef = (0, cloneStructDef_1.cloneSopDef)(PStruct_.type[1]); replaceAliasesWith(aliases, replacements, sDef); return (0, types_1.struct)(sDef); } exports.typeofGenericStruct = typeofGenericStruct; function replaceAliasesWith(aliases, replacements, sDef) { var ctors = Object.keys(sDef); for (var i = 0; i < ctors.length; i++) { var thisCtor = sDef[ctors[i]]; var fields = Object.keys(thisCtor); var _loop_1 = function (j) { var thisField = fields[i]; var thisType = thisCtor[thisField]; if ((0, isWellFormedType_1.isStructType)(thisType)) { var thisTypeSDefClone = (0, cloneStructDef_1.cloneStructDef)(thisType[1]); replaceAliasesWith(aliases, replacements, thisTypeSDefClone); thisCtor[thisField] = (0, types_1.struct)(thisTypeSDefClone); } else if ((0, isTaggedAsAlias_1.isTaggedAsAlias)(thisType)) { var idx = aliases.findIndex( // object (pointer) equality function (alias) { return thisType === alias; }); if (idx < 0) return "continue"; thisCtor[thisField] = replacements[idx]; } }; for (var j = 0; j < fields.length; j++) { _loop_1(j); } } }