Skip to content

Commit

Permalink
Remove scripting:: namespace from squirrel/ code
Browse files Browse the repository at this point in the history
  • Loading branch information
Grumbel committed Oct 23, 2018
1 parent 13efd55 commit 85df27f
Show file tree
Hide file tree
Showing 23 changed files with 157 additions and 201 deletions.
2 changes: 1 addition & 1 deletion src/scripting/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ void load_level(const std::string& filename)
void import(HSQUIRRELVM vm, const std::string& filename)
{
IFileStream in(filename);
scripting::compile_and_run(vm, in, filename);
compile_and_run(vm, in, filename);
}

void debug_collrects(bool enable)
Expand Down
4 changes: 2 additions & 2 deletions src/squirrel/exposed_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ExposedObject : public ScriptInterface
log_debug << "Exposing " << m_parent->get_class() << " object " << name << std::endl;

auto object = std::make_unique<T>(m_parent->get_uid());
scripting::expose_object(vm, table_idx, std::move(object), name);
expose_object(vm, table_idx, std::move(object), name);
}

/**
Expand All @@ -94,7 +94,7 @@ class ExposedObject : public ScriptInterface

log_debug << "Unexposing object " << name << std::endl;

scripting::unexpose_object(vm, table_idx, name);
unexpose_object(vm, table_idx, name);
}

private:
Expand Down
18 changes: 9 additions & 9 deletions src/squirrel/script_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ ScriptEngine::ScriptEngine() :
sq_newtable(m_vm);
sq_pushroottable(m_vm);
if(SQ_FAILED(sq_setdelegate(m_vm, -2)))
throw scripting::SquirrelError(m_vm, "Couldn't set table delegate");
throw SquirrelError(m_vm, "Couldn't set table delegate");

sq_resetobject(&m_table);
if (SQ_FAILED(sq_getstackobj(m_vm, -1, &m_table))) {
throw scripting::SquirrelError(m_vm, "Couldn't get table");
throw SquirrelError(m_vm, "Couldn't get table");
}

sq_addref(m_vm, &m_table);
Expand All @@ -61,15 +61,15 @@ void
ScriptEngine::expose_self(const std::string& name)
{
sq_pushroottable(m_vm);
scripting::store_object(m_vm, name.c_str(), m_table);
store_object(m_vm, name.c_str(), m_table);
sq_pop(m_vm, 1);
}

void
ScriptEngine::unexpose_self(const std::string& name)
{
sq_pushroottable(m_vm);
scripting::delete_table_entry(m_vm, name.c_str());
delete_table_entry(m_vm, name.c_str());
sq_pop(m_vm, 1);
}

Expand Down Expand Up @@ -106,7 +106,7 @@ ScriptEngine::unexpose(const std::string& name)
SQInteger oldtop = sq_gettop(m_vm);
sq_pushobject(m_vm, m_table);
try {
scripting::unexpose_object(m_vm, -1, name.c_str());
unexpose_object(m_vm, -1, name.c_str());
} catch(std::exception& e) {
log_warning << "Couldn't unregister object: " << e.what() << std::endl;
}
Expand All @@ -130,7 +130,7 @@ ScriptEngine::run_script(std::istream& in, const std::string& sourcename)
// garbage collect thread list
for(auto i = m_scripts.begin(); i != m_scripts.end(); ) {
HSQOBJECT& object = *i;
HSQUIRRELVM vm = scripting::object_to_vm(object);
HSQUIRRELVM vm = object_to_vm(object);

if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
sq_release(m_vm, &object);
Expand All @@ -141,16 +141,16 @@ ScriptEngine::run_script(std::istream& in, const std::string& sourcename)
++i;
}

HSQOBJECT object = scripting::create_thread(m_vm);
HSQOBJECT object = create_thread(m_vm);
m_scripts.push_back(object);

HSQUIRRELVM vm = scripting::object_to_vm(object);
HSQUIRRELVM vm = object_to_vm(object);

// set root table
sq_pushobject(vm, m_table);
sq_setroottable(vm);

scripting::compile_and_run(vm, in, sourcename);
compile_and_run(vm, in, sourcename);
}
catch(const std::exception& e)
{
Expand Down
2 changes: 1 addition & 1 deletion src/squirrel/script_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ScriptEngine
{
HSQUIRRELVM vm = scripting::global_vm;
sq_pushobject(vm, m_table);
scripting::expose_object(vm, -1, std::move(script_object), name.c_str());
expose_object(vm, -1, std::move(script_object), name.c_str());
sq_pop(vm, 1);
}
void unexpose(const std::string& name);
Expand Down
52 changes: 26 additions & 26 deletions src/squirrel/scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,54 +65,56 @@ namespace scripting {

HSQUIRRELVM global_vm = nullptr;

} // namespace scripting

Scripting::Scripting(bool enable_debugger)
{
global_vm = sq_open(64);
if(global_vm == nullptr)
scripting::global_vm = sq_open(64);
if(scripting::global_vm == nullptr)
throw std::runtime_error("Couldn't initialize squirrel vm");

if(enable_debugger) {
#ifdef ENABLE_SQDBG
sq_enabledebuginfo(global_vm, SQTrue);
debugger = sq_rdbg_init(global_vm, 1234, SQFalse);
sq_enabledebuginfo(scripting::global_vm, SQTrue);
debugger = sq_rdbg_init(scripting::global_vm, 1234, SQFalse);
if(debugger == nullptr)
throw SquirrelError(global_vm, "Couldn't initialize squirrel debugger");
throw SquirrelError(scripting::global_vm, "Couldn't initialize squirrel debugger");

sq_enabledebuginfo(global_vm, SQTrue);
sq_enabledebuginfo(scripting::global_vm, SQTrue);
log_info << "Waiting for debug client..." << std::endl;
if(SQ_FAILED(sq_rdbg_waitforconnections(debugger)))
throw SquirrelError(global_vm, "Waiting for debug clients failed");
throw SquirrelError(scripting::global_vm, "Waiting for debug clients failed");
log_info << "debug client connected." << std::endl;
#endif
}

sq_pushroottable(global_vm);
if(SQ_FAILED(sqstd_register_bloblib(global_vm)))
throw SquirrelError(global_vm, "Couldn't register blob lib");
if(SQ_FAILED(sqstd_register_mathlib(global_vm)))
throw SquirrelError(global_vm, "Couldn't register math lib");
if(SQ_FAILED(sqstd_register_stringlib(global_vm)))
throw SquirrelError(global_vm, "Couldn't register string lib");
sq_pushroottable(scripting::global_vm);
if(SQ_FAILED(sqstd_register_bloblib(scripting::global_vm)))
throw SquirrelError(scripting::global_vm, "Couldn't register blob lib");
if(SQ_FAILED(sqstd_register_mathlib(scripting::global_vm)))
throw SquirrelError(scripting::global_vm, "Couldn't register math lib");
if(SQ_FAILED(sqstd_register_stringlib(scripting::global_vm)))
throw SquirrelError(scripting::global_vm, "Couldn't register string lib");

// remove rand and srand calls from sqstdmath, we'll provide our own
scripting::delete_table_entry(global_vm, "srand");
scripting::delete_table_entry(global_vm, "rand");
delete_table_entry(scripting::global_vm, "srand");
delete_table_entry(scripting::global_vm, "rand");

// register supertux API
register_supertux_wrapper(global_vm);
scripting::register_supertux_wrapper(scripting::global_vm);

sq_pop(global_vm, 1);
sq_pop(scripting::global_vm, 1);

// register print function
sq_setprintfunc(global_vm, printfunc, printfunc);
sq_setprintfunc(scripting::global_vm, printfunc, printfunc);
// register default error handlers
sqstd_seterrorhandlers(global_vm);
sqstd_seterrorhandlers(scripting::global_vm);

// try to load default script
try {
std::string filename = "scripts/default.nut";
IFileStream stream(filename);
scripting::compile_and_run(global_vm, stream, filename);
compile_and_run(scripting::global_vm, stream, filename);
} catch(std::exception& e) {
log_warning << "Couldn't load default.nut: " << e.what() << std::endl;
}
Expand All @@ -127,10 +129,10 @@ Scripting::~Scripting()
}
#endif

if (global_vm)
sq_close(global_vm);
if (scripting::global_vm)
sq_close(scripting::global_vm);

global_vm = nullptr;
scripting::global_vm = nullptr;
}

void
Expand All @@ -142,6 +144,4 @@ Scripting::update_debugger()
#endif
}

} // namespace scripting

/* EOF */
4 changes: 2 additions & 2 deletions src/squirrel/scripting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace scripting {

extern HSQUIRRELVM global_vm;

} // namespace scripting

class Scripting final : public Currenton<Scripting>
{
public:
Expand All @@ -38,8 +40,6 @@ class Scripting final : public Currenton<Scripting>
Scripting& operator=(const Scripting&) = delete;
};

} // namespace scripting

#endif

/* EOF */
6 changes: 1 addition & 5 deletions src/squirrel/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#include "util/reader_mapping.hpp"
#include "util/writer.hpp"

namespace scripting {

void load_squirrel_table(HSQUIRRELVM vm, SQInteger table_idx, const ReaderMapping& lisp)
{
if(table_idx < 0)
Expand Down Expand Up @@ -84,7 +82,7 @@ void load_squirrel_table(HSQUIRRELVM vm, SQInteger table_idx, const ReaderMappin
}

if(SQ_FAILED(sq_createslot(vm, table_idx)))
throw scripting::SquirrelError(vm, "Couldn't create new index");
throw SquirrelError(vm, "Couldn't create new index");
}
}

Expand Down Expand Up @@ -148,6 +146,4 @@ void save_squirrel_table(HSQUIRRELVM vm, SQInteger table_idx, Writer& writer)
sq_pop(vm, 1);
}

} // namespace scripting

/* EOF */
4 changes: 0 additions & 4 deletions src/squirrel/serialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@
class ReaderMapping;
class Writer;

namespace scripting {

void save_squirrel_table(HSQUIRRELVM vm, SQInteger table_idx, Writer& writer);
void load_squirrel_table(HSQUIRRELVM vm, SQInteger table_idx, const ReaderMapping& lisp);

} // namespace scripting

#endif

/* EOF */
4 changes: 0 additions & 4 deletions src/squirrel/squirrel_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

#include <sstream>

namespace scripting {

SquirrelError::SquirrelError(HSQUIRRELVM v, const std::string& message_) throw() :
message()
{
Expand Down Expand Up @@ -49,6 +47,4 @@ SquirrelError::what() const throw()
return message.c_str();
}

}

/* EOF */
4 changes: 0 additions & 4 deletions src/squirrel/squirrel_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
#include <stdexcept>
#include <string>

namespace scripting {

/** Exception class for squirrel errors, it takes a squirrelvm and uses
* sq_geterror() to retrieve additional information about the last error that
* occurred and creates a readable message from that.
Expand All @@ -39,8 +37,6 @@ class SquirrelError final : public std::exception
std::string message;
};

} // namespace scripting

#endif

/* EOF */
Loading

0 comments on commit 85df27f

Please sign in to comment.