Skip to content

Instantly share code, notes, and snippets.

@tondrej
Last active December 29, 2020 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tondrej/8d7c66ad2599b345c621563e2857cd52 to your computer and use it in GitHub Desktop.
Save tondrej/8d7c66ad2599b345c621563e2857cd52 to your computer and use it in GitHub Desktop.
chakracore-delphi Variant example
// Calling javascript methods on variants creates temporary variant arrays for passing parameters.
// To avoid access violations after freeing the runtime, make sure all your code using Variants gets out of scope first.
procedure Test(Context: TChakraCoreContext);
var
Global, Json, MyObj: Variant;
begin
// assign javascript Global to a Variant
Global := JsValueToVariant(Context.Global);
// read its (JSON) property and assign it to another Variant
Json := Global.JSON;
// call its method, assign result to another Variant
MyObj := Json.parse('{ "name": "world" }');
// access property
Writeln(Format('Hello, %s!', [MyObj.name]));
// call another method
Writeln(Json.stringify(MyObj));
end;
procedure Main;
var
Runtime: TChakraCoreRuntime;
Context: TChakraCoreContext;
begin
Runtime := nil;
Context := nil;
try
Runtime := TChakraCoreRuntime.Create;
Context := TChakraCoreContext.Create(Runtime);
Context.Activate;
Test(Context);
finally
Context.Free;
Runtime.Free;
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment