JSAtomic.cpp 11 KB

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