Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ option(BUILD_JSRUNTIME_APP "BUILD_JSRUNTIME_APP" ON)

option(ENABLE_JSRUNTIME_SERVER "ENABLE_JSRUNTIME_SERVER" OFF)
option(BUILD_JSRUNTIME_CLIENT "BUILD_JSRUNTIME_CLIENT" OFF)
option(NATIVEJS_DEVELOPER_MODE "NATIVEJS_DEVELOPER_MODE" OFF)

#can be jsc or node or v8 or quickjs
option(JSRUNTIME_ENGINE_NAME "JSRUNTIME_ENGINE_NAME" "jsc")
Expand Down
74 changes: 60 additions & 14 deletions include/NativeJSRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <IJavaScriptContext.h>
#include <ModuleSettings.h>
#include <condition_variable>
#include <list>

namespace JsRuntime {

Expand Down Expand Up @@ -75,29 +76,62 @@ namespace JsRuntime {
IJavaScriptContext* consoleContext = nullptr;
ModuleSettings moduleSettings{};
};


struct ApplicationDetails{
uint32_t id;
std::string url;
};

enum RequestType{
CREATE=0,
RUN,
TERMINATE,
RUNSCRIPT
};

struct ApplicationRequest
{
ApplicationRequest(uint32_t id, RequestType requestType, std::string url="", bool enableHttp=false, bool enableXHR=false, bool enableWebSocket=false, bool enableWebSocketEnhanced=false, bool enableFetch=false, bool enableJSDOM=false, bool enableWindow=false, bool enablePlayer=false): mId(id), mRequestType(requestType), mUrl(url), mEnableHttp(enableHttp), mEnableXHR(enableXHR), mEnableWebSocket(enableWebSocket), mEnableWebSocketEnhanced(enableWebSocketEnhanced), mEnableFetch(enableFetch), mEnableJSDOM(enableJSDOM), mEnableWindow(enableWindow), mEnablePlayer(enablePlayer)
{
}
uint32_t mId;
RequestType mRequestType;
std::string mUrl;
bool mEnableHttp;
bool mEnableXHR;
bool mEnableWebSocket;
bool mEnableWebSocketEnhanced;
bool mEnableFetch;
bool mEnableJSDOM;
bool mEnableWindow;
bool mEnablePlayer;
//ModuleSettings mModuleSettings;
};
class NativeJSRenderer
{
{
public:
~NativeJSRenderer();
~NativeJSRenderer();
NativeJSRenderer(std::string waylandDisplay="");
bool initialize();
bool terminate();
void run();
void launchApplication(std::string url, ModuleSettings& moduleSettings);
void terminateApplication(std::string url);
void setEnvForConsoleMode(ModuleSettings& moduleSettings);
std::vector<std::string> getApplications();

private:
void loadApplication(std::string url, ModuleSettings& moduleSettings);
void unloadApplication(std::string url);
bool runApplication(uint32_t id, std::string url);
bool runJavaScript(uint32_t id, std::string code);
bool createApplication(ModuleSettings& moduleSettings) ;
bool terminateApplication(uint32_t id);
std::list<ApplicationDetails> getApplications();
uint32_t mId;
private:
bool downloadFile(std::string& url, MemoryStruct& chunk);
void processDevConsoleRequests();
void runDeveloperConsole(ModuleSettings moduleSettings);
void createApplicationInternal(ApplicationRequest& appRequest);
void runApplicationInternal(ApplicationRequest& appRequest);
void terminateApplicationInternal(ApplicationRequest& appRequest);
void runJavaScriptInternal(ApplicationRequest& appRequest);
static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream);
IJavaScriptEngine* mEngine;
std::map<std::string, IJavaScriptContext*> mContextMap;
bool mRunning;
std::string mTestFileName;
std::unique_ptr<ConsoleState> mConsoleState;
Expand All @@ -107,6 +141,18 @@ namespace JsRuntime {
bool mEnableWebSocketServer;
bool mEssosInitialized;
bool mConsoleMode;
std::mutex mUserMutex;
};
}
std::mutex mUserMutex;
struct ApplicationData{
std::string url;
IJavaScriptContext* context;
};
std::map<uint32_t, ApplicationData> mContextMap;
std::vector<ApplicationRequest> gPendingRequests;

};
};





91 changes: 69 additions & 22 deletions src/JSRuntimeServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ class JsonWrap
return res;
}

uint32_t getUint32(const char *name, bool &err)
{
uint32_t res;
cJSON *itm = cJSON_GetObjectItem(mPtr, name);
if (!itm || !cJSON_IsNumber(itm))
{
std::cerr << "Error: " << name << "is not a Uint32_t" << std::endl;
err = true;
}
else
{
res = (uint32_t) itm->valuedouble;
err = false;
}
return res;
}

cJSON *get() { return mPtr; }

private:
Expand All @@ -81,7 +98,6 @@ class JsonWrap
cJSON *mPtr;
bool mIsRoot;
};

JSRuntimeServer *JSRuntimeServer::getInstance()
{
static JSRuntimeServer instance;
Expand Down Expand Up @@ -180,7 +196,6 @@ void JSRuntimeServer::onMessage(websocketpp::connection_hdl hdl, message_ptr msg
{
break;
}

if (method == "launchApplication")
{
JsonWrap jParams(jRoot, "params");
Expand All @@ -193,40 +208,72 @@ void JSRuntimeServer::onMessage(websocketpp::connection_hdl hdl, message_ptr msg
{
break;
}
std::string options = jParams.getString("options", error);
ModuleSettings settings;
settings.fromString(options);
mRenderer->launchApplication(url, settings);
result = "ok";
std::string options = jParams.getString("moduleSettings", error);
ModuleSettings moduleSettings;
moduleSettings.fromString(options);
mRenderer->createApplication(moduleSettings);
uint32_t id = mRenderer->mId;
mRenderer->runApplication(id, url);
std::ostringstream oss;
oss<< "ID : " << id;
result = oss.str();
}
else if (method == "terminateApplication")
else if (method == "runJavaScript")
{
JsonWrap jParams(jRoot, "params");
if ( jParams.get() == nullptr)
{
break;
}
uint32_t id = jParams.getUint32("id", error);
if (error)
{
break;
}
std::string code = jParams.getString("code", error);
if (error)
{
break;
}
mRenderer->runJavaScript(id, code);
result = "ok";

}
else if (method == "destroyApplication")
{
JsonWrap jParams(jRoot, "params");
if (jParams.get() == nullptr)
{
break;
}
std::string url = jParams.getString("url", error);
if (error)
uint32_t id = jParams.getUint32("id", error);
if (error)
{
break;
}
mRenderer->terminateApplication(url);
mRenderer->terminateApplication(id);
result = "ok";
}

else if (method == "getApplications")
{
std::vector<std::string> apps = mRenderer->getApplications();
std::ostringstream oss;
for (size_t i = 0; i < apps.size(); ++i)
{
if (i != 0)
{
oss << ' ';
}
oss << apps[i];
}
result = oss.str();
std::list<JsRuntime::ApplicationDetails> apps = mRenderer->getApplications();
if(!apps.empty())
{
std::ostringstream oss;
for (const auto& app : apps)
{
if (!oss.str().empty())
{
oss << " ";
}
oss << "ID: " << app.id << ", URL: " << app.url;
}
result = oss.str();
}
else
result = "No ID found";

}
else if (method == "ping")
{
Expand Down
Loading