CSFunctionWriter.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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 (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
  272. source += IndentLine(line);
  273. JSBClass* klass = function_->GetClass();
  274. JSBPackage* package = klass->GetPackage();
  275. String returnType = CSTypeHelper::GetNativeTypeString(function_->GetReturnType());
  276. if (function_->IsConstructor())
  277. returnType = "IntPtr";
  278. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  279. Vector<String> args;
  280. if (!function_->IsConstructor())
  281. {
  282. args.Push("IntPtr self");
  283. }
  284. if (parameters.Size())
  285. {
  286. for (unsigned int i = 0; i < parameters.Size(); i++)
  287. {
  288. JSBFunctionType* ptype = parameters.At(i);
  289. // ignore "Context" parameters
  290. if (ptype->type_->asClassType())
  291. {
  292. JSBClassType* classType = ptype->type_->asClassType();
  293. JSBClass* klass = classType->class_;
  294. if (klass->GetName() == "Context")
  295. {
  296. continue;
  297. }
  298. if (klass->IsNumberArray())
  299. {
  300. args.Push("ref " + klass->GetName() + " " + ptype->name_);
  301. }
  302. else
  303. {
  304. args.Push("IntPtr " + ptype->name_);
  305. }
  306. }
  307. else
  308. {
  309. args.Push(CSTypeHelper::GetNativeTypeString(ptype) + " " + ptype->name_);
  310. }
  311. }
  312. }
  313. if (function_->GetReturnType())
  314. {
  315. if (function_->GetReturnType()->type_->asClassType())
  316. {
  317. JSBClass* retClass = function_->GetReturnType()->type_->asClassType()->class_;
  318. if (retClass->IsNumberArray())
  319. {
  320. args.Push("ref " + retClass->GetName() + " retValue");
  321. }
  322. }
  323. }
  324. String pstring;
  325. pstring.Join(args, ", ");
  326. String fname = function_->IsConstructor() ? "Constructor" : function_->GetName();
  327. line = ToString("private static extern %s csb_%s_%s_%s(%s);\n",
  328. returnType.CString(), package->GetName().CString(), klass->GetName().CString(),
  329. fname.CString(), pstring.CString());
  330. source += IndentLine(line);
  331. source += "\n";
  332. }
  333. void CSFunctionWriter::GenManagedFunctionParameters(String& sig)
  334. {
  335. // generate args
  336. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  337. if (parameters.Size())
  338. {
  339. for (unsigned int i = 0; i < parameters.Size(); i++)
  340. {
  341. JSBFunctionType* ptype = parameters.At(i);
  342. // ignore "Context" parameters
  343. if (ptype->type_->asClassType())
  344. {
  345. JSBClassType* classType = ptype->type_->asClassType();
  346. JSBClass* klass = classType->class_;
  347. if (klass->GetName() == "Context")
  348. {
  349. continue;
  350. }
  351. }
  352. sig += CSTypeHelper::GetManagedTypeString(ptype);
  353. if (i + 1 != parameters.Size())
  354. sig += ", ";
  355. }
  356. }
  357. }
  358. void CSFunctionWriter::WriteManagedConstructor(String& source)
  359. {
  360. JSBClass* klass = function_->GetClass();
  361. JSBPackage* package = klass->GetPackage();
  362. // wrapping constructor
  363. String line;
  364. if (klass->GetName() != "RefCounted")
  365. {
  366. line = ToString("public %s (IntPtr native) : base (native)\n", klass->GetName().CString());
  367. source += IndentLine(line);
  368. source += IndentLine("{\n");
  369. source += IndentLine("}\n\n");
  370. }
  371. String sig;
  372. GenManagedFunctionParameters(sig);
  373. line = ToString("public %s (%s)\n", klass->GetName().CString(), sig.CString());
  374. source += IndentLine(line);
  375. source += IndentLine("{\n");
  376. Indent();
  377. source += IndentLine("if (nativeInstance == IntPtr.Zero)");
  378. source += IndentLine("{\n");
  379. Indent();
  380. String callSig;
  381. GenPInvokeCallParameters(callSig);
  382. line = ToString("nativeInstance = NativeCore.RegisterNative (csb_%s_%s_Constructor(%s), this);\n",
  383. package->GetName().CString(), klass->GetName().CString(), callSig.CString());
  384. source += IndentLine(line);
  385. Dedent();
  386. source += IndentLine("}\n");
  387. Dedent();
  388. source += IndentLine("}\n");
  389. }
  390. void CSFunctionWriter::GenPInvokeCallParameters(String& sig)
  391. {
  392. // generate args
  393. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  394. if (parameters.Size())
  395. {
  396. for (unsigned int i = 0; i < parameters.Size(); i++)
  397. {
  398. JSBFunctionType* ptype = parameters.At(i);
  399. // ignore "Context" parameters
  400. if (ptype->type_->asClassType())
  401. {
  402. JSBClassType* classType = ptype->type_->asClassType();
  403. JSBClass* klass = classType->class_;
  404. if (klass->GetName() == "Context")
  405. {
  406. continue;
  407. }
  408. }
  409. if (ptype->type_->asClassType())
  410. {
  411. JSBClass* pclass = ptype->type_->asClassType()->class_;
  412. if (pclass->IsNumberArray())
  413. {
  414. sig += "ref " + ptype->name_;
  415. }
  416. else
  417. {
  418. sig += ptype->name_ + " == null ? IntPtr.Zero : " + ptype->name_+ ".nativeInstance";
  419. }
  420. }
  421. else
  422. {
  423. sig += ptype->name_;
  424. }
  425. if (i + 1 != parameters.Size())
  426. sig += ", ";
  427. }
  428. }
  429. // data marshaller
  430. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  431. {
  432. if (function_->GetReturnType()->type_->asClassType()->class_->IsNumberArray())
  433. {
  434. if (sig.Length())
  435. sig += ", ";
  436. JSBClass* klass = function_->GetClass();
  437. sig += ToString("ref %s%sReturnValue", klass->GetName().CString(), function_->GetName().CString());
  438. }
  439. }
  440. }
  441. void CSFunctionWriter::WriteManagedFunction(String& source)
  442. {
  443. JSBClass* klass = function_->GetClass();
  444. JSBPackage* package = klass->GetPackage();
  445. String sig;
  446. String returnType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  447. GenManagedFunctionParameters(sig);
  448. String line = ToString("public %s %s (%s)\n", returnType.CString(), function_->GetName().CString(), sig.CString());
  449. source += IndentLine(line);
  450. source += IndentLine("{\n");
  451. Indent();
  452. line.Clear();
  453. if (function_->GetReturnType())
  454. {
  455. if (CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  456. line += "return ";
  457. else
  458. {
  459. if (function_->GetReturnType()->type_->asClassType())
  460. {
  461. if (!function_->GetReturnType()->type_->asClassType()->class_->IsNumberArray())
  462. line += "IntPtr retNativeInstance = ";
  463. }
  464. }
  465. }
  466. String callSig;
  467. GenPInvokeCallParameters(callSig);
  468. line += ToString("csb_%s_%s_%s(nativeInstance",
  469. package->GetName().CString(), klass->GetName().CString(), function_->GetName().CString());
  470. if (callSig.Length())
  471. {
  472. line += ", " + callSig;
  473. }
  474. line += ");\n";
  475. source += IndentLine(line);
  476. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  477. {
  478. if (function_->GetReturnType()->type_->asClassType())
  479. {
  480. JSBClass* retClass = function_->GetReturnType()->type_->asClassType()->class_;
  481. JSBClass* klass = function_->GetClass();
  482. if (retClass->IsNumberArray())
  483. {
  484. line = ToString("return %s%sReturnValue;", klass->GetName().CString(), function_->GetName().CString());
  485. }
  486. else
  487. {
  488. line = ToString("return retNativeInstance == IntPtr.Zero ? null : NativeCore.WrapNative<%s> (retNativeInstance);", retClass->GetName().CString());
  489. }
  490. source += IndentLine(line);
  491. }
  492. }
  493. source+= "\n";
  494. Dedent();
  495. source += IndentLine("}\n");
  496. }
  497. void CSFunctionWriter::GenerateManagedSource(String& sourceOut)
  498. {
  499. String source = "";
  500. Indent();
  501. Indent();
  502. Indent();
  503. if (function_->IsConstructor())
  504. WriteManagedConstructor(source);
  505. else
  506. WriteManagedFunction(source);
  507. WriteManagedPInvokeFunctionSignature(source);
  508. // data marshaller
  509. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  510. {
  511. if (function_->GetReturnType()->type_->asClassType())
  512. {
  513. JSBClass* retClass = function_->GetReturnType()->type_->asClassType()->class_;
  514. if (retClass->IsNumberArray())
  515. {
  516. JSBClass* klass = function_->GetClass();
  517. String managedType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  518. String marshal = "private " + managedType + " ";
  519. marshal += ToString("%s%sReturnValue = new %s();\n", klass->GetName().CString(), function_->GetName().CString(), managedType.CString());
  520. sourceOut += IndentLine(marshal);
  521. }
  522. }
  523. }
  524. Dedent();
  525. Dedent();
  526. Dedent();
  527. sourceOut += source;
  528. }
  529. void CSFunctionWriter::GenerateSource(String& sourceOut)
  530. {
  531. }
  532. }