1
0
forked from mirrors/0ad

Split off JSON-related function from ScriptInterface, clean up headers.

Follows 34b1920e7b.

JSON functions and ToString are movec to their own headers.
Also clean out a few PersistentRooted usage to use the 2-phase init to
clean up scriptInterface usage.

With these functions split off, we can finally clean out headers and
remove ScriptInterface.h from most of them, in favour of smaller and
more precise headers.


Take the opportunity to clarify some comments regarding Mutability.

Differential Revision: https://code.wildfiregames.com/D3961
This was SVN commit r25434.
This commit is contained in:
wraitii
2021-05-14 10:18:03 +00:00
parent f368e1a69e
commit 4f972bc623
57 changed files with 396 additions and 280 deletions
+4 -28
View File
@@ -18,8 +18,6 @@
#ifndef INCLUDED_SCRIPTINTERFACE
#define INCLUDED_SCRIPTINTERFACE
#include "lib/file/vfs/vfs_path.h"
#include "maths/Fixed.h"
#include "ps/Errors.h"
#include "scriptinterface/ScriptConversions.h"
#include "scriptinterface/ScriptExceptions.h"
@@ -55,10 +53,13 @@ struct ScriptInterface_impl;
class ScriptContext;
// Using a global object for the context is a workaround until Simulation, AI, etc,
// use their own threads and also their own contexts.
extern thread_local shared_ptr<ScriptContext> g_ScriptContext;
extern thread_local std::shared_ptr<ScriptContext> g_ScriptContext;
namespace boost { namespace random { class rand48; } }
class Path;
using VfsPath = Path;
/**
* Abstraction around a SpiderMonkey JS::Realm.
*
@@ -145,31 +146,6 @@ public:
bool SetPrototype(JS::HandleValue obj, JS::HandleValue proto);
/**
* Convert an object to a UTF-8 encoded string, either with JSON
* (if pretty == true and there is no JSON error) or with toSource().
*
* We have to use a mutable handle because JS_Stringify requires that for unknown reasons.
*/
std::string ToString(JS::MutableHandleValue obj, bool pretty = false) const;
/**
* Parse a UTF-8-encoded JSON string. Returns the unmodified value on error
* and prints an error message.
* @return true on success; false otherwise
*/
bool ParseJSON(const std::string& string_utf8, JS::MutableHandleValue out) const;
/**
* Read a JSON file. Returns the unmodified value on error and prints an error message.
*/
void ReadJSONFile(const VfsPath& path, JS::MutableHandleValue out) const;
/**
* Stringify to a JSON string, UTF-8 encoded. Returns an empty string on error.
*/
std::string StringifyJSON(JS::MutableHandleValue obj, bool indent = true) const;
/**
* Load and execute the given script in a new function scope.
* @param filename Name for debugging purposes (not used to load the file)