just starting
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"$schema": "https://raw.githubusercontent.com/LuaLS/vscode-lua/master/setting/schema.json",
|
||||
"runtime.version": "LuaJIT",
|
||||
"runtime.version": "Lua 5.4",
|
||||
"runtime.path": [
|
||||
"./?/init.lua",
|
||||
"./?.lua",
|
||||
|
||||
27
Makefile
27
Makefile
@@ -1,9 +1,13 @@
|
||||
SRCDIR := .
|
||||
CC := $(CROSS)gcc
|
||||
CFLAGS := -fPIC -O2
|
||||
CFLAGS += -Wall -I$(SRCDIR)/kiwi
|
||||
CFLAGS := -fPIC -Os
|
||||
CXXFLAGS := -I$(SRCDIR)/kiwi -fno-rtti
|
||||
F_LTO := -flto=auto
|
||||
CXXFLAGS_EXTRA := -pedantic -std=c++14 -Wall $(F_LTO)
|
||||
CFLAGS_EXTRA := -pedantic -std=c99 -Wall $(F_LTO)
|
||||
LIBFLAG := -shared
|
||||
LIB_EXT := so
|
||||
LUA_INCDIR := /usr/include
|
||||
|
||||
ifeq ($(findstring gcc,$(CC)),gcc)
|
||||
CXX := $(subst gcc,g++,$(CC))
|
||||
@@ -23,6 +27,9 @@ else
|
||||
endif
|
||||
endif
|
||||
|
||||
OBJS := ckiwi.o luakiwi.o
|
||||
|
||||
VPATH = $(SRCDIR)/ckiwi
|
||||
|
||||
all: ckiwi.$(LIB_EXT)
|
||||
|
||||
@@ -31,9 +38,19 @@ install:
|
||||
cp -f kiwi.lua $(INST_LUADIR)/kiwi.lua
|
||||
|
||||
clean:
|
||||
rm -f ckiwi.$(LIB_EXT)
|
||||
rm -f ckiwi.$(LIB_EXT) $(OBJS)
|
||||
|
||||
ckiwi.$(LIB_EXT): $(SRCDIR)/ckiwi/ckiwi.cpp $(SRCDIR)/ckiwi/ckiwi.h
|
||||
$(CXX) $(CXXFLAGS) $(CFLAGS) -fPIC -Wall -I$(SRCDIR)/kiwi $(LIBFLAG) -o $@ $<
|
||||
|
||||
ckiwi.$(LIB_EXT): $(OBJS)
|
||||
$(CXX) $(CXXFLAGS) $(CFLAGS) $(CFLAGS_EXTRA) -I$(SRCDIR)/kiwi $(LIBFLAG) -o $@ $^
|
||||
|
||||
ckiwi.o: ckiwi.h
|
||||
luakiwi.o: ckiwi.h luakiwi-int.h luacompat.h
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) $(CFLAGS_EXTRA) -I$(LUA_INCDIR) -c -o $@ $<
|
||||
|
||||
%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) $(CFLAGS) $(CXXFLAGS_EXTRA) -c -o $@ $<
|
||||
|
||||
.PHONY: all install clean
|
||||
|
||||
138
ckiwi/luacompat.h
Normal file
138
ckiwi/luacompat.h
Normal file
@@ -0,0 +1,138 @@
|
||||
#ifndef LKIWI_LUACOMPAT_H_
|
||||
#define LKIWI_LUACOMPAT_H_
|
||||
|
||||
#include <lauxlib.h>
|
||||
#include <lua.h>
|
||||
#include <lualib.h>
|
||||
|
||||
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501
|
||||
|
||||
#define LUA_OPADD 0
|
||||
#define LUA_OPSUB 1
|
||||
#define LUA_OPMUL 2
|
||||
#define LUA_OPDIV 3
|
||||
#define LUA_OPMOD 4
|
||||
#define LUA_OPPOW 5
|
||||
#define LUA_OPUNM 6
|
||||
|
||||
static int lua_absindex(lua_State* L, int i) {
|
||||
if (i < 0 && i > LUA_REGISTRYINDEX)
|
||||
i += lua_gettop(L) + 1;
|
||||
return i;
|
||||
}
|
||||
|
||||
static lua_Number lua_tonumberx(lua_State* L, int i, int* isnum) {
|
||||
lua_Number n = lua_tonumber(L, i);
|
||||
if (isnum != NULL) {
|
||||
*isnum = (n != 0 || lua_isnumber(L, i));
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static int luaL_typeerror(lua_State* L, int arg, const char* tname) {
|
||||
const char* msg;
|
||||
const char* typearg; /* name for the type of the actual argument */
|
||||
if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING)
|
||||
typearg = lua_tostring(L, -1); /* use the given type name */
|
||||
else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA)
|
||||
typearg = "light userdata"; /* special name for messages */
|
||||
else
|
||||
typearg = luaL_typename(L, arg); /* standard name */
|
||||
msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg);
|
||||
return luaL_argerror(L, arg, msg);
|
||||
}
|
||||
|
||||
#endif /* LUA_VERSION_NUM == 501 */
|
||||
|
||||
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
|
||||
|
||||
static lua_Integer lua_tointegerx(lua_State* L, int i, int* isnum) {
|
||||
int ok = 0;
|
||||
lua_Number n = lua_tonumberx(L, i, &ok);
|
||||
if (ok) {
|
||||
if (n == (lua_Integer)n) {
|
||||
if (isnum)
|
||||
*isnum = 1;
|
||||
return (lua_Integer)n;
|
||||
}
|
||||
}
|
||||
if (isnum)
|
||||
*isnum = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void compat_reverse(lua_State* L, int a, int b) {
|
||||
for (; a < b; ++a, --b) {
|
||||
lua_pushvalue(L, a);
|
||||
lua_pushvalue(L, b);
|
||||
lua_replace(L, a);
|
||||
lua_replace(L, b);
|
||||
}
|
||||
}
|
||||
|
||||
static void lua_rotate(lua_State* L, int idx, int n) {
|
||||
int n_elems = 0;
|
||||
idx = lua_absindex(L, idx);
|
||||
n_elems = lua_gettop(L) - idx + 1;
|
||||
if (n < 0)
|
||||
n += n_elems;
|
||||
if (n > 0 && n < n_elems) {
|
||||
luaL_checkstack(L, 2, "not enough stack slots available");
|
||||
n = n_elems - n;
|
||||
compat_reverse(L, idx, idx + n - 1);
|
||||
compat_reverse(L, idx + n, idx + n_elems - 1);
|
||||
compat_reverse(L, idx, idx + n_elems - 1);
|
||||
}
|
||||
}
|
||||
|
||||
static int lua_geti(lua_State* L, int index, lua_Integer i) {
|
||||
index = lua_absindex(L, index);
|
||||
lua_pushinteger(L, i);
|
||||
lua_gettable(L, index);
|
||||
return lua_type(L, -1);
|
||||
}
|
||||
|
||||
static const char* luaL_tolstring(lua_State* L, int idx, size_t* len) {
|
||||
if (!luaL_callmeta(L, idx, "__tostring")) {
|
||||
int t = lua_type(L, idx), tt = 0;
|
||||
char const* name = NULL;
|
||||
switch (t) {
|
||||
case LUA_TNIL:
|
||||
lua_pushliteral(L, "nil");
|
||||
break;
|
||||
case LUA_TSTRING:
|
||||
case LUA_TNUMBER:
|
||||
lua_pushvalue(L, idx);
|
||||
break;
|
||||
case LUA_TBOOLEAN:
|
||||
if (lua_toboolean(L, idx))
|
||||
lua_pushliteral(L, "true");
|
||||
else
|
||||
lua_pushliteral(L, "false");
|
||||
break;
|
||||
default:
|
||||
tt = luaL_getmetafield(L, idx, "__name");
|
||||
name = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : lua_typename(L, t);
|
||||
lua_pushfstring(L, "%s: %p", name, lua_topointer(L, idx));
|
||||
if (tt != LUA_TNIL)
|
||||
lua_replace(L, -2);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (!lua_isstring(L, -1))
|
||||
luaL_error(L, "'__tostring' must return a string");
|
||||
}
|
||||
return lua_tolstring(L, -1, len);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if !defined(luaL_newlibtable)
|
||||
#define luaL_newlibtable(L, l) lua_createtable(L, 0, sizeof(l) / sizeof((l)[0]) - 1)
|
||||
#endif
|
||||
|
||||
#if !defined(luaL_checkversion)
|
||||
#define luaL_checkversion(L) ((void)0)
|
||||
#endif
|
||||
|
||||
#endif // LKIWI_LUACOMPAT_H_
|
||||
84
ckiwi/luakiwi-int.h
Normal file
84
ckiwi/luakiwi-int.h
Normal file
@@ -0,0 +1,84 @@
|
||||
#ifndef LUAKIWI_INT_H_
|
||||
#define LUAKIWI_INT_H_
|
||||
|
||||
#include "luacompat.h"
|
||||
|
||||
#define ARR_COUNT(x) (sizeof(x) / sizeof((x)[0]))
|
||||
|
||||
#if defined(__GNUC__) && !defined(LKIWI_NO_BUILTIN)
|
||||
#define l_likely(x) (__builtin_expect(((x) != 0), 1))
|
||||
#define l_unlikely(x) (__builtin_expect(((x) != 0), 0))
|
||||
#else
|
||||
#define l_likely(x) (x)
|
||||
#define l_unlikely(x) (x)
|
||||
#endif
|
||||
|
||||
#ifndef LKIWI_NO_STALLOC
|
||||
#if defined(_MSC_VER)
|
||||
#include <malloc.h>
|
||||
|
||||
static inline void* stalloc(size_t sz) {
|
||||
void* p = _malloca(sz);
|
||||
if (!p) {
|
||||
luaL_error(L, "out of memory");
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
#define stfree(ptr) _freea(ptr)
|
||||
#else
|
||||
#include <alloca.h>
|
||||
#define stalloc alloca
|
||||
#define stfree(ptr)
|
||||
#endif
|
||||
#else
|
||||
#include <stdlib.h>
|
||||
|
||||
static inline void* stalloc(size_t sz) {
|
||||
void* p = malloc(sz);
|
||||
if (l_unlikely(!p)) {
|
||||
luaL_error(L, "out of memory");
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
#define stfree(ptr) free(ptr)
|
||||
#endif
|
||||
|
||||
// Lua 5.1 compatibility for missing lua_arith.
|
||||
static void compat_arith_unm(lua_State* L) {
|
||||
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
|
||||
lua_Number n = lua_tonumber(L, -1);
|
||||
if (n != 0 || lua_isnumber(L, -1)) {
|
||||
lua_pop(L, 1);
|
||||
lua_pushnumber(L, -n);
|
||||
} else {
|
||||
if (!luaL_callmeta(L, -1, "__unm"))
|
||||
luaL_error(L, "attempt to perform arithmetic on a %s value", luaL_typename(L, -1));
|
||||
lua_replace(L, -2);
|
||||
}
|
||||
#else
|
||||
lua_arith(L, LUA_OPUNM);
|
||||
#endif
|
||||
}
|
||||
|
||||
// This version supports placeholders.
|
||||
static void setfuncs(lua_State* L, const luaL_Reg* l, int nup) {
|
||||
luaL_checkstack(L, nup, "too many upvalues");
|
||||
for (; l->name != NULL; l++) { /* fill the table with given functions */
|
||||
if (l->func == NULL) /* place holder? */
|
||||
lua_pushboolean(L, 0);
|
||||
else {
|
||||
int i;
|
||||
for (i = 0; i < nup; i++) /* copy upvalues to the top */
|
||||
lua_pushvalue(L, -nup);
|
||||
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
|
||||
}
|
||||
lua_setfield(L, -(nup + 2), l->name);
|
||||
}
|
||||
lua_pop(L, nup); /* remove upvalues */
|
||||
}
|
||||
|
||||
#define newlib(L, l) (lua_newtable((L)), setfuncs((L), (l), 0))
|
||||
|
||||
#endif // LUAKIWI_INT_H_
|
||||
1699
ckiwi/luakiwi.c
Normal file
1699
ckiwi/luakiwi.c
Normal file
File diff suppressed because it is too large
Load Diff
14
kiwi.lua
14
kiwi.lua
@@ -345,7 +345,7 @@ end
|
||||
do
|
||||
--- Variables are the values the constraint solver calculates.
|
||||
---@class kiwi.Var: ffi.cdata*
|
||||
---@overload fun(name: string): kiwi.Var
|
||||
---@overload fun(name: string?): kiwi.Var
|
||||
---@operator mul(number): kiwi.Term
|
||||
---@operator div(number): kiwi.Term
|
||||
---@operator unm: kiwi.Term
|
||||
@@ -559,7 +559,7 @@ do
|
||||
---@param expr kiwi.Expression
|
||||
---@param constant number
|
||||
---@nodiscard
|
||||
local function mul_expr_constant(expr, constant)
|
||||
local function mul_expr_coeff(expr, constant)
|
||||
local ret = ffi_gc(ffi_new(Expression, expr.term_count), ckiwi.kiwi_expression_del_vars) --[[@as kiwi.Expression]]
|
||||
for i = 0, expr.term_count - 1 do
|
||||
local st = expr.terms_[i] --[[@as kiwi.Term]]
|
||||
@@ -662,9 +662,9 @@ do
|
||||
|
||||
function Expression_mt.__mul(a, b)
|
||||
if type(a) == "number" then
|
||||
return mul_expr_constant(b, a)
|
||||
return mul_expr_coeff(b, a)
|
||||
elseif type(b) == "number" then
|
||||
return mul_expr_constant(a, b)
|
||||
return mul_expr_coeff(a, b)
|
||||
end
|
||||
op_error(a, b, "*")
|
||||
end
|
||||
@@ -673,11 +673,11 @@ do
|
||||
if type(b) ~= "number" then
|
||||
op_error(a, b, "/")
|
||||
end
|
||||
return mul_expr_constant(a, 1.0 / b)
|
||||
return mul_expr_coeff(a, 1.0 / b)
|
||||
end
|
||||
|
||||
function Expression_mt:__unm()
|
||||
return mul_expr_constant(self, -1.0)
|
||||
return mul_expr_coeff(self, -1.0)
|
||||
end
|
||||
|
||||
function Expression_mt.__add(a, b)
|
||||
@@ -719,7 +719,7 @@ do
|
||||
--- Constraints can be built with arbitrary left and right hand expressions. But
|
||||
--- ultimately they all have the form `expression [op] 0`.
|
||||
---@class kiwi.Constraint: ffi.cdata*
|
||||
---@overload fun(lhs: kiwi.Expression, rhs: kiwi.Expression?, op: kiwi.RelOp?, strength: number?): kiwi.Constraint
|
||||
---@overload fun(lhs: kiwi.Expression?, rhs: kiwi.Expression?, op: kiwi.RelOp?, strength: number?): kiwi.Constraint
|
||||
local Constraint_cls = {
|
||||
--- The strength of the constraint.
|
||||
---@type fun(self: kiwi.Constraint): number
|
||||
|
||||
@@ -22,6 +22,7 @@ build = {
|
||||
type = "make",
|
||||
build_variables = {
|
||||
CFLAGS = "$(CFLAGS)",
|
||||
LUA_INCDIR = "$(LUA_INCDIR)",
|
||||
LIBFLAG = "$(LIBFLAG)",
|
||||
LIB_EXT = "$(LIB_EXTENSION)",
|
||||
OBJ_EXT = "$(OBJ_EXTENSION)",
|
||||
|
||||
@@ -13,7 +13,7 @@ describe("solver", function()
|
||||
|
||||
it("should create a solver", function()
|
||||
assert.True(kiwi.is_solver(solver))
|
||||
assert.False(kiwi.is_solver(kiwi.Term()))
|
||||
assert.False(kiwi.is_solver(kiwi.Term(kiwi.Var("v1"))))
|
||||
end)
|
||||
|
||||
describe("edit variables", function()
|
||||
@@ -104,25 +104,15 @@ describe("solver", function()
|
||||
end)
|
||||
|
||||
it("tolerates a nil self", function()
|
||||
local _, err = pcall(function()
|
||||
return kiwi.Solver.add_edit_var(nil, v1, kiwi.strength.STRONG) ---@diagnostic disable-line: param-type-mismatch
|
||||
assert.error(function()
|
||||
kiwi.Solver.add_edit_var(nil, v1, kiwi.strength.STRONG) ---@diagnostic disable-line: param-type-mismatch
|
||||
end)
|
||||
assert.True(kiwi.is_error(err))
|
||||
assert.Nil(err.solver)
|
||||
assert.equal(v1, err.item)
|
||||
assert.equal("KiwiErrNullObject", err.kind)
|
||||
assert.equal("null object passed as argument #0 (self)", err.message)
|
||||
end)
|
||||
|
||||
it("tolerates a nil var", function()
|
||||
local _, err = pcall(function()
|
||||
return solver:add_edit_var(nil, kiwi.strength.STRONG) ---@diagnostic disable-line: param-type-mismatch
|
||||
assert.error(function()
|
||||
solver:add_edit_var(nil, kiwi.strength.STRONG) ---@diagnostic disable-line: param-type-mismatch
|
||||
end)
|
||||
assert.True(kiwi.is_error(err))
|
||||
assert.True(kiwi.is_solver(err.solver))
|
||||
assert.Nil(err.item)
|
||||
assert.equal("KiwiErrNullObject", err.kind)
|
||||
assert.equal("null object passed as argument #1", err.message)
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -154,7 +144,7 @@ describe("solver", function()
|
||||
it("should require a strength argument", function()
|
||||
assert.error(function()
|
||||
solver:add_edit_vars({ v1, v2 }) ---@diagnostic disable-line: missing-parameter
|
||||
end, "bad argument #3 to 'f' (cannot convert 'nil' to 'double')")
|
||||
end)
|
||||
end)
|
||||
|
||||
it("should error on duplicate variable", function()
|
||||
@@ -211,14 +201,9 @@ describe("solver", function()
|
||||
end)
|
||||
|
||||
it("tolerates a nil self", function()
|
||||
local _, err = pcall(function()
|
||||
return kiwi.Solver.add_edit_vars(nil, { v1, v2 }, kiwi.strength.STRONG) ---@diagnostic disable-line: param-type-mismatch
|
||||
assert.has_error(function()
|
||||
kiwi.Solver.add_edit_vars(nil, { v1, v2 }, kiwi.strength.STRONG) ---@diagnostic disable-line: param-type-mismatch
|
||||
end)
|
||||
assert.True(kiwi.is_error(err))
|
||||
assert.Nil(err.solver)
|
||||
assert.equal(v1, err.item)
|
||||
assert.equal("KiwiErrNullObject", err.kind)
|
||||
assert.equal("null object passed as argument #0 (self)", err.message)
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -274,25 +259,15 @@ describe("solver", function()
|
||||
end)
|
||||
|
||||
it("tolerates a nil self", function()
|
||||
local _, err = pcall(function()
|
||||
return kiwi.Solver.remove_edit_var(nil, v1) ---@diagnostic disable-line: param-type-mismatch
|
||||
assert.has_error(function()
|
||||
kiwi.Solver.remove_edit_var(nil, v1) ---@diagnostic disable-line: param-type-mismatch
|
||||
end)
|
||||
assert.True(kiwi.is_error(err))
|
||||
assert.Nil(err.solver)
|
||||
assert.equal(v1, err.item)
|
||||
assert.equal("KiwiErrNullObject", err.kind)
|
||||
assert.equal("null object passed as argument #0 (self)", err.message)
|
||||
end)
|
||||
|
||||
it("tolerates a nil var", function()
|
||||
local _, err = pcall(function()
|
||||
return solver:remove_edit_var(nil) ---@diagnostic disable-line: param-type-mismatch
|
||||
assert.has_error(function()
|
||||
solver:remove_edit_var(nil) ---@diagnostic disable-line: param-type-mismatch
|
||||
end)
|
||||
assert.True(kiwi.is_error(err))
|
||||
assert.True(kiwi.is_solver(err.solver))
|
||||
assert.Nil(err.item)
|
||||
assert.equal("KiwiErrNullObject", err.kind)
|
||||
assert.equal("null object passed as argument #1", err.message)
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -351,14 +326,9 @@ describe("solver", function()
|
||||
end)
|
||||
|
||||
it("tolerates a nil self", function()
|
||||
local _, err = pcall(function()
|
||||
return kiwi.Solver.remove_edit_vars(nil, { v1, v2 }) ---@diagnostic disable-line: param-type-mismatch
|
||||
assert.has_error(function()
|
||||
kiwi.Solver.remove_edit_vars(nil, { v1, v2 }) ---@diagnostic disable-line: param-type-mismatch
|
||||
end)
|
||||
assert.True(kiwi.is_error(err))
|
||||
assert.Nil(err.solver)
|
||||
assert.equal(v1, err.item)
|
||||
assert.equal("KiwiErrNullObject", err.kind)
|
||||
assert.equal("null object passed as argument #0 (self)", err.message)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -10,7 +10,7 @@ describe("Var", function()
|
||||
assert.False(kiwi.is_var(kiwi.Constraint()))
|
||||
|
||||
assert.error(function()
|
||||
kiwi.Var(1)
|
||||
kiwi.Var({})
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -26,7 +26,7 @@ describe("Var", function()
|
||||
v:set_name("Δ")
|
||||
assert.equal("Δ", v:name())
|
||||
assert.error(function()
|
||||
v:set_name(1)
|
||||
v:set_name({})
|
||||
end)
|
||||
end)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user