JSAtomic.cpp 12 KB

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