本文整理匯總了Java中org.mozilla.javascript.Context.compileString方法的典型用法代碼示例。如果您正苦於以下問題:Java Context.compileString方法的具體用法?Java Context.compileString怎麽用?Java Context.compileString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.mozilla.javascript.Context
的用法示例。
在下文中一共展示了Context.compileString方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: evalInlineScript
import org.mozilla.javascript.Context; //導入方法依賴的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.Context; //導入方法依賴的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: setFilter
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
public void setFilter(String filter) throws ServiceException {
if (filter == null) {
filter = "";
}
if (!this.filter.equals(filter)) {
this.filter = filter;
if (filter.length() != 0) {
Context js_context = Context.enter();
try {
js_and.reset(filter);
js_or.reset(js_and.replaceAll(" && "));
filter = js_or.replaceAll(" || ");
js_filter = js_context.compileString(filter, "filter", 0, null);
} catch (EvaluatorException e) {
throw new ServiceException("Failed to compile JS filter : " + e.getMessage(), e);
} finally {
Context.exit();
}
} else {
js_filter = null;
}
bContinue = false;
}
}
示例4: loadScriptFromSource
import org.mozilla.javascript.Context; //導入方法依賴的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: testScriptWithContinuations
import org.mozilla.javascript.Context; //導入方法依賴的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();
}
}
示例6: testScriptWithNestedContinuations
import org.mozilla.javascript.Context; //導入方法依賴的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();
}
}
示例7: testErrorOnEvalCall
import org.mozilla.javascript.Context; //導入方法依賴的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();
}
}
示例8: compileScript
import org.mozilla.javascript.Context; //導入方法依賴的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();
}
}
示例9: processFileSecure
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
static void processFileSecure(Context cx, Scriptable scope,
String path, Object securityDomain)
throws IOException {
boolean isClass = path.endsWith(".class");
Object source = readFileOrUrl(path, !isClass);
byte[] digest = getDigest(source);
String key = path + "_" + cx.getOptimizationLevel();
ScriptReference ref = scriptCache.get(key, digest);
Script script = ref != null ? ref.get() : null;
if (script == null) {
if (isClass) {
script = loadCompiledScript(cx, path, (byte[]) source, securityDomain);
} else {
String strSrc = (String) source;
// Support the executable script #! syntax: If
// the first line begins with a '#', treat the whole
// line as a comment.
if (strSrc.length() > 0 && strSrc.charAt(0) == '#') {
for (int i = 1; i != strSrc.length(); ++i) {
int c = strSrc.charAt(i);
if (c == '\n' || c == '\r') {
strSrc = strSrc.substring(i);
break;
}
}
}
script = cx.compileString(strSrc, path, 1, securityDomain);
}
scriptCache.put(key, digest, script);
}
if (script != null) {
script.exec(cx, scope);
}
}
示例10: execute
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
/**
* @see org.alfresco.service.cmr.repository.ScriptProcessor#execute(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.util.Map)
*/
public Object execute(NodeRef nodeRef, QName contentProp, Map<String, Object> model)
{
try
{
if (this.services.getNodeService().exists(nodeRef) == false)
{
throw new AlfrescoRuntimeException("Script Node does not exist: " + nodeRef);
}
if (contentProp == null)
{
contentProp = ContentModel.PROP_CONTENT;
}
ContentReader cr = this.services.getContentService().getReader(nodeRef, contentProp);
if (cr == null || cr.exists() == false)
{
throw new AlfrescoRuntimeException("Script Node content not found: " + nodeRef);
}
// compile the script based on the node content
Script script;
Context cx = Context.enter();
try
{
script = cx.compileString(resolveScriptImports(cr.getContentString()), nodeRef.toString(), 1, null);
}
finally
{
Context.exit();
}
return executeScriptImpl(script, model, false, nodeRef.toString());
}
catch (Throwable err)
{
throw new ScriptException("Failed to execute script '" + nodeRef.toString() + "': " + err.getMessage(), err);
}
}
示例11: testScriptWithMultipleContinuations
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
public void testScriptWithMultipleContinuations() {
Context cx = Context.enter();
try {
cx.setOptimizationLevel(-1); // must use interpreter mode
Script script = cx.compileString(
"myObject.f(3) + myObject.g(3) + 2;",
"test source", 1, null);
cx.executeScriptWithContinuations(script, globalScope);
fail("Should throw ContinuationPending");
} catch (ContinuationPending pending) {
try {
Object applicationState = pending.getApplicationState();
assertEquals(new Integer(3), 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(6), applicationState2);
int saved2 = (Integer) applicationState2;
Object result2 = cx.resumeContinuation(pending2.getContinuation(), globalScope, saved2 + 1);
assertEquals(13, ((Number)result2).intValue());
}
} finally {
Context.exit();
}
}
示例12: doTest
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
public void doTest(final String scriptCode) throws Exception {
final Context cx = ContextFactory.getGlobal().enterContext();
try {
Scriptable topScope = cx.initStandardObjects();
topScope.put("javaNameGetter", topScope, new JavaNameGetter());
Script script = cx.compileString(scriptCode, "myScript", 1, null);
script.exec(cx, topScope);
} finally {
Context.exit();
}
}
示例13: newObject0Arg
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
/**
* As of head of trunk on 30.09.09, decompile of "new Date()" returns "new Date" without parentheses.
* @see <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=519692">Bug 519692</a>
*/
@Test
public void newObject0Arg()
{
final String source = "var x = new Date().getTime();";
final ContextAction action = new ContextAction() {
public Object run(final Context cx) {
final Script script = cx.compileString(source, "my script", 0, null);
Assert.assertEquals(source, cx.decompileScript(script, 4).trim());
return null;
}
};
Utils.runWithAllOptimizationLevels(action);
}