www.digitalmars.com         C & C++   DMDScript  

DMDScript - Walter - can you please look into this? Thanks!

reply adzheng <adzheng_member pathlink.com> writes:
It's kind of a scope issue. Here is the test java script:

var Util = {
bar: function() {
function it() {
return "I'm in it()";
}
function g() {
println("This is bar! - " + it());
}
g();
}
}
Util.bar();

It runs well on IE & Mozilla but failed on your JS engine which reports
"property [it] is undefined and no call method". I've to add 'this' in front of
it() to make it work. But I think this is not necessary since function it() is
defined in the same scope as function g(). It seems like it() will only look for
global scope if there is no 'this' in front of it.

Can you please expain the details and point me a way to solve this issue since
we found many scripts on web will not have 'this' when refer to an inner
function. 

Thanks!
Dec 07 2005
parent reply "Walter Bright" <newshound digitalmars.com> writes:
"adzheng" <adzheng_member pathlink.com> wrote in message
news:dn7umc$2pal$1 digitaldaemon.com...
 It's kind of a scope issue. Here is the test java script:

 var Util = {
 bar: function() {
 function it() {
 return "I'm in it()";
 }
 function g() {
 println("This is bar! - " + it());
 }
 g();
 }
 }
 Util.bar();

 It runs well on IE & Mozilla but failed on your JS engine which reports
 "property [it] is undefined and no call method". I've to add 'this' in

 it() to make it work. But I think this is not necessary since function

 defined in the same scope as function g(). It seems like it() will only

 global scope if there is no 'this' in front of it.

 Can you please expain the details and point me a way to solve this issue

 we found many scripts on web will not have 'this' when refer to an inner
 function.

The 'scope chain' is described in ECMA 262 v3 10.1.4: "... the scope chain of the execution context is affected only by with statements and catch clauses." In other words, nested functions do not add to the scope chain. g() is found because it is a member of the 'activation object' for the function it is nested in. Inside g(), the it() function is not part of the activation object created for g(). The script code is apparently relying on bugs in IE and Mozilla.
Dec 08 2005
parent reply adzheng <adzheng_member pathlink.com> writes:
Thank you, Walter, for your quick reply.

In order to be consistent with other scripts which run well on IE&Mozilla we've
to find a way to solve this issue and we did it.

Here are some evidence found on web:

http://www.webreference.com/js/column80/6.html

You can see that in the section on scope, it says:
------------------------------------------------
Function code is code in the body of a function. The scope of function code is
not trivial. It includes:

* The variable object of the calling code's execution context. This object
includes all variables that were defined up to the point where the function (or
its outer function) is called.
* The variable object of the outer function, if there is an outer function.
* The global object which is the browser window object.
* The arguments object which includes all passed parameter values. 

The 2nd asterisk stands the point.

Anyway, thanks a lot for your help. We probably need your help to solve other
problems in the future!

In article <dnb7ah$3pc$1 digitaldaemon.com>, Walter Bright says...
"adzheng" <adzheng_member pathlink.com> wrote in message
news:dn7umc$2pal$1 digitaldaemon.com...
 It's kind of a scope issue. Here is the test java script:

 var Util = {
 bar: function() {
 function it() {
 return "I'm in it()";
 }
 function g() {
 println("This is bar! - " + it());
 }
 g();
 }
 }
 Util.bar();

 It runs well on IE & Mozilla but failed on your JS engine which reports
 "property [it] is undefined and no call method". I've to add 'this' in

 it() to make it work. But I think this is not necessary since function

 defined in the same scope as function g(). It seems like it() will only

 global scope if there is no 'this' in front of it.

 Can you please expain the details and point me a way to solve this issue

 we found many scripts on web will not have 'this' when refer to an inner
 function.

The 'scope chain' is described in ECMA 262 v3 10.1.4: "... the scope chain of the execution context is affected only by with statements and catch clauses." In other words, nested functions do not add to the scope chain. g() is found because it is a member of the 'activation object' for the function it is nested in. Inside g(), the it() function is not part of the activation object created for g(). The script code is apparently relying on bugs in IE and Mozilla.

Dec 09 2005
next sibling parent "Walter Bright" <newshound digitalmars.com> writes:
"adzheng" <adzheng_member pathlink.com> wrote in message
news:dnckk9$2tp8$1 digitaldaemon.com...
 Here are some evidence found on web:

 http://www.webreference.com/js/column80/6.html

The web site seems to be down at the moment.
 You can see that in the section on scope, it says:
 ------------------------------------------------
 Function code is code in the body of a function. The scope of function

 not trivial. It includes:

 * The variable object of the calling code's execution context. This object
 includes all variables that were defined up to the point where the

 its outer function) is called.
 * The variable object of the outer function, if there is an outer

 * The global object which is the browser window object.
 * The arguments object which includes all passed parameter values.

 The 2nd asterisk stands the point.

I can't find support for (2) in the ECMA 262 v3 standard.
Dec 09 2005
prev sibling parent reply "Walter Bright" <newshound digitalmars.com> writes:
"adzheng" <adzheng_member pathlink.com> wrote in message
news:dnckk9$2tp8$1 digitaldaemon.com...
 In order to be consistent with other scripts which run well on IE&Mozilla

 to find a way to solve this issue and we did it.

What was the solution you found?
Dec 09 2005
parent reply adzheng <adzheng_member pathlink.com> writes:
In fact we modified the scope chain.

In article <dncn6k$30e$2 digitaldaemon.com>, Walter Bright says...
"adzheng" <adzheng_member pathlink.com> wrote in message
news:dnckk9$2tp8$1 digitaldaemon.com...
 In order to be consistent with other scripts which run well on IE&Mozilla

 to find a way to solve this issue and we did it.

What was the solution you found?

Dec 16 2005
parent reply "Walter Bright" <newshound digitalmars.com> writes:
Can you post the diffs?

"adzheng" <adzheng_member pathlink.com> wrote in message 
news:dnvnde$2vpi$1 digitaldaemon.com...
 In fact we modified the scope chain.

 In article <dncn6k$30e$2 digitaldaemon.com>, Walter Bright says...
"adzheng" <adzheng_member pathlink.com> wrote in message
news:dnckk9$2tp8$1 digitaldaemon.com...
 In order to be consistent with other scripts which run well on 
 IE&Mozilla

 to find a way to solve this issue and we did it.

What was the solution you found?


Dec 19 2005
parent adzheng <adzheng_member pathlink.com> writes:
Here is the code snip. Please let me know your opinion. Thanks!
So far we haven't found any side effects.

unsigned scoperootsave;
case IRcallscope:     // a = s(argc, argv)
...
scoperootsave = cc->scoperoot;
cc->scoperoot = scope->dim;
...
cc->scoperoot = scoperootsave;

In article <do74c9$1gj9$1 digitaldaemon.com>, Walter Bright says...
Can you post the diffs?

"adzheng" <adzheng_member pathlink.com> wrote in message 
news:dnvnde$2vpi$1 digitaldaemon.com...
 In fact we modified the scope chain.

 In article <dncn6k$30e$2 digitaldaemon.com>, Walter Bright says...
"adzheng" <adzheng_member pathlink.com> wrote in message
news:dnckk9$2tp8$1 digitaldaemon.com...
 In order to be consistent with other scripts which run well on 
 IE&Mozilla

 to find a way to solve this issue and we did it.

What was the solution you found?



Dec 22 2005