CSFunctionWriter.cpp 18 KB

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