lua: Add Version() class

This commit is contained in:
David Capello 2019-04-21 00:04:49 -03:00
parent 20618ff321
commit d35251d431
6 changed files with 159 additions and 2 deletions

2
laf

@ -1 +1 @@
Subproject commit 4217417b9531a4f371a3ccb38e8d87a21cacf64a
Subproject commit f77b374638e088bcc9f806f454d23360362e6560

View File

@ -183,6 +183,7 @@ if(ENABLE_SCRIPTING)
script/tag_class.cpp
script/tags_class.cpp
script/tool_class.cpp
script/version_class.cpp
shell.cpp
${scripting_files_ui})
endif()

View File

@ -36,6 +36,7 @@
#include "app/ui/timeline/timeline.h"
#include "app/ui_context.h"
#include "base/fs.h"
#include "base/version.h"
#include "doc/frame_tag.h"
#include "doc/layer.h"
#include "doc/primitives.h"
@ -483,7 +484,7 @@ int App_get_isUIAvailable(lua_State* L)
int App_get_version(lua_State* L)
{
lua_pushstring(L, VERSION);
push_version(L, base::Version(VERSION));
return 1;
}

View File

@ -165,6 +165,7 @@ void register_sprites_class(lua_State* L);
void register_tag_class(lua_State* L);
void register_tags_class(lua_State* L);
void register_tool_class(lua_State* L);
void register_version_class(lua_State* L);
void set_app_params(lua_State* L, const Params& params);
@ -346,6 +347,7 @@ Engine::Engine()
register_tag_class(L);
register_tags_class(L);
register_tool_class(L);
register_version_class(L);
// Check that we have a clean start (without dirty in the stack)
ASSERT(lua_gettop(L) == top);

View File

@ -26,6 +26,10 @@
struct lua_State;
namespace base {
class Version;
}
namespace gfx {
class ColorSpace;
}
@ -132,6 +136,7 @@ namespace app {
void push_sprites(lua_State* L);
void push_tool(lua_State* L, tools::Tool* tool);
void push_userdata(lua_State* L, doc::WithUserData* userData);
void push_version(lua_State* L, const base::Version& ver);
gfx::Point convert_args_into_point(lua_State* L, int index);
gfx::Rect convert_args_into_rect(lua_State* L, int index);

View File

@ -0,0 +1,148 @@
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/script/luacpp.h"
#include "base/version.h"
namespace app {
namespace script {
namespace {
base::Version Version_new(lua_State* L, int index)
{
base::Version ver;
if (auto ver2 = may_get_obj<base::Version>(L, index)) {
ver = *ver2;
}
else if (const char* verStr = lua_tostring(L, index)) {
ver = base::Version(verStr);
}
return ver;
}
int Version_new(lua_State* L)
{
push_obj(L, Version_new(L, 1));
return 1;
}
int Version_gc(lua_State* L)
{
get_obj<base::Version>(L, 1)->~Version();
return 0;
}
int Version_eq(lua_State* L)
{
const auto a = get_obj<base::Version>(L, 1);
const auto b = get_obj<base::Version>(L, 2);
lua_pushboolean(L, *a == *b);
return 1;
}
int Version_lt(lua_State* L)
{
const auto a = get_obj<base::Version>(L, 1);
const auto b = get_obj<base::Version>(L, 2);
lua_pushboolean(L, *a < *b);
return 1;
}
int Version_le(lua_State* L)
{
const auto a = get_obj<base::Version>(L, 1);
const auto b = get_obj<base::Version>(L, 2);
lua_pushboolean(L, (*a < *b) || (*a == *b));
return 1;
}
int Version_tostring(lua_State* L)
{
const auto ver = get_obj<base::Version>(L, 1);
lua_pushstring(L, ver->str().c_str());
return 1;
}
int Version_get_major(lua_State* L)
{
const auto ver = get_obj<base::Version>(L, 1);
const auto& digits = ver->digits();
lua_pushinteger(L, digits.size() > 0 ? digits[0]: 0);
return 1;
}
int Version_get_minor(lua_State* L)
{
const auto ver = get_obj<base::Version>(L, 1);
const auto& digits = ver->digits();
lua_pushinteger(L, digits.size() > 1 ? digits[1]: 0);
return 1;
}
int Version_get_patch(lua_State* L)
{
const auto ver = get_obj<base::Version>(L, 1);
const auto& digits = ver->digits();
lua_pushinteger(L, digits.size() > 2 ? digits[2]: 0);
return 1;
}
int Version_get_prerelease(lua_State* L)
{
const auto ver = get_obj<base::Version>(L, 1);
lua_pushstring(L, ver->prerelease().c_str());
return 1;
}
int Version_get_prereleaseDigit(lua_State* L)
{
const auto ver = get_obj<base::Version>(L, 1);
lua_pushinteger(L, ver->prereleaseDigit());
return 1;
}
const luaL_Reg Version_methods[] = {
{ "__gc", Version_gc },
{ "__eq", Version_eq },
{ "__lt", Version_lt },
{ "__le", Version_le },
{ "__tostring", Version_tostring },
{ nullptr, nullptr }
};
const Property Version_properties[] = {
{ "major", Version_get_major, nullptr },
{ "minor", Version_get_minor, nullptr },
{ "patch", Version_get_patch, nullptr },
{ "prerelease", Version_get_prerelease, nullptr },
{ "prereleaseDigit", Version_get_prereleaseDigit, nullptr },
{ nullptr, nullptr, nullptr }
};
} // anonymous namespace
DEF_MTNAME(base::Version);
void register_version_class(lua_State* L)
{
using base::Version;
REG_CLASS(L, Version);
REG_CLASS_NEW(L, Version);
REG_CLASS_PROPERTIES(L, Version);
}
void push_version(lua_State* L, const base::Version& ver)
{
push_obj(L, ver);
}
} // namespace script
} // namespace app