2 # This file is Copyright 2003, 2006, 2007, 2009, 2010 Dean Hall.
4 # This file is part of the PyMite VM.
5 # The PyMite VM is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU GENERAL PUBLIC LICENSE Version 2.
8 # The PyMite VM is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 # A copy of the GNU GENERAL PUBLIC LICENSE Version 2
12 # is seen in the file COPYING in this directory.
17 #define __FILE_ID__ 0x0E
22 * \brief Module Object Type
24 * Module object type operations.
32 mod_new(pPmObj_t pco
, pPmObj_t
*pmod
)
39 /* If it's not a code obj, raise TypeError */
40 if (OBJ_GET_TYPE(pco
) != OBJ_TYPE_COB
)
42 PM_RAISE(retval
, PM_RET_EX_TYPE
);
46 /* Alloc and init func obj */
47 retval
= heap_getChunk(sizeof(PmFunc_t
), &pchunk
);
48 PM_RETURN_IF_ERROR(retval
);
49 *pmod
= (pPmObj_t
)pchunk
;
50 OBJ_SET_TYPE(*pmod
, OBJ_TYPE_MOD
);
51 ((pPmFunc_t
)*pmod
)->f_co
= (pPmCo_t
)pco
;
53 #ifdef HAVE_DEFAULTARGS
54 /* Clear the default args (only used by funcs) */
55 ((pPmFunc_t
)*pmod
)->f_defaultargs
= C_NULL
;
56 #endif /* HAVE_DEFAULTARGS */
59 /* Clear field for closure tuple */
60 ((pPmFunc_t
)*pmod
)->f_closure
= C_NULL
;
61 #endif /* HAVE_CLOSURES */
63 /* Alloc and init attrs dict */
64 heap_gcPushTempRoot(*pmod
, &objid
);
65 retval
= dict_new(&pobj
);
66 heap_gcPopTempRoot(objid
);
67 ((pPmFunc_t
)*pmod
)->f_attrs
= (pPmDict_t
)pobj
;
69 /* A module's globals is the same as its attrs */
70 ((pPmFunc_t
)*pmod
)->f_globals
= (pPmDict_t
)pobj
;
77 mod_import(pPmObj_t pstr
, pPmObj_t
*pmod
)
79 PmMemSpace_t memspace
;
80 uint8_t const *imgaddr
= C_NULL
;
82 PmReturn_t retval
= PM_RET_OK
;
86 /* If it's not a string obj, raise SyntaxError */
87 if (OBJ_GET_TYPE(pstr
) != OBJ_TYPE_STR
)
89 PM_RAISE(retval
, PM_RET_EX_SYNTAX
);
93 /* Try to find the image in the paths */
94 retval
= img_findInPaths(pstr
, &memspace
, &imgaddr
);
96 /* If img was not found, raise ImportError */
97 if (retval
== PM_RET_NO
)
99 PM_RAISE(retval
, PM_RET_EX_IMPRT
);
103 /* Load img into code obj */
104 retval
= obj_loadFromImg(memspace
, &imgaddr
, &pobj
);
105 PM_RETURN_IF_ERROR(retval
);
108 heap_gcPushTempRoot(pobj
, &objid
);
109 retval
= mod_new((pPmObj_t
)pco
, pmod
);
110 heap_gcPopTempRoot(objid
);