/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.javascript.commonjs.module;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.mozilla.javascript.BaseFunction;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
/**
* Implements the require() function as defined by
* Common JS modules.
*
Thread safety
* You will ordinarily create one instance of require() for every top-level
* scope. This ordinarily means one instance per program execution, except if
* you use shared top-level scopes and installing most objects into them.
* Module loading is thread safe, so using a single require() in a shared
* top-level scope is also safe.
* Creation
* If you need to create many otherwise identical require() functions for
* different scopes, you might want to use {@link RequireBuilder} for
* convenience.
* Making it available
* In order to make the require() function available to your JavaScript
* program, you need to invoke either {@link #install(Scriptable)} or
* {@link #requireMain(Context, String)}.
* @author Attila Szegedi
* @version $Id: Require.java,v 1.4 2011/04/07 20:26:11 hannes%helma.at Exp $
*/
public class Require extends BaseFunction
{
private static final long serialVersionUID = 1L;
private final ModuleScriptProvider moduleScriptProvider;
private final Scriptable nativeScope;
private final Scriptable paths;
private final boolean sandboxed;
private final Script preExec;
private final Script postExec;
private String mainModuleId = null;
private Scriptable mainExports;
// Modules that completed loading; visible to all threads
private final Map exportedModuleInterfaces =
new ConcurrentHashMap();
private final Object loadLock = new Object();
// Modules currently being loaded on the thread. Used to resolve circular
// dependencies while loading.
private static final ThreadLocal