- All Implemented Interfaces:
Serializable,Callable,ConstProperties<Scriptable>,Constructable,DebuggableObject,Function,PropHolder<Scriptable>,Scriptable,SymbolScriptable
<h1>Thread safety</h1>
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.
<h1>Creation</h1>
If you need to create many otherwise identical require() functions for different scopes, you
might want to use RequireBuilder for convenience.
<h1>Making it available</h1>
In order to make the require() function available to your JavaScript program, you need to
invoke either install(VarScope) or requireMain(Context, String).
- Version:
- $Id: Require.java,v 1.4 2011/04/07 20:26:11 hannes%helma.at Exp $
- Author:
- Attila Szegedi
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.htmlunit.corejs.javascript.ScriptableObject
ScriptableObject.DescriptorInfo, ScriptableObject.KeyComparator, ScriptableObject.LambdaGetterFunction, ScriptableObject.LambdaSetterFunction -
Field Summary
Fields inherited from class org.htmlunit.corejs.javascript.BaseFunction
Id_arguments, Id_arity, Id_length, Id_name, Id_prototype, MAX_INSTANCE_IDFields inherited from class org.htmlunit.corejs.javascript.ScriptableObject
CONST, DONTENUM, EMPTY, LEGACY_CONST, PERMANENT, READONLY, STRICTLY_READONLY, UNINITIALIZED_CONSTFields inherited from class org.htmlunit.corejs.javascript.SlotMapOwner
isSealedFields inherited from interface org.htmlunit.corejs.javascript.Scriptable
NOT_FOUND -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCall the function as a constructor.intgetArity()intvoidBinds this instance of require() into the specified scope under the property name "require".requireMain(Context cx, String mainModuleId) Calling this method establishes a module as being the main module of the program to which this require() instance belongs.Methods inherited from class org.htmlunit.corejs.javascript.BaseFunction
call, createObject, createObject, createProperties, createPrototypeProperty, createPrototypeProperty, defaultGet, defaultHas, defaultPut, getClassName, getClassPrototype, getDeclarationScope, getHomeObject, getPrototypeProperty, getTypeOf, hasDefaultParameters, hasInstance, hasPrototypeProperty, isConstructor, isGeneratorFunction, prototypeDescSetter, setHomeObject, setImmunePrototypeProperty, setPrototypeProperty, setPrototypePropertyAttributes, setStandardPropertyAttributes, setupDefaultPrototypeMethods inherited from class org.htmlunit.corejs.javascript.ScriptableObject
applyDescriptorToAttributeBitset, avoidObjectDetection, buildDataDescriptor, callMethod, callMethod, checkNotSealed, checkPropertyChangeForSlot, checkPropertyChangeForSlot, checkPropertyDefinition, checkPropertyDefinition, checkSlotRemoval, defineBuiltinProperty, defineBuiltinProperty, defineBuiltInProperty, defineClass, defineClass, defineClass, defineConst, defineConstProperty, defineFunctionProperties, defineOwnProperties, defineOwnProperty, defineOwnProperty, defineOwnProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, delete, delete, delete, deleteProperty, deleteProperty, deleteProperty, ensureScriptable, ensureScriptableObject, ensureScriptableObjectButNotSymbol, ensureSymbolScriptable, ensureType, equivalentValues, get, get, getAllIds, getAncestor, getArrayPrototype, getClassPrototype, getClassPrototype, getDefaultValue, getDefaultValue, getExternalArrayData, getExternalArrayLength, getFunctionPrototype, getGeneratorFunctionPrototype, getGetterOrSetter, getGetterOrSetter, getObjectPrototype, getOwnPropertyDescriptor, getParentScope, getProperty, getProperty, getProperty, getProperty, getProperty, getPropertyIds, getPrototype, getSuperProperty, getSuperProperty, getSuperProperty, getThis, getTopLevelScope, getTopScopeValue, getTypedProperty, getTypedProperty, has, hasProperty, hasProperty, hasProperty, hasProperty, isAccessorDescriptor, isAccessorDescriptor, isConst, isDataDescriptor, isEmpty, isExtensible, isFalse, isGenericDescriptor, isGenericDescriptor, isGetterOrSetter, isGetterOrSetter, isTrue, preventExtensions, put, put, put, putConst, putConstProperty, putConstProperty, putOwnProperty, putOwnProperty, putOwnProperty, putProperty, putProperty, putProperty, putProperty, putSuperProperty, putSuperProperty, putSuperProperty, querySlot, redefineProperty, redefineProperty, sameValue, sealObject, setCommonDescriptorProperties, setExternalArrayData, setGetterOrSetter, setParentScope, setPrototype, sizeMethods inherited from class org.htmlunit.corejs.javascript.SlotMapOwner
associateValue, checkNotSealed, copyAssociatedValue, createSlotMap, createSlotMap, defineProperty, defineProperty, get, get, getAssociatedValue, getAttributes, getAttributes, getAttributes, getAttributes, getAttributes, getIds, has, has, isSealed, readMaps, setAttributes, setAttributes, setAttributes, setAttributes, setAttributes, writeMapsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.htmlunit.corejs.javascript.Constructable
constructMethods inherited from interface org.htmlunit.corejs.javascript.Scriptable
delete, delete, delete, get, get, get, getAncestor, getDefaultValue, getIds, getParentScope, getPrototype, has, has, has, put, put, put, setParentScope, setPrototypeMethods inherited from interface org.htmlunit.corejs.javascript.SymbolScriptable
get, has
-
Constructor Details
-
Require
public Require(Context cx, TopLevel nativeScope, ModuleScriptProvider moduleScriptProvider, Script preExec, Script postExec, boolean sandboxed) Creates a new instance of the require() function. Upon constructing it, you will either want to install it in the global (or some other) scope usinginstall(VarScope), or alternatively, you can load the program's main module usingrequireMain(Context, String)and then act on the main module's exports.- Parameters:
cx- the current contextnativeScope- a scope that provides the standard native JavaScript objects.moduleScriptProvider- a provider for module scriptspreExec- an optional script that is executed in every module's scope before its module script is run.postExec- an optional script that is executed in every module's scope after its module script is run.sandboxed- if set to true, the require function will be sandboxed. This means that it doesn't have the "paths" property, and also that the modules it loads don't export the "module.uri" property.
-
-
Method Details
-
requireMain
Calling this method establishes a module as being the main module of the program to which this require() instance belongs. The module will be loaded as if require()'d and its "module" property will be set as the "main" property of this require() instance. You have to call this method before the module has been loaded (that is, the call to this method must be the first to require the module and thus trigger its loading). Note that the main module will execute in its own scope and not in the global scope. Since all other modules see the global scope, executing the main module in the global scope would open it for tampering by other modules.- Parameters:
cx- the current contextmainModuleId- the ID of the main module- Returns:
- the "exports" property of the main module
- Throws:
IllegalStateException- if the main module is already loaded when required, or if this require() instance already has a different main module set.
-
install
Binds this instance of require() into the specified scope under the property name "require".- Parameters:
scope- the scope where the require() function is to be installed.
-
call
- Overrides:
callin classBaseFunction
-
construct
Description copied from interface:FunctionCall the function as a constructor.This method is invoked by the runtime in order to satisfy a use of the JavaScript
newoperator. This method is expected to create a new object and return it.If nt is equal to this then this call is equivalent `new f(...arguments), but reflection and super constructor calls may pass in different values.
- Specified by:
constructin interfaceConstructable- Specified by:
constructin interfaceFunction- Overrides:
constructin classBaseFunction- Parameters:
cx- the current Context for this threadnt- the new.target for this call,scope- an enclosing scope of the caller except when the function is called from a closure.args- the array of arguments- Returns:
- the allocated object
-
getFunctionName
- Overrides:
getFunctionNamein classBaseFunction
-
getArity
public int getArity()- Overrides:
getArityin classBaseFunction
-
getLength
public int getLength()- Overrides:
getLengthin classBaseFunction
-