Skip to content

Commit e50faed

Browse files
committed
COMMON: defer call to module init function
1 parent dc8386c commit e50faed

File tree

3 files changed

+23
-46
lines changed

3 files changed

+23
-46
lines changed

src/common/extlib.c

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
typedef int (*sblib_exec_fn)(int, int, slib_par_t *, var_t *);
3838
typedef int (*sblib_getname_fn) (int, char *);
3939
typedef int (*sblib_count_fn) (void);
40-
typedef const char *(*sblib_get_module_name_fn) (void);
41-
typedef int (*sblib_init_fn) (void);
40+
typedef int (*sblib_init_fn) (const char *);
4241
typedef void (*sblib_close_fn) (void);
4342

4443
typedef struct {
@@ -278,13 +277,18 @@ void slib_import(int lib_id, int comp) {
278277
slib_import_routines(lib, comp);
279278
lib->imported = 1;
280279
}
280+
if (lib && !comp) {
281+
sblib_init_fn minit = slib_getoptptr(lib, "sblib_init");
282+
if (minit && !minit(gsb_last_file)) {
283+
rt_raise("LIB: %s->sblib_init(), failed", lib->name);
284+
}
285+
}
281286
}
282287

283288
/**
284-
* opens the library and invokes the init function
289+
* opens the library
285290
*/
286291
void slib_open(const char *fullname, const char *name) {
287-
int success = 0;
288292
int name_index = 0;
289293

290294
if (strncmp(name, "lib", 3) == 0) {
@@ -303,30 +307,12 @@ void slib_open(const char *fullname, const char *name) {
303307
log_printf("LIB: importing %s", fullname);
304308
}
305309
if (slib_llopen(lib)) {
306-
success = 1;
307-
308-
// init
309-
sblib_init_fn minit = slib_getoptptr(lib, "sblib_init");
310-
if (minit) {
311-
if (!minit()) {
312-
sc_raise("LIB: %s->sblib_init(), failed", lib->name);
313-
success = 0;
314-
}
315-
}
316-
317-
// override default name
318-
sblib_get_module_name_fn get_module_name = slib_getoptptr(lib, "sblib_get_module_name");
319-
if (get_module_name) {
320-
strlcpy(lib->name, get_module_name(), NAME_SIZE);
321-
}
322-
} else {
323-
sc_raise("LIB: can't open %s", fullname);
324-
}
325-
if (success) {
326310
slib_count++;
327311
if (!opt_quiet) {
328312
log_printf("... done\n");
329313
}
314+
} else {
315+
sc_raise("LIB: can't open %s", fullname);
330316
}
331317
}
332318

src/include/module.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,7 @@ typedef struct {
3131
*
3232
* @return non-zero on success
3333
*/
34-
int sblib_init(void);
35-
36-
/**
37-
* @ingroup modlib
38-
*
39-
* returns the module name
40-
*
41-
* @return module name
42-
*/
43-
const char *sblib_get_module_name();
34+
int sblib_init(const char *sourceFile);
4435

4536
/**
4637
* @ingroup modstd

src/platform/sdl/main.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ const char *FONTS[] = {
4040
};
4141

4242
static struct option OPTIONS[] = {
43-
{"help", no_argument, NULL, 'h'},
44-
{"verbose", no_argument, NULL, 'v'},
45-
{"keywords", no_argument, NULL, 'k'},
46-
{"command", optional_argument, NULL, 'c'},
47-
{"font", optional_argument, NULL, 'f'},
48-
{"run", optional_argument, NULL, 'r'},
49-
{"run-live", optional_argument, NULL, 'x'},
50-
{"run-n-wait",optional_argument, NULL, 'n'},
51-
{"module", optional_argument, NULL, 'm'},
52-
{"edit", optional_argument, NULL, 'e'},
53-
{"debug", optional_argument, NULL, 'd'},
54-
{"debugPort", optional_argument, NULL, 'p'},
43+
{"help", no_argument, NULL, 'h'},
44+
{"verbose", no_argument, NULL, 'v'},
45+
{"keywords", no_argument, NULL, 'k'},
46+
{"command", optional_argument, NULL, 'c'},
47+
{"font", optional_argument, NULL, 'f'},
48+
{"run", optional_argument, NULL, 'r'},
49+
{"run-live", optional_argument, NULL, 'x'},
50+
{"run-n-wait", optional_argument, NULL, 'n'},
51+
{"module-path", optional_argument, NULL, 'm'},
52+
{"edit", optional_argument, NULL, 'e'},
53+
{"debug", optional_argument, NULL, 'd'},
54+
{"debugPort", optional_argument, NULL, 'p'},
5555
{0, 0, 0, 0}
5656
};
5757

0 commit comments

Comments
 (0)