MyNativePlugin.cpp 700 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "AtomicPlugin.h"
  2. #ifdef _MSC_VER
  3. #include <windows.h>
  4. #define EXPORT_API __declspec(dllexport)
  5. #else
  6. #define EXPORT_API
  7. #endif
  8. extern "C"
  9. {
  10. static int js_getAnswer(duk_context* ctx)
  11. {
  12. duk_push_number(ctx, 42);
  13. return 1;
  14. }
  15. // plugin init is a duk cfunction itself, so we can pcall the import
  16. int EXPORT_API atomic_plugin_init(duk_context* ctx)
  17. {
  18. // exports object at 0
  19. if (!duk_is_object(ctx, 0))
  20. {
  21. duk_push_boolean(ctx, 0);
  22. return 1;
  23. }
  24. // export out native functions
  25. duk_push_c_function(ctx, js_getAnswer, 0);
  26. duk_put_prop_string(ctx, -2, "getAnswer");
  27. // and return success
  28. duk_push_boolean(ctx, 1);
  29. return 1;
  30. }
  31. }