CSFunctionWriter.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 "CSFunctionWriter.h"
  15. namespace ToolCore
  16. {
  17. CSFunctionWriter::CSFunctionWriter(JSBFunction *function) : JSBFunctionWriter(function)
  18. {
  19. }
  20. void CSFunctionWriter::WriteParameterMarshal(String& source)
  21. {
  22. }
  23. void CSFunctionWriter::WriteConstructor(String& source)
  24. {
  25. }
  26. void CSFunctionWriter::GenNativeFunctionSignature(String& sig)
  27. {
  28. // generate args
  29. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  30. int cparam = 0;
  31. if (parameters.Size())
  32. {
  33. for (unsigned int i = 0; i < parameters.Size(); i++, cparam++)
  34. {
  35. JSBFunctionType * ptype = parameters.At(i);
  36. // ignore "Context" parameters
  37. if (ptype->type_->asClassType())
  38. {
  39. JSBClassType* classType = ptype->type_->asClassType();
  40. JSBClass* klass = classType->class_;
  41. if (klass->GetName() == "Context")
  42. {
  43. cparam--;
  44. continue;
  45. }
  46. }
  47. String pstring = ptype->ToArgString(cparam);
  48. if (ptype->type_->asClassType())
  49. {
  50. JSBClassType* classType = ptype->type_->asClassType();
  51. JSBClass* klass = classType->class_;
  52. if (!klass->IsNumberArray())
  53. {
  54. sig.AppendWithFormat("%s", pstring.CString());
  55. }
  56. else
  57. {
  58. sig.AppendWithFormat("%s __arg%i", klass->GetNativeName().CString(), cparam);
  59. }
  60. }
  61. else if (ptype->type_->asStringType() || ptype->type_->asStringHashType())
  62. {
  63. sig.AppendWithFormat("char* __arg%i", cparam);
  64. }
  65. else if (ptype->type_->asHeapPtrType())
  66. {
  67. assert(0);
  68. }
  69. else if (ptype->type_->asPrimitiveType())
  70. {
  71. JSBPrimitiveType* prtype = ptype->type_->asPrimitiveType();
  72. sig.AppendWithFormat("%s __arg%i", prtype->ToString().CString(), cparam);
  73. }
  74. else if (ptype->type_->asEnumType())
  75. {
  76. JSBEnumType* etype = ptype->type_->asEnumType();
  77. sig.AppendWithFormat("%s __arg%i", etype->enum_->GetName().CString(), cparam);
  78. }
  79. else if (ptype->type_->asVectorType())
  80. {
  81. // read only vector arguments
  82. if (ptype->isConst_)
  83. {
  84. JSBVectorType* vtype = ptype->type_->asVectorType();
  85. sig.AppendWithFormat("%s __arg%i", vtype->ToString().CString(), cparam);
  86. }
  87. }
  88. else
  89. {
  90. assert(0);
  91. }
  92. sig += ", ";
  93. }
  94. }
  95. if (sig.EndsWith(", "))
  96. sig = sig.Substring(0, sig.Length() - 2);
  97. }
  98. void CSFunctionWriter::WriteFunction(String& source)
  99. {
  100. JSBClass* klass = function_->class_;
  101. String sig;
  102. GenNativeFunctionSignature(sig);
  103. JSBFunctionType* returnType = function_->returnType_;
  104. String rTypeString = "void";
  105. // if the marshalling local variable type is differnt
  106. // for example SharedPtr ->Object *
  107. String rMarshalTypeString;
  108. if (returnType)
  109. {
  110. if (returnType->type_->asStringType())
  111. {
  112. rTypeString = "String";
  113. }
  114. else if (returnType->type_->asPrimitiveType())
  115. {
  116. JSBPrimitiveType* prtype = returnType->type_->asPrimitiveType();
  117. rTypeString = prtype->ToString();
  118. }
  119. else if (returnType->type_->asClassType())
  120. {
  121. JSBClassType* klassType = returnType->type_->asClassType();
  122. if (returnType->isTemplate_)
  123. {
  124. if (klassType->class_->IsObject())
  125. rTypeString = "const Object*";
  126. else
  127. rTypeString = "const RefCounted*";
  128. rMarshalTypeString.AppendWithFormat("SharedPtr<%s>", klassType->class_->GetNativeName().CString());
  129. }
  130. else if (klassType->class_->IsObject())
  131. {
  132. rTypeString = "const Object*";
  133. }
  134. else if (klassType->class_->IsNumberArray())
  135. {
  136. rTypeString = klassType->class_->GetName().CString();
  137. }
  138. else
  139. {
  140. rTypeString = "const RefCounted*";
  141. }
  142. }
  143. else if (returnType->type_->asEnumType())
  144. {
  145. JSBEnumType* enumType = returnType->type_->asEnumType();
  146. rTypeString = enumType->enum_->GetName().CString();
  147. }
  148. else if (returnType->type_->asVectorType())
  149. {
  150. JSBVectorType* vtype = returnType->type_->asVectorType();
  151. rTypeString = "";
  152. rTypeString.AppendWithFormat("%s", vtype->ToString().CString());
  153. }
  154. }
  155. source.AppendWithFormat("%s csb_%s_%s(%s* self%s)\n{\n", rTypeString == "String" ? "const char*" : rTypeString.CString(), klass->GetName().CString(),
  156. function_->name_.CString(), klass->GetNativeName().CString(), sig.Length() ? (", " + sig).CString() : "");
  157. if (rTypeString != "void")
  158. {
  159. source.AppendWithFormat("%s retValue = ", rMarshalTypeString.Length()? rMarshalTypeString.CString() : rTypeString.CString());
  160. }
  161. // call
  162. source.AppendWithFormat("self->%s(", function_->name_.CString());
  163. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  164. for (unsigned int i = 0; i < parameters.Size(); i++)
  165. {
  166. source.AppendWithFormat("__arg%i", i);
  167. if (i != parameters.Size() - 1)
  168. {
  169. source += ", ";
  170. }
  171. }
  172. source += ");\n";
  173. if (rTypeString != "void")
  174. {
  175. if (rTypeString == "String")
  176. source.AppendWithFormat("\nreturn retValue.CString();\n");
  177. else
  178. source.AppendWithFormat("\nreturn retValue;\n");
  179. }
  180. source.AppendWithFormat("}\n\n");
  181. }
  182. void CSFunctionWriter::GenerateSource(String& sourceOut)
  183. {
  184. String source = "";
  185. if (function_->IsConstructor())
  186. {
  187. WriteConstructor(source);
  188. }
  189. else
  190. {
  191. WriteFunction(source);
  192. }
  193. sourceOut += source;
  194. }
  195. }