JSAPI.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. static int js_push_native_event_callbackdata(duk_context* ctx) {
  167. duk_push_current_function(ctx);
  168. duk_push_object(ctx);
  169. duk_get_prop_string(ctx, -2, "_eventType");
  170. duk_put_prop_string(ctx, -2, "_eventType");
  171. duk_dup(ctx, 0);
  172. duk_put_prop_string(ctx, -2, "_callbackData");
  173. return 1;
  174. }
  175. void js_define_native_event(duk_context* ctx, const String& eventType, const String &eventName)
  176. {
  177. // Set up the pieces for subscribe to eventj
  178. // push c function which takes 1 argument, the callback
  179. duk_push_c_function(ctx, js_push_native_event_metadata, 1);
  180. // store the event type in the function object
  181. // function {event}( eventName, callback ) : { _eventType , _callback }
  182. duk_push_string(ctx, eventType.CString());
  183. duk_put_prop_string(ctx, -2, "_eventType");
  184. // store to module object
  185. duk_put_prop_string(ctx, -2, eventName.CString());
  186. // Set up the pieces for sendEvent
  187. // push c function which takes 1 argument, the callback
  188. duk_push_c_function(ctx, js_push_native_event_callbackdata, 1);
  189. // store the event type in the function object
  190. // function {event}Data( eventName, callbackData ) : { _eventType , _callbackData }
  191. duk_push_string(ctx, eventType.CString());
  192. duk_put_prop_string(ctx, -2, "_eventType");
  193. // store to module object
  194. duk_put_prop_string(ctx, -2, ToString("%sData", eventName.CString()).CString());
  195. }
  196. void js_object_to_variantmap(duk_context* ctx, int objIdx, VariantMap &v)
  197. {
  198. v.Clear();
  199. duk_enum(ctx, objIdx, DUK_ENUM_OWN_PROPERTIES_ONLY);
  200. while (duk_next(ctx, -1 /*enum_index*/, 1 /*get_value*/)) {
  201. /* [ ... enum key ] */
  202. const char* key = duk_to_string(ctx, -2);
  203. if (duk_is_number(ctx, -1)) {
  204. v[key] = (float)duk_to_number(ctx, -1);
  205. }
  206. else if (duk_is_boolean(ctx, -1)) {
  207. v[key] = duk_to_boolean(ctx, -1) ? true : false;
  208. }
  209. else if (duk_is_string(ctx, -1)) {
  210. v[key] = duk_to_string(ctx, -1);
  211. }
  212. else if (duk_get_heapptr(ctx, -1)) {
  213. v[key] = js_to_class_instance<Object>(ctx, -1, 0);
  214. }
  215. duk_pop_2(ctx); /* pop_key & value*/
  216. }
  217. duk_pop(ctx); /* pop enum object */
  218. }
  219. duk_bool_t js_check_is_buffer_and_get_data(duk_context* ctx, duk_idx_t idx, void** data, duk_size_t* size)
  220. {
  221. void* temp;
  222. if (duk_is_buffer(ctx, idx))
  223. {
  224. temp = duk_get_buffer_data(ctx, idx, size);
  225. if (data)
  226. {
  227. *data = temp;
  228. }
  229. return true;
  230. }
  231. if (!(duk_is_object(ctx, idx) &&
  232. duk_has_prop_string(ctx, idx, "length") &&
  233. duk_has_prop_string(ctx, idx, "byteLength") &&
  234. duk_has_prop_string(ctx, idx, "byteOffset") &&
  235. duk_has_prop_string(ctx, idx, "BYTES_PER_ELEMENT")))
  236. {
  237. if (data)
  238. {
  239. *data = nullptr;
  240. }
  241. if (size)
  242. {
  243. *size = 0;
  244. }
  245. return false;
  246. }
  247. temp = duk_require_buffer_data(ctx, idx, size);
  248. if (data)
  249. {
  250. *data = temp;
  251. }
  252. return true;
  253. }
  254. void js_get_default_variant(VariantType variantType, Variant& value)
  255. {
  256. value = Variant::EMPTY;
  257. switch (variantType)
  258. {
  259. case VAR_NONE:
  260. break;
  261. case VAR_INT:
  262. value = 0;
  263. break;
  264. case VAR_BOOL:
  265. value = false;
  266. break;
  267. case VAR_FLOAT:
  268. value = 0.0f;
  269. break;
  270. case VAR_VECTOR2:
  271. value = Vector2::ZERO;
  272. break;
  273. case VAR_VECTOR3:
  274. value = Vector3::ZERO;
  275. break;
  276. case VAR_VECTOR4:
  277. value = Vector4::ZERO;
  278. break;
  279. case VAR_QUATERNION:
  280. value = Quaternion::IDENTITY;
  281. break;
  282. case VAR_COLOR:
  283. value = Color::WHITE;
  284. break;
  285. case VAR_STRING:
  286. value = "";
  287. break;
  288. case VAR_INTRECT:
  289. value = IntRect::ZERO;
  290. break;
  291. case VAR_INTVECTOR2:
  292. value = IntVector2::ZERO;
  293. break;
  294. case VAR_DOUBLE:
  295. // set float here too, so standard edits work
  296. value = 0.0f;
  297. break;
  298. case VAR_RESOURCEREF:
  299. value = ResourceRef();
  300. break;
  301. default:
  302. break;
  303. }
  304. }
  305. void js_to_variant(duk_context* ctx, int variantIdx, Variant &v, VariantType variantType)
  306. {
  307. v.Clear();
  308. // convert to abs index
  309. if (variantIdx < 0)
  310. variantIdx = duk_get_top(ctx) + variantIdx;
  311. if (duk_is_boolean(ctx, variantIdx))
  312. {
  313. v = duk_to_boolean(ctx, variantIdx) ? true : false;
  314. return;
  315. }
  316. if (duk_is_string(ctx, variantIdx))
  317. {
  318. v = duk_to_string(ctx, variantIdx);
  319. return;
  320. }
  321. if (duk_is_number(ctx, variantIdx))
  322. {
  323. v = (float)duk_to_number(ctx, variantIdx);
  324. return;
  325. }
  326. if (duk_is_pointer(ctx, variantIdx))
  327. {
  328. v = (RefCounted*)duk_get_pointer(ctx, variantIdx);
  329. return;
  330. }
  331. if (duk_is_array(ctx, variantIdx))
  332. {
  333. if (duk_get_length(ctx, variantIdx) == 2)
  334. {
  335. Vector2 v2;
  336. duk_get_prop_index(ctx, variantIdx, 0);
  337. v2.x_ = duk_to_number(ctx, -1);
  338. duk_get_prop_index(ctx, variantIdx, 1);
  339. v2.y_ = duk_to_number(ctx, -1);
  340. duk_pop_n(ctx, 2);
  341. v = v2;
  342. return;
  343. }
  344. else if (duk_get_length(ctx, variantIdx) == 3)
  345. {
  346. Vector3 v3;
  347. duk_get_prop_index(ctx, variantIdx, 0);
  348. v3.x_ = duk_to_number(ctx, -1);
  349. duk_get_prop_index(ctx, variantIdx, 1);
  350. v3.y_ = duk_to_number(ctx, -1);
  351. duk_get_prop_index(ctx, variantIdx, 2);
  352. v3.z_ = duk_to_number(ctx, -1);
  353. duk_pop_n(ctx, 3);
  354. v = v3;
  355. return;
  356. }
  357. else if (duk_get_length(ctx, variantIdx) == 4)
  358. {
  359. Vector4 v4;
  360. duk_get_prop_index(ctx, variantIdx, 0);
  361. v4.x_ = duk_to_number(ctx, -1);
  362. duk_get_prop_index(ctx, variantIdx, 1);
  363. v4.y_ = duk_to_number(ctx, -1);
  364. duk_get_prop_index(ctx, variantIdx, 2);
  365. v4.z_ = duk_to_number(ctx, -1);
  366. duk_get_prop_index(ctx, variantIdx, 3);
  367. v4.w_ = duk_to_number(ctx, -1);
  368. duk_pop_n(ctx, 4);
  369. v = v4;
  370. return;
  371. }
  372. return;
  373. }
  374. {
  375. void* bufferData;
  376. duk_size_t bufferSize;
  377. if (js_check_is_buffer_and_get_data(ctx, variantIdx, &bufferData, &bufferSize))
  378. {
  379. // copy the buffer into the variant
  380. v.SetBuffer(bufferData, (unsigned)bufferSize);
  381. return;
  382. }
  383. }
  384. // object check after array and buffer object check
  385. if (duk_is_object(ctx, variantIdx))
  386. {
  387. if (variantType == VAR_RESOURCEREFLIST)
  388. {
  389. ResourceRefList refList;
  390. duk_get_prop_string(ctx, variantIdx, "typeName");
  391. refList.type_ = duk_to_string(ctx, -1);
  392. duk_get_prop_string(ctx, variantIdx, "resources");
  393. int length = duk_get_length(ctx, -1);
  394. for (int i = 0; i < length; i++) {
  395. duk_get_prop_index(ctx, -1, i);
  396. Resource* resource = NULL;
  397. if (duk_is_object(ctx, -1))
  398. {
  399. resource = js_to_class_instance<Resource>(ctx, -1, 0);
  400. }
  401. if (resource) {
  402. refList.names_.Push(resource->GetName());
  403. }
  404. else
  405. refList.names_.Push(String::EMPTY);
  406. duk_pop(ctx);
  407. }
  408. duk_pop_n(ctx, 2);
  409. v = refList;
  410. }
  411. else
  412. {
  413. RefCounted* o = js_to_class_instance<RefCounted>(ctx, variantIdx, 0);
  414. if (o)
  415. v = o;
  416. }
  417. return;
  418. }
  419. }
  420. // variant map Proxy getter, so we can convert access to string based
  421. // member lookup, to string hash on the fly
  422. static int variantmap_property_get(duk_context* ctx)
  423. {
  424. // targ, key, recv
  425. if (duk_is_string(ctx, 1))
  426. {
  427. StringHash key = duk_to_string(ctx, 1);
  428. duk_get_prop_index(ctx, 0, (unsigned)key.Value());
  429. return 1;
  430. }
  431. duk_push_undefined(ctx);
  432. return 1;
  433. }
  434. // removes all keys from the variant map proxy target, REGARDLESS of key given for delete
  435. // see (lengthy) note in JSEventDispatcher::EndSendEvent
  436. static int variantmap_property_deleteproperty(duk_context* ctx)
  437. {
  438. // deleteProperty: function (targ, key)
  439. duk_enum(ctx, 0, DUK_ENUM_OWN_PROPERTIES_ONLY);
  440. while (duk_next(ctx, -1, 0)) {
  441. duk_del_prop(ctx, 0);
  442. }
  443. duk_push_boolean(ctx, 1);
  444. return 1;
  445. }
  446. void js_push_variantmap(duk_context* ctx, const VariantMap &vmap)
  447. {
  448. // setup proxy so we can map string
  449. duk_get_global_string(ctx, "Proxy");
  450. duk_push_object(ctx);
  451. VariantMap::ConstIterator itr = vmap.Begin();
  452. while (itr != vmap.End()) {
  453. js_push_variant(ctx, itr->second_);
  454. if (duk_is_undefined(ctx, -1)) {
  455. duk_pop(ctx);
  456. }
  457. else
  458. {
  459. duk_put_prop_index(ctx, -2, (unsigned)itr->first_.Value());
  460. }
  461. itr++;
  462. }
  463. // setup property handler
  464. duk_push_object(ctx);
  465. duk_push_c_function(ctx, variantmap_property_get, 3);
  466. duk_put_prop_string(ctx, -2, "get");
  467. duk_push_c_function(ctx, variantmap_property_deleteproperty, 2);
  468. duk_put_prop_string(ctx, -2, "deleteProperty");
  469. duk_new(ctx, 2);
  470. }
  471. void js_push_variant(duk_context *ctx, const Variant& v, int arrayIndex)
  472. {
  473. switch (v.GetType())
  474. {
  475. case VAR_NONE:
  476. duk_push_undefined(ctx);
  477. break;
  478. case VAR_VOIDPTR:
  479. duk_push_null(ctx);
  480. break;
  481. case VAR_PTR:
  482. {
  483. RefCounted* ref = v.GetPtr();
  484. // if we're null or don't have any refs, return null
  485. if (!ref || !ref->Refs())
  486. {
  487. duk_push_null(ctx);
  488. break;
  489. }
  490. // check that class is supported
  491. duk_push_heap_stash(ctx);
  492. duk_push_pointer(ctx, (void*)ref->GetClassID());
  493. duk_get_prop(ctx, -2);
  494. if (!duk_is_object(ctx, -1))
  495. {
  496. duk_pop_2(ctx);
  497. duk_push_undefined(ctx);
  498. }
  499. else
  500. {
  501. duk_pop_2(ctx);
  502. js_push_class_object_instance(ctx, ref);
  503. }
  504. } break;
  505. case VAR_RESOURCEREF:
  506. {
  507. const ResourceRef& resourceRef(v.GetResourceRef());
  508. ResourceCache* cache = JSVM::GetJSVM(ctx)->GetContext()->GetSubsystem<ResourceCache>();
  509. Resource* resource = cache->GetResource(resourceRef.type_, resourceRef.name_);
  510. js_push_class_object_instance(ctx, resource);
  511. } break;
  512. case VAR_RESOURCEREFLIST:
  513. {
  514. const ResourceRefList& resourceRefList(v.GetResourceRefList());
  515. const Context* context = JSVM::GetJSVM(ctx)->GetContext();
  516. duk_push_object(ctx);
  517. duk_push_string(ctx, context->GetTypeName(resourceRefList.type_).CString());
  518. duk_put_prop_string(ctx, -2, "typeName");
  519. duk_push_array(ctx);
  520. ResourceCache* cache = context->GetSubsystem<ResourceCache>();
  521. for (unsigned i = 0; i < resourceRefList.names_.Size(); i++) {
  522. Resource* resource = cache->GetResource(resourceRefList.type_, resourceRefList.names_[i]);
  523. js_push_class_object_instance(ctx, resource);
  524. duk_put_prop_index(ctx, -2, i);
  525. }
  526. duk_put_prop_string(ctx, -2, "resources");
  527. } break;
  528. case VAR_BOOL:
  529. duk_push_boolean(ctx, v.GetBool() ? 1 : 0);
  530. break;
  531. case VAR_INT:
  532. duk_push_number(ctx, v.GetInt());
  533. break;
  534. case VAR_FLOAT:
  535. duk_push_number(ctx, v.GetFloat());
  536. break;
  537. case VAR_DOUBLE:
  538. duk_push_number(ctx, v.GetFloat());
  539. break;
  540. case VAR_STRING:
  541. {
  542. const String& string(v.GetString());
  543. duk_push_lstring(ctx, string.CString(), string.Length());
  544. } break;
  545. case VAR_BUFFER:
  546. {
  547. const PODVector<unsigned char>& buffer(v.GetBuffer()); // The braces are to scope this reference.
  548. duk_push_fixed_buffer(ctx, buffer.Size());
  549. duk_push_buffer_object(ctx, -1, 0, buffer.Size(), DUK_BUFOBJ_UINT8ARRAY);
  550. duk_replace(ctx, -2);
  551. unsigned char* data = (unsigned char*)duk_require_buffer_data(ctx, -1, (duk_size_t*)nullptr);
  552. memcpy(data, buffer.Buffer(), buffer.Size());
  553. } break;
  554. case VAR_VECTOR2:
  555. {
  556. const Vector2& vector2(v.GetVector2());
  557. duk_push_array(ctx);
  558. duk_push_number(ctx, vector2.x_);
  559. duk_put_prop_index(ctx, -2, 0);
  560. duk_push_number(ctx, vector2.y_);
  561. duk_put_prop_index(ctx, -2, 1);
  562. } break;
  563. case VAR_INTVECTOR2:
  564. {
  565. const IntVector2& intVector2(v.GetIntVector2());
  566. duk_push_array(ctx);
  567. duk_push_number(ctx, intVector2.x_);
  568. duk_put_prop_index(ctx, -2, 0);
  569. duk_push_number(ctx, intVector2.y_);
  570. duk_put_prop_index(ctx, -2, 1);
  571. } break;
  572. case VAR_VECTOR3:
  573. {
  574. const Vector3& vector3(v.GetVector3());
  575. duk_push_array(ctx);
  576. duk_push_number(ctx, vector3.x_);
  577. duk_put_prop_index(ctx, -2, 0);
  578. duk_push_number(ctx, vector3.y_);
  579. duk_put_prop_index(ctx, -2, 1);
  580. duk_push_number(ctx, vector3.z_);
  581. duk_put_prop_index(ctx, -2, 2);
  582. } break;
  583. case VAR_QUATERNION:
  584. {
  585. const Vector3& vector3(v.GetQuaternion().EulerAngles());
  586. duk_push_array(ctx);
  587. duk_push_number(ctx, vector3.x_);
  588. duk_put_prop_index(ctx, -2, 0);
  589. duk_push_number(ctx, vector3.y_);
  590. duk_put_prop_index(ctx, -2, 1);
  591. duk_push_number(ctx, vector3.z_);
  592. duk_put_prop_index(ctx, -2, 2);
  593. } break;
  594. case VAR_COLOR:
  595. {
  596. const Color& color(v.GetColor());
  597. duk_push_array(ctx);
  598. duk_push_number(ctx, color.r_);
  599. duk_put_prop_index(ctx, -2, 0);
  600. duk_push_number(ctx, color.g_);
  601. duk_put_prop_index(ctx, -2, 1);
  602. duk_push_number(ctx, color.b_);
  603. duk_put_prop_index(ctx, -2, 2);
  604. duk_push_number(ctx, color.a_);
  605. duk_put_prop_index(ctx, -2, 3);
  606. } break;
  607. case VAR_VECTOR4:
  608. {
  609. const Vector4& vector4(v.GetVector4());
  610. duk_push_array(ctx);
  611. duk_push_number(ctx, vector4.x_);
  612. duk_put_prop_index(ctx, -2, 0);
  613. duk_push_number(ctx, vector4.y_);
  614. duk_put_prop_index(ctx, -2, 1);
  615. duk_push_number(ctx, vector4.z_);
  616. duk_put_prop_index(ctx, -2, 2);
  617. duk_push_number(ctx, vector4.w_);
  618. duk_put_prop_index(ctx, -2, 3);
  619. } break;
  620. case VAR_VARIANTVECTOR:
  621. {
  622. const VariantVector& vector(v.GetVariantVector());
  623. // if we don't specify an array index, wrap and push the vector (EXPENSIVE!)
  624. if (arrayIndex == -1)
  625. {
  626. SharedPtr<ScriptVector> scriptVector(new ScriptVector());
  627. scriptVector->AdaptFromVector(vector);
  628. js_push_class_object_instance(ctx, scriptVector);
  629. }
  630. else
  631. {
  632. if (arrayIndex < 0 || arrayIndex >= vector.Size())
  633. {
  634. duk_push_undefined(ctx);
  635. }
  636. else
  637. {
  638. // recursively push the variant
  639. js_push_variant(ctx, vector[arrayIndex]);
  640. }
  641. }
  642. } break;
  643. default:
  644. duk_push_undefined(ctx);
  645. break;
  646. }
  647. }
  648. }