mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-07-26 20:12:45 +00:00
Changes the Console to expose global functions to scripts instead of properties.
Fixes #2140 Refs #1886 This was SVN commit r13884.
This commit is contained in:
@@ -41,6 +41,7 @@
|
||||
#include "ps/ProfileViewer.h"
|
||||
#include "ps/Pyrogenesis.h"
|
||||
#include "ps/SavedGame.h"
|
||||
#include "ps/scripting/JSInterface_Console.h"
|
||||
#include "ps/UserReport.h"
|
||||
#include "ps/GameSetup/Atlas.h"
|
||||
#include "ps/GameSetup/Config.h"
|
||||
@@ -654,6 +655,7 @@ void GuiScriptingInit(ScriptInterface& scriptInterface)
|
||||
{
|
||||
JSI_GameView::RegisterScriptFunctions(scriptInterface);
|
||||
JSI_Renderer::RegisterScriptFunctions(scriptInterface);
|
||||
JSI_Console::RegisterScriptFunctions(scriptInterface);
|
||||
|
||||
// GUI manager functions:
|
||||
scriptInterface.RegisterFunction<CScriptVal, &GetActiveGui>("GetActiveGui");
|
||||
|
||||
@@ -321,9 +321,6 @@ static void RegisterJavascriptInterfaces()
|
||||
// maths
|
||||
JSI_Vector3D::init();
|
||||
|
||||
// ps
|
||||
JSI_Console::init();
|
||||
|
||||
// GUI
|
||||
CGUI::ScriptingInit();
|
||||
|
||||
|
||||
@@ -15,110 +15,47 @@
|
||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// JavaScript interface to native code selection and group objects
|
||||
|
||||
#include "precompiled.h"
|
||||
|
||||
#include "scriptinterface/ScriptInterface.h"
|
||||
#include "JSInterface_Console.h"
|
||||
#include "ps/CConsole.h"
|
||||
#include "scripting/JSConversions.h"
|
||||
#include "ps/CLogger.h"
|
||||
|
||||
JSClass JSI_Console::JSI_class =
|
||||
{
|
||||
"Console", 0,
|
||||
JS_PropertyStub, JS_PropertyStub,
|
||||
JSI_Console::getProperty, JSI_Console::setProperty,
|
||||
JS_EnumerateStub, JS_ResolveStub,
|
||||
JS_ConvertStub, JS_FinalizeStub,
|
||||
NULL, NULL, NULL, NULL
|
||||
};
|
||||
|
||||
JSPropertySpec JSI_Console::JSI_props[] =
|
||||
{
|
||||
{ "visible", JSI_Console::console_visible, JSPROP_ENUMERATE },
|
||||
{ 0 }
|
||||
};
|
||||
|
||||
JSFunctionSpec JSI_Console::JSI_methods[] =
|
||||
{
|
||||
{ "write", JSI_Console::writeConsole, 1, 0 },
|
||||
{ 0 },
|
||||
};
|
||||
|
||||
JSBool JSI_Console::getProperty(JSContext* UNUSED(cx), JSObject* UNUSED(obj), jsid id, jsval* vp)
|
||||
{
|
||||
if (!JSID_IS_INT(id))
|
||||
return JS_TRUE;
|
||||
|
||||
int i = JSID_TO_INT(id);
|
||||
|
||||
switch (i)
|
||||
bool JSI_Console::CheckGlobalInitialized()
|
||||
{
|
||||
if (!g_Console)
|
||||
{
|
||||
case console_visible:
|
||||
*vp = BOOLEAN_TO_JSVAL(g_Console->IsActive());
|
||||
return JS_TRUE;
|
||||
default:
|
||||
*vp = JSVAL_NULL;
|
||||
return JS_TRUE;
|
||||
LOGERROR(L"Trying to access the console when it's not initialized!");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
JSBool JSI_Console::setProperty(JSContext* UNUSED(cx), JSObject* UNUSED(obj), jsid id, JSBool UNUSED(strict), jsval* vp)
|
||||
bool JSI_Console::GetVisibleEnabled(void* UNUSED(cbdata))
|
||||
{
|
||||
if (!JSID_IS_INT(id))
|
||||
return JS_TRUE;
|
||||
|
||||
int i = JSID_TO_INT(id);
|
||||
|
||||
switch (i)
|
||||
{
|
||||
case console_visible:
|
||||
try
|
||||
{
|
||||
g_Console->SetVisible(ToPrimitive<bool> (*vp));
|
||||
return JS_TRUE;
|
||||
}
|
||||
catch (PSERROR_Scripting_ConversionFailed&)
|
||||
{
|
||||
return JS_TRUE;
|
||||
}
|
||||
default:
|
||||
return JS_TRUE;
|
||||
}
|
||||
if (!CheckGlobalInitialized())
|
||||
return false;
|
||||
return g_Console->IsActive();
|
||||
}
|
||||
|
||||
void JSI_Console::init()
|
||||
void JSI_Console::SetVisibleEnabled(void* UNUSED(cbdata), bool Enabled)
|
||||
{
|
||||
g_ScriptingHost.DefineCustomObjectType(&JSI_class, NULL, 0, JSI_props, JSI_methods, NULL, NULL);
|
||||
if (!CheckGlobalInitialized())
|
||||
return;
|
||||
g_Console->SetVisible(Enabled);
|
||||
}
|
||||
|
||||
JSBool JSI_Console::getConsole(JSContext* cx, JSObject* UNUSED(obj), jsid UNUSED(id), jsval* vp)
|
||||
void JSI_Console::Write(void* UNUSED(cbdata), std::wstring output)
|
||||
{
|
||||
JSObject* console = JS_NewObject(cx, &JSI_Console::JSI_class, NULL, NULL);
|
||||
*vp = OBJECT_TO_JSVAL(console);
|
||||
return JS_TRUE;
|
||||
if (!CheckGlobalInitialized())
|
||||
return;
|
||||
g_Console->InsertMessage(L"%ls", output.c_str());
|
||||
}
|
||||
|
||||
JSBool JSI_Console::writeConsole(JSContext* cx, uintN argc, jsval* vp)
|
||||
void JSI_Console::RegisterScriptFunctions(ScriptInterface& scriptInterface)
|
||||
{
|
||||
UNUSED2(cx);
|
||||
|
||||
CStrW output;
|
||||
for (uintN i = 0; i < argc; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
CStrW arg = g_ScriptingHost.ValueToUCString(JS_ARGV(cx, vp)[i]);
|
||||
output += arg;
|
||||
}
|
||||
catch (PSERROR_Scripting_ConversionFailed&)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: What if the console has been destroyed already?
|
||||
if (g_Console)
|
||||
g_Console->InsertMessage(L"%ls", output.c_str());
|
||||
|
||||
JS_SET_RVAL(cx, vp, JSVAL_VOID);
|
||||
return JS_TRUE;
|
||||
scriptInterface.RegisterFunction<bool, &JSI_Console::GetVisibleEnabled>("Console_GetVisibleEnabled");
|
||||
scriptInterface.RegisterFunction<void, bool, &JSI_Console::SetVisibleEnabled>("Console_SetVisibleEnabled");
|
||||
scriptInterface.RegisterFunction<void, std::wstring, &JSI_Console::Write>("Console_Write");
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2009 Wildfire Games.
|
||||
/* Copyright (C) 2013 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
@@ -15,33 +15,19 @@
|
||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// JSInterface_Console.h
|
||||
//
|
||||
// The JavaScript wrapper around the console system
|
||||
|
||||
#include "scripting/ScriptingHost.h"
|
||||
|
||||
#ifndef INCLUDED_JSI_CONSOLE
|
||||
#define INCLUDED_JSI_CONSOLE
|
||||
|
||||
class ScriptInterface;
|
||||
|
||||
namespace JSI_Console
|
||||
{
|
||||
enum
|
||||
{
|
||||
console_visible
|
||||
};
|
||||
extern JSClass JSI_class;
|
||||
extern JSPropertySpec JSI_props[];
|
||||
extern JSFunctionSpec JSI_methods[];
|
||||
|
||||
JSBool getProperty(JSContext* cx, JSObject* obj, jsid id, jsval* vp);
|
||||
JSBool setProperty(JSContext* cx, JSObject* obj, jsid id, JSBool strict, jsval* vp);
|
||||
|
||||
JSBool getConsole(JSContext* context, JSObject* obj, jsid id, jsval* vp);
|
||||
|
||||
void init();
|
||||
|
||||
JSBool writeConsole(JSContext* cx, uintN argc, jsval* vp);
|
||||
bool CheckGlobalInitialized();
|
||||
bool GetVisibleEnabled(void* cbdata);
|
||||
void SetVisibleEnabled(void* cbdata, bool Enabled);
|
||||
void Write(void* cbdata, std::wstring output);
|
||||
|
||||
void RegisterScriptFunctions(ScriptInterface& scriptInterface);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -402,26 +402,3 @@ JSFunctionSpec ScriptFunctionTable[] =
|
||||
{0}
|
||||
};
|
||||
#undef JS_FUNC
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// property accessors
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
enum ScriptGlobalTinyIDs
|
||||
{
|
||||
GLOBAL_SELECTION,
|
||||
GLOBAL_GROUPSARRAY,
|
||||
GLOBAL_CAMERA,
|
||||
GLOBAL_CONSOLE,
|
||||
GLOBAL_LIGHTENV
|
||||
};
|
||||
|
||||
JSPropertySpec ScriptGlobalTable[] =
|
||||
{
|
||||
{ "console" , GLOBAL_CONSOLE, JSPROP_PERMANENT|JSPROP_READONLY, JSI_Console::getConsole, 0 },
|
||||
|
||||
// end of table marker
|
||||
{ 0, 0, 0, 0, 0 },
|
||||
};
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
|
||||
// referenced by ScriptingHost.cpp
|
||||
extern JSFunctionSpec ScriptFunctionTable[];
|
||||
extern JSPropertySpec ScriptGlobalTable[];
|
||||
|
||||
// dependencies (moved to header to avoid L4 warnings)
|
||||
// .. from main.cpp:
|
||||
|
||||
@@ -37,9 +37,6 @@ ScriptingHost::ScriptingHost()
|
||||
|
||||
if (!JS_DefineFunctions(m_Context, m_GlobalObject, ScriptFunctionTable))
|
||||
throw PSERROR_Scripting_SetupFailed();
|
||||
|
||||
if (!JS_DefineProperties(m_Context, m_GlobalObject, ScriptGlobalTable))
|
||||
throw PSERROR_Scripting_SetupFailed();
|
||||
}
|
||||
|
||||
ScriptingHost::~ScriptingHost()
|
||||
|
||||
Reference in New Issue
Block a user