Browse Source

js引擎修改 因为 并发的时候可能导致出错

andy 1 year ago
parent
commit
b4ee66112b
1 changed files with 4 additions and 10 deletions
  1. 4 10
      src/main/java/com/scbfkj/uni/library/script/JsScriptEngineUtil.java

+ 4 - 10
src/main/java/com/scbfkj/uni/library/script/JsScriptEngineUtil.java

@@ -25,7 +25,7 @@ public final class JsScriptEngineUtil {
             }
             """;
     private static final String ARGS = """
-            let args = JSON.parse(params)
+            let args = JSON.parse(paramsArgsObject)
             """;
     private static final KeyedObjectPool<String, Source> sourcePool = new GenericKeyedObjectPool<>(new BaseKeyedPooledObjectFactory<>() {
         @Override
@@ -49,10 +49,10 @@ public final class JsScriptEngineUtil {
 
         String sc = ARGS + script;
 
-        Source source = sourcePool.borrowObject(sc);
+        Source source = Source.create("js", sc);
         try (Context context = Context.create("js")) {
             Value bindings = context.getBindings("js");
-            bindings.putMember("params", DataFormatUtil.toString(args));
+            bindings.putMember("paramsArgsObject", DataFormatUtil.toString(args));
             Value function = context.eval(source);
             if (function.canExecute()) {
                 Value result = function.execute();
@@ -64,10 +64,6 @@ public final class JsScriptEngineUtil {
             System.out.println(script);
             e.printStackTrace();
             throw e;
-        } finally {
-            if (Objects.nonNull(source)) {
-                sourcePool.returnObject(sc, source);
-            }
         }
     }
 
@@ -81,13 +77,11 @@ public final class JsScriptEngineUtil {
     }
 
     private static String toString(Value value) throws Exception {
-        Source source = sourcePool.borrowObject(TO_STRING_SCRIPT);
+        Source source = Source.create("js", TO_STRING_SCRIPT);
         try (Context context = Context.create("js")) {
             Value function = context.eval(source);
 
             return function.execute(value).asString();
-        } finally {
-            sourcePool.returnObject(TO_STRING_SCRIPT, source);
         }
     }
 }