{
std::string fromfile = original_file != "" ? original_file : inputfile;
+ if(selected_namespace != "") {
+ ns_prefix = selected_namespace;
+ ns_prefix += "::";
+ }
+
// hpp file
hppout
<< "/**\n"
<< "#ifndef __" << modulename << "_WRAPPER_H__\n"
<< "#define __" << modulename << "_WRAPPER_H__\n"
<< "\n"
- << "#include \"wrapper_util.hpp\"\n"
- << "\n"
- << "extern WrappedFunction " << modulename << "_global_functions[];\n"
- << "extern WrappedClass " << modulename << "_classes[];\n"
- << "extern WrappedConstant<int> "
- << modulename << "_int_constants[];\n"
- << "extern WrappedConstant<float> "
- << modulename << "_float_constants[];\n"
- << "extern WrappedConstant<const char*> "
- << modulename << "_string_constants[];\n"
+ << "#include <squirrel.h>\n"
+ << "#include \"wrapper.interface.hpp\"\n"
<< "\n"
- << "static inline void register_"
- << modulename << "_wrapper(HSQUIRRELVM v)\n"
+ << "namespace SquirrelWrapper\n"
<< "{\n"
- << " register_functions(v, "
- << modulename << "_global_functions);\n"
- << " register_classes(v, " << modulename << "_classes);\n"
- << " register_constants(v, " << modulename << "_int_constants);\n"
- << " register_constants(v, " << modulename << "_float_constants);\n"
- << " register_constants(v, "<< modulename << "_string_constants);\n"
- << "}\n"
- << "\n"
- << "#endif\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"
<< "#include <squirrel.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...
- create_function_list(ns);
- create_const_lists(ns);
+ out << "void register_" << modulename << "_wrapper(HSQUIRRELVM v)\n"
+ << "{\n"
+ << ind << "sq_pushroottable(v);\n";
- // create class list...
- std::ostringstream classlist;
- classlist << "WrappedClass " << modulename << "_classes[] = {\n";
+ create_register_constants_code(ns);
+ create_register_functions_code(ns);
+ create_register_classes_code(ns);
- 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;
-
- classlist << ind << "{ \"" << _class->name << "\", "
- << modulename << "_" << _class->name << "_methods, "
- << modulename << "_" << _class->name << "_int_consts, "
- << modulename << "_" << _class->name << "_float_consts, "
- << modulename << "_" << _class->name << "_string_consts "
- << "},\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_pop(v, 1);\n"
+ << "}\n"
+ << "\n"
+ << "}\n"
+ << "\n";
+}
- out << ind << "{ \"" << function->name << "\", &"
- << _class->name << "_" << function->name << "_wrapper },\n";
- }
- out << "};\n"
- << "\n";
- create_class_const_lists(_class);
- }
- classlist << ind << "{ 0, 0, 0, 0, 0 }\n";
- classlist << "};\n";
- out << classlist.str();
- out << "\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_function_list(Namespace* ns)
+WrapperCreator::create_register_functions_code(Namespace* ns)
{
- out << "WrappedFunction " << modulename << "_global_functions[] = {\n";
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";
}
void
-WrapperCreator::create_const_lists(Namespace* ns)
+WrapperCreator::create_register_classes_code(Namespace* ns)
{
- out << "WrappedConstant<int> " << modulename << "_int_constants[] = {\n";
- for(std::vector<Field*>::iterator i = ns->fields.begin();
- i != ns->fields.end(); ++i) {
- Field* field = *i;
- if(!field->has_const_value
- || field->type->atomic_type != &BasicType::INT)
+ 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;
- out << ind << "{ \"" << field->name << "\", "
- << field->const_int_value << "},\n";
- }
- out << ind << "{ 0, 0}\n";
- out << "};\n";
- out << "\n";
-
- out << "WrappedConstant<float> "
- << modulename << "_float_constants[] = {\n";
- for(std::vector<Field*>::iterator i = ns->fields.begin();
- i != ns->fields.end(); ++i) {
- Field* field = *i;
- if(!field->has_const_value
- || field->type->atomic_type != &BasicType::FLOAT)
+ if(_class->super_classes.size() > 0)
continue;
- out << ind << "{ \"" << field->name << "\", "
- << field->const_float_value << "},\n";
- }
- out << ind << "{ 0, 0}\n";
- out << "};\n";
- out << "\n";
- out << "WrappedConstant<const char*> "
- << modulename << "_string_constants[] = {\n";
- for(std::vector<Field*>::iterator i = ns->fields.begin();
- i != ns->fields.end(); ++i) {
- Field* field = *i;
- if(!field->has_const_value
- || field->type->atomic_type != StringType::instance())
- continue;
- out << ind << "{ \"" << field->name << "\", "
- << field->const_float_value << "},\n";
- }
- out << ind << "{ 0, 0}\n";
- out << "};\n";
- out << "\n";
+ create_register_class_code(_class);
+ }
}
void
-WrapperCreator::create_class_const_lists(Class* _class)
+WrapperCreator::create_register_class_code(Class* _class)
{
- out << "static WrappedConstant<int> "
- << modulename << "_" << _class->name << "_int_consts[] = {\n";
+ 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());
+ }
+
+ 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";
+
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)
- continue;
- if(!field->has_const_value
- || field->type->atomic_type != &BasicType::INT)
- continue;
- out << ind << "{ \"" << field->name << "\", "
- << field->const_int_value << "},\n";
+ if(field) {
+ create_register_constant_code(field);
+ }
}
- out << ind << "{ 0, 0}\n";
- out << "};\n";
+
+ create_register_slot_code("class", _class->name);
out << "\n";
- out << "WrappedConstant<float> "
- << modulename << "_" << _class->name << "_float_consts[] = {\n";
- for(std::vector<ClassMember*>::iterator i = _class->members.begin();
- i != _class->members.end(); ++i) {
- ClassMember* member = *i;
- if(member->visibility != ClassMember::PUBLIC)
- continue;
- Field* field = dynamic_cast<Field*> (member);
- if(!field)
- continue;
- if(!field->has_const_value
- || field->type->atomic_type != &BasicType::FLOAT)
- continue;
- out << ind << "{ \"" << field->name << "\", "
- << field->const_float_value << "},\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);
}
- out << ind << "{ 0, 0}\n";
- out << "};\n";
- out << "\n";
+}
- out << "WrappedConstant<const char*> "
- << modulename << "_" << _class->name << "_string_consts[] = {\n";
- for(std::vector<ClassMember*>::iterator i = _class->members.begin();
- i != _class->members.end(); ++i) {
- ClassMember* member = *i;
- if(member->visibility != ClassMember::PUBLIC)
- continue;
- Field* field = dynamic_cast<Field*> (member);
- if(!field)
- continue;
- if(!field->has_const_value
- || field->type->atomic_type != StringType::instance())
- continue;
- out << ind << "{ \"" << field->name << "\", "
- << field->const_float_value << "},\n";
- }
- out << ind << "{ 0, 0}\n";
- out << "};\n";
+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
WrapperCreator::create_function_wrapper(Class* _class, Function* function)
{
if(function->type == Function::DESTRUCTOR)
}
out << ind << "\n";
// push return value back on stack and return
- if(function->return_type.is_void()) {
- if(function->docu_comment.find("@SUSPEND@") != std::string::npos) {
- out << ind << "return sq_suspendvm(v);\n";
- } else {
- out << ind << "return 0;\n";
+ 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");
out << ind << "return 1;\n";
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"