CSFunctionWriter.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 "CSFunctionWriter.h"
  16. /*
  17. *
  18. C# getters/setters
  19. local instance storage so we're not constantly creating managed Vector3, etc
  20. Vector2/Vector3/BoundingBox, etc C# structs so assign by value
  21. Object lifetime
  22. C# enum of module types for type info?
  23. C# version of push class instance?
  24. new instance from C# needs constructor
  25. wrapping does not, wrapping doesn't use constructors at all (JS needs this for prototype)
  26. Store GCHandle to keep an object alive (Component, UI) C# side?
  27. typedef const void* ClassID;
  28. which changed based on address, so need register at startup
  29. so at package startup time, need to setup mapping between
  30. IntPtr and C# class, we also need to be able to new a class
  31. instance with existing native or create a native when new'ing from C#
  32. IntPtr to RefCounted native side is the "ID", like JSHeapPtr
  33. Lifetime:
  34. // you cannot derive from native engine classes, other than script components
  35. a C# instance can be new'd, handed to native, stored in native, the C# side could be GC'd
  36. future access to this instance would be a new instance
  37. */
  38. /*
  39. // struct marshal Vector2, Vector3, BoundingBox, etc
  40. // RefCounted*
  41. // primitive bool, int, uint, float, double
  42. // String
  43. RefCounted* csb_Node_Constructor()
  44. {
  45. return new Node(AtomicSharp::GetContext());
  46. }
  47. void csb_Node_GetPosition(Node* self, Vector3* out)
  48. {
  49. *out = self->GetPosition();
  50. }
  51. void csb_Node_SetPosition(Node* self, Vector3*__arg0)
  52. {
  53. self->SetPosition(*__arg0);
  54. }
  55. void csb_Node_SetPosition(Node* self, Vector3*__arg0)
  56. {
  57. self->SetPosition(*__arg0);
  58. }
  59. bool csb_Audio_Play(Audio* self)
  60. {
  61. bool retValue = self->Play();
  62. return retValue;
  63. }
  64. const RefCounted* csb_Node_GetParent(Node* self)
  65. {
  66. const RefCounted* retValue = self->GetParent();
  67. return RefCounted;
  68. }
  69. RefCounted* csb_ObjectAnimation_Constructor()
  70. {
  71. return new ObjectAnimation(AtomicSharp::GetContext());
  72. }
  73. */
  74. namespace ToolCore
  75. {
  76. CSFunctionWriter::CSFunctionWriter(JSBFunction *function) : JSBFunctionWriter(function)
  77. {
  78. }
  79. void CSFunctionWriter::WriteNativeParameterMarshal(String& source)
  80. {
  81. }
  82. void CSFunctionWriter::WriteNativeConstructor(String& source)
  83. {
  84. JSBClass* klass = function_->class_;
  85. if (klass->IsAbstract())
  86. return;
  87. // just object for now, as constructor takes a context
  88. if (!klass->IsObject())
  89. return;
  90. // more than context arg, don't marshal yet
  91. if (function_->GetParameters().Size() > 1)
  92. {
  93. return;
  94. }
  95. source.AppendWithFormat("RefCounted* csb_%s_Constructor()\n{\nreturn new %s(AtomicSharp::GetContext());\n}\n",
  96. klass->GetName().CString(), klass->GetNativeName().CString());
  97. }
  98. void CSFunctionWriter::GenNativeFunctionSignature(String& sig)
  99. {
  100. // generate args
  101. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  102. int cparam = 0;
  103. if (parameters.Size())
  104. {
  105. for (unsigned int i = 0; i < parameters.Size(); i++, cparam++)
  106. {
  107. JSBFunctionType * ptype = parameters.At(i);
  108. // ignore "Context" parameters
  109. if (ptype->type_->asClassType())
  110. {
  111. JSBClassType* classType = ptype->type_->asClassType();
  112. JSBClass* klass = classType->class_;
  113. if (klass->GetName() == "Context")
  114. {
  115. cparam--;
  116. continue;
  117. }
  118. }
  119. String pstring = ptype->ToArgString(cparam);
  120. if (ptype->type_->asClassType())
  121. {
  122. JSBClassType* classType = ptype->type_->asClassType();
  123. JSBClass* klass = classType->class_;
  124. if (!klass->IsNumberArray())
  125. {
  126. sig.AppendWithFormat("%s", pstring.CString());
  127. }
  128. else
  129. {
  130. sig.AppendWithFormat("%s __arg%i", klass->GetNativeName().CString(), cparam);
  131. }
  132. }
  133. else if (ptype->type_->asStringType() || ptype->type_->asStringHashType())
  134. {
  135. sig.AppendWithFormat("char* __arg%i", cparam);
  136. }
  137. else if (ptype->type_->asHeapPtrType())
  138. {
  139. assert(0);
  140. }
  141. else if (ptype->type_->asPrimitiveType())
  142. {
  143. JSBPrimitiveType* prtype = ptype->type_->asPrimitiveType();
  144. sig.AppendWithFormat("%s __arg%i", prtype->ToString().CString(), cparam);
  145. }
  146. else if (ptype->type_->asEnumType())
  147. {
  148. JSBEnumType* etype = ptype->type_->asEnumType();
  149. sig.AppendWithFormat("%s __arg%i", etype->enum_->GetName().CString(), cparam);
  150. }
  151. else if (ptype->type_->asVectorType())
  152. {
  153. // read only vector arguments
  154. if (ptype->isConst_)
  155. {
  156. JSBVectorType* vtype = ptype->type_->asVectorType();
  157. sig.AppendWithFormat("%s __arg%i", vtype->ToString().CString(), cparam);
  158. }
  159. }
  160. else
  161. {
  162. assert(0);
  163. }
  164. sig += ", ";
  165. }
  166. }
  167. if (sig.EndsWith(", "))
  168. sig = sig.Substring(0, sig.Length() - 2);
  169. }
  170. void CSFunctionWriter::WriteNativeFunction(String& source)
  171. {
  172. JSBClass* klass = function_->class_;
  173. String sig;
  174. GenNativeFunctionSignature(sig);
  175. JSBFunctionType* returnType = function_->returnType_;
  176. String rTypeString = "void";
  177. // if the marshalling local variable type is differnt
  178. // for example SharedPtr ->Object *
  179. String rMarshalTypeString;
  180. if (returnType)
  181. {
  182. if (returnType->type_->asStringType())
  183. {
  184. rTypeString = "const String&";
  185. }
  186. else if (returnType->type_->asPrimitiveType())
  187. {
  188. JSBPrimitiveType* prtype = returnType->type_->asPrimitiveType();
  189. rTypeString = prtype->ToString();
  190. }
  191. else if (returnType->type_->asClassType())
  192. {
  193. JSBClassType* klassType = returnType->type_->asClassType();
  194. if (returnType->isTemplate_)
  195. {
  196. if (klassType->class_->IsObject())
  197. rTypeString = "const Object*";
  198. else
  199. rTypeString = "const RefCounted*";
  200. rMarshalTypeString.AppendWithFormat("SharedPtr<%s>", klassType->class_->GetNativeName().CString());
  201. }
  202. else if (klassType->class_->IsObject())
  203. {
  204. rTypeString = "const Object*";
  205. }
  206. else if (klassType->class_->IsNumberArray())
  207. {
  208. rTypeString = klassType->class_->GetName().CString();
  209. }
  210. else
  211. {
  212. rTypeString = "const RefCounted*";
  213. }
  214. }
  215. else if (returnType->type_->asEnumType())
  216. {
  217. JSBEnumType* enumType = returnType->type_->asEnumType();
  218. rTypeString = enumType->enum_->GetName().CString();
  219. }
  220. else if (returnType->type_->asVectorType())
  221. {
  222. JSBVectorType* vtype = returnType->type_->asVectorType();
  223. rTypeString = "";
  224. rTypeString.AppendWithFormat("%s", vtype->ToString().CString());
  225. }
  226. }
  227. source.AppendWithFormat("%s csb_%s_%s(%s* self%s)\n{\n", rTypeString == "const String&" ? "const char*" : rTypeString.CString(), klass->GetName().CString(),
  228. function_->name_.CString(), klass->GetNativeName().CString(), sig.Length() ? (", " + sig).CString() : "");
  229. if (rTypeString != "void")
  230. {
  231. source.AppendWithFormat("%s retValue = ", rMarshalTypeString.Length()? rMarshalTypeString.CString() : rTypeString.CString());
  232. }
  233. // call
  234. source.AppendWithFormat("self->%s(", function_->name_.CString());
  235. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  236. for (unsigned int i = 0; i < parameters.Size(); i++)
  237. {
  238. source.AppendWithFormat("__arg%i", i);
  239. if (i != parameters.Size() - 1)
  240. {
  241. source += ", ";
  242. }
  243. }
  244. source += ");\n";
  245. if (rTypeString != "void")
  246. {
  247. if (rTypeString == "const String&")
  248. source.AppendWithFormat("\nreturn retValue.CString();\n");
  249. else
  250. source.AppendWithFormat("\nreturn retValue;\n");
  251. }
  252. source.AppendWithFormat("}\n\n");
  253. }
  254. void CSFunctionWriter::GenerateNativeSource(String& sourceOut)
  255. {
  256. String source = "";
  257. if (function_->IsConstructor())
  258. {
  259. WriteNativeConstructor(source);
  260. }
  261. else
  262. {
  263. WriteNativeFunction(source);
  264. }
  265. sourceOut += source;
  266. }
  267. // MANAGED----------------------------------------------------------------------------------------
  268. void CSFunctionWriter::WriteManagedPInvokeFunctionSignature(String& source)
  269. {
  270. source += "\n";
  271. String line = "[DllImport (LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
  272. source += IndentLine(line);
  273. JSBClass* klass = function_->GetClass();
  274. JSBPackage* package = klass->GetPackage();
  275. line = ToString("private static extern IntPtr csb_%s_%s_%s(IntPtr self);\n",
  276. package->GetName().CString(), klass->GetName().CString(), function_->GetName().CString());
  277. source += IndentLine(line);
  278. source += "\n";
  279. }
  280. void CSFunctionWriter::WriteManagedPInvokeConstructorSignature(String& source)
  281. {
  282. source += "\n";
  283. String line = "[DllImport (LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
  284. source += IndentLine(line);
  285. JSBClass* klass = function_->GetClass();
  286. JSBPackage* package = klass->GetPackage();
  287. line = ToString("private static extern IntPtr csb_%s_%s_Constructor(IntPtr self);\n",
  288. package->GetName().CString(), klass->GetName().CString());
  289. source += IndentLine(line);
  290. source += "\n";
  291. }
  292. void CSFunctionWriter::GenManagedFunctionParameters(String& sig)
  293. {
  294. // generate args
  295. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  296. if (parameters.Size())
  297. {
  298. for (unsigned int i = 0; i < parameters.Size(); i++)
  299. {
  300. JSBFunctionType* ptype = parameters.At(i);
  301. // ignore "Context" parameters
  302. if (ptype->type_->asClassType())
  303. {
  304. JSBClassType* classType = ptype->type_->asClassType();
  305. JSBClass* klass = classType->class_;
  306. if (klass->GetName() == "Context")
  307. {
  308. continue;
  309. }
  310. }
  311. sig += CSTypeHelper::GetManagedTypeString(ptype);
  312. if (i + 1 != parameters.Size())
  313. sig += ", ";
  314. }
  315. }
  316. }
  317. void CSFunctionWriter::WriteManagedConstructor(String& source)
  318. {
  319. JSBClass* klass = function_->GetClass();
  320. String sig;
  321. GenManagedFunctionParameters(sig);
  322. String line = ToString("public %s (%s)\n", klass->GetName().CString(), sig.CString());
  323. source += IndentLine(line);
  324. source += IndentLine("{");
  325. source+= "\n";
  326. source += IndentLine("}\n");
  327. }
  328. void CSFunctionWriter::GenPInvokeCallParameters(String& sig)
  329. {
  330. // generate args
  331. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  332. if (parameters.Size())
  333. {
  334. for (unsigned int i = 0; i < parameters.Size(); i++)
  335. {
  336. JSBFunctionType* ptype = parameters.At(i);
  337. // ignore "Context" parameters
  338. if (ptype->type_->asClassType())
  339. {
  340. JSBClassType* classType = ptype->type_->asClassType();
  341. JSBClass* klass = classType->class_;
  342. if (klass->GetName() == "Context")
  343. {
  344. continue;
  345. }
  346. }
  347. if (ptype->type_->asClassType())
  348. {
  349. JSBClass* pclass = ptype->type_->asClassType()->class_;
  350. if (pclass->IsNumberArray())
  351. {
  352. sig += "ref " + ptype->name_;
  353. }
  354. else
  355. {
  356. sig += ptype->name_ + " == null ? IntPtr.Zero : " + ptype->name_+ ".nativeInstance";
  357. }
  358. }
  359. else
  360. {
  361. sig += ptype->name_;
  362. }
  363. if (i + 1 != parameters.Size())
  364. sig += ", ";
  365. }
  366. }
  367. // data marshaller
  368. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  369. {
  370. if (function_->GetReturnType()->type_->asClassType()->class_->IsNumberArray())
  371. {
  372. if (sig.Length())
  373. sig += ", ";
  374. JSBClass* klass = function_->GetClass();
  375. sig += ToString("ref %s%sReturnValue", klass->GetName().CString(), function_->GetName().CString());
  376. }
  377. }
  378. }
  379. void CSFunctionWriter::WriteManagedFunction(String& source)
  380. {
  381. JSBClass* klass = function_->GetClass();
  382. JSBPackage* package = klass->GetPackage();
  383. String sig;
  384. String returnType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  385. GenManagedFunctionParameters(sig);
  386. String line = ToString("public %s %s (%s)\n", returnType.CString(), function_->GetName().CString(), sig.CString());
  387. source += IndentLine(line);
  388. source += IndentLine("{\n");
  389. Indent();
  390. line.Clear();
  391. if (function_->GetReturnType())
  392. {
  393. if (CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  394. line += "return ";
  395. else
  396. {
  397. if (function_->GetReturnType()->type_->asClassType())
  398. {
  399. if (!function_->GetReturnType()->type_->asClassType()->class_->IsNumberArray())
  400. line += "IntPtr retNativeInstance = ";
  401. }
  402. }
  403. }
  404. String callSig;
  405. GenPInvokeCallParameters(callSig);
  406. line += ToString("csb_%s_%s_%s(nativeInstance",
  407. package->GetName().CString(), klass->GetName().CString(), function_->GetName().CString());
  408. if (callSig.Length())
  409. {
  410. line += ", " + callSig;
  411. }
  412. line += ");\n";
  413. source += IndentLine(line);
  414. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  415. {
  416. if (function_->GetReturnType()->type_->asClassType())
  417. {
  418. JSBClass* retClass = function_->GetReturnType()->type_->asClassType()->class_;
  419. JSBClass* klass = function_->GetClass();
  420. if (retClass->IsNumberArray())
  421. {
  422. line = ToString("return %s%sReturnValue;", klass->GetName().CString(), function_->GetName().CString());
  423. }
  424. else
  425. {
  426. line = ToString("return retNativeInstance == IntPtr.Zero ? null : NativeCore.WrapNative<%s> (retNativeInstance);", retClass->GetName().CString());
  427. }
  428. source += IndentLine(line);
  429. }
  430. }
  431. source+= "\n";
  432. Dedent();
  433. source += IndentLine("}\n");
  434. }
  435. void CSFunctionWriter::GenerateManagedSource(String& sourceOut)
  436. {
  437. String source = "";
  438. Indent();
  439. Indent();
  440. Indent();
  441. if (function_->IsConstructor())
  442. WriteManagedConstructor(source);
  443. else
  444. WriteManagedFunction(source);
  445. if (function_->IsConstructor())
  446. WriteManagedPInvokeConstructorSignature(source);
  447. else
  448. WriteManagedPInvokeFunctionSignature(source);
  449. // data marshaller
  450. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  451. {
  452. if (function_->GetReturnType()->type_->asClassType())
  453. {
  454. JSBClass* retClass = function_->GetReturnType()->type_->asClassType()->class_;
  455. if (retClass->IsNumberArray())
  456. {
  457. JSBClass* klass = function_->GetClass();
  458. String managedType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  459. String marshal = "private " + managedType + " ";
  460. marshal += ToString("%s%sReturnValue = new %s();\n", klass->GetName().CString(), function_->GetName().CString(), managedType.CString());
  461. sourceOut += IndentLine(marshal);
  462. }
  463. }
  464. }
  465. Dedent();
  466. Dedent();
  467. Dedent();
  468. sourceOut += source;
  469. }
  470. void CSFunctionWriter::GenerateSource(String& sourceOut)
  471. {
  472. }
  473. }