Browse Source

pastojs: property default value

git-svn-id: trunk@37314 -
Mattias Gaertner 7 years ago
parent
commit
5eb078cccd
2 changed files with 133 additions and 6 deletions
  1. 13 6
      packages/pastojs/src/fppas2js.pp
  2. 120 0
      packages/pastojs/tests/tcmodules.pas

+ 13 - 6
packages/pastojs/src/fppas2js.pp

@@ -557,7 +557,7 @@ const
     'reftype',
     'flags',
     'procsig',
-    'defaultvalue',
+    'Default',
     'stored',
     'comptype',
     'Self',
@@ -9707,7 +9707,7 @@ var
   VarType: TPasType;
   StoredExpr: TPasExpr;
   StoredResolved: TPasResolverResult;
-  StoredValue: TResEvalValue;
+  StoredValue, Value: TResEvalValue;
 begin
   Result:=nil;
   OptionsEl:=nil;
@@ -9791,8 +9791,17 @@ begin
         CreateLiteralString(Prop,GetAccessorName(DeclEl)));
       end;
 
-    // add option defaultvalue
-    // ToDo
+    // add option "defaultvalue"
+    if Prop.DefaultExpr<>nil then
+      begin
+      Value:=AContext.Resolver.Eval(Prop.DefaultExpr,[refConst],false);
+      try
+        AddOption(FBuiltInNames[pbivnRTTIPropDefault],
+          ConvertConstValue(Value,AContext,Prop));
+      finally
+        ReleaseEvalValue(Value);
+      end;
+      end;
 
     // add option Index
     // ToDo
@@ -9909,8 +9918,6 @@ begin
     RaiseNotSupported(El.ImplementsFunc,AContext,20170215102923,'property implements function');
   if El.DispIDExpr<>nil then
     RaiseNotSupported(El.DispIDExpr,AContext,20170215103029,'property dispid expression');
-  if El.DefaultExpr<>nil then
-    RaiseNotSupported(El.DefaultExpr,AContext,20170215103129,'property default modifier');
   // does not need any declaration. Access is redirected to getter/setter.
 end;
 

+ 120 - 0
packages/pastojs/tests/tcmodules.pas

@@ -503,6 +503,7 @@ type
     Procedure TestRTTI_PublishedClassFieldFail;
     Procedure TestRTTI_PublishedFieldExternalFail;
     Procedure TestRTTI_StoredModifier;
+    Procedure TestRTTI_DefaultValue;
     Procedure TestRTTI_Class_Field;
     Procedure TestRTTI_Class_Method;
     Procedure TestRTTI_Class_MethodArgFlags;
@@ -13374,6 +13375,125 @@ begin
     '']));
 end;
 
+procedure TTestModule.TestRTTI_DefaultValue;
+begin
+  StartProgram(false);
+  Add([
+  'const',
+  '  CB = true or false;',
+  '  CI = 1+2;',
+  'type',
+  '  TEnum = (red, blue);',
+  '  TObject = class',
+  '    FB: boolean;',
+  '    FI: longint;',
+  '    FE: TEnum;',
+  '  published',
+  '    property B1: boolean read FB default true;',
+  '    property B2: boolean read FB default CB;',
+  '    property B3: boolean read FB default test1.cb;',
+  '    property I1: longint read FI default 2;',
+  '    property I2: longint read FI default CI;',
+  '    property E1: TEnum read FE default red;',
+  '    property E2: TEnum read FE default TEnum.blue;',
+  '  end;',
+  'begin']);
+  ConvertProgram;
+  CheckSource('TestRTTI_DefaultValue',
+    LinesToStr([ // statements
+    'this.CB = true || false;',
+    'this.CI = 1 + 2;',
+    'this.TEnum = {',
+    '  "0": "red",',
+    '  red: 0,',
+    '  "1": "blue",',
+    '  blue: 1',
+    '};',
+    'rtl.createClass($mod, "TObject", null, function () {',
+    '  this.$init = function () {',
+    '    this.FB = false;',
+    '    this.FI = 0;',
+    '    this.FE = 0;',
+    '  };',
+    '  this.$final = function () {',
+    '  };',
+    '  var $r = this.$rtti;',
+    '  $r.addProperty(',
+    '    "B1",',
+    '    0,',
+    '    rtl.boolean,',
+    '    "FB",',
+    '    "",',
+    '    {',
+    '      Default: true',
+    '    }',
+    '  );',
+    '  $r.addProperty(',
+    '    "B2",',
+    '    0,',
+    '    rtl.boolean,',
+    '    "FB",',
+    '    "",',
+    '    {',
+    '      Default: true',
+    '    }',
+    '  );',
+    '  $r.addProperty(',
+    '    "B3",',
+    '    0,',
+    '    rtl.boolean,',
+    '    "FB",',
+    '    "",',
+    '    {',
+    '      Default: true',
+    '    }',
+    '  );',
+    '  $r.addProperty(',
+    '    "I1",',
+    '    0,',
+    '    rtl.longint,',
+    '    "FI",',
+    '    "",',
+    '    {',
+    '      Default: 2',
+    '    }',
+    '  );',
+    '  $r.addProperty(',
+    '    "I2",',
+    '    0,',
+    '    rtl.longint,',
+    '    "FI",',
+    '    "",',
+    '    {',
+    '      Default: 3',
+    '    }',
+    '  );',
+    '  $r.addProperty(',
+    '    "E1",',
+    '    0,',
+    '    $mod.$rtti["TEnum"],',
+    '    "FE",',
+    '    "",',
+    '    {',
+    '      Default: $mod.TEnum.red',
+    '    }',
+    '  );',
+    '  $r.addProperty(',
+    '    "E2",',
+    '    0,',
+    '    $mod.$rtti["TEnum"],',
+    '    "FE",',
+    '    "",',
+    '    {',
+    '      Default: $mod.TEnum.blue',
+    '    }',
+    '  );',
+    '});',
+    '']),
+    LinesToStr([ // $mod.$main
+    '']));
+end;
+
 procedure TTestModule.TestRTTI_Class_Field;
 begin
   Converter.Options:=Converter.Options-[coNoTypeInfo];