migrate from dynamic::Value to dv::value & total erase namespace 'dynamic'

This commit is contained in:
MihailRis
2024-09-18 23:31:18 +03:00
parent d3ba4b2e3e
commit 34d2e6d400
69 changed files with 1247 additions and 2248 deletions
+56 -55
View File
@@ -59,7 +59,7 @@ public:
}
}
dynamic::Value parseValue() {
dv::value parseValue() {
char c = peek();
if (is_cmd_identifier_start(c) || c == '@') {
auto str = parseIdentifier(true);
@@ -68,7 +68,7 @@ public:
} else if (str == "false") {
return false;
} else if (str == "none" || str == "nil" || str == "null") {
return dynamic::NONE;
return nullptr;
}
return str;
}
@@ -116,8 +116,8 @@ public:
enumname = parseEnum();
}
bool optional = false;
dynamic::Value def {};
dynamic::Value origin {};
dv::value def;
dv::value origin;
bool loop = true;
while (hasNext() && loop) {
char c = peek();
@@ -173,18 +173,17 @@ public:
inline parsing_error typeError(
const std::string& argname,
const std::string& expected,
const dynamic::Value& value
const dv::value& value
) {
return argumentError(
argname, expected + " expected, got " + dynamic::type_name(value)
argname, expected + " expected, got " + dv::type_name(value)
);
}
template <typename T>
inline bool typeCheck(
Argument* arg, const dynamic::Value& value, const std::string& tname
Argument* arg, dv::value_type type, const dv::value& value, const std::string& tname
) {
if (!std::holds_alternative<T>(value)) {
if (value.getType() != type) {
if (arg->optional) {
return false;
} else {
@@ -194,10 +193,11 @@ public:
return true;
}
inline bool selectorCheck(Argument* arg, const dynamic::Value& value) {
if (auto string = std::get_if<std::string>(&value)) {
if ((*string)[0] == '@') {
if (!util::is_integer((*string).substr(1))) {
inline bool selectorCheck(Argument* arg, const dv::value& value) {
if (value.isString()) {
const auto& string = value.asString();
if (string[0] == '@') {
if (!util::is_integer(string.substr(1))) {
throw argumentError(arg->name, "invalid selector");
}
return true;
@@ -210,12 +210,13 @@ public:
}
}
bool typeCheck(Argument* arg, const dynamic::Value& value) {
bool typeCheck(Argument* arg, const dv::value& value) {
switch (arg->type) {
case ArgType::enumvalue: {
if (auto* string = std::get_if<std::string>(&value)) {
if (value.getType() == dv::value_type::string) {
const auto& string = value.asString();
auto& enumname = arg->enumname;
if (enumname.find("|" + *string + "|") ==
if (enumname.find("|" + string + "|") ==
std::string::npos) {
throw error(
"argument " + util::quote(arg->name) +
@@ -231,7 +232,7 @@ public:
break;
}
case ArgType::number:
if (!dynamic::is_numeric(value)) {
if (!dv::is_numeric(value)) {
if (arg->optional) {
return false;
} else {
@@ -242,9 +243,9 @@ public:
case ArgType::selector:
return selectorCheck(arg, value);
case ArgType::integer:
return typeCheck<integer_t>(arg, value, "integer");
return typeCheck(arg, dv::value_type::integer, value, "integer");
case ArgType::string:
if (!std::holds_alternative<std::string>(value)) {
if (!value.isString()) {
return !arg->optional;
}
break;
@@ -252,36 +253,35 @@ public:
return true;
}
dynamic::Value fetchOrigin(
dv::value fetchOrigin(
CommandsInterpreter* interpreter, Argument* arg
) {
if (dynamic::is_numeric(arg->origin)) {
if (dv::is_numeric(arg->origin)) {
return arg->origin;
} else if (auto string = std::get_if<std::string>(&arg->origin)) {
return (*interpreter)[*string];
} else if (arg->origin.getType() == dv::value_type::string) {
return (*interpreter)[arg->origin.asString()];
}
return dynamic::NONE;
return nullptr;
}
dynamic::Value applyRelative(
Argument* arg, dynamic::Value value, const dynamic::Value& origin
dv::value applyRelative(
Argument* arg, dv::value value, const dv::value& origin
) {
if (origin.index() == 0) {
if (origin == nullptr) {
return value;
}
try {
if (arg->type == ArgType::number) {
return dynamic::get_number(origin) + dynamic::get_number(value);
return origin.asNumber() + value.asNumber();
} else {
return dynamic::get_integer(origin) +
dynamic::get_integer(value);
return origin.asInteger() + value.asInteger();
}
} catch (std::runtime_error& err) {
throw argumentError(arg->name, err.what());
}
}
dynamic::Value parseRelativeValue(
dv::value parseRelativeValue(
CommandsInterpreter* interpreter, Argument* arg
) {
if (arg->type != ArgType::number && arg->type != ArgType::integer) {
@@ -293,13 +293,13 @@ public:
return origin;
}
auto value = parseValue();
if (origin.index() == 0) {
if (origin == nullptr) {
return value;
}
return applyRelative(arg, value, origin);
}
inline dynamic::Value performKeywordArg(
inline dv::value performKeywordArg(
CommandsInterpreter* interpreter,
Command* command,
const std::string& key
@@ -322,14 +322,14 @@ public:
if (command == nullptr) {
throw error("unknown command " + util::quote(name));
}
auto args = dynamic::create_list();
auto kwargs = dynamic::create_map();
auto args = dv::list();
auto kwargs = dv::object();
int arg_index = 0;
while (hasNext()) {
bool relative = false;
dynamic::Value value = dynamic::NONE;
dv::value value;
if (peek() == '~') {
relative = true;
value = static_cast<integer_t>(0);
@@ -338,18 +338,17 @@ public:
if (hasNext() && peekNoJump() != ' ') {
value = parseValue();
if (auto string = std::get_if<std::string>(&value)) {
if ((*string)[0] == '$') {
value = (*interpreter)[string->substr(1)];
if (value.isString()) {
const auto& string = value.asString();
if (string[0] == '$') {
value = (*interpreter)[string.substr(1)];
}
}
// keyword argument
if (!relative && hasNext() && peek() == '=') {
auto key = std::get<std::string>(value);
kwargs->put(
key, performKeywordArg(interpreter, command, key)
);
if (value.isString() && !relative && hasNext() && peek() == '=') {
const auto& key = value.asString();
kwargs[key] = performKeywordArg(interpreter, command, key);
}
}
@@ -357,21 +356,22 @@ public:
Argument* arg = nullptr;
do {
if (arg) {
if (auto string = std::get_if<std::string>(&arg->def)) {
if ((*string)[0] == '$') {
args->put((*interpreter)[string->substr(1)]);
if (arg->def.isString()) {
const auto& string = arg->def.asString();
if (string[0] == '$') {
args.add((*interpreter)[string.substr(1)]);
} else {
args->put(arg->def);
args.add(arg->def);
}
} else {
args->put(arg->def);
args.add(arg->def);
}
}
arg = command->getArgument(arg_index++);
if (arg == nullptr) {
throw error("extra positional argument");
}
if (arg->origin.index() && relative) {
if (arg->origin != nullptr && relative) {
break;
}
} while (!typeCheck(arg, value));
@@ -380,20 +380,21 @@ public:
value =
applyRelative(arg, value, fetchOrigin(interpreter, arg));
}
args->put(value);
args.add(value);
}
while (auto arg = command->getArgument(arg_index++)) {
if (!arg->optional) {
throw error("missing argument " + util::quote(arg->name));
} else {
if (auto string = std::get_if<std::string>(&arg->def)) {
if ((*string)[0] == '$') {
args->put((*interpreter)[string->substr(1)]);
if (arg->def.isString()) {
const auto& string = arg->def.asString();
if (string[0] == '$') {
args.add((*interpreter)[string.substr(1)]);
continue;
}
}
args->put(arg->def);
args.add(arg->def);
}
}
return Prompt {command, args, kwargs};
+12 -12
View File
@@ -5,7 +5,7 @@
#include <unordered_map>
#include <vector>
#include "data/dynamic.hpp"
#include "data/dv.hpp"
namespace cmd {
enum class ArgType { number, integer, enumvalue, selector, string };
@@ -31,8 +31,8 @@ namespace cmd {
std::string name;
ArgType type;
bool optional;
dynamic::Value def;
dynamic::Value origin;
dv::value def;
dv::value origin;
std::string enumname;
};
@@ -41,12 +41,12 @@ namespace cmd {
struct Prompt {
Command* command;
dynamic::List_sptr args; // positional arguments list
dynamic::Map_sptr kwargs; // keyword arguments table
dv::value args; // positional arguments list
dv::value kwargs; // keyword arguments table
};
using executor_func = std::function<dynamic::Value(
CommandsInterpreter*, dynamic::List_sptr args, dynamic::Map_sptr kwargs
using executor_func = std::function<dv::value(
CommandsInterpreter*, dv::value args, dv::value kwargs
)>;
class Command {
@@ -85,7 +85,7 @@ namespace cmd {
return &found->second;
}
dynamic::Value execute(
dv::value execute(
CommandsInterpreter* interpreter, const Prompt& prompt
) {
return executor(interpreter, prompt.args, prompt.kwargs);
@@ -127,7 +127,7 @@ namespace cmd {
class CommandsInterpreter {
std::unique_ptr<CommandsRepository> repository;
std::unordered_map<std::string, dynamic::Value> variables;
std::unordered_map<std::string, dv::value> variables;
public:
CommandsInterpreter()
: repository(std::make_unique<CommandsRepository>()) {
@@ -141,15 +141,15 @@ namespace cmd {
Prompt parse(std::string_view text);
dynamic::Value execute(std::string_view input) {
dv::value execute(std::string_view input) {
return execute(parse(input));
}
dynamic::Value execute(const Prompt& prompt) {
dv::value execute(const Prompt& prompt) {
return prompt.command->execute(this, prompt);
}
dynamic::Value& operator[](const std::string& name) {
dv::value& operator[](const std::string& name) {
return variables[name];
}
+5 -6
View File
@@ -88,14 +88,13 @@ void show_convert_request(
static void show_content_missing(
Engine* engine, const std::shared_ptr<ContentLUT>& lut
) {
using namespace dynamic;
auto root = create_map();
auto& contentEntries = root->putList("content");
auto root = dv::object();
auto& contentEntries = root.list("content");
for (auto& entry : lut->getMissingContent()) {
std::string contentName = contenttype_name(entry.type);
auto& contentEntry = contentEntries.putMap();
contentEntry.put("type", contentName);
contentEntry.put("name", entry.name);
auto& contentEntry = contentEntries.object();
contentEntry["type"] = contentName;
contentEntry["name"] = entry.name;
}
menus::show(engine, "reports/missing_content", {root});
}
+2 -5
View File
@@ -50,12 +50,9 @@ static int l_spawn(lua::State* L) {
auto defname = lua::tostring(L, 1);
auto& def = content->entities.require(defname);
auto pos = lua::tovec3(L, 2);
dynamic::Map_sptr args = nullptr;
dv::value args = nullptr;
if (lua::gettop(L) > 2) {
auto value = lua::tovalue(L, 3);
if (auto map = std::get_if<dynamic::Map_sptr>(&value)) {
args = *map;
}
args = lua::tovalue(L, 3);
}
level->entities->spawn(def, pos, args);
return 1;
+3 -8
View File
@@ -1,17 +1,12 @@
#include "coders/json.hpp"
#include "data/dynamic.hpp"
#include "api_lua.hpp"
static int l_json_stringify(lua::State* L) {
auto value = lua::tovalue(L, 1);
if (auto mapptr = std::get_if<dynamic::Map_sptr>(&value)) {
bool nice = lua::toboolean(L, 2);
auto string = json::stringify(mapptr->get(), nice, " ");
return lua::pushstring(L, string);
} else {
throw std::runtime_error("table expected");
}
bool nice = lua::toboolean(L, 2);
auto string = json::stringify(value, nice, " ");
return lua::pushstring(L, string);
}
static int l_json_parse(lua::State* L) {
+3 -5
View File
@@ -1,5 +1,4 @@
#include "coders/toml.hpp"
#include "data/dynamic.hpp"
#include "api_lua.hpp"
using namespace scripting;
@@ -7,8 +6,8 @@ using namespace scripting;
static int l_toml_stringify(lua::State* L) {
auto value = lua::tovalue(L, 1);
if (auto mapptr = std::get_if<dynamic::Map_sptr>(&value)) {
auto string = toml::stringify(**mapptr);
if (value.isObject()) {
auto string = toml::stringify(value);
return lua::pushstring(L, string);
} else {
throw std::runtime_error("table expected");
@@ -18,8 +17,7 @@ static int l_toml_stringify(lua::State* L) {
static int l_toml_parse(lua::State* L) {
auto string = lua::require_string(L, 1);
auto element = toml::parse("<string>", string);
auto value = std::make_unique<dynamic::Value>(element);
return lua::pushvalue(L, *value);
return lua::pushvalue(L, element);
}
const luaL_Reg tomllib[] = {
-1
View File
@@ -3,7 +3,6 @@
#include <stdexcept>
#include <string>
#include "data/dynamic.hpp"
#include "delegates.hpp"
#include "logic/scripting/scripting_functional.hpp"
#include "lua_util.hpp"
+42 -33
View File
@@ -22,33 +22,42 @@ std::string lua::env_name(int env) {
return "_ENV" + util::mangleid(env);
}
int lua::pushvalue(State* L, const dynamic::Value& value) {
using namespace dynamic;
int lua::pushvalue(State* L, const dv::value& value) {
using dv::value_type;
if (auto* flag = std::get_if<bool>(&value)) {
pushboolean(L, *flag);
} else if (auto* num = std::get_if<integer_t>(&value)) {
pushinteger(L, *num);
} else if (auto* num = std::get_if<number_t>(&value)) {
pushnumber(L, *num);
} else if (auto* str = std::get_if<std::string>(&value)) {
pushstring(L, *str);
} else if (auto listptr = std::get_if<List_sptr>(&value)) {
auto list = *listptr;
createtable(L, list->size(), 0);
for (size_t i = 0; i < list->size(); i++) {
pushvalue(L, list->get(i));
rawseti(L, i + 1);
switch (value.getType()) {
case value_type::none:
pushnil(L);
break;
case value_type::boolean:
pushboolean(L, value.asBoolean());
break;
case value_type::number:
pushnumber(L, value.asNumber());
break;
case value_type::integer:
pushinteger(L, value.asInteger());
break;
case value_type::string:
pushstring(L, value.asString());
break;
case value_type::list: {
createtable(L, value.size(), 0);
size_t index = 1;
for (const auto& elem : value) {
pushvalue(L, elem);
rawseti(L, index);
index++;
}
break;
}
} else if (auto mapptr = std::get_if<Map_sptr>(&value)) {
auto map = *mapptr;
createtable(L, 0, map->size());
for (auto& entry : map->values) {
pushvalue(L, entry.second);
setfield(L, entry.first);
case value_type::object: {
createtable(L, 0, value.size());
for (const auto& [key, elem] : value.asObject()) {
pushvalue(L, elem);
setfield(L, key);
}
}
} else {
pushnil(L);
}
return 1;
}
@@ -61,13 +70,13 @@ int lua::pushwstring(State* L, const std::wstring& str) {
return pushstring(L, util::wstr2str_utf8(str));
}
dynamic::Value lua::tovalue(State* L, int idx) {
using namespace dynamic;
dv::value lua::tovalue(State* L, int idx) {
using dv::value_type;
auto type = lua::type(L, idx);
switch (type) {
case LUA_TNIL:
case LUA_TNONE:
return dynamic::NONE;
return nullptr;
case LUA_TBOOLEAN:
return toboolean(L, idx) == 1;
case LUA_TNUMBER: {
@@ -91,22 +100,22 @@ dynamic::Value lua::tovalue(State* L, int idx) {
int len = objlen(L, idx);
if (len) {
// array
auto list = create_list();
auto list = dv::list();
for (int i = 1; i <= len; i++) {
rawgeti(L, i, idx);
list->put(tovalue(L, -1));
list.add(tovalue(L, -1));
pop(L);
}
return list;
} else {
// table
auto map = create_map();
auto map = dv::object();
pushvalue(L, idx);
pushnil(L);
while (next(L, -2)) {
pushvalue(L, -2);
auto key = tostring(L, -1);
map->put(key, tovalue(L, -2));
map[key] = tovalue(L, -2);
pop(L, 2);
}
pop(L);
@@ -219,7 +228,7 @@ runnable lua::create_runnable(State* L) {
scripting::common_func lua::create_lambda(State* L) {
auto funcptr = create_lambda_handler(L);
return [=](const std::vector<dynamic::Value>& args) {
return [=](const std::vector<dv::value>& args) -> dv::value {
getglobal(L, LAMBDAS_TABLE);
getfield(L, *funcptr);
for (const auto& arg : args) {
@@ -230,7 +239,7 @@ scripting::common_func lua::create_lambda(State* L) {
pop(L);
return result;
}
return dynamic::Value(dynamic::NONE);
return nullptr;
};
}
+3 -2
View File
@@ -4,6 +4,7 @@
#include <typeinfo>
#include <unordered_map>
#include "data/dv.hpp"
#include "lua_wrapper.hpp"
#include "lua_custom_types.hpp"
#define GLM_ENABLE_EXPERIMENTAL
@@ -394,8 +395,8 @@ namespace lua {
return glm::vec4(r / 255, g / 255, b / 255, a / 255);
}
int pushvalue(lua::State*, const dynamic::Value& value);
dynamic::Value tovalue(lua::State*, int idx);
int pushvalue(lua::State*, const dv::value& value);
dv::value tovalue(lua::State*, int idx);
inline bool getfield(lua::State* L, const std::string& name, int idx = -1) {
lua_getfield(L, idx, name.c_str());
+10 -10
View File
@@ -325,7 +325,7 @@ bool scripting::on_item_break_block(
);
}
dynamic::Value scripting::get_component_value(
dv::value scripting::get_component_value(
const scriptenv& env, const std::string& name
) {
auto L = lua::get_main_thread();
@@ -333,15 +333,15 @@ dynamic::Value scripting::get_component_value(
if (lua::getfield(L, name)) {
return lua::tovalue(L, -1);
}
return dynamic::NONE;
return nullptr;
}
void scripting::on_entity_spawn(
const EntityDef&,
entityid_t eid,
const std::vector<std::unique_ptr<UserComponent>>& components,
dynamic::Map_sptr args,
dynamic::Map_sptr saved
dv::value args,
dv::value saved
) {
auto L = lua::get_main_thread();
lua::requireglobal(L, STDCOMP);
@@ -364,8 +364,8 @@ void scripting::on_entity_spawn(
if (args != nullptr) {
std::string compfieldname = component->name;
util::replaceAll(compfieldname, ":", "__");
if (auto datamap = args->map(compfieldname)) {
lua::pushvalue(L, datamap);
if (args.has(compfieldname)) {
lua::pushvalue(L, args[compfieldname]);
} else {
lua::createtable(L, 0, 0);
}
@@ -377,8 +377,8 @@ void scripting::on_entity_spawn(
if (saved == nullptr) {
lua::createtable(L, 0, 0);
} else {
if (auto datamap = saved->map(component->name)) {
lua::pushvalue(L, datamap);
if (saved.has(component->name)) {
lua::pushvalue(L, saved[component->name]);
} else {
lua::createtable(L, 0, 0);
}
@@ -559,10 +559,10 @@ void scripting::on_entities_render(float delta) {
}
void scripting::on_ui_open(
UiDocument* layout, std::vector<dynamic::Value> args
UiDocument* layout, std::vector<dv::value> args
) {
auto argsptr =
std::make_shared<std::vector<dynamic::Value>>(std::move(args));
std::make_shared<std::vector<dv::value>>(std::move(args));
std::string name = layout->getId() + ".open";
lua::emit_event(lua::get_main_thread(), name, [=](auto L) {
for (const auto& value : *argsptr) {
+5 -5
View File
@@ -6,7 +6,7 @@
#include <string>
#include <vector>
#include "data/dynamic.hpp"
#include "data/dv.hpp"
#include "delegates.hpp"
#include "typedefs.hpp"
#include "scripting_functional.hpp"
@@ -87,15 +87,15 @@ namespace scripting {
Player* player, const ItemDef& item, int x, int y, int z
);
dynamic::Value get_component_value(
dv::value get_component_value(
const scriptenv& env, const std::string& name
);
void on_entity_spawn(
const EntityDef& def,
entityid_t eid,
const std::vector<std::unique_ptr<UserComponent>>& components,
dynamic::Map_sptr args,
dynamic::Map_sptr saved
dv::value args,
dv::value saved
);
void on_entity_despawn(const Entity& entity);
void on_entity_grounded(const Entity& entity, float force);
@@ -111,7 +111,7 @@ namespace scripting {
void on_entity_used(const Entity& entity, Player* player);
/// @brief Called on UI view show
void on_ui_open(UiDocument* layout, std::vector<dynamic::Value> args);
void on_ui_open(UiDocument* layout, std::vector<dv::value> args);
void on_ui_progress(UiDocument* layout, int workDone, int totalWork);
+2 -2
View File
@@ -160,7 +160,7 @@ vec2supplier scripting::create_vec2_supplier(
};
}
dynamic::to_string_func scripting::create_tostring(
dv::to_string_func scripting::create_tostring(
const scriptenv& env, const std::string& src, const std::string& file
) {
auto L = lua::get_main_thread();
@@ -168,7 +168,7 @@ dynamic::to_string_func scripting::create_tostring(
lua::loadbuffer(L, *env, src, file);
lua::call(L, 0, 1);
auto func = lua::create_lambda(L);
return [func](const dynamic::Value& value) {
return [func](const dv::value& value) {
auto result = func({value});
return json::stringify(result, true, " ");
};
+3 -4
View File
@@ -3,13 +3,12 @@
#include <glm/glm.hpp>
#include <string>
#include "data/dynamic.hpp"
#include "data/dv.hpp"
#include "delegates.hpp"
#include "typedefs.hpp"
namespace scripting {
using common_func =
std::function<dynamic::Value(const std::vector<dynamic::Value>&)>;
using common_func = std::function<dv::value(const std::vector<dv::value>&)>;
runnable create_runnable(
const scriptenv& env,
@@ -71,7 +70,7 @@ namespace scripting {
const std::string& file = "<string>"
);
dynamic::to_string_func create_tostring(
dv::to_string_func create_tostring(
const scriptenv& env,
const std::string& src,
const std::string& file = "<string>"