Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
chakrashim: using new jsrt interface
Browse files Browse the repository at this point in the history
Several scenarios involve moving buffers of 8-bit chars around.
Previously the only way to get 8-bit chars out of jsrt was to get
16-bit chars and then marshal them manually. To assist with this,
we've added a new JsCopyStringOneByte function to directly extract
8-bit strings without needing intermediate storage.

PR-URL: #344
Reviewed-By: Kunal Pathak <kunal.pathak@microsoft.com>
Reviewed-By: Hitesh Kanwathirtha <hiteshk@microsoft.com>
Reviewed-By: Kyle Farnung <kfarnung@microsoft.com>
  • Loading branch information
MSLaguana authored and kfarnung committed Jul 25, 2017
1 parent 72b7dc2 commit 5c6f12a
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions deps/chakrashim/src/v8string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,14 @@ int String::Write(uint16_t *buffer, int start, int length, int options) const {

int String::WriteOneByte(
uint8_t* buffer, int start, int length, int options) const {
// The JSRT API only supports utf8 and utf16 encoded strings. In order to get
// 8 bit bytes from the string (i.e. Latin1) we can get the utf16 string and
// cast each character down to a uint8_t. This will only work for characters
// between U+0000 and U+00FF in the source string.
uint16_t* tmpBuffer = new uint16_t[length];
size_t count = 0;
if (JsCopyStringUtf16((JsValueRef)this, start, length,
tmpBuffer, &count) == JsNoError) {
for (size_t i = 0; i < count; i++) {
buffer[i] = (uint8_t)tmpBuffer[i];
}

if (!(options & String::NO_NULL_TERMINATION)) {
buffer[count] = 0;
if (JsCopyStringOneByte((JsValueRef)this, start, length,
(char *)buffer, &count) == JsNoError) {
if (!(options & String::NO_NULL_TERMINATION) &&
(length == -1 || count < length)) {
buffer[count] = '\0';
}
}
delete[] tmpBuffer;
return count;
}

Expand Down

0 comments on commit 5c6f12a

Please sign in to comment.