Mercurial > hg > orthanc
comparison Core/Lua/LuaContext.cpp @ 1437:02f5a3f5c0a0
access to the REST API from Lua
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 30 Jun 2015 18:41:33 +0200 |
parents | 6e7e5ed91c2d |
children | af112b7d9cba |
comparison
equal
deleted
inserted
replaced
1436:0a3e3be59094 | 1437:02f5a3f5c0a0 |
---|---|
44 | 44 |
45 namespace Orthanc | 45 namespace Orthanc |
46 { | 46 { |
47 LuaContext& LuaContext::GetLuaContext(lua_State *state) | 47 LuaContext& LuaContext::GetLuaContext(lua_State *state) |
48 { | 48 { |
49 // Get the pointer to the "LuaContext" underlying object | 49 const void* value = GetGlobalVariable(state, "_LuaContext"); |
50 lua_getglobal(state, "_LuaContext"); | 50 assert(value != NULL); |
51 assert(lua_type(state, -1) == LUA_TLIGHTUSERDATA); | 51 |
52 LuaContext* that = const_cast<LuaContext*>(reinterpret_cast<const LuaContext*>(lua_topointer(state, -1))); | 52 return *const_cast<LuaContext*>(reinterpret_cast<const LuaContext*>(value)); |
53 assert(that != NULL); | |
54 lua_pop(state, 1); | |
55 | |
56 return *that; | |
57 } | 53 } |
58 | 54 |
59 int LuaContext::PrintToLog(lua_State *state) | 55 int LuaContext::PrintToLog(lua_State *state) |
60 { | 56 { |
61 LuaContext& that = GetLuaContext(state); | 57 LuaContext& that = GetLuaContext(state); |
92 | 88 |
93 return 0; | 89 return 0; |
94 } | 90 } |
95 | 91 |
96 | 92 |
93 int LuaContext::ParseJsonString(lua_State *state) | |
94 { | |
95 LuaContext& that = GetLuaContext(state); | |
96 | |
97 int nArgs = lua_gettop(state); | |
98 if (nArgs != 1 || | |
99 !lua_isstring(state, 1)) // Password | |
100 { | |
101 lua_pushnil(state); | |
102 return 1; | |
103 } | |
104 | |
105 const char* str = lua_tostring(state, 1); | |
106 | |
107 Json::Value value; | |
108 Json::Reader reader; | |
109 if (reader.parse(str, str + strlen(str), value)) | |
110 { | |
111 that.PushJson(value); | |
112 } | |
113 else | |
114 { | |
115 lua_pushnil(state); | |
116 } | |
117 | |
118 return 1; | |
119 } | |
120 | |
121 | |
97 int LuaContext::SetHttpCredentials(lua_State *state) | 122 int LuaContext::SetHttpCredentials(lua_State *state) |
98 { | 123 { |
99 LuaContext& that = GetLuaContext(state); | 124 LuaContext& that = GetLuaContext(state); |
100 | 125 |
101 // Check the types of the arguments | 126 // Check the types of the arguments |
331 throw LuaException("Unable to create the Lua context"); | 356 throw LuaException("Unable to create the Lua context"); |
332 } | 357 } |
333 | 358 |
334 luaL_openlibs(lua_); | 359 luaL_openlibs(lua_); |
335 lua_register(lua_, "print", PrintToLog); | 360 lua_register(lua_, "print", PrintToLog); |
361 lua_register(lua_, "ParseJson", ParseJsonString); | |
336 lua_register(lua_, "HttpGet", CallHttpGet); | 362 lua_register(lua_, "HttpGet", CallHttpGet); |
337 lua_register(lua_, "HttpPost", CallHttpPost); | 363 lua_register(lua_, "HttpPost", CallHttpPost); |
338 lua_register(lua_, "HttpPut", CallHttpPut); | 364 lua_register(lua_, "HttpPut", CallHttpPut); |
339 lua_register(lua_, "HttpDelete", CallHttpDelete); | 365 lua_register(lua_, "HttpDelete", CallHttpDelete); |
340 lua_register(lua_, "SetHttpCredentials", SetHttpCredentials); | 366 lua_register(lua_, "SetHttpCredentials", SetHttpCredentials); |
341 | 367 |
342 lua_pushlightuserdata(lua_, this); | 368 SetGlobalVariable("_LuaContext", this); |
343 lua_setglobal(lua_, "_LuaContext"); | |
344 } | 369 } |
345 | 370 |
346 | 371 |
347 LuaContext::~LuaContext() | 372 LuaContext::~LuaContext() |
348 { | 373 { |
401 { | 426 { |
402 throw OrthancException(ErrorCode_BadFileFormat); | 427 throw OrthancException(ErrorCode_BadFileFormat); |
403 } | 428 } |
404 } | 429 } |
405 | 430 |
431 | |
432 void LuaContext::RegisterFunction(const char* name, | |
433 lua_CFunction func) | |
434 { | |
435 lua_register(lua_, name, func); | |
436 } | |
437 | |
438 | |
439 void LuaContext::SetGlobalVariable(const char* name, | |
440 void* value) | |
441 { | |
442 lua_pushlightuserdata(lua_, value); | |
443 lua_setglobal(lua_, name); | |
444 } | |
445 | |
446 | |
447 const void* LuaContext::GetGlobalVariable(lua_State* state, | |
448 const char* name) | |
449 { | |
450 lua_getglobal(state, name); | |
451 assert(lua_type(state, -1) == LUA_TLIGHTUSERDATA); | |
452 const void* value = lua_topointer(state, -1); | |
453 lua_pop(state, 1); | |
454 return value; | |
455 } | |
406 } | 456 } |