diff --git a/include/JavaScriptContextBase.h b/include/JavaScriptContextBase.h index 412ae26..dba8f89 100644 --- a/include/JavaScriptContextBase.h +++ b/include/JavaScriptContextBase.h @@ -68,5 +68,9 @@ class JavaScriptContextBase:public IJavaScriptContext, public JavaScriptKeyListe bool mEmbedWebBridge; bool mEnableWebSockerServer; ModuleSettings mModuleSettings; + + static std::string sModulesPath; + static std::string getModulesPath(); + }; #endif diff --git a/src/JavaScriptContextBase.cpp b/src/JavaScriptContextBase.cpp index c649c94..90f9e42 100644 --- a/src/JavaScriptContextBase.cpp +++ b/src/JavaScriptContextBase.cpp @@ -25,9 +25,12 @@ #ifdef ENABLE_ESSOS #include #endif +#include +#include std::string JavaScriptContextBase::sThunderJSCode = ""; std::string JavaScriptContextBase::sWebBridgeCode = ""; +std::string JavaScriptContextBase::sModulesPath = "" ; JavaScriptContextFeatures::JavaScriptContextFeatures(bool embedThunderJS, bool embedWebBridge, bool enableWebSockerServer, ModuleSettings& moduleSettings):mEmbedThunderJS(embedThunderJS), mEmbedWebBridge(embedWebBridge), mEnableWebSockerServer(enableWebSockerServer), mModuleSettings(moduleSettings) { @@ -35,20 +38,22 @@ JavaScriptContextFeatures::JavaScriptContextFeatures(bool embedThunderJS, bool e JavaScriptContextBase::JavaScriptContextBase(JavaScriptContextFeatures& features, std::string url, IJavaScriptEngine* jsEngine): mApplicationUrl(url), mEngine(jsEngine), mEmbedThunderJS(features.mEmbedThunderJS), mEmbedWebBridge(features.mEmbedWebBridge), mEnableWebSockerServer(features.mEnableWebSockerServer), mModuleSettings(features.mModuleSettings) { + getModulesPath(); if (mEmbedThunderJS) { if (sThunderJSCode.empty()) - { - sThunderJSCode = readFile("modules/thunderJS.js"); + { + sThunderJSCode = readFile("thunderJS.js"); } } if (mEmbedWebBridge) { if (sWebBridgeCode.empty()) - { - sWebBridgeCode = readFile("modules/webbridgesdk.js"); + { + sWebBridgeCode = readFile("webbridgesdk.js"); } } + #ifdef ENABLE_ESSOS EssosInstance::instance()->registerKeyListener(this); #endif @@ -78,6 +83,22 @@ std::string JavaScriptContextBase::readFile(const char *file) std::ifstream src_file(file); std::stringstream src_script; src_script << src_file.rdbuf(); + if(src_script.str().empty()) + { + std::string fileName=sModulesPath; + fileName.append(file); + struct stat path; + if (stat(fileName.c_str(), &path) == 0) { + std::cout << "File exists at: " << fileName << std::endl; + std::ifstream src_file(fileName.c_str()); + src_script.str(""); + src_script.clear(); + src_script << src_file.rdbuf(); + } + else { + std::cout << file << "does not exist at: " << fileName << std::endl; + } + } return src_script.str(); } @@ -94,7 +115,8 @@ bool JavaScriptContextBase::runFile(const char *file, const char* args, bool isA scriptToRun = readFile(file); if(scriptToRun.empty()) { - std::string fileName("/home/root/"); + //newly added + std::string fileName=sModulesPath; fileName.append(file); scriptToRun = readFile(fileName.c_str()); NativeJSLogger::log(INFO, "Checking in [%s]\n", fileName.c_str()); @@ -103,7 +125,6 @@ bool JavaScriptContextBase::runFile(const char *file, const char* args, bool isA NativeJSLogger::log(ERROR, "%s ... load error / not found. %s\n", __PRETTY_FUNCTION__, file); fflush(stdout); return false; - } } return evaluateScript(scriptToRun.c_str(), isApplication?file:nullptr, args, isApplication); } @@ -127,3 +148,35 @@ void JavaScriptContextBase::onKeyRelease(struct JavaScriptKeyDetails& details) { processKeyEvent(details, false); } + +std::string JavaScriptContextBase::getModulesPath(){ + if(!sModulesPath.empty()){ + return sModulesPath; + } + else{ + struct stat info; + std::string home; + char* cwd = getcwd(nullptr,0); + std::string PWD=cwd; + PWD=PWD+"/modules/"; + if (stat(PWD.c_str(), &info) == 0 && (info.st_mode & S_IFDIR)){ + home = PWD; // "/home/root/modules/" + } + else if(stat(PWD.c_str(), &info) == 0 && (info.st_mode & S_IFDIR)){ + home = PWD; // "/runtime/modules/" + } + else if(stat(PWD.c_str(), &info) == 0 && (info.st_mode & S_IFDIR)){ + home = PWD; + } + sModulesPath = home; + if(setenv("JSRUNTIME_MODULES_PATH",home.c_str(),1)==0){ + std::cout<<"JSRUNTIME_MODULES_PATH:"<