PmReturn_t class_method(pPmObj_t pinstance, pPmObj_t pfunc, pPmObj_t *r_pmeth) { PmReturn_t retval = PM_RET_OK; uint8_t *pchunk; pPmMethod_t pmeth; pPmObj_t pattrs; uint8_t objid; /* Allocate a method */ retval = heap_getChunk(sizeof(PmMethod_t), &pchunk); PM_RETURN_IF_ERROR(retval); OBJ_SET_TYPE(pchunk, OBJ_TYPE_MTH); /* Set method fields */ pmeth = (pPmMethod_t)pchunk; pmeth->m_instance = (pPmInstance_t)pinstance; pmeth->m_func = (pPmFunc_t)pfunc; pmeth->m_attrs = C_NULL; /* Create the attributes dict */ heap_gcPushTempRoot((pPmObj_t)pmeth, &objid); retval = dict_new(&pattrs); heap_gcPopTempRoot(objid); pmeth->m_attrs = (pPmDict_t)pattrs; *r_pmeth = (pPmObj_t)pmeth; return retval; }
/* Returns an instance of the class by reference */ PmReturn_t class_instantiate(pPmObj_t pclass, pPmObj_t *r_pobj) { PmReturn_t retval = PM_RET_OK; uint8_t *pchunk; pPmObj_t pobj; pPmObj_t pattrs; uint8_t objid; /* Allocate a class instance */ retval = heap_getChunk(sizeof(PmInstance_t), &pchunk); PM_RETURN_IF_ERROR(retval); pobj = (pPmObj_t)pchunk; OBJ_SET_TYPE(pobj, OBJ_TYPE_CLI); /* Set the instance's fields */ ((pPmInstance_t)pobj)->cli_class = (pPmClass_t)pclass; ((pPmInstance_t)pobj)->cli_attrs = C_NULL; /* Create the attributes dict */ heap_gcPushTempRoot(pobj, &objid); retval = dict_new(&pattrs); heap_gcPopTempRoot(objid); ((pPmInstance_t)pobj)->cli_attrs = (pPmDict_t)pattrs; /* TODO: Store pclass in __class__ attr */ *r_pobj = pobj; return retval; }
PmReturn_t global_setBuiltins(pPmFunc_t pmod) { PmReturn_t retval = PM_RET_OK; pPmObj_t pkey = C_NULL; uint8_t const *pbistr = bistr; uint8_t objid; if (PM_PBUILTINS == C_NULL) { /* Need to load builtins first */ global_loadBuiltins(); } /* Put builtins module in the module's attrs dict */ retval = string_new(&pbistr, &pkey); PM_RETURN_IF_ERROR(retval); heap_gcPushTempRoot(pkey, &objid); retval = dict_setItem((pPmObj_t)pmod->f_attrs, pkey, PM_PBUILTINS); heap_gcPopTempRoot(objid); return retval; }
PmReturn_t pm_run(uint8_t const *modstr) { PmReturn_t retval; pPmObj_t pmod; pPmObj_t pstring; uint8_t const *pmodstr = modstr; uint8_t objid1; uint8_t objid2; /* Import module from global struct */ retval = string_new(&pmodstr, &pstring); PM_RETURN_IF_ERROR(retval); heap_gcPushTempRoot(pstring, &objid1); retval = mod_import(pstring, &pmod); PM_RETURN_IF_ERROR(retval); /* Load builtins into thread */ heap_gcPushTempRoot(pmod, &objid2); retval = global_setBuiltins((pPmFunc_t)pmod); PM_RETURN_IF_ERROR(retval); /* Interpret the module's bcode */ retval = interp_addThread((pPmFunc_t)pmod); PM_RETURN_IF_ERROR(retval); heap_gcPopTempRoot(objid1); retval = interpret(INTERP_RETURN_ON_NO_THREADS); /* * De-initialize the hardware platform. * Ignore plat_deinit's retval so interpret's retval returns to caller. */ plat_deinit(); return retval; }