JSAPI.cpp 14 KB

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