1.1 --- a/docs/invocation.txt Wed Dec 04 16:17:20 2013 +0100
1.2 +++ b/docs/invocation.txt Sun Dec 08 00:05:51 2013 +0100
1.3 @@ -53,6 +53,19 @@
1.4 These cases require additional structures to be created, potentially at
1.5 run-time.
1.6
1.7 +Invocations and argument lists:
1.8 +
1.9 + General procedure:
1.10 +
1.11 + 1. Load target.
1.12 + 2. Put target in list (if appropriate).
1.13 + 3. Put context in list (if appropriate).
1.14 + 4. Put arguments in list.
1.15 + 5. Jump to target.
1.16 +
1.17 + The target is needed for dynamic functions and methods, but is external to
1.18 + any notion of arguments or locals.
1.19 +
1.20 Methods vs. functions:
1.21
1.22 f(obj, 1, 2) # f known as function at compile-time:
1.23 @@ -196,21 +209,13 @@
1.24 def f(x, y=nonconst):
1.25 ...
1.26
1.27 - Defines instance with method:
1.28 -
1.29 - def <lambda>(<context>, x, y=nonconst):
1.30 - ...
1.31 -
1.32 - def f(<context>, x, y=nonconst):
1.33 - ...
1.34 + Defines object referencing function:
1.35
1.36 - Where default is attribute #1.
1.37 -
1.38 - f(obj) # f not known at compile-time
1.39 + def <lambda>(x, y=nonconst): # references lambda x, ...
1.40 + # Obtain nonconst from <lambda> for y, if necessary
1.41
1.42 - f -> f
1.43 - -> load context for argument #1 (f, since an instance is referenced)
1.44 - obj -> argument #2
1.45 + def <f>(x, y=nonconst): # references f
1.46 + # Obtain nonconst from <f> for y, if necessary
1.47
1.48 Dynamic default information and methods:
1.49
1.50 @@ -218,11 +223,14 @@
1.51 def f(self, y=nonconst):
1.52 ...
1.53
1.54 - Defines additional context for the method:
1.55 + Defines additional state for the method:
1.56
1.57 class C:
1.58 - def f(<context>, self, y=nonconst):
1.59 - ...
1.60 + def <f>(self, y=nonconst): # references C.f
1.61 + # Obtain nonconst from <f> for y, if necessary
1.62 +
1.63 + Non-constant information is only likely to be introduced to method defaults
1.64 + within loops or if classes are permitted within functions.
1.65
1.66 Functions as methods:
1.67
2.1 --- a/docs/syspython.txt Wed Dec 04 16:17:20 2013 +0100
2.2 +++ b/docs/syspython.txt Sun Dec 08 00:05:51 2013 +0100
2.3 @@ -33,21 +33,17 @@
2.4 applystaticmethod(fn, obj, ...) # specific invocation of a method via a class
2.5 applymethod(fn, obj, ...) # specific invocation of a method via self
2.6
2.7 -Where dynamic functions are to be invoked, the context providing the defaults
2.8 -needs to be supplied to the function or method, but this can be done using the
2.9 -above special functions as follows:
2.10 -
2.11 - applyclass(cls, __context__, ...)
2.12 - applyfunction(fn, __context__, ...)
2.13 - applystaticmethod(fn, __context__, obj, ...)
2.14 - applymethod(fn, __context__, obj, ...)
2.15 +Where dynamic functions are to be invoked, the object providing the defaults
2.16 +needs to be supplied to the function or method, but this should be done by the
2.17 +invocation mechanism implemented on the target architecture, exposing only a
2.18 +special __context__ local name providing access to the defaults.
2.19
2.20 Where optimisation possibilities cannot be identified in advance, the apply
2.21 function must deal with the following aspects of invocation:
2.22
2.23 + * Whether a dynamic function is being invoked, thus requiring an object for
2.24 + access to defaults
2.25 * Whether a context argument is required
2.26 - * Whether a dynamic function is being invoked, thus requiring a context for
2.27 - access to defaults
2.28 * Whether an appropriate number of arguments have been provided
2.29
2.30 Low-Level Code
2.31 @@ -186,17 +182,17 @@
2.32 This is represented as follows:
2.33
2.34 def outer(x):
2.35 - def inner(__context__, y, z=x):
2.36 - localnames(__context__, y, z)
2.37 + def inner(y, z=x):
2.38 + localnames(y, z, __context__)
2.39 ...
2.40 storelocal(inner, makedynamic(static(module.outer.inner), x))
2.41 return inner
2.42
2.43 The special makedynamic invocation creates an object referring to the function
2.44 and incorporating any specified defaults as attributes of that object. The
2.45 -function itself uses a special __context__ parameter that acts somewhat like
2.46 -the self parameter in methods: when invoked, the __context__ provides access
2.47 -to any default information that needs to be transferred to the local
2.48 +function itself will use a special __context__ local that acts somewhat like
2.49 +the self parameter in methods: in the invoked function, __context__ provides
2.50 +access to any default information that needs to be transferred to the local
2.51 namespace.
2.52
2.53 Function defaults that do not require a dynamic function are initialised using
3.1 --- a/micropython/data.py Wed Dec 04 16:17:20 2013 +0100
3.2 +++ b/micropython/data.py Sun Dec 08 00:05:51 2013 +0100
3.3 @@ -1256,12 +1256,9 @@
3.4
3.5 def _make_dynamic(self):
3.6
3.7 - "Where functions have dynamic defaults, add a context argument."
3.8 + "Where functions have dynamic defaults, add a context local."
3.9
3.10 - name = "__context__"
3.11 - self.argnames.insert(0, name)
3.12 - self.positional_names.insert(0, name)
3.13 - self.set(name, make_instance())
3.14 + self.set("__context__", make_instance())
3.15
3.16 # Namespace-related methods.
3.17
3.18 @@ -1362,7 +1359,7 @@
3.19 if self.finalised:
3.20 return
3.21
3.22 - # Defaults.
3.23 + # Defaults are positioned in the function structure.
3.24
3.25 for i, default in enumerate(self.default_attrs):
3.26 default.position = i
3.27 @@ -1376,7 +1373,8 @@
3.28 else:
3.29 nparams = 0
3.30
3.31 - # Locals (and tuple parameter names).
3.32 + # Locals (and tuple parameter names) are positioned in the current stack
3.33 + # frame.
3.34
3.35 i = None
3.36 for i, attr in enumerate(self.locals().values()):