JSAPI.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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, VariantType variantType)
  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. if (variantType == VAR_RESOURCEREFLIST)
  293. {
  294. ResourceRefList refList;
  295. duk_get_prop_string(ctx, variantIdx, "typeName");
  296. refList.type_ = duk_to_string(ctx, -1);
  297. duk_get_prop_string(ctx, variantIdx, "resources");
  298. int length = duk_get_length(ctx, -1);
  299. for (int i = 0; i < length; i++) {
  300. duk_get_prop_index(ctx, -1, i);
  301. Resource* resource = NULL;
  302. if (duk_is_object(ctx, -1))
  303. {
  304. resource = js_to_class_instance<Resource>(ctx, -1, 0);
  305. }
  306. if (resource) {
  307. refList.names_.Push(resource->GetName());
  308. }
  309. else
  310. refList.names_.Push(String::EMPTY);
  311. duk_pop(ctx);
  312. }
  313. duk_pop_n(ctx, 2);
  314. v = refList;
  315. }
  316. else
  317. {
  318. RefCounted* o = js_to_class_instance<RefCounted>(ctx, variantIdx, 0);
  319. if (o)
  320. v = o;
  321. }
  322. return;
  323. }
  324. }
  325. // variant map Proxy getter, so we can convert access to string based
  326. // member lookup, to string hash on the fly
  327. static int variantmap_property_get(duk_context* ctx)
  328. {
  329. // targ, key, recv
  330. if (duk_is_string(ctx, 1))
  331. {
  332. StringHash key = duk_to_string(ctx, 1);
  333. duk_get_prop_index(ctx, 0, (unsigned) key.Value());
  334. return 1;
  335. }
  336. duk_push_undefined(ctx);
  337. return 1;
  338. }
  339. // removes all keys from the variant map proxy target, REGARDLESS of key given for delete
  340. // see (lengthy) note in JSEventDispatcher::EndSendEvent
  341. static int variantmap_property_deleteproperty(duk_context* ctx)
  342. {
  343. // deleteProperty: function (targ, key)
  344. duk_enum(ctx, 0, DUK_ENUM_OWN_PROPERTIES_ONLY);
  345. while (duk_next(ctx, -1, 0)) {
  346. duk_del_prop(ctx, 0);
  347. }
  348. duk_push_boolean(ctx, 1);
  349. return 1;
  350. }
  351. void js_push_variantmap(duk_context* ctx, const VariantMap &vmap)
  352. {
  353. // setup proxy so we can map string
  354. duk_get_global_string(ctx, "Proxy");
  355. duk_push_object(ctx);
  356. VariantMap::ConstIterator itr = vmap.Begin();
  357. while (itr != vmap.End()) {
  358. js_push_variant(ctx, itr->second_);
  359. if (duk_is_undefined(ctx, -1)) {
  360. duk_pop(ctx);
  361. }
  362. else
  363. {
  364. duk_put_prop_index(ctx, -2, (unsigned) itr->first_.Value());
  365. }
  366. itr++;
  367. }
  368. // setup property handler
  369. duk_push_object(ctx);
  370. duk_push_c_function(ctx, variantmap_property_get, 3);
  371. duk_put_prop_string(ctx, -2, "get");
  372. duk_push_c_function(ctx, variantmap_property_deleteproperty, 2);
  373. duk_put_prop_string(ctx, -2, "deleteProperty");
  374. duk_new(ctx, 2);
  375. }
  376. void js_push_variant(duk_context *ctx, const Variant& v)
  377. {
  378. switch (v.GetType())
  379. {
  380. case VAR_NONE:
  381. duk_push_undefined(ctx);
  382. break;
  383. case VAR_VOIDPTR:
  384. duk_push_null(ctx);
  385. break;
  386. case VAR_PTR:
  387. {
  388. RefCounted* ref = v.GetPtr();
  389. // if we're null or don't have any refs, return null
  390. if (!ref || !ref->Refs())
  391. {
  392. duk_push_null(ctx);
  393. break;
  394. }
  395. // check that class is supported
  396. duk_push_heap_stash(ctx);
  397. duk_push_pointer(ctx, (void*)ref->GetClassID());
  398. duk_get_prop(ctx, -2);
  399. if (!duk_is_object(ctx, -1))
  400. {
  401. duk_pop_2(ctx);
  402. duk_push_undefined(ctx);
  403. }
  404. else
  405. {
  406. duk_pop_2(ctx);
  407. js_push_class_object_instance(ctx, ref);
  408. }
  409. } break;
  410. case VAR_RESOURCEREF:
  411. {
  412. const ResourceRef& resourceRef(v.GetResourceRef());
  413. ResourceCache* cache = JSVM::GetJSVM(ctx)->GetContext()->GetSubsystem<ResourceCache>();
  414. Resource* resource = cache->GetResource(resourceRef.type_, resourceRef.name_);
  415. js_push_class_object_instance(ctx, resource);
  416. } break;
  417. case VAR_RESOURCEREFLIST:
  418. {
  419. const ResourceRefList& resourceRefList(v.GetResourceRefList());
  420. const Context* context = JSVM::GetJSVM(ctx)->GetContext();
  421. duk_push_object(ctx);
  422. duk_push_string(ctx, context->GetTypeName(resourceRefList.type_).CString());
  423. duk_put_prop_string(ctx, -2, "typeName");
  424. duk_push_array(ctx);
  425. ResourceCache* cache = context->GetSubsystem<ResourceCache>();
  426. for (unsigned i = 0; i < resourceRefList.names_.Size(); i++) {
  427. Resource* resource = cache->GetResource(resourceRefList.type_, resourceRefList.names_[i]);
  428. js_push_class_object_instance(ctx, resource);
  429. duk_put_prop_index(ctx, -2, i);
  430. }
  431. duk_put_prop_string(ctx, -2, "resources");
  432. } break;
  433. case VAR_BOOL:
  434. duk_push_boolean(ctx, v.GetBool() ? 1 : 0);
  435. break;
  436. case VAR_INT:
  437. duk_push_number(ctx, v.GetInt());
  438. break;
  439. case VAR_FLOAT:
  440. duk_push_number(ctx, v.GetFloat());
  441. break;
  442. case VAR_STRING:
  443. {
  444. const String& string(v.GetString());
  445. duk_push_lstring(ctx, string.CString(), string.Length());
  446. } break;
  447. case VAR_BUFFER:
  448. {
  449. const PODVector<unsigned char>& buffer(v.GetBuffer()); // The braces are to scope this reference.
  450. duk_push_fixed_buffer(ctx, buffer.Size());
  451. duk_push_buffer_object(ctx, -1, 0, buffer.Size(), DUK_BUFOBJ_UINT8ARRAY);
  452. duk_replace(ctx, -2);
  453. unsigned char* data = (unsigned char*)duk_require_buffer_data(ctx, -1, (duk_size_t*)nullptr);
  454. memcpy(data, buffer.Buffer(), buffer.Size());
  455. } break;
  456. case VAR_VECTOR2:
  457. {
  458. const Vector2& vector2(v.GetVector2());
  459. duk_push_array(ctx);
  460. duk_push_number(ctx, vector2.x_);
  461. duk_put_prop_index(ctx, -2, 0);
  462. duk_push_number(ctx, vector2.y_);
  463. duk_put_prop_index(ctx, -2, 1);
  464. } break;
  465. case VAR_INTVECTOR2:
  466. {
  467. const IntVector2& intVector2(v.GetIntVector2());
  468. duk_push_array(ctx);
  469. duk_push_number(ctx, intVector2.x_);
  470. duk_put_prop_index(ctx, -2, 0);
  471. duk_push_number(ctx, intVector2.y_);
  472. duk_put_prop_index(ctx, -2, 1);
  473. } break;
  474. case VAR_VECTOR3:
  475. {
  476. const Vector3& vector3(v.GetVector3());
  477. duk_push_array(ctx);
  478. duk_push_number(ctx, vector3.x_);
  479. duk_put_prop_index(ctx, -2, 0);
  480. duk_push_number(ctx, vector3.y_);
  481. duk_put_prop_index(ctx, -2, 1);
  482. duk_push_number(ctx, vector3.z_);
  483. duk_put_prop_index(ctx, -2, 2);
  484. } break;
  485. case VAR_QUATERNION:
  486. {
  487. const Vector3& vector3(v.GetQuaternion().EulerAngles());
  488. duk_push_array(ctx);
  489. duk_push_number(ctx, vector3.x_);
  490. duk_put_prop_index(ctx, -2, 0);
  491. duk_push_number(ctx, vector3.y_);
  492. duk_put_prop_index(ctx, -2, 1);
  493. duk_push_number(ctx, vector3.z_);
  494. duk_put_prop_index(ctx, -2, 2);
  495. } break;
  496. case VAR_COLOR:
  497. {
  498. const Color& color(v.GetColor());
  499. duk_push_array(ctx);
  500. duk_push_number(ctx, color.r_);
  501. duk_put_prop_index(ctx, -2, 0);
  502. duk_push_number(ctx, color.g_);
  503. duk_put_prop_index(ctx, -2, 1);
  504. duk_push_number(ctx, color.b_);
  505. duk_put_prop_index(ctx, -2, 2);
  506. duk_push_number(ctx, color.a_);
  507. duk_put_prop_index(ctx, -2, 3);
  508. } break;
  509. case VAR_VECTOR4:
  510. {
  511. const Vector4& vector4(v.GetVector4());
  512. duk_push_array(ctx);
  513. duk_push_number(ctx, vector4.x_);
  514. duk_put_prop_index(ctx, -2, 0);
  515. duk_push_number(ctx, vector4.y_);
  516. duk_put_prop_index(ctx, -2, 1);
  517. duk_push_number(ctx, vector4.z_);
  518. duk_put_prop_index(ctx, -2, 2);
  519. duk_push_number(ctx, vector4.w_);
  520. duk_put_prop_index(ctx, -2, 3);
  521. } break;
  522. default:
  523. duk_push_undefined(ctx);
  524. break;
  525. }
  526. }
  527. }