0

The tdwsUnit declares a property of type TObject, as well as a getter and a setter. An instantiated object shall be stored in an "object store" on the Delphi side. The goal is to transport the object from script A to script B, where script A, when run, creates and stores the object on the Delphi side and script B, when run (same Delphi process) shall read the object from the Delphi side and use it.

Var ObjectStore : TObject;

procedure TDWSLibTools.TApp_GetObject(Info: TProgramInfo; ExtObject: TObject);
begin
 // How to return object in ObjectStore?
 <ResultOfThisGetter> := ObjectStore;
end;

procedure TDWSLibTools.TApp_SetObject(Info: TProgramInfo; ExtObject: TObject);
begin
 // How to access the object in parameter named "Value"?
 ObjectStore := Info.Vars['Value'].Get?????
end;

And: Even when done correctly, will the object "survive" between the script runs?

1
  • 2
    As far as I know any objects created within one script are only available from within that script only. I might be mistaken on this. However it is possible to call external methods from a script meaning that in theory you could create methods fro creating and destroying that object in Delphi code and then execute those method from the script. I don't have enough experience working with DWScript in order to write a full answer. So all I can do is recommend you read DWScript documentation on accessing external methods and objects. Sep 18 at 20:10

1 Answer 1

0

You can expose any Delphi object instance to any script, so there shouldn't be a problem with creating a Delphi object in one script and accessing that object from another script.

It sounds like your object is initially being created from the script. I'm assuming that this object is declared on the Delphi side and the class type then exposed to the script somehow - e.g. via a TdwsUnit component.

For example on the Delphi side you have a TDelphiClass declared and you have a corresponding script class called TScriptClass. I'm further assuming that you have declared a TApp script class (e.g. in a TdwsUnit) with an Object property so the script side API looks something like this:

type
  TApp = class
  protected
    function GetObject: TScriptClass;
    procedure SetObject(Value: TScriptClass);
  public
    property Object: TScriptClass read GetObject write SetObject;
  end;

Now, as far as I can tell you are just asking how to implement the GetObject and SetObject TdwsUnit event handlers on the Delphi side. In the following example I assume that your ObjectStore variable if of the type TDelphiClass (to make it more obvious where I reference it):

var
  ObjectStore: TDelphiClass;


// TScriptClass.Create
procedure TDataModuleScript.dwsUnitStuffClassesTScriptClassConstructorsCreateEval(
  Info: TProgramInfo; var ExtObject: TObject);
begin
  if (ExtObject = nil) then
    ExtObject := TDelphiClass.Create;
end;

// TApp.GetObject: TScriptObject
procedure TDataModuleScript.dwsUnitStuffClassesTAppMethodsGetObjectEval(
  Info: TProgramInfo; ExtObject: TObject);
var
  ClassSym: TClassSymbol;
  ScriptObj: IScriptObj;
begin
  // Get the script side class type
  ClassSym := TClassSymbol(Info.FindSymbolInUnits('TScriptClass'));

  if (ClassSym <> nil) then
  begin
    // We're not using the TApp Delphi side object here, but if 
    // there is one then ExtObject would point to it.

    // Create a script side object to represent the Delphi side object
    ScriptObj := TScriptObjInstance.Create(ClassSym, Info.Execution);

    // Store a reference to the Delphi side object inside the 
    // script side object
    ScriptObj.ExternalObject := ObjectStore;

    // Return the script side object
    Info.ResultAsVariant := ScriptObj;
  end;
end;

// TApp.SetObject(Value: TScriptObject)
procedure TDataModuleScript.dwsUnitStuffClassesTAppMethodsSetObjectEval(
  Info: TProgramInfo; ExtObject: TObject);
var
  ParamInfo: IInfo;
  ExtObj: TDelphiClass;
begin
  // Get the parameter value
  ParamInfo := Info.Vars['Value'];
  if (ParamInfo.ValueIsEmpty) then
    // parameter is nil
    ExtObj := nil 
  else
    // Parameter is object.
    // Get the Delphi side reference and case to correct 
    // type (with type check)
    ExtObj := ParamInfo.ExternalObject as TDelphiClass;

  // Stash the reference to the Delphi side object
  ObjectStore := TDelphiClass(ExtObj);
end;
4
  • Thank you @SpeedFreak for your comprehensive response. This already gives me some inside view on the transport mechanism. There is just 1 twist: The Delphi side does not know anything about the class type of the data to be stored in the object store, hence I declared it as generic TObject in my question. The business logic behind this is that script A produces some arbitrary data of which Delphi knows nothing about, except that it derives from TObject, and then script B, knowing the class type, shall process the stored data. Delphi being just a "persistence layer" between the scripts.
    – LuWey
    Sep 21 at 7:31
  • @LuWey That makes sense. Remember to accept the answer if it satisfies your question. Sep 21 at 11:27
  • As written in my previous comment, unfortunately, it does not satisfy my question yet. I still do not see how I can deal with the situation that the type class of the object is unknown to the Delphi side (except for deriving from TObject). Second part of my question was if the object instance, created in script A, survives the end of life of script A or instead leaves an invalid dangling pointer on the Delphi side.
    – LuWey
    Sep 21 at 12:21
  • 1) You can determine the object type like you would for any other Delphi object. E.g. use the is operator or the ClassType property. Or did you mean the script side object type? 2) The Delphi side object instance, unless you explicitly destroy it somehow (e.g. with the OnCleanUp TdwsUnit event handler), will not be affected by the destruction of the script side object or the termination of the script from which it was created. Sep 21 at 19:52

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.