NativeScript / ios

NativeScript for iOS and visionOS using V8
https://docs.nativescript.org/guide/ios-marshalling
131 stars 33 forks source link

Question on declarations generation #205

Open legion151 opened 1 year ago

legion151 commented 1 year ago

Hello and thanks for your work.

I have a question regarding the generation of ios typescript declarations hoping to be in the right place.

We're using a robovm based toolchain to build a native library for iOS from Java-code. This toolchain emits header files in the following (shortened) form:


@protocol Option                                                                                
-(NSString *) optionId;                                 
-(NSString *) type;                    
@end                                      
typedef NSObject<Option> Option; 

@protocol Info                                                                                 
-(NSArray<NSObject<Option> *> *) getOptions;
@end
typedef NSObject<Info> Info;

the (shortened) declarations in ios.d.ts are


interface Option {                                      

        optionId(): string;                             

        type(): string;                                 
}                                                       
declare var Option: {                                   

        prototype: Option;                              
};

interface Info {

        getOptions(): NSArray<NSObject>;

}                  
declare var Info: {       

        prototype: Info;          
};

You may notice that the returned Array of getOptions is typed with NSObject instead of Option, which is the reason for this writing.

In fact are all argument- and return- types of functions which are defined by our headers typed with NSObject instead of the type which it should be.

After digging a while i understand that the types are evaluated in that function https://github.com/NativeScript/ios/blob/main/metadata-generator/src/TypeScript/DefinitionWriter.cpp#L862 which might be wrong since it's been a while that i read some cpp code.

I assume that this codes falls back to NSObject if it cannot determine the correct type. Note, that if i manually adjust the types in ios.d.ts everything works fine.

So basically my questions:

Cheers and again thanks for your work. legion

rigor789 commented 1 year ago

I don't know the answer, we'll have to look a bit deeper and understand what's going on - I would expect the return type to be NSArray<Option> but it's totally possible the metadata-generator/types-generator doesn't cover/handle this case yet and has to be implemented.

edusperoni commented 1 year ago

Shouldn't the generated declaration have Option extend NSObject in some way? Theoretically it returns NSObjects which is correct, but it's also Option. I might be wrong though

rigor789 commented 1 year ago

Yeah, I guess it should generate something like

interface Option extends NSObject {                                      
  optionId(): string;                             
  type(): string;                                 
}                                                       
declare var Option: {                                   
  prototype: Option;                              
};

interface Info {
  getOptions(): NSArray<Option>;                                      
}                  
declare var Info: {       
  prototype: Info;          
};
edusperoni commented 1 year ago

@rigor789 I'm not sure about it, but the runtime sometimes can do some magic when converting struct types where you can pass Option without implementing NSObject and it'll convert into an NSObject

https://docs.nativescript.org/advanced-concepts.html#struct-types

Maybe this is the correct output:

interface Option {
  optionId(): string
  type(): string
}

declare var Option: {
  prototype: Option
}

interface Info {
  getOptions(): NSArray<NSObject & Option>
}

declare var Info: {
  prototype: Info
}

This way you can use Option (and the magic) in a transparent way, but the return is NSObject & Option, so you know it's not a simple struct, but a full NSObject with those added methods