Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Setup the switch DSL
let clauses: Array<{ match: number; label: string; callback: () => CompileActions }> = [];
let count = 0;
function when(match: number, callback: () => CompileActions): void {
clauses.push({ match, callback, label: `CLAUSE${count++}` });
}
// Call the callback
callback(when);
// Emit the opcodes for the switch
let out: CompileActions = [
op(Op.Enter, 2),
op(Op.AssertSame),
op(Op.ReifyU32),
op('StartLabels'),
];
// First, emit the jump opcodes. We don't need a jump for the last
// opcode, since it bleeds directly into its clause.
for (let clause of clauses.slice(0, -1)) {
out.push(op(Op.JumpEq, label(clause.label), clause.match));
}
// Enumerate the clauses in reverse order. Earlier matches will
// require fewer checks.
for (let i = clauses.length - 1; i >= 0; i--) {
let clause = clauses[i];
out.push(op('Label', clause.label), op(Op.Pop, 2), clause.callback());
when(ContentType.Node, () => [op(Op.AssertSame), op(Op.AppendNode)]);
}),
stackChange: 0,
ops: [
{
name: 'to',
type: 'i32',
},
{
name: 'comparison',
type: 'i32',
},
],
operands: 2,
check: true,
};
METADATA[Op.AssertSame] = {
name: 'AssertSame',
mnemonic: 'assert_eq',
before: null,
stackChange: 0,
ops: [],
operands: 0,
check: true,
};
METADATA[Op.Enter] = {
name: 'Enter',
mnemonic: 'blk_start',
before: null,
stackChange: 0,
ops: [
{
when(ContentType.String, () => {
if (trusting) {
return [op(Op.AssertSame), op(Op.AppendHTML)];
} else {
return op(Op.AppendText);
}
});