newLISP Fan Club

Forum => Anything else we might add? => Topic started by: Dmi on October 30, 2005, 12:06:48 PM

Title: unattended symbol
Post by: Dmi on October 30, 2005, 12:06:48 PM
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?
Title:
Post by: Dmi on October 30, 2005, 12:28:03 PM
Hmm... moreover, as I can see, "import" always creates symbols directly in MAIN.

Is it right?
Title: Re: unattended symbol
Post by: PaipoJim on October 30, 2005, 02:34:22 PM
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".

-
Title:
Post by: Dmi on October 30, 2005, 03:10:37 PM
Oh... really defined when function is declared!

Nice side effect, that isn't documented (afaik) :-)
Title:
Post by: Lutz on October 31, 2005, 09:48:58 AM
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
Title:
Post by: Dmi on October 31, 2005, 10:00:28 AM
Yes!! I forgot to switch context :-))



Thanks!