@neo-one/smart-contract-compiler
Version:
NEO•ONE TypeScript smart contract compiler.
51 lines (49 loc) • 1.96 kB
JavaScript
import { tsUtils } from '@neo-one/ts-utils';
import ts from 'typescript';
import { isOnlyString } from '../../helper/types';
import { BuiltinMemberCall } from '../BuiltinMemberCall';
export class BufferFrom extends BuiltinMemberCall {
emitCall(sb, _func, node, optionsIn) {
const options = sb.pushValueOptions(optionsIn);
if (tsUtils.argumented.getArguments(node).length < 1) {
return;
}
const result = this.getHashAndEncoding(node);
if (result === undefined) {
sb.context.reportUnsupported(node);
return;
}
const { hash, encoding } = result;
if (hash === undefined) {
const arg = tsUtils.argumented.getArguments(node)[0];
const argType = sb.context.analysis.getType(arg);
if (argType !== undefined && !isOnlyString(sb.context, arg, argType)) {
sb.context.reportUnsupported(node);
}
sb.visit(arg, options);
sb.emitHelper(node, options, sb.helpers.unwrapString);
}
else {
sb.emitPushBuffer(node, Buffer.from(hash, Buffer.isEncoding(encoding) ? encoding : undefined));
}
sb.emitHelper(node, optionsIn, sb.helpers.wrapBuffer);
}
getHashAndEncoding(node) {
const args = tsUtils.argumented.getArguments(node);
const hashArg = args[0];
const encodingArg = args[1];
if (encodingArg !== undefined && !ts.isStringLiteral(encodingArg)) {
return undefined;
}
const encoding = encodingArg === undefined ? 'utf8' : tsUtils.literal.getLiteralValue(encodingArg);
if (ts.isStringLiteral(hashArg)) {
const hash = tsUtils.literal.getLiteralValue(hashArg);
return { hash, encoding };
}
if (encoding === 'utf8') {
return { encoding };
}
return undefined;
}
}
//# sourceMappingURL=from.js.map