JSCore.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. js_push_class_object_instance(ctx, new JSEventHelper(object->GetContext(), object));
  63. duk_push_object(ctx);
  64. duk_put_prop_string(ctx, -2, "__eventHelperFunctions");
  65. // dup so when we set the helper is left on stack
  66. duk_dup_top(ctx);
  67. duk_put_prop_string(ctx, -3, "__eventHelper");
  68. }
  69. JSEventHelper* helper = js_to_class_instance<JSEventHelper>(ctx, -1, 0);
  70. duk_get_prop_string(ctx, -1, "__eventHelperFunctions");
  71. assert(duk_is_object(ctx, -1));
  72. assert(duk_is_function(ctx, sender ? 2 : 1));
  73. duk_dup(ctx, sender ? 2 : 1);
  74. duk_put_prop_string(ctx, -2, eventType.ToString().CString());
  75. duk_pop(ctx);
  76. if (sender)
  77. helper->AddEventHandler(sender, eventType);
  78. else
  79. helper->AddEventHandler(eventType);
  80. return 0;
  81. }
  82. static int Object_SendEvent(duk_context* ctx)
  83. {
  84. int top = duk_get_top(ctx);
  85. duk_push_this(ctx);
  86. // event sender
  87. Object* sender = js_to_class_instance<Object>(ctx, -1, 0);
  88. if (top == 1)
  89. {
  90. sender->SendEvent(duk_to_string(ctx, 0));
  91. }
  92. else if (top == 2)
  93. {
  94. if (duk_is_object(ctx, 1)) {
  95. VariantMap sendEventVMap;
  96. js_object_to_variantmap(ctx, 1, sendEventVMap);
  97. sender->SendEvent(duk_to_string(ctx, 0), sendEventVMap);
  98. }
  99. }
  100. return 0;
  101. }
  102. static int Atomic_GetArguments(duk_context* ctx)
  103. {
  104. duk_push_array(ctx);
  105. for (unsigned i = 0; i < GetArguments().Size(); i++)
  106. {
  107. duk_push_string(ctx, GetArguments()[i].CString());
  108. duk_put_prop_index(ctx, -2, i);
  109. }
  110. return 1;
  111. }
  112. void jsapi_init_core(JSVM* vm)
  113. {
  114. duk_context* ctx = vm->GetJSContext();
  115. duk_get_global_string(ctx, "Atomic");
  116. duk_push_c_function(ctx, Atomic_GetArguments, 0);
  117. duk_put_prop_string(ctx, -2, "getArguments");
  118. duk_pop(ctx); // pop Atomic object
  119. js_class_get_prototype(ctx, "Atomic", "AObject");
  120. duk_push_c_function(ctx, Object_SubscribeToEvent, DUK_VARARGS);
  121. duk_put_prop_string(ctx, -2, "subscribeToEvent");
  122. duk_push_c_function(ctx, Object_SendEvent, DUK_VARARGS);
  123. duk_put_prop_string(ctx, -2, "sendEvent");
  124. duk_pop(ctx); // pop AObject prototype
  125. }
  126. }