Skip to content

Commit

Permalink
fix(基础模块): 修复js脚本中无法使用console问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Mar 14, 2023
1 parent d020896 commit 552dfd5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ protected final String prepare(Script script) {
"this.eval = function(e){};" +
"function readFully(){};" +
"function readLine(){};" +
"const console=_$console;" +
"const utils=_$utils;" +
"const print = function(e){console.log(e)};" +
"const echo = print;");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ private CompiledScript compile(Script script, boolean convert) {

ExecutionContext ctx = ExecutionContext.create();

ctx.setAttribute("console", new Jsr223ScriptFactory.Console(
ctx.setAttribute("_$console", new Jsr223ScriptFactory.Console(
LoggerFactory.getLogger("org.jetlinks.community.script." + script.getName())),
ScriptContext.ENGINE_SCOPE);
ctx.setAttribute("utils", getUtils(), ScriptContext.ENGINE_SCOPE);
ctx.setAttribute("_$utils", getUtils(), ScriptContext.ENGINE_SCOPE);

ctx.setAttribute("engine", null, ScriptContext.ENGINE_SCOPE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,18 @@ void testMake() {

}

@Test
@SneakyThrows
void testLog() {
{
JavaScriptFactory factory = getFactory();

factory.compile(Script.of("test","console.log(123)"))
.call();

}
}

public interface Api {
int add(int a, int b);

Expand Down

0 comments on commit 552dfd5

Please sign in to comment.