CSClassWriter.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/IO/FileSystem.h>
  8. #include "../JSBind.h"
  9. #include "../JSBModule.h"
  10. #include "../JSBPackage.h"
  11. #include "../JSBEnum.h"
  12. #include "../JSBClass.h"
  13. #include "../JSBFunction.h"
  14. #include "CSTypeHelper.h"
  15. #include "CSClassWriter.h"
  16. #include "CSFunctionWriter.h"
  17. namespace ToolCore
  18. {
  19. CSClassWriter::CSClassWriter(JSBClass *klass) : JSBClassWriter(klass)
  20. {
  21. }
  22. void CSClassWriter::WriteNativeFunctions(String& source)
  23. {
  24. for (unsigned i = 0; i < klass_->functions_.Size(); i++)
  25. {
  26. JSBFunction* function = klass_->functions_.At(i);
  27. if (function->Skip())
  28. continue;
  29. if (function->IsDestructor())
  30. continue;
  31. if (CSTypeHelper::OmitFunction(function))
  32. continue;
  33. CSFunctionWriter writer(function);
  34. writer.GenerateNativeSource(source);
  35. }
  36. }
  37. void CSClassWriter::GenerateNativeSource(String& sourceOut)
  38. {
  39. String source = "";
  40. if (klass_->IsNumberArray())
  41. return;
  42. JSBPackage* package = klass_->GetPackage();
  43. source.AppendWithFormat("ATOMIC_EXPORT_API ClassID csb_%s_%s_GetClassIDStatic()\n{\n", package->GetName().CString(),klass_->GetName().CString());
  44. source.AppendWithFormat(" return %s::GetClassIDStatic();\n}\n\n", klass_->GetNativeName().CString());
  45. WriteNativeFunctions(source);
  46. sourceOut += source;
  47. }
  48. void CSClassWriter::WriteManagedProperties(String& sourceOut)
  49. {
  50. String source;
  51. if (klass_->HasProperties())
  52. {
  53. Vector<String> pnames;
  54. klass_->GetPropertyNames(pnames);
  55. for (unsigned j = 0; j < pnames.Size(); j++)
  56. {
  57. JSBProperty* prop = klass_->GetProperty(pnames[j]);
  58. JSBFunctionType* fType = NULL;
  59. JSBFunctionType* getType = NULL;
  60. JSBFunctionType* setType = NULL;
  61. if (CSTypeHelper::OmitFunction(prop->getter_) || CSTypeHelper::OmitFunction(prop->setter_))
  62. continue;
  63. if (prop->getter_ && !prop->getter_->Skip())
  64. {
  65. fType = getType = prop->getter_->GetReturnType();
  66. }
  67. if (prop->setter_ && !prop->setter_->Skip())
  68. {
  69. setType = prop->setter_->GetParameters()[0];
  70. if (!fType)
  71. fType = setType;
  72. else if (fType->type_->ToString() != setType->type_->ToString())
  73. continue;
  74. }
  75. if (!fType)
  76. continue;
  77. String type = CSTypeHelper::GetManagedTypeString(fType, false);
  78. String line = ToString("public %s %s\n", type.CString(), prop->name_.CString());
  79. source += IndentLine(line);
  80. source += IndentLine("{\n");
  81. Indent();
  82. if (prop->getter_)
  83. {
  84. source += IndentLine("get\n");
  85. source += IndentLine("{\n");
  86. Indent();
  87. source += IndentLine(ToString("return %s();\n", prop->getter_->GetName().CString()));
  88. Dedent();
  89. source += IndentLine("}\n");
  90. }
  91. if (prop->setter_)
  92. {
  93. source += IndentLine("set\n");
  94. source += IndentLine("{\n");
  95. Indent();
  96. source += IndentLine(ToString("%s(value);\n", prop->setter_->GetName().CString()));
  97. Dedent();
  98. source += IndentLine("}\n");
  99. }
  100. Dedent();
  101. source += IndentLine("}\n\n");
  102. }
  103. }
  104. sourceOut += source;
  105. }
  106. void CSClassWriter::GenerateManagedSource(String& sourceOut)
  107. {
  108. String source = "";
  109. if (klass_->IsNumberArray())
  110. return;
  111. Indent();
  112. source += "\n";
  113. String line;
  114. if (klass_->GetBaseClass())
  115. line = "public partial class " + klass_->GetName() + " : " + klass_->GetBaseClass()->GetName() + "\n";
  116. else
  117. line = "public partial class " + klass_->GetName() + "\n";
  118. source += IndentLine(line);
  119. source += IndentLine("{\n");
  120. Indent();
  121. WriteManagedProperties(source);
  122. Indent();
  123. JSBPackage* package = klass_->GetPackage();
  124. // CoreCLR has pinvoke security demand code commented out, so we do not (currently) need this optimization:
  125. // https://github.com/dotnet/coreclr/issues/1605
  126. // line = "[SuppressUnmanagedCodeSecurity]\n";
  127. // source += IndentLine(line);
  128. line = "[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
  129. source += IndentLine(line);
  130. line = ToString("public static extern IntPtr csb_%s_%s_GetClassIDStatic();\n", package->GetName().CString(),klass_->GetName().CString());
  131. source += IndentLine(line);
  132. source += "\n";
  133. Dedent();
  134. // managed functions
  135. bool wroteConstructor = false;
  136. for (unsigned i = 0; i < klass_->functions_.Size(); i++)
  137. {
  138. JSBFunction* function = klass_->functions_.At(i);
  139. if (function->Skip())
  140. continue;
  141. if (function->IsDestructor())
  142. continue;
  143. if (CSTypeHelper::OmitFunction(function))
  144. continue;
  145. if (function->IsConstructor())
  146. wroteConstructor = true;
  147. CSFunctionWriter fwriter(function);
  148. fwriter.GenerateManagedSource(source);
  149. }
  150. // There are some constructors being skipped (like HTTPRequest as it uses a vector of strings in args)
  151. // Make sure we have at least a IntPtr version
  152. if (!wroteConstructor)
  153. {
  154. LOGINFOF("WARNING: %s class didn't write a constructor, filling in generated native constructor");
  155. line = ToString("public %s (IntPtr native) : base (native)\n", klass_->GetName().CString());
  156. source += IndentLine(line);
  157. source += IndentLine("{\n");
  158. source += IndentLine("}\n\n");
  159. }
  160. Dedent();
  161. source += IndentLine("}\n");
  162. Dedent();
  163. sourceOut += source;
  164. }
  165. void CSClassWriter::GenerateSource(String& sourceOut)
  166. {
  167. }
  168. }