Don't put properties on native objects

This commit is contained in:
copy 2015-01-12 18:50:15 +01:00
parent 2673dd6fbf
commit 1664d39fa8
7 changed files with 114 additions and 98 deletions

View file

@ -1352,7 +1352,7 @@ CPU.prototype.bsf16 = function(old, bit_base)
this.flags &= ~flag_zero;
// http://jsperf.com/lowest-bit-index
return Math.int_log2(-bit_base & bit_base);
return v86util.int_log2(-bit_base & bit_base);
}
}
@ -1370,7 +1370,7 @@ CPU.prototype.bsf32 = function(old, bit_base)
{
this.flags &= ~flag_zero;
return Math.int_log2((-bit_base & bit_base) >>> 0);
return v86util.int_log2((-bit_base & bit_base) >>> 0);
}
}
@ -1387,7 +1387,7 @@ CPU.prototype.bsr16 = function(old, bit_base)
{
this.flags &= ~flag_zero;
return Math.int_log2(bit_base);
return v86util.int_log2(bit_base);
}
}
@ -1403,7 +1403,7 @@ CPU.prototype.bsr32 = function(old, bit_base)
else
{
this.flags &= ~flag_zero;
return Math.int_log2(bit_base >>> 0);
return v86util.int_log2(bit_base >>> 0);
}
}

View file

@ -1,7 +1,5 @@
"use strict";
var v86util = v86util || {};
(function()
{
v86util.load_file = load_file;

View file

@ -53,13 +53,13 @@
}
else if(time < 3600)
{
return (time / 60 | 0) + "m " + String.pad0(time % 60, 2) + "s";
return (time / 60 | 0) + "m " + v86util.pad0(time % 60, 2) + "s";
}
else
{
return (time / 3600 | 0) + "h " +
String.pad0((time / 60 | 0) % 60, 2) + "m " +
String.pad0(time % 60, 2) + "s";
v86util.pad0((time / 60 | 0) % 60, 2) + "m " +
v86util.pad0(time % 60, 2) + "s";
}
}

View file

@ -322,7 +322,7 @@
function add(ip, op)
{
out += h(ip, 8) + ": " +
String.pads(opcode_map[op] || "unkown", 20) + h(op, 2) + "\n";
v86util.pads(opcode_map[op] || "unkown", 20) + h(op, 2) + "\n";
}
var opcodes;

View file

@ -1,7 +1,9 @@
"use strict";
var v86util = v86util || {};
// pad string with spaces on the right
String.pads = function(str, len)
v86util.pads = function(str, len)
{
str = str ? str + "" : "";
@ -14,7 +16,7 @@ String.pads = function(str, len)
}
// pad string with zeros on the left
String.pad0 = function(str, len)
v86util.pad0 = function(str, len)
{
str = str ? str + "" : "";
@ -36,11 +38,11 @@ function h(n, len)
{
//dbg_assert(typeof n === "number");
if(!n) return String.pad0("", len || 1);
if(!n) return v86util.pad0("", len || 1);
if(len)
{
return String.pad0(n.toString(16).toUpperCase(), len);
return v86util.pad0(n.toString(16).toUpperCase(), len);
}
else
{
@ -93,93 +95,70 @@ SyncBuffer.prototype.get_buffer = function(fn)
};
/**
* Simple circular queue for logs
*
* @param {number} size
* @constructor
*/
function CircularQueue(size)
(function()
{
this.data = [];
this.index = 0;
this.size = size;
}
var int_log2_table = new Int8Array(256);
CircularQueue.prototype.add = function(item)
{
this.data[this.index] = item;
this.index = (this.index + 1) % this.size;
};
CircularQueue.prototype.toArray = function()
{
return [].slice.call(this.data, this.index).concat([].slice.call(this.data, 0, this.index));
};
CircularQueue.prototype.clear = function()
{
this.data = [];
this.index = 0;
};
/**
* @param {Array} new_data
*/
CircularQueue.prototype.set = function(new_data)
{
this.data = new_data;
this.index = 0;
};
var int_log2_table = new Int8Array(256);
for(var i = 0, b = -2; i < 256; i++)
{
if(!(i & i - 1))
b++;
int_log2_table[i] = b;
}
/**
* calculate the integer logarithm base 2
* @param {number} x
* @return {number}
*/
Math.int_log2 = function(x)
{
dbg_assert(x > 0);
// http://jsperf.com/integer-log2/6
var tt = x >>> 16;
if(tt)
for(var i = 0, b = -2; i < 256; i++)
{
var t = tt >>> 8;
if(t)
if(!(i & i - 1))
b++;
int_log2_table[i] = b;
}
/**
* calculate the integer logarithm base 2 of a byte
* @param {number} x
* @return {number}
*/
v86util.int_log2_byte = function(x)
{
dbg_assert(x > 0);
dbg_assert(x < 0x100);
return int_log2_table[x];
};
/**
* calculate the integer logarithm base 2
* @param {number} x
* @return {number}
*/
v86util.int_log2 = function(x)
{
dbg_assert(x > 0);
// http://jsperf.com/integer-log2/6
var tt = x >>> 16;
if(tt)
{
return 24 + int_log2_table[t];
var t = tt >>> 8;
if(t)
{
return 24 + int_log2_table[t];
}
else
{
return 16 + int_log2_table[tt];
}
}
else
{
return 16 + int_log2_table[tt];
var t = x >>> 8;
if(t)
{
return 8 + int_log2_table[t];
}
else
{
return int_log2_table[x];
}
}
}
else
{
var t = x >>> 8;
if(t)
{
return 8 + int_log2_table[t];
}
else
{
return int_log2_table[x];
}
}
}
})();
/**
@ -252,3 +231,42 @@ function ByteQueue(size)
this.clear();
}
/**
* Simple circular queue for logs
*
* @param {number} size
* @constructor
*/
function CircularQueue(size)
{
this.data = [];
this.index = 0;
this.size = size;
}
CircularQueue.prototype.add = function(item)
{
this.data[this.index] = item;
this.index = (this.index + 1) % this.size;
};
CircularQueue.prototype.toArray = function()
{
return [].slice.call(this.data, this.index).concat([].slice.call(this.data, 0, this.index));
};
CircularQueue.prototype.clear = function()
{
this.data = [];
this.index = 0;
};
/**
* @param {Array} new_data
*/
CircularQueue.prototype.set = function(new_data)
{
this.data = new_data;
this.index = 0;
};

View file

@ -28,7 +28,7 @@ var dbg_log = (function()
if(level & LOG_LEVEL)
{
var level_name = dbg_names[level] || "",
message = "[" + String.pads(level_name, 4) + "] " + stuff;
message = "[" + v86util.pads(level_name, 4) + "] " + stuff;
if(message === log_last_message)
{
@ -41,9 +41,9 @@ var dbg_log = (function()
}
var now = new Date();
var time_str = String.pad0(now.getHours(), 2) + ":" +
String.pad0(now.getMinutes(), 2) + ":" +
String.pad0(now.getSeconds(), 2) + " ";
var time_str = v86util.pad0(now.getHours(), 2) + ":" +
v86util.pad0(now.getMinutes(), 2) + ":" +
v86util.pad0(now.getSeconds(), 2) + " ";
if(log_message_repetitions)
{

View file

@ -67,7 +67,7 @@ function PIC(cpu, master)
}
dbg_assert(irq !== 0);
var irq_number = int_log2_table[irq];
var irq_number = v86util.int_log2_byte(irq);
irq = 1 << irq_number;
this.irr &= ~irq;
@ -115,7 +115,7 @@ function PIC(cpu, master)
}
dbg_assert(irq !== 0);
var irq_number = int_log2_table[irq];
var irq_number = v86util.int_log2_byte(irq);
irq = 1 << irq_number;
this.irr &= ~irq;