# HG changeset patch # User Paul Boddie # Date 1193703759 -3600 # Node ID 093eff91a3ff40479c46083048cc217ebdac06b4 # Parent 2675f922e209c9b353ed8fd6acb756527b0b0c78 Added notes on data structures. diff -r 2675f922e209 -r 093eff91a3ff README.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.txt Tue Oct 30 01:22:39 2007 +0100 @@ -0,0 +1,88 @@ +Data Structures +=============== + +Since classes, functions and instances are all "objects", each must support +certain features and operations in the same way. + +The __class__ Attribute +----------------------- + +All objects support the __class__ attribute: + +Class: refers to the type class (type.__class__ also refers to the type class) +Function: refers to the function class +Instance: refers to the class instantiated to make the object + +Invocation +---------- + +The following actions need to be supported: + +Class: create instance, call __init__ with instance, return object +Function: call function body, return result +Instance: call __call__ method, return result + +Structure Layout +---------------- + +A suitable structure layout might be something like this: + + 0 1 2 3 4 + classcode invocation __class__ attribute ... + reference reference reference + +Here, the classcode refers to the attribute lookup table for the object. Since +classes and instances share the same classcode, they might resemble the +following: + +Class C: + + 0 1 2 3 4 + code for C __new__ class type attribute ... + reference reference reference + +Instance of C: + + 0 1 2 3 4 + code for C C.__call__ class C attribute ... + reference reference reference + (if exists) + +The __new__ reference would lead to code consisting of the following +instructions: + + create instance for C + call C.__init__(instance, ...) + return instance + +If C has a __call__ attribute, the invocation "slot" of C instances would +refer to the same thing as C.__call__. + +For functions, the same general layout applies: + +Function f: + + 0 1 2 3 4 + code for code class attribute ... + function reference function reference + reference + +Here, the code reference would lead to code for the function. + +Invocation Operation +-------------------- + +Consequently, regardless of the object an invocation is always done as +follows: + + get invocation reference (at object+1) + jump to reference + +Additional preparation is necessary before the above code: positional +arguments must be saved to the parameter stack, and keyword arguments must be +resolved and saved to the appropriate position in the parameter stack. + +Attribute Operations +-------------------- + +Attribute access needs to go through the attribute lookup table.