Skip to content
This repository was archived by the owner on Feb 20, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/include/macrosmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DLLIMPORT MacrosManager : public Mgr<MacrosManager>
friend class Mgr<MacrosManager>;
void CreateMenu(wxMenuBar* menuBar);
void ReleaseMenu(wxMenuBar* menuBar);
void ReplaceMacros(wxString& buffer, ProjectBuildTarget* target = nullptr, bool subrequest = false);
void ReplaceMacros(wxString& buffer, ProjectBuildTarget* target = nullptr, bool subrequest = false,wxString name = wxEmptyString);
wxString ReplaceMacros(const wxString& buffer, ProjectBuildTarget* target = nullptr);
void ReplaceEnvVars(wxString& buffer) { ReplaceMacros(buffer); } /* misnomer, should be ReplaceVariables */;
void RecalcVars(cbProject* project, EditorBase* editor, ProjectBuildTarget* target);
Expand Down
110 changes: 98 additions & 12 deletions src/include/scriptingmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,30 @@
#ifndef CB_PRECOMP
#include "cbexception.h" // cbThrow
#include "globals.h" // cbC2U
#include <wx/timer.h>
#endif

#include "settings.h"
#include "manager.h"
#include "menuitemsmanager.h"
#include <wx/intl.h>
#include <scripting/bindings/sc_cb_vm.h>
#include <scripting/sqrat/sqratUtil.h>
#include <scripting/bindings/sc_plugin.h>
#include "scripting/squirrel/sqrdbg.h"
#include "scripting/squirrel/sqdbgserver.h"

struct SquirrelError;
#define SCRIPT_BINDING_VERSION_MAJOR 2
#define SCRIPT_BINDING_VERSION_MINOR 0
#define SCRIPT_BINDING_VERSION_RELEASE 0

namespace ScriptBindings
{
class cbScriptPlugin;
}


void PrintSquirrelToWxString(wxString &msg,const SQChar *s, va_list& vl);

/** @brief Provides scripting in Code::Blocks.
*
Expand All @@ -31,6 +47,7 @@ struct SquirrelError;
* Manager::Get()->GetScriptingManager()->LoadScript(_T("some.script"));
* @endcode
*
* The following is outdated ;) soon:
* And here's an example to call a script function:
*
* @code
Expand All @@ -47,6 +64,9 @@ struct SquirrelError;
* The templated type denotes the function's return type. Also note that the
* function name is not unicode (we 're not using Squirrel in unicode mode).
*/

using namespace ScriptBindings;

class DLLIMPORT ScriptingManager : public Mgr<ScriptingManager>, public wxEvtHandler
{
friend class Mgr<ScriptingManager>;
Expand Down Expand Up @@ -74,39 +94,54 @@ class DLLIMPORT ScriptingManager : public Mgr<ScriptingManager>, public wxEvtHan
*
* @param buffer The script buffer to compile and run.
* @param debugName A debug name. This will appear in any errors displayed.
* @param real_path If the debug name is not a path to a real file this has to be set on false. (This is needed to create a temporary file to allow the debugger to open also memory files)
* @return True if the script compiled, false if not.
*/
bool LoadBuffer(const wxString& buffer, const wxString& debugName = _T("CommandLine"));
bool LoadBuffer(const wxString& buffer,wxString debugName/* = _T("CommandLine")*/,bool real_path = false);

/** @brief Loads a string buffer and captures its output.
*
* @param buffer The script buffer to compile and run.
* @return The script's output (if any).
*/
wxString LoadBufferRedirectOutput(const wxString& buffer);
wxString LoadBufferRedirectOutput(const wxString& buffer,const wxString& name);

// TODO (bluehazzard#1#): Delete this if the Sqrat API doesn't change
/** @brief Returns an accumulated error string.
*
* Returns an error string for the passed exception (if any) plus
* Returns an error string for the passed vm (if any) plus
* any accumulated script engine errors (e.g. from failed function calls).
* @param exception A pointer to the exception object containing the error. Can be NULL (default).
* @param The vm from which the error should come
* @param clearErrors If true (default), when this function returns all
* accumulated error messages are cleared.
* @return The error string. If empty, it means "no errors".
*/
wxString GetErrorString(SquirrelError* exception = nullptr, bool clearErrors = true);
//wxString GetErrorString(Sqrat::Exception* exception = nullptr, bool clearErrors = true);
wxString GetErrorString(bool clearErrors = true);

/** @brief Display error dialog.
*
* Displays an error dialog containing exception info and any other
* script errors. If error_msg isempty the GetErrorString String is called
* internally and if no error is found no error is displayed and the function returns _false_
* @param error_msg The error message displayed with the error dialog
* @param clearErrors If true (default), when this function returns all
* accumulated error messages are cleared.
* @return _true_ if a error occurred, _false_ otherwise
*/
bool DisplayErrors(wxString error_msg = wxEmptyString, bool clearErrors = true);

/** @brief Display error dialog.
*
* Displays an error dialog containing exception info and any other
* script errors. Calls GetErrorString() internally.
* You should normally call this function inside your catch handler for
* SquirrelFunction<>() calls.
* @param exception A pointer to the exception object containing the error. Can be NULL (default).
* script errors. If error_msg isempty the GetErrorString String is called
* internally and if no error is found no error is displayed and the function returns _false_
* @param pre_error Text printed previous the error
* @param clearErrors If true (default), when this function returns all
* accumulated error messages are cleared.
* @return _true_ if a error occurred, _false_ otherwise
*/
void DisplayErrors(SquirrelError* exception = nullptr, bool clearErrors = true);
bool DisplayErrorsAndText(wxString pre_error, bool clearErrors = true);

/** @brief Injects script output.
*
Expand All @@ -124,14 +159,22 @@ class DLLIMPORT ScriptingManager : public Mgr<ScriptingManager>, public wxEvtHan
*/
int Configure();

/** @brief Registers a script plugin menu IDs with the callback function.
/** @brief Registers a script plugin
*
* @param name The script plugin's name.
* @param ids The menu IDs to bind.
* @return True on success, false on failure.
*/
bool RegisterScriptPlugin(const wxString& name, const wxArrayInt& ids);

/** @brief Unregisters a script plugin
*
* @param name The script plugin's name.
* @param ids The menu IDs to bind.
* @return True on success, false on failure.
*/
bool UnRegisterScriptPlugin(const wxString& name);

/** @brief Script-bound function to register a script with a menu item.
*
* @param menuPath The full menu path. This can be separated by slashes (/)
Expand Down Expand Up @@ -221,13 +264,52 @@ class DLLIMPORT ScriptingManager : public Mgr<ScriptingManager>, public wxEvtHan
cbThrow(_T("Can't assign a ScriptingManager* !!!"));
return *this;
}

ScriptBindings::CBSquirrelThread * CreateSandbox();

ScriptBindings::CBsquirrelVM* GetVM() {return m_vm;};


/** \brief Search the plugin _Name_ and run its _Execute_ function
*
* \param Name wxString
* \return int 1 On success
* -1 On script error. The error was reported by the script
* -2 "Execute" could not be found in the script
* -3 The script _Name_ could not be found
*
*/
int ExecutePlugin(wxString Name);

/** \brief Search and return the Plugin with the _Name_
*
* \param Name wxString
* \return cbScriptPlugin* nullptr it not found
*
*/
cbScriptPlugin* GetPlugin(wxString Name);

/** \brief Ask all script plugins to create the "right click" menu
*
* \param type const ModuleType
* \param menu wxMenu*
* \param data const FileTreeData*
* \return void
*
*/
void CreateModuleMenu(const ModuleType type, wxMenu* menu, const FileTreeData* data);


private:
// needed for SqPlus bindings
ScriptingManager(cb_unused const ScriptingManager& rhs); // prevent copy construction

void OnScriptMenu(wxCommandEvent& event);
void OnScriptPluginMenu(wxCommandEvent& event);
void RegisterScriptFunctions();
void OnDebugTimer(wxTimerEvent& event);
Copy link
Contributor Author

@bluehazzard bluehazzard Jun 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is without functionality at the moment and can be removed...
void OnDebugTimer(wxTimerEvent& event);
wxTimer m_DebugerUpdateTimer;


wxTimer m_DebugerUpdateTimer;

ScriptingManager();
~ScriptingManager();
Expand All @@ -241,9 +323,11 @@ class DLLIMPORT ScriptingManager : public Mgr<ScriptingManager>, public wxEvtHan
wxString scriptOrFunc;
bool isFunc;
};

typedef std::map<int, MenuBoundScript> MenuIDToScript;
MenuIDToScript m_MenuIDToScript;


bool m_AttachedToMainWindow;
wxString m_CurrentlyRunningScriptFile;

Expand All @@ -252,6 +336,8 @@ class DLLIMPORT ScriptingManager : public Mgr<ScriptingManager>, public wxEvtHan

MenuItemsManager m_MenuItemsManager;

ScriptBindings::CBsquirrelVM* m_vm;

DECLARE_EVENT_TABLE()
};

Expand Down
13 changes: 12 additions & 1 deletion src/plugins/codecompletion/nativeparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#endif

#include <wx/tokenzr.h>
#include <wx/vector.h>

#include <cbstyledtextctrl.h>
#include <compilercommandgenerator.h>
Expand Down Expand Up @@ -2228,10 +2229,12 @@ bool NativeParser::AddProjectDefinedMacros(cbProject* project, ParserBase* parse

wxString defs;
wxArrayString opts;
wxArrayString names;
if ( !parser->Options().platformCheck
|| (parser->Options().platformCheck && project->SupportsCurrentPlatform()) )
{
opts = project->GetCompilerOptions();
names.Add(project->GetTitle() + _T(" compiler options"),opts.Count());
}

ProjectBuildTarget* target = project->GetBuildTarget(project->GetActiveBuildTarget());
Expand All @@ -2242,7 +2245,11 @@ bool NativeParser::AddProjectDefinedMacros(cbProject* project, ParserBase* parse
{
wxArrayString targetOpts = target->GetCompilerOptions();
for (size_t i = 0; i < targetOpts.GetCount(); ++i)
{
opts.Add(targetOpts[i]);
names.Add(target->GetTitle());
}

}
}
// In case of virtual targets, collect the defines from all child targets.
Expand All @@ -2257,15 +2264,19 @@ bool NativeParser::AddProjectDefinedMacros(cbProject* project, ParserBase* parse
{
wxArrayString targetOpts = target->GetCompilerOptions();
for (size_t j = 0; j < targetOpts.GetCount(); ++j)
{
opts.Add(targetOpts[j]);
names.Add(target->GetTitle());
}

}
}
}

for (size_t i = 0; i < opts.GetCount(); ++i)
{
wxString def = opts[i];
Manager::Get()->GetMacrosManager()->ReplaceMacros(def);
Manager::Get()->GetMacrosManager()->ReplaceMacros(def,nullptr,false,names[i]);
if ( !def.StartsWith(defineCompilerSwitch) )
continue;

Expand Down
23 changes: 15 additions & 8 deletions src/sdk/compilercommandgenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "filefilters.h"

#include "scripting/bindings/sc_base_types.h"
#include "scripting/sqplus/sqplus.h"
#include "scripting/bindings/sc_cb_vm.h"

// move this to globals if needed
inline wxString UnquoteStringIfNeeded(const wxString& str)
Expand Down Expand Up @@ -530,23 +530,30 @@ void CompilerCommandGenerator::DoBuildScripts(cbProject* project, CompileTargetB
}

// clear previous script's context
Manager::Get()->GetScriptingManager()->LoadBuffer(clearout_buildscripts);
Manager::Get()->GetScriptingManager()->LoadBuffer(clearout_buildscripts,_T("ClearBuildScript"));

// if the script doesn't exist, just return
if (!Manager::Get()->GetScriptingManager()->LoadScript(script_nomacro))
{
m_NotLoadedScripts.Add(script_nomacro);
continue;
}

try
ScriptBindings::CBsquirrelVM *vm = Manager::Get()->GetScriptingManager()->GetVM();
Sqrat::Function func(Sqrat::RootTable(vm->GetSqVM()),funcName.ToUTF8());
if(func.IsNull())
{
SqPlus::SquirrelFunction<void> f(cbU2C(funcName));
f(target);
//Could not find the function
wxString msg;
msg << _("Function ") << funcName << _("could not be found in:\n ") << script_nomacro;
Manager::Get()->GetScriptingManager()->DisplayErrors(msg);
m_ScriptsWithErrors.Add(script_nomacro);
}
catch (SquirrelError& e)
func(target);
if(vm->HasError())
{
Manager::Get()->GetScriptingManager()->DisplayErrors(&e);
wxString msg;
msg << _("In Script ") << script_nomacro << _(" occurred this error:\n ") << vm->getLastErrorMsg();
Manager::Get()->GetScriptingManager()->DisplayErrors(msg);
m_ScriptsWithErrors.Add(script_nomacro);
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/sdk/macrosmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
#include <wx/stdpaths.h> // wxStandardPaths
#include <cstdlib>

#include "scripting/sqplus/sqplus.h"
#include "scripting/bindings/scriptbindings.h"

#include "cbstyledtextctrl.h"

Expand Down Expand Up @@ -429,7 +427,7 @@ void MacrosManager::RecalcVars(cbProject* project, EditorBase* editor, ProjectBu
m_Macros[_T("DAYCOUNT")] = wxString::Format(_T("%d"), ts.GetDays());
}

void MacrosManager::ReplaceMacros(wxString& buffer, ProjectBuildTarget* target, bool subrequest)
void MacrosManager::ReplaceMacros(wxString& buffer, ProjectBuildTarget* target, bool subrequest,wxString name)
{
if (buffer.IsEmpty())
return;
Expand Down Expand Up @@ -474,7 +472,12 @@ void MacrosManager::ReplaceMacros(wxString& buffer, ProjectBuildTarget* target,
while (m_RE_Script.Matches(buffer))
{
search = m_RE_Script.GetMatch(buffer, 1);
replace = Manager::Get()->GetScriptingManager()->LoadBufferRedirectOutput(m_RE_Script.GetMatch(buffer, 2));
wxString sc_name(_T("Replace Macro Target: "));
if(target != nullptr)
sc_name.Append(target->GetTitle());
else
sc_name.Append(name);
replace = Manager::Get()->GetScriptingManager()->LoadBufferRedirectOutput(m_RE_Script.GetMatch(buffer, 2),sc_name);
buffer.Replace(search, replace, false);
}

Expand Down
Loading