JSCore.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/ProcessUtils.h>
  23. #include "JSCore.h"
  24. #include "JSEventHelper.h"
  25. #include "JSVM.h"
  26. namespace Atomic
  27. {
  28. static int Object_SubscribeToEvent(duk_context* ctx)
  29. {
  30. int top = duk_get_top(ctx);
  31. Object* sender = NULL;
  32. StringHash eventType = StringHash::ZERO;
  33. // if we have a single argument, it is event meta data
  34. if (top == 1)
  35. {
  36. if (duk_is_object(ctx, 0))
  37. {
  38. duk_get_prop_string(ctx, 0, "_callback");
  39. duk_get_prop_string(ctx, 0, "_eventType");
  40. if (duk_is_string(ctx, -1) && duk_is_function(ctx, -2))
  41. {
  42. duk_replace(ctx, 0);
  43. top = duk_get_top(ctx);
  44. }
  45. }
  46. if (top != 2)
  47. {
  48. duk_push_string(ctx, "Object.subscribeToEvent() - Bad meta data");
  49. duk_throw(ctx);
  50. }
  51. }
  52. // unwrap event data
  53. if ( top == 2 && duk_is_object(ctx, 0) && duk_is_object(ctx, 1))
  54. {
  55. duk_get_prop_string(ctx, 1, "_callback");
  56. duk_get_prop_string(ctx, 1, "_eventType");
  57. if (duk_is_string(ctx, -1) && duk_is_function(ctx, -2))
  58. {
  59. duk_replace(ctx, 1);
  60. top = duk_get_top(ctx);
  61. }
  62. if (top != 3)
  63. {
  64. duk_push_string(ctx, "Object.subscribeToEvent() - Bad sender meta data");
  65. duk_throw(ctx);
  66. }
  67. }
  68. if ( top == 2 ) // General notification: subscribeToEvent("ScreenMode", function() {});
  69. {
  70. if (duk_is_string(ctx, 0) && duk_is_function(ctx, 1))
  71. {
  72. eventType = duk_to_string(ctx, 0);
  73. } else if(duk_is_number(ctx, 0) && duk_is_function(ctx, 1)) {
  74. eventType = StringHash(duk_to_number(ctx, 0));
  75. }
  76. }
  77. else if (top == 3) // Listen to specific object sender subscribeToEvent(graphics, "ScreenMode", function() {});
  78. {
  79. if (duk_is_object(ctx, 0) && duk_is_string(ctx, 1) && duk_is_function(ctx, 2))
  80. {
  81. sender = js_to_class_instance<Object>(ctx, 0, 0);
  82. eventType = duk_to_string(ctx, 1);
  83. }
  84. else if (duk_is_object(ctx, 0) && duk_is_number(ctx, 1) && duk_is_function(ctx, 2)) {
  85. sender = js_to_class_instance<Object>(ctx, 0, 0);
  86. eventType = StringHash(duk_to_number(ctx, 1));
  87. }
  88. }
  89. if ( eventType == StringHash::ZERO)
  90. {
  91. duk_push_string(ctx, "Object.subscribeToEvent() - Bad Arguments");
  92. duk_throw(ctx);
  93. }
  94. duk_push_this(ctx);
  95. // event receiver
  96. Object* object = js_to_class_instance<Object>(ctx, -1, 0);
  97. duk_get_prop_string(ctx, -1, "__eventHelper");
  98. // need to setup the handler
  99. if (duk_is_null_or_undefined(ctx, -1))
  100. {
  101. duk_pop(ctx); // pop null or undefined
  102. // construct a new event helper
  103. JSEventHelper* eventHelper = new JSEventHelper(object->GetContext(), object);
  104. eventHelper->SetInstantiationType(INSTANTIATION_JAVASCRIPT);
  105. js_push_class_object_instance(ctx, eventHelper);
  106. duk_push_object(ctx);
  107. duk_put_prop_string(ctx, -2, "__eventHelperFunctions");
  108. // dup so when we set the helper is left on stack
  109. duk_dup_top(ctx);
  110. duk_put_prop_string(ctx, -3, "__eventHelper");
  111. }
  112. JSEventHelper* helper = js_to_class_instance<JSEventHelper>(ctx, -1, 0);
  113. duk_get_prop_string(ctx, -1, "__eventHelperFunctions");
  114. assert(duk_is_object(ctx, -1));
  115. assert(duk_is_function(ctx, sender ? 2 : 1));
  116. duk_dup(ctx, sender ? 2 : 1);
  117. duk_put_prop_string(ctx, -2, eventType.ToString().CString());
  118. duk_pop(ctx);
  119. if (sender)
  120. helper->AddEventHandler(sender, eventType);
  121. else
  122. helper->AddEventHandler(eventType);
  123. return 0;
  124. }
  125. static int Object_SendEvent(duk_context* ctx)
  126. {
  127. int top = duk_get_top(ctx);
  128. duk_push_this(ctx);
  129. // event sender
  130. Object* sender = js_to_class_instance<Object>(ctx, -1, 0);
  131. if (top == 1)
  132. {
  133. if (duk_is_string(ctx, 0))
  134. {
  135. sender->SendEvent(duk_to_string(ctx, 0));
  136. } else if (duk_is_object(ctx, 0)) {
  137. duk_get_prop_string(ctx, 0, "_callbackData");
  138. duk_get_prop_string(ctx, 0, "_eventType");
  139. if (!duk_is_string(ctx, -1))
  140. {
  141. duk_push_string(ctx, "Object.sendEvent() - Bad callback meta data");
  142. duk_throw(ctx);
  143. }
  144. if (duk_is_object(ctx, -2))
  145. {
  146. VariantMap sendEventVMap;
  147. js_object_to_variantmap(ctx, -2, sendEventVMap);
  148. sender->SendEvent(duk_to_string(ctx, -1), sendEventVMap);
  149. } else {
  150. sender->SendEvent(duk_to_string(ctx, -1));
  151. }
  152. } else {
  153. duk_push_string(ctx, "Object.sendEvent() - Bad callback meta data");
  154. duk_throw(ctx);
  155. }
  156. }
  157. else if (top == 2)
  158. {
  159. if (duk_is_object(ctx, 1)) {
  160. VariantMap sendEventVMap;
  161. js_object_to_variantmap(ctx, 1, sendEventVMap);
  162. sender->SendEvent(duk_to_string(ctx, 0), sendEventVMap);
  163. }
  164. }
  165. return 0;
  166. }
  167. static int Atomic_GetArguments(duk_context* ctx)
  168. {
  169. duk_push_array(ctx);
  170. for (unsigned i = 0; i < GetArguments().Size(); i++)
  171. {
  172. duk_push_string(ctx, GetArguments()[i].CString());
  173. duk_put_prop_index(ctx, -2, i);
  174. }
  175. return 1;
  176. }
  177. void jsapi_init_core(JSVM* vm)
  178. {
  179. duk_context* ctx = vm->GetJSContext();
  180. duk_get_global_string(ctx, "Atomic");
  181. duk_push_c_function(ctx, Atomic_GetArguments, 0);
  182. duk_put_prop_string(ctx, -2, "getArguments");
  183. duk_pop(ctx); // pop Atomic object
  184. js_class_get_prototype(ctx, "Atomic", "AObject");
  185. duk_push_c_function(ctx, Object_SubscribeToEvent, DUK_VARARGS);
  186. duk_put_prop_string(ctx, -2, "subscribeToEvent");
  187. duk_push_c_function(ctx, Object_SendEvent, DUK_VARARGS);
  188. duk_put_prop_string(ctx, -2, "sendEvent");
  189. duk_pop(ctx); // pop AObject prototype
  190. }
  191. }