本文整理匯總了Java中org.mozilla.javascript.Script類的典型用法代碼示例。如果您正苦於以下問題:Java Script類的具體用法?Java Script怎麽用?Java Script使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Script類屬於org.mozilla.javascript包,在下文中一共展示了Script類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: evalInlineScript
import org.mozilla.javascript.Script; //導入依賴的package包/類
static void evalInlineScript(Context cx, String scriptText) {
try {
Script script = cx.compileString(scriptText, "<command>", 1, null);
if (script != null) {
script.exec(cx, getShellScope());
}
} catch (RhinoException rex) {
ToolErrorReporter.reportException(
cx.getErrorReporter(), rex);
exitCode = EXITCODE_RUNTIME_ERROR;
} catch (VirtualMachineError ex) {
// Treat StackOverflow and OutOfMemory as runtime errors
ex.printStackTrace();
String msg = ToolErrorReporter.getMessage(
"msg.uncaughtJSException", ex.toString());
Context.reportError(msg);
exitCode = EXITCODE_RUNTIME_ERROR;
}
}
示例2: executeString
import org.mozilla.javascript.Script; //導入依賴的package包/類
/**
* @see org.alfresco.service.cmr.repository.ScriptProcessor#executeString(java.lang.String, java.util.Map)
*/
public Object executeString(String source, Map<String, Object> model)
{
try
{
// compile the script based on the node content
Script script;
Context cx = Context.enter();
try
{
script = cx.compileString(resolveScriptImports(source), "AlfrescoJS", 1, null);
}
finally
{
Context.exit();
}
return executeScriptImpl(script, model, true, "string script");
}
catch (Throwable err)
{
throw new ScriptException("Failed to execute supplied script: " + err.getMessage(), err);
}
}
示例3: run
import org.mozilla.javascript.Script; //導入依賴的package包/類
public Object run(Context cx)
{
if (modulePath != null || mainModule != null) {
require = global.installRequire(cx, modulePath, sandboxed);
}
if (type == PROCESS_FILES) {
processFiles(cx, args);
} else if (type == EVAL_INLINE_SCRIPT) {
Script script = loadScriptFromSource(cx, scriptText,
"<command>", 1, null);
if (script != null) {
evaluateScript(script, cx, getGlobal());
}
} else {
throw Kit.codeBug();
}
return null;
}
示例4: loadScriptFromSource
import org.mozilla.javascript.Script; //導入依賴的package包/類
public static Script loadScriptFromSource(Context cx, String scriptSource,
String path, int lineno,
Object securityDomain)
{
try {
return cx.compileString(scriptSource, path, lineno,
securityDomain);
} catch (EvaluatorException ee) {
// Already printed message.
exitCode = EXITCODE_RUNTIME_ERROR;
} catch (RhinoException rex) {
ToolErrorReporter.reportException(
cx.getErrorReporter(), rex);
exitCode = EXITCODE_RUNTIME_ERROR;
} catch (VirtualMachineError ex) {
// Treat StackOverflow and OutOfMemory as runtime errors
ex.printStackTrace();
String msg = ToolErrorReporter.getMessage(
"msg.uncaughtJSException", ex.toString());
exitCode = EXITCODE_RUNTIME_ERROR;
Context.reportError(msg);
}
return null;
}
示例5: evaluateScript
import org.mozilla.javascript.Script; //導入依賴的package包/類
public static Object evaluateScript(Script script, Context cx,
Scriptable scope)
{
try {
return script.exec(cx, scope);
} catch (RhinoException rex) {
ToolErrorReporter.reportException(
cx.getErrorReporter(), rex);
exitCode = EXITCODE_RUNTIME_ERROR;
} catch (VirtualMachineError ex) {
// Treat StackOverflow and OutOfMemory as runtime errors
ex.printStackTrace();
String msg = ToolErrorReporter.getMessage(
"msg.uncaughtJSException", ex.toString());
exitCode = EXITCODE_RUNTIME_ERROR;
Context.reportError(msg);
}
return Context.getUndefinedValue();
}
示例6: testJsApi
import org.mozilla.javascript.Script; //導入依賴的package包/類
public void testJsApi() throws Exception {
Context cx = Context.enter();
cx.setOptimizationLevel(-1);
Script script = cx.compileReader(new InputStreamReader(
Bug482203.class.getResourceAsStream("conttest.js")),
"", 1, null);
Scriptable scope = cx.initStandardObjects();
script.exec(cx, scope);
for(;;)
{
Object cont = ScriptableObject.getProperty(scope, "c");
if(cont == null)
{
break;
}
((Callable)cont).call(cx, scope, scope, new Object[] { null });
}
}
示例7: testJavaApi
import org.mozilla.javascript.Script; //導入依賴的package包/類
public void testJavaApi() throws Exception {
Context cx = Context.enter();
try {
cx.setOptimizationLevel(-1);
Script script = cx.compileReader(new InputStreamReader(
Bug482203.class.getResourceAsStream("conttest.js")),
"", 1, null);
Scriptable scope = cx.initStandardObjects();
cx.executeScriptWithContinuations(script, scope);
for(;;)
{
Object cont = ScriptableObject.getProperty(scope, "c");
if(cont == null)
{
break;
}
cx.resumeContinuation(cont, scope, null);
}
} finally {
Context.exit();
}
}
示例8: testScriptWithContinuations
import org.mozilla.javascript.Script; //導入依賴的package包/類
public void testScriptWithContinuations() {
Context cx = Context.enter();
try {
cx.setOptimizationLevel(-1); // must use interpreter mode
Script script = cx.compileString("myObject.f(3) + 1;",
"test source", 1, null);
cx.executeScriptWithContinuations(script, globalScope);
fail("Should throw ContinuationPending");
} catch (ContinuationPending pending) {
Object applicationState = pending.getApplicationState();
assertEquals(new Integer(3), applicationState);
int saved = (Integer) applicationState;
Object result = cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1);
assertEquals(5, ((Number)result).intValue());
} finally {
Context.exit();
}
}
示例9: testScriptWithNestedContinuations
import org.mozilla.javascript.Script; //導入依賴的package包/類
public void testScriptWithNestedContinuations() {
Context cx = Context.enter();
try {
cx.setOptimizationLevel(-1); // must use interpreter mode
Script script = cx.compileString("myObject.g( myObject.f(1) ) + 2;",
"test source", 1, null);
cx.executeScriptWithContinuations(script, globalScope);
fail("Should throw ContinuationPending");
} catch (ContinuationPending pending) {
try {
Object applicationState = pending.getApplicationState();
assertEquals(new Integer(1), applicationState);
int saved = (Integer) applicationState;
cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1);
fail("Should throw another ContinuationPending");
} catch (ContinuationPending pending2) {
Object applicationState2 = pending2.getApplicationState();
assertEquals(new Integer(4), applicationState2);
int saved2 = (Integer) applicationState2;
Object result2 = cx.resumeContinuation(pending2.getContinuation(), globalScope, saved2 + 2);
assertEquals(8, ((Number)result2).intValue());
}
} finally {
Context.exit();
}
}
示例10: testErrorOnEvalCall
import org.mozilla.javascript.Script; //導入依賴的package包/類
/**
* Since a continuation can only capture JavaScript frames and not Java
* frames, ensure that Rhino throws an exception when the JavaScript frames
* don't reach all the way to the code called by
* executeScriptWithContinuations or callFunctionWithContinuations.
*/
public void testErrorOnEvalCall() {
Context cx = Context.enter();
try {
cx.setOptimizationLevel(-1); // must use interpreter mode
Script script = cx.compileString("eval('myObject.f(3);');",
"test source", 1, null);
cx.executeScriptWithContinuations(script, globalScope);
fail("Should throw IllegalStateException");
} catch (WrappedException we) {
Throwable t = we.getWrappedException();
assertTrue(t instanceof IllegalStateException);
assertTrue(t.getMessage().startsWith("Cannot capture continuation"));
} finally {
Context.exit();
}
}
示例11: compileScript
import org.mozilla.javascript.Script; //導入依賴的package包/類
private Script compileScript() {
String scriptSource = "importPackage(java.util);\n"
+ "var searchmon = 3;\n"
+ "var searchday = 10;\n"
+ "var searchyear = 2008;\n"
+ "var searchwkday = 0;\n"
+ "\n"
+ "var myDate = Calendar.getInstance();\n // this is a java.util.Calendar"
+ "myDate.set(Calendar.MONTH, searchmon);\n"
+ "myDate.set(Calendar.DATE, searchday);\n"
+ "myDate.set(Calendar.YEAR, searchyear);\n"
+ "searchwkday.value = myDate.get(Calendar.DAY_OF_WEEK);";
Script script;
Context context = factory.enterContext();
try {
script = context.compileString(scriptSource, "testScript", 1, null);
return script;
} finally {
Context.exit();
}
}
示例12: testJsApi
import org.mozilla.javascript.Script; //導入依賴的package包/類
public void testJsApi() throws Exception {
Context cx = RhinoAndroidHelper.prepareContext();
try {
cx.setOptimizationLevel(-1);
Script script = cx.compileString(TestUtils.readAsset("Bug482203.js"),
"", 1, null);
Scriptable scope = cx.initStandardObjects();
script.exec(cx, scope);
int counter = 0;
for(;;)
{
Object cont = ScriptableObject.getProperty(scope, "c");
if(cont == null)
{
break;
}
counter++;
((Callable)cont).call(cx, scope, scope, new Object[] { null });
}
assertEquals(counter, 5);
assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result"));
} finally {
Context.exit();
}
}
示例13: testJavaApi
import org.mozilla.javascript.Script; //導入依賴的package包/類
public void testJavaApi() throws Exception {
Context cx = RhinoAndroidHelper.prepareContext();
try {
cx.setOptimizationLevel(-1);
Script script = cx.compileString(TestUtils.readAsset("Bug482203.js"),
"", 1, null);
Scriptable scope = cx.initStandardObjects();
cx.executeScriptWithContinuations(script, scope);
int counter = 0;
for(;;)
{
Object cont = ScriptableObject.getProperty(scope, "c");
if(cont == null)
{
break;
}
counter++;
cx.resumeContinuation(cont, scope, null);
}
assertEquals(counter, 5);
assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result"));
} finally {
Context.exit();
}
}
示例14: test
import org.mozilla.javascript.Script; //導入依賴的package包/類
@Test
public void test() {
String source = "var state = '';";
source += "function A(){state += 'A'}";
source += "function B(){state += 'B'}";
source += "function C(){state += 'C'}";
source += "try { A(); continuation(); B() } finally { C() }";
source += "state";
String[] functions = new String[] { "continuation" };
scope.defineFunctionProperties(functions, Bug685403Test.class,
ScriptableObject.DONTENUM);
Object state = null;
Script script = cx.compileString(source, "", 1, null);
try {
cx.executeScriptWithContinuations(script, scope);
fail("expected ContinuationPending exception");
} catch (ContinuationPending pending) {
state = cx.resumeContinuation(pending.getContinuation(), scope, "");
}
assertEquals("ABC", state);
}
示例15: evaluateExpression
import org.mozilla.javascript.Script; //導入依賴的package包/類
public Object evaluateExpression(Script expression)
{
ensureContext();
Object value = expression.exec(context, scope);
Object javaValue;
// not converting Number objects because the generic conversion call below
// always converts to Double
if (value == null || value instanceof Number)
{
javaValue = value;
}
else
{
try
{
javaValue = Context.jsToJava(value, Object.class);
}
catch (EvaluatorException e)
{
throw new JRRuntimeException(e);
}
}
return javaValue;
}