JSAPI.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include "JSAPI.h"
  5. #include "JSVM.h"
  6. /*
  7. // script can new an instance
  8. // script can get an engine ptr back (which may be to a previously script new'd instance)
  9. // script should always get the same JS Object back which means a lookup table (strong ref)
  10. // we can look directly at the tval (exposed from duktape internals) to see if the reference count is 1
  11. // script owned
  12. // new'd script side, script holds a RefCounted reference
  13. // native owned
  14. // native script classes not derived from refcounted
  15. // if not inherited from RefCounted can script access?
  16. // if so, script side will always need to delete?
  17. */
  18. namespace Atomic
  19. {
  20. void js_class_get_prototype(duk_context* ctx, const char* package, const char *classname)
  21. {
  22. duk_get_global_string(ctx, package);
  23. duk_get_prop_string(ctx, -1, classname);
  24. duk_get_prop_string(ctx, -1, "prototype");
  25. duk_remove(ctx, -2); // remove class object
  26. duk_remove(ctx, -2); // remove Atomic object
  27. }
  28. void js_class_get_constructor(duk_context* ctx, const char* package, const char *classname)
  29. {
  30. duk_get_global_string(ctx, package);
  31. duk_get_prop_string(ctx, -1, classname);
  32. duk_remove(ctx, -2); // remove package
  33. }
  34. void js_constructor_basecall(duk_context* ctx, const char* package, const char* baseclass)
  35. {
  36. int top = duk_get_top(ctx);
  37. duk_get_global_string(ctx, package);
  38. duk_get_prop_string(ctx, -1, baseclass);
  39. assert(duk_is_function(ctx, -1));
  40. duk_push_this(ctx);
  41. duk_call_method(ctx, 0);
  42. duk_pop_n(ctx, 2);
  43. assert (top == duk_get_top(ctx));
  44. }
  45. void js_class_declare_internal(JSVM* vm, void* uniqueClassID, const char* package, const char* classname, duk_c_function constructor)
  46. {
  47. duk_context* ctx = vm->GetJSContext();
  48. // stash a lookup from the uniqueID to the package name
  49. // (NULL) == non-object, so core "Atomic" package
  50. if (uniqueClassID)
  51. {
  52. duk_push_heap_stash(ctx);
  53. duk_push_pointer(ctx, uniqueClassID);
  54. duk_push_string(ctx, package);
  55. duk_put_prop(ctx, -3);
  56. duk_pop(ctx);
  57. }
  58. if (!duk_get_global_string(ctx, package))
  59. {
  60. // Failure leaves undefined on the stack
  61. duk_pop(ctx);
  62. // Create a new namespace object for the package
  63. duk_push_object(ctx);
  64. duk_put_global_string(ctx, package);
  65. duk_get_global_string(ctx, package);
  66. }
  67. duk_push_c_function(ctx, constructor, DUK_VARARGS);
  68. duk_put_prop_string(ctx, -2, classname);
  69. duk_pop(ctx);
  70. }
  71. void js_class_push_propertyobject(JSVM* vm, const char* package, const char* classname)
  72. {
  73. duk_context* ctx = vm->GetJSContext();
  74. String pname;
  75. pname.AppendWithFormat("__%s__Properties", classname);
  76. duk_get_global_string(ctx, package);
  77. duk_push_object(ctx);
  78. duk_dup(ctx, -1);
  79. duk_put_prop_string(ctx, -3, pname.CString());
  80. duk_remove(ctx, -2); // remove Atomic object
  81. }
  82. void js_setup_prototype(JSVM* vm, const char* package, const char* classname, const char* basePackage, const char* basename, bool hasProperties)
  83. {
  84. duk_context* ctx = vm->GetJSContext();
  85. String pname;
  86. pname.AppendWithFormat("__%s__Properties", classname);
  87. int top = duk_get_top(ctx);
  88. duk_get_global_string(ctx,package);
  89. duk_get_prop_string(ctx, -1, classname);
  90. assert(duk_is_c_function(ctx, -1));
  91. if (!strlen(basename))
  92. {
  93. // prototype
  94. duk_push_object(ctx);
  95. duk_dup(ctx, -2); // AObject constructor function
  96. duk_put_prop_string(ctx, -2, "constructor");
  97. duk_put_prop_string(ctx, -2, "prototype");
  98. duk_pop_n(ctx, 2);
  99. assert (top == duk_get_top(ctx));
  100. return;
  101. }
  102. // prototype
  103. duk_get_global_string(ctx, "Object");
  104. duk_get_prop_string(ctx, -1, "create");
  105. assert(duk_is_function(ctx, -1));
  106. duk_remove(ctx, -2); // remove Object
  107. duk_get_global_string(ctx, basePackage);
  108. duk_get_prop_string(ctx, -1, basename);
  109. assert(duk_is_function(ctx, -1));
  110. duk_get_prop_string(ctx, -1, "prototype");
  111. assert(duk_is_object(ctx, -1));
  112. duk_remove(ctx, -2); // remove basename
  113. int numargs = 1;
  114. if (hasProperties)
  115. {
  116. duk_get_global_string(ctx, package);
  117. duk_get_prop_string(ctx, -1, pname.CString());
  118. assert(duk_is_object(ctx, -1));
  119. duk_remove(ctx, -2);
  120. duk_remove(ctx, -3); // remove package
  121. numargs++;
  122. }
  123. else
  124. duk_remove(ctx, -2); // remove package
  125. duk_call(ctx, numargs);
  126. assert(duk_is_object(ctx, -1));
  127. duk_dup(ctx, -2);
  128. duk_put_prop_string(ctx, -2, "constructor");
  129. //duk_dup(ctx, -1);
  130. duk_put_prop_string(ctx, -2, "prototype");
  131. // pop the classname object
  132. duk_pop(ctx);
  133. // pop the Atomic Object
  134. duk_pop(ctx);
  135. assert (top == duk_get_top(ctx));
  136. }
  137. void js_object_to_variantmap(duk_context* ctx, int objIdx, VariantMap &v)
  138. {
  139. v.Clear();
  140. duk_enum(ctx, objIdx, DUK_ENUM_OWN_PROPERTIES_ONLY);
  141. while (duk_next(ctx, -1 /*enum_index*/, 1 /*get_value*/)) {
  142. /* [ ... enum key ] */
  143. const char* key = duk_to_string(ctx, -2);
  144. if (duk_is_number(ctx, -1)) {
  145. v[key] = (float) duk_to_number(ctx, -1);
  146. } else if (duk_is_boolean(ctx, -1)) {
  147. v[key] = duk_to_boolean(ctx, -1) ? true : false;
  148. }
  149. else if (duk_is_string(ctx, -1)) {
  150. v[key] = duk_to_string(ctx, -1);
  151. } else if (duk_get_heapptr(ctx, -1)) {
  152. v[key] = js_to_class_instance<Object>(ctx, -1, 0);
  153. }
  154. duk_pop_2(ctx); /* pop_key & value*/
  155. }
  156. duk_pop(ctx); /* pop enum object */
  157. }
  158. void js_to_variant(duk_context* ctx, int variantIdx, Variant &v)
  159. {
  160. v.Clear();
  161. // convert to abs index
  162. if (variantIdx < 0)
  163. variantIdx = duk_get_top(ctx) + variantIdx;
  164. if (duk_is_boolean(ctx, variantIdx))
  165. {
  166. v = duk_to_boolean(ctx, variantIdx) ? true : false;
  167. return;
  168. }
  169. if (duk_is_string(ctx, variantIdx))
  170. {
  171. v = duk_to_string(ctx, variantIdx);
  172. return;
  173. }
  174. if (duk_is_number(ctx, variantIdx))
  175. {
  176. v = (float) duk_to_number(ctx, variantIdx);
  177. return;
  178. }
  179. if (duk_is_pointer(ctx, variantIdx))
  180. {
  181. v = (RefCounted*) duk_get_pointer(ctx, variantIdx);
  182. return;
  183. }
  184. if (duk_is_array(ctx, variantIdx))
  185. {
  186. if (duk_get_length(ctx, variantIdx) == 2)
  187. {
  188. Vector2 v2;
  189. duk_get_prop_index(ctx, variantIdx, 0);
  190. v2.x_ = duk_to_number(ctx, -1);
  191. duk_get_prop_index(ctx, variantIdx, 1);
  192. v2.y_ = duk_to_number(ctx, -1);
  193. duk_pop_n(ctx, 2);
  194. v = v2;
  195. return;
  196. }
  197. else if (duk_get_length(ctx, variantIdx) == 3)
  198. {
  199. Vector3 v3;
  200. duk_get_prop_index(ctx, variantIdx, 0);
  201. v3.x_ = duk_to_number(ctx, -1);
  202. duk_get_prop_index(ctx, variantIdx, 1);
  203. v3.y_ = duk_to_number(ctx, -1);
  204. duk_get_prop_index(ctx, variantIdx, 2);
  205. v3.z_ = duk_to_number(ctx, -1);
  206. duk_pop_n(ctx, 3);
  207. v = v3;
  208. return;
  209. }
  210. else if (duk_get_length(ctx, variantIdx) == 4)
  211. {
  212. Vector4 v4;
  213. duk_get_prop_index(ctx, variantIdx, 0);
  214. v4.x_ = duk_to_number(ctx, -1);
  215. duk_get_prop_index(ctx, variantIdx, 1);
  216. v4.y_ = duk_to_number(ctx, -1);
  217. duk_get_prop_index(ctx, variantIdx, 2);
  218. v4.z_ = duk_to_number(ctx, -1);
  219. duk_get_prop_index(ctx, variantIdx, 3);
  220. v4.w_ = duk_to_number(ctx, -1);
  221. duk_pop_n(ctx, 4);
  222. v = v4;
  223. return;
  224. }
  225. return;
  226. }
  227. }
  228. // variant map Proxy getter, so we can convert access to string based
  229. // member lookup, to string hash on the fly
  230. static int variantmap_property_get(duk_context* ctx)
  231. {
  232. // targ, key, recv
  233. if (duk_is_string(ctx, 1))
  234. {
  235. StringHash key = duk_to_string(ctx, 1);
  236. duk_get_prop_index(ctx, 0, (unsigned) key.Value());
  237. return 1;
  238. }
  239. duk_push_undefined(ctx);
  240. return 1;
  241. }
  242. // removes all keys from the variant map proxy target, REGARDLESS of key given for delete
  243. // see (lengthy) note in JSEventDispatcher::EndSendEvent
  244. static int variantmap_property_deleteproperty(duk_context* ctx)
  245. {
  246. // deleteProperty: function (targ, key)
  247. duk_enum(ctx, 0, DUK_ENUM_OWN_PROPERTIES_ONLY);
  248. while (duk_next(ctx, -1, 0)) {
  249. duk_del_prop(ctx, 0);
  250. }
  251. duk_push_boolean(ctx, 1);
  252. return 1;
  253. }
  254. void js_push_variantmap(duk_context* ctx, const VariantMap &vmap)
  255. {
  256. // setup proxy so we can map string
  257. duk_get_global_string(ctx, "Proxy");
  258. duk_push_object(ctx);
  259. VariantMap::ConstIterator itr = vmap.Begin();
  260. while (itr != vmap.End()) {
  261. js_push_variant(ctx, itr->second_);
  262. if (duk_is_undefined(ctx, -1)) {
  263. duk_pop(ctx);
  264. }
  265. else
  266. {
  267. duk_put_prop_index(ctx, -2, (unsigned) itr->first_.Value());
  268. }
  269. itr++;
  270. }
  271. // setup property handler
  272. duk_push_object(ctx);
  273. duk_push_c_function(ctx, variantmap_property_get, 3);
  274. duk_put_prop_string(ctx, -2, "get");
  275. duk_push_c_function(ctx, variantmap_property_deleteproperty, 2);
  276. duk_put_prop_string(ctx, -2, "deleteProperty");
  277. duk_new(ctx, 2);
  278. }
  279. void js_push_variant(duk_context *ctx, const Variant& v)
  280. {
  281. VariantType type = v.GetType();
  282. RefCounted* ref;
  283. Object* object;
  284. Vector2 vector2 = Vector2::ZERO;
  285. Vector3 vector3 = Vector3::ZERO;
  286. Vector4 vector4 = Vector4::ZERO;
  287. Color color = Color::BLACK;
  288. void* uniqueClassID = NULL;
  289. const char* package = NULL;
  290. switch (type)
  291. {
  292. case VAR_NONE:
  293. duk_push_undefined(ctx);
  294. break;
  295. case VAR_VOIDPTR:
  296. duk_push_null(ctx);
  297. break;
  298. case VAR_PTR:
  299. ref = v.GetPtr();
  300. if (!ref || !ref->IsObject() || !ref->Refs())
  301. {
  302. duk_push_null(ctx);
  303. break;
  304. }
  305. object = (Object*) ref;
  306. // check that class is supported
  307. uniqueClassID = (void *) object->GetTypeName().CString();
  308. duk_push_heap_stash(ctx);
  309. duk_push_pointer(ctx, uniqueClassID);
  310. duk_get_prop(ctx, -2);
  311. package = duk_to_string(ctx, -1);
  312. duk_pop_2(ctx);
  313. // check that class is supported
  314. duk_get_global_string(ctx, package);
  315. // will not handle renamed classes!
  316. duk_get_prop_string(ctx, -1, object->GetTypeName().CString());
  317. if (!duk_is_function(ctx, -1))
  318. object = NULL;
  319. duk_pop_n(ctx, 2);
  320. if (object)
  321. js_push_class_object_instance(ctx, object);
  322. else
  323. duk_push_undefined(ctx);
  324. break;
  325. case VAR_BOOL:
  326. duk_push_boolean(ctx, v.GetBool() ? 1 : 0);
  327. break;
  328. case VAR_INT:
  329. duk_push_number(ctx, v.GetInt());
  330. break;
  331. case VAR_FLOAT:
  332. duk_push_number(ctx, v.GetFloat());
  333. break;
  334. case VAR_STRING:
  335. duk_push_string(ctx, v.GetString().CString());
  336. break;
  337. case VAR_VECTOR2:
  338. vector2 = v.GetVector2();
  339. duk_push_array(ctx);
  340. duk_push_number(ctx, vector2.x_);
  341. duk_put_prop_index(ctx, -2, 0);
  342. duk_push_number(ctx, vector2.y_);
  343. duk_put_prop_index(ctx, -2, 1);
  344. break;
  345. case VAR_VECTOR3:
  346. vector3 = v.GetVector3();
  347. duk_push_array(ctx);
  348. duk_push_number(ctx, vector3.x_);
  349. duk_put_prop_index(ctx, -2, 0);
  350. duk_push_number(ctx, vector3.y_);
  351. duk_put_prop_index(ctx, -2, 1);
  352. duk_push_number(ctx, vector3.z_);
  353. duk_put_prop_index(ctx, -2, 2);
  354. break;
  355. case VAR_QUATERNION:
  356. vector3 = v.GetQuaternion().EulerAngles();
  357. duk_push_array(ctx);
  358. duk_push_number(ctx, vector3.x_);
  359. duk_put_prop_index(ctx, -2, 0);
  360. duk_push_number(ctx, vector3.y_);
  361. duk_put_prop_index(ctx, -2, 1);
  362. duk_push_number(ctx, vector3.z_);
  363. duk_put_prop_index(ctx, -2, 2);
  364. break;
  365. case VAR_COLOR:
  366. color = v.GetColor();
  367. duk_push_array(ctx);
  368. duk_push_number(ctx, color.r_);
  369. duk_put_prop_index(ctx, -2, 0);
  370. duk_push_number(ctx, color.g_);
  371. duk_put_prop_index(ctx, -2, 1);
  372. duk_push_number(ctx, color.b_);
  373. duk_put_prop_index(ctx, -2, 2);
  374. duk_push_number(ctx, color.a_);
  375. duk_put_prop_index(ctx, -2, 3);
  376. break;
  377. case VAR_VECTOR4:
  378. vector4 = v.GetVector4();
  379. duk_push_array(ctx);
  380. duk_push_number(ctx, vector4.x_);
  381. duk_put_prop_index(ctx, -2, 0);
  382. duk_push_number(ctx, vector4.y_);
  383. duk_put_prop_index(ctx, -2, 1);
  384. duk_push_number(ctx, vector4.z_);
  385. duk_put_prop_index(ctx, -2, 2);
  386. duk_push_number(ctx, vector4.w_);
  387. duk_put_prop_index(ctx, -2, 3);
  388. break;
  389. default:
  390. duk_push_undefined(ctx);
  391. break;
  392. }
  393. }
  394. }