Skip to content

Commit

Permalink
Add ScriptCompiler method
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Mar 14, 2013
1 parent 18eaf51 commit 5e51127
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
Expand Up @@ -39,6 +39,7 @@ private void addGoalToExecution(Goal goal) {
goal.run(selector);
}

@Override
public void addPrioritisableGoal(final PrioritisableGoal goal) {
Preconditions.checkNotNull(goal, "goal cannot be null");
possibleGoals.add(new GoalEntry() {
Expand Down Expand Up @@ -167,8 +168,9 @@ private void trySelectGoal() {
GoalEntry entry = possibleGoals.get(i);
if (searchPriority > entry.getPriority())
return;
if (entry.getGoal() == executingRootGoal || !entry.getGoal().shouldExecute(selector))
if (entry.getGoal() == executingRootGoal || !entry.getGoal().shouldExecute(selector)) {
continue;
}
if (i == 0) {
setupExecution(entry);
return;
Expand Down
77 changes: 50 additions & 27 deletions src/main/java/net/citizensnpcs/api/scripting/ScriptCompiler.java
Expand Up @@ -9,6 +9,10 @@
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

import javax.script.Compilable;
import javax.script.CompiledScript;
Expand Down Expand Up @@ -132,34 +136,11 @@ public void run() {
CompileTask task;
try {
task = toCompile.take();
task.future.get();
} catch (InterruptedException e) {
return;
}
for (FileEngine engine : task.files) {
Compilable compiler = (Compilable) engine.engine;
Reader reader = null;
try {
reader = new FileReader(engine.file);
CompiledScript src = compiler.compile(reader);
ScriptFactory compiled = new SimpleScriptFactory(src, task.contextProviders);
for (CompileCallback callback : task.callbacks) {
callback.onScriptCompiled(engine.file, compiled);
}
} catch (IOException e) {
Messaging.severe("IO error while reading " + engine.file + " for scripting.");
e.printStackTrace();
} catch (ScriptException e) {
Messaging.severe("Compile error while parsing script at " + engine.file.getName() + ".");
Throwables.getRootCause(e).printStackTrace();
} catch (Throwable t) {
Messaging.severe("[Unexpected error while parsing script at " + engine.file.getName() + ".");
t.printStackTrace();
} finally {
Closeables.closeQuietly(reader);
}
}
for (CompileCallback callback : task.callbacks) {
callback.onCompileTaskFinished();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
Expand All @@ -178,17 +159,53 @@ public void run(String code, String extension, Map<String, Object> vars) throws
engine.eval(extension, context);
}

private class CompileTask {
private class CompileTask implements Callable<ScriptFactory[]> {
private final CompileCallback[] callbacks;
private final ContextProvider[] contextProviders;
private final FileEngine[] files;
private final Future<ScriptFactory[]> future;

public CompileTask(CompileTaskBuilder builder) {
List<ContextProvider> copy = Lists.newArrayList(builder.contextProviders);
copy.addAll(globalContextProviders);
this.contextProviders = copy.toArray(new ContextProvider[copy.size()]);
this.files = builder.files;
this.callbacks = builder.callbacks.toArray(new CompileCallback[builder.callbacks.size()]);
this.future = new FutureTask<ScriptFactory[]>(this);
}

@Override
public ScriptFactory[] call() throws Exception {
ScriptFactory[] compiledFactories = new ScriptFactory[files.length];
for (int i = 0; i < files.length; i++) {
FileEngine engine = files[i];
Compilable compiler = (Compilable) engine.engine;
Reader reader = null;
try {
reader = new FileReader(engine.file);
CompiledScript src = compiler.compile(reader);
ScriptFactory compiled = new SimpleScriptFactory(src, contextProviders);
for (CompileCallback callback : callbacks) {
callback.onScriptCompiled(engine.file, compiled);
}
compiledFactories[i] = compiled;
} catch (IOException e) {
Messaging.severe("IO error while reading " + engine.file + " for scripting.");
e.printStackTrace();
} catch (ScriptException e) {
Messaging.severe("Compile error while parsing script at " + engine.file.getName() + ".");
Throwables.getRootCause(e).printStackTrace();
} catch (Throwable t) {
Messaging.severe("Unexpected error while parsing script at " + engine.file.getName() + ".");
t.printStackTrace();
} finally {
Closeables.closeQuietly(reader);
}
}
for (CompileCallback callback : callbacks) {
callback.onCompileTaskFinished();
}
return compiledFactories;
}
}

Expand All @@ -205,6 +222,12 @@ public boolean begin() {
return toCompile.offer(new CompileTask(this));
}

public Future<ScriptFactory[]> beginWithFuture() {
CompileTask t = new CompileTask(this);
toCompile.offer(t);
return t.future;
}

public CompileTaskBuilder withCallback(CompileCallback callback) {
callbacks.add(callback);
return this;
Expand Down

0 comments on commit 5e51127

Please sign in to comment.