changeset 386:7dec4f3c922c lua-scripting

lua wrapper
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 29 Apr 2013 17:14:10 +0200
parents 18fe778eeb95
children 466c992a9a42
files CMakeLists.txt Core/Lua/LuaContext.cpp Core/Lua/LuaContext.h Core/Lua/LuaException.h Core/Lua/LuaFunctionCall.cpp Core/Lua/LuaFunctionCall.h UnitTests/Lua.cpp
diffstat 7 files changed, 575 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Mon Apr 29 16:59:31 2013 +0200
+++ b/CMakeLists.txt	Mon Apr 29 17:14:10 2013 +0200
@@ -144,6 +144,8 @@
   Core/SQLite/Transaction.cpp
   Core/Toolbox.cpp
   Core/Uuid.cpp
+  Core/Lua/LuaContext.cpp
+  Core/Lua/LuaFunctionCall.cpp
 
   OrthancCppClient/HttpClient.cpp
   OrthancCppClient/HttpException.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Core/Lua/LuaContext.cpp	Mon Apr 29 17:14:10 2013 +0200
@@ -0,0 +1,124 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege,
+ * Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "LuaContext.h"
+
+#include <glog/logging.h>
+
+extern "C" 
+{
+#include <lualib.h>
+#include <lauxlib.h>
+}
+
+namespace Orthanc
+{
+  int LuaContext::PrintToLog(lua_State *L)
+  {
+    // http://medek.wordpress.com/2009/02/03/wrapping-lua-errors-and-print-function/
+    int nArgs = lua_gettop(L);
+    lua_getglobal(L, "tostring");
+
+    // Make sure you start at 1 *NOT* 0 for arrays in Lua.
+    std::string result;
+
+    for (int i = 1; i <= nArgs; i++)
+    {
+      const char *s;
+      lua_pushvalue(L, -1);
+      lua_pushvalue(L, i);
+      lua_call(L, 1, 1);
+      s = lua_tostring(L, -1);
+
+      if (result.size() > 0)
+        result.append(", ");
+
+      if (s == NULL)
+        result.append("<No conversion to string>");
+      else
+        result.append(s);
+ 
+      lua_pop(L, 1);
+    }
+
+    LOG(WARNING) << "Lua: " << result;         
+
+    return 0;
+  }
+
+
+  LuaContext::LuaContext()
+  {
+    lua_ = luaL_newstate();
+    if (!lua_)
+    {
+      throw LuaException("Unable to create the Lua context");
+    }
+
+    luaL_openlibs(lua_);
+    lua_register(lua_, "print", PrintToLog);
+  }
+
+
+  LuaContext::~LuaContext()
+  {
+    lua_close(lua_);
+  }
+
+
+  void LuaContext::Execute(const std::string& command)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+
+    lua_settop(lua_, 0);
+
+    int error = (luaL_loadbuffer(lua_, command.c_str(), command.size(), "line") ||
+                 lua_pcall(lua_, 0, 0, 0));
+
+    if (error) 
+    {
+      assert(lua_gettop(lua_) >= 1);
+
+      std::string description(lua_tostring(lua_, -1));
+      lua_pop(lua_, 1); /* pop error message from the stack */
+      throw LuaException(description);
+    }
+  }
+
+
+  void LuaContext::Execute(EmbeddedResources::FileResourceId resource)
+  {
+    std::string command;
+    EmbeddedResources::GetFileResource(command, resource);
+    Execute(command);
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Core/Lua/LuaContext.h	Mon Apr 29 17:14:10 2013 +0200
@@ -0,0 +1,68 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege,
+ * Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#include "LuaException.h"
+
+#include <boost/thread.hpp>
+
+extern "C" 
+{
+#include <lua.h>
+}
+
+#include <EmbeddedResources.h>
+
+
+namespace Orthanc
+{
+  class LuaContext : public boost::noncopyable
+  {
+  private:
+    friend class LuaFunctionCall;
+
+    lua_State *lua_;
+    boost::mutex mutex_;
+
+    static int PrintToLog(lua_State *L);
+
+  public:
+    LuaContext();
+
+    ~LuaContext();
+
+    void Execute(const std::string& command);
+
+    void Execute(EmbeddedResources::FileResourceId resource);
+  };
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Core/Lua/LuaException.h	Mon Apr 29 17:14:10 2013 +0200
@@ -0,0 +1,52 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege,
+ * Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#include "../OrthancException.h"
+
+namespace Orthanc
+{
+  class LuaException : public OrthancException
+  {
+  public:
+    LuaException(const char* explanation) : 
+      OrthancException(explanation)
+    {
+    }
+
+    LuaException(const std::string& explanation) : 
+      OrthancException(explanation)
+    {
+    }
+  };
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Core/Lua/LuaFunctionCall.cpp	Mon Apr 29 17:14:10 2013 +0200
@@ -0,0 +1,192 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege,
+ * Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "LuaFunctionCall.h"
+
+
+namespace Orthanc
+{
+  void LuaFunctionCall::CheckAlreadyExecuted()
+  {
+    if (isExecuted_)
+    {
+      throw LuaException("Arguments cannot be pushed after the function is executed");
+    }
+  }
+
+  LuaFunctionCall::LuaFunctionCall(LuaContext& context,
+                                   const char* functionName) : 
+    context_(context),
+    lock_(context.mutex_),
+    isExecuted_(false)
+  {
+    // Clear the stack to fulfill the invariant
+    lua_settop(context_.lua_, 0);
+    lua_getglobal(context_.lua_, functionName);
+  }
+
+  void LuaFunctionCall::PushString(const std::string& value)
+  {
+    CheckAlreadyExecuted();
+    lua_pushstring(context_.lua_, value.c_str());
+  }
+
+  void LuaFunctionCall::PushBoolean(bool value)
+  {
+    CheckAlreadyExecuted();
+    lua_pushboolean(context_.lua_, value);
+  }
+
+  void LuaFunctionCall::PushInteger(int value)
+  {
+    CheckAlreadyExecuted();
+    lua_pushinteger(context_.lua_, value);
+  }
+
+  void LuaFunctionCall::PushDouble(double value)
+  {
+    CheckAlreadyExecuted();
+    lua_pushnumber(context_.lua_, value);
+  }
+
+  void LuaFunctionCall::PushJSON(const Json::Value& value)
+  {
+    CheckAlreadyExecuted();
+
+    if (value.isString())
+    {
+      lua_pushstring(context_.lua_, value.asCString());
+    }
+    else if (value.isDouble())
+    {
+      lua_pushnumber(context_.lua_, value.asDouble());
+    }
+    else if (value.isInt())
+    {
+      lua_pushinteger(context_.lua_, value.asInt());
+    }
+    else if (value.isUInt())
+    {
+      lua_pushinteger(context_.lua_, value.asUInt());
+    }
+    else if (value.isBool())
+    {
+      lua_pushboolean(context_.lua_, value.asBool());
+    }
+    else if (value.isNull())
+    {
+      lua_pushnil(context_.lua_);
+    }
+    else if (value.isArray())
+    {
+      lua_newtable(context_.lua_);
+
+      // http://lua-users.org/wiki/SimpleLuaApiExample
+      for (Json::Value::ArrayIndex i = 0; i < value.size(); i++)
+      {
+        // Push the table index (note the "+1" because of Lua conventions)
+        lua_pushnumber(context_.lua_, i + 1);
+
+        // Push the value of the cell
+        PushJSON(value[i]);
+
+        // Stores the pair in the table
+        lua_rawset(context_.lua_, -3);
+      }
+    }
+    else if (value.isObject())
+    {
+      lua_newtable(context_.lua_);
+
+      Json::Value::Members members = value.getMemberNames();
+
+      for (Json::Value::Members::const_iterator 
+             it = members.begin(); it != members.end(); it++)
+      {
+        // Push the index of the cell
+        lua_pushstring(context_.lua_, it->c_str());
+
+        // Push the value of the cell
+        PushJSON(value[*it]);
+
+        // Stores the pair in the table
+        lua_rawset(context_.lua_, -3);
+      }
+    }
+    else
+    {
+      throw LuaException("Unsupported JSON conversion");
+    }
+  }
+
+  void LuaFunctionCall::Execute(int numOutputs)
+  {
+    CheckAlreadyExecuted();
+
+    assert(lua_gettop(context_.lua_) >= 1);
+    int nargs = lua_gettop(context_.lua_) - 1;
+    int error = lua_pcall(context_.lua_, nargs, numOutputs, 0);
+
+    if (error) 
+    {
+      assert(lua_gettop(context_.lua_) >= 1);
+          
+      std::string description(lua_tostring(context_.lua_, -1));
+      lua_pop(context_.lua_, 1); /* pop error message from the stack */
+      throw LuaException(description);
+    }
+
+    if (lua_gettop(context_.lua_) < numOutputs)
+    {
+      throw LuaException("The function does not give the expected number of outputs");
+    }
+
+    isExecuted_ = true;
+  }
+
+  bool LuaFunctionCall::ExecutePredicate()
+  {
+    Execute(1);
+        
+    if (lua_gettop(context_.lua_) == 0)
+    {
+      throw LuaException("No output was provided by the function");
+    }
+
+    if (!lua_isboolean(context_.lua_, 1))
+    {
+      throw LuaException("The function is not a predicate (only true/false outputs allowed)");
+    }
+
+    return lua_toboolean(context_.lua_, 1) != 0;
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Core/Lua/LuaFunctionCall.h	Mon Apr 29 17:14:10 2013 +0200
@@ -0,0 +1,69 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege,
+ * Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#include "LuaContext.h"
+
+#include <json/json.h>
+
+
+namespace Orthanc
+{
+  class LuaFunctionCall : public boost::noncopyable
+  {
+  private:
+    LuaContext& context_;
+    boost::mutex::scoped_lock lock_;
+    bool isExecuted_;
+
+    void CheckAlreadyExecuted();
+
+  public:
+    LuaFunctionCall(LuaContext& context,
+                    const char* functionName);
+
+    void PushString(const std::string& value);
+
+    void PushBoolean(bool value);
+
+    void PushInteger(int value);
+
+    void PushDouble(double value);
+
+    void PushJSON(const Json::Value& value);
+
+    void Execute(int numOutputs = 0);
+
+    bool ExecutePredicate();
+  };
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UnitTests/Lua.cpp	Mon Apr 29 17:14:10 2013 +0200
@@ -0,0 +1,68 @@
+#include "gtest/gtest.h"
+
+#include "../Core/Lua/LuaFunctionCall.h"
+
+
+TEST(Lua, Simple)
+{
+  try
+  {
+    Orthanc::LuaContext lua;
+    lua.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
+    lua.Execute("a={}");
+    lua.Execute("a['x'] = 10");
+    lua.Execute("a['y'] = {}");
+    lua.Execute("a['y'][1] = 20");
+    lua.Execute("a['y'][2] = 20");
+    lua.Execute("rPrint(a)");
+
+    lua.Execute("function f(a) print(a.bool) return a.bool,20,30,40,50,60 end");
+
+    Json::Value v, vv, o;
+    //v["a"] = "b";
+    v.append("hello");
+    v.append("world");
+    v.append("42");
+    vv.append("coucou");
+    vv.append("toi");
+    v.append(vv);
+    o = Json::objectValue;
+    o["x"] = 10;
+    o["y"] = 20;
+    o["z"] = 20.5f;
+    v.append(o);
+
+    {
+      Orthanc::LuaFunctionCall f(lua, "rPrint");
+      f.PushJSON(v);
+      f.Execute();
+    }
+
+    {
+      Orthanc::LuaFunctionCall f(lua, "f");
+      f.PushJSON(o);
+      ASSERT_THROW(f.ExecutePredicate(), Orthanc::LuaException);
+    }
+
+    o["bool"] = false;
+
+    {
+      Orthanc::LuaFunctionCall f(lua, "f");
+      f.PushJSON(o);
+      ASSERT_FALSE(f.ExecutePredicate());
+    }
+
+    o["bool"] = true;
+
+    {
+      Orthanc::LuaFunctionCall f(lua, "f");
+      f.PushJSON(o);
+      ASSERT_TRUE(f.ExecutePredicate());
+    }
+
+  }
+  catch (Orthanc::LuaException e)
+  {
+    std::cerr << "EXCEPTION: [" << e.What() << "]" << std::endl;
+  }
+}