Skip to content

Commit

Permalink
*Create fallback context in executor
Browse files Browse the repository at this point in the history
*Use __context instead of ContextFacade
*Use evalCode in ScriptEngineExecutorsManager
*Cleanup module code
  • Loading branch information
tdermendjiev authored and delchev committed Sep 21, 2021
1 parent 4011d62 commit 1c47d3c
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 deletions.
Expand Up @@ -73,7 +73,7 @@ public static Object evalModule(String code, Map<Object, Object> executionContex
public static Object executeServiceCode(String engineType, String code, Map<Object, Object> executionContext) throws ScriptingException {
IScriptEngineExecutor scriptEngineExecutor = ScriptEngineExecutorFactory.getScriptEngineExecutor(engineType);
if (scriptEngineExecutor != null) {
return scriptEngineExecutor.executeServiceCode(code, executionContext);
return scriptEngineExecutor.evalCode(code, executionContext);
}

throw new ScriptingException(
Expand Down
Expand Up @@ -78,7 +78,7 @@ public static final String MODULE_CODE(Boolean isDebugEnabled) throws IOExceptio
}

public static final String MODULE_CREATE_CODE = "let mainModule = createModule(\".\");\n" +
"org.eclipse.dirigible.api.v3.core.ContextFacade.set(\"main_module\", mainModule);";
"__context.put(\"main_module\", mainModule);";

public static final String MODULE_LOAD_CODE = "mainModule.load(MODULE_FILENAME);";

Expand Down
Expand Up @@ -125,6 +125,10 @@ public Object executeService(String moduleOrCode, Map<Object, Object> executionC
throw new ScriptingException("JavaScript module name cannot be null");
}

if (executionContext == null) {
executionContext = new HashMap<Object, Object>();
}

if (isModule) {
ResourcePath resourcePath = getResourcePath(moduleOrCode, MODULE_EXT_JS, MODULE_EXT_GRAALVM);
moduleOrCode = resourcePath.getModule();
Expand Down Expand Up @@ -181,6 +185,7 @@ public Object executeService(String moduleOrCode, Map<Object, Object> executionC
beforeEval(context);

if (commonJSModule) {

context.eval(ENGINE_JAVA_SCRIPT, Require.LOAD_CONSOLE_CODE);
context.eval(ENGINE_JAVA_SCRIPT, Require.MODULE_CODE(isDebugEnabled));
context.eval(ENGINE_JAVA_SCRIPT, Require.MODULE_CREATE_CODE);
Expand Down
Expand Up @@ -174,7 +174,6 @@ function loadModule (request, parent) {
} else {
id = request;
// debug("ABSOLUTE: id="+id);
// paths = org.eclipse.dirigible.api.v3.core.ContextFacade.get("paths")
paths = ["/"];
}

Expand Down Expand Up @@ -215,9 +214,8 @@ Module.prototype.loadScript = function (filename) {
return loadModule(url, self);
}

// require.paths = org.eclipse.dirigible.api.v3.core.ContextFacade.get("paths");
require.paths = ["/"];
require.main = org.eclipse.dirigible.api.v3.core.ContextFacade.get("main_module");
require.main = __context.get("main_module");
// create wrapper function
var wrapper = "var __wrap__ = function (exports, require, module, __filename, __dirname) { "
+ "\ndebugger;\n\n" +
Expand Down Expand Up @@ -246,9 +244,8 @@ Module.prototype.loadScriptString = function (script) {
return loadModule(url, self);
}

// require.paths = org.eclipse.dirigible.api.v3.core.ContextFacade.get("paths");
require.paths = ["/"];
require.main = org.eclipse.dirigible.api.v3.core.ContextFacade.get("main_module");
require.main = __context.get("main_module");
// create wrapper function
var wrapper = "var __wrap__ = function (exports, require, module, __filename, __dirname) { "
+ "\ndebugger;\n\n" +
Expand Down
Expand Up @@ -174,7 +174,6 @@
} else {
id = request;
// debug("ABSOLUTE: id="+id);
// paths = org.eclipse.dirigible.api.v3.core.ContextFacade.get("paths")
paths = ["/"];
}

Expand Down Expand Up @@ -215,9 +214,8 @@
return loadModule(url, self);
}

// require.paths = org.eclipse.dirigible.api.v3.core.ContextFacade.get("paths");
require.paths = ["/"];
require.main = org.eclipse.dirigible.api.v3.core.ContextFacade.get("main_module");
require.main = __context.get("main_module");
// create wrapper function
var wrapper = "var __wrap__ = function (exports, require, module, __filename, __dirname) { "
+ content
Expand Down Expand Up @@ -245,9 +243,8 @@
return loadModule(url, self);
}

// require.paths = org.eclipse.dirigible.api.v3.core.ContextFacade.get("paths");
require.paths = ["/"];
require.main = org.eclipse.dirigible.api.v3.core.ContextFacade.get("main_module");
require.main = __context.get("main_module");
// create wrapper function
var wrapper = "var __wrap__ = function (exports, require, module, __filename, __dirname) { "
+ content
Expand Down

0 comments on commit 1c47d3c

Please sign in to comment.