Page 1 of 1

unattended symbol

Posted: Sun Oct 30, 2005 8:06 pm
by Dmi
here the code (modified newdep's example):

Code: Select all

(context 'NCR)
(set 'ncfuncs '(
  "initscr" "box" "newwin" "endwin" "delwin" "wgetch" "wrefresh" "mvwprintw"
  "wprintw" "refresh" "wmove" "scrollok" "nl" "werase" "LINES" "COLS" "noecho"
  "delch" "waddch" "keypad" "wclrtoeol"))

;;; import library functions
(define (import-ncurses)
  (let (ctx (context))
    (dolist (x ncfuncs) (import "/lib/libncurses.so.5" x))
    (context ctx)))
(context 'MAIN)
when I load it, I have following contents of NCR context:

Code: Select all

newLISP v.8.7.0 on linux, execute 'newlisp -h' for more info.
> (NCR:import-ncurses)
MAIN 
> (symbols NCR)
(NCR:COLS NCR:LINES NCR:attach-ncurses NCR:cons-win NCR:ctx NCR:import-ncurses 
 NCR:init-ncurses NCR:ncfuncs NCR:str NCR:termsize NCR:wappend NCR:werase 
 NCR:win NCR:wprintw NCR:wrefresh NCR:wset NCR:x) 
> 
Note about the last NCR:x symbol. Where is it from?
I think it must not be present... Or I have a mistake somewhere?

Posted: Sun Oct 30, 2005 8:28 pm
by Dmi
Hmm... moreover, as I can see, "import" always creates symbols directly in MAIN.
Is it right?

Re: unattended symbol

Posted: Sun Oct 30, 2005 10:34 pm
by PaipoJim
Dmi wrote: Note about the last NCR:x symbol. Where is it from?
I think it must not be present... Or I have a mistake somewhere?
The NCR:x is the symbol x in the dolist and was created along with NCR:ctx and NCR:import-ncurses when the function "import-ncurses" was *defined*.

The various strings were used to create symbols like NCR:COLS etc... during the *execution* of "import-ncurses".
-

Posted: Sun Oct 30, 2005 11:10 pm
by Dmi
Oh... really defined when function is declared!
Nice side effect, that isn't documented (afaik) :-)

Posted: Mon Oct 31, 2005 5:48 pm
by Lutz
This is the way it will work:

Code: Select all

(context 'NCR)

(set 'ncfuncs '("initscr" "box"))

(define (import-ncurses)
  (set 'ctx (context))  ; save callers context
  (context 'NCR)        ; set translation/import context to NCR
  (dolist (x ncfuncs) (import "/lib/libncurses.so.5" x))
  (context ctx)          ; switch back to calling context
)

(context 'MAIN)

(NCR:import-ncurses)  ; all imports will be created in NCR

(NCR:initscr ...)         ; uses the functions

You where thinking in the right directtion in your code, trying to set the context before the 'import' statement. But when you call '(NCR:import-ncurses)' from MAIN then the code executing in NCR will also execute from MAIN, but all symbols inside NCR where created when loading the NCR file in NCR. The context set with (context ...) will set the context to use when creating symbols via 'import', 'sym' 'load' etc., but you context of execution still is the one when you called the function.

Lutz

Posted: Mon Oct 31, 2005 6:00 pm
by Dmi
Yes!! I forgot to switch context :-))

Thanks!