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__ 0x04 00018 00019 00028 #include "pm.h" 00029 00030 00031 PmReturn_t 00032 func_new(pPmObj_t pco, pPmObj_t pglobals, pPmObj_t *r_pfunc) 00033 { 00034 PmReturn_t retval = PM_RET_OK; 00035 pPmFunc_t pfunc = C_NULL; 00036 uint8_t *pchunk; 00037 pPmObj_t pobj; 00038 uint8_t objid; 00039 00040 C_ASSERT(OBJ_GET_TYPE(pco) != OBJ_TYPE_COB 00041 || OBJ_GET_TYPE(pco) != OBJ_TYPE_NOB); 00042 C_ASSERT(OBJ_GET_TYPE(pglobals) == OBJ_TYPE_DIC); 00043 00044 /* Allocate a func obj */ 00045 retval = heap_getChunk(sizeof(PmFunc_t), &pchunk); 00046 PM_RETURN_IF_ERROR(retval); 00047 pfunc = (pPmFunc_t)pchunk; 00048 00049 /* Init func */ 00050 OBJ_SET_TYPE(pfunc, OBJ_TYPE_FXN); 00051 pfunc->f_co = (pPmCo_t)pco; 00052 pfunc->f_globals = C_NULL; 00053 00054 #ifdef HAVE_DEFAULTARGS 00055 /* Clear default args (will be set later, if at all) */ 00056 pfunc->f_defaultargs = C_NULL; 00057 #endif /* HAVE_DEFAULTARGS */ 00058 00059 #ifdef HAVE_CLOSURES 00060 /* Clear field for closure tuple */ 00061 pfunc->f_closure = C_NULL; 00062 #endif /* HAVE_CLOSURES */ 00063 00064 /* Create attrs dict for regular func (not native) */ 00065 if (OBJ_GET_TYPE(pco) == OBJ_TYPE_COB) 00066 { 00067 heap_gcPushTempRoot((pPmObj_t)pfunc, &objid); 00068 retval = dict_new(&pobj); 00069 heap_gcPopTempRoot(objid); 00070 PM_RETURN_IF_ERROR(retval); 00071 pfunc->f_attrs = (pPmDict_t)pobj; 00072 00073 /* Store the given globals dict */ 00074 pfunc->f_globals = (pPmDict_t)pglobals; 00075 } 00076 else 00077 { 00078 pfunc->f_attrs = C_NULL; 00079 } 00080 00081 *r_pfunc = (pPmObj_t)pfunc; 00082 return PM_RET_OK; 00083 }