Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. void doFunction(lua_State *L, int argc, t_atom *argv)
  2. {
  3. lua_getglobal(L, "require");
  4. lua_pushstring(L, "foo"); //require("foo")
  5. if (lua_pcall(L, 1, LUA_MULTRET, 0))
  6. {
  7. error("error: %s", lua_tostring(L, -1));
  8. lua_pop(L, 1);
  9. return;
  10. }
  11. int top = lua_gettop(L);
  12. lua_getfield(L, -1, "test"); //find foo.test() function
  13. if (lua_type(L, -1) != LUA_TFUNCTION)
  14. {
  15. lua_pop(L, 2);
  16. return;
  17. }
  18. lua_newtable(L);
  19. for (int i = 0; i < argc; ++i) //convert array to lua table
  20. {
  21. lua_pushinteger(L, static_cast<lua_Integer>(i + 1));
  22. if (argv[i].a_type == A_FLOAT)
  23. lua_pushnumber(L, static_cast<lua_Number>(argv[i].a_w.w_float));
  24. else if (argv[i].a_type == A_SYMBOL)
  25. lua_pushstring(L, argv[i].a_w.w_symbol->s_name);
  26. lua_settable(L, -3);
  27. }
  28. if (lua_pcall(L, 1, LUA_MULTRET, 0)) //call foo.test() function
  29. {
  30. error("error: %s", lua_tostring(L, -1));
  31. lua_pop(L, 1);
  32. return;
  33. }
  34. if (lua_gettop(L) - top)
  35. {
  36. //do something with the returned value
  37. }
  38. lua_pop(L, 1);
  39. }
  40.  
  41. lua_Debug ar;
  42. lua_getinfo(L, ">u", &ar);
  43. printf("%dn", ar.nparams);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement