JSAtomic.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include <Atomic/Core/ProcessUtils.h>
  5. #include <Atomic/IO/FileSystem.h>
  6. #include <Atomic/IO/Log.h>
  7. #include <Atomic/Resource/ResourceCache.h>
  8. #include <Atomic/Input/Input.h>
  9. #include <Atomic/Graphics/Renderer.h>
  10. #include <Atomic/Graphics/Graphics.h>
  11. #include <Atomic/Engine/Engine.h>
  12. #ifdef ATOMIC_NETWORK
  13. #include <Atomic/Network/Network.h>
  14. #endif
  15. #include "JSEvents.h"
  16. #include "JSVM.h"
  17. #include "JSComponent.h"
  18. #include "JSCore.h"
  19. #include "JSFileSystem.h"
  20. #include "JSGraphics.h"
  21. #include "JSIO.h"
  22. #include "JSUIAPI.h"
  23. #include "JSScene.h"
  24. #ifdef ATOMIC_NETWORK
  25. #include "JSNetwork.h"
  26. #endif
  27. #include "JSAtomicGame.h"
  28. #include "JSAtomic.h"
  29. #include <Atomic/Scene/Scene.h>
  30. #include <Atomic/Environment/ProcSky.h>
  31. namespace Atomic
  32. {
  33. extern void jsb_package_atomic_init(JSVM* vm);
  34. static int js_module_read_file(duk_context* ctx)
  35. {
  36. JSVM* vm = JSVM::GetJSVM(ctx);
  37. ResourceCache* cache = vm->GetContext()->GetSubsystem<ResourceCache>();
  38. String path = duk_to_string(ctx, 0);
  39. SharedPtr<File> file = cache->GetFile(path);
  40. if (!file->IsOpen())
  41. {
  42. duk_push_string(ctx, "Unable to open module file");
  43. duk_throw(ctx);
  44. return 0;
  45. }
  46. unsigned size = file->GetSize();
  47. SharedArrayPtr<char> data;
  48. data = new char[size + 1];
  49. data[size] = '\0';
  50. file->Read(data, size);
  51. duk_push_string(ctx, data);
  52. return 1;
  53. }
  54. static int js_print(duk_context* ctx)
  55. {
  56. duk_concat(ctx, duk_get_top(ctx));
  57. VariantMap eventData;
  58. using namespace JSPrint;
  59. eventData[P_TEXT] = duk_to_string(ctx, -1);
  60. JSVM* vm = JSVM::GetJSVM(ctx);
  61. vm->SendEvent(E_JSPRINT, eventData);
  62. LOGINFOF("%s", duk_to_string(ctx, -1));
  63. return 0;
  64. }
  65. static int js_openConsoleWindow(duk_context* ctx)
  66. {
  67. #ifdef _WIN32
  68. OpenConsoleWindow();
  69. #endif
  70. return 0;
  71. }
  72. static int js_assert(duk_context* ctx)
  73. {
  74. if (!duk_to_boolean(ctx, 0))
  75. {
  76. assert(0);
  77. }
  78. return 0;
  79. }
  80. static int js_atomic_GetVM(duk_context* ctx)
  81. {
  82. JSVM* vm = JSVM::GetJSVM(ctx);
  83. js_push_class_object_instance(ctx, vm);
  84. return 1;
  85. }
  86. static int js_atomic_GetEngine(duk_context* ctx)
  87. {
  88. JSVM* vm = JSVM::GetJSVM(ctx);
  89. js_push_class_object_instance(ctx, vm->GetSubsystem<Engine>());
  90. return 1;
  91. }
  92. static int js_atomic_GetResourceCache(duk_context* ctx)
  93. {
  94. JSVM* vm = JSVM::GetJSVM(ctx);
  95. js_push_class_object_instance(ctx, vm->GetSubsystem<ResourceCache>());
  96. return 1;
  97. }
  98. static int js_atomic_GetRenderer(duk_context* ctx)
  99. {
  100. JSVM* vm = JSVM::GetJSVM(ctx);
  101. js_push_class_object_instance(ctx, vm->GetSubsystem<Renderer>());
  102. return 1;
  103. }
  104. static int js_atomic_GetGraphics(duk_context* ctx)
  105. {
  106. JSVM* vm = JSVM::GetJSVM(ctx);
  107. js_push_class_object_instance(ctx, vm->GetSubsystem<Graphics>());
  108. return 1;
  109. }
  110. static int js_atomic_GetInput(duk_context* ctx)
  111. {
  112. JSVM* vm = JSVM::GetJSVM(ctx);
  113. js_push_class_object_instance(ctx, vm->GetSubsystem<Input>());
  114. return 1;
  115. }
  116. static int js_atomic_GetFileSystem(duk_context* ctx)
  117. {
  118. JSVM* vm = JSVM::GetJSVM(ctx);
  119. js_push_class_object_instance(ctx, vm->GetSubsystem<FileSystem>());
  120. return 1;
  121. }
  122. #ifdef ATOMIC_NETWORK
  123. static int js_atomic_GetNetwork(duk_context* ctx)
  124. {
  125. JSVM* vm = JSVM::GetJSVM(ctx);
  126. js_push_class_object_instance(ctx, vm->GetSubsystem<Network>());
  127. return 1;
  128. }
  129. #endif
  130. static int js_atomic_script(duk_context* ctx)
  131. {
  132. JSVM* vm = JSVM::GetJSVM(ctx);
  133. if (duk_is_string(ctx, 0))
  134. {
  135. if ( vm->ExecuteScript(duk_to_string(ctx, 0)))
  136. duk_push_boolean(ctx, 1);
  137. else
  138. duk_push_boolean(ctx, 0);
  139. }
  140. else
  141. duk_push_boolean(ctx, 0);
  142. return 1;
  143. }
  144. static void js_atomic_destroy_node(Node* node, duk_context* ctx, bool root = false)
  145. {
  146. if (root)
  147. {
  148. PODVector<Node*> children;
  149. node->GetChildren(children, true);
  150. for (unsigned i = 0; i < children.Size(); i++)
  151. {
  152. if (children.At(i)->JSGetHeapPtr())
  153. js_atomic_destroy_node(children.At(i), ctx);
  154. }
  155. }
  156. const Vector<SharedPtr<Component> >& components = node->GetComponents();
  157. for (unsigned i = 0; i < components.Size(); i++)
  158. {
  159. Component* component = components[i];
  160. if (component->GetType() == JSComponent::GetTypeStatic())
  161. {
  162. // FIX COMPONENTS
  163. //JSComponent* jscomponent = (JSComponent*) component;
  164. //jscomponent->SetDestroyed();
  165. }
  166. component->UnsubscribeFromAllEvents();
  167. }
  168. node->RemoveAllComponents();
  169. node->UnsubscribeFromAllEvents();
  170. if (node->GetParent())
  171. {
  172. assert(node->Refs() >= 2);
  173. node->Remove();
  174. }
  175. int top = duk_get_top(ctx);
  176. duk_push_global_stash(ctx);
  177. duk_get_prop_index(ctx, -1, JS_GLOBALSTASH_INDEX_NODE_REGISTRY);
  178. duk_push_pointer(ctx, (void*) node);
  179. duk_del_prop(ctx, -2);
  180. duk_pop_2(ctx);
  181. assert(top = duk_get_top(ctx));
  182. }
  183. static void js_atomic_destroy_scene(Scene* scene, duk_context* ctx)
  184. {
  185. js_atomic_destroy_node(scene, ctx, true);
  186. }
  187. static int js_atomic_destroy(duk_context* ctx)
  188. {
  189. if (!duk_is_object(ctx, 0))
  190. return 0;
  191. Object* obj = js_to_class_instance<Object>(ctx, 0, 0);
  192. if (!obj)
  193. return 0;
  194. if (obj->GetType() == Node::GetTypeStatic())
  195. {
  196. Node* node = (Node*) obj;
  197. js_atomic_destroy_node(node, ctx, true);
  198. return 0;
  199. }
  200. if (obj->GetType() == Scene::GetTypeStatic())
  201. {
  202. Scene* scene = (Scene*) obj;
  203. js_atomic_destroy_scene(scene, ctx);
  204. return 0;
  205. }
  206. else if (obj->GetType() == JSComponent::GetTypeStatic())
  207. {
  208. // FIXME: want to be able to destroy a single component
  209. assert(0);
  210. JSComponent* component = (JSComponent*) obj;
  211. component->UnsubscribeFromAllEvents();
  212. component->Remove();
  213. return 0;
  214. }
  215. return 0;
  216. }
  217. void jsapi_init_atomic(JSVM* vm)
  218. {
  219. // core modules
  220. jsb_package_atomic_init(vm);
  221. // extensions
  222. jsapi_init_core(vm);
  223. jsapi_init_filesystem(vm);
  224. jsapi_init_io(vm);
  225. #ifdef ATOMIC_NETWORK
  226. jsapi_init_network(vm);
  227. #endif
  228. jsapi_init_graphics(vm);
  229. jsapi_init_ui(vm);
  230. jsapi_init_scene(vm);
  231. jsapi_init_atomicgame(vm);
  232. duk_context* ctx = vm->GetJSContext();
  233. // globals
  234. duk_push_global_object(ctx);
  235. duk_push_c_function(ctx, js_print, DUK_VARARGS);
  236. duk_put_prop_string(ctx, -2, "print");
  237. duk_push_c_function(ctx, js_assert, 1);
  238. duk_put_prop_string(ctx, -2, "assert");
  239. duk_push_c_function(ctx, js_module_read_file, 1);
  240. duk_put_prop_string(ctx, -2, "js_module_read_file");
  241. duk_pop(ctx);
  242. // Atomic
  243. duk_get_global_string(ctx, "Atomic");
  244. String platform = GetPlatform();
  245. if (platform == "Mac OS X")
  246. platform = "MacOSX";
  247. duk_push_string(ctx, platform.CString());
  248. duk_put_prop_string(ctx, -2, "platform");
  249. // Node registry
  250. duk_push_global_stash(ctx);
  251. duk_push_object(ctx);
  252. duk_put_prop_index(ctx, -2, JS_GLOBALSTASH_INDEX_NODE_REGISTRY);
  253. duk_pop(ctx);
  254. duk_push_c_function(ctx, js_openConsoleWindow, 0);
  255. duk_put_prop_string(ctx, -2, "openConsoleWindow");
  256. duk_push_c_function(ctx, js_atomic_GetVM, 0);
  257. duk_put_prop_string(ctx, -2, "getVM");
  258. duk_push_c_function(ctx, js_atomic_GetEngine, 0);
  259. duk_put_prop_string(ctx, -2, "getEngine");
  260. duk_push_c_function(ctx, js_atomic_GetGraphics, 0);
  261. duk_put_prop_string(ctx, -2, "getGraphics");
  262. duk_push_c_function(ctx, js_atomic_GetRenderer, 0);
  263. duk_put_prop_string(ctx, -2, "getRenderer");
  264. duk_push_c_function(ctx, js_atomic_GetResourceCache, 0);
  265. duk_put_prop_string(ctx, -2, "getResourceCache");
  266. duk_push_c_function(ctx, js_atomic_GetInput, 0);
  267. duk_put_prop_string(ctx, -2, "getInput");
  268. duk_push_c_function(ctx, js_atomic_GetFileSystem, 0);
  269. duk_put_prop_string(ctx, -2, "getFileSystem");
  270. #ifdef ATOMIC_NETWORK
  271. duk_push_c_function(ctx, js_atomic_GetNetwork, 0);
  272. duk_put_prop_string(ctx, -2, "getNetwork");
  273. #endif
  274. duk_push_c_function(ctx, js_atomic_script, 1);
  275. duk_put_prop_string(ctx, -2, "script");
  276. duk_push_c_function(ctx, js_atomic_destroy, 1);
  277. duk_put_prop_string(ctx, -2, "destroy");
  278. duk_pop(ctx);
  279. }
  280. }