Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.dynjs.runtime.builtins;
 
 
public class ClasspathModuleProvider implements ModuleProvider {
    private DynObject module;
    private DynObject exports;
    @Override
    public Object load(ExecutionContext contextString moduleName) {
        if (moduleName == null) {
            return null;
        }
        DynJS runtime = context.getGlobalObject().getRuntime();
        ExecutionContext requireContext = ExecutionContext.createGlobalExecutionContext(runtime);
        GlobalObject requireGlobal = requireContext.getGlobalObject();
        this. = new DynObject(requireGlobal);
        this. = new DynObject(requireGlobal);
        .put(null"exports"true);
        requireGlobal.put(requireContext"module"true);
        requireGlobal.put(requireContext"exports"true);
        
        String originalName = moduleName;
        moduleName = normalizeName(moduleName);
//        System.err.println("Looking for: " + moduleName);
        ClassLoader classLoader = requireGlobal.getConfig().getClassLoader();
        try {
            InputStream is = classLoader.getResourceAsStream(moduleName);
            if (is == null) {
                is = classLoader.getResourceAsStream(originalName + "/index.js");
                if (is == null) {
                    throw new FileNotFoundException("Cannot find module: " + moduleName);
                }
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            requireGlobal.getRuntime().newRunner().withContext(requireContext).withSource(reader).execute();
            try {
                is.close();
            } catch (IOException ignore) {
            }
            return .get(requireContext"exports");
        }
        catch(FileNotFoundException e) {
            ..println("Module not found: " + moduleName);
        }
        return null;
    }
    
    private String normalizeName(String originalName) {
        if (originalName.endsWith(".js")) {
            return originalName;
        }
        return originalName + ".js";
    }
    
New to GrepCode? Check out our FAQ X