@neo-one/smart-contract-compiler
Version:
NEO•ONE TypeScript smart contract compiler.
95 lines (93 loc) • 5.1 kB
JavaScript
import { tsUtils } from '@neo-one/ts-utils';
import ts from 'typescript';
import { Helper } from '../Helper';
import { createWrapParam, findSuperDeployPropInfo } from './utils';
export class DeployHelper extends Helper {
constructor({ contractInfo, propInfo }) {
super();
this.contractInfo = contractInfo;
this.propInfo = propInfo;
}
emit(sb, node, optionsIn) {
const options = sb.pushValueOptions(optionsIn);
const handleDeployProperties = (contractInfo, innerOptions) => {
contractInfo.propInfos
.filter((prop) => prop.classDecl === contractInfo.smartContract)
.forEach((propertyPropInfo) => {
if (propertyPropInfo.type === 'property' && propertyPropInfo.structuredStorageType === undefined) {
const property = propertyPropInfo.decl;
if (ts.isPropertyDeclaration(property)) {
const initializer = tsUtils.initializer.getInitializer(property);
const propNode = initializer === undefined ? property : initializer;
if (initializer === undefined) {
sb.emitHelper(propNode, sb.pushValueOptions(innerOptions), sb.helpers.wrapUndefined);
}
else {
sb.visit(initializer, sb.pushValueOptions(innerOptions));
}
sb.emitPushString(property, tsUtils.node.getName(property));
sb.emitHelper(property, innerOptions, sb.helpers.putCommonStorage);
}
else if (ts.isParameterPropertyDeclaration(property, property.parent)) {
const name = tsUtils.node.getName(property);
sb.scope.get(sb, property, sb.pushValueOptions(innerOptions), name);
sb.emitPushString(property, name);
sb.emitHelper(property, innerOptions, sb.helpers.putCommonStorage);
}
}
});
};
const handleDeploy = (currentContractInfo, propInfo, innerOptions, entry = true) => {
const decl = propInfo.decl;
const superDeploy = findSuperDeployPropInfo(currentContractInfo);
if (decl === undefined) {
if (superDeploy === undefined) {
handleDeployProperties(currentContractInfo, innerOptions);
}
else {
handleDeploy(superDeploy[0], superDeploy[1], innerOptions, entry);
handleDeployProperties(currentContractInfo, innerOptions);
}
}
else {
if (entry) {
sb.emitPushInt(decl, 1);
sb.emitHelper(decl, innerOptions, sb.helpers.getArgument);
}
sb.withScope(decl, innerOptions, (innerInnerOptions) => {
sb.emitHelper(decl, innerInnerOptions, sb.helpers.parameters({
params: tsUtils.parametered.getParameters(decl),
mapParam: entry ? createWrapParam(sb) : undefined,
}));
const invokeOptions = sb.handleSuperConstructOptions(sb.noPushValueOptions(innerInnerOptions), (expr, _superExpr, innerInnerInnerOptionsIn) => {
if (superDeploy === undefined) {
return;
}
const innerInnerInnerOptions = sb.pushValueOptions(innerInnerInnerOptionsIn);
sb.emitHelper(expr, innerInnerInnerOptions, sb.helpers.args);
handleDeploy(superDeploy[0], superDeploy[1], innerInnerInnerOptions, false);
handleDeployProperties(currentContractInfo, innerInnerInnerOptions);
});
if (superDeploy === undefined) {
handleDeployProperties(currentContractInfo, innerInnerOptions);
}
sb.emitHelper(decl, invokeOptions, sb.helpers.invokeSmartContractMethod({ method: decl }));
});
}
};
sb.emitHelper(node, options, sb.helpers.if({
condition: () => {
sb.emitHelper(this.propInfo.decl === undefined ? node : this.propInfo.decl, options, sb.helpers.isDeployed);
},
whenTrue: () => {
sb.emitPushBoolean(this.propInfo.decl === undefined ? this.propInfo.classDecl : this.propInfo.decl, false);
},
whenFalse: () => {
handleDeploy(this.contractInfo, this.propInfo, options);
sb.emitHelper(this.propInfo.decl === undefined ? node : this.propInfo.decl, options, sb.helpers.setDeployed);
sb.emitPushBoolean(this.propInfo.decl === undefined ? this.propInfo.classDecl : this.propInfo.decl, true);
},
}));
}
}
//# sourceMappingURL=DeployHelper.js.map