Skip to content

Commit

Permalink
FEAT: function and closure accepting also module! for its `obje…
Browse files Browse the repository at this point in the history
…ct` argument

resolves: Oldes/Rebol-issues#2575
  • Loading branch information
Oldes committed Oct 30, 2023
1 parent bee8e09 commit 455f045
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/mezz/base-funcs.reb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function: funct: func [
spec [block!] {Help string (opt) followed by arg words (and opt type and string)}
body [block!] {The body block of the function}
/with {Define or use a persistent object (self)}
object [object! block! map!] {The object or spec}
object [any-object! block! map!] {The object or spec}
/extern words [block!] {These words are not local}
][
; Copy the spec and add /local to the end if not found
Expand All @@ -43,7 +43,7 @@ function: funct: func [
; them to the spec. Don't include the words already in the spec or object.
insert find/tail spec /local collect-words/deep/set/ignore body either with [
; Make our own local object if a premade one is not provided
unless object? object [object: make object! object]
unless any-object? object [object: make object! object]
bind body object ; Bind any object words found in the body
; Ignore the words in the spec and those in the object. The spec needs
; to be copied since the object words shouldn't be added to the locals.
Expand Down
4 changes: 2 additions & 2 deletions src/mezz/mezz-func.reb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ closure: func [
spec [block!] {Help string (opt) followed by arg words (and opt type and string)}
body [block!] {The body block of the function}
/with {Define or use a persistent object (self)}
object [object! block! map!] {The object or spec}
object [any-object! block! map!] {The object or spec}
/extern words [block!] {These words are not local}
][
; Copy the spec and add /local to the end if not found
Expand All @@ -37,7 +37,7 @@ closure: func [
; them to the spec. Don't include the words already in the spec or object.
insert find/tail spec /local collect-words/deep/set/ignore body either with [
; Make our own local object if a premade one is not provided
unless object? object [object: make object! object]
unless any-object? object [object: make object! object]
bind body object ; Bind any object words found in the body
; Ignore the words in the spec and those in the object. The spec needs
; to be copied since the object words shouldn't be added to the locals.
Expand Down
21 changes: 21 additions & 0 deletions src/tests/units/func-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,27 @@ Rebol [
clear second b
--assert [1 2 3] = second body-of :fun

--test-- "function/with object and module"
;@@ https://github.com/Oldes/Rebol-issues/issues/2575
o: object [a: 1 test: does [a * 10]]
--assert all [
function? try [fun: function/with [][test] o]
10 = fun
]
--assert all [
closure? try [fun: closure/with [][test] o]
10 = fun
]
m: module [][a: 1 test: does [a * 20]]
--assert all [
function? try [fun: function/with [][test] m]
20 = fun
]
--assert all [
closure? try [fun: closure/with [][test] m]
20 = fun
]

===end-group===


Expand Down

0 comments on commit 455f045

Please # to comment.