JSCore.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. sender->SendEvent(duk_to_string(ctx, 0));
  134. }
  135. else if (top == 2)
  136. {
  137. if (duk_is_object(ctx, 1)) {
  138. VariantMap sendEventVMap;
  139. js_object_to_variantmap(ctx, 1, sendEventVMap);
  140. sender->SendEvent(duk_to_string(ctx, 0), sendEventVMap);
  141. }
  142. }
  143. return 0;
  144. }
  145. static int Atomic_GetArguments(duk_context* ctx)
  146. {
  147. duk_push_array(ctx);
  148. for (unsigned i = 0; i < GetArguments().Size(); i++)
  149. {
  150. duk_push_string(ctx, GetArguments()[i].CString());
  151. duk_put_prop_index(ctx, -2, i);
  152. }
  153. return 1;
  154. }
  155. void jsapi_init_core(JSVM* vm)
  156. {
  157. duk_context* ctx = vm->GetJSContext();
  158. duk_get_global_string(ctx, "Atomic");
  159. duk_push_c_function(ctx, Atomic_GetArguments, 0);
  160. duk_put_prop_string(ctx, -2, "getArguments");
  161. duk_pop(ctx); // pop Atomic object
  162. js_class_get_prototype(ctx, "Atomic", "AObject");
  163. duk_push_c_function(ctx, Object_SubscribeToEvent, DUK_VARARGS);
  164. duk_put_prop_string(ctx, -2, "subscribeToEvent");
  165. duk_push_c_function(ctx, Object_SendEvent, DUK_VARARGS);
  166. duk_put_prop_string(ctx, -2, "sendEvent");
  167. duk_pop(ctx); // pop AObject prototype
  168. }
  169. }