Untitled

mail@pastecode.io avatar
unknown
javascript
a month ago
3.6 kB
4
Indexable
Never
var indentLevel = 0;
function log(message) {
  console.log(" ".repeat(indentLevel) + message);
}
class VMContext {
  constructor(bytecode) {
    this.ip = 0;
    this.registers = new Array(32).fill(0);
    this.stack = [];
    this.frames = [];
    this.bytecode = bytecode;
  }

  readNextValue() {
    const valueType = this.bytecode[this.ip++];
    if (valueType === 1) {
      return this.bytecode[this.ip++];
    }
    if (valueType === 2) {
      return (this.bytecode[this.ip++] << 8) | this.bytecode[this.ip++];
    }
    if (valueType === 4) {
      const value = this.bytecode.slice(this.ip, this.ip + 4);
      this.ip += 4;
      return (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
    }
    if (valueType === 5) {
      const value = this.bytecode.slice(this.ip, this.ip + 4);
      this.ip += 4;
      const buffer = new ArrayBuffer(4);
      const view = new DataView(buffer);
      for (let i = 0; i < 4; i++) {
        view.setUint8(i, value[i]);
      }
      return view.getFloat32(0);
    }
    if (valueType === 3) {
      const length = this.bytecode[this.ip++] | (this.bytecode[this.ip++] << 8);
      let stringChars = [];
      for (let i = 0; i < length; i++) {
        stringChars.push(this.bytecode[this.ip] ^ this.ip);
        this.ip++;
      }
      return String.fromCharCode(...stringChars);
    }
    throw new Error("Invalid value type");
  }
}

const handlers = [
  (context) => {
    context.stack.push(context.readNextValue());
  },
  (context) => {
    const reg = context.readNextValue();
    const value = context.stack.pop();
    context.registers[reg] = value;
  },
  (context) => {
    const a = context.stack.pop();
    const b = context.stack.pop();
    context.stack.push(b === a ? 1 : 0);
  },
  (context) => {
    const address = context.readNextValue();
    const value = context.stack.pop();
    if (value === 0) {
      context.ip = address;
    }
  },
  (context) => {
    const reg = context.readNextValue();
    context.stack.push(context.registers[reg]);
  },
  null,
  (context) => {
    indentLevel--;
    var ret_address = context.stack.pop();
    var ret_value = context.stack.pop();
    context.ip = ret_address;
    context.stack.push(ret_value);
    context.registers = context.frames.pop();
    return 3;
  },
];

function do_addition(a, b) {
  var carry = a & b;
  var result = a ^ b;
  while (carry != 0) {
    var shifted_carry = carry << 1;
    carry = result & shifted_carry;
    result = result ^ shifted_carry;
  }
  return result;
}

function fib(n) {
  if (n < 2) {
    return n;
  }
  return do_addition(fib(n - 1), fib(n - 2));
}

/**
 * @virtualize
 */
function isPasswordCorrect(input) {
  {
    const bytecode = [
      0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x03, 0x14, 0x00, 0x66, 0x38,
      0x3f, 0x79, 0x51, 0x67, 0x79, 0x75, 0x76, 0x20, 0x7a, 0x4a, 0x66, 0x23,
      0x6b, 0x6a, 0x6d, 0x2b, 0x6e, 0x79, 0x01, 0x01, 0x02, 0x04, 0x01, 0x01,
      0x04, 0x01, 0x02, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x3c, 0x04, 0x01,
      0x01, 0x04, 0x01, 0x02, 0x02, 0x00, 0x01, 0x01, 0x04, 0x01, 0x00, 0x06,
      0x00, 0x01, 0x00, 0x04, 0x01, 0x00, 0x06,
    ];
    let vm = new VMContext(bytecode);
    vm.stack.push(input);
    vm.stack.push(-1);
    while (true) {
      const opcode = bytecode[vm.ip++];
      if (handlers[opcode](vm) == 3) {
        return vm.stack.pop();
      }
    }
  }
}

var a = do_addition(18, 2);
var b = fib(4);
Leave a Comment