#include "include/flutter_qjs/flutter_qjs_plugin.h" // This must be included before many other Windows headers. #include // For getPlatformVersion; remove unless needed for your plugin implementation. #include #include #include #include #include "js_engine.hpp" namespace { class FlutterQjsPlugin : public flutter::Plugin { public: static void RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar); FlutterQjsPlugin(); virtual ~FlutterQjsPlugin(); private: // Called when a method is called on this plugin's channel from Dart. void HandleMethodCall( const flutter::MethodCall &method_call, std::unique_ptr> result); }; std::shared_ptr> channel; // static void FlutterQjsPlugin::RegisterWithRegistrar( flutter::PluginRegistrarWindows *registrar) { channel = std::make_unique>( registrar->messenger(), "soko.ekibun.flutter_qjs", &flutter::StandardMethodCodec::GetInstance()); auto plugin = std::make_unique(); channel->SetMethodCallHandler( [plugin_pointer = plugin.get()](const auto &call, auto result) { plugin_pointer->HandleMethodCall(call, std::move(result)); }); registrar->AddPlugin(std::move(plugin)); } FlutterQjsPlugin::FlutterQjsPlugin() {} FlutterQjsPlugin::~FlutterQjsPlugin() {} const flutter::EncodableValue &ValueOrNull(const flutter::EncodableMap &map, const char *key) { static flutter::EncodableValue null_value; auto it = map.find(flutter::EncodableValue(key)); if (it == map.end()) { return null_value; } return it->second; } qjs::Engine *engine = nullptr; void FlutterQjsPlugin::HandleMethodCall( const flutter::MethodCall &method_call, std::unique_ptr> result) { // Replace "getPlatformVersion" check with your plugin's method. // See: // https://github.com/flutter/engine/tree/master/shell/platform/common/cpp/client_wrapper/include/flutter // and // https://github.com/flutter/engine/tree/master/shell/platform/glfw/client_wrapper/include/flutter // for the relevant Flutter APIs. if (method_call.method_name().compare("initEngine") == 0) { engine = new qjs::Engine(channel); flutter::EncodableValue response((long)engine); result->Success(&response); } else if (method_call.method_name().compare("evaluate") == 0) { flutter::EncodableMap args = *std::get_if(method_call.arguments()); std::string script = std::get(ValueOrNull(args, "script")); std::string name = std::get(ValueOrNull(args, "name")); auto presult = result.release(); engine->commit(qjs::EngineTask{ script, name, [presult](std::string resolve) { flutter::EncodableValue response(resolve); presult->Success(&response); }, [presult](std::string reject) { presult->Error("FlutterJSException", reject); }}); } else if (method_call.method_name().compare("close") == 0) { delete engine; result->Success(); } else { result->NotImplemented(); } } } // namespace void FlutterQjsPluginRegisterWithRegistrar( FlutterDesktopPluginRegistrarRef registrar) { FlutterQjsPlugin::RegisterWithRegistrar( flutter::PluginRegistrarManager::GetInstance() ->GetRegistrar(registrar)); }