本文整理匯總了Java中org.mozilla.javascript.Context.evaluateString方法的典型用法代碼示例。如果您正苦於以下問題:Java Context.evaluateString方法的具體用法?Java Context.evaluateString怎麽用?Java Context.evaluateString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.mozilla.javascript.Context
的用法示例。
在下文中一共展示了Context.evaluateString方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: decryptSignature
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
private String decryptSignature(String encryptedSig, String decryptionCode) throws DecryptException {
Context context = Context.enter();
context.setOptimizationLevel(-1);
Object result;
try {
ScriptableObject scope = context.initStandardObjects();
context.evaluateString(scope, decryptionCode, "decryptionCode", 1, null);
Function decryptionFunc = (Function) scope.get("decrypt", scope);
result = decryptionFunc.call(context, scope, scope, new Object[]{encryptedSig});
} catch (Exception e) {
throw new DecryptException("could not get decrypt signature", e);
} finally {
Context.exit();
}
return result == null ? "" : result.toString();
}
示例2: start
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
public void start() {
try {
Context ctx = Context.enter();
scope = ctx.initStandardObjects();
scope.put("pacMethods", scope, new PacScriptMethods());
ctx.evaluateString(scope, getScriptContent(), "pac", 0, null);
for (PacScriptMethods.jsFunctions function : PacScriptMethods.jsFunctions.values()) {
ctx.evaluateString(scope,
"function " + function.name() + " () {" + "return pacMethods." + function.name() + ".apply(pacMethods, arguments); }",
function.name(), 0, null);
}
} catch (IOException e) {
Engine.logProxyManager.error("(PacManager) Failed to declare PacScriptMethods wrapper", e);
} finally {
Context.exit();
}
}
示例3: testSetNullForScriptableSetter
import org.mozilla.javascript.Context; //導入方法依賴的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();
}
}
示例4: runWithExpectedStackTrace
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
private void runWithExpectedStackTrace(final String _source, final String _expectedStackTrace)
{
final ContextAction action = new ContextAction() {
public Object run(Context cx) {
final Scriptable scope = cx.initStandardObjects();
try {
cx.evaluateString(scope, _source, "test.js", 0, null);
}
catch (final JavaScriptException e)
{
assertEquals(_expectedStackTrace, e.getScriptStackTrace());
return null;
}
throw new RuntimeException("Exception expected!");
}
};
Utils.runWithOptimizationLevel(action, -1);
}
示例5: helper
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
public void helper(String source) {
Context cx = Context.enter();
Context.ClassShutterSetter setter = cx.getClassShutterSetter();
try {
Scriptable globalScope = cx.initStandardObjects();
if (setter == null) {
setter = classShutterSetter;
} else {
classShutterSetter = setter;
}
setter.setClassShutter(new OpaqueShutter());
cx.evaluateString(globalScope, source, "test source", 1, null);
} finally {
setter.setClassShutter(null);
Context.exit();
}
}
示例6: testFunctionWithContinuations
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
public void testFunctionWithContinuations() {
Context cx = Context.enter();
try {
cx.setOptimizationLevel(-1); // must use interpreter mode
cx.evaluateString(globalScope,
"function f(a) { return myObject.f(a); }",
"function test source", 1, null);
Function f = (Function) globalScope.get("f", globalScope);
Object[] args = { 7 };
cx.callFunctionWithContinuations(f, globalScope, args);
fail("Should throw ContinuationPending");
} catch (ContinuationPending pending) {
Object applicationState = pending.getApplicationState();
assertEquals(7, ((Number)applicationState).intValue());
int saved = (Integer) applicationState;
Object result = cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1);
assertEquals(8, ((Number)result).intValue());
} finally {
Context.exit();
}
}
示例7: testWithTwoScopes
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
private void testWithTwoScopes(final String scriptScope1,
final String scriptScope2)
{
final ContextAction action = new ContextAction()
{
public Object run(final Context cx)
{
final Scriptable scope1 = cx.initStandardObjects(
new MySimpleScriptableObject("scope1"));
final Scriptable scope2 = cx.initStandardObjects(
new MySimpleScriptableObject("scope2"));
cx.evaluateString(scope2, scriptScope2, "source2", 1, null);
scope1.put("scope2", scope1, scope2);
return cx.evaluateString(scope1, scriptScope1, "source1", 1,
null);
}
};
Utils.runWithAllOptimizationLevels(action);
}
示例8: decipherKey
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
/**
* After finding the decrypted code in the js html5 player code
* run the code passing the encryptedSig parameter
*
* @param encryptedSig
* @param html5player
* @return
* @throws Exception
*/
private static String decipherKey(String encryptedSig, String html5player)
throws Exception {
String decipherFunc = loadFunction(html5player);
Context context = Context.enter();
// Rhino interpreted mode
context.setOptimizationLevel(-1);
Object result = null;
try {
ScriptableObject scope = context.initStandardObjects();
context.evaluateString(scope, decipherFunc, "decipherFunc", 1, null);
Function decryptionFunc = (Function) scope.get("decrypt", scope);
result = decryptionFunc.call(context, scope, scope, new Object[]{encryptedSig});
} catch (Exception e) {
e.printStackTrace();
} finally {
Context.exit();
}
if (result == null) {
return "";
} else {
return result.toString();
}
}
示例9: setUp
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
@Override
protected void setUp() {
// set up a reference map
reference = new ArrayList<Object>();
reference.add("a");
reference.add(Boolean.TRUE);
reference.add(new HashMap<Object, Object>());
reference.add(new Integer(42));
reference.add("a");
// get a js object as map
Context context = Context.enter();
ScriptableObject scope = context.initStandardObjects();
list = (List<Object>) context.evaluateString(scope,
"(['a', true, new java.util.HashMap(), 42, 'a']);",
"testsrc", 1, null);
Context.exit();
}
示例10: testIt
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
public void testIt()
{
final String script = "var fn = function() { return this; }\n"
+ "fn.apply(1)";
final ContextAction action = new ContextAction()
{
public Object run(final Context _cx)
{
final ScriptableObject scope = _cx.initStandardObjects();
final Object result = _cx.evaluateString(scope, script, "test script", 0, null);
assertEquals("object", ScriptRuntime.typeof(result));
assertEquals("1", Context.toString(result));
return null;
}
};
Utils.runWithAllOptimizationLevels(action);
}
示例11: test0
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
/**
* ECMA 11.4.3 says that typeof on host object is Implementation-dependent
*/
public void test0() throws Exception
{
final Function f = new BaseFunction()
{
@Override
public Object call(Context _cx, Scriptable _scope, Scriptable _thisObj,
Object[] _args)
{
return _args[0].getClass().getName();
}
};
final ContextAction action = new ContextAction()
{
public Object run(final Context context)
{
final Scriptable scope = context.initStandardObjects();
scope.put("myObj", scope, f);
return context.evaluateString(scope, "typeof myObj", "test script", 1, null);
}
};
doTest("function", action);
}
示例12: testAttributeName
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
@Test
public void testAttributeName() {
Context context = Context.enter();
Scriptable scriptable = context.initStandardObjects();
context.setOptimizationLevel(-1);
Object o = context.evaluateString(scriptable, "XML.ignoreProcessingInstructions = true; (<xml id=\"foo\"></xml>).attributes()[0].name()", "<e4x>", 1, null);
System.out.println(o);
}
示例13: parseRhino
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
public static <T> T parseRhino(File rhinoScript, ScopeOperation<T> operation) {
Context context = Context.enter();
try {
operation.initContext(context);
Scriptable scope = context.initStandardObjects();
String printFunction = "function print(message) {}";
context.evaluateString(scope, printFunction, "print", 1, null);
context.evaluateString(scope, readFile(rhinoScript, "UTF-8"), rhinoScript.getName(), 1, null);
return operation.action(scope, context);
} finally {
Context.exit();
}
}
示例14: assertEvaluates
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
private void assertEvaluates(final Object expected, final String source) {
final ContextAction action = new ContextAction() {
public Object run(Context cx) {
final Scriptable scope = cx.initStandardObjects();
final Object rep = cx.evaluateString(scope, source, "test.js",
0, null);
assertEquals(expected, rep);
return null;
}
};
Utils.runWithAllOptimizationLevels(action);
}
示例15: functionObjectPrimitiveToObject
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
/**
* Test that FunctionObject use the right top scope to convert a primitive
* to an object
*/
@Test
public void functionObjectPrimitiveToObject() throws Exception {
final String scriptScope2 = "function f() {\n"
+ "String.prototype.foo = 'from 2'; \n"
+ "var s2 = 's2';\n"
+ "var s2Foo = s2.foo;\n"
+ "var s2FooReadByFunction = myObject.readPropFoo(s2);\n"
+ "if (s2Foo != s2FooReadByFunction)\n"
+ "throw 's2 got: ' + s2FooReadByFunction;\n"
+ "}";
// define object with custom method
final MyObject myObject = new MyObject();
final String[] functionNames = { "readPropFoo" };
myObject.defineFunctionProperties(functionNames, MyObject.class,
ScriptableObject.EMPTY);
final String scriptScope1 = "String.prototype.foo = 'from 1'; scope2.f()";
final ContextAction action = new ContextAction()
{
public Object run(final Context cx)
{
final Scriptable scope1 = cx.initStandardObjects(
new MySimpleScriptableObject("scope1"));
final Scriptable scope2 = cx.initStandardObjects(
new MySimpleScriptableObject("scope2"));
scope2.put("myObject", scope2, myObject);
cx.evaluateString(scope2, scriptScope2, "source2", 1, null);
scope1.put("scope2", scope1, scope2);
return cx.evaluateString(scope1, scriptScope1, "source1", 1, null);
}
};
Utils.runWithAllOptimizationLevels(action);
}