Pyrogenesis  trunk
ScriptExceptions.h
Go to the documentation of this file.
1 /* Copyright (C) 2020 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_SCRIPTEXCEPTIONS
19 #define INCLUDED_SCRIPTEXCEPTIONS
20 
21 struct JSContext;
22 class JSErrorReport;
23 class ScriptRequest;
24 
25 namespace ScriptException
26 {
27 /**
28  * @return Whether there is a JS exception pending.
29  */
30 bool IsPending(const ScriptRequest& rq);
31 
32 /**
33  * Log and then clear the current pending exception. This function should always be called after calling a
34  * JS script (or anything that can throw JS errors, such as structured clones),
35  * in case that script doesn't catch an exception thrown during its execution.
36  * If no exception is pending, this does nothing.
37  * Note that JS code that wants to throw errors should throw new Error(...), otherwise the stack cannot be used.
38  * @return Whether there was a pending exception.
39  */
40 bool CatchPending(const ScriptRequest& rq);
41 
42 /**
43  * Raise a JS exception from C++ code.
44  * This is only really relevant in JSNative functions that don't use ObjectOpResult,
45  * as the latter overwrites the pending exception.
46  * Prefer either simply logging an error if you know a stack-trace will be raised elsewhere.
47  */
48 void Raise(const ScriptRequest& rq, const char* format, ...);
49 }
50 
51 #endif // INCLUDED_SCRIPTEXCEPTIONS
Definition: ScriptExceptions.h:25
bool IsPending(const ScriptRequest &rq)
Definition: ScriptExceptions.cpp:28
void Raise(const ScriptRequest &rq, const char *format,...)
Raise a JS exception from C++ code.
Definition: ScriptExceptions.cpp:95
bool CatchPending(const ScriptRequest &rq)
Log and then clear the current pending exception.
Definition: ScriptExceptions.cpp:33
Spidermonkey maintains some &#39;local&#39; state via the JSContext* object.
Definition: ScriptRequest.h:59