1. tuck
  2. pymite

Commits

Dean Hall  committed d5c7474

#109: Mainlining directly

  • Participants
  • Parent commits a0f7d13
  • Branches default

Comments (0)

Files changed (3)

File src/tests/system/t071.py

View file
  • Ignore whitespace
 # Tests implementation of builtin function eval()
 #
 
+# Create the code image blow by following these steps::
+#
+#   >>> pic = pmImgCreator.PmImgCreator()
+#   >>> codestr = "import sys; sys.putb(0x34); n=42; sys.putb(0x32)"
+#   >>> co = compile(codestr, "", "single")
+#   >>> codeimg = pic.co_to_str(co)
+#   >>> print codeimg
+#
+
 # The variable "n" does not exist
 # "n" will be initialized by the code contained in the image below
 # The image below is an image of the following code snippet::
 #
 #   ipm> import sys; sys.putb(0x34); n=42; sys.putb(0x32)
 #
-img = '\nh\x00\x00\x03\x00\x04\x04\x03\x03\x00sys\x03\x04\x00putb\x03\x01\x00n\x03\x05\x00<ipm>\x04\x05\x01\xff\xff\xff\xff\x00\x014\x00\x00\x00\x01*\x00\x00\x00\x012\x00\x00\x00d\x00\x00d\x01\x00k\x00\x00Z\x00\x00e\x00\x00i\x01\x00d\x02\x00\x83\x01\x00Fd\x03\x00Z\x02\x00e\x00\x00i\x01\x00d\x04\x00\x83\x01\x00Fd\x01\x00S'
+img = '\nc\x00\x00\x03\x00\x04\x04\x03\x03\x00sys\x03\x04\x00putb\x03\x01\x00n\x03\x00\x00\x04\x05\x01\xff\xff\xff\xff\x00\x014\x00\x00\x00\x01*\x00\x00\x00\x012\x00\x00\x00d\x00\x00d\x01\x00k\x00\x00Z\x00\x00e\x00\x00i\x01\x00d\x02\x00\x83\x01\x00Fd\x03\x00Z\x02\x00e\x00\x00i\x01\x00d\x04\x00\x83\x01\x00Fd\x01\x00S'
 
 # Convert the image to a code object and evaluate it
 co = Co(img)

File src/tools/dismantle.py

View file
  • Ignore whitespace
-#!/usr/bin/python
+#!/usr/bin/env python
 
 # PyMite - A flyweight Python interpreter for 8-bit microcontrollers and more.
 # Copyright 2002 Dean Hall

File src/vm/interp.c

View file
  • Ignore whitespace
                 continue;
 
             case RETURN_VALUE:
-                /* TODO #109: Check that stack should have only 1 item */
                 /* Get expiring frame's TOS */
                 pobj2 = PM_POP();
 
+#if __DEBUG__
+                /* #109: Check that stack should now be empty */
+                /* Get the number of local variables for this code obj */
+                uint8_t const *paddr = FP->fo_func->f_co->co_codeimgaddr
+                                       + CI_STACKSIZE_FIELD + 1;
+                t8 = mem_getByte(FP->fo_func->f_co->co_memspace, &paddr);
+
+                /* SP should point to one past the end of the locals */
+                C_ASSERT(SP == &(FP->fo_locals[t8]));
+#endif
+
                 /* Keep ref of expiring frame */
                 pobj1 = (pPmObj_t)FP;
 
                 /* Get name String obj */
                 pobj1 = FP->fo_func->f_co->co_names->val[t16];
 
+                /* Pop unused None object */
+                pobj3 = PM_POP();
+
+                /* Ensure "level" is -1; no support for relative import yet */
+                pobj3 = TOS;
+                C_ASSERT(obj_compare(pobj3, PM_NEGONE) == C_SAME);
+
                 /* TODO #110: Prevent importing previously-loaded module */
                 /* Load module from image */
                 retval = mod_import(pobj1, &pobj2);
                 PM_BREAK_IF_ERROR(retval);
 
-                /* Module overwrites None on stack */
+                /* Put Module on top of stack */
                 TOS = pobj2;
 
                 /* Code after here is a duplicate of CALL_FUNCTION */
                         gVmGlobal.nativeframe.nf_locals[t16] = PM_POP();
                     }
 
+                    /* Pop the function object */
+                    PM_POP();
+
                     /* Get native function index */
                     pobj2 = (pPmObj_t)((pPmFunc_t)pobj1)->f_co;
                     t16 = ((pPmNo_t)pobj2)->no_funcindx;
                      * RETURN FROM NATIVE FXN
                      */
 
-                    /* If the frame pointer was switched, proceed with it */
+                    /* If the frame pointer was switched, do nothing to TOS */
                     if (retval == PM_RET_FRAME_SWITCH)
                     {
-                        TOS = PM_NONE;
                         retval = PM_RET_OK;
                     }
 
                     /* Otherwise, return the result from the native function */
                     else
                     {
-                        TOS = gVmGlobal.nativeframe.nf_stack;
+                        PM_PUSH(gVmGlobal.nativeframe.nf_stack);
                     }
                     PM_BREAK_IF_ERROR(retval);
                 }