summaryrefslogtreecommitdiffstats
path: root/generic/tclStubStaticPackage.c
blob: 2765b3bee40a1b913bb311580e14132dc4932332 (plain)
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
/*
 * tclStubStaticPackage.c --
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

#include "tclInt.h"
#ifndef _WIN32
#   include <dlfcn.h>
#else
#   define dlopen(a,b) (void *)LoadLibraryW(JOIN(L,a))
#   define dlsym(a,b) (void *)GetProcAddress((HMODULE)(a),b)
#   define dlerror() ""
#endif

MODULE_SCOPE void *tclStubsHandle;

/*
 *----------------------------------------------------------------------
 *
 * Tcl_InitSubsystems --
 *
 *	Load the Tcl core dynamically, version "9.0" (or higher, in future versions)
 *
 * Results:
 *	Outputs the value of the "version" argument.
 *
 * Side effects:
 *	Sets the stub table pointers.
 *
 *----------------------------------------------------------------------
 */

static const char PROCNAME[] = "_Tcl_StaticPackage";

MODULE_SCOPE const char *
TclStubStaticPackage(Tcl_Interp *interp,
	const char *pkgName,
	Tcl_PackageInitProc *initProc,
	Tcl_PackageInitProc *safeInitProc)
{
    static void *stubFn = NULL;
    static const char *version = NULL;

    if (tclStubsHandle == (void *)-1) {
	fprintf(stderr, "Cannot call %s from stubbed extension\n", PROCNAME + 1);
	abort();
    }
    if (!stubFn) {
	if (!tclStubsHandle) {
	    tclStubsHandle = dlopen(TCL_DLL_FILE, RTLD_NOW|RTLD_LOCAL);
	    if (!tclStubsHandle) {
		tclStubsPtr->tcl_Panic("Cannot find " TCL_DLL_FILE ": %s\n", dlerror());
	    }
	}
	stubFn = dlsym(tclStubsHandle, PROCNAME + 1);
	if (!stubFn) {
		stubFn = dlsym(tclStubsHandle, PROCNAME);
	}
	if (stubFn) {
	    version = ((const char *(*)(Tcl_Interp *, const char *, Tcl_PackageInitProc *, Tcl_PackageInitProc *))stubFn)(interp, pkgName, initProc, safeInitProc);
	}
    }
    return version;
}

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 78
 * End:
 */