Advertisement
WarPie90

FFI Simba 1.3 bug

Jul 16th, 2018
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.39 KB | None | 0 0
  1. { Example based upon the SPS Plugin }
  2. library proj;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. {$macro on}
  7. {$define callconv:=
  8.     {$IFDEF WINDOWS}{$IFDEF CPU32}cdecl;{$ELSE}{$ENDIF}{$ENDIF}
  9.     {$IFDEF LINUX}{$IFDEF CPU32}cdecl;{$ELSE}{$ENDIF}{$ENDIF}
  10. }
  11.  
  12. uses
  13.   classes, sysutils, math
  14.   { you can add units after this };
  15.  
  16. type
  17.   TCharPtrToStr = function(x: PChar): AnsiString; cdecl;
  18.  
  19. var
  20.   PCharToStr: TCharPtrToStr = nil;
  21.  
  22. procedure SetStringAllocator(a: TCharPtrToStr); callconv
  23. begin
  24.   PCharToStr := a;
  25. end;
  26.  
  27. function TestAlloc(): String; callconv
  28. var
  29.   tmp: String;
  30. begin
  31.   tmp := 'hello world'+#0;
  32.   Result := PCharToStr(@tmp[1]);
  33. end;
  34.  
  35. // --------------------
  36.  
  37. function GetPluginABIVersion: Integer; callconv export;
  38. begin
  39.   Result := 2;
  40. end;
  41.  
  42. procedure SetPluginMemManager(MemMgr : TMemoryManager); callconv export;
  43. begin
  44.   SetMemoryManager(MemMgr);
  45. end;
  46.  
  47. function GetTypeCount(): Integer; callconv export;
  48. begin
  49.   Result := 2;
  50. end;
  51.  
  52. function GetTypeInfo(x: Integer; var sType, sTypeDef: PChar): Integer; callconv export;
  53. begin
  54.   case x of
  55.     0: begin
  56.         StrPCopy(sType, '_TStringAllocator');
  57.         StrPCopy(sTypeDef, 'function(x: PChar): AnsiString;');
  58.        end;
  59.     1: begin
  60.         //-- cdecl on 32bit | ffi_win64 = 64bit
  61.         StrPCopy(sType, 'TStringAllocator');
  62.         StrPCopy(sTypeDef, 'native(_TStringAllocator, ffi_cdecl);');
  63.        end;
  64.     else
  65.       x := -1;
  66.   end;
  67.   Result := x;
  68. end;
  69.  
  70. function GetFunctionCount(): Integer; callconv export;
  71. begin
  72.   Result := 2;
  73. end;
  74.  
  75. function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; callconv export;
  76. begin
  77.   case x of
  78.     0:begin
  79.         ProcAddr := @SetStringAllocator;
  80.         StrPCopy(ProcDef, 'procedure SetStringAllocator(a: TStringAllocator);');
  81.       end;
  82.     1:begin
  83.         ProcAddr := @TestAlloc;
  84.         StrPCopy(ProcDef, 'function TestAlloc(): String;');
  85.       end;
  86.     else
  87.       x := -1;
  88.   end;
  89.   Result := x;
  90. end;
  91.  
  92. exports GetPluginABIVersion;
  93. exports SetPluginMemManager;
  94. exports GetTypeCount;
  95. exports GetTypeInfo;
  96. exports GetFunctionCount;
  97. exports GetFunctionInfo;
  98.  
  99. begin
  100. end.
  101.  
  102.  
  103.  
  104.  
  105. Simba test script:
  106. {$loadlib proj}
  107.  
  108. function StringFromChars(chars: PChar): AnsiString;
  109. begin
  110.   while chars^ <> #0 do
  111.   begin
  112.     Result += chars^;
  113.     Inc(chars);
  114.   end;
  115. end;
  116.  
  117. begin
  118.   SetStringAllocator(@StringFromChars);
  119.   TestAlloc;
  120. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement