[fpc-devel] What's the status of Maciej's Smart Pointer enhancements?

Anthony Walter sysrpl at gmail.com
Mon Apr 30 13:15:19 CEST 2018

Okay great. Thanks for the information. I'll experiment with the smart
pointers and their potential performance impact. In the interim I've
created a solution using the approach of records containing private
reference counted interfaces. Here is an example of my JSContext record:

   1.   JSContext = record
   2.   private
   3.     FRef: IJSContext;
   4.   public
   5.     class function Create: JSContext; static;
   6.     class function Retain(C: JSContextRef): JSContext; static;
   7.     class operator Implicit(C: JSContext): JSContextRef;
   8.     function EvaluateScript(const Script: string): JSValue;
   9.     function CreateFunction(const Name: string; Callback: JSFunction):
   10.     function CreateMethod(const Name: string; Callback: JSMethod):
   11.     function CreateUndefined: JSValue;
   12.     function CreateNull: JSValue;
   13.     function CreateBoolean(Value: Boolean): JSValue;
   14.     function CreateNumber(Value: Double): JSValue;
   15.     function CreateString(const Value: string): JSValue;
   16.     function CreateJSON(const Value: string): JSValue;
   17.   end;

So you could use it to embed a javascript engine in your free pascal
application like so:

   1. function TestCallback(Context: JSContext; Args: JSValues): JSValue;
   2. begin
   3.   Result := JSContext.CreateString('You passed ' + IntToStr(Args.
   Length) + ' arguments');
   4. end;
   6. function MessageBoxCallback(Context: JSContext; Args: JSValues):
   7. begin
   8.   if Args.Length > 0 then
   9.     ShowMessage(Args[0].AsString);
   10.   Result := JSContext.CreateUndefined;
   11. end;
   13. procedure TForm1.Button1Click(Sender: TObject);
   14. var
   15.   C: JSContext;
   16.   R: JSValue;
   17. begin
   18.   C := JSContext.Create;
   19.   C.CreateFunction('test', TestCallback);
   20.   C.CreateFunction('messageBox', MessageBoxCallback);
   21.   R := C.ExecuteScript(
   22.     'let s = test(123, "hello");' +
   23.     'messageBox("test gave us:" + s);' +
   24.     'let result = "okay, we are done.";');
   25.   Caption := R.AsString;
   26. end;

Note, using my records with interfaces we don't have to clean up anything,
and the javascript context is destroyed when Button1Click exits. The only
problem I have right now is you cannot forward declare records, so with
pascal it's not possible for two records to use each other in their type
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180430/d6b022f4/attachment.html>

More information about the fpc-devel mailing list