unattended symbol

Started by Dmi, October 30, 2005, 12:06:48 PM

Previous topic - Next topic

Dmi

here the code (modified newdep's example):
(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:
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?
WBR, Dmi

Dmi

#1
Hmm... moreover, as I can see, "import" always creates symbols directly in MAIN.

Is it right?
WBR, Dmi

PaipoJim

#2
Quote from: "Dmi"
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".

-

Dmi

#3
Oh... really defined when function is declared!

Nice side effect, that isn't documented (afaik) :-)
WBR, Dmi

Lutz

#4
This is the way it will work:

(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

Dmi

#5
Yes!! I forgot to switch context :-))



Thanks!
WBR, Dmi