From a12ca1c1a0ec61d542f8ab1801dfc3f75df3d428 Mon Sep 17 00:00:00 2001 From: Mattias Gaertner Date: Mon, 11 Feb 2019 09:29:16 +0000 Subject: [PATCH] pastojs: type helper set git-svn-id: trunk@41295 - --- packages/pastojs/src/fppas2js.pp | 41 +++++++++++++++++++++++++--- packages/pastojs/tests/tcmodules.pas | 36 ++++++++++++++++++++---- 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/packages/pastojs/src/fppas2js.pp b/packages/pastojs/src/fppas2js.pp index a7e2315773..157d9c83c9 100644 --- a/packages/pastojs/src/fppas2js.pp +++ b/packages/pastojs/src/fppas2js.pp @@ -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; diff --git a/packages/pastojs/tests/tcmodules.pas b/packages/pastojs/tests/tcmodules.pas index 211ac08363..f9928be268 100644 --- a/packages/pastojs/tests/tcmodules.pas +++ b/packages/pastojs/tests/tcmodules.pas @@ -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;