Skip to content

Commit

Permalink
fix: boolean to NSNumber marshalling (#118)
Browse files Browse the repository at this point in the history
* fix: bool to NSNumber marshalling

* fix: check type encoding
  • Loading branch information
triniwiz authored Jul 3, 2021
1 parent 4dd79d2 commit 2c4d186
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions NativeScript/runtime/Interop.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class Interop {
static void RegisterAdoptFunction(v8::Local<v8::Context> context, v8::Local<v8::Object> interop);
static void RegisterSizeOfFunction(v8::Local<v8::Context> context, v8::Local<v8::Object> interop);
static void SetFFIParams(v8::Local<v8::Context> 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<v8::Array> ToArray(v8::Local<v8::Object> object);
static v8::Local<v8::Value> StructToValue(v8::Local<v8::Context> context, void* result, StructInfo structInfo, std::shared_ptr<v8::Persistent<v8::Value>> parentStruct);
static const TypeEncoding* CreateEncoding(BinaryTypeEncodingType type);
Expand Down
9 changes: 9 additions & 0 deletions NativeScript/runtime/Interop.mm
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,22 @@
}
}

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> context, const TypeEncoding* typeEncoding, void* dest, Local<Value> arg) {
Isolate* isolate = context->GetIsolate();

if (arg.IsEmpty() || arg->IsNullOrUndefined()) {
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 && isRefTypeEqual(typeEncoding, "NSNumber")) {
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);
Expand Down
3 changes: 2 additions & 1 deletion TestFixtures/Marshalling/TNSPrimitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
10 changes: 10 additions & 0 deletions TestFixtures/Marshalling/TNSPrimitives.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -263,4 +268,9 @@ - (unichar)methodWithUnichar:(unichar)x {
return x;
}

- (NSNumber*)methodWithNSNumber:(NSNumber*) x {
TNSLog([NSString stringWithFormat:@"%@", x]);
return x;
}

@end
16 changes: 16 additions & 0 deletions TestRunner/app/tests/Marshalling/Primitives/Instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
17 changes: 17 additions & 0 deletions TestRunner/app/tests/Marshalling/Primitives/Static.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});

0 comments on commit 2c4d186

Please sign in to comment.