diff options
| author | Li Jin <dragon-fly@qq.com> | 2021-09-19 17:01:59 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2021-09-19 17:01:59 +0800 |
| commit | ddb5afc6283e13479e866db41350b188aab1a813 (patch) | |
| tree | 6a56fcdbd8bb0f642c49e58546d0842f82536509 | |
| parent | 8e02ec2eefc700b0ef307f24596e7c36bdd84a4f (diff) | |
| download | yuescript-ddb5afc6283e13479e866db41350b188aab1a813.tar.gz yuescript-ddb5afc6283e13479e866db41350b188aab1a813.tar.bz2 yuescript-ddb5afc6283e13479e866db41350b188aab1a813.zip | |
cleanup.
| -rw-r--r-- | src/yue.cpp | 3 | ||||
| -rw-r--r-- | src/yuescript/stacktraceplus.h | 4 | ||||
| -rw-r--r-- | src/yuescript/yuescript.cpp | 48 | ||||
| -rw-r--r-- | src/yuescript/yuescript.h | 104 |
4 files changed, 61 insertions, 98 deletions
diff --git a/src/yue.cpp b/src/yue.cpp index f891ebe..b04bd92 100644 --- a/src/yue.cpp +++ b/src/yue.cpp | |||
| @@ -37,7 +37,8 @@ int luaopen_yue(lua_State* L); | |||
| 37 | static void openlibs(void* state) { | 37 | static void openlibs(void* state) { |
| 38 | lua_State* L = static_cast<lua_State*>(state); | 38 | lua_State* L = static_cast<lua_State*>(state); |
| 39 | luaL_openlibs(L); | 39 | luaL_openlibs(L); |
| 40 | luaopen_yue(L); | 40 | luaL_requiref(L, "yue", luaopen_yue, 1); |
| 41 | lua_pop(L, 1); | ||
| 41 | } | 42 | } |
| 42 | 43 | ||
| 43 | void pushYue(lua_State* L, std::string_view name) { | 44 | void pushYue(lua_State* L, std::string_view name) { |
diff --git a/src/yuescript/stacktraceplus.h b/src/yuescript/stacktraceplus.h index b165754..48ea8c4 100644 --- a/src/yuescript/stacktraceplus.h +++ b/src/yuescript/stacktraceplus.h | |||
| @@ -398,9 +398,9 @@ function _M.stacktrace(thread, message, level) | |||
| 398 | end | 398 | end |
| 399 | dumper:add("\r\n}") | 399 | dumper:add("\r\n}") |
| 400 | elseif type(message) == "string" then | 400 | elseif type(message) == "string" then |
| 401 | local fname, line, msg = message:match('(.+):(%d+): (.*)$') | 401 | local fname, line, msg = message:match('([^\n]+):(%d+): (.*)$') |
| 402 | if fname then | 402 | if fname then |
| 403 | local nfname, nline, nmsg = fname:match('(.+):(%d+): (.*)$') | 403 | local nfname, nmsg = fname:match('(.+):%d+: (.*)$') |
| 404 | if nfname then | 404 | if nfname then |
| 405 | fname = nmsg | 405 | fname = nmsg |
| 406 | end | 406 | end |
diff --git a/src/yuescript/yuescript.cpp b/src/yuescript/yuescript.cpp index 9928f4d..d534f3e 100644 --- a/src/yuescript/yuescript.cpp +++ b/src/yuescript/yuescript.cpp | |||
| @@ -19,9 +19,12 @@ static void init_yuescript(lua_State* L) { | |||
| 19 | if (luaL_loadbuffer(L, yuescriptCodes, sizeof(yuescriptCodes) / sizeof(yuescriptCodes[0]) - 1, "=(yuescript)") != 0) { | 19 | if (luaL_loadbuffer(L, yuescriptCodes, sizeof(yuescriptCodes) / sizeof(yuescriptCodes[0]) - 1, "=(yuescript)") != 0) { |
| 20 | std::string err = std::string("failed to load yuescript module.\n") + lua_tostring(L, -1); | 20 | std::string err = std::string("failed to load yuescript module.\n") + lua_tostring(L, -1); |
| 21 | luaL_error(L, err.c_str()); | 21 | luaL_error(L, err.c_str()); |
| 22 | } else if (lua_pcall(L, 0, 0, 0) != 0) { | 22 | } else { |
| 23 | std::string err = std::string("failed to init yuescript module.\n") + lua_tostring(L, -1); | 23 | lua_insert(L, -2); |
| 24 | luaL_error(L, err.c_str()); | 24 | if (lua_pcall(L, 1, 0, 0) != 0) { |
| 25 | std::string err = std::string("failed to init yuescript module.\n") + lua_tostring(L, -1); | ||
| 26 | luaL_error(L, err.c_str()); | ||
| 27 | } | ||
| 25 | } | 28 | } |
| 26 | } | 29 | } |
| 27 | 30 | ||
| @@ -115,24 +118,29 @@ static int yuetolua(lua_State* L) { | |||
| 115 | return 3; | 118 | return 3; |
| 116 | } | 119 | } |
| 117 | 120 | ||
| 121 | static const luaL_Reg yuelib[] = { | ||
| 122 | {"to_lua", yuetolua}, | ||
| 123 | {"version", nullptr}, | ||
| 124 | {"options", nullptr}, | ||
| 125 | {"load_stacktraceplus", nullptr}, | ||
| 126 | {nullptr, nullptr} | ||
| 127 | }; | ||
| 128 | |||
| 118 | int luaopen_yue(lua_State* L) { | 129 | int luaopen_yue(lua_State* L) { |
| 119 | lua_getglobal(L, "package"); // package | 130 | luaL_newlib(L, yuelib); // yue |
| 120 | lua_getfield(L, -1, "loaded"); // package loaded | 131 | lua_pushlstring(L, &yue::version.front(), yue::version.size()); // yue version |
| 121 | lua_createtable(L, 0, 0); // package loaded yue | 132 | lua_setfield(L, -2, "version"); // yue["version"] = version, yue |
| 122 | lua_pushcfunction(L, yuetolua); // package loaded yue func | 133 | lua_createtable(L, 0, 0); // yue options |
| 123 | lua_setfield(L, -2, "to_lua"); // yue["to_lua"] = func, package loaded yue | 134 | lua_pushlstring(L, &yue::extension.front(), yue::extension.size()); // yue options ext |
| 124 | lua_pushlstring(L, &yue::version.front(), yue::version.size()); // package loaded yue version | 135 | lua_setfield(L, -2, "extension"); // options["extension"] = ext, yue options |
| 125 | lua_setfield(L, -2, "version"); // yue["version"] = version, package loaded yue | 136 | lua_pushliteral(L, LUA_DIRSEP); |
| 126 | lua_createtable(L, 0, 0); // package loaded yue options | 137 | lua_setfield(L, -2, "dirsep"); // options["dirsep"] = dirsep, yue options |
| 127 | lua_pushlstring(L, &yue::extension.front(), yue::extension.size()); // package loaded yue options ext | 138 | lua_setfield(L, -2, "options"); // yue["options"] = options, yue |
| 128 | lua_setfield(L, -2, "extension"); // options["extension"] = ext, package loaded yue options | 139 | lua_pushcfunction(L, init_stacktraceplus); // yue func1 |
| 129 | lua_setfield(L, -2, "options"); // yue["options"] = options, package loaded yue | 140 | lua_setfield(L, -2, "load_stacktraceplus"); // yue["load_stacktraceplus"] = func1, yue |
| 130 | lua_pushcfunction(L, init_stacktraceplus); // package loaded yue func1 | 141 | lua_pushvalue(L, -1); // yue yue |
| 131 | lua_setfield(L, -2, "load_stacktraceplus"); // yue["load_stacktraceplus"] = func1, package loaded yue | 142 | init_yuescript(L); // yue |
| 132 | lua_setfield(L, -2, "yue"); // loaded["yue"] = yue, package loaded | 143 | return 1; |
| 133 | lua_pop(L, 2); // empty | ||
| 134 | init_yuescript(L); | ||
| 135 | return 0; | ||
| 136 | } | 144 | } |
| 137 | 145 | ||
| 138 | } // extern "C" | 146 | } // extern "C" |
diff --git a/src/yuescript/yuescript.h b/src/yuescript/yuescript.h index 6fc9c20..30006ba 100644 --- a/src/yuescript/yuescript.h +++ b/src/yuescript/yuescript.h | |||
| @@ -20,15 +20,9 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |||
| 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | THE SOFTWARE.]] | 21 | THE SOFTWARE.]] |
| 22 | 22 | ||
| 23 | local yue = require("yue") | 23 | local yue = select(1, ...) |
| 24 | local concat, insert, remove = table.concat, table.insert, table.remove | 24 | local concat, insert = table.concat, table.insert |
| 25 | local unpack = unpack or table.unpack | 25 | local unpack = unpack or table.unpack |
| 26 | local lua = { | ||
| 27 | loadstring = loadstring, | ||
| 28 | load = load | ||
| 29 | } | ||
| 30 | local split, get_options, create_yuepath, yue_loader, load_text, yue_call, loadstring, loadfile, dofile, insert_loader, remove_loader, yue_require, find_modulepath | ||
| 31 | yue.dirsep = "/" | ||
| 32 | yue.yue_compiled = { } | 26 | yue.yue_compiled = { } |
| 33 | yue.file_exist = function(fname) | 27 | yue.file_exist = function(fname) |
| 34 | local file = io.open(fname) | 28 | local file = io.open(fname) |
| @@ -48,18 +42,7 @@ yue.read_file = function(fname) | |||
| 48 | file:close() | 42 | file:close() |
| 49 | return text | 43 | return text |
| 50 | end | 44 | end |
| 51 | split = function(str, delim) | 45 | local function get_options(...) |
| 52 | if str == "" then | ||
| 53 | return { } | ||
| 54 | end | ||
| 55 | str = str .. delim | ||
| 56 | local tokens = { } | ||
| 57 | for m in str:gmatch("(.-)" .. delim) do | ||
| 58 | table.insert(tokens, m) | ||
| 59 | end | ||
| 60 | return tokens | ||
| 61 | end | ||
| 62 | get_options = function(...) | ||
| 63 | local count = select("#", ...) | 46 | local count = select("#", ...) |
| 64 | local opts = select(count, ...) | 47 | local opts = select(count, ...) |
| 65 | if type(opts) == "table" then | 48 | if type(opts) == "table" then |
| @@ -70,27 +53,13 @@ get_options = function(...) | |||
| 70 | return { }, ... | 53 | return { }, ... |
| 71 | end | 54 | end |
| 72 | end | 55 | end |
| 73 | create_yuepath = function(package_path) | 56 | local function find_modulepath(name) |
| 74 | local extension = yue.options.extension | 57 | local suffix = "." .. yue.options.extension |
| 75 | local yuepaths = { } | 58 | local dirsep = yue.options.dirsep |
| 76 | local tokens = split(package_path, ";") | 59 | local name_path = name:match("[\\/]") and name or name:gsub("%.", dirsep) |
| 77 | for i = 1, #tokens do | ||
| 78 | local path = tokens[i] | ||
| 79 | local prefix = path:match("^(.-)%.lua$") | ||
| 80 | if prefix then | ||
| 81 | table.insert(yuepaths, prefix .. "." .. extension) | ||
| 82 | end | ||
| 83 | end | ||
| 84 | return concat(yuepaths, ";") | ||
| 85 | end | ||
| 86 | find_modulepath = function(name) | ||
| 87 | if not package.yuepath then | ||
| 88 | package.yuepath = create_yuepath(package.path) | ||
| 89 | end | ||
| 90 | local name_path = name:match("[\\/]") and name or name:gsub("%.", yue.dirsep) | ||
| 91 | local file_exist, file_path | 60 | local file_exist, file_path |
| 92 | for path in package.yuepath:gmatch("[^;]+") do | 61 | for path in package.path:gmatch("[^;]+") do |
| 93 | file_path = path:gsub("?", name_path) | 62 | file_path = path:gsub("?", name_path):gsub("%.lua$", suffix) |
| 94 | file_exist = yue.file_exist(file_path) | 63 | file_exist = yue.file_exist(file_path) |
| 95 | if file_exist then | 64 | if file_exist then |
| 96 | break | 65 | break |
| @@ -102,17 +71,18 @@ find_modulepath = function(name) | |||
| 102 | return nil | 71 | return nil |
| 103 | end | 72 | end |
| 104 | end | 73 | end |
| 105 | load_text = function(name) | 74 | local function load_text(name) |
| 106 | local file_path = find_modulepath(name) | 75 | local file_path = find_modulepath(name) |
| 107 | if file_path then | 76 | if file_path then |
| 108 | return yue.read_file(file_path), file_path | 77 | return yue.read_file(file_path), file_path |
| 109 | end | 78 | end |
| 110 | return nil, nil | 79 | return nil, nil |
| 111 | end | 80 | end |
| 112 | yue_loader = function(name) | 81 | local yue_loadstring |
| 82 | local function yue_loader(name) | ||
| 113 | local text, file_path = load_text(name) | 83 | local text, file_path = load_text(name) |
| 114 | if text then | 84 | if text then |
| 115 | local res, err = loadstring(text, file_path) | 85 | local res, err = yue_loadstring(text, file_path) |
| 116 | if not res then | 86 | if not res then |
| 117 | error(file_path .. ": " .. err) | 87 | error(file_path .. ": " .. err) |
| 118 | end | 88 | end |
| @@ -120,7 +90,7 @@ yue_loader = function(name) | |||
| 120 | end | 90 | end |
| 121 | return nil, "Could not find yue file" | 91 | return nil, "Could not find yue file" |
| 122 | end | 92 | end |
| 123 | yue_call = function(f, ...) | 93 | local function yue_call(f, ...) |
| 124 | local args = { | 94 | local args = { |
| 125 | ... | 95 | ... |
| 126 | } | 96 | } |
| @@ -130,7 +100,7 @@ yue_call = function(f, ...) | |||
| 130 | return yue.stp.stacktrace(err, 1) | 100 | return yue.stp.stacktrace(err, 1) |
| 131 | end) | 101 | end) |
| 132 | end | 102 | end |
| 133 | loadstring = function(...) | 103 | yue_loadstring = function(...) |
| 134 | local options, str, chunk_name, mode, env = get_options(...) | 104 | local options, str, chunk_name, mode, env = get_options(...) |
| 135 | chunk_name = chunk_name or "=(yuescript.loadstring)" | 105 | chunk_name = chunk_name or "=(yuescript.loadstring)" |
| 136 | local code, err = yue.to_lua(str, options) | 106 | local code, err = yue.to_lua(str, options) |
| @@ -140,26 +110,23 @@ loadstring = function(...) | |||
| 140 | if chunk_name then | 110 | if chunk_name then |
| 141 | yue.yue_compiled["@" .. chunk_name] = code | 111 | yue.yue_compiled["@" .. chunk_name] = code |
| 142 | end | 112 | end |
| 143 | return (lua.loadstring or lua.load)(code, chunk_name, unpack({ | 113 | return (loadstring or load)(code, chunk_name, unpack({ |
| 144 | mode, | 114 | mode, |
| 145 | env | 115 | env |
| 146 | })) | 116 | })) |
| 147 | end | 117 | end |
| 148 | loadfile = function(fname, ...) | 118 | local function yue_loadfile(fname, ...) |
| 149 | local text = yue.read_file(fname) | 119 | local text = yue.read_file(fname) |
| 150 | return loadstring(text, tostring(fname), ...) | 120 | return yue_loadstring(text, tostring(fname), ...) |
| 151 | end | 121 | end |
| 152 | dofile = function(...) | 122 | local function yue_dofile(...) |
| 153 | local f = assert(loadfile(...)) | 123 | local f = assert(yue_loadfile(...)) |
| 154 | return f() | 124 | return f() |
| 155 | end | 125 | end |
| 156 | insert_loader = function(pos) | 126 | local function insert_loader(pos) |
| 157 | if pos == nil then | 127 | if pos == nil then |
| 158 | pos = 2 | 128 | pos = 2 |
| 159 | end | 129 | end |
| 160 | if not package.yuepath then | ||
| 161 | package.yuepath = create_yuepath(package.path) | ||
| 162 | end | ||
| 163 | local loaders = package.loaders or package.searchers | 130 | local loaders = package.loaders or package.searchers |
| 164 | for i = 1, #loaders do | 131 | for i = 1, #loaders do |
| 165 | local loader = loaders[i] | 132 | local loader = loaders[i] |
| @@ -170,17 +137,7 @@ insert_loader = function(pos) | |||
| 170 | insert(loaders, pos, yue_loader) | 137 | insert(loaders, pos, yue_loader) |
| 171 | return true | 138 | return true |
| 172 | end | 139 | end |
| 173 | remove_loader = function() | 140 | local function yue_require(name) |
| 174 | local loaders = package.loaders or package.searchers | ||
| 175 | for i, loader in ipairs(loaders) do | ||
| 176 | if loader == yue_loader then | ||
| 177 | remove(loaders, i) | ||
| 178 | return true | ||
| 179 | end | ||
| 180 | end | ||
| 181 | return false | ||
| 182 | end | ||
| 183 | yue_require = function(name) | ||
| 184 | insert_loader() | 141 | insert_loader() |
| 185 | local success, res = xpcall((function() | 142 | local success, res = xpcall((function() |
| 186 | return require(name) | 143 | return require(name) |
| @@ -195,6 +152,8 @@ yue_require = function(name) | |||
| 195 | return nil | 152 | return nil |
| 196 | end | 153 | end |
| 197 | end | 154 | end |
| 155 | local load_stacktraceplus = yue.load_stacktraceplus | ||
| 156 | yue.load_stacktraceplus = nil | ||
| 198 | setmetatable(yue, { | 157 | setmetatable(yue, { |
| 199 | __index = function(self, key) | 158 | __index = function(self, key) |
| 200 | if not (key == "stp") then | 159 | if not (key == "stp") then |
| @@ -202,11 +161,10 @@ setmetatable(yue, { | |||
| 202 | end | 161 | end |
| 203 | local stp = rawget(yue, "stp") | 162 | local stp = rawget(yue, "stp") |
| 204 | if not stp then | 163 | if not stp then |
| 205 | stp = yue.load_stacktraceplus() | 164 | stp = load_stacktraceplus() |
| 206 | stp.dump_locals = false | 165 | stp.dump_locals = false |
| 207 | stp.simplified = true | 166 | stp.simplified = true |
| 208 | rawset(yue, "stp", stp) | 167 | yue.stp = stp |
| 209 | rawset(yue, "load_stacktraceplus", nil) | ||
| 210 | end | 168 | end |
| 211 | return stp | 169 | return stp |
| 212 | end, | 170 | end, |
| @@ -248,13 +206,9 @@ local function p(...) | |||
| 248 | print(concat(args)) | 206 | print(concat(args)) |
| 249 | end | 207 | end |
| 250 | yue.insert_loader = insert_loader | 208 | yue.insert_loader = insert_loader |
| 251 | yue.remove_loader = remove_loader | 209 | yue.dofile = yue_dofile |
| 252 | yue.loader = yue_loader | 210 | yue.loadfile = yue_loadfile |
| 253 | yue.dofile = dofile | 211 | yue.loadstring = yue_loadstring |
| 254 | yue.loadfile = loadfile | ||
| 255 | yue.loadstring = loadstring | ||
| 256 | yue.create_yuepath = create_yuepath | ||
| 257 | yue.find_modulepath = find_modulepath | ||
| 258 | yue.pcall = yue_call | 212 | yue.pcall = yue_call |
| 259 | yue.require = yue_require | 213 | yue.require = yue_require |
| 260 | yue.p = p | 214 | yue.p = p |
