CSFunctionWriter.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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. bool CSFunctionWriter::wroteConstructor_ = false;
  92. CSFunctionWriter::CSFunctionWriter(JSBFunction *function) : JSBFunctionWriter(function)
  93. {
  94. }
  95. void CSFunctionWriter::WriteNativeParameterMarshal(String& source)
  96. {
  97. }
  98. void CSFunctionWriter::WriteNativeConstructor(String& source)
  99. {
  100. }
  101. void CSFunctionWriter::GenNativeCallParameters(String& sig)
  102. {
  103. JSBClass* klass = function_->GetClass();
  104. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  105. Vector<String> args;
  106. if (parameters.Size())
  107. {
  108. for (unsigned int i = 0; i < parameters.Size(); i++)
  109. {
  110. JSBFunctionType* ptype = parameters.At(i);
  111. // ignore "Context" parameters
  112. if (ptype->type_->asClassType())
  113. {
  114. JSBClassType* classType = ptype->type_->asClassType();
  115. JSBClass* klass = classType->class_;
  116. if (klass->GetName() == "Context")
  117. {
  118. continue;
  119. }
  120. if (klass->IsNumberArray() || ptype->isReference_)
  121. args.Push(ToString("*%s", ptype->name_.CString()));
  122. else
  123. args.Push(ToString("%s", ptype->name_.CString()));
  124. }
  125. else if (ptype->type_->asVectorType())
  126. {
  127. args.Push(ToString("%s__vector", ptype->name_.CString()));
  128. }
  129. else
  130. {
  131. if (ptype->type_->asStringType() || ptype->type_->asStringHashType())
  132. {
  133. args.Push(ToString("%s ? String(%s) : String::EMPTY", ptype->name_.CString(), ptype->name_.CString()));
  134. }
  135. else
  136. {
  137. args.Push(ToString("%s", ptype->name_.CString()));
  138. }
  139. }
  140. }
  141. }
  142. sig.Join(args, ", ");
  143. }
  144. void CSFunctionWriter::WriteNativeFunction(String& source)
  145. {
  146. JSBClass* klass = function_->GetClass();
  147. JSBPackage* package = klass->GetPackage();
  148. String fname = function_->IsConstructor() ? "Constructor" : function_->GetName();
  149. String returnType;
  150. String functionSig = CSTypeHelper::GetNativeFunctionSignature(function_, returnType);
  151. String line;
  152. line = ToString("ATOMIC_EXPORT_API %s %s\n",
  153. returnType.CString(), functionSig.CString());
  154. source += IndentLine(line);
  155. source += IndentLine("{\n");
  156. Indent();
  157. source += "\n";
  158. // vector marshal
  159. bool hasVectorMarshal = false;
  160. Vector<JSBFunctionType*>& fparams = function_->GetParameters();
  161. for (unsigned i = 0; i < fparams.Size(); i++)
  162. {
  163. JSBFunctionType* ftype = fparams[i];
  164. JSBVectorType* vtype = ftype->type_->asVectorType();
  165. if (!vtype)
  166. continue;
  167. JSBClassType* classType = vtype->vectorType_->asClassType();
  168. if (!classType)
  169. continue;
  170. String className = classType->class_->GetName();
  171. String vectorMarshal;
  172. hasVectorMarshal = true;
  173. if (vtype->isPODVector_)
  174. {
  175. const String& pname = ftype->name_;
  176. source += IndentLine(ToString("PODVector<%s*> %s__vector;\n", className.CString(), pname.CString()));
  177. source += IndentLine(ToString("if (%s) %s->AdaptToVector<%s*>(%s__vector);\n", pname.CString(), pname.CString(), className.CString(), pname.CString()));
  178. }
  179. else
  180. {
  181. // vectorMarshal = ToString("PODVector<%s*> %s__vector", className.CString(), ftype->name_.CString());
  182. }
  183. if (vectorMarshal.Length())
  184. {
  185. source += IndentLine(vectorMarshal);
  186. vectorMarshal = String::EMPTY;
  187. }
  188. }
  189. bool returnValue = false;
  190. bool sharedPtrReturn = false;
  191. String returnStatement;
  192. if (returnType == "const char*")
  193. {
  194. returnValue = true;
  195. source += IndentLine("static String returnValue;\n");
  196. returnStatement = "returnValue = ";
  197. }
  198. else if (function_->GetReturnClass() && function_->GetReturnClass()->IsNumberArray())
  199. {
  200. returnStatement = "*returnValue = ";
  201. }
  202. else if (function_->GetReturnClass() && function_->GetReturnType()->isSharedPtr_)
  203. {
  204. returnStatement = ToString("SharedPtr<%s> returnValue = ", function_->GetReturnClass()->GetNativeName().CString());
  205. sharedPtrReturn = true;
  206. }
  207. else if (function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
  208. {
  209. // we have an out parameter
  210. JSBVectorType* vtype = function_->GetReturnType()->type_->asVectorType();
  211. if (!vtype->vectorTypeIsSharedPtr_ && !vtype->vectorTypeIsWeakPtr_)
  212. {
  213. returnStatement = ToString("%sVector<%s*> returnValue__vector = ", vtype->isPODVector_ ? "POD" : "", vtype->vectorType_->asClassType()->class_->GetName().CString());
  214. }
  215. else
  216. {
  217. returnStatement = ToString("%sVector<%s<%s>> returnValue__vector = ", vtype->isPODVector_ ? "POD" : "", vtype->vectorTypeIsSharedPtr_ ? "SharedPtr" : "WeakPtr", vtype->vectorType_->asClassType()->class_->GetName().CString());
  218. }
  219. }
  220. else
  221. {
  222. if (returnType != "void" && !hasVectorMarshal)
  223. {
  224. returnStatement = "return ";
  225. }
  226. else if (returnType != "void")
  227. {
  228. returnStatement = ToString("%s returnValue = ", returnType.CString());
  229. }
  230. }
  231. String callSig;
  232. GenNativeCallParameters(callSig);
  233. if (!function_->isConstructor_)
  234. {
  235. if (function_->IsStatic())
  236. {
  237. line = ToString("%s%s::%s(%s);\n", returnStatement.CString(), klass->GetNativeName().CString(), function_->GetName().CString(), callSig.CString());
  238. }
  239. else
  240. {
  241. line = ToString("%sself->%s(%s);\n", returnStatement.CString(), function_->GetName().CString(), callSig.CString());
  242. }
  243. }
  244. else
  245. {
  246. if (klass->IsAbstract())
  247. {
  248. line = "return 0; // Abstract Class\n";
  249. }
  250. else if (klass->IsObject())
  251. {
  252. if (callSig.Length())
  253. line = ToString("return new %s(NETCore::GetContext(), %s);\n", klass->GetNativeName().CString(), callSig.CString());
  254. else
  255. line = ToString("return new %s(NETCore::GetContext());\n", klass->GetNativeName().CString());
  256. }
  257. else
  258. {
  259. line = ToString("return new %s(%s);\n", klass->GetNativeName().CString(), callSig.CString());
  260. }
  261. }
  262. source += IndentLine(line);
  263. // Vector marshaling
  264. for (unsigned i = 0; i < fparams.Size(); i++)
  265. {
  266. JSBFunctionType* ftype = fparams[i];
  267. JSBVectorType* vtype = ftype->type_->asVectorType();
  268. if (!vtype)
  269. continue;
  270. JSBClassType* classType = vtype->vectorType_->asClassType();
  271. if (!classType)
  272. continue;
  273. String className = classType->class_->GetName();
  274. String vectorMarshal;
  275. if (vtype->isPODVector_)
  276. {
  277. const String& pname = ftype->name_;
  278. source += IndentLine(ToString("if (%s) %s->AdaptFromVector<%s*>(%s__vector);\n", pname.CString(), pname.CString(), className.CString(), pname.CString()));
  279. }
  280. else
  281. {
  282. // vectorMarshal = ToString("PODVector<%s*> %s__vector", className.CString(), ftype->name_.CString());
  283. }
  284. if (vectorMarshal.Length())
  285. {
  286. source += IndentLine(vectorMarshal);
  287. vectorMarshal = String::EMPTY;
  288. }
  289. }
  290. if (sharedPtrReturn)
  291. {
  292. source += IndentLine("if (returnValue.NotNull()) returnValue->AddRef();\n");
  293. source += IndentLine("return returnValue;\n");
  294. }
  295. else if (returnType == "const char*")
  296. {
  297. source += IndentLine("return returnValue.CString();\n");
  298. }
  299. else if (function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
  300. {
  301. // we have an out parameter
  302. JSBVectorType* vtype = function_->GetReturnType()->type_->asVectorType();
  303. source += IndentLine("if (returnValue) returnValue->AdaptFromVector(returnValue__vector);\n");
  304. }
  305. else if (returnType != "void" && hasVectorMarshal)
  306. {
  307. source += IndentLine("return returnValue;\n");
  308. }
  309. Dedent();
  310. source += IndentLine("}\n");
  311. source += "\n";
  312. }
  313. void CSFunctionWriter::GenerateNativeSource(String& sourceOut)
  314. {
  315. String source = "";
  316. WriteNativeFunction(source);
  317. sourceOut += source;
  318. }
  319. // MANAGED----------------------------------------------------------------------------------------
  320. void CSFunctionWriter::WriteDefaultStructParameters(String& source)
  321. {
  322. for (unsigned i = 0; i < defaultStructParameters_.Size(); i++)
  323. {
  324. const DefaultStructParameter& dparm = defaultStructParameters_[i];
  325. String line = ToString("if (default(%s).Equals(%s)) %s = %s;\n",
  326. dparm.type.CString(), dparm.parameterName.CString(), dparm.parameterName.CString(),
  327. dparm.assignment.CString());
  328. source += IndentLine(line);
  329. }
  330. }
  331. void CSFunctionWriter::WriteManagedPInvokeFunctionSignature(String& source)
  332. {
  333. source += "\n";
  334. // CoreCLR has pinvoke security demand code commented out, so we do not (currently) need this optimization:
  335. // https://github.com/dotnet/coreclr/issues/1605
  336. // line = "[SuppressUnmanagedCodeSecurity]\n";
  337. // source += IndentLine(line);
  338. String line = "[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
  339. source += IndentLine(line);
  340. JSBClass* klass = function_->GetClass();
  341. JSBPackage* package = klass->GetPackage();
  342. String returnType = CSTypeHelper::GetPInvokeTypeString(function_->GetReturnType());
  343. // handled by out parameter
  344. if (function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
  345. returnType = "void";
  346. if (returnType == "bool")
  347. {
  348. // default boolean marshal is 4 byte windows type BOOL and not 1 byte bool
  349. // https://blogs.msdn.microsoft.com/jaredpar/2008/10/14/pinvoke-and-bool-or-should-i-say-bool/
  350. source += IndentLine("[return: MarshalAs(UnmanagedType.I1)]\n");
  351. }
  352. if (returnType == "string")
  353. returnType = "IntPtr";
  354. if (function_->IsConstructor())
  355. returnType = "IntPtr";
  356. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  357. Vector<String> args;
  358. if (!function_->IsConstructor() && !function_->IsStatic())
  359. {
  360. args.Push("IntPtr self");
  361. }
  362. if (parameters.Size())
  363. {
  364. for (unsigned int i = 0; i < parameters.Size(); i++)
  365. {
  366. JSBFunctionType* ptype = parameters.At(i);
  367. String name = ptype->name_;
  368. if (name == "object")
  369. name = "_object";
  370. else if (name == "readonly")
  371. name = "readOnly";
  372. else if (name == "params")
  373. name = "parameters";
  374. // ignore "Context" parameters
  375. if (ptype->type_->asClassType())
  376. {
  377. JSBClassType* classType = ptype->type_->asClassType();
  378. JSBClass* klass = classType->class_;
  379. if (klass->GetName() == "Context")
  380. {
  381. continue;
  382. }
  383. if (klass->IsNumberArray())
  384. {
  385. args.Push("ref " + klass->GetName() + " " + name);
  386. }
  387. else
  388. {
  389. args.Push("IntPtr " + name);
  390. }
  391. }
  392. else
  393. {
  394. args.Push(CSTypeHelper::GetPInvokeTypeString(ptype) + " " + name);
  395. }
  396. }
  397. }
  398. if (function_->GetReturnClass())
  399. {
  400. JSBClass* retClass = function_->GetReturnClass();
  401. if (retClass->IsNumberArray())
  402. {
  403. args.Push("ref " + retClass->GetName() + " retValue");
  404. }
  405. }
  406. else if (function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
  407. {
  408. args.Push("IntPtr returnValue");
  409. }
  410. String pstring;
  411. pstring.Join(args, ", ");
  412. String fname = function_->IsConstructor() ? "Constructor" : function_->GetName();
  413. line = ToString("private static extern %s csb_%s_%s_%s_%u(%s);\n",
  414. returnType.CString(), package->GetName().CString(), klass->GetName().CString(),
  415. fname.CString(), function_->GetID(), pstring.CString());
  416. source += IndentLine(line);
  417. source += "\n";
  418. }
  419. void CSFunctionWriter::GenManagedFunctionParameters(String& sig)
  420. {
  421. // generate args
  422. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  423. if (parameters.Size())
  424. {
  425. for (unsigned int i = 0; i < parameters.Size(); i++)
  426. {
  427. JSBFunctionType* ptype = parameters.At(i);
  428. // ignore "Context" parameters
  429. if (ptype->type_->asClassType())
  430. {
  431. JSBClassType* classType = ptype->type_->asClassType();
  432. JSBClass* klass = classType->class_;
  433. if (klass->GetName() == "Context")
  434. {
  435. continue;
  436. }
  437. }
  438. sig += CSTypeHelper::GetManagedTypeString(ptype);
  439. String init = ptype->initializer_;
  440. if (init.Length())
  441. {
  442. init = MapDefaultParameter(ptype);
  443. if (init.Length())
  444. sig += " = " + init;
  445. }
  446. if (i + 1 != parameters.Size())
  447. sig += ", ";
  448. }
  449. }
  450. }
  451. void CSFunctionWriter::WriteManagedConstructor(String& source)
  452. {
  453. JSBClass* klass = function_->GetClass();
  454. JSBPackage* package = klass->GetPackage();
  455. if (klass->GetName() == "RefCounted")
  456. return;
  457. // wrapping constructor
  458. String line;
  459. if (!wroteConstructor_)
  460. {
  461. line = ToString("public %s (IntPtr native) : base (native)\n", klass->GetName().CString());
  462. source += IndentLine(line);
  463. source += IndentLine("{\n");
  464. source += IndentLine("}\n\n");
  465. }
  466. // don't add wrapping constructor for overloads
  467. wroteConstructor_ = true;
  468. String sig;
  469. GenManagedFunctionParameters(sig);
  470. line = ToString("public %s (%s)\n", klass->GetName().CString(), sig.CString());
  471. source += IndentLine(line);
  472. source += IndentLine("{\n");
  473. Indent();
  474. WriteDefaultStructParameters(source);
  475. source += IndentLine("if (nativeInstance == IntPtr.Zero)\n");
  476. source += IndentLine("{\n");
  477. Indent();
  478. source += IndentLine(ToString("var classType = typeof(%s);\n", klass->GetName().CString()));
  479. source += IndentLine("var thisType = this.GetType();\n");
  480. source += IndentLine("var nativeThisType = NativeCore.IsNativeType(thisType);\n");
  481. source += IndentLine("var nativeBaseType = NativeCore.IsNativeType(thisType.BaseType);\n");
  482. source += IndentLine("if ( (nativeThisType && (thisType == classType)) || (!nativeThisType && (nativeBaseType && (thisType.BaseType == classType))))\n");
  483. source += IndentLine("{\n");
  484. Indent();
  485. String callSig;
  486. GenPInvokeCallParameters(callSig);
  487. source += IndentLine("IntPtr nativeInstanceOverride = NativeCore.NativeContructorOverride;\n");
  488. line = ToString("nativeInstance = NativeCore.RegisterNative (nativeInstanceOverride != IntPtr.Zero ? nativeInstanceOverride : csb_%s_%s_Constructor_%u(%s), this);\n",
  489. package->GetName().CString(), klass->GetName().CString(), function_->GetID(), callSig.CString());
  490. source += IndentLine(line);
  491. Dedent();
  492. source += IndentLine("}\n");
  493. Dedent();
  494. source += IndentLine("}\n");
  495. Dedent();
  496. source += IndentLine("}\n");
  497. }
  498. void CSFunctionWriter::GenPInvokeCallParameters(String& sig)
  499. {
  500. // generate args
  501. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  502. if (parameters.Size())
  503. {
  504. for (unsigned int i = 0; i < parameters.Size(); i++)
  505. {
  506. JSBFunctionType* ptype = parameters.At(i);
  507. // ignore "Context" parameters
  508. if (ptype->type_->asClassType())
  509. {
  510. JSBClassType* classType = ptype->type_->asClassType();
  511. JSBClass* klass = classType->class_;
  512. if (klass->GetName() == "Context")
  513. {
  514. continue;
  515. }
  516. }
  517. String name = ptype->name_;
  518. if (name == "object")
  519. name = "_object";
  520. else if (name == "readonly")
  521. name = "readOnly";
  522. else if (name == "params")
  523. name = "parameters";
  524. if (ptype->type_->asClassType())
  525. {
  526. JSBClass* pclass = ptype->type_->asClassType()->class_;
  527. if (pclass->IsNumberArray())
  528. {
  529. sig += "ref " + name;
  530. }
  531. else
  532. {
  533. sig += name + " == null ? IntPtr.Zero : " + name + ".nativeInstance";
  534. }
  535. }
  536. else
  537. {
  538. sig += name;
  539. }
  540. if (i + 1 != parameters.Size())
  541. sig += ", ";
  542. }
  543. }
  544. // data marshaller
  545. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  546. {
  547. if (function_->GetReturnClass()->IsNumberArray())
  548. {
  549. if (sig.Length())
  550. sig += ", ";
  551. JSBClass* klass = function_->GetClass();
  552. sig += ToString("ref %s%s%uReturnValue", klass->GetName().CString(), function_->GetName().CString(), function_->GetID());
  553. }
  554. }
  555. else if (!function_->IsStatic() && function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
  556. {
  557. if (sig.Length())
  558. sig += ", ";
  559. JSBClass* klass = function_->GetClass();
  560. sig += "returnScriptVector";
  561. }
  562. }
  563. void CSFunctionWriter::WriteManagedFunction(String& source)
  564. {
  565. JSBClass* klass = function_->GetClass();
  566. JSBPackage* package = klass->GetPackage();
  567. String sig;
  568. String returnType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  569. GenManagedFunctionParameters(sig);
  570. String line = "public ";
  571. if (function_->IsStatic())
  572. {
  573. line += "static ";
  574. }
  575. bool marked = false;
  576. JSBClass* baseClass = klass->GetBaseClass();
  577. if (baseClass)
  578. {
  579. JSBFunction* override = baseClass->MatchFunction(function_, true);
  580. if (override)
  581. {
  582. marked = true;
  583. if (override->IsVirtual())
  584. line += "override ";
  585. else
  586. line += "new ";
  587. }
  588. }
  589. if (!marked && function_->IsVirtual())
  590. line += "virtual ";
  591. line += ToString("%s %s (%s)\n", returnType.CString(), function_->GetName().CString(), sig.CString());
  592. source += IndentLine(line);
  593. source += IndentLine("{\n");
  594. Indent();
  595. WriteDefaultStructParameters(source);
  596. line.Clear();
  597. if (function_->GetReturnType())
  598. {
  599. if (function_->GetReturnType()->type_->asStringType() || function_->GetReturnType()->type_->asStringHashType())
  600. {
  601. line += "return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(";
  602. }
  603. else if (function_->GetReturnType()->type_->asVectorType())
  604. {
  605. source += IndentLine(ToString("var returnScriptVector = %s%s%uReturnValue.GetScriptVector();\n", klass->GetName().CString(), function_->GetName().CString(), function_->GetID()));
  606. }
  607. else if (CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  608. line += "return ";
  609. else
  610. {
  611. if (function_->GetReturnClass())
  612. {
  613. if (!function_->GetReturnClass()->IsNumberArray())
  614. line += "IntPtr retNativeInstance = ";
  615. }
  616. }
  617. }
  618. String callSig;
  619. GenPInvokeCallParameters(callSig);
  620. String nativeInstance;
  621. if (!function_->IsStatic())
  622. nativeInstance = "nativeInstance";
  623. line += ToString("csb_%s_%s_%s_%u(%s",
  624. package->GetName().CString(), klass->GetName().CString(), function_->GetName().CString(), function_->GetID(), nativeInstance.CString());
  625. if (callSig.Length())
  626. {
  627. if (nativeInstance.Length())
  628. line += ", " + callSig;
  629. else
  630. line += callSig;
  631. }
  632. if (function_->GetReturnType())
  633. {
  634. if (function_->GetReturnType()->type_->asStringType() || function_->GetReturnType()->type_->asStringHashType())
  635. line += ")";
  636. }
  637. line += ");\n";
  638. source += IndentLine(line);
  639. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  640. {
  641. if (function_->GetReturnType()->type_->asClassType())
  642. {
  643. JSBClass* retClass = function_->GetReturnClass();
  644. JSBClass* klass = function_->GetClass();
  645. if (retClass->IsNumberArray())
  646. {
  647. line = ToString("return %s%s%uReturnValue;", klass->GetName().CString(), function_->GetName().CString(), function_->GetID());
  648. }
  649. else
  650. {
  651. line = ToString("return retNativeInstance == IntPtr.Zero ? null : NativeCore.WrapNative<%s> (retNativeInstance);", retClass->GetName().CString());
  652. }
  653. source += IndentLine(line);
  654. source+= "\n";
  655. }
  656. }
  657. else if (function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
  658. {
  659. if (!function_->IsStatic())
  660. {
  661. source += IndentLine(ToString("return %s%s%uReturnValue;", klass->GetName().CString(), function_->GetName().CString(), function_->GetID()));
  662. source+= "\n";
  663. }
  664. }
  665. Dedent();
  666. source += IndentLine("}\n");
  667. }
  668. void CSFunctionWriter::GenerateManagedSource(String& sourceOut)
  669. {
  670. String source = "";
  671. Indent();
  672. Indent();
  673. if (function_->GetDocString().Length())
  674. {
  675. // monodocer -assembly:NETCore.dll -path:en -pretty
  676. // mdoc export-html -o htmldocs en
  677. source += IndentLine("/// <summary>\n");
  678. if (function_->GetDocString().Contains('\n'))
  679. source += IndentLine("/* " + function_->GetDocString() + "*/\n");
  680. else
  681. source += IndentLine("/// " + function_->GetDocString() + "\n");
  682. source += IndentLine("/// </summary>\n");
  683. }
  684. if (function_->IsConstructor())
  685. WriteManagedConstructor(source);
  686. else
  687. WriteManagedFunction(source);
  688. WriteManagedPInvokeFunctionSignature(source);
  689. // data marshaller
  690. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  691. {
  692. if (function_->GetReturnClass())
  693. {
  694. JSBClass* retClass = function_->GetReturnClass();
  695. if (retClass->IsNumberArray())
  696. {
  697. JSBClass* klass = function_->GetClass();
  698. String managedType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  699. String marshal = "private ";
  700. if (function_->IsStatic())
  701. marshal += "static ";
  702. marshal += managedType + " ";
  703. marshal += ToString("%s%s%uReturnValue = new %s();\n", klass->GetName().CString(), function_->GetName().CString(), function_->GetID(), managedType.CString());
  704. sourceOut += IndentLine(marshal);
  705. }
  706. }
  707. }
  708. else if (!function_->IsStatic() && function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
  709. {
  710. JSBVectorType* vtype = function_->GetReturnType()->type_->asVectorType();
  711. if (vtype->vectorType_->asClassType())
  712. {
  713. String classname = vtype->vectorType_->asClassType()->class_->GetName();
  714. String typestring = "Vector<" + classname + ">";
  715. String marshal = "private " + typestring + " ";
  716. marshal += ToString("%s%s%uReturnValue = new %s();\n", function_->GetClass()->GetName().CString(), function_->GetName().CString(), function_->GetID(), typestring.CString());
  717. sourceOut += IndentLine(marshal);
  718. }
  719. }
  720. Dedent();
  721. Dedent();
  722. sourceOut += source;
  723. }
  724. void CSFunctionWriter::GenerateSource(String& sourceOut)
  725. {
  726. }
  727. String CSFunctionWriter::MapDefaultParameter(JSBFunctionType* parameter)
  728. {
  729. String init = parameter->initializer_;
  730. if (!init.Length())
  731. return init;
  732. if (parameter->type_->asClassType())
  733. {
  734. if (init == "0")
  735. return "null";
  736. }
  737. if (parameter->type_->asEnumType())
  738. {
  739. return parameter->type_->asEnumType()->enum_->GetName() + "." + init;
  740. }
  741. if (function_->class_->GetPackage()->ContainsConstant(init))
  742. return "Constants." + init;
  743. if (init == "true" || init == "false")
  744. return init;
  745. if (init == "0.0f")
  746. return init;
  747. if (init == "1.0f")
  748. return init;
  749. if (init == "0.1f")
  750. return init;
  751. if (init == "0")
  752. return init;
  753. if (init == "3")
  754. return init;
  755. if (init == "-1")
  756. return init;
  757. if (init == "\"\\t\"")
  758. return init;
  759. if (init == "NULL")
  760. return "null";
  761. if (init == "M_MAX_UNSIGNED")
  762. return "0xffffffff";
  763. if (init == "String::EMPTY")
  764. return "\"\"";
  765. // this kind of sucks, can't define const structs
  766. // and default parameters need to be const :/
  767. DefaultStructParameter dparm;
  768. dparm.parameterName = parameter->name_;
  769. if (init == "Vector3::ZERO")
  770. {
  771. dparm.type = "Vector3";
  772. dparm.assignment = "Vector3.Zero";
  773. defaultStructParameters_.Push(dparm);
  774. return "default(Vector3)";
  775. }
  776. if (init == "Vector3::ONE")
  777. {
  778. dparm.type = "Vector3";
  779. dparm.assignment = "Vector3.One";
  780. defaultStructParameters_.Push(dparm);
  781. return "default(Vector3)";
  782. }
  783. if (init == "Vector3::UP")
  784. {
  785. dparm.type = "Vector3";
  786. dparm.assignment = "Vector3.Up";
  787. defaultStructParameters_.Push(dparm);
  788. return "default(Vector3)";
  789. }
  790. if (init == "IntVector2::ZERO")
  791. {
  792. dparm.type = "IntVector2";
  793. dparm.assignment = "IntVector2.Zero";
  794. defaultStructParameters_.Push(dparm);
  795. return "default(IntVector2)";
  796. }
  797. if (init == "Quaternion::IDENTITY")
  798. {
  799. dparm.type = "Quaternion";
  800. dparm.assignment = "Quaternion.Identity";
  801. defaultStructParameters_.Push(dparm);
  802. return "default(Quaternion)";
  803. }
  804. return String::EMPTY;
  805. }
  806. }