mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-06-22 11:25:45 +00:00
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
/* Copyright (C) 2026 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 0 A.D. is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include "precompiled.h"
|
||||
|
||||
#include "Exceptions.h"
|
||||
|
||||
#include "lib/code_annotation.h"
|
||||
#include "lib/debug.h"
|
||||
#include "lib/secure_crt.h"
|
||||
#include "ps/CLogger.h"
|
||||
#include "ps/CStr.h"
|
||||
#include "scriptinterface/FunctionWrapper.h"
|
||||
#include "scriptinterface/Conversions.h"
|
||||
#include "scriptinterface/Request.h"
|
||||
|
||||
#include <cstdarg>
|
||||
#include <js/CharacterEncoding.h>
|
||||
#include <js/ErrorReport.h>
|
||||
#include <js/Exception.h>
|
||||
#include <js/RootingAPI.h>
|
||||
#include <js/TypeDecls.h>
|
||||
#include <js/Value.h>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
namespace Script {
|
||||
|
||||
bool Exception::IsPending(const Request& rq)
|
||||
{
|
||||
return JS_IsExceptionPending(rq.cx);
|
||||
}
|
||||
|
||||
bool Exception::CatchPending(const Request& rq)
|
||||
{
|
||||
if (!JS_IsExceptionPending(rq.cx))
|
||||
return false;
|
||||
|
||||
JS::RootedValue excn(rq.cx);
|
||||
ENSURE(JS_GetPendingException(rq.cx, &excn));
|
||||
JS_ClearPendingException(rq.cx);
|
||||
|
||||
if (excn.isUndefined())
|
||||
{
|
||||
LOGERROR("JavaScript error: (undefined)");
|
||||
return true;
|
||||
}
|
||||
|
||||
// As of SM45/52, there is no way to recover a stack in case the thrown thing is not an Error object.
|
||||
if (!excn.isObject())
|
||||
{
|
||||
CStr error;
|
||||
FromJSVal(rq, excn, error);
|
||||
LOGERROR("JavaScript error: %s", error);
|
||||
return true;
|
||||
}
|
||||
|
||||
JS::RootedObject excnObj(rq.cx, &excn.toObject());
|
||||
JSErrorReport* report = JS_ErrorFromException(rq.cx, excnObj);
|
||||
|
||||
if (!report)
|
||||
{
|
||||
CStr error;
|
||||
FromJSVal(rq, excn, error);
|
||||
LOGERROR("JavaScript error: %s", error);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::stringstream msg;
|
||||
msg << "JavaScript error: ";
|
||||
if (report->filename)
|
||||
{
|
||||
msg << report->filename.c_str();
|
||||
msg << " line " << report->lineno << "\n";
|
||||
}
|
||||
|
||||
msg << report->message().c_str();
|
||||
|
||||
JS::RootedObject stackObj(rq.cx, ExceptionStackOrNull(excnObj));
|
||||
JS::RootedValue stackVal(rq.cx, JS::ObjectOrNullValue(stackObj));
|
||||
|
||||
if (!stackVal.isNull())
|
||||
{
|
||||
std::string stackText;
|
||||
Script::Function::Call(rq, stackVal, "toString", stackText);
|
||||
|
||||
std::istringstream stream(stackText);
|
||||
for (std::string line; std::getline(stream, line);)
|
||||
msg << "\n " << line;
|
||||
}
|
||||
|
||||
LOGERROR("%s", msg.str().c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
void Exception::Raise(const Request& rq, const char* format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
// SM is single-threaded, so a static thread_local buffer needs no locking.
|
||||
thread_local static char buffer[256];
|
||||
vsprintf_s(buffer, ARRAY_SIZE(buffer), format, ap);
|
||||
va_end(ap);
|
||||
// Rather annoyingly, there are no va_list versions of this function, hence the preformatting above.
|
||||
JS_ReportErrorUTF8(rq.cx, "%s", buffer);
|
||||
}
|
||||
|
||||
} // namespace Script
|
||||
Reference in New Issue
Block a user