module.c

Go to the documentation of this file.
00001 /*
00002 # This file is Copyright 2003, 2006, 2007, 2009, 2010 Dean Hall.
00003 #
00004 # This file is part of the PyMite VM.
00005 # The PyMite VM is free software: you can redistribute it and/or modify
00006 # it under the terms of the GNU GENERAL PUBLIC LICENSE Version 2.
00007 #
00008 # The PyMite VM is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00011 # A copy of the GNU GENERAL PUBLIC LICENSE Version 2
00012 # is seen in the file COPYING in this directory.
00013 */
00014 
00015 
00016 #undef __FILE_ID__
00017 #define __FILE_ID__ 0x0E
00018 
00019 
00028 #include "pm.h"
00029 
00030 
00031 PmReturn_t
00032 mod_new(pPmObj_t pco, pPmObj_t *pmod)
00033 {
00034     PmReturn_t retval;
00035     uint8_t *pchunk;
00036     pPmObj_t pobj;
00037     uint8_t objid;
00038 
00039     /* If it's not a code obj, raise TypeError */
00040     if (OBJ_GET_TYPE(pco) != OBJ_TYPE_COB)
00041     {
00042         PM_RAISE(retval, PM_RET_EX_TYPE);
00043         return retval;
00044     }
00045 
00046     /* Alloc and init func obj */
00047     retval = heap_getChunk(sizeof(PmFunc_t), &pchunk);
00048     PM_RETURN_IF_ERROR(retval);
00049     *pmod = (pPmObj_t)pchunk;
00050     OBJ_SET_TYPE(*pmod, OBJ_TYPE_MOD);
00051     ((pPmFunc_t)*pmod)->f_co = (pPmCo_t)pco;
00052 
00053 #ifdef HAVE_DEFAULTARGS
00054     /* Clear the default args (only used by funcs) */
00055     ((pPmFunc_t)*pmod)->f_defaultargs = C_NULL;
00056 #endif /* HAVE_DEFAULTARGS */
00057 
00058 #ifdef HAVE_CLOSURES
00059     /* Clear field for closure tuple */
00060     ((pPmFunc_t)*pmod)->f_closure = C_NULL;
00061 #endif /* HAVE_CLOSURES */
00062 
00063     /* Alloc and init attrs dict */
00064     heap_gcPushTempRoot(*pmod, &objid);
00065     retval = dict_new(&pobj);
00066     heap_gcPopTempRoot(objid);
00067     ((pPmFunc_t)*pmod)->f_attrs = (pPmDict_t)pobj;
00068 
00069     /* A module's globals is the same as its attrs */
00070     ((pPmFunc_t)*pmod)->f_globals = (pPmDict_t)pobj;
00071 
00072     return retval;
00073 }
00074 
00075 
00076 PmReturn_t
00077 mod_import(pPmObj_t pstr, pPmObj_t *pmod)
00078 {
00079     PmMemSpace_t memspace;
00080     uint8_t const *imgaddr = C_NULL;
00081     pPmCo_t pco = C_NULL;
00082     PmReturn_t retval = PM_RET_OK;
00083     pPmObj_t pobj;
00084     uint8_t objid;
00085 
00086     /* If it's not a string obj, raise SyntaxError */
00087     if (OBJ_GET_TYPE(pstr) != OBJ_TYPE_STR)
00088     {
00089         PM_RAISE(retval, PM_RET_EX_SYNTAX);
00090         return retval;
00091     }
00092 
00093     /* Try to find the image in the paths */
00094     retval = img_findInPaths(pstr, &memspace, &imgaddr);
00095 
00096     /* If img was not found, raise ImportError */
00097     if (retval == PM_RET_NO)
00098     {
00099         PM_RAISE(retval, PM_RET_EX_IMPRT);
00100         return retval;
00101     }
00102 
00103     /* Load img into code obj */
00104     retval = obj_loadFromImg(memspace, &imgaddr, &pobj);
00105     PM_RETURN_IF_ERROR(retval);
00106     pco = (pPmCo_t)pobj;
00107 
00108     heap_gcPushTempRoot(pobj, &objid);
00109     retval = mod_new((pPmObj_t)pco, pmod);
00110     heap_gcPopTempRoot(objid);
00111 
00112     return retval;
00113 }

Generated on Mon Oct 18 07:40:47 2010 for Python-on-a-chip by  doxygen 1.5.9