File: create_wrapper.cpp

package info (click to toggle)
supertux 0.6.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 264,124 kB
  • sloc: cpp: 113,426; ansic: 9,654; sh: 4,483; cs: 1,296; makefile: 407; yacc: 398; python: 382; lisp: 285; objc: 248; csh: 219; lex: 140; perl: 118; xml: 53; ruby: 36
file content (555 lines) | stat: -rw-r--r-- 19,082 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#include <config.h>

#include "tree.hpp"
#include "create_wrapper.hpp"
#include "globals.hpp"

#include <iostream>
#include <sstream>
#include <stdexcept>
#include <stdio.h>

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:\n"
        << " *  '" << fromfile << "'\n"
        << " * DO NOT CHANGE\n"
        << " */\n"
        << "#ifndef HEADER_SUPERTUX_SCRIPTING_WRAPPER_HPP\n" //TODO avoid hardcoding
        << "#define HEADER_SUPERTUX_SCRIPTING_WRAPPER_HPP\n"
        << "\n"
        << "#include <squirrel.h>\n"
        << "\n"
        << "namespace scripting {\n"
        << "\n";

    hppout << "void register_" << modulename << "_wrapper(HSQUIRRELVM v);\n"
           << "\n";

    for(auto& type : ns->types) {
        auto _class = dynamic_cast<Class*> (type);
        if(_class == 0)
            continue;

        hppout << "class " << _class->name << ";\n";
        hppout << "void create_squirrel_instance(HSQUIRRELVM v, "
               << ns_prefix << _class->name
               << "* object, bool setup_releasehook = false);\n";
    }
    hppout <<"\n"
           << "}\n"
           << "\n"
           << "#endif\n"
           << "\n"
           << "/* EOF */\n";

    // cpp header
    out << "/**\n"
        << " * WARNING: This file is automatically generated from:\n"
        << " *  '" << fromfile << "'\n"
        << " * DO NOT CHANGE\n"
        << " */\n"
        << "\n"
        << "#include \"scripting/wrapper.hpp\"\n"
        << "\n"
        << "#include <assert.h>\n"
        << "#include <limits>\n"
        << "#include <sstream>\n"
        << "\n"
        << "#include \"squirrel/squirrel_error.hpp\"\n"
        << "#include \"scripting/wrapper.interface.hpp\"\n"
        << "\n"
        << "namespace scripting {\n"
        << "namespace wrapper {\n"
        << "\n";

    for(auto& type : ns->types) {
        auto _class = dynamic_cast<Class*> (type);
        if(_class != 0)
            create_class_wrapper(_class);
    }
    for(auto& func : ns->functions) {
        create_function_wrapper(0, func);
    }

    out << "} // namespace wrapper\n";

    for(auto& type : ns->types) {
        Class* _class = dynamic_cast<Class*> (type);
        if(_class != 0)
            create_squirrel_instance(_class);
    }

    out << "void register_" << modulename << "_wrapper(HSQUIRRELVM v)\n"
        << "{\n"
        << ind << "using namespace wrapper;\n"
        << "\n";

    create_register_constants_code(ns);
    create_register_functions_code(ns);
    create_register_classes_code(ns);

    out << "}\n"
        << "\n"
        << "} // namespace scripting\n"
        << "\n"
        << "/* EOF */\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";

    if(function->custom) {
      out << ind << "sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, " << function->parameter_spec << ");\n";
    } else {
      out << ind << "sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, \"";

      out << "x|t";

      if(!function->parameters.empty())
        {
          std::vector<Parameter>::iterator p = function->parameters.begin();

          // Skip the first parameter since its a HSQUIRRELVM that is
          // handled internally
          if (function->suspend) {
            ++p;
          } else if (p->type.atomic_type == HSQUIRRELVMType::instance()) {
            ++p;
          }

          for(; p != function->parameters.end(); ++p) {
            if(p->type.atomic_type == &BasicType::INT) {
              out << "i";
            } else if(p->type.atomic_type == &BasicType::FLOAT) {
              out << "n";
            } else if(p->type.atomic_type == &BasicType::BOOL) {
              out << "b";
            } else if(p->type.atomic_type == StringType::instance()) {
              out << "s";
            } else {
              out << ".";
            }
          }
      }
      out << "\");\n";
    }

    create_register_slot_code("function", function->name);
    out << "\n";
}

void
WrapperCreator::create_register_functions_code(Namespace* ns)
{
    for(auto& function : ns->functions) {
        create_register_function_code(function, 0);
    }
}

void
WrapperCreator::create_register_classes_code(Namespace* ns)
{
    for(auto& type : ns->types) {
        auto _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());
        }

        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(auto& member : _class->members) {
        if(member->visibility != ClassMember::PUBLIC)
            continue;
        auto function = dynamic_cast<Function*> (member);
        if(function) {
            create_register_function_code(function, _class);
        }
        auto field = dynamic_cast<Field*> (member);
        if(field) {
            create_register_constant_code(field);
        }
    }

    create_register_slot_code("class", _class->name);
    out << "\n";

    for(auto& c : _class->sub_classes) {
        create_register_class_code(c);
    }
}

void
WrapperCreator::create_register_constants_code(Namespace* ns)
{
    for(auto& field: ns->fields) {
        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_FAILED(sq_createslot(v, -3))) {\n";
    out << ind << ind << "throw SquirrelError(v, \""
        << "Couldn't register " << what << " '" << name << "'\");\n";
    out << ind << "}\n";
}

void
WrapperCreator::create_function_wrapper(Class* _class, Function* function)
{
    if(function->type == Function::DESTRUCTOR)
        assert(false);

    std::string ns_prefix;
    if(selected_namespace != "")
        ns_prefix = selected_namespace + "::";
    if(function->type == Function::CONSTRUCTOR)
        function->name = "constructor";

    out << "static SQInteger ";
    if(_class != 0) {
        out << _class->name << "_";
    }
    out << function->name << "_wrapper(HSQUIRRELVM vm)\n"
        << "{\n";
    // avoid warning...
    if(_class == 0 && function->parameters.empty()
            && function->return_type.is_void()
            && function->type != Function::CONSTRUCTOR) {
        out << ind << "(void) vm;\n";
    }

    // retrieve pointer to class instance
    if(_class != 0 && function->type != Function::CONSTRUCTOR) {
        out << ind << "SQUserPointer data;\n";
        out << ind << "if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, nullptr)) || !data) {\n";
        out << ind << ind << "sq_throwerror(vm, _SC(\"'" << function->name << "' called without instance\"));\n";
        out << ind << ind << "return SQ_ERROR;\n";
        out << ind << "}\n";
        out << ind << "auto _this = reinterpret_cast<" << ns_prefix << _class->name << "*> (data);\n";
        out << "\n";
        out << ind << "if (_this == nullptr) {\n";
        out << ind << ind << "return SQ_ERROR;\n";
        out << ind << "}\n";
        out << "\n";
    }

    // custom function?
    if(function->custom) {
        if(function->type != Function::FUNCTION)
            throw std::runtime_error(
                    "custom not allow constructor+destructor yet");
        if(function->return_type.atomic_type != SQIntegerType::instance())
            throw std::runtime_error("custom function '" + function->name + "' has to return SQInteger");
        if(function->parameters.size() != 1)
            throw std::runtime_error(
                    "custom function '" + function->name + "' must have 1 HSQUIRRELVM parameter");

        out << ind << "return ";
        if(_class != 0)
            out << "_this->";
        else
            out << ns_prefix;
        out << function->name << "(vm);\n";
        out << "}\n";
        out << "\n";
        return;
    }

    // declare and retrieve arguments
    int i = 0;
    int arg_offset = 2;
    for(auto& p : function->parameters) {
        if(i == 0 && p.type.atomic_type == HSQUIRRELVMType::instance()) {
            out << ind << "HSQUIRRELVM arg0 = vm;\n";
            arg_offset--;
        } else {
            char argname[64];
            snprintf(argname, sizeof(argname), "arg%d", i);
            prepare_argument(p.type, i + arg_offset, argname);
        }
        ++i;
    }

    // call function
    out << "\n";
    out << ind << "try {\n";
    out << ind << ind;
    if(!function->return_type.is_void()) {
        function->return_type.write_c_type(out);
        out << " return_value = ";
    }
    if(_class != 0) {
        if(function->type == Function::CONSTRUCTOR) {
            out << "auto _this = new " << ns_prefix;
        } else {
            out << "_this->";
        }
    } else {
        out << ns_prefix;
    }
    if(function->type == Function::CONSTRUCTOR) {
        out << _class->name << "(";
    } else {
        out << function->name << "(";
    }
    for(size_t i = 0; i < function->parameters.size(); ++i) {
        if(i != 0)
            out << ", ";
        const Parameter param = function->parameters[i];
        if(param.type.ref == 0 && param.type.pointer == 0) {
            if(param.type.atomic_type == &BasicType::INT)
                out << "static_cast<int> (arg" << i << ")";
            else if(param.type.atomic_type == &BasicType::FLOAT)
                out << "static_cast<float> (arg" << i << ")";
            else if(param.type.atomic_type == &BasicType::BOOL)
                out << "arg" << i << " == SQTrue";
            else
                out << "arg" << i;
        } else {
            out << "arg" << i;
        }
    }
    out << ");\n";
    if(function->type == Function::CONSTRUCTOR) {
        out << ind << "if(SQ_FAILED(sq_setinstanceup(vm, 1, _this))) {\n";
        out << ind << ind << "sq_throwerror(vm, _SC(\"Couldn't setup instance of '" << _class->name << "' class\"));\n";
        out << ind << ind << "return SQ_ERROR;\n";
        out << ind << "}\n";
        out << ind << "sq_setreleasehook(vm, 1, "
            << _class->name << "_release_hook);\n";
    }
    out << "\n";
    // push return value back on stack and return
    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 << ind << "return sq_suspendvm(vm);\n";
    } else if(function->return_type.is_void()) {
        out << ind << ind << "return 0;\n";
    } else {
        push_to_stack(function->return_type, "return_value");
        out << ind << ind << "return 1;\n";
    }

    out << "\n";
    out << ind << "} catch(std::exception& e) {\n";
    out << ind << ind << "sq_throwerror(vm, e.what());\n";
    out << ind << ind << "return SQ_ERROR;\n";
    out << ind << "} catch(...) {\n";
    out << ind << ind << "sq_throwerror(vm, _SC(\"Unexpected exception while executing function '" << function->name << "'\"));\n";
    out << ind << ind << "return SQ_ERROR;\n";
    out << ind << "}\n";
    out << "\n";

    out << "}\n";
    out << "\n";
}

void
WrapperCreator::prepare_argument(const Type& type, size_t index,
        const std::string& var)
{
    if(type.ref > 0 && type.atomic_type != StringType::instance())
        throw std::runtime_error("References not handled yet");
    if(type.pointer > 0)
        throw std::runtime_error("Pointers not handled yet");
    if(type.atomic_type == &BasicType::INT) {
        out << ind << "SQInteger " << var << ";\n";
        out << ind << "if(SQ_FAILED(sq_getinteger(vm, " << index << ", &" << var << "))) {\n";
        out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not an integer\"));\n";
        out << ind << ind << "return SQ_ERROR;\n";
        out << ind << "}\n";
    } else if(type.atomic_type == &BasicType::FLOAT) {
        out << ind << "SQFloat " << var << ";\n";
        out << ind << "if(SQ_FAILED(sq_getfloat(vm, " << index << ", &" << var << "))) {\n";
        out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a float\"));\n";
        out << ind << ind << "return SQ_ERROR;\n";
        out << ind << "}\n";
    } else if(type.atomic_type == &BasicType::BOOL) {
        out << ind << "SQBool " << var << ";\n";
        out << ind << "if(SQ_FAILED(sq_getbool(vm, " << index << ", &" << var << "))) {\n";
        out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a bool\"));\n";
        out << ind << ind << "return SQ_ERROR;\n";
        out << ind << "}\n";
    } else if(type.atomic_type == StringType::instance()) {
        out << ind << "const SQChar* " << var << ";\n";
        out << ind << "if(SQ_FAILED(sq_getstring(vm, " << index << ", &" << var << "))) {\n";
        out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a string\"));\n";
        out << ind << ind << "return SQ_ERROR;\n";
        out << ind << "}\n";
    } else {
        std::ostringstream msg;
        msg << "Type '" << type.atomic_type->name << "' not supported yet.";
        throw std::runtime_error(msg.str());
    }
}

void
WrapperCreator::push_to_stack(const Type& type, const std::string& var)
{
    if(type.ref > 0 && type.atomic_type != StringType::instance())
        throw std::runtime_error("References not handled yet");
    if(type.pointer > 0)
        throw std::runtime_error("Pointers not handled yet");
    out << ind << ind;
    if(type.atomic_type == &BasicType::INT) {
        out << "sq_pushinteger(vm, " << var << ");\n";
    } else if(type.atomic_type == &BasicType::FLOAT) {
        out << "sq_pushfloat(vm, " << var << ");\n";
    } else if(type.atomic_type == &BasicType::BOOL) {
        out << "sq_pushbool(vm, " << var << ");\n";
    } else if(type.atomic_type == StringType::instance()) {
        out << "assert(" << var << ".size() < static_cast<size_t>(std::numeric_limits<SQInteger>::max()));\n"
            << ind << ind << "sq_pushstring(vm, " << var << ".c_str(), static_cast<SQInteger>("
            << var << ".size()));\n";
    } else {
        std::ostringstream msg;
        msg << "Type '" << type.atomic_type->name << "' not supported yet.";
        throw std::runtime_error(msg.str());
    }
}

void
WrapperCreator::create_class_wrapper(Class* _class)
{
    create_class_release_hook(_class);
    for(auto& member : _class->members) {
        if(member->visibility != ClassMember::PUBLIC)
            continue;
        Function* function = dynamic_cast<Function*> (member);
        if(!function)
            continue;
        // don't wrap destructors
        if(function->type == Function::DESTRUCTOR)
            continue;
        create_function_wrapper(_class, function);
    }
}

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 << "using namespace wrapper;\n"
        << "\n"
        << ind << "sq_pushroottable(v);\n"
        << ind << "sq_pushstring(v, \"" << _class->name << "\", -1);\n"
        << ind << "if(SQ_FAILED(sq_get(v, -2))) {\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_FAILED(sq_createinstance(v, -1)) || "
        << "SQ_FAILED(sq_setinstanceup(v, -1, object))) {\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); // remove object name\n"
        << "\n"
        << ind << "if(setup_releasehook) {\n"
        << ind << ind << "sq_setreleasehook(v, -1, "
        << _class->name << "_release_hook);\n"
        << ind << "}\n"
        << "\n"
        << ind << "sq_remove(v, -2); // remove root table\n"
        << "}\n"
        << "\n";
}

void
WrapperCreator::create_class_release_hook(Class* _class)
{
    out << "static SQInteger " << _class->name << "_release_hook(SQUserPointer ptr, SQInteger )\n"
        << "{\n"
        << ind << "auto _this = reinterpret_cast<" << ns_prefix << _class->name
        << "*> (ptr);\n"
        << ind << "delete _this;\n"
        << ind << "return 0;\n"
        << "}\n"
        << "\n";
}