pastojs: type helper set

git-svn-id: trunk@41295 -
This commit is contained in:
Mattias Gaertner 2019-02-11 09:29:16 +00:00
parent 1b6c69e079
commit a12ca1c1a0
2 changed files with 68 additions and 9 deletions

View File

@ -388,12 +388,16 @@ Works:
- record helpers:
- in function allow assign Self
- type helpers:
- var, const, read only const
- arg default, arg const, arg var, arg out
- result element
- pass var, const, read only const
- pass arg default, arg const, arg var, arg out
- pass result element
- pass function result
- pass field, class field
- pass property getter field, property getter function,
- pass class property, static class property
- pass array property
ToDos:
- class helpers, type helpers, record helpers
- cmd line param to set modeswitch
- Result:=inherited;
- asm-block annotate/reference
@ -12265,9 +12269,38 @@ Var
Procedure AddReturnThis;
var
RetSt: TJSReturnStatement;
HelperForType: TPasType;
Call: TJSCallExpression;
Proc: TPasProcedure;
aResolver: TPas2JSResolver;
ClassOrRec: TPasMembersType;
begin
// "return this"
RetSt:=TJSReturnStatement(CreateElement(TJSReturnStatement,El));
RetSt.Expr:=TJSPrimaryExpressionThis(CreateElement(TJSPrimaryExpressionThis,El));
aResolver:=AContext.Resolver;
if aResolver<>nil then
begin
Proc:=TPasProcedure(El.Parent);
ProcScope:=Proc.CustomData as TPas2JSProcedureScope;
ClassOrRec:=ProcScope.ClassRecScope.Element as TPasMembersType;
if (ClassOrRec.ClassType=TPasClassType)
and (TPasClassType(ClassOrRec).HelperForType<>nil) then
begin
HelperForType:=AContext.Resolver.ResolveAliasType(TPasClassType(ClassOrRec).HelperForType);
if HelperForType is TPasMembersType then
// helper constructor for class or record -> "this" is the class/record
else
begin
// helper constructor for a simpletype -> "this" is a reference
// -> return this.get()
Call:=CreateCallExpression(El);
Call.Expr:=CreateDotExpression(El,RetSt.Expr,
CreatePrimitiveDotExpr(TempRefObjGetterName,El));
RetSt.Expr:=Call;
end;
end;
end;
Add(RetSt,El);
end;

View File

@ -672,7 +672,7 @@ type
Procedure TestTypeHelper_StringChar;
Procedure TestTypeHelper_Array;
Procedure TestTypeHelper_EnumType;
Procedure TestTypeHelper_SetType; // ToDo
Procedure TestTypeHelper_SetType;
// proc types
Procedure TestProcType;
@ -22694,6 +22694,7 @@ begin
' THelper = type helper for TSetOfEnum',
' procedure DoIt(e: byte = 123);',
' constructor Init(e: TEnum);',
' constructor InitEmpty;',
' end;',
'procedure THelper.DoIt(e: byte);',
'begin',
@ -22707,14 +22708,17 @@ begin
' Self:=[e];',
' Include(Self,blue);',
'end;',
'constructor THelper.InitEmpty;',
'begin',
'end;',
'var s: TSetOfEnum;',
'begin',
//' s.DoIt;',
' s.DoIt;',
//' [red].DoIt;',
//' with s do DoIt;',
//' with [red,blue] do DoIt;',
//' s:=TSetOfEnum.Init(blue);',
//' s:=s.Init(blue);',
' s:=TSetOfEnum.Init(blue);',
' s:=s.Init(blue);',
'']);
ConvertProgram;
CheckSource('TestTypeHelper_SetType',
@ -22735,7 +22739,10 @@ begin
' this.set({});',
' this.set(rtl.createSet(e));',
' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
' return this;',
' return this.get();',
' };',
' this.InitEmpty = function () {',
' return this.get();',
' };',
' this.$new = function (fn, args) {',
' return this[fn].call({',
@ -22752,6 +22759,25 @@ begin
'this.s = {};',
'']),
LinesToStr([ // $mod.$main
'$mod.THelper.DoIt.apply({',
' p: $mod,',
' get: function () {',
' return this.p.s;',
' },',
' set: function (v) {',
' this.p.s = v;',
' }',
'}, 123);',
'$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
'$mod.s = rtl.refSet($mod.THelper.Init.apply({',
' p: $mod,',
' get: function () {',
' return this.p.s;',
' },',
' set: function (v) {',
' this.p.s = v;',
' }',
'}, $mod.TEnum.blue));',
'']));
end;