Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: boolean to NSNumber marshalling #118

Merged
merged 2 commits into from
Jul 3, 2021
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
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");
});
});