add yaml serialization library

This commit is contained in:
MihailRis 2025-03-24 07:43:41 +03:00
parent 396fab02b3
commit c28bfae679
8 changed files with 59 additions and 4 deletions

View File

@ -11,7 +11,7 @@ Subsections:
- [Libraries](#)
- [app](scripting/builtins/libapp.md)
- [base64](scripting/builtins/libbase64.md)
- [bjson, json, toml](scripting/filesystem.md)
- [bjson, json, toml, yaml](scripting/filesystem.md)
- [block](scripting/builtins/libblock.md)
- [byteutil](scripting/builtins/libbyteutil.md)
- [cameras](scripting/builtins/libcameras.md)

View File

@ -36,6 +36,22 @@ toml.parse(code: str) -> table
Parses a TOML string into a table.
## *yaml* library
The library contains functions for serializing and deserializing tables:
```python
yaml.tostring(object: table) -> str
```
Serializes an object into a YAML string.
```python
yaml.parse(code: str) -> table
```
Parses a YAML string into a table.
## *bjson* library
The library contains functions for working with the binary data exchange format [vcbjson](../../specs/binary_json_spec.md).

View File

@ -11,7 +11,7 @@
- [Библиотеки](#)
- [app](scripting/builtins/libapp.md)
- [base64](scripting/builtins/libbase64.md)
- [bjson, json, toml](scripting/filesystem.md)
- [bjson, json, toml, yaml](scripting/filesystem.md)
- [block](scripting/builtins/libblock.md)
- [byteutil](scripting/builtins/libbyteutil.md)
- [cameras](scripting/builtins/libcameras.md)

View File

@ -36,6 +36,22 @@ toml.parse(code: str) -> table
Парсит TOML строку в таблицу.
## Библиотека yaml
Библиотека содержит функции для сериализации и десериализации таблиц:
```python
yaml.tostring(object: table) -> str
```
Сериализует объект в YAML строку.
```python
yaml.parse(code: str) -> table
```
Парсит YAML строку в таблицу.
## Библиотека bjson
Библиотека содержит функции для работы с двоичным форматом обмена данными [vcbjson](../../specs/binary_json_spec.md).

View File

@ -14,6 +14,7 @@
/// int l_function_name(lua::State* L);
// Libraries
extern const luaL_Reg applib[];
extern const luaL_Reg audiolib[];
extern const luaL_Reg base64lib[];
extern const luaL_Reg bjsonlib[];
@ -38,7 +39,6 @@ extern const luaL_Reg packlib[];
extern const luaL_Reg particleslib[]; // gfx.particles
extern const luaL_Reg playerlib[];
extern const luaL_Reg quatlib[];
extern const luaL_Reg applib[];
extern const luaL_Reg text3dlib[]; // gfx.text3d
extern const luaL_Reg timelib[];
extern const luaL_Reg tomllib[];
@ -48,6 +48,7 @@ extern const luaL_Reg vec3lib[]; // vecn.cpp
extern const luaL_Reg vec4lib[]; // vecn.cpp
extern const luaL_Reg weatherlib[];
extern const luaL_Reg worldlib[];
extern const luaL_Reg yamllib[];
// Components
extern const luaL_Reg skeletonlib[];

View File

@ -19,4 +19,5 @@ static int l_json_parse(lua::State* L) {
const luaL_Reg jsonlib[] = {
{"tostring", lua::wrap<l_json_stringify>},
{"parse", lua::wrap<l_json_parse>},
{NULL, NULL}};
{NULL, NULL}
};

View File

@ -0,0 +1,20 @@
#include "coders/yaml.hpp"
#include "api_lua.hpp"
static int l_stringify(lua::State* L) {
auto value = lua::tovalue(L, 1);
auto string = yaml::stringify(value);
return lua::pushstring(L, string);
}
static int l_parse(lua::State* L) {
auto string = lua::require_string(L, 1);
auto element = yaml::parse("[string]", string);
return lua::pushvalue(L, element);
}
const luaL_Reg yamllib[] = {
{"tostring", lua::wrap<l_stringify>},
{"parse", lua::wrap<l_parse>},
{NULL, NULL}
};

View File

@ -56,6 +56,7 @@ static void create_libs(State* L, StateType stateType) {
openlib(L, "vec2", vec2lib);
openlib(L, "vec3", vec3lib);
openlib(L, "vec4", vec4lib);
openlib(L, "yaml", yamllib);
if (stateType == StateType::SCRIPT) {
openlib(L, "app", applib);