-#include "tree.h"
+#include "tree.hpp"
#include <iostream>
#include <sstream>
#include <stdexcept>
-#include "create_wrapper.h"
-#include "globals.h"
+#include "create_wrapper.hpp"
+#include "globals.hpp"
void
WrapperCreator::create_wrapper(Namespace* ns)
{
+ std::string fromfile = original_file != "" ? original_file : inputfile;
+
+ if(selected_namespace != "") {
+ ns_prefix = selected_namespace;
+ ns_prefix += "::";
+ }
+
// hpp file
hppout
<< "/**\n"
- << " * WARNING: This file is automatically generated from '"
- << inputfile << "' - do not change\n"
+ << " * WARNING: This file is automatically generated from:\n"
+ << " * '" << fromfile << "'\n"
+ << " * DO NOT CHANGE\n"
<< " */\n"
<< "#ifndef __" << modulename << "_WRAPPER_H__\n"
<< "#define __" << modulename << "_WRAPPER_H__\n"
<< "\n"
- << "#include \"wrapper_util.h\"\n"
- << "\n"
- << "extern WrappedFunction " << modulename << "_global_functions[];\n"
- << "extern WrappedClass " << modulename << "_classes[];\n"
+ << "#include <squirrel.h>\n"
+ << "#include \"wrapper.interface.hpp\"\n"
<< "\n"
- << "#endif\n"
+ << "namespace SquirrelWrapper\n"
+ << "{\n"
<< "\n";
+
+ hppout << "void register_" << modulename << "_wrapper(HSQUIRRELVM v);\n"
+ << "\n";
+
+ for(std::vector<AtomicType*>::iterator i = ns->types.begin();
+ i != ns->types.end(); ++i) {
+ AtomicType* type = *i;
+ Class* _class = dynamic_cast<Class*> (type);
+ if(_class == 0)
+ continue;
+
+ hppout << "void create_squirrel_instance(HSQUIRRELVM v, "
+ << ns_prefix << _class->name
+ << "* object, bool setup_releasehook = false);\n";
+ }
+ hppout <<"\n"
+ << "}\n"
+ << "\n"
+ << "#endif\n"
+ << "\n";
// cpp header
out << "/**\n"
- << " * WARNING: This file is automatically generated from '"
- << inputfile << "' - do not change\n"
+ << " * WARNING: This file is automatically generated from:\n"
+ << " * '" << fromfile << "'\n"
+ << " * DO NOT CHANGE\n"
<< " */\n"
- << "\n"
<< "#include <config.h>\n"
+ << "\n"
<< "#include <new>\n"
<< "#include <assert.h>\n"
<< "#include <string>\n"
<< "#include <squirrel.h>\n"
- << "#include \"wrapper_util.h\"\n"
- << "#include \"wrapper.interface.h\"\n"
+ << "#include \"wrapper_util.hpp\"\n"
+ << "#include \"wrapper.interface.hpp\"\n"
+ << "\n"
+ << "namespace SquirrelWrapper\n"
+ << "{\n"
<< "\n";
- if(selected_namespace != "") {
- out << "using namespace " << selected_namespace << ";\n";
- out << "\n";
- }
-
+
for(std::vector<AtomicType*>::iterator i = ns->types.begin();
i != ns->types.end(); ++i) {
AtomicType* type = *i;
create_function_wrapper(0, *i);
}
- // create function list...
- out << "WrappedFunction " << modulename << "_global_functions[] = {\n";
+ out << "void register_" << modulename << "_wrapper(HSQUIRRELVM v)\n"
+ << "{\n"
+ << ind << "sq_pushroottable(v);\n";
+
+ create_register_constants_code(ns);
+ create_register_functions_code(ns);
+ create_register_classes_code(ns);
+
+ out << ind << "sq_pop(v, 1);\n"
+ << "}\n"
+ << "\n"
+ << "}\n"
+ << "\n";
+}
+
+void
+WrapperCreator::create_register_function_code(Function* function, Class* _class)
+{
+ if(function->type == Function::DESTRUCTOR)
+ return;
+
+ out << ind << "sq_pushstring(v, \"" << function->name << "\", -1);\n";
+ out << ind << "sq_newclosure(v, &"
+ << (_class != 0 ? _class->name + "_" : "") << function->name
+ << "_wrapper, 0);\n";
+ create_register_slot_code("function", function->name);
+ out << "\n";
+}
+
+void
+WrapperCreator::create_register_functions_code(Namespace* ns)
+{
for(std::vector<Function*>::iterator i = ns->functions.begin();
i != ns->functions.end(); ++i) {
Function* function = *i;
- out << ind << "{ \"" << function->name << "\", &"
- << function->name << "_wrapper },\n";
+ create_register_function_code(function, 0);
}
- out << ind << "{ 0, 0 }\n"
- << "};\n"
- << "\n";
-
- // create class list...
- std::ostringstream classlist;
- classlist << "WrappedClass " << modulename << "_classes[] = {\n";
+}
+void
+WrapperCreator::create_register_classes_code(Namespace* ns)
+{
for(std::vector<AtomicType*>::iterator i = ns->types.begin();
i != ns->types.end(); ++i) {
AtomicType* type = *i;
- Class* _class = dynamic_cast<Class*> (type);
+ Class* _class = dynamic_cast<Class*> (type);
if(_class == 0)
continue;
+ if(_class->super_classes.size() > 0)
+ continue;
+
+ create_register_class_code(_class);
+ }
+}
+
+void
+WrapperCreator::create_register_class_code(Class* _class)
+{
+ out << ind << "// Register class " << _class->name << "\n";
+ out << ind << "sq_pushstring(v, \""
+ << _class->name << "\", -1);\n";
+
+ if(_class->super_classes.size() > 0) {
+ if(_class->super_classes.size() > 1) {
+ std::ostringstream msg;
+ msg << "Multiple inheritance not supported (at class '"
+ << _class->name << "')";
+ throw std::runtime_error(msg.str());
+ }
- classlist << ind << "{ \"" << _class->name << "\", "
- << modulename << "_" << _class->name
- << "_methods },\n";
-
- out << "static WrappedFunction " << modulename << "_"
- << _class->name << "_methods[] = {\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::DESTRUCTOR)
- continue;
+ out << ind << "sq_pushstring(v, \""
+ << _class->super_classes[0]->name << "\", -1);\n";
+ out << ind << "sq_get(v, -3);\n";
+ }
+ out << ind << "if(sq_newclass(v, "
+ << (_class->super_classes.size() > 0 ? "SQTrue" : "SQFalse")
+ << ") < 0) {\n";
+ out << ind << ind << "std::ostringstream msg;\n";
+ out << ind << ind << "msg << \"Couldn't create new class '"
+ << _class->name << "'\";\n";
+ out << ind << ind << "throw SquirrelError(v, msg.str());\n";
+ out << ind << "}\n";
- out << ind << "{ \"" << function->name << "\", &"
- << _class->name << "_" << function->name << "_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) {
+ create_register_function_code(function, _class);
+ }
+ Field* field = dynamic_cast<Field*> (member);
+ if(field) {
+ create_register_constant_code(field);
}
- out << "};\n"
- << "\n";
}
- classlist << ind << "{ 0, 0 }\n";
- classlist << "};\n";
- out << classlist.str();
+
+ create_register_slot_code("class", _class->name);
out << "\n";
+
+ for(std::vector<Class*>::iterator i = _class->sub_classes.begin();
+ i != _class->sub_classes.end(); ++i) {
+ Class* _class = *i;
+ create_register_class_code(_class);
+ }
+}
+
+void
+WrapperCreator::create_register_constants_code(Namespace* ns)
+{
+ for(std::vector<Field*>::iterator i = ns->fields.begin();
+ i != ns->fields.end(); ++i) {
+ Field* field = *i;
+ create_register_constant_code(field);
+ }
+}
+
+void
+WrapperCreator::create_register_constant_code(Field* field)
+{
+ if(!field->has_const_value)
+ return;
+ out << ind << "sq_pushstring(v, \"" << field->name << "\", -1);\n";
+ if(field->type->atomic_type == &BasicType::INT) {
+ out << ind << "sq_pushinteger(v, " << field->const_int_value << ");\n";
+ } else if(field->type->atomic_type == &BasicType::FLOAT) {
+ out << ind << "sq_pushfloat(v, " << field->const_float_value << ");\n";
+ } else if(field->type->atomic_type == StringType::instance()) {
+ out << ind << "sq_pushstring(v, \""
+ << field->const_string_value << "\", -1);\n";
+ } else {
+ throw std::runtime_error("Constant is not int, float or string");
+ }
+ create_register_slot_code("constant", field->name);
+ out << "\n";
+}
+
+void
+WrapperCreator::create_register_slot_code(const std::string& what,
+ const std::string& name)
+{
+ out << ind << "if(sq_createslot(v, -3) < 0) {\n";
+ out << ind << ind << "std::ostringstream msg;\n";
+ out << ind << ind << "msg << \"Couldn't register " << what << "'"
+ << name << "'\";\n";
+ out << ind << ind << "throw SquirrelError(v, msg.str());\n";
+ out << ind << "}\n";
}
void
}
// declare and retrieve arguments
- size_t i = 0;
+ int i = 0;
+ int arg_offset = 2;
for(std::vector<Parameter>::iterator p = function->parameters.begin();
p != function->parameters.end(); ++p) {
- char argname[64];
- snprintf(argname, sizeof(argname), "arg%u", i);
- prepare_argument(p->type, i + 2, argname);
-
+ if(i == 0 && p->type.atomic_type == HSQUIRRELVMType::instance()) {
+ out << ind << "HSQUIRRELVM arg0 = v;\n";
+ arg_offset--;
+ } else {
+ char argname[64];
+ snprintf(argname, sizeof(argname), "arg%d", i);
+ prepare_argument(p->type, i + arg_offset, argname);
+ }
++i;
}
}
out << ind << "\n";
// push return value back on stack and return
- if(function->return_type.is_void()) {
+ if(function->suspend) {
+ if(!function->return_type.is_void()) {
+ std::stringstream msg;
+ msg << "Function '" << function->name << "' declared as suspend"
+ << " but has a return value.";
+ throw std::runtime_error(msg.str());
+ }
+ out << ind << "return sq_suspendvm(v);\n";
+ } else if(function->return_type.is_void()) {
out << ind << "return 0;\n";
} else {
push_to_stack(function->return_type, "return_value");
void
WrapperCreator::create_class_wrapper(Class* _class)
{
- bool release_hook_created = false;
+ create_class_release_hook(_class);
+ create_squirrel_instance(_class);
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;
- 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;
}
void
+WrapperCreator::create_squirrel_instance(Class* _class)
+{
+ out << "void create_squirrel_instance(HSQUIRRELVM v, "
+ << ns_prefix << _class->name
+ << "* object, bool setup_releasehook)\n"
+ << "{\n"
+ << ind << "sq_pushstring(v, \"" << _class->name << "\", -1);\n"
+ << ind << "if(sq_get(v, -2) < 0) {\n"
+ << ind << ind << "std::ostringstream msg;\n"
+ << ind << ind << "msg << \"Couldn't resolved squirrel type '"
+ << _class->name << "'\";\n"
+ << ind << ind << "throw SquirrelError(v, msg.str());\n"
+ << ind << "}\n"
+ << "\n"
+ << ind << "if(sq_createinstance(v, -1) < 0 || "
+ << "sq_setinstanceup(v, -1, object) < 0) {\n"
+ << ind << ind << "std::ostringstream msg;\n"
+ << ind << ind << "msg << \"Couldn't setup squirrel instance for "
+ << "object of type '" << _class->name << "'\";\n"
+ << ind << ind << "throw SquirrelError(v, msg.str());\n"
+ << ind << "}\n"
+ << ind << "sq_remove(v, -2);\n"
+ << "\n"
+ << ind << "if(setup_releasehook) {\n"
+ << ind << ind << "sq_setreleasehook(v, -1, "
+ << _class->name << "_release_hook);\n"
+ << ind << "}\n"
+ << "}\n";
+}
+
+void
WrapperCreator::create_class_release_hook(Class* _class)
{
out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n"
<< "{\n"
- << ind << _class->name
- << "* _this = reinterpret_cast<" << _class->name << "*> (ptr);\n"
+ << ind << ns_prefix << _class->name
+ << "* _this = reinterpret_cast<" << ns_prefix << _class->name
+ << "*> (ptr);\n"
<< ind << "delete _this;\n"
<< ind << "return 0;\n"
<< "}\n"