export module Crafter.CppDOM:BindingsExport; import std; import :EventTypes; import :BindingsImport; export namespace Crafter::CppDOMBindings { std::int32_t clickHandlerMaxId = 0; std::unordered_map>* clickHandlers = new std::unordered_map>(); std::int32_t mouseOverHandlerMaxId = 0; std::unordered_map>* mouseOverHandlers = new std::unordered_map>(); std::int32_t mouseOutHandlerMaxId = 0; std::unordered_map>* mouseOutHandlers = new std::unordered_map>(); std::int32_t mouseMoveHandlerMaxId = 0; std::unordered_map>* mouseMoveHandlers = new std::unordered_map>(); std::int32_t mouseDownHandlerMaxId = 0; std::unordered_map>* mouseDownHandlers = new std::unordered_map>(); std::int32_t mouseUpHandlerMaxId = 0; std::unordered_map>* mouseUpHandlers = new std::unordered_map>(); std::int32_t focusHandlerMaxId = 0; std::unordered_map>* focusHandlers = new std::unordered_map>(); std::int32_t blurHandlerMaxId = 0; std::unordered_map>* blurHandlers = new std::unordered_map>(); std::int32_t keyDownHandlerMaxId = 0; std::unordered_map>* keyDownHandlers = new std::unordered_map>(); std::int32_t keyUpHandlerMaxId = 0; std::unordered_map>* keyUpHandlers = new std::unordered_map>(); std::int32_t keyPressHandlerMaxId = 0; std::unordered_map>* keyPressHandlers = new std::unordered_map>(); std::int32_t changeHandlerMaxId = 0; std::unordered_map>* changeHandlers = new std::unordered_map>(); std::int32_t submitHandlerMaxId = 0; std::unordered_map>* submitHandlers = new std::unordered_map>(); std::int32_t inputHandlerMaxId = 0; std::unordered_map>* inputHandlers = new std::unordered_map>(); std::int32_t resizeHandlerMaxId = 0; std::unordered_map>* resizeHandlers = new std::unordered_map>(); std::int32_t scrollHandlerMaxId = 0; std::unordered_map>* scrollHandlers = new std::unordered_map>(); std::int32_t contextMenuHandlerMaxId = 0; std::unordered_map>* contextMenuHandlers = new std::unordered_map>(); std::int32_t dragStartHandlerMaxId = 0; std::unordered_map>* dragStartHandlers = new std::unordered_map>(); std::int32_t dragEndHandlerMaxId = 0; std::unordered_map>* dragEndHandlers = new std::unordered_map>(); std::int32_t dropHandlerMaxId = 0; std::unordered_map>* dropHandlers = new std::unordered_map>(); std::int32_t wheelHandlerMaxId = 0; std::unordered_map>* wheelHandlers = new std::unordered_map>(); std::int32_t dragOverHandlerMaxId = 0; std::unordered_map>* dragOverHandlers = new std::unordered_map>(); std::int32_t dragEnterHandlerMaxId = 0; std::unordered_map>* dragEnterHandlers = new std::unordered_map>(); std::int32_t dragLeaveHandlerMaxId = 0; std::unordered_map>* dragLeaveHandlers = new std::unordered_map>(); std::int32_t popStateHandlerMaxId = 0; std::unordered_map>* popStateHandlers = new std::unordered_map>(); std::int32_t fetchHandlerMaxId = 0; std::unordered_map>* fetchHandlers = new std::unordered_map>(); } extern "C" { __attribute__((export_name("WasmAlloc"))) void* WasmAlloc(std::int32_t size) { return std::malloc(size); } __attribute__((export_name("WasmFree"))) void WasmFree(void* ptr) { std::free(ptr); } __attribute__((export_name("deleteElement"))) void DeleteElement(void* ptr) { // This will be implemented in JavaScript } __attribute__((export_name("ExecuteClickHandler"))) void ExecuteClickHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::clickHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteMouseOverHandler"))) void ExecuteMouseOverHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::mouseOverHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteMouseOutHandler"))) void ExecuteMouseOutHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::mouseOutHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteMouseMoveHandler"))) void ExecuteMouseMoveHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::mouseMoveHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteMouseDownHandler"))) void ExecuteMouseDownHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button,std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::mouseDownHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteMouseUpHandler"))) void ExecuteMouseUpHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::mouseUpHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteFocusHandler"))) void ExecuteFocusHandler(std::int32_t handlerID, void* target, void* relatedTarget) { Crafter::CppDOMBindings::focusHandlers->find(handlerID)->second(Crafter::FocusEvent(target, relatedTarget)); } __attribute__((export_name("ExecuteBlurHandler"))) void ExecuteBlurHandler(std::int32_t handlerID, void* target, void* relatedTarget) { Crafter::CppDOMBindings::blurHandlers->find(handlerID)->second(Crafter::FocusEvent(target, relatedTarget)); } __attribute__((export_name("ExecuteKeyDownHandler"))) void ExecuteKeyDownHandler(std::int32_t handlerID, const char* key, std::int32_t keyCode, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::keyDownHandlers->find(handlerID)->second(Crafter::KeyboardEvent(key, keyCode, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteKeyUpHandler"))) void ExecuteKeyUpHandler(std::int32_t handlerID, const char* key, std::int32_t keyCode, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::keyUpHandlers->find(handlerID)->second(Crafter::KeyboardEvent(key, keyCode, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteKeyPressHandler"))) void ExecuteKeyPressHandler(std::int32_t handlerID, const char* key, std::int32_t keyCode, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::keyPressHandlers->find(handlerID)->second(Crafter::KeyboardEvent(key, keyCode, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteChangeHandler"))) void ExecuteChangeHandler(std::int32_t handlerID, const char* value) { Crafter::CppDOMBindings::changeHandlers->find(handlerID)->second(Crafter::ChangeEvent(value)); } __attribute__((export_name("ExecuteSubmitHandler"))) void ExecuteSubmitHandler(std::int32_t handlerID) { Crafter::CppDOMBindings::submitHandlers->find(handlerID)->second(); } __attribute__((export_name("ExecuteInputHandler"))) void ExecuteInputHandler(std::int32_t handlerID, const char* data, bool isComposing) { Crafter::CppDOMBindings::inputHandlers->find(handlerID)->second(Crafter::InputEvent(data, isComposing)); } __attribute__((export_name("ExecuteResizeHandler"))) void ExecuteResizeHandler(std::int32_t handlerID, std::int32_t width, std::int32_t height) { Crafter::CppDOMBindings::resizeHandlers->find(handlerID)->second(Crafter::ResizeEvent(width, height)); } __attribute__((export_name("ExecuteScrollHandler"))) void ExecuteScrollHandler(std::int32_t handlerID, double scrollX, double scrollY) { Crafter::CppDOMBindings::scrollHandlers->find(handlerID)->second(Crafter::ScrollEvent(scrollX, scrollY)); } __attribute__((export_name("ExecuteContextMenuHandler"))) void ExecuteContextMenuHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::contextMenuHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteDragStartHandler"))) void ExecuteDragStartHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::dragStartHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteDragEndHandler"))) void ExecuteDragEndHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::dragEndHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteDropHandler"))) void ExecuteDropHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::dropHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteWheelHandler"))) void ExecuteWheelHandler(std::int32_t handlerID, double deltaX, double deltaY, double deltaZ, std::int32_t deltaMode, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::wheelHandlers->find(handlerID)->second(Crafter::WheelEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey, deltaX, deltaY, deltaZ)); } __attribute__((export_name("ExecuteDragOverHandler"))) void ExecuteDragOverHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::dragOverHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteDragEnterHandler"))) void ExecuteDragEnterHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::dragEnterHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteDragLeaveHandler"))) void ExecuteDragLeaveHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, std::int32_t button, std::int32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) { Crafter::CppDOMBindings::dragLeaveHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey)); } __attribute__((export_name("ExecuteFetchHandler"))) void ExecuteFetchHandler(std::int32_t handlerID, const char* response) { std::cout << handlerID << std::endl; Crafter::CppDOMBindings::fetchHandlers->find(handlerID)->second(response); Crafter::CppDOMBindings::fetchHandlers->erase(handlerID); } __attribute__((export_name("ExecutePopStateHandler"))) void ExecutePopStateHandler(std::int32_t handlerID) { Crafter::CppDOMBindings::popStateHandlers->find(handlerID)->second(); } } export namespace Crafter::CppDOMBindings { __attribute__((import_module("env"), import_name("fetch"))) void Fetch(const char* url, std::int32_t urlLenght, std::int32_t handlerID); __attribute__((import_module("env"), import_name("fetchWithBody"))) void FetchWithBody(const char* url, std::int32_t urlLenght, const char* body, std::int32_t bodyLength, std::int32_t handlerID); void Fetch(const std::string_view url, std::function callback) { std::int32_t id = fetchHandlerMaxId++; CppDOMBindings::fetchHandlers->insert({id, callback}); Fetch(url.data(), url.size(), id); } void Fetch(const std::string_view url, const std::string_view body, std::function callback) { std::int32_t id = fetchHandlerMaxId++; CppDOMBindings::fetchHandlers->insert({id, callback}); FetchWithBody(url.data(), url.size(), body.data(), body.size(), id); } __attribute__((import_module("env"), import_name("getPathName"))) const char* GetPathName(); std::string GetPathNameString() { const char* path = GetPathName(); return std::string(path); } std::int32_t AddPopStateListener(std::function callback) { std::int32_t id = popStateHandlerMaxId++; popStateHandlers->insert({id, callback}); AddPopStateListener(id); return id; } void RemovePopStateListener(std::int32_t id) { RemovePopStateListener(id); popStateHandlers->erase(id); } void PushState(const std::string_view data, const std::string_view title, const std::string_view url) { PushState(data.data(), data.size(), title.data(), title.size(), url.data(), url.size()); } }