View in color | License | Download script | History | Other scripts by: vincentecuye |
9-Mar 1:39 UTC
[0.092] 13.601k
[0.092] 13.601k
import-script.rREBOL [
Title: "Import Script"
Date: 26-Feb-2025
Version: 1.0.0
File: %import-script.r
Author: Rights: "Annick ECUYER"
Purpose: "Loads and runs a script inside a new context."
History: [
26-Feb-2025 "Annick" {Initial version}
]
Usage: {
new-context: import-script %my-script.r
new-context/script-command args
Examples :
1) fixes xpm.r to works with REBOL/View 2.7.8
import-script/patch/with %xpm.r [
reverse: func [value] [:value] ; byte order has changed in raw image data
select: func [value item] [system/words/select/case value item] ; fix bug with case sensitive color names
] 'xpm-to-img ; import the main function into global context
xpm-to-img %test.xpm
2) uses tiffLib.r without cluttering the global context:
import-script/with %tifflib.r 'Write_Tiff_File
Write_tiff_File logo.gif
3) It works with blocks of code too:
>> t: "test"
>> ctx: import-script [parse/all t [copy data to "s"]]
>> probe ctx
make object! [
data: "te"
]
}
Notes: {
As REBOL is very flexible, it's impossible to implement all possible forms of changing words.
So it's *not* a proper module system by itself, and for proper global context protection,
protect / protect-system should be used too.
The collected / bound words are limited to theses patterns :
word: ...
set 'word ...
(parse) set word ...
(parse) copy word ...
Note that one can get the current list of global words by examining the system/words context:
words: copy []
foreach word first :system/words [
if all [in system/words word value? (in system/words word)] [append words word]
]
words
}
Library: [
level: 'intermediate
platform: 'all
type: [function tool]
domain: [files extension parse security]
tested-under: [view 2.7.8.3.1]
support: none
license: 'MIT
see-also: none
]
]
import-script: func [
"Loads and runs a script inside a new context."
source [file! url! block!] "The script or code to import."
/with imports [word! block!] "Import selected words to the global context."
/patch code [block!] "Additional code to evaluate at import."
/local set-words words rule import-actions pos vars
] [
if any [file? source url? source] [source: load source]
imports: any [
all [with word? imports reduce [imports]]
all [with block? imports imports]
copy []
]
set-words: copy []
import-actions: copy []
pos: none
parse source rule: [
some [
; word: ... | set 'word ... | (parse) set word ... | (parse) copy word ...
[set word pos: set-word! | 'set set word pos: lit-word! | 'set set word word! | 'copy set word word!] (
either find imports to-word word [
append set-words to-set-word rejoin [to-string to-word word "*"]
either lit-word? word [change pos to-lit-word last set-words] [change pos last set-words]
if not find import-actions to-lit-word word [
append import-actions compose [
set (to-lit-word word) (to-get-word last set-words)
]
]
][
append set-words to-set-word word
]
pos: none
)
|
into rule
|
1 skip
]
]
set-words: unique set-words
append set-words none
context bind append (
append (
append set-words source
) either patch [code][[]]
) either with [import-actions] [[]] (in system/words 'system)
]
system/script/header/version |