1 Data Structures
2 ===============
3
4 Since classes, functions and instances are all "objects", each must support
5 certain features and operations in the same way.
6
7 The __class__ Attribute
8 -----------------------
9
10 All objects support the __class__ attribute:
11
12 Class: refers to the type class (type.__class__ also refers to the type class)
13 Function: refers to the function class
14 Instance: refers to the class instantiated to make the object
15
16 Invocation
17 ----------
18
19 The following actions need to be supported:
20
21 Class: create instance, call __init__ with instance, return object
22 Function: call function body, return result
23 Instance: call __call__ method, return result
24
25 Structure Layout
26 ----------------
27
28 A suitable structure layout might be something like this:
29
30 0 1 2 3 4
31 classcode invocation __class__ attribute ...
32 reference reference reference
33
34 Here, the classcode refers to the attribute lookup table for the object. Since
35 classes and instances share the same classcode, they might resemble the
36 following:
37
38 Class C:
39
40 0 1 2 3 4
41 code for C __new__ class type attribute ...
42 reference reference reference
43
44 Instance of C:
45
46 0 1 2 3 4
47 code for C C.__call__ class C attribute ...
48 reference reference reference
49 (if exists)
50
51 The __new__ reference would lead to code consisting of the following
52 instructions:
53
54 create instance for C
55 call C.__init__(instance, ...)
56 return instance
57
58 If C has a __call__ attribute, the invocation "slot" of C instances would
59 refer to the same thing as C.__call__.
60
61 For functions, the same general layout applies:
62
63 Function f:
64
65 0 1 2 3 4
66 code for code class attribute ...
67 function reference function reference
68 reference
69
70 Here, the code reference would lead to code for the function.
71
72 Invocation Operation
73 --------------------
74
75 Consequently, regardless of the object an invocation is always done as
76 follows:
77
78 get invocation reference (at object+1)
79 jump to reference
80
81 Additional preparation is necessary before the above code: positional
82 arguments must be saved to the parameter stack, and keyword arguments must be
83 resolved and saved to the appropriate position in the parameter stack.
84
85 Attribute Operations
86 --------------------
87
88 Attribute access needs to go through the attribute lookup table.