JSAPI.cpp 19 KB

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