Package org.mozilla.javascript.commonjs.module

Examples of org.mozilla.javascript.commonjs.module.ModuleScope


    final ScriptableObject scope = context.initStandardObjects();
    final Require require = new Require(Context.getCurrentContext(), scope,
        getModuleScriptProvider(clazz), null, null, false);
    require.install(scope);
    try {
      this.moduleScope = new ModuleScope(scope, new URI("./" + this.name), null);
    } catch (final URISyntaxException e) {
      throw new SmallerException("Failed to create moduleScope", e);
    }
    addGlobalFunction("print", LOGGER, "info");
  }
View Full Code Here


                throw ScriptRuntime.throwError(cx, scope,
                        "Can't resolve relative module ID \"" + id +
                                "\" when require() is used outside of a module");
            }

            ModuleScope moduleScope = (ModuleScope) thisObj;
            base = moduleScope.getBase();
            URI current = moduleScope.getUri();
            uri = current.resolve(id);

            if (base == null) {
                // calling module is absolute, resolve to absolute URI
                // (but without file extension)
View Full Code Here

        defineReadOnlyProperty(moduleObject, "require", this);
       
        if(!sandboxed) {
            defineReadOnlyProperty(moduleObject, "uri", uri.toString());
        }
        final Scriptable executionScope = new ModuleScope(nativeScope, uri, base);
        // Set this so it can access the global JS environment objects.
        // This means we're currently using the "MGN" approach (ModuleScript
        // with Global Natives) as specified here:
        // <http://wiki.commonjs.org/wiki/Modules/ProposalForNativeExtension>
        executionScope.put("exports", executionScope, exports);
        executionScope.put("module", executionScope, moduleObject);
        moduleObject.put("exports", moduleObject, exports);
        install(executionScope);
        if(isMain) {
            defineReadOnlyProperty(this, "main", moduleObject);
        }
View Full Code Here

                Scriptable extensions = ScriptableObject.getTypedProperty(nodeRequire, "extensions", Scriptable.class);
                BaseFunction myExtensionCallback = new BaseFunction() {
                };
                ScriptableObject.putProperty(extensions, ".pose", myExtensionCallback);

                ModuleScope moduleScope = mock(ModuleScope.class);
                when(moduleScope.getUri()).thenReturn(file.getParentFile().toURI());
                when(moduleScope.getBase()).thenReturn(file.getParentFile().toURI());
                NodeRequire.TryExtensionsResult extensionFound =
                        nodeRequire.tryExtensions(FilenameUtils.concat(file.getParent(),
                                FilenameUtils.getBaseName(file.getAbsolutePath())), moduleScope);

                assertNotNull(extensionFound);
View Full Code Here

            ScriptableObject.putProperty(thisObj, "_compile", compile);
            return super.call(cx, scope, thisObj, args);
        } catch (JavaScriptException e) {
            if ( thisObj instanceof ModuleScope) {
                String id = (String)Context.jsToJava(args[0], String.class);
                ModuleScope moduleScope = (ModuleScope) thisObj;
                URI base = moduleScope.getBase();
                URI current = moduleScope.getUri();
                URI uri = current.resolve(id  + ".js/");

                if (!id.startsWith("./") && !id.startsWith("../") && base != null &&
                        new File(uri.getPath()).exists()) {
                    // try to convert to a relative URI rooted on base
View Full Code Here

        return cwdFile;
    }

    private File getBasePathForModule(ModuleScope thisObj) {
        ModuleScope moduleScope = thisObj;
        if (moduleScope == null) {
            return null;
        }
        URI base = moduleScope.getUri();
        File cwdFile = new File(base.getPath());
        if ( !cwdFile.isDirectory() ) {
            cwdFile = cwdFile.getParentFile();
        }
        return cwdFile;
View Full Code Here

                    }
                } else {
                    uri = new File(path).toURI();
                }
            }
            return new ModuleScope(global, uri, null);
        } else {
            return global;
        }
    }
View Full Code Here

    return null;
  }

  private URI getCurrentModuleURI(){
    ModuleScope currentScope = NodeRequire.moduleScope.get();
    if(currentScope == null) {
      return new File(".").toURI().resolve("");
    }
    return currentScope.getUri();
  }
View Full Code Here

  // Store current moduleScope
  public Object call(Context cx, Scriptable scope, Scriptable thisObj,
                     Object[] args)
  {

    ModuleScope mScope = (ModuleScope) thisObj;
    moduleScope.set(mScope);

    return super.call(cx, scope, thisObj, args);
  }
View Full Code Here

TOP

Related Classes of org.mozilla.javascript.commonjs.module.ModuleScope

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.