From 7b54a534144ea278e477637d4db30326b6be8e37 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Thu, 4 Dec 2014 10:45:10 -0300 Subject: [PATCH] add Opcode.smallInt() --- lib/opcode.js | 10 ++++++++++ test/opcode.js | 15 ++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/opcode.js b/lib/opcode.js index ccd6535..75e8120 100644 --- a/lib/opcode.js +++ b/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, diff --git a/test/opcode.js b/test/opcode.js index c9d8307..41ccc43 100644 --- a/test/opcode.js +++ b/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() {