This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod-library.c
230 lines (183 loc) · 5.26 KB
/
mod-library.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// File: %t-library.c
// Summary: "External Library Support"
// Section: datatypes
// Project: "Rebol 3 Interpreter and Run-time (Ren-C branch)"
// Homepage: https://github.com/metaeducation/ren-c/
//
//=////////////////////////////////////////////////////////////////////////=//
//
// Copyright 2014 Atronix Engineering, Inc.
// Copyright 2014-2017 Ren-C Open Source Contributors
// REBOL is a trademark of REBOL Technologies
//
// See README.md and CREDITS.md for more information.
//
// Licensed under the Lesser GPL, Version 3.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.gnu.org/licenses/lgpl-3.0.html
//
//=////////////////////////////////////////////////////////////////////////=//
//
#include "sys-core.h"
#include "sys-library.h"
#include "tmp-mod-library.h"
REBTYP *EG_Library_Type = nullptr; // (E)xtension (G)lobal LIBRARY! type
//
// CT_Library: C
//
REBINT CT_Library(noquote(Cell(const*)) a, noquote(Cell(const*)) b, bool strict)
{
UNUSED(strict);
return VAL_LIBRARY(a) == VAL_LIBRARY(b);
}
//
// MAKE_Library: C
//
Bounce MAKE_Library(
Frame(*) frame_,
enum Reb_Kind kind,
option(const REBVAL*) parent,
const REBVAL *arg
){
assert(kind == REB_CUSTOM);
if (parent)
return RAISE(Error_Bad_Make_Parent(kind, unwrap(parent)));
if (not IS_FILE(arg))
return RAISE(Error_Unexpected_Type(REB_FILE, VAL_TYPE(arg)));
void *fd = Open_Library(arg);
if (fd == NULL)
return RAISE(arg);
REBLIB *lib = Alloc_Singular(FLAG_FLAVOR(LIBRARY) | NODE_FLAG_MANAGED);
Init_Trash(ARR_SINGLE(lib)); // !!! save name? other data?
lib->link.fd = fd; // seen as shared by all instances
node_MISC(Meta, lib) = nullptr; // !!! build from spec, e.g. arg?
RESET_CUSTOM_CELL(OUT, EG_Library_Type, CELL_FLAG_FIRST_IS_NODE);
INIT_VAL_NODE1(OUT, lib);
return OUT;
}
//
// TO_Library: C
//
Bounce TO_Library(Frame(*) frame_, enum Reb_Kind kind, const REBVAL *arg)
{
return MAKE_Library(frame_, kind, nullptr, arg);
}
//
// MF_Library: C
//
void MF_Library(REB_MOLD *mo, noquote(Cell(const*)) v, bool form)
{
UNUSED(form);
Pre_Mold(mo, v);
End_Mold(mo);
}
//
// REBTYPE: C
//
REBTYPE(Library)
{
switch (ID_OF_SYMBOL(verb)) {
case SYM_CLOSE: {
INCLUDE_PARAMS_OF_CLOSE;
REBVAL *lib = ARG(port); // !!! generic arg name is "port"?
if (VAL_LIBRARY_FD(lib) == NULL) {
// allow to CLOSE an already closed library
}
else {
Close_Library(VAL_LIBRARY_FD(lib));
VAL_LIBRARY(lib)->link.fd = nullptr;
}
return nullptr; }
default:
break;
}
return BOUNCE_UNHANDLED;
}
Symbol(const*) S_Library(void) {
return Canon(LIBRARY_X);
}
//
// startup*: native [
//
// {Register the LIBRARY! datatype (so MAKE LIBRARY! [] etc. work)}
//
// return: <none>
// ]
//
DECLARE_NATIVE(startup_p)
{
LIBRARY_INCLUDE_PARAMS_OF_STARTUP_P;
// !!! See notes on Hook_Datatype for this poor-man's substitute for a
// coherent design of an extensible object system (as per Lisp's CLOS)
//
EG_Library_Type = Hook_Datatype(
"http://datatypes.rebol.info/library",
"external library reference",
&S_Library,
&T_Library,
&CT_Library,
&MAKE_Library,
&TO_Library,
&MF_Library
);
Extend_Generics_Someday(nullptr); // !!! See comments
return NONE;
}
extern RL_LIB Ext_Lib;
//
// export run-library-collator: native [
//
// {Execute a function in a DLL or other library that returns a REBVAL*}
//
// return: [<opt> any-value!]
// library [library!]
// linkname [text!]
// ]
//
DECLARE_NATIVE(run_library_collator)
{
LIBRARY_INCLUDE_PARAMS_OF_RUN_LIBRARY_COLLATOR;
// !!! This code used to check for loading an already loaded
// extension. It looked in an "extensions list", but now that the
// extensions are modules really this should just be the same as
// looking in the modules list. Such code should be in usermode
// (very awkward in C). The only unusual C bit was:
//
// // found the existing extension, decrease the reference
// // added by MAKE_library
// //
// OS_CLOSE_LIBRARY(VAL_LIBRARY_FD(lib));
//
CFUNC *cfunc = Find_Function(
VAL_LIBRARY_FD(ARG(library)),
cs_cast(STR_HEAD(VAL_STRING(ARG(linkname))))
);
if (cfunc == nullptr)
fail ("Could not find collator function in library");
COLLATE_CFUNC *collator = cast(COLLATE_CFUNC*, cfunc);
// We pass the collation entry point the table of API functions. This is
// how DLLs know the addresses of functions in the EXE that they can call.
// If the extension is built in with the executable, it uses a shortcut
// and just calls the RL_rebXXX() functions directly...so it does not use
// the table we're passing.
//
return collator(&Ext_Lib);
}
//
// shutdown_p: native [
//
// {Unregister the LIBRARY! datatype (MAKE LIBRARY! will fail)}
//
// return: <none>
// ]
//
DECLARE_NATIVE(shutdown_p)
{
LIBRARY_INCLUDE_PARAMS_OF_SHUTDOWN_P;
Unhook_Datatype(EG_Library_Type);
EG_Library_Type = nullptr;
return NONE;
}