JSCore.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 ( top == 2 ) // General notification: subscribeToEvent("ScreenMode", function() {});
  34. {
  35. if (duk_is_string(ctx, 0) && duk_is_function(ctx, 1))
  36. {
  37. eventType = duk_to_string(ctx, 0);
  38. } else if(duk_is_number(ctx, 0) && duk_is_function(ctx, 1)) {
  39. eventType = StringHash(duk_to_number(ctx, 0));
  40. }
  41. }
  42. else if (top == 3) // Listen to specific object sender subscribeToEvent(graphics, "ScreenMode", function() {});
  43. {
  44. if (duk_is_object(ctx, 0) && duk_is_string(ctx, 1) && duk_is_function(ctx, 2))
  45. {
  46. sender = js_to_class_instance<Object>(ctx, 0, 0);
  47. eventType = duk_to_string(ctx, 1);
  48. } else if (duk_is_object(ctx, 0) && duk_is_number(ctx, 1) && duk_is_function(ctx, 2)) {
  49. sender = js_to_class_instance<Object>(ctx, 0, 0);
  50. eventType = StringHash(duk_to_number(ctx, 1));
  51. }
  52. }
  53. if ( eventType == StringHash::ZERO)
  54. {
  55. duk_push_string(ctx, "Object.subscribeToEvent() - Bad Arguments");
  56. duk_throw(ctx);
  57. }
  58. duk_push_this(ctx);
  59. // event receiver
  60. Object* object = js_to_class_instance<Object>(ctx, -1, 0);
  61. duk_get_prop_string(ctx, -1, "__eventHelper");
  62. // need to setup the handler
  63. if (duk_is_null_or_undefined(ctx, -1))
  64. {
  65. duk_pop(ctx); // pop null or undefined
  66. // construct a new event helper
  67. JSEventHelper* eventHelper = new JSEventHelper(object->GetContext(), object);
  68. eventHelper->SetInstantiationType(INSTANTIATION_JAVASCRIPT);
  69. js_push_class_object_instance(ctx, eventHelper);
  70. duk_push_object(ctx);
  71. duk_put_prop_string(ctx, -2, "__eventHelperFunctions");
  72. // dup so when we set the helper is left on stack
  73. duk_dup_top(ctx);
  74. duk_put_prop_string(ctx, -3, "__eventHelper");
  75. }
  76. JSEventHelper* helper = js_to_class_instance<JSEventHelper>(ctx, -1, 0);
  77. duk_get_prop_string(ctx, -1, "__eventHelperFunctions");
  78. assert(duk_is_object(ctx, -1));
  79. assert(duk_is_function(ctx, sender ? 2 : 1));
  80. duk_dup(ctx, sender ? 2 : 1);
  81. duk_put_prop_string(ctx, -2, eventType.ToString().CString());
  82. duk_pop(ctx);
  83. if (sender)
  84. helper->AddEventHandler(sender, eventType);
  85. else
  86. helper->AddEventHandler(eventType);
  87. return 0;
  88. }
  89. static int Object_SendEvent(duk_context* ctx)
  90. {
  91. int top = duk_get_top(ctx);
  92. duk_push_this(ctx);
  93. // event sender
  94. Object* sender = js_to_class_instance<Object>(ctx, -1, 0);
  95. if (top == 1)
  96. {
  97. sender->SendEvent(duk_to_string(ctx, 0));
  98. }
  99. else if (top == 2)
  100. {
  101. if (duk_is_object(ctx, 1)) {
  102. VariantMap sendEventVMap;
  103. js_object_to_variantmap(ctx, 1, sendEventVMap);
  104. sender->SendEvent(duk_to_string(ctx, 0), sendEventVMap);
  105. }
  106. }
  107. return 0;
  108. }
  109. static int Atomic_GetArguments(duk_context* ctx)
  110. {
  111. duk_push_array(ctx);
  112. for (unsigned i = 0; i < GetArguments().Size(); i++)
  113. {
  114. duk_push_string(ctx, GetArguments()[i].CString());
  115. duk_put_prop_index(ctx, -2, i);
  116. }
  117. return 1;
  118. }
  119. void jsapi_init_core(JSVM* vm)
  120. {
  121. duk_context* ctx = vm->GetJSContext();
  122. duk_get_global_string(ctx, "Atomic");
  123. duk_push_c_function(ctx, Atomic_GetArguments, 0);
  124. duk_put_prop_string(ctx, -2, "getArguments");
  125. duk_pop(ctx); // pop Atomic object
  126. js_class_get_prototype(ctx, "Atomic", "AObject");
  127. duk_push_c_function(ctx, Object_SubscribeToEvent, DUK_VARARGS);
  128. duk_put_prop_string(ctx, -2, "subscribeToEvent");
  129. duk_push_c_function(ctx, Object_SendEvent, DUK_VARARGS);
  130. duk_put_prop_string(ctx, -2, "sendEvent");
  131. duk_pop(ctx); // pop AObject prototype
  132. }
  133. }