@neo-one/smart-contract-compiler
Version:
NEO•ONE TypeScript smart contract compiler.
65 lines (63 loc) • 2.39 kB
JavaScript
import { tsUtils } from '@neo-one/ts-utils';
import { Types } from '../../../constants';
import { BuiltinInstanceMemberCall } from '../../BuiltinInstanceMemberCall';
export class ForwardValueAs extends BuiltinInstanceMemberCall {
constructor(type, isNullable = false) {
super();
this.type = type;
this.isNullable = isNullable;
}
canCall(_sb, _func, _node) {
return true;
}
emitCall(sb, func, node, optionsIn, visited) {
const options = sb.pushValueOptions(optionsIn);
if (!visited) {
sb.visit(tsUtils.expression.getExpression(func), options);
}
const handleValue = () => {
if (this.type === Types.Array) {
sb.emitHelper(node, options, sb.helpers.arrMap({
map: (innerOptions) => {
sb.emitHelper(node, innerOptions, sb.helpers.wrapForwardValue);
},
}));
}
if (this.type === Types.Map) {
sb.emitHelper(node, options, sb.helpers.mapMap({
map: (innerOptions) => {
sb.emitHelper(node, innerOptions, sb.helpers.wrapForwardValue);
sb.emitOp(node, 'SWAP');
sb.emitHelper(node, innerOptions, sb.helpers.wrapForwardValue);
sb.emitOp(node, 'SWAP');
},
}));
}
sb.emitHelper(node, options, sb.helpers.wrapVal({ type: this.type }));
};
sb.emitHelper(node, options, sb.helpers.unwrapForwardValue);
if (this.isNullable) {
sb.emitHelper(node, options, sb.helpers.if({
condition: () => {
sb.emitOp(node, 'DUP');
sb.emitPushBuffer(node, Buffer.alloc(0, 0));
sb.emitOp(node, 'EQUAL');
},
whenTrue: () => {
sb.emitOp(node, 'DROP');
sb.emitHelper(node, options, sb.helpers.wrapUndefined);
},
whenFalse: () => {
handleValue();
},
}));
}
else {
handleValue();
}
if (!optionsIn.pushValue) {
sb.emitOp(node, 'DROP');
}
}
}
//# sourceMappingURL=ForwardValueAs.js.map