Skip to content

Instantly share code, notes, and snippets.

@devaex
Created March 23, 2021 16:39
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 devaex/0ee6ef34bfc1659dc33fa0aacab0a562 to your computer and use it in GitHub Desktop.
Save devaex/0ee6ef34bfc1659dc33fa0aacab0a562 to your computer and use it in GitHub Desktop.
mORMot2 QuickJS sample
function Log(ctx: JSContext; this_val: JSValueConst; argc: Integer; argv: PJSValueConstArr): JSValue; cdecl;
var
i : Integer;
str : PChar;
bStr : String;
begin
bStr := '';
for i := 0 to Pred(argc) do
begin
if i <> 0 then
bStr := ' ';
str := JS_ToCString(ctx, argv[i]);
if not Assigned(str) then
bStr := 'Invalid args';
bStr := bStr + str;
JS_FreeCString(ctx, str);
end;
Form1.Memo1.Lines.Add(bStr);
Result := JSValue(JS_UNDEFINED);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ctx : JSContext;
rt: JSRuntime;
global : JSValueRaw;
begin
rt := JS_NewRuntime;
try
ctx := rt.New;
try
global := JS_GetGlobalObject(ctx);
JS_SetPropertyStr(ctx,global,'log',JS_NewCFunction(ctx, @log, 'log', 1));
ctx.Free(global);
Memo1.Lines.Add(ctx.EvalGlobal(SynEdit1.Lines.Text, 'code'));
finally
ctx.Done;
end;
finally
rt.Done;
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment