Commits

tuck...@gmail.com  committed cd64b36 Draft Merge

autboxing feature merge and stm32f4 target

  • Participants
  • Parent commits 5d2478a, 402d867

Comments (0)

Files changed (20)

File src/lib/dict.py

 ## @package dict
 #  @brief Provides PyMite's dict module.
 
+__name__ = "dict"
+
+
+class _Autobox:
+    def clear(self):
+        return clear(self.obj)
+
+    def keys(self):
+        return keys(self.obj)
+
+    def has_key(self, k):
+        return has_key(self.obj, k)
+
+    def values(self):
+        return values(self.obj)
+
+
 def clear(d):
     """__NATIVE__
     pPmObj_t pd;

File src/lib/list.py

 # - index(l, o) does not offer start and stop arguments.
 
 
+__name__ = "list"
+
+class _Autobox:
+    def append(self, o):
+        return append(self.obj, o)
+
+    def count(self, v):
+        return count(self.obj, v)
+
+    def extend(self, s):
+        return extend(self.obj, s)
+
+    def index(self, o):
+        return index(self.obj, o)
+
+    def insert(self, i, o):
+        return insert(self.obj, i, o)
+
+    def pop(self, i=None):
+        if i==None:
+            return pop(self.obj)
+        else:
+            return pop(self.obj, i)
+
+    def remove(self, v):
+        return remove(self.obj, v)
+
+
 def append(l, o):
     """__NATIVE__
     pPmObj_t pl;

File src/lib/string.py

 """
 
 
+__name__ = "string"
+
+
+class _Autobox:
+    def join(self, l):
+        return join(self.obj, l)
+
+    def count(self, s):
+        return count(self.obj, s)
+
+    def find(self, s):
+        return find(self.obj, s)
+
+
 digits = "0123456789"
 hexdigits = "0123456789abcdefABCDEF"
 letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

File src/platform/arduino_mega/pmfeatures.py

     "HAVE_BYTEARRAY": False,
     "HAVE_DEBUG_INFO": True,
     "HAVE_SNPRINTF_FORMAT": False,
+    "HAVE_AUTOBOX": False,
 }

File src/platform/at91sam7s-ek/pmfeatures.py

     "HAVE_BYTEARRAY": False,
     "HAVE_DEBUG_INFO": False,
     "HAVE_SNPRINTF_FORMAT": False,
+    "HAVE_AUTOBOX": False,
 }

File src/platform/avr/pmfeatures.py

     "HAVE_BYTEARRAY": False,
     "HAVE_DEBUG_INFO": False,
     "HAVE_SNPRINTF_FORMAT": False,
+    "HAVE_AUTOBOX": False,
 }

File src/platform/desktop/pmfeatures.py

     "HAVE_BYTEARRAY": False,
     "HAVE_DEBUG_INFO": True,
     "HAVE_SNPRINTF_FORMAT": False,
+    "HAVE_AUTOBOX": False,
 }

File src/platform/desktop64/pmfeatures.py

     "HAVE_BYTEARRAY": False,
     "HAVE_DEBUG_INFO": True,
     "HAVE_SNPRINTF_FORMAT": False,
+    "HAVE_AUTOBOX": True,
 }

File src/platform/econotag/pmfeatures.py

     "HAVE_BYTEARRAY": True,
     "HAVE_DEBUG_INFO": False,
     "HAVE_SNPRINTF_FORMAT": False,
+    "HAVE_AUTOBOX": False,
 }

File src/platform/mbed/pmfeatures.py

     "HAVE_BYTEARRAY": False,
     "HAVE_DEBUG_INFO": True,
     "HAVE_SNPRINTF_FORMAT": False,
+    "HAVE_AUTOBOX": True,
 }

File src/platform/mosync/pmfeatures.py

     "HAVE_BYTEARRAY": False,
     "HAVE_DEBUG_INFO": True,
     "HAVE_SNPRINTF_FORMAT": False,
+    "HAVE_AUTOBOX": False,
 }

File src/platform/pic24/pmfeatures.py

     "HAVE_BYTEARRAY": False,
     "HAVE_DEBUG_INFO": True,
     "HAVE_SNPRINTF_FORMAT": False,
+    "HAVE_AUTOBOX": False,
 }

File src/platform/stm32/pmfeatures.py

     "HAVE_BYTEARRAY": False,
     "HAVE_DEBUG_INFO": False,
     "HAVE_SNPRINTF_FORMAT": False,
+    "HAVE_AUTOBOX": False,
 }

File src/platform/stm32f4/pmfeatures.py

     "HAVE_CLOSURES": False,
     "HAVE_BYTEARRAY": False,
     "HAVE_DEBUG_INFO": False,
+    "HAVE_AUTOBOX": False,
 }

File src/platform/teensy/pmfeatures.py

     "HAVE_BYTEARRAY": False,
     "HAVE_DEBUG_INFO": False,
     "HAVE_SNPRINTF_FORMAT": False,
+    "HAVE_AUTOBOX": False,
 }

File src/platform/windows/pmfeatures.py

     "HAVE_BYTEARRAY": False,
     "HAVE_DEBUG_INFO": True,
     "HAVE_SNPRINTF_FORMAT": False,
+    "HAVE_AUTOBOX": True,
 }

File src/vm/class.c

 #include "pm.h"
 
 
+#ifdef HAVE_AUTOBOX
+static uint8_t const *liststr = (uint8_t const *)"list";
+static uint8_t const *dictstr = (uint8_t const *)"dict";
+static uint8_t const *stringstr = (uint8_t const *)"string";
+static uint8_t const *autoboxstr = (uint8_t const *)"_Autobox";
+static uint8_t const *objstr = (uint8_t const *)"obj";
+#endif
+
+
 PmReturn_t
 class_new(pPmObj_t pattrs, pPmObj_t pbases, pPmObj_t pname, pPmObj_t *r_pclass)
 {
 }
 
 
+#ifdef HAVE_AUTOBOX
+PmReturn_t
+class_autobox(pPmObj_t *pobj)
+{
+    PmReturn_t retval = PM_RET_OK;
+    pPmObj_t pmodule, pstr, pclass, pwrapped, pmodcache;
+
+    uint8_t const *pliststr = liststr;
+    uint8_t const *pdictstr = dictstr;
+    uint8_t const *pstringstr = stringstr;
+
+    uint8_t const *pAutoboxstr = autoboxstr;
+    uint8_t const *pobjstr = objstr;
+
+    /* Load the appropriate module name,
+     * or do nothing if we have a non-boxable type
+     */
+    if (OBJ_GET_TYPE(*pobj) == OBJ_TYPE_LST) {
+        retval = string_new(&pliststr, &pstr);
+        PM_RETURN_IF_ERROR(retval);
+    } else if (OBJ_GET_TYPE(*pobj) == OBJ_TYPE_DIC) {
+        retval = string_new(&pdictstr, &pstr);
+        PM_RETURN_IF_ERROR(retval);
+    } else if (OBJ_GET_TYPE(*pobj) == OBJ_TYPE_STR) {
+        retval = string_new(&pstringstr, &pstr);
+        PM_RETURN_IF_ERROR(retval);
+    } else {
+        return retval;
+    }
+
+    /** first, try to get the module from the cache */
+    retval = dict_getItem(PM_PBUILTINS, PM_MD_STR, &pmodcache);
+    PM_RETURN_IF_ERROR(retval);
+
+    retval = dict_getItem(pmodcache, pstr, &pmodule);
+    PM_RETURN_IF_ERROR(retval);
+
+    if (!((retval == PM_RET_OK) && (OBJ_GET_TYPE(pmodule) == OBJ_TYPE_MOD)))
+    {
+        PM_RAISE(retval, PM_RET_EX_SYS);
+        return retval;
+    }
+
+    /* grab the class from within the loaded module */
+    retval = string_new(&pAutoboxstr, &pstr);
+    PM_RETURN_IF_ERROR(retval);
+    retval = dict_getItem((pPmObj_t) ((pPmFunc_t)pmodule)->f_attrs, pstr, &pclass);
+    PM_RETURN_IF_ERROR(retval);
+
+    /* instantiate instance of (type)._Autobox */
+    retval = class_instantiate(pclass, &pwrapped);
+    PM_RETURN_IF_ERROR(retval);
+
+    /* store object as _Autobox().obj */
+    retval = string_new(&pobjstr, &pstr);
+    PM_RETURN_IF_ERROR(retval);
+    retval = dict_setItem((pPmObj_t)((pPmInstance_t)pwrapped)->cli_attrs,
+                          pstr, *pobj);
+    PM_RETURN_IF_ERROR(retval);
+
+    /** replace old object with new instance in place */
+    *pobj = pwrapped;
+
+    return retval;
+}
+#endif
+
+
 PmReturn_t
 class_method(pPmObj_t pinstance, pPmObj_t pfunc, pPmObj_t *r_pmeth)
 {

File src/vm/class.h

  */
 PmReturn_t class_instantiate(pPmObj_t pclass, pPmObj_t *r_pobj);
 
+#ifdef HAVE_AUTOBOX
+/**
+ * Autoboxes an object in place
+ *
+ * @param pclass Pointer to object
+ * @return  Return status
+ */
+PmReturn_t class_autobox(pPmObj_t *pobj);
+#endif
+
 /**
  * Returns a method based on the given inputs
  *

File src/vm/interp.c

                 /* Implements TOS.attr */
                 t16 = GET_ARG();
 
+#ifdef HAVE_AUTOBOX
+                /* Autobox the object, if necessary */
+                retval = class_autobox(&TOS);
+                PM_BREAK_IF_ERROR(retval);
+#endif
+
                 /* Get attrs dict from obj */
                 if ((OBJ_GET_TYPE(TOS) == OBJ_TYPE_FXN) ||
                     (OBJ_GET_TYPE(TOS) == OBJ_TYPE_MOD))
 #include "func.h"
 #include "module.h"
 #include "frame.h"
+#include "class.h"
 #include "interp.h"
 #include "img.h"
 #include "global.h"
-#include "class.h"
 #include "thread.h"
 #include "float.h"
 #include "plat_interface.h"