Created
March 23, 2021 16:39
-
-
Save devaex/0ee6ef34bfc1659dc33fa0aacab0a562 to your computer and use it in GitHub Desktop.
mORMot2 QuickJS sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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