Skip to content

Commit e489fef

Browse files
committed
FEAT: native's specification may be defined directly in C code using special header structure
This is based on Hostile Fork's work on Ren-c, just without using his INCLUDE_PARAMS_OF_* macros. It is still possible to use the old way definitions, so the original code does not have to be completely modified. For example: /**********************************************************************/ // // test-native: native [ // {This is just a native function for test purpose.} // 'value [any-type!] "This can be value of any type" // ] // REBNATIVE(test_native) { REBVAL *val = D_ARG(1); printf("Value has type ID: %i\n", VAL_TYPE(val)); return R_ARG1; //return the same value }
1 parent 45fbc34 commit e489fef

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ make/libr3.so
99
make/libr3.dll
1010
src/boot/boot-code.r
1111
src/boot/host-init.r
12+
src/boot/tmp-natives.r
1213
src/core/b-boot.c
1314
src/include/ext-types.h
1415
src/include/host-*

src/tools/make-boot.r

+10-1
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,7 @@ boot-natives: load %natives.r
10241024
if has-graphics [append boot-natives load %graphics.r]
10251025

10261026
nats: append copy boot-booters boot-natives
1027+
nats-collected: load %tmp-natives.r
10271028

10281029
n: boot-sys
10291030
;while [n: find n 'native] [
@@ -1043,6 +1044,11 @@ foreach val nats [
10431044
]
10441045
]
10451046

1047+
foreach [name spec] nats-collected [
1048+
emit-line/decl "REBNATIVE(" name ");"
1049+
nat-count: nat-count + 1
1050+
]
1051+
10461052
print [nat-count "natives"]
10471053

10481054
emit [newline {const REBFUN Native_Funcs[} nat-count {] = ^{
@@ -1051,7 +1057,10 @@ foreach val nats [
10511057
if set-word? val [
10521058
emit-line/code "N_" to word! val "," ;R3
10531059
]
1054-
;nat-count: nat-count + 1
1060+
]
1061+
foreach [name spec] nats-collected [
1062+
emit-line/code "N_" name ","
1063+
append boot-natives to-block spec
10551064
]
10561065
emit-end
10571066
emit newline

src/tools/make-headers.r

+49-6
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,25 @@ tmp: context [
2929
change-dir %../core/
3030

3131
count: 0
32-
output: make string! 20000
32+
output: make string! 20000
33+
natives: make string! 20000
3334

34-
emit: func [d] [append repend output d newline]
35-
remit: func [d] [append repend rlib d newline]
35+
emit: func [d] [append repend output d newline]
36+
emit-rl: func [d] [append repend rlib d newline]
37+
emit-n: func [d] [append repend natives d newline]
3638

3739
emit-header: func [t f] [emit form-header/gen t f %make-headers]
3840
rlib: form-header/gen "REBOL Interface Library" %reb-lib.h %make-headers.r
3941
append rlib newline
4042

43+
emit-n {REBOL [
44+
Title: "REBOL automatically collected natives."
45+
Purpose: {Data in format: [c-name {rebol-specification}]}
46+
Note: "AUTO-GENERATED FILE - Do not modify. (From: make-headers.r)"
47+
]}
48+
49+
c-file: none
50+
4151
append-spec: func [spec] [
4252
;?? spec
4353
if all [
@@ -58,7 +68,7 @@ append-spec: func [spec] [
5868
append dups spec
5969
]
6070
either find spec "RL_API" [
61-
remit ["extern " spec "; // " the-file]
71+
emit-rl ["extern " spec "; // " the-file]
6272
][
6373
emit ["extern " spec "; // " the-file]
6474
]
@@ -77,14 +87,46 @@ func-header: [
7787
any [
7888
thru "**"
7989
[#" " | #"^-"]
80-
copy line thru newline
90+
copy line thru newline
8191
]
8292
thru "*/"
8393
|
8494
none
8595
]
8696
]
8797

98+
ch_func-chars: charset [#"a" - #"z" #"A"]
99+
100+
spec-reb: make string! 1024
101+
name: none
102+
103+
native-header: [
104+
;-- Scan for native header box:
105+
"^///" to newline (clear spec-reb)
106+
any [ "^///" copy line to newline (append append spec-reb line newline)]
107+
any [#"^/" | #" " | #"^-"]
108+
"REBNATIVE(" copy name to ")" (probe name
109+
either any [
110+
error? try [spec: load spec-reb]
111+
3 <> length? spec
112+
error? try [name: load name]
113+
not word? name
114+
][
115+
print "^[[1;32;49m** Invalid NATIVE spec definition found: ^[[0m"
116+
print spec-reb
117+
ask "Press ENTER to continue."
118+
][
119+
if c-file <> the-file [
120+
emit-n ["^/;-- " the-file]
121+
c-file: the-file
122+
]
123+
emit-n ["^/" name " {"]
124+
emit-n trim/head/tail detab spec-reb
125+
emit-n #"}"
126+
]
127+
)
128+
]
129+
88130
process: func [file] [
89131
if verbose [?? file]
90132
data: read the-file: file
@@ -93,7 +135,7 @@ process: func [file] [
93135
any [
94136
thru "/******" to newline
95137
[
96-
func-header | thru newline
138+
func-header | native-header | thru newline
97139
]
98140
]
99141
]
@@ -118,6 +160,7 @@ foreach file files [
118160
][process file]
119161
]
120162

163+
write %../boot/tmp-natives.r natives
121164
write %../include/tmp-funcs.h output
122165

123166
print [count "function prototypes"]

0 commit comments

Comments
 (0)