Skip to content

Commit

Permalink
fix(buffer): add .buffer and #set to Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtcoolguy committed Jul 6, 2020
1 parent 1720456 commit 264b175
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions common/Resources/ti.internal/extensions/node/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ class Buffer { // FIXME: Extend Uint8Array
* Also supports the deprecated Buffer() constructors which are safe
* to use outside of this module.
*
* @param {integer[]|Buffer|integer|string|Ti.Buffer} arg
* @param {string|integer} encodingOrOffset
* @param {integer} length
* @param {integer[]|Buffer|integer|string|Ti.Buffer} arg the underlying data/bytes
* @param {string|integer} encodingOrOffset encoding of the string, or start offset of array/buffer
* @param {integer} length length of the underlying array/buffer to wrap
*/
constructor(arg, encodingOrOffset, length) {
// FIXME: Split into Fast/SlowBuffer. Have SlowBuffer wrap an existing Ti.Buffer
// Have FastBuffer just be an extension of Uint8Array
if (typeof arg !== 'object' || arg.apiName !== 'Ti.Buffer') {
showFlaggedDeprecation();

Expand Down Expand Up @@ -109,7 +111,23 @@ class Buffer { // FIXME: Extend Uint8Array
value: tiBuffer
}
});
// FIXME: Support .buffer property that holds an ArrayBuffer!
}

// This is a method we should get by extending Uint8Array, so really should only be overriden on a "SlowBuffer" that wraps Ti.Buffer
get buffer() {
// Get the slice of the array from byteOffset to length
return Uint8Array.from(this).buffer;
}

// This is a method we should get by extending Uint8Array, so really should only be overriden on a "SlowBuffer" that wraps Ti.Buffer
set(src, offset = 0) {
const numBytes = src.length;
// check src.length + offset doesn't go beyond our length!
checkOffset(this, offset, numBytes);
// copy src values into this buffer starting at offset
for (let i = 0; i < numBytes; i++) {
setAdjustedIndex(this, i + offset, src[i]);
}
}

/**
Expand Down

0 comments on commit 264b175

Please sign in to comment.