diff --git a/source/gui/scripting/ScriptFunctions.cpp b/source/gui/scripting/ScriptFunctions.cpp index c4b30f70eb..5404c808a5 100644 --- a/source/gui/scripting/ScriptFunctions.cpp +++ b/source/gui/scripting/ScriptFunctions.cpp @@ -904,31 +904,6 @@ std::wstring GetBuildTimestamp(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), i return wstring_from_utf8(buf); } -JS::Value ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath) -{ - JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); - JSAutoRequest rq(cx); - JS::RootedValue out(cx); - pCxPrivate->pScriptInterface->ReadJSONFile(filePath, &out); - return out; -} - -void WriteJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath, JS::HandleValue val1) -{ - JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); - JSAutoRequest rq(cx); - - // TODO: This is a workaround because we need to pass a MutableHandle to StringifyJSON. - JS::RootedValue val(cx, val1); - - std::string str(pCxPrivate->pScriptInterface->StringifyJSON(&val, false)); - - VfsPath path(filePath); - WriteBuffer buf; - buf.Append(str.c_str(), str.length()); - g_VFS->CreateFile(path, buf.Data(), buf.Size()); -} - bool TemplateExists(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& templateName) { return g_GUI->TemplateExists(templateName); @@ -1053,8 +1028,6 @@ void GuiScriptingInit(ScriptInterface& scriptInterface) scriptInterface.RegisterFunction("SetPaused"); scriptInterface.RegisterFunction("GetFPS"); scriptInterface.RegisterFunction("GetBuildTimestamp"); - scriptInterface.RegisterFunction("ReadJSONFile"); - scriptInterface.RegisterFunction("WriteJSONFile"); scriptInterface.RegisterFunction("TemplateExists"); scriptInterface.RegisterFunction("GetTemplate"); scriptInterface.RegisterFunction("GetTextWidth"); diff --git a/source/ps/scripting/JSInterface_VFS.cpp b/source/ps/scripting/JSInterface_VFS.cpp index 05d7ee3751..f7b33d89bf 100644 --- a/source/ps/scripting/JSInterface_VFS.cpp +++ b/source/ps/scripting/JSInterface_VFS.cpp @@ -178,6 +178,31 @@ JS::Value JSI_VFS::ReadFileLines(ScriptInterface::CxPrivate* pCxPrivate, const s return JS::ObjectValue(*line_array); } +JS::Value JSI_VFS::ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath) +{ + JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); + JSAutoRequest rq(cx); + JS::RootedValue out(cx); + pCxPrivate->pScriptInterface->ReadJSONFile(filePath, &out); + return out; +} + +void JSI_VFS::WriteJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath, JS::HandleValue val1) +{ + JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); + JSAutoRequest rq(cx); + + // TODO: This is a workaround because we need to pass a MutableHandle to StringifyJSON. + JS::RootedValue val(cx, val1); + + std::string str(pCxPrivate->pScriptInterface->StringifyJSON(&val, false)); + + VfsPath path(filePath); + WriteBuffer buf; + buf.Append(str.c_str(), str.length()); + g_VFS->CreateFile(path, buf.Data(), buf.Size()); +} + void JSI_VFS::RegisterScriptFunctions(const ScriptInterface& scriptInterface) { scriptInterface.RegisterFunction("BuildDirEntList"); @@ -186,4 +211,6 @@ void JSI_VFS::RegisterScriptFunctions(const ScriptInterface& scriptInterface) scriptInterface.RegisterFunction("GetFileSize"); scriptInterface.RegisterFunction("ReadFile"); scriptInterface.RegisterFunction("ReadFileLines"); + scriptInterface.RegisterFunction("ReadJSONFile"); + scriptInterface.RegisterFunction("WriteJSONFile"); } diff --git a/source/ps/scripting/JSInterface_VFS.h b/source/ps/scripting/JSInterface_VFS.h index 977f502517..f50ff81679 100644 --- a/source/ps/scripting/JSInterface_VFS.h +++ b/source/ps/scripting/JSInterface_VFS.h @@ -55,6 +55,12 @@ namespace JSI_VFS // Return file contents as an array of lines. JS::Value ReadFileLines(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename); + // Return file contents parsed as a JS Object + JS::Value ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath); + + // Save given JS Object to a JSON file + void WriteJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath, JS::HandleValue val1); + void RegisterScriptFunctions(const ScriptInterface& scriptInterface); }