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

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

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

Context.javaToJS介绍

[英]Convenient method to convert java value to its closest representation in JavaScript.

If value is an instance of String, Number, Boolean, Function or Scriptable, it is returned as it and will be treated as the corresponding JavaScript type of string, number, boolean, function and object.

Note that for Number instances during any arithmetic operation in JavaScript the engine will always use the result of Number.doubleValue() resulting in a precision loss if the number can not fit into double.

If value is an instance of Character, it will be converted to string of length 1 and its JavaScript type will be string.

The rest of values will be wrapped as LiveConnect objects by calling WrapFactory#wrap(Context cx,Scriptable scope,Object obj,Class staticType) as in:

Context cx = Context.getCurrentContext(); 
return cx.getWrapFactory().wrap(cx, scope, value, null);

[中]将java值转换为JavaScript中最接近的表示形式的方便方法。
如果value是String、Number、Boolean、Function或Scriptable的实例,那么它将作为字符串、Number、Boolean、Function和object的对应JavaScript类型返回。
请注意,对于JavaScript中任何算术运算期间的数字实例,引擎将始终使用数字的结果。doubleValue(),如果数字不能放入double,则会导致精度损失。
若value是Character的实例,它将被转换为长度为1的字符串,其JavaScript类型将为string。
通过调用WrapFactory#wrap(上下文cx、脚本范围、对象obj、类staticType),其余的值将包装为LiveConnect对象,如下所示:

Context cx = Context.getCurrentContext(); 
return cx.getWrapFactory().wrap(cx, scope, value, null);

代码示例

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

public void putObject(String name, Object object) {
  ScriptableObject.putProperty(scope, name, Context.javaToJS(object, scope));
}

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

private void importVariables(@NonNull ScriptableObject scope) throws StethoJsException {
 // Define the variables
 for (Map.Entry<String, Object> entrySet : mVariables.entrySet()) {
  String varName = entrySet.getKey();
  Object varValue = entrySet.getValue();
  try {
   Object jsValue;
   if (varValue instanceof Scriptable || varValue instanceof Undefined) {
    jsValue = varValue;
   } else {
    jsValue = Context.javaToJS(varValue, scope);
   }
   ScriptableObject.putProperty(scope, varName, jsValue);
  } catch (Exception e) {
   throw new StethoJsException(e, "Failed to setup variable: %s", varName);
  }
 }
}

代码示例来源: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: facebook/stetho

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

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

Context.javaToJS(entry.getValue(), scope));

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

if ( getAddClasses() != null ) {
 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(

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

if ( input.getAddClasses() != null ) {
 for ( int i = 0; i < input.getAddClasses().length; i++ ) {
  Object jsOut = Context.javaToJS( input.getAddClasses()[i].getAddObject(), jsscope );
  ScriptableObject.putProperty( jsscope, input.getAddClasses()[i].getJSName(), jsOut );
Context.javaToJS( ScriptAddedFunctions.class, jsscope );
( (ScriptableObject) jsscope ).defineFunctionProperties(
 jsFunctionList, ScriptAddedFunctions.class, ScriptableObject.DONTENUM );

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

if ( input.getAddClasses() != null ) {
 for ( int i = 0; i < input.getAddClasses().length; i++ ) {
  Object jsOut = Context.javaToJS( input.getAddClasses()[i].getAddObject(), jsscope );
  ScriptableObject.putProperty( jsscope, input.getAddClasses()[i].getJSName(), jsOut );
Context.javaToJS( ScriptValuesAddedFunctions.class, jsscope );
( (ScriptableObject) jsscope ).defineFunctionProperties(
 jsFunctionList, ScriptValuesAddedFunctions.class, ScriptableObject.DONTENUM );

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

if ( meta.getAddClasses() != null ) {
 for ( int i = 0; i < meta.getAddClasses().length; i++ ) {
  Object jsOut = Context.javaToJS( meta.getAddClasses()[ i ].getAddObject(), data.scope );
  ScriptableObject.putProperty( data.scope, meta.getAddClasses()[ i ].getJSName(), jsOut );
Context.javaToJS( ScriptValuesAddedFunctions.class, data.scope );
( (ScriptableObject) data.scope ).defineFunctionProperties(
 ScriptValuesAddedFunctions.jsFunctionList, ScriptValuesAddedFunctions.class,

代码示例来源:origin: com.google.code.scriptengines/scriptengines-javascript

Object[] wrapArguments(Object[] args) {
  if (args == null) {
    return Context.emptyArgs;
  }
  Object[] res = new Object[args.length];
  for (int i = 0; i < res.length; i++) {
    res[i] = Context.javaToJS(args[i], topLevel);
  }
  return res;
}

代码示例来源:origin: rhq-project/rhq

Object[] wrapArguments(Object[] args) {
  if (args == null) {
    return Context.emptyArgs;
  }
  Object[] res = new Object[args.length];
  for (int i = 0; i < res.length; i++) {
    res[i] = Context.javaToJS(args[i], topLevel);
  }
  return res;
}

代码示例来源:origin: jasperreports/jasperreports

protected final Object toJSValue(Object value)
  {
    return Context.javaToJS(value, scope);
  }
}

代码示例来源:origin: org.bsc/jvm-npm-rhino

protected Object[] wrapArguments(Object[] args) {
  if (args == null) {
    return Context.emptyArgs;
  }
  Object[] res = new Object[args.length];
  for (int i = 0; i < res.length; i++) {
    res[i] = Context.javaToJS(args[i], topLevel);
  }
  return res;
}

代码示例来源:origin: cat.inspiracio/rhino-js-engine

Object[] wrapArguments(Object[] args) {
  if (args == null) {
    return Context.emptyArgs;
  }
  Object[] res = new Object[args.length];
  for (int i = 0; i < res.length; i++) {
    res[i] = Context.javaToJS(args[i], topLevel);
  }
  return res;
}

代码示例来源:origin: io.apisense/rhino-android

Object[] wrapArguments(Object[] args) {
  if (args == null) {
    return Context.emptyArgs;
  }
  Object[] res = new Object[args.length];
  for (int i = 0; i < res.length; i++) {
    res[i] = Context.javaToJS(args[i], topLevel);
  }
  return res;
}

代码示例来源:origin: org.apache.cocoon/cocoon-flowscript-impl

/**
 * Access components.
 *
 * TODO: Do we want to restrict the access of sitemap components? (RP)
 * TODO: Do we want to raise an error or return null? (RP)
 */
public Object jsFunction_getComponent(String id)
  throws Exception {
  return org.mozilla.javascript.Context.javaToJS(currentCall.webAppContext.getBean(id), 
                          getParentScope());
}

代码示例来源:origin: mulesoft-labs/rhinodo

@Override
  public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
    if ( args.length != 1) {
      throw new RuntimeException("Only existsSync with 1 parameter supported");
    }

    return Context.javaToJS(new File(Context.toString(args[0])).getAbsoluteFile().exists(), scope);
  }
}

代码示例来源:origin: io.apigee.trireme/trireme-util

@JSFunction
public static Object encodingExists(Context cx, Scriptable thisObj, Object[] args, Function funObj)
{
  String encoding = stringArg(args, 0);
  return Context.javaToJS(Charset.isSupported(encoding), funObj);
}

代码示例来源:origin: org.apache.cocoon/cocoon-flowscript-impl

public Object jsFunction_getAttribute(String name) {
  return org.mozilla.javascript.Context.javaToJS(
      wk.getAttribute(name),
      getParentScope());
}

代码示例来源:origin: org.apache.cocoon/cocoon-flowscript-impl

public Object jsFunction_getAttributeNames() {
  return org.mozilla.javascript.Context.javaToJS(
      wk.getAttributeNames(),
      getParentScope());
}

相关文章

Context类方法