using namespace Scripting;
-static int Display_release_wrapper(SQUserPointer ptr, int )
-{
- Display* _this = reinterpret_cast<Display*> (ptr);
- delete _this;
- return 0;
-}
-
-static int Display_construct_wrapper(HSQUIRRELVM v)
-{
- Display* _this = new Display();
- sq_setinstanceup(v, 1, _this);
- sq_setreleasehook(v, 1, Display_release_wrapper);
-
- return 0;
-}
-
static int Display_set_effect_wrapper(HSQUIRRELVM v)
{
- Display* _this;
+ Scripting::Display* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
const char* arg0;
sq_getstring(v, 2, &arg0);
return 0;
}
-static int Camera_release_wrapper(SQUserPointer ptr, int )
-{
- Camera* _this = reinterpret_cast<Camera*> (ptr);
- delete _this;
- return 0;
-}
-
-static int Camera_construct_wrapper(HSQUIRRELVM v)
-{
- Camera* _this = new Camera();
- sq_setinstanceup(v, 1, _this);
- sq_setreleasehook(v, 1, Camera_release_wrapper);
-
- return 0;
-}
-
static int Camera_shake_wrapper(HSQUIRRELVM v)
{
- Camera* _this;
+ Scripting::Camera* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
float arg0;
sq_getfloat(v, 2, &arg0);
float arg1;
static int Camera_set_pos_wrapper(HSQUIRRELVM v)
{
- Camera* _this;
+ Scripting::Camera* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
float arg0;
sq_getfloat(v, 2, &arg0);
float arg1;
static int Camera_set_mode_wrapper(HSQUIRRELVM v)
{
- Camera* _this;
+ Scripting::Camera* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
const char* arg0;
sq_getstring(v, 2, &arg0);
return 0;
}
-static int Level_release_wrapper(SQUserPointer ptr, int )
-{
- Level* _this = reinterpret_cast<Level*> (ptr);
- delete _this;
- return 0;
-}
-
-static int Level_construct_wrapper(HSQUIRRELVM v)
-{
- Level* _this = new Level();
- sq_setinstanceup(v, 1, _this);
- sq_setreleasehook(v, 1, Level_release_wrapper);
-
- return 0;
-}
-
static int Level_finish_wrapper(HSQUIRRELVM v)
{
- Level* _this;
+ Scripting::Level* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
_this->finish();
static int Level_spawn_wrapper(HSQUIRRELVM v)
{
- Level* _this;
+ Scripting::Level* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
const char* arg0;
sq_getstring(v, 2, &arg0);
const char* arg1;
return 0;
}
-static int ScriptedObject_release_wrapper(SQUserPointer ptr, int )
-{
- ScriptedObject* _this = reinterpret_cast<ScriptedObject*> (ptr);
- delete _this;
- return 0;
-}
-
-static int ScriptedObject_construct_wrapper(HSQUIRRELVM v)
-{
- ScriptedObject* _this = new ScriptedObject();
- sq_setinstanceup(v, 1, _this);
- sq_setreleasehook(v, 1, ScriptedObject_release_wrapper);
-
- return 0;
-}
-
static int ScriptedObject_set_animation_wrapper(HSQUIRRELVM v)
{
- ScriptedObject* _this;
+ Scripting::ScriptedObject* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
const char* arg0;
sq_getstring(v, 2, &arg0);
static int ScriptedObject_get_animation_wrapper(HSQUIRRELVM v)
{
- ScriptedObject* _this;
+ Scripting::ScriptedObject* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
std::string return_value = _this->get_animation();
static int ScriptedObject_move_wrapper(HSQUIRRELVM v)
{
- ScriptedObject* _this;
+ Scripting::ScriptedObject* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
float arg0;
sq_getfloat(v, 2, &arg0);
float arg1;
static int ScriptedObject_set_pos_wrapper(HSQUIRRELVM v)
{
- ScriptedObject* _this;
+ Scripting::ScriptedObject* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
float arg0;
sq_getfloat(v, 2, &arg0);
float arg1;
static int ScriptedObject_get_pos_x_wrapper(HSQUIRRELVM v)
{
- ScriptedObject* _this;
+ Scripting::ScriptedObject* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
float return_value = _this->get_pos_x();
static int ScriptedObject_get_pos_y_wrapper(HSQUIRRELVM v)
{
- ScriptedObject* _this;
+ Scripting::ScriptedObject* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
float return_value = _this->get_pos_y();
static int ScriptedObject_set_velocity_wrapper(HSQUIRRELVM v)
{
- ScriptedObject* _this;
+ Scripting::ScriptedObject* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
float arg0;
sq_getfloat(v, 2, &arg0);
float arg1;
static int ScriptedObject_get_velocity_x_wrapper(HSQUIRRELVM v)
{
- ScriptedObject* _this;
+ Scripting::ScriptedObject* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
float return_value = _this->get_velocity_x();
static int ScriptedObject_get_velocity_y_wrapper(HSQUIRRELVM v)
{
- ScriptedObject* _this;
+ Scripting::ScriptedObject* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
float return_value = _this->get_velocity_y();
return 1;
}
-static int Sound_release_wrapper(SQUserPointer ptr, int )
-{
- Sound* _this = reinterpret_cast<Sound*> (ptr);
- delete _this;
- return 0;
-}
-
-static int Sound_construct_wrapper(HSQUIRRELVM v)
-{
- Sound* _this = new Sound();
- sq_setinstanceup(v, 1, _this);
- sq_setreleasehook(v, 1, Sound_release_wrapper);
-
- return 0;
-}
-
static int Sound_play_music_wrapper(HSQUIRRELVM v)
{
- Sound* _this;
+ Scripting::Sound* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
const char* arg0;
sq_getstring(v, 2, &arg0);
static int Sound_play_sound_wrapper(HSQUIRRELVM v)
{
- Sound* _this;
+ Scripting::Sound* _this;
sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);
- assert(_this != 0);
const char* arg0;
sq_getstring(v, 2, &arg0);
};
static WrappedFunction supertux_Display_methods[] = {
- { "constructor", &Display_construct_wrapper },
{ "set_effect", &Display_set_effect_wrapper },
};
static WrappedFunction supertux_Camera_methods[] = {
- { "constructor", &Camera_construct_wrapper },
{ "shake", &Camera_shake_wrapper },
{ "set_pos", &Camera_set_pos_wrapper },
{ "set_mode", &Camera_set_mode_wrapper },
};
static WrappedFunction supertux_Level_methods[] = {
- { "constructor", &Level_construct_wrapper },
{ "finish", &Level_finish_wrapper },
{ "spawn", &Level_spawn_wrapper },
};
static WrappedFunction supertux_ScriptedObject_methods[] = {
- { "constructor", &ScriptedObject_construct_wrapper },
{ "set_animation", &ScriptedObject_set_animation_wrapper },
{ "get_animation", &ScriptedObject_get_animation_wrapper },
{ "move", &ScriptedObject_move_wrapper },
};
static WrappedFunction supertux_Sound_methods[] = {
- { "constructor", &Sound_construct_wrapper },
{ "play_music", &Sound_play_music_wrapper },
{ "play_sound", &Sound_play_sound_wrapper },
};
out << "static WrappedFunction " << modulename << "_"
<< _class->name << "_methods[] = {\n";
- out << ind << "{ \"constructor\", &"
- << _class->name << "_" << "construct_wrapper },\n";
for(std::vector<ClassMember*>::iterator i = _class->members.begin();
i != _class->members.end(); ++i) {
ClassMember* member = *i;
if(member->visibility != ClassMember::PUBLIC)
continue;
Function* function = dynamic_cast<Function*> (member);
- if(!function || function->type != Function::FUNCTION)
+ if(!function || function->type == Function::DESTRUCTOR)
continue;
out << ind << "{ \"" << function->name << "\", &"
void
WrapperCreator::create_function_wrapper(Class* _class, Function* function)
{
- if(function->type == Function::CONSTRUCTOR)
- throw std::runtime_error("Constructors not supported yet");
if(function->type == Function::DESTRUCTOR)
- throw std::runtime_error("Destructors not supported yet");
-
+ assert(false);
+
+ std::string ns_prefix;
+ if(selected_namespace != "")
+ ns_prefix = selected_namespace + "::";
+ if(function->type == Function::CONSTRUCTOR)
+ function->name = "constructor";
+
out << "static int ";
if(_class != 0) {
out << _class->name << "_";
<< "{\n";
// avoid warning...
if(_class == 0 && function->parameters.empty()
- && function->return_type.is_void()) {
+ && function->return_type.is_void()
+ && function->type != Function::CONSTRUCTOR) {
out << ind << "(void) v;\n";
}
- // eventually retrieve pointer to class
- if(_class != 0) {
- out << ind << _class->name << "* _this;\n";
+ // eventually retrieve pointer to class instance
+ if(_class != 0 && function->type != Function::CONSTRUCTOR) {
+ out << ind << ns_prefix << _class->name << "* _this;\n";
out << ind << "sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);\n";
- out << ind << "assert(_this != 0);\n";
}
// declare and retrieve arguments
++i;
}
+
// call function
out << ind << "\n";
out << ind;
out << " return_value = ";
}
if(_class != 0) {
- out << "_this->";
- } else if(selected_namespace != "") {
- out << selected_namespace << "::";
+ if(function->type == Function::CONSTRUCTOR) {
+ out << ns_prefix << _class->name << "* _this = new " << ns_prefix;
+ } else {
+ out << "_this->";
+ }
+ } else {
+ out << ns_prefix;
+ }
+ if(function->type == Function::CONSTRUCTOR) {
+ out << _class->name << "(";
+ } else {
+ out << function->name << "(";
}
- out << function->name << "(";
for(size_t i = 0; i < function->parameters.size(); ++i) {
if(i != 0)
out << ", ";
out << "arg" << i;
}
out << ");\n";
+ if(function->type == Function::CONSTRUCTOR) {
+ out << ind << "sq_setinstanceup(v, 1, _this);\n";
+ out << ind << "sq_setreleasehook(v, 1, "
+ << _class->name << "_release_hook);\n";
+ }
out << ind << "\n";
// push return value back on stack and return
if(function->return_type.is_void()) {
void
WrapperCreator::create_class_wrapper(Class* _class)
{
- create_class_destruct_function(_class);
- create_class_construct_function(_class);
+ bool release_hook_created = false;
for(std::vector<ClassMember*>::iterator i = _class->members.begin();
i != _class->members.end(); ++i) {
ClassMember* member = *i;
Function* function = dynamic_cast<Function*> (member);
if(!function)
continue;
- // don't wrap constructors and destructors (for now...)
- if(function->type != Function::FUNCTION)
+ if(function->type == Function::CONSTRUCTOR
+ && !release_hook_created) {
+ create_class_release_hook(_class);
+ release_hook_created = true;
+ }
+ // don't wrap destructors
+ if(function->type == Function::DESTRUCTOR)
continue;
create_function_wrapper(_class, function);
}
}
void
-WrapperCreator::create_class_construct_function(Class* _class)
-{
- out << "static int " << _class->name << "_construct_wrapper(HSQUIRRELVM v)\n";
- out << "{\n";
- out << ind << _class->name << "* _this = new "
- << _class->name << "();\n";
- out << ind << "sq_setinstanceup(v, 1, _this);\n";
- out << ind << "sq_setreleasehook(v, 1, "
- << _class->name << "_release_wrapper);\n";
- out << "\n";
- out << ind << "return 0;\n";
- out << "}\n";
- out << "\n";
-}
-
-void
-WrapperCreator::create_class_destruct_function(Class* _class)
+WrapperCreator::create_class_release_hook(Class* _class)
{
- out << "static int " << _class->name << "_release_wrapper(SQUserPointer ptr, int )\n"
+ out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n"
<< "{\n"
<< ind << _class->name
<< "* _this = reinterpret_cast<" << _class->name << "*> (ptr);\n"