mirror of https://github.com/OpenTTD/OpenTTD
Codechange: use std::string_view for SQ stack and function info
parent
0bc773215e
commit
d464961c41
|
@ -164,9 +164,9 @@ typedef struct tagSQObject
|
||||||
}SQObject;
|
}SQObject;
|
||||||
|
|
||||||
typedef struct tagSQStackInfos{
|
typedef struct tagSQStackInfos{
|
||||||
const SQChar* funcname;
|
std::string_view funcname;
|
||||||
const SQChar* source;
|
std::string_view source;
|
||||||
SQInteger line;
|
SQInteger line = -1;
|
||||||
}SQStackInfos;
|
}SQStackInfos;
|
||||||
|
|
||||||
typedef struct SQVM* HSQUIRRELVM;
|
typedef struct SQVM* HSQUIRRELVM;
|
||||||
|
@ -190,8 +190,8 @@ typedef struct tagSQRegFunction{
|
||||||
|
|
||||||
typedef struct tagSQFunctionInfo {
|
typedef struct tagSQFunctionInfo {
|
||||||
SQUserPointer funcid;
|
SQUserPointer funcid;
|
||||||
const SQChar *name;
|
std::string_view name;
|
||||||
const SQChar *source;
|
std::string_view source;
|
||||||
}SQFunctionInfo;
|
}SQFunctionInfo;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -21,21 +21,17 @@ void sqstd_printcallstack(HSQUIRRELVM v)
|
||||||
pf(v,"\nCALLSTACK\n");
|
pf(v,"\nCALLSTACK\n");
|
||||||
while(SQ_SUCCEEDED(sq_stackinfos(v,level,&si)))
|
while(SQ_SUCCEEDED(sq_stackinfos(v,level,&si)))
|
||||||
{
|
{
|
||||||
const SQChar *fn="unknown";
|
std::string_view fn="unknown";
|
||||||
const SQChar *src="unknown";
|
std::string_view src="unknown";
|
||||||
if(si.funcname)fn=si.funcname;
|
if(!si.funcname.empty())fn=si.funcname;
|
||||||
if(si.source) {
|
if(!si.source.empty()) {
|
||||||
/* We don't want to bother users with absolute paths to all AI files.
|
/* We don't want to bother users with absolute paths to all AI files.
|
||||||
* Since the path only reaches NoAI code in a formatted string we have
|
* Since the path only reaches NoAI code in a formatted string we have
|
||||||
* to strip it here. Let's hope nobody installs openttd in a subdirectory
|
* to strip it here. Let's hope nobody installs openttd in a subdirectory
|
||||||
* of a directory named /ai/. */
|
* of a directory named /ai/. */
|
||||||
src = strstr(si.source, "\\ai\\");
|
auto p = si.source.find("\\ai\\");
|
||||||
if (!src) src = strstr(si.source, "/ai/");
|
if (p == std::string_view::npos) p = si.source.find("/ai/");
|
||||||
if (src) {
|
src = (p == std::string_view::npos) ? si.source : si.source.substr(p + 4);
|
||||||
src += 4;
|
|
||||||
} else {
|
|
||||||
src = si.source;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pf(v,fmt::format("*FUNCTION [{}()] {} line [{}]\n",fn,src,si.line));
|
pf(v,fmt::format("*FUNCTION [{}()] {} line [{}]\n",fn,src,si.line));
|
||||||
level++;
|
level++;
|
||||||
|
|
|
@ -37,7 +37,7 @@ SQRESULT sq_stackinfos(HSQUIRRELVM v, SQInteger level, SQStackInfos *si)
|
||||||
{
|
{
|
||||||
SQInteger cssize = v->_callsstacksize;
|
SQInteger cssize = v->_callsstacksize;
|
||||||
if (cssize > level) {
|
if (cssize > level) {
|
||||||
memset(si, 0, sizeof(SQStackInfos));
|
*si = {};
|
||||||
SQVM::CallInfo &ci = v->_callsstack[cssize-level-1];
|
SQVM::CallInfo &ci = v->_callsstack[cssize-level-1];
|
||||||
switch (type(ci._closure)) {
|
switch (type(ci._closure)) {
|
||||||
case OT_CLOSURE:{
|
case OT_CLOSURE:{
|
||||||
|
|
|
@ -48,13 +48,9 @@ SQInteger SquirrelStd::require(HSQUIRRELVM vm)
|
||||||
/* Get the script-name of the current file, so we can work relative from it */
|
/* Get the script-name of the current file, so we can work relative from it */
|
||||||
SQStackInfos si;
|
SQStackInfos si;
|
||||||
sq_stackinfos(vm, 1, &si);
|
sq_stackinfos(vm, 1, &si);
|
||||||
if (si.source == nullptr) {
|
|
||||||
Debug(misc, 0, "[squirrel] Couldn't detect the script-name of the 'require'-caller; this should never happen!");
|
|
||||||
return SQ_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Keep the dir, remove the rest */
|
/* Keep the dir, remove the rest */
|
||||||
std::string path = si.source;
|
std::string path{si.source};
|
||||||
auto p = path.find_last_of(PATHSEPCHAR);
|
auto p = path.find_last_of(PATHSEPCHAR);
|
||||||
/* Keep the PATHSEPCHAR there, remove the rest */
|
/* Keep the PATHSEPCHAR there, remove the rest */
|
||||||
if (p != std::string::npos) path.erase(p + 1);
|
if (p != std::string::npos) path.erase(p + 1);
|
||||||
|
|
Loading…
Reference in New Issue