本文整理汇总了Java中org.mozilla.javascript.ContextFactory.enterContext方法的典型用法代码示例。如果您正苦于以下问题:Java ContextFactory.enterContext方法的具体用法?Java ContextFactory.enterContext怎么用?Java ContextFactory.enterContext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.mozilla.javascript.ContextFactory
的用法示例。
在下文中一共展示了ContextFactory.enterContext方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: runJsTests
import org.mozilla.javascript.ContextFactory; //导入方法依赖的package包/类
public void runJsTests(File[] tests) throws IOException {
ContextFactory factory = ContextFactory.getGlobal();
Context cx = factory.enterContext();
try {
cx.setOptimizationLevel(this.optimizationLevel);
Scriptable shared = cx.initStandardObjects();
for (File f : tests) {
int length = (int) f.length(); // don't worry about very long
// files
char[] buf = new char[length];
new FileReader(f).read(buf, 0, length);
String session = new String(buf);
runJsTest(cx, shared, f.getName(), session);
}
} finally {
Context.exit();
}
}
示例2: testCustomContextFactory
import org.mozilla.javascript.ContextFactory; //导入方法依赖的package包/类
public void testCustomContextFactory() {
ContextFactory factory = new MyFactory();
Context cx = factory.enterContext();
try {
Scriptable globalScope = cx.initStandardObjects();
// Test that FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME is enabled
/* TODO(stevey): fix this functionality in parser
Object result = cx.evaluateString(globalScope,
"var obj = {};" +
"function obj.foo() { return 'bar'; }" +
"obj.foo();",
"test source", 1, null);
assertEquals("bar", result);
*/
} catch (RhinoException e) {
fail(e.toString());
} finally {
Context.exit();
}
}
示例3: runDoctest
import org.mozilla.javascript.ContextFactory; //导入方法依赖的package包/类
@Test
public void runDoctest() throws Exception {
ContextFactory factory = ContextFactory.getGlobal();
Context cx = factory.enterContext();
try {
cx.setOptimizationLevel(optimizationLevel);
Global global = new Global(cx);
// global.runDoctest throws an exception on any failure
int testsPassed = global.runDoctest(cx, global, source, name, 1);
System.out.println(name + "(" + optimizationLevel + "): " +
testsPassed + " passed.");
assertTrue(testsPassed > 0);
} catch (Exception ex) {
System.out.println(name + "(" + optimizationLevel + "): FAILED due to "+ex);
throw ex;
} finally {
Context.exit();
}
}
示例4: testSetNullForScriptableSetter
import org.mozilla.javascript.ContextFactory; //导入方法依赖的package包/类
public void testSetNullForScriptableSetter() throws Exception {
final String scriptCode = "foo.myProp = new Foo2();\n"
+ "foo.myProp = null;";
final ContextFactory factory = new ContextFactory();
final Context cx = factory.enterContext();
try {
final ScriptableObject topScope = cx.initStandardObjects();
final Foo foo = new Foo();
// define custom setter method
final Method setMyPropMethod = Foo.class.getMethod("setMyProp", Foo2.class);
foo.defineProperty("myProp", null, null, setMyPropMethod, ScriptableObject.EMPTY);
topScope.put("foo", topScope, foo);
ScriptableObject.defineClass(topScope, Foo2.class);
cx.evaluateString(topScope, scriptCode, "myScript", 1, null);
}
finally {
Context.exit();
}
}
示例5: runDoctest
import org.mozilla.javascript.ContextFactory; //导入方法依赖的package包/类
@Test
public void runDoctest() throws Exception {
ContextFactory factory = ContextFactory.getGlobal();
Context cx = factory.enterContext();
try {
cx.setOptimizationLevel(optimizationLevel);
Global global = new Global(cx);
// global.runDoctest throws an exception on any failure
int testsPassed = global.runDoctest(cx, global, source, name, 1);
System.out.println(name + "(" + optimizationLevel + "): " +
testsPassed + " passed.");
assertTrue(testsPassed > 0);
} catch (Exception ex) {
System.out.println(name + "(" + optimizationLevel + "): FAILED due to "+ex);
throw ex;
} finally {
Context.exit();
}
}
示例6: testSetNullForScriptableSetter
import org.mozilla.javascript.ContextFactory; //导入方法依赖的package包/类
public void testSetNullForScriptableSetter() throws Exception {
final String scriptCode = "foo.myProp = new Foo2();\n"
+ "foo.myProp = null;";
final ContextFactory factory = new ContextFactory();
final Context cx = factory.enterContext();
try {
final ScriptableObject topScope = cx.initStandardObjects();
final Foo foo = new Foo();
// define custom setter method
final Method setMyPropMethod = Foo.class.getMethod("setMyProp", Foo2.class);
foo.defineProperty("myProp", null, null, setMyPropMethod, ScriptableObject.EMPTY);
topScope.put("foo", topScope, foo);
ScriptableObject.defineClass(topScope, Foo2.class);
cx.evaluateString(topScope, scriptCode, "myScript", 1, null);
}
finally {
Context.exit();
}
}
示例7: init
import org.mozilla.javascript.ContextFactory; //导入方法依赖的package包/类
@Initialize
public void init() {
_contextFactory = new ContextFactory();
Context context = _contextFactory.enterContext();
try {
_script = context.compileString(sourceCode, this.getClass().getSimpleName(), 1, null);
_sharedScope = context.initStandardObjects();
JavaScriptUtils.addToScope(_sharedScope, logger, "logger", "log");
JavaScriptUtils.addToScope(_sharedScope, System.out, "out");
_script.exec(context, _sharedScope);
_transformerObj = (NativeObject) _sharedScope.get("transformerObj");
if (_transformerObj == null) {
throw new IllegalStateException("Required JS object 'transformerObj' not found!");
}
_initializeFunction = (Function) _transformerObj.get("initialize");
_transformFunction = (Function) _transformerObj.get("transform");
_closeFunction = (Function) _transformerObj.get("close");
_initializeFunction.call(context, _sharedScope, _sharedScope, new Object[0]);
} finally {
Context.exit();
}
}
示例8: init
import org.mozilla.javascript.ContextFactory; //导入方法依赖的package包/类
@Initialize
public void init() {
_contextFactory = new ContextFactory();
Context context = _contextFactory.enterContext();
try {
_script = context.compileString(sourceCode, this.getClass()
.getSimpleName(), 1, null);
_sharedScope = context.initStandardObjects();
JavaScriptUtils.addToScope(_sharedScope, logger, "logger", "log");
JavaScriptUtils.addToScope(_sharedScope, System.out, "out");
} finally {
Context.exit();
}
}
示例9: runWithOptimizationLevel
import org.mozilla.javascript.ContextFactory; //导入方法依赖的package包/类
/**
* Runs the provided action at the given optimization level
*/
public static void runWithOptimizationLevel(final ContextFactory contextFactory, final ContextAction action, final int optimizationLevel)
{
final Context cx = contextFactory.enterContext();
try
{
cx.setOptimizationLevel(optimizationLevel);
action.run(cx);
}
finally
{
Context.exit();
}
}
示例10: getContext
import org.mozilla.javascript.ContextFactory; //导入方法依赖的package包/类
public Context getContext()
{
if (cx == null) {
ContextFactory contextFactory = new ContextFactory();
cx = contextFactory.enterContext();
initFuncs();
}
return cx;
}
示例11: RhinoScriptHost
import org.mozilla.javascript.ContextFactory; //导入方法依赖的package包/类
/**
* Create a script host
* @param optimizationLevel Rhino optimization level (Valid range is -1 between 9, must be -1 on Android)
*/
public RhinoScriptHost(int optimizationLevel) {
cxf = new ContextFactory();
cx = cxf.enterContext();
cx.setOptimizationLevel(optimizationLevel);
scope = cx.initStandardObjects();
}
示例12: init
import org.mozilla.javascript.ContextFactory; //导入方法依赖的package包/类
@Initialize
public void init() {
_contextFactory = new ContextFactory();
final Context context = _contextFactory.enterContext();
try {
_script = context.compileString(sourceCode, this.getClass().getSimpleName(), 1, null);
_sharedScope = context.initStandardObjects();
JavaScriptUtils.addToScope(_sharedScope, new JavaScriptLogger(), "logger", "log");
JavaScriptUtils.addToScope(_sharedScope, System.out, "out");
} finally {
Context.exit();
}
}
示例13: init
import org.mozilla.javascript.ContextFactory; //导入方法依赖的package包/类
@Initialize
public void init() {
_contextFactory = new ContextFactory();
Context context = _contextFactory.enterContext();
try {
_script = context.compileString(sourceCode, this.getClass().getSimpleName(), 1, null);
_sharedScope = context.initStandardObjects();
JavaScriptUtils.addToScope(_sharedScope, logger, "logger", "log");
JavaScriptUtils.addToScope(_sharedScope, System.out, "out");
} finally {
Context.exit();
}
}
示例14: debug
import org.mozilla.javascript.ContextFactory; //导入方法依赖的package包/类
@Test
public void debug() {
// ContextFactory contextFactory = getDebugContextFactory();
ContextFactory contextFactory = getNormalContextFactory();
Scriptable scope; // this is THE OBJECT
Context cx;
cx = contextFactory.enterContext();
try {
scope = cx.initStandardObjects();
} finally {
Context.exit();
}
displayDebugger2(contextFactory, scope);
cx = contextFactory.enterContext();
try {
Object result = cx.evaluateString(scope, "2 + 4 + 5", "<cmd>", 1, null);
result = cx.evaluateString(scope, "2 + 55 + 5", "<cmd2>", 1, null);
result = cx.evaluateString(scope, "1/3", "<cmd3>", 1, null);
result = cx.evaluateString(scope, "32323.333+3232", "<cmd4>", 1, null);
result = cx.evaluateString(scope, "'cocoloco'", "<cmd5>", 1, null);
System.out.println(Context.toString(result));
} finally {
Context.exit();
}
}
示例15: readScripts
import org.mozilla.javascript.ContextFactory; //导入方法依赖的package包/类
private void readScripts(List<User> userList) {
ContextFactory contextFactory = ContextFactory.getGlobal();
Context context = contextFactory.enterContext();
try {
for (User user : userList) {
Company company = new Company(this, user.getUsername(), grocer);
ThreadLocal.setCompany(company);
ScriptableObject prototype = context.initStandardObjects();
prototype.setParentScope(null);
Scriptable scope = context.newObject(prototype);
scope.setPrototype(prototype);
Object jsCompany = new SandboxNativeJavaObject(scope, company, Company.class);
prototype.put("company", scope, jsCompany);
Object jsSystemout = new SandboxNativeJavaObject(scope,
new DebugAdapter(this, gameRun.getResult(), company.getName()), DebugAdapter.class);
prototype.put("out", scope, jsSystemout);
prototype.put("console", scope, jsSystemout);
try {
long time = System.nanoTime();
context.evaluateString(scope, user.getMainJavaScript(), company.getName(), 1, null);
getResult().getCreateNotExists(company.getName()).addRunTime("init", System.nanoTime() - time);
getResult().getCreateNotExists(company.getName()).setCode(user.getMainJavaScript());
} catch (RhinoException e) {
if (e.getCause() instanceof GameException) {
log.info("Failed to initialize the JavaScript, but found a GameException", e);
} else {
String formattedStackTrace = ExceptionConverter.convertToString(e);
gameRun.getResult().addError(formattedStackTrace);
log.error("Failed to initialize the JavaScript. Player " + company.getName() + " bankrupt", e);
company.setBankruptFromError(formattedStackTrace);
}
}
companies.add(company);
}
ThreadLocal.resetCompany();
} finally {
Context.exit();
}
}