本文整理汇总了Java中org.mozilla.javascript.ContextFactory类的典型用法代码示例。如果您正苦于以下问题:Java ContextFactory类的具体用法?Java ContextFactory怎么用?Java ContextFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ContextFactory类属于org.mozilla.javascript包,在下文中一共展示了ContextFactory类的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: callOverloadedFunction
import org.mozilla.javascript.ContextFactory; //导入依赖的package包/类
@Test
public void callOverloadedFunction() {
new ContextFactory().call(new ContextAction() {
public Object run(Context cx) {
cx.evaluateString(
cx.initStandardObjects(),
"new org.mozilla.javascript.tests.Bug496585().method('one', 'two', 'three')",
"<test>", 1, null);
cx.evaluateString(
cx.initStandardObjects(),
"new org.mozilla.javascript.tests.Bug496585().method('one', function() {})",
"<test>", 1, null);
return null;
}
});
}
示例6: init
import org.mozilla.javascript.ContextFactory; //导入依赖的package包/类
public void init() {
//this can be initiated only once
if (mScriptContextFactory == null) {
mScriptContextFactory = new ScriptContextFactory();
ContextFactory.initGlobal(mScriptContextFactory);
}
mScriptContextFactory.setInterpreter(this);
rhino = Context.enter();
// observingDebugger = new ObservingDebugger();
// rhino.setDebugger(observingDebugger, new Integer(0));
// rhino.setGeneratingDebug(true);
// give some android love
rhino.setOptimizationLevel(-1);
scope = rhino.initStandardObjects();
//let rhino do some java <-> js transformations for us
rhino.getWrapFactory().setJavaPrimitiveWrap(false);
}
示例7: enter
import org.mozilla.javascript.ContextFactory; //导入依赖的package包/类
protected static Context enter(Context context)
{
Context currentContext = Context.getCurrentContext();
if (context != null && context == currentContext)
{
// already the current context
return currentContext;
}
// exit the current context if any
if (currentContext != null)
{
Context.exit();
}
Context newContext = ContextFactory.getGlobal().enterContext(context);
if (log.isDebugEnabled())
{
log.debug("entered context " + newContext + ", requested " + context);
}
return newContext;
}
示例8: evaluate
import org.mozilla.javascript.ContextFactory; //导入依赖的package包/类
public Object evaluate(final String expression, final Map<String, ?> values) throws ExpressionEvaluationException {
LOG.debug("Evaluating JavaScript expression: {1}", expression);
try {
final Context ctx = ContextFactory.getGlobal().enterContext();
Script script = scriptCache.get(expression);
if (script == null) {
ctx.setOptimizationLevel(9);
script = ctx.compileString(expression, "<cmd>", 1, null);
scriptCache.put(expression, script);
}
final Scriptable scope = ctx.newObject(parentScope);
scope.setPrototype(parentScope);
scope.setParentScope(null);
for (final Entry<String, ?> entry : values.entrySet()) {
scope.put(entry.getKey(), scope, Context.javaToJS(entry.getValue(), scope));
}
return script.exec(ctx, scope);
} catch (final EvaluatorException ex) {
throw new ExpressionEvaluationException("Evaluating JavaScript expression failed: " + expression, ex);
} finally {
Context.exit();
}
}
示例9: callOverloadedFunction
import org.mozilla.javascript.ContextFactory; //导入依赖的package包/类
@Test
public void callOverloadedFunction() {
new ContextFactory().call(new ContextAction() {
public Object run(Context cx) {
cx.getWrapFactory().setJavaPrimitiveWrap(false);
Assert.assertEquals("string[]", cx.evaluateString(
cx.initStandardObjects(),
"new org.mozilla.javascript.tests.Bug496585Test().method('one', 'two', 'three')",
"<test>", 1, null));
Assert.assertEquals("string+function", cx.evaluateString(
cx.initStandardObjects(),
"new org.mozilla.javascript.tests.Bug496585Test().method('one', function() {})",
"<test>", 1, null));
return null;
}
});
}
示例10: 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();
}
}
示例11: 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();
}
}
示例12: getImplementationVersion
import org.mozilla.javascript.ContextFactory; //导入依赖的package包/类
/**
* Rhino implementation version.
*
* @return Rhino implementation version
*/
private static String getImplementationVersion()
{
Context context = new ContextFactory().enterContext();
try
{
String version = context.getImplementationVersion();
if( ( version != null ) && version.startsWith( "Rhino " ) )
version = version.substring( 6 );
return version;
}
finally
{
Context.exit();
}
}
示例13: compare
import org.mozilla.javascript.ContextFactory; //导入依赖的package包/类
/**
* @param maxTests the maximum number of tests to generate
* @return the set of differences between the old and new programs, given as
* pairs (x, y), where x is the {@link Output} from the original
* program and y is the new program's output on {@code x.input}
* @throws IOException
*/
public Set<Pair<Output, Object>> compare(FileLoader loader, int maxTests)
throws IOException {
Cvc3Context cvc3Context = Cvc3Context.create(arguments.length, loader);
NewInputGenerator inputGenerator = new ApiBasedInputGenerator(cvc3Context);
SymbolicExecutor executor = SymbolicExecutor.create(
originalProgram, loader, inputGenerator, maxTests, entryFunction,
arguments);
Context rhinoContext = Context.enter();
Set<Pair<Output, Object>> differences = Sets.newHashSet();
// Run symbolic execution, and iterate through all generated inputs
for (Output output : executor.run()) {
// Run the new program with the old arguments
ScriptableObject scope = rhinoContext.initStandardObjects();
rhinoContext.evaluateString(scope, loader.toString(newProgram),
"new program", 0, null);
Object result = ScriptableObject.callMethod(scope, entryFunction,
output.input.toArray(ContextFactory.getGlobal()));
// If the results differ, add them to the set of differences
if (!output.result.equals(result)) {
differences.add(new Pair<Output, Object>(output, result));
}
}
return differences;
}
示例14: testfromJS
import org.mozilla.javascript.ContextFactory; //导入依赖的package包/类
public void testfromJS() {
Object symbolicOp = ContextFactory.getGlobal().call(new ContextEval(
"({ getSymbol: function() {"
+ " return 'f';"
+ "},"
+ "getArg: function(n) {"
+ " return n===0 ? -11 : n===1 ? (1,2) : 44;"
+ "},"
+ "arity: function() {"
+ "return [0,0,0,0].length;" // This makes Rhino return a Double(4.),
// which is what SymbolicOperationAdapter really sees.
+ "}"
+ "})"));
assertEquals("(f -11.0 2.0 44.0 44.0)",
SymbolicOperationAdapter.ADAPTER.fromJS(symbolicOp,
AdapterList.DEFAULT_ADAPTERS).toString());
}
示例15: 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");
_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();
}
}