Пример #1
0
 /**
  * Invoke a script from the command line.
  *
  * @param scriptResource the script resource of path
  * @param scriptArgs an array of command line arguments
  * @return the return value
  * @throws IOException an I/O related error occurred
  * @throws JavaScriptException the script threw an error during compilation or execution
  */
 public Object runScript(Object scriptResource, String... scriptArgs)
     throws IOException, JavaScriptException {
   Resource resource;
   if (scriptResource instanceof Resource) {
     resource = (Resource) scriptResource;
   } else if (scriptResource instanceof String) {
     resource = findResource((String) scriptResource, null, null);
   } else {
     throw new IOException("Unsupported script resource: " + scriptResource);
   }
   if (!resource.exists()) {
     throw new FileNotFoundException(scriptResource.toString());
   }
   Context cx = contextFactory.enterContext();
   try {
     Object retval;
     Map<Trackable, ReloadableScript> scripts = getScriptCache(cx);
     commandLineArgs = Arrays.asList(scriptArgs);
     ReloadableScript script = new ReloadableScript(resource, this);
     scripts.put(resource, script);
     mainScope = new ModuleScope(resource.getModuleName(), resource, globalScope, mainWorker);
     retval = mainWorker.evaluateScript(cx, script, mainScope);
     mainScope.updateExports();
     return retval instanceof Wrapper ? ((Wrapper) retval).unwrap() : retval;
   } finally {
     Context.exit();
   }
 }
Пример #2
0
 Singleton getSingleton(Singleton singleton) {
   synchronized (singletons) {
     Singleton st = singletons.get(singleton);
     if (st == null) {
       st = singleton;
       singletons.put(singleton, singleton);
     }
     return st;
   }
 }
Пример #3
0
 /**
  * Resolves a type name to a script file within our script directory and returns a Scriptable
  * evaluated to the file.
  *
  * @param moduleName the name of the module to load
  * @param localPath the path of the resource issuing this call
  * @return The raw compiled script for the module
  * @throws JavaScriptException if an error occurred evaluating the script file
  * @throws IOException if an error occurred reading the script file
  */
 public ReloadableScript getScript(String moduleName, Repository localPath)
     throws JavaScriptException, IOException {
   ReloadableScript script;
   Resource source = findResource(moduleName, loaders, localPath);
   if (!source.exists()) {
     source = loadPackage(moduleName, localPath);
     if (!source.exists()) {
       source = findResource(moduleName, null, localPath);
     }
   }
   Context cx = Context.getCurrentContext();
   Map<Trackable, ReloadableScript> scripts = getScriptCache(cx);
   if (scripts.containsKey(source)) {
     script = scripts.get(source);
   } else {
     script = new ReloadableScript(source, this);
     if (source.exists()) {
       scripts.put(source, script);
     }
   }
   return script;
 }