ECMAScript 4 Netscape Proposal
Core Language
Packages
|
Wednesday, June 4, 2003
Packages were originally part of the ECMAScript Edition 4 proposal but have been removed due to time constraints. If implemented, packages and import directives might be defined as described below.
Packages are an abstraction mechanism for grouping and distributing related code. Packages are designed to be linked at run time to allow a program to take advantage of packages written elsewhere or provided by the embedding environment. ECMAScript 4 offers a number of facilities to make packages robust for dynamic linking:
A package is defined using the following syntax:
When a package is defined, it may, but is not required to, be given a PackageName, which is either a string or a series of dot-separated identifiers. It is implementation-defined what the restrictions, if any, are on naming packages to avoid clashes with other packages that may be present.
The Block contains the body of a package P. The Block
is evaluated at the time package P is loaded. Any public
top-level definitions are available to other
packages that import
package P. Any public
class member definitions are available to
all other packages, regardless of whether they import
package P. Top-level and class definitions defined
by P in another namespace N are available to other packages only if they use
namespace
N or qualify the access with namespace N.
A package is loaded (its body is evaluated) when the package is first imported or invoked directly (if, for example, the
package is on an HTML web page). Some standard packages are loaded when the ECMAScript engine first starts up. When a package
is loaded, its statements are evaluated in order, which may cause other packages to be loaded along the way when import
directives are encountered. Circularities are not allowed in the graph of package imports.
Two attempts to load the same package in the same environment result in sharing of that package. What constitutes an environment is necessarily application-dependent. However, if package P1 loads packages P2 and P3, both of which load package P4, then P4 is loaded only once and thereafter its code and data is shared by P2 and P3.
Javascript does not support package definition circularities (two packages A and B that each import the other), although an implementation may provide such a facility as an extension.
A package P can reference another package Q via an import
directive:
An import
directive may be preceded by attributes; however, all such
attributes must evaluate to either true
or false
.
There are two ways an import
directive can name a package to be imported:
An import
directive does the following:
const
-bind it to
P in the current scope.explicit
top-level definition N::
n (n in namespace
N) in P, bind an alias N::
n to P’s N::
n
in the global scope unless N::
n is already defined in the global scope.If package P has a top-level definition n and package Q imports P using import PkgP =
P,
then package Q can refer to n as either n or PkgP.
n. The shorter
form n is not available if it conflicts with some other n. If package P has an explicit
top-level definition n and package Q imports P, then package Q can refer to that
n only as PkgP.
n.
Waldemar Horwat Last modified Wednesday, June 4, 2003 |