Skip to content

Commit

Permalink
Fixes for bun
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Nov 6, 2022
1 parent 51a494b commit 8e46ed4
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function writeKey(key, target, position, inSequence) {
}
} else {
if (target.utf8Write)
position += target.utf8Write(key, position)
position += target.utf8Write(key, position, 0xffffffff)
else
position += textEncoder.encodeInto(key, target.subarray(position)).written
if (position > target.length - 4)
Expand Down Expand Up @@ -305,7 +305,23 @@ function finishUtf8(byte1, src) {
}
}

const readString = eval(makeStringBuilder())
const readString =
typeof process !== 'undefined' && process.isBun ? // the eval in bun doesn't properly closure on position, so we
// have to manually update it
(function(reading) {
let { setPosition, getPosition, readString } = reading;
return (source) => {
setPosition(position);
let value = readString(source);
position = getPosition();
return value;
};
})((new Function('fromCharCode', 'let position; let readString = ' + makeStringBuilder() +
';return {' +
'setPosition(p) { position = p },' +
'getPosition() { return position },' +
'readString }'))(fromCharCode)) :
eval(makeStringBuilder())

export function compareKeys(a, b) {
// compare with type consistency that matches binary comparison
Expand Down

0 comments on commit 8e46ed4

Please sign in to comment.