skynet_env.c 782 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "skynet.h"
  2. #include "skynet_env.h"
  3. #include "spinlock.h"
  4. #include <lua.h>
  5. #include <lauxlib.h>
  6. #include <stdlib.h>
  7. #include <assert.h>
  8. struct skynet_env {
  9. struct spinlock lock;
  10. lua_State *L;
  11. };
  12. static struct skynet_env *E = NULL;
  13. const char *
  14. skynet_getenv(const char *key) {
  15. SPIN_LOCK(E)
  16. lua_State *L = E->L;
  17. lua_getglobal(L, key);
  18. const char * result = lua_tostring(L, -1);
  19. lua_pop(L, 1);
  20. SPIN_UNLOCK(E)
  21. return result;
  22. }
  23. void
  24. skynet_setenv(const char *key, const char *value) {
  25. SPIN_LOCK(E)
  26. lua_State *L = E->L;
  27. lua_getglobal(L, key);
  28. assert(lua_isnil(L, -1));
  29. lua_pop(L,1);
  30. lua_pushstring(L,value);
  31. lua_setglobal(L,key);
  32. SPIN_UNLOCK(E)
  33. }
  34. void
  35. skynet_env_init() {
  36. E = skynet_malloc(sizeof(*E));
  37. SPIN_INIT(E)
  38. E->L = luaL_newstate();
  39. }