make_wrappers.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. proto_mod = """
  2. #define MODBIND$VER($RETTYPE m_name$ARG) \\
  3. virtual $RETVAL _##m_name($FUNCARGS) $CONST; \\
  4. _FORCE_INLINE_ virtual $RETVAL m_name($FUNCARGS) $CONST override { \\
  5. $RETX _##m_name($CALLARGS);\\
  6. }
  7. """
  8. def generate_mod_version(argcount, const=False, returns=False):
  9. s = proto_mod
  10. sproto = str(argcount)
  11. method_info = ""
  12. if returns:
  13. sproto += "R"
  14. s = s.replace("$RETTYPE", "m_ret, ")
  15. s = s.replace("$RETVAL", "m_ret")
  16. s = s.replace("$RETX", "return")
  17. else:
  18. s = s.replace("$RETTYPE", "")
  19. s = s.replace("$RETVAL", "void")
  20. s = s.replace("$RETX", "")
  21. if const:
  22. sproto += "C"
  23. s = s.replace("$CONST", "const")
  24. else:
  25. s = s.replace("$CONST", "")
  26. s = s.replace("$VER", sproto)
  27. argtext = ""
  28. funcargs = ""
  29. callargs = ""
  30. for i in range(argcount):
  31. if i > 0:
  32. funcargs += ", "
  33. callargs += ", "
  34. argtext += ", m_type" + str(i + 1)
  35. funcargs += "m_type" + str(i + 1) + " arg" + str(i + 1)
  36. callargs += "arg" + str(i + 1)
  37. if argcount:
  38. s = s.replace("$ARG", argtext)
  39. s = s.replace("$FUNCARGS", funcargs)
  40. s = s.replace("$CALLARGS", callargs)
  41. else:
  42. s = s.replace("$ARG", "")
  43. s = s.replace("$FUNCARGS", funcargs)
  44. s = s.replace("$CALLARGS", callargs)
  45. return s
  46. proto_ex = """
  47. #define EXBIND$VER($RETTYPE m_name$ARG) \\
  48. GDVIRTUAL$VER($RETTYPE_##m_name$ARG)\\
  49. virtual $RETVAL m_name($FUNCARGS) $CONST override { \\
  50. $RETPRE\\
  51. GDVIRTUAL_REQUIRED_CALL(_##m_name$CALLARGS$RETREF);\\
  52. $RETPOST\\
  53. }
  54. """
  55. def generate_ex_version(argcount, const=False, returns=False):
  56. s = proto_ex
  57. sproto = str(argcount)
  58. method_info = ""
  59. if returns:
  60. sproto += "R"
  61. s = s.replace("$RETTYPE", "m_ret, ")
  62. s = s.replace("$RETVAL", "m_ret")
  63. s = s.replace("$RETPRE", "m_ret ret; ZeroInitializer<m_ret>::initialize(ret);\\\n")
  64. s = s.replace("$RETPOST", "return ret;\\\n")
  65. else:
  66. s = s.replace("$RETTYPE", "")
  67. s = s.replace("$RETVAL", "void")
  68. s = s.replace("$RETPRE", "")
  69. s = s.replace("$RETPOST", "return;")
  70. if const:
  71. sproto += "C"
  72. s = s.replace("$CONST", "const")
  73. else:
  74. s = s.replace("$CONST", "")
  75. s = s.replace("$VER", sproto)
  76. argtext = ""
  77. funcargs = ""
  78. callargs = ""
  79. for i in range(argcount):
  80. if i > 0:
  81. funcargs += ", "
  82. argtext += ", m_type" + str(i + 1)
  83. funcargs += "m_type" + str(i + 1) + " arg" + str(i + 1)
  84. callargs += ", arg" + str(i + 1)
  85. if argcount:
  86. s = s.replace("$ARG", argtext)
  87. s = s.replace("$FUNCARGS", funcargs)
  88. s = s.replace("$CALLARGS", callargs)
  89. else:
  90. s = s.replace("$ARG", "")
  91. s = s.replace("$FUNCARGS", funcargs)
  92. s = s.replace("$CALLARGS", callargs)
  93. if returns:
  94. s = s.replace("$RETREF", ", ret")
  95. else:
  96. s = s.replace("$RETREF", "")
  97. return s
  98. def run(target, source, env):
  99. max_versions = 12
  100. txt = """
  101. #ifndef GDEXTENSION_WRAPPERS_GEN_H
  102. #define GDEXTENSION_WRAPPERS_GEN_H
  103. """
  104. for i in range(max_versions + 1):
  105. txt += "\n/* Extension Wrapper " + str(i) + " Arguments */\n"
  106. txt += generate_ex_version(i, False, False)
  107. txt += generate_ex_version(i, False, True)
  108. txt += generate_ex_version(i, True, False)
  109. txt += generate_ex_version(i, True, True)
  110. for i in range(max_versions + 1):
  111. txt += "\n/* Module Wrapper " + str(i) + " Arguments */\n"
  112. txt += generate_mod_version(i, False, False)
  113. txt += generate_mod_version(i, False, True)
  114. txt += generate_mod_version(i, True, False)
  115. txt += generate_mod_version(i, True, True)
  116. txt += "\n#endif\n"
  117. with open(target[0], "w") as f:
  118. f.write(txt)
  119. if __name__ == "__main__":
  120. from platform_methods import subprocess_main
  121. subprocess_main(globals())