UNPKG

@harmoniclabs/plu-ts-onchain

Version:

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

95 lines (94 loc) 4.37 kB
"use strict"; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addUserMethods = void 0; var obj_utils_1 = require("@harmoniclabs/obj-utils"); var typeExtends_1 = require("../../../../type_system/typeExtends.js"); var types_1 = require("../../../../type_system/types.js"); var papp_1 = require("../../../papp.js"); var getFnTypes_1 = require("../../../../Script/Parametrized/getFnTypes.js"); var minimal_1 = require("../../../plet/minimal.js"); var addUtilityForType_1 = require("../addUtilityForType.js"); var assertWellFormedMethods_1 = require("./assertWellFormedMethods.js"); var stringify_1 = require("../../../../../utils/stringify.js"); function getMethodsWithFirstInputOfType(methods, type) { var e_1, _a; var filtered = {}; var method; var methodNames = Object.keys(methods); try { for (var methodNames_1 = __values(methodNames), methodNames_1_1 = methodNames_1.next(); !methodNames_1_1.done; methodNames_1_1 = methodNames_1.next()) { var methodName = methodNames_1_1.value; method = methods[methodName]; if (method.type[0] !== types_1.PrimType.Lambda) throw new Error("user defined method is expected to be a funciton"); if ((0, typeExtends_1.typeExtends)(method.type[1], type)) { filtered[methodName] = method; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (methodNames_1_1 && !methodNames_1_1.done && (_a = methodNames_1.return)) _a.call(methodNames_1); } finally { if (e_1) throw e_1.error; } } return Object.freeze(filtered); } function addUserMethods(term, methods) { if (!(0, assertWellFormedMethods_1.isWellFormedMethods)(methods)) { throw new Error("user-specified methods are not well formed, definition contains methods with ambigous names: " + (0, stringify_1.stringify)(Object.keys(methods), undefined, 2)); } var t = term.type; var filtered = getMethodsWithFirstInputOfType(methods, t); var method; var _loop_1 = function (methodName) { method = filtered[methodName]; var fnTypes = (0, getFnTypes_1.getFnTypes)(method.type); // don't add terms that do not accept `self_t` as first argument as methods if (!(0, typeExtends_1.typeExtends)(fnTypes[0], t)) return "continue"; // -1 (term application) // -1 (the output type) var missingArgsAfterApplication = fnTypes.length - 2; var _appliedTerm = (0, papp_1.papp)(method, term); var appliedTerm = (0, addUtilityForType_1.addUtilityForType)(_appliedTerm.type)((0, minimal_1._plet)(_appliedTerm)); if (missingArgsAfterApplication === 0) { (0, obj_utils_1.defineReadOnlyProperty)(term, methodName, appliedTerm); return "continue"; } (0, obj_utils_1.defineReadOnlyProperty)(term, "p" + methodName, appliedTerm); (0, obj_utils_1.defineReadOnlyProperty)(term, methodName, function () { var other_terms = []; /* PappArg<PType>[] */ for (var _i = 0 /* PappArg<PType>[] */; _i < arguments.length /* PappArg<PType>[] */; _i++ /* PappArg<PType>[] */) { other_terms[_i] = arguments[_i]; /* PappArg<PType>[] */ } var result = appliedTerm; if (other_terms.length < missingArgsAfterApplication) throw new Error("not enough arguments for method + '" + methodName + "'"); for (var i = 0; i < missingArgsAfterApplication; i++) { result = (0, papp_1.papp)(result, // PappArg<PType> other_terms[i]); } return result; }); }; for (var methodName in filtered) { _loop_1(methodName); } return term; } exports.addUserMethods = addUserMethods;