JSCore.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 <Atomic/Core/Profiler.h>
  24. #include "JSCore.h"
  25. #include "JSEventHelper.h"
  26. #include "JSVM.h"
  27. namespace Atomic
  28. {
  29. static int Object_SubscribeToEvent(duk_context* ctx)
  30. {
  31. int top = duk_get_top(ctx);
  32. Object* sender = NULL;
  33. StringHash eventType = StringHash::ZERO;
  34. // if we have a single argument, it is event meta data
  35. if (top == 1)
  36. {
  37. if (duk_is_object(ctx, 0))
  38. {
  39. duk_get_prop_string(ctx, 0, "_callback");
  40. duk_get_prop_string(ctx, 0, "_eventType");
  41. if (duk_is_string(ctx, -1) && duk_is_function(ctx, -2))
  42. {
  43. duk_replace(ctx, 0);
  44. top = duk_get_top(ctx);
  45. }
  46. }
  47. if (top != 2)
  48. {
  49. duk_push_string(ctx, "Object.subscribeToEvent() - Bad meta data");
  50. duk_throw(ctx);
  51. }
  52. }
  53. // unwrap event data
  54. if ( top == 2 && duk_is_object(ctx, 0) && duk_is_object(ctx, 1))
  55. {
  56. duk_get_prop_string(ctx, 1, "_callback");
  57. duk_get_prop_string(ctx, 1, "_eventType");
  58. if (duk_is_string(ctx, -1) && duk_is_function(ctx, -2))
  59. {
  60. duk_replace(ctx, 1);
  61. top = duk_get_top(ctx);
  62. }
  63. if (top != 3)
  64. {
  65. duk_push_string(ctx, "Object.subscribeToEvent() - Bad sender meta data");
  66. duk_throw(ctx);
  67. }
  68. }
  69. if ( top == 2 ) // General notification: subscribeToEvent("ScreenMode", function() {});
  70. {
  71. if (duk_is_string(ctx, 0) && duk_is_function(ctx, 1))
  72. {
  73. eventType = duk_to_string(ctx, 0);
  74. } else if(duk_is_number(ctx, 0) && duk_is_function(ctx, 1)) {
  75. eventType = StringHash(duk_to_number(ctx, 0));
  76. }
  77. }
  78. else if (top == 3) // Listen to specific object sender subscribeToEvent(graphics, "ScreenMode", function() {});
  79. {
  80. if (duk_is_object(ctx, 0) && duk_is_string(ctx, 1) && duk_is_function(ctx, 2))
  81. {
  82. sender = js_to_class_instance<Object>(ctx, 0, 0);
  83. eventType = duk_to_string(ctx, 1);
  84. }
  85. else if (duk_is_object(ctx, 0) && duk_is_number(ctx, 1) && duk_is_function(ctx, 2)) {
  86. sender = js_to_class_instance<Object>(ctx, 0, 0);
  87. eventType = StringHash(duk_to_number(ctx, 1));
  88. }
  89. }
  90. if ( eventType == StringHash::ZERO)
  91. {
  92. duk_push_string(ctx, "Object.subscribeToEvent() - Bad Arguments");
  93. duk_throw(ctx);
  94. }
  95. duk_push_this(ctx);
  96. // event receiver
  97. Object* object = js_to_class_instance<Object>(ctx, -1, 0);
  98. duk_get_prop_string(ctx, -1, "__eventHelper");
  99. // need to setup the handler
  100. if (duk_is_null_or_undefined(ctx, -1))
  101. {
  102. duk_pop(ctx); // pop null or undefined
  103. // construct a new event helper
  104. JSEventHelper* eventHelper = new JSEventHelper(object->GetContext(), object);
  105. eventHelper->SetInstantiationType(INSTANTIATION_JAVASCRIPT);
  106. js_push_class_object_instance(ctx, eventHelper);
  107. duk_push_object(ctx);
  108. duk_put_prop_string(ctx, -2, "__eventHelperFunctions");
  109. // dup so when we set the helper is left on stack
  110. duk_dup_top(ctx);
  111. duk_put_prop_string(ctx, -3, "__eventHelper");
  112. }
  113. JSEventHelper* helper = js_to_class_instance<JSEventHelper>(ctx, -1, 0);
  114. duk_get_prop_string(ctx, -1, "__eventHelperFunctions");
  115. assert(duk_is_object(ctx, -1));
  116. assert(duk_is_function(ctx, sender ? 2 : 1));
  117. duk_dup(ctx, sender ? 2 : 1);
  118. duk_put_prop_string(ctx, -2, eventType.ToString().CString());
  119. duk_pop(ctx);
  120. if (sender)
  121. helper->AddEventHandler(sender, eventType);
  122. else
  123. helper->AddEventHandler(eventType);
  124. return 0;
  125. }
  126. static int Object_SendEvent(duk_context* ctx)
  127. {
  128. int top = duk_get_top(ctx);
  129. duk_push_this(ctx);
  130. // event sender
  131. Object* sender = js_to_class_instance<Object>(ctx, -1, 0);
  132. if (top == 1)
  133. {
  134. if (duk_is_string(ctx, 0))
  135. {
  136. sender->SendEvent(duk_to_string(ctx, 0));
  137. } else if (duk_is_object(ctx, 0)) {
  138. duk_get_prop_string(ctx, 0, "_callbackData");
  139. duk_get_prop_string(ctx, 0, "_eventType");
  140. if (!duk_is_string(ctx, -1))
  141. {
  142. duk_push_string(ctx, "Object.sendEvent() - Bad callback meta data");
  143. duk_throw(ctx);
  144. }
  145. if (duk_is_object(ctx, -2))
  146. {
  147. VariantMap sendEventVMap;
  148. js_object_to_variantmap(ctx, -2, sendEventVMap);
  149. sender->SendEvent(duk_to_string(ctx, -1), sendEventVMap);
  150. } else {
  151. sender->SendEvent(duk_to_string(ctx, -1));
  152. }
  153. } else {
  154. duk_push_string(ctx, "Object.sendEvent() - Bad callback meta data");
  155. duk_throw(ctx);
  156. }
  157. }
  158. else if (top == 2)
  159. {
  160. if (duk_is_object(ctx, 1)) {
  161. VariantMap sendEventVMap;
  162. js_object_to_variantmap(ctx, 1, sendEventVMap);
  163. sender->SendEvent(duk_to_string(ctx, 0), sendEventVMap);
  164. }
  165. }
  166. return 0;
  167. }
  168. static int Atomic_GetArguments(duk_context* ctx)
  169. {
  170. duk_push_array(ctx);
  171. for (unsigned i = 0; i < GetArguments().Size(); i++)
  172. {
  173. duk_push_string(ctx, GetArguments()[i].CString());
  174. duk_put_prop_index(ctx, -2, i);
  175. }
  176. return 1;
  177. }
  178. static int Profiler_BeginBlock(duk_context* ctx)
  179. {
  180. #if ATOMIC_PROFILING
  181. // Get the Profiler instance
  182. duk_push_this(ctx);
  183. Profiler* profiler = js_to_class_instance<Profiler>(ctx, -1, 0);
  184. duk_pop(ctx);
  185. // early out, this should never happen
  186. if (!profiler)
  187. {
  188. return 0;
  189. }
  190. // get the number of args
  191. duk_idx_t top = duk_get_top(ctx);
  192. // we need at least 3
  193. if ( top < 3)
  194. {
  195. return 0;
  196. }
  197. // parse args
  198. const char* name = duk_require_string(ctx, 0);
  199. const char* filename = duk_require_string(ctx, 1);
  200. int line = duk_require_number(ctx, 2);
  201. unsigned argb = (top > 3) ? (unsigned) duk_require_number(ctx, 3) : PROFILER_COLOR_DEFAULT;
  202. unsigned char status = (top > 4) ? (unsigned) duk_require_number(ctx, 4) : ProfilerBlockStatus::ON;
  203. profiler->BeginBlock(name, filename, line, argb, status);
  204. #else
  205. static bool warned = false;
  206. if (!warned)
  207. {
  208. warned = true;
  209. ATOMIC_LOGWARNING("Engine is built without profiler support.");
  210. }
  211. #endif
  212. return 0;
  213. }
  214. void jsapi_init_core(JSVM* vm)
  215. {
  216. duk_context* ctx = vm->GetJSContext();
  217. duk_get_global_string(ctx, "Atomic");
  218. duk_push_c_function(ctx, Atomic_GetArguments, 0);
  219. duk_put_prop_string(ctx, -2, "getArguments");
  220. duk_pop(ctx); // pop Atomic object
  221. js_class_get_prototype(ctx, "Atomic", "AObject");
  222. duk_push_c_function(ctx, Object_SubscribeToEvent, DUK_VARARGS);
  223. duk_put_prop_string(ctx, -2, "subscribeToEvent");
  224. duk_push_c_function(ctx, Object_SendEvent, DUK_VARARGS);
  225. duk_put_prop_string(ctx, -2, "sendEvent");
  226. duk_pop(ctx); // pop AObject prototype
  227. js_class_get_prototype(ctx, "Atomic", "Profiler");
  228. duk_push_c_function(ctx, Profiler_BeginBlock, DUK_VARARGS);
  229. duk_put_prop_string(ctx, -2, "beginBlock");
  230. duk_pop(ctx); // pop Profiler prototype
  231. }
  232. }