2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit 1d99f396 authored by Artur Grunau's avatar Artur Grunau
Browse files

Use Lua helper classes to simplify LuaPipeline

LuaPipeline used to create a Lua VM itself and interact with it
directly. That caused its code to be unnecassarily bloated and complex as
it dealt with rather low-level details.

Delegate the management of Lua VM's state to the recently introduced
LuaVmState class and use the new table helpers to simplify LuaPipeline.

References #1
parent 4e87e812
......@@ -22,11 +22,12 @@ INCLUDE(${SWIG_USE_FILE})
FILE(GLOB CampvisScriptingSources
*.cpp
*.c
glue/*.cpp
)
FILE(GLOB CampvisScriptingHeaders
*.h
glue/*.h
)
......
......@@ -6,102 +6,48 @@ extern "C" {
#include "lauxlib.h"
}
#include "swigluarun.h"
#include "glue/globalluatable.h"
#include "glue/luavmstate.h"
namespace campvis {
void LuaPipeline::callLuaFunc(int nargs, int nresults) {
if (lua_pcall(_luaState, nargs, nresults, 0) != LUA_OK) {
this->logLuaError();
}
}
LuaPipeline::LuaPipeline(std::string scriptPath, DataContainer* dc)
: AutoEvaluationPipeline(dc)
, _scriptPath(scriptPath)
, _luaVmState(new LuaVmState())
, _pipelineTable(_luaVmState->getGlobalTable()->getTable("pipeline"))
{
_luaState = luaL_newstate();
// load the libs
luaL_openlibs(_luaState);
/* Let Lua know where CAMPVis modules are located */
if (luaL_dostring(_luaState, "package.cpath = '" CAMPVIS_LUA_MODS_PATH "'")) {
this->logLuaError();
// Let Lua know where CAMPVis modules are located
if (!_luaVmState->execString("package.cpath = '" CAMPVIS_LUA_MODS_PATH "'"))
return;
}
if (luaL_dostring(_luaState, "require(\"campvis\")")) {
this->logLuaError();
if (!_luaVmState->execString("require(\"campvis\")"))
return;
}
swig_type_info* autoEvaluationPipelineType = SWIG_TypeQuery(_luaState, "campvis::AutoEvaluationPipeline *");
if (autoEvaluationPipelineType == nullptr)
printf("SWIG wrapper for campvis::AutoEvaluationPipeline not found");
else {
SWIG_NewPointerObj(_luaState, this, autoEvaluationPipelineType, 0);
lua_setglobal(_luaState, "instance");
}
// run a Lua script here; true is returned if there were errors
if (luaL_dofile(_luaState, scriptPath.c_str())) {
this->logLuaError();
if (!_luaVmState->injectObjectPointer(this, "campvis::AutoEvaluationPipeline *", "instance"))
return;
}
lua_getglobal(_luaState, "pipeline");
if (!lua_istable(_luaState, -1))
printf("No valid Lua pipeline found (pipeline is %s)\n", lua_typename(_luaState, lua_type(_luaState, -1)));
else {
lua_getfield(_luaState, -1, "ctor");
lua_getglobal(_luaState, "pipeline");
callLuaFunc(1, 0);
}
// Try executing the pipeline's Lua script
if (!_luaVmState->execFile(scriptPath))
return;
// Pop the pipeline table
lua_pop(_luaState, 1);
if (!_pipelineTable->isValid())
std::cerr << "No valid Lua pipeline found (global variable `pipeline` is not a table)" << std::endl;
else
_pipelineTable->callInstanceMethod("ctor");
}
LuaPipeline::~LuaPipeline() {
lua_close(_luaState);
}
void LuaPipeline::logLuaError() {
const char* errorMsg = lua_tostring(_luaState, -1);
if (errorMsg == nullptr)
std::cerr << "(error object is not a string)" << std::endl;
else
std::cerr << errorMsg << std::endl;
lua_pop(_luaState, 1);
}
void LuaPipeline::init() {
AutoEvaluationPipeline::init();
lua_getglobal(_luaState, "pipeline");
lua_getfield(_luaState, -1, "init");
lua_getglobal(_luaState, "pipeline");
callLuaFunc(1, 0);
// Pop the pipeline table
lua_pop(_luaState, 1);
_pipelineTable->callInstanceMethod("init");
}
void LuaPipeline::deinit() {
AutoEvaluationPipeline::deinit();
lua_getglobal(_luaState, "pipeline");
lua_getfield(_luaState, -1, "deinit");
lua_getglobal(_luaState, "pipeline");
callLuaFunc(1, 0);
// Pop the pipeline table
lua_pop(_luaState, 1);
_pipelineTable->callInstanceMethod("deinit");
}
}
#ifndef LUAPIPELINE_H__
#define LUAPIPELINE_H__
#include <memory>
#include "core/pipeline/autoevaluationpipeline.h"
struct lua_State;
namespace campvis {
class LuaTable;
class LuaVmState;
class LuaPipeline : public AutoEvaluationPipeline {
public:
/**
......@@ -33,18 +35,9 @@ namespace campvis {
virtual void deinit();
protected:
/**
* Log the most recent Lua error to the console.
*/
void logLuaError();
/**
* Call the Lua function that's at the top of the stack.
*/
void callLuaFunc(int nargs, int nresults);
const std::string _scriptPath; ///< path to the Lua script defining the pipeline
struct lua_State* _luaState; ///< Lua state used to evaluate the pipeline
const std::string _scriptPath; ///< path to the Lua script defining the pipeline
LuaVmState* _luaVmState; ///< Lua VM state used to evaluate the pipeline
std::shared_ptr<LuaTable> _pipelineTable; ///< Pointer to the Lua table associated with the pipeline
};
}
......
......@@ -37,7 +37,7 @@ function pipeline:init()
local geometry2 = campvis.TFGeometry1D_createQuad(tgt.vec2(.4, .5), tgt.col4(0, 255, 0, 128),
tgt.vec4(0, 255, 0, 128))
local dvrTF = campvis.Geometry1DTransferFunction(128, campvis.vec2(0, 0.05))
local dvrTF = campvis.Geometry1DTransferFunction(128, tgt.vec2(0, 0.05))
dvrTF:addGeometry(geometry1)
dvrTF:addGeometry(geometry2)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment