JSBFunction.cpp 16 KB

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