JSAPI.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 <Atomic/Script/ScriptVector.h>
  25. #include "JSAPI.h"
  26. #include "JSVM.h"
  27. /*
  28. // script can new an instance
  29. // script can get an engine ptr back (which may be to a previously script new'd instance)
  30. // script should always get the same JS Object back which means a lookup table (strong ref)
  31. // we can look directly at the tval (exposed from duktape internals) to see if the reference count is 1
  32. // script owned
  33. // new'd script side, script holds a RefCounted reference
  34. // native owned
  35. // native script classes not derived from refcounted
  36. // if not inherited from RefCounted can script access?
  37. // if so, script side will always need to delete?
  38. */
  39. namespace Atomic
  40. {
  41. void js_class_get_prototype(duk_context* ctx, const char* package, const char *classname)
  42. {
  43. duk_get_global_string(ctx, package);
  44. duk_get_prop_string(ctx, -1, classname);
  45. duk_get_prop_string(ctx, -1, "prototype");
  46. duk_remove(ctx, -2); // remove class object
  47. duk_remove(ctx, -2); // remove Atomic object
  48. }
  49. void js_class_get_constructor(duk_context* ctx, const char* package, const char *classname)
  50. {
  51. duk_get_global_string(ctx, package);
  52. duk_get_prop_string(ctx, -1, classname);
  53. duk_remove(ctx, -2); // remove package
  54. }
  55. void js_constructor_basecall(duk_context* ctx, const char* package, const char* baseclass)
  56. {
  57. int top = duk_get_top(ctx);
  58. duk_get_global_string(ctx, package);
  59. duk_get_prop_string(ctx, -1, baseclass);
  60. assert(duk_is_function(ctx, -1));
  61. duk_push_this(ctx);
  62. duk_call_method(ctx, 0);
  63. duk_pop_n(ctx, 2);
  64. assert(top == duk_get_top(ctx));
  65. }
  66. void js_class_declare_internal(JSVM* vm, void* uniqueClassID, const char* package, const char* classname, duk_c_function constructor)
  67. {
  68. duk_context* ctx = vm->GetJSContext();
  69. // uniqueClassID must be non-null
  70. assert(uniqueClassID);
  71. // stash a lookup from the uniqueID to the package and class name
  72. duk_push_heap_stash(ctx);
  73. duk_push_pointer(ctx, uniqueClassID);
  74. duk_push_object(ctx);
  75. duk_push_string(ctx, package);
  76. duk_put_prop_index(ctx, -2, 0);
  77. duk_push_string(ctx, classname);
  78. duk_put_prop_index(ctx, -2, 1);
  79. // store class object into uniqueClassID key
  80. duk_put_prop(ctx, -3);
  81. // pop heap stash
  82. duk_pop(ctx);
  83. // store the constructor
  84. duk_get_global_string(ctx, package);
  85. duk_push_c_function(ctx, constructor, DUK_VARARGS);
  86. duk_put_prop_string(ctx, -2, classname);
  87. duk_pop(ctx);
  88. }
  89. void js_class_push_propertyobject(JSVM* vm, const char* package, const char* classname)
  90. {
  91. duk_context* ctx = vm->GetJSContext();
  92. String pname;
  93. pname.AppendWithFormat("__%s__Properties", classname);
  94. duk_get_global_string(ctx, package);
  95. duk_push_object(ctx);
  96. duk_dup(ctx, -1);
  97. duk_put_prop_string(ctx, -3, pname.CString());
  98. duk_remove(ctx, -2); // remove Atomic object
  99. }
  100. void js_setup_prototype(JSVM* vm, const char* package, const char* classname, const char* basePackage, const char* basename, bool hasProperties)
  101. {
  102. duk_context* ctx = vm->GetJSContext();
  103. String pname;
  104. pname.AppendWithFormat("__%s__Properties", classname);
  105. int top = duk_get_top(ctx);
  106. duk_get_global_string(ctx, package);
  107. duk_get_prop_string(ctx, -1, classname);
  108. assert(duk_is_c_function(ctx, -1));
  109. if (!strlen(basename))
  110. {
  111. // prototype
  112. duk_push_object(ctx);
  113. duk_dup(ctx, -2); // AObject constructor function
  114. duk_put_prop_string(ctx, -2, "constructor");
  115. duk_put_prop_string(ctx, -2, "prototype");
  116. duk_pop_n(ctx, 2);
  117. assert(top == duk_get_top(ctx));
  118. return;
  119. }
  120. // prototype
  121. duk_get_global_string(ctx, "Object");
  122. duk_get_prop_string(ctx, -1, "create");
  123. assert(duk_is_function(ctx, -1));
  124. duk_remove(ctx, -2); // remove Object
  125. duk_get_global_string(ctx, basePackage);
  126. duk_get_prop_string(ctx, -1, basename);
  127. assert(duk_is_function(ctx, -1));
  128. duk_get_prop_string(ctx, -1, "prototype");
  129. assert(duk_is_object(ctx, -1));
  130. duk_remove(ctx, -2); // remove basename
  131. int numargs = 1;
  132. if (hasProperties)
  133. {
  134. duk_get_global_string(ctx, package);
  135. duk_get_prop_string(ctx, -1, pname.CString());
  136. assert(duk_is_object(ctx, -1));
  137. duk_remove(ctx, -2);
  138. duk_remove(ctx, -3); // remove package
  139. numargs++;
  140. }
  141. else
  142. duk_remove(ctx, -2); // remove package
  143. duk_call(ctx, numargs);
  144. assert(duk_is_object(ctx, -1));
  145. duk_dup(ctx, -2);
  146. duk_put_prop_string(ctx, -2, "constructor");
  147. //duk_dup(ctx, -1);
  148. duk_put_prop_string(ctx, -2, "prototype");
  149. // pop the classname object
  150. duk_pop(ctx);
  151. // pop the Atomic Object
  152. duk_pop(ctx);
  153. assert(top == duk_get_top(ctx));
  154. }
  155. // When subscribing to native event, this method will be called
  156. // to provide the event meta data (type and callback)
  157. static int js_push_native_event_metadata(duk_context* ctx) {
  158. duk_push_current_function(ctx);
  159. duk_push_object(ctx);
  160. duk_get_prop_string(ctx, -2, "_eventType");
  161. duk_put_prop_string(ctx, -2, "_eventType");
  162. duk_dup(ctx, 0);
  163. duk_put_prop_string(ctx, -2, "_callback");
  164. return 1;
  165. }
  166. void js_define_native_event(duk_context* ctx, const String& eventType, const String &eventName)
  167. {
  168. // push c function which takes 1 argument, the callback
  169. duk_push_c_function(ctx, js_push_native_event_metadata, 1);
  170. // store the event type in the function object
  171. duk_push_string(ctx, eventType.CString());
  172. duk_put_prop_string(ctx, -2, "_eventType");
  173. // store to module object
  174. duk_put_prop_string(ctx, -2, eventName.CString());
  175. }
  176. void js_object_to_variantmap(duk_context* ctx, int objIdx, VariantMap &v)
  177. {
  178. v.Clear();
  179. duk_enum(ctx, objIdx, DUK_ENUM_OWN_PROPERTIES_ONLY);
  180. while (duk_next(ctx, -1 /*enum_index*/, 1 /*get_value*/)) {
  181. /* [ ... enum key ] */
  182. const char* key = duk_to_string(ctx, -2);
  183. if (duk_is_number(ctx, -1)) {
  184. v[key] = (float)duk_to_number(ctx, -1);
  185. }
  186. else if (duk_is_boolean(ctx, -1)) {
  187. v[key] = duk_to_boolean(ctx, -1) ? true : false;
  188. }
  189. else if (duk_is_string(ctx, -1)) {
  190. v[key] = duk_to_string(ctx, -1);
  191. }
  192. else if (duk_get_heapptr(ctx, -1)) {
  193. v[key] = js_to_class_instance<Object>(ctx, -1, 0);
  194. }
  195. duk_pop_2(ctx); /* pop_key & value*/
  196. }
  197. duk_pop(ctx); /* pop enum object */
  198. }
  199. duk_bool_t js_check_is_buffer_and_get_data(duk_context* ctx, duk_idx_t idx, void** data, duk_size_t* size)
  200. {
  201. void* temp;
  202. if (duk_is_buffer(ctx, idx))
  203. {
  204. temp = duk_get_buffer_data(ctx, idx, size);
  205. if (data)
  206. {
  207. *data = temp;
  208. }
  209. return true;
  210. }
  211. if (!(duk_is_object(ctx, idx) &&
  212. duk_has_prop_string(ctx, idx, "length") &&
  213. duk_has_prop_string(ctx, idx, "byteLength") &&
  214. duk_has_prop_string(ctx, idx, "byteOffset") &&
  215. duk_has_prop_string(ctx, idx, "BYTES_PER_ELEMENT")))
  216. {
  217. if (data)
  218. {
  219. *data = nullptr;
  220. }
  221. if (size)
  222. {
  223. *size = 0;
  224. }
  225. return false;
  226. }
  227. temp = duk_require_buffer_data(ctx, idx, size);
  228. if (data)
  229. {
  230. *data = temp;
  231. }
  232. return true;
  233. }
  234. void js_get_default_variant(VariantType variantType, Variant& value)
  235. {
  236. value = Variant::EMPTY;
  237. switch (variantType)
  238. {
  239. case VAR_NONE:
  240. break;
  241. case VAR_INT:
  242. value = 0;
  243. break;
  244. case VAR_BOOL:
  245. value = false;
  246. break;
  247. case VAR_FLOAT:
  248. value = 0.0f;
  249. break;
  250. case VAR_VECTOR2:
  251. value = Vector2::ZERO;
  252. break;
  253. case VAR_VECTOR3:
  254. value = Vector3::ZERO;
  255. break;
  256. case VAR_VECTOR4:
  257. value = Vector4::ZERO;
  258. break;
  259. case VAR_QUATERNION:
  260. value = Quaternion::IDENTITY;
  261. break;
  262. case VAR_COLOR:
  263. value = Color::WHITE;
  264. break;
  265. case VAR_STRING:
  266. value = "";
  267. break;
  268. case VAR_INTRECT:
  269. value = IntRect::ZERO;
  270. break;
  271. case VAR_INTVECTOR2:
  272. value = IntVector2::ZERO;
  273. break;
  274. case VAR_DOUBLE:
  275. // set float here too, so standard edits work
  276. value = 0.0f;
  277. break;
  278. case VAR_RESOURCEREF:
  279. value = ResourceRef();
  280. break;
  281. default:
  282. break;
  283. }
  284. }
  285. void js_to_variant(duk_context* ctx, int variantIdx, Variant &v, VariantType variantType)
  286. {
  287. v.Clear();
  288. // convert to abs index
  289. if (variantIdx < 0)
  290. variantIdx = duk_get_top(ctx) + variantIdx;
  291. if (duk_is_boolean(ctx, variantIdx))
  292. {
  293. v = duk_to_boolean(ctx, variantIdx) ? true : false;
  294. return;
  295. }
  296. if (duk_is_string(ctx, variantIdx))
  297. {
  298. v = duk_to_string(ctx, variantIdx);
  299. return;
  300. }
  301. if (duk_is_number(ctx, variantIdx))
  302. {
  303. v = (float)duk_to_number(ctx, variantIdx);
  304. return;
  305. }
  306. if (duk_is_pointer(ctx, variantIdx))
  307. {
  308. v = (RefCounted*)duk_get_pointer(ctx, variantIdx);
  309. return;
  310. }
  311. if (duk_is_array(ctx, variantIdx))
  312. {
  313. if (duk_get_length(ctx, variantIdx) == 2)
  314. {
  315. Vector2 v2;
  316. duk_get_prop_index(ctx, variantIdx, 0);
  317. v2.x_ = duk_to_number(ctx, -1);
  318. duk_get_prop_index(ctx, variantIdx, 1);
  319. v2.y_ = duk_to_number(ctx, -1);
  320. duk_pop_n(ctx, 2);
  321. v = v2;
  322. return;
  323. }
  324. else if (duk_get_length(ctx, variantIdx) == 3)
  325. {
  326. Vector3 v3;
  327. duk_get_prop_index(ctx, variantIdx, 0);
  328. v3.x_ = duk_to_number(ctx, -1);
  329. duk_get_prop_index(ctx, variantIdx, 1);
  330. v3.y_ = duk_to_number(ctx, -1);
  331. duk_get_prop_index(ctx, variantIdx, 2);
  332. v3.z_ = duk_to_number(ctx, -1);
  333. duk_pop_n(ctx, 3);
  334. v = v3;
  335. return;
  336. }
  337. else if (duk_get_length(ctx, variantIdx) == 4)
  338. {
  339. Vector4 v4;
  340. duk_get_prop_index(ctx, variantIdx, 0);
  341. v4.x_ = duk_to_number(ctx, -1);
  342. duk_get_prop_index(ctx, variantIdx, 1);
  343. v4.y_ = duk_to_number(ctx, -1);
  344. duk_get_prop_index(ctx, variantIdx, 2);
  345. v4.z_ = duk_to_number(ctx, -1);
  346. duk_get_prop_index(ctx, variantIdx, 3);
  347. v4.w_ = duk_to_number(ctx, -1);
  348. duk_pop_n(ctx, 4);
  349. v = v4;
  350. return;
  351. }
  352. return;
  353. }
  354. {
  355. void* bufferData;
  356. duk_size_t bufferSize;
  357. if (js_check_is_buffer_and_get_data(ctx, variantIdx, &bufferData, &bufferSize))
  358. {
  359. // copy the buffer into the variant
  360. v.SetBuffer(bufferData, (unsigned)bufferSize);
  361. return;
  362. }
  363. }
  364. // object check after array and buffer object check
  365. if (duk_is_object(ctx, variantIdx))
  366. {
  367. if (variantType == VAR_RESOURCEREFLIST)
  368. {
  369. ResourceRefList refList;
  370. duk_get_prop_string(ctx, variantIdx, "typeName");
  371. refList.type_ = duk_to_string(ctx, -1);
  372. duk_get_prop_string(ctx, variantIdx, "resources");
  373. int length = duk_get_length(ctx, -1);
  374. for (int i = 0; i < length; i++) {
  375. duk_get_prop_index(ctx, -1, i);
  376. Resource* resource = NULL;
  377. if (duk_is_object(ctx, -1))
  378. {
  379. resource = js_to_class_instance<Resource>(ctx, -1, 0);
  380. }
  381. if (resource) {
  382. refList.names_.Push(resource->GetName());
  383. }
  384. else
  385. refList.names_.Push(String::EMPTY);
  386. duk_pop(ctx);
  387. }
  388. duk_pop_n(ctx, 2);
  389. v = refList;
  390. }
  391. else
  392. {
  393. RefCounted* o = js_to_class_instance<RefCounted>(ctx, variantIdx, 0);
  394. if (o)
  395. v = o;
  396. }
  397. return;
  398. }
  399. }
  400. // variant map Proxy getter, so we can convert access to string based
  401. // member lookup, to string hash on the fly
  402. static int variantmap_property_get(duk_context* ctx)
  403. {
  404. // targ, key, recv
  405. if (duk_is_string(ctx, 1))
  406. {
  407. StringHash key = duk_to_string(ctx, 1);
  408. duk_get_prop_index(ctx, 0, (unsigned)key.Value());
  409. return 1;
  410. }
  411. duk_push_undefined(ctx);
  412. return 1;
  413. }
  414. // removes all keys from the variant map proxy target, REGARDLESS of key given for delete
  415. // see (lengthy) note in JSEventDispatcher::EndSendEvent
  416. static int variantmap_property_deleteproperty(duk_context* ctx)
  417. {
  418. // deleteProperty: function (targ, key)
  419. duk_enum(ctx, 0, DUK_ENUM_OWN_PROPERTIES_ONLY);
  420. while (duk_next(ctx, -1, 0)) {
  421. duk_del_prop(ctx, 0);
  422. }
  423. duk_push_boolean(ctx, 1);
  424. return 1;
  425. }
  426. void js_push_variantmap(duk_context* ctx, const VariantMap &vmap)
  427. {
  428. // setup proxy so we can map string
  429. duk_get_global_string(ctx, "Proxy");
  430. duk_push_object(ctx);
  431. VariantMap::ConstIterator itr = vmap.Begin();
  432. while (itr != vmap.End()) {
  433. js_push_variant(ctx, itr->second_);
  434. if (duk_is_undefined(ctx, -1)) {
  435. duk_pop(ctx);
  436. }
  437. else
  438. {
  439. duk_put_prop_index(ctx, -2, (unsigned)itr->first_.Value());
  440. }
  441. itr++;
  442. }
  443. // setup property handler
  444. duk_push_object(ctx);
  445. duk_push_c_function(ctx, variantmap_property_get, 3);
  446. duk_put_prop_string(ctx, -2, "get");
  447. duk_push_c_function(ctx, variantmap_property_deleteproperty, 2);
  448. duk_put_prop_string(ctx, -2, "deleteProperty");
  449. duk_new(ctx, 2);
  450. }
  451. void js_push_variant(duk_context *ctx, const Variant& v, int arrayIndex)
  452. {
  453. switch (v.GetType())
  454. {
  455. case VAR_NONE:
  456. duk_push_undefined(ctx);
  457. break;
  458. case VAR_VOIDPTR:
  459. duk_push_null(ctx);
  460. break;
  461. case VAR_PTR:
  462. {
  463. RefCounted* ref = v.GetPtr();
  464. // if we're null or don't have any refs, return null
  465. if (!ref || !ref->Refs())
  466. {
  467. duk_push_null(ctx);
  468. break;
  469. }
  470. // check that class is supported
  471. duk_push_heap_stash(ctx);
  472. duk_push_pointer(ctx, (void*)ref->GetClassID());
  473. duk_get_prop(ctx, -2);
  474. if (!duk_is_object(ctx, -1))
  475. {
  476. duk_pop_2(ctx);
  477. duk_push_undefined(ctx);
  478. }
  479. else
  480. {
  481. duk_pop_2(ctx);
  482. js_push_class_object_instance(ctx, ref);
  483. }
  484. } break;
  485. case VAR_RESOURCEREF:
  486. {
  487. const ResourceRef& resourceRef(v.GetResourceRef());
  488. ResourceCache* cache = JSVM::GetJSVM(ctx)->GetContext()->GetSubsystem<ResourceCache>();
  489. Resource* resource = cache->GetResource(resourceRef.type_, resourceRef.name_);
  490. js_push_class_object_instance(ctx, resource);
  491. } break;
  492. case VAR_RESOURCEREFLIST:
  493. {
  494. const ResourceRefList& resourceRefList(v.GetResourceRefList());
  495. const Context* context = JSVM::GetJSVM(ctx)->GetContext();
  496. duk_push_object(ctx);
  497. duk_push_string(ctx, context->GetTypeName(resourceRefList.type_).CString());
  498. duk_put_prop_string(ctx, -2, "typeName");
  499. duk_push_array(ctx);
  500. ResourceCache* cache = context->GetSubsystem<ResourceCache>();
  501. for (unsigned i = 0; i < resourceRefList.names_.Size(); i++) {
  502. Resource* resource = cache->GetResource(resourceRefList.type_, resourceRefList.names_[i]);
  503. js_push_class_object_instance(ctx, resource);
  504. duk_put_prop_index(ctx, -2, i);
  505. }
  506. duk_put_prop_string(ctx, -2, "resources");
  507. } break;
  508. case VAR_BOOL:
  509. duk_push_boolean(ctx, v.GetBool() ? 1 : 0);
  510. break;
  511. case VAR_INT:
  512. duk_push_number(ctx, v.GetInt());
  513. break;
  514. case VAR_FLOAT:
  515. duk_push_number(ctx, v.GetFloat());
  516. break;
  517. case VAR_DOUBLE:
  518. duk_push_number(ctx, v.GetFloat());
  519. break;
  520. case VAR_STRING:
  521. {
  522. const String& string(v.GetString());
  523. duk_push_lstring(ctx, string.CString(), string.Length());
  524. } break;
  525. case VAR_BUFFER:
  526. {
  527. const PODVector<unsigned char>& buffer(v.GetBuffer()); // The braces are to scope this reference.
  528. duk_push_fixed_buffer(ctx, buffer.Size());
  529. duk_push_buffer_object(ctx, -1, 0, buffer.Size(), DUK_BUFOBJ_UINT8ARRAY);
  530. duk_replace(ctx, -2);
  531. unsigned char* data = (unsigned char*)duk_require_buffer_data(ctx, -1, (duk_size_t*)nullptr);
  532. memcpy(data, buffer.Buffer(), buffer.Size());
  533. } break;
  534. case VAR_VECTOR2:
  535. {
  536. const Vector2& vector2(v.GetVector2());
  537. duk_push_array(ctx);
  538. duk_push_number(ctx, vector2.x_);
  539. duk_put_prop_index(ctx, -2, 0);
  540. duk_push_number(ctx, vector2.y_);
  541. duk_put_prop_index(ctx, -2, 1);
  542. } break;
  543. case VAR_INTVECTOR2:
  544. {
  545. const IntVector2& intVector2(v.GetIntVector2());
  546. duk_push_array(ctx);
  547. duk_push_number(ctx, intVector2.x_);
  548. duk_put_prop_index(ctx, -2, 0);
  549. duk_push_number(ctx, intVector2.y_);
  550. duk_put_prop_index(ctx, -2, 1);
  551. } break;
  552. case VAR_VECTOR3:
  553. {
  554. const Vector3& vector3(v.GetVector3());
  555. duk_push_array(ctx);
  556. duk_push_number(ctx, vector3.x_);
  557. duk_put_prop_index(ctx, -2, 0);
  558. duk_push_number(ctx, vector3.y_);
  559. duk_put_prop_index(ctx, -2, 1);
  560. duk_push_number(ctx, vector3.z_);
  561. duk_put_prop_index(ctx, -2, 2);
  562. } break;
  563. case VAR_QUATERNION:
  564. {
  565. const Vector3& vector3(v.GetQuaternion().EulerAngles());
  566. duk_push_array(ctx);
  567. duk_push_number(ctx, vector3.x_);
  568. duk_put_prop_index(ctx, -2, 0);
  569. duk_push_number(ctx, vector3.y_);
  570. duk_put_prop_index(ctx, -2, 1);
  571. duk_push_number(ctx, vector3.z_);
  572. duk_put_prop_index(ctx, -2, 2);
  573. } break;
  574. case VAR_COLOR:
  575. {
  576. const Color& color(v.GetColor());
  577. duk_push_array(ctx);
  578. duk_push_number(ctx, color.r_);
  579. duk_put_prop_index(ctx, -2, 0);
  580. duk_push_number(ctx, color.g_);
  581. duk_put_prop_index(ctx, -2, 1);
  582. duk_push_number(ctx, color.b_);
  583. duk_put_prop_index(ctx, -2, 2);
  584. duk_push_number(ctx, color.a_);
  585. duk_put_prop_index(ctx, -2, 3);
  586. } break;
  587. case VAR_VECTOR4:
  588. {
  589. const Vector4& vector4(v.GetVector4());
  590. duk_push_array(ctx);
  591. duk_push_number(ctx, vector4.x_);
  592. duk_put_prop_index(ctx, -2, 0);
  593. duk_push_number(ctx, vector4.y_);
  594. duk_put_prop_index(ctx, -2, 1);
  595. duk_push_number(ctx, vector4.z_);
  596. duk_put_prop_index(ctx, -2, 2);
  597. duk_push_number(ctx, vector4.w_);
  598. duk_put_prop_index(ctx, -2, 3);
  599. } break;
  600. case VAR_VARIANTVECTOR:
  601. {
  602. const VariantVector& vector(v.GetVariantVector());
  603. // if we don't specify an array index, wrap and push the vector (EXPENSIVE!)
  604. if (arrayIndex == -1)
  605. {
  606. SharedPtr<ScriptVector> scriptVector(new ScriptVector());
  607. scriptVector->AdaptFromVector(vector);
  608. js_push_class_object_instance(ctx, scriptVector);
  609. }
  610. else
  611. {
  612. if (arrayIndex < 0 || arrayIndex >= vector.Size())
  613. {
  614. duk_push_undefined(ctx);
  615. }
  616. else
  617. {
  618. // recursively push the variant
  619. js_push_variant(ctx, vector[arrayIndex]);
  620. }
  621. }
  622. } break;
  623. default:
  624. duk_push_undefined(ctx);
  625. break;
  626. }
  627. }
  628. }