JSCore.cpp 4.7 KB

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