Add class vector. Change probe_lua_call to handle multiple return values.
This commit is contained in:
@@ -71,6 +71,7 @@ OBJ_CORE=\
|
|||||||
build/$(OS)/core/planemap.obj\
|
build/$(OS)/core/planemap.obj\
|
||||||
build/$(OS)/core/pprint.obj\
|
build/$(OS)/core/pprint.obj\
|
||||||
build/$(OS)/core/luaconsole.obj\
|
build/$(OS)/core/luaconsole.obj\
|
||||||
|
build/$(OS)/core/luavector.obj\
|
||||||
build/$(OS)/core/idalloc.obj\
|
build/$(OS)/core/idalloc.obj\
|
||||||
build/$(OS)/core/globaldb.obj\
|
build/$(OS)/core/globaldb.obj\
|
||||||
build/$(OS)/core/sched.obj\
|
build/$(OS)/core/sched.obj\
|
||||||
|
|||||||
41
luprex/cpp/core/luavector.cpp
Normal file
41
luprex/cpp/core/luavector.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#include "luastack.hpp"
|
||||||
|
|
||||||
|
LuaDefine(vec, "(x, y, z)",
|
||||||
|
"|Create a vector."
|
||||||
|
"|"
|
||||||
|
"|The function 'vec' is shorthand for vector.create."
|
||||||
|
"|") {
|
||||||
|
LuaRet result;
|
||||||
|
LuaArg lx, ly, lz;
|
||||||
|
LuaVar vclass;
|
||||||
|
LuaDefStack LS(L, result, lx, ly, lz, vclass);
|
||||||
|
double x = LS.cknumber(lx);
|
||||||
|
double y = LS.cknumber(ly);
|
||||||
|
double z = LS.cknumber(lz);
|
||||||
|
LS.createtable(result, 3, 0);
|
||||||
|
LS.rawset(result, 1, x);
|
||||||
|
LS.rawset(result, 2, y);
|
||||||
|
LS.rawset(result, 3, z);
|
||||||
|
LS.getclass(vclass, "vector");
|
||||||
|
LS.setmetatable(result, vclass);
|
||||||
|
return LS.result();
|
||||||
|
}
|
||||||
|
|
||||||
|
LuaDefine(vector_create, "(x, y, z)",
|
||||||
|
"|Create a vector."
|
||||||
|
"|") {
|
||||||
|
LuaRet result;
|
||||||
|
LuaArg lx, ly, lz;
|
||||||
|
LuaVar vclass;
|
||||||
|
LuaDefStack LS(L, result, lx, ly, lz, vclass);
|
||||||
|
double x = LS.cknumber(lx);
|
||||||
|
double y = LS.cknumber(ly);
|
||||||
|
double z = LS.cknumber(lz);
|
||||||
|
LS.createtable(result, 3, 0);
|
||||||
|
LS.rawset(result, 1, x);
|
||||||
|
LS.rawset(result, 2, y);
|
||||||
|
LS.rawset(result, 3, z);
|
||||||
|
LS.getclass(vclass, "vector");
|
||||||
|
LS.setmetatable(result, vclass);
|
||||||
|
return LS.result();
|
||||||
|
}
|
||||||
6
luprex/cpp/core/luavector.hpp
Normal file
6
luprex/cpp/core/luavector.hpp
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
///////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// Lua Vectors - this file contains functions that are entirely
|
||||||
|
// for lua, no C++ interface, so this header file is empty.
|
||||||
|
//
|
||||||
|
///////////////////////////////////////////////////////////////////////
|
||||||
@@ -470,6 +470,7 @@ void World::probe_lua_call(int64_t place_id, int64_t actor_id, std::string_view
|
|||||||
if (!LS.isfunction(lfunc)) return;
|
if (!LS.isfunction(lfunc)) return;
|
||||||
|
|
||||||
// Call the closure.
|
// Call the closure.
|
||||||
|
int calltop = lua_gettop(L);
|
||||||
lua_pushvalue(L, lfunc.index());
|
lua_pushvalue(L, lfunc.index());
|
||||||
int nargs = 0;
|
int nargs = 0;
|
||||||
try {
|
try {
|
||||||
@@ -482,20 +483,35 @@ void World::probe_lua_call(int64_t place_id, int64_t actor_id, std::string_view
|
|||||||
}
|
}
|
||||||
|
|
||||||
open_lthread_state(actor_id, place_id, 0, false, true);
|
open_lthread_state(actor_id, place_id, 0, false, true);
|
||||||
eng::string msg = traceback_pcall(L, nargs, 1);
|
eng::string msg = traceback_pcall(L, nargs, LUA_MULTRET);
|
||||||
|
LuaExtraArgs returnvalues(calltop + 1, lua_gettop(L) - calltop);
|
||||||
|
|
||||||
// Send any prints to the console.
|
// Send any prints to the console.
|
||||||
eng::string prints = lthread_prints_->str();
|
eng::string prints = lthread_prints_->str();
|
||||||
lthread_prints_.reset();
|
lthread_prints_.reset();
|
||||||
util::dprint(prints);
|
util::dprint(prints);
|
||||||
|
|
||||||
if (msg.empty()) {
|
if (msg.empty()) {
|
||||||
lua_replace(L, retvec.index());
|
bool ok = true;
|
||||||
if (LS.istable(retvec)) {
|
for (int i = 0; ok && (i < returnvalues.size()); i++) {
|
||||||
for (int i = 1 ; ; i++) {
|
LS.set(retvec, returnvalues[i]);
|
||||||
LS.rawget(retval, retvec, i);
|
|
||||||
bool ok = encode_simple_dynamic(LS, retval, retvals);
|
// If it's a general table without an MT, then
|
||||||
if (!ok) break;
|
// treat it as a list of return values.
|
||||||
|
bool is_compound = false;
|
||||||
|
if (LS.xtype(retvec) == LUA_TT_GENERAL) {
|
||||||
|
LS.getmetatable(mt, retvec);
|
||||||
|
if (LS.isnil(mt)) is_compound = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_compound) {
|
||||||
|
for (int j = 1; ok; j++) {
|
||||||
|
LS.rawget(retval, retvec, j);
|
||||||
|
if (LS.isnil(retval)) break;
|
||||||
|
ok = encode_simple_dynamic(LS, retval, retvals);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ok = encode_simple_dynamic(LS, retvec, retvals);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -30,6 +30,6 @@ function engio.printhi(a1, a2, a3, a4, a5)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function engio.retthree(a1, a2)
|
function engio.retthree(a1, a2)
|
||||||
return { 5, {3, 4, 5}, "Howdy" }
|
return 7, vec(8,9,10), "Yo"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user