JSAPI.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. duk_bool_t js_check_is_buffer_and_get_data(duk_context* ctx, duk_idx_t idx, void** data, duk_size_t* size)
  176. {
  177. void* temp;
  178. if (duk_is_buffer(ctx, idx))
  179. {
  180. temp = duk_get_buffer_data(ctx, idx, size);
  181. if (data)
  182. {
  183. *data = temp;
  184. }
  185. return true;
  186. }
  187. if (!(duk_is_object(ctx, idx) &&
  188. duk_has_prop_string(ctx, idx, "length") &&
  189. duk_has_prop_string(ctx, idx, "byteLength") &&
  190. duk_has_prop_string(ctx, idx, "byteOffset") &&
  191. duk_has_prop_string(ctx, idx, "BYTES_PER_ELEMENT")))
  192. {
  193. if (data)
  194. {
  195. *data = nullptr;
  196. }
  197. if (size)
  198. {
  199. *size = 0;
  200. }
  201. return false;
  202. }
  203. temp = duk_require_buffer_data(ctx, idx, size);
  204. if (data)
  205. {
  206. *data = temp;
  207. }
  208. return true;
  209. }
  210. void js_to_variant(duk_context* ctx, int variantIdx, Variant &v)
  211. {
  212. v.Clear();
  213. // convert to abs index
  214. if (variantIdx < 0)
  215. variantIdx = duk_get_top(ctx) + variantIdx;
  216. if (duk_is_boolean(ctx, variantIdx))
  217. {
  218. v = duk_to_boolean(ctx, variantIdx) ? true : false;
  219. return;
  220. }
  221. if (duk_is_string(ctx, variantIdx))
  222. {
  223. v = duk_to_string(ctx, variantIdx);
  224. return;
  225. }
  226. if (duk_is_number(ctx, variantIdx))
  227. {
  228. v = (float) duk_to_number(ctx, variantIdx);
  229. return;
  230. }
  231. if (duk_is_pointer(ctx, variantIdx))
  232. {
  233. v = (RefCounted*) duk_get_pointer(ctx, variantIdx);
  234. return;
  235. }
  236. if (duk_is_array(ctx, variantIdx))
  237. {
  238. if (duk_get_length(ctx, variantIdx) == 2)
  239. {
  240. Vector2 v2;
  241. duk_get_prop_index(ctx, variantIdx, 0);
  242. v2.x_ = duk_to_number(ctx, -1);
  243. duk_get_prop_index(ctx, variantIdx, 1);
  244. v2.y_ = duk_to_number(ctx, -1);
  245. duk_pop_n(ctx, 2);
  246. v = v2;
  247. return;
  248. }
  249. else if (duk_get_length(ctx, variantIdx) == 3)
  250. {
  251. Vector3 v3;
  252. duk_get_prop_index(ctx, variantIdx, 0);
  253. v3.x_ = duk_to_number(ctx, -1);
  254. duk_get_prop_index(ctx, variantIdx, 1);
  255. v3.y_ = duk_to_number(ctx, -1);
  256. duk_get_prop_index(ctx, variantIdx, 2);
  257. v3.z_ = duk_to_number(ctx, -1);
  258. duk_pop_n(ctx, 3);
  259. v = v3;
  260. return;
  261. }
  262. else if (duk_get_length(ctx, variantIdx) == 4)
  263. {
  264. Vector4 v4;
  265. duk_get_prop_index(ctx, variantIdx, 0);
  266. v4.x_ = duk_to_number(ctx, -1);
  267. duk_get_prop_index(ctx, variantIdx, 1);
  268. v4.y_ = duk_to_number(ctx, -1);
  269. duk_get_prop_index(ctx, variantIdx, 2);
  270. v4.z_ = duk_to_number(ctx, -1);
  271. duk_get_prop_index(ctx, variantIdx, 3);
  272. v4.w_ = duk_to_number(ctx, -1);
  273. duk_pop_n(ctx, 4);
  274. v = v4;
  275. return;
  276. }
  277. return;
  278. }
  279. {
  280. void* bufferData;
  281. duk_size_t bufferSize;
  282. if (js_check_is_buffer_and_get_data(ctx, variantIdx, &bufferData, &bufferSize))
  283. {
  284. // copy the buffer into the variant
  285. v.SetBuffer(bufferData, (unsigned)bufferSize);
  286. return;
  287. }
  288. }
  289. // object check after array and buffer object check
  290. if (duk_is_object(ctx, variantIdx))
  291. {
  292. RefCounted* o = js_to_class_instance<RefCounted>(ctx, variantIdx, 0);
  293. if (o)
  294. v = o;
  295. return;
  296. }
  297. }
  298. // variant map Proxy getter, so we can convert access to string based
  299. // member lookup, to string hash on the fly
  300. static int variantmap_property_get(duk_context* ctx)
  301. {
  302. // targ, key, recv
  303. if (duk_is_string(ctx, 1))
  304. {
  305. StringHash key = duk_to_string(ctx, 1);
  306. duk_get_prop_index(ctx, 0, (unsigned) key.Value());
  307. return 1;
  308. }
  309. duk_push_undefined(ctx);
  310. return 1;
  311. }
  312. // removes all keys from the variant map proxy target, REGARDLESS of key given for delete
  313. // see (lengthy) note in JSEventDispatcher::EndSendEvent
  314. static int variantmap_property_deleteproperty(duk_context* ctx)
  315. {
  316. // deleteProperty: function (targ, key)
  317. duk_enum(ctx, 0, DUK_ENUM_OWN_PROPERTIES_ONLY);
  318. while (duk_next(ctx, -1, 0)) {
  319. duk_del_prop(ctx, 0);
  320. }
  321. duk_push_boolean(ctx, 1);
  322. return 1;
  323. }
  324. void js_push_variantmap(duk_context* ctx, const VariantMap &vmap)
  325. {
  326. // setup proxy so we can map string
  327. duk_get_global_string(ctx, "Proxy");
  328. duk_push_object(ctx);
  329. VariantMap::ConstIterator itr = vmap.Begin();
  330. while (itr != vmap.End()) {
  331. js_push_variant(ctx, itr->second_);
  332. if (duk_is_undefined(ctx, -1)) {
  333. duk_pop(ctx);
  334. }
  335. else
  336. {
  337. duk_put_prop_index(ctx, -2, (unsigned) itr->first_.Value());
  338. }
  339. itr++;
  340. }
  341. // setup property handler
  342. duk_push_object(ctx);
  343. duk_push_c_function(ctx, variantmap_property_get, 3);
  344. duk_put_prop_string(ctx, -2, "get");
  345. duk_push_c_function(ctx, variantmap_property_deleteproperty, 2);
  346. duk_put_prop_string(ctx, -2, "deleteProperty");
  347. duk_new(ctx, 2);
  348. }
  349. void js_push_variant(duk_context *ctx, const Variant& v)
  350. {
  351. switch (v.GetType())
  352. {
  353. case VAR_NONE:
  354. duk_push_undefined(ctx);
  355. break;
  356. case VAR_VOIDPTR:
  357. duk_push_null(ctx);
  358. break;
  359. case VAR_PTR:
  360. {
  361. RefCounted* ref = v.GetPtr();
  362. // if we're null or don't have any refs, return null
  363. if (!ref || !ref->Refs())
  364. {
  365. duk_push_null(ctx);
  366. break;
  367. }
  368. // check that class is supported
  369. duk_push_heap_stash(ctx);
  370. duk_push_pointer(ctx, (void*)ref->GetClassID());
  371. duk_get_prop(ctx, -2);
  372. if (!duk_is_object(ctx, -1))
  373. {
  374. duk_pop_2(ctx);
  375. duk_push_undefined(ctx);
  376. }
  377. else
  378. {
  379. duk_pop_2(ctx);
  380. js_push_class_object_instance(ctx, ref);
  381. }
  382. } break;
  383. case VAR_RESOURCEREF:
  384. {
  385. const ResourceRef& resourceRef(v.GetResourceRef());
  386. ResourceCache* cache = JSVM::GetJSVM(ctx)->GetContext()->GetSubsystem<ResourceCache>();
  387. Resource* resource = cache->GetResource(resourceRef.type_, resourceRef.name_);
  388. js_push_class_object_instance(ctx, resource);
  389. } break;
  390. case VAR_BOOL:
  391. duk_push_boolean(ctx, v.GetBool() ? 1 : 0);
  392. break;
  393. case VAR_INT:
  394. duk_push_number(ctx, v.GetInt());
  395. break;
  396. case VAR_FLOAT:
  397. duk_push_number(ctx, v.GetFloat());
  398. break;
  399. case VAR_STRING:
  400. {
  401. const String& string(v.GetString());
  402. duk_push_lstring(ctx, string.CString(), string.Length());
  403. } break;
  404. case VAR_BUFFER:
  405. {
  406. const PODVector<unsigned char>& buffer(v.GetBuffer()); // The braces are to scope this reference.
  407. duk_push_fixed_buffer(ctx, buffer.Size());
  408. duk_push_buffer_object(ctx, -1, 0, buffer.Size(), DUK_BUFOBJ_UINT8ARRAY);
  409. duk_replace(ctx, -2);
  410. unsigned char* data = (unsigned char*)duk_require_buffer_data(ctx, -1, (duk_size_t*)nullptr);
  411. memcpy(data, buffer.Buffer(), buffer.Size());
  412. } break;
  413. case VAR_VECTOR2:
  414. {
  415. const Vector2& vector2(v.GetVector2());
  416. duk_push_array(ctx);
  417. duk_push_number(ctx, vector2.x_);
  418. duk_put_prop_index(ctx, -2, 0);
  419. duk_push_number(ctx, vector2.y_);
  420. duk_put_prop_index(ctx, -2, 1);
  421. } break;
  422. case VAR_INTVECTOR2:
  423. {
  424. const IntVector2& intVector2(v.GetIntVector2());
  425. duk_push_array(ctx);
  426. duk_push_number(ctx, intVector2.x_);
  427. duk_put_prop_index(ctx, -2, 0);
  428. duk_push_number(ctx, intVector2.y_);
  429. duk_put_prop_index(ctx, -2, 1);
  430. } break;
  431. case VAR_VECTOR3:
  432. {
  433. const Vector3& vector3(v.GetVector3());
  434. duk_push_array(ctx);
  435. duk_push_number(ctx, vector3.x_);
  436. duk_put_prop_index(ctx, -2, 0);
  437. duk_push_number(ctx, vector3.y_);
  438. duk_put_prop_index(ctx, -2, 1);
  439. duk_push_number(ctx, vector3.z_);
  440. duk_put_prop_index(ctx, -2, 2);
  441. } break;
  442. case VAR_QUATERNION:
  443. {
  444. const Vector3& vector3(v.GetQuaternion().EulerAngles());
  445. duk_push_array(ctx);
  446. duk_push_number(ctx, vector3.x_);
  447. duk_put_prop_index(ctx, -2, 0);
  448. duk_push_number(ctx, vector3.y_);
  449. duk_put_prop_index(ctx, -2, 1);
  450. duk_push_number(ctx, vector3.z_);
  451. duk_put_prop_index(ctx, -2, 2);
  452. } break;
  453. case VAR_COLOR:
  454. {
  455. const Color& color(v.GetColor());
  456. duk_push_array(ctx);
  457. duk_push_number(ctx, color.r_);
  458. duk_put_prop_index(ctx, -2, 0);
  459. duk_push_number(ctx, color.g_);
  460. duk_put_prop_index(ctx, -2, 1);
  461. duk_push_number(ctx, color.b_);
  462. duk_put_prop_index(ctx, -2, 2);
  463. duk_push_number(ctx, color.a_);
  464. duk_put_prop_index(ctx, -2, 3);
  465. } break;
  466. case VAR_VECTOR4:
  467. {
  468. const Vector4& vector4(v.GetVector4());
  469. duk_push_array(ctx);
  470. duk_push_number(ctx, vector4.x_);
  471. duk_put_prop_index(ctx, -2, 0);
  472. duk_push_number(ctx, vector4.y_);
  473. duk_put_prop_index(ctx, -2, 1);
  474. duk_push_number(ctx, vector4.z_);
  475. duk_put_prop_index(ctx, -2, 2);
  476. duk_push_number(ctx, vector4.w_);
  477. duk_put_prop_index(ctx, -2, 3);
  478. } break;
  479. default:
  480. duk_push_undefined(ctx);
  481. break;
  482. }
  483. }
  484. }