MCPcopy Create free account
hub / github.com/CashScript/cashscript / explicitlyCastable

Function explicitlyCastable

packages/utils/src/types.ts:61–109  ·  view source on GitHub ↗
(from?: Type, to?: Type)

Source from the content-addressed store, hash-verified

59};
60
61export function explicitlyCastable(from?: Type, to?: Type): boolean {
62 if (!from || !to) return false;
63
64 // Tuples can't be cast
65 if (from instanceof TupleType || to instanceof TupleType) return false;
66
67 // Arrays can be cast if their elements can be cast (don't think this is actually used ever)
68 if (from instanceof ArrayType && to instanceof ArrayType) {
69 return explicitlyCastable(from.elementType, to.elementType);
70 }
71
72 // Can't cast between Array and non-Array
73 if (from instanceof ArrayType || to instanceof ArrayType) return false;
74
75 if (to instanceof BytesType) {
76 // Can't cast bool to bytes
77 if (from === PrimitiveType.BOOL) return false;
78 // Can cast int to any size bytes
79 if (from === PrimitiveType.INT) return true;
80
81 // Can freely cast to unbounded bytes
82 if (!to.bound) return true;
83
84 if (from instanceof BytesType) {
85 // Can freely cast from unbounded bytes
86 if (!from.bound) return true;
87 // Can only cast bounded bytes to bounded bytes if bounds are equal
88 return from.bound === to.bound;
89 }
90
91 // Cannot cast other primitive types directly to bounded bytes types
92 return false;
93 }
94
95 if (from instanceof BytesType) {
96 // Can cast any bytes to int
97 if (to === PrimitiveType.INT) return true;
98 // Can't cast bytes to bool or string
99 if (to === PrimitiveType.BOOL) return false;
100 if (to === PrimitiveType.STRING) return false;
101 // Can cast any bytes to pubkey, sig, datasig
102 if (to === PrimitiveType.PUBKEY) return true;
103 if (to === PrimitiveType.SIG) return true;
104 if (to === PrimitiveType.DATASIG) return true;
105 return true;
106 }
107
108 return ExplicitlyCastableTo[from].includes(to);
109}
110
111export function implicitlyCastable(actual?: Type, expected?: Type): boolean {
112 if (!actual || !expected) return false;

Callers 2

visitCastMethod · 0.90
types.test.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected