@@ -1438,18 +1438,29 @@ const (
14381438` )
14391439}
14401440
1441+ type fldAdder struct {
1442+ Fields []* types.Var
1443+ pkgTypes * types.Package
1444+ }
1445+
1446+ func (p * fldAdder ) addFld (idx int , name string , typ types.Type , embed bool ) {
1447+ fld := types .NewField (token .NoPos , p .pkgTypes , name , typ , embed )
1448+ p .Fields = append (p .Fields , fld )
1449+ }
1450+
14411451func TestClassDefsInitWithoutType (t * testing.T ) {
14421452 pkg := newMainPackage ()
1453+ fa := fldAdder {pkgTypes : pkg .Types }
14431454 typ := pkg .NewTypeDefs ().NewType ("Rect" )
14441455 recv := types .NewParam (token .NoPos , pkg .Types , "this" , types .NewPointer (typ .Type ()))
1445- defs := pkg .ClassDefsStart (recv , nil , nil )
1456+ defs := pkg .ClassDefsStart (recv , fa . addFld )
14461457 defs .NewAndInit (func (cb * gogen.CodeBuilder ) int {
14471458 cb .Val (1 ).Val (2 ).BinaryOp (token .ADD ).
14481459 Val ("1" ).Val ("2" ).BinaryOp (token .ADD )
14491460 return 2
1450- }, "" , token .NoPos , nil , "n" , "s" )
1461+ }, token .NoPos , nil , "n" , "s" )
14511462 defs .End ()
1452- typ .InitType (pkg , types .NewStruct (defs .Fields , defs . Tags ))
1463+ typ .InitType (pkg , types .NewStruct (fa .Fields , nil ))
14531464 domTest (t , pkg , `package main
14541465
14551466type Rect struct {
@@ -1466,16 +1477,17 @@ func (this *Rect) XGo_Init() *Rect {
14661477
14671478func TestClassDefsInitWithType (t * testing.T ) {
14681479 pkg := newMainPackage ()
1480+ fa := fldAdder {pkgTypes : pkg .Types }
14691481 typ := pkg .NewTypeDefs ().NewType ("Rect" )
14701482 recv := types .NewParam (token .NoPos , pkg .Types , "this" , types .NewPointer (typ .Type ()))
1471- defs := pkg .ClassDefsStart (recv , nil , nil )
1483+ defs := pkg .ClassDefsStart (recv , fa . addFld )
14721484 defs .NewAndInit (func (cb * gogen.CodeBuilder ) int {
14731485 cb .Val (1 ).Val (2 ).BinaryOp (token .ADD ).
14741486 Val (5 )
14751487 return 2
1476- }, "" , token .NoPos , types .Typ [types .Int ], "a" , "b" )
1488+ }, token .NoPos , types .Typ [types .Int ], "a" , "b" )
14771489 defs .End ()
1478- typ .InitType (pkg , types .NewStruct (defs .Fields , defs . Tags ))
1490+ typ .InitType (pkg , types .NewStruct (fa .Fields , nil ))
14791491 domTest (t , pkg , `package main
14801492
14811493type Rect struct {
@@ -1497,23 +1509,25 @@ func TestClassDefsInitPanic(t *testing.T) {
14971509 }
14981510 }()
14991511 pkg := newMainPackage ()
1512+ fa := fldAdder {pkgTypes : pkg .Types }
15001513 typ := pkg .NewTypeDefs ().NewType ("Rect" )
15011514 recv := types .NewParam (token .NoPos , pkg .Types , "this" , types .NewPointer (typ .Type ()))
1502- defs := pkg .ClassDefsStart (recv , nil , nil )
1515+ defs := pkg .ClassDefsStart (recv , fa . addFld )
15031516 defs .NewAndInit (func (cb * gogen.CodeBuilder ) int {
15041517 panic ("fail" )
1505- }, "" , token .NoPos , types .Typ [types .Int ], "a" , "b" )
1518+ }, token .NoPos , types .Typ [types .Int ], "a" , "b" )
15061519 defs .End ()
1507- typ .InitType (pkg , types .NewStruct (defs .Fields , defs . Tags ))
1520+ typ .InitType (pkg , types .NewStruct (fa .Fields , nil ))
15081521}
15091522
15101523func TestClassDefsNoInit (t * testing.T ) {
15111524 pkg := newMainPackage ()
1525+ fa := fldAdder {pkgTypes : pkg .Types }
15121526 typ := pkg .NewTypeDefs ().NewType ("Rect" )
1513- defs := pkg .ClassDefsStart (nil , nil , nil )
1514- defs .NewAndInit (nil , "" , token .NoPos , types .Typ [types .Int ], "a" , "b" )
1527+ defs := pkg .ClassDefsStart (nil , fa . addFld )
1528+ defs .NewAndInit (nil , token .NoPos , types .Typ [types .Int ], "a" , "b" )
15151529 defs .End ()
1516- typ .InitType (pkg , types .NewStruct (defs .Fields , defs . Tags ))
1530+ typ .InitType (pkg , types .NewStruct (fa .Fields , nil ))
15171531 domTest (t , pkg , `package main
15181532
15191533type Rect struct {
@@ -1523,6 +1537,47 @@ type Rect struct {
15231537` )
15241538}
15251539
1540+ func TestClassDefsEmbedNoInit (t * testing.T ) {
1541+ pkg := newMainPackage ()
1542+ fa := fldAdder {pkgTypes : pkg .Types }
1543+ typ := pkg .NewTypeDefs ().NewType ("Rect" )
1544+ defs := pkg .ClassDefsStart (nil , fa .addFld )
1545+ defs .NewAndInit (nil , token .NoPos , types .Typ [types .Int ])
1546+ defs .End ()
1547+ typ .InitType (pkg , types .NewStruct (fa .Fields , nil ))
1548+ domTest (t , pkg , `package main
1549+
1550+ type Rect struct {
1551+ int
1552+ }
1553+ ` )
1554+ }
1555+
1556+ func TestClassDefsEmbedInit (t * testing.T ) {
1557+ pkg := newMainPackage ()
1558+ fa := fldAdder {pkgTypes : pkg .Types }
1559+ typ := pkg .NewTypeDefs ().NewType ("Rect" )
1560+ recv := types .NewParam (token .NoPos , pkg .Types , "this" , types .NewPointer (typ .Type ()))
1561+ defs := pkg .ClassDefsStart (recv , fa .addFld )
1562+ defs .NewAndInit (func (cb * gogen.CodeBuilder ) int {
1563+ cb .Val (5 )
1564+ return 1
1565+ }, token .NoPos , types .Typ [types .Int ])
1566+ defs .End ()
1567+ typ .InitType (pkg , types .NewStruct (fa .Fields , nil ))
1568+ domTest (t , pkg , `package main
1569+
1570+ type Rect struct {
1571+ int
1572+ }
1573+
1574+ func (this *Rect) XGo_Init() *Rect {
1575+ this.int = 5
1576+ return this
1577+ }
1578+ ` )
1579+ }
1580+
15261581func TestDeleteVarDecl (t * testing.T ) {
15271582 pkg := newMainPackage ()
15281583 pkg .SetRedeclarable (true )
0 commit comments