ECMAScript 4 Netscape Proposal
Core Language
Namespaces
previousupnext

Wednesday, September 4, 2002

Namespace Definition

NamespaceDefinition  namespace Identifier

A namespace definition defines a new namespace named Identifier.

A namespace definition may only be located at a scope that allows class definitions. If a namespace is defined as a member of a class, then the namespace must be declared static.

Use Directive

UseDirective  use namespace ParenListExpression

A use namespace directive makes the contents of each namespace in the comma-separated list ParenListExpression accessible without a qualifier.

use namespace directives are lexically scoped and their effect does not extend past the end of the enclosing block, directive group, or substatement group. A use namespace directive may be preceded by attributes; however, all such attributes must evaluate to either true or false.

Name Lookup

The following paragraphs describe what happens when a name is looked up. See also the description of how namespace attributes affect name definitions.

Properties

Conceptually, an instance x is a collection of properties. All properties have property names q::nC, where q is a namespace, n is an identifier, and C is a class. There may be several aliases that refer to the same property (due to either multiple namespace attributes or aliases introduced with the export definition), but a property name q::nC can refer to at most one property of an instance.

An instance x can have several properties q::nC with the same namespace q and name n but different classes C. In the following descriptions, q::n denotes the most derived of these properties, which is the one with the most derived class C.

A property reference can be either unqualified or qualified and is looked up according to the table below. There are two entries in the table for each kind of lookup, depending on whether the left operand of the . operator is a SuperExpression or not. x is an expression that evaluates to an instance, is the set of all scopes enclosing the property reference, and Q is the set of all namespaces q that are used by the scopes in .

Qualified reference x.q::n where q is a namespace Select x’s most derived property q::n. Signal an error if there is no such property.
Qualified reference super x.q::n where q is a namespace This form may only be used for references in the scope of a class C other than Object. Let S be C’s superclass. Among all of x’s properties q::nA select the one whose class A is most derived but still either S or an ancestor of S. Signal an error if there is no such property.
Unqualified reference x.n Let A be the least derived (closest to Object) class such that x contains at least one property named q::nA where q is any element of Q; signal an error if x has no such properties. Let Q' be the set of all namespaces q such that q is in Q and x contains the property named q::nA. Let P be the set of all most derived properties q::n of x such that q is in Q'. If P has only one element p or if all of P’s elements are aliases of one property p, select p; otherwise signal an error.
Unqualified reference super x.n This form may only be used for references in the scope of a class C other than Object. Let S be C’s superclass. Let A be the least derived (closest to Object) class such that x contains at least one property named q::nA where q is any element of Q; signal an error if x has no such properties or if A is not S or an ancestor of S. Let Q' be the set of all namespaces q such that q is in Q and x contains the property named q::nA. For each q in Q' let Bq be the most derived class such that Bq is S or an ancestor of S and x contains the property q::nBq; for each q in Q' let pq be the property q::nBq. Let P be the set of all such properties pq. If P has only one element p or if all of P’s elements are aliases of one property p, select p; otherwise signal an error.
Dynamic reference x[s] Let s evaluate to a string n. Get the property x.public::n. Signal an error if there is no such property.
Dynamic reference super x[s] Let s evaluate to a string n. Get the property super x.public::n. Signal an error if there is no such property.

Note that the only way to access an overridden method is to use super. This is by design to prevent security attacks.

Variables

Conceptually, all variables (which for the purpose of this section also include constants, functions, classes, and such) have qualified names q::n, where q is a namespace and n an identifier. There may be several aliases that refer to the same variable (due to either multiple namespace attributes or aliases introduced with the export definition), but there cannot be two different variables defined using the same qualified name in the same scope.

A variable reference can be either unqualified or qualified and is looked up as follows:

Qualified reference q::n where q is a namespace

Let be the set of all scopes enclosing the qualified reference q::n. Search the scopes in , starting from the innermost one and continuing outwards until a value is found or all scopes have been examined. If no binding has been found after all scopes have been examined, signal an error. For each scope S in , do the following:

  1. If S’s activation frame currently has a binding for q::n, select the value to which q::n is bound.
  2. Otherwise, if S is the scope of the definition of a class C and C or any of its ancestors contains a global member named q::n, then select C’s most derived property q::n. Signal an error if there is no such property.
  3. Otherwise, if S is the top-level scope of a constructor or instance method defined in class C and C or any of its ancestors contains an instance member named q::n, then let this be the value of this corresponding to S’s activation frame and select this’s most derived property q::n. Signal an error if there is no such property.
  4. Otherwise, if S is the scope of the definition of a class C and C or any of its ancestors contains an instance member named q::n, then signal an error.
Unqualified reference n

Let be the set of all scopes enclosing the unqualified reference n. Search the scopes in , starting from the innermost one and continuing outwards until a value is found or all scopes have been examined. If no binding has been found after all scopes have been examined, signal an error. For each scope S in , do the following:

  1. If S’s activation frame A currently has a binding for a variable V, V has a name q::n for some namespace q that is used by a scope in , and that use includes n, then select the value to which q::n is bound in A. If A contains more than one such variable V (not counting aliases of the same variable), signal an error.
  2. Otherwise, if S is the scope of the definition of a class C, C or any of its ancestors contains a global member named q::n for some namespace q that is used by a scope in , then select C’s most derived property q::n. Signal an error if there is no such property or if there are multiple such properties (not counting aliases of the same property) for different q’s.
  3. Otherwise, if S is the top-level scope of a constructor or instance method defined in class C and C or any of its ancestors contains an instance member named q::n for some namespace q that is used by a scope in , then select this’s most derived property q::n using the value of this corresponding to S’s activation frame. Signal an error if there is no such property or if there are multiple such properties (not counting aliases of the same property) for different q’s.
  4. Otherwise, if S is the scope of the definition of a class C, C or any of its ancestors contains an instance member named q::n for some namespace q that is used by a scope in , then signal an error.

 


Waldemar Horwat
Last modified Wednesday, September 4, 2002
previousupnext