CSFunctionWriter.cpp 29 KB

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