2
2
3
3
package py
4
4
5
- // FIXME If we only have two types of Init and New function pointers
6
- // we don't really need function pointers, though the ported C code
7
- // expects them to be pointers
8
-
9
5
import (
10
6
"fmt"
11
7
)
@@ -68,6 +64,10 @@ const (
68
64
TPFLAGS_DEFAULT = TPFLAGS_HAVE_VERSION_TAG
69
65
)
70
66
67
+ type NewFunc func (metatype * Type , args Tuple , kwargs StringDict ) Object
68
+
69
+ type InitFunc func (self Object , args Tuple , kwargs StringDict )
70
+
71
71
type Type struct {
72
72
ObjectType * Type // Type of this object -- FIXME this is redundant in Base?
73
73
Name string // For printing, in format "<module>.<name>"
@@ -83,8 +83,8 @@ type Type struct {
83
83
// Cache Object
84
84
// Subclasses Tuple
85
85
// Weaklist Tuple
86
- New func ( metatype * Type , args Tuple , kwargs StringDict ) * Type
87
- Init func ( self * Type , args Tuple , kwargs StringDict )
86
+ New NewFunc
87
+ Init InitFunc
88
88
Flags int // Flags to define presence of optional/expanded features
89
89
Qualname string
90
90
@@ -168,17 +168,19 @@ type Type struct {
168
168
*/
169
169
}
170
170
171
- var TypeType * Type = & Type {Name : "type" , Doc : "type(object) -> the object's type\n type(name, bases, dict) -> a new type" }
172
- var BaseObjectType = NewType ("object" , "The most base type" )
171
+ var TypeType * Type = & Type {
172
+ Name : "type" ,
173
+ Doc : "type(object) -> the object's type\n type(name, bases, dict) -> a new type" ,
174
+ }
175
+ var BaseObjectType = NewTypeX ("object" , "The most base type" , ObjectNew , ObjectInit )
173
176
174
177
func init () {
175
- TypeType .ObjectType = TypeType
176
- // FIXME put this into NewType
178
+ // Initialises like this to avoid initialisation loops
177
179
TypeType .New = TypeNew
178
180
TypeType .Init = TypeInit
181
+ TypeType .ObjectType = TypeType
182
+ // FIXME put this into NewType
179
183
BaseObjectType .Flags |= TPFLAGS_BASETYPE
180
- BaseObjectType .New = ObjectNew
181
- BaseObjectType .Init = ObjectInit
182
184
}
183
185
184
186
// Type of this object
@@ -197,6 +199,19 @@ func NewType(Name string, Doc string) *Type {
197
199
}
198
200
}
199
201
202
+ // Make a new type with constructors
203
+ //
204
+ // For making Go types
205
+ func NewTypeX (Name string , Doc string , New NewFunc , Init InitFunc ) * Type {
206
+ return & Type {
207
+ ObjectType : TypeType ,
208
+ Name : Name ,
209
+ Doc : Doc ,
210
+ New : New ,
211
+ Init : Init ,
212
+ }
213
+ }
214
+
200
215
// Determine the most derived metatype.
201
216
func (metatype * Type ) CalculateMetaclass (bases Tuple ) * Type {
202
217
// Determine the proper metatype to deal with this,
@@ -1021,7 +1036,7 @@ func (t *Type) Alloc() *Type {
1021
1036
}
1022
1037
1023
1038
// Create a new type
1024
- func TypeNew (metatype * Type , args Tuple , kwargs StringDict ) * Type {
1039
+ func TypeNew (metatype * Type , args Tuple , kwargs StringDict ) Object {
1025
1040
fmt .Printf ("TypeNew(type=%q, args=%v, kwargs=%v\n " , metatype .Name , args , kwargs )
1026
1041
var nameObj , basesObj , orig_dictObj Object
1027
1042
var new_type , base , winner * Type
@@ -1365,7 +1380,7 @@ func TypeNew(metatype *Type, args Tuple, kwargs StringDict) *Type {
1365
1380
return new_type
1366
1381
}
1367
1382
1368
- func TypeInit (cls * Type , args Tuple , kwargs StringDict ) {
1383
+ func TypeInit (cls Object , args Tuple , kwargs StringDict ) {
1369
1384
if len (kwargs ) != 0 {
1370
1385
// FIXME TypeError
1371
1386
panic (fmt .Sprintf ("TypeError: type.__init__() takes no keyword arguments" ))
@@ -1426,7 +1441,7 @@ func excess_args(args Tuple, kwargs StringDict) bool {
1426
1441
return len (args ) != 0 || len (kwargs ) != 0
1427
1442
}
1428
1443
1429
- func ObjectInit (self * Type , args Tuple , kwargs StringDict ) {
1444
+ func ObjectInit (self Object , args Tuple , kwargs StringDict ) {
1430
1445
t := self .Type ()
1431
1446
// FIXME bodge to compare function pointers
1432
1447
if excess_args (args , kwargs ) && (fmt .Sprintf ("%x" , t .New ) == fmt .Sprintf ("%x" , ObjectNew ) || fmt .Sprintf ("%x" , t .Init ) != fmt .Sprintf ("%x" , ObjectInit )) {
@@ -1435,7 +1450,8 @@ func ObjectInit(self *Type, args Tuple, kwargs StringDict) {
1435
1450
}
1436
1451
// Call the __init__ method if it exists
1437
1452
// FIXME this isn't the way cpython does it - it adjusts the function pointers
1438
- init := self .GetAttrOrNil ("__init__" )
1453
+ t = self .(* Type )
1454
+ init := t .GetAttrOrNil ("__init__" )
1439
1455
fmt .Printf ("init = %v\n " , init )
1440
1456
if init != nil {
1441
1457
newArgs := make (Tuple , len (args )+ 1 )
@@ -1445,7 +1461,7 @@ func ObjectInit(self *Type, args Tuple, kwargs StringDict) {
1445
1461
}
1446
1462
}
1447
1463
1448
- func ObjectNew (t * Type , args Tuple , kwargs StringDict ) * Type {
1464
+ func ObjectNew (t * Type , args Tuple , kwargs StringDict ) Object {
1449
1465
// FIXME bodge to compare function pointers
1450
1466
if excess_args (args , kwargs ) && (fmt .Sprintf ("%x" , t .Init ) == fmt .Sprintf ("%x" , ObjectInit ) || fmt .Sprintf ("%x" , t .New ) != fmt .Sprintf ("%x" , ObjectNew )) {
1451
1467
// FIXME type error
0 commit comments