From 826e27fa79a96014e02d5e7d3224e6a80178a099 Mon Sep 17 00:00:00 2001 From: triniwiz Date: Wed, 2 Jun 2021 06:20:46 -0400 Subject: [PATCH 1/2] fix: bool to NSNumber marshalling --- NativeScript/runtime/Interop.mm | 4 ++++ TestFixtures/Marshalling/TNSPrimitives.h | 3 ++- TestFixtures/Marshalling/TNSPrimitives.m | 10 ++++++++++ .../tests/Marshalling/Primitives/Instance.js | 16 ++++++++++++++++ .../app/tests/Marshalling/Primitives/Static.js | 17 +++++++++++++++++ 5 files changed, 49 insertions(+), 1 deletion(-) diff --git a/NativeScript/runtime/Interop.mm b/NativeScript/runtime/Interop.mm index e661424f..ce52e13c 100644 --- a/NativeScript/runtime/Interop.mm +++ b/NativeScript/runtime/Interop.mm @@ -132,6 +132,10 @@ ffi_type* ffiType = FFICall::GetArgumentType(typeEncoding, true); size_t size = ffiType->size; memset(dest, 0, size); + } else if (tns::IsBool(arg) && typeEncoding->type == BinaryTypeEncodingType::InterfaceDeclarationReference) { + bool value = tns::ToBool(arg); + NSNumber *num = [NSNumber numberWithBool: value]; + Interop::SetValue(dest, num); } else if (tns::IsBool(arg) && typeEncoding->type == BinaryTypeEncodingType::IdEncoding) { bool value = tns::ToBool(arg); NSObject* o = @(value); diff --git a/TestFixtures/Marshalling/TNSPrimitives.h b/TestFixtures/Marshalling/TNSPrimitives.h index ed3408bb..0a67decd 100644 --- a/TestFixtures/Marshalling/TNSPrimitives.h +++ b/TestFixtures/Marshalling/TNSPrimitives.h @@ -43,6 +43,7 @@ unichar functionWithUnichar(unichar x); + (NSNull*)methodWithNull:(NSNull*)x; + (unichar)methodWithUnichar:(unichar)x; + (id)methodWithId:(id)x; ++ (NSNumber*)methodWithNSNumber:(NSNumber*)x; - (char)methodWithChar:(char)x; - (short)methodWithShort:(short)x; @@ -64,5 +65,5 @@ unichar functionWithUnichar(unichar x); - (Protocol*)methodWithProtocol:(Protocol*)x; - (NSNull*)methodWithNull:(NSNull*)x; - (unichar)methodWithUnichar:(unichar)x; - +- (NSNumber*)methodWithNSNumber:(NSNumber*)x; @end diff --git a/TestFixtures/Marshalling/TNSPrimitives.m b/TestFixtures/Marshalling/TNSPrimitives.m index 82b2e7bf..577662bc 100644 --- a/TestFixtures/Marshalling/TNSPrimitives.m +++ b/TestFixtures/Marshalling/TNSPrimitives.m @@ -182,6 +182,11 @@ + (int)methodVariadicSum:(int)count, ... { return sum; } ++(NSNumber*)methodWithNSNumber:(NSNumber*) x { + TNSLog([NSString stringWithFormat:@"%@", x]); + return x; +} + - (char)methodWithChar:(char)x { TNSLog([NSString stringWithFormat:@"%hhd", x]); return x; @@ -263,4 +268,9 @@ - (unichar)methodWithUnichar:(unichar)x { return x; } +- (NSNumber*)methodWithNSNumber:(NSNumber*) x { + TNSLog([NSString stringWithFormat:@"%@", x]); + return x; +} + @end diff --git a/TestRunner/app/tests/Marshalling/Primitives/Instance.js b/TestRunner/app/tests/Marshalling/Primitives/Instance.js index 5257a604..161efb23 100644 --- a/TestRunner/app/tests/Marshalling/Primitives/Instance.js +++ b/TestRunner/app/tests/Marshalling/Primitives/Instance.js @@ -224,4 +224,20 @@ describe(module.id, function () { TNSPrimitives.alloc().init().methodWithUnichar('iPhone'); }).toThrowError(); }); + + it("InstanceMethodWithNSNumber1", function () { + var result = TNSPrimitives.alloc().init().methodWithNSNumber(0); + expect(result).toBe(0); + + var actual = TNSGetOutput(); + expect(actual).toBe("0"); + }); + + it("InstanceMethodWithNSNumber2", function () { + var result = TNSPrimitives.alloc().init().methodWithNSNumber(true); + expect(result).toBe(true); + + var actual = TNSGetOutput(); + expect(actual).toBe("1"); + }); }); diff --git a/TestRunner/app/tests/Marshalling/Primitives/Static.js b/TestRunner/app/tests/Marshalling/Primitives/Static.js index 92339170..8028e135 100644 --- a/TestRunner/app/tests/Marshalling/Primitives/Static.js +++ b/TestRunner/app/tests/Marshalling/Primitives/Static.js @@ -224,4 +224,21 @@ describe(module.id, function () { TNSPrimitives.methodWithUnichar('iPhone'); }).toThrowError(); }); + + + it("StaticMethodWithNSNumber1", function () { + var result = TNSPrimitives.methodWithNSNumber(0); + expect(result).toBe(0); + + var actual = TNSGetOutput(); + expect(actual).toBe("0"); + }); + + it("StaticMethodWithNSNumber2", function () { + var result = TNSPrimitives.methodWithNSNumber(true); + expect(result).toBe(true); + + var actual = TNSGetOutput(); + expect(actual).toBe("1"); + }); }); From fb4068340aa1e6896fcba046cbdecf007caab6d8 Mon Sep 17 00:00:00 2001 From: triniwiz Date: Wed, 2 Jun 2021 09:45:57 -0400 Subject: [PATCH 2/2] fix: check type encoding --- NativeScript/runtime/Interop.h | 1 + NativeScript/runtime/Interop.mm | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/NativeScript/runtime/Interop.h b/NativeScript/runtime/Interop.h index bf1863d4..dd6f6621 100644 --- a/NativeScript/runtime/Interop.h +++ b/NativeScript/runtime/Interop.h @@ -138,6 +138,7 @@ class Interop { static void RegisterAdoptFunction(v8::Local context, v8::Local interop); static void RegisterSizeOfFunction(v8::Local context, v8::Local interop); static void SetFFIParams(v8::Local context, const TypeEncoding* typeEncoding, FFICall* call, const int argsCount, const int initialParameterIndex, V8Args& args); + static bool isRefTypeEqual(const TypeEncoding* typeEncoding,const char* clazz); static v8::Local ToArray(v8::Local object); static v8::Local StructToValue(v8::Local context, void* result, StructInfo structInfo, std::shared_ptr> parentStruct); static const TypeEncoding* CreateEncoding(BinaryTypeEncodingType type); diff --git a/NativeScript/runtime/Interop.mm b/NativeScript/runtime/Interop.mm index ce52e13c..4e13348c 100644 --- a/NativeScript/runtime/Interop.mm +++ b/NativeScript/runtime/Interop.mm @@ -125,6 +125,11 @@ } } +bool Interop::isRefTypeEqual(const TypeEncoding* typeEncoding, const char* clazz){ + std::string n(&typeEncoding->details.interfaceDeclarationReference.name.value()); + return n.compare(clazz) == 0; +} + void Interop::WriteValue(Local context, const TypeEncoding* typeEncoding, void* dest, Local arg) { Isolate* isolate = context->GetIsolate(); @@ -132,7 +137,7 @@ ffi_type* ffiType = FFICall::GetArgumentType(typeEncoding, true); size_t size = ffiType->size; memset(dest, 0, size); - } else if (tns::IsBool(arg) && typeEncoding->type == BinaryTypeEncodingType::InterfaceDeclarationReference) { + } else if (tns::IsBool(arg) && typeEncoding->type == BinaryTypeEncodingType::InterfaceDeclarationReference && isRefTypeEqual(typeEncoding, "NSNumber")) { bool value = tns::ToBool(arg); NSNumber *num = [NSNumber numberWithBool: value]; Interop::SetValue(dest, num);