CSFunctionWriter.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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. source += IndentLine("IntPtr nativeInstanceOverride = NativeCore.NativeContructorOverride;\n");
  460. line = ToString("nativeInstance = NativeCore.RegisterNative (nativeInstanceOverride != IntPtr.Zero ? nativeInstanceOverride : csb_%s_%s_Constructor_%u(%s), this);\n",
  461. package->GetName().CString(), klass->GetName().CString(), function_->GetID(), callSig.CString());
  462. source += IndentLine(line);
  463. Dedent();
  464. source += IndentLine("}\n");
  465. Dedent();
  466. source += IndentLine("}\n");
  467. Dedent();
  468. source += IndentLine("}\n");
  469. }
  470. void CSFunctionWriter::GenPInvokeCallParameters(String& sig)
  471. {
  472. // generate args
  473. const Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  474. if (parameters.Size())
  475. {
  476. for (unsigned int i = 0; i < parameters.Size(); i++)
  477. {
  478. JSBFunctionType* ptype = parameters.At(i);
  479. // ignore "Context" parameters
  480. if (ptype->type_->asClassType())
  481. {
  482. JSBClassType* classType = ptype->type_->asClassType();
  483. JSBClass* klass = classType->class_;
  484. if (klass->GetName() == "Context")
  485. {
  486. continue;
  487. }
  488. }
  489. String name = ptype->name_;
  490. if (name == "object")
  491. name = "_object";
  492. else if (name == "readonly")
  493. name = "readOnly";
  494. else if (name == "params")
  495. name = "parameters";
  496. if (ptype->type_->asClassType())
  497. {
  498. JSBClass* pclass = ptype->type_->asClassType()->class_;
  499. if (pclass->IsNumberArray())
  500. {
  501. sig += "ref " + name;
  502. }
  503. else
  504. {
  505. sig += name + " == null ? IntPtr.Zero : " + name + ".NativeInstance";
  506. }
  507. }
  508. else
  509. {
  510. sig += name;
  511. }
  512. if (i + 1 != parameters.Size())
  513. sig += ", ";
  514. }
  515. }
  516. // data marshaller
  517. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  518. {
  519. if (function_->GetReturnClass()->IsNumberArray())
  520. {
  521. if (sig.Length())
  522. sig += ", ";
  523. JSBClass* klass = function_->GetClass();
  524. sig += ToString("ref %s%s%uReturnValue", klass->GetName().CString(), function_->GetName().CString(), function_->GetID());
  525. }
  526. }
  527. else if (!function_->IsStatic() && function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
  528. {
  529. if (sig.Length())
  530. sig += ", ";
  531. JSBClass* klass = function_->GetClass();
  532. sig += "returnScriptVector";
  533. }
  534. }
  535. void CSFunctionWriter::WriteManagedFunction(String& source)
  536. {
  537. JSBClass* klass = function_->GetClass();
  538. JSBPackage* package = klass->GetPackage();
  539. String sig;
  540. String returnType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  541. GenManagedFunctionParameters(sig);
  542. String line = klass->IsInterface() ? "" : "public ";
  543. if (function_->IsStatic())
  544. {
  545. line += "static ";
  546. }
  547. bool marked = false;
  548. JSBClass* baseClass = klass->GetBaseClass();
  549. if (baseClass)
  550. {
  551. JSBFunction* override = baseClass->MatchFunction(function_, true);
  552. if (override)
  553. {
  554. marked = true;
  555. if (override->IsVirtual())
  556. line += "override ";
  557. else
  558. line += "new ";
  559. }
  560. }
  561. if (!marked && function_->IsVirtual() && !klass->IsInterface())
  562. line += "virtual ";
  563. line += ToString("%s %s (%s)", returnType.CString(), function_->GetName().CString(), sig.CString());
  564. if (klass->IsInterface())
  565. {
  566. // If we're an interface we have no implementation
  567. line += ";\n\n";
  568. source += IndentLine(line);
  569. return;
  570. }
  571. else
  572. line += "\n";
  573. source += IndentLine(line);
  574. source += IndentLine("{\n");
  575. Indent();
  576. WriteDefaultStructParameters(source);
  577. line.Clear();
  578. if (function_->GetReturnType())
  579. {
  580. if (function_->GetReturnType()->type_->asStringType())
  581. {
  582. line += "return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(";
  583. }
  584. else if (function_->GetReturnType()->type_->asStringHashType())
  585. {
  586. line += "return ";
  587. }
  588. else if (function_->GetReturnType()->type_->asVectorType())
  589. {
  590. source += IndentLine(ToString("var returnScriptVector = %s%s%uReturnValue.GetScriptVector();\n", klass->GetName().CString(), function_->GetName().CString(), function_->GetID()));
  591. }
  592. else if (CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  593. line += "return ";
  594. else
  595. {
  596. if (function_->GetReturnClass())
  597. {
  598. if (!function_->GetReturnClass()->IsNumberArray())
  599. line += "IntPtr retNativeInstance = ";
  600. }
  601. }
  602. }
  603. String callSig;
  604. GenPInvokeCallParameters(callSig);
  605. String nativeInstance;
  606. if (!function_->IsStatic())
  607. nativeInstance = "nativeInstance";
  608. line += ToString("csb_%s_%s_%s_%u(%s",
  609. package->GetName().CString(), klass->GetName().CString(), function_->GetName().CString(), function_->GetID(), nativeInstance.CString());
  610. if (callSig.Length())
  611. {
  612. if (nativeInstance.Length())
  613. line += ", " + callSig;
  614. else
  615. line += callSig;
  616. }
  617. if (function_->GetReturnType())
  618. {
  619. if (function_->GetReturnType()->type_->asStringType())
  620. line += ")";
  621. }
  622. line += ");\n";
  623. source += IndentLine(line);
  624. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  625. {
  626. if (function_->GetReturnType()->type_->asClassType())
  627. {
  628. JSBClass* retClass = function_->GetReturnClass();
  629. JSBClass* klass = function_->GetClass();
  630. if (retClass->IsNumberArray())
  631. {
  632. line = ToString("return %s%s%uReturnValue;", klass->GetName().CString(), function_->GetName().CString(), function_->GetID());
  633. }
  634. else
  635. {
  636. line = ToString("return retNativeInstance == IntPtr.Zero ? null : NativeCore.WrapNative<%s> (retNativeInstance);", retClass->GetName().CString());
  637. }
  638. source += IndentLine(line);
  639. source+= "\n";
  640. }
  641. }
  642. else if (function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
  643. {
  644. if (!function_->IsStatic())
  645. {
  646. source += IndentLine(ToString("return %s%s%uReturnValue;", klass->GetName().CString(), function_->GetName().CString(), function_->GetID()));
  647. source+= "\n";
  648. }
  649. }
  650. Dedent();
  651. source += IndentLine("}\n");
  652. }
  653. void CSFunctionWriter::GenerateManagedSource(String& sourceOut)
  654. {
  655. JSBClass* klass = function_->GetClass();
  656. String source = "";
  657. Indent();
  658. Indent();
  659. if (function_->GetDocString().Length())
  660. {
  661. // monodocer -assembly:NETCore.dll -path:en -pretty
  662. // mdoc export-html -o htmldocs en
  663. source += IndentLine("/// <summary>\n");
  664. if (function_->GetDocString().Contains('\n'))
  665. source += IndentLine("/* " + function_->GetDocString() + "*/\n");
  666. else
  667. source += IndentLine("/// " + function_->GetDocString() + "\n");
  668. source += IndentLine("/// </summary>\n");
  669. }
  670. if (function_->IsConstructor())
  671. WriteManagedConstructor(source);
  672. else
  673. WriteManagedFunction(source);
  674. WriteManagedPInvokeFunctionSignature(source);
  675. if (!klass->IsInterface())
  676. {
  677. // data marshaller
  678. if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
  679. {
  680. if (function_->GetReturnClass())
  681. {
  682. JSBClass* retClass = function_->GetReturnClass();
  683. if (retClass->IsNumberArray())
  684. {
  685. String managedType = CSTypeHelper::GetManagedTypeString(function_->GetReturnType());
  686. String marshal = "private ";
  687. if (function_->IsStatic())
  688. marshal += "static ";
  689. marshal += managedType + " ";
  690. marshal += ToString("%s%s%uReturnValue = new %s();\n", klass->GetName().CString(), function_->GetName().CString(), function_->GetID(), managedType.CString());
  691. sourceOut += IndentLine(marshal);
  692. }
  693. }
  694. }
  695. else if (!function_->IsStatic() && function_->GetReturnType() && function_->GetReturnType()->type_->asVectorType())
  696. {
  697. JSBVectorType* vtype = function_->GetReturnType()->type_->asVectorType();
  698. if (vtype->vectorType_->asClassType())
  699. {
  700. String classname = vtype->vectorType_->asClassType()->class_->GetName();
  701. String typestring = "Vector<" + classname + ">";
  702. String marshal = "private " + typestring + " ";
  703. marshal += ToString("%s%s%uReturnValue = new %s();\n", function_->GetClass()->GetName().CString(), function_->GetName().CString(), function_->GetID(), typestring.CString());
  704. sourceOut += IndentLine(marshal);
  705. }
  706. }
  707. }
  708. Dedent();
  709. Dedent();
  710. sourceOut += source;
  711. }
  712. void CSFunctionWriter::GenerateSource(String& sourceOut)
  713. {
  714. }
  715. String CSFunctionWriter::MapDefaultParameter(JSBFunctionType* parameter)
  716. {
  717. String init = parameter->initializer_;
  718. if (!init.Length())
  719. return init;
  720. if (parameter->type_->asClassType())
  721. {
  722. if (init == "0")
  723. return "null";
  724. }
  725. if (parameter->type_->asEnumType())
  726. {
  727. return parameter->type_->asEnumType()->enum_->GetName() + "." + init;
  728. }
  729. if (function_->class_->GetPackage()->ContainsConstant(init))
  730. return "Constants." + init;
  731. if (init == "true" || init == "false")
  732. return init;
  733. if (init == "0.0f")
  734. return init;
  735. if (init == "1.0f")
  736. return init;
  737. if (init == "0.1f")
  738. return init;
  739. if (init == "0")
  740. return init;
  741. if (init == "3")
  742. return init;
  743. if (init == "-1")
  744. return init;
  745. if (init == "\"\\t\"")
  746. return init;
  747. if (init == "NULL")
  748. return "null";
  749. if (init == "M_MAX_UNSIGNED")
  750. return "0xffffffff";
  751. if (init == "String::EMPTY")
  752. return "\"\"";
  753. // this kind of sucks, can't define const structs
  754. // and default parameters need to be const :/
  755. DefaultStructParameter dparm;
  756. dparm.parameterName = parameter->name_;
  757. if (init == "Vector3::ZERO")
  758. {
  759. dparm.type = "Vector3";
  760. dparm.assignment = "Vector3.Zero";
  761. defaultStructParameters_.Push(dparm);
  762. return "default(Vector3)";
  763. }
  764. if (init == "Vector3::ONE")
  765. {
  766. dparm.type = "Vector3";
  767. dparm.assignment = "Vector3.One";
  768. defaultStructParameters_.Push(dparm);
  769. return "default(Vector3)";
  770. }
  771. if (init == "Vector3::UP")
  772. {
  773. dparm.type = "Vector3";
  774. dparm.assignment = "Vector3.Up";
  775. defaultStructParameters_.Push(dparm);
  776. return "default(Vector3)";
  777. }
  778. if (init == "IntVector2::ZERO")
  779. {
  780. dparm.type = "IntVector2";
  781. dparm.assignment = "IntVector2.Zero";
  782. defaultStructParameters_.Push(dparm);
  783. return "default(IntVector2)";
  784. }
  785. if (init == "Quaternion::IDENTITY")
  786. {
  787. dparm.type = "Quaternion";
  788. dparm.assignment = "Quaternion.Identity";
  789. defaultStructParameters_.Push(dparm);
  790. return "default(Quaternion)";
  791. }
  792. if (init == "StringHash::ZERO")
  793. {
  794. dparm.type = "StringHash";
  795. dparm.assignment = "StringHash.Zero";
  796. defaultStructParameters_.Push(dparm);
  797. return "default(StringHash)";
  798. }
  799. return String::EMPTY;
  800. }
  801. }