[Question] List property names of a DWSCript Class

Issue #245 resolved
Toky Olivier Razanakotonarivo created an issue

Hello,

In JS, one can list all property names of an object (for example: https://www.w3docs.com/snippets/javascript/how-to-list-the-properties-of-a-javascript-object.html) but in DWScript, is there any method to list all properties? I’m working mostly with the JSCodegen. When the output js code is not optimized, may be I can list procedure/function but not property names, but when the code is optimized for size, I cannot find the procudure/function names, property names of the Class.

Thank you,

Comments (6)

  1. Eric Grange repo owner

    Hi,

    When optimizing for size, what’s not marked as “published” can be renamed to a shorter name or eliminated completely by the linker. Is that what you are referring to ?

  2. Toky Olivier Razanakotonarivo reporter

    Hi Eric,

    I would like to be able do this in DWS (in JSCodeGen or DWS script), I don’t know if it’s related to RTTI as I never used it :

    type
      TMyObject = class
      private
        FProp1, FProp2: String;
      published
        property Prop1: String read (FProp1);
        property Prop2: String read (FProp2) write (FProp2);
      end;
    
      begin
        var obj: TMyObject := TMyObject.Create();
        obj.Prop1 := 'Test1';
        obj.Prop2 := 'Test2';
    
        write(obj.PublishedProperties.Count); //Prints 2 (or more if it's inherited from another classes)
        write(obj.PublishedProperties['Prop1'].Value); //Prints Test1
        write(obj.PublishedProperties['Prop2'].Value); //Prints Test2
        write(obj.PublishedProperties['Prop1'].ReadOnly); //Prints True
        write(obj.PublishedProperties.IndexOf('Prop3')); //Prints False
        //etc...
    
        obj.Destroy();
      end.
    

    I know, every one may not need this but as in JS we can use them, I think it’s better to have that in DWS. I see that classes in DWS are not exactly like JS classes so I cannot do this even with asm blocks:

    asm
      var keys=Object.keys(@obj); //I won't have Prop1 and Prop2 in keys
    end;
    

    I have one need of it and that’s why I ask the question.

    Thank you.

  3. Eric Grange repo owner

    Yes, this is covered by RTTI, which needs to be enabled in the compile option, and you need to disable optimization for size.

    There are code examples in the FunctionsRTTI test folder. There is no high-level API for the RTTI right now, it’s a big array with all the RTTI data

  4. Toky Olivier Razanakotonarivo reporter

    Thank you Eric. It’s really what I need. But I tested with the Live Scripting Demo, none of the code examples can be compiled. Can you check please?

  5. Log in to comment