Norimasa Okamoto
/
pymite
python-on-a-chip online compiler
Embed:
(wiki syntax)
Show/hide line numbers
module.c
Go to the documentation of this file.
00001 /* 00002 # This file is Copyright 2002 Dean Hall. 00003 # This file is part of the PyMite VM. 00004 # This file is licensed under the MIT License. 00005 # See the LICENSE file for details. 00006 */ 00007 00008 00009 #undef __FILE_ID__ 00010 #define __FILE_ID__ 0x0E 00011 00012 00013 /** 00014 * \file 00015 * \brief Module Object Type 00016 * 00017 * Module object type operations. 00018 */ 00019 00020 00021 #include "pm.h" 00022 00023 00024 PmReturn_t 00025 mod_new(pPmObj_t pco, pPmObj_t *pmod) 00026 { 00027 PmReturn_t retval; 00028 uint8_t *pchunk; 00029 pPmObj_t pobj; 00030 uint8_t objid; 00031 00032 /* If it's not a code obj, raise TypeError */ 00033 if (OBJ_GET_TYPE(pco) != OBJ_TYPE_COB) 00034 { 00035 PM_RAISE(retval, PM_RET_EX_TYPE); 00036 return retval; 00037 } 00038 00039 /* Alloc and init func obj */ 00040 retval = heap_getChunk(sizeof(PmFunc_t), &pchunk); 00041 PM_RETURN_IF_ERROR(retval); 00042 *pmod = (pPmObj_t)pchunk; 00043 OBJ_SET_TYPE(*pmod, OBJ_TYPE_MOD); 00044 ((pPmFunc_t)*pmod)->f_co = (pPmCo_t)pco; 00045 ((pPmFunc_t)*pmod)->f_attrs = C_NULL; 00046 ((pPmFunc_t)*pmod)->f_globals = C_NULL; 00047 00048 #ifdef HAVE_DEFAULTARGS 00049 /* Clear the default args (only used by funcs) */ 00050 ((pPmFunc_t)*pmod)->f_defaultargs = C_NULL; 00051 #endif /* HAVE_DEFAULTARGS */ 00052 00053 #ifdef HAVE_CLOSURES 00054 /* Clear field for closure tuple */ 00055 ((pPmFunc_t)*pmod)->f_closure = C_NULL; 00056 #endif /* HAVE_CLOSURES */ 00057 00058 /* Alloc and init attrs dict */ 00059 heap_gcPushTempRoot(*pmod, &objid); 00060 retval = dict_new(&pobj); 00061 heap_gcPopTempRoot(objid); 00062 ((pPmFunc_t)*pmod)->f_attrs = (pPmDict_t)pobj; 00063 00064 /* A module's globals is the same as its attrs */ 00065 ((pPmFunc_t)*pmod)->f_globals = (pPmDict_t)pobj; 00066 00067 return retval; 00068 } 00069 00070 00071 PmReturn_t 00072 mod_import(pPmObj_t pstr, pPmObj_t *pmod) 00073 { 00074 PmMemSpace_t memspace; 00075 uint8_t const *imgaddr = C_NULL; 00076 pPmCo_t pco = C_NULL; 00077 PmReturn_t retval = PM_RET_OK; 00078 pPmObj_t pobj; 00079 uint8_t objid; 00080 00081 /* If it's not a string obj, raise SyntaxError */ 00082 if (OBJ_GET_TYPE(pstr) != OBJ_TYPE_STR) 00083 { 00084 PM_RAISE(retval, PM_RET_EX_SYNTAX); 00085 return retval; 00086 } 00087 00088 /* Try to find the image in the paths */ 00089 retval = img_findInPaths(pstr, &memspace, &imgaddr); 00090 00091 /* If img was not found, raise ImportError */ 00092 if (retval == PM_RET_NO) 00093 { 00094 PM_RAISE(retval, PM_RET_EX_IMPRT); 00095 return retval; 00096 } 00097 00098 /* Load img into code obj */ 00099 retval = obj_loadFromImg(memspace, &imgaddr, &pobj); 00100 PM_RETURN_IF_ERROR(retval); 00101 pco = (pPmCo_t)pobj; 00102 00103 heap_gcPushTempRoot(pobj, &objid); 00104 retval = mod_new((pPmObj_t)pco, pmod); 00105 heap_gcPopTempRoot(objid); 00106 00107 return retval; 00108 }
Generated on Tue Jul 12 2022 23:13:47 by 1.7.2