Refine search
jsscope = jscx.initStandardObjects( null, false ); try { jscx.setOptimizationLevel( Integer.valueOf( transMeta.environmentSubstitute( optimizationLevel ) ) ); } catch ( NumberFormatException nfe ) { error_message = Scriptable jsR = Context.toObject( jsScripts[i].getScript(), jsscope ); jsscope.put( jsScripts[i].getScriptName(), jsscope, jsR ); for ( int i = 0; i < getAddClasses().length; i++ ) { Object jsOut = Context.javaToJS( getAddClasses()[i].getAddObject(), jsscope ); ScriptableObject.putProperty( jsscope, getAddClasses()[i].getJSName(), jsOut ); Context.javaToJS( ScriptValuesAddedFunctions.class, jsscope ); ( (ScriptableObject) jsscope ) .defineFunctionProperties( ScriptValuesAddedFunctions.jsFunctionList, ScriptValuesAddedFunctions.class, ScriptableObject.DONTENUM ); jsscope.put( "SKIP_TRANSFORMATION", jsscope, Integer.valueOf( ScriptValuesMod.SKIP_TRANSFORMATION ) ); jsscope.put( "ABORT_TRANSFORMATION", jsscope, Integer.valueOf( ScriptValuesMod.ABORT_TRANSFORMATION ) ); jsscope.put( "ERROR_TRANSFORMATION", jsscope, Integer.valueOf( ScriptValuesMod.ERROR_TRANSFORMATION ) ); jsscope
public TemplateCollectionModel values() throws TemplateModelException { Object[] ids = scriptable.getIds(); Object[] values = new Object[ids.length]; for (int i = 0; i < values.length; i++) { Object id = ids[i]; if (id instanceof Number) { values[i] = ScriptableObject.getProperty(scriptable, ((Number) id).intValue()); } else { values[i] = ScriptableObject.getProperty(scriptable, String.valueOf(id)); } } return (TemplateCollectionModel) wrapper.wrap(values); }
String script = "function abc(x,y) {return x+y;}" + "function def(u,v) {return u-v;}"; Context context = Context.enter(); try { ScriptableObject scope = context.initStandardObjects(); context.evaluateString(scope, script, "script", 1, null); Function fct = (Function)scope.get("abc", scope); Object result = fct.call( context, scope, scope, new Object[] {2, 3}); System.out.println(Context.jsToJava(result, int.class)); } finally { Context.exit(); }
private void importConsole(@NonNull ScriptableObject scope) throws StethoJsException { // Set the `console` object try { ScriptableObject.defineClass(scope, JsConsole.class); JsConsole console = new JsConsole(scope); scope.defineProperty("console", console, ScriptableObject.DONTENUM); } catch (Exception e) { throw new StethoJsException(e, "Failed to setup javascript console"); } }
@Override public @Nullable Object evaluate(@NonNull String expression) throws Throwable { Object result; final Context jsContext = enterJsContext(); try { result = jsContext.evaluateString(mJsScope, expression, "chrome", 1, null); // Google chrome automatically saves the last expression to `$_`, we do the same Object jsValue = Context.javaToJS(result, mJsScope); ScriptableObject.putProperty(mJsScope, "$_", jsValue); } finally { Context.exit(); } return Context.jsToJava(result, Object.class); }
Context cx = factory.enterContext(); ScriptableObject scriptable = new ImporterTopLevel(cx); Scriptable scope = cx.initStandardObjects(scriptable); ScriptableObject.putProperty(scope, entry.getKey(), Context.javaToJS(entry.getValue(), scope)); return cx.evaluateString(scope, script, filename, 1, null); } catch (Error e) { throw new ScriptException(e.getMessage());
public Object exec(List arguments) throws TemplateModelException { Context cx = Context.getCurrentContext(); Object[] args = arguments.toArray(); BeansWrapper wrapper = getWrapper(); for (int i = 0; i < args.length; i++) { args[i] = wrapper.unwrap((TemplateModel) args[i]); } return wrapper.wrap(((Function) getScriptable()).call(cx, ScriptableObject.getTopLevelScope(fnThis), fnThis, args)); } }
public static Object setDefaultNamespace(Object namespace, Context cx) { Scriptable scope = cx.currentActivationCall; if (scope == null) { scope = getTopCallScope(cx); } XMLLib xmlLib = currentXMLLib(cx); Object ns = xmlLib.toDefaultXmlNamespace(cx, namespace); // XXX : this should be in separated namesapce from Scriptable.get/put if (!scope.has(DEFAULT_NS_TAG, scope)) { // XXX: this is racy of cause ScriptableObject.defineProperty(scope, DEFAULT_NS_TAG, ns, ScriptableObject.PERMANENT | ScriptableObject.DONTENUM); } else { scope.put(DEFAULT_NS_TAG, scope, ns); } return Undefined.instance; }
@Override public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { Scriptable curr = cx.newObject(scope); Scriptable prev = cx.newObject(scope); long mtime = new File(item).lastModified(); Object date = cx.evaluateString(scope, "new Date(" + mtime + ");", "date", 0, null); ScriptableObject.putProperty(prev, "mtime", date); ScriptableObject.putProperty(curr, "mtime", date); callback.call(cx,scope,thisObj, new Object[]{prev,curr}); return Undefined.instance; } });
private @NonNull ScriptableObject initJsScope(@NonNull Context jsContext) { // Set the main Rhino goodies ImporterTopLevel importerTopLevel = new ImporterTopLevel(jsContext); ScriptableObject scope = jsContext.initStandardObjects(importerTopLevel, false); ScriptableObject.putProperty(scope, "context", Context.javaToJS(mContext, scope)); try { importClasses(jsContext, scope); importPackages(jsContext, scope); importConsole(scope); importVariables(scope); importFunctions(scope); } catch (StethoJsException e) { String message = String.format("%s\n%s", e.getMessage(), Log.getStackTraceString(e)); LogUtil.e(e, message); CLog.writeToConsole(Console.MessageLevel.ERROR, Console.MessageSource.JAVASCRIPT, message); } return scope; }
@Override public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { Scriptable repl = (Scriptable) ScriptableObject.getTypedProperty(scope, "require", Function.class) .call(cx, scope, thisObj, new Object[]{"repl"}); Function start = ScriptableObject.getTypedProperty(repl, "start", Function.class); Scriptable options = cx.newObject(scope); ScriptableObject.putProperty(options, "prompt", Context.javaToJS(PROMPT, scope)); ScriptableObject.putProperty(options, "terminal", Context.javaToJS(true, scope)); start.call(cx, scope, thisObj, new Object[]{options}); return Undefined.instance; } });
public class RJsDemo { @Test public void simpleRhinoTest() throws FileNotFoundException, IOException { Context cx = Context.enter(); final JsRuntimeSupport browserSupport = new JsRuntimeSupport(); final ScriptableObject sharedScope = cx.initStandardObjects(browserSupport, true); String[] names = { "print", "load" }; sharedScope.defineFunctionProperties(names, sharedScope.getClass(), ScriptableObject.DONTENUM); Scriptable argsObj = cx.newArray(sharedScope, new Object[] {}); sharedScope.defineProperty("arguments", argsObj, ScriptableObject.DONTENUM); cx.evaluateReader(sharedScope, new FileReader("./r.js"), "require", 1, null); cx.evaluateReader(sharedScope, new FileReader("./loader.js"), "loader", 1, null); Context.exit(); } }
@Override protected boolean isValid(Row row) throws Exception { Map<String, Object> values = new ScriptAccessColumnMap(_columns, row, _evaluatedColumns); Scriptable scope = _context.newObject(_standardObjectsScope); scope.setPrototype(_standardObjectsScope); scope.setParentScope(null); Object wrappedValues = Context.javaToJS(values, scope); ScriptableObject.putProperty(scope, "values", wrappedValues); Object result = _script.exec(_context, scope); if (result instanceof Boolean) { return (Boolean) result; } throw new IllegalStateException( "Javascript expression did not return a boolean"); }
@Override public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { if ( args.length != 3 && args.length != 2 ) { throw new IllegalArgumentException(); } String code = (String) Context.toString(args[0]); Scriptable vmContext = (Scriptable) args[1]; String scriptName = (String) (args.length > 2 ? args[2] : "_RunInConext_NoName" ); ScriptableObject.putProperty(vmContext, "window", vmContext); vmContext.setParentScope(scope ); return cx.evaluateString(vmContext, code, scriptName, 0, null); } }
public DocumentConverter(final Context context, final View view) throws IOException, JSONException { this.context = context; scope = context.initStandardObjects(); context.setLanguageVersion(Context.VERSION_1_8); // Allow custom document helper class. try { ScriptableObject.defineClass(scope, RhinoDocument.class); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } // Add a log object ScriptableObject.putProperty(scope, "log", new JSLog()); // Compile user-specified function try { viewFun = view.compileFunction(context, scope); } catch (final RhinoException e) { LOG.error("View code for " + view + " does not compile."); throw e; } }
public static Object setName(Scriptable bound, Object value, Context cx, Scriptable scope, String id) { if (bound != null) { // TODO: we used to special-case XMLObject here, but putProperty // seems to work for E4X and it's better to optimize the common case ScriptableObject.putProperty(bound, id, value); } else { // "newname = 7;", where 'newname' has not yet // been defined, creates a new property in the // top scope unless strict mode is specified. if (cx.hasFeature(Context.FEATURE_STRICT_MODE) || cx.hasFeature(Context.FEATURE_STRICT_VARS)) { Context.reportWarning( ScriptRuntime.getMessage1("msg.assn.create.strict", id)); } // Find the top scope by walking up the scope chain. bound = ScriptableObject.getTopLevelScope(scope); if (cx.useDynamicScope) { bound = checkDynamicScope(cx.topCallScope, bound); } bound.put(id, bound, value); } return value; }
@Override public Scriptable registerExports(Context cx, Scriptable global, NodeRuntime runtime) throws InvocationTargetException, IllegalAccessException, InstantiationException { Scriptable exp = cx.newObject(global); exp.setPrototype(global); exp.setParentScope(null); ScriptableObject.defineClass(exp, ContextImpl.class); ScriptableObject.defineClass(exp, ScriptImpl.class); return exp; }
/** * Call a method of an object. * @param cx the Context object associated with the current thread. * @param obj the JavaScript object * @param methodName the name of the function property * @param args the arguments for the call */ public static Object callMethod(Context cx, Scriptable obj, String methodName, Object[] args) { Object funObj = getProperty(obj, methodName); if (!(funObj instanceof Function)) { throw ScriptRuntime.notFunctionError(obj, methodName); } Function fun = (Function)funObj; // XXX: What should be the scope when calling funObj? // The following favor scope stored in the object on the assumption // that is more useful especially under dynamic scope setup. // An alternative is to check for dynamic scope flag // and use ScriptableObject.getTopLevelScope(fun) if the flag is not // set. But that require access to Context and messy code // so for now it is not checked. Scriptable scope = ScriptableObject.getTopLevelScope(obj); if (cx != null) { return fun.call(cx, scope, obj, args); } else { return Context.call(null, fun, scope, obj, args); } }
public Object run(Context cx) { ScriptableObject global = getGlobal(cx); // get the command line arguments and define "arguments" // array in the top-level object Object[] argsCopy = new Object[args.length]; System.arraycopy(args, 0, argsCopy, 0, args.length); Scriptable argsObj = cx.newArray(global, argsCopy); global.defineProperty("arguments", argsObj, ScriptableObject.DONTENUM); script.exec(cx, global); return null; } });
private static void js_captureStackTrace(Context cx, Scriptable thisObj, Object[] args) { ScriptableObject obj = (ScriptableObject)ScriptRuntime.toObjectOrNull(cx, args[0], thisObj); Function func = null; if (args.length > 1) { func = (Function)ScriptRuntime.toObjectOrNull(cx, args[1], thisObj); } // Create a new error that will have the correct prototype so we can re-use "getStackTrace" NativeError err = (NativeError)cx.newObject(thisObj, "Error"); // Wire it up so that it will have an actual exception with a stack trace err.setStackProvider(new EvaluatorException("[object Object]")); // Figure out if they passed a function used to hide part of the stack if (func != null) { Object funcName = func.get("name", func); if ((funcName != null) && !Undefined.instance.equals(funcName)) { err.associateValue(STACK_HIDE_KEY, Context.toString(funcName)); } } // Define a property on the specified object to get that stack // that delegates to our new error. Build the stack trace lazily // using the "getStack" code from NativeError. obj.defineProperty("stack", err, ERROR_DELEGATE_GET_STACK, ERROR_DELEGATE_SET_STACK, 0); }