Java Code Examples for org.mozilla.javascript.ScriptRuntime#throwError()

The following examples show how to use org.mozilla.javascript.ScriptRuntime#throwError() . 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: Require.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
        Object[] args)
{
    if(args == null || args.length < 1) {
        throw ScriptRuntime.throwError(cx, scope,
                "require() needs one argument");
    }

    String id = (String)Context.jsToJava(args[0], String.class);
    URI uri = null;
    URI base = null;
    if (id.startsWith("./") || id.startsWith("../")) {
        if (!(thisObj instanceof ModuleScope)) {
            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)
            id = uri.toString();
        } else {
            // try to convert to a relative URI rooted on base
            id = base.relativize(current).resolve(id).toString();
            if (id.charAt(0) == '.') {
                // resulting URI is not contained in base,
                // throw error or make absolute depending on sandbox flag.
                if (sandboxed) {
                    throw ScriptRuntime.throwError(cx, scope,
                        "Module \"" + id + "\" is not contained in sandbox.");
                } else {
                    id = uri.toString();
                }
            }
        }
    }
    return getExportedModuleInterface(cx, id, uri, base, false);
}
 
Example 2
Source File: Require.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
    throw ScriptRuntime.throwError(cx, scope,
            "require() can not be invoked as a constructor");
}
 
Example 3
Source File: Require.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
private Scriptable getExportedModuleInterface(Context cx, String id,
        URI uri, URI base, boolean isMain)
{
    // Check if the requested module is already completely loaded
    Scriptable exports = exportedModuleInterfaces.get(id);
    if(exports != null) {
        if(isMain) {
            throw new IllegalStateException(
                    "Attempt to set main module after it was loaded");
        }
        return exports;
    }
    // Check if it is currently being loaded on the current thread
    // (supporting circular dependencies).
    Map<String, Scriptable> threadLoadingModules =
        loadingModuleInterfaces.get();
    if(threadLoadingModules != null) {
        exports = threadLoadingModules.get(id);
        if(exports != null) {
            return exports;
        }
    }
    // The requested module is neither already loaded, nor is it being
    // loaded on the current thread. End of fast path. We must synchronize
    // now, as we have to guarantee that at most one thread can load
    // modules at any one time. Otherwise, two threads could end up
    // attempting to load two circularly dependent modules in opposite
    // order, which would lead to either unacceptable non-determinism or
    // deadlock, depending on whether we underprotected or overprotected it
    // with locks.
    synchronized(loadLock) {
        // Recheck if it is already loaded - other thread might've
        // completed loading it just as we entered the synchronized block.
        exports = exportedModuleInterfaces.get(id);
        if(exports != null) {
            return exports;
        }
        // Nope, still not loaded; we're loading it then.
        final ModuleScript moduleScript = getModule(cx, id, uri, base);
        if (sandboxed && !moduleScript.isSandboxed()) {
            throw ScriptRuntime.throwError(cx, nativeScope, "Module \""
                    + id + "\" is not contained in sandbox.");
        }
        exports = cx.newObject(nativeScope);
        // Are we the outermost locked invocation on this thread?
        final boolean outermostLocked = threadLoadingModules == null;
        if(outermostLocked) {
            threadLoadingModules = new HashMap<String, Scriptable>();
            loadingModuleInterfaces.set(threadLoadingModules);
        }
        // Must make the module exports available immediately on the
        // current thread, to satisfy the CommonJS Modules/1.1 requirement
        // that "If there is a dependency cycle, the foreign module may not
        // have finished executing at the time it is required by one of its
        // transitive dependencies; in this case, the object returned by
        // "require" must contain at least the exports that the foreign
        // module has prepared before the call to require that led to the
        // current module's execution."
        threadLoadingModules.put(id, exports);
        try {
            // Support non-standard Node.js feature to allow modules to
            // replace the exports object by setting module.exports.
            Scriptable newExports = executeModuleScript(cx, id, exports,
                    moduleScript, isMain);
            if (exports != newExports) {
                threadLoadingModules.put(id, newExports);
                exports = newExports;
            }
        }
        catch(RuntimeException e) {
            // Throw loaded module away if there was an exception
            threadLoadingModules.remove(id);
            throw e;
        }
        finally {
            if(outermostLocked) {
                // Make loaded modules visible to other threads only after
                // the topmost triggering load has completed. This strategy
                // (compared to the one where we'd make each module
                // globally available as soon as it loads) prevents other
                // threads from observing a partially loaded circular
                // dependency of a module that completed loading.
                exportedModuleInterfaces.putAll(threadLoadingModules);
                loadingModuleInterfaces.set(null);
            }
        }
    }
    return exports;
}
 
Example 4
Source File: Issue176Test.java    From rhino-android with Apache License 2.0 4 votes vote down vote up
public void throwError(String msg) {
    throw ScriptRuntime.throwError(cx, scope, msg);
}
 
Example 5
Source File: Require.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
        Object[] args)
{
    if(args == null || args.length < 1) {
        throw ScriptRuntime.throwError(cx, scope,
                "require() needs one argument");
    }

    String id = (String)Context.jsToJava(args[0], String.class);
    URI uri = null;
    URI base = null;
    if (id.startsWith("./") || id.startsWith("../")) {
        if (!(thisObj instanceof ModuleScope)) {
            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)
            id = uri.toString();
        } else {
            // try to convert to a relative URI rooted on base
            id = base.relativize(current).resolve(id).toString();
            if (id.charAt(0) == '.') {
                // resulting URI is not contained in base,
                // throw error or make absolute depending on sandbox flag.
                if (sandboxed) {
                    throw ScriptRuntime.throwError(cx, scope,
                        "Module \"" + id + "\" is not contained in sandbox.");
                } else {
                    id = uri.toString();
                }
            }
        }
    }
    return getExportedModuleInterface(cx, id, uri, base, false);
}
 
Example 6
Source File: Require.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
    throw ScriptRuntime.throwError(cx, scope,
            "require() can not be invoked as a constructor");
}
 
Example 7
Source File: Require.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private Scriptable getExportedModuleInterface(Context cx, String id,
        URI uri, URI base, boolean isMain)
{
    // Check if the requested module is already completely loaded
    Scriptable exports = exportedModuleInterfaces.get(id);
    if(exports != null) {
        if(isMain) {
            throw new IllegalStateException(
                    "Attempt to set main module after it was loaded");
        }
        return exports;
    }
    // Check if it is currently being loaded on the current thread
    // (supporting circular dependencies).
    Map<String, Scriptable> threadLoadingModules =
        loadingModuleInterfaces.get();
    if(threadLoadingModules != null) {
        exports = threadLoadingModules.get(id);
        if(exports != null) {
            return exports;
        }
    }
    // The requested module is neither already loaded, nor is it being
    // loaded on the current thread. End of fast path. We must synchronize
    // now, as we have to guarantee that at most one thread can load
    // modules at any one time. Otherwise, two threads could end up
    // attempting to load two circularly dependent modules in opposite
    // order, which would lead to either unacceptable non-determinism or
    // deadlock, depending on whether we underprotected or overprotected it
    // with locks.
    synchronized(loadLock) {
        // Recheck if it is already loaded - other thread might've
        // completed loading it just as we entered the synchronized block.
        exports = exportedModuleInterfaces.get(id);
        if(exports != null) {
            return exports;
        }
        // Nope, still not loaded; we're loading it then.
        final ModuleScript moduleScript = getModule(cx, id, uri, base);
        if (sandboxed && !moduleScript.isSandboxed()) {
            throw ScriptRuntime.throwError(cx, nativeScope, "Module \""
                    + id + "\" is not contained in sandbox.");
        }
        exports = cx.newObject(nativeScope);
        // Are we the outermost locked invocation on this thread?
        final boolean outermostLocked = threadLoadingModules == null;
        if(outermostLocked) {
            threadLoadingModules = new HashMap<String, Scriptable>();
            loadingModuleInterfaces.set(threadLoadingModules);
        }
        // Must make the module exports available immediately on the
        // current thread, to satisfy the CommonJS Modules/1.1 requirement
        // that "If there is a dependency cycle, the foreign module may not
        // have finished executing at the time it is required by one of its
        // transitive dependencies; in this case, the object returned by
        // "require" must contain at least the exports that the foreign
        // module has prepared before the call to require that led to the
        // current module's execution."
        threadLoadingModules.put(id, exports);
        try {
            // Support non-standard Node.js feature to allow modules to
            // replace the exports object by setting module.exports.
            Scriptable newExports = executeModuleScript(cx, id, exports,
                    moduleScript, isMain);
            if (exports != newExports) {
                threadLoadingModules.put(id, newExports);
                exports = newExports;
            }
        }
        catch(RuntimeException e) {
            // Throw loaded module away if there was an exception
            threadLoadingModules.remove(id);
            throw e;
        }
        finally {
            if(outermostLocked) {
                // Make loaded modules visible to other threads only after
                // the topmost triggering load has completed. This strategy
                // (compared to the one where we'd make each module
                // globally available as soon as it loads) prevents other
                // threads from observing a partially loaded circular
                // dependency of a module that completed loading.
                exportedModuleInterfaces.putAll(threadLoadingModules);
                loadingModuleInterfaces.set(null);
            }
        }
    }
    return exports;
}