org.mozilla.javascript.Context.evaluateString()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 JavaScript  
字(9.0k)|赞(0)|评价(0)|浏览(232)

本文整理了Java中org.mozilla.javascript.Context.evaluateString()方法的一些代码示例,展示了Context.evaluateString()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.evaluateString()方法的具体详情如下:
包路径:org.mozilla.javascript.Context
类名称:Context
方法名:evaluateString

Context.evaluateString介绍

[英]Evaluate a JavaScript source string. The provided source name and line number are used for error messages and for producing debug information.
[中]计算JavaScript源字符串的值。提供的源名称和行号用于错误消息和生成调试信息。

代码示例

代码示例来源:origin: galenframework/galen

public Object eval(String jsCode) {
  return context.evaluateString(scope, jsCode, "<cmd>", 1, null);
}

代码示例来源:origin: galenframework/galen

private void importClasses(Class<?>[] classes) {
  for (Class<?> clazz : classes) {
    context.evaluateString(scope, "importClass(" + clazz.getName() + ");", "<cmd>", 1, null);
  }
}

代码示例来源:origin: facebook/stetho

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);
   }
  }
 }
}

代码示例来源:origin: facebook/stetho

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);
   }
  }
 }
}

代码示例来源:origin: galenframework/galen

/**
 * 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";
}

代码示例来源:origin: pentaho/pentaho-kettle

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());
 }
}

代码示例来源:origin: stackoverflow.com

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();
}

代码示例来源:origin: facebook/stetho

@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);
}

代码示例来源:origin: stackoverflow.com

shell.defineFunctionProperties(names, Shell.class, ScriptableObject.DONTENUM);
  Scriptable scope = context.initStandardObjects(shell);
  context.evaluateString(scope, "print('Hello, world!')", null, 0, null);
} finally {
  Context.exit();

代码示例来源:origin: spring-projects/spring-framework

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());
}

代码示例来源:origin: xtuhcy/gecco

@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();
}

代码示例来源:origin: spring-projects/spring-framework

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());
}

代码示例来源:origin: EngineHub/WorldEdit

return cx.evaluateString(scope, script, filename, 1, null);
} catch (Error e) {
  throw new ScriptException(e.getMessage());

代码示例来源:origin: EngineHub/WorldEdit

@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();
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

Object res = cx.evaluateString( scope, this.script, "<cmd>", 1, null );
boolean retval = Context.toBoolean( res );

代码示例来源:origin: pentaho/pentaho-kettle

/* 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 );

代码示例来源:origin: stackoverflow.com

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();
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

String strStartScript =
 getStyledTextComp( folder.getItem( getCTabPosition( strActiveStartScript ) ) ).getText();
/* Object startScript = */jscx.evaluateString( jsscope, strStartScript, "trans_Start", 1, null );

代码示例来源:origin: pentaho/pentaho-kettle

String strStartScript =
 getStyledTextComp( folder.getItem( getCTabPosition( strActiveStartScript ) ) ).getText();
/* Object startScript = */jscx.evaluateString( jsscope, strStartScript, "trans_Start", 1, null );

代码示例来源:origin: org.apache.pig/pig

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
}

相关文章

微信公众号

Context类方法