From 63878b93b0f142af74b397a02b2c80be039b03ec Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 23 Feb 2022 00:22:07 +0800 Subject: add yue.traceback. --- src/yuescript/yue_compiler.cpp | 2 +- src/yuescript/yuescript.h | 42 ++++++++++++++++++++---------------------- 2 files changed, 21 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index d1e4d24..274027b 100755 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -60,7 +60,7 @@ using namespace parserlib; typedef std::list str_list; -const std::string_view version = "0.10.2"sv; +const std::string_view version = "0.10.3"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { diff --git a/src/yuescript/yuescript.h b/src/yuescript/yuescript.h index eac95da..769eb8a 100644 --- a/src/yuescript/yuescript.h +++ b/src/yuescript/yuescript.h @@ -1,6 +1,6 @@ R"yuescript_codes( --[[ -Copyright (C) 2020 by Leaf Corcoran, modified by Li Jin, 2021 +Copyright (C) 2020 by Leaf Corcoran, modified by Li Jin, 2022 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -97,7 +97,7 @@ local function yue_call(f, ...) return xpcall((function() return f(unpack(args)) end), function(err) - return yue.stp.stacktrace(err, 1) + return yue.traceback(err, 1) end) end yue_loadstring = function(...) @@ -138,37 +138,34 @@ local function insert_loader(pos) insert(loaders, pos, yue_loader) return true end +yue.options.dump_locals = false +yue.options.simplified = true +local load_stacktraceplus = yue.load_stacktraceplus +yue.load_stacktraceplus = nil +local stp +local function yue_traceback(err, level) + if not stp then + stp = load_stacktraceplus() + end + stp.dump_locals = yue.options.dump_locals + stp.simplified = yue.options.simplified + return stp.stacktrace(err, level) +end local function yue_require(name) insert_loader() - local success, res = xpcall((function() + local success, res = xpcall(function() return require(name) - end), function(err) - local msg = yue.stp.stacktrace(err, 1) - print(msg) - return msg + end, function(err) + return yue_traceback(err, 2) end) if success then return res else + print(res) return nil end end -local load_stacktraceplus = yue.load_stacktraceplus -yue.load_stacktraceplus = nil setmetatable(yue, { - __index = function(self, key) - if not (key == "stp") then - return nil - end - local stp = rawget(yue, "stp") - if not stp then - stp = load_stacktraceplus() - stp.dump_locals = false - stp.simplified = true - yue.stp = stp - end - return stp - end, __call = function(self, name) return self.require(name) end @@ -214,4 +211,5 @@ yue.loadstring = yue_loadstring yue.pcall = yue_call yue.require = yue_require yue.p = p +yue.traceback = yue_traceback )yuescript_codes"; -- cgit v1.2.3-55-g6feb