CSFunctionWriter.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/FileSystem.h>
  23. #include "../JSBind.h"
  24. #include "../JSBModule.h"
  25. #include "../JSBPackage.h"
  26. #include "../JSBEnum.h"
  27. #include "../JSBClass.h"
  28. #include "../JSBFunction.h"
  29. #include "CSTypeHelper.h"
  30. #include "CSFunctionWriter.h"
  31. /*
  32. *
  33. C# getters/setters
  34. local instance storage so we're not constantly creating managed Vector3, etc
  35. Vector2/Vector3/BoundingBox, etc C# structs so assign by value
  36. Object lifetime
  37. C# enum of module types for type info?
  38. C# version of push class instance?
  39. new instance from C# needs constructor
  40. wrapping does not, wrapping doesn't use constructors at all (JS needs this for prototype)
  41. Store GCHandle to keep an object alive (Component, UI) C# side?
  42. typedef const void* ClassID;
  43. which changed based on address, so need register at startup
  44. so at package startup time, need to setup mapping between
  45. IntPtr and C# class, we also need to be able to new a class
  46. instance with existing native or create a native when new'ing from C#
  47. IntPtr to RefCounted native side is the "ID", like JSHeapPtr
  48. Lifetime:
  49. // you cannot derive from native engine classes, other than script components
  50. a C# instance can be new'd, handed to native, stored in native, the C# side could be GC'd
  51. future access to this instance would be a new instance
  52. */
  53. /*
  54. // struct marshal Vector2, Vector3, BoundingBox, etc
  55. // RefCounted*
  56. // primitive bool, int, uint, float, double
  57. // String
  58. RefCounted* csb_Node_Constructor()
  59. {
  60. return new Node(NETCore::GetContext());
  61. }
  62. void csb_Node_GetPosition(Node* self, Vector3* out)
  63. {
  64. *out = self->GetPosition();
  65. }
  66. void csb_Node_SetPosition(Node* self, Vector3*__arg0)
  67. {
  68. self->SetPosition(*__arg0);
  69. }
  70. void csb_Node_SetPosition(Node* self, Vector3*__arg0)
  71. {
  72. self->SetPosition(*__arg0);
  73. }
  74. bool csb_Audio_Play(Audio* self)
  75. {
  76. bool retValue = self->Play();
  77. return retValue;
  78. }
  79. const RefCounted* csb_Node_GetParent(Node* self)
  80. {
  81. const RefCounted* retValue = self->GetParent();
  82. return RefCounted;
  83. }
  84. RefCounted* csb_ObjectAnimation_Constructor()
  85. {
  86. return new ObjectAnimation(NETCore::GetContext());
  87. }
  88. */
  89. namespace ToolCore
  90. {
  91. CSFunctionWriter::CSFunctionWriter(JSBFunction *function) : JSBFunctionWriter(function)
  92. {
  93. }
  94. void CSFunctionWriter::WriteNativeParameterMarshal(String& source)
  95. {
  96. }
  97. void CSFunctionWriter::WriteNativeConstructor(String& source)
  98. {
  99. }
  100. void CSFunctionWriter::GenNativeCallParameters(String& sig)
  101. {
  102. JSBClass* klass = function_->GetClass();
  103. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  104. Vector<String> args;
  105. if (parameters.Size())
  106. {
  107. for (unsigned int i = 0; i < parameters.Size(); i++)
  108. {
  109. JSBFunctionType* ptype = parameters.At(i);
  110. // ignore "Context" parameters
  111. if (ptype->type_->asClassType())
  112. {
  113. JSBClassType* classType = ptype->type_->asClassType();
  114. JSBClass* klass = classType->class_;
  115. if (klass->GetName() == "Context")
  116. {
  117. continue;
  118. }
  119. if (klass->IsNumberArray())
  120. args.Push(ToString("*%s", ptype->name_.CString()));
  121. else
  122. args.Push(ToString("%s", ptype->name_.CString()));
  123. }
  124. else if (ptype->type_->asVectorType())
  125. {
  126. args.Push(ToString("%s__vector", ptype->name_.CString()));
  127. }
  128. else
  129. {
  130. args.Push(ToString("%s", ptype->name_.CString()));
  131. }
  132. }
  133. }
  134. sig.Join(args, ", ");
  135. }
  136. void CSFunctionWriter::WriteNativeFunction(String& source)
  137. {
  138. JSBClass* klass = function_->GetClass();
  139. JSBPackage* package = klass->GetPackage();
  140. String fname = function_->IsConstructor() ? "Constructor" : function_->GetName();
  141. String returnType;
  142. String functionSig = CSTypeHelper::GetNativeFunctionSignature(function_, returnType);
  143. String line;
  144. line = ToString("ATOMIC_EXPORT_API %s %s\n",
  145. returnType.CString(), functionSig.CString());
  146. source += IndentLine(line);
  147. source += IndentLine("{\n");
  148. Indent();
  149. source += "\n";
  150. // vector marshal
  151. bool hasVectorMarshal = false;
  152. Vector<JSBFunctionType*>& fparams = function_->GetParameters();
  153. for (unsigned i = 0; i < fparams.Size(); i++)
  154. {
  155. JSBFunctionType* ftype = fparams[i];
  156. JSBVectorType* vtype = ftype->type_->asVectorType();
  157. if (!vtype)
  158. continue;
  159. JSBClassType* classType = vtype->vectorType_->asClassType();
  160. if (!classType)
  161. continue;
  162. String className = classType->class_->GetName();
  163. String vectorMarshal;
  164. hasVectorMarshal = true;
  165. if (vtype->isPODVector_)
  166. {
  167. const String& pname = ftype->name_;
  168. source += IndentLine(ToString("PODVector<%s*> %s__vector;\n", className.CString(), pname.CString()));
  169. source += IndentLine(ToString("if (%s) %s->AdaptToPODVector<%s*>(%s__vector);\n", pname.CString(), pname.CString(), className.CString(), pname.CString()));
  170. }
  171. else
  172. {
  173. // vectorMarshal = ToString("PODVector<%s*> %s__vector", className.CString(), ftype->name_.CString());
  174. }
  175. if (vectorMarshal.Length())
  176. {
  177. source += IndentLine(vectorMarshal);
  178. vectorMarshal = String::EMPTY;
  179. }
  180. }
  181. bool returnValue = false;
  182. bool sharedPtrReturn = false;
  183. String returnStatement;
  184. if (returnType == "const char*")
  185. {
  186. returnValue = true;
  187. source += IndentLine("static String returnValue;\n");
  188. returnStatement = "returnValue = ";
  189. }
  190. else if (function_->GetReturnClass() && function_->GetReturnClass()->IsNumberArray())
  191. {
  192. returnStatement = "*returnValue = ";
  193. }
  194. else if (function_->GetReturnClass() && function_->GetReturnType()->isSharedPtr_)
  195. {
  196. returnStatement = ToString("SharedPtr<%s> returnValue = ", function_->GetReturnClass()->GetNativeName().CString());
  197. sharedPtrReturn = true;
  198. }
  199. else
  200. {
  201. if (returnType != "void" && !hasVectorMarshal)
  202. {
  203. returnStatement = "return ";
  204. }
  205. else if (returnType != "void")
  206. {
  207. returnStatement = ToString("%s returnValue = ", returnType.CString());
  208. }
  209. }
  210. String callSig;
  211. GenNativeCallParameters(callSig);
  212. if (!function_->isConstructor_)
  213. {
  214. if (function_->IsStatic())
  215. {
  216. line = ToString("%s%s::%s(%s);\n", returnStatement.CString(), klass->GetNativeName().CString(), function_->GetName().CString(), callSig.CString());
  217. }
  218. else
  219. {
  220. line = ToString("%sself->%s(%s);\n", returnStatement.CString(), function_->GetName().CString(), callSig.CString());
  221. }
  222. }
  223. else
  224. {
  225. if (klass->IsAbstract())
  226. {
  227. line = "return 0; // Abstract Class\n";
  228. }
  229. else if (klass->IsObject())
  230. {
  231. if (callSig.Length())
  232. line = ToString("return new %s(NETCore::GetContext(), %s);\n", klass->GetNativeName().CString(), callSig.CString());
  233. else
  234. line = ToString("return new %s(NETCore::GetContext());\n", klass->GetNativeName().CString());
  235. }
  236. else
  237. {
  238. line = ToString("return new %s(%s);\n", klass->GetNativeName().CString(), callSig.CString());
  239. }
  240. }
  241. source += IndentLine(line);
  242. // Vector marshaling
  243. for (unsigned i = 0; i < fparams.Size(); i++)
  244. {
  245. JSBFunctionType* ftype = fparams[i];
  246. JSBVectorType* vtype = ftype->type_->asVectorType();
  247. if (!vtype)
  248. continue;
  249. JSBClassType* classType = vtype->vectorType_->asClassType();
  250. if (!classType)
  251. continue;
  252. String className = classType->class_->GetName();
  253. String vectorMarshal;
  254. if (vtype->isPODVector_)
  255. {
  256. const String& pname = ftype->name_;
  257. source += IndentLine(ToString("if (%s) %s->AdaptFromPODVector<%s*>(%s__vector);\n", pname.CString(), pname.CString(), className.CString(), pname.CString()));
  258. }
  259. else
  260. {
  261. // vectorMarshal = ToString("PODVector<%s*> %s__vector", className.CString(), ftype->name_.CString());
  262. }
  263. if (vectorMarshal.Length())
  264. {
  265. source += IndentLine(vectorMarshal);
  266. vectorMarshal = String::EMPTY;
  267. }
  268. }
  269. if (sharedPtrReturn)
  270. {
  271. source += IndentLine("if (returnValue.NotNull()) returnValue->AddRef();\n");
  272. source += IndentLine("return returnValue;\n");
  273. }
  274. else if (returnType == "const char*")
  275. {
  276. source += IndentLine("return returnValue.CString();\n");
  277. }
  278. else if (returnType != "void" && hasVectorMarshal)
  279. {
  280. source += IndentLine("return returnValue;\n");
  281. }
  282. Dedent();
  283. source += IndentLine("}\n");
  284. source += "\n";
  285. }
  286. void CSFunctionWriter::GenerateNativeSource(String& sourceOut)
  287. {
  288. String source = "";
  289. WriteNativeFunction(source);
  290. sourceOut += source;
  291. }
  292. // MANAGED----------------------------------------------------------------------------------------
  293. void CSFunctionWriter::WriteDefaultStructParameters(String& source)
  294. {
  295. for (unsigned i = 0; i < defaultStructParameters_.Size(); i++)
  296. {
  297. const DefaultStructParameter& dparm = defaultStructParameters_[i];
  298. String line = ToString("if (default(%s).Equals(%s)) %s = %s;\n",
  299. dparm.type.CString(), dparm.parameterName.CString(), dparm.parameterName.CString(),
  300. dparm.assignment.CString());
  301. source += IndentLine(line);
  302. }
  303. }
  304. void CSFunctionWriter::WriteManagedPInvokeFunctionSignature(String& source)
  305. {
  306. source += "\n";
  307. // CoreCLR has pinvoke security demand code commented out, so we do not (currently) need this optimization:
  308. // https://github.com/dotnet/coreclr/issues/1605
  309. // line = "[SuppressUnmanagedCodeSecurity]\n";
  310. // source += IndentLine(line);
  311. String line = "[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
  312. source += IndentLine(line);
  313. JSBClass* klass = function_->GetClass();
  314. JSBPackage* package = klass->GetPackage();
  315. String returnType = CSTypeHelper::GetPInvokeTypeString(function_->GetReturnType());
  316. if (returnType == "bool")
  317. {
  318. // default boolean marshal is 4 byte windows type BOOL and not 1 byte bool
  319. // https://blogs.msdn.microsoft.com/jaredpar/2008/10/14/pinvoke-and-bool-or-should-i-say-bool/
  320. source += IndentLine("[return: MarshalAs(UnmanagedType.I1)]\n");
  321. }
  322. if (returnType == "string")
  323. returnType = "IntPtr";
  324. if (function_->IsConstructor())
  325. returnType = "IntPtr";
  326. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  327. Vector<String> args;
  328. if (!function_->IsConstructor() && !function_->IsStatic())
  329. {
  330. args.Push("IntPtr self");
  331. }
  332. if (parameters.Size())
  333. {
  334. for (unsigned int i = 0; i < parameters.Size(); i++)
  335. {
  336. JSBFunctionType* ptype = parameters.At(i);
  337. String name = ptype->name_;
  338. if (name == "object")
  339. name = "_object";
  340. else if (name == "readonly")
  341. name = "readOnly";
  342. else if (name == "params")
  343. name = "parameters";
  344. // ignore "Context" parameters
  345. if (ptype->type_->asClassType())
  346. {
  347. JSBClassType* classType = ptype->type_->asClassType();
  348. JSBClass* klass = classType->class_;
  349. if (klass->GetName() == "Context")
  350. {
  351. continue;
  352. }
  353. if (klass->IsNumberArray())
  354. {
  355. args.Push("ref " + klass->GetName() + " " + name);
  356. }
  357. else
  358. {
  359. args.Push("IntPtr " + name);
  360. }
  361. }
  362. else
  363. {
  364. args.Push(CSTypeHelper::GetPInvokeTypeString(ptype) + " " + name);
  365. }
  366. }
  367. }
  368. if (function_->GetReturnClass())
  369. {
  370. JSBClass* retClass = function_->GetReturnClass();
  371. if (retClass->IsNumberArray())
  372. {
  373. args.Push("ref " + retClass->GetName() + " retValue");
  374. }
  375. }
  376. String pstring;
  377. pstring.Join(args, ", ");
  378. String fname = function_->IsConstructor() ? "Constructor" : function_->GetName();
  379. line = ToString("private static extern %s csb_%s_%s_%s(%s);\n",
  380. returnType.CString(), package->GetName().CString(), klass->GetName().CString(),
  381. fname.CString(), pstring.CString());
  382. source += IndentLine(line);
  383. source += "\n";
  384. }
  385. void CSFunctionWriter::GenManagedFunctionParameters(String& sig)
  386. {
  387. // generate args
  388. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  389. if (parameters.Size())
  390. {
  391. for (unsigned int i = 0; i < parameters.Size(); i++)
  392. {
  393. JSBFunctionType* ptype = parameters.At(i);
  394. // ignore "Context" parameters
  395. if (ptype->type_->asClassType())
  396. {
  397. JSBClassType* classType = ptype->type_->asClassType();
  398. JSBClass* klass = classType->class_;
  399. if (klass->GetName() == "Context")
  400. {
  401. continue;
  402. }
  403. }
  404. sig += CSTypeHelper::GetManagedTypeString(ptype);
  405. String init = ptype->initializer_;
  406. if (init.Length())
  407. {
  408. init = MapDefaultParameter(ptype);
  409. if (init.Length())
  410. sig += " = " + init;
  411. }
  412. if (i + 1 != parameters.Size())
  413. sig += ", ";
  414. }
  415. }
  416. }
  417. void CSFunctionWriter::WriteManagedConstructor(String& source)
  418. {
  419. JSBClass* klass = function_->GetClass();
  420. JSBPackage* package = klass->GetPackage();
  421. if (klass->GetName() == "RefCounted")
  422. return;
  423. // wrapping constructor
  424. String line;
  425. line = ToString("public %s (IntPtr native) : base (native)\n", klass->GetName().CString());
  426. source += IndentLine(line);
  427. source += IndentLine("{\n");
  428. source += IndentLine("}\n\n");
  429. String sig;
  430. GenManagedFunctionParameters(sig);
  431. line = ToString("public %s (%s)\n", klass->GetName().CString(), sig.CString());
  432. source += IndentLine(line);
  433. source += IndentLine("{\n");
  434. Indent();
  435. WriteDefaultStructParameters(source);
  436. source += IndentLine("if (nativeInstance == IntPtr.Zero)\n");
  437. source += IndentLine("{\n");
  438. Indent();
  439. source += IndentLine(ToString("var classType = typeof(%s);\n", klass->GetName().CString()));
  440. source += IndentLine("var thisType = this.GetType();\n");
  441. source += IndentLine("var nativeThisType = NativeCore.IsNativeType(thisType);\n");
  442. source += IndentLine("var nativeBaseType = NativeCore.IsNativeType(thisType.BaseType);\n");
  443. source += IndentLine("if ( (nativeThisType && (thisType == classType)) || (!nativeThisType && (nativeBaseType && (thisType.BaseType == classType))))\n");
  444. source += IndentLine("{\n");
  445. Indent();
  446. String callSig;
  447. GenPInvokeCallParameters(callSig);
  448. source += IndentLine("IntPtr nativeInstanceOverride = NativeCore.NativeContructorOverride;\n");
  449. line = ToString("nativeInstance = NativeCore.RegisterNative (nativeInstanceOverride != IntPtr.Zero ? nativeInstanceOverride : csb_%s_%s_Constructor(%s), this);\n",
  450. package->GetName().CString(), klass->GetName().CString(), callSig.CString());
  451. source += IndentLine(line);
  452. Dedent();
  453. source += IndentLine("}\n");
  454. Dedent();
  455. source += IndentLine("}\n");
  456. Dedent();
  457. source += IndentLine("}\n");
  458. }
  459. void CSFunctionWriter::GenPInvokeCallParameters(String& sig)
  460. {
  461. // generate args
  462. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  463. if (parameters.Size())
  464. {
  465. for (unsigned int i = 0; i < parameters.Size(); i++)
  466. {
  467. JSBFunctionType* ptype = parameters.At(i);
  468. // ignore "Context" parameters
  469. if (ptype->type_->asClassType())
  470. {
  471. JSBClassType* classType = ptype->type_->asClassType();
  472. JSBClass* klass = classType->class_;
  473. if (klass->GetName() == "Context")
  474. {
  475. continue;
  476. }
  477. }
  478. String name = ptype->name_;
  479. if (name == "object")
  480. name = "_object";
  481. else if (name == "readonly")
  482. name = "readOnly";
  483. else if (name == "params")
  484. name = "parameters";
  485. if (ptype->type_->asClassType())
  486. {
  487. JSBClass* pclass = ptype->type_->asClassType()->class_;
  488. if (pclass->IsNumberArray())
  489. {
  490. sig += "ref " + name;
  491. }
  492. else
  493. {
  494. sig += name + " == null ? IntPtr.Zero : " + name + ".nativeInstance";
  495. }
  496. }
  497. else
  498. {
  499. sig += name;
  500. }
  501. if (i + 1 != parameters.Size())
  502. sig += ", ";
  503. }
  504. }
  505. // data marshaller
  506. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  507. {
  508. if (function_->GetReturnClass()->IsNumberArray())
  509. {
  510. if (sig.Length())
  511. sig += ", ";
  512. JSBClass* klass = function_->GetClass();
  513. sig += ToString("ref %s%sReturnValue", klass->GetName().CString(), function_->GetName().CString());
  514. }
  515. }
  516. }
  517. void CSFunctionWriter::WriteManagedFunction(String& source)
  518. {
  519. JSBClass* klass = function_->GetClass();
  520. JSBPackage* package = klass->GetPackage();
  521. String sig;
  522. String returnType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  523. GenManagedFunctionParameters(sig);
  524. String line = "public ";
  525. if (function_->IsStatic())
  526. {
  527. line += "static ";
  528. }
  529. bool marked = false;
  530. JSBClass* baseClass = klass->GetBaseClass();
  531. if (baseClass)
  532. {
  533. JSBFunction* override = baseClass->MatchFunction(function_, true);
  534. if (override)
  535. {
  536. marked = true;
  537. if (override->IsVirtual())
  538. line += "override ";
  539. else
  540. line += "new ";
  541. }
  542. }
  543. if (!marked && function_->IsVirtual())
  544. line += "virtual ";
  545. line += ToString("%s %s (%s)\n", returnType.CString(), function_->GetName().CString(), sig.CString());
  546. source += IndentLine(line);
  547. source += IndentLine("{\n");
  548. Indent();
  549. WriteDefaultStructParameters(source);
  550. line.Clear();
  551. if (function_->GetReturnType())
  552. {
  553. if (function_->GetReturnType()->type_->asStringType() || function_->GetReturnType()->type_->asStringHashType())
  554. {
  555. line += "return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(";
  556. }
  557. else if (CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  558. line += "return ";
  559. else
  560. {
  561. if (function_->GetReturnClass())
  562. {
  563. if (!function_->GetReturnClass()->IsNumberArray())
  564. line += "IntPtr retNativeInstance = ";
  565. }
  566. }
  567. }
  568. String callSig;
  569. GenPInvokeCallParameters(callSig);
  570. String nativeInstance;
  571. if (!function_->IsStatic())
  572. nativeInstance = "nativeInstance";
  573. line += ToString("csb_%s_%s_%s(%s",
  574. package->GetName().CString(), klass->GetName().CString(), function_->GetName().CString(), nativeInstance.CString());
  575. if (callSig.Length())
  576. {
  577. if (nativeInstance.Length())
  578. line += ", " + callSig;
  579. else
  580. line += callSig;
  581. }
  582. if (function_->GetReturnType())
  583. {
  584. if (function_->GetReturnType()->type_->asStringType() || function_->GetReturnType()->type_->asStringHashType())
  585. line += ")";
  586. }
  587. line += ");\n";
  588. source += IndentLine(line);
  589. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  590. {
  591. if (function_->GetReturnType()->type_->asClassType())
  592. {
  593. JSBClass* retClass = function_->GetReturnClass();
  594. JSBClass* klass = function_->GetClass();
  595. if (retClass->IsNumberArray())
  596. {
  597. line = ToString("return %s%sReturnValue;", klass->GetName().CString(), function_->GetName().CString());
  598. }
  599. else
  600. {
  601. line = ToString("return retNativeInstance == IntPtr.Zero ? null : NativeCore.WrapNative<%s> (retNativeInstance);", retClass->GetName().CString());
  602. }
  603. source += IndentLine(line);
  604. source+= "\n";
  605. }
  606. }
  607. Dedent();
  608. source += IndentLine("}\n");
  609. }
  610. void CSFunctionWriter::GenerateManagedSource(String& sourceOut)
  611. {
  612. String source = "";
  613. Indent();
  614. Indent();
  615. if (function_->GetDocString().Length())
  616. {
  617. // monodocer -assembly:NETCore.dll -path:en -pretty
  618. // mdoc export-html -o htmldocs en
  619. source += IndentLine("/// <summary>\n");
  620. if (function_->GetDocString().Contains('\n'))
  621. source += IndentLine("/* " + function_->GetDocString() + "*/\n");
  622. else
  623. source += IndentLine("/// " + function_->GetDocString() + "\n");
  624. source += IndentLine("/// </summary>\n");
  625. }
  626. if (function_->IsConstructor())
  627. WriteManagedConstructor(source);
  628. else
  629. WriteManagedFunction(source);
  630. WriteManagedPInvokeFunctionSignature(source);
  631. // data marshaller
  632. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  633. {
  634. if (function_->GetReturnClass())
  635. {
  636. JSBClass* retClass = function_->GetReturnClass();
  637. if (retClass->IsNumberArray())
  638. {
  639. JSBClass* klass = function_->GetClass();
  640. String managedType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  641. String marshal = "private ";
  642. if (function_->IsStatic())
  643. marshal += "static ";
  644. marshal += managedType + " ";
  645. marshal += ToString("%s%sReturnValue = new %s();\n", klass->GetName().CString(), function_->GetName().CString(), managedType.CString());
  646. sourceOut += IndentLine(marshal);
  647. }
  648. }
  649. }
  650. Dedent();
  651. Dedent();
  652. sourceOut += source;
  653. }
  654. void CSFunctionWriter::GenerateSource(String& sourceOut)
  655. {
  656. }
  657. String CSFunctionWriter::MapDefaultParameter(JSBFunctionType* parameter)
  658. {
  659. String init = parameter->initializer_;
  660. if (!init.Length())
  661. return init;
  662. if (parameter->type_->asClassType())
  663. {
  664. if (init == "0")
  665. return "null";
  666. }
  667. if (parameter->type_->asEnumType())
  668. {
  669. return parameter->type_->asEnumType()->enum_->GetName() + "." + init;
  670. }
  671. if (function_->class_->GetPackage()->ContainsConstant(init))
  672. return "Constants." + init;
  673. if (init == "true" || init == "false")
  674. return init;
  675. if (init == "0.0f")
  676. return init;
  677. if (init == "1.0f")
  678. return init;
  679. if (init == "0.1f")
  680. return init;
  681. if (init == "0")
  682. return init;
  683. if (init == "3")
  684. return init;
  685. if (init == "-1")
  686. return init;
  687. if (init == "\"\\t\"")
  688. return init;
  689. if (init == "NULL")
  690. return "null";
  691. if (init == "M_MAX_UNSIGNED")
  692. return "0xffffffff";
  693. if (init == "String::EMPTY")
  694. return "\"\"";
  695. // this kind of sucks, can't define const structs
  696. // and default parameters need to be const :/
  697. DefaultStructParameter dparm;
  698. dparm.parameterName = parameter->name_;
  699. if (init == "Vector3::ZERO")
  700. {
  701. dparm.type = "Vector3";
  702. dparm.assignment = "Vector3.Zero";
  703. defaultStructParameters_.Push(dparm);
  704. return "default(Vector3)";
  705. }
  706. if (init == "Vector3::ONE")
  707. {
  708. dparm.type = "Vector3";
  709. dparm.assignment = "Vector3.One";
  710. defaultStructParameters_.Push(dparm);
  711. return "default(Vector3)";
  712. }
  713. if (init == "Vector3::UP")
  714. {
  715. dparm.type = "Vector3";
  716. dparm.assignment = "Vector3.Up";
  717. defaultStructParameters_.Push(dparm);
  718. return "default(Vector3)";
  719. }
  720. if (init == "IntVector2::ZERO")
  721. {
  722. dparm.type = "IntVector2";
  723. dparm.assignment = "IntVector2.Zero";
  724. defaultStructParameters_.Push(dparm);
  725. return "default(IntVector2)";
  726. }
  727. if (init == "Quaternion::IDENTITY")
  728. {
  729. dparm.type = "Quaternion";
  730. dparm.assignment = "Quaternion.Identity";
  731. defaultStructParameters_.Push(dparm);
  732. return "default(Quaternion)";
  733. }
  734. return String::EMPTY;
  735. }
  736. }