github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/test/mixin.js (about) 1 'use strict'; 2 3 var Mixin = function () {}; 4 5 Mixin.UNPAYABLE = function () { 6 if (Blockchain.transaction.value.gt(0)) { 7 return false; 8 } 9 return true; 10 }; 11 12 Mixin.PAYABLE = function () { 13 if (Blockchain.transaction.value.gt(0)) { 14 return true; 15 } 16 return false; 17 }; 18 19 Mixin.POSITIVE = function () { 20 console.log("POSITIVE"); 21 return true; 22 }; 23 24 Mixin.UNPOSITIVE = function () { 25 console.log("UNPOSITIVE"); 26 return false; 27 }; 28 29 Mixin.decorator = function () { 30 var funcs = arguments; 31 if (funcs.length < 1) { 32 throw new Error("mixin decorator need parameters"); 33 } 34 35 return function () { 36 for (var i = 0; i < funcs.length - 1; i ++) { 37 var func = funcs[i]; 38 if (typeof func !== "function" || !func()) { 39 throw new Error("mixin decorator failure"); 40 } 41 } 42 43 var exeFunc = funcs[funcs.length - 1]; 44 if (typeof exeFunc === "function") { 45 exeFunc.apply(this, arguments); 46 } else { 47 throw new Error("mixin decorator need an executable method"); 48 } 49 }; 50 }; 51 52 var SampleContract = function () { 53 }; 54 55 SampleContract.prototype = { 56 init: function () { 57 }, 58 unpayable: function () { 59 console.log("contract function unpayable:", arguments); 60 }, 61 payable: Mixin.decorator(Mixin.PAYABLE, function () { 62 console.log("contract function payable:",arguments); 63 }), 64 contract1: Mixin.decorator(Mixin.POSITIVE, function (arg) { 65 console.log("contract1 function:", arg); 66 }), 67 contract2: Mixin.decorator(Mixin.UNPOSITIVE, function (arg) { 68 console.log("contract2 function:", arg); 69 }), 70 contract3: Mixin.decorator(Mixin.PAYABLE, Mixin.POSITIVE, function (arg) { 71 console.log("contract3 function:", arg); 72 }), 73 contract4: Mixin.decorator(Mixin.PAYABLE, Mixin.UNPOSITIVE, function (arg) { 74 console.log("contract4 function:", arg); 75 }) 76 }; 77 78 module.exports = SampleContract;