CSFunctionWriter.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. JSBClass* klass = function_->GetClass();
  291. JSBPackage* package = klass->GetPackage();
  292. if (klass->IsInterface())
  293. return;
  294. source += "\n";
  295. // CoreCLR has pinvoke security demand code commented out, so we do not (currently) need this optimization:
  296. // https://github.com/dotnet/coreclr/issues/1605
  297. // line = "[SuppressUnmanagedCodeSecurity]\n";
  298. // source += IndentLine(line);
  299. String line = "[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
  300. source += IndentLine(line);
  301. String returnType = CSTypeHelper::GetPInvokeTypeString(function_->GetReturnType());
  302. // handled by out parameter
  303. if (function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
  304. returnType = "void";
  305. if (returnType == "bool")
  306. {
  307. // default boolean marshal is 4 byte windows type BOOL and not 1 byte bool
  308. // https://blogs.msdn.microsoft.com/jaredpar/2008/10/14/pinvoke-and-bool-or-should-i-say-bool/
  309. source += IndentLine("[return: MarshalAs(UnmanagedType.I1)]\n");
  310. }
  311. if (returnType == "string")
  312. returnType = "IntPtr";
  313. if (function_->IsConstructor())
  314. returnType = "IntPtr";
  315. const Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  316. Vector<String> args;
  317. if (!function_->IsConstructor() && !function_->IsStatic())
  318. {
  319. args.Push("IntPtr self");
  320. }
  321. if (parameters.Size())
  322. {
  323. for (unsigned int i = 0; i < parameters.Size(); i++)
  324. {
  325. JSBFunctionType* ptype = parameters.At(i);
  326. String name = ptype->name_;
  327. if (name == "object")
  328. name = "_object";
  329. else if (name == "readonly")
  330. name = "readOnly";
  331. else if (name == "params")
  332. name = "parameters";
  333. // ignore "Context" parameters
  334. if (ptype->type_->asClassType())
  335. {
  336. JSBClassType* classType = ptype->type_->asClassType();
  337. JSBClass* klass = classType->class_;
  338. if (klass->GetName() == "Context")
  339. {
  340. continue;
  341. }
  342. if (klass->IsNumberArray())
  343. {
  344. args.Push("ref " + klass->GetName() + " " + name);
  345. }
  346. else
  347. {
  348. args.Push("IntPtr " + name);
  349. }
  350. }
  351. else
  352. {
  353. args.Push(CSTypeHelper::GetPInvokeTypeString(ptype) + " " + name);
  354. }
  355. }
  356. }
  357. if (function_->GetReturnClass())
  358. {
  359. JSBClass* retClass = function_->GetReturnClass();
  360. if (retClass->IsNumberArray())
  361. {
  362. args.Push("ref " + retClass->GetName() + " retValue");
  363. }
  364. }
  365. else if (function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
  366. {
  367. args.Push("IntPtr returnValue");
  368. }
  369. String pstring;
  370. pstring.Join(args, ", ");
  371. String fname = function_->IsConstructor() ? "Constructor" : function_->GetName();
  372. line = ToString("private static extern %s csb_%s_%s_%s_%u(%s);\n",
  373. returnType.CString(), package->GetName().CString(), klass->GetName().CString(),
  374. fname.CString(), function_->GetID(), pstring.CString());
  375. source += IndentLine(line);
  376. source += "\n";
  377. }
  378. void CSFunctionWriter::GenManagedFunctionParameters(String& sig)
  379. {
  380. // generate args
  381. const Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  382. if (parameters.Size())
  383. {
  384. for (unsigned int i = 0; i < parameters.Size(); i++)
  385. {
  386. bool isStruct = false;
  387. JSBFunctionType* ptype = parameters.At(i);
  388. // ignore "Context" parameters
  389. if (ptype->type_->asClassType())
  390. {
  391. JSBClassType* classType = ptype->type_->asClassType();
  392. JSBClass* klass = classType->class_;
  393. if (klass->GetName() == "Context")
  394. {
  395. continue;
  396. }
  397. // TODO: we should have a better system for struct type in general
  398. // This number array is really for JS
  399. if (klass->IsNumberArray())
  400. {
  401. isStruct = true;
  402. }
  403. }
  404. String managedTypeString = CSTypeHelper::GetManagedTypeString(ptype);
  405. if (!ptype->isConst_ && (ptype->isReference_ && isStruct))
  406. {
  407. // pass by reference
  408. managedTypeString = "ref " + managedTypeString;
  409. }
  410. sig += managedTypeString;
  411. String init = ptype->initializer_;
  412. if (init.Length())
  413. {
  414. init = MapDefaultParameter(ptype);
  415. if (init.Length())
  416. sig += " = " + init;
  417. }
  418. if (i + 1 != parameters.Size())
  419. sig += ", ";
  420. }
  421. }
  422. }
  423. void CSFunctionWriter::WriteManagedConstructor(String& source)
  424. {
  425. JSBClass* klass = function_->GetClass();
  426. JSBPackage* package = klass->GetPackage();
  427. if (klass->GetName() == "RefCounted")
  428. return;
  429. // wrapping constructor
  430. String line;
  431. if (!wroteConstructor_)
  432. {
  433. line = ToString("public %s (IntPtr native) : base (native)\n", klass->GetName().CString());
  434. source += IndentLine(line);
  435. source += IndentLine("{\n");
  436. source += IndentLine("}\n\n");
  437. }
  438. // don't add wrapping constructor for overloads
  439. wroteConstructor_ = true;
  440. String sig;
  441. GenManagedFunctionParameters(sig);
  442. line = ToString("public %s (%s)\n", klass->GetName().CString(), sig.CString());
  443. source += IndentLine(line);
  444. source += IndentLine("{\n");
  445. Indent();
  446. WriteDefaultStructParameters(source);
  447. source += IndentLine("if (nativeInstance == IntPtr.Zero)\n");
  448. source += IndentLine("{\n");
  449. Indent();
  450. source += IndentLine(ToString("var classType = typeof(%s);\n", klass->GetName().CString()));
  451. source += IndentLine("var thisType = this.GetType();\n");
  452. source += IndentLine("var thisTypeIsNative = NativeCore.IsNativeType(thisType);\n");
  453. source += IndentLine("var nativeAncsestorType = NativeCore.GetNativeAncestorType(thisType);\n");
  454. source += IndentLine("if ( (thisTypeIsNative && (thisType == classType)) || (!thisTypeIsNative && (nativeAncsestorType == classType)))\n");
  455. source += IndentLine("{\n");
  456. Indent();
  457. String callSig;
  458. GenPInvokeCallParameters(callSig);
  459. line = ToString("nativeInstance = NativeCore.RegisterNative (csb_%s_%s_Constructor_%u(%s), this);\n",
  460. package->GetName().CString(), klass->GetName().CString(), function_->GetID(), callSig.CString());
  461. source += IndentLine(line);
  462. Dedent();
  463. source += IndentLine("}\n");
  464. Dedent();
  465. source += IndentLine("}\n");
  466. Dedent();
  467. source += IndentLine("}\n");
  468. }
  469. void CSFunctionWriter::GenPInvokeCallParameters(String& sig)
  470. {
  471. // generate args
  472. const Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  473. if (parameters.Size())
  474. {
  475. for (unsigned int i = 0; i < parameters.Size(); i++)
  476. {
  477. JSBFunctionType* ptype = parameters.At(i);
  478. // ignore "Context" parameters
  479. if (ptype->type_->asClassType())
  480. {
  481. JSBClassType* classType = ptype->type_->asClassType();
  482. JSBClass* klass = classType->class_;
  483. if (klass->GetName() == "Context")
  484. {
  485. continue;
  486. }
  487. }
  488. String name = ptype->name_;
  489. if (name == "object")
  490. name = "_object";
  491. else if (name == "readonly")
  492. name = "readOnly";
  493. else if (name == "params")
  494. name = "parameters";
  495. if (ptype->type_->asClassType())
  496. {
  497. JSBClass* pclass = ptype->type_->asClassType()->class_;
  498. if (pclass->IsNumberArray())
  499. {
  500. sig += "ref " + name;
  501. }
  502. else
  503. {
  504. sig += name + " == null ? IntPtr.Zero : " + name + ".NativeInstance";
  505. }
  506. }
  507. else
  508. {
  509. sig += name;
  510. }
  511. if (i + 1 != parameters.Size())
  512. sig += ", ";
  513. }
  514. }
  515. // data marshaller
  516. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  517. {
  518. if (function_->GetReturnClass()->IsNumberArray())
  519. {
  520. if (sig.Length())
  521. sig += ", ";
  522. JSBClass* klass = function_->GetClass();
  523. sig += ToString("ref %s%s%uReturnValue", klass->GetName().CString(), function_->GetName().CString(), function_->GetID());
  524. }
  525. }
  526. else if (!function_->IsStatic() && function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
  527. {
  528. if (sig.Length())
  529. sig += ", ";
  530. JSBClass* klass = function_->GetClass();
  531. sig += "returnScriptVector";
  532. }
  533. }
  534. void CSFunctionWriter::WriteManagedFunction(String& source)
  535. {
  536. JSBClass* klass = function_->GetClass();
  537. JSBPackage* package = klass->GetPackage();
  538. String sig;
  539. String returnType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  540. GenManagedFunctionParameters(sig);
  541. String line = klass->IsInterface() ? "" : "public ";
  542. if (function_->IsStatic())
  543. {
  544. line += "static ";
  545. }
  546. bool marked = false;
  547. JSBClass* baseClass = klass->GetBaseClass();
  548. if (baseClass)
  549. {
  550. JSBFunction* override = baseClass->MatchFunction(function_, true);
  551. if (override)
  552. {
  553. marked = true;
  554. if (override->IsVirtual())
  555. line += "override ";
  556. else
  557. line += "new ";
  558. }
  559. }
  560. if (!marked && function_->IsVirtual() && !klass->IsInterface())
  561. line += "virtual ";
  562. line += ToString("%s %s (%s)", returnType.CString(), function_->GetName().CString(), sig.CString());
  563. if (klass->IsInterface())
  564. {
  565. // If we're an interface we have no implementation
  566. line += ";\n\n";
  567. source += IndentLine(line);
  568. return;
  569. }
  570. else
  571. line += "\n";
  572. source += IndentLine(line);
  573. source += IndentLine("{\n");
  574. Indent();
  575. WriteDefaultStructParameters(source);
  576. line.Clear();
  577. if (function_->GetReturnType())
  578. {
  579. if (function_->GetReturnType()->type_->asStringType())
  580. {
  581. line += "return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(";
  582. }
  583. else if (function_->GetReturnType()->type_->asStringHashType())
  584. {
  585. line += "return ";
  586. }
  587. else if (function_->GetReturnType()->type_->asVectorType())
  588. {
  589. source += IndentLine(ToString("var returnScriptVector = %s%s%uReturnValue.GetScriptVector();\n", klass->GetName().CString(), function_->GetName().CString(), function_->GetID()));
  590. }
  591. else if (CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  592. line += "return ";
  593. else
  594. {
  595. if (function_->GetReturnClass())
  596. {
  597. if (!function_->GetReturnClass()->IsNumberArray())
  598. line += "IntPtr retNativeInstance = ";
  599. }
  600. }
  601. }
  602. String callSig;
  603. GenPInvokeCallParameters(callSig);
  604. String nativeInstance;
  605. if (!function_->IsStatic())
  606. nativeInstance = "nativeInstance";
  607. line += ToString("csb_%s_%s_%s_%u(%s",
  608. package->GetName().CString(), klass->GetName().CString(), function_->GetName().CString(), function_->GetID(), nativeInstance.CString());
  609. if (callSig.Length())
  610. {
  611. if (nativeInstance.Length())
  612. line += ", " + callSig;
  613. else
  614. line += callSig;
  615. }
  616. if (function_->GetReturnType())
  617. {
  618. if (function_->GetReturnType()->type_->asStringType())
  619. line += ")";
  620. }
  621. line += ");\n";
  622. source += IndentLine(line);
  623. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  624. {
  625. if (function_->GetReturnType()->type_->asClassType())
  626. {
  627. JSBClass* retClass = function_->GetReturnClass();
  628. JSBClass* klass = function_->GetClass();
  629. if (retClass->IsNumberArray())
  630. {
  631. line = ToString("return %s%s%uReturnValue;", klass->GetName().CString(), function_->GetName().CString(), function_->GetID());
  632. }
  633. else
  634. {
  635. line = ToString("return retNativeInstance == IntPtr.Zero ? null : NativeCore.WrapNative<%s> (retNativeInstance);", retClass->GetName().CString());
  636. }
  637. source += IndentLine(line);
  638. source+= "\n";
  639. }
  640. }
  641. else if (function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
  642. {
  643. if (!function_->IsStatic())
  644. {
  645. source += IndentLine(ToString("return %s%s%uReturnValue;", klass->GetName().CString(), function_->GetName().CString(), function_->GetID()));
  646. source+= "\n";
  647. }
  648. }
  649. Dedent();
  650. source += IndentLine("}\n");
  651. }
  652. void CSFunctionWriter::GenerateManagedSource(String& sourceOut)
  653. {
  654. JSBClass* klass = function_->GetClass();
  655. String source = "";
  656. Indent();
  657. Indent();
  658. if (function_->GetDocString().Length())
  659. {
  660. // monodocer -assembly:NETCore.dll -path:en -pretty
  661. // mdoc export-html -o htmldocs en
  662. source += IndentLine("/// <summary>\n");
  663. if (function_->GetDocString().Contains('\n'))
  664. source += IndentLine("/* " + function_->GetDocString() + "*/\n");
  665. else
  666. source += IndentLine("/// " + function_->GetDocString() + "\n");
  667. source += IndentLine("/// </summary>\n");
  668. }
  669. if (function_->IsConstructor())
  670. WriteManagedConstructor(source);
  671. else
  672. WriteManagedFunction(source);
  673. WriteManagedPInvokeFunctionSignature(source);
  674. if (!klass->IsInterface())
  675. {
  676. // data marshaller
  677. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  678. {
  679. if (function_->GetReturnClass())
  680. {
  681. JSBClass* retClass = function_->GetReturnClass();
  682. if (retClass->IsNumberArray())
  683. {
  684. String managedType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  685. String marshal = "private ";
  686. if (function_->IsStatic())
  687. marshal += "static ";
  688. marshal += managedType + " ";
  689. marshal += ToString("%s%s%uReturnValue = new %s();\n", klass->GetName().CString(), function_->GetName().CString(), function_->GetID(), managedType.CString());
  690. sourceOut += IndentLine(marshal);
  691. }
  692. }
  693. }
  694. else if (!function_->IsStatic() && function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
  695. {
  696. JSBVectorType* vtype = function_->GetReturnType()->type_->asVectorType();
  697. if (vtype->vectorType_->asClassType())
  698. {
  699. String classname = vtype->vectorType_->asClassType()->class_->GetName();
  700. String typestring = "Vector<" + classname + ">";
  701. String marshal = "private " + typestring + " ";
  702. marshal += ToString("%s%s%uReturnValue = new %s();\n", function_->GetClass()->GetName().CString(), function_->GetName().CString(), function_->GetID(), typestring.CString());
  703. sourceOut += IndentLine(marshal);
  704. }
  705. }
  706. }
  707. Dedent();
  708. Dedent();
  709. sourceOut += source;
  710. }
  711. void CSFunctionWriter::GenerateSource(String& sourceOut)
  712. {
  713. }
  714. String CSFunctionWriter::MapDefaultParameter(JSBFunctionType* parameter)
  715. {
  716. String init = parameter->initializer_;
  717. if (!init.Length())
  718. return init;
  719. if (parameter->type_->asClassType())
  720. {
  721. if (init == "0")
  722. return "null";
  723. }
  724. if (parameter->type_->asEnumType())
  725. {
  726. return parameter->type_->asEnumType()->enum_->GetName() + "." + init;
  727. }
  728. if (function_->class_->GetPackage()->ContainsConstant(init))
  729. return "Constants." + init;
  730. if (init == "true" || init == "false")
  731. return init;
  732. if (init == "0.0f")
  733. return init;
  734. if (init == "1.0f")
  735. return init;
  736. if (init == "0.1f")
  737. return init;
  738. if (init == "0")
  739. return init;
  740. if (init == "3")
  741. return init;
  742. if (init == "-1")
  743. return init;
  744. if (init == "\"\\t\"")
  745. return init;
  746. if (init == "NULL")
  747. return "null";
  748. if (init == "M_MAX_UNSIGNED")
  749. return "0xffffffff";
  750. if (init == "String::EMPTY")
  751. return "\"\"";
  752. // this kind of sucks, can't define const structs
  753. // and default parameters need to be const :/
  754. DefaultStructParameter dparm;
  755. dparm.parameterName = parameter->name_;
  756. if (init == "Vector3::ZERO")
  757. {
  758. dparm.type = "Vector3";
  759. dparm.assignment = "Vector3.Zero";
  760. defaultStructParameters_.Push(dparm);
  761. return "default(Vector3)";
  762. }
  763. if (init == "Vector3::ONE")
  764. {
  765. dparm.type = "Vector3";
  766. dparm.assignment = "Vector3.One";
  767. defaultStructParameters_.Push(dparm);
  768. return "default(Vector3)";
  769. }
  770. if (init == "Vector3::UP")
  771. {
  772. dparm.type = "Vector3";
  773. dparm.assignment = "Vector3.Up";
  774. defaultStructParameters_.Push(dparm);
  775. return "default(Vector3)";
  776. }
  777. if (init == "IntVector2::ZERO")
  778. {
  779. dparm.type = "IntVector2";
  780. dparm.assignment = "IntVector2.Zero";
  781. defaultStructParameters_.Push(dparm);
  782. return "default(IntVector2)";
  783. }
  784. if (init == "Quaternion::IDENTITY")
  785. {
  786. dparm.type = "Quaternion";
  787. dparm.assignment = "Quaternion.Identity";
  788. defaultStructParameters_.Push(dparm);
  789. return "default(Quaternion)";
  790. }
  791. if (init == "StringHash::ZERO")
  792. {
  793. dparm.type = "StringHash";
  794. dparm.assignment = "StringHash.Zero";
  795. defaultStructParameters_.Push(dparm);
  796. return "default(StringHash)";
  797. }
  798. return String::EMPTY;
  799. }
  800. }