1

I have a DWScript like this

var Outputter: TOutputter;

procedure OutputterTester;
begin
  Outputter.Print;
end;

TOutputter (is only a example for a complex class) is declared and created in delphi code and exposed to the DWScript via Rtti.

TOutputter = class
  procedure Print;
end;

I want to use the compiled script for several instances, but change the value Outputter that it link to current instance. I know i can access a script variable with:

var Exec : IdwsProgramExecution;
...
AVar := Exec.Info.Vars['Outputter'].Value;

But the value is a Variant so i can't assigne a object. How can I change the value? If i first create the class in script like:

procedure Init;
   Outputter := TOutputter.Create;
end;

I can assign Exec.Info.Vars['Outputter'].ScriptObj.ExtObject a arbitrary instance of TOutputter (created in delphi code) and access them in scriptcode over Outputter. But i want to assign a delphi code created instance of TOutputter without the init part.

Thank you for help!

  • What is TOutputter? a class? a record? an alias to a type? Also who is responsible for creating the script-side object, and who is responsible for maintaining the Delphi-side object behind that script-side object? (by "who" I mean the script? the Delphi code? something else?) – Eric Grange Apr 03 '12 at 07:43
  • I have adjust my question to answer inexplicit facts. Thank you for help! – DragonFlyOfGold Apr 03 '12 at 09:36

1 Answers1

0

If I understood correctly, you want to skip the Init procedure, but if that means you have to make Outputter either an external variable or a magic name or you won't be able to recognize which variable it is.

One approach for the above could be to just prepend your boiler plate code to the user script:

var Outputter := TOutputter.Create;

Another option would be to create an external variable in a TdwsUnit, you'll then be responsible for creating the script-side object from the Delphi side when the user accesses that external variable, and can handle what happens if the user assign something to the variable as well.

But if Outputter is meant to be read-only by the user, you could just declare an Outputter() function in a TdwsUnit (and create and return the script object there).

Last option would be to use an RTTI Environment, depending on what you want to do with it, that could be the simplest option, as you can change the instance in the environment directly (one of the unit tests for RTTI environment does that).

Eric Grange
  • 5,931
  • 1
  • 39
  • 61