Delphi DLL
Delphi DLL
Call a JavaScript Function Returning an Integer
See more JavaScript Examples
Demonstrates how to call a JavaScript function that returns an integer.Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Js, StringBuilder, JsonObject;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
sbScript: HCkStringBuilder;
js: HCkJs;
result: HCkJsonObject;
funcCall: HCkJsonObject;
retval: Integer;
begin
success := False;
// ----------------------------------------------------------------------------------
// The Javascript function called in this example is shown at the bottom of this page.
// -----------------------------------------------------------------------------------
// In this example, we'll load the Javascript function definition from a file.
// It doesn't need to come from a file. It could just as easily be loaded from a string.
sbScript := CkStringBuilder_Create();
success := CkStringBuilder_LoadFile(sbScript,'js_call_function.js','utf-8');
if (success = False) then
begin
Memo1.Lines.Add(CkStringBuilder__lastErrorText(sbScript));
Exit;
end;
// Note: Each instance of a Chilkat Js object automatically establishes
// its own internal runtime and context. Applications do not need to explicitly create
// the JavaScript runtime or context.
js := CkJs_Create();
result := CkJsonObject_Create();
CkJsonObject_putEmitCompact(result,False);
// Call Eval to add the function (shown at the bottom of this page) to the context's global object
success := CkJs_Eval(js,sbScript,result);
if (success = False) then
begin
// Examine the result for an exception.
Memo1.Lines.Add(CkJsonObject__emit(result));
// Also examine the LastErrorText.
Memo1.Lines.Add(CkJs__lastErrorText(js));
Exit;
end;
Memo1.Lines.Add(CkJsonObject__emit(result));
// The expected output is "undefined":
// {
// "type": "undefined",
// "value": "undefined"
// }
// When Eval processes a script containing only a function declaration,
// it successfully performs the action (the function becomes defined).
// However, since the script consists of a statement that produces no value,
// the script's overall completion value is empty. In JavaScript, the
// absence of a value is represented by `undefined`.
//
// Therefore, the Eval call returns `undefined`.
// ------------------------------------------------------------------------------
// Call the function calculateScore("Player1", 10, 20)
funcCall := CkJsonObject_Create();
// Create JSON defining the function call:
// {
// "name": "calculateScore",
// "args": [ "Player1", 10, 20 ]
// }
CkJsonObject_UpdateString(funcCall,'name','calculateScore');
CkJsonObject_UpdateString(funcCall,'args[0]','Player1');
CkJsonObject_UpdateInt(funcCall,'args[1]',10);
CkJsonObject_UpdateInt(funcCall,'args[2]',20);
CkJsonObject_putEmitCompact(funcCall,False);
Memo1.Lines.Add(CkJsonObject__emit(funcCall));
success := CkJs_CallFunction(js,funcCall,result);
if (success = False) then
begin
// Examine the result for an exception.
Memo1.Lines.Add(CkJsonObject__emit(result));
// Also examine the LastErrorText.
Memo1.Lines.Add(CkJs__lastErrorText(js));
Exit;
end;
Memo1.Lines.Add(CkJsonObject__emit(result));
// Output:
// {
// "type": "int",
// "value": 37
// }
retval := CkJsonObject_IntOf(result,'value');
Memo1.Lines.Add('retval = ' + IntToStr(retval));
CkStringBuilder_Dispose(sbScript);
CkJs_Dispose(js);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(funcCall);
end;