variable scoping and namespaces

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

variable scoping and namespaces

Brad Reynolds
I have an SWT based console that is working.  In order to be able to execute SWT code from the interpreter I have to execute the code in the display thread which isn't the thread that the interpreter is running in.  So instead of evaluating the user input directly I have to wrap it in a call to display.syncExec().  I had a version of this working but I'm trying to clean up the code and I'm now running into a few problems.  In order to execute the user commands I have a method as follows:

runInDisplay(command) {
    super._swtcommand = command; //bit of a hack to get around namespace change
    setNameSpace(this.caller.namespace);

    

    run () {
        eval(_swtcommand);
    }

    

    display.syncExec(this);
}

The scenario I'm running is the user inputs "shell = new Shell(display);".  This executes fine but the next time this method is invoked shell == void.  It doesn't seem to be storing any user created variables in the global namespace which should be this.caller.namespace.  If the user inputs "shell = new Shell(display);shell.open();" a shell opens so at least I know it will work.

I feel like I'm missing something obvious and hopefully someone can straighten me out.

Thanks,
Brad
Reply | Threaded
Open this post in threaded view
|

Re: variable scoping and namespaces

Brad Reynolds
I figured it out.  For completeness I figured I'd respond and answer my own question.  The problem was that 'this.caller' wasn't global when I thought it was.  So using 'super' instead of 'this.caller' made it work.  Newbie mistake.

super._swtcommand = command; //bit of a hack to get around namespace change
setNameSpace(super.namespace);
display = _swtbsh.display;

if (_swtbsh.echo) {
print("\"" + _swtcommand + "\"");
}

if (_swtbsh.syncExec) {
run () {
setNameSpace(super.namespace);
eval(_swtcommand);
}


display.syncExec(this);
} else if (_swtbsh.asyncExec){
run () {
setNameSpace(super.namespace);
eval(_swtcommand);
}


display.asyncExec(this);
} else {
eval(_swtcommand);
}


On Oct 31, 2005, at 6:34 AM, Brad Reynolds wrote:

I have an SWT based console that is working.  In order to be able to execute SWT code from the interpreter I have to execute the code in the display thread which isn't the thread that the interpreter is running in.  So instead of evaluating the user input directly I have to wrap it in a call to display.syncExec().  I had a version of this working but I'm trying to clean up the code and I'm now running into a few problems.  In order to execute the user commands I have a method as follows:

runInDisplay(command) {
    super._swtcommand = command; //bit of a hack to get around namespace change
    setNameSpace(this.caller.namespace);
    
    run () {
        eval(_swtcommand);
    }
    
    display.syncExec(this);
}

The scenario I'm running is the user inputs "shell = new Shell(display);".  This executes fine but the next time this method is invoked shell == void.  It doesn't seem to be storing any user created variables in the global namespace which should be this.caller.namespace.  If the user inputs "shell = new Shell(display);shell.open();" a shell opens so at least I know it will work.

I feel like I'm missing something obvious and hopefully someone can straighten me out.

Thanks,
Brad