Norimasa Okamoto
/
pymite
python-on-a-chip online compiler
- http://pymbed.appspot.com/
- https://code.google.com/p/python-on-a-chip/
- http://www.youtube.com/watch?v=Oyqc2bFRW9I
- https://bitbucket.org/va009039/pymbed/
more info: python-on-a-chip
vm/module.c@15:94ca5c8003e5, 2016-04-14 (annotated)
- Committer:
- va009039
- Date:
- Thu Apr 14 22:32:57 2016 +0000
- Revision:
- 15:94ca5c8003e5
- Parent:
- 0:65f1469d6bfb
update Nucleo-F401RE.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:65f1469d6bfb | 1 | /* |
va009039 | 0:65f1469d6bfb | 2 | # This file is Copyright 2002 Dean Hall. |
va009039 | 0:65f1469d6bfb | 3 | # This file is part of the PyMite VM. |
va009039 | 0:65f1469d6bfb | 4 | # This file is licensed under the MIT License. |
va009039 | 0:65f1469d6bfb | 5 | # See the LICENSE file for details. |
va009039 | 0:65f1469d6bfb | 6 | */ |
va009039 | 0:65f1469d6bfb | 7 | |
va009039 | 0:65f1469d6bfb | 8 | |
va009039 | 0:65f1469d6bfb | 9 | #undef __FILE_ID__ |
va009039 | 0:65f1469d6bfb | 10 | #define __FILE_ID__ 0x0E |
va009039 | 0:65f1469d6bfb | 11 | |
va009039 | 0:65f1469d6bfb | 12 | |
va009039 | 0:65f1469d6bfb | 13 | /** |
va009039 | 0:65f1469d6bfb | 14 | * \file |
va009039 | 0:65f1469d6bfb | 15 | * \brief Module Object Type |
va009039 | 0:65f1469d6bfb | 16 | * |
va009039 | 0:65f1469d6bfb | 17 | * Module object type operations. |
va009039 | 0:65f1469d6bfb | 18 | */ |
va009039 | 0:65f1469d6bfb | 19 | |
va009039 | 0:65f1469d6bfb | 20 | |
va009039 | 0:65f1469d6bfb | 21 | #include "pm.h" |
va009039 | 0:65f1469d6bfb | 22 | |
va009039 | 0:65f1469d6bfb | 23 | |
va009039 | 0:65f1469d6bfb | 24 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 25 | mod_new(pPmObj_t pco, pPmObj_t *pmod) |
va009039 | 0:65f1469d6bfb | 26 | { |
va009039 | 0:65f1469d6bfb | 27 | PmReturn_t retval; |
va009039 | 0:65f1469d6bfb | 28 | uint8_t *pchunk; |
va009039 | 0:65f1469d6bfb | 29 | pPmObj_t pobj; |
va009039 | 0:65f1469d6bfb | 30 | uint8_t objid; |
va009039 | 0:65f1469d6bfb | 31 | |
va009039 | 0:65f1469d6bfb | 32 | /* If it's not a code obj, raise TypeError */ |
va009039 | 0:65f1469d6bfb | 33 | if (OBJ_GET_TYPE(pco) != OBJ_TYPE_COB) |
va009039 | 0:65f1469d6bfb | 34 | { |
va009039 | 0:65f1469d6bfb | 35 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 36 | return retval; |
va009039 | 0:65f1469d6bfb | 37 | } |
va009039 | 0:65f1469d6bfb | 38 | |
va009039 | 0:65f1469d6bfb | 39 | /* Alloc and init func obj */ |
va009039 | 0:65f1469d6bfb | 40 | retval = heap_getChunk(sizeof(PmFunc_t), &pchunk); |
va009039 | 0:65f1469d6bfb | 41 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 42 | *pmod = (pPmObj_t)pchunk; |
va009039 | 0:65f1469d6bfb | 43 | OBJ_SET_TYPE(*pmod, OBJ_TYPE_MOD); |
va009039 | 0:65f1469d6bfb | 44 | ((pPmFunc_t)*pmod)->f_co = (pPmCo_t)pco; |
va009039 | 0:65f1469d6bfb | 45 | ((pPmFunc_t)*pmod)->f_attrs = C_NULL; |
va009039 | 0:65f1469d6bfb | 46 | ((pPmFunc_t)*pmod)->f_globals = C_NULL; |
va009039 | 0:65f1469d6bfb | 47 | |
va009039 | 0:65f1469d6bfb | 48 | #ifdef HAVE_DEFAULTARGS |
va009039 | 0:65f1469d6bfb | 49 | /* Clear the default args (only used by funcs) */ |
va009039 | 0:65f1469d6bfb | 50 | ((pPmFunc_t)*pmod)->f_defaultargs = C_NULL; |
va009039 | 0:65f1469d6bfb | 51 | #endif /* HAVE_DEFAULTARGS */ |
va009039 | 0:65f1469d6bfb | 52 | |
va009039 | 0:65f1469d6bfb | 53 | #ifdef HAVE_CLOSURES |
va009039 | 0:65f1469d6bfb | 54 | /* Clear field for closure tuple */ |
va009039 | 0:65f1469d6bfb | 55 | ((pPmFunc_t)*pmod)->f_closure = C_NULL; |
va009039 | 0:65f1469d6bfb | 56 | #endif /* HAVE_CLOSURES */ |
va009039 | 0:65f1469d6bfb | 57 | |
va009039 | 0:65f1469d6bfb | 58 | /* Alloc and init attrs dict */ |
va009039 | 0:65f1469d6bfb | 59 | heap_gcPushTempRoot(*pmod, &objid); |
va009039 | 0:65f1469d6bfb | 60 | retval = dict_new(&pobj); |
va009039 | 0:65f1469d6bfb | 61 | heap_gcPopTempRoot(objid); |
va009039 | 0:65f1469d6bfb | 62 | ((pPmFunc_t)*pmod)->f_attrs = (pPmDict_t)pobj; |
va009039 | 0:65f1469d6bfb | 63 | |
va009039 | 0:65f1469d6bfb | 64 | /* A module's globals is the same as its attrs */ |
va009039 | 0:65f1469d6bfb | 65 | ((pPmFunc_t)*pmod)->f_globals = (pPmDict_t)pobj; |
va009039 | 0:65f1469d6bfb | 66 | |
va009039 | 0:65f1469d6bfb | 67 | return retval; |
va009039 | 0:65f1469d6bfb | 68 | } |
va009039 | 0:65f1469d6bfb | 69 | |
va009039 | 0:65f1469d6bfb | 70 | |
va009039 | 0:65f1469d6bfb | 71 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 72 | mod_import(pPmObj_t pstr, pPmObj_t *pmod) |
va009039 | 0:65f1469d6bfb | 73 | { |
va009039 | 0:65f1469d6bfb | 74 | PmMemSpace_t memspace; |
va009039 | 0:65f1469d6bfb | 75 | uint8_t const *imgaddr = C_NULL; |
va009039 | 0:65f1469d6bfb | 76 | pPmCo_t pco = C_NULL; |
va009039 | 0:65f1469d6bfb | 77 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 78 | pPmObj_t pobj; |
va009039 | 0:65f1469d6bfb | 79 | uint8_t objid; |
va009039 | 0:65f1469d6bfb | 80 | |
va009039 | 0:65f1469d6bfb | 81 | /* If it's not a string obj, raise SyntaxError */ |
va009039 | 0:65f1469d6bfb | 82 | if (OBJ_GET_TYPE(pstr) != OBJ_TYPE_STR) |
va009039 | 0:65f1469d6bfb | 83 | { |
va009039 | 0:65f1469d6bfb | 84 | PM_RAISE(retval, PM_RET_EX_SYNTAX); |
va009039 | 0:65f1469d6bfb | 85 | return retval; |
va009039 | 0:65f1469d6bfb | 86 | } |
va009039 | 0:65f1469d6bfb | 87 | |
va009039 | 0:65f1469d6bfb | 88 | /* Try to find the image in the paths */ |
va009039 | 0:65f1469d6bfb | 89 | retval = img_findInPaths(pstr, &memspace, &imgaddr); |
va009039 | 0:65f1469d6bfb | 90 | |
va009039 | 0:65f1469d6bfb | 91 | /* If img was not found, raise ImportError */ |
va009039 | 0:65f1469d6bfb | 92 | if (retval == PM_RET_NO) |
va009039 | 0:65f1469d6bfb | 93 | { |
va009039 | 0:65f1469d6bfb | 94 | PM_RAISE(retval, PM_RET_EX_IMPRT); |
va009039 | 0:65f1469d6bfb | 95 | return retval; |
va009039 | 0:65f1469d6bfb | 96 | } |
va009039 | 0:65f1469d6bfb | 97 | |
va009039 | 0:65f1469d6bfb | 98 | /* Load img into code obj */ |
va009039 | 0:65f1469d6bfb | 99 | retval = obj_loadFromImg(memspace, &imgaddr, &pobj); |
va009039 | 0:65f1469d6bfb | 100 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 101 | pco = (pPmCo_t)pobj; |
va009039 | 0:65f1469d6bfb | 102 | |
va009039 | 0:65f1469d6bfb | 103 | heap_gcPushTempRoot(pobj, &objid); |
va009039 | 0:65f1469d6bfb | 104 | retval = mod_new((pPmObj_t)pco, pmod); |
va009039 | 0:65f1469d6bfb | 105 | heap_gcPopTempRoot(objid); |
va009039 | 0:65f1469d6bfb | 106 | |
va009039 | 0:65f1469d6bfb | 107 | return retval; |
va009039 | 0:65f1469d6bfb | 108 | } |