@neo-one/smart-contract-compiler
Version:
NEO•ONE TypeScript smart contract compiler.
76 lines (74 loc) • 2.71 kB
JavaScript
import { tsUtils } from '@neo-one/ts-utils';
import { BuiltinInterface } from '../BuiltinInterface';
import { BuiltinNew } from '../BuiltinNew';
import { MapDelete } from '../map/delete';
import { MapHas } from '../map/has';
import { MapSize } from '../map/size';
import { SetAdd } from './add';
import { SetForEach } from './forEach';
import { SetIterator } from './iterator';
class SetInterface extends BuiltinInterface {
}
class ReadonlySetInterface extends BuiltinInterface {
}
class SetValue extends BuiltinNew {
constructor() {
super(...arguments);
this.type = 'SetConstructor';
}
emitInstanceOf(sb, node, optionsIn) {
const options = sb.pushValueOptions(optionsIn);
sb.visit(node, options);
if (optionsIn.pushValue) {
sb.emitHelper(node, options, sb.helpers.isSet);
sb.emitHelper(node, options, sb.helpers.wrapBoolean);
}
else {
sb.emitOp(node, 'DROP');
}
}
emitNew(sb, node, optionsIn) {
const options = sb.pushValueOptions(optionsIn);
const args = tsUtils.argumented.getArgumentsArray(node);
sb.emitOp(node, 'NEWMAP');
if (args.length > 0) {
sb.visit(args[0], options);
sb.emitHelper(node, options, sb.helpers.unwrapArray);
sb.emitOp(node, 'SWAP');
sb.emitHelper(node, options, sb.helpers.arrReduce({
each: () => {
sb.emitOp(node, 'TUCK');
sb.emitOp(node, 'SWAP');
sb.emitSysCall(node, 'Neo.Runtime.Serialize');
sb.emitPushBoolean(node, true);
sb.emitOp(node, 'SETITEM');
},
}));
}
sb.emitHelper(node, options, sb.helpers.wrapSet);
if (!optionsIn.pushValue) {
sb.emitOp(node, 'DROP');
}
}
}
class SetConstructorInterface extends BuiltinInterface {
}
const COMMON = [
['__@iterator', new SetIterator()],
['forEach', new SetForEach()],
['has', new MapHas()],
['size', new MapSize()],
];
export const add = (builtins) => {
builtins.addInterface('Set', new SetInterface());
builtins.addInterface('ReadonlySet', new ReadonlySetInterface());
builtins.addValue('Set', new SetValue());
COMMON.forEach(([name, builtin]) => {
builtins.addGlobalMember('Set', name, builtin);
builtins.addGlobalMember('ReadonlySet', name, builtin);
});
builtins.addGlobalMember('Set', 'delete', new MapDelete());
builtins.addGlobalMember('Set', 'add', new SetAdd());
builtins.addInterface('SetConstructor', new SetConstructorInterface());
};
//# sourceMappingURL=index.js.map