gpointer module_new (const char* module_name) { if(!module_name) return NULL; int index = 0; char name[256]; gpointer mod = NULL; for(index = -1 ; index < MAX_MODULE_INDEX ; index++){ if(index >= 0){ if( 0 >= g_snprintf(name, 256, "%s%d", module_name, index)){ LOCATION_LOGW("module name(%s) is wrong", name); continue; } }else{ if( 0 >= g_snprintf(name, 256, "%s", module_name)){ LOCATION_LOGW("module name(%s) is wrong", name); continue; } } mod = mod_new(name); if(mod){ LOCATION_LOGW("module (%s) open success", name); break; } LOCATION_LOGW("module (%s) open failed", name); } return mod; }
PmReturn_t mod_import(pPmObj_t pstr, pPmObj_t *pmod) { pPmImgInfo_t pii = C_NULL; uint8_t const *imgaddr = C_NULL; pPmCo_t pco = C_NULL; PmReturn_t retval = PM_RET_OK; pPmObj_t pobj; /* If it's not a string obj, raise SyntaxError */ if (OBJ_GET_TYPE(pstr) != OBJ_TYPE_STR) { PM_RAISE(retval, PM_RET_EX_SYNTAX); return retval; } /* Iterate through the global img list */ pii = gVmGlobal.pimglist; /* Scan until end of list or string matches */ while ((pii != C_NULL) && (string_compare((pPmString_t)pstr, pii->ii_name) == C_DIFFER)) { pii = pii->next; } /* If img was not found, raise ImportError */ if (pii == C_NULL) { PM_RAISE(retval, PM_RET_EX_IMPRT); return retval; } /* Make copy of addr so image list pointer isn't modified */ imgaddr = pii->ii_addr; /* Load img into code obj */ retval = obj_loadFromImg(pii->ii_memspace, &imgaddr, &pobj); PM_RETURN_IF_ERROR(retval); pco = (pPmCo_t)pobj; return mod_new((pPmObj_t)pco, pmod); }
/** * Tests interpret(): * retval is OK */ void ut_interp_interpret_000(CuTest *tc) { PmReturn_t retval; uint8_t const *pimg = test_code_image0; pPmObj_t pcodeobject; pPmObj_t pmodule; pm_init(MEMSPACE_RAM, C_NULL); /* Check the return value of the load function */ retval = obj_loadFromImg(MEMSPACE_PROG, &pimg, &pcodeobject); CuAssertTrue(tc, retval == PM_RET_OK); retval = mod_new(pcodeobject, &pmodule); CuAssertTrue(tc, retval == PM_RET_OK); retval = interp_addThread((pPmFunc_t)pmodule); CuAssertTrue(tc, retval == PM_RET_OK); /* Check that the module is interpreted and its return value is ok */ retval = interpret(C_TRUE); CuAssertTrue(tc, retval == PM_RET_OK); }