Skip to content

Commit

Permalink
-Disable uninitialization of StrategoObserver after every foreign call.
Browse files Browse the repository at this point in the history
-Cache StrategoObservers per language across subsequent foreign calls.
  • Loading branch information
oskar-van-rest committed Aug 1, 2013
1 parent f70fda7 commit d7b6d6e
Showing 1 changed file with 29 additions and 14 deletions.
@@ -1,6 +1,8 @@
package org.strategoxt.imp.runtime.stratego;

import org.eclipse.core.resources.IProject;
import java.util.Map;
import java.util.WeakHashMap;

import org.eclipse.imp.language.Language;
import org.eclipse.imp.language.LanguageRegistry;
import org.spoofax.interpreter.core.IContext;
Expand All @@ -27,6 +29,12 @@ public ForeignLangCallPrimitive() {
super("SSL_EXT_foreigncall", 0, 2);
}

/**
* Cache for language-specific StrategoObserver. Avoids creation of an
* observer on every subsequent invocation for a language
*/
private static final Map<Descriptor, StrategoObserver> asyncCache = new WeakHashMap<Descriptor, StrategoObserver>();

/**
* Example usage:
*
Expand All @@ -49,24 +57,22 @@ public boolean call(final IContext env, Strategy[] svars, IStrategoTerm[] tvars)
final String strategyName = ((IStrategoString) tvars[1]).stringValue();
boolean result = false;
try {
final IStrategoTerm inputTerm = env.current();
final EditorIOAgent agent = (EditorIOAgent) SSLLibrary.instance(env).getIOAgent();
final IProject project = agent.getProject();
final String dir = ((EditorIOAgent) SSLLibrary.instance(env).getIOAgent())
.getProjectPath();

final Language oLang = LanguageRegistry.findLanguage(oLangName);
IStrategoTerm inputTerm = env.current();
Language oLang = LanguageRegistry.findLanguage(oLangName);
if (oLang == null)
return false;
final Descriptor oLangDescr = Environment.getDescriptor(oLang);
assert oLangDescr != null;
final StrategoObserver observer = oLangDescr
.createService(StrategoObserver.class, null);
observer.configureRuntime(project, dir);
Descriptor oLangDescr = Environment.getDescriptor(oLang);
StrategoObserver observer = asyncCache.get(oLangDescr);

if (observer == null) {
String dir = ((EditorIOAgent) SSLLibrary.instance(env).getIOAgent())
.getProjectPath();
observer = loadDescriptor(oLangDescr, dir);
}
assert observer != null;
observer.getRuntime().setCurrent(inputTerm);
result = observer.getRuntime().invoke(strategyName);
env.setCurrent(observer.getRuntime().current());
observer.uninitialize();
} catch (RuntimeException cex) {
Environment.logException(cex);
} catch (BadDescriptorException e) {
Expand All @@ -76,4 +82,13 @@ public boolean call(final IContext env, Strategy[] svars, IStrategoTerm[] tvars)
}
return result;
}

private StrategoObserver loadDescriptor(Descriptor oLangDescr, String path)
throws BadDescriptorException {
StrategoObserver observer = oLangDescr.createService(StrategoObserver.class, null);
observer.configureRuntime(null, path);
oLangDescr.createParseController();
asyncCache.put(oLangDescr, observer);
return observer;
}
}

0 comments on commit d7b6d6e

Please sign in to comment.