Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers module.c Source File

module.c

Go to the documentation of this file.
00001 /*
00002 # This file is Copyright 2003, 2006, 2007, 2009 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 
00020 /**
00021  * \file
00022  * \brief Module Object Type
00023  *
00024  * Module object type operations.
00025  */
00026 
00027 
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 
00038     /* If it's not a code obj, raise TypeError */
00039     if (OBJ_GET_TYPE(pco) != OBJ_TYPE_COB)
00040     {
00041         PM_RAISE(retval, PM_RET_EX_TYPE);
00042         return retval;
00043     }
00044 
00045     /* Alloc and init func obj */
00046     retval = heap_getChunk(sizeof(PmFunc_t), &pchunk);
00047     PM_RETURN_IF_ERROR(retval);
00048     *pmod = (pPmObj_t)pchunk;
00049     OBJ_SET_TYPE(*pmod, OBJ_TYPE_MOD);
00050     ((pPmFunc_t)*pmod)->f_co = (pPmCo_t)pco;
00051 
00052     /* Alloc and init attrs dict */
00053     retval = dict_new(&pobj);
00054     ((pPmFunc_t)*pmod)->f_attrs = (pPmDict_t)pobj;
00055 
00056     /* A module's globals is the same as its attrs */
00057     ((pPmFunc_t)*pmod)->f_globals = (pPmDict_t)pobj;
00058 
00059 #ifdef HAVE_DEFAULTARGS
00060     /* Clear the default args (only used by funcs) */
00061     ((pPmFunc_t)*pmod)->f_defaultargs = C_NULL;
00062 #endif /* HAVE_DEFAULTARGS */
00063 
00064     return retval;
00065 }
00066 
00067 
00068 PmReturn_t
00069 mod_import(pPmObj_t pstr, pPmObj_t *pmod)
00070 {
00071     PmMemSpace_t memspace;
00072     uint8_t const *imgaddr = C_NULL;
00073     pPmCo_t pco = C_NULL;
00074     PmReturn_t retval = PM_RET_OK;
00075     pPmObj_t pobj;
00076 
00077     /* If it's not a string obj, raise SyntaxError */
00078     if (OBJ_GET_TYPE(pstr) != OBJ_TYPE_STR)
00079     {
00080         PM_RAISE(retval, PM_RET_EX_SYNTAX);
00081         return retval;
00082     }
00083 
00084     /* Try to find the image in the paths */
00085     retval = img_findInPaths(pstr, &memspace, &imgaddr);
00086 
00087     /* If img was not found, raise ImportError */
00088     if (retval == PM_RET_NO)
00089     {
00090         PM_RAISE(retval, PM_RET_EX_IMPRT);
00091         return retval;
00092     }
00093 
00094     /* Load img into code obj */
00095     retval = obj_loadFromImg(memspace, &imgaddr, &pobj);
00096     PM_RETURN_IF_ERROR(retval);
00097     pco = (pPmCo_t)pobj;
00098 
00099     return mod_new((pPmObj_t)pco, pmod);
00100 }