diff --git a/include/cache/commons.hpp b/include/cache/commons.hpp index b1e19bee..db2bd6f4 100644 --- a/include/cache/commons.hpp +++ b/include/cache/commons.hpp @@ -24,7 +24,7 @@ enum class resource_t{ BUFFER, //uint8_t[] XML_TREE, //pugi::xml_document SCRIPT, //struct script_t - DYNAMIC_LIB, //? + PATH, //a string used as path reference, basically like buffer. Used to save info about path resolution. OTHER_RESOURCE, //? }; diff --git a/include/cache/memory-storage.hpp b/include/cache/memory-storage.hpp index 3795dd07..8ab59ee9 100644 --- a/include/cache/memory-storage.hpp +++ b/include/cache/memory-storage.hpp @@ -8,6 +8,7 @@ //Its content is not exposed via any sort of API to embedded scripts. +#include #include #include #include @@ -69,7 +70,10 @@ class mem_storage_t{ inline entry_it end() {return entries.end();} static inline mem_key_t empty_key = {"",0,resource_t::NONE}; public: + //Add a memory entry from various sources. Interpreting it is a task for the consumer later on, based on the resource type tag. + entry_it fetch_from_buffer(const mem_key_t& path, std::span str); + entry_it fetch_from_cstring(const mem_key_t& path, std::string_view str); entry_it fetch_from_fs(const mem_key_t& path); # ifdef HAS_CURL entry_it fetch_from_http(const mem_key_t& path); diff --git a/include/cbindings/vs.h b/include/cbindings/vs.h index f8aa01a4..23ba38c3 100644 --- a/include/cbindings/vs.h +++ b/include/cbindings/vs.h @@ -70,7 +70,7 @@ enum vs_frame_access_t{ }; typedef enum vs_frame_access_t vs_frame_access_t; -enum struct vs_component_t{ +enum vs_component_t{ VS_COMPONENT_NONE, VS_COMPONENT_XML, VS_COMPONENT_WASM, diff --git a/include/fetcher.hpp b/include/fetcher.hpp index e1e9e052..9c160107 100644 --- a/include/fetcher.hpp +++ b/include/fetcher.hpp @@ -16,7 +16,16 @@ enum struct component_t{ component_t component_t_i(const char* t); constexpr const char* component_t_s(component_t t); -std::tuple fetch_component(); +/*Fully fetches a component by: +- Looking for the best match which exists +- Save the path resolution to cache +- Load it based on its type: + - XML load and store as xml document (compile it if needed, and store the template as its own xml document?) + - WASM load in memory as binary (buffer) or a script once WASM scripts are supported?. + - LIB skip, the library will be loaded on demand + - CNATIVE compile and save it as script. Technically it is not, but I am not going to compile it over and over +*/ +std::tuple fetch_component(); /** * @brief Fetch resource using its path and some contextual information (or get it if already on cache) diff --git a/src/cache/memory-storage.cpp b/src/cache/memory-storage.cpp index a331a5fe..b3984388 100644 --- a/src/cache/memory-storage.cpp +++ b/src/cache/memory-storage.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -16,6 +17,27 @@ namespace vs{ namespace cache{ +mem_storage_t::entry_it mem_storage_t::fetch_from_buffer(const mem_key_t& path, std::span str){ + uint8_t* buffer = new uint8_t[str.size()]; + memcpy(buffer,str.data(),str.size()); + auto w =std::shared_ptr(new buffer_t{buffer,str.size()}, +[](buffer_t* p){delete[] p->data;delete p;}); + auto it = entries.emplace(path, w); + + if(it.second==true)return it.first; + else return entries.end(); +} + +mem_storage_t::entry_it mem_storage_t::fetch_from_cstring(const mem_key_t& path, std::string_view str){ + uint8_t* buffer = new uint8_t[str.size()+1]; + memcpy(buffer,str.data(),str.size()); + buffer[str.size()]=0; + auto w =std::shared_ptr(new buffer_t{buffer,str.size()+1}, +[](buffer_t* p){delete[] p->data;delete p;}); + auto it = entries.emplace(path, w); + + if(it.second==true)return it.first; + else return entries.end(); +} + mem_storage_t::entry_it mem_storage_t::fetch_from_fs(const mem_key_t& path){ uint8_t* buffer = nullptr; size_t fsize = 0; @@ -36,7 +58,7 @@ mem_storage_t::entry_it mem_storage_t::fetch_from_fs(const mem_key_t& path){ else return entries.end(); } //Special destructor that must also destroy the buffer itself once done. - auto w =std::shared_ptr(new buffer_t{buffer,fsize}, +[](buffer_t* p){delete[] p->data;delete p;}); + auto w =std::shared_ptr(new buffer_t{buffer,fsize+1}, +[](buffer_t* p){delete[] p->data;delete p;}); auto it = entries.emplace(path, w); if(it.second==true)return it.first; else return entries.end(); @@ -73,6 +95,7 @@ mem_storage_t::entry_it mem_storage_t::fetch_from_http(const mem_key_t& path){ return entries.end(); } else{ + //TODO: Check if I need NULL termination and explcit fsize+1; auto w =std::shared_ptr(new buffer_t{buffer,fsize}, +[](buffer_t* p){free((void*) p->data);delete p;}); auto it = entries.emplace(path, w); curl_easy_cleanup(curl); @@ -118,6 +141,7 @@ mem_storage_t::entry_it mem_storage_t::fetch_from_http(const mem_key_t& path){ return entries.end(); } else{ + //TODO: Check if I need NULL termination and explcit fsize+1; auto w =std::shared_ptr(new buffer_t{buffer,fsize}, +[](buffer_t* p){free((void*)p->data);delete p;}); auto it = entries.emplace(path, w); curl_easy_cleanup(curl); @@ -136,7 +160,7 @@ mem_storage_t::entry_it mem_storage_t::fetch_from_http(const mem_key_t& path){ #endif mem_storage_t::entry_it mem_storage_t::fetch_from_res_storage(const mem_key_t& path){ - //I need to have SQLITE integrate first. + //TODO: I need to have SQLITE integrate first. return entries.end(); } @@ -145,7 +169,6 @@ mem_storage_t::entry_it mem_storage_t::fetch_from_http(const mem_key_t& path){ auto it = entries.emplace(k,src); if(it.second==true)return it.first; else return entries.end(); -; } void mem_storage_t::drop(const mem_key_t& key){ diff --git a/src/fetcher.cpp b/src/fetcher.cpp index 178a36d4..c320507f 100644 --- a/src/fetcher.cpp +++ b/src/fetcher.cpp @@ -36,7 +36,7 @@ constexpr const char* component_t_s(component_t t){ else return nullptr; } -std::tuple fetch_component(){ +std::tuple fetch_component(){ //TODO: }