CSFunctionWriter.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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(NETCore::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(NETCore::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::WriteNativeFunction(String& source)
  118. {
  119. JSBClass* klass = function_->GetClass();
  120. JSBPackage* package = klass->GetPackage();
  121. String fname = function_->IsConstructor() ? "Constructor" : function_->GetName();
  122. String returnType;
  123. String functionSig = CSTypeHelper::GetNativeFunctionSignature(function_, returnType);
  124. String line;
  125. line = ToString("ATOMIC_EXPORT_API %s %s\n",
  126. returnType.CString(), functionSig.CString());
  127. source += IndentLine(line);
  128. source += IndentLine("{\n");
  129. Indent();
  130. source += "\n";
  131. bool returnValue = false;
  132. bool sharedPtrReturn = false;
  133. String returnStatement;
  134. if (returnType == "const char*")
  135. {
  136. returnValue = true;
  137. source += IndentLine("static String returnValue;\n");
  138. returnStatement = "returnValue = ";
  139. }
  140. else if (function_->GetReturnClass() && function_->GetReturnClass()->IsNumberArray())
  141. {
  142. returnStatement = "*returnValue = ";
  143. }
  144. else if (function_->GetReturnClass() && function_->GetReturnType()->isSharedPtr_)
  145. {
  146. returnStatement = ToString("SharedPtr<%s> returnValue = ", function_->GetReturnClass()->GetNativeName().CString());
  147. sharedPtrReturn = true;
  148. }
  149. else
  150. {
  151. if (returnType != "void")
  152. {
  153. returnStatement = "return ";
  154. }
  155. }
  156. String callSig;
  157. GenNativeCallParameters(callSig);
  158. if (!function_->isConstructor_)
  159. line = ToString("%sself->%s(%s);\n", returnStatement.CString(), function_->GetName().CString(), callSig.CString());
  160. else
  161. {
  162. if (klass->IsAbstract())
  163. {
  164. line = "return 0; // Abstract Class\n";
  165. }
  166. else if (klass->IsObject())
  167. {
  168. if (callSig.Length())
  169. line = ToString("return new %s(NETCore::GetContext(), %s);\n", klass->GetNativeName().CString(), callSig.CString());
  170. else
  171. line = ToString("return new %s(NETCore::GetContext());\n", klass->GetNativeName().CString());
  172. }
  173. else
  174. {
  175. line = ToString("return new %s(%s);\n", klass->GetNativeName().CString(), callSig.CString());
  176. }
  177. }
  178. source += IndentLine(line);
  179. if (sharedPtrReturn)
  180. {
  181. source += IndentLine("returnValue->AddRef();\n");
  182. source += IndentLine("return returnValue;\n");
  183. }
  184. else if (returnType == "const char*")
  185. {
  186. source += IndentLine("return returnValue.CString();\n");
  187. }
  188. Dedent();
  189. source += IndentLine("}\n");
  190. source += "\n";
  191. }
  192. void CSFunctionWriter::GenerateNativeSource(String& sourceOut)
  193. {
  194. String source = "";
  195. WriteNativeFunction(source);
  196. sourceOut += source;
  197. }
  198. // MANAGED----------------------------------------------------------------------------------------
  199. void CSFunctionWriter::WriteDefaultStructParameters(String& source)
  200. {
  201. for (unsigned i = 0; i < defaultStructParameters_.Size(); i++)
  202. {
  203. const DefaultStructParameter& dparm = defaultStructParameters_[i];
  204. String line = ToString("if (default(%s).Equals(%s)) %s = %s;\n",
  205. dparm.type.CString(), dparm.parameterName.CString(), dparm.parameterName.CString(),
  206. dparm.assignment.CString());
  207. source += IndentLine(line);
  208. }
  209. }
  210. void CSFunctionWriter::WriteManagedPInvokeFunctionSignature(String& source)
  211. {
  212. source += "\n";
  213. String line = "[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
  214. source += IndentLine(line);
  215. JSBClass* klass = function_->GetClass();
  216. JSBPackage* package = klass->GetPackage();
  217. String returnType = CSTypeHelper::GetPInvokeTypeString(function_->GetReturnType());
  218. if (returnType == "string")
  219. returnType = "IntPtr";
  220. if (function_->IsConstructor())
  221. returnType = "IntPtr";
  222. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  223. Vector<String> args;
  224. if (!function_->IsConstructor())
  225. {
  226. args.Push("IntPtr self");
  227. }
  228. if (parameters.Size())
  229. {
  230. for (unsigned int i = 0; i < parameters.Size(); i++)
  231. {
  232. JSBFunctionType* ptype = parameters.At(i);
  233. String name = ptype->name_;
  234. if (name == "object")
  235. name = "_object";
  236. else if (name == "readonly")
  237. name = "readOnly";
  238. else if (name == "params")
  239. name = "parameters";
  240. // ignore "Context" parameters
  241. if (ptype->type_->asClassType())
  242. {
  243. JSBClassType* classType = ptype->type_->asClassType();
  244. JSBClass* klass = classType->class_;
  245. if (klass->GetName() == "Context")
  246. {
  247. continue;
  248. }
  249. if (klass->IsNumberArray())
  250. {
  251. args.Push("ref " + klass->GetName() + " " + name);
  252. }
  253. else
  254. {
  255. args.Push("IntPtr " + name);
  256. }
  257. }
  258. else
  259. {
  260. args.Push(CSTypeHelper::GetPInvokeTypeString(ptype) + " " + name);
  261. }
  262. }
  263. }
  264. if (function_->GetReturnClass())
  265. {
  266. JSBClass* retClass = function_->GetReturnClass();
  267. if (retClass->IsNumberArray())
  268. {
  269. args.Push("ref " + retClass->GetName() + " retValue");
  270. }
  271. }
  272. String pstring;
  273. pstring.Join(args, ", ");
  274. String fname = function_->IsConstructor() ? "Constructor" : function_->GetName();
  275. line = ToString("private static extern %s csb_%s_%s_%s(%s);\n",
  276. returnType.CString(), package->GetName().CString(), klass->GetName().CString(),
  277. fname.CString(), pstring.CString());
  278. source += IndentLine(line);
  279. source += "\n";
  280. }
  281. void CSFunctionWriter::GenManagedFunctionParameters(String& sig)
  282. {
  283. // generate args
  284. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  285. if (parameters.Size())
  286. {
  287. for (unsigned int i = 0; i < parameters.Size(); i++)
  288. {
  289. JSBFunctionType* ptype = parameters.At(i);
  290. // ignore "Context" parameters
  291. if (ptype->type_->asClassType())
  292. {
  293. JSBClassType* classType = ptype->type_->asClassType();
  294. JSBClass* klass = classType->class_;
  295. if (klass->GetName() == "Context")
  296. {
  297. continue;
  298. }
  299. }
  300. sig += CSTypeHelper::GetManagedTypeString(ptype);
  301. String init = ptype->initializer_;
  302. if (init.Length())
  303. {
  304. init = MapDefaultParameter(ptype);
  305. if (init.Length())
  306. sig += " = " + init;
  307. }
  308. if (i + 1 != parameters.Size())
  309. sig += ", ";
  310. }
  311. }
  312. }
  313. void CSFunctionWriter::WriteManagedConstructor(String& source)
  314. {
  315. JSBClass* klass = function_->GetClass();
  316. JSBPackage* package = klass->GetPackage();
  317. if (klass->GetName() == "RefCounted")
  318. return;
  319. // wrapping constructor
  320. String line;
  321. line = ToString("public %s (IntPtr native) : base (native)\n", klass->GetName().CString());
  322. source += IndentLine(line);
  323. source += IndentLine("{\n");
  324. source += IndentLine("}\n\n");
  325. String sig;
  326. GenManagedFunctionParameters(sig);
  327. line = ToString("public %s (%s)\n", klass->GetName().CString(), sig.CString());
  328. source += IndentLine(line);
  329. source += IndentLine("{\n");
  330. Indent();
  331. WriteDefaultStructParameters(source);
  332. line = ToString("if (typeof(%s) == this.GetType()", klass->GetName().CString());
  333. line += ToString(" || (this.GetType().BaseType == typeof(%s) && !NativeCore.GetNativeType(this.GetType())))\n", klass->GetName().CString());
  334. source += IndentLine(line);
  335. source += IndentLine("{\n");
  336. Indent();
  337. String callSig;
  338. GenPInvokeCallParameters(callSig);
  339. line = ToString("nativeInstance = NativeCore.RegisterNative (csb_%s_%s_Constructor(%s), this);\n",
  340. package->GetName().CString(), klass->GetName().CString(), callSig.CString());
  341. source += IndentLine(line);
  342. Dedent();
  343. source += IndentLine("}\n");
  344. Dedent();
  345. source += IndentLine("}\n");
  346. }
  347. void CSFunctionWriter::GenPInvokeCallParameters(String& sig)
  348. {
  349. // generate args
  350. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  351. if (parameters.Size())
  352. {
  353. for (unsigned int i = 0; i < parameters.Size(); i++)
  354. {
  355. JSBFunctionType* ptype = parameters.At(i);
  356. // ignore "Context" parameters
  357. if (ptype->type_->asClassType())
  358. {
  359. JSBClassType* classType = ptype->type_->asClassType();
  360. JSBClass* klass = classType->class_;
  361. if (klass->GetName() == "Context")
  362. {
  363. continue;
  364. }
  365. }
  366. String name = ptype->name_;
  367. if (name == "object")
  368. name = "_object";
  369. else if (name == "readonly")
  370. name = "readOnly";
  371. else if (name == "params")
  372. name = "parameters";
  373. if (ptype->type_->asClassType())
  374. {
  375. JSBClass* pclass = ptype->type_->asClassType()->class_;
  376. if (pclass->IsNumberArray())
  377. {
  378. sig += "ref " + name;
  379. }
  380. else
  381. {
  382. sig += name + " == null ? IntPtr.Zero : " + name + ".nativeInstance";
  383. }
  384. }
  385. else
  386. {
  387. sig += name;
  388. }
  389. if (i + 1 != parameters.Size())
  390. sig += ", ";
  391. }
  392. }
  393. // data marshaller
  394. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  395. {
  396. if (function_->GetReturnClass()->IsNumberArray())
  397. {
  398. if (sig.Length())
  399. sig += ", ";
  400. JSBClass* klass = function_->GetClass();
  401. sig += ToString("ref %s%sReturnValue", klass->GetName().CString(), function_->GetName().CString());
  402. }
  403. }
  404. }
  405. void CSFunctionWriter::WriteManagedFunction(String& source)
  406. {
  407. JSBClass* klass = function_->GetClass();
  408. JSBPackage* package = klass->GetPackage();
  409. String sig;
  410. String returnType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  411. GenManagedFunctionParameters(sig);
  412. String line = ToString("public %s %s (%s)\n", returnType.CString(), function_->GetName().CString(), sig.CString());
  413. source += IndentLine(line);
  414. source += IndentLine("{\n");
  415. Indent();
  416. WriteDefaultStructParameters(source);
  417. line.Clear();
  418. if (function_->GetReturnType())
  419. {
  420. if (function_->GetReturnType()->type_->asStringType() || function_->GetReturnType()->type_->asStringHashType())
  421. {
  422. line += "return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(";
  423. }
  424. else if (CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  425. line += "return ";
  426. else
  427. {
  428. if (function_->GetReturnClass())
  429. {
  430. if (!function_->GetReturnClass()->IsNumberArray())
  431. line += "IntPtr retNativeInstance = ";
  432. }
  433. }
  434. }
  435. String callSig;
  436. GenPInvokeCallParameters(callSig);
  437. line += ToString("csb_%s_%s_%s(nativeInstance",
  438. package->GetName().CString(), klass->GetName().CString(), function_->GetName().CString());
  439. if (callSig.Length())
  440. {
  441. line += ", " + callSig;
  442. }
  443. if (function_->GetReturnType())
  444. {
  445. if (function_->GetReturnType()->type_->asStringType() || function_->GetReturnType()->type_->asStringHashType())
  446. line += ")";
  447. }
  448. line += ");\n";
  449. source += IndentLine(line);
  450. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  451. {
  452. if (function_->GetReturnType()->type_->asClassType())
  453. {
  454. JSBClass* retClass = function_->GetReturnClass();
  455. JSBClass* klass = function_->GetClass();
  456. if (retClass->IsNumberArray())
  457. {
  458. line = ToString("return %s%sReturnValue;", klass->GetName().CString(), function_->GetName().CString());
  459. }
  460. else
  461. {
  462. line = ToString("return retNativeInstance == IntPtr.Zero ? null : NativeCore.WrapNative<%s> (retNativeInstance);", retClass->GetName().CString());
  463. }
  464. source += IndentLine(line);
  465. }
  466. }
  467. source+= "\n";
  468. Dedent();
  469. source += IndentLine("}\n");
  470. }
  471. void CSFunctionWriter::GenerateManagedSource(String& sourceOut)
  472. {
  473. String source = "";
  474. Indent();
  475. Indent();
  476. Indent();
  477. if (function_->GetDocString().Length())
  478. {
  479. // monodocer -assembly:NETCore.dll -path:en -pretty
  480. // mdoc export-html -o htmldocs en
  481. source += IndentLine("/// <summary>\n");
  482. source += IndentLine("/// " + function_->GetDocString() + "\n");
  483. source += IndentLine("/// </summary>\n");
  484. }
  485. if (function_->IsConstructor())
  486. WriteManagedConstructor(source);
  487. else
  488. WriteManagedFunction(source);
  489. WriteManagedPInvokeFunctionSignature(source);
  490. // data marshaller
  491. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  492. {
  493. if (function_->GetReturnClass())
  494. {
  495. JSBClass* retClass = function_->GetReturnClass();
  496. if (retClass->IsNumberArray())
  497. {
  498. JSBClass* klass = function_->GetClass();
  499. String managedType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  500. String marshal = "private " + managedType + " ";
  501. marshal += ToString("%s%sReturnValue = new %s();\n", klass->GetName().CString(), function_->GetName().CString(), managedType.CString());
  502. sourceOut += IndentLine(marshal);
  503. }
  504. }
  505. }
  506. Dedent();
  507. Dedent();
  508. Dedent();
  509. sourceOut += source;
  510. }
  511. void CSFunctionWriter::GenerateSource(String& sourceOut)
  512. {
  513. }
  514. String CSFunctionWriter::MapDefaultParameter(JSBFunctionType* parameter)
  515. {
  516. String init = parameter->initializer_;
  517. if (!init.Length())
  518. return init;
  519. if (parameter->type_->asClassType())
  520. {
  521. if (init == "0")
  522. return "null";
  523. }
  524. if (parameter->type_->asEnumType())
  525. {
  526. return parameter->type_->asEnumType()->enum_->GetName() + "." + init;
  527. }
  528. if (function_->class_->GetPackage()->ContainsConstant(init))
  529. return "Constants." + init;
  530. if (init == "true" || init == "false")
  531. return init;
  532. if (init == "0.0f")
  533. return init;
  534. if (init == "1.0f")
  535. return init;
  536. if (init == "0.1f")
  537. return init;
  538. if (init == "0")
  539. return init;
  540. if (init == "-1")
  541. return init;
  542. if (init == "\"\\t\"")
  543. return init;
  544. if (init == "NULL")
  545. return "null";
  546. if (init == "M_MAX_UNSIGNED")
  547. return "0xffffffff";
  548. if (init == "String::EMPTY")
  549. return "\"\"";
  550. // this kind of sucks, can't define const structs
  551. // and default parameters need to be const :/
  552. DefaultStructParameter dparm;
  553. dparm.parameterName = parameter->name_;
  554. if (init == "Vector3::ZERO")
  555. {
  556. dparm.type = "Vector3";
  557. dparm.assignment = "Vector3.Zero";
  558. defaultStructParameters_.Push(dparm);
  559. return "default(Vector3)";
  560. }
  561. if (init == "Vector3::ONE")
  562. {
  563. dparm.type = "Vector3";
  564. dparm.assignment = "Vector3.One";
  565. defaultStructParameters_.Push(dparm);
  566. return "default(Vector3)";
  567. }
  568. if (init == "Vector3::UP")
  569. {
  570. dparm.type = "Vector3";
  571. dparm.assignment = "Vector3.Up";
  572. defaultStructParameters_.Push(dparm);
  573. return "default(Vector3)";
  574. }
  575. if (init == "IntVector2::ZERO")
  576. {
  577. dparm.type = "IntVector2";
  578. dparm.assignment = "IntVector2.Zero";
  579. defaultStructParameters_.Push(dparm);
  580. return "default(IntVector2)";
  581. }
  582. if (init == "Quaternion::IDENTITY")
  583. {
  584. dparm.type = "Quaternion";
  585. dparm.assignment = "Quaternion.Identity";
  586. defaultStructParameters_.Push(dparm);
  587. return "default(Quaternion)";
  588. }
  589. LOGINFOF("HEY! %s", init.CString());
  590. return String::EMPTY;
  591. }
  592. }