Пример #1
0
  public static ScriptableObject require(
      Context cx, Scriptable thisObj, Object[] args, Function funObj)
      throws ScriptException, IOException {
    String functionName = "require";
    int argsCount = args.length;
    if (argsCount != 1) {
      HostObjectUtil.invalidNumberOfArgs(HOST_OBJECT_NAME, functionName, argsCount, false);
    }
    if (!(args[0] instanceof String)) {
      HostObjectUtil.invalidArgsError(
          HOST_OBJECT_NAME, functionName, "1", "string", args[0], false);
    }
    String moduleName = (String) args[0];
    JaggeryContext context = getJaggeryContext();
    // RhinoEngine engine = context.getEngine();
    // ScriptableObject scope = context.getScope();
    CommonManager manager = (CommonManager) context.getProperty(Constants.JAGGERY_CORE_MANAGER);
    ModuleManager moduleManager = manager.getModuleManager();
    JavaScriptModule module = moduleManager.getModule(moduleName);

    if (module == null) {
      String msg = "A module cannot be found with the specified name : " + moduleName;
      log.error(msg);
      throw new ScriptException(msg);
    }
    ScriptableObject object = (ScriptableObject) cx.newObject(thisObj);
    object.setPrototype(thisObj);
    object.setParentScope(thisObj);
    exposeModule(cx, object, module);
    return object;
  }
Пример #2
0
 private static void exposeDefaultModules(
     RhinoEngine engine, Map<String, JavaScriptModule> modules) throws ScriptException {
   for (JavaScriptModule module : modules.values()) {
     if (module.isExpose()) {
       String namespace = module.getNamespace();
       if (namespace == null || namespace.equals("")) {
         // expose globally
         exposeModule(engine, module);
       } else {
         engine.defineModule(module);
       }
     }
   }
 }