org.mozilla.javascript.ImporterTopLevel类的使用及代码示例

x33g5p2x  于2022-01-21 转载在 JavaScript  
字(10.0k)|赞(0)|评价(0)|浏览(202)

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

ImporterTopLevel介绍

[英]Class ImporterTopLevel This class defines a ScriptableObject that can be instantiated as a top-level ("global") object to provide functionality similar to Java's "import" statement.

This class can be used to create a top-level scope using the following code:

Scriptable scope = new ImporterTopLevel(cx);

Then JavaScript code will have access to the following methods:

  • importClass - will "import" a class by making its unqualified name available as a property of the top-level scope
  • importPackage - will "import" all the classes of the package by searching for unqualified names as classes qualified by the given package.
    The following code from the shell illustrates this use:
js> importClass(java.io.File) 
js> f = new File('help.txt') 
help.txt 
js> importPackage(java.util) 
js> v = new Vector() 
[]

[中]类ImporterTopLevel该类定义了一个ScriptableObject,可以将其实例化为顶级(“全局”)对象,以提供类似于Java的“导入”语句的功能。
此类可用于使用以下代码创建顶级作用域:

Scriptable scope = new ImporterTopLevel(cx);

然后JavaScript代码将可以访问以下方法:
*importClass—将通过使其非限定名称作为顶级作用域的属性可用来“导入”类
*importPackage-将通过搜索非限定名称作为给定包限定的类来“导入”包的所有类。
shell中的以下代码演示了此用法:

js> importClass(java.io.File) 
js> f = new File('help.txt') 
help.txt 
js> importPackage(java.util) 
js> v = new Vector() 
[]

代码示例

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

RhinoContextFactory factory = new RhinoContextFactory(timeLimit);
Context cx = factory.enterContext();
ScriptableObject scriptable = new ImporterTopLevel(cx);
Scriptable scope = cx.initStandardObjects(scriptable);
      Context.javaToJS(entry.getValue(), scope));
  return cx.evaluateString(scope, script, filename, 1, null);
} catch (Error e) {
  throw new ScriptException(e.getMessage());

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

public GalenJsExecutor() {
  this.context = Context.enter();
  this.scope = new ImporterTopLevel(context);
  
  this.loadFunction = new JsFunctionLoad();
  scope.defineProperty("load", loadFunction, ScriptableObject.DONTENUM);
  importAllMajorClasses();
}

代码示例来源:origin: org.ofbiz/ofbcore-jira-share

/**
 * initialize the engine. put the manager into the context -> manager
 * map hashtable too.
 */
public void initialize(BSFManager mgr, String lang, Vector declaredBeans) throws BSFException {
  super.initialize(mgr, lang, declaredBeans);
  
  // Initialize context and global scope object
  try {
    Context cx = Context.enter();
    global = cx.initStandardObjects(new ImporterTopLevel(cx));
    Scriptable bsf = Context.toObject(new BSFFunctions(mgr, this), global);
    global.put("bsf", global, bsf);
    
    int size = declaredBeans.size();
    for (int i = 0; i < size; i++) {
      declareBean((BSFDeclaredBean) declaredBeans.elementAt(i));
    }
  } finally {
    Context.exit();
  }
}

代码示例来源:origin: com.couchbase.mock/CouchbaseMock

public JavascriptRun() {
  Context cx = Context.enter();
  scope = new ImporterTopLevel(cx);
  try {
    cx.evaluateString(scope, COLLATE_JS, "collate.js", 1, null);
    cx.evaluateString(scope, VIEWIDXR_JS, "viewidxr.js", 1, null);
  } finally {
    Context.exit();
  }
  execFunc = (Function) scope.get("execute", scope);
}

代码示例来源:origin: org.apache.sling/org.apache.sling.scripting.javascript

@SuppressWarnings("unchecked")
private Scriptable getRootScope() {
  if (rootScope == null) {
    final Context rhinoContext = Context.enter();
    try {
      rhinoContext.setOptimizationLevel(optimizationLevel);
      rhinoContext.setLanguageVersion(RHINO_LANGUAGE_VERSION);
      Scriptable tmpScope = rhinoContext.initStandardObjects(new ImporterTopLevel(rhinoContext), false);
      // default classes
      addHostObjects(tmpScope, (Class<? extends ScriptableObject>[]) HOSTOBJECT_CLASSES);
      // provided classes
      for (RhinoHostObjectProvider provider : hostObjectProvider) {
        addHostObjects(tmpScope, provider.getHostObjectClasses());
        addImportedClasses(rhinoContext, tmpScope, provider.getImportedClasses());
        addImportedPackages(rhinoContext, tmpScope, provider.getImportedPackages());
      }
      // only assign the root scope when complete set up
      rootScope = tmpScope;
    } finally {
      // ensure the context is exited after setting up the
      // the new root scope
      Context.exit();
    }
  }
  return rootScope;
}

代码示例来源:origin: deas/alfresco

private Object executeScript(String script, Map<String, Object> model, boolean secure)
  Context cx = Context.enter();
  try
    cx.setWrapFactory(wrapFactory);
    Scriptable scope;
    if (!secure)
      scope = cx.initStandardObjects();
      scope = new ImporterTopLevel(cx);

代码示例来源:origin: org.apache.sling/org.apache.sling.scripting.javascript

final Context rhinoContext = Context.enter();
rhinoContext.setLanguageVersion(((RhinoJavaScriptEngineFactory)getFactory()).rhinoLanguageVersion());
rhinoContext.setOptimizationLevel(optimizationLevel());
  scope = new ImporterTopLevel();

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

private Scriptable setupScope(Context cx, ScriptContext context) {
    ScriptableObject scriptable = new ImporterTopLevel(cx);
    Scriptable scope = cx.initStandardObjects(scriptable);
    //ScriptableObject.putProperty(scope, "argv", Context.javaToJS(args, scope));
    return scope;
  }
}

代码示例来源:origin: rhino/js

public void initStandardObjects(Context cx, boolean sealed)
{
  // Assume that Context.initStandardObjects initialize JavaImporter
  // property lazily so the above init call is not yet called
  cx.initStandardObjects(this, sealed);
  topScopeFlag = true;
  // If seal is true then exportAsJSClass(cx, seal) would seal
  // this obj. Since this is scope as well, it would not allow
  // to add variables.
  IdFunctionObject ctor = exportAsJSClass(MAX_PROTOTYPE_ID, this, false);
  if (sealed) {
    ctor.sealObject();
  }
  // delete "constructor" defined by exportAsJSClass so "constructor"
  // name would refer to Object.constructor
  // and not to JavaImporter.prototype.constructor.
  delete("constructor");
}

代码示例来源:origin: ro.isdc.wro4j/rhino

switch (type) {
 case IPROXY_COMPILE_SCRIPT:
  cx.compileString(text, url, 1, null);
  break;
      scope = new ImporterTopLevel(cx);
    cx.evaluateString(scope, text, url, 1, null);
  booleanResult = cx.stringIsCompilableUnit(text);
  break;

代码示例来源:origin: couchbase/CouchbaseMock

private Indexer(String mapTxt, Context cx) {
  scope = new ImporterTopLevel(cx);
  cx.evaluateString(scope, INDEX_JS, "index.js", 1, null); // Index source
  cx.evaluateString(scope, JavascriptRun.getCollateJS(), "collate.js", 1, null); // Collation
  mapFunction = cx.compileFunction(scope, mapTxt, "map", 1, null);
  // var index = new Index()
  indexResults = cx.newObject(scope, "Index");
  indexFunction = (Function) indexResults.getPrototype().get("indexDoc", indexResults);
  // var emit = index.emit
  Function emitFunc = (Function) indexResults.getPrototype().get("emit", indexResults);
  emitFunc = new BoundFunction(cx, scope, emitFunc, indexResults, NO_ARGS);
  scope.put("emit", scope, emitFunc);
}

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

private Object js_construct(Scriptable scope, Object[] args)
{
  ImporterTopLevel result = new ImporterTopLevel();
  for (int i = 0; i != args.length; ++i) {
    Object arg = args[i];
    if (arg instanceof NativeJavaClass) {
      result.importClass((NativeJavaClass)arg);
    } else if (arg instanceof NativeJavaPackage) {
      result.importPackage((NativeJavaPackage)arg);
    } else {
      throw Context.reportRuntimeError1(
        "msg.not.class.not.pkg", Context.toString(arg));
    }
  }
  // set explicitly prototype and scope
  // as otherwise in top scope mode BaseFunction.construct
  // would keep them set to null. It also allow to use
  // JavaImporter without new and still get properly
  // initialized object.
  result.setParentScope(scope);
  result.setPrototype(this);
  return result;
}

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

topLevel = new ImporterTopLevel(cx, false);
new LazilyLoadedCtor(topLevel, "JSAdapter",
  "com.sun.phobos.script.javascript.JSAdapter",
cx.exit();

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

Scriptable scope = new ImporterTopLevel(cx);
  scope.put("parent", scope, parentContainer);
  scope.put("assemblyScope", scope, assemblyScope);
  cx.evaluateReader(scope, getScriptReader(), "picocontainer.js", 1, null);
  Object pico = scope.get("pico", scope);
  throw new ScriptedPicoContainerMarkupException("IOException encountered, message -'" + e.getMessage() + "'", e);
} finally {
  Context.exit();

代码示例来源:origin: com.couchbase.mock/CouchbaseMock

private Reducer(String reduceTxt, Context cx) {
  Scriptable scope = new ImporterTopLevel(cx);
  cx.evaluateString(scope, REDUCE_JS, "reduce.js", 1, null);
  Scriptable builtins = (Scriptable) scope.get("BUILTIN_REDUCERS", scope);
  if (builtins.has(reduceTxt, builtins)) {
    reduceFunc = (Function)builtins.get(reduceTxt, builtins);
  } else {
    reduceFunc = cx.compileFunction(scope, reduceTxt, "reduce", 1, null);
  }
}

代码示例来源:origin: rhino/js

private Object js_importPackage(Object[] args)
{
  for (int i = 0; i != args.length; i++) {
    Object arg = args[i];
    if (!(arg instanceof NativeJavaPackage)) {
      throw Context.reportRuntimeError1(
        "msg.not.pkg", Context.toString(arg));
    }
    importPackage((NativeJavaPackage)arg);
  }
  return Undefined.instance;
}

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

private Object js_importClass(Object[] args)
{
  for (int i = 0; i != args.length; i++) {
    Object arg = args[i];
    if (!(arg instanceof NativeJavaClass)) {
      throw Context.reportRuntimeError1(
        "msg.not.class", Context.toString(arg));
    }
    importClass((NativeJavaClass)arg);
  }
  return Undefined.instance;
}

代码示例来源: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
}

代码示例来源:origin: rhino/js

public static void init(Context cx, Scriptable scope, boolean sealed)
{
  ImporterTopLevel obj = new ImporterTopLevel();
  obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
}

相关文章