JSAPI.cpp 18 KB

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