Index: codeObjectAssembler.py
===================================================================
RCS file: /usr/local/cvsroot/compyler/codeObjectAssembler.py,v
retrieving revision 1.76
diff -u -r1.76 codeObjectAssembler.py
--- codeObjectAssembler.py	2 Jul 2006 18:16:42 -0000	1.76
+++ codeObjectAssembler.py	5 Jul 2006 21:48:01 -0000
@@ -7,6 +7,7 @@
 from pyasm.structs import PyObject, PyTypeObject, PyFunctionObject, \
 PyTupleObject
 
+
 ob_refcnt = PyObject.ob_refcnt
 ob_type = PyObject.ob_type
 tp_dealloc = PyTypeObject.tp_dealloc
@@ -20,7 +21,7 @@
 del PyFunctionObject
 del PyTupleObject
 
-debug = 0
+debug = 1
 
 codeType = type(compile("1","","single"))
 
@@ -126,8 +127,9 @@
         self.currentExitFunc = self.getTempLabel("exit_func")
         
         self.emit_const_defs(co.co_consts)
-        self.emit_cellvar_defs(co.co_cellvars)
         self.emit_var_defs(co.co_varnames)
+        self.emit_cellvar_defs(co.co_cellvars)
+        
         # No more variables after this point, begin code emission
         self.debugTestStackBegin()
         self.debugPrintRt("BEGIN RUNNING %s" % name)
@@ -196,7 +198,7 @@
 
        
         self.emit_const_values(co.co_consts)
-        self.emit_var_values(co.co_varnames)
+        self.emit_var_values(co.co_argcount, len(co.co_varnames))
         self.emit_names(co.co_names)
         self.emit_cellvar_values(co.co_cellvars)
         
@@ -363,19 +365,26 @@
 
     def emit_cellvar_values(self, cellvars):
         for i in range(len(cellvars)):
+            self.debugPrintRt("Setting cellvar%s to None" % i,2)
             self.emit_none()
             self.a("PUSH EAX")
             self.a("CALL PyCell_New")
-            self.emit_py_error_check(8)
+            self.emit_py_error_check(4)
             self.a("MOV cellvar%s, EAX" % i)
 
-    def emit_var_values(self,vars):
+    def emit_var_values(self,argcount,varcount):
+        """
+        We're really only loading the function arguments here, not all
+        variables.  The remaining ones will be loaded by appropriate
+        LOAD_* opcodes.
+        """
         if self.co_type != 'normal': return
-        if len(vars) == 0: return
+        if argcount == 0: return
 
         loop = self.getTempLabel("loopStart")
         defaults = self.getTempLabel("defaults")
         defaultsLoop = self.getTempLabel("defaultsLoop")
+        varsLoop = self.getTempLabel("varsLoop")
         exit = self.getTempLabel("exit")
 
         self.a("PUSH EDX")
@@ -405,9 +414,9 @@
         self.a("ADD EDX,0x1")
         self.a("SUB EBX,0x4")
 
-        self.a("CMP EDX, %s" % len(vars))
+        self.a("CMP EDX, %s" % argcount)
         self.a("JL %s" % loop)
-        self.a("JMP %s" % exit)
+        self.a("JMP %s" % varsLoop)
 
         self.a.AIL(defaults)
         # plug in defaults if we need to.
@@ -422,6 +431,8 @@
         self.a("!CALL PyXFunction_GetDefaults self")
         #self.emit_py_error_check(4)
         self.a("ADD ESP,4") #can't use emit_py_error_check: NULL == no defaults
+        self.a("TEST EAX,EAX")
+        self.a("JZ %s" % varsLoop)
         self.a("MOV v,EAX")
 
 
@@ -431,7 +442,7 @@
         self.a("MOV w, EAX")
 
         #adjust defaults indexes accordingly
-        self.a("SUB EDX, %s" % len(vars))
+        self.a("SUB EDX, %s" % argcount)
         self.a("ADD EDX, w")
 
         #self.a("INT 3")
@@ -451,6 +462,24 @@
         self.a("CMP EDX, w")
         self.a("JL %s" % defaultsLoop)
 
+        self.a("CMP EDX, %s" % varcount)
+        self.a("JGE %s" % exit)
+
+        self.a.AIL(varsLoop)
+
+        self.a("PUSH EDX")
+
+        self.emit_none()
+
+        self.a("POP EDX")
+
+        self.a("MOV [EBX], EAX")
+        self.a("ADD EDX,0x1")
+        self.a("SUB EBX,0x4")
+
+        self.a("CMP EDX, %s" % varcount)
+        self.a("JL %s" % varsLoop)
+
         self.a.AIL(exit)
 
         self.a("POP EBX")
@@ -2252,7 +2281,7 @@
         self.debugPrintRt("CALL_FUNCTION",1)
         self.emit_pos_and_kw_args(argc)
 
-    def MAKE_FUNCTION(self, argc):
+    def MAKE_FUNCTION(self, argc, closure=False):
         """
         Pushes a new function object on the stack. TOS is the code associated with the
         function. The function object is defined to have argc default parameters, which
@@ -2268,6 +2297,11 @@
         self.emit_incref_reg()
         self.a("MOV t, EAX")
 
+        if closure:
+            self.debugPrintRt("DOING CLOSURE STUFF",1)
+            self.emit_none()
+            self.a("POP u")
+
         if argc > 0:
             self.emit_build_tuple_or_list(argc,"Tuple")
             #tuple already on top of stack
@@ -2285,7 +2319,9 @@
         variables. The function also has argc default parameters, where are found
         before the cells.
         """
-        pass
+        self.debugPrintCt("MAKE_CLOSURE %s" % argc,1)
+        self.debugPrintRt("MAKE_CLOSURE %s" % argc,1)
+        self.MAKE_FUNCTION(argc,closure=True)
 
     def BUILD_SLICE (self,argc):
         """
