org.mozilla.javascript.commonjs.module.ModuleScript Java Examples

The following examples show how to use org.mozilla.javascript.commonjs.module.ModuleScript. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SoftCachingModuleScriptProvider.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public ModuleScript getModuleScript(Context cx, String moduleId,
        URI uri, URI base, Scriptable paths)
        throws Exception
{
    // Overridden to clear the reference queue before retrieving the
    // script.
    for(;;) {
        ScriptReference ref = (ScriptReference)scriptRefQueue.poll();
        if(ref == null) {
            break;
        }
        scripts.remove(ref.getModuleId(), ref);
    }
    return super.getModuleScript(cx, moduleId, uri, base, paths);
}
 
Example #2
Source File: SoftCachingModuleScriptProvider.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public ModuleScript getModuleScript(Context cx, String moduleId,
        URI uri, URI base, Scriptable paths)
        throws Exception
{
    // Overridden to clear the reference queue before retrieving the
    // script.
    for(;;) {
        ScriptReference ref = (ScriptReference)scriptRefQueue.poll();
        if(ref == null) {
            break;
        }
        scripts.remove(ref.getModuleId(), ref);
    }
    return super.getModuleScript(cx, moduleId, uri, base, paths);
}
 
Example #3
Source File: CachingModuleScriptProviderBase.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
public ModuleScript getModuleScript(Context cx, String moduleId,
        URI moduleUri, URI baseUri, Scriptable paths) throws Exception
{
    final CachedModuleScript cachedModule1 = getLoadedModule(moduleId);
    final Object validator1 = getValidator(cachedModule1);
    final ModuleSource moduleSource = (moduleUri == null)
            ? moduleSourceProvider.loadSource(moduleId, paths, validator1)
            : moduleSourceProvider.loadSource(moduleUri, baseUri, validator1);
    if(moduleSource == ModuleSourceProvider.NOT_MODIFIED) {
        return cachedModule1.getModule();
    }
    if(moduleSource == null) {
        return null;
    }
    final Reader reader = moduleSource.getReader();
    try {
        final int idHash = moduleId.hashCode();
        synchronized(loadLocks[(idHash >>> loadLockShift) & loadLockMask]) {
            final CachedModuleScript cachedModule2 = getLoadedModule(moduleId);
            if(cachedModule2 != null) {
                if(!equal(validator1, getValidator(cachedModule2))) {
                    return cachedModule2.getModule();
                }
            }
            final URI sourceUri = moduleSource.getUri();
            final ModuleScript moduleScript = new ModuleScript(
                    cx.compileReader(reader, sourceUri.toString(), 1,
                            moduleSource.getSecurityDomain()),
                    sourceUri, moduleSource.getBase());
            putLoadedModule(moduleId, moduleScript,
                    moduleSource.getValidator());
            return moduleScript;
        }
    }
    finally {
        reader.close();
    }
}
 
Example #4
Source File: SoftCachingModuleScriptProvider.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
CachedModuleScript getCachedModuleScript() {
    final Script script = get();
    if(script == null) {
        return null;
    }
    return new CachedModuleScript(new ModuleScript(script, uri, base),
            validator);
}
 
Example #5
Source File: SoftCachingModuleScriptProvider.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void putLoadedModule(String moduleId, ModuleScript moduleScript,
        Object validator)
{
    scripts.put(moduleId, new ScriptReference(moduleScript.getScript(),
            moduleId, moduleScript.getUri(), moduleScript.getBase(),
            validator, scriptRefQueue));
}
 
Example #6
Source File: CachingModuleScriptProviderBase.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public ModuleScript getModuleScript(Context cx, String moduleId,
        URI moduleUri, URI baseUri, Scriptable paths) throws Exception
{
    final CachedModuleScript cachedModule1 = getLoadedModule(moduleId);
    final Object validator1 = getValidator(cachedModule1);
    final ModuleSource moduleSource = (moduleUri == null)
            ? moduleSourceProvider.loadSource(moduleId, paths, validator1)
            : moduleSourceProvider.loadSource(moduleUri, baseUri, validator1);
    if(moduleSource == ModuleSourceProvider.NOT_MODIFIED) {
        return cachedModule1.getModule();
    }
    if(moduleSource == null) {
        return null;
    }
    final Reader reader = moduleSource.getReader();
    try {
        final int idHash = moduleId.hashCode();
        synchronized(loadLocks[(idHash >>> loadLockShift) & loadLockMask]) {
            final CachedModuleScript cachedModule2 = getLoadedModule(moduleId);
            if(cachedModule2 != null) {
                if(!equal(validator1, getValidator(cachedModule2))) {
                    return cachedModule2.getModule();
                }
            }
            final URI sourceUri = moduleSource.getUri();
            final ModuleScript moduleScript = new ModuleScript(
                    cx.compileReader(reader, sourceUri.toString(), 1,
                            moduleSource.getSecurityDomain()),
                    sourceUri, moduleSource.getBase());
            putLoadedModule(moduleId, moduleScript,
                    moduleSource.getValidator());
            return moduleScript;
        }
    }
    finally {
        reader.close();
    }
}
 
Example #7
Source File: MultiModuleScriptProvider.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public ModuleScript getModuleScript(Context cx, String moduleId, URI uri,
                                    URI base, Scriptable paths) throws Exception {
    for (ModuleScriptProvider provider : providers) {
        final ModuleScript script = provider.getModuleScript(cx, moduleId,
                uri, base, paths);
        if(script != null) {
            return script;
        }
    }
    return null;
}
 
Example #8
Source File: SoftCachingModuleScriptProvider.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
CachedModuleScript getCachedModuleScript() {
    final Script script = get();
    if(script == null) {
        return null;
    }
    return new CachedModuleScript(new ModuleScript(script, uri, base),
            validator);
}
 
Example #9
Source File: SoftCachingModuleScriptProvider.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
@Override
protected void putLoadedModule(String moduleId, ModuleScript moduleScript,
        Object validator)
{
    scripts.put(moduleId, new ScriptReference(moduleScript.getScript(),
            moduleId, moduleScript.getUri(), moduleScript.getBase(),
            validator, scriptRefQueue));
}
 
Example #10
Source File: MultiModuleScriptProvider.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
public ModuleScript getModuleScript(Context cx, String moduleId, URI uri,
                                    URI base, Scriptable paths) throws Exception {
    for (ModuleScriptProvider provider : providers) {
        final ModuleScript script = provider.getModuleScript(cx, moduleId,
                uri, base, paths);
        if(script != null) {
            return script;
        }
    }
    return null;
}
 
Example #11
Source File: StrongCachingModuleScriptProvider.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
@Override
protected void putLoadedModule(String moduleId, ModuleScript moduleScript,
        Object validator) {
    modules.put(moduleId, new CachedModuleScript(moduleScript, validator));
}
 
Example #12
Source File: StrongCachingModuleScriptProvider.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void putLoadedModule(String moduleId, ModuleScript moduleScript,
        Object validator) {
    modules.put(moduleId, new CachedModuleScript(moduleScript, validator));
}
 
Example #13
Source File: CachingModuleScriptProviderBase.java    From JsDroidCmd with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Returns the module script.
 * @return the module script.
 */
ModuleScript getModule() {
    return moduleScript;
}
 
Example #14
Source File: CachingModuleScriptProviderBase.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Store a loaded module script for later retrieval using
 * {@link #getLoadedModule(String)}.
 * @param moduleId the ID of the module
 * @param moduleScript the module script
 * @param validator the validator for the module's source text entity
 */
protected abstract void putLoadedModule(String moduleId,
        ModuleScript moduleScript, Object validator);
 
Example #15
Source File: CachingModuleScriptProviderBase.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Creates a new cached module script.
 * @param moduleScript the module script itself
 * @param validator a validator for the moduleScript's source text
 * entity.
 */
public CachedModuleScript(ModuleScript moduleScript, Object validator) {
    this.moduleScript = moduleScript;
    this.validator = validator;
}
 
Example #16
Source File: CachingModuleScriptProviderBase.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the module script.
 * @return the module script.
 */
ModuleScript getModule() {
    return moduleScript;
}
 
Example #17
Source File: CachingModuleScriptProviderBase.java    From JsDroidCmd with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Creates a new cached module script.
 * @param moduleScript the module script itself
 * @param validator a validator for the moduleScript's source text
 * entity.
 */
public CachedModuleScript(ModuleScript moduleScript, Object validator) {
    this.moduleScript = moduleScript;
    this.validator = validator;
}
 
Example #18
Source File: CachingModuleScriptProviderBase.java    From JsDroidCmd with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Store a loaded module script for later retrieval using
 * {@link #getLoadedModule(String)}.
 * @param moduleId the ID of the module
 * @param moduleScript the module script
 * @param validator the validator for the module's source text entity
 */
protected abstract void putLoadedModule(String moduleId,
        ModuleScript moduleScript, Object validator);