misc/liblua/loadlib.c
author nemo
Sun, 17 Mar 2013 14:09:54 -0400
changeset 8744 6c87486fd89b
parent 3697 d5b30d6373fc
child 10017 de822cd3df3a
permissions -rw-r--r--
Add some flags for a few of the magic Land values
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     1
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     2
** $Id: loadlib.c,v 1.52.1.3 2008/08/06 13:29:28 roberto Exp $
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     3
** Dynamic library loader for Lua
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     4
** See Copyright Notice in lua.h
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     5
**
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     6
** This module contains an implementation of loadlib for Unix systems
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     7
** that have dlfcn, an implementation for Darwin (Mac OS X), an
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     8
** implementation for Windows, and a stub for other systems.
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     9
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    10
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    11
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    12
#include <stdlib.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    13
#include <string.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    14
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    15
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    16
#define loadlib_c
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    17
#define LUA_LIB
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    18
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    19
#include "lua.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    20
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    21
#include "lauxlib.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    22
#include "lualib.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    23
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    24
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    25
/* prefix for open functions in C libraries */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    26
#define LUA_POF		"luaopen_"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    27
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    28
/* separator for open functions in C libraries */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    29
#define LUA_OFSEP	"_"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    30
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    31
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    32
#define LIBPREFIX	"LOADLIB: "
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    33
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    34
#define POF		LUA_POF
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    35
#define LIB_FAIL	"open"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    36
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    37
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    38
/* error codes for ll_loadfunc */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    39
#define ERRLIB		1
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    40
#define ERRFUNC		2
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    41
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    42
#define setprogdir(L)		((void)0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    43
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    44
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    45
static void ll_unloadlib (void *lib);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    46
static void *ll_load (lua_State *L, const char *path);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    47
static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    48
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    49
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    50
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    51
#if defined(LUA_DL_DLOPEN)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    52
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    53
** {========================================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    54
** This is an implementation of loadlib based on the dlfcn interface.
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    55
** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    56
** NetBSD, AIX 4.2, HPUX 11, and  probably most other Unix flavors, at least
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    57
** as an emulation layer on top of native functions.
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    58
** =========================================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    59
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    60
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    61
#include <dlfcn.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    62
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    63
static void ll_unloadlib (void *lib) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    64
  dlclose(lib);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    65
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    66
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    67
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    68
static void *ll_load (lua_State *L, const char *path) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    69
  void *lib = dlopen(path, RTLD_NOW);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    70
  if (lib == NULL) lua_pushstring(L, dlerror());
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    71
  return lib;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    72
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    73
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    74
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    75
static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    76
  lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    77
  if (f == NULL) lua_pushstring(L, dlerror());
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    78
  return f;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    79
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    80
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    81
/* }====================================================== */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    82
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    83
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    84
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    85
#elif defined(LUA_DL_DLL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    86
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    87
** {======================================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    88
** This is an implementation of loadlib for Windows using native functions.
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    89
** =======================================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    90
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    91
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    92
#include <windows.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    93
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    94
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    95
#undef setprogdir
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    96
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    97
static void setprogdir (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    98
  char buff[MAX_PATH + 1];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    99
  char *lb;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   100
  DWORD nsize = sizeof(buff)/sizeof(char);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   101
  DWORD n = GetModuleFileNameA(NULL, buff, nsize);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   102
  if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   103
    luaL_error(L, "unable to get ModuleFileName");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   104
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   105
    *lb = '\0';
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   106
    luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   107
    lua_remove(L, -2);  /* remove original string */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   108
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   109
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   110
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   111
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   112
static void pusherror (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   113
  int error = GetLastError();
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   114
  char buffer[128];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   115
  if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   116
      NULL, error, 0, buffer, sizeof(buffer), NULL))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   117
    lua_pushstring(L, buffer);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   118
  else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   119
    lua_pushfstring(L, "system error %d\n", error);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   120
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   121
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   122
static void ll_unloadlib (void *lib) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   123
  FreeLibrary((HINSTANCE)lib);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   124
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   125
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   126
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   127
static void *ll_load (lua_State *L, const char *path) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   128
  HINSTANCE lib = LoadLibraryA(path);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   129
  if (lib == NULL) pusherror(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   130
  return lib;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   131
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   132
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   133
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   134
static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   135
  lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   136
  if (f == NULL) pusherror(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   137
  return f;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   138
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   139
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   140
/* }====================================================== */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   141
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   142
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   143
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   144
#elif defined(LUA_DL_DYLD)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   145
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   146
** {======================================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   147
** Native Mac OS X / Darwin Implementation
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   148
** =======================================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   149
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   150
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   151
#include <mach-o/dyld.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   152
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   153
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   154
/* Mac appends a `_' before C function names */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   155
#undef POF
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   156
#define POF	"_" LUA_POF
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   157
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   158
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   159
static void pusherror (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   160
  const char *err_str;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   161
  const char *err_file;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   162
  NSLinkEditErrors err;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   163
  int err_num;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   164
  NSLinkEditError(&err, &err_num, &err_file, &err_str);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   165
  lua_pushstring(L, err_str);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   166
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   167
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   168
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   169
static const char *errorfromcode (NSObjectFileImageReturnCode ret) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   170
  switch (ret) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   171
    case NSObjectFileImageInappropriateFile:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   172
      return "file is not a bundle";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   173
    case NSObjectFileImageArch:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   174
      return "library is for wrong CPU type";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   175
    case NSObjectFileImageFormat:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   176
      return "bad format";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   177
    case NSObjectFileImageAccess:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   178
      return "cannot access file";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   179
    case NSObjectFileImageFailure:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   180
    default:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   181
      return "unable to load library";
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   182
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   183
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   184
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   185
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   186
static void ll_unloadlib (void *lib) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   187
  NSUnLinkModule((NSModule)lib, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   188
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   189
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   190
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   191
static void *ll_load (lua_State *L, const char *path) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   192
  NSObjectFileImage img;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   193
  NSObjectFileImageReturnCode ret;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   194
  /* this would be a rare case, but prevents crashing if it happens */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   195
  if(!_dyld_present()) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   196
    lua_pushliteral(L, "dyld not present");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   197
    return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   198
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   199
  ret = NSCreateObjectFileImageFromFile(path, &img);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   200
  if (ret == NSObjectFileImageSuccess) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   201
    NSModule mod = NSLinkModule(img, path, NSLINKMODULE_OPTION_PRIVATE |
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   202
                       NSLINKMODULE_OPTION_RETURN_ON_ERROR);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   203
    NSDestroyObjectFileImage(img);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   204
    if (mod == NULL) pusherror(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   205
    return mod;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   206
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   207
  lua_pushstring(L, errorfromcode(ret));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   208
  return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   209
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   210
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   211
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   212
static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   213
  NSSymbol nss = NSLookupSymbolInModule((NSModule)lib, sym);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   214
  if (nss == NULL) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   215
    lua_pushfstring(L, "symbol " LUA_QS " not found", sym);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   216
    return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   217
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   218
  return (lua_CFunction)NSAddressOfSymbol(nss);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   219
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   220
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   221
/* }====================================================== */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   222
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   223
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   224
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   225
#else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   226
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   227
** {======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   228
** Fallback for other systems
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   229
** =======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   230
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   231
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   232
#undef LIB_FAIL
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   233
#define LIB_FAIL	"absent"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   234
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   235
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   236
#define DLMSG	"dynamic libraries not enabled; check your Lua installation"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   237
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   238
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   239
static void ll_unloadlib (void *lib) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   240
  (void)lib;  /* to avoid warnings */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   241
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   242
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   243
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   244
static void *ll_load (lua_State *L, const char *path) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   245
  (void)path;  /* to avoid warnings */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   246
  lua_pushliteral(L, DLMSG);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   247
  return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   248
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   249
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   250
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   251
static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   252
  (void)lib; (void)sym;  /* to avoid warnings */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   253
  lua_pushliteral(L, DLMSG);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   254
  return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   255
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   256
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   257
/* }====================================================== */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   258
#endif
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   259
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   260
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   261
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   262
static void **ll_register (lua_State *L, const char *path) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   263
  void **plib;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   264
  lua_pushfstring(L, "%s%s", LIBPREFIX, path);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   265
  lua_gettable(L, LUA_REGISTRYINDEX);  /* check library in registry? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   266
  if (!lua_isnil(L, -1))  /* is there an entry? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   267
    plib = (void **)lua_touserdata(L, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   268
  else {  /* no entry yet; create one */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   269
    lua_pop(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   270
    plib = (void **)lua_newuserdata(L, sizeof(const void *));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   271
    *plib = NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   272
    luaL_getmetatable(L, "_LOADLIB");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   273
    lua_setmetatable(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   274
    lua_pushfstring(L, "%s%s", LIBPREFIX, path);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   275
    lua_pushvalue(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   276
    lua_settable(L, LUA_REGISTRYINDEX);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   277
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   278
  return plib;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   279
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   280
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   281
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   282
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   283
** __gc tag method: calls library's `ll_unloadlib' function with the lib
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   284
** handle
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   285
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   286
static int gctm (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   287
  void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   288
  if (*lib) ll_unloadlib(*lib);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   289
  *lib = NULL;  /* mark library as closed */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   290
  return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   291
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   292
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   293
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   294
static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   295
  void **reg = ll_register(L, path);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   296
  if (*reg == NULL) *reg = ll_load(L, path);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   297
  if (*reg == NULL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   298
    return ERRLIB;  /* unable to load library */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   299
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   300
    lua_CFunction f = ll_sym(L, *reg, sym);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   301
    if (f == NULL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   302
      return ERRFUNC;  /* unable to find function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   303
    lua_pushcfunction(L, f);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   304
    return 0;  /* return function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   305
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   306
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   307
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   308
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   309
static int ll_loadlib (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   310
  const char *path = luaL_checkstring(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   311
  const char *init = luaL_checkstring(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   312
  int stat = ll_loadfunc(L, path, init);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   313
  if (stat == 0)  /* no errors? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   314
    return 1;  /* return the loaded function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   315
  else {  /* error; error message is on stack top */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   316
    lua_pushnil(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   317
    lua_insert(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   318
    lua_pushstring(L, (stat == ERRLIB) ?  LIB_FAIL : "init");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   319
    return 3;  /* return nil, error message, and where */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   320
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   321
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   322
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   323
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   324
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   325
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   326
** {======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   327
** 'require' function
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   328
** =======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   329
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   330
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   331
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   332
static int readable (const char *filename) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   333
  FILE *f = fopen(filename, "r");  /* try to open file */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   334
  if (f == NULL) return 0;  /* open failed */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   335
  fclose(f);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   336
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   337
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   338
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   339
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   340
static const char *pushnexttemplate (lua_State *L, const char *path) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   341
  const char *l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   342
  while (*path == *LUA_PATHSEP) path++;  /* skip separators */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   343
  if (*path == '\0') return NULL;  /* no more templates */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   344
  l = strchr(path, *LUA_PATHSEP);  /* find next separator */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   345
  if (l == NULL) l = path + strlen(path);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   346
  lua_pushlstring(L, path, l - path);  /* template */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   347
  return l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   348
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   349
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   350
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   351
static const char *findfile (lua_State *L, const char *name,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   352
                                           const char *pname) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   353
  const char *path;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   354
  name = luaL_gsub(L, name, ".", LUA_DIRSEP);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   355
  lua_getfield(L, LUA_ENVIRONINDEX, pname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   356
  path = lua_tostring(L, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   357
  if (path == NULL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   358
    luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   359
  lua_pushliteral(L, "");  /* error accumulator */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   360
  while ((path = pushnexttemplate(L, path)) != NULL) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   361
    const char *filename;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   362
    filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   363
    lua_remove(L, -2);  /* remove path template */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   364
    if (readable(filename))  /* does file exist and is readable? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   365
      return filename;  /* return that file name */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   366
    lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   367
    lua_remove(L, -2);  /* remove file name */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   368
    lua_concat(L, 2);  /* add entry to possible error message */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   369
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   370
  return NULL;  /* not found */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   371
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   372
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   373
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   374
static void loaderror (lua_State *L, const char *filename) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   375
  luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s",
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   376
                lua_tostring(L, 1), filename, lua_tostring(L, -1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   377
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   378
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   379
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   380
static int loader_Lua (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   381
  const char *filename;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   382
  const char *name = luaL_checkstring(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   383
  filename = findfile(L, name, "path");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   384
  if (filename == NULL) return 1;  /* library not found in this path */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   385
  if (luaL_loadfile(L, filename) != 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   386
    loaderror(L, filename);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   387
  return 1;  /* library loaded successfully */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   388
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   389
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   390
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   391
static const char *mkfuncname (lua_State *L, const char *modname) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   392
  const char *funcname;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   393
  const char *mark = strchr(modname, *LUA_IGMARK);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   394
  if (mark) modname = mark + 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   395
  funcname = luaL_gsub(L, modname, ".", LUA_OFSEP);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   396
  funcname = lua_pushfstring(L, POF"%s", funcname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   397
  lua_remove(L, -2);  /* remove 'gsub' result */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   398
  return funcname;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   399
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   400
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   401
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   402
static int loader_C (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   403
  const char *funcname;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   404
  const char *name = luaL_checkstring(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   405
  const char *filename = findfile(L, name, "cpath");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   406
  if (filename == NULL) return 1;  /* library not found in this path */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   407
  funcname = mkfuncname(L, name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   408
  if (ll_loadfunc(L, filename, funcname) != 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   409
    loaderror(L, filename);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   410
  return 1;  /* library loaded successfully */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   411
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   412
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   413
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   414
static int loader_Croot (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   415
  const char *funcname;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   416
  const char *filename;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   417
  const char *name = luaL_checkstring(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   418
  const char *p = strchr(name, '.');
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   419
  int stat;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   420
  if (p == NULL) return 0;  /* is root */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   421
  lua_pushlstring(L, name, p - name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   422
  filename = findfile(L, lua_tostring(L, -1), "cpath");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   423
  if (filename == NULL) return 1;  /* root not found */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   424
  funcname = mkfuncname(L, name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   425
  if ((stat = ll_loadfunc(L, filename, funcname)) != 0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   426
    if (stat != ERRFUNC) loaderror(L, filename);  /* real error */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   427
    lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   428
                       name, filename);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   429
    return 1;  /* function not found */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   430
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   431
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   432
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   433
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   434
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   435
static int loader_preload (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   436
  const char *name = luaL_checkstring(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   437
  lua_getfield(L, LUA_ENVIRONINDEX, "preload");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   438
  if (!lua_istable(L, -1))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   439
    luaL_error(L, LUA_QL("package.preload") " must be a table");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   440
  lua_getfield(L, -1, name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   441
  if (lua_isnil(L, -1))  /* not found? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   442
    lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   443
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   444
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   445
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   446
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   447
static const int sentinel_ = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   448
#define sentinel	((void *)&sentinel_)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   449
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   450
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   451
static int ll_require (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   452
  const char *name = luaL_checkstring(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   453
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   454
  lua_settop(L, 1);  /* _LOADED table will be at index 2 */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   455
  lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   456
  lua_getfield(L, 2, name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   457
  if (lua_toboolean(L, -1)) {  /* is it there? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   458
    if (lua_touserdata(L, -1) == sentinel)  /* check loops */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   459
      luaL_error(L, "loop or previous error loading module " LUA_QS, name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   460
    return 1;  /* package is already loaded */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   461
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   462
  /* else must load it; iterate over available loaders */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   463
  lua_getfield(L, LUA_ENVIRONINDEX, "loaders");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   464
  if (!lua_istable(L, -1))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   465
    luaL_error(L, LUA_QL("package.loaders") " must be a table");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   466
  lua_pushliteral(L, "");  /* error message accumulator */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   467
  for (i=1; ; i++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   468
    lua_rawgeti(L, -2, i);  /* get a loader */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   469
    if (lua_isnil(L, -1))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   470
      luaL_error(L, "module " LUA_QS " not found:%s",
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   471
                    name, lua_tostring(L, -2));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   472
    lua_pushstring(L, name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   473
    lua_call(L, 1, 1);  /* call it */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   474
    if (lua_isfunction(L, -1))  /* did it find module? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   475
      break;  /* module loaded successfully */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   476
    else if (lua_isstring(L, -1))  /* loader returned error message? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   477
      lua_concat(L, 2);  /* accumulate it */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   478
    else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   479
      lua_pop(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   480
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   481
  lua_pushlightuserdata(L, sentinel);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   482
  lua_setfield(L, 2, name);  /* _LOADED[name] = sentinel */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   483
  lua_pushstring(L, name);  /* pass name as argument to module */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   484
  lua_call(L, 1, 1);  /* run loaded module */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   485
  if (!lua_isnil(L, -1))  /* non-nil return? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   486
    lua_setfield(L, 2, name);  /* _LOADED[name] = returned value */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   487
  lua_getfield(L, 2, name);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   488
  if (lua_touserdata(L, -1) == sentinel) {   /* module did not set a value? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   489
    lua_pushboolean(L, 1);  /* use true as result */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   490
    lua_pushvalue(L, -1);  /* extra copy to be returned */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   491
    lua_setfield(L, 2, name);  /* _LOADED[name] = true */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   492
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   493
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   494
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   495
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   496
/* }====================================================== */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   497
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   498
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   499
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   500
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   501
** {======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   502
** 'module' function
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   503
** =======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   504
*/
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2812
diff changeset
   505
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   506
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   507
static void setfenv (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   508
  lua_Debug ar;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   509
  if (lua_getstack(L, 1, &ar) == 0 ||
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   510
      lua_getinfo(L, "f", &ar) == 0 ||  /* get calling function */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   511
      lua_iscfunction(L, -1))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   512
    luaL_error(L, LUA_QL("module") " not called from a Lua function");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   513
  lua_pushvalue(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   514
  lua_setfenv(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   515
  lua_pop(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   516
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   517
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   518
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   519
static void dooptions (lua_State *L, int n) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   520
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   521
  for (i = 2; i <= n; i++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   522
    lua_pushvalue(L, i);  /* get option (a function) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   523
    lua_pushvalue(L, -2);  /* module */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   524
    lua_call(L, 1, 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   525
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   526
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   527
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   528
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   529
static void modinit (lua_State *L, const char *modname) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   530
  const char *dot;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   531
  lua_pushvalue(L, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   532
  lua_setfield(L, -2, "_M");  /* module._M = module */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   533
  lua_pushstring(L, modname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   534
  lua_setfield(L, -2, "_NAME");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   535
  dot = strrchr(modname, '.');  /* look for last dot in module name */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   536
  if (dot == NULL) dot = modname;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   537
  else dot++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   538
  /* set _PACKAGE as package name (full module name minus last part) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   539
  lua_pushlstring(L, modname, dot - modname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   540
  lua_setfield(L, -2, "_PACKAGE");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   541
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   542
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   543
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   544
static int ll_module (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   545
  const char *modname = luaL_checkstring(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   546
  int loaded = lua_gettop(L) + 1;  /* index of _LOADED table */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   547
  lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   548
  lua_getfield(L, loaded, modname);  /* get _LOADED[modname] */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   549
  if (!lua_istable(L, -1)) {  /* not found? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   550
    lua_pop(L, 1);  /* remove previous result */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   551
    /* try global variable (and create one if it does not exist) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   552
    if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   553
      return luaL_error(L, "name conflict for module " LUA_QS, modname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   554
    lua_pushvalue(L, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   555
    lua_setfield(L, loaded, modname);  /* _LOADED[modname] = new table */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   556
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   557
  /* check whether table already has a _NAME field */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   558
  lua_getfield(L, -1, "_NAME");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   559
  if (!lua_isnil(L, -1))  /* is table an initialized module? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   560
    lua_pop(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   561
  else {  /* no; initialize it */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   562
    lua_pop(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   563
    modinit(L, modname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   564
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   565
  lua_pushvalue(L, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   566
  setfenv(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   567
  dooptions(L, loaded - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   568
  return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   569
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   570
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   571
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   572
static int ll_seeall (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   573
  luaL_checktype(L, 1, LUA_TTABLE);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   574
  if (!lua_getmetatable(L, 1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   575
    lua_createtable(L, 0, 1); /* create new metatable */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   576
    lua_pushvalue(L, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   577
    lua_setmetatable(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   578
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   579
  lua_pushvalue(L, LUA_GLOBALSINDEX);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   580
  lua_setfield(L, -2, "__index");  /* mt.__index = _G */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   581
  return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   582
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   583
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   584
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   585
/* }====================================================== */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   586
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   587
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   588
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   589
/* auxiliary mark (for internal use) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   590
#define AUXMARK		"\1"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   591
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   592
static void setpath (lua_State *L, const char *fieldname, const char *envname,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   593
                                   const char *def) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   594
  const char *path = getenv(envname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   595
  if (path == NULL)  /* no environment variable? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   596
    lua_pushstring(L, def);  /* use default */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   597
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   598
    /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   599
    path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   600
                              LUA_PATHSEP AUXMARK LUA_PATHSEP);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   601
    luaL_gsub(L, path, AUXMARK, def);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   602
    lua_remove(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   603
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   604
  setprogdir(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   605
  lua_setfield(L, -2, fieldname);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   606
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   607
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   608
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   609
static const luaL_Reg pk_funcs[] = {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   610
  {"loadlib", ll_loadlib},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   611
  {"seeall", ll_seeall},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   612
  {NULL, NULL}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   613
};
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   614
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   615
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   616
static const luaL_Reg ll_funcs[] = {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   617
  {"module", ll_module},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   618
  {"require", ll_require},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   619
  {NULL, NULL}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   620
};
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   621
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   622
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   623
static const lua_CFunction loaders[] =
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   624
  {loader_preload, loader_Lua, loader_C, loader_Croot, NULL};
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   625
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   626
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   627
LUALIB_API int luaopen_package (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   628
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   629
  /* create new type _LOADLIB */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   630
  luaL_newmetatable(L, "_LOADLIB");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   631
  lua_pushcfunction(L, gctm);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   632
  lua_setfield(L, -2, "__gc");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   633
  /* create `package' table */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   634
  luaL_register(L, LUA_LOADLIBNAME, pk_funcs);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2812
diff changeset
   635
#if defined(LUA_COMPAT_LOADLIB)
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   636
  lua_getfield(L, -1, "loadlib");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   637
  lua_setfield(L, LUA_GLOBALSINDEX, "loadlib");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   638
#endif
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   639
  lua_pushvalue(L, -1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   640
  lua_replace(L, LUA_ENVIRONINDEX);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   641
  /* create `loaders' table */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   642
  lua_createtable(L, 0, sizeof(loaders)/sizeof(loaders[0]) - 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   643
  /* fill it with pre-defined loaders */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   644
  for (i=0; loaders[i] != NULL; i++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   645
    lua_pushcfunction(L, loaders[i]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   646
    lua_rawseti(L, -2, i+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   647
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   648
  lua_setfield(L, -2, "loaders");  /* put it in field `loaders' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   649
  setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT);  /* set field `path' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   650
  setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   651
  /* store config information */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   652
  lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   653
                     LUA_EXECDIR "\n" LUA_IGMARK);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   654
  lua_setfield(L, -2, "config");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   655
  /* set field `loaded' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   656
  luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   657
  lua_setfield(L, -2, "loaded");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   658
  /* set field `preload' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   659
  lua_newtable(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   660
  lua_setfield(L, -2, "preload");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   661
  lua_pushvalue(L, LUA_GLOBALSINDEX);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   662
  luaL_register(L, NULL, ll_funcs);  /* open lib into global table */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   663
  lua_pop(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   664
  return 1;  /* return 'package' table */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   665
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   666