Skip to content
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
8 changes: 6 additions & 2 deletions NativeScript/runtime/NSDataAdapter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ - (void*)mutableBytes {

Local<ArrayBufferView> bufferView = obj.As<ArrayBufferView>();
if (bufferView->HasBuffer()) {
void* data = bufferView->Buffer()->GetBackingStore()->Data();
return data;
uint8_t* data = static_cast<uint8_t*>(bufferView->Buffer()->GetBackingStore()->Data());
if (data == nullptr) {
return nullptr;
}

return data + bufferView->ByteOffset();
}

size_t length = bufferView->ByteLength();
Expand Down
1 change: 1 addition & 0 deletions TestFixtures/Marshalling/TNSObjCTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ typedef int (^NumberReturner)(int, int, int);
- (id)methodWithNSArrayWrappingDictionary:(id)array;
- (NSDictionary*)methodWithNSDictionary:(NSDictionary*)dictionary;
- (NSData*)methodWithNSData:(NSData*)data;
- (NSMutableData*)methodWithNSMutableData:(NSMutableData*)data;
- (NSDecimalNumber*)methodWithNSDecimalNumber:(NSDecimalNumber*)number;
- (NSNumber*)methodWithNSCFBool;
- (NSNull*)methodWithNSNull;
Expand Down
9 changes: 9 additions & 0 deletions TestFixtures/Marshalling/TNSObjCTypes.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ - (NSData*)methodWithNSData:(NSData*)data {
return data;
}

- (NSMutableData*)methodWithNSMutableData:(NSMutableData*)data {
if (data.length > 0) {
uint8_t* bytes = (uint8_t*)data.mutableBytes;
bytes[0] = 'A';
}

return data;
}

- (NSDecimalNumber*)methodWithNSDecimalNumber:(NSDecimalNumber*)number {
TNSLog([number stringValue]);
return number;
Expand Down
25 changes: 25 additions & 0 deletions TestRunner/app/tests/Marshalling/ObjCTypesTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,31 @@ describe(module.id, function () {
TNSClearOutput();
});

it("should respect ArrayBufferView byteOffset when wrapping in NSData", function () {
var source = new Uint8Array([48, 49, 50, 51, 52, 53]);
var view = new Uint8Array(source.buffer, 1, 4);

var wrappedArrayBufferViewData = TNSObjCTypes.alloc().init().methodWithNSData(view);

expect(TNSGetOutput()).toBe('1234');
expect(wrappedArrayBufferViewData).toBe(view);
TNSClearOutput();
});

it("should respect ArrayBufferView byteOffset when mutating NSMutableData", function () {
var source = new Uint8Array([48, 49, 50, 51, 52]);
var view = new Uint8Array(source.buffer, 2, 2);

var wrappedArrayBufferViewData = TNSObjCTypes.alloc().init().methodWithNSMutableData(view);

expect(wrappedArrayBufferViewData).toBe(view);
expect(source[0]).toEqual(48);
expect(source[1]).toEqual(49);
expect(source[2]).toEqual(65);
expect(source[3]).toEqual(51);
expect(source[4]).toEqual(52);
});

it("should be possible to wrap NSData in an ArrayBuffer", function () {
var data = NSString.stringWithString("test").dataUsingEncoding(NSUTF8StringEncoding);

Expand Down
Loading