JSBFunctionWriter.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. #include <Atomic/IO/FileSystem.h>
  2. #include "JSBind.h"
  3. #include "JSBModule.h"
  4. #include "JSBPackage.h"
  5. #include "JSBEnum.h"
  6. #include "JSBClass.h"
  7. #include "JSBFunction.h"
  8. #include "JSBFunctionWriter.h"
  9. namespace ToolCore
  10. {
  11. JSBFunctionWriter::JSBFunctionWriter(JSBFunction *function) : function_(function)
  12. {
  13. }
  14. void JSBFunctionWriter::WriteParameterMarshal(String& source)
  15. {
  16. // generate args
  17. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  18. int cparam = 0;
  19. if (parameters.Size())
  20. {
  21. for (unsigned int i = 0; i < parameters.Size(); i++, cparam++)
  22. {
  23. JSBFunctionType * ptype = parameters.At(i);
  24. // ignore "Context" parameters
  25. if (ptype->type_->asClassType())
  26. {
  27. JSBClassType* classType = ptype->type_->asClassType();
  28. JSBClass* klass = classType->class_;
  29. if (klass->GetName() == "Context")
  30. {
  31. cparam--;
  32. continue;
  33. }
  34. }
  35. String pstring = ptype->ToArgString(cparam);
  36. const String& init = ptype->initializer_;
  37. if (ptype->type_->asClassType())
  38. {
  39. JSBClassType* classType = ptype->type_->asClassType();
  40. JSBClass* klass = classType->class_;
  41. if (!klass->IsNumberArray())
  42. {
  43. if (init.Length())
  44. {
  45. source.AppendWithFormat("%s = duk_get_top(ctx) >= %i ? js_to_class_instance<%s>(ctx, %i, 0) : %s;\n",
  46. pstring.CString(), cparam + 1, klass->GetNativeName().CString(), cparam, init.CString());
  47. }
  48. else
  49. {
  50. source.AppendWithFormat("%s = js_to_class_instance<%s>(ctx, %i, 0);\n",
  51. pstring.CString(), klass->GetNativeName().CString(), cparam);
  52. }
  53. }
  54. else
  55. {
  56. int elements = klass->GetNumberArrayElements();
  57. String elementType = klass->GetArrayElementType();
  58. source.AppendWithFormat("%s arrayData%i[%i];\n", elementType.CString(), cparam, elements);
  59. if (init.Length())
  60. {
  61. source.AppendWithFormat("const %s& defaultArg%i = %s;\n", klass->GetNativeName().CString(), cparam, init.CString());
  62. source.AppendWithFormat("if (duk_get_top(ctx) >= %i) {\n", cparam + 1);
  63. }
  64. for (int j = 0; j < elements; j++)
  65. {
  66. source.AppendWithFormat("duk_get_prop_index(ctx, %i, %i);\n", cparam, j);
  67. source.AppendWithFormat("arrayData%i[%i] = (%s) duk_to_number(ctx, -1);\n", cparam, j, elementType.CString());
  68. }
  69. source.AppendWithFormat("duk_pop_n(ctx, %i);\n", elements);
  70. if (init.Length())
  71. {
  72. source.Append("}\n");
  73. source.AppendWithFormat("%s __arg%i(duk_get_top(ctx) >= %i ? arrayData%i : defaultArg%i);\n",
  74. klass->GetNativeName().CString(), cparam, cparam + 1, cparam, cparam);
  75. }
  76. else
  77. {
  78. source.AppendWithFormat("%s __arg%i(arrayData%i);\n", klass->GetNativeName().CString(), cparam, cparam);
  79. }
  80. }
  81. }
  82. else if (ptype->type_->asStringType() || ptype->type_->asStringHashType())
  83. {
  84. if (init.Length())
  85. {
  86. source.AppendWithFormat("%s = duk_get_top(ctx) >= %i ? duk_to_string(ctx, %i) : %s;\n", pstring.CString(), cparam + 1, cparam, init.CString());
  87. }
  88. else
  89. {
  90. source.AppendWithFormat("%s = duk_to_string(ctx, %i);\n", pstring.CString(), cparam);
  91. }
  92. }
  93. else if (ptype->type_->asHeapPtrType())
  94. {
  95. if (init.Length())
  96. {
  97. source.AppendWithFormat("%s = duk_get_top(ctx) >= %i ? duk_get_heapptr(ctx, %i) : %s;\n", pstring.CString(), cparam + 1, cparam, init.CString());
  98. }
  99. else
  100. {
  101. source.AppendWithFormat("%s = duk_get_heapptr(ctx, %i);\n", pstring.CString(), cparam);
  102. }
  103. }
  104. else if (ptype->type_->asPrimitiveType())
  105. {
  106. JSBPrimitiveType* prtype = ptype->type_->asPrimitiveType();
  107. if (prtype->kind_ == JSBPrimitiveType::Bool)
  108. {
  109. if (init.Length())
  110. {
  111. source.AppendWithFormat("bool __arg%i = duk_get_top(ctx) >= %i ? (duk_to_boolean(ctx, %i) ? true : false) : %s;\n",
  112. cparam, cparam + 1, cparam, init.CString());
  113. }
  114. else
  115. {
  116. source.AppendWithFormat("bool __arg%i = duk_to_boolean(ctx, %i) ? true : false;\n", cparam, cparam);
  117. }
  118. }
  119. else
  120. {
  121. if (init.Length())
  122. {
  123. source.AppendWithFormat("double __arg%i = duk_get_top(ctx) >= %i ? (duk_to_number(ctx, %i)) : %s;\n",
  124. cparam, cparam + 1, cparam, init.CString());
  125. }
  126. else
  127. {
  128. source.AppendWithFormat("double __arg%i = duk_to_number(ctx, %i);\n", cparam, cparam);
  129. }
  130. }
  131. }
  132. else if (ptype->type_->asEnumType())
  133. {
  134. JSBEnumType* etype = ptype->type_->asEnumType();
  135. if (init.Length())
  136. {
  137. source.AppendWithFormat("%s __arg%i = duk_get_top(ctx) >= %i ? ((%s) ((int) duk_to_number(ctx, %i))) : %s;\n", etype->enum_->GetName().CString(),
  138. cparam, cparam + 1, etype->enum_->GetName().CString(), cparam, init.CString());
  139. }
  140. else
  141. {
  142. source.AppendWithFormat("%s __arg%i = (%s) ((int)duk_to_number(ctx, %i));\n", etype->enum_->GetName().CString(),
  143. cparam, etype->enum_->GetName().CString(), cparam);
  144. }
  145. }
  146. else if (ptype->type_->asVectorType())
  147. {
  148. // read only vector arguments
  149. if (ptype->isConst_)
  150. {
  151. JSBVectorType* vtype = ptype->type_->asVectorType();
  152. source.AppendWithFormat("%s __arg%i;\n", vtype->ToString().CString(), cparam);
  153. source.AppendWithFormat("if (duk_get_top(ctx) >= %i)\n{\n", cparam + 1);
  154. source.AppendWithFormat("duk_require_object_coercible(ctx, %i);\n", cparam);
  155. source.AppendWithFormat("unsigned sz = duk_get_length(ctx, %i);\n", cparam);
  156. source.AppendWithFormat("for (unsigned i = 0; i < sz; i++)\n{\n");
  157. source.AppendWithFormat("duk_get_prop_index(ctx, 2, i);\n");
  158. if (vtype->vectorType_->asStringType() || vtype->vectorType_->asStringHashType() )
  159. {
  160. source.AppendWithFormat("__arg%i.Push(duk_get_string(ctx, -1));\n", cparam);
  161. }
  162. source.AppendWithFormat("duk_pop(ctx);\n");
  163. source.AppendWithFormat("\n}\n");
  164. source.AppendWithFormat("\n}\n");
  165. }
  166. }
  167. }
  168. }
  169. }
  170. void JSBFunctionWriter::WriteConstructor(String& source)
  171. {
  172. // TODO: refactor this
  173. if (function_->name_ == "RefCounted")
  174. {
  175. source.Append("// finalizer may be called more than once\n" \
  176. "static int jsb_finalizer_RefCounted(duk_context *ctx)\n" \
  177. "{\n" \
  178. "JSVM* vm = JSVM::GetJSVM(ctx);\n" \
  179. \
  180. "duk_get_prop_index(ctx, 0, JS_INSTANCE_INDEX_FINALIZED);\n" \
  181. \
  182. "if (!duk_is_boolean(ctx, -1))\n" \
  183. "{\n" \
  184. "RefCounted* ref = vm->GetObjectPtr(duk_get_heapptr(ctx, 0));\n" \
  185. "vm->RemoveObject(ref);\n" \
  186. "ref->ReleaseRef();\n" \
  187. "duk_push_boolean(ctx, 1);\n" \
  188. "duk_put_prop_index(ctx, 0, JS_INSTANCE_INDEX_FINALIZED);\n" \
  189. "}\n" \
  190. \
  191. "return 0;\n" \
  192. "}\n");
  193. }
  194. JSBClass* klass = function_->class_;
  195. JSBClass* base = klass->GetBaseClass();
  196. // Constructor
  197. source.AppendWithFormat("duk_ret_t jsb_constructor_%s(duk_context* ctx)\n{\n", klass->GetName().CString());
  198. /*
  199. duk_ret_t jsb_constructor_MyJSClass(duk_context* ctx)
  200. {
  201. JSVM* vm = JSVM::GetJSVM(ctx);
  202. duk_push_this(ctx);
  203. void *ptr = duk_get_heapptr(ctx, -1);
  204. duk_pop(ctx);
  205. if (!vm->GetObjectPtr(ptr, true))
  206. {
  207. if (!duk_get_top(ctx) || !duk_is_pointer(ctx, 0))
  208. {
  209. MyJSClass* native = new MyJSClass(JSVM::GetJSVM(ctx)->GetContext());
  210. vm->AddObject(ptr, native);
  211. }
  212. else if (duk_is_pointer(ctx, 0))
  213. {
  214. vm->AddObject(ptr, (RefCounted*) duk_get_pointer(ctx, 0));
  215. }
  216. }
  217. js_constructor_basecall(ctx, "Atomic", "AObject");
  218. return 0;
  219. }
  220. */
  221. source.Append( "\nJSVM* vm = JSVM::GetJSVM(ctx);\n" \
  222. "duk_push_this(ctx);\n" \
  223. "void *ptr = duk_get_heapptr(ctx, -1);\n" \
  224. "duk_pop(ctx);\n\n");
  225. source.Append(" if (!vm->GetObjectPtr(ptr, true))\n {\n");
  226. if (!klass->IsAbstract() && !klass->IsNumberArray())
  227. {
  228. String marshal;
  229. WriteParameterMarshal(marshal);
  230. String sparams;
  231. int cparam = 0;
  232. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  233. for (unsigned i = 0; i < parameters.Size(); i++, cparam++)
  234. {
  235. JSBFunctionType * ptype = parameters.At(i);
  236. String sarg;
  237. if (ptype->type_->asClassType())
  238. {
  239. JSBClassType* classType = ptype->type_->asClassType();
  240. JSBClass* klass = classType->class_;
  241. if (klass->GetName() == "Context")
  242. {
  243. sarg = "vm->GetContext()";
  244. cparam--;
  245. }
  246. }
  247. if (!sarg.Length())
  248. {
  249. sarg.AppendWithFormat("__arg%i", cparam);
  250. }
  251. sparams += sarg;
  252. if (i + 1 < parameters.Size())
  253. sparams += ", ";
  254. }
  255. source.AppendWithFormat("if (!duk_get_top(ctx) || !duk_is_pointer(ctx, 0))\n"\
  256. "{\n"\
  257. "%s\n"\
  258. "%s* native = new %s(%s);\n" \
  259. "vm->AddObject(ptr, native);\n"\
  260. "}\n" \
  261. "else if (duk_is_pointer(ctx, 0))\n" \
  262. "{\n" \
  263. "vm->AddObject(ptr, (RefCounted*) duk_get_pointer(ctx, 0));\n" \
  264. "}\n", marshal.CString(), klass->GetNativeName().CString(), klass->GetNativeName().CString(), sparams.CString());
  265. }
  266. else
  267. {
  268. if (klass->IsAbstract())
  269. source.Append("assert(0); // abstract class new'd\n");
  270. if (klass->IsNumberArray())
  271. source.Append("assert(0); // number array class new'd\n");
  272. }
  273. source.Append(" }\n");
  274. if (base)
  275. {
  276. String basePackage = base->GetModule()->GetPackage()->GetName();
  277. source.AppendWithFormat(" js_constructor_basecall(ctx, \"%s\", \"%s\");\n", basePackage.CString(), base->GetName().CString());
  278. }
  279. if (function_->name_ == "RefCounted")
  280. {
  281. source.Append("duk_push_this(ctx);\n "\
  282. "duk_push_c_function(ctx, jsb_finalizer_RefCounted, 1);\n "\
  283. "duk_set_finalizer(ctx, -2);\n "\
  284. \
  285. "RefCounted* ref = JSVM::GetJSVM(ctx)->GetObjectPtr(duk_get_heapptr(ctx, -1));\n "\
  286. "ref->AddRef();\n "\
  287. \
  288. "duk_pop(ctx);\n");
  289. }
  290. source += " return 0;";
  291. source += "\n}\n";
  292. }
  293. void JSBFunctionWriter::WriteFunction(String& source)
  294. {
  295. JSBClass* klass = function_->class_;
  296. source.AppendWithFormat("static int jsb_class_%s_%s(duk_context* ctx)\n{\n", klass->GetName().CString(), function_->name_.CString());
  297. WriteParameterMarshal(source);
  298. if (!function_->IsStatic())
  299. {
  300. source.Append("duk_push_this(ctx);\n");
  301. source.AppendWithFormat("%s* native = js_to_class_instance<%s>(ctx, -1, 0);\n", klass->GetNativeName().CString(), klass->GetNativeName().CString());
  302. }
  303. // declare return value;
  304. bool returnDeclared = false;
  305. JSBFunctionType* returnType = function_->returnType_;
  306. if (returnType)
  307. {
  308. if (returnType->type_->asStringType())
  309. {
  310. returnDeclared = true;
  311. source.Append("const String& retValue = ");
  312. }
  313. else if (returnType->type_->asPrimitiveType())
  314. {
  315. returnDeclared = true;
  316. JSBPrimitiveType* prtype = returnType->type_->asPrimitiveType();
  317. if (prtype->kind_ == JSBPrimitiveType::Bool)
  318. {
  319. source.Append("bool retValue = ");
  320. }
  321. else
  322. {
  323. source.Append("double retValue = ");
  324. }
  325. }
  326. else if (returnType->type_->asClassType())
  327. {
  328. JSBClassType* klassType = returnType->type_->asClassType();
  329. if (returnType->isTemplate_)
  330. {
  331. returnDeclared = true;
  332. source.AppendWithFormat("SharedPtr<%s> object = ", klassType->class_->GetNativeName().CString());
  333. }
  334. else if (klassType->class_->IsObject())
  335. {
  336. returnDeclared = true;
  337. source.Append("const Object* object = ");
  338. }
  339. else if (klassType->class_->IsNumberArray())
  340. {
  341. returnDeclared = true;
  342. source.AppendWithFormat("const %s& retValue = ", klassType->class_->GetName().CString());
  343. }
  344. else
  345. {
  346. returnDeclared = true;
  347. source.Append("const RefCounted* object = ");
  348. }
  349. }
  350. else if (returnType->type_->asEnumType())
  351. {
  352. JSBEnumType* enumType = returnType->type_->asEnumType();
  353. returnDeclared = true;
  354. source.AppendWithFormat("%s retValue = ", enumType->enum_->GetName().CString());
  355. }
  356. else if (returnType->type_->asVectorType())
  357. {
  358. returnDeclared = true;
  359. JSBVectorType* vtype = returnType->type_->asVectorType();
  360. source.AppendWithFormat("const %s& retValue = ", vtype->ToString().CString());
  361. }
  362. }
  363. if (function_->IsStatic())
  364. {
  365. source.AppendWithFormat("%s::%s(", klass->GetNativeName().CString(), function_->name_.CString());
  366. }
  367. else
  368. {
  369. source.AppendWithFormat("native->%s(", function_->name_.CString());
  370. }
  371. Vector<JSBFunctionType*>& parameters = function_->GetParameters();
  372. for (unsigned int i = 0; i < parameters.Size(); i++)
  373. {
  374. source.AppendWithFormat("__arg%i", i);
  375. if (i != parameters.Size() - 1)
  376. {
  377. source += ", ";
  378. }
  379. }
  380. source += ");\n";
  381. if (returnDeclared)
  382. {
  383. if (returnType->type_->asStringType())
  384. {
  385. source.Append("duk_push_string(ctx, retValue.CString());\n");
  386. }
  387. else if (returnType->type_->asPrimitiveType())
  388. {
  389. JSBPrimitiveType* prtype = returnType->type_->asPrimitiveType();
  390. if (prtype->kind_ == JSBPrimitiveType::Bool)
  391. {
  392. source.Append("duk_push_boolean(ctx, retValue ? 1 : 0);\n");
  393. }
  394. else
  395. {
  396. source.Append("duk_push_number(ctx, retValue);\n");
  397. }
  398. }
  399. else if (returnType->type_->asClassType())
  400. {
  401. JSBClassType* klassType = returnType->type_->asClassType();
  402. if (klassType->class_->IsObject())
  403. {
  404. returnDeclared = true;
  405. source.Append("js_push_class_object_instance(ctx, object);\n");
  406. }
  407. else if (klassType->class_->IsNumberArray())
  408. {
  409. returnDeclared = true;
  410. String elementType = klassType->class_->GetArrayElementType();
  411. source.AppendWithFormat("const %s* arrayData = retValue.Data();\n", elementType.CString());
  412. source.Append("duk_push_array(ctx);\n");
  413. for (int i = 0; i < klassType->class_->GetNumberArrayElements(); i++)
  414. {
  415. source.AppendWithFormat("duk_push_number(ctx, arrayData[%i]);\n", i);
  416. source.AppendWithFormat("duk_put_prop_index(ctx, -2, %i);\n", i);
  417. }
  418. }
  419. else
  420. {
  421. returnDeclared = true;
  422. source.AppendWithFormat("js_push_class_object_instance(ctx, object, \"%s\");\n", klassType->class_->GetName().CString());
  423. }
  424. }
  425. else if (returnType->type_->asEnumType())
  426. {
  427. returnDeclared = true;
  428. source.Append("duk_push_number(ctx, (double) retValue);\n");
  429. }
  430. else if (returnType->type_->asVectorType())
  431. {
  432. source.Append("duk_push_array(ctx);\n");
  433. source.Append("for (unsigned i = 0; i < retValue.Size(); i++)\n{\n");
  434. source.Append("duk_push_string(ctx, retValue[i].CString());\n");
  435. source.Append("duk_put_prop_index(ctx, -2, i);\n}\n");
  436. }
  437. source += "return 1;\n";
  438. }
  439. else
  440. {
  441. source += "return 0;\n";
  442. }
  443. source.Append("}\n");
  444. }
  445. void JSBFunctionWriter::GenerateSource(String& sourceOut)
  446. {
  447. String source = "";
  448. if (function_->IsConstructor())
  449. {
  450. WriteConstructor(source);
  451. }
  452. else
  453. {
  454. WriteFunction(source);
  455. }
  456. sourceOut += source;
  457. }
  458. }