protected void executeSimpleHandlerCore(String handlerType, org.mozilla.javascript.Context myJavascriptContext) throws EcmaError, EvaluatorException, JavaScriptException, EngineException { handlerName = "on" + handlerType; Engine.logBeans.trace("(Transaction) Searching the " + handlerType + " handler (" + handlerName + ")"); Object object = scope.get(handlerName, scope); Engine.logBeans.trace("(Transaction) Rhino returned: [" + object.getClass().getName() + "] " + object.toString()); if (!(object instanceof Function)) { Engine.logBeans.debug("(Transaction) No " + handlerType + " handler (" + handlerName + ") found"); return; } else { Engine.logBeans.debug("(Transaction) Execution of the " + handlerType + " handler (" + handlerName + ") for the transaction '" + getName() + "'"); } function = (Function) object; Object returnedValue = function.call(myJavascriptContext, scope, scope, null); if (returnedValue instanceof org.mozilla.javascript.Undefined) { handlerResult = ""; } else { handlerResult = returnedValue.toString(); } }
/** * load * @param response */ public void load( WebResponse response ) { Function onLoadEvent=null; try { Context context = Context.enter(); context.initStandardObjects( null ); HTMLDocument htmlDocument = ((DomWindow) response.getScriptingHandler()).getDocument(); if (!(htmlDocument instanceof HTMLDocumentImpl)) return; HTMLBodyElementImpl body = (HTMLBodyElementImpl) htmlDocument.getBody(); if (body == null) return; onLoadEvent = body.getOnloadEvent(); if (onLoadEvent == null) return; onLoadEvent.call( context, body, body, new Object[0] ); } catch (JavaScriptException e) { ScriptingEngineImpl.handleScriptException(e, onLoadEvent.toString()); // HttpUnitUtils.handleException(e); } catch (EcmaError ee) { //throw ee; ScriptingEngineImpl.handleScriptException(ee, onLoadEvent.toString()); } finally { Context.exit(); } }
public void invokeFunction(String name, Object... parameters) { Function func = (Function) globalScope.get(name, globalScope); if(func != null) { Context.enter(); try { func.call(context, globalScope, globalScope, parameters); } catch (EcmaError err) { KakaoManager.getInstance().receiveError(err); } String params = ""; int i = 0; for(Object p : parameters) { i++; params += " -> " + String.valueOf(p); if(i != parameters.length) { params += "\n"; } } Logger.Log log = new Logger.Log(); log.type = Logger.Type.APP; log.title = "call \"" + name + "\""; log.index = "parameters\n" + params; Logger.getInstance().add(log); } }
public void receiveError(final EcmaError err) { if(isForeground) { MainActivity.UIThread(new Runnable() { @Override public void run() { MainActivity.settingPower.setChecked(false); Toast.makeText(ctx, "Error: " + err.getName() + " (" + err.lineNumber() + ", " + err.columnNumber() + ")\n" + err.getErrorMessage(), Toast.LENGTH_SHORT).show(); } }); } Logger.Log log = new Logger.Log(); log.type = Logger.Type.ERROR; log.title = err.getName(); log.index = "at (" + err.lineNumber() + ", " + err.columnNumber() + ")\n" + err.getErrorMessage(); Logger.getInstance().add(log); }
@Override protected void executeSimpleHandlerCore(String handlerType, Context myJavascriptContext) throws EcmaError, EvaluatorException, JavaScriptException, EngineException { handlerName = "on" + handlerType; Engine.logBeans.trace("(HtmlTransaction) Searching the " + handlerType + " handler (" + handlerName + ")"); HandlerStatement handlerStatement = getHandlerStatement(handlerName); handlerResult = ""; if (handlerStatement != null) { if (!handlerStatement.isEnabled()) return; Engine.logBeans.debug("(HtmlTransaction) Execution of the " + handlerType + " handler (" + handlerName + ") for the transaction '" + getName() + "'"); handlerStatement.execute(myJavascriptContext, scope); Object returnedValue = handlerStatement.getReturnedValue(); if (returnedValue instanceof org.mozilla.javascript.Undefined) { handlerResult = ""; } else { handlerResult = returnedValue.toString(); } } else { Engine.logBeans.debug("(HtmlTransaction) No " + handlerType + " handler (" + handlerName + ") found"); } }
@Override protected void executeHandlerCore(String handlerType, org.mozilla.javascript.Context javascriptContext) throws EcmaError, EvaluatorException, JavaScriptException, EngineException { if (!AbstractHttpTransaction.EVENT_DATA_RETRIEVED.equals(handlerType)) { super.executeHandlerCore(handlerType, javascriptContext); return; } executeSimpleHandlerCore(handlerType, javascriptContext); }
protected void executeHandlerCore(String handlerType, org.mozilla.javascript.Context myJavascriptContext) throws EcmaError, EvaluatorException, JavaScriptException, EngineException { if ((!Transaction.EVENT_XML_GENERATED.equals(handlerType)) && (!Transaction.EVENT_TRANSACTION_STARTED.equals(handlerType))) { throw new IllegalArgumentException("Unknown handler type: " + handlerType); } executeSimpleHandlerCore(handlerType, myJavascriptContext); }
/** * @throws Exception if the test fails */ @Test public void testWriteReadOnly_throws() throws Exception { try { testWriteReadOnly(false); Assert.fail(); } catch (EcmaError e) { Assert.assertTrue(e.getMessage(), e.getMessage().contains("Cannot set property myProp that has only a getter")); } }
public void eval(String content) { try { this.context.evaluateString(this.scope, content, this.getAbsoluteName(), 1, null); } catch(EcmaError e) { KnuddelsServer.getDefaultLogger().error(e.getMessage()); } }
/** * evaluate a javascript String * @param name the name of the script (for error messages) * @param script the content of the script * @return the result */ public Object jsEval(String name, String script) { try { return getContext().evaluateString(scope, script, name, 1, null); } catch (EcmaError e) { throw new RuntimeException("can't evaluate "+name+": "+script,e); } }
private Function getFunction(String name, Scriptable scope, Context ctx) { Object fun; try { fun = ctx.compileString(name, null, 1, null).exec(ctx, scope); } catch (EcmaError ee) { throw new RuntimeException ("Function '" + name + "()' not found."); } if (!(fun instanceof Function)) { throw new RuntimeException("'" + name + "' is not a function"); } else { return (Function) fun; } }
private Object executeInternal(final String script, final String scriptName, final ScriptContext context, final boolean function, final Class<?> expectedResultClass) throws ScriptException { // never ever execute an empty script, it's just a waste of time if( !Check.isEmpty(script) ) { // In most cases apart from calls from birt, we can expect that // there'll be no current context, thus one will be generated by // this call into the ContextFactory return ContextFactory.getGlobal().call(new ContextAction() { @Override public Object run(Context cx) { Logger errorLogger = context.getLogger(); if( errorLogger == null ) { errorLogger = logger; } Scriptable userScriptScope = ((DefaultScriptContext) context).getUserScriptScope(cx); ScriptableModuleSourceProvider sourceProvider = new ScriptableModuleSourceProvider(userScriptScope); ModuleScriptProvider scriptProvider = new SoftCachingModuleScriptProvider(sourceProvider); cx.initStandardObjects(); Scriptable scope = ((DefaultScriptContext) context).getScope(cx); RequireBuilder builder = new RequireBuilder(); // The "uri" property must not exist in a sandbox builder.setSandboxed(false); builder.setModuleScriptProvider(scriptProvider); Require require = builder.createRequire(cx, scope); cx.setOptimizationLevel(-1); cx.setErrorReporter(new ScriptErrorReporter(errorLogger)); final String execScript = (function ? "function runScript() {\n" + script + "\n}\n runScript();" : script); final String execScriptName = (scriptName != null ? scriptName : "script"); try { context.scriptEnter(); require.install(scope); Object result = cx.evaluateString(scope, execScript, execScriptName, 1, null); context.scriptExit(); if( expectedResultClass == String.class ) { return result.toString(); } return result; } catch( JavaScriptException js ) { throw new ScriptException(js); } catch( EcmaError ec ) { throw new ScriptException(ec); } catch( EvaluatorException ev ) { throw new ScriptException(ev); } } }); } return Boolean.TRUE; }
@Test(expected = EcmaError.class) public void testS15_10_2_15_A1_T4() { String source = "(new RegExp('[\\\\Db-G]').exec('a'))"; String sourceName = "Conformance/15_Native/15.10_RegExp_Objects/15.10.2_Pattern_Semantics/15.10.2.15_NonemptyClassRanges/S15.10.2.15_A1_T4.js"; cx.evaluateString(scope, source, sourceName, 0, null); }
@Test(expected = EcmaError.class) public void testS15_10_2_15_A1_T5() { String source = "(new RegExp('[\\\\sb-G]').exec('a'))"; String sourceName = "Conformance/15_Native/15.10_RegExp_Objects/15.10.2_Pattern_Semantics/15.10.2.15_NonemptyClassRanges/S15.10.2.15_A1_T5.js"; cx.evaluateString(scope, source, sourceName, 0, null); }
@Test(expected = EcmaError.class) public void testS15_10_2_15_A1_T6() { String source = "(new RegExp('[\\\\Sb-G]').exec('a'))"; String sourceName = "Conformance/15_Native/15.10_RegExp_Objects/15.10.2_Pattern_Semantics/15.10.2.15_NonemptyClassRanges/S15.10.2.15_A1_T6.js"; cx.evaluateString(scope, source, sourceName, 0, null); }
@Test(expected = EcmaError.class) public void testS15_10_2_15_A1_T7() { String source = "(new RegExp('[\\\\wb-G]').exec('a'))"; String sourceName = "Conformance/15_Native/15.10_RegExp_Objects/15.10.2_Pattern_Semantics/15.10.2.15_NonemptyClassRanges/S15.10.2.15_A1_T7.js"; cx.evaluateString(scope, source, sourceName, 0, null); }
@Test(expected = EcmaError.class) public void testS15_10_2_15_A1_T8() { String source = "(new RegExp('[\\\\Wb-G]').exec('a'))"; String sourceName = "Conformance/15_Native/15.10_RegExp_Objects/15.10.2_Pattern_Semantics/15.10.2.15_NonemptyClassRanges/S15.10.2.15_A1_T8.js"; cx.evaluateString(scope, source, sourceName, 0, null); }
/** * This method runs part of the core module controller.js script so that we * can register all operations without having to duplicate configuration * here. */ public void loadModule(String name, String path, String... initFunctions) throws IOException { ButterflyModule core = new ButterflyModuleStub(name); // File controllerFile = new File(Configurations.get("refine.root", // "../OpenRefine"), path); String script = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(path)); if (script == null) { fLogger.warn(String.format( "Can't find controller script for module %s at %s -- " + "module may not work as expected.", name, name)); return; } // Compiles and "executes" the controller script. The script basically // contains function declarations. Context context = ContextFactory.getGlobal().enterContext(); Script controller = context.compileString( script, "init.js", 1, null); // Initializes the scope. ScriptableObject scope = new ImporterTopLevel(context); scope.put("module", scope, core); controller.exec(context, scope); for (String function : initFunctions) { // Runs the function that initializes the OperationRegistry. try { Object fun = context.compileString(function, null, 1, null) .exec(context, scope); if (fun != null && fun instanceof Function) { ((Function) fun).call(context, scope, scope, new Object[]{}); } } catch (EcmaError ex) { fLogger.error("Error running controller script.", ex); } } }
/** * Gets the event function. * * @param varValue * the var value * @param attributeName * the attribute name * @return the event function */ public Function getEventFunction(Function varValue, String attributeName) { if (varValue != null) { return varValue; } String normalAttributeName = this.normalizeAttributeName(attributeName); synchronized (this) { Map<String, Function> fba = this.functionByAttribute; Function f = fba == null ? null : (Function) fba.get(normalAttributeName); if (f != null) { return f; } UserAgentContext uac = this.getUserAgentContext(); if (uac == null) { throw new IllegalStateException("No user agent context."); } if (uac.isScriptingEnabled()) { String attributeValue = this.getAttribute(attributeName); if (attributeValue == null || attributeValue.length() == 0) { f = null; } else { String functionCode = "function " + normalAttributeName + "_" + System.identityHashCode(this) + "() {" + attributeValue + "}"; Document doc = this.document; if (doc == null) { throw new IllegalStateException("Element does not belong to a document."); } Context ctx = Executor.createContext(this.getDocumentURL(), uac); try { Scriptable scope = (Scriptable) doc.getUserData(Executor.SCOPE_KEY); if (scope == null) { throw new IllegalStateException( "Scriptable (scope) instance was expected to be keyed as UserData to document using " + Executor.SCOPE_KEY); } Scriptable thisScope = (Scriptable) JavaScript.getInstance().getJavascriptObject(this, scope); try { // TODO: Get right line number for script. //TODO: // Optimize this in case it's called multiple times? // Is that done? f = ctx.compileFunction(thisScope, functionCode, this.getTagName() + "[" + this.getId() + "]." + attributeName, 1, null); } catch (EcmaError ecmaError) { logger.error("Javascript error at " + ecmaError.sourceName() + ":" + ecmaError.lineNumber() + ": " + ecmaError.getMessage(), ecmaError); f = null; } catch (Exception err) { logger.error("Unable to evaluate Javascript code", err); f = null; } } finally { Context.exit(); } } if (fba == null) { fba = new HashMap<String, Function>(1); this.functionByAttribute = fba; } fba.put(normalAttributeName, f); } return f; } }