@neo-one/smart-contract-compiler
Version:
NEO•ONE TypeScript smart contract compiler.
52 lines (50 loc) • 2.12 kB
JavaScript
import { tsUtils } from '@neo-one/ts-utils';
import ts from 'typescript';
import * as constants from '../../../constants';
import { Helper } from '../Helper';
const isValidParent = (catchPC, finallyPC, node) => node !== undefined &&
((ts.isTryStatement(node) && (catchPC !== undefined || finallyPC !== undefined)) ||
tsUtils.guards.isFunctionLikeDeclarationBase(node) ||
ts.isArrowFunction(node) ||
ts.isSourceFile(node));
export class ThrowCompletionBaseHelper extends Helper {
emit(sb, node, optionsIn) {
const options = sb.pushValueOptions(optionsIn);
const finallyPC = options.finallyPC;
const catchPC = options.catchPC;
let parent = tsUtils.node.getParent(node);
while (parent !== undefined && !isValidParent(catchPC, finallyPC, parent)) {
parent = tsUtils.node.getParent(parent);
}
if (catchPC !== undefined) {
sb.emitPushInt(node, constants.THROW_COMPLETION);
sb.emitJmp(node, 'JMP', catchPC);
}
else if (finallyPC !== undefined) {
sb.emitPushInt(node, constants.THROW_COMPLETION);
sb.emitPushInt(node, constants.FINALLY_COMPLETION);
sb.emitJmp(node, 'JMP', finallyPC);
}
else if (ts.isSourceFile(node) || (parent !== undefined && ts.isSourceFile(parent))) {
sb.emitOp(node, 'DROP');
sb.emitHelper(node, options, sb.helpers.if({
condition: () => {
sb.emitHelper(node, options, sb.helpers.invocationIsCaller);
},
whenTrue: () => {
sb.emitOp(node, 'THROW');
},
whenFalse: () => {
sb.emitPushBoolean(node, false);
sb.emitPushInt(node, constants.NORMAL_COMPLETION);
sb.emitOp(node, 'RET');
},
}));
}
else {
sb.emitPushInt(node, constants.THROW_COMPLETION);
sb.emitOp(node, 'RET');
}
}
}
//# sourceMappingURL=ThrowCompletionBaseHelper.js.map