Namespaces in ECMA
On 7/15/07, Alexandre Bergel <Alexandre.Bergel at hpi.uni-potsdam.de> wrote:
The notion of namespace of ECMA 4 is very interesting. I spent some time in reading the new javascript description [1]. I try to understand whether a "stack of namespace" is present or not at runtime. [...]
Thanks for the question. You're right, that wiki page doesn't seem to say.
The scope of "use namespace" is determined statically. So is the "set of open namespaces" that the spec talks about. When the spec says "context", it means the lexical context. The runtime stack is not a factor.
When an expression like "this.update()" is evaluated, the runtime searches the open namespaces, in order, until an "update" property is found. The sequence of namespaces to search is determined statically. The search happens at runtime, because you can't tell statically which properties the runtime value of "this" will have.
Is there a chance to get a fixed interpreter ?
Yes, in time. Both the spec and the reference implementation are works in progress.
For the impatient, this example can be munged into a form the current interpreter can handle:
namespace N1; namespace N2; N1 var x: int = 10; N2 var x : String = "hello"; {use namespace N1; print(x);} 10
Likewise:
namespace V2; class Component {function paint() {print("Component paint")}
function update() {this.paint()} V2 function paint() {print("Component paint V2")}}
{use namespace V2; var c = new Component; c.update();} Component paint
On 7/15/07, Jason Orendorff <jason.orendorff at gmail.com> wrote:
On 7/15/07, Alexandre Bergel <Alexandre.Bergel at hpi.uni-potsdam.de> wrote:
The notion of namespace of ECMA 4 is very interesting. I spent some time in reading the new javascript description [1]. I try to understand whether a "stack of namespace" is present or not at runtime. [...]
Thanks for the question. You're right, that wiki page doesn't seem to say.
The scope of "use namespace" is determined statically. So is the "set of open namespaces" that the spec talks about. When the spec says "context", it means the lexical context. The runtime stack is not a factor.
When an expression like "this.update()" is evaluated, the runtime searches the open namespaces, in order, until an "update" property is found. The sequence of namespaces to search is determined statically. The search happens at runtime, because you can't tell statically which properties the runtime value of "this" will have.
Is there a chance to get a fixed interpreter ?
Yes, in time. Both the spec and the reference implementation are works in progress.
For the impatient, this example can be munged into a form the current interpreter can handle:
namespace N1; namespace N2; N1 var x: int = 10; N2 var x : String = "hello"; {use namespace N1; print(x);} 10
Likewise:
namespace V2; class Component {function paint() {print("Component paint")} function update() {this.paint()} V2 function paint() {print("Component paint V2")}}
{use namespace V2; var c = new Component; c.update();} Component paint
This is to my knowledge not a bug in the interpreter. "use namespace" can only be used at the top of a block, not at the top level. This fact is probably well hidden on the wiki pages :-/
Thanks for your answer.
I would be delighted to test the new interpreter and/or to "debug"
the info on the wiki :-)
, Alexandre
Hello,
Few questions about Namespaces are buzzing me. Please, if you are the
right person to address those questions, feel free to forward this
email.
The notion of namespace of ECMA 4 is very interesting. I spent some
time in reading the new javascript description [1]. I try to
understand whether a "stack of namespace" is present or not at
runtime. Let's assume the following code:
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= namespace V2;
class Component { function paint () {print("Component paint")} function update () {this.paint()} V2 function paint () {print("Component paint V2")} }
use namespace V2 var c = new Component(); c.update(); // Does it print V2 or not ? -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
update() is defined in the default namespace, therefore I imagine
that the default implementation of paint() is invoked. "V2" should
not be displayed, however, would you mind to confirm ?
I tried to execute this code on the interpreter es4 for Macosx (intel
version on www.ecmascript-lang.org/download.php), but the "use
namespace" clause is not accepted. For example, the following code
cannot be parsed:
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= namespace N1 namespace N2 N1 var x : int = 10 N2 var x : String = "hello" use namespace N1 print(x) // print 10 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Is there a chance to get a fixed interpreter ?
Best , Alexandre
[1] developer.mozilla.org/es4/spec/chapter_12_namespaces.html