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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
/* Copyright (c) 2017-2025 Li Jin <dragon-fly@qq.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include "yuescript/yue_compiler.h"
extern "C" {
#include "lauxlib.h"
#include "lua.h"
#include "lualib.h"
int luaopen_yue(lua_State* L);
} // extern "C"
static void openlibs(void* state) {
lua_State* L = static_cast<lua_State*>(state);
luaL_openlibs(L);
luaopen_yue(L);
luaL_requiref(L, "yue", luaopen_yue, 1);
lua_pop(L, 1);
}
#define YUE_ARGS nullptr, openlibs
#include <string_view>
using namespace std::string_view_literals;
#include <emscripten/bind.h>
using namespace emscripten;
struct YueResult {
std::string codes;
std::string err;
};
YueResult tolua(const std::string& codes, bool reserveLineNumber = true, bool implicitReturnRoot = true, bool useSpaceOverTab = true) {
yue::YueConfig config;
config.reserveLineNumber = reserveLineNumber;
config.implicitReturnRoot = implicitReturnRoot;
config.useSpaceOverTab = useSpaceOverTab;
auto result = yue::YueCompiler{YUE_ARGS}.compile(codes, config);
if (result.error) {
return {std::string(), result.error.value().displayMessage};
} else {
return {result.codes, std::string()};
}
}
std::string version() { return std::string(yue::version); }
#define _DEFER(code, line) std::shared_ptr<void> _defer_##line(nullptr, [&](auto) { code; })
#define DEFER(code) _DEFER(code, __LINE__)
void pushYue(lua_State* L, std::string_view name) {
lua_getglobal(L, "package"); // package
lua_getfield(L, -1, "loaded"); // package loaded
lua_getfield(L, -1, "yue"); // package loaded yue
lua_pushlstring(L, &name.front(), name.size()); // package loaded yue name
lua_gettable(L, -2); // loaded[name], package loaded yue item
lua_insert(L, -4); // item package loaded yue
lua_pop(L, 3); // item
}
void pushOptions(lua_State* L, int lineOffset) {
lua_newtable(L);
lua_pushliteral(L, "lint_global");
lua_pushboolean(L, 0);
lua_rawset(L, -3);
lua_pushliteral(L, "implicit_return_root");
lua_pushboolean(L, 1);
lua_rawset(L, -3);
lua_pushliteral(L, "reserve_line_number");
lua_pushboolean(L, 1);
lua_rawset(L, -3);
lua_pushliteral(L, "space_over_tab");
lua_pushboolean(L, 0);
lua_rawset(L, -3);
lua_pushliteral(L, "same_module");
lua_pushboolean(L, 1);
lua_rawset(L, -3);
lua_pushliteral(L, "line_offset");
lua_pushinteger(L, lineOffset);
lua_rawset(L, -3);
}
std::string exec(const std::string& codes) {
lua_State* L = luaL_newstate();
openlibs(L);
DEFER(lua_close(L));
auto execStr = [&](const std::string& s) {
std::string result;
pushYue(L, "insert_loader"sv);
if (lua_pcall(L, 0, 0, 0) != 0) {
result += lua_tostring(L, -1);
result += '\n';
return result;
}
pushYue(L, "loadstring"sv);
lua_pushlstring(L, s.c_str(), s.size());
lua_pushliteral(L, "=(eval str)");
if (lua_pcall(L, 2, 2, 0) != 0) {
result += lua_tostring(L, -1);
result += '\n';
return result;
}
if (lua_isnil(L, -2) != 0) {
result += lua_tostring(L, -1);
result += '\n';
return result;
}
lua_pop(L, 1);
pushYue(L, "pcall"sv);
lua_insert(L, -2);
int last = lua_gettop(L) - 2;
if (lua_pcall(L, 1, LUA_MULTRET, 0) != 0) {
result = lua_tostring(L, -1);
result += '\n';
return result;
}
int cur = lua_gettop(L);
int retCount = cur - last;
bool success = lua_toboolean(L, -retCount) != 0;
if (success) {
if (retCount > 1) {
for (int i = 1; i < retCount; ++i) {
result += luaL_tolstring(L, -retCount + i, nullptr);
result += '\n';
lua_pop(L, 1);
}
}
} else {
result += lua_tostring(L, -1);
result += '\n';
}
return result;
};
execStr(R"yuescript(
_G.__output = []
_G.print = (...)->
_G.__output[] = table.concat [tostring select i, ... for i = 1, select "#", ...], " "
_G.__output[] = "\n"
_io_write = io.write
_G.io.write = (...)->
_G.__output[] = table.concat [tostring select i, ... for i = 1, select "#", ...]
_io_write ...
)yuescript");
std::string res2 = execStr(codes);
std::string res1 = execStr(R"yuescript(
res = table.concat _G.__output
return res if res ~= ""
)yuescript");
if (res1.empty()) {
return res2;
} else {
if (res2.empty()) {
return res1;
} else {
return res1 + res2;
}
}
}
EMSCRIPTEN_BINDINGS(yue) {
value_array<YueResult>("YueResult")
.element(&YueResult::codes)
.element(&YueResult::err);
function("tolua", &tolua);
function("exec", &exec);
function("version", &version);
}
|