diff --git a/framework/ios/utils/NSObject+CtxValue.h b/framework/ios/utils/NSObject+CtxValue.h index 50ad7be36d5..573f6953577 100644 --- a/framework/ios/utils/NSObject+CtxValue.h +++ b/framework/ios/utils/NSObject+CtxValue.h @@ -77,6 +77,10 @@ using CtxPtr = std::shared_ptr; @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 diff --git a/framework/ios/utils/NSObject+CtxValue.mm b/framework/ios/utils/NSObject+CtxValue.mm index c7177eeade8..591c24aa007 100644 --- a/framework/ios/utils/NSObject+CtxValue.mm +++ b/framework/ios/utils/NSObject+CtxValue.mm @@ -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; @@ -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)) { @@ -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]; } @@ -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; } } diff --git a/tests/ios/HippyCtxValueConvertTest.mm b/tests/ios/HippyCtxValueConvertTest.mm index 040def8b410..ed45bab3183 100644 --- a/tests/ios/HippyCtxValueConvertTest.mm +++ b/tests/ios/HippyCtxValueConvertTest.mm @@ -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]; @@ -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];