CSFunctionWriter.cpp 23 KB

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