CSFunctionWriter.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. }
  85. void CSFunctionWriter::GenNativeCallParameters(String& sig)
  86. {
  87. JSBClass* klass = function_->GetClass();
  88. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  89. Vector<String> args;
  90. if (parameters.Size())
  91. {
  92. for (unsigned int i = 0; i < parameters.Size(); i++)
  93. {
  94. JSBFunctionType* ptype = parameters.At(i);
  95. // ignore "Context" parameters
  96. if (ptype->type_->asClassType())
  97. {
  98. JSBClassType* classType = ptype->type_->asClassType();
  99. JSBClass* klass = classType->class_;
  100. if (klass->GetName() == "Context")
  101. {
  102. continue;
  103. }
  104. if (klass->IsNumberArray())
  105. args.Push(ToString("*%s", ptype->name_.CString()));
  106. else
  107. args.Push(ToString("%s", ptype->name_.CString()));
  108. }
  109. else
  110. {
  111. args.Push(ToString("%s", ptype->name_.CString()));
  112. }
  113. }
  114. }
  115. sig.Join(args, ", ");
  116. }
  117. void CSFunctionWriter::GenNativeFunctionSignature(String& sig)
  118. {
  119. JSBClass* klass = function_->GetClass();
  120. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  121. Vector<String> args;
  122. if (!function_->IsConstructor())
  123. {
  124. args.Push(ToString("%s* self", klass->GetNativeName().CString()));
  125. }
  126. if (parameters.Size())
  127. {
  128. for (unsigned int i = 0; i < parameters.Size(); i++)
  129. {
  130. JSBFunctionType* ptype = parameters.At(i);
  131. // ignore "Context" parameters
  132. if (ptype->type_->asClassType())
  133. {
  134. JSBClassType* classType = ptype->type_->asClassType();
  135. JSBClass* klass = classType->class_;
  136. if (klass->GetName() == "Context")
  137. {
  138. continue;
  139. }
  140. args.Push(ToString("%s* %s", klass->GetNativeName().CString(), ptype->name_.CString()));
  141. }
  142. else
  143. {
  144. args.Push(CSTypeHelper::GetNativeTypeString(ptype) + " " + ptype->name_);
  145. }
  146. }
  147. }
  148. if (function_->GetReturnClass() && function_->GetReturnClass()->IsNumberArray())
  149. {
  150. args.Push(ToString("%s* returnValue", function_->GetReturnClass()->GetNativeName().CString()));
  151. }
  152. sig.Join(args, ", ");
  153. }
  154. void CSFunctionWriter::WriteNativeFunction(String& source)
  155. {
  156. JSBClass* klass = function_->GetClass();
  157. JSBPackage* package = klass->GetPackage();
  158. String fname = function_->IsConstructor() ? "Constructor" : function_->GetName();
  159. String returnType = "void";
  160. bool simpleReturn = true;
  161. if (function_->IsConstructor())
  162. {
  163. returnType = "RefCounted*";
  164. }
  165. else if (function_->GetReturnType())
  166. {
  167. if (function_->IsConstructor())
  168. {
  169. returnType = ToString("%s*", klass->GetNativeName().CString());
  170. }
  171. else if (function_->GetReturnClass())
  172. {
  173. if (!function_->GetReturnClass()->IsNumberArray())
  174. {
  175. returnType = ToString("const %s*", function_->GetReturnClass()->GetNativeName().CString());
  176. }
  177. }
  178. else if (function_->GetReturnType()->type_->asStringHashType())
  179. {
  180. returnType = "unsigned";
  181. }
  182. else
  183. {
  184. returnType = ToString("%s", CSTypeHelper::GetNativeTypeString(function_->GetReturnType()).CString());
  185. }
  186. }
  187. String line;
  188. String sig;
  189. GenNativeFunctionSignature(sig);
  190. line = ToString("%s csb_%s_%s_%s(%s)\n",
  191. returnType.CString(), package->GetName().CString(), klass->GetName().CString(),
  192. fname.CString(), sig.CString());
  193. source += IndentLine(line);
  194. source += IndentLine("{\n");
  195. Indent();
  196. bool returnValue = false;
  197. String returnStatement;
  198. if (returnType == "const char*")
  199. {
  200. returnValue = true;
  201. source += IndentLine("static String returnValue;\n");
  202. returnStatement = "returnValue = ";
  203. }
  204. else if (function_->GetReturnClass() && function_->GetReturnClass()->IsNumberArray())
  205. {
  206. returnStatement = "*returnValue = ";
  207. }
  208. else
  209. {
  210. if (returnType != "void")
  211. {
  212. if (simpleReturn)
  213. returnStatement = "return ";
  214. }
  215. }
  216. String callSig;
  217. GenNativeCallParameters(callSig);
  218. if (!function_->isConstructor_)
  219. line = ToString("%sself->%s(%s);\n", returnStatement.CString(), function_->GetName().CString(), callSig.CString());
  220. else
  221. {
  222. if (klass->IsAbstract())
  223. {
  224. line = "return 0; // Abstract Class\n";
  225. }
  226. else if (klass->IsObject())
  227. {
  228. if (callSig.Length())
  229. line = ToString("return new %s(AtomicSharp::GetContext(), %s);\n", klass->GetNativeName().CString(), callSig.CString());
  230. else
  231. line = ToString("return new %s(AtomicSharp::GetContext());\n", klass->GetNativeName().CString());
  232. }
  233. else
  234. {
  235. line = ToString("return new %s(%s);\n", klass->GetNativeName().CString(), callSig.CString());
  236. }
  237. }
  238. source += IndentLine(line);
  239. if (returnType == "const char*")
  240. {
  241. source += IndentLine("return returnValue.CString();\n");
  242. }
  243. Dedent();
  244. source += IndentLine("}\n");
  245. source += "\n";
  246. }
  247. void CSFunctionWriter::GenerateNativeSource(String& sourceOut)
  248. {
  249. String source = "";
  250. WriteNativeFunction(source);
  251. sourceOut += source;
  252. }
  253. // MANAGED----------------------------------------------------------------------------------------
  254. void CSFunctionWriter::WriteManagedPInvokeFunctionSignature(String& source)
  255. {
  256. source += "\n";
  257. String line = "[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
  258. source += IndentLine(line);
  259. JSBClass* klass = function_->GetClass();
  260. JSBPackage* package = klass->GetPackage();
  261. String returnType = CSTypeHelper::GetPInvokeTypeString(function_->GetReturnType());
  262. if (function_->IsConstructor())
  263. returnType = "IntPtr";
  264. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  265. Vector<String> args;
  266. if (!function_->IsConstructor())
  267. {
  268. args.Push("IntPtr self");
  269. }
  270. if (parameters.Size())
  271. {
  272. for (unsigned int i = 0; i < parameters.Size(); i++)
  273. {
  274. JSBFunctionType* ptype = parameters.At(i);
  275. String name = ptype->name_;
  276. if (name == "object")
  277. name = "_object";
  278. // ignore "Context" parameters
  279. if (ptype->type_->asClassType())
  280. {
  281. JSBClassType* classType = ptype->type_->asClassType();
  282. JSBClass* klass = classType->class_;
  283. if (klass->GetName() == "Context")
  284. {
  285. continue;
  286. }
  287. if (klass->IsNumberArray())
  288. {
  289. args.Push("ref " + klass->GetName() + " " + name);
  290. }
  291. else
  292. {
  293. args.Push("IntPtr " + name);
  294. }
  295. }
  296. else
  297. {
  298. args.Push(CSTypeHelper::GetPInvokeTypeString(ptype) + " " + name);
  299. }
  300. }
  301. }
  302. if (function_->GetReturnClass())
  303. {
  304. JSBClass* retClass = function_->GetReturnClass();
  305. if (retClass->IsNumberArray())
  306. {
  307. args.Push("ref " + retClass->GetName() + " retValue");
  308. }
  309. }
  310. String pstring;
  311. pstring.Join(args, ", ");
  312. String fname = function_->IsConstructor() ? "Constructor" : function_->GetName();
  313. line = ToString("private static extern %s csb_%s_%s_%s(%s);\n",
  314. returnType.CString(), package->GetName().CString(), klass->GetName().CString(),
  315. fname.CString(), pstring.CString());
  316. source += IndentLine(line);
  317. source += "\n";
  318. }
  319. void CSFunctionWriter::GenManagedFunctionParameters(String& sig)
  320. {
  321. // generate args
  322. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  323. if (parameters.Size())
  324. {
  325. for (unsigned int i = 0; i < parameters.Size(); i++)
  326. {
  327. JSBFunctionType* ptype = parameters.At(i);
  328. // ignore "Context" parameters
  329. if (ptype->type_->asClassType())
  330. {
  331. JSBClassType* classType = ptype->type_->asClassType();
  332. JSBClass* klass = classType->class_;
  333. if (klass->GetName() == "Context")
  334. {
  335. continue;
  336. }
  337. }
  338. sig += CSTypeHelper::GetManagedTypeString(ptype);
  339. // hack for Drawable as this causes a compilation error (need to add default params)
  340. if (sig.EndsWith("drawableFlags"))
  341. sig += " = '\\0'";
  342. if (i + 1 != parameters.Size())
  343. sig += ", ";
  344. }
  345. }
  346. }
  347. void CSFunctionWriter::WriteManagedConstructor(String& source)
  348. {
  349. JSBClass* klass = function_->GetClass();
  350. JSBPackage* package = klass->GetPackage();
  351. // wrapping constructor
  352. String line;
  353. if (klass->GetName() != "RefCounted")
  354. {
  355. line = ToString("public %s (IntPtr native) : base (native)\n", klass->GetName().CString());
  356. source += IndentLine(line);
  357. source += IndentLine("{\n");
  358. source += IndentLine("}\n\n");
  359. }
  360. String sig;
  361. GenManagedFunctionParameters(sig);
  362. line = ToString("public %s (%s)\n", klass->GetName().CString(), sig.CString());
  363. source += IndentLine(line);
  364. source += IndentLine("{\n");
  365. Indent();
  366. source += IndentLine(ToString("if (typeof(%s) == this.GetType())\n", klass->GetName().CString()));
  367. source += IndentLine("{\n");
  368. Indent();
  369. String callSig;
  370. GenPInvokeCallParameters(callSig);
  371. line = ToString("nativeInstance = NativeCore.RegisterNative (csb_%s_%s_Constructor(%s), this);\n",
  372. package->GetName().CString(), klass->GetName().CString(), callSig.CString());
  373. source += IndentLine(line);
  374. Dedent();
  375. source += IndentLine("}\n");
  376. Dedent();
  377. source += IndentLine("}\n");
  378. }
  379. void CSFunctionWriter::GenPInvokeCallParameters(String& sig)
  380. {
  381. // generate args
  382. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  383. if (parameters.Size())
  384. {
  385. for (unsigned int i = 0; i < parameters.Size(); i++)
  386. {
  387. JSBFunctionType* ptype = parameters.At(i);
  388. // ignore "Context" parameters
  389. if (ptype->type_->asClassType())
  390. {
  391. JSBClassType* classType = ptype->type_->asClassType();
  392. JSBClass* klass = classType->class_;
  393. if (klass->GetName() == "Context")
  394. {
  395. continue;
  396. }
  397. }
  398. String name = ptype->name_;
  399. if (name == "object")
  400. name = "_object";
  401. if (ptype->type_->asClassType())
  402. {
  403. JSBClass* pclass = ptype->type_->asClassType()->class_;
  404. if (pclass->IsNumberArray())
  405. {
  406. sig += "ref " + name;
  407. }
  408. else
  409. {
  410. sig += name + " == null ? IntPtr.Zero : " + name + ".nativeInstance";
  411. }
  412. }
  413. else
  414. {
  415. sig += name;
  416. }
  417. if (i + 1 != parameters.Size())
  418. sig += ", ";
  419. }
  420. }
  421. // data marshaller
  422. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  423. {
  424. if (function_->GetReturnClass()->IsNumberArray())
  425. {
  426. if (sig.Length())
  427. sig += ", ";
  428. JSBClass* klass = function_->GetClass();
  429. sig += ToString("ref %s%sReturnValue", klass->GetName().CString(), function_->GetName().CString());
  430. }
  431. }
  432. }
  433. void CSFunctionWriter::WriteManagedFunction(String& source)
  434. {
  435. JSBClass* klass = function_->GetClass();
  436. JSBPackage* package = klass->GetPackage();
  437. String sig;
  438. String returnType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  439. GenManagedFunctionParameters(sig);
  440. String line = ToString("public %s %s (%s)\n", returnType.CString(), function_->GetName().CString(), sig.CString());
  441. source += IndentLine(line);
  442. source += IndentLine("{\n");
  443. Indent();
  444. line.Clear();
  445. if (function_->GetReturnType())
  446. {
  447. if (CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  448. line += "return ";
  449. else
  450. {
  451. if (function_->GetReturnClass())
  452. {
  453. if (!function_->GetReturnClass()->IsNumberArray())
  454. line += "IntPtr retNativeInstance = ";
  455. }
  456. }
  457. }
  458. String callSig;
  459. GenPInvokeCallParameters(callSig);
  460. line += ToString("csb_%s_%s_%s(nativeInstance",
  461. package->GetName().CString(), klass->GetName().CString(), function_->GetName().CString());
  462. if (callSig.Length())
  463. {
  464. line += ", " + callSig;
  465. }
  466. line += ");\n";
  467. source += IndentLine(line);
  468. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  469. {
  470. if (function_->GetReturnType()->type_->asClassType())
  471. {
  472. JSBClass* retClass = function_->GetReturnClass();
  473. JSBClass* klass = function_->GetClass();
  474. if (retClass->IsNumberArray())
  475. {
  476. line = ToString("return %s%sReturnValue;", klass->GetName().CString(), function_->GetName().CString());
  477. }
  478. else
  479. {
  480. line = ToString("return retNativeInstance == IntPtr.Zero ? null : NativeCore.WrapNative<%s> (retNativeInstance);", retClass->GetName().CString());
  481. }
  482. source += IndentLine(line);
  483. }
  484. }
  485. source+= "\n";
  486. Dedent();
  487. source += IndentLine("}\n");
  488. }
  489. void CSFunctionWriter::GenerateManagedSource(String& sourceOut)
  490. {
  491. String source = "";
  492. Indent();
  493. Indent();
  494. Indent();
  495. if (function_->IsConstructor())
  496. WriteManagedConstructor(source);
  497. else
  498. WriteManagedFunction(source);
  499. WriteManagedPInvokeFunctionSignature(source);
  500. // data marshaller
  501. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  502. {
  503. if (function_->GetReturnClass())
  504. {
  505. JSBClass* retClass = function_->GetReturnClass();
  506. if (retClass->IsNumberArray())
  507. {
  508. JSBClass* klass = function_->GetClass();
  509. String managedType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  510. String marshal = "private " + managedType + " ";
  511. marshal += ToString("%s%sReturnValue = new %s();\n", klass->GetName().CString(), function_->GetName().CString(), managedType.CString());
  512. sourceOut += IndentLine(marshal);
  513. }
  514. }
  515. }
  516. Dedent();
  517. Dedent();
  518. Dedent();
  519. sourceOut += source;
  520. }
  521. void CSFunctionWriter::GenerateSource(String& sourceOut)
  522. {
  523. }
  524. }