Skip to content

Commit

Permalink
fix(ios): resolve boolean ctx value conversion issue
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Sep 19, 2024
1 parent aaebcc5 commit de870a2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
6 changes: 5 additions & 1 deletion framework/ios/utils/NSObject+CtxValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ using CtxPtr = std::shared_ptr<hippy::napi::Ctx>;

@end

HIPPY_EXTERN id ObjectFromCtxValue(CtxPtr context, CtxValuePtr value);
/// Convert CtxValue to NSObject
/// - Parameters:
/// - context: hippy context
/// - value: ctx value
HIPPY_EXTERN __nullable id ObjectFromCtxValue(CtxPtr context, CtxValuePtr value);

NS_ASSUME_NONNULL_END
23 changes: 18 additions & 5 deletions framework/ios/utils/NSObject+CtxValue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ - (CtxValuePtr)convertToCtxValue:(const CtxPtr &)context {

@end

id ObjectFromCtxValue(CtxPtr context, CtxValuePtr value) {
__nullable id ObjectFromCtxValue(CtxPtr context, CtxValuePtr value) {
@autoreleasepool {
if (!context || !value) {
return [NSNull null];
return nil;
}
if (context->IsString(value)) {
footstone::string_view view;
Expand All @@ -157,6 +157,11 @@ id ObjectFromCtxValue(CtxPtr context, CtxValuePtr value) {
NSString *string = [NSString stringWithCharacters:(const unichar *)u16String.c_str() length:u16String.length()];
return string;
}
} else if (context->IsBoolean(value)) {
bool result = false;
if (context->GetValueBoolean(value, &result)) {
return @(result);
}
} else if (context->IsNumber(value)) {
double number = 0;
if (context->GetValueNumber(value, &number)) {
Expand All @@ -168,7 +173,9 @@ id ObjectFromCtxValue(CtxPtr context, CtxValuePtr value) {
for (uint32_t index = 0; index < length; index++) {
auto element = context->CopyArrayElement(value, index);
id obj = ObjectFromCtxValue(context, element);
[array addObject:obj];
if (obj) {
[array addObject:obj];
}
}
return [array copy];
}
Expand Down Expand Up @@ -199,11 +206,17 @@ id ObjectFromCtxValue(CtxPtr context, CtxValuePtr value) {
NSString *string = [NSString stringWithCharacters:(const unichar *)u16Str.c_str() length:u16Str.length()];
auto &value = it.second;
id obj = ObjectFromCtxValue(context, value);
[dictionary setObject:obj forKey:string];
if (string && obj) {
[dictionary setObject:obj forKey:string];
}
}
return [dictionary copy];
}
} else if (context->IsNull(value)) {
return [NSNull null];
} else if (context->IsUndefined(value)) {
return nil;
}
return [NSNull null];
return nil;
}
}
15 changes: 15 additions & 0 deletions tests/ios/HippyCtxValueConvertTest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ - (void)testNSNumberToCtxValue {
XCTAssertTrue([ObjectFromCtxValue(_context, ctxValue) isEqualToNumber:testOCNumber]);
}

// NSNumber (Boolean)
- (void)testBoolToCtxValue {
auto testCtxBoolean = _context->CreateBoolean(true);
NSNumber *testOCBool = @YES;
XCTAssert(_context->IsBoolean(testCtxBoolean));
XCTAssertTrue([ObjectFromCtxValue(_context, testCtxBoolean) boolValue] == [testOCBool boolValue]);
}

// NSArray
- (void)testNSArrayToCtxValue {
NSArray *testOCArray = @[@"Hello", @42, @YES];
Expand Down Expand Up @@ -113,6 +121,13 @@ - (void)testNSNullToCtxValue {
XCTAssert([ObjectFromCtxValue(_context, ctxValue) isKindOfClass:[NSNull class]]);
}

// Nil (Undefined)
- (void)testUndefinedCtxValue {
auto testCtxUndefined = _context->CreateUndefined();
XCTAssert(_context->IsUndefined(testCtxUndefined));
XCTAssertNil(ObjectFromCtxValue(_context, testCtxUndefined));
}

// NSError
- (void)testNSErrorToCtxValue {
NSError *testOCError = [NSError errorWithDomain:@"com.example" code:42 userInfo:nil];
Expand Down

0 comments on commit de870a2

Please sign in to comment.