Unable to access RTTI for SmartMobileStudio

167 Views Asked by At

Trying to use Smart's RTTI as defined in this post, I was not able to let RTTI be emitted to the HTML.

When I define:

type
   TBase = class
      published
         Field : Integer = 1;
   end;

 var base := new TBase;
 PrintPropertiesForType(base);

Then, there is no RTTI available for this class.

In index.html, I have:

var $RTTI = [];

Meaning that there is no RTTI emitted.

This occurs for whatever Project Options I have set. (in short, the RTTI compilation option does not make any difference)

I’m using SMS 2.0.1.741.

I’m stuck with implementing a native SMS client for mORMot..

3

There are 3 best solutions below

1
Jon Lennart Aasenden On BEST ANSWER

There seem to be a bug with this issue in the latest hotfix. I have reported the issue to the developer team and it should be fixed shortly. I noticed the exact same thing myself testing the RTTI methods on different versions of SMS.

An immediate solution is to just roll-back to version 2.0.0.723.

You can download that version here: http://smartmobilestudio.com/download/v2_0_0_723/

1
André On

I found out that it works in the main unit (TApplication) but not in other referenced units. So you can try that in the mean time

2
Jon Lennart Aasenden On

As an alternative to automatic persistence (since RTTI is not working in the latest hotfix and you have opted not to use RTTI for object mapping), it should be mentioned that Smart Pascal supports JavaScript structures. This will greatly simplify object storage. If you have ever used a Microsoft property-bag (com class) then this is more or less the same thing.

In JavaScript you would write:

var mbag = {};
mbag["test"] = {};
mbag["test"]["somevalue"] = 12;
mBag["text"] = "this is a string value";

Resulting in a memory structure like this:

Type
TMyBagContent = Record
  bcTest = Record
             bcSomeValue:Integer;
           end;
  bcText:String;
End;

In Delphi these kind of structures are implemented in SuperObject (I believe). RemObjects also have some support classes that mimic this. But thankfully Smart Pascal achieves this automatically by grace of targeting JavaScript directly.

So in Smart Pascal you would do the following to achieve the same results:

var mBag:Variant;
mBag:=TVariant.CreateObject;
mBag['test'] := TVariant.CreateObject;
mBag['test']['somevalue']:=12;
mBag['text']:='this is a string';

You can also save some time and use an assembly section:

var mBag:variant;
asm
  @mbag = {};
  @mbag["test"] = {};
  @mbag["test"]["somevalue"] = 12;
  @mbag["text"] = "this is a string value";
end;

Once you have populated your object's data, you then use JSON to serialize and make it portable:

var mObj: String;
asm
  @mObj = JSON.stringify(@mbag);
end;

Creating reader/writer class for storage is very easy. Naturally automatic RTTI property mapping is better (and that should be fixed shortly), but for frameworks that does it manually - which does have some advantages over full automation - there are plenty of options for creative programmers such as yourself.

There is also speed to consider, and using a JavaScript object as a lookup table is very, very fast compared to a for/next recursive algorithm. Especially for large and complex data structures.

Looking forward to testing your creation :)