Skip to content

Commit 625bb0e

Browse files
committed
fix: Reference with encoding can accept index access
1 parent 6ac6728 commit 625bb0e

File tree

2 files changed

+49
-58
lines changed

2 files changed

+49
-58
lines changed

NativeScript/runtime/Reference.cpp

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -336,65 +336,65 @@ Reference::DataPair Reference::GetDataPair(Local<Object> obj) {
336336
BaseDataWrapper* wrapper = tns::GetValue(isolate, obj);
337337
tns::Assert(wrapper != nullptr && wrapper->Type() == WrapperType::Reference, isolate);
338338
ReferenceWrapper* refWrapper = static_cast<ReferenceWrapper*>(wrapper);
339-
340339
BaseDataWrapper* typeWrapper = refWrapper->TypeWrapper();
341-
if (typeWrapper == nullptr) {
342-
// TODO: Missing type when creating the Reference instance
343-
tns::Assert(false, isolate);
344-
}
345340

346341
size_t size = 0;
347-
const TypeEncoding* typeEncoding = nullptr;
348-
bool isUnknownType = false;
349-
350-
if (Reference::IsSupportedType(typeWrapper->Type())) {
351-
switch(typeWrapper->Type()) {
352-
case WrapperType::Primitive: {
353-
PrimitiveDataWrapper* primitiveWrapper = static_cast<PrimitiveDataWrapper*>(typeWrapper);
354-
355-
size = primitiveWrapper->Size();
356-
typeEncoding = primitiveWrapper->TypeEncoding();
357-
break;
358-
}
359-
case WrapperType::StructType: {
360-
StructTypeWrapper* structTypeWrapper = static_cast<StructTypeWrapper*>(refWrapper->TypeWrapper());
361-
StructInfo structInfo = structTypeWrapper->StructInfo();
362-
363-
size = structInfo.FFIType()->size;
364-
break;
365-
}
366-
default: {
367-
isUnknownType = true;
368-
break;
342+
343+
if (typeWrapper != nullptr) {
344+
const TypeEncoding* typeEncoding = nullptr;
345+
346+
if (Reference::IsSupportedType(typeWrapper->Type())) {
347+
switch(typeWrapper->Type()) {
348+
case WrapperType::Primitive: {
349+
PrimitiveDataWrapper* primitiveWrapper = static_cast<PrimitiveDataWrapper*>(typeWrapper);
350+
351+
size = primitiveWrapper->Size();
352+
typeEncoding = primitiveWrapper->TypeEncoding();
353+
break;
354+
}
355+
case WrapperType::StructType: {
356+
StructTypeWrapper* structTypeWrapper = static_cast<StructTypeWrapper*>(refWrapper->TypeWrapper());
357+
StructInfo structInfo = structTypeWrapper->StructInfo();
358+
359+
size = structInfo.FFIType()->size;
360+
break;
361+
}
362+
default: {
363+
break;
364+
}
369365
}
366+
} else {
367+
// TODO: Currently only PrimitiveDataWrappers and Structs are supported as type parameters
368+
// Objective C class classes should also be handled
369+
tns::Assert(false, isolate);
370370
}
371-
} else {
372-
isUnknownType = true;
373-
}
374-
375-
if (isUnknownType) {
376-
// TODO: Currently only PrimitiveDataWrappers and Structs are supported as type parameters
377-
// Objective C class classes should also be handled
378-
tns::Assert(false, isolate);
379-
}
380371

381-
Local<Value> value = refWrapper->Value()->Get(isolate);
382-
BaseDataWrapper* wrappedValue = tns::GetValue(isolate, value);
383-
if (wrappedValue != nullptr && wrappedValue->Type() == WrapperType::Pointer) {
384-
PointerWrapper* pw = static_cast<PointerWrapper*>(wrappedValue);
385-
void* data = pw->Data();
372+
Local<Value> value = refWrapper->Value()->Get(isolate);
373+
BaseDataWrapper* wrappedValue = tns::GetValue(isolate, value);
374+
if (wrappedValue != nullptr && wrappedValue->Type() == WrapperType::Pointer) {
375+
PointerWrapper* pw = static_cast<PointerWrapper*>(wrappedValue);
376+
void* data = pw->Data();
386377

387-
DataPair pair(typeWrapper, typeEncoding, data, size);
388-
return pair;
378+
DataPair pair(typeWrapper, typeEncoding, data, size);
379+
return pair;
380+
}
389381
}
390382

391383
if (refWrapper->Encoding() != nullptr && refWrapper->Data() != nullptr) {
392-
DataPair pair(typeWrapper, refWrapper->Encoding(), refWrapper->Data(), size);
384+
const TypeEncoding* typeEncoding = refWrapper->Encoding();
385+
386+
if (typeWrapper == nullptr) {
387+
ffi_type* ffiType = FFICall::GetArgumentType(typeEncoding);
388+
size = ffiType->size;
389+
FFICall::DisposeFFIType(ffiType, typeEncoding);
390+
}
391+
392+
DataPair pair(typeWrapper, typeEncoding, refWrapper->Data(), size);
393393
return pair;
394394
}
395395

396396
tns::Assert(false, isolate);
397-
return DataPair(typeWrapper, nullptr, nullptr, 0);
397+
return DataPair(typeWrapper, nullptr, nullptr, size);
398398
}
399399

400400
bool Reference::IsSupportedType(WrapperType type) {

TestRunner/app/tests/Marshalling/ReferenceTests.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -412,18 +412,14 @@ describe(module.id, function () {
412412
}
413413

414414
const pointCollection = TNSPointCollection.alloc().initWithPointsCount(ptr, length);
415-
416-
// Runtime converts pointers into references for cases with type so use handleof to get pointer
417-
const pointsPtr = interop.handleof(pointCollection.points);
415+
const pointsRef = pointCollection.points;
418416

419417
// Check if values were retrieved from pointer
420-
const resultRef = new interop.Reference(TNSPoint, pointsPtr);
421418
for (let i = 0; i < length; i++) {
422-
expect(TNSPoint.equals(resultRef[i], structs[i])).toBe(true);
419+
expect(TNSPoint.equals(pointsRef[i], structs[i])).toBe(true);
423420
}
424421

425422
interop.free(ptr);
426-
interop.free(pointsPtr);
427423
});
428424

429425
it("Reference access value from pointer array property", function () {
@@ -441,16 +437,11 @@ describe(module.id, function () {
441437
}
442438

443439
const pointCollection = TNSPointCollection.alloc().initWithPointsCount(ptr, length);
444-
445-
// Runtime converts pointers into references for cases with type so use handleof to get pointer
446-
const pointsPtr = interop.handleof(pointCollection.points);
440+
const pointsRef = pointCollection.points;
447441

448-
// Check if values were retrieved from pointer
449-
const resultRef = new interop.Reference(TNSPoint, pointsPtr);
450-
expect(TNSPoint.equals(resultRef.value, structs[0])).toBe(true);
442+
expect(TNSPoint.equals(pointsRef.value, structs[0])).toBe(true);
451443

452444
interop.free(ptr);
453-
interop.free(pointsPtr);
454445
});
455446

456447
it("interops string from CString with fixed length", function () {

0 commit comments

Comments
 (0)