CSFunctionWriter.cpp 31 KB

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