/** * call a JS function and return its result as Object * * @param functionName * @param args * @throws ScriptException * @return */ private Object callFunction(String functionName, Object[] args) throws ScriptException { Object functionObj = scope.get(functionName, scope); if (!(functionObj instanceof Function)) { throw new ScriptException(functionName+ " is undefined or not a function"); } Function func = (Function)functionObj; try { Object result = func.call(ctx, scope, scope, args); // LOGGER.info("Call to javascript function "+functionName+" returned "+result.toString()); return result; } catch (RhinoException e) { throw makeScriptException(e); } }
/** * Call JavaScript functions with an argument array. * * @param f * The function to be executed * @param oaArgs * The Java object arguments passed to the function being * executed */ private Object callJavaScriptFunction( Function f, Object[] oaArgs ) throws CrosstabException { final Context cx = Context.enter( ); Object oReturnValue = null; try { oReturnValue = f.call( cx, scope, scope, oaArgs ); } catch ( RhinoException ex ) { throw convertException( ex ); } finally { Context.exit( ); } return oReturnValue; }
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; } }
public static void processFileNoThrow(Context cx, Scriptable scope, String filename) { try { processFile(cx, scope, filename); } catch (IOException ioex) { Context.reportError(ToolErrorReporter.getMessage( "msg.couldnt.read.source", filename, ioex.getMessage())); exitCode = EXITCODE_FILE_NOT_FOUND; } 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; } }
@Override public void onException(ScriptExecution execution, Exception e) { RhinoException rhinoException = getRhinoException(e); int line = -1, col = 0; if (rhinoException != null) { line = rhinoException.lineNumber(); col = rhinoException.columnNumber(); } if (ScriptInterruptedException.causedByInterrupted(e)) { App.getApp().sendBroadcast(new Intent(ACTION_ON_EXECUTION_FINISHED) .putExtra(EXTRA_EXCEPTION_LINE_NUMBER, line) .putExtra(EXTRA_EXCEPTION_COLUMN_NUMBER, col)); } else { App.getApp().sendBroadcast(new Intent(ACTION_ON_EXECUTION_FINISHED) .putExtra(EXTRA_EXCEPTION_MESSAGE, e.getMessage()) .putExtra(EXTRA_EXCEPTION_LINE_NUMBER, line) .putExtra(EXTRA_EXCEPTION_COLUMN_NUMBER, col)); } }
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; }
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(); }
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(); } }
public PluginBase load(File file){ try{ PluginBase plugin; if(file.getName().endsWith(".jar")) plugin = this.loadJar(file); else if(file.getName().endsWith(".js")) plugin = new JavaScriptPlugin(file); else return null; Takoyaki.getInstance().getLogger().info("플러그인을 불러옵니다: " + plugin.getName() + (plugin.getVersion() != null ? " v" + plugin.getVersion() : "")); plugin.onLoad(); return plugin; }catch(IOException | RhinoException | ReflectiveOperationException e){ Takoyaki.getInstance().getLogger().error(e.getClass().getName() + ": " + e.getMessage()); } return null; }
/** * Wraps given exception and throws it as ScriptException. * * @param ex Exception to be wrapped. * @throws ScriptException Thrown always by this method. */ public static void throwWrappedScriptException(Exception ex) throws ScriptException { if ( ex instanceof RhinoException) { RhinoException rhinoException = (RhinoException)ex; int line = rhinoException.lineNumber(); int column = rhinoException.columnNumber(); String message; if (ex instanceof JavaScriptException) { message = String.valueOf(((JavaScriptException)ex).getValue()); } else { message = ex.toString(); } ScriptException scriptException = new ScriptException(message, rhinoException.sourceName(), line, column); scriptException.initCause(ex); throw scriptException; } else { throw new ScriptException(ex); } }
public Scriptable construct(Context cx, Scriptable scope, Object[] args) throws RhinoException { if (isPrototype) { Scriptable topLevel = ScriptableObject.getTopLevelScope(scope); JSAdapter newObj; if (args.length > 0) { newObj = new JSAdapter(Context.toObject(args[0], topLevel)); } else { throw Context.reportRuntimeError("JSAdapter requires adaptee"); } return newObj; } else { Scriptable tmp = getAdaptee(); if (tmp instanceof Function) { return ((Function)tmp).construct(cx, scope, args); } else { throw Context.reportRuntimeError("TypeError: not a constructor"); } } }
private Object call(Function func, Object[] args) { Context cx = Context.getCurrentContext(); Scriptable thisObj = getAdaptee(); Scriptable scope = func.getParentScope(); try { return func.call(cx, scope, thisObj, args); } catch (RhinoException re) { throw Context.reportRuntimeError(re.getMessage()); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) public Scriptable construct(Context cx, Scriptable scope, Object[] args) throws RhinoException { if (args.length == 2) { Class clazz = null; Object obj1 = args[0]; if (obj1 instanceof Wrapper) { Object o = ((Wrapper) obj1).unwrap(); if (o instanceof Class && ((Class) o).isInterface()) { clazz = (Class) o; } } else if (obj1 instanceof Class) { if (((Class) obj1).isInterface()) { clazz = (Class) obj1; } } if (clazz == null) { throw Context.reportRuntimeError("JavaAdapter: first arg should be interface Class"); } Scriptable topLevel = ScriptableObject.getTopLevelScope(scope); return Context.toObject(engine.getInterface(args[1], clazz), topLevel); } else { throw Context.reportRuntimeError("JavaAdapter requires two arguments"); } }
public Object eval(Reader reader, ScriptContext ctxt) throws ScriptException { Object ret; Context cx = enterContext(); try { Scriptable scope = getRuntimeScope(ctxt); String filename = (String) get(ScriptEngine.FILENAME); filename = filename == null ? "<Unknown source>" : filename; ret = cx.evaluateReader(scope, reader, filename , 1, null); } catch (RhinoException re) { int line = (line = re.lineNumber()) == 0 ? -1 : line; throw new ScriptException(re.toString(), re.sourceName(), line); } catch (IOException ee) { throw new ScriptException(ee); } finally { Context.exit(); } return unwrapReturnValue(ret); }
public Object eval(String script, ScriptContext ctxt) throws ScriptException { if (script == null) { throw new NullPointerException("null script"); } Object ret; Context cx = enterContext(); int level=cx.getOptimizationLevel(); try { if(script.length()>71680){ cx.setOptimizationLevel(-1); } Scriptable scope = getRuntimeScope(ctxt); String filename = (String) get(ScriptEngine.FILENAME); filename = filename == null ? "<Unknown source>" : filename; ret = cx.evaluateString(scope, script, filename , 1, null); cx.setOptimizationLevel(level); } catch (RhinoException re) { int line = (line = re.lineNumber()) == 0 ? -1 : line; throw new ScriptException(re.toString(), re.sourceName(), line); } finally { Context.exit(); } return unwrapReturnValue(ret); }
private Object invoke(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException { Context cx = enterContext(); try { if (name == null) { throw new NullPointerException("method name is null"); } if (thiz != null && !(thiz instanceof Scriptable)) { thiz = Context.toObject(thiz, topLevel); } Scriptable engineScope = getRuntimeScope(context); Scriptable localScope = (thiz != null)? (Scriptable) thiz : engineScope; Object obj = ScriptableObject.getProperty(localScope, name); if (! (obj instanceof Function)) { throw new NoSuchMethodException("no such method: " + name); } Function func = (Function) obj; Scriptable scope = func.getParentScope(); if (scope == null) { scope = engineScope; } Object result = func.call(cx, scope, localScope, wrapArguments(args)); return unwrapReturnValue(result); } catch (RhinoException re) { int line = (line = re.lineNumber()) == 0 ? -1 : line; throw new ScriptException(re.toString(), re.sourceName(), line); } finally { Context.exit(); } }
/** * Executes an arbitrary expression.<br> * It fails if the expression throws a JsAssertException.<br> * It fails if the expression throws a RhinoException.<br> * * Code from JsTester (http://jstester.sf.net/) */ public Object eval(String expr) { Object value = null; try { value = context.evaluateString(globalScope, expr, "", 1, null); } catch (JavaScriptException jse) { Scriptable jsAssertException = (Scriptable) globalScope.get( "currentException", globalScope); jse.printStackTrace(); String message = (String) jsAssertException.get("message", jsAssertException); if (message != null) { fail(message); } } catch (RhinoException re) { fail(re.getMessage()); } return value; }
/** * Initialize the JavaScript context using given parent scope. * * @param scPrototype * Parent scope object. If it's null, use default scope. */ public void init( Scriptable scPrototype ) throws CrosstabException { final Context cx = Context.enter( ); try { if ( scPrototype == null ) { scope = new ImporterTopLevel( cx ); } else { scope = cx.newObject( scPrototype ); scope.setPrototype( scPrototype ); } } catch ( RhinoException jsx ) { throw convertException( jsx ); } finally { Context.exit( ); } }
/** * Converts general exception to more readable format. * * @param ex * @return */ protected CrosstabException convertException( Exception ex ) { if ( ex instanceof RhinoException ) { RhinoException e = (RhinoException) ex; String lineSource = e.lineSource( ); String details = e.details( ); String lineNumber = String.valueOf( e.lineNumber( ) ); if ( lineSource == null ) lineSource = "";//$NON-NLS-1$ return new CrosstabException( Messages.getString( "CrosstabScriptHandler.error.javascript", //$NON-NLS-1$ new Object[]{ details, lineNumber, lineSource } ) ); } else { return new CrosstabException( ex ); } }
/** * Call JavaScript functions with an argument array. * * @param f * The function to be executed * @param oaArgs * The Java object arguments passed to the function being * executed */ private final Object callJavaScriptFunction( Function f, Object[] oaArgs ) throws ChartException { final Context cx = Context.enter( ); Object oReturnValue = null; // #229402 ClassLoader oldLoader = cx.getApplicationClassLoader( ); ClassLoader appLader = SecurityUtil.getClassLoader( AbstractScriptHandler.this.getClass( ) ); cx.setApplicationClassLoader( appLader ); // Initialize BIRT functions, register them into current script context. new CoreJavaScriptInitializer( ).initialize( cx, scope ); try { oReturnValue = f.call( cx, scope, scope, oaArgs ); } catch ( RhinoException ex ) { throw convertException( ex ); } finally { cx.setApplicationClassLoader( oldLoader ); Context.exit( ); } return oReturnValue; }
/** * Evaluates the given expression and returns the value. * * @param sScriptContent */ public final Object evaluate( String sScriptContent ) throws ChartException { final Context cx = Context.enter( ); try { return cx.evaluateString( scope, sScriptContent, "<cmd>", 1, null ); //$NON-NLS-1$ } catch ( RhinoException jsx ) { throw convertException( jsx ); } finally { Context.exit( ); } }
protected void checkScriptSyntax( String string ) throws RhinoException { if ( string == null ) return; if ( !isJavaClassName( string ) ) { try { Context cx = Context.enter( ); cx.compileString( string, "chartScript", 1, null ); //$NON-NLS-1$ } finally { Context.exit( ); } } }
/** * Evaluates the compiled byte code */ public Object evaluate( ScriptContext context, Scriptable scope ) throws DataException { try { Object result = JavascriptEvalUtil.convertJavascriptValue( m_script.exec( Context.getCurrentContext( ), scope ) ); return result; } catch ( RhinoException e ) { throw DataException.wrap( JavascriptEvalUtil.wrapRhinoException(e, "<compiled script>", null, 0) ); } }