43 static TaskContext* __getTC(lua_State*);
48 # define _DBG(fmt, ...) printf("%s:%d\t" fmt "\n", __FUNCTION__, __LINE__, __VA_ARGS__) 50 # define _DBG(fmt, ...) do { } while(0) 58 void*
operator new(
size_t size, lua_State* L,
const char* mt)
60 void* ptr = lua_newuserdata(L, size);
61 luaL_getmetatable(L, mt);
63 lua_setmetatable(L, -2);
73 #define luaM_pushobject(L, T) new(L, #T) T 74 #define luaM_pushobject_mt(L, MT, T) new(L, MT) T 77 #define luaM_checkudata(L, pos, T) reinterpret_cast<T*>(luaL_checkudata((L), (pos), #T)) 78 #define luaM_checkudata_mt(L, pos, MT, T) reinterpret_cast<T*>(luaL_checkudata((L), (pos), MT)) 81 #define luaM_testudata(L, pos, T) (T*) (luaL_testudata((L), (pos), #T)) 82 #define luaM_testudata_mt(L, pos, MT, T) (T*) (luaL_testudata((L), (pos), MT)) 89 #define luaM_checkudata_bx(L, pos, T) (T**) (luaL_checkudata((L), (pos), #T)) 90 #define luaM_checkudata_mt_bx(L, pos, MT, T) (T**) (luaL_checkudata((L), (pos), MT)) 93 #define luaM_testudata_bx(L, pos, T) (T**) (luaL_testudata((L), (pos), #T)) 94 #define luaM_testudata_mt_bx(L, pos, MT, T) (T**) (luaL_testudata((L), (pos), MT)) 97 #define gen_push_bxptr(name, MT, T) \ 98 static void name(lua_State *L, T* ptr) \ 100 T** ptrptr = (T**) lua_newuserdata(L, sizeof(T*)); \ 102 luaL_getmetatable(L, MT); \ 103 lua_setmetatable(L, -2); \ 108 int GCMethod(lua_State* L)
110 reinterpret_cast<T*
>(lua_touserdata(L, 1))->~T();
120 void* luaL_testudata (lua_State *L,
int ud,
const char *tname)
122 void *p = lua_touserdata(L, ud);
127 if (!lua_getmetatable(L, ud)) {
133 lua_pushstring(L, tname);
134 lua_rawget(L, LUA_REGISTRYINDEX);
136 if (!lua_rawequal(L, -1, -2))
145 void push_vect_str(lua_State *L,
const std::vector<std::string> &v)
148 lua_createtable(L, v.size(), 0);
150 for(vector<std::string>::const_iterator it = v.begin(); it != v.end(); ++it) {
151 lua_pushstring(L, it->c_str());
152 lua_rawseti(L, -2, key++);
157 static void Variable_fromlua(lua_State *L, DataSourceBase::shared_ptr& dsb,
int valind);
158 static DataSourceBase::shared_ptr Variable_fromlua(lua_State *L,
const types::TypeInfo* ti,
int valind);
159 static DataSourceBase::shared_ptr Variable_fromlua(lua_State *L,
const char* type,
int valind);
165 static const TypeInfo* ti_lookup(lua_State *L,
const char *name)
167 #ifndef TYPEINFO_CACHING 168 return types::TypeInfoRepository::Instance()->type(name);
171 int top = lua_gettop(L);
175 lua_pushstring(L,
"typeinfo_cache");
176 lua_rawget(L, LUA_REGISTRYINDEX);
178 if(lua_type(L, -1) == LUA_TTABLE)
184 lua_pushstring(L,
"typeinfo_cache");
185 lua_pushvalue(L, -2);
186 lua_rawset(L, LUA_REGISTRYINDEX);
190 lua_pushstring(L, name);
193 if(lua_type(L, -1) != LUA_TLIGHTUSERDATA)
196 ti = (
const TypeInfo*) lua_touserdata(L, -1);
201 ti = types::TypeInfoRepository::Instance()->type(name);
203 lua_pushstring(L, name);
204 lua_pushlightuserdata(L, (
void*) ti);
215 static bool __typenames_cmp(lua_State *L,
const types::TypeInfo *ti1,
const char* type2)
217 const types::TypeInfo *ti2 = ti_lookup(L, type2);
223 static bool Variable_is_a(lua_State *L,
const types::TypeInfo *ti1,
const char* type)
225 const types::TypeInfo *ti2 = ti_lookup(L, type);
230 static bool __Variable_isbasic(lua_State *L, DataSourceBase::shared_ptr &dsb)
232 const types::TypeInfo *ti = dsb->getTypeInfo();
234 if ( Variable_is_a(L, ti,
"bool") ||
235 Variable_is_a(L, ti,
"double") ||
236 Variable_is_a(L, ti,
"float") ||
237 Variable_is_a(L, ti,
"uint") ||
238 Variable_is_a(L, ti,
"int") ||
239 Variable_is_a(L, ti,
"long") ||
240 Variable_is_a(L, ti,
"char") ||
241 Variable_is_a(L, ti,
"uint8") || Variable_is_a(L, ti,
"int8") ||
242 Variable_is_a(L, ti,
"uint16") || Variable_is_a(L, ti,
"int16") ||
243 Variable_is_a(L, ti,
"uint32") || Variable_is_a(L, ti,
"int32") ||
244 Variable_is_a(L, ti,
"uint64") || Variable_is_a(L, ti,
"int64") ||
245 Variable_is_a(L, ti,
"string") ||
246 Variable_is_a(L, ti,
"void"))
252 static int Variable_isbasic(lua_State *L)
254 DataSourceBase::shared_ptr dsb = *(luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr));
255 lua_pushboolean(L, __Variable_isbasic(L, dsb));
264 static int __Variable_tolua(lua_State *L, DataSourceBase::shared_ptr dsb)
266 DataSourceBase *ds = dsb.get();
267 const types::TypeInfo* ti = dsb->getTypeInfo();
270 if(Variable_is_a(L, ti,
"bool")) {
271 DataSource<bool>* dsb = DataSource<bool>::narrow(ds);
272 if(dsb) lua_pushboolean(L, dsb->get());
274 }
else if (Variable_is_a(L, ti,
"float")) {
275 DataSource<float>* dsb = DataSource<float>::narrow(ds);
276 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
278 }
else if (Variable_is_a(L, ti,
"double")) {
279 DataSource<double>* dsb = DataSource<double>::narrow(ds);
280 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
282 }
else if (Variable_is_a(L, ti,
"uint8")) {
283 DataSource<uint8_t>* dsb = DataSource<uint8_t>::narrow(ds);
284 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
286 }
else if (Variable_is_a(L, ti,
"int8")) {
287 DataSource<int8_t>* dsb = DataSource<int8_t>::narrow(ds);
288 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
290 }
else if (Variable_is_a(L, ti,
"uint16")) {
291 DataSource<uint16_t>* dsb = DataSource<uint16_t>::narrow(ds);
292 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
294 }
else if (Variable_is_a(L, ti,
"int16")) {
295 DataSource<int16_t>* dsb = DataSource<int16_t>::narrow(ds);
296 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
298 }
else if (Variable_is_a(L, ti,
"uint32")) {
299 DataSource<uint32_t>* dsb = DataSource<uint32_t>::narrow(ds);
300 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
302 }
else if (Variable_is_a(L, ti,
"int32")) {
303 DataSource<int32_t>* dsb = DataSource<int32_t>::narrow(ds);
304 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
306 }
else if (Variable_is_a(L, ti,
"uint64")) {
307 DataSource<uint64_t>* dsb = DataSource<uint64_t>::narrow(ds);
308 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
310 }
else if (Variable_is_a(L, ti,
"int64")) {
311 DataSource<int64_t>* dsb = DataSource<int64_t>::narrow(ds);
312 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
314 }
else if (Variable_is_a(L, ti,
"uint")) {
315 DataSource<unsigned int>* dsb = DataSource<unsigned int>::narrow(ds);
316 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
318 }
else if (Variable_is_a(L, ti,
"long")) {
319 DataSource<long>* dsb = DataSource<long>::narrow(ds);
320 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
322 }
else if (Variable_is_a(L, ti,
"int")) {
323 DataSource<int>* dsb = DataSource<int>::narrow(ds);
324 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
326 }
else if (Variable_is_a(L, ti,
"char")) {
327 DataSource<char>* dsb = DataSource<char>::narrow(ds);
329 if(dsb) lua_pushlstring(L, &c, 1);
331 }
else if (Variable_is_a(L, ti,
"string")) {
332 DataSource<std::string>* dsb = DataSource<std::string>::narrow(ds);
333 if(dsb) lua_pushlstring(L, dsb->get().c_str(), dsb->get().size());
335 }
else if (Variable_is_a(L, ti,
"void")) {
336 DataSource<void>* dsb = DataSource<void>::narrow(ds);
337 if(dsb) lua_pushnil(L);
347 luaL_error(L,
"Variable.tolua: can't convert type %s", dsb->getTypeName().c_str());
351 luaL_error(L,
"Variable.tolua: narrow failed for %s Variable", dsb->getTypeName().c_str());
355 static int Variable_tolua(lua_State *L)
357 DataSourceBase::shared_ptr dsb = *(luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr));
358 return __Variable_tolua(L, dsb);
366 static void Variable_coerce(lua_State *L, DataSourceBase::shared_ptr dsb)
368 if (__Variable_isbasic(L, dsb)) {
370 __Variable_tolua(L, dsb);
377 static void Variable_push_coerce(lua_State *L, DataSourceBase::shared_ptr dsb)
379 if (__Variable_isbasic(L, dsb))
380 __Variable_tolua(L, dsb);
382 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(dsb);
386 static int Variable_getTypes(lua_State *L)
388 push_vect_str(L, Types()->getTypes());
392 static int Variable_getMemberNames(lua_State *L)
394 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr);
395 push_vect_str(L, (*dsbp)->getMemberNames());
399 static int Variable_tolightuserdata(lua_State *L)
401 DataSourceBase::shared_ptr dsb = *(luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr));
402 lua_pushlightuserdata(L, dsb->getRawPointer());
411 static DataSourceBase::shared_ptr lookup_member(lua_State *L, DataSourceBase::shared_ptr parent,
const char* mem)
413 DataSourceBase *varptr;
414 DataSourceBase::shared_ptr *dsbp;
415 DataSourceBase::shared_ptr memdsb;
416 int top = lua_gettop(L);
418 varptr = parent.get();
420 lua_pushlightuserdata(L, (
void*) varptr);
421 lua_rawget(L, LUA_REGISTRYINDEX);
423 if(lua_type(L, -1) == LUA_TNIL)
426 lua_pushstring(L, mem);
429 if ((dsbp = luaM_testudata_mt(L, -1,
"Variable", DataSourceBase::shared_ptr)) != NULL) {
438 memdsb = parent->getMember(mem);
444 if(lua_type(L, -1) == LUA_TNIL) {
446 lua_pushlightuserdata(L, (
void*) varptr);
447 lua_pushvalue(L, -2);
448 lua_rawset(L, LUA_REGISTRYINDEX);
452 lua_pushstring(L, mem);
453 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(memdsb);
455 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(memdsb);
458 lua_replace(L, top+1);
459 lua_settop(L, top+1);
465 static void cache_clear(lua_State *L, DataSourceBase *varptr)
467 lua_pushlightuserdata(L, (
void*) varptr);
469 lua_rawset(L, LUA_REGISTRYINDEX);
472 static int Variable_getMember(lua_State *L)
474 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr);
475 DataSourceBase::shared_ptr memdsb;
476 const char *mem = luaL_checkstring(L, 2);
478 if ((memdsb = lookup_member(L, *dsbp, mem)) == 0)
479 luaL_error(L,
"Variable.getMember: indexing failed, no member %s", mem);
481 Variable_coerce(L, memdsb);
486 static int Variable_getMemberRaw(lua_State *L)
488 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr);
489 DataSourceBase::shared_ptr memdsb;
490 const char *mem = luaL_checkstring(L, 2);
492 if ((memdsb = lookup_member(L, (*dsbp), mem)) == 0)
493 luaL_error(L,
"Variable.getMemberRaw: indexing failed, no member %s", mem);
500 static int Variable_update(lua_State *L)
503 DataSourceBase::shared_ptr dsb;
504 DataSourceBase::shared_ptr *dsbp;
505 DataSourceBase::shared_ptr
self = *(luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr));
507 if ((dsbp = luaM_testudata_mt(L, 2,
"Variable", DataSourceBase::shared_ptr)) != NULL) {
509 ret =
self->update(dsb.get());
510 if (!ret) luaL_error(L,
"Variable.assign: assignment failed");
512 Variable_fromlua(L,
self, 2);
519 static int Variable_create(lua_State *L)
522 type = luaL_checkstring(L, 1);
524 if(!strcmp(type,
"void"))
525 luaL_error(L,
"Variable.new: can't create void variable");
527 TypeInfo* ti = Types()->type(type);
530 luaL_error(L,
"Variable.new: unknown type %s", type);
532 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(ti->buildValue());
536 #define CONVERT_TO_NUMBER(CTGT) \ 538 if (luatype == LUA_TNUMBER) x = lua_tonumber(L, valind); \ 539 else goto out_conv_err; \ 540 AssignableDataSource<CTGT> *ads = ValueDataSource<CTGT>::narrow(dsb.get()); \ 541 if (ads == NULL) luaL_error(L, "Variable_fromlua: failed to narrow target dsb to %s.", #CTGT ); \ 546 static void Variable_fromlua(lua_State *L, DataSourceBase::shared_ptr& dsb,
int valind)
548 const types::TypeInfo* ti = dsb->getTypeInfo();
550 luaL_checkany(L, valind);
551 int luatype = lua_type(L, valind);
553 if(__typenames_cmp(L, ti,
"bool")) {
555 if(luatype == LUA_TBOOLEAN)
556 x = (lua_Number) lua_toboolean(L, valind);
557 else if (luatype == LUA_TNUMBER)
558 x = lua_tonumber(L, valind);
562 AssignableDataSource<bool> *ads = ValueDataSource<bool>::narrow(dsb.get());
564 luaL_error(L,
"Variable_fromlua: failed to narrow target dsb to bool");
567 else if (__typenames_cmp(L, ti,
"uint")) { CONVERT_TO_NUMBER(
unsigned int); }
568 else if (__typenames_cmp(L, ti,
"int")) { CONVERT_TO_NUMBER(
int); }
569 else if (__typenames_cmp(L, ti,
"double")) { CONVERT_TO_NUMBER(
double); }
570 else if (__typenames_cmp(L, ti,
"long")) { CONVERT_TO_NUMBER(
double); }
571 else if (__typenames_cmp(L, ti,
"uint8")) { CONVERT_TO_NUMBER(uint8_t); }
572 else if (__typenames_cmp(L, ti,
"int8")) { CONVERT_TO_NUMBER(int8_t); }
573 else if (__typenames_cmp(L, ti,
"uint16")) { CONVERT_TO_NUMBER(uint16_t); }
574 else if (__typenames_cmp(L, ti,
"int16")) { CONVERT_TO_NUMBER(int16_t); }
575 else if (__typenames_cmp(L, ti,
"uint32")) { CONVERT_TO_NUMBER(uint32_t); }
576 else if (__typenames_cmp(L, ti,
"int32")) { CONVERT_TO_NUMBER(int32_t); }
577 else if (__typenames_cmp(L, ti,
"uint64")) { CONVERT_TO_NUMBER(uint64_t); }
578 else if (__typenames_cmp(L, ti,
"int64")) { CONVERT_TO_NUMBER(int64_t); }
579 else if (__typenames_cmp(L, ti,
"float")) { CONVERT_TO_NUMBER(
float); }
581 else if (__typenames_cmp(L, ti,
"char")) {
584 if (luatype == LUA_TSTRING) x = lua_tolstring(L, valind, &l);
585 else goto out_conv_err;
586 AssignableDataSource<char> *ads = ValueDataSource<char>::narrow(dsb.get());
587 if (ads == NULL) luaL_error(L,
"Variable_fromlua: failed to narrow target dsb to char");
588 ads->set((
char) x[0]);
590 }
else if (__typenames_cmp(L, ti,
"string")) {
592 if (luatype == LUA_TSTRING) x = lua_tostring(L, valind);
593 else goto out_conv_err;
594 AssignableDataSource<std::string> *ads = ValueDataSource<std::string>::narrow(dsb.get());
595 if (ads == NULL) luaL_error(L,
"Variable_fromlua: failed to narrow target dsb to std::string");
596 ads->set((std::string) x);
606 luaL_error(L,
"__lua_todsb: can't convert lua %s to %s variable",
607 lua_typename(L, luatype), ti->getTypeName().c_str());
613 static DataSourceBase::shared_ptr Variable_fromlua(lua_State *L,
const types::TypeInfo *ti,
int valind)
615 DataSourceBase::shared_ptr dsb = ti->buildValue();
616 Variable_fromlua(L, dsb, valind);
624 static DataSourceBase::shared_ptr Variable_fromlua(lua_State *L,
const char* type,
int valind)
626 const types::TypeInfo* ti = ti_lookup(L, type);
627 if(!ti) luaL_error(L,
"Variable_fromlua: %s is not a known type. Load typekit?", type);
628 return Variable_fromlua(L, ti, valind);
632 static int Variable_create_ival(lua_State *L,
int typeind,
int valind)
634 DataSourceBase::shared_ptr dsb;
635 luaL_checkany(L, valind);
636 const char* type = luaL_checkstring(L, typeind);
637 dsb = Variable_fromlua(L, type, valind);
638 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(dsb);
642 static int Variable_new(lua_State *L)
644 int argc = lua_gettop(L);
646 return Variable_create(L);
648 return Variable_create_ival(L, 1, 2);
650 luaL_error(L,
"Variable.new: invalid number of args");
655 static int Variable_toString(lua_State *L)
657 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr);
658 lua_pushstring(L, ((*dsbp)->toString()).c_str());
662 static int Variable_getType(lua_State *L)
664 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr);
665 lua_pushstring(L, (*dsbp)->getType().c_str());
669 static int Variable_getTypeIdName(lua_State *L)
671 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr);
672 lua_pushstring(L, (*dsbp)->getTypeInfo()->getTypeIdName());
676 static int Variable_getTypeName(lua_State *L)
678 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr);
679 lua_pushstring(L, (*dsbp)->getTypeName().c_str());
683 static int Variable_resize(lua_State *L)
686 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr);
687 size = luaL_checknumber(L, 2);
688 const TypeInfo *ti = (*dsbp)->getTypeInfo();
689 lua_pushboolean(L, ti->resize(*dsbp, size));
697 static int Variable_unm(lua_State *L)
699 types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance();
700 DataSourceBase::shared_ptr arg = *(luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr));
701 DataSourceBase::shared_ptr res = opreg->applyUnary(
"-", arg.get());
702 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(res);
708 #define gen_opmet(name, op) \ 709 static int name(lua_State *L) \ 711 DataSourceBase::shared_ptr arg1 = *(luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr)); \ 712 DataSourceBase::shared_ptr arg2 = *(luaM_checkudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)); \ 713 types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance(); \ 714 DataSourceBase *res = opreg->applyBinary(#op, arg1.get(), arg2.get()); \ 716 luaL_error(L , "%s (operator %s) failed", #name, #op); \ 718 luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(res); \ 722 gen_opmet(Variable_add, +)
723 gen_opmet(Variable_sub, -)
724 gen_opmet(Variable_mul, *)
725 gen_opmet(Variable_div, /)
726 gen_opmet(Variable_mod, %)
727 gen_opmet(Variable_pow, ^)
730 #define gen_opmet_bool(name, op) \ 731 static int name(lua_State *L) \ 733 DataSourceBase::shared_ptr arg1 = *(luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr)); \ 734 DataSourceBase::shared_ptr arg2 = *(luaM_checkudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)); \ 735 types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance(); \ 736 DataSourceBase *res = opreg->applyBinary(#op, arg1.get(), arg2.get()); \ 738 luaL_error(L , "%s (operator %s) failed", #name, #op); \ 740 return __Variable_tolua(L, res); \ 743 gen_opmet_bool(Variable_eq, ==)
744 gen_opmet_bool(Variable_lt, <)
745 gen_opmet_bool(Variable_le, <=)
747 static
int Variable_opBinary(lua_State *L)
749 types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance();
750 const char *op = luaL_checkstring(L, 1);
751 DataSourceBase::shared_ptr arg1 = *(luaM_checkudata_mt(L, 2,
"Variable", DataSourceBase::shared_ptr));
752 DataSourceBase::shared_ptr arg2 = *(luaM_checkudata_mt(L, 3,
"Variable", DataSourceBase::shared_ptr));
755 res = opreg->applyBinary(op, arg1.get(), arg2.get());
757 luaL_error(L ,
"Variable.opBinary '%s' not applicable to args", op);
761 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(res);
770 static int Variable_index(lua_State *L)
772 const char* key = luaL_checkstring(L, 2);
774 lua_getmetatable(L, 1);
775 lua_getfield(L, -1, key);
778 if(!lua_isnil(L, -1))
783 return Variable_getMember(L);
786 static int Variable_newindex(lua_State *L)
788 DataSourceBase::shared_ptr *newvalp;
789 DataSourceBase::shared_ptr newval;
790 DataSourceBase::shared_ptr parent = *(luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr));
791 const char* mem = luaL_checkstring(L, 2);
794 types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance();
795 DataSourceBase::shared_ptr curval;
797 if ((curval = lookup_member(L, parent, mem)) == 0)
798 luaL_error(L,
"Variable.newindex: indexing failed, no member %s", mem);
802 if ((newvalp = luaM_testudata_mt(L, 3,
"Variable", DataSourceBase::shared_ptr)) != NULL) {
804 if(!curval->update(newval.get())) {
805 luaL_error(L,
"Variable.newindex: failed to assign %s to member %s of type %s",
806 newval->getType().c_str(), mem, curval->getType().c_str());
809 Variable_fromlua(L, curval, 3);
823 int VariableGC(lua_State* L)
825 T* dsbp = (T*) lua_touserdata(L, 1);
826 cache_clear(L, dsbp->get());
827 reinterpret_cast<T*
>(dsbp)->~T();
832 static const struct luaL_Reg Variable_f [] = {
833 {
"new", Variable_new },
834 {
"tolua", Variable_tolua },
835 {
"isbasic", Variable_isbasic },
836 {
"toString", Variable_toString },
837 {
"getTypes", Variable_getTypes },
838 {
"getType", Variable_getType },
839 {
"getTypeName", Variable_getTypeName },
840 {
"getTypeIdName", Variable_getTypeIdName },
841 {
"getMemberNames", Variable_getMemberNames },
842 {
"getMember", Variable_getMember },
843 {
"getMemberRaw", Variable_getMemberRaw },
844 {
"tolud", Variable_tolightuserdata },
845 {
"resize", Variable_resize },
846 {
"opBinary", Variable_opBinary },
847 {
"assign", Variable_update },
848 {
"unm", Variable_unm },
849 {
"add", Variable_add },
850 {
"sub", Variable_sub },
851 {
"mul", Variable_mul },
852 {
"div", Variable_div },
853 {
"mod", Variable_mod },
854 {
"pow", Variable_pow },
855 {
"eq", Variable_eq },
856 {
"lt", Variable_lt },
857 {
"le", Variable_le },
861 static const struct luaL_Reg Variable_m [] = {
862 {
"tolua", Variable_tolua },
863 {
"isbasic", Variable_isbasic },
864 {
"toString", Variable_toString },
865 {
"getType", Variable_getType },
866 {
"getTypeName", Variable_getTypeName },
867 {
"getTypeIdName", Variable_getTypeIdName },
868 {
"getMemberNames", Variable_getMemberNames },
869 {
"getMember", Variable_getMember },
870 {
"getMemberRaw", Variable_getMemberRaw },
871 {
"tolud", Variable_tolightuserdata },
872 {
"resize", Variable_resize },
873 {
"opBinary", Variable_opBinary },
874 {
"assign", Variable_update },
875 {
"__unm", Variable_unm },
876 {
"__add", Variable_add },
877 {
"__sub", Variable_sub },
878 {
"__mul", Variable_mul },
879 {
"__div", Variable_div },
880 {
"__mod", Variable_mod },
881 {
"__pow", Variable_pow },
882 {
"__eq", Variable_eq },
883 {
"__lt", Variable_lt },
884 {
"__le", Variable_le },
885 {
"__index", Variable_index },
886 {
"__newindex", Variable_newindex },
889 {
"__gc", VariableGC<DataSourceBase::shared_ptr> },
898 gen_push_bxptr(Property_push,
"Property", PropertyBase)
900 static
int Property_new(lua_State *L)
902 const char *type, *name, *desc;
904 int argc = lua_gettop(L);
905 type = luaL_checkstring(L, 1);
908 name = (argc > 1) ? luaL_checkstring(L, 2) :
"";
909 desc = (argc > 2) ? luaL_checkstring(L, 3) :
"";
911 types::TypeInfo *ti = types::TypeInfoRepository::Instance()->type(type);
914 luaL_error(L,
"Property.new: unknown type %s", type);
916 pb = ti->buildProperty(name, desc);
917 Property_push(L, pb);
921 static int Property_get(lua_State *L)
923 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Property", PropertyBase));
924 Variable_push_coerce(L, pb->getDataSource());
928 static int Property_getRaw(lua_State *L)
930 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Property", PropertyBase));
931 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(pb->getDataSource());
935 static int Property_set(lua_State *L)
937 DataSourceBase::shared_ptr newdsb;
938 DataSourceBase::shared_ptr *newdsbp;
939 DataSourceBase::shared_ptr propdsb;
940 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Property", PropertyBase));
941 propdsb = pb->getDataSource();
944 if ((newdsbp = luaM_testudata_mt(L, 2,
"Variable", DataSourceBase::shared_ptr)) != NULL) {
946 if(!propdsb->update(newdsb.get()))
947 luaL_error(L,
"Property.set: failed to assign type %s to type %s",
948 newdsb->getType().c_str(), propdsb->getType().c_str());
950 Variable_fromlua(L, propdsb, 2);
955 static int Property_info(lua_State *L)
957 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Property", PropertyBase));
959 lua_pushstring(L,
"name"); lua_pushstring(L, pb->getName().c_str()); lua_rawset(L, -3);
960 lua_pushstring(L,
"desc"); lua_pushstring(L, pb->getDescription().c_str()); lua_rawset(L, -3);
961 lua_pushstring(L,
"type"); lua_pushstring(L, pb->getType().c_str()); lua_rawset(L, -3);
971 static int Property_gc(lua_State *L)
973 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Property", PropertyBase));
980 static int Property_del(lua_State *L)
982 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Property", PropertyBase));
986 luaL_getmetatable(L,
"__dead__");
987 lua_setmetatable(L, -2);
997 static int Property_index(lua_State *L)
999 const char* key = luaL_checkstring(L, 2);
1001 lua_getmetatable(L, 1);
1002 lua_getfield(L, -1, key);
1005 if(!lua_isnil(L, -1))
1011 return Variable_index(L);
1014 static int Property_newindex(lua_State *L)
1018 return Variable_newindex(L);
1021 static const struct luaL_Reg Property_f [] = {
1022 {
"new", Property_new },
1023 {
"get", Property_get },
1024 {
"getRaw", Property_getRaw },
1025 {
"set", Property_set },
1026 {
"info", Property_info },
1027 {
"delete", Property_del },
1031 static const struct luaL_Reg Property_m [] = {
1032 {
"get", Property_get },
1033 {
"getRaw", Property_getRaw },
1034 {
"set", Property_set },
1035 {
"info", Property_info },
1037 {
"delete", Property_del },
1038 {
"__index", Property_index },
1039 {
"__newindex", Property_newindex },
1047 gen_push_bxptr(Attribute_push,
"Attribute", AttributeBase)
1049 static
int Attribute_new(lua_State *L)
1051 const char *type, *name;
1053 int argc = lua_gettop(L);
1054 type = luaL_checkstring(L, 1);
1057 name = (argc > 1) ? luaL_checkstring(L, 2) :
"";
1059 types::TypeInfo *ti = types::TypeInfoRepository::Instance()->type(type);
1062 luaL_error(L,
"Attribute.new: unknown type %s", type);
1064 pb = ti->buildAttribute(name);
1065 Attribute_push(L, pb);
1069 static int Attribute_get(lua_State *L)
1071 AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Attribute", AttributeBase));
1072 Variable_push_coerce(L, pb->getDataSource());
1076 static int Attribute_getRaw(lua_State *L)
1078 AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Attribute", AttributeBase));
1079 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(pb->getDataSource());
1083 static int Attribute_set(lua_State *L)
1085 DataSourceBase::shared_ptr newdsb;
1086 DataSourceBase::shared_ptr *newdsbp;
1087 DataSourceBase::shared_ptr propdsb;
1088 AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Attribute", AttributeBase));
1089 propdsb = pb->getDataSource();
1092 if ((newdsbp = luaM_testudata_mt(L, 2,
"Variable", DataSourceBase::shared_ptr)) != NULL) {
1094 if(!propdsb->update(newdsb.get()))
1095 luaL_error(L,
"Attribute.set: failed to assign type %s to type %s",
1096 newdsb->getType().c_str(), propdsb->getType().c_str());
1098 Variable_fromlua(L, propdsb, 2);
1103 static int Attribute_info(lua_State *L)
1105 AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Attribute", AttributeBase));
1107 lua_pushstring(L,
"name"); lua_pushstring(L, pb->getName().c_str()); lua_rawset(L, -3);
1108 lua_pushstring(L,
"type"); lua_pushstring(L, pb->getDataSource()->getType().c_str()); lua_rawset(L, -3);
1118 static int Attribute_gc(lua_State *L)
1120 AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Attribute", AttributeBase));
1128 static int Attribute_del(lua_State *L)
1130 AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Attribute", AttributeBase));
1134 luaL_getmetatable(L,
"__dead__");
1135 lua_setmetatable(L, -2);
1145 static int Attribute_index(lua_State *L)
1147 const char* key = luaL_checkstring(L, 2);
1149 lua_getmetatable(L, 1);
1150 lua_getfield(L, -1, key);
1153 if(!lua_isnil(L, -1))
1159 return Variable_index(L);
1162 static int Attribute_newindex(lua_State *L)
1166 return Variable_newindex(L);
1169 static const struct luaL_Reg Attribute_f [] = {
1170 {
"new", Attribute_new },
1171 {
"get", Attribute_get },
1172 {
"getRaw", Attribute_getRaw },
1173 {
"set", Attribute_set },
1174 {
"info", Attribute_info },
1175 {
"delete", Attribute_del },
1179 static const struct luaL_Reg Attribute_m [] = {
1180 {
"get", Attribute_get },
1181 {
"getRaw", Attribute_getRaw },
1182 {
"set", Attribute_set },
1183 {
"info", Attribute_info },
1185 {
"delete", Attribute_del },
1186 {
"__index", Attribute_index },
1187 {
"__newindex", Attribute_newindex },
1196 static int Port_info(lua_State *L)
1199 const char* port_type = NULL;
1200 PortInterface **pip;
1201 PortInterface *pi = NULL;
1203 if((pip = (PortInterface**) luaL_testudata(L, 1,
"InputPort")) != NULL) {
1206 }
else if((pip = (PortInterface**) luaL_testudata(L, 1,
"OutputPort")) != NULL) {
1211 arg_type = lua_type(L, 1);
1212 luaL_error(L,
"Port.info: invalid argument, expected Port, got %s",
1213 lua_typename(L, arg_type));
1217 lua_pushstring(L,
"name"); lua_pushstring(L, pi->getName().c_str()); lua_rawset(L, -3);
1218 lua_pushstring(L,
"desc"); lua_pushstring(L, pi->getDescription().c_str()); lua_rawset(L, -3);
1219 lua_pushstring(L,
"connected"); lua_pushboolean(L, pi->connected()); lua_rawset(L, -3);
1220 lua_pushstring(L,
"isLocal"); lua_pushboolean(L, pi->isLocal()); lua_rawset(L, -3);
1221 lua_pushstring(L,
"type"); lua_pushstring(L, pi->getTypeInfo()->getTypeName().c_str()); lua_rawset(L, -3);
1222 lua_pushstring(L,
"porttype"); lua_pushstring(L, port_type); lua_rawset(L, -3);
1227 static int Port_connect(lua_State *L)
1230 PortInterface **pip1, **pip2;
1231 PortInterface *pi1 = NULL;
1232 PortInterface *pi2 = NULL;
1234 ConnPolicy *cp = NULL;
1236 if((pip1 = (PortInterface**) luaL_testudata(L, 1,
"InputPort")) != NULL) {
1238 }
else if((pip1 = (PortInterface**) luaL_testudata(L, 1,
"OutputPort")) != NULL) {
1242 arg_type = lua_type(L, 1);
1243 luaL_error(L,
"Port.info: invalid argument 1, expected Port, got %s",
1244 lua_typename(L, arg_type));
1246 if((pip2 = (PortInterface**) luaL_testudata(L, 2,
"InputPort")) != NULL) {
1248 }
else if((pip2 = (PortInterface**) luaL_testudata(L, 2,
"OutputPort")) != NULL) {
1252 arg_type = lua_type(L, 2);
1253 luaL_error(L,
"Port.connect: invalid argument 2, expected Port, got %s",
1254 lua_typename(L, arg_type));
1257 if((cpp = (ConnPolicy**) luaL_testudata(L, 3,
"ConnPolicy")) != NULL) {
1262 ret = pi1->connectTo(pi2, *cp);
1264 ret = pi1->connectTo(pi2);
1266 lua_pushboolean(L, ret);
1271 static int Port_disconnect(lua_State *L)
1274 PortInterface **pip1, **pip2;
1275 PortInterface *pi1 = NULL;
1276 PortInterface *pi2 = NULL;
1278 if((pip1 = (PortInterface**) luaL_testudata(L, 1,
"InputPort")) != NULL) {
1280 }
else if((pip1 = (PortInterface**) luaL_testudata(L, 1,
"OutputPort")) != NULL) {
1284 arg_type = lua_type(L, 1);
1285 luaL_error(L,
"Port.info: invalid argument 1, expected Port, got %s",
1286 lua_typename(L, arg_type));
1288 if((pip2 = (PortInterface**) luaL_testudata(L, 2,
"InputPort")) != NULL) {
1290 }
else if((pip2 = (PortInterface**) luaL_testudata(L, 2,
"OutputPort")) != NULL) {
1295 ret = pi1->disconnect(pi2);
1300 lua_pushboolean(L, ret);
1309 gen_push_bxptr(InputPort_push,
"InputPort", InputPortInterface)
1311 static
int InputPort_new(lua_State *L)
1313 const char *type, *name, *desc;
1314 InputPortInterface* ipi;
1315 int argc = lua_gettop(L);
1317 type = luaL_checkstring(L, 1);
1320 name = (argc > 1) ? luaL_checkstring(L, 2) :
"";
1321 desc = (argc > 2) ? luaL_checkstring(L, 3) :
"";
1323 types::TypeInfo *ti = types::TypeInfoRepository::Instance()->type(type);
1325 luaL_error(L,
"InputPort.new: unknown type %s", type);
1327 ipi = ti->inputPort(name);
1330 luaL_error(L,
"InputPort.new: creating port of type %s failed", type);
1333 InputPort_push(L, ipi);
1337 static int InputPort_read(lua_State *L)
1340 InputPortInterface *ip = *(luaM_checkudata_mt_bx(L, 1,
"InputPort", InputPortInterface));
1341 DataSourceBase::shared_ptr dsb;
1342 DataSourceBase::shared_ptr *dsbp;
1346 if ((dsbp = luaM_testudata_mt(L, 2,
"Variable", DataSourceBase::shared_ptr)) != NULL)
1349 dsb = ip->getTypeInfo()->buildValue();
1355 if(fs == NoData) lua_pushstring(L,
"NoData");
1356 else if (fs == NewData) lua_pushstring(L,
"NewData");
1357 else if (fs == OldData) lua_pushstring(L,
"OldData");
1358 else luaL_error(L,
"InputPort.read: unknown FlowStatus returned");
1361 Variable_push_coerce(L, dsb);
1367 static int InputPort_gc(lua_State *L)
1369 InputPortInterface *ip = *(luaM_checkudata_mt_bx(L, 1,
"InputPort", InputPortInterface));
1376 static int InputPort_del(lua_State *L)
1378 InputPortInterface *ip = *(luaM_checkudata_mt_bx(L, 1,
"InputPort", InputPortInterface));
1382 luaL_getmetatable(L,
"__dead__");
1383 lua_setmetatable(L, -2);
1387 static const struct luaL_Reg InputPort_f [] = {
1388 {
"new", InputPort_new },
1389 {
"read", InputPort_read },
1390 {
"info", Port_info },
1391 {
"connect", Port_connect },
1392 {
"disconnect", Port_disconnect },
1393 {
"delete", InputPort_del },
1397 static const struct luaL_Reg InputPort_m [] = {
1398 {
"read", InputPort_read },
1399 {
"info", Port_info },
1400 {
"delete", InputPort_del },
1401 {
"connect", Port_connect },
1402 {
"disconnect", Port_disconnect },
1409 gen_push_bxptr(OutputPort_push,
"OutputPort", OutputPortInterface)
1412 static
int OutputPort_new(lua_State *L)
1414 const char *type, *name, *desc;
1415 OutputPortInterface* opi;
1416 int argc = lua_gettop(L);
1418 type = luaL_checkstring(L, 1);
1421 name = (argc > 1) ? luaL_checkstring(L, 2) :
"";
1422 desc = (argc > 2) ? luaL_checkstring(L, 3) :
"";
1424 types::TypeInfo *ti = types::TypeInfoRepository::Instance()->type(type);
1427 luaL_error(L,
"OutputPort.new: unknown type %s", type);
1429 opi = ti->outputPort(name);
1432 luaL_error(L,
"OutputPort.new: creating port of type %s failed", type);
1435 OutputPort_push(L, opi);
1439 static int OutputPort_write(lua_State *L)
1441 DataSourceBase::shared_ptr dsb;
1442 DataSourceBase::shared_ptr *dsbp;
1444 OutputPortInterface *op = *(luaM_checkudata_mt_bx(L, 1,
"OutputPort", OutputPortInterface));
1447 if ((dsbp = luaM_testudata_mt(L, 2,
"Variable", DataSourceBase::shared_ptr)) != NULL) {
1451 dsb = Variable_fromlua(L, op->getTypeInfo(), 2);
1458 static int OutputPort_gc(lua_State *L)
1460 OutputPortInterface *op = *(luaM_checkudata_mt_bx(L, 1,
"OutputPort", OutputPortInterface));
1467 static int OutputPort_del(lua_State *L)
1469 OutputPortInterface *op = *(luaM_checkudata_mt_bx(L, 1,
"OutputPort", OutputPortInterface));
1473 luaL_getmetatable(L,
"__dead__");
1474 lua_setmetatable(L, -2);
1478 static const struct luaL_Reg OutputPort_f [] = {
1479 {
"new", OutputPort_new },
1480 {
"write", OutputPort_write },
1481 {
"info", Port_info },
1482 {
"connect", Port_connect },
1483 {
"disconnect", Port_disconnect },
1484 {
"delete", OutputPort_del },
1488 static const struct luaL_Reg OutputPort_m [] = {
1489 {
"write", OutputPort_write },
1490 {
"info", Port_info },
1491 {
"connect", Port_connect },
1492 {
"disconnect", Port_disconnect },
1493 {
"delete", OutputPort_del },
1503 OperationInterfacePart *oip;
1504 OperationCallerC *occ;
1513 std::vector<base::DataSourceBase::shared_ptr> dsb_store;
1514 std::vector<internal::Reference*> args;
1515 base::DataSourceBase::shared_ptr call_dsb;
1516 base::DataSourceBase::shared_ptr ret_dsb;
1519 template<
typename T>
1520 int OperationGC(lua_State* L)
1522 T* oh = (T*) lua_touserdata(L, 1);
1524 reinterpret_cast<T*
>(lua_touserdata(L, 1))->~T();
1528 static int Operation_info(lua_State *L)
1531 std::vector<ArgumentDescription> args;
1534 lua_pushstring(L, op->oip->getName().c_str());
1535 lua_pushstring(L, op->oip->description().c_str());
1536 lua_pushstring(L, op->oip->resultType().c_str());
1537 lua_pushinteger(L, op->arity);
1539 args = op->oip->getArgumentList();
1543 for (std::vector<ArgumentDescription>::iterator it = args.begin(); it != args.end(); it++) {
1545 lua_pushstring(L,
"name"); lua_pushstring(L, it->name.c_str()); lua_rawset(L, -3);
1546 lua_pushstring(L,
"type"); lua_pushstring(L, it->type.c_str()); lua_rawset(L, -3);
1547 lua_pushstring(L,
"desc"); lua_pushstring(L, it->description.c_str()); lua_rawset(L, -3);
1548 lua_rawseti(L, -2, i++);
1553 static int __Operation_call(lua_State *L)
1556 DataSourceBase::shared_ptr dsb, *dsbp;
1559 OperationInterfacePart *oip = oh->oip;
1560 unsigned int argc = lua_gettop(L);
1562 if(oh->arity != argc-1)
1563 luaL_error(L,
"Operation.call: wrong number of args. expected %d, got %d", oh->arity, argc-1);
1566 for(
unsigned int arg=2; arg<=argc; arg++) {
1568 if ((dsbp = luaM_testudata_mt(L, arg,
"Variable", DataSourceBase::shared_ptr)) != NULL) {
1572 dsb = Variable_fromlua(L, oip->getArgumentType(arg-1), arg);
1575 oh->dsb_store.push_back(dsb);
1577 if(!dsb->isAssignable())
1578 luaL_error(L,
"Operation.call: argument %d is not assignable.", arg-1);
1580 ret = oh->args[arg-2]->setReference(dsb);
1582 luaL_error(L,
"Operation_call: setReference failed, wrong type of argument?");
1585 if(!oh->occ->call())
1586 luaL_error(L,
"Operation.call: call failed.");
1588 oh->dsb_store.clear();
1591 Variable_push_coerce(L, oh->ret_dsb);
1597 static int __Operation_send(lua_State *L)
1599 DataSourceBase::shared_ptr dsb, *dsbp;
1602 OperationInterfacePart *oip = oh->oip;
1603 unsigned int argc = lua_gettop(L);
1605 if(oh->arity != argc-1)
1606 luaL_error(L,
"Operation.send: wrong number of args. expected %d, got %d", oh->arity, argc-1);
1609 for(
unsigned int arg=2; arg<=argc; arg++) {
1611 if ((dsbp = luaM_testudata_mt(L, arg,
"Variable", DataSourceBase::shared_ptr)) != NULL) {
1615 dsb = Variable_fromlua(L, oip->getArgumentType(arg-1), arg);
1618 oh->dsb_store.push_back(dsb);
1620 oh->args[arg-2]->setReference(dsb);
1623 luaM_pushobject_mt(L,
"SendHandle", SendHandleC)(oh->occ->send());
1627 static int Operation_call(lua_State *L)
1631 ret = __Operation_call(L);
1632 }
catch(
const std::exception &exc) {
1633 luaL_error(L,
"Operation.call: caught exception '%s'", exc.what());
1635 luaL_error(L,
"Operation.call: caught unknown exception");
1640 static int Operation_send(lua_State *L)
1644 ret = __Operation_send(L);
1645 }
catch(
const std::exception &exc) {
1646 luaL_error(L,
"Operation.send: caught exception '%s'", exc.what());
1648 luaL_error(L,
"Operation.send: caught unknown exception");
1654 static const struct luaL_Reg Operation_f [] = {
1655 {
"info", Operation_info },
1656 {
"call", Operation_call },
1657 {
"send", Operation_send },
1662 static const struct luaL_Reg Operation_m [] = {
1663 {
"info", Operation_info },
1664 {
"send", Operation_send },
1665 {
"__call", Operation_call },
1666 {
"__gc", OperationGC<OperationHandle> },
1674 static int Service_getName(lua_State *L)
1676 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1677 lua_pushstring(L, srv->getName().c_str());
1681 static int Service_doc(lua_State *L)
1685 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1686 if(lua_gettop(L) == 1) {
1687 lua_pushstring(L, srv->doc().c_str());
1690 doc = luaL_checkstring(L, 2);
1698 static int Service_getProviderNames(lua_State *L)
1700 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1701 push_vect_str(L, srv->getProviderNames());
1705 static int Service_getOperationNames(lua_State *L)
1707 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1708 push_vect_str(L, srv->getOperationNames());
1713 static int Service_hasOperation(lua_State *L)
1716 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1717 const char* op = luaL_checkstring(L, 2);
1718 ret = srv->hasOperation(op);
1719 lua_pushboolean(L, ret);
1723 static int Service_getPortNames(lua_State *L)
1725 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1726 push_vect_str(L, srv->getPortNames());
1730 static int Service_provides(lua_State *L)
1733 const char* subsrv_str;
1734 Service::shared_ptr srv, subsrv;
1736 srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1745 for(i=2; i<=argc; i++) {
1746 subsrv_str = luaL_checkstring(L, i);
1747 subsrv = srv->getService(subsrv_str);
1749 luaL_error(L,
"Service.provides: no subservice %s of service %s",
1750 subsrv_str, srv->getName().c_str() );
1752 luaM_pushobject_mt(L,
"Service", Service::shared_ptr)(subsrv);
1760 static int Service_getOperation(lua_State *L)
1763 OperationInterfacePart *oip;
1764 Service::shared_ptr srv;
1765 DataSourceBase::shared_ptr dsb;
1766 const types::TypeInfo *ti;
1768 TaskContext *this_tc;
1770 srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1771 op_str = luaL_checkstring(L, 2);
1772 oip = srv->getOperation(op_str);
1775 luaL_error(L,
"Service_getOperation: service %s has no operation %s",
1776 srv->getName().c_str(), op_str);
1780 oh->arity = oip->arity();
1781 oh->args.reserve(oh->arity);
1782 this_tc = __getTC(L);
1784 oh->occ =
new OperationCallerC(oip, op_str, this_tc->engine());
1789 for(
unsigned int arg=1; arg <= oh->arity; arg++) {
1790 std::string type = oip->getArgumentType(arg)->getTypeName();
1791 ti = types::TypeInfoRepository::Instance()->type(type);
1793 luaL_error(L,
"Operation.call: '%s', failed to locate TypeInfo for arg %d of type '%s'",
1794 op_str, arg, type.c_str());
1796 dsb = ti->buildReference((
void*) 0xdeadbeef);
1798 luaL_error(L,
"Operation.call: '%s', failed to build DSB for arg %d of type '%s'",
1799 op_str, arg, type.c_str());
1801 oh->args.push_back(dynamic_cast<internal::Reference*>(dsb.get()));
1806 if(oip->resultType() !=
"void"){
1807 ti = oip->getArgumentType(0);
1809 luaL_error(L,
"Operation.call: '%s', failed to locate TypeInfo for return value of type '%s'",
1810 op_str, oip->resultType().c_str());
1811 oh->ret_dsb=ti->buildValue();
1813 luaL_error(L,
"Operation.call: '%s', failed to build DSB for return value of type '%s'",
1814 op_str, oip->resultType().c_str());
1816 oh->occ->ret(oh->ret_dsb);
1822 if(!oh->occ->ready())
1823 luaL_error(L,
"Service.getOperation: OperationCallerC not ready!");
1828 static int Service_getPort(lua_State *L)
1832 InputPortInterface *ipi;
1833 OutputPortInterface *opi;
1835 Service::shared_ptr srv;
1837 srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1838 name = luaL_checkstring(L, 2);
1840 pi = srv->getPort(name);
1842 luaL_error(L,
"Service.getPort: service %s has no port %",
1843 srv->getName().c_str(), name);
1846 if ((ipi = dynamic_cast<InputPortInterface *> (pi)) != NULL)
1847 InputPort_push(L, ipi);
1848 else if ((opi = dynamic_cast<OutputPortInterface *> (pi)) != NULL)
1849 OutputPort_push(L, opi);
1851 luaL_error(L,
"Service.getPort: unknown port type returned");
1856 static int Service_getProperty(lua_State *L)
1861 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1862 name = luaL_checkstring(L, 2);
1864 prop = srv->getProperty(name);
1867 luaL_error(L,
"%s failed. No such property", __FILE__);
1869 Property_push(L, prop);
1873 static int Service_getPropertyNames(lua_State *L)
1875 Service::shared_ptr srv;
1876 srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1877 std::vector<std::string> plist = srv->properties()->list();
1878 push_vect_str(L, plist);
1882 static int Service_getProperties(lua_State *L)
1884 Service::shared_ptr srv;
1885 srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1886 vector<PropertyBase*> props = srv->properties()->getProperties();
1889 lua_createtable(L, props.size(), 0);
1890 for(vector<PropertyBase*>::iterator it = props.begin(); it != props.end(); ++it) {
1891 Property_push(L, *it);
1892 lua_rawseti(L, -2, key++);
1898 static int Service_getAttribute(lua_State *L)
1901 AttributeBase *prop;
1903 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1904 name = luaL_checkstring(L, 2);
1906 prop = srv->getAttribute(name);
1909 luaL_error(L,
"%s failed. No such Attribute", __FILE__);
1911 Attribute_push(L, prop);
1915 static int Service_getAttributeNames(lua_State *L)
1917 Service::shared_ptr srv;
1918 srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1919 std::vector<std::string> plist = srv->getAttributeNames();
1920 push_vect_str(L, plist);
1924 static int Service_getAttributes(lua_State *L)
1926 Service::shared_ptr srv;
1927 srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1928 vector<AttributeBase*> props = srv->getValues();
1931 lua_createtable(L, props.size(), 0);
1932 for(vector<AttributeBase*>::iterator it = props.begin(); it != props.end(); ++it) {
1933 Attribute_push(L, *it);
1934 lua_rawseti(L, -2, key++);
1940 static const struct luaL_Reg Service_f [] = {
1941 {
"getName", Service_getName },
1942 {
"doc", Service_doc },
1943 {
"getProviderNames", Service_getProviderNames },
1944 {
"getOperationNames", Service_getOperationNames },
1945 {
"hasOperation", Service_hasOperation },
1946 {
"getPortNames", Service_getPortNames },
1947 {
"provides", Service_provides },
1948 {
"getOperation", Service_getOperation },
1949 {
"getPort", Service_getPort },
1950 {
"getProperty", Service_getProperty },
1951 {
"getProperties", Service_getProperties },
1952 {
"getPropertyNames", Service_getPropertyNames },
1953 {
"getAttribute", Service_getAttribute },
1954 {
"getAttributes", Service_getAttributes },
1955 {
"getAttributeNames", Service_getAttributeNames },
1959 static const struct luaL_Reg Service_m [] = {
1960 {
"getName", Service_getName },
1961 {
"doc", Service_doc },
1962 {
"getProviderNames", Service_getProviderNames },
1963 {
"getOperationNames", Service_getOperationNames },
1964 {
"hasOperation", Service_hasOperation },
1965 {
"getPortNames", Service_getPortNames },
1966 {
"provides", Service_provides },
1967 {
"getOperation", Service_getOperation },
1968 {
"getPort", Service_getPort },
1969 {
"getProperty", Service_getProperty },
1970 {
"getProperties", Service_getProperties },
1971 {
"getPropertyNames", Service_getPropertyNames },
1972 {
"getAttribute", Service_getAttribute },
1973 {
"getAttributes", Service_getAttributes },
1974 {
"getAttributeNames", Service_getAttributeNames },
1975 {
"__gc", GCMethod<Service::shared_ptr> },
1983 gen_push_bxptr(ServiceRequester_push,
"ServiceRequester", ServiceRequester)
1985 static int ServiceRequester_getRequestName(lua_State *L)
1987 ServiceRequester *sr;
1989 sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
1990 lua_pushstring(L, sr->getRequestName().c_str());
1994 static int ServiceRequester_getRequesterNames(lua_State *L)
1996 ServiceRequester *sr;
1997 sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
1998 push_vect_str(L, sr->getRequesterNames());
2002 static int ServiceRequester_ready(lua_State *L)
2005 ServiceRequester *sr;
2006 sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
2008 lua_pushboolean(L, ret);
2012 static int ServiceRequester_disconnect(lua_State *L)
2014 ServiceRequester *sr;
2015 sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
2020 static int ServiceRequester_requires(lua_State *L)
2023 const char* subsr_str;
2024 ServiceRequester *sr;
2025 ServiceRequester::shared_ptr subsr;
2027 sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
2028 argc = lua_gettop(L);
2036 for(i=2; i<=argc; i++) {
2037 subsr_str = luaL_checkstring(L, i);
2038 subsr = sr->requires(subsr_str);
2040 luaL_error(L,
"ServiceRequester: no required subservice %s of service %s",
2041 subsr_str, sr->getRequestName().c_str());
2043 ServiceRequester_push(L, subsr.get());
2051 static const struct luaL_Reg ServiceRequester_f [] = {
2052 {
"getRequestName", ServiceRequester_getRequestName },
2053 {
"getRequesterNames", ServiceRequester_getRequesterNames },
2054 {
"ready", ServiceRequester_ready },
2055 {
"disconnect", ServiceRequester_disconnect },
2056 {
"requires", ServiceRequester_requires },
2060 static const struct luaL_Reg ServiceRequester_m [] = {
2061 {
"getRequestName", ServiceRequester_getRequestName },
2062 {
"getRequesterNames", ServiceRequester_getRequesterNames },
2063 {
"ready", ServiceRequester_ready },
2064 {
"disconnect", ServiceRequester_disconnect },
2065 {
"requires", ServiceRequester_requires },
2074 gen_push_bxptr(TaskContext_push,
"TaskContext", TaskContext)
2076 static int TaskContext_getName(lua_State *L)
2079 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2080 s = tc->getName().c_str();
2081 lua_pushstring(L, s);
2085 static int TaskContext_start(lua_State *L)
2087 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2088 bool b = tc->start();
2089 lua_pushboolean(L, b);
2093 static int TaskContext_stop(lua_State *L)
2095 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2096 bool b = tc->stop();
2097 lua_pushboolean(L, b);
2101 static int TaskContext_configure(lua_State *L)
2103 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2104 bool ret = tc->configure();
2105 lua_pushboolean(L, ret);
2109 static int TaskContext_activate(lua_State *L)
2111 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2112 bool ret = tc->activate();
2113 lua_pushboolean(L, ret);
2117 static int TaskContext_cleanup(lua_State *L)
2119 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2120 bool ret = tc->cleanup();
2121 lua_pushboolean(L, ret);
2125 static int TaskContext_error(lua_State *L)
2127 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2132 static int TaskContext_recover(lua_State *L)
2134 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2135 bool ret = tc->recover();
2136 lua_pushboolean(L, ret);
2140 static int TaskContext_getState(lua_State *L)
2142 TaskCore::TaskState ts;
2143 TaskContext **tc = (TaskContext**) luaM_checkudata_bx(L, 1, TaskContext);
2144 ts = (*tc)->getTaskState();
2147 case TaskCore::Init: lua_pushstring(L,
"Init");
break;
2148 case TaskCore::PreOperational: lua_pushstring(L,
"PreOperational");
break;
2149 case TaskCore::FatalError: lua_pushstring(L,
"FatalError");
break;
2150 case TaskCore::Exception: lua_pushstring(L,
"Exception");
break;
2151 case TaskCore::Stopped: lua_pushstring(L,
"Stopped");
break;
2152 case TaskCore::Running: lua_pushstring(L,
"Running");
break;
2153 case TaskCore::RunTimeError: lua_pushstring(L,
"RunTimeError");
break;
2154 default: lua_pushstring(L,
"unknown");
2161 static int TaskContext_getPeers(lua_State *L)
2163 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2164 std::vector<std::string> plist = tc->getPeerList();
2165 push_vect_str(L, plist);
2170 static int TaskContext_addPeer(lua_State *L)
2173 TaskContext *
self = *(luaM_checkudata_bx(L, 1, TaskContext));
2174 TaskContext *peer = *(luaM_checkudata_bx(L, 2, TaskContext));
2175 ret =
self->addPeer(peer);
2176 lua_pushboolean(L, ret);
2181 static int TaskContext_connectPeers(lua_State *L)
2184 TaskContext *
self = *(luaM_checkudata_bx(L, 1, TaskContext));
2185 TaskContext *peer = *(luaM_checkudata_bx(L, 2, TaskContext));
2186 ret =
self->connectPeers(peer);
2187 lua_pushboolean(L, ret);
2192 static int TaskContext_removePeer(lua_State *L)
2195 TaskContext *
self = *(luaM_checkudata_bx(L, 1, TaskContext));
2196 peer = luaL_checkstring(L, 2);
2197 self->removePeer(peer);
2202 static int TaskContext_getPeer(lua_State *L)
2204 std::string strpeer;
2206 TaskContext *
self = *(luaM_checkudata_bx(L, 1, TaskContext));
2207 strpeer = luaL_checkstring(L, 2);
2208 peer =
self->getPeer(strpeer);
2211 luaL_error(L,
"TaskContext.getPeer: no peer %s", strpeer.c_str());
2215 TaskContext_push(L, peer);
2220 static int TaskContext_getPortNames(lua_State *L)
2222 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2223 std::vector<std::string> plist = tc->ports()->getPortNames();
2224 push_vect_str(L, plist);
2228 static int TaskContext_addPort(lua_State *L)
2230 const char* name, *desc;
2232 int argc = lua_gettop(L);
2233 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2235 pi = (PortInterface**) luaL_testudata(L, 2,
"InputPort");
2236 if(pi)
goto check_name;
2238 pi = (PortInterface**) luaL_testudata(L, 2,
"OutputPort");
2239 if(pi)
goto check_name;
2241 return luaL_error(L,
"addPort: invalid argument, not a Port");
2245 name = luaL_checkstring(L, 3);
2246 (*pi)->setName(name);
2250 desc = luaL_checkstring(L, 4);
2254 tc->ports()->addPort(**pi);
2258 static int TaskContext_addEventPort(lua_State *L)
2260 const char* name, *desc;
2261 InputPortInterface **ipi;
2262 int argc = lua_gettop(L);
2263 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2265 if((ipi = (InputPortInterface**) luaL_testudata(L, 2,
"InputPort")) == NULL)
2266 return luaL_error(L,
"addEventPort: invalid argument, not an InputPort");
2269 name = luaL_checkstring(L, 3);
2270 (*ipi)->setName(name);
2274 desc = luaL_checkstring(L, 4);
2278 tc->ports()->addEventPort(**ipi);
2282 static int TaskContext_getPort(lua_State *L)
2286 InputPortInterface *ipi;
2287 OutputPortInterface *opi;
2289 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2290 name = luaL_checkstring(L, 2);
2292 pi = tc->getPort(name);
2294 luaL_error(L,
"TaskContext.getPort: no port %s for taskcontext %s",
2295 name, tc->getName().c_str());
2298 if ((ipi = dynamic_cast<InputPortInterface *> (pi)) != NULL)
2299 InputPort_push(L, ipi);
2300 else if ((opi = dynamic_cast<OutputPortInterface *> (pi)) != NULL)
2301 OutputPort_push(L, opi);
2303 luaL_error(L,
"TaskContext.getPort: unknown port returned");
2308 static int TaskContext_removePort(lua_State *L)
2310 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2311 const char *port = luaL_checkstring(L, 2);
2312 tc->ports()->removePort(port);
2316 static int TaskContext_addProperty(lua_State *L)
2318 const char *name, *desc;
2319 int argc = lua_gettop(L);
2320 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2321 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 2,
"Property", PropertyBase));
2324 name = luaL_checkstring(L, 3);
2329 desc = luaL_checkstring(L, 4);
2330 pb->setDescription(desc);
2334 if(!tc->addProperty(*pb))
2335 luaL_error(L,
"TaskContext.addProperty: failed to add property %s.",
2336 pb->getName().c_str());
2341 static int TaskContext_getProperty(lua_State *L)
2346 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2347 name = luaL_checkstring(L, 2);
2349 prop = tc->getProperty(name);
2352 luaL_error(L,
"%s failed. No such property", __FILE__);
2354 Property_push(L, prop);
2359 static int TaskContext_getPropertyNames(lua_State *L)
2361 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2362 std::vector<std::string> plist = tc->properties()->list();
2363 push_vect_str(L, plist);
2367 static int TaskContext_getProperties(lua_State *L)
2369 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2370 vector<PropertyBase*> props = tc->properties()->getProperties();
2373 lua_createtable(L, props.size(), 0);
2374 for(vector<PropertyBase*>::iterator it = props.begin(); it != props.end(); ++it) {
2375 Property_push(L, *it);
2376 lua_rawseti(L, -2, key++);
2382 static int TaskContext_removeProperty(lua_State *L)
2387 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2388 name = luaL_checkstring(L, 2);
2390 prop = tc->getProperty(name);
2393 luaL_error(L,
"%s failed. No such property", __FILE__);
2395 tc->properties()->remove(prop);
2399 static int TaskContext_addAttribute(lua_State *L)
2401 int argc = lua_gettop(L);
2402 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2403 AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 2,
"Attribute", AttributeBase));
2406 const char *name = luaL_checkstring(L, 3);
2410 if(!tc->addAttribute(*pb))
2411 luaL_error(L,
"TaskContext.addAttribute: failed to add attribute %s.",
2412 pb->getName().c_str());
2417 static int TaskContext_getAttribute(lua_State *L)
2420 AttributeBase *prop;
2422 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2423 name = luaL_checkstring(L, 2);
2425 prop = tc->getAttribute(name);
2428 luaL_error(L,
"%s failed. No such Attribute", __FILE__);
2430 Attribute_push(L, prop);
2435 static int TaskContext_getAttributeNames(lua_State *L)
2437 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2438 std::vector<std::string> plist = tc->attributes()->getAttributeNames();
2439 push_vect_str(L, plist);
2443 static int TaskContext_getAttributes(lua_State *L)
2445 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2446 vector<AttributeBase*> props = tc->attributes()->getValues();
2449 lua_createtable(L, props.size(), 0);
2450 for(vector<AttributeBase*>::iterator it = props.begin(); it != props.end(); ++it) {
2451 Attribute_push(L, *it);
2452 lua_rawseti(L, -2, key++);
2458 static int TaskContext_removeAttribute(lua_State *L)
2460 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2461 const char *name = luaL_checkstring(L, 2);
2463 if(!tc->attributes()->hasAttribute(name))
2464 luaL_error(L,
"%s failed. No such attribute", __FILE__);
2466 tc->attributes()->removeAttribute(name);
2471 static int TaskContext_getOps(lua_State *L)
2473 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2474 std::vector<std::string> oplst = tc->operations()->getNames();
2475 push_vect_str(L, oplst);
2480 static int TaskContext_getOpInfo(lua_State *L)
2483 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2484 const char *op = luaL_checkstring(L, 2);
2485 std::vector<ArgumentDescription> args;
2487 if(!tc->operations()->hasMember(op))
2488 luaL_error(L,
"TaskContext.getOpInfo failed: no such operation");
2490 lua_pushstring(L, tc->operations()->getResultType(op).c_str());
2491 lua_pushinteger(L, tc->operations()->getArity(op));
2492 lua_pushstring(L, tc->operations()->getDescription(op).c_str());
2494 args = tc->operations()->getArgumentList(op);
2498 for (std::vector<ArgumentDescription>::iterator it = args.begin(); it != args.end(); it++) {
2500 lua_pushstring(L,
"name"); lua_pushstring(L, it->name.c_str()); lua_rawset(L, -3);
2501 lua_pushstring(L,
"type"); lua_pushstring(L, it->type.c_str()); lua_rawset(L, -3);
2502 lua_pushstring(L,
"desc"); lua_pushstring(L, it->description.c_str()); lua_rawset(L, -3);
2503 lua_rawseti(L, -2, i++);
2509 static int TaskContext_provides(lua_State *L)
2511 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2512 Service::shared_ptr srv = tc->provides();
2515 luaL_error(L,
"TaskContext.provides: no default service");
2518 luaM_pushobject_mt(L,
"Service", Service::shared_ptr)(srv);
2520 return Service_provides(L);
2523 static int TaskContext_getProviderNames(lua_State *L)
2525 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2526 Service::shared_ptr srv = tc->provides();
2527 push_vect_str(L, srv->getProviderNames());
2531 static int TaskContext_requires(lua_State *L)
2533 ServiceRequester::shared_ptr sr;
2534 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2535 sr = tc->requires();
2538 luaL_error(L,
"TaskContext.requires returned NULL");
2540 ServiceRequester_push(L, sr.get());
2542 return ServiceRequester_requires(L);
2545 static int TaskContext_connectServices(lua_State *L)
2548 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2549 TaskContext *peer = *(luaM_checkudata_bx(L, 2, TaskContext));
2550 ret = tc->connectServices(peer);
2551 lua_pushboolean(L, ret);
2555 static int TaskContext_hasOperation(lua_State *L)
2557 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2558 Service::shared_ptr srv = tc->provides();
2561 luaL_error(L,
"TaskContext.provides: no default service");
2564 luaM_pushobject_mt(L,
"Service", Service::shared_ptr)(srv);
2566 return Service_hasOperation(L);
2570 static int TaskContext_getOperation(lua_State *L)
2572 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2573 Service::shared_ptr srv = tc->provides();
2576 luaL_error(L,
"TaskContext.getOperation: no default service");
2579 luaM_pushobject_mt(L,
"Service", Service::shared_ptr)(srv);
2581 return Service_getOperation(L);
2588 static void SendStatus_push(lua_State *L, SendStatus ss)
2591 case SendSuccess: lua_pushstring(L,
"SendSuccess");
break;
2592 case SendNotReady: lua_pushstring(L,
"SendNotReady");
break;
2593 case SendFailure: lua_pushstring(L,
"SendFailure");
break;
2594 default: lua_pushstring(L,
"unkown");
2598 static int __SendHandle_collect(lua_State *L,
bool block)
2600 unsigned int coll_argc;
2601 std::vector<DataSourceBase::shared_ptr> coll_args;
2603 const types::TypeInfo *ti;
2604 OperationInterfacePart *oip;
2605 DataSourceBase::shared_ptr dsb, *dsbp;
2607 unsigned int argc = lua_gettop(L);
2608 SendHandleC *shc = luaM_checkudata_mt(L, 1,
"SendHandle", SendHandleC);
2611 oip = shc->getOrp();
2612 coll_argc = oip->collectArity();
2614 if(block && (argc == 1)) {
2616 for(
unsigned int i=1; i<=coll_argc; i++) {
2617 ti = oip->getCollectType(i);
2618 dsb = ti->buildValue();
2619 coll_args.push_back(dsb);
2622 }
else if (argc-1 == coll_argc) {
2624 if (!shc->ready()) {
2625 for(
unsigned int arg=2; arg<=argc; arg++) {
2626 if ((dsbp = luaM_testudata_mt(L, arg,
"Variable", DataSourceBase::shared_ptr)) != NULL)
2629 luaL_error(L,
"SendHandle.collect: expected Variable argument at position %d", arg-1);
2635 luaL_error(L,
"SendHandle.collect: wrong number of args. expected either 0 or %d, got %d",
2638 luaL_error(L,
"SendHandle.collectIfDone: wrong number of args. expected %d, got %d",
2643 if(block) ss = shc->collect();
2644 else ss = shc->collectIfDone();
2646 SendStatus_push(L, ss);
2648 if(ss == SendSuccess) {
2649 for (
unsigned int i=0; i<coll_args.size(); i++)
2650 Variable_push_coerce(L, coll_args[i]);
2653 return coll_args.size() + 1;
2661 static int SendHandle_collect(lua_State *L) {
return __SendHandle_collect(L,
true); }
2662 static int SendHandle_collectIfDone(lua_State *L) {
return __SendHandle_collect(L,
false); }
2664 static const struct luaL_Reg SendHandle_f [] = {
2665 {
"collect", SendHandle_collect },
2666 {
"collectIfDone", SendHandle_collectIfDone },
2670 static const struct luaL_Reg SendHandle_m [] = {
2671 {
"collect", SendHandle_collect },
2672 {
"collectIfDone", SendHandle_collectIfDone },
2673 {
"__gc", GCMethod<SendHandleC> },
2678 static int TaskContext_del(lua_State *L)
2680 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2684 luaL_getmetatable(L,
"__dead__");
2685 lua_setmetatable(L, -2);
2689 static const struct luaL_Reg TaskContext_f [] = {
2690 {
"getName", TaskContext_getName },
2691 {
"start", TaskContext_start },
2692 {
"stop", TaskContext_stop },
2693 {
"configure", TaskContext_configure },
2694 {
"activate", TaskContext_activate },
2695 {
"cleanup", TaskContext_cleanup },
2696 {
"error", TaskContext_error },
2697 {
"recover", TaskContext_recover },
2698 {
"getState", TaskContext_getState },
2699 {
"getPeers", TaskContext_getPeers },
2700 {
"addPeer", TaskContext_addPeer },
2701 {
"removePeer", TaskContext_removePeer },
2702 {
"getPeer", TaskContext_getPeer },
2703 {
"getPortNames", TaskContext_getPortNames },
2704 {
"addPort", TaskContext_addPort },
2705 {
"addEventPort", TaskContext_addEventPort },
2706 {
"getPort", TaskContext_getPort },
2707 {
"removePort", TaskContext_removePort },
2708 {
"addProperty", TaskContext_addProperty },
2709 {
"getProperty", TaskContext_getProperty },
2710 {
"getProperties", TaskContext_getProperties },
2711 {
"getPropertyNames", TaskContext_getPropertyNames },
2712 {
"removeProperty", TaskContext_removeProperty },
2713 {
"addAttribute", TaskContext_addAttribute },
2714 {
"getAttribute", TaskContext_getAttribute },
2715 {
"getAttributes", TaskContext_getAttributes },
2716 {
"getAttributeNames", TaskContext_getAttributeNames },
2717 {
"removeAttribute", TaskContext_removeAttribute },
2718 {
"getOps", TaskContext_getOps },
2719 {
"getOpInfo", TaskContext_getOpInfo },
2720 {
"hasOperation", TaskContext_hasOperation },
2721 {
"provides", TaskContext_provides },
2722 {
"getProviderNames", TaskContext_getProviderNames },
2723 {
"connectServices", TaskContext_connectServices },
2724 {
"getOperation", TaskContext_getOperation },
2725 {
"delete", TaskContext_del },
2729 static const struct luaL_Reg TaskContext_m [] = {
2730 {
"getName", TaskContext_getName },
2731 {
"start", TaskContext_start },
2732 {
"stop", TaskContext_stop },
2733 {
"configure", TaskContext_configure },
2734 {
"activate", TaskContext_activate },
2735 {
"cleanup", TaskContext_cleanup },
2736 {
"error", TaskContext_error },
2737 {
"recover", TaskContext_recover },
2738 {
"getState", TaskContext_getState },
2739 {
"getPeers", TaskContext_getPeers },
2740 {
"addPeer", TaskContext_addPeer },
2741 {
"connectPeers", TaskContext_connectPeers },
2742 {
"removePeer", TaskContext_removePeer },
2743 {
"getPeer", TaskContext_getPeer },
2744 {
"getPortNames", TaskContext_getPortNames },
2745 {
"addPort", TaskContext_addPort },
2746 {
"addEventPort", TaskContext_addEventPort },
2747 {
"getPort", TaskContext_getPort },
2748 {
"removePort", TaskContext_removePort },
2749 {
"addProperty", TaskContext_addProperty },
2750 {
"getProperty", TaskContext_getProperty },
2751 {
"getProperties", TaskContext_getProperties },
2752 {
"getPropertyNames", TaskContext_getPropertyNames },
2753 {
"addAttribute", TaskContext_addAttribute },
2754 {
"getAttribute", TaskContext_getAttribute },
2755 {
"getAttributes", TaskContext_getAttributes },
2756 {
"getAttributeNames", TaskContext_getAttributeNames },
2757 {
"removeAttribute", TaskContext_removeAttribute },
2758 {
"removeProperty", TaskContext_removeProperty },
2759 {
"getOps", TaskContext_getOps },
2760 {
"getOpInfo", TaskContext_getOpInfo },
2761 {
"hasOperation", TaskContext_hasOperation },
2762 {
"provides", TaskContext_provides },
2763 {
"getProviderNames", TaskContext_getProviderNames },
2764 {
"requires", TaskContext_requires },
2765 {
"connectServices", TaskContext_connectServices },
2766 {
"getOperation", TaskContext_getOperation },
2767 {
"delete", TaskContext_del },
2779 class EEHook :
public base::ExecutableInterface
2787 EEHook(lua_State *_L, std::string _func) { L = _L; func = _func; tc = __getTC(L); }
2788 bool execute() {
return call_func(L, func.c_str(), tc, 1, 1); }
2791 static int EEHook_new(lua_State *L)
2794 func = luaL_checkstring(L, 1);
2795 luaM_pushobject(L,
EEHook)(L, func);
2799 static int EEHook_enable(lua_State *L)
2802 TaskContext *tc = __getTC(L);
2803 lua_pushboolean(L, tc->engine()->runFunction(eeh));
2807 static int EEHook_disable(lua_State *L)
2809 TaskContext *tc = __getTC(L);
2810 lua_pushboolean(L, tc->engine()->removeFunction(eeh));
2815 static int EEHook_gc(lua_State *L)
2819 reinterpret_cast<EEHook*
>(lua_touserdata(L, 1))->~
EEHook();
2824 static const struct luaL_Reg EEHook_f [] = {
2825 {
"new", EEHook_new },
2826 {
"enable", EEHook_enable },
2827 {
"disable", EEHook_disable },
2831 static const struct luaL_Reg EEHook_m [] = {
2832 {
"enable", EEHook_enable },
2833 {
"disable", EEHook_disable },
2841 static const char *
const loglevels[] = {
2842 "Never",
"Fatal",
"Critical",
"Error",
"Warning",
"Info",
"Debug",
"RealTime", NULL
2845 static int Logger_setLogLevel(lua_State *L)
2847 Logger::LogLevel ll = (Logger::LogLevel) luaL_checkoption(L, 1, NULL, loglevels);
2848 log().setLogLevel(ll);
2852 static int Logger_getLogLevel(lua_State *L)
2854 Logger::LogLevel ll = log().getLogLevel();
2857 case Logger::Never: lua_pushstring(L,
"Never");
break;
2858 case Logger::Fatal: lua_pushstring(L,
"Fatal");
break;
2859 case Logger::Critical: lua_pushstring(L,
"Critical");
break;
2860 case Logger::Error: lua_pushstring(L,
"Error");
break;
2861 case Logger::Warning: lua_pushstring(L,
"Warning");
break;
2862 case Logger::Info: lua_pushstring(L,
"Info");
break;
2863 case Logger::Debug: lua_pushstring(L,
"Debug");
break;
2864 case Logger::RealTime: lua_pushstring(L,
"RealTime");
break;
2866 lua_pushstring(L,
"unknown");
2871 static int Logger_log(lua_State *L)
2874 for(
int i=1; i<=lua_gettop(L); i++) {
2875 mes = luaL_checkstring(L, i);
2876 Logger::log() << mes;
2878 Logger::log() << endlog();
2882 static int Logger_logl(lua_State *L)
2885 Logger::LogLevel ll = (Logger::LogLevel) luaL_checkoption(L, 1, NULL, loglevels);
2886 for(
int i=2; i<=lua_gettop(L); i++) {
2887 mes = luaL_checkstring(L, i);
2888 Logger::log(ll) << mes;
2890 Logger::log(ll) << endlog();
2896 static int getTime(lua_State *L)
2898 unsigned long nsec, sec;
2899 RTT::os::TimeService::nsecs total_nsec = TimeService::Instance()->getNSecs();
2900 sec = total_nsec / 1000000000;
2901 nsec = total_nsec % 1000000000;
2902 lua_pushinteger(L, sec);
2903 lua_pushinteger(L, nsec);
2907 static int rtt_sleep(lua_State *L)
2910 ts.tv_sec = luaL_checknumber(L, 1);
2911 ts.tv_nsec = luaL_checknumber(L, 2);
2912 rtos_nanosleep(&ts, NULL);
2916 static int getTC(lua_State *L)
2918 lua_pushstring(L,
"this_TC");
2919 lua_rawget(L, LUA_REGISTRYINDEX);
2923 static TaskContext* __getTC(lua_State *L)
2927 tc = *(luaM_checkudata_bx(L, -1, TaskContext));
2933 static int globals_getNames(lua_State *L)
2935 GlobalsRepository::shared_ptr gr = GlobalsRepository::Instance();
2936 push_vect_str(L, gr->getAttributeNames() );
2940 static int globals_get(lua_State *L)
2943 base::AttributeBase *ab;
2944 DataSourceBase::shared_ptr dsb;
2946 name = luaL_checkstring(L, 1);
2947 GlobalsRepository::shared_ptr gr = GlobalsRepository::Instance();
2949 ab = gr->getAttribute(name);
2952 Variable_push_coerce(L, ab->getDataSource());
2960 static int provides_global(lua_State *L)
2962 luaM_pushobject_mt(L,
"Service", Service::shared_ptr)(GlobalService::Instance());
2964 return Service_provides(L);
2967 static int rtt_services(lua_State *L)
2969 push_vect_str(L, PluginLoader::Instance()->listServices());
2973 static int rtt_typekits(lua_State *L)
2975 push_vect_str(L, PluginLoader::Instance()->listTypekits());
2979 static int rtt_types(lua_State *L)
2981 push_vect_str(L, TypeInfoRepository::Instance()->getTypes());
2985 static const struct luaL_Reg rtt_f [] = {
2986 {
"getTime", getTime },
2987 {
"sleep", rtt_sleep },
2989 {
"globals_getNames", globals_getNames },
2990 {
"globals_get", globals_get },
2991 {
"provides", provides_global },
2992 {
"services", rtt_services },
2993 {
"typekits", rtt_typekits },
2994 {
"types", rtt_types },
2995 {
"setLogLevel", Logger_setLogLevel },
2996 {
"getLogLevel", Logger_getLogLevel },
2997 {
"log", Logger_log },
2998 {
"logl", Logger_logl },
3002 extern "C" int luaopen_rtt(lua_State *L);
3004 int luaopen_rtt(lua_State *L)
3007 lua_replace(L, LUA_ENVIRONINDEX);
3009 luaL_newmetatable(L,
"__dead__");
3019 luaL_newmetatable(L,
"TaskContext");
3020 lua_pushvalue(L, -1);
3021 lua_setfield(L, -2,
"__index");
3022 luaL_register(L, NULL, TaskContext_m);
3023 luaL_register(L,
"rtt.TaskContext", TaskContext_f);
3025 luaL_newmetatable(L,
"Operation");
3026 lua_pushvalue(L, -1);
3027 lua_setfield(L, -2,
"__index");
3028 luaL_register(L, NULL, Operation_m);
3029 luaL_register(L,
"rtt.Operation", Operation_f);
3031 luaL_newmetatable(L,
"Service");
3032 lua_pushvalue(L, -1);
3033 lua_setfield(L, -2,
"__index");
3034 luaL_register(L, NULL, Service_m);
3035 luaL_register(L,
"rtt.Service", Service_f);
3037 luaL_newmetatable(L,
"ServiceRequester");
3038 lua_pushvalue(L, -1);
3039 lua_setfield(L, -2,
"__index");
3040 luaL_register(L, NULL, ServiceRequester_m);
3041 luaL_register(L,
"rtt.ServiceRequester", ServiceRequester_f);
3043 luaL_newmetatable(L,
"SendHandle");
3044 lua_pushvalue(L, -1);
3045 lua_setfield(L, -2,
"__index");
3046 luaL_register(L, NULL, SendHandle_m);
3047 luaL_register(L,
"rtt.SendHandle", SendHandle_f);
3049 luaL_newmetatable(L,
"InputPort");
3050 lua_pushvalue(L, -1);
3051 lua_setfield(L, -2,
"__index");
3052 luaL_register(L, NULL, InputPort_m);
3053 luaL_register(L,
"rtt.InputPort", InputPort_f);
3055 luaL_newmetatable(L,
"OutputPort");
3056 lua_pushvalue(L, -1);
3057 lua_setfield(L, -2,
"__index");
3058 luaL_register(L, NULL, OutputPort_m);
3059 luaL_register(L,
"rtt.OutputPort", OutputPort_f);
3061 luaL_newmetatable(L,
"Variable");
3062 lua_pushvalue(L, -1);
3063 lua_setfield(L, -2,
"__index");
3064 luaL_register(L, NULL, Variable_m);
3065 luaL_register(L,
"rtt.Variable", Variable_f);
3067 luaL_newmetatable(L,
"Property");
3068 lua_pushvalue(L, -1);
3069 lua_setfield(L, -2,
"__index");
3070 luaL_register(L, NULL, Property_m);
3071 luaL_register(L,
"rtt.Property", Property_f);
3073 luaL_newmetatable(L,
"Attribute");
3074 lua_pushvalue(L, -1);
3075 lua_setfield(L, -2,
"__index");
3076 luaL_register(L, NULL, Attribute_m);
3077 luaL_register(L,
"rtt.Attribute", Attribute_f);
3079 luaL_newmetatable(L,
"EEHook");
3080 lua_pushvalue(L, -1);
3081 lua_setfield(L, -2,
"__index");
3082 luaL_register(L, NULL, EEHook_m);
3083 luaL_register(L,
"rtt.EEHook", EEHook_f);
3086 luaL_register(L,
"rtt", rtt_f);
3092 int set_context_tc(TaskContext *tc, lua_State *L)
3094 TaskContext **new_tc;
3095 lua_pushstring(L,
"this_TC");
3096 new_tc = (TaskContext**) lua_newuserdata(L,
sizeof(TaskContext*));
3097 *new_tc = (TaskContext*) tc;
3098 luaL_getmetatable(L,
"TaskContext");
3099 lua_setmetatable(L, -2);
3100 lua_rawset(L, LUA_REGISTRYINDEX);
3107 bool call_func(lua_State *L,
const char *fname, TaskContext *tc,
3108 int require_function,
int require_result)
3111 int num_res = (require_result != 0) ? 1 : 0;
3112 lua_getglobal(L, fname);
3114 if(lua_isnil(L, -1)) {
3116 if(require_function)
3117 luaL_error(L,
"%s: no (required) Lua function %s", tc->getName().c_str(), fname);
3122 if (lua_pcall(L, 0, num_res, 0) != 0) {
3123 Logger::log(Logger::Error) <<
"LuaComponent '"<< tc->getName() <<
"': error calling function " 3124 << fname <<
": " << lua_tostring(L, -1) << endlog();
3130 if(require_result) {
3131 if (!lua_isboolean(L, -1)) {
3132 Logger::log(Logger::Error) <<
"LuaComponent '" << tc->getName() <<
"': " << fname
3133 <<
" must return a bool but returned a " 3134 << lua_typename(L, lua_type(L, -1)) << endlog();
3139 ret = lua_toboolean(L, -1);