43 static TaskContext* __getTC(lua_State*);
49 # define _DBG(fmt, ...) printf("%s:%d\t" fmt "\n", __FUNCTION__, __LINE__, __VA_ARGS__) 51 # define _DBG(fmt, ...) do { } while(0); 55 # define _DBG(fmt, args...) printf("%s:%d\t" fmt "\n", __FUNCTION__, __LINE__, ##args) 57 # define _DBG(fmt, args...) do { } while(0); 66 void*
operator new(
size_t size, lua_State* L,
const char* mt)
68 void* ptr = lua_newuserdata(L, size);
69 luaL_getmetatable(L, mt);
71 lua_setmetatable(L, -2);
81 #define luaM_pushobject(L, T) new(L, #T) T 82 #define luaM_pushobject_mt(L, MT, T) new(L, MT) T 85 #define luaM_checkudata(L, pos, T) reinterpret_cast<T*>(luaL_checkudata((L), (pos), #T)) 86 #define luaM_checkudata_mt(L, pos, MT, T) reinterpret_cast<T*>(luaL_checkudata((L), (pos), MT)) 89 #define luaM_testudata(L, pos, T) (T*) (luaL_testudata((L), (pos), #T)) 90 #define luaM_testudata_mt(L, pos, MT, T) (T*) (luaL_testudata((L), (pos), MT)) 97 #define luaM_checkudata_bx(L, pos, T) (T**) (luaL_checkudata((L), (pos), #T)) 98 #define luaM_checkudata_mt_bx(L, pos, MT, T) (T**) (luaL_checkudata((L), (pos), MT)) 101 #define luaM_testudata_bx(L, pos, T) (T**) (luaL_testudata((L), (pos), #T)) 102 #define luaM_testudata_mt_bx(L, pos, MT, T) (T**) (luaL_testudata((L), (pos), MT)) 105 #define gen_push_bxptr(name, MT, T) \ 106 static void name(lua_State *L, T* ptr) \ 108 T** ptrptr = (T**) lua_newuserdata(L, sizeof(T*)); \ 110 luaL_getmetatable(L, MT); \ 111 lua_setmetatable(L, -2); \ 116 int GCMethod(lua_State* L)
118 reinterpret_cast<T*
>(lua_touserdata(L, 1))->~T();
128 void* luaL_testudata (lua_State *L,
int ud,
const char *tname)
130 void *p = lua_touserdata(L, ud);
135 if (!lua_getmetatable(L, ud)) {
141 lua_pushstring(L, tname);
142 lua_rawget(L, LUA_REGISTRYINDEX);
144 if (!lua_rawequal(L, -1, -2))
153 void push_vect_str(lua_State *L,
const std::vector<std::string> &v)
156 lua_createtable(L, v.size(), 0);
158 for(vector<std::string>::const_iterator it = v.begin(); it != v.end(); ++it) {
159 lua_pushstring(L, it->c_str());
160 lua_rawseti(L, -2, key++);
165 static void Variable_fromlua(lua_State *L, DataSourceBase::shared_ptr& dsb,
int valind);
166 static DataSourceBase::shared_ptr Variable_fromlua(lua_State *L,
const types::TypeInfo* ti,
int valind);
167 static DataSourceBase::shared_ptr Variable_fromlua(lua_State *L,
const char* type,
int valind);
173 static const TypeInfo* ti_lookup(lua_State *L,
const char *name)
175 #ifndef TYPEINFO_CACHING 176 return types::TypeInfoRepository::Instance()->type(name);
179 int top = lua_gettop(L);
183 lua_pushstring(L,
"typeinfo_cache");
184 lua_rawget(L, LUA_REGISTRYINDEX);
186 if(lua_type(L, -1) == LUA_TTABLE)
192 lua_pushstring(L,
"typeinfo_cache");
193 lua_pushvalue(L, -2);
194 lua_rawset(L, LUA_REGISTRYINDEX);
198 lua_pushstring(L, name);
201 if(lua_type(L, -1) != LUA_TLIGHTUSERDATA)
204 ti = (
const TypeInfo*) lua_touserdata(L, -1);
209 ti = types::TypeInfoRepository::Instance()->type(name);
211 lua_pushstring(L, name);
212 lua_pushlightuserdata(L, (
void*) ti);
223 static bool __typenames_cmp(lua_State *L,
const types::TypeInfo *ti1,
const char* type2)
225 const types::TypeInfo *ti2 = ti_lookup(L, type2);
231 static bool Variable_is_a(lua_State *L,
const types::TypeInfo *ti1,
const char* type)
233 const types::TypeInfo *ti2 = ti_lookup(L, type);
238 static bool __Variable_isbasic(lua_State *L, DataSourceBase::shared_ptr &dsb)
240 const types::TypeInfo *ti = dsb->getTypeInfo();
242 if ( Variable_is_a(L, ti,
"bool") ||
243 Variable_is_a(L, ti,
"double") ||
244 Variable_is_a(L, ti,
"float") ||
245 Variable_is_a(L, ti,
"uint") ||
246 Variable_is_a(L, ti,
"int") ||
247 Variable_is_a(L, ti,
"long") ||
248 Variable_is_a(L, ti,
"char") ||
249 Variable_is_a(L, ti,
"uint8") || Variable_is_a(L, ti,
"int8") ||
250 Variable_is_a(L, ti,
"uint16") || Variable_is_a(L, ti,
"int16") ||
251 Variable_is_a(L, ti,
"uint32") || Variable_is_a(L, ti,
"int32") ||
252 Variable_is_a(L, ti,
"uint64") || Variable_is_a(L, ti,
"int64") ||
253 Variable_is_a(L, ti,
"string") ||
254 Variable_is_a(L, ti,
"void"))
260 static int Variable_isbasic(lua_State *L)
262 DataSourceBase::shared_ptr dsb = *(luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr));
263 lua_pushboolean(L, __Variable_isbasic(L, dsb));
272 static int __Variable_tolua(lua_State *L, DataSourceBase::shared_ptr dsb)
274 DataSourceBase *ds = dsb.get();
275 const types::TypeInfo* ti = dsb->getTypeInfo();
278 if(Variable_is_a(L, ti,
"bool")) {
279 DataSource<bool>* dsb = DataSource<bool>::narrow(ds);
280 if(dsb) lua_pushboolean(L, dsb->get());
282 }
else if (Variable_is_a(L, ti,
"float")) {
283 DataSource<float>* dsb = DataSource<float>::narrow(ds);
284 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
286 }
else if (Variable_is_a(L, ti,
"double")) {
287 DataSource<double>* dsb = DataSource<double>::narrow(ds);
288 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
290 }
else if (Variable_is_a(L, ti,
"uint8")) {
291 DataSource<uint8_t>* dsb = DataSource<uint8_t>::narrow(ds);
292 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
294 }
else if (Variable_is_a(L, ti,
"int8")) {
295 DataSource<int8_t>* dsb = DataSource<int8_t>::narrow(ds);
296 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
298 }
else if (Variable_is_a(L, ti,
"uint16")) {
299 DataSource<uint16_t>* dsb = DataSource<uint16_t>::narrow(ds);
300 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
302 }
else if (Variable_is_a(L, ti,
"int16")) {
303 DataSource<int16_t>* dsb = DataSource<int16_t>::narrow(ds);
304 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
306 }
else if (Variable_is_a(L, ti,
"uint32")) {
307 DataSource<uint32_t>* dsb = DataSource<uint32_t>::narrow(ds);
308 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
310 }
else if (Variable_is_a(L, ti,
"int32")) {
311 DataSource<int32_t>* dsb = DataSource<int32_t>::narrow(ds);
312 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
314 }
else if (Variable_is_a(L, ti,
"uint64")) {
315 DataSource<uint64_t>* dsb = DataSource<uint64_t>::narrow(ds);
316 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
318 }
else if (Variable_is_a(L, ti,
"int64")) {
319 DataSource<int64_t>* dsb = DataSource<int64_t>::narrow(ds);
320 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
322 }
else if (Variable_is_a(L, ti,
"uint")) {
323 DataSource<unsigned int>* dsb = DataSource<unsigned int>::narrow(ds);
324 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
326 }
else if (Variable_is_a(L, ti,
"long")) {
327 DataSource<long>* dsb = DataSource<long>::narrow(ds);
328 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
330 }
else if (Variable_is_a(L, ti,
"int")) {
331 DataSource<int>* dsb = DataSource<int>::narrow(ds);
332 if(dsb) lua_pushnumber(L, ((lua_Number) dsb->get()));
334 }
else if (Variable_is_a(L, ti,
"char")) {
335 DataSource<char>* dsb = DataSource<char>::narrow(ds);
337 if(dsb) lua_pushlstring(L, &c, 1);
339 }
else if (Variable_is_a(L, ti,
"string")) {
340 DataSource<std::string>* dsb = DataSource<std::string>::narrow(ds);
341 if(dsb) lua_pushlstring(L, dsb->get().c_str(), dsb->get().size());
343 }
else if (Variable_is_a(L, ti,
"void")) {
344 DataSource<void>* dsb = DataSource<void>::narrow(ds);
345 if(dsb) lua_pushnil(L);
355 luaL_error(L,
"Variable.tolua: can't convert type %s", dsb->getTypeName().c_str());
359 luaL_error(L,
"Variable.tolua: narrow failed for %s Variable", dsb->getTypeName().c_str());
363 static int Variable_tolua(lua_State *L)
365 DataSourceBase::shared_ptr dsb = *(luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr));
366 return __Variable_tolua(L, dsb);
374 static void Variable_coerce(lua_State *L, DataSourceBase::shared_ptr dsb)
376 if (__Variable_isbasic(L, dsb)) {
378 __Variable_tolua(L, dsb);
385 static void Variable_push_coerce(lua_State *L, DataSourceBase::shared_ptr dsb)
387 if (__Variable_isbasic(L, dsb))
388 __Variable_tolua(L, dsb);
390 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(dsb);
394 static int Variable_getTypes(lua_State *L)
396 push_vect_str(L, Types()->getTypes());
400 static int Variable_getMemberNames(lua_State *L)
402 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr);
403 push_vect_str(L, (*dsbp)->getMemberNames());
407 static int Variable_tolightuserdata(lua_State *L)
409 DataSourceBase::shared_ptr dsb = *(luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr));
410 lua_pushlightuserdata(L, dsb->getRawPointer());
419 static DataSourceBase::shared_ptr lookup_member(lua_State *L, DataSourceBase::shared_ptr parent,
const char* mem)
421 DataSourceBase *varptr;
422 DataSourceBase::shared_ptr *dsbp;
423 DataSourceBase::shared_ptr memdsb;
424 int top = lua_gettop(L);
426 varptr = parent.get();
428 lua_pushlightuserdata(L, (
void*) varptr);
429 lua_rawget(L, LUA_REGISTRYINDEX);
431 if(lua_type(L, -1) == LUA_TNIL)
434 lua_pushstring(L, mem);
437 if ((dsbp = luaM_testudata_mt(L, -1,
"Variable", DataSourceBase::shared_ptr)) != NULL) {
446 memdsb = parent->getMember(mem);
452 if(lua_type(L, -1) == LUA_TNIL) {
454 lua_pushlightuserdata(L, (
void*) varptr);
455 lua_pushvalue(L, -2);
456 lua_rawset(L, LUA_REGISTRYINDEX);
460 lua_pushstring(L, mem);
461 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(memdsb);
463 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(memdsb);
466 lua_replace(L, top+1);
467 lua_settop(L, top+1);
473 static void cache_clear(lua_State *L, DataSourceBase *varptr)
475 lua_pushlightuserdata(L, (
void*) varptr);
477 lua_rawset(L, LUA_REGISTRYINDEX);
480 static int Variable_getMember(lua_State *L)
482 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr);
483 DataSourceBase::shared_ptr memdsb;
484 const char *mem = luaL_checkstring(L, 2);
486 if ((memdsb = lookup_member(L, *dsbp, mem)) == 0)
487 luaL_error(L,
"Variable.getMember: indexing failed, no member %s", mem);
489 Variable_coerce(L, memdsb);
494 static int Variable_getMemberRaw(lua_State *L)
496 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr);
497 DataSourceBase::shared_ptr memdsb;
498 const char *mem = luaL_checkstring(L, 2);
500 if ((memdsb = lookup_member(L, (*dsbp), mem)) == 0)
501 luaL_error(L,
"Variable.getMemberRaw: indexing failed, no member %s", mem);
508 static int Variable_update(lua_State *L)
511 DataSourceBase::shared_ptr dsb;
512 DataSourceBase::shared_ptr *dsbp;
513 DataSourceBase::shared_ptr
self = *(luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr));
515 if ((dsbp = luaM_testudata_mt(L, 2,
"Variable", DataSourceBase::shared_ptr)) != NULL) {
517 ret =
self->update(dsb.get());
518 if (!ret) luaL_error(L,
"Variable.assign: assignment failed");
520 Variable_fromlua(L,
self, 2);
527 static int Variable_create(lua_State *L)
530 type = luaL_checkstring(L, 1);
532 if(!strcmp(type,
"void"))
533 luaL_error(L,
"Variable.new: can't create void variable");
535 TypeInfo* ti = Types()->type(type);
538 luaL_error(L,
"Variable.new: unknown type %s", type);
540 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(ti->buildValue());
544 #define CONVERT_TO_NUMBER(CTGT) \ 546 if (luatype == LUA_TNUMBER) x = lua_tonumber(L, valind); \ 547 else goto out_conv_err; \ 548 AssignableDataSource<CTGT> *ads = ValueDataSource<CTGT>::narrow(dsb.get()); \ 549 if (ads == NULL) luaL_error(L, "Variable_fromlua: failed to narrow target dsb to %s.", #CTGT ); \ 554 static void Variable_fromlua(lua_State *L, DataSourceBase::shared_ptr& dsb,
int valind)
556 const types::TypeInfo* ti = dsb->getTypeInfo();
558 luaL_checkany(L, valind);
559 int luatype = lua_type(L, valind);
561 if(__typenames_cmp(L, ti,
"bool")) {
563 if(luatype == LUA_TBOOLEAN)
564 x = (lua_Number) lua_toboolean(L, valind);
565 else if (luatype == LUA_TNUMBER)
566 x = lua_tonumber(L, valind);
570 AssignableDataSource<bool> *ads = ValueDataSource<bool>::narrow(dsb.get());
572 luaL_error(L,
"Variable_fromlua: failed to narrow target dsb to bool");
575 else if (__typenames_cmp(L, ti,
"uint")) { CONVERT_TO_NUMBER(
unsigned int); }
576 else if (__typenames_cmp(L, ti,
"int")) { CONVERT_TO_NUMBER(
int); }
577 else if (__typenames_cmp(L, ti,
"double")) { CONVERT_TO_NUMBER(
double); }
578 else if (__typenames_cmp(L, ti,
"long")) { CONVERT_TO_NUMBER(
double); }
579 else if (__typenames_cmp(L, ti,
"uint8")) { CONVERT_TO_NUMBER(uint8_t); }
580 else if (__typenames_cmp(L, ti,
"int8")) { CONVERT_TO_NUMBER(int8_t); }
581 else if (__typenames_cmp(L, ti,
"uint16")) { CONVERT_TO_NUMBER(uint16_t); }
582 else if (__typenames_cmp(L, ti,
"int16")) { CONVERT_TO_NUMBER(int16_t); }
583 else if (__typenames_cmp(L, ti,
"uint32")) { CONVERT_TO_NUMBER(uint32_t); }
584 else if (__typenames_cmp(L, ti,
"int32")) { CONVERT_TO_NUMBER(int32_t); }
585 else if (__typenames_cmp(L, ti,
"uint64")) { CONVERT_TO_NUMBER(uint64_t); }
586 else if (__typenames_cmp(L, ti,
"int64")) { CONVERT_TO_NUMBER(int64_t); }
587 else if (__typenames_cmp(L, ti,
"float")) { CONVERT_TO_NUMBER(
float); }
589 else if (__typenames_cmp(L, ti,
"char")) {
592 if (luatype == LUA_TSTRING) x = lua_tolstring(L, valind, &l);
593 else goto out_conv_err;
594 AssignableDataSource<char> *ads = ValueDataSource<char>::narrow(dsb.get());
595 if (ads == NULL) luaL_error(L,
"Variable_fromlua: failed to narrow target dsb to char");
596 ads->set((
char) x[0]);
598 }
else if (__typenames_cmp(L, ti,
"string")) {
600 if (luatype == LUA_TSTRING) x = lua_tostring(L, valind);
601 else goto out_conv_err;
602 AssignableDataSource<std::string> *ads = ValueDataSource<std::string>::narrow(dsb.get());
603 if (ads == NULL) luaL_error(L,
"Variable_fromlua: failed to narrow target dsb to std::string");
604 ads->set((std::string) x);
614 luaL_error(L,
"__lua_todsb: can't convert lua %s to %s variable",
615 lua_typename(L, luatype), ti->getTypeName().c_str());
621 static DataSourceBase::shared_ptr Variable_fromlua(lua_State *L,
const types::TypeInfo *ti,
int valind)
623 DataSourceBase::shared_ptr dsb = ti->buildValue();
624 Variable_fromlua(L, dsb, valind);
632 static DataSourceBase::shared_ptr Variable_fromlua(lua_State *L,
const char* type,
int valind)
634 const types::TypeInfo* ti = ti_lookup(L, type);
635 if(!ti) luaL_error(L,
"Variable_fromlua: %s is not a known type. Load typekit?", type);
636 return Variable_fromlua(L, ti, valind);
640 static int Variable_create_ival(lua_State *L,
int typeind,
int valind)
642 DataSourceBase::shared_ptr dsb;
643 luaL_checkany(L, valind);
644 const char* type = luaL_checkstring(L, typeind);
645 dsb = Variable_fromlua(L, type, valind);
646 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(dsb);
650 static int Variable_new(lua_State *L)
652 int argc = lua_gettop(L);
654 return Variable_create(L);
656 return Variable_create_ival(L, 1, 2);
658 luaL_error(L,
"Variable.new: invalid number of args");
663 static int Variable_toString(lua_State *L)
665 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr);
666 lua_pushstring(L, ((*dsbp)->toString()).c_str());
670 static int Variable_getType(lua_State *L)
672 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr);
673 lua_pushstring(L, (*dsbp)->getType().c_str());
677 static int Variable_getTypeIdName(lua_State *L)
679 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr);
680 lua_pushstring(L, (*dsbp)->getTypeInfo()->getTypeIdName());
684 static int Variable_getTypeName(lua_State *L)
686 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr);
687 lua_pushstring(L, (*dsbp)->getTypeName().c_str());
691 static int Variable_resize(lua_State *L)
694 DataSourceBase::shared_ptr *dsbp = luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr);
695 size = luaL_checknumber(L, 2);
696 const TypeInfo *ti = (*dsbp)->getTypeInfo();
697 lua_pushboolean(L, ti->resize(*dsbp, size));
705 static int Variable_unm(lua_State *L)
707 types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance();
708 DataSourceBase::shared_ptr arg = *(luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr));
709 DataSourceBase::shared_ptr res = opreg->applyUnary(
"-", arg.get());
710 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(res);
716 #define gen_opmet(name, op) \ 717 static int name(lua_State *L) \ 719 DataSourceBase::shared_ptr arg1 = *(luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr)); \ 720 DataSourceBase::shared_ptr arg2 = *(luaM_checkudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)); \ 721 types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance(); \ 722 DataSourceBase *res = opreg->applyBinary(#op, arg1.get(), arg2.get()); \ 724 luaL_error(L , "%s (operator %s) failed", #name, #op); \ 726 luaM_pushobject_mt(L, "Variable", DataSourceBase::shared_ptr)(res); \ 730 gen_opmet(Variable_add, +)
731 gen_opmet(Variable_sub, -)
732 gen_opmet(Variable_mul, *)
733 gen_opmet(Variable_div, /)
734 gen_opmet(Variable_mod, %)
735 gen_opmet(Variable_pow, ^)
738 #define gen_opmet_bool(name, op) \ 739 static int name(lua_State *L) \ 741 DataSourceBase::shared_ptr arg1 = *(luaM_checkudata_mt(L, 1, "Variable", DataSourceBase::shared_ptr)); \ 742 DataSourceBase::shared_ptr arg2 = *(luaM_checkudata_mt(L, 2, "Variable", DataSourceBase::shared_ptr)); \ 743 types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance(); \ 744 DataSourceBase *res = opreg->applyBinary(#op, arg1.get(), arg2.get()); \ 746 luaL_error(L , "%s (operator %s) failed", #name, #op); \ 748 return __Variable_tolua(L, res); \ 751 gen_opmet_bool(Variable_eq, ==)
752 gen_opmet_bool(Variable_lt, <)
753 gen_opmet_bool(Variable_le, <=)
755 static
int Variable_opBinary(lua_State *L)
757 types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance();
758 const char *op = luaL_checkstring(L, 1);
759 DataSourceBase::shared_ptr arg1 = *(luaM_checkudata_mt(L, 2,
"Variable", DataSourceBase::shared_ptr));
760 DataSourceBase::shared_ptr arg2 = *(luaM_checkudata_mt(L, 3,
"Variable", DataSourceBase::shared_ptr));
763 res = opreg->applyBinary(op, arg1.get(), arg2.get());
765 luaL_error(L ,
"Variable.opBinary '%s' not applicable to args", op);
769 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(res);
778 static int Variable_index(lua_State *L)
780 const char* key = luaL_checkstring(L, 2);
782 lua_getmetatable(L, 1);
783 lua_getfield(L, -1, key);
786 if(!lua_isnil(L, -1))
791 return Variable_getMember(L);
794 static int Variable_newindex(lua_State *L)
796 DataSourceBase::shared_ptr *newvalp;
797 DataSourceBase::shared_ptr newval;
798 DataSourceBase::shared_ptr parent = *(luaM_checkudata_mt(L, 1,
"Variable", DataSourceBase::shared_ptr));
799 const char* mem = luaL_checkstring(L, 2);
802 types::OperatorRepository::shared_ptr opreg = types::OperatorRepository::Instance();
803 DataSourceBase::shared_ptr curval;
805 if ((curval = lookup_member(L, parent, mem)) == 0)
806 luaL_error(L,
"Variable.newindex: indexing failed, no member %s", mem);
810 if ((newvalp = luaM_testudata_mt(L, 3,
"Variable", DataSourceBase::shared_ptr)) != NULL) {
812 if(!curval->update(newval.get())) {
813 luaL_error(L,
"Variable.newindex: failed to assign %s to member %s of type %s",
814 newval->getType().c_str(), mem, curval->getType().c_str());
817 Variable_fromlua(L, curval, 3);
831 int VariableGC(lua_State* L)
833 T* dsbp = (T*) lua_touserdata(L, 1);
834 cache_clear(L, dsbp->get());
835 reinterpret_cast<T*
>(dsbp)->~T();
840 static const struct luaL_Reg Variable_f [] = {
841 {
"new", Variable_new },
842 {
"tolua", Variable_tolua },
843 {
"isbasic", Variable_isbasic },
844 {
"toString", Variable_toString },
845 {
"getTypes", Variable_getTypes },
846 {
"getType", Variable_getType },
847 {
"getTypeName", Variable_getTypeName },
848 {
"getTypeIdName", Variable_getTypeIdName },
849 {
"getMemberNames", Variable_getMemberNames },
850 {
"getMember", Variable_getMember },
851 {
"getMemberRaw", Variable_getMemberRaw },
852 {
"tolud", Variable_tolightuserdata },
853 {
"resize", Variable_resize },
854 {
"opBinary", Variable_opBinary },
855 {
"assign", Variable_update },
856 {
"unm", Variable_unm },
857 {
"add", Variable_add },
858 {
"sub", Variable_sub },
859 {
"mul", Variable_mul },
860 {
"div", Variable_div },
861 {
"mod", Variable_mod },
862 {
"pow", Variable_pow },
863 {
"eq", Variable_eq },
864 {
"lt", Variable_lt },
865 {
"le", Variable_le },
869 static const struct luaL_Reg Variable_m [] = {
870 {
"tolua", Variable_tolua },
871 {
"isbasic", Variable_isbasic },
872 {
"toString", Variable_toString },
873 {
"getType", Variable_getType },
874 {
"getTypeName", Variable_getTypeName },
875 {
"getTypeIdName", Variable_getTypeIdName },
876 {
"getMemberNames", Variable_getMemberNames },
877 {
"getMember", Variable_getMember },
878 {
"getMemberRaw", Variable_getMemberRaw },
879 {
"tolud", Variable_tolightuserdata },
880 {
"resize", Variable_resize },
881 {
"opBinary", Variable_opBinary },
882 {
"assign", Variable_update },
883 {
"__unm", Variable_unm },
884 {
"__add", Variable_add },
885 {
"__sub", Variable_sub },
886 {
"__mul", Variable_mul },
887 {
"__div", Variable_div },
888 {
"__mod", Variable_mod },
889 {
"__pow", Variable_pow },
890 {
"__eq", Variable_eq },
891 {
"__lt", Variable_lt },
892 {
"__le", Variable_le },
893 {
"__index", Variable_index },
894 {
"__newindex", Variable_newindex },
897 {
"__gc", VariableGC<DataSourceBase::shared_ptr> },
906 gen_push_bxptr(Property_push,
"Property", PropertyBase)
908 static
int Property_new(lua_State *L)
910 const char *type, *name, *desc;
912 int argc = lua_gettop(L);
913 type = luaL_checkstring(L, 1);
916 name = (argc > 1) ? luaL_checkstring(L, 2) :
"";
917 desc = (argc > 2) ? luaL_checkstring(L, 3) :
"";
919 types::TypeInfo *ti = types::TypeInfoRepository::Instance()->type(type);
922 luaL_error(L,
"Property.new: unknown type %s", type);
924 pb = ti->buildProperty(name, desc);
925 Property_push(L, pb);
929 static int Property_get(lua_State *L)
931 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Property", PropertyBase));
932 Variable_push_coerce(L, pb->getDataSource());
936 static int Property_getRaw(lua_State *L)
938 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Property", PropertyBase));
939 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(pb->getDataSource());
943 static int Property_set(lua_State *L)
945 DataSourceBase::shared_ptr newdsb;
946 DataSourceBase::shared_ptr *newdsbp;
947 DataSourceBase::shared_ptr propdsb;
948 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Property", PropertyBase));
949 propdsb = pb->getDataSource();
952 if ((newdsbp = luaM_testudata_mt(L, 2,
"Variable", DataSourceBase::shared_ptr)) != NULL) {
954 if(!propdsb->update(newdsb.get()))
955 luaL_error(L,
"Property.set: failed to assign type %s to type %s",
956 newdsb->getType().c_str(), propdsb->getType().c_str());
958 Variable_fromlua(L, propdsb, 2);
963 static int Property_info(lua_State *L)
965 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Property", PropertyBase));
967 lua_pushstring(L,
"name"); lua_pushstring(L, pb->getName().c_str()); lua_rawset(L, -3);
968 lua_pushstring(L,
"desc"); lua_pushstring(L, pb->getDescription().c_str()); lua_rawset(L, -3);
969 lua_pushstring(L,
"type"); lua_pushstring(L, pb->getType().c_str()); lua_rawset(L, -3);
979 static int Property_gc(lua_State *L)
981 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Property", PropertyBase));
988 static int Property_del(lua_State *L)
990 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Property", PropertyBase));
994 luaL_getmetatable(L,
"__dead__");
995 lua_setmetatable(L, -2);
1005 static int Property_index(lua_State *L)
1007 const char* key = luaL_checkstring(L, 2);
1009 lua_getmetatable(L, 1);
1010 lua_getfield(L, -1, key);
1013 if(!lua_isnil(L, -1))
1019 return Variable_index(L);
1022 static int Property_newindex(lua_State *L)
1026 return Variable_newindex(L);
1029 static const struct luaL_Reg Property_f [] = {
1030 {
"new", Property_new },
1031 {
"get", Property_get },
1032 {
"getRaw", Property_getRaw },
1033 {
"set", Property_set },
1034 {
"info", Property_info },
1035 {
"delete", Property_del },
1039 static const struct luaL_Reg Property_m [] = {
1040 {
"get", Property_get },
1041 {
"getRaw", Property_getRaw },
1042 {
"set", Property_set },
1043 {
"info", Property_info },
1045 {
"delete", Property_del },
1046 {
"__index", Property_index },
1047 {
"__newindex", Property_newindex },
1055 gen_push_bxptr(Attribute_push,
"Attribute", AttributeBase)
1057 static
int Attribute_new(lua_State *L)
1059 const char *type, *name;
1061 int argc = lua_gettop(L);
1062 type = luaL_checkstring(L, 1);
1065 name = (argc > 1) ? luaL_checkstring(L, 2) :
"";
1067 types::TypeInfo *ti = types::TypeInfoRepository::Instance()->type(type);
1070 luaL_error(L,
"Attribute.new: unknown type %s", type);
1072 pb = ti->buildAttribute(name);
1073 Attribute_push(L, pb);
1077 static int Attribute_get(lua_State *L)
1079 AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Attribute", AttributeBase));
1080 Variable_push_coerce(L, pb->getDataSource());
1084 static int Attribute_getRaw(lua_State *L)
1086 AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Attribute", AttributeBase));
1087 luaM_pushobject_mt(L,
"Variable", DataSourceBase::shared_ptr)(pb->getDataSource());
1091 static int Attribute_set(lua_State *L)
1093 DataSourceBase::shared_ptr newdsb;
1094 DataSourceBase::shared_ptr *newdsbp;
1095 DataSourceBase::shared_ptr propdsb;
1096 AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Attribute", AttributeBase));
1097 propdsb = pb->getDataSource();
1100 if ((newdsbp = luaM_testudata_mt(L, 2,
"Variable", DataSourceBase::shared_ptr)) != NULL) {
1102 if(!propdsb->update(newdsb.get()))
1103 luaL_error(L,
"Attribute.set: failed to assign type %s to type %s",
1104 newdsb->getType().c_str(), propdsb->getType().c_str());
1106 Variable_fromlua(L, propdsb, 2);
1111 static int Attribute_info(lua_State *L)
1113 AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Attribute", AttributeBase));
1115 lua_pushstring(L,
"name"); lua_pushstring(L, pb->getName().c_str()); lua_rawset(L, -3);
1116 lua_pushstring(L,
"type"); lua_pushstring(L, pb->getDataSource()->getType().c_str()); lua_rawset(L, -3);
1126 static int Attribute_gc(lua_State *L)
1128 AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Attribute", AttributeBase));
1136 static int Attribute_del(lua_State *L)
1138 AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 1,
"Attribute", AttributeBase));
1142 luaL_getmetatable(L,
"__dead__");
1143 lua_setmetatable(L, -2);
1153 static int Attribute_index(lua_State *L)
1155 const char* key = luaL_checkstring(L, 2);
1157 lua_getmetatable(L, 1);
1158 lua_getfield(L, -1, key);
1161 if(!lua_isnil(L, -1))
1167 return Variable_index(L);
1170 static int Attribute_newindex(lua_State *L)
1174 return Variable_newindex(L);
1177 static const struct luaL_Reg Attribute_f [] = {
1178 {
"new", Attribute_new },
1179 {
"get", Attribute_get },
1180 {
"getRaw", Attribute_getRaw },
1181 {
"set", Attribute_set },
1182 {
"info", Attribute_info },
1183 {
"delete", Attribute_del },
1187 static const struct luaL_Reg Attribute_m [] = {
1188 {
"get", Attribute_get },
1189 {
"getRaw", Attribute_getRaw },
1190 {
"set", Attribute_set },
1191 {
"info", Attribute_info },
1193 {
"delete", Attribute_del },
1194 {
"__index", Attribute_index },
1195 {
"__newindex", Attribute_newindex },
1204 static int Port_info(lua_State *L)
1207 const char* port_type = NULL;
1208 PortInterface **pip;
1209 PortInterface *pi = NULL;
1211 if((pip = (PortInterface**) luaL_testudata(L, 1,
"InputPort")) != NULL) {
1214 }
else if((pip = (PortInterface**) luaL_testudata(L, 1,
"OutputPort")) != NULL) {
1219 arg_type = lua_type(L, 1);
1220 luaL_error(L,
"Port.info: invalid argument, expected Port, got %s",
1221 lua_typename(L, arg_type));
1225 lua_pushstring(L,
"name"); lua_pushstring(L, pi->getName().c_str()); lua_rawset(L, -3);
1226 lua_pushstring(L,
"desc"); lua_pushstring(L, pi->getDescription().c_str()); lua_rawset(L, -3);
1227 lua_pushstring(L,
"connected"); lua_pushboolean(L, pi->connected()); lua_rawset(L, -3);
1228 lua_pushstring(L,
"isLocal"); lua_pushboolean(L, pi->isLocal()); lua_rawset(L, -3);
1229 lua_pushstring(L,
"type"); lua_pushstring(L, pi->getTypeInfo()->getTypeName().c_str()); lua_rawset(L, -3);
1230 lua_pushstring(L,
"porttype"); lua_pushstring(L, port_type); lua_rawset(L, -3);
1235 static int Port_connect(lua_State *L)
1238 PortInterface **pip1, **pip2;
1239 PortInterface *pi1 = NULL;
1240 PortInterface *pi2 = NULL;
1242 ConnPolicy *cp = NULL;
1244 if((pip1 = (PortInterface**) luaL_testudata(L, 1,
"InputPort")) != NULL) {
1246 }
else if((pip1 = (PortInterface**) luaL_testudata(L, 1,
"OutputPort")) != NULL) {
1250 arg_type = lua_type(L, 1);
1251 luaL_error(L,
"Port.info: invalid argument 1, expected Port, got %s",
1252 lua_typename(L, arg_type));
1254 if((pip2 = (PortInterface**) luaL_testudata(L, 2,
"InputPort")) != NULL) {
1256 }
else if((pip2 = (PortInterface**) luaL_testudata(L, 2,
"OutputPort")) != NULL) {
1260 arg_type = lua_type(L, 2);
1261 luaL_error(L,
"Port.connect: invalid argument 2, expected Port, got %s",
1262 lua_typename(L, arg_type));
1265 if((cpp = (ConnPolicy**) luaL_testudata(L, 3,
"ConnPolicy")) != NULL) {
1270 ret = pi1->connectTo(pi2, *cp);
1272 ret = pi1->connectTo(pi2);
1274 lua_pushboolean(L, ret);
1279 static int Port_disconnect(lua_State *L)
1282 PortInterface **pip1, **pip2;
1283 PortInterface *pi1 = NULL;
1284 PortInterface *pi2 = NULL;
1286 if((pip1 = (PortInterface**) luaL_testudata(L, 1,
"InputPort")) != NULL) {
1288 }
else if((pip1 = (PortInterface**) luaL_testudata(L, 1,
"OutputPort")) != NULL) {
1292 arg_type = lua_type(L, 1);
1293 luaL_error(L,
"Port.info: invalid argument 1, expected Port, got %s",
1294 lua_typename(L, arg_type));
1296 if((pip2 = (PortInterface**) luaL_testudata(L, 2,
"InputPort")) != NULL) {
1298 }
else if((pip2 = (PortInterface**) luaL_testudata(L, 2,
"OutputPort")) != NULL) {
1303 ret = pi1->disconnect(pi2);
1308 lua_pushboolean(L, ret);
1317 gen_push_bxptr(InputPort_push,
"InputPort", InputPortInterface)
1319 static
int InputPort_new(lua_State *L)
1321 const char *type, *name, *desc;
1322 InputPortInterface* ipi;
1323 int argc = lua_gettop(L);
1325 type = luaL_checkstring(L, 1);
1328 name = (argc > 1) ? luaL_checkstring(L, 2) :
"";
1329 desc = (argc > 2) ? luaL_checkstring(L, 3) :
"";
1331 types::TypeInfo *ti = types::TypeInfoRepository::Instance()->type(type);
1333 luaL_error(L,
"InputPort.new: unknown type %s", type);
1335 ipi = ti->inputPort(name);
1338 luaL_error(L,
"InputPort.new: creating port of type %s failed", type);
1341 InputPort_push(L, ipi);
1345 static int InputPort_read(lua_State *L)
1348 InputPortInterface *ip = *(luaM_checkudata_mt_bx(L, 1,
"InputPort", InputPortInterface));
1349 DataSourceBase::shared_ptr dsb;
1350 DataSourceBase::shared_ptr *dsbp;
1354 if ((dsbp = luaM_testudata_mt(L, 2,
"Variable", DataSourceBase::shared_ptr)) != NULL)
1357 dsb = ip->getTypeInfo()->buildValue();
1363 if(fs == NoData) lua_pushstring(L,
"NoData");
1364 else if (fs == NewData) lua_pushstring(L,
"NewData");
1365 else if (fs == OldData) lua_pushstring(L,
"OldData");
1366 else luaL_error(L,
"InputPort.read: unknown FlowStatus returned");
1369 Variable_push_coerce(L, dsb);
1375 static int InputPort_gc(lua_State *L)
1377 InputPortInterface *ip = *(luaM_checkudata_mt_bx(L, 1,
"InputPort", InputPortInterface));
1384 static int InputPort_del(lua_State *L)
1386 InputPortInterface *ip = *(luaM_checkudata_mt_bx(L, 1,
"InputPort", InputPortInterface));
1390 luaL_getmetatable(L,
"__dead__");
1391 lua_setmetatable(L, -2);
1395 static const struct luaL_Reg InputPort_f [] = {
1396 {
"new", InputPort_new },
1397 {
"read", InputPort_read },
1398 {
"info", Port_info },
1399 {
"connect", Port_connect },
1400 {
"disconnect", Port_disconnect },
1401 {
"delete", InputPort_del },
1405 static const struct luaL_Reg InputPort_m [] = {
1406 {
"read", InputPort_read },
1407 {
"info", Port_info },
1408 {
"delete", InputPort_del },
1409 {
"connect", Port_connect },
1410 {
"disconnect", Port_disconnect },
1417 gen_push_bxptr(OutputPort_push,
"OutputPort", OutputPortInterface)
1420 static
int OutputPort_new(lua_State *L)
1422 const char *type, *name, *desc;
1423 OutputPortInterface* opi;
1424 int argc = lua_gettop(L);
1426 type = luaL_checkstring(L, 1);
1429 name = (argc > 1) ? luaL_checkstring(L, 2) :
"";
1430 desc = (argc > 2) ? luaL_checkstring(L, 3) :
"";
1432 types::TypeInfo *ti = types::TypeInfoRepository::Instance()->type(type);
1435 luaL_error(L,
"OutputPort.new: unknown type %s", type);
1437 opi = ti->outputPort(name);
1440 luaL_error(L,
"OutputPort.new: creating port of type %s failed", type);
1443 OutputPort_push(L, opi);
1447 static int OutputPort_write(lua_State *L)
1449 DataSourceBase::shared_ptr dsb;
1450 DataSourceBase::shared_ptr *dsbp;
1452 OutputPortInterface *op = *(luaM_checkudata_mt_bx(L, 1,
"OutputPort", OutputPortInterface));
1455 if ((dsbp = luaM_testudata_mt(L, 2,
"Variable", DataSourceBase::shared_ptr)) != NULL) {
1459 dsb = Variable_fromlua(L, op->getTypeInfo(), 2);
1466 static int OutputPort_gc(lua_State *L)
1468 OutputPortInterface *op = *(luaM_checkudata_mt_bx(L, 1,
"OutputPort", OutputPortInterface));
1475 static int OutputPort_del(lua_State *L)
1477 OutputPortInterface *op = *(luaM_checkudata_mt_bx(L, 1,
"OutputPort", OutputPortInterface));
1481 luaL_getmetatable(L,
"__dead__");
1482 lua_setmetatable(L, -2);
1486 static const struct luaL_Reg OutputPort_f [] = {
1487 {
"new", OutputPort_new },
1488 {
"write", OutputPort_write },
1489 {
"info", Port_info },
1490 {
"connect", Port_connect },
1491 {
"disconnect", Port_disconnect },
1492 {
"delete", OutputPort_del },
1496 static const struct luaL_Reg OutputPort_m [] = {
1497 {
"write", OutputPort_write },
1498 {
"info", Port_info },
1499 {
"connect", Port_connect },
1500 {
"disconnect", Port_disconnect },
1501 {
"delete", OutputPort_del },
1511 OperationInterfacePart *oip;
1512 OperationCallerC *occ;
1521 std::vector<base::DataSourceBase::shared_ptr> dsb_store;
1522 std::vector<internal::Reference*> args;
1523 base::DataSourceBase::shared_ptr call_dsb;
1524 base::DataSourceBase::shared_ptr ret_dsb;
1527 template<
typename T>
1528 int OperationGC(lua_State* L)
1530 T* oh = (T*) lua_touserdata(L, 1);
1532 reinterpret_cast<T*
>(lua_touserdata(L, 1))->~T();
1536 static int Operation_info(lua_State *L)
1539 std::vector<ArgumentDescription> args;
1542 lua_pushstring(L, op->oip->getName().c_str());
1543 lua_pushstring(L, op->oip->description().c_str());
1544 lua_pushstring(L, op->oip->resultType().c_str());
1545 lua_pushinteger(L, op->arity);
1547 args = op->oip->getArgumentList();
1551 for (std::vector<ArgumentDescription>::iterator it = args.begin(); it != args.end(); it++) {
1553 lua_pushstring(L,
"name"); lua_pushstring(L, it->name.c_str()); lua_rawset(L, -3);
1554 lua_pushstring(L,
"type"); lua_pushstring(L, it->type.c_str()); lua_rawset(L, -3);
1555 lua_pushstring(L,
"desc"); lua_pushstring(L, it->description.c_str()); lua_rawset(L, -3);
1556 lua_rawseti(L, -2, i++);
1561 static int __Operation_call(lua_State *L)
1564 DataSourceBase::shared_ptr dsb, *dsbp;
1567 OperationInterfacePart *oip = oh->oip;
1568 unsigned int argc = lua_gettop(L);
1570 if(oh->arity != argc-1)
1571 luaL_error(L,
"Operation.call: wrong number of args. expected %d, got %d", oh->arity, argc-1);
1574 for(
unsigned int arg=2; arg<=argc; arg++) {
1576 if ((dsbp = luaM_testudata_mt(L, arg,
"Variable", DataSourceBase::shared_ptr)) != NULL) {
1580 dsb = Variable_fromlua(L, oip->getArgumentType(arg-1), arg);
1583 oh->dsb_store.push_back(dsb);
1585 if(!dsb->isAssignable())
1586 luaL_error(L,
"Operation.call: argument %d is not assignable.", arg-1);
1588 ret = oh->args[arg-2]->setReference(dsb);
1590 luaL_error(L,
"Operation_call: setReference failed, wrong type of argument?");
1593 if(!oh->occ->call())
1594 luaL_error(L,
"Operation.call: call failed.");
1596 oh->dsb_store.clear();
1599 Variable_push_coerce(L, oh->ret_dsb);
1605 static int __Operation_send(lua_State *L)
1607 DataSourceBase::shared_ptr dsb, *dsbp;
1610 OperationInterfacePart *oip = oh->oip;
1611 unsigned int argc = lua_gettop(L);
1613 if(oh->arity != argc-1)
1614 luaL_error(L,
"Operation.send: wrong number of args. expected %d, got %d", oh->arity, argc-1);
1617 for(
unsigned int arg=2; arg<=argc; arg++) {
1619 if ((dsbp = luaM_testudata_mt(L, arg,
"Variable", DataSourceBase::shared_ptr)) != NULL) {
1623 dsb = Variable_fromlua(L, oip->getArgumentType(arg-1), arg);
1626 oh->dsb_store.push_back(dsb);
1628 oh->args[arg-2]->setReference(dsb);
1631 luaM_pushobject_mt(L,
"SendHandle", SendHandleC)(oh->occ->send());
1635 static int Operation_call(lua_State *L)
1639 ret = __Operation_call(L);
1640 }
catch(
const std::exception &exc) {
1641 luaL_error(L,
"Operation.call: caught exception '%s'", exc.what());
1643 luaL_error(L,
"Operation.call: caught unknown exception");
1648 static int Operation_send(lua_State *L)
1652 ret = __Operation_send(L);
1653 }
catch(
const std::exception &exc) {
1654 luaL_error(L,
"Operation.send: caught exception '%s'", exc.what());
1656 luaL_error(L,
"Operation.send: caught unknown exception");
1662 static const struct luaL_Reg Operation_f [] = {
1663 {
"info", Operation_info },
1664 {
"call", Operation_call },
1665 {
"send", Operation_send },
1670 static const struct luaL_Reg Operation_m [] = {
1671 {
"info", Operation_info },
1672 {
"send", Operation_send },
1673 {
"__call", Operation_call },
1674 {
"__gc", OperationGC<OperationHandle> },
1682 static int Service_getName(lua_State *L)
1684 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1685 lua_pushstring(L, srv->getName().c_str());
1689 static int Service_doc(lua_State *L)
1693 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1694 if(lua_gettop(L) == 1) {
1695 lua_pushstring(L, srv->doc().c_str());
1698 doc = luaL_checkstring(L, 2);
1706 static int Service_getProviderNames(lua_State *L)
1708 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1709 push_vect_str(L, srv->getProviderNames());
1713 static int Service_getOperationNames(lua_State *L)
1715 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1716 push_vect_str(L, srv->getOperationNames());
1721 static int Service_hasOperation(lua_State *L)
1724 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1725 const char* op = luaL_checkstring(L, 2);
1726 ret = srv->hasOperation(op);
1727 lua_pushboolean(L, ret);
1731 static int Service_getPortNames(lua_State *L)
1733 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1734 push_vect_str(L, srv->getPortNames());
1738 static int Service_provides(lua_State *L)
1741 const char* subsrv_str;
1742 Service::shared_ptr srv, subsrv;
1744 srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1753 for(i=2; i<=argc; i++) {
1754 subsrv_str = luaL_checkstring(L, i);
1755 subsrv = srv->getService(subsrv_str);
1757 luaL_error(L,
"Service.provides: no subservice %s of service %s",
1758 subsrv_str, srv->getName().c_str() );
1760 luaM_pushobject_mt(L,
"Service", Service::shared_ptr)(subsrv);
1768 static int Service_getOperation(lua_State *L)
1771 OperationInterfacePart *oip;
1772 Service::shared_ptr srv;
1773 DataSourceBase::shared_ptr dsb;
1774 const types::TypeInfo *ti;
1776 TaskContext *this_tc;
1778 srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1779 op_str = luaL_checkstring(L, 2);
1780 oip = srv->getOperation(op_str);
1783 luaL_error(L,
"Service_getOperation: service %s has no operation %s",
1784 srv->getName().c_str(), op_str);
1788 oh->arity = oip->arity();
1789 oh->args.reserve(oh->arity);
1790 this_tc = __getTC(L);
1792 oh->occ =
new OperationCallerC(oip, op_str, this_tc->engine());
1797 for(
unsigned int arg=1; arg <= oh->arity; arg++) {
1798 std::string type = oip->getArgumentType(arg)->getTypeName();
1799 ti = types::TypeInfoRepository::Instance()->type(type);
1801 luaL_error(L,
"Operation.call: '%s', failed to locate TypeInfo for arg %d of type '%s'",
1802 op_str, arg, type.c_str());
1804 dsb = ti->buildReference((
void*) 0xdeadbeef);
1806 luaL_error(L,
"Operation.call: '%s', failed to build DSB for arg %d of type '%s'",
1807 op_str, arg, type.c_str());
1809 oh->args.push_back(dynamic_cast<internal::Reference*>(dsb.get()));
1814 if(oip->resultType() !=
"void"){
1815 ti = oip->getArgumentType(0);
1817 luaL_error(L,
"Operation.call: '%s', failed to locate TypeInfo for return value of type '%s'",
1818 op_str, oip->resultType().c_str());
1819 oh->ret_dsb=ti->buildValue();
1821 luaL_error(L,
"Operation.call: '%s', failed to build DSB for return value of type '%s'",
1822 op_str, oip->resultType().c_str());
1824 oh->occ->ret(oh->ret_dsb);
1830 if(!oh->occ->ready())
1831 luaL_error(L,
"Service.getOperation: OperationCallerC not ready!");
1836 static int Service_getPort(lua_State *L)
1840 InputPortInterface *ipi;
1841 OutputPortInterface *opi;
1843 Service::shared_ptr srv;
1845 srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1846 name = luaL_checkstring(L, 2);
1848 pi = srv->getPort(name);
1850 luaL_error(L,
"Service.getPort: service %s has no port %",
1851 srv->getName().c_str(), name);
1854 if ((ipi = dynamic_cast<InputPortInterface *> (pi)) != NULL)
1855 InputPort_push(L, ipi);
1856 else if ((opi = dynamic_cast<OutputPortInterface *> (pi)) != NULL)
1857 OutputPort_push(L, opi);
1859 luaL_error(L,
"Service.getPort: unknown port type returned");
1864 static int Service_getProperty(lua_State *L)
1869 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1870 name = luaL_checkstring(L, 2);
1872 prop = srv->getProperty(name);
1875 luaL_error(L,
"%s failed. No such property", __FILE__);
1877 Property_push(L, prop);
1881 static int Service_getPropertyNames(lua_State *L)
1883 Service::shared_ptr srv;
1884 srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1885 std::vector<std::string> plist = srv->properties()->list();
1886 push_vect_str(L, plist);
1890 static int Service_getProperties(lua_State *L)
1892 Service::shared_ptr srv;
1893 srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1894 vector<PropertyBase*> props = srv->properties()->getProperties();
1897 lua_createtable(L, props.size(), 0);
1898 for(vector<PropertyBase*>::iterator it = props.begin(); it != props.end(); ++it) {
1899 Property_push(L, *it);
1900 lua_rawseti(L, -2, key++);
1906 static int Service_getAttribute(lua_State *L)
1909 AttributeBase *prop;
1911 Service::shared_ptr srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1912 name = luaL_checkstring(L, 2);
1914 prop = srv->getAttribute(name);
1917 luaL_error(L,
"%s failed. No such Attribute", __FILE__);
1919 Attribute_push(L, prop);
1923 static int Service_getAttributeNames(lua_State *L)
1925 Service::shared_ptr srv;
1926 srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1927 std::vector<std::string> plist = srv->getAttributeNames();
1928 push_vect_str(L, plist);
1932 static int Service_getAttributes(lua_State *L)
1934 Service::shared_ptr srv;
1935 srv = *(luaM_checkudata_mt(L, 1,
"Service", Service::shared_ptr));
1936 vector<AttributeBase*> props = srv->getValues();
1939 lua_createtable(L, props.size(), 0);
1940 for(vector<AttributeBase*>::iterator it = props.begin(); it != props.end(); ++it) {
1941 Attribute_push(L, *it);
1942 lua_rawseti(L, -2, key++);
1948 static const struct luaL_Reg Service_f [] = {
1949 {
"getName", Service_getName },
1950 {
"doc", Service_doc },
1951 {
"getProviderNames", Service_getProviderNames },
1952 {
"getOperationNames", Service_getOperationNames },
1953 {
"hasOperation", Service_hasOperation },
1954 {
"getPortNames", Service_getPortNames },
1955 {
"provides", Service_provides },
1956 {
"getOperation", Service_getOperation },
1957 {
"getPort", Service_getPort },
1958 {
"getProperty", Service_getProperty },
1959 {
"getProperties", Service_getProperties },
1960 {
"getPropertyNames", Service_getPropertyNames },
1961 {
"getAttribute", Service_getAttribute },
1962 {
"getAttributes", Service_getAttributes },
1963 {
"getAttributeNames", Service_getAttributeNames },
1967 static const struct luaL_Reg Service_m [] = {
1968 {
"getName", Service_getName },
1969 {
"doc", Service_doc },
1970 {
"getProviderNames", Service_getProviderNames },
1971 {
"getOperationNames", Service_getOperationNames },
1972 {
"hasOperation", Service_hasOperation },
1973 {
"getPortNames", Service_getPortNames },
1974 {
"provides", Service_provides },
1975 {
"getOperation", Service_getOperation },
1976 {
"getPort", Service_getPort },
1977 {
"getProperty", Service_getProperty },
1978 {
"getProperties", Service_getProperties },
1979 {
"getPropertyNames", Service_getPropertyNames },
1980 {
"getAttribute", Service_getAttribute },
1981 {
"getAttributes", Service_getAttributes },
1982 {
"getAttributeNames", Service_getAttributeNames },
1983 {
"__gc", GCMethod<Service::shared_ptr> },
1991 gen_push_bxptr(ServiceRequester_push,
"ServiceRequester", ServiceRequester)
1993 static int ServiceRequester_getRequestName(lua_State *L)
1995 ServiceRequester *sr;
1997 sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
1998 lua_pushstring(L, sr->getRequestName().c_str());
2002 static int ServiceRequester_getRequesterNames(lua_State *L)
2004 ServiceRequester *sr;
2005 sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
2006 push_vect_str(L, sr->getRequesterNames());
2010 static int ServiceRequester_ready(lua_State *L)
2013 ServiceRequester *sr;
2014 sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
2016 lua_pushboolean(L, ret);
2020 static int ServiceRequester_disconnect(lua_State *L)
2022 ServiceRequester *sr;
2023 sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
2028 static int ServiceRequester_requires(lua_State *L)
2031 const char* subsr_str;
2032 ServiceRequester *sr;
2033 ServiceRequester::shared_ptr subsr;
2035 sr = *(luaM_checkudata_bx(L, 1, ServiceRequester));
2036 argc = lua_gettop(L);
2044 for(i=2; i<=argc; i++) {
2045 subsr_str = luaL_checkstring(L, i);
2046 subsr = sr->requires(subsr_str);
2048 luaL_error(L,
"ServiceRequester: no required subservice %s of service %s",
2049 subsr_str, sr->getRequestName().c_str());
2051 ServiceRequester_push(L, subsr.get());
2059 static const struct luaL_Reg ServiceRequester_f [] = {
2060 {
"getRequestName", ServiceRequester_getRequestName },
2061 {
"getRequesterNames", ServiceRequester_getRequesterNames },
2062 {
"ready", ServiceRequester_ready },
2063 {
"disconnect", ServiceRequester_disconnect },
2064 {
"requires", ServiceRequester_requires },
2068 static const struct luaL_Reg ServiceRequester_m [] = {
2069 {
"getRequestName", ServiceRequester_getRequestName },
2070 {
"getRequesterNames", ServiceRequester_getRequesterNames },
2071 {
"ready", ServiceRequester_ready },
2072 {
"disconnect", ServiceRequester_disconnect },
2073 {
"requires", ServiceRequester_requires },
2082 gen_push_bxptr(TaskContext_push,
"TaskContext", TaskContext)
2084 static int TaskContext_getName(lua_State *L)
2087 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2088 s = tc->getName().c_str();
2089 lua_pushstring(L, s);
2093 static int TaskContext_start(lua_State *L)
2095 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2096 bool b = tc->start();
2097 lua_pushboolean(L, b);
2101 static int TaskContext_stop(lua_State *L)
2103 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2104 bool b = tc->stop();
2105 lua_pushboolean(L, b);
2109 static int TaskContext_configure(lua_State *L)
2111 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2112 bool ret = tc->configure();
2113 lua_pushboolean(L, ret);
2117 static int TaskContext_activate(lua_State *L)
2119 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2120 bool ret = tc->activate();
2121 lua_pushboolean(L, ret);
2125 static int TaskContext_cleanup(lua_State *L)
2127 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2128 bool ret = tc->cleanup();
2129 lua_pushboolean(L, ret);
2133 static int TaskContext_error(lua_State *L)
2135 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2140 static int TaskContext_recover(lua_State *L)
2142 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2143 bool ret = tc->recover();
2144 lua_pushboolean(L, ret);
2148 static int TaskContext_getState(lua_State *L)
2150 TaskCore::TaskState ts;
2151 TaskContext **tc = (TaskContext**) luaM_checkudata_bx(L, 1, TaskContext);
2152 ts = (*tc)->getTaskState();
2155 case TaskCore::Init: lua_pushstring(L,
"Init");
break;
2156 case TaskCore::PreOperational: lua_pushstring(L,
"PreOperational");
break;
2157 case TaskCore::FatalError: lua_pushstring(L,
"FatalError");
break;
2158 case TaskCore::Exception: lua_pushstring(L,
"Exception");
break;
2159 case TaskCore::Stopped: lua_pushstring(L,
"Stopped");
break;
2160 case TaskCore::Running: lua_pushstring(L,
"Running");
break;
2161 case TaskCore::RunTimeError: lua_pushstring(L,
"RunTimeError");
break;
2162 default: lua_pushstring(L,
"unknown");
2169 static int TaskContext_getPeers(lua_State *L)
2171 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2172 std::vector<std::string> plist = tc->getPeerList();
2173 push_vect_str(L, plist);
2178 static int TaskContext_addPeer(lua_State *L)
2181 TaskContext *
self = *(luaM_checkudata_bx(L, 1, TaskContext));
2182 TaskContext *peer = *(luaM_checkudata_bx(L, 2, TaskContext));
2183 ret =
self->addPeer(peer);
2184 lua_pushboolean(L, ret);
2189 static int TaskContext_connectPeers(lua_State *L)
2192 TaskContext *
self = *(luaM_checkudata_bx(L, 1, TaskContext));
2193 TaskContext *peer = *(luaM_checkudata_bx(L, 2, TaskContext));
2194 ret =
self->connectPeers(peer);
2195 lua_pushboolean(L, ret);
2200 static int TaskContext_removePeer(lua_State *L)
2203 TaskContext *
self = *(luaM_checkudata_bx(L, 1, TaskContext));
2204 peer = luaL_checkstring(L, 2);
2205 self->removePeer(peer);
2210 static int TaskContext_getPeer(lua_State *L)
2212 std::string strpeer;
2214 TaskContext *
self = *(luaM_checkudata_bx(L, 1, TaskContext));
2215 strpeer = luaL_checkstring(L, 2);
2216 peer =
self->getPeer(strpeer);
2219 luaL_error(L,
"TaskContext.getPeer: no peer %s", strpeer.c_str());
2223 TaskContext_push(L, peer);
2228 static int TaskContext_getPortNames(lua_State *L)
2230 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2231 std::vector<std::string> plist = tc->ports()->getPortNames();
2232 push_vect_str(L, plist);
2236 static int TaskContext_addPort(lua_State *L)
2238 const char* name, *desc;
2240 int argc = lua_gettop(L);
2241 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2243 pi = (PortInterface**) luaL_testudata(L, 2,
"InputPort");
2244 if(pi)
goto check_name;
2246 pi = (PortInterface**) luaL_testudata(L, 2,
"OutputPort");
2247 if(pi)
goto check_name;
2249 return luaL_error(L,
"addPort: invalid argument, not a Port");
2253 name = luaL_checkstring(L, 3);
2254 (*pi)->setName(name);
2258 desc = luaL_checkstring(L, 4);
2262 tc->ports()->addPort(**pi);
2266 static int TaskContext_addEventPort(lua_State *L)
2268 const char* name, *desc;
2269 InputPortInterface **ipi;
2270 int argc = lua_gettop(L);
2271 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2273 if((ipi = (InputPortInterface**) luaL_testudata(L, 2,
"InputPort")) == NULL)
2274 return luaL_error(L,
"addEventPort: invalid argument, not an InputPort");
2277 name = luaL_checkstring(L, 3);
2278 (*ipi)->setName(name);
2282 desc = luaL_checkstring(L, 4);
2286 tc->ports()->addEventPort(**ipi);
2290 static int TaskContext_getPort(lua_State *L)
2294 InputPortInterface *ipi;
2295 OutputPortInterface *opi;
2297 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2298 name = luaL_checkstring(L, 2);
2300 pi = tc->getPort(name);
2302 luaL_error(L,
"TaskContext.getPort: no port %s for taskcontext %s",
2303 name, tc->getName().c_str());
2306 if ((ipi = dynamic_cast<InputPortInterface *> (pi)) != NULL)
2307 InputPort_push(L, ipi);
2308 else if ((opi = dynamic_cast<OutputPortInterface *> (pi)) != NULL)
2309 OutputPort_push(L, opi);
2311 luaL_error(L,
"TaskContext.getPort: unknown port returned");
2316 static int TaskContext_removePort(lua_State *L)
2318 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2319 const char *port = luaL_checkstring(L, 2);
2320 tc->ports()->removePort(port);
2324 static int TaskContext_addProperty(lua_State *L)
2326 const char *name, *desc;
2327 int argc = lua_gettop(L);
2328 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2329 PropertyBase *pb = *(luaM_checkudata_mt_bx(L, 2,
"Property", PropertyBase));
2332 name = luaL_checkstring(L, 3);
2337 desc = luaL_checkstring(L, 4);
2338 pb->setDescription(desc);
2342 if(!tc->addProperty(*pb))
2343 luaL_error(L,
"TaskContext.addProperty: failed to add property %s.",
2344 pb->getName().c_str());
2349 static int TaskContext_getProperty(lua_State *L)
2354 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2355 name = luaL_checkstring(L, 2);
2357 prop = tc->getProperty(name);
2360 luaL_error(L,
"%s failed. No such property", __FILE__);
2362 Property_push(L, prop);
2367 static int TaskContext_getPropertyNames(lua_State *L)
2369 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2370 std::vector<std::string> plist = tc->properties()->list();
2371 push_vect_str(L, plist);
2375 static int TaskContext_getProperties(lua_State *L)
2377 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2378 vector<PropertyBase*> props = tc->properties()->getProperties();
2381 lua_createtable(L, props.size(), 0);
2382 for(vector<PropertyBase*>::iterator it = props.begin(); it != props.end(); ++it) {
2383 Property_push(L, *it);
2384 lua_rawseti(L, -2, key++);
2390 static int TaskContext_removeProperty(lua_State *L)
2395 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2396 name = luaL_checkstring(L, 2);
2398 prop = tc->getProperty(name);
2401 luaL_error(L,
"%s failed. No such property", __FILE__);
2403 tc->properties()->remove(prop);
2407 static int TaskContext_addAttribute(lua_State *L)
2409 int argc = lua_gettop(L);
2410 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2411 AttributeBase *pb = *(luaM_checkudata_mt_bx(L, 2,
"Attribute", AttributeBase));
2414 const char *name = luaL_checkstring(L, 3);
2418 if(!tc->addAttribute(*pb))
2419 luaL_error(L,
"TaskContext.addAttribute: failed to add attribute %s.",
2420 pb->getName().c_str());
2425 static int TaskContext_getAttribute(lua_State *L)
2428 AttributeBase *prop;
2430 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2431 name = luaL_checkstring(L, 2);
2433 prop = tc->getAttribute(name);
2436 luaL_error(L,
"%s failed. No such Attribute", __FILE__);
2438 Attribute_push(L, prop);
2443 static int TaskContext_getAttributeNames(lua_State *L)
2445 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2446 std::vector<std::string> plist = tc->attributes()->getAttributeNames();
2447 push_vect_str(L, plist);
2451 static int TaskContext_getAttributes(lua_State *L)
2453 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2454 vector<AttributeBase*> props = tc->attributes()->getValues();
2457 lua_createtable(L, props.size(), 0);
2458 for(vector<AttributeBase*>::iterator it = props.begin(); it != props.end(); ++it) {
2459 Attribute_push(L, *it);
2460 lua_rawseti(L, -2, key++);
2466 static int TaskContext_removeAttribute(lua_State *L)
2468 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2469 const char *name = luaL_checkstring(L, 2);
2471 if(!tc->attributes()->hasAttribute(name))
2472 luaL_error(L,
"%s failed. No such attribute", __FILE__);
2474 tc->attributes()->removeAttribute(name);
2479 static int TaskContext_getOps(lua_State *L)
2481 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2482 std::vector<std::string> oplst = tc->operations()->getNames();
2483 push_vect_str(L, oplst);
2488 static int TaskContext_getOpInfo(lua_State *L)
2491 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2492 const char *op = luaL_checkstring(L, 2);
2493 std::vector<ArgumentDescription> args;
2495 if(!tc->operations()->hasMember(op))
2496 luaL_error(L,
"TaskContext.getOpInfo failed: no such operation");
2498 lua_pushstring(L, tc->operations()->getResultType(op).c_str());
2499 lua_pushinteger(L, tc->operations()->getArity(op));
2500 lua_pushstring(L, tc->operations()->getDescription(op).c_str());
2502 args = tc->operations()->getArgumentList(op);
2506 for (std::vector<ArgumentDescription>::iterator it = args.begin(); it != args.end(); it++) {
2508 lua_pushstring(L,
"name"); lua_pushstring(L, it->name.c_str()); lua_rawset(L, -3);
2509 lua_pushstring(L,
"type"); lua_pushstring(L, it->type.c_str()); lua_rawset(L, -3);
2510 lua_pushstring(L,
"desc"); lua_pushstring(L, it->description.c_str()); lua_rawset(L, -3);
2511 lua_rawseti(L, -2, i++);
2517 static int TaskContext_provides(lua_State *L)
2519 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2520 Service::shared_ptr srv = tc->provides();
2523 luaL_error(L,
"TaskContext.provides: no default service");
2526 luaM_pushobject_mt(L,
"Service", Service::shared_ptr)(srv);
2528 return Service_provides(L);
2531 static int TaskContext_getProviderNames(lua_State *L)
2533 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2534 Service::shared_ptr srv = tc->provides();
2535 push_vect_str(L, srv->getProviderNames());
2539 static int TaskContext_requires(lua_State *L)
2541 ServiceRequester::shared_ptr sr;
2542 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2543 sr = tc->requires();
2546 luaL_error(L,
"TaskContext.requires returned NULL");
2548 ServiceRequester_push(L, sr.get());
2550 return ServiceRequester_requires(L);
2553 static int TaskContext_connectServices(lua_State *L)
2556 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2557 TaskContext *peer = *(luaM_checkudata_bx(L, 2, TaskContext));
2558 ret = tc->connectServices(peer);
2559 lua_pushboolean(L, ret);
2563 static int TaskContext_hasOperation(lua_State *L)
2565 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2566 Service::shared_ptr srv = tc->provides();
2569 luaL_error(L,
"TaskContext.provides: no default service");
2572 luaM_pushobject_mt(L,
"Service", Service::shared_ptr)(srv);
2574 return Service_hasOperation(L);
2578 static int TaskContext_getOperation(lua_State *L)
2580 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2581 Service::shared_ptr srv = tc->provides();
2584 luaL_error(L,
"TaskContext.getOperation: no default service");
2587 luaM_pushobject_mt(L,
"Service", Service::shared_ptr)(srv);
2589 return Service_getOperation(L);
2596 static void SendStatus_push(lua_State *L, SendStatus ss)
2599 case SendSuccess: lua_pushstring(L,
"SendSuccess");
break;
2600 case SendNotReady: lua_pushstring(L,
"SendNotReady");
break;
2601 case SendFailure: lua_pushstring(L,
"SendFailure");
break;
2602 default: lua_pushstring(L,
"unkown");
2606 static int __SendHandle_collect(lua_State *L,
bool block)
2608 unsigned int coll_argc;
2609 std::vector<DataSourceBase::shared_ptr> coll_args;
2611 const types::TypeInfo *ti;
2612 OperationInterfacePart *oip;
2613 DataSourceBase::shared_ptr dsb, *dsbp;
2615 unsigned int argc = lua_gettop(L);
2616 SendHandleC *shc = luaM_checkudata_mt(L, 1,
"SendHandle", SendHandleC);
2619 oip = shc->getOrp();
2620 coll_argc = oip->collectArity();
2622 if(block && (argc == 1)) {
2624 for(
unsigned int i=1; i<=coll_argc; i++) {
2625 ti = oip->getCollectType(i);
2626 dsb = ti->buildValue();
2627 coll_args.push_back(dsb);
2630 }
else if (argc-1 == coll_argc) {
2632 if (!shc->ready()) {
2633 for(
unsigned int arg=2; arg<=argc; arg++) {
2634 if ((dsbp = luaM_testudata_mt(L, arg,
"Variable", DataSourceBase::shared_ptr)) != NULL)
2637 luaL_error(L,
"SendHandle.collect: expected Variable argument at position %d", arg-1);
2643 luaL_error(L,
"SendHandle.collect: wrong number of args. expected either 0 or %d, got %d",
2646 luaL_error(L,
"SendHandle.collectIfDone: wrong number of args. expected %d, got %d",
2651 if(block) ss = shc->collect();
2652 else ss = shc->collectIfDone();
2654 SendStatus_push(L, ss);
2656 if(ss == SendSuccess) {
2657 for (
unsigned int i=0; i<coll_args.size(); i++)
2658 Variable_push_coerce(L, coll_args[i]);
2661 return coll_args.size() + 1;
2669 static int SendHandle_collect(lua_State *L) {
return __SendHandle_collect(L,
true); }
2670 static int SendHandle_collectIfDone(lua_State *L) {
return __SendHandle_collect(L,
false); }
2672 static const struct luaL_Reg SendHandle_f [] = {
2673 {
"collect", SendHandle_collect },
2674 {
"collectIfDone", SendHandle_collectIfDone },
2678 static const struct luaL_Reg SendHandle_m [] = {
2679 {
"collect", SendHandle_collect },
2680 {
"collectIfDone", SendHandle_collectIfDone },
2681 {
"__gc", GCMethod<SendHandleC> },
2686 static int TaskContext_del(lua_State *L)
2688 TaskContext *tc = *(luaM_checkudata_bx(L, 1, TaskContext));
2692 luaL_getmetatable(L,
"__dead__");
2693 lua_setmetatable(L, -2);
2697 static const struct luaL_Reg TaskContext_f [] = {
2698 {
"getName", TaskContext_getName },
2699 {
"start", TaskContext_start },
2700 {
"stop", TaskContext_stop },
2701 {
"configure", TaskContext_configure },
2702 {
"activate", TaskContext_activate },
2703 {
"cleanup", TaskContext_cleanup },
2704 {
"error", TaskContext_error },
2705 {
"recover", TaskContext_recover },
2706 {
"getState", TaskContext_getState },
2707 {
"getPeers", TaskContext_getPeers },
2708 {
"addPeer", TaskContext_addPeer },
2709 {
"removePeer", TaskContext_removePeer },
2710 {
"getPeer", TaskContext_getPeer },
2711 {
"getPortNames", TaskContext_getPortNames },
2712 {
"addPort", TaskContext_addPort },
2713 {
"addEventPort", TaskContext_addEventPort },
2714 {
"getPort", TaskContext_getPort },
2715 {
"removePort", TaskContext_removePort },
2716 {
"addProperty", TaskContext_addProperty },
2717 {
"getProperty", TaskContext_getProperty },
2718 {
"getProperties", TaskContext_getProperties },
2719 {
"getPropertyNames", TaskContext_getPropertyNames },
2720 {
"removeProperty", TaskContext_removeProperty },
2721 {
"addAttribute", TaskContext_addAttribute },
2722 {
"getAttribute", TaskContext_getAttribute },
2723 {
"getAttributes", TaskContext_getAttributes },
2724 {
"getAttributeNames", TaskContext_getAttributeNames },
2725 {
"removeAttribute", TaskContext_removeAttribute },
2726 {
"getOps", TaskContext_getOps },
2727 {
"getOpInfo", TaskContext_getOpInfo },
2728 {
"hasOperation", TaskContext_hasOperation },
2729 {
"provides", TaskContext_provides },
2730 {
"getProviderNames", TaskContext_getProviderNames },
2731 {
"connectServices", TaskContext_connectServices },
2732 {
"getOperation", TaskContext_getOperation },
2733 {
"delete", TaskContext_del },
2737 static const struct luaL_Reg TaskContext_m [] = {
2738 {
"getName", TaskContext_getName },
2739 {
"start", TaskContext_start },
2740 {
"stop", TaskContext_stop },
2741 {
"configure", TaskContext_configure },
2742 {
"activate", TaskContext_activate },
2743 {
"cleanup", TaskContext_cleanup },
2744 {
"error", TaskContext_error },
2745 {
"recover", TaskContext_recover },
2746 {
"getState", TaskContext_getState },
2747 {
"getPeers", TaskContext_getPeers },
2748 {
"addPeer", TaskContext_addPeer },
2749 {
"connectPeers", TaskContext_connectPeers },
2750 {
"removePeer", TaskContext_removePeer },
2751 {
"getPeer", TaskContext_getPeer },
2752 {
"getPortNames", TaskContext_getPortNames },
2753 {
"addPort", TaskContext_addPort },
2754 {
"addEventPort", TaskContext_addEventPort },
2755 {
"getPort", TaskContext_getPort },
2756 {
"removePort", TaskContext_removePort },
2757 {
"addProperty", TaskContext_addProperty },
2758 {
"getProperty", TaskContext_getProperty },
2759 {
"getProperties", TaskContext_getProperties },
2760 {
"getPropertyNames", TaskContext_getPropertyNames },
2761 {
"addAttribute", TaskContext_addAttribute },
2762 {
"getAttribute", TaskContext_getAttribute },
2763 {
"getAttributes", TaskContext_getAttributes },
2764 {
"getAttributeNames", TaskContext_getAttributeNames },
2765 {
"removeAttribute", TaskContext_removeAttribute },
2766 {
"removeProperty", TaskContext_removeProperty },
2767 {
"getOps", TaskContext_getOps },
2768 {
"getOpInfo", TaskContext_getOpInfo },
2769 {
"hasOperation", TaskContext_hasOperation },
2770 {
"provides", TaskContext_provides },
2771 {
"getProviderNames", TaskContext_getProviderNames },
2772 {
"requires", TaskContext_requires },
2773 {
"connectServices", TaskContext_connectServices },
2774 {
"getOperation", TaskContext_getOperation },
2775 {
"delete", TaskContext_del },
2787 class EEHook :
public base::ExecutableInterface
2795 EEHook(lua_State *_L, std::string _func) { L = _L; func = _func; tc = __getTC(L); }
2796 bool execute() {
return call_func(L, func.c_str(), tc, 1, 1); }
2799 static int EEHook_new(lua_State *L)
2802 func = luaL_checkstring(L, 1);
2803 luaM_pushobject(L,
EEHook)(L, func);
2807 static int EEHook_enable(lua_State *L)
2810 TaskContext *tc = __getTC(L);
2811 lua_pushboolean(L, tc->engine()->runFunction(eeh));
2815 static int EEHook_disable(lua_State *L)
2817 TaskContext *tc = __getTC(L);
2818 lua_pushboolean(L, tc->engine()->removeFunction(eeh));
2823 static int EEHook_gc(lua_State *L)
2827 reinterpret_cast<EEHook*
>(lua_touserdata(L, 1))->~
EEHook();
2832 static const struct luaL_Reg EEHook_f [] = {
2833 {
"new", EEHook_new },
2834 {
"enable", EEHook_enable },
2835 {
"disable", EEHook_disable },
2839 static const struct luaL_Reg EEHook_m [] = {
2840 {
"enable", EEHook_enable },
2841 {
"disable", EEHook_disable },
2849 static const char *
const loglevels[] = {
2850 "Never",
"Fatal",
"Critical",
"Error",
"Warning",
"Info",
"Debug",
"RealTime", NULL
2853 static int Logger_setLogLevel(lua_State *L)
2855 Logger::LogLevel ll = (Logger::LogLevel) luaL_checkoption(L, 1, NULL, loglevels);
2856 log().setLogLevel(ll);
2860 static int Logger_getLogLevel(lua_State *L)
2862 Logger::LogLevel ll = log().getLogLevel();
2865 case Logger::Never: lua_pushstring(L,
"Never");
break;
2866 case Logger::Fatal: lua_pushstring(L,
"Fatal");
break;
2867 case Logger::Critical: lua_pushstring(L,
"Critical");
break;
2868 case Logger::Error: lua_pushstring(L,
"Error");
break;
2869 case Logger::Warning: lua_pushstring(L,
"Warning");
break;
2870 case Logger::Info: lua_pushstring(L,
"Info");
break;
2871 case Logger::Debug: lua_pushstring(L,
"Debug");
break;
2872 case Logger::RealTime: lua_pushstring(L,
"RealTime");
break;
2874 lua_pushstring(L,
"unknown");
2879 static int Logger_log(lua_State *L)
2882 for(
int i=1; i<=lua_gettop(L); i++) {
2883 mes = luaL_checkstring(L, i);
2884 Logger::log() << mes;
2886 Logger::log() << endlog();
2890 static int Logger_logl(lua_State *L)
2893 Logger::LogLevel ll = (Logger::LogLevel) luaL_checkoption(L, 1, NULL, loglevels);
2894 for(
int i=2; i<=lua_gettop(L); i++) {
2895 mes = luaL_checkstring(L, i);
2896 Logger::log(ll) << mes;
2898 Logger::log(ll) << endlog();
2904 static int getTime(lua_State *L)
2906 unsigned long nsec, sec;
2907 RTT::os::TimeService::nsecs total_nsec = TimeService::Instance()->getNSecs();
2908 sec = total_nsec / 1000000000;
2909 nsec = total_nsec % 1000000000;
2910 lua_pushinteger(L, sec);
2911 lua_pushinteger(L, nsec);
2915 static int rtt_sleep(lua_State *L)
2918 ts.tv_sec = luaL_checknumber(L, 1);
2919 ts.tv_nsec = luaL_checknumber(L, 2);
2920 rtos_nanosleep(&ts, NULL);
2924 static int getTC(lua_State *L)
2926 lua_pushstring(L,
"this_TC");
2927 lua_rawget(L, LUA_REGISTRYINDEX);
2931 static TaskContext* __getTC(lua_State *L)
2935 tc = *(luaM_checkudata_bx(L, -1, TaskContext));
2941 static int globals_getNames(lua_State *L)
2943 GlobalsRepository::shared_ptr gr = GlobalsRepository::Instance();
2944 push_vect_str(L, gr->getAttributeNames() );
2948 static int globals_get(lua_State *L)
2951 base::AttributeBase *ab;
2952 DataSourceBase::shared_ptr dsb;
2954 name = luaL_checkstring(L, 1);
2955 GlobalsRepository::shared_ptr gr = GlobalsRepository::Instance();
2957 ab = gr->getAttribute(name);
2960 Variable_push_coerce(L, ab->getDataSource());
2968 static int provides_global(lua_State *L)
2970 luaM_pushobject_mt(L,
"Service", Service::shared_ptr)(GlobalService::Instance());
2972 return Service_provides(L);
2975 static int rtt_services(lua_State *L)
2977 push_vect_str(L, PluginLoader::Instance()->listServices());
2981 static int rtt_typekits(lua_State *L)
2983 push_vect_str(L, PluginLoader::Instance()->listTypekits());
2987 static int rtt_types(lua_State *L)
2989 push_vect_str(L, TypeInfoRepository::Instance()->getTypes());
2993 static const struct luaL_Reg rtt_f [] = {
2994 {
"getTime", getTime },
2995 {
"sleep", rtt_sleep },
2997 {
"globals_getNames", globals_getNames },
2998 {
"globals_get", globals_get },
2999 {
"provides", provides_global },
3000 {
"services", rtt_services },
3001 {
"typekits", rtt_typekits },
3002 {
"types", rtt_types },
3003 {
"setLogLevel", Logger_setLogLevel },
3004 {
"getLogLevel", Logger_getLogLevel },
3005 {
"log", Logger_log },
3006 {
"logl", Logger_logl },
3010 extern "C" int luaopen_rtt(lua_State *L);
3012 int luaopen_rtt(lua_State *L)
3015 lua_replace(L, LUA_ENVIRONINDEX);
3017 luaL_newmetatable(L,
"__dead__");
3027 luaL_newmetatable(L,
"TaskContext");
3028 lua_pushvalue(L, -1);
3029 lua_setfield(L, -2,
"__index");
3030 luaL_register(L, NULL, TaskContext_m);
3031 luaL_register(L,
"rtt.TaskContext", TaskContext_f);
3033 luaL_newmetatable(L,
"Operation");
3034 lua_pushvalue(L, -1);
3035 lua_setfield(L, -2,
"__index");
3036 luaL_register(L, NULL, Operation_m);
3037 luaL_register(L,
"rtt.Operation", Operation_f);
3039 luaL_newmetatable(L,
"Service");
3040 lua_pushvalue(L, -1);
3041 lua_setfield(L, -2,
"__index");
3042 luaL_register(L, NULL, Service_m);
3043 luaL_register(L,
"rtt.Service", Service_f);
3045 luaL_newmetatable(L,
"ServiceRequester");
3046 lua_pushvalue(L, -1);
3047 lua_setfield(L, -2,
"__index");
3048 luaL_register(L, NULL, ServiceRequester_m);
3049 luaL_register(L,
"rtt.ServiceRequester", ServiceRequester_f);
3051 luaL_newmetatable(L,
"SendHandle");
3052 lua_pushvalue(L, -1);
3053 lua_setfield(L, -2,
"__index");
3054 luaL_register(L, NULL, SendHandle_m);
3055 luaL_register(L,
"rtt.SendHandle", SendHandle_f);
3057 luaL_newmetatable(L,
"InputPort");
3058 lua_pushvalue(L, -1);
3059 lua_setfield(L, -2,
"__index");
3060 luaL_register(L, NULL, InputPort_m);
3061 luaL_register(L,
"rtt.InputPort", InputPort_f);
3063 luaL_newmetatable(L,
"OutputPort");
3064 lua_pushvalue(L, -1);
3065 lua_setfield(L, -2,
"__index");
3066 luaL_register(L, NULL, OutputPort_m);
3067 luaL_register(L,
"rtt.OutputPort", OutputPort_f);
3069 luaL_newmetatable(L,
"Variable");
3070 lua_pushvalue(L, -1);
3071 lua_setfield(L, -2,
"__index");
3072 luaL_register(L, NULL, Variable_m);
3073 luaL_register(L,
"rtt.Variable", Variable_f);
3075 luaL_newmetatable(L,
"Property");
3076 lua_pushvalue(L, -1);
3077 lua_setfield(L, -2,
"__index");
3078 luaL_register(L, NULL, Property_m);
3079 luaL_register(L,
"rtt.Property", Property_f);
3081 luaL_newmetatable(L,
"Attribute");
3082 lua_pushvalue(L, -1);
3083 lua_setfield(L, -2,
"__index");
3084 luaL_register(L, NULL, Attribute_m);
3085 luaL_register(L,
"rtt.Attribute", Attribute_f);
3087 luaL_newmetatable(L,
"EEHook");
3088 lua_pushvalue(L, -1);
3089 lua_setfield(L, -2,
"__index");
3090 luaL_register(L, NULL, EEHook_m);
3091 luaL_register(L,
"rtt.EEHook", EEHook_f);
3094 luaL_register(L,
"rtt", rtt_f);
3100 int set_context_tc(TaskContext *tc, lua_State *L)
3102 TaskContext **new_tc;
3103 lua_pushstring(L,
"this_TC");
3104 new_tc = (TaskContext**) lua_newuserdata(L,
sizeof(TaskContext*));
3105 *new_tc = (TaskContext*) tc;
3106 luaL_getmetatable(L,
"TaskContext");
3107 lua_setmetatable(L, -2);
3108 lua_rawset(L, LUA_REGISTRYINDEX);
3115 bool call_func(lua_State *L,
const char *fname, TaskContext *tc,
3116 int require_function,
int require_result)
3119 int num_res = (require_result != 0) ? 1 : 0;
3120 lua_getglobal(L, fname);
3122 if(lua_isnil(L, -1)) {
3124 if(require_function)
3125 luaL_error(L,
"%s: no (required) Lua function %s", tc->getName().c_str(), fname);
3130 if (lua_pcall(L, 0, num_res, 0) != 0) {
3131 Logger::log(Logger::Error) <<
"LuaComponent '"<< tc->getName() <<
"': error calling function " 3132 << fname <<
": " << lua_tostring(L, -1) << endlog();
3138 if(require_result) {
3139 if (!lua_isboolean(L, -1)) {
3140 Logger::log(Logger::Error) <<
"LuaComponent '" << tc->getName() <<
"': " << fname
3141 <<
" must return a bool but returned a " 3142 << lua_typename(L, lua_type(L, -1)) << endlog();
3147 ret = lua_toboolean(L, -1);