add luajit::TaskContext::Push(const nf7::Value&)

This commit is contained in:
2023-08-05 23:22:35 +09:00
parent cf3898d7bf
commit 1319a81ce2

View File

@@ -1,6 +1,8 @@
// No copyright
#include "core/luajit/thread.hh"
#include <string>
#include "core/luajit/context.hh"
@@ -9,18 +11,20 @@ namespace nf7::core::luajit {
void Thread::SetUpThread() noexcept {
luaL_newmetatable(th_, Context::kGlobalTableName);
{
static const char* kName = "nf7::core::luajit::Thread";
new (lua_newuserdata(th_, sizeof(this))) Thread* {this};
if (luaL_newmetatable(th_, "nf7::Thread")) {
if (luaL_newmetatable(th_, kName)) {
lua_createtable(th_, 0, 0);
{
lua_pushcfunction(th_, [](auto L) {
luaL_checkudata(L, 1, "nf7::Thread");
luaL_checkudata(L, 1, kName);
return luaL_error(L, lua_tostring(L, 2));
});
lua_setfield(th_, -2, "throw");
lua_pushcfunction(th_, [](auto L) {
luaL_checkudata(L, 1, "nf7::Thread");
luaL_checkudata(L, 1, kName);
if (lua_toboolean(L, 2)) {
return 0;
} else {
@@ -28,6 +32,54 @@ void Thread::SetUpThread() noexcept {
}
});
lua_setfield(th_, -2, "assert");
lua_pushcfunction(th_, ([](auto L) {
auto th = TaskContext::CheckUserData<Thread*>(L, 1, kName);
TaskContext lua {th->context_, L};
const auto type = std::string {lua_tostring(L, 3)};
if (type.empty()) {
const auto type = lua_type(L, 2);
switch (type) {
case LUA_TNIL:
lua.Push(nf7::Value {});
break;
case LUA_TNUMBER:
lua.Push(nf7::Value {
static_cast<nf7::Value::Real>(lua_tonumber(L, 2))});
break;
case LUA_TSTRING: {
size_t len;
const auto ptr = lua_tolstring(L, 2, &len);
lua.Push(nf7::Value::MakeBuffer(ptr, ptr+len));
} break;
case LUA_TUSERDATA:
lua.Push(lua.CheckValue(2));
break;
default:
return luaL_error(L, "invalid type in #1 param");
}
} else {
if ("null" == type) {
lua.Push(nf7::Value {});
} else if ("integer" == type) {
lua.Push(nf7::Value {
static_cast<nf7::Value::Integer>(lua_tointeger(L, 2))});
} else if ("real" == type) {
lua.Push(nf7::Value {
static_cast<nf7::Value::Real>(lua_tonumber(L, 2))});
} else if ("string" == type) {
size_t len;
const auto ptr = lua_tolstring(L, 2, &len);
lua.Push(nf7::Value::MakeBuffer(ptr, ptr+len));
} else {
return luaL_error(L, "unknown type specifier: %s", type.c_str());
}
}
return 1;
}));
lua_setfield(th_, -2, "value");
}
lua_setfield(th_, -2, "__index");
}