RTTI Script basic examples

After a few requests for examples and an assertion in the comments that noting actually compiles or runs, I’ve updated the repository a little.

I’ve fixed a few bugs in the parser, and updated the executor to support the Environment object.  I added an Examples folder with two simple Hello World projects.  They both display a message box with a simple greeting.  The message box, of course, is not built in to the scripting system, so it has to be imported from the Delphi project.  These two sample programs demonstrate the two ways to do this: through external unit registration and with an Environment object.

The rsCompiler unit provides a class called TrsTypeImporter that’s used to import new types, variables, constants and routines (hmm… maybe I should change the name to just TrsImporter?) to make the compiler aware of them.  To register a new external unit, call RegisterStandardUnit on the compiler.  It takes two parameters: a unit name and an anonymous reference to a procedure that takes a TrsTypeImporter as an argument.  The first time a unit with that name is used in the script, the compiler will call the method reference, providing an importer to register things with the compiler.  A unit named “system” is always used, so if you want to import things that will always be available to the script, call RegisterStandardUnit and use “system” as the unit name.

Any units registered with the compiler also have to be registered with the script executor by a similar process.  The interface for the executor’s RegisterStandardUnit system is not stable yet, but it currently allows you to register your standalone routines in the callback.  I’ll add support for registering classes and external variables soon.

The second, simpler way to do imports is through the Environment object.  The second sample program shows how this works.  You declare a class and register it with the compiler, and every public method and property on it will be available to the script engine as if they were global routines and variables declared in the “system” import.  Then you register an instance of the class with the script executor before loading the program.  (It’s important to note that the Environment has to be registered with the compiler before compiling, and with the script executor before loading the program.)

It’s possible to use both systems together, of course.  If you want to break up your external routines into groups, you could declare and register multiple external units, but still have a group of globally-available methods in an Environment object.

Hopefully these examples will show people how to get started.  I’ll add some more examples as needed and when I get some more features ready.

3 Comments

  1. Brian Frost says:

    Mason, How does RTTIScript differ from, say PascalScript or Eric’s DWScript? Is there any actual documentation? I see the two simple examples but it’s not clear how far the code goes (to me at least!).

    • Well, so far it’s still an alpha-level system, still under development with some major features missing. But the two major differences between RTTIScript and the other engines you mentioned are:

      * It’s built upon Delphi’s extended RTTI system and able to use its features to simplify a lot of the work involved in making your scripts interface with the rest of your program.
      * It’s able to utilize class creation technology to allow you to define your own classes within scripts, including by descending from existing native classes, and pass them out into native code seamlessly.

  2. Dave B says:

    Was trying to test under XE2. It seems RTTI has changed.
    Cannot build examples (there is no PivateHeap.pas file any more).

Leave a Reply to Dave B