Skip to content
Merged
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
2 changes: 2 additions & 0 deletions include/JavaScriptContextBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,7 @@ class JavaScriptContextBase:public IJavaScriptContext, public JavaScriptKeyListe
bool mEmbedWebBridge;
bool mEnableWebSockerServer;
ModuleSettings mModuleSettings;
static std::string sModulesPath;
static void populateModulesPath();
};
#endif
59 changes: 43 additions & 16 deletions src/JavaScriptContextBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,32 @@
#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)
{
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
Expand Down Expand Up @@ -75,9 +79,23 @@ void JavaScriptContextBase::registerCommonUtils()

std::string JavaScriptContextBase::readFile(const char *file)
{
std::ifstream src_file(file);
bool isModule = true;
std::ifstream src_file;
std::stringstream src_script;
src_script << src_file.rdbuf();
struct stat path;
if(stat(file, &path) == 0){
isModule=false;
}
if(!isModule){
src_file.open(file);
src_script << src_file.rdbuf();
}
else{
std::string fileName=sModulesPath;
fileName.append(file);
src_file.open(fileName);
src_script << src_file.rdbuf();
}
return src_script.str();
}

Expand All @@ -86,24 +104,17 @@ bool JavaScriptContextBase::runFile(const char *file, const char* args, bool isA
if (!file)
{
NativeJSLogger::log(WARN, "%s ... no script given.\n", __PRETTY_FUNCTION__);
fflush(stdout);
fflush(stdout);
return false;
}

std::string scriptToRun;
scriptToRun = readFile(file);
NativeJSLogger::log(INFO, "Checking in [%s]\n", file);
if(scriptToRun.empty())
{
std::string fileName("/home/root/");
fileName.append(file);
scriptToRun = readFile(fileName.c_str());
NativeJSLogger::log(INFO, "Checking in [%s]\n", fileName.c_str());
if(scriptToRun.empty())
{
NativeJSLogger::log(ERROR, "%s ... load error / not found. %s\n", __PRETTY_FUNCTION__, file);
fflush(stdout);
fflush(stdout);
return false;
}
}
return evaluateScript(scriptToRun.c_str(), isApplication?file:nullptr, args, isApplication);
}
Expand Down Expand Up @@ -132,3 +143,19 @@ ModuleSettings JavaScriptContextBase::getModuleSettings()
{
return mModuleSettings;
}

void JavaScriptContextBase::populateModulesPath(){
if(getenv("JSRUNTIME_MODULES_PATH"))
{
std::cout<<"JSRUNTIME_MODULES_PATH variable is set"<<std::endl;
sModulesPath = std::string(getenv("JSRUNTIME_MODULES_PATH"));
return;
}
else{
char* cwd = getcwd(nullptr,0);
std::string PWD=cwd;
sModulesPath=PWD+"/modules/";
}
std::cout<<"Modules Path:"<<sModulesPath<<std::endl;
return;
}
34 changes: 18 additions & 16 deletions src/jsc/JavaScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
#include <dlfcn.h>
#endif
#endif
#include <sstream>
#include <fstream>

extern "C" JS_EXPORT void JSSynchronousGarbageCollectForDebugging(JSContextRef);
#ifdef ENABLE_AAMP_JSBINDINGS_STATIC
Expand Down Expand Up @@ -407,48 +409,48 @@ 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)
{
NativeJSLogger::log(INFO, "enabling websocket server\n");
runFile("modules/wsserver.js", nullptr);
runFile("wsserver.js", nullptr);
}
#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)
{
runFile("modules/linkedjsdom.js", nullptr/*, true*/);
runFile("modules/linkedjsdomwrapper.js", nullptr/*, true*/);
runFile("modules/windowwrapper.js", nullptr/*, true*/);
runFile("linkedjsdom.js", nullptr/*, true*/);
runFile("linkedjsdomwrapper.js", nullptr/*, true*/);
runFile("windowwrapper.js", nullptr/*, true*/);
if(getenv("FIREBOLT_ENDPOINT")!=NULL)
{
auto FireboltEndpoint = std::string(getenv("FIREBOLT_ENDPOINT"));
Expand Down