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
4 changes: 4 additions & 0 deletions include/JavaScriptContextBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
65 changes: 59 additions & 6 deletions src/JavaScriptContextBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,35 @@
#ifdef ENABLE_ESSOS
#include <EssosInstance.h>
#endif
#include <sys/stat.h>
#include <cstdlib>

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)
{
}

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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename as populateModulesPath

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
Expand Down Expand Up @@ -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())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1, Get one argument to readFile isModule (by default true). Pass it false, when we are calling this api for running local file/application
2, This will avoid read to fail everytime for all modules fist time.
3, Also, if isModule is set, read the file after appending modulespath, else ready directly

{
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();
}

Expand All @@ -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());
Expand All @@ -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);
}
Expand All @@ -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){

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1, Perfrom getenv and use it as sModulesPath if environment is set
2,Else, set module path to pwd+/modules as written
3, Don;t want multiple if else

std::cout<<"JSRUNTIME_MODULES_PATH:"<<std::getenv("JSRUNTIME_MODULES_PATH")<<std::endl;
std::cout<<"Modules path variable set successfully"<<std::endl;
}
else{
std::cout<<"Modules path variable cannot be set!"<<std::endl;
}
return sModulesPath;
}

}
29 changes: 17 additions & 12 deletions src/jsc/JavaScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,30 +405,32 @@ if (mModuleSettings.enablePlayer)
injectFun(mContext, "require", requireCallback);
if(mModuleSettings.enablePlayer)
{
runFile("modules/video.js", nullptr);
runFile("video.js", nullptr);
}
if (mModuleSettings.enableXHR)
{
runFile("modules/xhr.js", nullptr);
runFile("xhr.js", nullptr);
}
if (mModuleSettings.enableHttp)
{
runFile("modules/http.js", nullptr);
runFile("modules/https.js", nullptr);
runFile("http.js", nullptr);
runFile("https.js", nullptr);

}
if (mModuleSettings.enableFetch)
{
runFile("modules/node-fetch.js", nullptr/*, true*/);
runFile("node-fetch.js" , nullptr/*, true*/);
}
runFile("modules/utils.js", nullptr);
runFile("utils.js", nullptr);

if (mModuleSettings.enableWebSocketEnhanced)
{
runFile("modules/event.js", nullptr);
runFile("modules/wsenhanced.js", nullptr);
}
runFile("event.js", nullptr);
runFile("wsenhanced.js", nullptr);
}
else if(mModuleSettings.enableWebSocket)
{
runFile("modules/ws.js", nullptr);
runFile("ws.js", nullptr);
}
#ifdef WS_SERVER_ENABLED
if (mEnableWebSockerServer)
Expand All @@ -439,8 +441,8 @@ if (mModuleSettings.enablePlayer)
#endif
if (mModuleSettings.enableWindow)
{
runFile("modules/window.js", nullptr/*, true*/);
runFile("modules/windowwrapper.js", nullptr/*, true*/);
runFile("window.js", nullptr/*, true*/);
runFile("windowwrapper.js", nullptr/*, true*/);
}
else if (mModuleSettings.enableJSDOM)
{
Expand Down Expand Up @@ -552,3 +554,6 @@ void JavaScriptContext::setPlaybackStartTime(double time)
double launchTime = mPerformanceMetrics.playbackStartTime - mPerformanceMetrics.createApplicationStartTime;
NativeJSLogger::log(INFO, "Launch_Duration for ID %d | URL %s : %.3f ms\n", mIds, mUrls.c_str(), launchTime);
}



Loading