CSFunctionWriter.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. /*
  16. *
  17. C# getters/setters
  18. local instance storage so we're not constantly creating managed Vector3, etc
  19. Vector2/Vector3/BoundingBox, etc C# structs so assign by value
  20. Object lifetime
  21. C# enum of module types for type info?
  22. C# version of push class instance?
  23. new instance from C# needs constructor
  24. wrapping does not, wrapping doesn't use constructors at all (JS needs this for prototype)
  25. Store GCHandle to keep an object alive (Component, UI) C# side?
  26. typedef const void* ClassID;
  27. which changed based on address, so need register at startup
  28. so at package startup time, need to setup mapping between
  29. IntPtr and C# class, we also need to be able to new a class
  30. instance with existing native or create a native when new'ing from C#
  31. IntPtr to RefCounted native side is the "ID", like JSHeapPtr
  32. Lifetime:
  33. // you cannot derive from native engine classes, other than script components
  34. a C# instance can be new'd, handed to native, stored in native, the C# side could be GC'd
  35. future access to this instance would be a new instance
  36. */
  37. /*
  38. // struct marshal Vector2, Vector3, BoundingBox, etc
  39. // RefCounted*
  40. // primitive bool, int, uint, float, double
  41. // String
  42. RefCounted* csb_Node_Constructor()
  43. {
  44. return new Node(AtomicSharp::GetContext());
  45. }
  46. void csb_Node_GetPosition(Node* self, Vector3* out)
  47. {
  48. *out = self->GetPosition();
  49. }
  50. void csb_Node_SetPosition(Node* self, Vector3*__arg0)
  51. {
  52. self->SetPosition(*__arg0);
  53. }
  54. void csb_Node_SetPosition(Node* self, Vector3*__arg0)
  55. {
  56. self->SetPosition(*__arg0);
  57. }
  58. bool csb_Audio_Play(Audio* self)
  59. {
  60. bool retValue = self->Play();
  61. return retValue;
  62. }
  63. const RefCounted* csb_Node_GetParent(Node* self)
  64. {
  65. const RefCounted* retValue = self->GetParent();
  66. return RefCounted;
  67. }
  68. RefCounted* csb_ObjectAnimation_Constructor()
  69. {
  70. return new ObjectAnimation(AtomicSharp::GetContext());
  71. }
  72. */
  73. namespace ToolCore
  74. {
  75. CSFunctionWriter::CSFunctionWriter(JSBFunction *function) : JSBFunctionWriter(function)
  76. {
  77. }
  78. void CSFunctionWriter::WriteParameterMarshal(String& source)
  79. {
  80. }
  81. void CSFunctionWriter::WriteConstructor(String& source)
  82. {
  83. JSBClass* klass = function_->class_;
  84. if (klass->IsAbstract())
  85. return;
  86. // just object for now, as constructor takes a context
  87. if (!klass->IsObject())
  88. return;
  89. // more than context arg, don't marshal yet
  90. if (function_->GetParameters().Size() > 1)
  91. {
  92. return;
  93. }
  94. source.AppendWithFormat("RefCounted* csb_%s_Constructor()\n{\nreturn new %s(AtomicSharp::GetContext());\n}\n",
  95. klass->GetName().CString(), klass->GetNativeName().CString());
  96. }
  97. void CSFunctionWriter::GenNativeFunctionSignature(String& sig)
  98. {
  99. // generate args
  100. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  101. int cparam = 0;
  102. if (parameters.Size())
  103. {
  104. for (unsigned int i = 0; i < parameters.Size(); i++, cparam++)
  105. {
  106. JSBFunctionType * ptype = parameters.At(i);
  107. // ignore "Context" parameters
  108. if (ptype->type_->asClassType())
  109. {
  110. JSBClassType* classType = ptype->type_->asClassType();
  111. JSBClass* klass = classType->class_;
  112. if (klass->GetName() == "Context")
  113. {
  114. cparam--;
  115. continue;
  116. }
  117. }
  118. String pstring = ptype->ToArgString(cparam);
  119. if (ptype->type_->asClassType())
  120. {
  121. JSBClassType* classType = ptype->type_->asClassType();
  122. JSBClass* klass = classType->class_;
  123. if (!klass->IsNumberArray())
  124. {
  125. sig.AppendWithFormat("%s", pstring.CString());
  126. }
  127. else
  128. {
  129. sig.AppendWithFormat("%s __arg%i", klass->GetNativeName().CString(), cparam);
  130. }
  131. }
  132. else if (ptype->type_->asStringType() || ptype->type_->asStringHashType())
  133. {
  134. sig.AppendWithFormat("char* __arg%i", cparam);
  135. }
  136. else if (ptype->type_->asHeapPtrType())
  137. {
  138. assert(0);
  139. }
  140. else if (ptype->type_->asPrimitiveType())
  141. {
  142. JSBPrimitiveType* prtype = ptype->type_->asPrimitiveType();
  143. sig.AppendWithFormat("%s __arg%i", prtype->ToString().CString(), cparam);
  144. }
  145. else if (ptype->type_->asEnumType())
  146. {
  147. JSBEnumType* etype = ptype->type_->asEnumType();
  148. sig.AppendWithFormat("%s __arg%i", etype->enum_->GetName().CString(), cparam);
  149. }
  150. else if (ptype->type_->asVectorType())
  151. {
  152. // read only vector arguments
  153. if (ptype->isConst_)
  154. {
  155. JSBVectorType* vtype = ptype->type_->asVectorType();
  156. sig.AppendWithFormat("%s __arg%i", vtype->ToString().CString(), cparam);
  157. }
  158. }
  159. else
  160. {
  161. assert(0);
  162. }
  163. sig += ", ";
  164. }
  165. }
  166. if (sig.EndsWith(", "))
  167. sig = sig.Substring(0, sig.Length() - 2);
  168. }
  169. void CSFunctionWriter::WriteFunction(String& source)
  170. {
  171. JSBClass* klass = function_->class_;
  172. String sig;
  173. GenNativeFunctionSignature(sig);
  174. JSBFunctionType* returnType = function_->returnType_;
  175. String rTypeString = "void";
  176. // if the marshalling local variable type is differnt
  177. // for example SharedPtr ->Object *
  178. String rMarshalTypeString;
  179. if (returnType)
  180. {
  181. if (returnType->type_->asStringType())
  182. {
  183. rTypeString = "const String&";
  184. }
  185. else if (returnType->type_->asPrimitiveType())
  186. {
  187. JSBPrimitiveType* prtype = returnType->type_->asPrimitiveType();
  188. rTypeString = prtype->ToString();
  189. }
  190. else if (returnType->type_->asClassType())
  191. {
  192. JSBClassType* klassType = returnType->type_->asClassType();
  193. if (returnType->isTemplate_)
  194. {
  195. if (klassType->class_->IsObject())
  196. rTypeString = "const Object*";
  197. else
  198. rTypeString = "const RefCounted*";
  199. rMarshalTypeString.AppendWithFormat("SharedPtr<%s>", klassType->class_->GetNativeName().CString());
  200. }
  201. else if (klassType->class_->IsObject())
  202. {
  203. rTypeString = "const Object*";
  204. }
  205. else if (klassType->class_->IsNumberArray())
  206. {
  207. rTypeString = klassType->class_->GetName().CString();
  208. }
  209. else
  210. {
  211. rTypeString = "const RefCounted*";
  212. }
  213. }
  214. else if (returnType->type_->asEnumType())
  215. {
  216. JSBEnumType* enumType = returnType->type_->asEnumType();
  217. rTypeString = enumType->enum_->GetName().CString();
  218. }
  219. else if (returnType->type_->asVectorType())
  220. {
  221. JSBVectorType* vtype = returnType->type_->asVectorType();
  222. rTypeString = "";
  223. rTypeString.AppendWithFormat("%s", vtype->ToString().CString());
  224. }
  225. }
  226. source.AppendWithFormat("%s csb_%s_%s(%s* self%s)\n{\n", rTypeString == "const String&" ? "const char*" : rTypeString.CString(), klass->GetName().CString(),
  227. function_->name_.CString(), klass->GetNativeName().CString(), sig.Length() ? (", " + sig).CString() : "");
  228. if (rTypeString != "void")
  229. {
  230. source.AppendWithFormat("%s retValue = ", rMarshalTypeString.Length()? rMarshalTypeString.CString() : rTypeString.CString());
  231. }
  232. // call
  233. source.AppendWithFormat("self->%s(", function_->name_.CString());
  234. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  235. for (unsigned int i = 0; i < parameters.Size(); i++)
  236. {
  237. source.AppendWithFormat("__arg%i", i);
  238. if (i != parameters.Size() - 1)
  239. {
  240. source += ", ";
  241. }
  242. }
  243. source += ");\n";
  244. if (rTypeString != "void")
  245. {
  246. if (rTypeString == "const String&")
  247. source.AppendWithFormat("\nreturn retValue.CString();\n");
  248. else
  249. source.AppendWithFormat("\nreturn retValue;\n");
  250. }
  251. source.AppendWithFormat("}\n\n");
  252. }
  253. void CSFunctionWriter::GenerateSource(String& sourceOut)
  254. {
  255. String source = "";
  256. if (function_->IsConstructor())
  257. {
  258. WriteConstructor(source);
  259. }
  260. else
  261. {
  262. WriteFunction(source);
  263. }
  264. sourceOut += source;
  265. }
  266. }