JSAtomic.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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/IO/FileSystem.h>
  24. #include <Atomic/IO/Log.h>
  25. #include <Atomic/Resource/ResourceCache.h>
  26. #include <Atomic/Input/Input.h>
  27. #include <Atomic/Graphics/Renderer.h>
  28. #include <Atomic/Graphics/Graphics.h>
  29. #include <Atomic/Engine/Engine.h>
  30. #include <Atomic/Audio/Audio.h>
  31. #include <Atomic/UI/UI.h>
  32. #ifdef ATOMIC_NETWORK
  33. #include <Atomic/Network/Network.h>
  34. #endif
  35. #include "JSEvents.h"
  36. #include "JSVM.h"
  37. #include "JSComponent.h"
  38. #include "JSCore.h"
  39. #include "JSFileSystem.h"
  40. #include "JSGraphics.h"
  41. #ifdef ATOMIC_3D
  42. #include "JSAtomic3D.h"
  43. #endif
  44. #include "JSIO.h"
  45. #include "JSInput.h"
  46. #include "JSUIAPI.h"
  47. #include "JSScene.h"
  48. #ifdef ATOMIC_NETWORK
  49. #include "JSNetwork.h"
  50. #endif
  51. #include "JSAtomicPlayer.h"
  52. #include "JSAtomic.h"
  53. #include <Atomic/Scene/Scene.h>
  54. #include <Atomic/Environment/ProcSky.h>
  55. namespace Atomic
  56. {
  57. extern void jsb_package_atomic_init(JSVM* vm);
  58. static int js_module_read_file(duk_context* ctx)
  59. {
  60. JSVM* vm = JSVM::GetJSVM(ctx);
  61. ResourceCache* cache = vm->GetContext()->GetSubsystem<ResourceCache>();
  62. String path = duk_to_string(ctx, 0);
  63. SharedPtr<File> file = cache->GetFile(path);
  64. if (!file->IsOpen())
  65. {
  66. duk_push_string(ctx, "Unable to open module file");
  67. duk_throw(ctx);
  68. return 0;
  69. }
  70. unsigned size = file->GetSize();
  71. SharedArrayPtr<char> data;
  72. data = new char[size + 1];
  73. data[size] = '\0';
  74. file->Read(data, size);
  75. duk_push_string(ctx, data);
  76. return 1;
  77. }
  78. static int js_print(duk_context* ctx)
  79. {
  80. duk_concat(ctx, duk_get_top(ctx));
  81. VariantMap eventData;
  82. using namespace JSPrint;
  83. eventData[P_TEXT] = duk_to_string(ctx, -1);
  84. JSVM* vm = JSVM::GetJSVM(ctx);
  85. vm->SendEvent(E_JSPRINT, eventData);
  86. LOGINFOF("%s", duk_to_string(ctx, -1));
  87. return 0;
  88. }
  89. static int js_openConsoleWindow(duk_context* ctx)
  90. {
  91. #ifdef _WIN32
  92. OpenConsoleWindow();
  93. #endif
  94. return 0;
  95. }
  96. static int js_assert(duk_context* ctx)
  97. {
  98. if (!duk_to_boolean(ctx, 0))
  99. {
  100. assert(0);
  101. }
  102. return 0;
  103. }
  104. static int js_atomic_GetEngine(duk_context* ctx)
  105. {
  106. JSVM* vm = JSVM::GetJSVM(ctx);
  107. js_push_class_object_instance(ctx, vm->GetSubsystem<Engine>());
  108. return 1;
  109. }
  110. static int js_atomic_GetResourceCache(duk_context* ctx)
  111. {
  112. JSVM* vm = JSVM::GetJSVM(ctx);
  113. js_push_class_object_instance(ctx, vm->GetSubsystem<ResourceCache>());
  114. return 1;
  115. }
  116. static int js_atomic_GetRenderer(duk_context* ctx)
  117. {
  118. JSVM* vm = JSVM::GetJSVM(ctx);
  119. js_push_class_object_instance(ctx, vm->GetSubsystem<Renderer>());
  120. return 1;
  121. }
  122. static int js_atomic_GetGraphics(duk_context* ctx)
  123. {
  124. JSVM* vm = JSVM::GetJSVM(ctx);
  125. js_push_class_object_instance(ctx, vm->GetSubsystem<Graphics>());
  126. return 1;
  127. }
  128. static int js_atomic_GetInput(duk_context* ctx)
  129. {
  130. JSVM* vm = JSVM::GetJSVM(ctx);
  131. js_push_class_object_instance(ctx, vm->GetSubsystem<Input>());
  132. return 1;
  133. }
  134. static int js_atomic_GetFileSystem(duk_context* ctx)
  135. {
  136. JSVM* vm = JSVM::GetJSVM(ctx);
  137. js_push_class_object_instance(ctx, vm->GetSubsystem<FileSystem>());
  138. return 1;
  139. }
  140. #ifdef ATOMIC_NETWORK
  141. static int js_atomic_GetNetwork(duk_context* ctx)
  142. {
  143. JSVM* vm = JSVM::GetJSVM(ctx);
  144. js_push_class_object_instance(ctx, vm->GetSubsystem<Network>());
  145. return 1;
  146. }
  147. #endif
  148. static int js_atomic_GetUI(duk_context* ctx)
  149. {
  150. JSVM* vm = JSVM::GetJSVM(ctx);
  151. js_push_class_object_instance(ctx, vm->GetSubsystem<UI>());
  152. return 1;
  153. }
  154. static int js_atomic_script(duk_context* ctx)
  155. {
  156. JSVM* vm = JSVM::GetJSVM(ctx);
  157. if (duk_is_string(ctx, 0))
  158. {
  159. if ( vm->ExecuteScript(duk_to_string(ctx, 0)))
  160. duk_push_boolean(ctx, 1);
  161. else
  162. duk_push_boolean(ctx, 0);
  163. }
  164. else
  165. duk_push_boolean(ctx, 0);
  166. return 1;
  167. }
  168. static void js_atomic_destroy_node(Node* node, duk_context* ctx, bool root = false)
  169. {
  170. if (root)
  171. {
  172. PODVector<Node*> children;
  173. node->GetChildren(children, true);
  174. for (unsigned i = 0; i < children.Size(); i++)
  175. {
  176. if (children.At(i)->JSGetHeapPtr())
  177. js_atomic_destroy_node(children.At(i), ctx);
  178. }
  179. }
  180. const Vector<SharedPtr<Component> >& components = node->GetComponents();
  181. for (unsigned i = 0; i < components.Size(); i++)
  182. {
  183. Component* component = components[i];
  184. if (component->GetType() == JSComponent::GetTypeStatic())
  185. {
  186. JSComponent* jscomponent = (JSComponent*) component;
  187. jscomponent->SetDestroyed();
  188. }
  189. component->UnsubscribeFromAllEvents();
  190. }
  191. node->RemoveAllComponents();
  192. node->UnsubscribeFromAllEvents();
  193. node->Remove();
  194. }
  195. static void js_atomic_destroy_scene(Scene* scene, duk_context* ctx)
  196. {
  197. js_atomic_destroy_node(scene, ctx, true);
  198. }
  199. static int js_atomic_destroy(duk_context* ctx)
  200. {
  201. if (!duk_is_object(ctx, 0))
  202. return 0;
  203. Object* obj = js_to_class_instance<Object>(ctx, 0, 0);
  204. if (!obj)
  205. return 0;
  206. if (obj->GetType() == Node::GetTypeStatic())
  207. {
  208. Node* node = (Node*) obj;
  209. js_atomic_destroy_node(node, ctx, true);
  210. return 0;
  211. }
  212. if (obj->GetType() == Scene::GetTypeStatic())
  213. {
  214. Scene* scene = (Scene*) obj;
  215. js_atomic_destroy_scene(scene, ctx);
  216. return 0;
  217. }
  218. else if (obj->GetType() == JSComponent::GetTypeStatic())
  219. {
  220. // FIXME: want to be able to destroy a single component
  221. assert(0);
  222. JSComponent* component = (JSComponent*) obj;
  223. component->UnsubscribeFromAllEvents();
  224. component->Remove();
  225. return 0;
  226. }
  227. return 0;
  228. }
  229. void jsapi_init_atomic(JSVM* vm)
  230. {
  231. // core modules
  232. jsb_package_atomic_init(vm);
  233. // extensions
  234. jsapi_init_core(vm);
  235. jsapi_init_filesystem(vm);
  236. jsapi_init_io(vm);
  237. #ifdef ATOMIC_NETWORK
  238. jsapi_init_network(vm);
  239. #endif
  240. jsapi_init_graphics(vm);
  241. #ifdef ATOMIC_3D
  242. jsapi_init_atomic3d(vm);
  243. #endif
  244. jsapi_init_input(vm);
  245. jsapi_init_ui(vm);
  246. jsapi_init_scene(vm);
  247. jsapi_init_atomicplayer(vm);
  248. duk_context* ctx = vm->GetJSContext();
  249. // globals
  250. duk_push_global_object(ctx);
  251. // console.log
  252. duk_push_object(ctx);
  253. duk_dup(ctx, -1);
  254. duk_put_prop_string(ctx, -3, "console");
  255. duk_push_c_function(ctx, js_print, DUK_VARARGS);
  256. duk_put_prop_string(ctx, -2, "log");
  257. duk_pop(ctx);
  258. duk_push_c_function(ctx, js_print, DUK_VARARGS);
  259. duk_put_prop_string(ctx, -2, "print");
  260. duk_push_c_function(ctx, js_assert, 1);
  261. duk_put_prop_string(ctx, -2, "assert");
  262. duk_push_c_function(ctx, js_module_read_file, 1);
  263. duk_put_prop_string(ctx, -2, "js_module_read_file");
  264. duk_pop(ctx);
  265. // Atomic
  266. duk_get_global_string(ctx, "Atomic");
  267. // Atomic.print
  268. duk_push_c_function(ctx, js_print, DUK_VARARGS);
  269. duk_put_prop_string(ctx, -2, "print");
  270. String platform = GetPlatform();
  271. if (platform == "Mac OS X")
  272. platform = "MacOSX";
  273. duk_push_string(ctx, platform.CString());
  274. duk_put_prop_string(ctx, -2, "platform");
  275. // Node registry
  276. duk_push_global_stash(ctx);
  277. duk_push_object(ctx);
  278. duk_put_prop_index(ctx, -2, JS_GLOBALSTASH_INDEX_NODE_REGISTRY);
  279. duk_push_object(ctx);
  280. duk_put_prop_index(ctx, -2, JS_GLOBALSTASH_VARIANTMAP_CACHE);
  281. duk_pop(ctx);
  282. duk_push_c_function(ctx, js_openConsoleWindow, 0);
  283. duk_put_prop_string(ctx, -2, "openConsoleWindow");
  284. // subsystems
  285. duk_push_c_function(ctx, js_atomic_GetEngine, 0);
  286. duk_put_prop_string(ctx, -2, "getEngine");
  287. js_push_class_object_instance(ctx, vm->GetSubsystem<Engine>(), "Engine");
  288. duk_put_prop_string(ctx, -2, "engine");
  289. duk_push_c_function(ctx, js_atomic_GetGraphics, 0);
  290. duk_put_prop_string(ctx, -2, "getGraphics");
  291. js_push_class_object_instance(ctx, vm->GetSubsystem<Graphics>(), "Graphics");
  292. duk_put_prop_string(ctx, -2, "graphics");
  293. duk_push_c_function(ctx, js_atomic_GetRenderer, 0);
  294. duk_put_prop_string(ctx, -2, "getRenderer");
  295. js_push_class_object_instance(ctx, vm->GetSubsystem<Renderer>(), "Renderer");
  296. duk_put_prop_string(ctx, -2, "renderer");
  297. duk_push_c_function(ctx, js_atomic_GetResourceCache, 0);
  298. duk_put_prop_string(ctx, -2, "getResourceCache");
  299. js_push_class_object_instance(ctx, vm->GetSubsystem<ResourceCache>(), "ResourceCache");
  300. duk_put_prop_string(ctx, -2, "cache");
  301. duk_push_c_function(ctx, js_atomic_GetInput, 0);
  302. duk_put_prop_string(ctx, -2, "getInput");
  303. js_push_class_object_instance(ctx, vm->GetSubsystem<Audio>(), "Audio");
  304. duk_put_prop_string(ctx, -2, "audio");
  305. js_push_class_object_instance(ctx, vm->GetSubsystem<Input>(), "Input");
  306. duk_put_prop_string(ctx, -2, "input");
  307. duk_push_c_function(ctx, js_atomic_GetFileSystem, 0);
  308. duk_put_prop_string(ctx, -2, "getFileSystem");
  309. js_push_class_object_instance(ctx, vm->GetSubsystem<FileSystem>(), "FileSystem");
  310. duk_put_prop_string(ctx, -2, "fileSystem");
  311. #ifdef ATOMIC_NETWORK
  312. duk_push_c_function(ctx, js_atomic_GetNetwork, 0);
  313. duk_put_prop_string(ctx, -2, "getNetwork");
  314. js_push_class_object_instance(ctx, vm->GetSubsystem<Network>(), "Network");
  315. duk_put_prop_string(ctx, -2, "network");
  316. #endif
  317. duk_push_c_function(ctx, js_atomic_GetUI, 0);
  318. duk_put_prop_string(ctx, -2, "getUI");
  319. js_push_class_object_instance(ctx, vm->GetSubsystem<UI>(), "UI");
  320. duk_put_prop_string(ctx, -2, "ui");
  321. // end subsystems
  322. duk_push_c_function(ctx, js_atomic_script, 1);
  323. duk_put_prop_string(ctx, -2, "script");
  324. duk_push_c_function(ctx, js_atomic_destroy, 1);
  325. duk_put_prop_string(ctx, -2, "destroy");
  326. duk_pop(ctx);
  327. }
  328. }