public Object eval(String jsCode) { return context.evaluateString(scope, jsCode, "<cmd>", 1, null); }
private void importClasses(Class<?>[] classes) { for (Class<?> clazz : classes) { context.evaluateString(scope, "importClass(" + clazz.getName() + ");", "<cmd>", 1, null); } }
private void importPackages(@NonNull Context jsContext, @NonNull ScriptableObject scope) throws StethoJsException { // Import the packages that the caller requested for (String packageName : mPackages) { try { // import from default packages String expression = String.format("importPackage(%s)", packageName); jsContext.evaluateString(scope, expression, SOURCE_NAME, 1, null); } catch (Exception e) { try { // import from application packages String expression = String.format("importPackage(Packages.%s)", packageName); jsContext.evaluateString(scope, expression, SOURCE_NAME, 1, null); } catch (Exception e1) { throw new StethoJsException(e, "Failed to import package: %s", packageName); } } } }
private void importClasses(@NonNull Context jsContext, @NonNull ScriptableObject scope) throws StethoJsException { // Import the classes that the caller requested for (Class<?> aClass : mClasses) { String className = aClass.getName(); try { // import from default classes String expression = String.format("importClass(%s)", className); jsContext.evaluateString(scope, expression, SOURCE_NAME, 1, null); } catch (Exception e) { try { // import from application classes String expression = String.format("importClass(Packages.%s)", className); jsContext.evaluateString(scope, expression, SOURCE_NAME, 1, null); } catch (Exception e1) { throw new StethoJsException(e1, "Failed to import class: %s", className); } } } }
/** * Used for processing js expressions in page spec reader. In case of failure throws an exception * @param script - JavaScript code * @return result of JavaScript code execution */ @Override public String evalStrictToString(String script) { Object returnedObject = context.evaluateString(scope, script, "<cmd>", 1, null); String unwrappedObject = unwrapProcessedObjectToString(returnedObject); if (unwrappedObject != null) { return unwrappedObject; } else return "null"; }
public static void LoadScriptFromTab( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { try { for ( int i = 0; i < ArgList.length; i++ ) { // don't worry about "undefined" arguments String strToLoad = Context.toString( ArgList[i] ); String strScript = actualObject.get( strToLoad, actualObject ).toString(); actualContext.evaluateString( actualObject, strScript, "_" + strToLoad + "_", 0, null ); } } catch ( Exception e ) { // System.out.println(e.toString()); } }
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(); }
@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); }
shell.defineFunctionProperties(names, Shell.class, ScriptableObject.DONTENUM); Scriptable scope = context.initStandardObjects(shell); context.evaluateString(scope, "print('Hello, world!')", null, 0, null); } finally { Context.exit();
private void validateResult() throws Exception { Object xmlResult = jsContext.evaluateString(jsScope, "(" + response.getContentAsString() + ")", "XML Stream", 1, null); assertNotNull("XML Result did not eval as valid JavaScript", xmlResult); assertEquals("application/xml", response.getContentType()); }
@Override @SuppressWarnings({ "unchecked" }) public void render(HttpRequest request, HttpResponse response, BeanMap beanMap, SpiderBean bean) { Context cx = Context.enter(); ScriptableObject scope = cx.initSafeStandardObjects(); String windowScript = "var window = {};var document = {};"; cx.evaluateString(scope, windowScript, "window", 1, null); HtmlParser parser = new HtmlParser(request.getUrl(), response.getContent()); for (Element ele : parser.$("script")) { String sc = ele.html(); if (StringUtils.isNotEmpty(sc)) { try { cx.evaluateString(scope, sc, "", 1, null); } catch (Exception ex) { // ex.printStackTrace(); } } } Map<String, Object> fieldMap = new HashMap<String, Object>(); Set<Field> jsVarFields = ReflectionUtils.getAllFields(bean.getClass(), ReflectionUtils.withAnnotation(JSVar.class)); for (Field jsVarField : jsVarFields) { Object value = injectJsVarField(request, beanMap, jsVarField, cx, scope); if(value != null) { fieldMap.put(jsVarField.getName(), value); } } beanMap.putAll(fieldMap); Context.exit(); }
private void validateResult() throws Exception { String json = response.getContentAsString(); DirectFieldAccessor viewAccessor = new DirectFieldAccessor(view); String jsonPrefix = (String)viewAccessor.getPropertyValue("jsonPrefix"); if (jsonPrefix != null) { json = json.substring(5); } Object jsResult = jsContext.evaluateString(jsScope, "(" + json + ")", "JSON Stream", 1, null); assertNotNull("Json Result did not eval as valid JavaScript", jsResult); assertEquals("application/json", response.getContentType()); }
return cx.evaluateString(scope, script, filename, 1, null); } catch (Error e) { throw new ScriptException(e.getMessage());
@Override public Object eval(String script, ScriptContext context) throws ScriptException { Scriptable scope = setupScope(cx, context); String filename = (filename = (String) get(ScriptEngine.FILENAME)) == null ? "<unknown>" : filename; try { return cx.evaluateString(scope, script, filename, 1, null); } catch (RhinoException e) { String msg; int line = (line = e.lineNumber()) == 0 ? -1 : line; if (e instanceof JavaScriptException) { msg = String.valueOf(((JavaScriptException) e).getValue()); } else { msg = e.getMessage(); } ScriptException scriptException = new ScriptException(msg, e.sourceName(), line); scriptException.initCause(e); throw scriptException; } finally { Context.exit(); } }
Object res = cx.evaluateString( scope, this.script, "<cmd>", 1, null ); boolean retval = Context.toBoolean( res );
/* Object startScript = */jscx.evaluateString( jsscope, strActiveStartScript, "trans_Start", 1, null ); error_message = "Found Start Script. " + strActiveStartScriptName + " Processing OK"; cr = new CheckResult( CheckResultInterface.TYPE_RESULT_OK, error_message, stepMeta ); /* Object endScript = */jscx.evaluateString( jsscope, strActiveEndScript, "trans_End", 1, null ); error_message = "Found End Script. " + strActiveEndScriptName + " Processing OK"; cr = new CheckResult( CheckResultInterface.TYPE_RESULT_OK, error_message, stepMeta );
import org.mozilla.javascript.Context; import org.mozilla.javascript.tools.shell.Global; /** * Hello world! * */ public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); Context cx = Context.enter(); Global global = new Global(cx); cx.evaluateString(global, "print('Hello World!')", "helloWorld.js", 1, null); Context.exit(); } }
String strStartScript = getStyledTextComp( folder.getItem( getCTabPosition( strActiveStartScript ) ) ).getText(); /* Object startScript = */jscx.evaluateString( jsscope, strStartScript, "trans_Start", 1, null );
String strStartScript = getStyledTextComp( folder.getItem( getCTabPosition( strActiveStartScript ) ) ).getText(); /* Object startScript = */jscx.evaluateString( jsscope, strStartScript, "trans_Start", 1, null );
public JsScriptEngine() { Context context = getContext(); scope = new ImporterTopLevel(context); // so that we can use importPackage() context.evaluateString(scope, printSource, "print", 1, null); // so that we can print on std out }