Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RangeError, and make it a teeny bit faster #8

Merged
merged 2 commits into from
May 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/quixe/gi_load.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,9 +742,16 @@ var decode_base64;
of the above.)
*/
if (window.btoa) {
encode_base64 = function(arr) {
return window.btoa(String.fromCharCode.apply(this, arr));
}
encode_base64 = function(image) {
// There's a limit on how much can be piped into .apply() at a time, so
// do this in chunks
var blocks = [];
for (var i = 0, l = image.length; i < l; i += 16384) {
blocks.push(String.fromCharCode.apply(String, image.slice(i, i + 16384)));
}

return btoa(blocks.join(''));
};
}
else {
encode_base64 = function(arr) {
Expand Down
42 changes: 21 additions & 21 deletions src/quixe/quixe.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ function Mem4(addr) {
return (memmap[addr] * 0x1000000) + (memmap[addr+1] * 0x10000)
+ (memmap[addr+2] * 0x100) + (memmap[addr+3]);
}
function MemSlice(addr, length) {
return memmap.slice(addr, addr + length);
}
function MemW1(addr, val) {
// ignore high bytes if necessary
memmap[addr] = val & 0xFF;
Expand All @@ -302,6 +305,7 @@ function MemW4(addr, val) {
self.Mem1 = Mem1;
self.Mem2 = Mem2;
self.Mem4 = Mem4;
self.MemSlice = MemSlice;
self.MemW1 = MemW1;
self.MemW2 = MemW2;
self.MemW4 = MemW4;
Expand Down Expand Up @@ -5077,52 +5081,48 @@ self.do_gestalt = do_gestalt;
/* This fetches a search key, and returns an array containing the key
(bytewise). Actually it always returns the same array.
*/
var tempsearchkey = [];
function fetch_search_key(addr, len, options) {
var ix;
tempsearchkey.length = len;

if (options & 1) {
/* indirect key */
for (ix=0; ix<len; ix++)
tempsearchkey[ix] = Mem1(addr+ix);
return MemSlice(addr, len);
}
else {
switch (len) {
case 4:
tempsearchkey[0] = (addr >> 24) & 0xFF;
tempsearchkey[1] = (addr >> 16) & 0xFF;
tempsearchkey[2] = (addr >> 8) & 0xFF;
tempsearchkey[3] = addr & 0xFF;
break;
return [
(addr >> 24) & 0xFF,
(addr >> 16) & 0xFF,
(addr >> 8) & 0xFF,
addr & 0xFF
];
case 2:
tempsearchkey[0] = (addr >> 8) & 0xFF;
tempsearchkey[1] = addr & 0xFF;
break;
return [
(addr >> 8) & 0xFF,
addr & 0xFF
];
case 1:
tempsearchkey[0] = addr & 0xFF;
break;
return [addr & 0xFF];
default:
throw('Direct search key must hold one, two, or four bytes.');
}
}

return tempsearchkey;
}

function linear_search(key, keysize, start,
structsize, numstructs, keyoffset, options) {

var ix, count, match, byt;
var ix, count, match, bytes;
var retindex = ((options & 4) != 0);
var zeroterm = ((options & 2) != 0);
var keybuf = fetch_search_key(key, keysize, options);

for (count=0; count<numstructs; count++, start+=structsize) {
match = true;
bytes = MemSlice(start + keyoffset, keysize);
for (ix=0; match && ix<keysize; ix++) {
byt = Mem1(start + keyoffset + ix);
if (byt != keybuf[ix])
if (bytes[ix] != keybuf[ix])
match = false;
}

Expand All @@ -5135,9 +5135,9 @@ function linear_search(key, keysize, start,

if (zeroterm) {
match = true;
bytes = MemSlice(start + keyoffset, keysize);
for (ix=0; match && ix<keysize; ix++) {
byt = Mem1(start + keyoffset + ix);
if (byt != 0)
if (bytes[ix] != 0)
match = false;
}

Expand Down