-
Notifications
You must be signed in to change notification settings - Fork 2
/
ToLua.cpp
105 lines (96 loc) · 3.32 KB
/
ToLua.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// Created by koncord on 19.01.19.
//
#include "Parser.hpp"
#include "Utils.hpp"
#include "ToLua.hpp"
#include <regex>
static std::regex reFFIDefined(R"([u]?int[\d]+_t|bool|[u]?intptr_t|ptrdiff_t|size_t|wchar_t|va_list|__builtin_va_list|__gnuc_va_list)");
std::ostream &ToLua::Serialize(std::ostream &out) const
{
out << "local ffi = require(\"ffi\")\n\nffi.cdef [[\n";
const auto &defsInUse = parser.GetDefinesInUse();
if (!defsInUse.empty())
{
for (const auto &tdefName : defsInUse)
{
if (std::regex_match(tdefName, reFFIDefined))
continue;
out << " typedef ";
out << clang_getTypeSpelling(parser.GetTypeDef(tdefName)) << " " << tdefName << ";\n";
}
out << "\n";
}
for (const auto &decl : parser.GetTokens())
{
out << " " << clang_getTypeSpelling(decl.retType);
if (decl.retType.kind != CXType_Pointer)
out << " ";
out << decl.declName << "(";
auto endIt = decl.args.end();
for (auto it = decl.args.begin(); it != endIt; ++it)
{
out << clang_getTypeSpelling(it->type);
if (it->type.kind != CXType_Pointer)
out << " ";
out << it->name;
if (std::next(it) != endIt)
out << ", ";
}
out << ");\n";
}
out << "]]\n\n";
out << "local M = {}\n";
out << "M.C = ffi.C\n\n";
for (const auto &decl : parser.GetTokens())
{
// if (const auto it = docstrings.find(decl.declName); it != docstrings.end())
// {
// std::string luaComment = it->second;
// //luaComment.replace(luaComment.begin(), luaComment.end() -1 , "/**", "--!");
// //luaComment.replace(luaComment.begin(), luaComment.end() , "*/", "--!");
// luaComment.replace(luaComment.begin(), luaComment.end() , "*", "--!");
// out << luaComment;
// }
std::string func = "M.";
std::string args;
auto endIt = decl.args.end();
for (auto it = decl.args.begin(); it != endIt; ++it)
{
args += it->name;
if (std::next(it) != endIt)
args += ", ";
}
func += decl.declName;
func += " = function(" + args + ")\n";
std::string cfunc = "M.C.";
cfunc += decl.declName;
cfunc += "(" + args + ")";
if (decl.retType.kind == CXType_Pointer)
{
CXType popinteeType = clang_getPointeeType(decl.retType);
//bool isConst = clang_isConstQualifiedType(popinteeType);
if (popinteeType.kind == CXType_Char_S || popinteeType.kind == CXType_SChar)
{
func += " local result = " + cfunc + "\n";
func += " return ffi.string(result)\n";
}
else
{
throw std::runtime_error("Unsupported pointee return type: "
+ GetCXXString(clang_getTypeKindSpelling(popinteeType.kind)));
}
}
else
{
func += " ";
if (decl.retType.kind != CXType_Void)
func += "return ";
func += cfunc + "\n";
}
func += "end\n";
out << func << "\n";
}
out << "\nreturn M\n";
return out;
}