Recent

Author Topic: Use Kotlin Native library  (Read 810 times)

mikutu

  • New Member
  • *
  • Posts: 11
Use Kotlin Native library
« on: June 29, 2020, 05:47:19 am »
https://kotlinlang.org/docs/tutorials/native/dynamic-libraries.html#creating-a-kotlin-library.

Problem in extern written only function return struct
Code: C  [Select][+][-]
  1. typedef struct {
  2.   ...
  3.   /* User functions. */
  4.   struct {
  5.     struct {
  6.       struct {
  7.         const char* (*hello)();
  8.         struct {
  9.           liblibnative_KType* (*_type)(void);
  10.           liblibnative_kref_sample_Platform (*_instance)();
  11.           const char* (*get_name)(liblibnative_kref_sample_Platform thiz);
  12.         } Platform;
  13.         struct {
  14.           liblibnative_KType* (*_type)(void);
  15.           liblibnative_kref_sample_Sample (*Sample)();
  16.           liblibnative_KInt (*checkMe)(liblibnative_kref_sample_Sample thiz);
  17.         } Sample;
  18.       } sample;
  19.     } root;
  20.   } kotlin;
  21. } liblibnative_ExportedSymbols;
  22. extern liblibnative_ExportedSymbols* liblibnative_symbols(void);

1) How to call function hello()?

2) Whether the class Sample can be used?

https://stackoverflow.com/questions/1496318/how-to-use-c-classes-exported-by-a-dll-in-delphi - write can't import a class

I can write a library wrapper on с++, but I would like to do without it.

Kotlin documentation - https://kotlinlang.org/docs/tutorials/native/dynamic-libraries.html#creating-a-kotlin-library

PascalDragon

  • Hero Member
  • *****
  • Posts: 2135
  • Compiler Developer
Re: Use Kotlin Native library
« Reply #1 on: June 29, 2020, 09:26:11 am »
Not tested, but it should be translated something like this (of course that can be cleaned up a bit by declaring appropriate subtypes):

Code: Pascal  [Select][+][-]
  1. type
  2.   Tliblibnative_ExportedSymbols = record
  3.     kotlin: record
  4.       root: record
  5.         sample: record
  6.           hello: function: PChar; cdecl;
  7.           Platform: record
  8.             _type: function: Pliblibnative_KType; cdecl;
  9.             _instance: function: Pliblibnative_kref_sample_Platform; cdecl;
  10.           end;
  11.           Sample: record
  12.             _type: function: PLiblibnative_KType; cdecl;
  13.             Sample: function: liblibnative_kref_sample_Sample; cdecl;
  14.             checkMe: function(thiz: liblibnative_kref_sample_Sample): liblibnative_KInt; cdecl;
  15.           end;
  16.         end;
  17.       end;
  18.     end;
  19.   end;
  20.   Pliblibnative_ExportedSymbols = ^Tliblibnative_ExportedSymbols;
  21.  
  22. function liblibnative_symbols: Pliblibnative_ExportedSymbols; cdecl; external;

jamie

  • Hero Member
  • *****
  • Posts: 3532
Re: Use Kotlin Native library
« Reply #2 on: June 29, 2020, 04:17:44 pm »
How does the "Sample" and "sample" resolve when they are in the same branch block ? at least that is what it looks like.
The only true wisdom is knowing you know nothing

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8729
  • FPC developer.
Re: Use Kotlin Native library
« Reply #3 on: June 29, 2020, 05:12:33 pm »
How does the "Sample" and "sample" resolve when they are in the same branch block ? at least that is what it looks like.

The three sample idents aren't on the same level. The middle Sample and platform are on the same level, the first sample is a level higher, the third a level lower.

But kotlin.root.sample.sample.sample is not really literal programming :)

jamie

  • Hero Member
  • *****
  • Posts: 3532
Re: Use Kotlin Native library
« Reply #4 on: June 29, 2020, 05:22:54 pm »
Blindness must be setting in because I see two...

Root.Sample

and
Root.sample

and yes I see the other but that isn't what I was referring to.

Oh well.
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 3532
Re: Use Kotlin Native library
« Reply #5 on: June 29, 2020, 07:23:03 pm »
Would be interesting to see how all this works out...

I think maybe the {$PACKRECORDS C} may need also to be used, especially if a single load of the record is performed.


The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 2135
  • Compiler Developer
Re: Use Kotlin Native library
« Reply #6 on: June 30, 2020, 09:37:55 am »
Blindness must be setting in because I see two...

Root.Sample

and
Root.sample

and yes I see the other but that isn't what I was referring to.

Oh well.

I take it you mean this part?

Code: Pascal  [Select][+][-]
  1.           Sample: record
  2.             _type: function: PLiblibnative_KType; cdecl;
  3.             Sample: function: liblibnative_kref_sample_Sample; cdecl;
  4.             checkMe: function(thiz: liblibnative_kref_sample_Sample): liblibnative_KInt; cdecl;
  5.           end;
  6.  

This is not a problem, because the outer Sample is the field name. And even if it would be the type name it would work as well, because things like this work, too:

Code: Pascal  [Select][+][-]
  1. type
  2.   hwnd = THandle;
  3.  
  4.   TMyRec = record
  5.     hwnd: hwnd;
  6.   end;

jamie

  • Hero Member
  • *****
  • Posts: 3532
Re: Use Kotlin Native library
« Reply #7 on: June 30, 2020, 12:30:00 pm »
Yes I understand how that works, it was my tired old eyes that blurred over your post..

 Sometimes hard to track the opening and closing of the Record/End's

The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 2135
  • Compiler Developer
Re: Use Kotlin Native library
« Reply #8 on: June 30, 2020, 01:51:28 pm »
Sometimes hard to track the opening and closing of the Record/End's

And here I took extra care to ident them nicely... ;)

 

TinyPortal © 2005-2018