Skip to content

Commit

Permalink
将IEvalScope上线程不安全的函数移动到EvalRuntime类中,确保IServiceContext以及IServiceConte…
Browse files Browse the repository at this point in the history
…xt.getEvalScope()都是线程安全的对象。逻辑流编配时会出现并发执行Xpl模板的情况,需要确保不会出现并发冲突
  • Loading branch information
entropy-cloud committed Mar 18, 2024
1 parent 6b2c109 commit fb048c1
Show file tree
Hide file tree
Showing 185 changed files with 1,438 additions and 1,132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.nop.api.core.exceptions.NopException;
import io.nop.api.core.util.FutureHelper;
import io.nop.api.core.util.Guard;
import io.nop.api.core.util.ResolvedPromise;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
Expand Down Expand Up @@ -148,7 +147,7 @@ public static <T> CompletionStage<T> thenOnContext(CompletionStage<T> future) {
public static <T> CompletionStage<T> thenOnContext(CompletionStage<T> future, IContext context) {
Guard.notNull(context, "context");

if (future instanceof ResolvedPromise && context.isRunningOnContext()) {
if (FutureHelper.isDone(future) && context.isRunningOnContext()) {
return future;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public final class DefaultExpressionExecutor implements IExpressionExecutor {
public static final DefaultExpressionExecutor INSTANCE = new DefaultExpressionExecutor();

@Override
public Object execute(IExecutableExpression expr, IEvalScope scope) {
return expr.execute(this, scope);
public Object execute(IExecutableExpression expr, EvalRuntime rt) {
return expr.execute(this, rt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
public final class DisabledEvalScope implements IEvalScope {
public static final DisabledEvalScope INSTANCE = new DisabledEvalScope();

@Override
public EvalFrame getCurrentFrame() {
throw new NopEvalException(ERR_EVAL_DISABLED_EVAL_SCOPE);
}

@Override
public IBeanProvider getBeanProvider() {
Expand All @@ -48,16 +44,6 @@ public void setClassModelLoader(IClassModelLoader loader) {
throw new NopEvalException(ERR_EVAL_DISABLED_EVAL_SCOPE);
}

@Override
public IEvalOutput getOut() {
throw new NopEvalException(ERR_EVAL_DISABLED_EVAL_SCOPE);
}

@Override
public void setOut(IEvalOutput out) {
throw new NopEvalException(ERR_EVAL_DISABLED_EVAL_SCOPE);
}

@Override
public void setExtension(IVariableScope extension) {
throw new NopEvalException(ERR_EVAL_DISABLED_EVAL_SCOPE);
Expand All @@ -74,7 +60,7 @@ public boolean isInheritParentVars() {
}

@Override
public IEvalScope newChildScope(boolean inheritParentVars, boolean inheritParentOut, boolean threadSafe) {
public IEvalScope newChildScope(boolean inheritParentVars, boolean threadSafe) {
throw new NopEvalException(ERR_EVAL_DISABLED_EVAL_SCOPE);
}

Expand Down Expand Up @@ -133,11 +119,6 @@ public void setLocalValues(SourceLocation loc, Map<String, Object> values) {
throw new NopEvalException(ERR_EVAL_DISABLED_EVAL_SCOPE);
}

@Override
public IEvalScope duplicate() {
return this;
}

@Override
public void removeLocalValue(String name) {
throw new NopEvalException(ERR_EVAL_DISABLED_EVAL_SCOPE);
Expand All @@ -148,33 +129,4 @@ public void clear() {
throw new NopEvalException(ERR_EVAL_DISABLED_EVAL_SCOPE);
}

@Override
public ExitMode getExitMode() {
return null;
}

@Override
public void setExitMode(ExitMode exitMode) {
throw new NopEvalException(ERR_EVAL_DISABLED_EVAL_SCOPE);
}

@Override
public void pushFrame(EvalFrame frame) {

}

@Override
public void popFrame() {

}

@Override
public IExpressionExecutor getExpressionExecutor() {
return DefaultExpressionExecutor.INSTANCE;
}

@Override
public void setExpressionExecutor(IExpressionExecutor executor) {

}
}
12 changes: 2 additions & 10 deletions nop-core/src/main/java/io/nop/core/lang/eval/EvalExprProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,11 @@ public static boolean isGlobalVarName(String varName) {
return varName.charAt(0) == '$';
}

public static Object execute(IExecutableExpression expr, IEvalScope scope) {
return scope.getExpressionExecutor().execute(expr, scope);
}

public static IEvalScope newEvalScope(Map<String, Object> context) {
EvalScopeImpl scope = new EvalScopeImpl(context);
scope.setExpressionExecutor(_executor);
return scope;
return new EvalScopeImpl(context);
}

public static IEvalScope newEvalScope() {
EvalScopeImpl scope = new EvalScopeImpl();
scope.setExpressionExecutor(_executor);
return scope;
return new EvalScopeImpl();
}
}
104 changes: 104 additions & 0 deletions nop-core/src/main/java/io/nop/core/lang/eval/EvalRuntime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package io.nop.core.lang.eval;

import io.nop.api.core.util.IVariableScope;
import io.nop.api.core.util.SourceLocation;

public class EvalRuntime implements IVariableScope {
private final IEvalScope scope;
private ExitMode exitMode;
private EvalFrame currentFrame;
private IEvalOutput out = DisabledEvalOutput.INSTANCE;

public EvalRuntime(IEvalScope scope) {
this.scope = scope;
}

public EvalRuntime(IEvalScope scope, IEvalOutput out) {
this(scope);
this.out = out;
}

public EvalRuntime(IEvalScope scope, EvalFrame frame) {
this(scope);
this.currentFrame = frame;
}

public IEvalScope getScope() {
return scope;
}

public ExitMode getExitMode() {
return exitMode;
}

public void setExitMode(ExitMode exitMode) {
this.exitMode = exitMode;
}

public IEvalOutput getOut() {
return out;
}

public void setOut(IEvalOutput out) {
this.out = out;
}

public EvalFrame getCurrentFrame() {
return currentFrame;
}

public EvalFrame getFrame(int frameIndex) {
if (frameIndex <= 0)
return getCurrentFrame();

EvalFrame frame = getCurrentFrame();
for (int i = 0; i < frameIndex; i++) {
frame = frame.getParentFrame();
if (frame == null)
return null;
}
return frame;
}

public void pushFrame(EvalFrame frame) {
this.currentFrame = frame;
}

public void popFrame() {
if (currentFrame != null)
currentFrame = currentFrame.getParentFrame();
}

@Override
public Object getValueByPropPath(String propPath) {
return scope.getValueByPropPath(propPath);
}

public Object getValue(String name) {
return scope.getValue(name);
}

public boolean containsValue(String name) {
return scope.containsValue(name);
}

public Object getLocalValue(String name) {
return scope.getLocalValue(name);
}

public void setLocalValue(String name, Object value) {
scope.setLocalValue(name, value);
}

public void setLocalValue(SourceLocation loc, String name, Object value) {
scope.setLocalValue(loc, name, value);
}

public EvalRuntime getRuntimeForFrame(int frameIndex) {
EvalFrame frame = getFrame(frameIndex);
if (frame == null) {
frame = new EvalFrame(null, new String[0]);
}
return new EvalRuntime(scope, frame);
}
}
86 changes: 6 additions & 80 deletions nop-core/src/main/java/io/nop/core/lang/eval/EvalScopeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.nop.api.core.annotations.core.NoReflection;
import io.nop.api.core.ioc.BeanContainer;
import io.nop.api.core.ioc.IBeanProvider;
import io.nop.api.core.util.Guard;
import io.nop.api.core.util.IVariableScope;
import io.nop.api.core.util.SourceLocation;
import io.nop.commons.util.objects.ValueWithLocation;
Expand All @@ -31,32 +30,24 @@ public class EvalScopeImpl implements IEvalScope {
private final boolean inheritParentVars;
private IBeanProvider beanProvider;
private IClassModelLoader classModelLoader = ReflectionManager.instance();
private IEvalOutput output = DisabledEvalOutput.INSTANCE;
private IVariableScope extension;
private EvalFrame currentFrame;
private ExitMode exitMode;
private IExpressionExecutor executor = EvalExprProvider.getGlobalExecutor();

protected EvalScopeImpl(IEvalScope parentScope, Map<String, Object> variables, boolean inheritParentVars,
boolean inheritOutput) {
protected EvalScopeImpl(IEvalScope parentScope, Map<String, Object> variables, boolean inheritParentVars) {
this.parentScope = parentScope;
this.variables = variables == null ? new HashMap<>() : variables;
this.inheritParentVars = parentScope != null && inheritParentVars;
if (parentScope != null) {
this.setClassModelLoader(parentScope.getClassModelLoader());
this.setBeanProvider(parentScope.getBeanProvider());
this.setExpressionExecutor(parentScope.getExpressionExecutor());
if (inheritOutput)
this.output = parentScope.getOut();
}
}

public EvalScopeImpl() {
this(null, new HashMap<>(), false, false);
this(null, new HashMap<>(), false);
}

public EvalScopeImpl(Map<String, Object> variables) {
this(null, variables, false, false);
this(null, variables, false);
}

@Override
Expand Down Expand Up @@ -84,17 +75,6 @@ public void setClassModelLoader(IClassModelLoader classModelLoader) {
this.classModelLoader = classModelLoader;
}

@Override
public IEvalOutput getOut() {
return output;
}

@Override
public void setOut(IEvalOutput out) {
Guard.notNull(out, "output");
this.output = out;
}

@Override
public void setExtension(IVariableScope extension) {
this.extension = extension;
Expand All @@ -111,34 +91,16 @@ public boolean isInheritParentVars() {
}

@Override
public IEvalScope newChildScope(boolean inheritParentVars, boolean inheritParentOut, boolean threadSafe) {
public IEvalScope newChildScope(boolean inheritParentVars, boolean threadSafe) {
EvalScopeImpl scope = new EvalScopeImpl(this, threadSafe ? new ConcurrentHashMap<>() : new HashMap<>(),
inheritParentVars, inheritParentOut);
inheritParentVars);
return scope;
}

@Override
public IEvalScope newChildScope(Map<String, Object> childVars) {
EvalScopeImpl scope = new EvalScopeImpl(this, childVars,
true, true);
return scope;
}

@Override
public IEvalScope duplicate() {
EvalScopeImpl scope = new EvalScopeImpl(parentScope, variables, inheritParentVars, false);
scope.setOut(output);
scope.setExpressionExecutor(executor);
scope.setBeanProvider(beanProvider);
scope.setExtension(extension);
scope.setClassModelLoader(classModelLoader);

if (ENABLE_EVAL_DEBUG) {
if (locations == null) {
locations = new HashMap<>();
}
scope.locations = locations;
}
true);
return scope;
}

Expand Down Expand Up @@ -283,40 +245,4 @@ public void clear() {
}
}
}

@Override
public ExitMode getExitMode() {
return exitMode;
}

@Override
public void setExitMode(ExitMode exitMode) {
this.exitMode = exitMode;
}

@Override
public EvalFrame getCurrentFrame() {
return currentFrame;
}

@Override
public void pushFrame(EvalFrame frame) {
this.currentFrame = frame;
}

@Override
public void popFrame() {
if (currentFrame != null)
currentFrame = currentFrame.getParentFrame();
}

@Override
public IExpressionExecutor getExpressionExecutor() {
return executor;
}

@Override
public void setExpressionExecutor(IExpressionExecutor executor) {
this.executor = executor;
}
}
Loading

0 comments on commit fb048c1

Please sign in to comment.