JSPlugin.cpp 14 KB

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