// actor_obj = start_actor(callable) int ActorStart(lua_State *L) { // Get the global state lua_pushstring(L, RIDX_RUNTIMESTATE); lua_gettable(L, LUA_REGISTRYINDEX); TenshiRuntimeState s = (TenshiRuntimeState)lua_topointer(L, -1); lua_pop(L, 1); // Create an actor // TODO(rqou): Hope calling ActorCreate doesn't somehow introduce a subtle // bug of some sort. TenshiActorState a = ActorCreate(s); if (!a) { lua_pushstring(L, "could not create new actor object!"); lua_error(L); } // Move our callable onto the new actor state. lua_xmove(L, a->L, 1); // Append the new callable to the run queue ActorSetRunnable(a, 0); // our stack is empty // The object we want can be looked up in the global taskset. // TODO(rqou): This is not the most efficient. lua_pushcfunction(L, ActorFindInTaskset); // TODO(rqou): Is there a better way than doing this and then transplanting // the data back? lua_pushthread(a->L); lua_xmove(a->L, L, 1); lua_call(L, 1, 1); // stack is new actor object return 1; }
Actor *ContextActorFind(Context *cx, Obj *actor, int create_ok) { Actor *ac; for (ac = cx->actors; ac; ac = ac->next) { if (ac->actor == actor) return(ac); } if (create_ok) { ac = ActorCreate(actor, cx, cx->actors); cx->actors = ac; ContextInitiateHandlers(cx, actor); return(ac); } return(NULL); }
act_obj* actTestballSpawn(linked_list* act_list, long x, long y) { SDL_Rect bbox = {0,0,16,16}; SDL_Rect arect = {0,0,16,16}; gfx_anim* anim = gfxAnimCreate(NULL, 4, 0, gfxLoadFile("derp.bmp"), arect); gfx_spr* obj = gfxSpriteCreate(bbox, true, anim, 1); act_obj* aobj = ActorCreate(obj, (float)x, (float)y, NULL, TestballThinkProc); aobj->xvel = rand() % 300; aobj->yvel = -(rand() % 300); if((rand() % 100) > 50) aobj->xvel = -aobj->xvel; ListPushFront(act_list, aobj); return aobj; }
static void ZnpActorCreat(char* guid, char* psw, char* host, WORD port) { pZnpActor = ActorCreate(guid, psw, host, port); //Register callback to handle request package if (pZnpActor == NULL) { printf("Couldn't create actor\n"); return; } char* actionTopic; actionTopic = ActorMakeTopicName("action/", guid, "/add_devices"); ActorRegisterCallback(pZnpActor, actionTopic, ZnpActorOnRequestAddDevice, CALLBACK_RETAIN); free(actionTopic); actionTopic = ActorMakeTopicName("action/", guid, "/remove_device"); ActorRegisterCallback(pZnpActor, actionTopic, ZnpActorOnRequestRemoveDevice, CALLBACK_RETAIN); free(actionTopic); }
static void LedActorCreate(char* guid, char* psw, char* host, WORD port) { pLedActor = ActorCreate(guid, psw, host, port); //Register callback to handle request package if (pLedActor == NULL) { printf("Couldn't create actor\n"); return; } char* topicName; topicName = ActorMakeTopicName("action/", guid, "/turn_on"); ActorRegisterCallback(pLedActor, topicName, LedActorOnRequestTurnOn, CALLBACK_RETAIN); free(topicName); topicName = ActorMakeTopicName("action/", guid, "/turn_off"); ActorRegisterCallback(pLedActor, topicName, LedActorOnRequestTurnOff, CALLBACK_RETAIN); free(topicName); topicName = ActorMakeTopicName("action/", guid, "/blink"); ActorRegisterCallback(pLedActor, topicName, LedActorOnRequestBlink, CALLBACK_RETAIN); free(topicName); }
int LoadStudentcode(TenshiRuntimeState s, const char *data, size_t len, TenshiActorState *actor_state) { if (actor_state) { *actor_state = NULL; } TenshiActorState a = ActorCreate(s); if (!a) return LUA_ERRRUN; // TODO(rqou): Return the correct error? int ret = luaL_loadbuffer(a->L, data, len, "<student code>"); if (ret != LUA_OK) { ActorDestroy(a); return ret; } if (actor_state) { *actor_state = a; } return LUA_OK; }
TenshiRuntimeState TenshiRuntimeInit(void) { TenshiRuntimeState ret; ret = (TenshiRuntimeState)(malloc(sizeof(struct _TenshiRuntimeState))); if (!ret) return NULL; ret->L = luaL_newstate(); if (!ret->L) { TenshiRuntimeDeinit(ret); return NULL; } // Register checkxip function on ARM #ifdef __arm__ lua_setcheckxip(ret->L, lua_arm_checkxip); #endif // Load libraries TenshiRuntime_openlibs_phase1(ret->L); lua_gc(ret->L, LUA_GCCOLLECT, 0); // Load actor library. This is special because it loads into the global // scope and not into a specific module. tenshi_open_actor(ret->L); // Load mailbox library. Similar thing. tenshi_open_mbox(ret->L); // Load get_device implementation. tenshi_open_get_device(ret->L); // Load install_trap_global implementation. tenshi_open_trap_global(ret->L); TenshiRuntime_openlibs_phase2(ret->L); // Set up actor scheduler lua_pushcfunction(ret->L, ActorSchedulerInit); if (lua_pcall(ret->L, 0, 0, 0) != LUA_OK) { TenshiRuntimeDeinit(ret); return NULL; } // Set up preemption trick threading_setup(ret->L); // Store ourselves into the registry lua_pushstring(ret->L, RIDX_RUNTIMESTATE); lua_pushlightuserdata(ret->L, ret); lua_settable(ret->L, LUA_REGISTRYINDEX); // Load the code for the actor that handles reading sensor updates and // updating sensor mailboxes ret->sensor_actor = ActorCreate(ret); if (!ret->sensor_actor) { TenshiRuntimeDeinit(ret); return NULL; } int ret_ = luaL_loadbuffer(ret->sensor_actor->L, sensor_actor_lc, sizeof(sensor_actor_lc), "sensor_actor.lua"); if (ret_ != LUA_OK) { TenshiRuntimeDeinit(ret); return NULL; } // Load the code for the actor that handles reading actuator mailboxes and // updating actuators. ret->actuator_actor = ActorCreate(ret); if (!ret->actuator_actor) { TenshiRuntimeDeinit(ret); return NULL; } ret_ = luaL_loadbuffer(ret->actuator_actor->L, actuator_actor_lc, sizeof(actuator_actor_lc), "actuator_actor.lua"); if (ret_ != LUA_OK) { TenshiRuntimeDeinit(ret); return NULL; } // Construct the RIDX_CHANGED_SENSORS table lua_pushstring(ret->L, RIDX_CHANGED_SENSORS); lua_newtable(ret->L); lua_settable(ret->L, LUA_REGISTRYINDEX); // Initialize RIDX_SENSORDEVMAP as a table with weak values that will be // used to map lightuserdata to Lua objects lua_pushstring(ret->L, RIDX_SENSORDEVMAP); lua_newtable(ret->L); lua_pushstring(ret->L, "__mode"); lua_pushstring(ret->L, "v"); lua_settable(ret->L, -3); lua_pushvalue(ret->L, -1); lua_setmetatable(ret->L, -2); lua_settable(ret->L, LUA_REGISTRYINDEX); lua_gc(ret->L, LUA_GCCOLLECT, 0); return ret; }