Skip to content

Commit

Permalink
fix(common): expose Buffer.hexSlice to fix console.log of ArrayBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtcoolguy committed Jul 7, 2020
1 parent bcfe70b commit d7f863b
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions common/Resources/ti.internal/extensions/node/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -849,14 +849,7 @@ class Buffer { // FIXME: Extend Uint8Array
}

if (encoding === 'hex') {
let hexStr = '';
for (let i = 0; i < length; i++) {
// each one is a "byte"
let hex = (getAdjustedIndex(this, i) & 0xff).toString(16);
hex = (hex.length === 1) ? '0' + hex : hex;
hexStr += hex;
}
return hexStr;
return this.hexSlice(0, length);
}

if (encoding === 'latin1' || encoding === 'binary') {
Expand All @@ -881,6 +874,17 @@ class Buffer { // FIXME: Extend Uint8Array
return bufferToUTF16String(this._tiBuffer, this.byteOffset, this.length);
}

hexSlice(start, end) {
let hexStr = '';
for (let i = start; i < end; i++) {
// each one is a "byte"
let hex = (getAdjustedIndex(this, i) & 0xff).toString(16);
hex = (hex.length === 1) ? '0' + hex : hex;
hexStr += hex;
}
return hexStr;
}

/**
* Provides a conversion method for interacting with Ti APIs taht require a Ti.Buffer
* @returns {Ti.Buffer} the underlying Ti.Buffer backing this Buffer instance
Expand Down Expand Up @@ -1660,16 +1664,35 @@ const arrayIndexHandler = {
};

function getAdjustedIndex(buf, index) {
if (index < 0 || index >= buf._tiBuffer.length) {
if (index < 0) {
return undefined;
}
return buf._tiBuffer[index + buf.byteOffset];
// Wrapping Ti.Buffer?
if (buf._tiBuffer) {
if (index >= buf._tiBuffer.length) {
return undefined;
}
return buf._tiBuffer[index + buf.byteOffset];
}
// Raw TypedArray/ArrayBuffer
// FIXME: do we need to account for byteOffset here?
return buf[index];
}

function setAdjustedIndex(buf, index, value) {
if (index >= 0 || index < buf._tiBuffer.length) {
buf._tiBuffer[index + buf.byteOffset] = value;
if (index < 0) {
return;
}
// Wrapping Ti.Buffer?
if (buf._tiBuffer) {
if (index < buf._tiBuffer.length) {
buf._tiBuffer[index + buf.byteOffset] = value;
}
return;
}
// Raw TypedArray/ArrayBuffer
// FIXME: do we need to account for byteOffset here?
buf[index] = value;
}

/**
Expand Down

0 comments on commit d7f863b

Please sign in to comment.