unit Chilkat.Js;

{
  Pascal wrapper for Chilkat Js.
  Auto-generated by PascalWrapperGenerator -- do not edit manually.
}

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}

interface

uses
  SysUtils,
  Chilkat.Base;

type

  { TJs wraps CkJs via the C bridge layer. }
  TJs = class(TChilkatBase)
  private
    FOnAbortCheck: TChilkatAbortCheckEvent;
    FOnPercentDone: TChilkatPercentDoneEvent;
    FOnProgressInfo: TChilkatProgressInfoEvent;
    function  GetLastErrorHtml: string;
    function  GetLastErrorText: string;
    function  GetLastErrorXml: string;
    function  GetVersion: string;
    function  GetDebugLogFilePath: string;
    procedure SetDebugLogFilePath(AValue: string);
    function  GetVerboseLogging: Boolean;
    procedure SetVerboseLogging(AValue: Boolean);
    function  GetLastMethodSuccess: Boolean;
    procedure SetLastMethodSuccess(AValue: Boolean);
    function  GetTimeoutSeconds: Integer;
    procedure SetTimeoutSeconds(AValue: Integer);
    procedure SetOnAbortCheck(AValue: TChilkatAbortCheckEvent);
    procedure SetOnPercentDone(AValue: TChilkatPercentDoneEvent);
    procedure SetOnProgressInfo(AValue: TChilkatProgressInfoEvent);
  public
    constructor Create;
    destructor  Destroy; override;

    property LastErrorHtml: string read GetLastErrorHtml;
    property LastErrorText: string read GetLastErrorText;
    property LastErrorXml: string read GetLastErrorXml;
    property Version: string read GetVersion;
    property DebugLogFilePath: string read GetDebugLogFilePath write SetDebugLogFilePath;
    property VerboseLogging: Boolean read GetVerboseLogging write SetVerboseLogging;
    property LastMethodSuccess: Boolean read GetLastMethodSuccess write SetLastMethodSuccess;
    property TimeoutSeconds: Integer read GetTimeoutSeconds write SetTimeoutSeconds;

    { Event callbacks.  Return True from OnAbortCheck / OnPercentDone
      to abort the running method.  Callbacks are invoked on the
      thread that called the Chilkat method.  The HeartbeatMs
      property controls the AbortCheck frequency. }
    property OnAbortCheck: TChilkatAbortCheckEvent read FOnAbortCheck write SetOnAbortCheck;
    property OnPercentDone: TChilkatPercentDoneEvent read FOnPercentDone write SetOnPercentDone;
    property OnProgressInfo: TChilkatProgressInfoEvent read FOnProgressInfo write SetOnProgressInfo;

    function  Eval(jscript: TChilkatBase; resultJson: TChilkatBase): Boolean;
    procedure ConsoleOutputSb(sb: TChilkatBase);
    function  CallFunction(funcJson: TChilkatBase; resultJson: TChilkatBase): Boolean;
    function  SetCkPermission(const chilkatClassName: string; const permission: string): Boolean;
  end;

implementation

uses
  CkDllLoader,
  Chilkat.Utils;

{ ---- C bridge function pointer types ---- }
type
  TCkJs_Create  = function(): Pointer; cdecl;
  TCkJs_Dispose = procedure(handle: Pointer); cdecl;
  TCkBridgeAbortCheckCb   = function(userData: Pointer): Integer; cdecl;
  TCkBridgePercentDoneCb  = function(pctDone: Integer; userData: Pointer): Integer; cdecl;
  TCkBridgeProgressInfoCb = procedure(name: PAnsiChar; value: PAnsiChar; userData: Pointer); cdecl;
  TCkJs_SetAbortCheckCallback   = procedure(handle: Pointer; cb: TCkBridgeAbortCheckCb; userData: Pointer); cdecl;
  TCkJs_SetPercentDoneCallback  = procedure(handle: Pointer; cb: TCkBridgePercentDoneCb; userData: Pointer); cdecl;
  TCkJs_SetProgressInfoCallback = procedure(handle: Pointer; cb: TCkBridgeProgressInfoCb; userData: Pointer); cdecl;
  TCkJs_get_LastErrorHtml = function(handle: Pointer): PAnsiChar; cdecl;
  TCkJs_get_LastErrorText = function(handle: Pointer): PAnsiChar; cdecl;
  TCkJs_get_LastErrorXml = function(handle: Pointer): PAnsiChar; cdecl;
  TCkJs_get_Version = function(handle: Pointer): PAnsiChar; cdecl;
  TCkJs_get_DebugLogFilePath = function(handle: Pointer): PAnsiChar; cdecl;
  TCkJs_put_DebugLogFilePath = procedure(handle: Pointer; value: PAnsiChar); cdecl;
  TCkJs_get_VerboseLogging = function(handle: Pointer): LongBool; cdecl;
  TCkJs_put_VerboseLogging = procedure(handle: Pointer; value: LongBool); cdecl;
  TCkJs_get_LastMethodSuccess = function(handle: Pointer): LongBool; cdecl;
  TCkJs_put_LastMethodSuccess = procedure(handle: Pointer; value: LongBool); cdecl;
  TCkJs_get_TimeoutSeconds = function(handle: Pointer): Integer; cdecl;
  TCkJs_put_TimeoutSeconds = procedure(handle: Pointer; value: Integer); cdecl;
  TCkJs_Eval = function(handle: Pointer; jscript: Pointer; resultJson: Pointer): LongBool; cdecl;
  TCkJs_ConsoleOutputSb = procedure(handle: Pointer; sb: Pointer); cdecl;
  TCkJs_CallFunction = function(handle: Pointer; funcJson: Pointer; resultJson: Pointer): LongBool; cdecl;
  TCkJs_SetCkPermission = function(handle: Pointer; chilkatClassName: PAnsiChar; permission: PAnsiChar): LongBool; cdecl;

{ ---- C bridge function pointer variables ---- }
var
  CkJs_Create:  TCkJs_Create  = nil;
  CkJs_Dispose: TCkJs_Dispose = nil;
  CkJs_SetAbortCheckCallback:   TCkJs_SetAbortCheckCallback   = nil;
  CkJs_SetPercentDoneCallback:  TCkJs_SetPercentDoneCallback  = nil;
  CkJs_SetProgressInfoCallback: TCkJs_SetProgressInfoCallback = nil;
  CkJs_get_LastErrorHtml: TCkJs_get_LastErrorHtml = nil;
  CkJs_get_LastErrorText: TCkJs_get_LastErrorText = nil;
  CkJs_get_LastErrorXml: TCkJs_get_LastErrorXml = nil;
  CkJs_get_Version: TCkJs_get_Version = nil;
  CkJs_get_DebugLogFilePath: TCkJs_get_DebugLogFilePath = nil;
  CkJs_put_DebugLogFilePath: TCkJs_put_DebugLogFilePath = nil;
  CkJs_get_VerboseLogging: TCkJs_get_VerboseLogging = nil;
  CkJs_put_VerboseLogging: TCkJs_put_VerboseLogging = nil;
  CkJs_get_LastMethodSuccess: TCkJs_get_LastMethodSuccess = nil;
  CkJs_put_LastMethodSuccess: TCkJs_put_LastMethodSuccess = nil;
  CkJs_get_TimeoutSeconds: TCkJs_get_TimeoutSeconds = nil;
  CkJs_put_TimeoutSeconds: TCkJs_put_TimeoutSeconds = nil;
  CkJs_Eval: TCkJs_Eval = nil;
  CkJs_ConsoleOutputSb: TCkJs_ConsoleOutputSb = nil;
  CkJs_CallFunction: TCkJs_CallFunction = nil;
  CkJs_SetCkPermission: TCkJs_SetCkPermission = nil;

constructor TJs.Create;
begin
  inherited Create;
  FHandle := CkJs_Create();
end;

destructor TJs.Destroy;
begin
  if FHandle <> nil then
    CkJs_Dispose(FHandle);
  FHandle := nil;
  inherited;
end;

{ ---- Event callback thunks ---- }
{ Standalone cdecl functions registered with the C bridge.  The bridge
  passes back the userData pointer, which carries the Pascal object
  instance.  A non-zero return value aborts the running method. }

function TJs_AbortCheckThunk(userData: Pointer): Integer; cdecl;
var
  obj: TJs;
begin
  Result := 0;
  obj := TJs(userData);
  if Assigned(obj) and Assigned(obj.FOnAbortCheck) then
    if obj.FOnAbortCheck(obj) then Result := 1;
end;

function TJs_PercentDoneThunk(pctDone: Integer; userData: Pointer): Integer; cdecl;
var
  obj: TJs;
begin
  Result := 0;
  obj := TJs(userData);
  if Assigned(obj) and Assigned(obj.FOnPercentDone) then
    if obj.FOnPercentDone(obj, pctDone) then Result := 1;
end;

procedure TJs_ProgressInfoThunk(name: PAnsiChar; value: PAnsiChar; userData: Pointer); cdecl;
var
  obj: TJs;
begin
  obj := TJs(userData);
  if Assigned(obj) and Assigned(obj.FOnProgressInfo) then
    obj.FOnProgressInfo(obj, CkToStr(name), CkToStr(value));
end;

procedure TJs.SetOnAbortCheck(AValue: TChilkatAbortCheckEvent);
begin
  FOnAbortCheck := AValue;
  if Assigned(AValue) then
    CkJs_SetAbortCheckCallback(FHandle, @TJs_AbortCheckThunk, Self)
  else
    CkJs_SetAbortCheckCallback(FHandle, nil, nil);
end;

procedure TJs.SetOnPercentDone(AValue: TChilkatPercentDoneEvent);
begin
  FOnPercentDone := AValue;
  if Assigned(AValue) then
    CkJs_SetPercentDoneCallback(FHandle, @TJs_PercentDoneThunk, Self)
  else
    CkJs_SetPercentDoneCallback(FHandle, nil, nil);
end;

procedure TJs.SetOnProgressInfo(AValue: TChilkatProgressInfoEvent);
begin
  FOnProgressInfo := AValue;
  if Assigned(AValue) then
    CkJs_SetProgressInfoCallback(FHandle, @TJs_ProgressInfoThunk, Self)
  else
    CkJs_SetProgressInfoCallback(FHandle, nil, nil);
end;

function TJs.GetLastErrorHtml: string;
begin
  Result := CkToStr(CkJs_get_LastErrorHtml(FHandle));
end;

function TJs.GetLastErrorText: string;
begin
  Result := CkToStr(CkJs_get_LastErrorText(FHandle));
end;

function TJs.GetLastErrorXml: string;
begin
  Result := CkToStr(CkJs_get_LastErrorXml(FHandle));
end;

function TJs.GetVersion: string;
begin
  Result := CkToStr(CkJs_get_Version(FHandle));
end;

function TJs.GetDebugLogFilePath: string;
begin
  Result := CkToStr(CkJs_get_DebugLogFilePath(FHandle));
end;

procedure TJs.SetDebugLogFilePath(AValue: string);
begin
  CkJs_put_DebugLogFilePath(FHandle, PAnsiChar(StrToCk(AValue)));
end;

function TJs.GetVerboseLogging: Boolean;
begin
  Result := Boolean(CkJs_get_VerboseLogging(FHandle));
end;

procedure TJs.SetVerboseLogging(AValue: Boolean);
begin
  CkJs_put_VerboseLogging(FHandle, LongBool(AValue));
end;

function TJs.GetLastMethodSuccess: Boolean;
begin
  Result := Boolean(CkJs_get_LastMethodSuccess(FHandle));
end;

procedure TJs.SetLastMethodSuccess(AValue: Boolean);
begin
  CkJs_put_LastMethodSuccess(FHandle, LongBool(AValue));
end;

function TJs.GetTimeoutSeconds: Integer;
begin
  Result := CkJs_get_TimeoutSeconds(FHandle);
end;

procedure TJs.SetTimeoutSeconds(AValue: Integer);
begin
  CkJs_put_TimeoutSeconds(FHandle, AValue);
end;

function TJs.Eval(jscript: TChilkatBase; resultJson: TChilkatBase): Boolean;
begin
  Result := Boolean(CkJs_Eval(FHandle, jscript.Handle, resultJson.Handle));
end;

procedure TJs.ConsoleOutputSb(sb: TChilkatBase);
begin
  CkJs_ConsoleOutputSb(FHandle, sb.Handle);
end;

function TJs.CallFunction(funcJson: TChilkatBase; resultJson: TChilkatBase): Boolean;
begin
  Result := Boolean(CkJs_CallFunction(FHandle, funcJson.Handle, resultJson.Handle));
end;

function TJs.SetCkPermission(const chilkatClassName: string; const permission: string): Boolean;
begin
  Result := Boolean(CkJs_SetCkPermission(FHandle, PAnsiChar(StrToCk(chilkatClassName)), PAnsiChar(StrToCk(permission))));
end;

initialization
  Chilkat_GetProcedureAddress(CkJs_Create,  'CkJs_Create');
  Chilkat_GetProcedureAddress(CkJs_Dispose, 'CkJs_Dispose');
  Chilkat_GetProcedureAddress(CkJs_SetAbortCheckCallback, 'CkJs_SetAbortCheckCallback');
  Chilkat_GetProcedureAddress(CkJs_SetPercentDoneCallback, 'CkJs_SetPercentDoneCallback');
  Chilkat_GetProcedureAddress(CkJs_SetProgressInfoCallback, 'CkJs_SetProgressInfoCallback');
  Chilkat_GetProcedureAddress(CkJs_get_LastErrorHtml, 'CkJs_get_LastErrorHtml');
  Chilkat_GetProcedureAddress(CkJs_get_LastErrorText, 'CkJs_get_LastErrorText');
  Chilkat_GetProcedureAddress(CkJs_get_LastErrorXml, 'CkJs_get_LastErrorXml');
  Chilkat_GetProcedureAddress(CkJs_get_Version, 'CkJs_get_Version');
  Chilkat_GetProcedureAddress(CkJs_get_DebugLogFilePath, 'CkJs_get_DebugLogFilePath');
  Chilkat_GetProcedureAddress(CkJs_put_DebugLogFilePath, 'CkJs_put_DebugLogFilePath');
  Chilkat_GetProcedureAddress(CkJs_get_VerboseLogging, 'CkJs_get_VerboseLogging');
  Chilkat_GetProcedureAddress(CkJs_put_VerboseLogging, 'CkJs_put_VerboseLogging');
  Chilkat_GetProcedureAddress(CkJs_get_LastMethodSuccess, 'CkJs_get_LastMethodSuccess');
  Chilkat_GetProcedureAddress(CkJs_put_LastMethodSuccess, 'CkJs_put_LastMethodSuccess');
  Chilkat_GetProcedureAddress(CkJs_get_TimeoutSeconds, 'CkJs_get_TimeoutSeconds');
  Chilkat_GetProcedureAddress(CkJs_put_TimeoutSeconds, 'CkJs_put_TimeoutSeconds');
  Chilkat_GetProcedureAddress(CkJs_Eval, 'CkJs_Eval');
  Chilkat_GetProcedureAddress(CkJs_ConsoleOutputSb, 'CkJs_ConsoleOutputSb');
  Chilkat_GetProcedureAddress(CkJs_CallFunction, 'CkJs_CallFunction');
  Chilkat_GetProcedureAddress(CkJs_SetCkPermission, 'CkJs_SetCkPermission');

end.
