Browse Source

add Opcode.smallInt()

patch-2
Manuel Araoz 10 years ago
parent
commit
7b54a53414
  1. 10
      lib/opcode.js
  2. 15
      test/opcode.js

10
lib/opcode.js

@ -46,6 +46,16 @@ Opcode.prototype.toString = function() {
return str;
};
Opcode.smallInt = function(n) {
if (!(n >= 0 && n <= 16)) {
throw new Error('Invalid Argument: n must be between 0 and 16');
}
if (n === 0) {
return Opcode('OP_0');
}
return new Opcode(Opcode.map.OP_1 + n - 1);
};
Opcode.map = {
// push value
OP_FALSE: 0,

15
test/opcode.js

@ -92,13 +92,22 @@ describe('Opcode', function() {
Opcode('OP_16')
];
describe('@smallInt', function() {
var testSmallInt = function(n, op) {
Opcode.smallInt(n).toString().should.equal(op.toString());
};
for (var i = 0; i < smallints.length; i++) {
var op = smallints[i];
it('should work for small int ' + op, testSmallInt.bind(null, i, op));
}
});
describe('@isSmallIntOp', function() {
var testSmallInt = function() {
Opcode.isSmallIntOp(this).should.equal(true);
var testIsSmallInt = function(op) {
Opcode.isSmallIntOp(op).should.equal(true);
};
for (var i = 0; i < smallints.length; i++) {
var op = smallints[i];
it('should work for small int ' + op, testSmallInt.bind(op));
it('should work for small int ' + op, testIsSmallInt.bind(null, op));
}
it('should work for non-small ints', function() {

Loading…
Cancel
Save