JSPlugin.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #include <Atomic/IO/Log.h>
  2. #include "JSPlugin.h"
  3. #include "JSPluginExports.h"
  4. #include "JSVM.h"
  5. #include <ThirdParty/SDL/include/SDL.h>
  6. namespace Atomic
  7. {
  8. typedef bool(*atomic_plugin_validate_function)(int version, void *jsvmImports, size_t jsvmImportsSize);
  9. static void jsplugin_bind_jsvmexports();
  10. static JSVMImports gJSVMExports;
  11. static bool jsplugin_get_entry_points(const String& pluginLibrary, atomic_plugin_validate_function* fvalidate,
  12. duk_c_function* finit, String& errorMsg)
  13. {
  14. *fvalidate = NULL;
  15. *finit = NULL;
  16. // TODO: cache and use SDL_UnloadObject (when no longer needed)
  17. void* handle = SDL_LoadObject(pluginLibrary.CString());
  18. if (handle == NULL)
  19. {
  20. errorMsg = ToString("Native Plugin: Unable to load %s with error %s",
  21. pluginLibrary.CString(),
  22. SDL_GetError());
  23. return false;
  24. }
  25. *fvalidate = (atomic_plugin_validate_function) SDL_LoadFunction(handle, "atomic_plugin_validate");
  26. if (!*fvalidate)
  27. {
  28. errorMsg = ToString("Native Plugin: Unable to get atomic_plugin_validate entry point in %s", pluginLibrary.CString());
  29. return false;
  30. }
  31. *finit = (duk_c_function) SDL_LoadFunction(handle, "atomic_plugin_init");
  32. if (!*finit)
  33. {
  34. LOGERRORF("Native Plugin: Unable to get atomic_plugin_init entry point in %s", pluginLibrary.CString());
  35. return false;
  36. }
  37. return true;
  38. }
  39. bool jsplugin_load(JSVM* vm, const String& pluginLibrary)
  40. {
  41. String errorMsg;
  42. atomic_plugin_validate_function validatefunc;
  43. duk_c_function initfunc;
  44. duk_context* ctx = vm->GetJSContext();
  45. LOGINFOF("Loading Native Plugin: %s", pluginLibrary.CString());
  46. if (!jsplugin_get_entry_points(pluginLibrary, &validatefunc, &initfunc, errorMsg))
  47. {
  48. LOGERRORF("%s", errorMsg.CString());
  49. return false;
  50. }
  51. int version = ATOMIC_JSPLUGIN_VERSION;
  52. if (!validatefunc(version, &gJSVMExports, sizeof(JSVMImports)))
  53. {
  54. LOGERRORF("Native Plugin: atomic_plugin_validate failed: %s", pluginLibrary.CString());
  55. return false;
  56. }
  57. // just to verify that we're not doing anything funky with the stack
  58. int top = duk_get_top(ctx);
  59. // the import function is a standard duktape c function, neat
  60. duk_push_c_function(ctx, initfunc, 1);
  61. // requires exports to be at index 2
  62. duk_dup(ctx, 2);
  63. bool success = true;
  64. if (duk_pcall(ctx, 1) != DUK_EXEC_SUCCESS)
  65. {
  66. success = false;
  67. LOGERRORF("Native Plugin: error calling atomic_plugin_init %s with error %s",
  68. pluginLibrary.CString(),
  69. duk_safe_to_string(ctx, -1));
  70. }
  71. else
  72. {
  73. if (!duk_is_boolean(ctx, -1) || !duk_to_boolean(ctx, -1))
  74. {
  75. success = false;
  76. LOGERRORF("Native Plugin: error calling atomic_plugin_init, didn't return true %s", pluginLibrary.CString());
  77. }
  78. }
  79. duk_pop(ctx);
  80. assert(top == duk_get_top(ctx));
  81. return success;
  82. }
  83. void js_init_jsplugin(JSVM* vm)
  84. {
  85. // bind the public Duktape API for export to plugins
  86. jsplugin_bind_jsvmexports();
  87. }
  88. void jsplugin_bind_jsvmexports()
  89. {
  90. gJSVMExports.duk_create_heap = duk_create_heap;
  91. gJSVMExports.duk_destroy_heap = duk_destroy_heap;
  92. gJSVMExports.duk_alloc_raw = duk_alloc_raw;
  93. gJSVMExports.duk_free_raw = duk_free_raw;
  94. gJSVMExports.duk_realloc_raw = duk_realloc_raw;
  95. gJSVMExports.duk_alloc = duk_alloc;
  96. gJSVMExports.duk_free = duk_free;
  97. gJSVMExports.duk_realloc = duk_realloc;
  98. gJSVMExports.duk_get_memory_functions = duk_get_memory_functions;
  99. gJSVMExports.duk_gc = duk_gc;
  100. gJSVMExports.duk_throw = duk_throw;
  101. gJSVMExports.duk_fatal = duk_fatal;
  102. gJSVMExports.duk_error_raw = duk_error_raw;
  103. gJSVMExports.duk_error_va_raw = duk_error_va_raw;
  104. gJSVMExports.duk_is_strict_call = duk_is_strict_call;
  105. gJSVMExports.duk_is_constructor_call = duk_is_constructor_call;
  106. gJSVMExports.duk_normalize_index = duk_normalize_index;
  107. gJSVMExports.duk_require_normalize_index = duk_require_normalize_index;
  108. gJSVMExports.duk_is_valid_index = duk_is_valid_index;
  109. gJSVMExports.duk_require_valid_index = duk_require_valid_index;
  110. gJSVMExports.duk_get_top = duk_get_top;
  111. gJSVMExports.duk_set_top = duk_set_top;
  112. gJSVMExports.duk_get_top_index = duk_get_top_index;
  113. gJSVMExports.duk_require_top_index = duk_require_top_index;
  114. gJSVMExports.duk_check_stack = duk_check_stack;
  115. gJSVMExports.duk_require_stack = duk_require_stack;
  116. gJSVMExports.duk_check_stack_top = duk_check_stack_top;
  117. gJSVMExports.duk_require_stack_top = duk_require_stack_top;
  118. gJSVMExports.duk_swap = duk_swap;
  119. gJSVMExports.duk_swap_top = duk_swap_top;
  120. gJSVMExports.duk_dup = duk_dup;
  121. gJSVMExports.duk_dup_top = duk_dup_top;
  122. gJSVMExports.duk_insert = duk_insert;
  123. gJSVMExports.duk_replace = duk_replace;
  124. gJSVMExports.duk_copy = duk_copy;
  125. gJSVMExports.duk_remove = duk_remove;
  126. gJSVMExports.duk_xcopymove_raw = duk_xcopymove_raw;
  127. gJSVMExports.duk_push_undefined = duk_push_undefined;
  128. gJSVMExports.duk_push_null = duk_push_null;
  129. gJSVMExports.duk_push_boolean = duk_push_boolean;
  130. gJSVMExports.duk_push_true = duk_push_true;
  131. gJSVMExports.duk_push_false = duk_push_false;
  132. gJSVMExports.duk_push_number = duk_push_number;
  133. gJSVMExports.duk_push_nan = duk_push_nan;
  134. gJSVMExports.duk_push_int = duk_push_int;
  135. gJSVMExports.duk_push_uint = duk_push_uint;
  136. gJSVMExports.duk_push_string = duk_push_string;
  137. gJSVMExports.duk_push_lstring = duk_push_lstring;
  138. gJSVMExports.duk_push_pointer = duk_push_pointer;
  139. gJSVMExports.duk_push_sprintf = duk_push_sprintf;
  140. gJSVMExports.duk_push_vsprintf = duk_push_vsprintf;
  141. gJSVMExports.duk_push_string_file_raw = duk_push_string_file_raw;
  142. gJSVMExports.duk_push_this = duk_push_this;
  143. gJSVMExports.duk_push_current_function = duk_push_current_function;
  144. gJSVMExports.duk_push_current_thread = duk_push_current_thread;
  145. gJSVMExports.duk_push_global_object = duk_push_global_object;
  146. gJSVMExports.duk_push_heap_stash = duk_push_heap_stash;
  147. gJSVMExports.duk_push_global_stash = duk_push_global_stash;
  148. gJSVMExports.duk_push_thread_stash = duk_push_thread_stash;
  149. gJSVMExports.duk_push_object = duk_push_object;
  150. gJSVMExports.duk_push_array = duk_push_array;
  151. gJSVMExports.duk_push_c_function = duk_push_c_function;
  152. gJSVMExports.duk_push_c_lightfunc = duk_push_c_lightfunc;
  153. gJSVMExports.duk_push_thread_raw = duk_push_thread_raw;
  154. gJSVMExports.duk_push_error_object_raw = duk_push_error_object_raw;
  155. gJSVMExports.duk_push_error_object_va_raw = duk_push_error_object_va_raw;
  156. gJSVMExports.duk_push_buffer_raw = duk_push_buffer_raw;
  157. gJSVMExports.duk_push_heapptr = duk_push_heapptr;
  158. gJSVMExports.duk_pop = duk_pop;
  159. gJSVMExports.duk_pop_n = duk_pop_n;
  160. gJSVMExports.duk_pop_2 = duk_pop_2;
  161. gJSVMExports.duk_pop_3 = duk_pop_3;
  162. gJSVMExports.duk_get_type = duk_get_type;
  163. gJSVMExports.duk_check_type = duk_check_type;
  164. gJSVMExports.duk_get_type_mask = duk_get_type_mask;
  165. gJSVMExports.duk_check_type_mask = duk_check_type_mask;
  166. gJSVMExports.duk_is_undefined = duk_is_undefined;
  167. gJSVMExports.duk_is_null = duk_is_null;
  168. gJSVMExports.duk_is_null_or_undefined = duk_is_null_or_undefined;
  169. gJSVMExports.duk_is_boolean = duk_is_boolean;
  170. gJSVMExports.duk_is_number = duk_is_number;
  171. gJSVMExports.duk_is_nan = duk_is_nan;
  172. gJSVMExports.duk_is_string = duk_is_string;
  173. gJSVMExports.duk_is_object = duk_is_object;
  174. gJSVMExports.duk_is_buffer = duk_is_buffer;
  175. gJSVMExports.duk_is_pointer = duk_is_pointer;
  176. gJSVMExports.duk_is_lightfunc = duk_is_lightfunc;
  177. gJSVMExports.duk_is_array = duk_is_array;
  178. gJSVMExports.duk_is_function = duk_is_function;
  179. gJSVMExports.duk_is_c_function = duk_is_c_function;
  180. gJSVMExports.duk_is_ecmascript_function = duk_is_ecmascript_function;
  181. gJSVMExports.duk_is_bound_function = duk_is_bound_function;
  182. gJSVMExports.duk_is_thread = duk_is_thread;
  183. gJSVMExports.duk_is_callable = duk_is_callable;
  184. gJSVMExports.duk_is_dynamic_buffer = duk_is_dynamic_buffer;
  185. gJSVMExports.duk_is_fixed_buffer = duk_is_fixed_buffer;
  186. gJSVMExports.duk_is_primitive = duk_is_primitive;
  187. gJSVMExports.duk_get_error_code = duk_get_error_code;
  188. gJSVMExports.duk_get_boolean = duk_get_boolean;
  189. gJSVMExports.duk_get_number = duk_get_number;
  190. gJSVMExports.duk_get_int = duk_get_int;
  191. gJSVMExports.duk_get_uint = duk_get_uint;
  192. gJSVMExports.duk_get_string = duk_get_string;
  193. gJSVMExports.duk_get_lstring = duk_get_lstring;
  194. gJSVMExports.duk_get_buffer = duk_get_buffer;
  195. gJSVMExports.duk_get_pointer = duk_get_pointer;
  196. gJSVMExports.duk_get_c_function = duk_get_c_function;
  197. gJSVMExports.duk_get_context = duk_get_context;
  198. gJSVMExports.duk_get_heapptr = duk_get_heapptr;
  199. gJSVMExports.duk_get_length = duk_get_length;
  200. gJSVMExports.duk_require_undefined = duk_require_undefined;
  201. gJSVMExports.duk_require_null = duk_require_null;
  202. gJSVMExports.duk_require_boolean = duk_require_boolean;
  203. gJSVMExports.duk_require_number = duk_require_number;
  204. gJSVMExports.duk_require_int = duk_require_int;
  205. gJSVMExports.duk_require_uint = duk_require_uint;
  206. gJSVMExports.duk_require_string = duk_require_string;
  207. gJSVMExports.duk_require_lstring = duk_require_lstring;
  208. gJSVMExports.duk_require_buffer = duk_require_buffer;
  209. gJSVMExports.duk_require_pointer = duk_require_pointer;
  210. gJSVMExports.duk_require_c_function = duk_require_c_function;
  211. gJSVMExports.duk_require_context = duk_require_context;
  212. gJSVMExports.duk_require_heapptr = duk_require_heapptr;
  213. gJSVMExports.duk_to_undefined = duk_to_undefined;
  214. gJSVMExports.duk_to_null = duk_to_null;
  215. gJSVMExports.duk_to_boolean = duk_to_boolean;
  216. gJSVMExports.duk_to_number = duk_to_number;
  217. gJSVMExports.duk_to_int = duk_to_int;
  218. gJSVMExports.duk_to_uint = duk_to_uint;
  219. gJSVMExports.duk_to_int32 = duk_to_int32;
  220. gJSVMExports.duk_to_uint32 = duk_to_uint32;
  221. gJSVMExports.duk_to_uint16 = duk_to_uint16;
  222. gJSVMExports.duk_to_string = duk_to_string;
  223. gJSVMExports.duk_to_lstring = duk_to_lstring;
  224. gJSVMExports.duk_to_buffer_raw = duk_to_buffer_raw;
  225. gJSVMExports.duk_to_pointer = duk_to_pointer;
  226. gJSVMExports.duk_to_object = duk_to_object;
  227. gJSVMExports.duk_to_defaultvalue = duk_to_defaultvalue;
  228. gJSVMExports.duk_to_primitive = duk_to_primitive;
  229. gJSVMExports.duk_safe_to_lstring = duk_safe_to_lstring;
  230. gJSVMExports.duk_base64_encode = duk_base64_encode;
  231. gJSVMExports.duk_base64_decode = duk_base64_decode;
  232. gJSVMExports.duk_hex_encode = duk_hex_encode;
  233. gJSVMExports.duk_hex_decode = duk_hex_decode;
  234. gJSVMExports.duk_json_encode = duk_json_encode;
  235. gJSVMExports.duk_json_decode = duk_json_decode;
  236. gJSVMExports.duk_resize_buffer = duk_resize_buffer;
  237. gJSVMExports.duk_get_prop = duk_get_prop;
  238. gJSVMExports.duk_get_prop_string = duk_get_prop_string;
  239. gJSVMExports.duk_get_prop_index = duk_get_prop_index;
  240. gJSVMExports.duk_put_prop = duk_put_prop;
  241. gJSVMExports.duk_put_prop_string = duk_put_prop_string;
  242. gJSVMExports.duk_put_prop_index = duk_put_prop_index;
  243. gJSVMExports.duk_del_prop = duk_del_prop;
  244. gJSVMExports.duk_del_prop_string = duk_del_prop_string;
  245. gJSVMExports.duk_del_prop_index = duk_del_prop_index;
  246. gJSVMExports.duk_has_prop = duk_has_prop;
  247. gJSVMExports.duk_has_prop_string = duk_has_prop_string;
  248. gJSVMExports.duk_has_prop_index = duk_has_prop_index;
  249. gJSVMExports.duk_def_prop = duk_def_prop;
  250. gJSVMExports.duk_get_global_string = duk_get_global_string;
  251. gJSVMExports.duk_put_global_string = duk_put_global_string;
  252. gJSVMExports.duk_get_prototype = duk_get_prototype;
  253. gJSVMExports.duk_set_prototype = duk_set_prototype;
  254. gJSVMExports.duk_get_finalizer = duk_get_finalizer;
  255. gJSVMExports.duk_set_finalizer = duk_set_finalizer;
  256. gJSVMExports.duk_set_global_object = duk_set_global_object;
  257. gJSVMExports.duk_get_magic = duk_get_magic;
  258. gJSVMExports.duk_set_magic = duk_set_magic;
  259. gJSVMExports.duk_get_current_magic = duk_get_current_magic;
  260. gJSVMExports.duk_put_function_list = duk_put_function_list;
  261. gJSVMExports.duk_put_number_list = duk_put_number_list;
  262. gJSVMExports.duk_compact = duk_compact;
  263. gJSVMExports.duk_enum = duk_enum;
  264. gJSVMExports.duk_next = duk_next;
  265. gJSVMExports.duk_concat = duk_concat;
  266. gJSVMExports.duk_join = duk_join;
  267. gJSVMExports.duk_decode_string = duk_decode_string;
  268. gJSVMExports.duk_map_string = duk_map_string;
  269. gJSVMExports.duk_substring = duk_substring;
  270. gJSVMExports.duk_trim = duk_trim;
  271. gJSVMExports.duk_char_code_at = duk_char_code_at;
  272. gJSVMExports.duk_equals = duk_equals;
  273. gJSVMExports.duk_strict_equals = duk_strict_equals;
  274. gJSVMExports.duk_call = duk_call;
  275. gJSVMExports.duk_call_method = duk_call_method;
  276. gJSVMExports.duk_call_prop = duk_call_prop;
  277. gJSVMExports.duk_pcall = duk_pcall;
  278. gJSVMExports.duk_pcall_method = duk_pcall_method;
  279. gJSVMExports.duk_pcall_prop = duk_pcall_prop;
  280. gJSVMExports.duk_new = duk_new;
  281. gJSVMExports.duk_safe_call = duk_safe_call;
  282. gJSVMExports.duk_eval_raw = duk_eval_raw;
  283. gJSVMExports.duk_compile_raw = duk_compile_raw;
  284. gJSVMExports.duk_log = duk_log;
  285. gJSVMExports.duk_log_va = duk_log_va;
  286. gJSVMExports.duk_push_context_dump = duk_push_context_dump;
  287. gJSVMExports.duk_debugger_attach = duk_debugger_attach;
  288. gJSVMExports.duk_debugger_detach = duk_debugger_detach;
  289. gJSVMExports.duk_debugger_cooperate = duk_debugger_cooperate;
  290. }
  291. }