FastScriptを使ってみる DelphiのコードからFastScriptの関数を実行する

DelphiのコードからFastScriptの関数を実行するには、TfsScriptのCallFunctionメソッドを使用します。

最初の引数に関数名、2番目の引数に関数の引数を設定します。

val := fsScript1.CallFunction(
  'ScriptFunc',
  VarArrayOf(['hello', 1]));

次のサンプルコードでは、Script内のPlus関数をDelphiのコードから呼び出します。

var
  Val: Integer;
begin
  fsScript1.Parent := fsGlobalUnit;
  fsScript1.Lines.Add('function Plus(A, B: Integer): Integer;');
  fsScript1.Lines.Add('begin');
  fsScript1.Lines.Add('  Result := A + B;');
  fsScript1.Lines.Add('end;');
  fsScript1.Lines.Add('begin;');
  fsScript1.Lines.Add('end.');

  if not fsScript1.Compile then
  begin
     //文法に間違いがあるとき
    ShowMessage(Format('%s:%s', [fsScript1.ErrorPos, fsScript1.ErrorMsg]));
    Exit;
  end;

  Val := fsScript1.CallFunction('Plus', VarArrayOf([1, 2]));
  ShowMessage(IntToStr(Val));
end;

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください