Repository: cloudwu/skynet Branch: master Commit: 2b6b106e57ab Files: 363 Total size: 2.3 MB Directory structure: gitextract_nfy1fgz2/ ├── .github/ │ ├── ISSUE_TEMPLATE/ │ │ └── readme-first.md │ └── workflows/ │ └── build-release.yml ├── .gitignore ├── .gitmodules ├── 3rd/ │ ├── compat-mingw/ │ │ ├── arpa/ │ │ │ └── inet.h │ │ ├── compat.c │ │ ├── compat.h │ │ ├── dlfcn.c │ │ ├── dlfcn.h │ │ ├── netdb.h │ │ ├── netinet/ │ │ │ ├── in.h │ │ │ └── tcp.h │ │ ├── sys/ │ │ │ ├── epoll.h │ │ │ ├── file.h │ │ │ └── socket.h │ │ ├── unistd.c │ │ ├── unistd.h │ │ ├── wepoll.c │ │ └── wepoll.h │ ├── lpeg/ │ │ ├── HISTORY │ │ ├── README.md │ │ ├── lpcap.c │ │ ├── lpcap.h │ │ ├── lpcode.c │ │ ├── lpcode.h │ │ ├── lpcset.c │ │ ├── lpcset.h │ │ ├── lpeg.html │ │ ├── lpprint.c │ │ ├── lpprint.h │ │ ├── lptree.c │ │ ├── lptree.h │ │ ├── lptypes.h │ │ ├── lpvm.c │ │ ├── lpvm.h │ │ ├── makefile │ │ ├── re.html │ │ ├── re.lua │ │ └── test.lua │ ├── lua/ │ │ ├── README │ │ ├── README.md │ │ ├── lapi.c │ │ ├── lapi.h │ │ ├── lauxlib.c │ │ ├── lauxlib.h │ │ ├── lbaselib.c │ │ ├── lcode.c │ │ ├── lcode.h │ │ ├── lcorolib.c │ │ ├── lctype.c │ │ ├── lctype.h │ │ ├── ldblib.c │ │ ├── ldebug.c │ │ ├── ldebug.h │ │ ├── ldo.c │ │ ├── ldo.h │ │ ├── ldump.c │ │ ├── lfunc.c │ │ ├── lfunc.h │ │ ├── lgc.c │ │ ├── lgc.h │ │ ├── linit.c │ │ ├── liolib.c │ │ ├── ljumptab.h │ │ ├── llex.c │ │ ├── llex.h │ │ ├── llimits.h │ │ ├── lmathlib.c │ │ ├── lmem.c │ │ ├── lmem.h │ │ ├── loadlib.c │ │ ├── lobject.c │ │ ├── lobject.h │ │ ├── lopcodes.c │ │ ├── lopcodes.h │ │ ├── lopnames.h │ │ ├── loslib.c │ │ ├── lparser.c │ │ ├── lparser.h │ │ ├── lprefix.h │ │ ├── lstate.c │ │ ├── lstate.h │ │ ├── lstring.c │ │ ├── lstring.h │ │ ├── lstrlib.c │ │ ├── ltable.c │ │ ├── ltable.h │ │ ├── ltablib.c │ │ ├── ltests.c │ │ ├── ltests.h │ │ ├── ltm.c │ │ ├── ltm.h │ │ ├── lua.c │ │ ├── lua.h │ │ ├── lua.hpp │ │ ├── luac.c │ │ ├── luaconf.h │ │ ├── lualib.h │ │ ├── lundump.c │ │ ├── lundump.h │ │ ├── lutf8lib.c │ │ ├── lvm.c │ │ ├── lvm.h │ │ ├── lzio.c │ │ ├── lzio.h │ │ ├── makefile │ │ └── onelua.c │ └── lua-md5/ │ ├── README │ ├── compat-5.2.c │ ├── compat-5.2.h │ ├── md5.c │ ├── md5.h │ └── md5lib.c ├── HISTORY.md ├── LICENSE ├── Makefile ├── README.md ├── examples/ │ ├── abort.lua │ ├── agent.lua │ ├── checkdeadloop.lua │ ├── client.lua │ ├── cluster1.lua │ ├── cluster2.lua │ ├── clustername.lua │ ├── config │ ├── config.c1 │ ├── config.c2 │ ├── config.handle │ ├── config.login │ ├── config.mc │ ├── config.mongodb │ ├── config.mysql │ ├── config.path │ ├── config.userlog │ ├── config_log │ ├── globallog.lua │ ├── injectlaunch.lua │ ├── login/ │ │ ├── client.lua │ │ ├── gated.lua │ │ ├── logind.lua │ │ ├── main.lua │ │ └── msgagent.lua │ ├── main.lua │ ├── main_log.lua │ ├── main_mongodb.lua │ ├── main_mysql.lua │ ├── preload.lua │ ├── proto.lua │ ├── protoloader.lua │ ├── share.lua │ ├── simpledb.lua │ ├── simplemonitor.lua │ ├── simpleweb.lua │ ├── simplewebsocket.lua │ ├── userlog.lua │ └── watchdog.lua ├── lualib/ │ ├── compat10/ │ │ ├── cluster.lua │ │ ├── crypt.lua │ │ ├── datacenter.lua │ │ ├── dns.lua │ │ ├── memory.lua │ │ ├── mongo.lua │ │ ├── mqueue.lua │ │ ├── multicast.lua │ │ ├── mysql.lua │ │ ├── netpack.lua │ │ ├── profile.lua │ │ ├── redis.lua │ │ ├── sharedata.lua │ │ ├── sharemap.lua │ │ ├── snax.lua │ │ ├── socket.lua │ │ ├── socketchannel.lua │ │ ├── socketdriver.lua │ │ └── stm.lua │ ├── http/ │ │ ├── httpc.lua │ │ ├── httpd.lua │ │ ├── internal.lua │ │ ├── sockethelper.lua │ │ ├── tlshelper.lua │ │ ├── url.lua │ │ └── websocket.lua │ ├── loader.lua │ ├── md5.lua │ ├── skynet/ │ │ ├── cluster.lua │ │ ├── coroutine.lua │ │ ├── datacenter.lua │ │ ├── datasheet/ │ │ │ ├── builder.lua │ │ │ ├── dump.lua │ │ │ └── init.lua │ │ ├── db/ │ │ │ ├── mongo/ │ │ │ │ └── transaction.lua │ │ │ ├── mongo.lua │ │ │ ├── mysql.lua │ │ │ ├── redis/ │ │ │ │ ├── cluster.lua │ │ │ │ └── crc16.lua │ │ │ └── redis.lua │ │ ├── debug.lua │ │ ├── dns.lua │ │ ├── harbor.lua │ │ ├── inject.lua │ │ ├── injectcode.lua │ │ ├── manager.lua │ │ ├── mqueue.lua │ │ ├── multicast.lua │ │ ├── queue.lua │ │ ├── remotedebug.lua │ │ ├── require.lua │ │ ├── service.lua │ │ ├── sharedata/ │ │ │ └── corelib.lua │ │ ├── sharedata.lua │ │ ├── sharemap.lua │ │ ├── sharetable.lua │ │ ├── snax.lua │ │ ├── socket.lua │ │ └── socketchannel.lua │ ├── skynet.lua │ ├── snax/ │ │ ├── gateserver.lua │ │ ├── hotfix.lua │ │ ├── interface.lua │ │ ├── loginserver.lua │ │ └── msgserver.lua │ ├── sproto.lua │ ├── sprotoloader.lua │ └── sprotoparser.lua ├── lualib-src/ │ ├── lsha1.c │ ├── ltls.c │ ├── lua-bson.c │ ├── lua-clientsocket.c │ ├── lua-cluster.c │ ├── lua-crypt.c │ ├── lua-datasheet.c │ ├── lua-debugchannel.c │ ├── lua-memory.c │ ├── lua-mongo.c │ ├── lua-multicast.c │ ├── lua-netpack.c │ ├── lua-seri.c │ ├── lua-seri.h │ ├── lua-sharedata.c │ ├── lua-sharetable.c │ ├── lua-skynet.c │ ├── lua-socket.c │ ├── lua-stm.c │ └── sproto/ │ ├── README │ ├── README.md │ ├── lsproto.c │ ├── msvcint.h │ ├── sproto.c │ └── sproto.h ├── mingw.mk ├── platform.mk ├── service/ │ ├── bootstrap.lua │ ├── cdummy.lua │ ├── clusteragent.lua │ ├── clusterd.lua │ ├── clusterproxy.lua │ ├── clustersender.lua │ ├── cmaster.lua │ ├── cmemory.lua │ ├── console.lua │ ├── cslave.lua │ ├── datacenterd.lua │ ├── dbg.lua │ ├── debug_agent.lua │ ├── debug_console.lua │ ├── gate.lua │ ├── launcher.lua │ ├── multicastd.lua │ ├── service_cell.lua │ ├── service_mgr.lua │ ├── service_provider.lua │ ├── sharedatad.lua │ └── snaxd.lua ├── service-src/ │ ├── databuffer.h │ ├── hashid.h │ ├── service_gate.c │ ├── service_harbor.c │ ├── service_logger.c │ └── service_snlua.c ├── skynet-src/ │ ├── atomic.h │ ├── malloc_hook.c │ ├── malloc_hook.h │ ├── mem_info.c │ ├── mem_info.h │ ├── rwlock.h │ ├── skynet.h │ ├── skynet_daemon.c │ ├── skynet_daemon.h │ ├── skynet_env.c │ ├── skynet_env.h │ ├── skynet_error.c │ ├── skynet_handle.c │ ├── skynet_handle.h │ ├── skynet_harbor.c │ ├── skynet_harbor.h │ ├── skynet_imp.h │ ├── skynet_log.c │ ├── skynet_log.h │ ├── skynet_main.c │ ├── skynet_malloc.h │ ├── skynet_module.c │ ├── skynet_module.h │ ├── skynet_monitor.c │ ├── skynet_monitor.h │ ├── skynet_mq.c │ ├── skynet_mq.h │ ├── skynet_server.c │ ├── skynet_server.h │ ├── skynet_socket.c │ ├── skynet_socket.h │ ├── skynet_start.c │ ├── skynet_timer.c │ ├── skynet_timer.h │ ├── socket_buffer.h │ ├── socket_epoll.h │ ├── socket_info.h │ ├── socket_kqueue.h │ ├── socket_poll.h │ ├── socket_server.c │ ├── socket_server.h │ └── spinlock.h └── test/ ├── pingserver.lua ├── sharemap.sp ├── testbson.lua ├── testcoroutine.lua ├── testcrypt.lua ├── testdatacenter.lua ├── testdatasheet.lua ├── testdeadcall.lua ├── testdeadloop.lua ├── testdns.lua ├── testecho.lua ├── testendless.lua ├── testhandle.lua ├── testharborlink.lua ├── testhttp.lua ├── testmemlimit.lua ├── testmongodb.lua ├── testmulticast.lua ├── testmulticast2.lua ├── testmysql.lua ├── testoverload.lua ├── testping.lua ├── testpipeline.lua ├── testqueue.lua ├── testredis.lua ├── testredis2.lua ├── testrediscluster.lua ├── testresponse.lua ├── testselect.lua ├── testservice/ │ ├── init.lua │ └── kvdb.lua ├── testsha.lua ├── testsharetable.lua ├── testsm.lua ├── testsocket.lua ├── teststm.lua ├── testterm.lua ├── testtimeout.lua ├── testtimer.lua ├── testtobeclosed.lua ├── testudp.lua └── time.lua ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/ISSUE_TEMPLATE/readme-first.md ================================================ --- name: Readme First about: Issues is bug report only title: '' labels: '' assignees: '' --- The **Issues** is for bug report only. Goto **Discussions** for feature request , questions, etc. **Update to master branch HEAD first**, and **Describe the bug** A clear and concise description of what the bug is. **To Reproduce** Steps to reproduce the behavior. **Additional context** Add any other context about the problem here. ================================================ FILE: .github/workflows/build-release.yml ================================================ name: Build and Release Skynet on: push: branches: [ master ] tags: [ 'v*' ] permissions: contents: write actions: read jobs: build: name: Build ${{ matrix.platform }} runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: include: - platform: windows os: ubuntu-latest container: debian:13 target: mingw artifact: skynet-windows.zip - platform: linux os: ubuntu-latest container: debian:13 target: linux artifact: skynet-linux.zip - platform: macosx os: macos-latest target: macosx artifact: skynet-macosx.zip - platform: freebsd os: ubuntu-latest container: debian:13 target: freebsd artifact: skynet-freebsd.zip container: ${{ matrix.container }} steps: - name: Install dependencies (Debian - Windows/Linux) if: matrix.container == 'debian:13' run: | apt-get update apt-get install -y --no-install-recommends \ build-essential \ make \ pkg-config \ mingw-w64 \ mingw-w64-tools \ mingw-w64-i686-dev \ mingw-w64-x86-64-dev \ autoconf \ automake \ libtool \ git \ zip \ ca-certificates \ curl \ libreadline-dev \ libedit-dev \ rsync - name: Install dependencies (macOS) if: matrix.platform == 'macosx' run: | # macOS usually has most build tools pre-installed # Install any additional dependencies if needed brew install autoconf automake libtool || true - name: Install dependencies (FreeBSD) if: matrix.platform == 'freebsd' run: | # FreeBSD build using standard build tools (cross-compilation compatible) apt-get update apt-get install -y --no-install-recommends \ build-essential \ make \ pkg-config \ autoconf \ automake \ libtool \ git \ zip \ ca-certificates \ curl \ libreadline-dev \ libedit-dev \ rsync - name: Update CA certificates (Debian containers) if: matrix.container == 'debian:13' run: | update-ca-certificates - name: Configure Git SSL (Debian containers) if: matrix.container == 'debian:13' run: | git config --global http.sslverify true git config --global http.sslcainfo /etc/ssl/certs/ca-certificates.crt - name: Checkout code uses: actions/checkout@v6 with: submodules: recursive fetch-depth: 1 - name: Build ${{ matrix.platform }} run: | make cleanall make ${{ matrix.target }} - name: Prepare build files run: | mkdir -p build-output # Copy all files except .git and .github with ignore-errors flag - name: Clean and recreate directory run: | rm -rf build-output/ mkdir -p build-output/ - name: Sync files to build-output excluding specific directories run: | rsync -av --ignore-errors --exclude='.git*' --exclude='.github' --exclude='build-output' . build-output/ - name: Upload artifact uses: actions/upload-artifact@v4 with: name: ${{ matrix.artifact }} path: build-output/ retention-days: 30 release: name: Create Release needs: build runs-on: ubuntu-latest if: startsWith(github.ref, 'refs/tags/v') steps: - name: Download all artifacts uses: actions/download-artifact@v4 with: path: artifacts - name: Prepare release assets run: | mkdir -p release-assets # Copy zip files from artifact directories to release assets for artifact in skynet-windows.zip skynet-linux.zip skynet-macosx.zip skynet-freebsd.zip; do if [ -d "artifacts/${artifact}" ]; then # The artifacts are already organized, just copy them cp -r "artifacts/${artifact}/"* "release-assets/" 2>/dev/null || true # Or if we want to create new zip files with consistent naming platform=$(echo ${artifact} | sed 's/skynet-\(.*\)\.zip/\1/') cd "artifacts/${artifact}" zip -r "../../release-assets/skynet-${platform}.zip" . cd ../.. fi done ls -la release-assets/ - name: Create Release uses: softprops/action-gh-release@v1 with: files: release-assets/*.zip generate_release_notes: true draft: false prerelease: false env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} ================================================ FILE: .gitignore ================================================ *.o *.a /skynet /skynet.pid 3rd/lua/lua 3rd/lua/luac 3rd/lua/all /cservice /luaclib *.so *.dSYM .DS_Store .vscode 3rd/lua/lua.exe 3rd/lua/luac.exe 3rd/lua/lua54.dll skynet.exe *.dll ================================================ FILE: .gitmodules ================================================ [submodule "3rd/jemalloc"] path = 3rd/jemalloc url = https://github.com/jemalloc/jemalloc.git ================================================ FILE: 3rd/compat-mingw/arpa/inet.h ================================================ #pragma once ================================================ FILE: 3rd/compat-mingw/compat.c ================================================ #include "compat.h" #include "dlfcn.c" #include "unistd.c" #include "wepoll.c" ================================================ FILE: 3rd/compat-mingw/compat.h ================================================ #pragma once #include "unistd.h" #include "dlfcn.h" ================================================ FILE: 3rd/compat-mingw/dlfcn.c ================================================ #include "dlfcn.h" #define WIN32_LEAN_AND_MEAN #include void *dlopen(const char *path, int flag) { return LoadLibraryA(path); } const char *dlerror() { DWORD err = GetLastError(); HLOCAL LocalAddress = NULL; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, (PTSTR)&LocalAddress, 0, NULL); return (LPSTR)LocalAddress; } void *dlsym(void *dl, const char *sym) { return GetProcAddress(dl, sym); } ================================================ FILE: 3rd/compat-mingw/dlfcn.h ================================================ #pragma once enum { RTLD_NOW, RTLD_GLOBAL }; void *dlopen(const char *path, int flag); const char *dlerror(); void *dlsym(void *dl, const char *sym); ================================================ FILE: 3rd/compat-mingw/netdb.h ================================================ #pragma once #include ================================================ FILE: 3rd/compat-mingw/netinet/in.h ================================================ #pragma once ================================================ FILE: 3rd/compat-mingw/netinet/tcp.h ================================================ #pragma once ================================================ FILE: 3rd/compat-mingw/sys/epoll.h ================================================ #pragma once #include "wepoll.h" ================================================ FILE: 3rd/compat-mingw/sys/file.h ================================================ #pragma once ================================================ FILE: 3rd/compat-mingw/sys/socket.h ================================================ #pragma once #define _WINSOCK_DEPRECATED_NO_WARNINGS #define WIN32_LEAN_AND_MEAN #undef FD_SETSIZE #define FD_SETSIZE 1024 #include #include #include #include #include #include "socket_poll.h" #include "socket_epoll.h" ================================================ FILE: 3rd/compat-mingw/unistd.c ================================================ #include "unistd.h" #define _WINSOCK_DEPRECATED_NO_WARNINGS #define WIN32_LEAN_AND_MEAN #include #include #include #include #include #include // WSA error to errno mapping function static void set_errno_from_wsa_error(int wsa_error) { switch (wsa_error) { case WSAECONNRESET: errno = ECONNRESET; break; case WSAECONNABORTED: errno = ECONNABORTED; break; case WSAECONNREFUSED: errno = ECONNREFUSED; break; case WSAENETDOWN: errno = ENETDOWN; break; case WSAENETUNREACH: errno = ENETUNREACH; break; case WSAEHOSTDOWN: errno = EHOSTDOWN; break; case WSAEHOSTUNREACH: errno = EHOSTUNREACH; break; case WSAETIMEDOUT: errno = ETIMEDOUT; break; case WSAENOTCONN: errno = ENOTCONN; break; case WSAEWOULDBLOCK: errno = EAGAIN; break; case WSAEINTR: errno = EINTR; break; case WSAEINVAL: errno = EINVAL; break; case WSAEACCES: errno = EACCES; break; case WSAEADDRINUSE: errno = EADDRINUSE; break; case WSAEADDRNOTAVAIL: errno = EADDRNOTAVAIL; break; default: errno = EIO; // Generic I/O error for unknown cases break; } } // Windows Socket initialization static int winsock_initialized = 0; static int init_winsock(void) { if (!winsock_initialized) { WSADATA wsaData; int result = WSAStartup(MAKEWORD(2, 2), &wsaData); if (result != 0) { return -1; } winsock_initialized = 1; } return 0; } static void cleanup_winsock(void) { if (winsock_initialized) { WSACleanup(); winsock_initialized = 0; } } // Auto-initialize Winsock when the library is loaded __attribute__((constructor)) static void auto_init_winsock(void) { init_winsock(); } // Auto-cleanup Winsock when the library is unloaded __attribute__((destructor)) static void auto_cleanup_winsock(void) { cleanup_winsock(); } static LONGLONG get_cpu_freq() { LARGE_INTEGER freq; QueryPerformanceFrequency(&freq); return freq.QuadPart; } int kill(pid_t pid, int exit_code) { return TerminateProcess((HANDLE)(uintptr_t)pid, exit_code); } #define NANOSEC 1000000000 #define MICROSEC 1000000 void usleep(size_t us) { if (us > 1000) { Sleep(us / 1000); return; } LONGLONG delta = get_cpu_freq() / MICROSEC * us; LARGE_INTEGER counter; QueryPerformanceCounter(&counter); LONGLONG start = counter.QuadPart; for (;;) { QueryPerformanceCounter(&counter); if (counter.QuadPart - start >= delta) return; } } void sleep(size_t sec) { Sleep(sec * 1000UL); } int clock_gettime(int what, struct timespec* ti) { switch (what) { case CLOCK_MONOTONIC: static __int64 Freq = 0; static __int64 Start = 0; static __int64 StartTime = 0; if (Freq == 0) { StartTime = time(NULL); QueryPerformanceFrequency((LARGE_INTEGER*)&Freq); QueryPerformanceCounter((LARGE_INTEGER*)&Start); } __int64 Count = 0; QueryPerformanceCounter((LARGE_INTEGER*)&Count); // 乘以1000,把秒化为毫秒 __int64 now = (__int64)((double)(Count - Start) / (double)Freq * 1000.0) + StartTime * 1000; ti->tv_sec = now / 1000; ti->tv_nsec = (now - now / 1000 * 1000) * 1000 * 1000; return 0; case CLOCK_REALTIME: SYSTEMTIME st; GetSystemTime(&st); // 获取 UTC 时间 // 将 SYSTEMTIME 转换为 UNIX 时间戳 FILETIME ft; SystemTimeToFileTime(&st, &ft); ULARGE_INTEGER u64; u64.LowPart = ft.dwLowDateTime; u64.HighPart = ft.dwHighDateTime; ti->tv_sec = (uint32_t)((u64.QuadPart - 116444736000000000ULL) / 10000000); // 转换为秒 ti->tv_nsec = (uint32_t)((u64.QuadPart % 10000000) * 100); // 获取纳秒部分 return 0; // 响应成功 case CLOCK_THREAD_CPUTIME_ID: // 获取当前线程的 CPU 时间 FILETIME creation_time, exit_time, kernel_time, user_time; if (GetThreadTimes(GetCurrentThread(), &creation_time, &exit_time, &kernel_time, &user_time)) { ULARGE_INTEGER u64; u64.LowPart = user_time.dwLowDateTime; u64.HighPart = user_time.dwHighDateTime; ti->tv_sec = (uint32_t)((u64.QuadPart - 116444736000000000ULL) / 10000000); // 转换为秒 ti->tv_nsec = (uint32_t)((u64.QuadPart % 10000000) * 100); // 获取纳秒部分 return 0; } else { return -1; // 获取失败 } } return -1; } int flock(int fd, int flag) { // Not implemented return 3; } int fcntl(int fd, int cmd, long arg) { if (cmd == F_GETFL) return 0; if (cmd == F_SETFL && arg == O_NONBLOCK) { u_long ulOption = 1; ioctlsocket(fd, FIONBIO, &ulOption); } return 1; } void sigfillset(int* flag) { // Not implemented } int sigemptyset(int* set) { /*Not implemented*/ return 0; } void sigaction(int flag, struct sigaction* action, void* param) { // Not implemented } static void socket_keepalive(int fd) { int keepalive = 1; int ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void*)&keepalive, sizeof(keepalive)); assert(ret != SOCKET_ERROR); } int pipe(int fd[2]) { int listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (listen_fd == INVALID_SOCKET) { return -1; } struct sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); srand(time(NULL)); // use random port(range from 60000 to 60999) to simulate pipe() int port; for (;;) { port = 60000 + rand() % 1000; sin.sin_port = htons(port); if (!bind(listen_fd, (struct sockaddr*)&sin, sizeof(sin))) break; } if (listen(listen_fd, 5) == SOCKET_ERROR) { closesocket(listen_fd); return -1; } socket_keepalive(listen_fd); int client_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (client_fd == INVALID_SOCKET) { closesocket(listen_fd); return -1; } if (connect(client_fd, (struct sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR) { closesocket(listen_fd); closesocket(client_fd); return -1; } struct sockaddr_in client_addr; size_t name_len = sizeof(client_addr); int client_sock = accept(listen_fd, (struct sockaddr*)&client_addr, &name_len); if (client_sock == INVALID_SOCKET) { closesocket(listen_fd); closesocket(client_fd); return -1; } closesocket(listen_fd); // Close listen socket as it's no longer needed fd[0] = client_sock; fd[1] = client_fd; socket_keepalive(client_sock); socket_keepalive(client_fd); return 0; } int write(int fd, const void* ptr, unsigned int sz) { WSABUF vecs[1]; vecs[0].buf = (char*)ptr; vecs[0].len = sz; DWORD bytesSent; if (WSASend(fd, vecs, 1, &bytesSent, 0, NULL, NULL)) { int wsa_error = WSAGetLastError(); set_errno_from_wsa_error(wsa_error); return -1; } else { return bytesSent; } } int read(int fd, void* buffer, unsigned int sz) { WSABUF vecs[1]; vecs[0].buf = buffer; vecs[0].len = sz; DWORD bytesRecv = 0; DWORD flags = 0; if (WSARecv(fd, vecs, 1, &bytesRecv, &flags, NULL, NULL)) { int wsa_error = WSAGetLastError(); if (wsa_error == WSAECONNRESET) { return 0; // Connection closed by peer } // Map WSA error to errno for better error reporting set_errno_from_wsa_error(wsa_error); return -1; } else { return bytesRecv; } } // Wrapper for recv function with better error handling int compat_recv(SOCKET s, char *buf, int len, int flags) { WSABUF vecs[1]; vecs[0].buf = buf; vecs[0].len = len; DWORD bytesRecv = 0; DWORD wsaFlags = 0; if (WSARecv(s, vecs, 1, &bytesRecv, &wsaFlags, NULL, NULL)) { int wsa_error = WSAGetLastError(); // Handle non-blocking operations - these are not real errors if (wsa_error == WSAEWOULDBLOCK || wsa_error == WSAEINTR) { // For non-blocking sockets, this is normal - no data available right now set_errno_from_wsa_error(wsa_error); return -1; // Caller should check errno == EAGAIN } if (wsa_error == WSAECONNRESET) { return 0; // Connection closed by peer } // Map WSA error to errno for better error reporting set_errno_from_wsa_error(wsa_error); return -1; } else { return bytesRecv; } } int close(int fd) { shutdown(fd, SD_BOTH); return closesocket(fd); } int daemon(int a, int b) { // Not implemented return 0; } char* strsep(char** stringp, const char* delim) { char* s; const char* spanp; int c, sc; char* tok; if ((s = *stringp) == NULL) return (NULL); for (tok = s;;) { c = *s++; spanp = delim; do { if ((sc = *spanp++) == c) { if (c == 0) s = NULL; else s[-1] = 0; *stringp = s; return (tok); } } while (sc != 0); } /* NOTREACHED */ } ================================================ FILE: 3rd/compat-mingw/unistd.h ================================================ #pragma once #include #include #include #include #include #include #include #include // Define missing errno values for network errors #ifndef ECONNRESET #define ECONNRESET 104 #endif #ifndef ECONNABORTED #define ECONNABORTED 103 #endif #ifndef ECONNREFUSED #define ECONNREFUSED 111 #endif #ifndef ENETDOWN #define ENETDOWN 100 #endif #ifndef ENETUNREACH #define ENETUNREACH 101 #endif #ifndef EHOSTDOWN #define EHOSTDOWN 112 #endif #ifndef EHOSTUNREACH #define EHOSTUNREACH 113 #endif #ifndef ETIMEDOUT #define ETIMEDOUT 110 #endif #ifndef ENOTCONN #define ENOTCONN 107 #endif #ifndef EADDRINUSE #define EADDRINUSE 98 #endif #ifndef EADDRNOTAVAIL #define EADDRNOTAVAIL 99 #endif // Include winsock2.h for gethostname and other network functions #define _WINSOCK_DEPRECATED_NO_WARNINGS #define WIN32_LEAN_AND_MEAN #include #include // Undefine Windows legacy keywords that conflict with variable names #ifdef near #undef near #endif #ifdef far #undef far #endif // Socket compatibility macros #define SHUT_RD SD_RECEIVE #define SHUT_WR SD_SEND #define SHUT_RDWR SD_BOTH #define ssize_t size_t #define random rand #define srandom srand #define snprintf _snprintf #define localtime_r _localtime64_s #define pid_t int int kill(pid_t pid, int exit_code); void usleep(size_t us); void sleep(size_t ms); int clock_gettime(int what, struct timespec *ti); enum { LOCK_EX, LOCK_NB }; int flock(int fd, int flag); struct sigaction { void (*sa_handler)(int); int sa_flags; int sa_mask; }; enum { SIGPIPE, SIGHUP, SA_RESTART }; void sigfillset(int *flag); int sigemptyset(int* set); void sigaction(int flag, struct sigaction *action, void* param); int pipe(int fd[2]); int daemon(int a, int b); #define O_NONBLOCK 1 #define F_SETFL 0 #define F_GETFL 1 int fcntl(int fd, int cmd, long arg); char *strsep(char **stringp, const char *delim); int write(int fd, const void* ptr, unsigned int sz); int read(int fd, void* buffer, unsigned int sz); // Wrapper function for recv with better error handling int compat_recv(SOCKET s, char *buf, int len, int flags); // Macro to redirect recv calls to our wrapper #define recv compat_recv int close(int fd); #define getpid _getpid #define open _open #define dup2 _dup2 ================================================ FILE: 3rd/compat-mingw/wepoll.c ================================================ /* * wepoll - epoll for Windows * https://github.com/piscisaureus/wepoll * * Copyright 2012-2020, Bert Belder * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef WEPOLL_EXPORT #define WEPOLL_EXPORT #endif #include enum EPOLL_EVENTS { EPOLLIN = (int) (1U << 0), EPOLLPRI = (int) (1U << 1), EPOLLOUT = (int) (1U << 2), EPOLLERR = (int) (1U << 3), EPOLLHUP = (int) (1U << 4), EPOLLRDNORM = (int) (1U << 6), EPOLLRDBAND = (int) (1U << 7), EPOLLWRNORM = (int) (1U << 8), EPOLLWRBAND = (int) (1U << 9), EPOLLMSG = (int) (1U << 10), /* Never reported. */ EPOLLRDHUP = (int) (1U << 13), EPOLLONESHOT = (int) (1U << 31) }; #define EPOLLIN (1U << 0) #define EPOLLPRI (1U << 1) #define EPOLLOUT (1U << 2) #define EPOLLERR (1U << 3) #define EPOLLHUP (1U << 4) #define EPOLLRDNORM (1U << 6) #define EPOLLRDBAND (1U << 7) #define EPOLLWRNORM (1U << 8) #define EPOLLWRBAND (1U << 9) #define EPOLLMSG (1U << 10) #define EPOLLRDHUP (1U << 13) #define EPOLLONESHOT (1U << 31) #define EPOLL_CTL_ADD 1 #define EPOLL_CTL_MOD 2 #define EPOLL_CTL_DEL 3 typedef void* HANDLE; typedef uintptr_t SOCKET; typedef union epoll_data { void* ptr; int fd; uint32_t u32; uint64_t u64; SOCKET sock; /* Windows specific */ HANDLE hnd; /* Windows specific */ } epoll_data_t; struct epoll_event { uint32_t events; /* Epoll events and flags */ epoll_data_t data; /* User data variable */ }; #ifdef __cplusplus extern "C" { #endif WEPOLL_EXPORT HANDLE epoll_create(int size); WEPOLL_EXPORT HANDLE epoll_create1(int flags); WEPOLL_EXPORT int epoll_close(HANDLE ephnd); WEPOLL_EXPORT int epoll_ctl(HANDLE ephnd, int op, SOCKET sock, struct epoll_event* event); WEPOLL_EXPORT int epoll_wait(HANDLE ephnd, struct epoll_event* events, int maxevents, int timeout); #ifdef __cplusplus } /* extern "C" */ #endif #include #include #define WEPOLL_INTERNAL static #define WEPOLL_INTERNAL_EXTERN static #if defined(__clang__) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wnonportable-system-include-path" #pragma clang diagnostic ignored "-Wreserved-id-macro" #elif defined(_MSC_VER) #pragma warning(push, 1) #endif #undef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #undef _WIN32_WINNT #define _WIN32_WINNT 0x0600 #include #include #include #if defined(__clang__) #pragma clang diagnostic pop #elif defined(_MSC_VER) #pragma warning(pop) #endif WEPOLL_INTERNAL int nt_global_init(void); typedef LONG NTSTATUS; typedef NTSTATUS* PNTSTATUS; #ifndef NT_SUCCESS #define NT_SUCCESS(status) (((NTSTATUS)(status)) >= 0) #endif #ifndef STATUS_SUCCESS #define STATUS_SUCCESS ((NTSTATUS) 0x00000000L) #endif #ifndef STATUS_PENDING #define STATUS_PENDING ((NTSTATUS) 0x00000103L) #endif #ifndef STATUS_CANCELLED #define STATUS_CANCELLED ((NTSTATUS) 0xC0000120L) #endif #ifndef STATUS_NOT_FOUND #define STATUS_NOT_FOUND ((NTSTATUS) 0xC0000225L) #endif typedef struct _IO_STATUS_BLOCK { NTSTATUS Status; ULONG_PTR Information; } IO_STATUS_BLOCK, *PIO_STATUS_BLOCK; typedef VOID(NTAPI* PIO_APC_ROUTINE)(PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock, ULONG Reserved); typedef struct _UNICODE_STRING { USHORT Length; USHORT MaximumLength; PWSTR Buffer; } UNICODE_STRING, *PUNICODE_STRING; #define RTL_CONSTANT_STRING(s) \ { sizeof(s) - sizeof((s)[0]), sizeof(s), s } typedef struct _OBJECT_ATTRIBUTES { ULONG Length; HANDLE RootDirectory; PUNICODE_STRING ObjectName; ULONG Attributes; PVOID SecurityDescriptor; PVOID SecurityQualityOfService; } OBJECT_ATTRIBUTES, *POBJECT_ATTRIBUTES; #define RTL_CONSTANT_OBJECT_ATTRIBUTES(ObjectName, Attributes) \ { sizeof(OBJECT_ATTRIBUTES), NULL, ObjectName, Attributes, NULL, NULL } #ifndef FILE_OPEN #define FILE_OPEN 0x00000001UL #endif #define KEYEDEVENT_WAIT 0x00000001UL #define KEYEDEVENT_WAKE 0x00000002UL #define KEYEDEVENT_ALL_ACCESS \ (STANDARD_RIGHTS_REQUIRED | KEYEDEVENT_WAIT | KEYEDEVENT_WAKE) #define NT_NTDLL_IMPORT_LIST(X) \ X(NTSTATUS, \ NTAPI, \ NtCancelIoFileEx, \ (HANDLE FileHandle, \ PIO_STATUS_BLOCK IoRequestToCancel, \ PIO_STATUS_BLOCK IoStatusBlock)) \ \ X(NTSTATUS, \ NTAPI, \ NtCreateFile, \ (PHANDLE FileHandle, \ ACCESS_MASK DesiredAccess, \ POBJECT_ATTRIBUTES ObjectAttributes, \ PIO_STATUS_BLOCK IoStatusBlock, \ PLARGE_INTEGER AllocationSize, \ ULONG FileAttributes, \ ULONG ShareAccess, \ ULONG CreateDisposition, \ ULONG CreateOptions, \ PVOID EaBuffer, \ ULONG EaLength)) \ \ X(NTSTATUS, \ NTAPI, \ NtCreateKeyedEvent, \ (PHANDLE KeyedEventHandle, \ ACCESS_MASK DesiredAccess, \ POBJECT_ATTRIBUTES ObjectAttributes, \ ULONG Flags)) \ \ X(NTSTATUS, \ NTAPI, \ NtDeviceIoControlFile, \ (HANDLE FileHandle, \ HANDLE Event, \ PIO_APC_ROUTINE ApcRoutine, \ PVOID ApcContext, \ PIO_STATUS_BLOCK IoStatusBlock, \ ULONG IoControlCode, \ PVOID InputBuffer, \ ULONG InputBufferLength, \ PVOID OutputBuffer, \ ULONG OutputBufferLength)) \ \ X(NTSTATUS, \ NTAPI, \ NtReleaseKeyedEvent, \ (HANDLE KeyedEventHandle, \ PVOID KeyValue, \ BOOLEAN Alertable, \ PLARGE_INTEGER Timeout)) \ \ X(NTSTATUS, \ NTAPI, \ NtWaitForKeyedEvent, \ (HANDLE KeyedEventHandle, \ PVOID KeyValue, \ BOOLEAN Alertable, \ PLARGE_INTEGER Timeout)) \ \ X(ULONG, WINAPI, RtlNtStatusToDosError, (NTSTATUS Status)) #define X(return_type, attributes, name, parameters) \ WEPOLL_INTERNAL_EXTERN return_type(attributes* name) parameters; NT_NTDLL_IMPORT_LIST(X) #undef X #define AFD_POLL_RECEIVE 0x0001 #define AFD_POLL_RECEIVE_EXPEDITED 0x0002 #define AFD_POLL_SEND 0x0004 #define AFD_POLL_DISCONNECT 0x0008 #define AFD_POLL_ABORT 0x0010 #define AFD_POLL_LOCAL_CLOSE 0x0020 #define AFD_POLL_ACCEPT 0x0080 #define AFD_POLL_CONNECT_FAIL 0x0100 typedef struct _AFD_POLL_HANDLE_INFO { HANDLE Handle; ULONG Events; NTSTATUS Status; } AFD_POLL_HANDLE_INFO, *PAFD_POLL_HANDLE_INFO; typedef struct _AFD_POLL_INFO { LARGE_INTEGER Timeout; ULONG NumberOfHandles; ULONG Exclusive; AFD_POLL_HANDLE_INFO Handles[1]; } AFD_POLL_INFO, *PAFD_POLL_INFO; WEPOLL_INTERNAL int afd_create_device_handle(HANDLE iocp_handle, HANDLE* afd_device_handle_out); WEPOLL_INTERNAL int afd_poll(HANDLE afd_device_handle, AFD_POLL_INFO* poll_info, IO_STATUS_BLOCK* io_status_block); WEPOLL_INTERNAL int afd_cancel_poll(HANDLE afd_device_handle, IO_STATUS_BLOCK* io_status_block); #define return_map_error(value) \ do { \ err_map_win_error(); \ return (value); \ } while (0) #define return_set_error(value, error) \ do { \ err_set_win_error(error); \ return (value); \ } while (0) WEPOLL_INTERNAL void err_map_win_error(void); WEPOLL_INTERNAL void err_set_win_error(DWORD error); WEPOLL_INTERNAL int err_check_handle(HANDLE handle); #define IOCTL_AFD_POLL 0x00012024 static UNICODE_STRING afd__device_name = RTL_CONSTANT_STRING(L"\\Device\\Afd\\Wepoll"); static OBJECT_ATTRIBUTES afd__device_attributes = RTL_CONSTANT_OBJECT_ATTRIBUTES(&afd__device_name, 0); int afd_create_device_handle(HANDLE iocp_handle, HANDLE* afd_device_handle_out) { HANDLE afd_device_handle; IO_STATUS_BLOCK iosb; NTSTATUS status; /* By opening \Device\Afd without specifying any extended attributes, we'll * get a handle that lets us talk to the AFD driver, but that doesn't have an * associated endpoint (so it's not a socket). */ status = NtCreateFile(&afd_device_handle, SYNCHRONIZE, &afd__device_attributes, &iosb, NULL, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OPEN, 0, NULL, 0); if (status != STATUS_SUCCESS) return_set_error(-1, RtlNtStatusToDosError(status)); if (CreateIoCompletionPort(afd_device_handle, iocp_handle, 0, 0) == NULL) goto error; if (!SetFileCompletionNotificationModes(afd_device_handle, FILE_SKIP_SET_EVENT_ON_HANDLE)) goto error; *afd_device_handle_out = afd_device_handle; return 0; error: CloseHandle(afd_device_handle); return_map_error(-1); } int afd_poll(HANDLE afd_device_handle, AFD_POLL_INFO* poll_info, IO_STATUS_BLOCK* io_status_block) { NTSTATUS status; /* Blocking operation is not supported. */ assert(io_status_block != NULL); io_status_block->Status = STATUS_PENDING; status = NtDeviceIoControlFile(afd_device_handle, NULL, NULL, io_status_block, io_status_block, IOCTL_AFD_POLL, poll_info, sizeof *poll_info, poll_info, sizeof *poll_info); if (status == STATUS_SUCCESS) return 0; else if (status == STATUS_PENDING) return_set_error(-1, ERROR_IO_PENDING); else return_set_error(-1, RtlNtStatusToDosError(status)); } int afd_cancel_poll(HANDLE afd_device_handle, IO_STATUS_BLOCK* io_status_block) { NTSTATUS cancel_status; IO_STATUS_BLOCK cancel_iosb; /* If the poll operation has already completed or has been cancelled earlier, * there's nothing left for us to do. */ if (io_status_block->Status != STATUS_PENDING) return 0; cancel_status = NtCancelIoFileEx(afd_device_handle, io_status_block, &cancel_iosb); /* NtCancelIoFileEx() may return STATUS_NOT_FOUND if the operation completed * just before calling NtCancelIoFileEx(). This is not an error. */ if (cancel_status == STATUS_SUCCESS || cancel_status == STATUS_NOT_FOUND) return 0; else return_set_error(-1, RtlNtStatusToDosError(cancel_status)); } WEPOLL_INTERNAL int epoll_global_init(void); WEPOLL_INTERNAL int init(void); typedef struct port_state port_state_t; typedef struct queue queue_t; typedef struct sock_state sock_state_t; typedef struct ts_tree_node ts_tree_node_t; WEPOLL_INTERNAL port_state_t* port_new(HANDLE* iocp_handle_out); WEPOLL_INTERNAL int port_close(port_state_t* port_state); WEPOLL_INTERNAL int port_delete(port_state_t* port_state); WEPOLL_INTERNAL int port_wait(port_state_t* port_state, struct epoll_event* events, int maxevents, int timeout); WEPOLL_INTERNAL int port_ctl(port_state_t* port_state, int op, SOCKET sock, struct epoll_event* ev); WEPOLL_INTERNAL int port_register_socket(port_state_t* port_state, sock_state_t* sock_state, SOCKET socket); WEPOLL_INTERNAL void port_unregister_socket(port_state_t* port_state, sock_state_t* sock_state); WEPOLL_INTERNAL sock_state_t* port_find_socket(port_state_t* port_state, SOCKET socket); WEPOLL_INTERNAL void port_request_socket_update(port_state_t* port_state, sock_state_t* sock_state); WEPOLL_INTERNAL void port_cancel_socket_update(port_state_t* port_state, sock_state_t* sock_state); WEPOLL_INTERNAL void port_add_deleted_socket(port_state_t* port_state, sock_state_t* sock_state); WEPOLL_INTERNAL void port_remove_deleted_socket(port_state_t* port_state, sock_state_t* sock_state); WEPOLL_INTERNAL HANDLE port_get_iocp_handle(port_state_t* port_state); WEPOLL_INTERNAL queue_t* port_get_poll_group_queue(port_state_t* port_state); WEPOLL_INTERNAL port_state_t* port_state_from_handle_tree_node( ts_tree_node_t* tree_node); WEPOLL_INTERNAL ts_tree_node_t* port_state_to_handle_tree_node( port_state_t* port_state); /* The reflock is a special kind of lock that normally prevents a chunk of * memory from being freed, but does allow the chunk of memory to eventually be * released in a coordinated fashion. * * Under normal operation, threads increase and decrease the reference count, * which are wait-free operations. * * Exactly once during the reflock's lifecycle, a thread holding a reference to * the lock may "destroy" the lock; this operation blocks until all other * threads holding a reference to the lock have dereferenced it. After * "destroy" returns, the calling thread may assume that no other threads have * a reference to the lock. * * Attemmpting to lock or destroy a lock after reflock_unref_and_destroy() has * been called is invalid and results in undefined behavior. Therefore the user * should use another lock to guarantee that this can't happen. */ typedef struct reflock { volatile long state; /* 32-bit Interlocked APIs operate on `long` values. */ } reflock_t; WEPOLL_INTERNAL int reflock_global_init(void); WEPOLL_INTERNAL void reflock_init(reflock_t* reflock); WEPOLL_INTERNAL void reflock_ref(reflock_t* reflock); WEPOLL_INTERNAL void reflock_unref(reflock_t* reflock); WEPOLL_INTERNAL void reflock_unref_and_destroy(reflock_t* reflock); #include /* N.b.: the tree functions do not set errno or LastError when they fail. Each * of the API functions has at most one failure mode. It is up to the caller to * set an appropriate error code when necessary. */ typedef struct tree tree_t; typedef struct tree_node tree_node_t; typedef struct tree { tree_node_t* root; } tree_t; typedef struct tree_node { tree_node_t* left; tree_node_t* right; tree_node_t* parent; uintptr_t key; bool red; } tree_node_t; WEPOLL_INTERNAL void tree_init(tree_t* tree); WEPOLL_INTERNAL void tree_node_init(tree_node_t* node); WEPOLL_INTERNAL int tree_add(tree_t* tree, tree_node_t* node, uintptr_t key); WEPOLL_INTERNAL void tree_del(tree_t* tree, tree_node_t* node); WEPOLL_INTERNAL tree_node_t* tree_find(const tree_t* tree, uintptr_t key); WEPOLL_INTERNAL tree_node_t* tree_root(const tree_t* tree); typedef struct ts_tree { tree_t tree; SRWLOCK lock; } ts_tree_t; typedef struct ts_tree_node { tree_node_t tree_node; reflock_t reflock; } ts_tree_node_t; WEPOLL_INTERNAL void ts_tree_init(ts_tree_t* rtl); WEPOLL_INTERNAL void ts_tree_node_init(ts_tree_node_t* node); WEPOLL_INTERNAL int ts_tree_add(ts_tree_t* ts_tree, ts_tree_node_t* node, uintptr_t key); WEPOLL_INTERNAL ts_tree_node_t* ts_tree_del_and_ref(ts_tree_t* ts_tree, uintptr_t key); WEPOLL_INTERNAL ts_tree_node_t* ts_tree_find_and_ref(ts_tree_t* ts_tree, uintptr_t key); WEPOLL_INTERNAL void ts_tree_node_unref(ts_tree_node_t* node); WEPOLL_INTERNAL void ts_tree_node_unref_and_destroy(ts_tree_node_t* node); static ts_tree_t epoll__handle_tree; int epoll_global_init(void) { ts_tree_init(&epoll__handle_tree); return 0; } static HANDLE epoll__create(void) { port_state_t* port_state; HANDLE ephnd; ts_tree_node_t* tree_node; if (init() < 0) return NULL; port_state = port_new(&ephnd); if (port_state == NULL) return NULL; tree_node = port_state_to_handle_tree_node(port_state); if (ts_tree_add(&epoll__handle_tree, tree_node, (uintptr_t) ephnd) < 0) { /* This should never happen. */ port_delete(port_state); return_set_error(NULL, ERROR_ALREADY_EXISTS); } return ephnd; } HANDLE epoll_create(int size) { if (size <= 0) return_set_error(NULL, ERROR_INVALID_PARAMETER); return epoll__create(); } HANDLE epoll_create1(int flags) { if (flags != 0) return_set_error(NULL, ERROR_INVALID_PARAMETER); return epoll__create(); } int epoll_close(HANDLE ephnd) { ts_tree_node_t* tree_node; port_state_t* port_state; if (init() < 0) return -1; tree_node = ts_tree_del_and_ref(&epoll__handle_tree, (uintptr_t) ephnd); if (tree_node == NULL) { err_set_win_error(ERROR_INVALID_PARAMETER); goto err; } port_state = port_state_from_handle_tree_node(tree_node); port_close(port_state); ts_tree_node_unref_and_destroy(tree_node); return port_delete(port_state); err: err_check_handle(ephnd); return -1; } int epoll_ctl(HANDLE ephnd, int op, SOCKET sock, struct epoll_event* ev) { ts_tree_node_t* tree_node; port_state_t* port_state; int r; if (init() < 0) return -1; tree_node = ts_tree_find_and_ref(&epoll__handle_tree, (uintptr_t) ephnd); if (tree_node == NULL) { err_set_win_error(ERROR_INVALID_PARAMETER); goto err; } port_state = port_state_from_handle_tree_node(tree_node); r = port_ctl(port_state, op, sock, ev); ts_tree_node_unref(tree_node); if (r < 0) goto err; return 0; err: /* On Linux, in the case of epoll_ctl(), EBADF takes priority over other * errors. Wepoll mimics this behavior. */ err_check_handle(ephnd); err_check_handle((HANDLE) sock); return -1; } int epoll_wait(HANDLE ephnd, struct epoll_event* events, int maxevents, int timeout) { ts_tree_node_t* tree_node; port_state_t* port_state; int num_events; if (maxevents <= 0) return_set_error(-1, ERROR_INVALID_PARAMETER); if (init() < 0) return -1; tree_node = ts_tree_find_and_ref(&epoll__handle_tree, (uintptr_t) ephnd); if (tree_node == NULL) { err_set_win_error(ERROR_INVALID_PARAMETER); goto err; } port_state = port_state_from_handle_tree_node(tree_node); num_events = port_wait(port_state, events, maxevents, timeout); ts_tree_node_unref(tree_node); if (num_events < 0) goto err; return num_events; err: err_check_handle(ephnd); return -1; } #include #define ERR__ERRNO_MAPPINGS(X) \ X(ERROR_ACCESS_DENIED, EACCES) \ X(ERROR_ALREADY_EXISTS, EEXIST) \ X(ERROR_BAD_COMMAND, EACCES) \ X(ERROR_BAD_EXE_FORMAT, ENOEXEC) \ X(ERROR_BAD_LENGTH, EACCES) \ X(ERROR_BAD_NETPATH, ENOENT) \ X(ERROR_BAD_NET_NAME, ENOENT) \ X(ERROR_BAD_NET_RESP, ENETDOWN) \ X(ERROR_BAD_PATHNAME, ENOENT) \ X(ERROR_BROKEN_PIPE, EPIPE) \ X(ERROR_CANNOT_MAKE, EACCES) \ X(ERROR_COMMITMENT_LIMIT, ENOMEM) \ X(ERROR_CONNECTION_ABORTED, ECONNABORTED) \ X(ERROR_CONNECTION_ACTIVE, EISCONN) \ X(ERROR_CONNECTION_REFUSED, ECONNREFUSED) \ X(ERROR_CRC, EACCES) \ X(ERROR_DIR_NOT_EMPTY, ENOTEMPTY) \ X(ERROR_DISK_FULL, ENOSPC) \ X(ERROR_DUP_NAME, EADDRINUSE) \ X(ERROR_FILENAME_EXCED_RANGE, ENOENT) \ X(ERROR_FILE_NOT_FOUND, ENOENT) \ X(ERROR_GEN_FAILURE, EACCES) \ X(ERROR_GRACEFUL_DISCONNECT, EPIPE) \ X(ERROR_HOST_DOWN, EHOSTUNREACH) \ X(ERROR_HOST_UNREACHABLE, EHOSTUNREACH) \ X(ERROR_INSUFFICIENT_BUFFER, EFAULT) \ X(ERROR_INVALID_ADDRESS, EADDRNOTAVAIL) \ X(ERROR_INVALID_FUNCTION, EINVAL) \ X(ERROR_INVALID_HANDLE, EBADF) \ X(ERROR_INVALID_NETNAME, EADDRNOTAVAIL) \ X(ERROR_INVALID_PARAMETER, EINVAL) \ X(ERROR_INVALID_USER_BUFFER, EMSGSIZE) \ X(ERROR_IO_PENDING, EINPROGRESS) \ X(ERROR_LOCK_VIOLATION, EACCES) \ X(ERROR_MORE_DATA, EMSGSIZE) \ X(ERROR_NETNAME_DELETED, ECONNABORTED) \ X(ERROR_NETWORK_ACCESS_DENIED, EACCES) \ X(ERROR_NETWORK_BUSY, ENETDOWN) \ X(ERROR_NETWORK_UNREACHABLE, ENETUNREACH) \ X(ERROR_NOACCESS, EFAULT) \ X(ERROR_NONPAGED_SYSTEM_RESOURCES, ENOMEM) \ X(ERROR_NOT_ENOUGH_MEMORY, ENOMEM) \ X(ERROR_NOT_ENOUGH_QUOTA, ENOMEM) \ X(ERROR_NOT_FOUND, ENOENT) \ X(ERROR_NOT_LOCKED, EACCES) \ X(ERROR_NOT_READY, EACCES) \ X(ERROR_NOT_SAME_DEVICE, EXDEV) \ X(ERROR_NOT_SUPPORTED, ENOTSUP) \ X(ERROR_NO_MORE_FILES, ENOENT) \ X(ERROR_NO_SYSTEM_RESOURCES, ENOMEM) \ X(ERROR_OPERATION_ABORTED, EINTR) \ X(ERROR_OUT_OF_PAPER, EACCES) \ X(ERROR_PAGED_SYSTEM_RESOURCES, ENOMEM) \ X(ERROR_PAGEFILE_QUOTA, ENOMEM) \ X(ERROR_PATH_NOT_FOUND, ENOENT) \ X(ERROR_PIPE_NOT_CONNECTED, EPIPE) \ X(ERROR_PORT_UNREACHABLE, ECONNRESET) \ X(ERROR_PROTOCOL_UNREACHABLE, ENETUNREACH) \ X(ERROR_REM_NOT_LIST, ECONNREFUSED) \ X(ERROR_REQUEST_ABORTED, EINTR) \ X(ERROR_REQ_NOT_ACCEP, EWOULDBLOCK) \ X(ERROR_SECTOR_NOT_FOUND, EACCES) \ X(ERROR_SEM_TIMEOUT, ETIMEDOUT) \ X(ERROR_SHARING_VIOLATION, EACCES) \ X(ERROR_TOO_MANY_NAMES, ENOMEM) \ X(ERROR_TOO_MANY_OPEN_FILES, EMFILE) \ X(ERROR_UNEXP_NET_ERR, ECONNABORTED) \ X(ERROR_WAIT_NO_CHILDREN, ECHILD) \ X(ERROR_WORKING_SET_QUOTA, ENOMEM) \ X(ERROR_WRITE_PROTECT, EACCES) \ X(ERROR_WRONG_DISK, EACCES) \ X(WSAEACCES, EACCES) \ X(WSAEADDRINUSE, EADDRINUSE) \ X(WSAEADDRNOTAVAIL, EADDRNOTAVAIL) \ X(WSAEAFNOSUPPORT, EAFNOSUPPORT) \ X(WSAECONNABORTED, ECONNABORTED) \ X(WSAECONNREFUSED, ECONNREFUSED) \ X(WSAECONNRESET, ECONNRESET) \ X(WSAEDISCON, EPIPE) \ X(WSAEFAULT, EFAULT) \ X(WSAEHOSTDOWN, EHOSTUNREACH) \ X(WSAEHOSTUNREACH, EHOSTUNREACH) \ X(WSAEINPROGRESS, EBUSY) \ X(WSAEINTR, EINTR) \ X(WSAEINVAL, EINVAL) \ X(WSAEISCONN, EISCONN) \ X(WSAEMSGSIZE, EMSGSIZE) \ X(WSAENETDOWN, ENETDOWN) \ X(WSAENETRESET, EHOSTUNREACH) \ X(WSAENETUNREACH, ENETUNREACH) \ X(WSAENOBUFS, ENOMEM) \ X(WSAENOTCONN, ENOTCONN) \ X(WSAENOTSOCK, ENOTSOCK) \ X(WSAEOPNOTSUPP, EOPNOTSUPP) \ X(WSAEPROCLIM, ENOMEM) \ X(WSAESHUTDOWN, EPIPE) \ X(WSAETIMEDOUT, ETIMEDOUT) \ X(WSAEWOULDBLOCK, EWOULDBLOCK) \ X(WSANOTINITIALISED, ENETDOWN) \ X(WSASYSNOTREADY, ENETDOWN) \ X(WSAVERNOTSUPPORTED, ENOSYS) static errno_t err__map_win_error_to_errno(DWORD error) { switch (error) { #define X(error_sym, errno_sym) \ case error_sym: \ return errno_sym; ERR__ERRNO_MAPPINGS(X) #undef X } return EINVAL; } void err_map_win_error(void) { errno = err__map_win_error_to_errno(GetLastError()); } void err_set_win_error(DWORD error) { SetLastError(error); errno = err__map_win_error_to_errno(error); } int err_check_handle(HANDLE handle) { DWORD flags; /* GetHandleInformation() succeeds when passed INVALID_HANDLE_VALUE, so check * for this condition explicitly. */ if (handle == INVALID_HANDLE_VALUE) return_set_error(-1, ERROR_INVALID_HANDLE); if (!GetHandleInformation(handle, &flags)) return_map_error(-1); return 0; } #include #define array_count(a) (sizeof(a) / (sizeof((a)[0]))) #define container_of(ptr, type, member) \ ((type*) ((uintptr_t) (ptr) - offsetof(type, member))) #define unused_var(v) ((void) (v)) /* Polyfill `inline` for older versions of msvc (up to Visual Studio 2013) */ #if defined(_MSC_VER) && _MSC_VER < 1900 #define inline __inline #endif WEPOLL_INTERNAL int ws_global_init(void); WEPOLL_INTERNAL SOCKET ws_get_base_socket(SOCKET socket); static bool init__done = false; static INIT_ONCE init__once = INIT_ONCE_STATIC_INIT; static BOOL CALLBACK init__once_callback(INIT_ONCE* once, void* parameter, void** context) { unused_var(once); unused_var(parameter); unused_var(context); /* N.b. that initialization order matters here. */ if (ws_global_init() < 0 || nt_global_init() < 0 || reflock_global_init() < 0 || epoll_global_init() < 0) return FALSE; init__done = true; return TRUE; } int init(void) { if (!init__done && !InitOnceExecuteOnce(&init__once, init__once_callback, NULL, NULL)) /* `InitOnceExecuteOnce()` itself is infallible, and it doesn't set any * error code when the once-callback returns FALSE. We return -1 here to * indicate that global initialization failed; the failing init function is * resposible for setting `errno` and calling `SetLastError()`. */ return -1; return 0; } /* Set up a workaround for the following problem: * FARPROC addr = GetProcAddress(...); * MY_FUNC func = (MY_FUNC) addr; <-- GCC 8 warning/error. * MY_FUNC func = (MY_FUNC) (void*) addr; <-- MSVC warning/error. * To compile cleanly with either compiler, do casts with this "bridge" type: * MY_FUNC func = (MY_FUNC) (nt__fn_ptr_cast_t) addr; */ #ifdef __GNUC__ typedef void* nt__fn_ptr_cast_t; #else typedef FARPROC nt__fn_ptr_cast_t; #endif #define X(return_type, attributes, name, parameters) \ WEPOLL_INTERNAL return_type(attributes* name) parameters = NULL; NT_NTDLL_IMPORT_LIST(X) #undef X int nt_global_init(void) { HMODULE ntdll; FARPROC fn_ptr; ntdll = GetModuleHandleW(L"ntdll.dll"); if (ntdll == NULL) return -1; #define X(return_type, attributes, name, parameters) \ fn_ptr = GetProcAddress(ntdll, #name); \ if (fn_ptr == NULL) \ return -1; \ name = (return_type(attributes*) parameters)(nt__fn_ptr_cast_t) fn_ptr; NT_NTDLL_IMPORT_LIST(X) #undef X return 0; } #include typedef struct poll_group poll_group_t; typedef struct queue_node queue_node_t; WEPOLL_INTERNAL poll_group_t* poll_group_acquire(port_state_t* port); WEPOLL_INTERNAL void poll_group_release(poll_group_t* poll_group); WEPOLL_INTERNAL void poll_group_delete(poll_group_t* poll_group); WEPOLL_INTERNAL poll_group_t* poll_group_from_queue_node( queue_node_t* queue_node); WEPOLL_INTERNAL HANDLE poll_group_get_afd_device_handle(poll_group_t* poll_group); typedef struct queue_node { queue_node_t* prev; queue_node_t* next; } queue_node_t; typedef struct queue { queue_node_t head; } queue_t; WEPOLL_INTERNAL void queue_init(queue_t* queue); WEPOLL_INTERNAL void queue_node_init(queue_node_t* node); WEPOLL_INTERNAL queue_node_t* queue_first(const queue_t* queue); WEPOLL_INTERNAL queue_node_t* queue_last(const queue_t* queue); WEPOLL_INTERNAL void queue_prepend(queue_t* queue, queue_node_t* node); WEPOLL_INTERNAL void queue_append(queue_t* queue, queue_node_t* node); WEPOLL_INTERNAL void queue_move_to_start(queue_t* queue, queue_node_t* node); WEPOLL_INTERNAL void queue_move_to_end(queue_t* queue, queue_node_t* node); WEPOLL_INTERNAL void queue_remove(queue_node_t* node); WEPOLL_INTERNAL bool queue_is_empty(const queue_t* queue); WEPOLL_INTERNAL bool queue_is_enqueued(const queue_node_t* node); #define POLL_GROUP__MAX_GROUP_SIZE 32 typedef struct poll_group { port_state_t* port_state; queue_node_t queue_node; HANDLE afd_device_handle; size_t group_size; } poll_group_t; static poll_group_t* poll_group__new(port_state_t* port_state) { HANDLE iocp_handle = port_get_iocp_handle(port_state); queue_t* poll_group_queue = port_get_poll_group_queue(port_state); poll_group_t* poll_group = malloc(sizeof *poll_group); if (poll_group == NULL) return_set_error(NULL, ERROR_NOT_ENOUGH_MEMORY); memset(poll_group, 0, sizeof *poll_group); queue_node_init(&poll_group->queue_node); poll_group->port_state = port_state; if (afd_create_device_handle(iocp_handle, &poll_group->afd_device_handle) < 0) { free(poll_group); return NULL; } queue_append(poll_group_queue, &poll_group->queue_node); return poll_group; } void poll_group_delete(poll_group_t* poll_group) { assert(poll_group->group_size == 0); CloseHandle(poll_group->afd_device_handle); queue_remove(&poll_group->queue_node); free(poll_group); } poll_group_t* poll_group_from_queue_node(queue_node_t* queue_node) { return container_of(queue_node, poll_group_t, queue_node); } HANDLE poll_group_get_afd_device_handle(poll_group_t* poll_group) { return poll_group->afd_device_handle; } poll_group_t* poll_group_acquire(port_state_t* port_state) { queue_t* poll_group_queue = port_get_poll_group_queue(port_state); poll_group_t* poll_group = !queue_is_empty(poll_group_queue) ? container_of( queue_last(poll_group_queue), poll_group_t, queue_node) : NULL; if (poll_group == NULL || poll_group->group_size >= POLL_GROUP__MAX_GROUP_SIZE) poll_group = poll_group__new(port_state); if (poll_group == NULL) return NULL; if (++poll_group->group_size == POLL_GROUP__MAX_GROUP_SIZE) queue_move_to_start(poll_group_queue, &poll_group->queue_node); return poll_group; } void poll_group_release(poll_group_t* poll_group) { port_state_t* port_state = poll_group->port_state; queue_t* poll_group_queue = port_get_poll_group_queue(port_state); poll_group->group_size--; assert(poll_group->group_size < POLL_GROUP__MAX_GROUP_SIZE); queue_move_to_end(poll_group_queue, &poll_group->queue_node); /* Poll groups are currently only freed when the epoll port is closed. */ } WEPOLL_INTERNAL sock_state_t* sock_new(port_state_t* port_state, SOCKET socket); WEPOLL_INTERNAL void sock_delete(port_state_t* port_state, sock_state_t* sock_state); WEPOLL_INTERNAL void sock_force_delete(port_state_t* port_state, sock_state_t* sock_state); WEPOLL_INTERNAL int sock_set_event(port_state_t* port_state, sock_state_t* sock_state, const struct epoll_event* ev); WEPOLL_INTERNAL int sock_update(port_state_t* port_state, sock_state_t* sock_state); WEPOLL_INTERNAL int sock_feed_event(port_state_t* port_state, IO_STATUS_BLOCK* io_status_block, struct epoll_event* ev); WEPOLL_INTERNAL sock_state_t* sock_state_from_queue_node( queue_node_t* queue_node); WEPOLL_INTERNAL queue_node_t* sock_state_to_queue_node( sock_state_t* sock_state); WEPOLL_INTERNAL sock_state_t* sock_state_from_tree_node( tree_node_t* tree_node); WEPOLL_INTERNAL tree_node_t* sock_state_to_tree_node(sock_state_t* sock_state); #define PORT__MAX_ON_STACK_COMPLETIONS 256 typedef struct port_state { HANDLE iocp_handle; tree_t sock_tree; queue_t sock_update_queue; queue_t sock_deleted_queue; queue_t poll_group_queue; ts_tree_node_t handle_tree_node; CRITICAL_SECTION lock; size_t active_poll_count; } port_state_t; static inline port_state_t* port__alloc(void) { port_state_t* port_state = malloc(sizeof *port_state); if (port_state == NULL) return_set_error(NULL, ERROR_NOT_ENOUGH_MEMORY); return port_state; } static inline void port__free(port_state_t* port) { assert(port != NULL); free(port); } static inline HANDLE port__create_iocp(void) { HANDLE iocp_handle = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); if (iocp_handle == NULL) return_map_error(NULL); return iocp_handle; } port_state_t* port_new(HANDLE* iocp_handle_out) { port_state_t* port_state; HANDLE iocp_handle; port_state = port__alloc(); if (port_state == NULL) goto err1; iocp_handle = port__create_iocp(); if (iocp_handle == NULL) goto err2; memset(port_state, 0, sizeof *port_state); port_state->iocp_handle = iocp_handle; tree_init(&port_state->sock_tree); queue_init(&port_state->sock_update_queue); queue_init(&port_state->sock_deleted_queue); queue_init(&port_state->poll_group_queue); ts_tree_node_init(&port_state->handle_tree_node); InitializeCriticalSection(&port_state->lock); *iocp_handle_out = iocp_handle; return port_state; err2: port__free(port_state); err1: return NULL; } static inline int port__close_iocp(port_state_t* port_state) { HANDLE iocp_handle = port_state->iocp_handle; port_state->iocp_handle = NULL; if (!CloseHandle(iocp_handle)) return_map_error(-1); return 0; } int port_close(port_state_t* port_state) { int result; EnterCriticalSection(&port_state->lock); result = port__close_iocp(port_state); LeaveCriticalSection(&port_state->lock); return result; } int port_delete(port_state_t* port_state) { tree_node_t* tree_node; queue_node_t* queue_node; /* At this point the IOCP port should have been closed. */ assert(port_state->iocp_handle == NULL); while ((tree_node = tree_root(&port_state->sock_tree)) != NULL) { sock_state_t* sock_state = sock_state_from_tree_node(tree_node); sock_force_delete(port_state, sock_state); } while ((queue_node = queue_first(&port_state->sock_deleted_queue)) != NULL) { sock_state_t* sock_state = sock_state_from_queue_node(queue_node); sock_force_delete(port_state, sock_state); } while ((queue_node = queue_first(&port_state->poll_group_queue)) != NULL) { poll_group_t* poll_group = poll_group_from_queue_node(queue_node); poll_group_delete(poll_group); } assert(queue_is_empty(&port_state->sock_update_queue)); DeleteCriticalSection(&port_state->lock); port__free(port_state); return 0; } static int port__update_events(port_state_t* port_state) { queue_t* sock_update_queue = &port_state->sock_update_queue; /* Walk the queue, submitting new poll requests for every socket that needs * it. */ while (!queue_is_empty(sock_update_queue)) { queue_node_t* queue_node = queue_first(sock_update_queue); sock_state_t* sock_state = sock_state_from_queue_node(queue_node); if (sock_update(port_state, sock_state) < 0) return -1; /* sock_update() removes the socket from the update queue. */ } return 0; } static inline void port__update_events_if_polling(port_state_t* port_state) { if (port_state->active_poll_count > 0) port__update_events(port_state); } static inline int port__feed_events(port_state_t* port_state, struct epoll_event* epoll_events, OVERLAPPED_ENTRY* iocp_events, DWORD iocp_event_count) { int epoll_event_count = 0; DWORD i; for (i = 0; i < iocp_event_count; i++) { IO_STATUS_BLOCK* io_status_block = (IO_STATUS_BLOCK*) iocp_events[i].lpOverlapped; struct epoll_event* ev = &epoll_events[epoll_event_count]; epoll_event_count += sock_feed_event(port_state, io_status_block, ev); } return epoll_event_count; } static inline int port__poll(port_state_t* port_state, struct epoll_event* epoll_events, OVERLAPPED_ENTRY* iocp_events, DWORD maxevents, DWORD timeout) { DWORD completion_count; if (port__update_events(port_state) < 0) return -1; port_state->active_poll_count++; LeaveCriticalSection(&port_state->lock); BOOL r = GetQueuedCompletionStatusEx(port_state->iocp_handle, iocp_events, maxevents, &completion_count, timeout, FALSE); EnterCriticalSection(&port_state->lock); port_state->active_poll_count--; if (!r) return_map_error(-1); return port__feed_events( port_state, epoll_events, iocp_events, completion_count); } int port_wait(port_state_t* port_state, struct epoll_event* events, int maxevents, int timeout) { OVERLAPPED_ENTRY stack_iocp_events[PORT__MAX_ON_STACK_COMPLETIONS]; OVERLAPPED_ENTRY* iocp_events; uint64_t due = 0; DWORD gqcs_timeout; int result; /* Check whether `maxevents` is in range. */ if (maxevents <= 0) return_set_error(-1, ERROR_INVALID_PARAMETER); /* Decide whether the IOCP completion list can live on the stack, or allocate * memory for it on the heap. */ if ((size_t) maxevents <= array_count(stack_iocp_events)) { iocp_events = stack_iocp_events; } else if ((iocp_events = malloc((size_t) maxevents * sizeof *iocp_events)) == NULL) { iocp_events = stack_iocp_events; maxevents = array_count(stack_iocp_events); } /* Compute the timeout for GetQueuedCompletionStatus, and the wait end * time, if the user specified a timeout other than zero or infinite. */ if (timeout > 0) { due = GetTickCount64() + (uint64_t) timeout; gqcs_timeout = (DWORD) timeout; } else if (timeout == 0) { gqcs_timeout = 0; } else { gqcs_timeout = INFINITE; } EnterCriticalSection(&port_state->lock); /* Dequeue completion packets until either at least one interesting event * has been discovered, or the timeout is reached. */ for (;;) { uint64_t now; result = port__poll( port_state, events, iocp_events, (DWORD) maxevents, gqcs_timeout); if (result < 0 || result > 0) break; /* Result, error, or time-out. */ if (timeout < 0) continue; /* When timeout is negative, never time out. */ /* Update time. */ now = GetTickCount64(); /* Do not allow the due time to be in the past. */ if (now >= due) { SetLastError(WAIT_TIMEOUT); break; } /* Recompute time-out argument for GetQueuedCompletionStatus. */ gqcs_timeout = (DWORD)(due - now); } port__update_events_if_polling(port_state); LeaveCriticalSection(&port_state->lock); if (iocp_events != stack_iocp_events) free(iocp_events); if (result >= 0) return result; else if (GetLastError() == WAIT_TIMEOUT) return 0; else return -1; } static inline int port__ctl_add(port_state_t* port_state, SOCKET sock, struct epoll_event* ev) { sock_state_t* sock_state = sock_new(port_state, sock); if (sock_state == NULL) return -1; if (sock_set_event(port_state, sock_state, ev) < 0) { sock_delete(port_state, sock_state); return -1; } port__update_events_if_polling(port_state); return 0; } static inline int port__ctl_mod(port_state_t* port_state, SOCKET sock, struct epoll_event* ev) { sock_state_t* sock_state = port_find_socket(port_state, sock); if (sock_state == NULL) return -1; if (sock_set_event(port_state, sock_state, ev) < 0) return -1; port__update_events_if_polling(port_state); return 0; } static inline int port__ctl_del(port_state_t* port_state, SOCKET sock) { sock_state_t* sock_state = port_find_socket(port_state, sock); if (sock_state == NULL) return -1; sock_delete(port_state, sock_state); return 0; } static inline int port__ctl_op(port_state_t* port_state, int op, SOCKET sock, struct epoll_event* ev) { switch (op) { case EPOLL_CTL_ADD: return port__ctl_add(port_state, sock, ev); case EPOLL_CTL_MOD: return port__ctl_mod(port_state, sock, ev); case EPOLL_CTL_DEL: return port__ctl_del(port_state, sock); default: return_set_error(-1, ERROR_INVALID_PARAMETER); } } int port_ctl(port_state_t* port_state, int op, SOCKET sock, struct epoll_event* ev) { int result; EnterCriticalSection(&port_state->lock); result = port__ctl_op(port_state, op, sock, ev); LeaveCriticalSection(&port_state->lock); return result; } int port_register_socket(port_state_t* port_state, sock_state_t* sock_state, SOCKET socket) { if (tree_add(&port_state->sock_tree, sock_state_to_tree_node(sock_state), socket) < 0) return_set_error(-1, ERROR_ALREADY_EXISTS); return 0; } void port_unregister_socket(port_state_t* port_state, sock_state_t* sock_state) { tree_del(&port_state->sock_tree, sock_state_to_tree_node(sock_state)); } sock_state_t* port_find_socket(port_state_t* port_state, SOCKET socket) { tree_node_t* tree_node = tree_find(&port_state->sock_tree, socket); if (tree_node == NULL) return_set_error(NULL, ERROR_NOT_FOUND); return sock_state_from_tree_node(tree_node); } void port_request_socket_update(port_state_t* port_state, sock_state_t* sock_state) { if (queue_is_enqueued(sock_state_to_queue_node(sock_state))) return; queue_append(&port_state->sock_update_queue, sock_state_to_queue_node(sock_state)); } void port_cancel_socket_update(port_state_t* port_state, sock_state_t* sock_state) { unused_var(port_state); if (!queue_is_enqueued(sock_state_to_queue_node(sock_state))) return; queue_remove(sock_state_to_queue_node(sock_state)); } void port_add_deleted_socket(port_state_t* port_state, sock_state_t* sock_state) { if (queue_is_enqueued(sock_state_to_queue_node(sock_state))) return; queue_append(&port_state->sock_deleted_queue, sock_state_to_queue_node(sock_state)); } void port_remove_deleted_socket(port_state_t* port_state, sock_state_t* sock_state) { unused_var(port_state); if (!queue_is_enqueued(sock_state_to_queue_node(sock_state))) return; queue_remove(sock_state_to_queue_node(sock_state)); } HANDLE port_get_iocp_handle(port_state_t* port_state) { assert(port_state->iocp_handle != NULL); return port_state->iocp_handle; } queue_t* port_get_poll_group_queue(port_state_t* port_state) { return &port_state->poll_group_queue; } port_state_t* port_state_from_handle_tree_node(ts_tree_node_t* tree_node) { return container_of(tree_node, port_state_t, handle_tree_node); } ts_tree_node_t* port_state_to_handle_tree_node(port_state_t* port_state) { return &port_state->handle_tree_node; } void queue_init(queue_t* queue) { queue_node_init(&queue->head); } void queue_node_init(queue_node_t* node) { node->prev = node; node->next = node; } static inline void queue__detach_node(queue_node_t* node) { node->prev->next = node->next; node->next->prev = node->prev; } queue_node_t* queue_first(const queue_t* queue) { return !queue_is_empty(queue) ? queue->head.next : NULL; } queue_node_t* queue_last(const queue_t* queue) { return !queue_is_empty(queue) ? queue->head.prev : NULL; } void queue_prepend(queue_t* queue, queue_node_t* node) { node->next = queue->head.next; node->prev = &queue->head; node->next->prev = node; queue->head.next = node; } void queue_append(queue_t* queue, queue_node_t* node) { node->next = &queue->head; node->prev = queue->head.prev; node->prev->next = node; queue->head.prev = node; } void queue_move_to_start(queue_t* queue, queue_node_t* node) { queue__detach_node(node); queue_prepend(queue, node); } void queue_move_to_end(queue_t* queue, queue_node_t* node) { queue__detach_node(node); queue_append(queue, node); } void queue_remove(queue_node_t* node) { queue__detach_node(node); queue_node_init(node); } bool queue_is_empty(const queue_t* queue) { return !queue_is_enqueued(&queue->head); } bool queue_is_enqueued(const queue_node_t* node) { return node->prev != node; } #define REFLOCK__REF ((long) 0x00000001UL) #define REFLOCK__REF_MASK ((long) 0x0fffffffUL) #define REFLOCK__DESTROY ((long) 0x10000000UL) #define REFLOCK__DESTROY_MASK ((long) 0xf0000000UL) #define REFLOCK__POISON ((long) 0x300dead0UL) static HANDLE reflock__keyed_event = NULL; int reflock_global_init(void) { NTSTATUS status = NtCreateKeyedEvent( &reflock__keyed_event, KEYEDEVENT_ALL_ACCESS, NULL, 0); if (status != STATUS_SUCCESS) return_set_error(-1, RtlNtStatusToDosError(status)); return 0; } void reflock_init(reflock_t* reflock) { reflock->state = 0; } static void reflock__signal_event(void* address) { NTSTATUS status = NtReleaseKeyedEvent(reflock__keyed_event, address, FALSE, NULL); if (status != STATUS_SUCCESS) abort(); } static void reflock__await_event(void* address) { NTSTATUS status = NtWaitForKeyedEvent(reflock__keyed_event, address, FALSE, NULL); if (status != STATUS_SUCCESS) abort(); } void reflock_ref(reflock_t* reflock) { long state = InterlockedAdd(&reflock->state, REFLOCK__REF); /* Verify that the counter didn't overflow and the lock isn't destroyed. */ assert((state & REFLOCK__DESTROY_MASK) == 0); unused_var(state); } void reflock_unref(reflock_t* reflock) { long state = InterlockedAdd(&reflock->state, -REFLOCK__REF); /* Verify that the lock was referenced and not already destroyed. */ assert((state & REFLOCK__DESTROY_MASK & ~REFLOCK__DESTROY) == 0); if (state == REFLOCK__DESTROY) reflock__signal_event(reflock); } void reflock_unref_and_destroy(reflock_t* reflock) { long state = InterlockedAdd(&reflock->state, REFLOCK__DESTROY - REFLOCK__REF); long ref_count = state & REFLOCK__REF_MASK; /* Verify that the lock was referenced and not already destroyed. */ assert((state & REFLOCK__DESTROY_MASK) == REFLOCK__DESTROY); if (ref_count != 0) reflock__await_event(reflock); state = InterlockedExchange(&reflock->state, REFLOCK__POISON); assert(state == REFLOCK__DESTROY); } #define SOCK__KNOWN_EPOLL_EVENTS \ (EPOLLIN | EPOLLPRI | EPOLLOUT | EPOLLERR | EPOLLHUP | EPOLLRDNORM | \ EPOLLRDBAND | EPOLLWRNORM | EPOLLWRBAND | EPOLLMSG | EPOLLRDHUP) typedef enum sock__poll_status { SOCK__POLL_IDLE = 0, SOCK__POLL_PENDING, SOCK__POLL_CANCELLED } sock__poll_status_t; typedef struct sock_state { IO_STATUS_BLOCK io_status_block; AFD_POLL_INFO poll_info; queue_node_t queue_node; tree_node_t tree_node; poll_group_t* poll_group; SOCKET base_socket; epoll_data_t user_data; uint32_t user_events; uint32_t pending_events; sock__poll_status_t poll_status; bool delete_pending; } sock_state_t; static inline sock_state_t* sock__alloc(void) { sock_state_t* sock_state = malloc(sizeof *sock_state); if (sock_state == NULL) return_set_error(NULL, ERROR_NOT_ENOUGH_MEMORY); return sock_state; } static inline void sock__free(sock_state_t* sock_state) { assert(sock_state != NULL); free(sock_state); } static inline int sock__cancel_poll(sock_state_t* sock_state) { assert(sock_state->poll_status == SOCK__POLL_PENDING); if (afd_cancel_poll(poll_group_get_afd_device_handle(sock_state->poll_group), &sock_state->io_status_block) < 0) return -1; sock_state->poll_status = SOCK__POLL_CANCELLED; sock_state->pending_events = 0; return 0; } sock_state_t* sock_new(port_state_t* port_state, SOCKET socket) { SOCKET base_socket; poll_group_t* poll_group; sock_state_t* sock_state; if (socket == 0 || socket == INVALID_SOCKET) return_set_error(NULL, ERROR_INVALID_HANDLE); base_socket = ws_get_base_socket(socket); if (base_socket == INVALID_SOCKET) return NULL; poll_group = poll_group_acquire(port_state); if (poll_group == NULL) return NULL; sock_state = sock__alloc(); if (sock_state == NULL) goto err1; memset(sock_state, 0, sizeof *sock_state); sock_state->base_socket = base_socket; sock_state->poll_group = poll_group; tree_node_init(&sock_state->tree_node); queue_node_init(&sock_state->queue_node); if (port_register_socket(port_state, sock_state, socket) < 0) goto err2; return sock_state; err2: sock__free(sock_state); err1: poll_group_release(poll_group); return NULL; } static int sock__delete(port_state_t* port_state, sock_state_t* sock_state, bool force) { if (!sock_state->delete_pending) { if (sock_state->poll_status == SOCK__POLL_PENDING) sock__cancel_poll(sock_state); port_cancel_socket_update(port_state, sock_state); port_unregister_socket(port_state, sock_state); sock_state->delete_pending = true; } /* If the poll request still needs to complete, the sock_state object can't * be free()d yet. `sock_feed_event()` or `port_close()` will take care * of this later. */ if (force || sock_state->poll_status == SOCK__POLL_IDLE) { /* Free the sock_state now. */ port_remove_deleted_socket(port_state, sock_state); poll_group_release(sock_state->poll_group); sock__free(sock_state); } else { /* Free the socket later. */ port_add_deleted_socket(port_state, sock_state); } return 0; } void sock_delete(port_state_t* port_state, sock_state_t* sock_state) { sock__delete(port_state, sock_state, false); } void sock_force_delete(port_state_t* port_state, sock_state_t* sock_state) { sock__delete(port_state, sock_state, true); } int sock_set_event(port_state_t* port_state, sock_state_t* sock_state, const struct epoll_event* ev) { /* EPOLLERR and EPOLLHUP are always reported, even when not requested by the * caller. However they are disabled after a event has been reported for a * socket for which the EPOLLONESHOT flag was set. */ uint32_t events = ev->events | EPOLLERR | EPOLLHUP; sock_state->user_events = events; sock_state->user_data = ev->data; if ((events & SOCK__KNOWN_EPOLL_EVENTS & ~sock_state->pending_events) != 0) port_request_socket_update(port_state, sock_state); return 0; } static inline DWORD sock__epoll_events_to_afd_events(uint32_t epoll_events) { /* Always monitor for AFD_POLL_LOCAL_CLOSE, which is triggered when the * socket is closed with closesocket() or CloseHandle(). */ DWORD afd_events = AFD_POLL_LOCAL_CLOSE; if (epoll_events & (EPOLLIN | EPOLLRDNORM)) afd_events |= AFD_POLL_RECEIVE | AFD_POLL_ACCEPT; if (epoll_events & (EPOLLPRI | EPOLLRDBAND)) afd_events |= AFD_POLL_RECEIVE_EXPEDITED; if (epoll_events & (EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND)) afd_events |= AFD_POLL_SEND; if (epoll_events & (EPOLLIN | EPOLLRDNORM | EPOLLRDHUP)) afd_events |= AFD_POLL_DISCONNECT; if (epoll_events & EPOLLHUP) afd_events |= AFD_POLL_ABORT; if (epoll_events & EPOLLERR) afd_events |= AFD_POLL_CONNECT_FAIL; return afd_events; } static inline uint32_t sock__afd_events_to_epoll_events(DWORD afd_events) { uint32_t epoll_events = 0; if (afd_events & (AFD_POLL_RECEIVE | AFD_POLL_ACCEPT)) epoll_events |= EPOLLIN | EPOLLRDNORM; if (afd_events & AFD_POLL_RECEIVE_EXPEDITED) epoll_events |= EPOLLPRI | EPOLLRDBAND; if (afd_events & AFD_POLL_SEND) epoll_events |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND; if (afd_events & AFD_POLL_DISCONNECT) epoll_events |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; if (afd_events & AFD_POLL_ABORT) epoll_events |= EPOLLHUP; if (afd_events & AFD_POLL_CONNECT_FAIL) /* Linux reports all these events after connect() has failed. */ epoll_events |= EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLRDNORM | EPOLLWRNORM | EPOLLRDHUP; return epoll_events; } int sock_update(port_state_t* port_state, sock_state_t* sock_state) { assert(!sock_state->delete_pending); if ((sock_state->poll_status == SOCK__POLL_PENDING) && (sock_state->user_events & SOCK__KNOWN_EPOLL_EVENTS & ~sock_state->pending_events) == 0) { /* All the events the user is interested in are already being monitored by * the pending poll operation. It might spuriously complete because of an * event that we're no longer interested in; when that happens we'll submit * a new poll operation with the updated event mask. */ } else if (sock_state->poll_status == SOCK__POLL_PENDING) { /* A poll operation is already pending, but it's not monitoring for all the * events that the user is interested in. Therefore, cancel the pending * poll operation; when we receive it's completion package, a new poll * operation will be submitted with the correct event mask. */ if (sock__cancel_poll(sock_state) < 0) return -1; } else if (sock_state->poll_status == SOCK__POLL_CANCELLED) { /* The poll operation has already been cancelled, we're still waiting for * it to return. For now, there's nothing that needs to be done. */ } else if (sock_state->poll_status == SOCK__POLL_IDLE) { /* No poll operation is pending; start one. */ sock_state->poll_info.Exclusive = FALSE; sock_state->poll_info.NumberOfHandles = 1; sock_state->poll_info.Timeout.QuadPart = INT64_MAX; sock_state->poll_info.Handles[0].Handle = (HANDLE) sock_state->base_socket; sock_state->poll_info.Handles[0].Status = 0; sock_state->poll_info.Handles[0].Events = sock__epoll_events_to_afd_events(sock_state->user_events); if (afd_poll(poll_group_get_afd_device_handle(sock_state->poll_group), &sock_state->poll_info, &sock_state->io_status_block) < 0) { switch (GetLastError()) { case ERROR_IO_PENDING: /* Overlapped poll operation in progress; this is expected. */ break; case ERROR_INVALID_HANDLE: /* Socket closed; it'll be dropped from the epoll set. */ return sock__delete(port_state, sock_state, false); default: /* Other errors are propagated to the caller. */ return_map_error(-1); } } /* The poll request was successfully submitted. */ sock_state->poll_status = SOCK__POLL_PENDING; sock_state->pending_events = sock_state->user_events; } else { /* Unreachable. */ assert(false); } port_cancel_socket_update(port_state, sock_state); return 0; } int sock_feed_event(port_state_t* port_state, IO_STATUS_BLOCK* io_status_block, struct epoll_event* ev) { sock_state_t* sock_state = container_of(io_status_block, sock_state_t, io_status_block); AFD_POLL_INFO* poll_info = &sock_state->poll_info; uint32_t epoll_events = 0; sock_state->poll_status = SOCK__POLL_IDLE; sock_state->pending_events = 0; if (sock_state->delete_pending) { /* Socket has been deleted earlier and can now be freed. */ return sock__delete(port_state, sock_state, false); } else if (io_status_block->Status == STATUS_CANCELLED) { /* The poll request was cancelled by CancelIoEx. */ } else if (!NT_SUCCESS(io_status_block->Status)) { /* The overlapped request itself failed in an unexpected way. */ epoll_events = EPOLLERR; } else if (poll_info->NumberOfHandles < 1) { /* This poll operation succeeded but didn't report any socket events. */ } else if (poll_info->Handles[0].Events & AFD_POLL_LOCAL_CLOSE) { /* The poll operation reported that the socket was closed. */ return sock__delete(port_state, sock_state, false); } else { /* Events related to our socket were reported. */ epoll_events = sock__afd_events_to_epoll_events(poll_info->Handles[0].Events); } /* Requeue the socket so a new poll request will be submitted. */ port_request_socket_update(port_state, sock_state); /* Filter out events that the user didn't ask for. */ epoll_events &= sock_state->user_events; /* Return if there are no epoll events to report. */ if (epoll_events == 0) return 0; /* If the the socket has the EPOLLONESHOT flag set, unmonitor all events, * even EPOLLERR and EPOLLHUP. But always keep looking for closed sockets. */ if (sock_state->user_events & EPOLLONESHOT) sock_state->user_events = 0; ev->data = sock_state->user_data; ev->events = epoll_events; return 1; } sock_state_t* sock_state_from_queue_node(queue_node_t* queue_node) { return container_of(queue_node, sock_state_t, queue_node); } queue_node_t* sock_state_to_queue_node(sock_state_t* sock_state) { return &sock_state->queue_node; } sock_state_t* sock_state_from_tree_node(tree_node_t* tree_node) { return container_of(tree_node, sock_state_t, tree_node); } tree_node_t* sock_state_to_tree_node(sock_state_t* sock_state) { return &sock_state->tree_node; } void ts_tree_init(ts_tree_t* ts_tree) { tree_init(&ts_tree->tree); InitializeSRWLock(&ts_tree->lock); } void ts_tree_node_init(ts_tree_node_t* node) { tree_node_init(&node->tree_node); reflock_init(&node->reflock); } int ts_tree_add(ts_tree_t* ts_tree, ts_tree_node_t* node, uintptr_t key) { int r; AcquireSRWLockExclusive(&ts_tree->lock); r = tree_add(&ts_tree->tree, &node->tree_node, key); ReleaseSRWLockExclusive(&ts_tree->lock); return r; } static inline ts_tree_node_t* ts_tree__find_node(ts_tree_t* ts_tree, uintptr_t key) { tree_node_t* tree_node = tree_find(&ts_tree->tree, key); if (tree_node == NULL) return NULL; return container_of(tree_node, ts_tree_node_t, tree_node); } ts_tree_node_t* ts_tree_del_and_ref(ts_tree_t* ts_tree, uintptr_t key) { ts_tree_node_t* ts_tree_node; AcquireSRWLockExclusive(&ts_tree->lock); ts_tree_node = ts_tree__find_node(ts_tree, key); if (ts_tree_node != NULL) { tree_del(&ts_tree->tree, &ts_tree_node->tree_node); reflock_ref(&ts_tree_node->reflock); } ReleaseSRWLockExclusive(&ts_tree->lock); return ts_tree_node; } ts_tree_node_t* ts_tree_find_and_ref(ts_tree_t* ts_tree, uintptr_t key) { ts_tree_node_t* ts_tree_node; AcquireSRWLockShared(&ts_tree->lock); ts_tree_node = ts_tree__find_node(ts_tree, key); if (ts_tree_node != NULL) reflock_ref(&ts_tree_node->reflock); ReleaseSRWLockShared(&ts_tree->lock); return ts_tree_node; } void ts_tree_node_unref(ts_tree_node_t* node) { reflock_unref(&node->reflock); } void ts_tree_node_unref_and_destroy(ts_tree_node_t* node) { reflock_unref_and_destroy(&node->reflock); } void tree_init(tree_t* tree) { memset(tree, 0, sizeof *tree); } void tree_node_init(tree_node_t* node) { memset(node, 0, sizeof *node); } #define TREE__ROTATE(cis, trans) \ tree_node_t* p = node; \ tree_node_t* q = node->trans; \ tree_node_t* parent = p->parent; \ \ if (parent) { \ if (parent->left == p) \ parent->left = q; \ else \ parent->right = q; \ } else { \ tree->root = q; \ } \ \ q->parent = parent; \ p->parent = q; \ p->trans = q->cis; \ if (p->trans) \ p->trans->parent = p; \ q->cis = p; static inline void tree__rotate_left(tree_t* tree, tree_node_t* node) { TREE__ROTATE(left, right) } static inline void tree__rotate_right(tree_t* tree, tree_node_t* node) { TREE__ROTATE(right, left) } #define TREE__INSERT_OR_DESCEND(side) \ if (parent->side) { \ parent = parent->side; \ } else { \ parent->side = node; \ break; \ } #define TREE__REBALANCE_AFTER_INSERT(cis, trans) \ tree_node_t* grandparent = parent->parent; \ tree_node_t* uncle = grandparent->trans; \ \ if (uncle && uncle->red) { \ parent->red = uncle->red = false; \ grandparent->red = true; \ node = grandparent; \ } else { \ if (node == parent->trans) { \ tree__rotate_##cis(tree, parent); \ node = parent; \ parent = node->parent; \ } \ parent->red = false; \ grandparent->red = true; \ tree__rotate_##trans(tree, grandparent); \ } int tree_add(tree_t* tree, tree_node_t* node, uintptr_t key) { tree_node_t* parent; parent = tree->root; if (parent) { for (;;) { if (key < parent->key) { TREE__INSERT_OR_DESCEND(left) } else if (key > parent->key) { TREE__INSERT_OR_DESCEND(right) } else { return -1; } } } else { tree->root = node; } node->key = key; node->left = node->right = NULL; node->parent = parent; node->red = true; for (; parent && parent->red; parent = node->parent) { if (parent == parent->parent->left) { TREE__REBALANCE_AFTER_INSERT(left, right) } else { TREE__REBALANCE_AFTER_INSERT(right, left) } } tree->root->red = false; return 0; } #define TREE__REBALANCE_AFTER_REMOVE(cis, trans) \ tree_node_t* sibling = parent->trans; \ \ if (sibling->red) { \ sibling->red = false; \ parent->red = true; \ tree__rotate_##cis(tree, parent); \ sibling = parent->trans; \ } \ if ((sibling->left && sibling->left->red) || \ (sibling->right && sibling->right->red)) { \ if (!sibling->trans || !sibling->trans->red) { \ sibling->cis->red = false; \ sibling->red = true; \ tree__rotate_##trans(tree, sibling); \ sibling = parent->trans; \ } \ sibling->red = parent->red; \ parent->red = sibling->trans->red = false; \ tree__rotate_##cis(tree, parent); \ node = tree->root; \ break; \ } \ sibling->red = true; void tree_del(tree_t* tree, tree_node_t* node) { tree_node_t* parent = node->parent; tree_node_t* left = node->left; tree_node_t* right = node->right; tree_node_t* next; bool red; if (!left) { next = right; } else if (!right) { next = left; } else { next = right; while (next->left) next = next->left; } if (parent) { if (parent->left == node) parent->left = next; else parent->right = next; } else { tree->root = next; } if (left && right) { red = next->red; next->red = node->red; next->left = left; left->parent = next; if (next != right) { parent = next->parent; next->parent = node->parent; node = next->right; parent->left = node; next->right = right; right->parent = next; } else { next->parent = parent; parent = next; node = next->right; } } else { red = node->red; node = next; } if (node) node->parent = parent; if (red) return; if (node && node->red) { node->red = false; return; } do { if (node == tree->root) break; if (node == parent->left) { TREE__REBALANCE_AFTER_REMOVE(left, right) } else { TREE__REBALANCE_AFTER_REMOVE(right, left) } node = parent; parent = parent->parent; } while (!node->red); if (node) node->red = false; } tree_node_t* tree_find(const tree_t* tree, uintptr_t key) { tree_node_t* node = tree->root; while (node) { if (key < node->key) node = node->left; else if (key > node->key) node = node->right; else return node; } return NULL; } tree_node_t* tree_root(const tree_t* tree) { return tree->root; } #ifndef SIO_BSP_HANDLE_POLL #define SIO_BSP_HANDLE_POLL 0x4800001D #endif #ifndef SIO_BASE_HANDLE #define SIO_BASE_HANDLE 0x48000022 #endif int ws_global_init(void) { int r; WSADATA wsa_data; r = WSAStartup(MAKEWORD(2, 2), &wsa_data); if (r != 0) return_set_error(-1, (DWORD) r); return 0; } static inline SOCKET ws__ioctl_get_bsp_socket(SOCKET socket, DWORD ioctl) { SOCKET bsp_socket; DWORD bytes; if (WSAIoctl(socket, ioctl, NULL, 0, &bsp_socket, sizeof bsp_socket, &bytes, NULL, NULL) != SOCKET_ERROR) return bsp_socket; else return INVALID_SOCKET; } SOCKET ws_get_base_socket(SOCKET socket) { SOCKET base_socket; DWORD error; for (;;) { base_socket = ws__ioctl_get_bsp_socket(socket, SIO_BASE_HANDLE); if (base_socket != INVALID_SOCKET) return base_socket; error = GetLastError(); if (error == WSAENOTSOCK) return_set_error(INVALID_SOCKET, error); /* Even though Microsoft documentation clearly states that LSPs should * never intercept the `SIO_BASE_HANDLE` ioctl [1], Komodia based LSPs do * so anyway, breaking it, with the apparent intention of preventing LSP * bypass [2]. Fortunately they don't handle `SIO_BSP_HANDLE_POLL`, which * will at least let us obtain the socket associated with the next winsock * protocol chain entry. If this succeeds, loop around and call * `SIO_BASE_HANDLE` again with the returned BSP socket, to make sure that * we unwrap all layers and retrieve the actual base socket. * [1] https://docs.microsoft.com/en-us/windows/win32/winsock/winsock-ioctls * [2] https://www.komodia.com/newwiki/index.php?title=Komodia%27s_Redirector_bug_fixes#Version_2.2.2.6 */ base_socket = ws__ioctl_get_bsp_socket(socket, SIO_BSP_HANDLE_POLL); if (base_socket != INVALID_SOCKET && base_socket != socket) socket = base_socket; else return_set_error(INVALID_SOCKET, error); } } ================================================ FILE: 3rd/compat-mingw/wepoll.h ================================================ /* * wepoll - epoll for Windows * https://github.com/piscisaureus/wepoll * * Copyright 2012-2020, Bert Belder * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef WEPOLL_H_ #define WEPOLL_H_ #ifndef WEPOLL_EXPORT #define WEPOLL_EXPORT #endif #include enum EPOLL_EVENTS { EPOLLIN = (int) (1U << 0), EPOLLPRI = (int) (1U << 1), EPOLLOUT = (int) (1U << 2), EPOLLERR = (int) (1U << 3), EPOLLHUP = (int) (1U << 4), EPOLLRDNORM = (int) (1U << 6), EPOLLRDBAND = (int) (1U << 7), EPOLLWRNORM = (int) (1U << 8), EPOLLWRBAND = (int) (1U << 9), EPOLLMSG = (int) (1U << 10), /* Never reported. */ EPOLLRDHUP = (int) (1U << 13), EPOLLONESHOT = (int) (1U << 31) }; #define EPOLLIN (1U << 0) #define EPOLLPRI (1U << 1) #define EPOLLOUT (1U << 2) #define EPOLLERR (1U << 3) #define EPOLLHUP (1U << 4) #define EPOLLRDNORM (1U << 6) #define EPOLLRDBAND (1U << 7) #define EPOLLWRNORM (1U << 8) #define EPOLLWRBAND (1U << 9) #define EPOLLMSG (1U << 10) #define EPOLLRDHUP (1U << 13) #define EPOLLONESHOT (1U << 31) #define EPOLL_CTL_ADD 1 #define EPOLL_CTL_MOD 2 #define EPOLL_CTL_DEL 3 typedef void* HANDLE; typedef uintptr_t SOCKET; typedef union epoll_data { void* ptr; int fd; uint32_t u32; uint64_t u64; SOCKET sock; /* Windows specific */ HANDLE hnd; /* Windows specific */ } epoll_data_t; struct epoll_event { uint32_t events; /* Epoll events and flags */ epoll_data_t data; /* User data variable */ }; #ifdef __cplusplus extern "C" { #endif WEPOLL_EXPORT HANDLE epoll_create(int size); WEPOLL_EXPORT HANDLE epoll_create1(int flags); WEPOLL_EXPORT int epoll_close(HANDLE ephnd); WEPOLL_EXPORT int epoll_ctl(HANDLE ephnd, int op, SOCKET sock, struct epoll_event* event); WEPOLL_EXPORT int epoll_wait(HANDLE ephnd, struct epoll_event* events, int maxevents, int timeout); #ifdef __cplusplus } /* extern "C" */ #endif #endif /* WEPOLL_H_ */ ================================================ FILE: 3rd/lpeg/HISTORY ================================================ HISTORY for LPeg 1.1.0 * Changes from version 1.0.2 to 1.1.0 --------------------------------- + accumulator capture + UTF-8 ranges + Larger limit for number of rules in a grammar + Larger limit for number of captures in a match + bug fixes + other small improvements * Changes from version 1.0.1 to 1.0.2 --------------------------------- + some bugs fixed * Changes from version 0.12 to 1.0.1 --------------------------------- + group "names" can be any Lua value + some bugs fixed + other small improvements * Changes from version 0.11 to 0.12 --------------------------------- + no "unsigned short" limit for pattern sizes + mathtime captures considered nullable + some bugs fixed * Changes from version 0.10 to 0.11 ------------------------------- + complete reimplementation of the code generator + new syntax for table captures + new functions in module 're' + other small improvements * Changes from version 0.9 to 0.10 ------------------------------- + backtrack stack has configurable size + better error messages + Notation for non-terminals in 're' back to A instead o + experimental look-behind pattern + support for external extensions + works with Lua 5.2 + consumes less C stack - "and" predicates do not keep captures * Changes from version 0.8 to 0.9 ------------------------------- + The accumulator capture was replaced by a fold capture; programs that used the old 'lpeg.Ca' will need small changes. + Some support for character classes from old C locales. + A new named-group capture. * Changes from version 0.7 to 0.8 ------------------------------- + New "match-time" capture. + New "argument capture" that allows passing arguments into the pattern. + Better documentation for 're'. + Several small improvements for 're'. + The 're' module has an incompatibility with previous versions: now, any use of a non-terminal must be enclosed in angle brackets (like ). * Changes from version 0.6 to 0.7 ------------------------------- + Several improvements in module 're': - better documentation; - support for most captures (all but accumulator); - limited repetitions p{n,m}. + Small improvements in efficiency. + Several small bugs corrected (special thanks to Hans Hagen and Taco Hoekwater). * Changes from version 0.5 to 0.6 ------------------------------- + Support for non-numeric indices in grammars. + Some bug fixes (thanks to the luatex team). + Some new optimizations; (thanks to Mike Pall). + A new page layout (thanks to Andre Carregal). + Minimal documentation for module 're'. * Changes from version 0.4 to 0.5 ------------------------------- + Several optimizations. + lpeg.P now accepts booleans. + Some new examples. + A proper license. + Several small improvements. * Changes from version 0.3 to 0.4 ------------------------------- + Static check for loops in repetitions and grammars. + Removed label option in captures. + The implementation of captures uses less memory. * Changes from version 0.2 to 0.3 ------------------------------- + User-defined patterns in Lua. + Several new captures. * Changes from version 0.1 to 0.2 ------------------------------- + Several small corrections. + Handles embedded zeros like any other character. + Capture "name" can be any Lua value. + Unlimited number of captures. + Match gets an optional initial position. (end of HISTORY) ================================================ FILE: 3rd/lpeg/README.md ================================================ # LPeg - Parsing Expression Grammars For Lua For more information, see [Lpeg](//www.inf.puc-rio.br/~roberto/lpeg/). ================================================ FILE: 3rd/lpeg/lpcap.c ================================================ #include "lua.h" #include "lauxlib.h" #include "lpcap.h" #include "lpprint.h" #include "lptypes.h" #define getfromktable(cs,v) lua_rawgeti((cs)->L, ktableidx((cs)->ptop), v) #define pushluaval(cs) getfromktable(cs, (cs)->cap->idx) #define skipclose(cs,head) \ if (isopencap(head)) { assert(isclosecap(cs->cap)); cs->cap++; } /* ** Return the size of capture 'cap'. If it is an open capture, 'close' ** must be its corresponding close. */ static Index_t capsize (Capture *cap, Capture *close) { if (isopencap(cap)) { assert(isclosecap(close)); return close->index - cap->index; } else return cap->siz - 1; } static Index_t closesize (CapState *cs, Capture *head) { return capsize(head, cs->cap); } /* ** Put at the cache for Lua values the value indexed by 'v' in ktable ** of the running pattern (if it is not there yet); returns its index. */ static int updatecache (CapState *cs, int v) { int idx = cs->ptop + 1; /* stack index of cache for Lua values */ if (v != cs->valuecached) { /* not there? */ getfromktable(cs, v); /* get value from 'ktable' */ lua_replace(cs->L, idx); /* put it at reserved stack position */ cs->valuecached = v; /* keep track of what is there */ } return idx; } static int pushcapture (CapState *cs); /* ** Goes back in a list of captures looking for an open capture ** corresponding to a close one. */ static Capture *findopen (Capture *cap) { int n = 0; /* number of closes waiting an open */ for (;;) { cap--; if (isclosecap(cap)) n++; /* one more open to skip */ else if (isopencap(cap)) if (n-- == 0) return cap; } } /* ** Go to the next capture at the same level. */ static void nextcap (CapState *cs) { Capture *cap = cs->cap; if (isopencap(cap)) { /* must look for a close? */ int n = 0; /* number of opens waiting a close */ for (;;) { /* look for corresponding close */ cap++; if (isopencap(cap)) n++; else if (isclosecap(cap)) if (n-- == 0) break; } cs->cap = cap + 1; /* + 1 to skip last close */ } else { Capture *next; for (next = cap + 1; capinside(cap, next); next++) ; /* skip captures inside current one */ cs->cap = next; } } /* ** Push on the Lua stack all values generated by nested captures inside ** the current capture. Returns number of values pushed. 'addextra' ** makes it push the entire match after all captured values. The ** entire match is pushed also if there are no other nested values, ** so the function never returns zero. */ static int pushnestedvalues (CapState *cs, int addextra) { Capture *head = cs->cap++; /* original capture */ int n = 0; /* number of pushed subvalues */ /* repeat for all nested patterns */ while (capinside(head, cs->cap)) n += pushcapture(cs); if (addextra || n == 0) { /* need extra? */ lua_pushlstring(cs->L, cs->s + head->index, closesize(cs, head)); n++; } skipclose(cs, head); return n; } /* ** Push only the first value generated by nested captures */ static void pushonenestedvalue (CapState *cs) { int n = pushnestedvalues(cs, 0); if (n > 1) lua_pop(cs->L, n - 1); /* pop extra values */ } /* ** Checks whether group 'grp' is visible to 'ref', that is, 'grp' is ** not nested inside a full capture that does not contain 'ref'. (We ** only need to care for full captures because the search at 'findback' ** skips open-end blocks; so, if 'grp' is nested in a non-full capture, ** 'ref' is also inside it.) To check this, we search backward for the ** inner full capture enclosing 'grp'. A full capture cannot contain ** non-full captures, so a close capture means we cannot be inside a ** full capture anymore. */ static int capvisible (CapState *cs, Capture *grp, Capture *ref) { Capture *cap = grp; int i = MAXLOP; /* maximum distance for an 'open' */ while (i-- > 0 && cap-- > cs->ocap) { if (isclosecap(cap)) return 1; /* can stop the search */ else if (grp->index - cap->index >= UCHAR_MAX) return 1; /* can stop the search */ else if (capinside(cap, grp)) /* is 'grp' inside cap? */ return capinside(cap, ref); /* ok iff cap also contains 'ref' */ } return 1; /* 'grp' is not inside any capture */ } /* ** Try to find a named group capture with the name given at the top of ** the stack; goes backward from 'ref'. */ static Capture *findback (CapState *cs, Capture *ref) { lua_State *L = cs->L; Capture *cap = ref; while (cap-- > cs->ocap) { /* repeat until end of list */ if (isclosecap(cap)) cap = findopen(cap); /* skip nested captures */ else if (capinside(cap, ref)) continue; /* enclosing captures are not visible to 'ref' */ if (captype(cap) == Cgroup && capvisible(cs, cap, ref)) { getfromktable(cs, cap->idx); /* get group name */ if (lp_equal(L, -2, -1)) { /* right group? */ lua_pop(L, 2); /* remove reference name and group name */ return cap; } else lua_pop(L, 1); /* remove group name */ } } luaL_error(L, "back reference '%s' not found", lua_tostring(L, -1)); return NULL; /* to avoid warnings */ } /* ** Back-reference capture. Return number of values pushed. */ static int backrefcap (CapState *cs) { int n; Capture *curr = cs->cap; pushluaval(cs); /* reference name */ cs->cap = findback(cs, curr); /* find corresponding group */ n = pushnestedvalues(cs, 0); /* push group's values */ cs->cap = curr + 1; return n; } /* ** Table capture: creates a new table and populates it with nested ** captures. */ static int tablecap (CapState *cs) { lua_State *L = cs->L; Capture *head = cs->cap++; int n = 0; lua_newtable(L); while (capinside(head, cs->cap)) { if (captype(cs->cap) == Cgroup && cs->cap->idx != 0) { /* named group? */ pushluaval(cs); /* push group name */ pushonenestedvalue(cs); lua_settable(L, -3); } else { /* not a named group */ int i; int k = pushcapture(cs); for (i = k; i > 0; i--) /* store all values into table */ lua_rawseti(L, -(i + 1), n + i); n += k; } } skipclose(cs, head); return 1; /* number of values pushed (only the table) */ } /* ** Table-query capture */ static int querycap (CapState *cs) { int idx = cs->cap->idx; pushonenestedvalue(cs); /* get nested capture */ lua_gettable(cs->L, updatecache(cs, idx)); /* query cap. value at table */ if (!lua_isnil(cs->L, -1)) return 1; else { /* no value */ lua_pop(cs->L, 1); /* remove nil */ return 0; } } /* ** Fold capture */ static int foldcap (CapState *cs) { int n; lua_State *L = cs->L; Capture *head = cs->cap++; int idx = head->idx; if (isclosecap(cs->cap) || /* no nested captures (large subject)? */ (n = pushcapture(cs)) == 0) /* nested captures with no values? */ return luaL_error(L, "no initial value for fold capture"); if (n > 1) lua_pop(L, n - 1); /* leave only one result for accumulator */ while (capinside(head, cs->cap)) { lua_pushvalue(L, updatecache(cs, idx)); /* get folding function */ lua_insert(L, -2); /* put it before accumulator */ n = pushcapture(cs); /* get next capture's values */ lua_call(L, n + 1, 1); /* call folding function */ } skipclose(cs, head); return 1; /* only accumulator left on the stack */ } /* ** Function capture */ static int functioncap (CapState *cs) { int n; int top = lua_gettop(cs->L); pushluaval(cs); /* push function */ n = pushnestedvalues(cs, 0); /* push nested captures */ lua_call(cs->L, n, LUA_MULTRET); /* call function */ return lua_gettop(cs->L) - top; /* return function's results */ } /* ** Accumulator capture */ static int accumulatorcap (CapState *cs) { lua_State *L = cs->L; int n; if (lua_gettop(L) < cs->firstcap) luaL_error(L, "no previous value for accumulator capture"); pushluaval(cs); /* push function */ lua_insert(L, -2); /* previous value becomes first argument */ n = pushnestedvalues(cs, 0); /* push nested captures */ lua_call(L, n + 1, 1); /* call function */ return 0; /* did not add any extra value */ } /* ** Select capture */ static int numcap (CapState *cs) { int idx = cs->cap->idx; /* value to select */ if (idx == 0) { /* no values? */ nextcap(cs); /* skip entire capture */ return 0; /* no value produced */ } else { int n = pushnestedvalues(cs, 0); if (n < idx) /* invalid index? */ return luaL_error(cs->L, "no capture '%d'", idx); else { lua_pushvalue(cs->L, -(n - idx + 1)); /* get selected capture */ lua_replace(cs->L, -(n + 1)); /* put it in place of 1st capture */ lua_pop(cs->L, n - 1); /* remove other captures */ return 1; } } } /* ** Return the stack index of the first runtime capture in the given ** list of captures (or zero if no runtime captures) */ int finddyncap (Capture *cap, Capture *last) { for (; cap < last; cap++) { if (cap->kind == Cruntime) return cap->idx; /* stack position of first capture */ } return 0; /* no dynamic captures in this segment */ } /* ** Calls a runtime capture. Returns number of captures "removed" by the ** call, that is, those inside the group capture. Captures to be added ** are on the Lua stack. */ int runtimecap (CapState *cs, Capture *close, const char *s, int *rem) { int n, id; lua_State *L = cs->L; int otop = lua_gettop(L); Capture *open = findopen(close); /* get open group capture */ assert(captype(open) == Cgroup); id = finddyncap(open, close); /* get first dynamic capture argument */ close->kind = Cclose; /* closes the group */ close->index = s - cs->s; cs->cap = open; cs->valuecached = 0; /* prepare capture state */ luaL_checkstack(L, 4, "too many runtime captures"); pushluaval(cs); /* push function to be called */ lua_pushvalue(L, SUBJIDX); /* push original subject */ lua_pushinteger(L, s - cs->s + 1); /* push current position */ n = pushnestedvalues(cs, 0); /* push nested captures */ lua_call(L, n + 2, LUA_MULTRET); /* call dynamic function */ if (id > 0) { /* are there old dynamic captures to be removed? */ int i; for (i = id; i <= otop; i++) lua_remove(L, id); /* remove old dynamic captures */ *rem = otop - id + 1; /* total number of dynamic captures removed */ } else *rem = 0; /* no dynamic captures removed */ return close - open - 1; /* number of captures to be removed */ } /* ** Auxiliary structure for substitution and string captures: keep ** information about nested captures for future use, avoiding to push ** string results into Lua */ typedef struct StrAux { int isstring; /* whether capture is a string */ union { Capture *cp; /* if not a string, respective capture */ struct { /* if it is a string... */ Index_t idx; /* starts here */ Index_t siz; /* with this size */ } s; } u; } StrAux; #define MAXSTRCAPS 10 /* ** Collect values from current capture into array 'cps'. Current ** capture must be Cstring (first call) or Csimple (recursive calls). ** (In first call, fills %0 with whole match for Cstring.) ** Returns number of elements in the array that were filled. */ static int getstrcaps (CapState *cs, StrAux *cps, int n) { int k = n++; Capture *head = cs->cap++; cps[k].isstring = 1; /* get string value */ cps[k].u.s.idx = head->index; /* starts here */ while (capinside(head, cs->cap)) { if (n >= MAXSTRCAPS) /* too many captures? */ nextcap(cs); /* skip extra captures (will not need them) */ else if (captype(cs->cap) == Csimple) /* string? */ n = getstrcaps(cs, cps, n); /* put info. into array */ else { cps[n].isstring = 0; /* not a string */ cps[n].u.cp = cs->cap; /* keep original capture */ nextcap(cs); n++; } } cps[k].u.s.siz = closesize(cs, head); skipclose(cs, head); return n; } /* ** add next capture value (which should be a string) to buffer 'b' */ static int addonestring (luaL_Buffer *b, CapState *cs, const char *what); /* ** String capture: add result to buffer 'b' (instead of pushing ** it into the stack) */ static void stringcap (luaL_Buffer *b, CapState *cs) { StrAux cps[MAXSTRCAPS]; int n; size_t len, i; const char *fmt; /* format string */ fmt = lua_tolstring(cs->L, updatecache(cs, cs->cap->idx), &len); n = getstrcaps(cs, cps, 0) - 1; /* collect nested captures */ for (i = 0; i < len; i++) { /* traverse format string */ if (fmt[i] != '%') /* not an escape? */ luaL_addchar(b, fmt[i]); /* add it to buffer */ else if (fmt[++i] < '0' || fmt[i] > '9') /* not followed by a digit? */ luaL_addchar(b, fmt[i]); /* add to buffer */ else { int l = fmt[i] - '0'; /* capture index */ if (l > n) luaL_error(cs->L, "invalid capture index (%d)", l); else if (cps[l].isstring) luaL_addlstring(b, cs->s + cps[l].u.s.idx, cps[l].u.s.siz); else { Capture *curr = cs->cap; cs->cap = cps[l].u.cp; /* go back to evaluate that nested capture */ if (!addonestring(b, cs, "capture")) luaL_error(cs->L, "no values in capture index %d", l); cs->cap = curr; /* continue from where it stopped */ } } } } /* ** Substitution capture: add result to buffer 'b' */ static void substcap (luaL_Buffer *b, CapState *cs) { const char *curr = cs->s + cs->cap->index; Capture *head = cs->cap++; while (capinside(head, cs->cap)) { Capture *cap = cs->cap; const char *caps = cs->s + cap->index; luaL_addlstring(b, curr, caps - curr); /* add text up to capture */ if (addonestring(b, cs, "replacement")) curr = caps + capsize(cap, cs->cap - 1); /* continue after match */ else /* no capture value */ curr = caps; /* keep original text in final result */ } /* add last piece of text */ luaL_addlstring(b, curr, cs->s + head->index + closesize(cs, head) - curr); skipclose(cs, head); } /* ** Evaluates a capture and adds its first value to buffer 'b'; returns ** whether there was a value */ static int addonestring (luaL_Buffer *b, CapState *cs, const char *what) { switch (captype(cs->cap)) { case Cstring: stringcap(b, cs); /* add capture directly to buffer */ return 1; case Csubst: substcap(b, cs); /* add capture directly to buffer */ return 1; case Cacc: /* accumulator capture? */ return luaL_error(cs->L, "invalid context for an accumulator capture"); default: { lua_State *L = cs->L; int n = pushcapture(cs); if (n > 0) { if (n > 1) lua_pop(L, n - 1); /* only one result */ if (!lua_isstring(L, -1)) return luaL_error(L, "invalid %s value (a %s)", what, luaL_typename(L, -1)); luaL_addvalue(b); } return n; } } } #if !defined(MAXRECLEVEL) #define MAXRECLEVEL 200 #endif /* ** Push all values of the current capture into the stack; returns ** number of values pushed */ static int pushcapture (CapState *cs) { lua_State *L = cs->L; int res; luaL_checkstack(L, 4, "too many captures"); if (cs->reclevel++ > MAXRECLEVEL) return luaL_error(L, "subcapture nesting too deep"); switch (captype(cs->cap)) { case Cposition: { lua_pushinteger(L, cs->cap->index + 1); cs->cap++; res = 1; break; } case Cconst: { pushluaval(cs); cs->cap++; res = 1; break; } case Carg: { int arg = (cs->cap++)->idx; if (arg + FIXEDARGS > cs->ptop) return luaL_error(L, "reference to absent extra argument #%d", arg); lua_pushvalue(L, arg + FIXEDARGS); res = 1; break; } case Csimple: { int k = pushnestedvalues(cs, 1); lua_insert(L, -k); /* make whole match be first result */ res = k; break; } case Cruntime: { lua_pushvalue(L, (cs->cap++)->idx); /* value is in the stack */ res = 1; break; } case Cstring: { luaL_Buffer b; luaL_buffinit(L, &b); stringcap(&b, cs); luaL_pushresult(&b); res = 1; break; } case Csubst: { luaL_Buffer b; luaL_buffinit(L, &b); substcap(&b, cs); luaL_pushresult(&b); res = 1; break; } case Cgroup: { if (cs->cap->idx == 0) /* anonymous group? */ res = pushnestedvalues(cs, 0); /* add all nested values */ else { /* named group: add no values */ nextcap(cs); /* skip capture */ res = 0; } break; } case Cbackref: res = backrefcap(cs); break; case Ctable: res = tablecap(cs); break; case Cfunction: res = functioncap(cs); break; case Cacc: res = accumulatorcap(cs); break; case Cnum: res = numcap(cs); break; case Cquery: res = querycap(cs); break; case Cfold: res = foldcap(cs); break; default: assert(0); res = 0; } cs->reclevel--; return res; } /* ** Prepare a CapState structure and traverse the entire list of ** captures in the stack pushing its results. 's' is the subject ** string, 'r' is the final position of the match, and 'ptop' ** the index in the stack where some useful values were pushed. ** Returns the number of results pushed. (If the list produces no ** results, push the final position of the match.) */ int getcaptures (lua_State *L, const char *s, const char *r, int ptop) { Capture *capture = (Capture *)lua_touserdata(L, caplistidx(ptop)); int n = 0; /* printcaplist(capture); */ if (!isclosecap(capture)) { /* is there any capture? */ CapState cs; cs.ocap = cs.cap = capture; cs.L = L; cs.reclevel = 0; cs.s = s; cs.valuecached = 0; cs.ptop = ptop; cs.firstcap = lua_gettop(L) + 1; /* where first value (if any) will go */ do { /* collect their values */ n += pushcapture(&cs); } while (!isclosecap(cs.cap)); assert(lua_gettop(L) - cs.firstcap == n - 1); } if (n == 0) { /* no capture values? */ lua_pushinteger(L, r - s + 1); /* return only end position */ n = 1; } return n; } ================================================ FILE: 3rd/lpeg/lpcap.h ================================================ #if !defined(lpcap_h) #define lpcap_h #include "lptypes.h" /* kinds of captures */ typedef enum CapKind { Cclose, /* not used in trees */ Cposition, Cconst, /* ktable[key] is Lua constant */ Cbackref, /* ktable[key] is "name" of group to get capture */ Carg, /* 'key' is arg's number */ Csimple, /* next node is pattern */ Ctable, /* next node is pattern */ Cfunction, /* ktable[key] is function; next node is pattern */ Cacc, /* ktable[key] is function; next node is pattern */ Cquery, /* ktable[key] is table; next node is pattern */ Cstring, /* ktable[key] is string; next node is pattern */ Cnum, /* numbered capture; 'key' is number of value to return */ Csubst, /* substitution capture; next node is pattern */ Cfold, /* ktable[key] is function; next node is pattern */ Cruntime, /* not used in trees (is uses another type for tree) */ Cgroup /* ktable[key] is group's "name" */ } CapKind; /* ** An unsigned integer large enough to index any subject entirely. ** It can be size_t, but that will double the size of the array ** of captures in a 64-bit machine. */ #if !defined(Index_t) typedef uint Index_t; #endif #define MAXINDT (~(Index_t)0) typedef struct Capture { Index_t index; /* subject position */ unsigned short idx; /* extra info (group name, arg index, etc.) */ byte kind; /* kind of capture */ byte siz; /* size of full capture + 1 (0 = not a full capture) */ } Capture; typedef struct CapState { Capture *cap; /* current capture */ Capture *ocap; /* (original) capture list */ lua_State *L; int ptop; /* stack index of last argument to 'match' */ int firstcap; /* stack index of first capture pushed in the stack */ const char *s; /* original string */ int valuecached; /* value stored in cache slot */ int reclevel; /* recursion level */ } CapState; #define captype(cap) ((cap)->kind) #define isclosecap(cap) (captype(cap) == Cclose) #define isopencap(cap) ((cap)->siz == 0) /* true if c2 is (any number of levels) inside c1 */ #define capinside(c1,c2) \ (isopencap(c1) ? !isclosecap(c2) \ : (c2)->index < (c1)->index + (c1)->siz - 1) /** ** Maximum number of captures to visit when looking for an 'open'. */ #define MAXLOP 20 int runtimecap (CapState *cs, Capture *close, const char *s, int *rem); int getcaptures (lua_State *L, const char *s, const char *r, int ptop); int finddyncap (Capture *cap, Capture *last); #endif ================================================ FILE: 3rd/lpeg/lpcode.c ================================================ #include #include "lua.h" #include "lauxlib.h" #include "lptypes.h" #include "lpcode.h" #include "lpcset.h" /* signals a "no-instruction */ #define NOINST -1 static const Charset fullset_ = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}; static const Charset *fullset = &fullset_; /* ** {====================================================== ** Analysis and some optimizations ** ======================================================= */ /* ** A few basic operations on Charsets */ static void cs_complement (Charset *cs) { loopset(i, cs->cs[i] = ~cs->cs[i]); } static int cs_disjoint (const Charset *cs1, const Charset *cs2) { loopset(i, if ((cs1->cs[i] & cs2->cs[i]) != 0) return 0;) return 1; } /* ** Visit a TCall node taking care to stop recursion. If node not yet ** visited, return 'f(sib2(tree))', otherwise return 'def' (default ** value) */ static int callrecursive (TTree *tree, int f (TTree *t), int def) { int key = tree->key; assert(tree->tag == TCall); assert(sib2(tree)->tag == TRule); if (key == 0) /* node already visited? */ return def; /* return default value */ else { /* first visit */ int result; tree->key = 0; /* mark call as already visited */ result = f(sib2(tree)); /* go to called rule */ tree->key = key; /* restore tree */ return result; } } /* ** Check whether a pattern tree has captures */ int hascaptures (TTree *tree) { tailcall: switch (tree->tag) { case TCapture: case TRunTime: return 1; case TCall: return callrecursive(tree, hascaptures, 0); case TRule: /* do not follow siblings */ tree = sib1(tree); goto tailcall; case TOpenCall: assert(0); default: { switch (numsiblings[tree->tag]) { case 1: /* return hascaptures(sib1(tree)); */ tree = sib1(tree); goto tailcall; case 2: if (hascaptures(sib1(tree))) return 1; /* else return hascaptures(sib2(tree)); */ tree = sib2(tree); goto tailcall; default: assert(numsiblings[tree->tag] == 0); return 0; } } } } /* ** Checks how a pattern behaves regarding the empty string, ** in one of two different ways: ** A pattern is *nullable* if it can match without consuming any character; ** A pattern is *nofail* if it never fails for any string ** (including the empty string). ** The difference is only for predicates and run-time captures; ** for other patterns, the two properties are equivalent. ** (With predicates, &'a' is nullable but not nofail. Of course, ** nofail => nullable.) ** These functions are all convervative in the following way: ** p is nullable => nullable(p) ** nofail(p) => p cannot fail ** The function assumes that TOpenCall is not nullable; ** this will be checked again when the grammar is fixed. ** Run-time captures can do whatever they want, so the result ** is conservative. */ int checkaux (TTree *tree, int pred) { tailcall: switch (tree->tag) { case TChar: case TSet: case TAny: case TUTFR: case TFalse: case TOpenCall: return 0; /* not nullable */ case TRep: case TTrue: return 1; /* no fail */ case TNot: case TBehind: /* can match empty, but can fail */ if (pred == PEnofail) return 0; else return 1; /* PEnullable */ case TAnd: /* can match empty; fail iff body does */ if (pred == PEnullable) return 1; /* else return checkaux(sib1(tree), pred); */ tree = sib1(tree); goto tailcall; case TRunTime: /* can fail; match empty iff body does */ if (pred == PEnofail) return 0; /* else return checkaux(sib1(tree), pred); */ tree = sib1(tree); goto tailcall; case TSeq: if (!checkaux(sib1(tree), pred)) return 0; /* else return checkaux(sib2(tree), pred); */ tree = sib2(tree); goto tailcall; case TChoice: if (checkaux(sib2(tree), pred)) return 1; /* else return checkaux(sib1(tree), pred); */ tree = sib1(tree); goto tailcall; case TCapture: case TGrammar: case TRule: case TXInfo: /* return checkaux(sib1(tree), pred); */ tree = sib1(tree); goto tailcall; case TCall: /* return checkaux(sib2(tree), pred); */ tree = sib2(tree); goto tailcall; default: assert(0); return 0; } } /* ** number of characters to match a pattern (or -1 if variable) */ int fixedlen (TTree *tree) { int len = 0; /* to accumulate in tail calls */ tailcall: switch (tree->tag) { case TChar: case TSet: case TAny: return len + 1; case TUTFR: return (tree->cap == sib1(tree)->cap) ? len + tree->cap : -1; case TFalse: case TTrue: case TNot: case TAnd: case TBehind: return len; case TRep: case TRunTime: case TOpenCall: return -1; case TCapture: case TRule: case TGrammar: case TXInfo: /* return fixedlen(sib1(tree)); */ tree = sib1(tree); goto tailcall; case TCall: { int n1 = callrecursive(tree, fixedlen, -1); if (n1 < 0) return -1; else return len + n1; } case TSeq: { int n1 = fixedlen(sib1(tree)); if (n1 < 0) return -1; /* else return fixedlen(sib2(tree)) + len; */ len += n1; tree = sib2(tree); goto tailcall; } case TChoice: { int n1 = fixedlen(sib1(tree)); int n2 = fixedlen(sib2(tree)); if (n1 != n2 || n1 < 0) return -1; else return len + n1; } default: assert(0); return 0; }; } /* ** Computes the 'first set' of a pattern. ** The result is a conservative aproximation: ** match p ax -> x (for some x) ==> a belongs to first(p) ** or ** a not in first(p) ==> match p ax -> fail (for all x) ** ** The set 'follow' is the first set of what follows the ** pattern (full set if nothing follows it). ** ** The function returns 0 when this resulting set can be used for ** test instructions that avoid the pattern altogether. ** A non-zero return can happen for two reasons: ** 1) match p '' -> '' ==> return has bit 1 set ** (tests cannot be used because they would always fail for an empty input); ** 2) there is a match-time capture ==> return has bit 2 set ** (optimizations should not bypass match-time captures). */ static int getfirst (TTree *tree, const Charset *follow, Charset *firstset) { tailcall: switch (tree->tag) { case TChar: case TSet: case TAny: case TFalse: { tocharset(tree, firstset); return 0; } case TUTFR: { int c; clearset(firstset->cs); /* erase all chars */ for (c = tree->key; c <= sib1(tree)->key; c++) setchar(firstset->cs, c); return 0; } case TTrue: { loopset(i, firstset->cs[i] = follow->cs[i]); return 1; /* accepts the empty string */ } case TChoice: { Charset csaux; int e1 = getfirst(sib1(tree), follow, firstset); int e2 = getfirst(sib2(tree), follow, &csaux); loopset(i, firstset->cs[i] |= csaux.cs[i]); return e1 | e2; } case TSeq: { if (!nullable(sib1(tree))) { /* when p1 is not nullable, p2 has nothing to contribute; return getfirst(sib1(tree), fullset, firstset); */ tree = sib1(tree); follow = fullset; goto tailcall; } else { /* FIRST(p1 p2, fl) = FIRST(p1, FIRST(p2, fl)) */ Charset csaux; int e2 = getfirst(sib2(tree), follow, &csaux); int e1 = getfirst(sib1(tree), &csaux, firstset); if (e1 == 0) return 0; /* 'e1' ensures that first can be used */ else if ((e1 | e2) & 2) /* one of the children has a matchtime? */ return 2; /* pattern has a matchtime capture */ else return e2; /* else depends on 'e2' */ } } case TRep: { getfirst(sib1(tree), follow, firstset); loopset(i, firstset->cs[i] |= follow->cs[i]); return 1; /* accept the empty string */ } case TCapture: case TGrammar: case TRule: case TXInfo: { /* return getfirst(sib1(tree), follow, firstset); */ tree = sib1(tree); goto tailcall; } case TRunTime: { /* function invalidates any follow info. */ int e = getfirst(sib1(tree), fullset, firstset); if (e) return 2; /* function is not "protected"? */ else return 0; /* pattern inside capture ensures first can be used */ } case TCall: { /* return getfirst(sib2(tree), follow, firstset); */ tree = sib2(tree); goto tailcall; } case TAnd: { int e = getfirst(sib1(tree), follow, firstset); loopset(i, firstset->cs[i] &= follow->cs[i]); return e; } case TNot: { if (tocharset(sib1(tree), firstset)) { cs_complement(firstset); return 1; } /* else */ } /* FALLTHROUGH */ case TBehind: { /* instruction gives no new information */ /* call 'getfirst' only to check for math-time captures */ int e = getfirst(sib1(tree), follow, firstset); loopset(i, firstset->cs[i] = follow->cs[i]); /* uses follow */ return e | 1; /* always can accept the empty string */ } default: assert(0); return 0; } } /* ** If 'headfail(tree)' true, then 'tree' can fail only depending on the ** next character of the subject. */ static int headfail (TTree *tree) { tailcall: switch (tree->tag) { case TChar: case TSet: case TAny: case TFalse: return 1; case TTrue: case TRep: case TRunTime: case TNot: case TBehind: case TUTFR: return 0; case TCapture: case TGrammar: case TRule: case TXInfo: case TAnd: tree = sib1(tree); goto tailcall; /* return headfail(sib1(tree)); */ case TCall: tree = sib2(tree); goto tailcall; /* return headfail(sib2(tree)); */ case TSeq: if (!nofail(sib2(tree))) return 0; /* else return headfail(sib1(tree)); */ tree = sib1(tree); goto tailcall; case TChoice: if (!headfail(sib1(tree))) return 0; /* else return headfail(sib2(tree)); */ tree = sib2(tree); goto tailcall; default: assert(0); return 0; } } /* ** Check whether the code generation for the given tree can benefit ** from a follow set (to avoid computing the follow set when it is ** not needed) */ static int needfollow (TTree *tree) { tailcall: switch (tree->tag) { case TChar: case TSet: case TAny: case TUTFR: case TFalse: case TTrue: case TAnd: case TNot: case TRunTime: case TGrammar: case TCall: case TBehind: return 0; case TChoice: case TRep: return 1; case TCapture: tree = sib1(tree); goto tailcall; case TSeq: tree = sib2(tree); goto tailcall; default: assert(0); return 0; } } /* }====================================================== */ /* ** {====================================================== ** Code generation ** ======================================================= */ /* ** size of an instruction */ int sizei (const Instruction *i) { switch((Opcode)i->i.code) { case ISet: case ISpan: return 1 + i->i.aux2.set.size; case ITestSet: return 2 + i->i.aux2.set.size; case ITestChar: case ITestAny: case IChoice: case IJmp: case ICall: case IOpenCall: case ICommit: case IPartialCommit: case IBackCommit: case IUTFR: return 2; default: return 1; } } /* ** state for the compiler */ typedef struct CompileState { Pattern *p; /* pattern being compiled */ int ncode; /* next position in p->code to be filled */ lua_State *L; } CompileState; /* ** code generation is recursive; 'opt' indicates that the code is being ** generated as the last thing inside an optional pattern (so, if that ** code is optional too, it can reuse the 'IChoice' already in place for ** the outer pattern). 'tt' points to a previous test protecting this ** code (or NOINST). 'fl' is the follow set of the pattern. */ static void codegen (CompileState *compst, TTree *tree, int opt, int tt, const Charset *fl); static void finishrelcode (lua_State *L, Pattern *p, Instruction *block, int size) { if (block == NULL) luaL_error(L, "not enough memory"); block->codesize = size; p->code = (Instruction *)block + 1; } /* ** Initialize array 'p->code' */ static void newcode (lua_State *L, Pattern *p, int size) { void *ud; Instruction *block; lua_Alloc f = lua_getallocf(L, &ud); size++; /* slot for 'codesize' */ block = (Instruction*) f(ud, NULL, 0, size * sizeof(Instruction)); finishrelcode(L, p, block, size); } void freecode (lua_State *L, Pattern *p) { if (p->code != NULL) { void *ud; lua_Alloc f = lua_getallocf(L, &ud); uint osize = p->code[-1].codesize; f(ud, p->code - 1, osize * sizeof(Instruction), 0); /* free block */ } } /* ** Assume that 'nsize' is not zero and that 'p->code' already exists. */ static void realloccode (lua_State *L, Pattern *p, int nsize) { void *ud; lua_Alloc f = lua_getallocf(L, &ud); Instruction *block = p->code - 1; uint osize = block->codesize; nsize++; /* add the 'codesize' slot to size */ block = (Instruction*) f(ud, block, osize * sizeof(Instruction), nsize * sizeof(Instruction)); finishrelcode(L, p, block, nsize); } /* ** Add space for an instruction with 'n' slots and return its index. */ static int nextinstruction (CompileState *compst, int n) { int size = compst->p->code[-1].codesize - 1; int ncode = compst->ncode; if (ncode > size - n) { uint nsize = size + (size >> 1) + n; if (nsize >= INT_MAX) luaL_error(compst->L, "pattern code too large"); realloccode(compst->L, compst->p, nsize); } compst->ncode = ncode + n; return ncode; } #define getinstr(cs,i) ((cs)->p->code[i]) static int addinstruction (CompileState *compst, Opcode op, int aux) { int i = nextinstruction(compst, 1); getinstr(compst, i).i.code = op; getinstr(compst, i).i.aux1 = aux; return i; } /* ** Add an instruction followed by space for an offset (to be set later) */ static int addoffsetinst (CompileState *compst, Opcode op) { int i = addinstruction(compst, op, 0); /* instruction */ addinstruction(compst, (Opcode)0, 0); /* open space for offset */ assert(op == ITestSet || sizei(&getinstr(compst, i)) == 2); return i; } /* ** Set the offset of an instruction */ static void setoffset (CompileState *compst, int instruction, int offset) { getinstr(compst, instruction + 1).offset = offset; } static void codeutfr (CompileState *compst, TTree *tree) { int i = addoffsetinst(compst, IUTFR); int to = sib1(tree)->u.n; assert(sib1(tree)->tag == TXInfo); getinstr(compst, i + 1).offset = tree->u.n; getinstr(compst, i).i.aux1 = to & 0xff; getinstr(compst, i).i.aux2.key = to >> 8; } /* ** Add a capture instruction: ** 'op' is the capture instruction; 'cap' the capture kind; ** 'key' the key into ktable; 'aux' is the optional capture offset ** */ static int addinstcap (CompileState *compst, Opcode op, int cap, int key, int aux) { int i = addinstruction(compst, op, joinkindoff(cap, aux)); getinstr(compst, i).i.aux2.key = key; return i; } #define gethere(compst) ((compst)->ncode) #define target(code,i) ((i) + code[i + 1].offset) /* ** Patch 'instruction' to jump to 'target' */ static void jumptothere (CompileState *compst, int instruction, int target) { if (instruction >= 0) setoffset(compst, instruction, target - instruction); } /* ** Patch 'instruction' to jump to current position */ static void jumptohere (CompileState *compst, int instruction) { jumptothere(compst, instruction, gethere(compst)); } /* ** Code an IChar instruction, or IAny if there is an equivalent ** test dominating it */ static void codechar (CompileState *compst, int c, int tt) { if (tt >= 0 && getinstr(compst, tt).i.code == ITestChar && getinstr(compst, tt).i.aux1 == c) addinstruction(compst, IAny, 0); else addinstruction(compst, IChar, c); } /* ** Add a charset posfix to an instruction. */ static void addcharset (CompileState *compst, int inst, charsetinfo *info) { int p; Instruction *I = &getinstr(compst, inst); byte *charset; int isize = instsize(info->size); /* size in instructions */ int i; I->i.aux2.set.offset = info->offset * 8; /* offset in bits */ I->i.aux2.set.size = isize; I->i.aux1 = info->deflt; p = nextinstruction(compst, isize); /* space for charset */ charset = getinstr(compst, p).buff; /* charset buffer */ for (i = 0; i < isize * (int)sizeof(Instruction); i++) charset[i] = getbytefromcharset(info, i); /* copy the buffer */ } /* ** Check whether charset 'info' is dominated by instruction 'p' */ static int cs_equal (Instruction *p, charsetinfo *info) { if (p->i.code != ITestSet) return 0; else if (p->i.aux2.set.offset != info->offset * 8 || p->i.aux2.set.size != instsize(info->size) || p->i.aux1 != info->deflt) return 0; else { int i; for (i = 0; i < instsize(info->size) * (int)sizeof(Instruction); i++) { if ((p + 2)->buff[i] != getbytefromcharset(info, i)) return 0; } } return 1; } /* ** Code a char set, using IAny when instruction is dominated by an ** equivalent test. */ static void codecharset (CompileState *compst, TTree *tree, int tt) { charsetinfo info; tree2cset(tree, &info); if (tt >= 0 && cs_equal(&getinstr(compst, tt), &info)) addinstruction(compst, IAny, 0); else { int i = addinstruction(compst, ISet, 0); addcharset(compst, i, &info); } } /* ** Code a test set, optimizing unit sets for ITestChar, "complete" ** sets for ITestAny, and empty sets for IJmp (always fails). ** 'e' is true iff test should accept the empty string. (Test ** instructions in the current VM never accept the empty string.) */ static int codetestset (CompileState *compst, Charset *cs, int e) { if (e) return NOINST; /* no test */ else { charsetinfo info; Opcode op = charsettype(cs->cs, &info); switch (op) { case IFail: return addoffsetinst(compst, IJmp); /* always jump */ case IAny: return addoffsetinst(compst, ITestAny); case IChar: { int i = addoffsetinst(compst, ITestChar); getinstr(compst, i).i.aux1 = info.offset; return i; } default: { /* regular set */ int i = addoffsetinst(compst, ITestSet); addcharset(compst, i, &info); assert(op == ISet); return i; } } } } /* ** Find the final destination of a sequence of jumps */ static int finaltarget (Instruction *code, int i) { while (code[i].i.code == IJmp) i = target(code, i); return i; } /* ** final label (after traversing any jumps) */ static int finallabel (Instruction *code, int i) { return finaltarget(code, target(code, i)); } /* ** == behind n;

(where n = fixedlen(p)) */ static void codebehind (CompileState *compst, TTree *tree) { if (tree->u.n > 0) addinstruction(compst, IBehind, tree->u.n); codegen(compst, sib1(tree), 0, NOINST, fullset); } /* ** Choice; optimizations: ** - when p1 is headfail or when first(p1) and first(p2) are disjoint, ** than a character not in first(p1) cannot go to p1 and a character ** in first(p1) cannot go to p2, either because p1 will accept ** (headfail) or because it is not in first(p2) (disjoint). ** (The second case is not valid if p1 accepts the empty string, ** as then there is no character at all...) ** - when p2 is empty and opt is true; a IPartialCommit can reuse ** the Choice already active in the stack. */ static void codechoice (CompileState *compst, TTree *p1, TTree *p2, int opt, const Charset *fl) { int emptyp2 = (p2->tag == TTrue); Charset cs1, cs2; int e1 = getfirst(p1, fullset, &cs1); if (headfail(p1) || (!e1 && (getfirst(p2, fl, &cs2), cs_disjoint(&cs1, &cs2)))) { /* == test (fail(p1)) -> L1 ; p1 ; jmp L2; L1: p2; L2: */ int test = codetestset(compst, &cs1, 0); int jmp = NOINST; codegen(compst, p1, 0, test, fl); if (!emptyp2) jmp = addoffsetinst(compst, IJmp); jumptohere(compst, test); codegen(compst, p2, opt, NOINST, fl); jumptohere(compst, jmp); } else if (opt && emptyp2) { /* p1? == IPartialCommit; p1 */ jumptohere(compst, addoffsetinst(compst, IPartialCommit)); codegen(compst, p1, 1, NOINST, fullset); } else { /* == test(first(p1)) -> L1; choice L1; ; commit L2; L1: ; L2: */ int pcommit; int test = codetestset(compst, &cs1, e1); int pchoice = addoffsetinst(compst, IChoice); codegen(compst, p1, emptyp2, test, fullset); pcommit = addoffsetinst(compst, ICommit); jumptohere(compst, pchoice); jumptohere(compst, test); codegen(compst, p2, opt, NOINST, fl); jumptohere(compst, pcommit); } } /* ** And predicate ** optimization: fixedlen(p) = n ==> <&p> ==

; behind n ** (valid only when 'p' has no captures) */ static void codeand (CompileState *compst, TTree *tree, int tt) { int n = fixedlen(tree); if (n >= 0 && n <= MAXBEHIND && !hascaptures(tree)) { codegen(compst, tree, 0, tt, fullset); if (n > 0) addinstruction(compst, IBehind, n); } else { /* default: Choice L1; p1; BackCommit L2; L1: Fail; L2: */ int pcommit; int pchoice = addoffsetinst(compst, IChoice); codegen(compst, tree, 0, tt, fullset); pcommit = addoffsetinst(compst, IBackCommit); jumptohere(compst, pchoice); addinstruction(compst, IFail, 0); jumptohere(compst, pcommit); } } /* ** Captures: if pattern has fixed (and not too big) length, and it ** has no nested captures, use a single IFullCapture instruction ** after the match; otherwise, enclose the pattern with OpenCapture - ** CloseCapture. */ static void codecapture (CompileState *compst, TTree *tree, int tt, const Charset *fl) { int len = fixedlen(sib1(tree)); if (len >= 0 && len <= MAXOFF && !hascaptures(sib1(tree))) { codegen(compst, sib1(tree), 0, tt, fl); addinstcap(compst, IFullCapture, tree->cap, tree->key, len); } else { addinstcap(compst, IOpenCapture, tree->cap, tree->key, 0); codegen(compst, sib1(tree), 0, tt, fl); addinstcap(compst, ICloseCapture, Cclose, 0, 0); } } static void coderuntime (CompileState *compst, TTree *tree, int tt) { addinstcap(compst, IOpenCapture, Cgroup, tree->key, 0); codegen(compst, sib1(tree), 0, tt, fullset); addinstcap(compst, ICloseRunTime, Cclose, 0, 0); } /* ** Create a jump to 'test' and fix 'test' to jump to next instruction */ static void closeloop (CompileState *compst, int test) { int jmp = addoffsetinst(compst, IJmp); jumptohere(compst, test); jumptothere(compst, jmp, test); } /* ** Try repetition of charsets: ** For an empty set, repetition of fail is a no-op; ** For any or char, code a tight loop; ** For generic charset, use a span instruction. */ static int coderepcharset (CompileState *compst, TTree *tree) { switch (tree->tag) { case TFalse: return 1; /* 'fail*' is a no-op */ case TAny: { /* L1: testany -> L2; any; jmp L1; L2: */ int test = addoffsetinst(compst, ITestAny); addinstruction(compst, IAny, 0); closeloop(compst, test); return 1; } case TChar: { /* L1: testchar c -> L2; any; jmp L1; L2: */ int test = addoffsetinst(compst, ITestChar); getinstr(compst, test).i.aux1 = tree->u.n; addinstruction(compst, IAny, 0); closeloop(compst, test); return 1; } case TSet: { /* regular set */ charsetinfo info; int i = addinstruction(compst, ISpan, 0); tree2cset(tree, &info); addcharset(compst, i, &info); return 1; } default: return 0; /* not a charset */ } } /* ** Repetion; optimizations: ** When pattern is a charset, use special code. ** When pattern is head fail, or if it starts with characters that ** are disjoint from what follows the repetions, a simple test ** is enough (a fail inside the repetition would backtrack to fail ** again in the following pattern, so there is no need for a choice). ** When 'opt' is true, the repetion can reuse the Choice already ** active in the stack. */ static void coderep (CompileState *compst, TTree *tree, int opt, const Charset *fl) { if (!coderepcharset(compst, tree)) { Charset st; int e1 = getfirst(tree, fullset, &st); if (headfail(tree) || (!e1 && cs_disjoint(&st, fl))) { /* L1: test (fail(p1)) -> L2;

; jmp L1; L2: */ int test = codetestset(compst, &st, 0); codegen(compst, tree, 0, test, fullset); closeloop(compst, test); } else { /* test(fail(p1)) -> L2; choice L2; L1:

; partialcommit L1; L2: */ /* or (if 'opt'): partialcommit L1; L1:

; partialcommit L1; */ int commit, l2; int test = codetestset(compst, &st, e1); int pchoice = NOINST; if (opt) jumptohere(compst, addoffsetinst(compst, IPartialCommit)); else pchoice = addoffsetinst(compst, IChoice); l2 = gethere(compst); codegen(compst, tree, 0, NOINST, fullset); commit = addoffsetinst(compst, IPartialCommit); jumptothere(compst, commit, l2); jumptohere(compst, pchoice); jumptohere(compst, test); } } } /* ** Not predicate; optimizations: ** In any case, if first test fails, 'not' succeeds, so it can jump to ** the end. If pattern is headfail, that is all (it cannot fail ** in other parts); this case includes 'not' of simple sets. Otherwise, ** use the default code (a choice plus a failtwice). */ static void codenot (CompileState *compst, TTree *tree) { Charset st; int e = getfirst(tree, fullset, &st); int test = codetestset(compst, &st, e); if (headfail(tree)) /* test (fail(p1)) -> L1; fail; L1: */ addinstruction(compst, IFail, 0); else { /* test(fail(p))-> L1; choice L1;

; failtwice; L1: */ int pchoice = addoffsetinst(compst, IChoice); codegen(compst, tree, 0, NOINST, fullset); addinstruction(compst, IFailTwice, 0); jumptohere(compst, pchoice); } jumptohere(compst, test); } /* ** change open calls to calls, using list 'positions' to find ** correct offsets; also optimize tail calls */ static void correctcalls (CompileState *compst, int *positions, int from, int to) { int i; Instruction *code = compst->p->code; for (i = from; i < to; i += sizei(&code[i])) { if (code[i].i.code == IOpenCall) { int n = code[i].i.aux2.key; /* rule number */ int rule = positions[n]; /* rule position */ assert(rule == from || code[rule - 1].i.code == IRet); if (code[finaltarget(code, i + 2)].i.code == IRet) /* call; ret ? */ code[i].i.code = IJmp; /* tail call */ else code[i].i.code = ICall; jumptothere(compst, i, rule); /* call jumps to respective rule */ } } assert(i == to); } /* ** Code for a grammar: ** call L1; jmp L2; L1: rule 1; ret; rule 2; ret; ...; L2: */ static void codegrammar (CompileState *compst, TTree *grammar) { int positions[MAXRULES]; int rulenumber = 0; TTree *rule; int firstcall = addoffsetinst(compst, ICall); /* call initial rule */ int jumptoend = addoffsetinst(compst, IJmp); /* jump to the end */ int start = gethere(compst); /* here starts the initial rule */ jumptohere(compst, firstcall); for (rule = sib1(grammar); rule->tag == TRule; rule = sib2(rule)) { TTree *r = sib1(rule); assert(r->tag == TXInfo); positions[rulenumber++] = gethere(compst); /* save rule position */ codegen(compst, sib1(r), 0, NOINST, fullset); /* code rule */ addinstruction(compst, IRet, 0); } assert(rule->tag == TTrue); jumptohere(compst, jumptoend); correctcalls(compst, positions, start, gethere(compst)); } static void codecall (CompileState *compst, TTree *call) { int c = addoffsetinst(compst, IOpenCall); /* to be corrected later */ assert(sib1(sib2(call))->tag == TXInfo); getinstr(compst, c).i.aux2.key = sib1(sib2(call))->u.n; /* rule number */ } /* ** Code first child of a sequence ** (second child is called in-place to allow tail call) ** Return 'tt' for second child */ static int codeseq1 (CompileState *compst, TTree *p1, TTree *p2, int tt, const Charset *fl) { if (needfollow(p1)) { Charset fl1; getfirst(p2, fl, &fl1); /* p1 follow is p2 first */ codegen(compst, p1, 0, tt, &fl1); } else /* use 'fullset' as follow */ codegen(compst, p1, 0, tt, fullset); if (fixedlen(p1) != 0) /* can 'p1' consume anything? */ return NOINST; /* invalidate test */ else return tt; /* else 'tt' still protects sib2 */ } /* ** Main code-generation function: dispatch to auxiliar functions ** according to kind of tree. ('needfollow' should return true ** only for consructions that use 'fl'.) */ static void codegen (CompileState *compst, TTree *tree, int opt, int tt, const Charset *fl) { tailcall: switch (tree->tag) { case TChar: codechar(compst, tree->u.n, tt); break; case TAny: addinstruction(compst, IAny, 0); break; case TSet: codecharset(compst, tree, tt); break; case TTrue: break; case TFalse: addinstruction(compst, IFail, 0); break; case TUTFR: codeutfr(compst, tree); break; case TChoice: codechoice(compst, sib1(tree), sib2(tree), opt, fl); break; case TRep: coderep(compst, sib1(tree), opt, fl); break; case TBehind: codebehind(compst, tree); break; case TNot: codenot(compst, sib1(tree)); break; case TAnd: codeand(compst, sib1(tree), tt); break; case TCapture: codecapture(compst, tree, tt, fl); break; case TRunTime: coderuntime(compst, tree, tt); break; case TGrammar: codegrammar(compst, tree); break; case TCall: codecall(compst, tree); break; case TSeq: { tt = codeseq1(compst, sib1(tree), sib2(tree), tt, fl); /* code 'p1' */ /* codegen(compst, p2, opt, tt, fl); */ tree = sib2(tree); goto tailcall; } default: assert(0); } } /* ** Optimize jumps and other jump-like instructions. ** * Update labels of instructions with labels to their final ** destinations (e.g., choice L1; ... L1: jmp L2: becomes ** choice L2) ** * Jumps to other instructions that do jumps become those ** instructions (e.g., jump to return becomes a return; jump ** to commit becomes a commit) */ static void peephole (CompileState *compst) { Instruction *code = compst->p->code; int i; for (i = 0; i < compst->ncode; i += sizei(&code[i])) { redo: switch (code[i].i.code) { case IChoice: case ICall: case ICommit: case IPartialCommit: case IBackCommit: case ITestChar: case ITestSet: case ITestAny: { /* instructions with labels */ jumptothere(compst, i, finallabel(code, i)); /* optimize label */ break; } case IJmp: { int ft = finaltarget(code, i); switch (code[ft].i.code) { /* jumping to what? */ case IRet: case IFail: case IFailTwice: case IEnd: { /* instructions with unconditional implicit jumps */ code[i] = code[ft]; /* jump becomes that instruction */ code[i + 1].i.code = IEmpty; /* 'no-op' for target position */ break; } case ICommit: case IPartialCommit: case IBackCommit: { /* inst. with unconditional explicit jumps */ int fft = finallabel(code, ft); code[i] = code[ft]; /* jump becomes that instruction... */ jumptothere(compst, i, fft); /* but must correct its offset */ goto redo; /* reoptimize its label */ } default: { jumptothere(compst, i, ft); /* optimize label */ break; } } break; } default: break; } } assert(code[i - 1].i.code == IEnd); } /* ** Compile a pattern. 'size' is the size of the pattern's tree, ** which gives a hint for the size of the final code. */ Instruction *compile (lua_State *L, Pattern *p, uint size) { CompileState compst; compst.p = p; compst.ncode = 0; compst.L = L; newcode(L, p, size/2u + 2); /* set initial size */ codegen(&compst, p->tree, 0, NOINST, fullset); addinstruction(&compst, IEnd, 0); realloccode(L, p, compst.ncode); /* set final size */ peephole(&compst); return p->code; } /* }====================================================== */ ================================================ FILE: 3rd/lpeg/lpcode.h ================================================ #if !defined(lpcode_h) #define lpcode_h #include "lua.h" #include "lptypes.h" #include "lptree.h" #include "lpvm.h" int checkaux (TTree *tree, int pred); int fixedlen (TTree *tree); int hascaptures (TTree *tree); int lp_gc (lua_State *L); Instruction *compile (lua_State *L, Pattern *p, uint size); void freecode (lua_State *L, Pattern *p); int sizei (const Instruction *i); #define PEnullable 0 #define PEnofail 1 /* ** nofail(t) implies that 't' cannot fail with any input */ #define nofail(t) checkaux(t, PEnofail) /* ** (not nullable(t)) implies 't' cannot match without consuming ** something */ #define nullable(t) checkaux(t, PEnullable) #endif ================================================ FILE: 3rd/lpeg/lpcset.c ================================================ #include "lptypes.h" #include "lpcset.h" /* ** Add to 'c' the index of the (only) bit set in byte 'b' */ static int onlybit (int c, int b) { if ((b & 0xF0) != 0) { c += 4; b >>= 4; } if ((b & 0x0C) != 0) { c += 2; b >>= 2; } if ((b & 0x02) != 0) { c += 1; } return c; } /* ** Check whether a charset is empty (returns IFail), singleton (IChar), ** full (IAny), or none of those (ISet). When singleton, 'info.offset' ** returns which character it is. When generic set, 'info' returns ** information about its range. */ Opcode charsettype (const byte *cs, charsetinfo *info) { int low0, low1, high0, high1; for (low1 = 0; low1 < CHARSETSIZE && cs[low1] == 0; low1++) /* find lowest byte with a 1-bit */; if (low1 == CHARSETSIZE) return IFail; /* no characters in set */ for (high1 = CHARSETSIZE - 1; cs[high1] == 0; high1--) /* find highest byte with a 1-bit; low1 is a sentinel */; if (low1 == high1) { /* only one byte with 1-bits? */ int b = cs[low1]; if ((b & (b - 1)) == 0) { /* does byte has only one 1-bit? */ info->offset = onlybit(low1 * BITSPERCHAR, b); /* get that bit */ return IChar; /* single character */ } } for (low0 = 0; low0 < CHARSETSIZE && cs[low0] == 0xFF; low0++) /* find lowest byte with a 0-bit */; if (low0 == CHARSETSIZE) return IAny; /* set has all bits set */ for (high0 = CHARSETSIZE - 1; cs[high0] == 0xFF; high0--) /* find highest byte with a 0-bit; low0 is a sentinel */; if (high1 - low1 <= high0 - low0) { /* range of 1s smaller than of 0s? */ info->offset = low1; info->size = high1 - low1 + 1; info->deflt = 0; /* all discharged bits were 0 */ } else { info->offset = low0; info->size = high0 - low0 + 1; info->deflt = 0xFF; /* all discharged bits were 1 */ } info->cs = cs + info->offset; return ISet; } /* ** Get a byte from a compact charset. If index is inside the charset ** range, get the byte from the supporting charset (correcting it ** by the offset). Otherwise, return the default for the set. */ byte getbytefromcharset (const charsetinfo *info, int index) { if (index < info->size) return info->cs[index]; else return info->deflt; } /* ** If 'tree' is a 'char' pattern (TSet, TChar, TAny, TFalse), convert it ** into a charset and return 1; else return 0. */ int tocharset (TTree *tree, Charset *cs) { switch (tree->tag) { case TChar: { /* only one char */ assert(0 <= tree->u.n && tree->u.n <= UCHAR_MAX); clearset(cs->cs); /* erase all chars */ setchar(cs->cs, tree->u.n); /* add that one */ return 1; } case TAny: { fillset(cs->cs, 0xFF); /* add all characters to the set */ return 1; } case TFalse: { clearset(cs->cs); /* empty set */ return 1; } case TSet: { /* fill set */ int i; fillset(cs->cs, tree->u.set.deflt); for (i = 0; i < tree->u.set.size; i++) cs->cs[tree->u.set.offset + i] = treebuffer(tree)[i]; return 1; } default: return 0; } } void tree2cset (TTree *tree, charsetinfo *info) { assert(tree->tag == TSet); info->offset = tree->u.set.offset; info->size = tree->u.set.size; info->deflt = tree->u.set.deflt; info->cs = treebuffer(tree); } ================================================ FILE: 3rd/lpeg/lpcset.h ================================================ #if !defined(lpset_h) #define lpset_h #include "lpcset.h" #include "lpcode.h" #include "lptree.h" /* ** Extra information for the result of 'charsettype'. When result is ** IChar, 'offset' is the character. When result is ISet, 'cs' is the ** supporting bit array (with offset included), 'offset' is the offset ** (in bytes), 'size' is the size (in bytes), and 'delt' is the default ** value for bytes outside the set. */ typedef struct { const byte *cs; int offset; int size; int deflt; } charsetinfo; int tocharset (TTree *tree, Charset *cs); Opcode charsettype (const byte *cs, charsetinfo *info); byte getbytefromcharset (const charsetinfo *info, int index); void tree2cset (TTree *tree, charsetinfo *info); #endif ================================================ FILE: 3rd/lpeg/lpeg.html ================================================ LPeg - Parsing Expression Grammars For Lua

LPeg
Parsing Expression Grammars For Lua, version 1.1

Introduction

LPeg is a new pattern-matching library for Lua, based on Parsing Expression Grammars (PEGs). This text is a reference manual for the library. For a more formal treatment of LPeg, as well as some discussion about its implementation, see A Text Pattern-Matching Tool based on Parsing Expression Grammars. (You may also be interested in my talk about LPeg given at the III Lua Workshop.)

Following the Snobol tradition, LPeg defines patterns as first-class objects. That is, patterns are regular Lua values (represented by userdata). The library offers several functions to create and compose patterns. With the use of metamethods, several of these functions are provided as infix or prefix operators. On the one hand, the result is usually much more verbose than the typical encoding of patterns using the so called regular expressions (which typically are not regular expressions in the formal sense). On the other hand, first-class patterns allow much better documentation (as it is easy to comment the code, to break complex definitions in smaller parts, etc.) and are extensible, as we can define new functions to create and compose patterns.

For a quick glance of the library, the following table summarizes its basic operations for creating patterns:

OperatorDescription
lpeg.P(string) Matches string literally
lpeg.P(n) Matches exactly n characters
lpeg.S(string) Matches any character in string (Set)
lpeg.R("xy") Matches any character between x and y (Range)
lpeg.utfR(cp1, cp2) Matches an UTF-8 code point between cp1 and cp2
patt^n Matches at least n repetitions of patt
patt^-n Matches at most n repetitions of patt
patt1 * patt2 Matches patt1 followed by patt2
patt1 + patt2 Matches patt1 or patt2 (ordered choice)
patt1 - patt2 Matches patt1 if patt2 does not match
-patt Equivalent to ("" - patt)
#patt Matches patt but consumes no input
lpeg.B(patt) Matches patt behind the current position, consuming no input

As a very simple example, lpeg.R("09")^1 creates a pattern that matches a non-empty sequence of digits. As a not so simple example, -lpeg.P(1) (which can be written as lpeg.P(-1), or simply -1 for operations expecting a pattern) matches an empty string only if it cannot match a single character; so, it succeeds only at the end of the subject.

LPeg also offers the re module, which implements patterns following a regular-expression style (e.g., [09]+). (This module is 270 lines of Lua code, and of course it uses LPeg to parse regular expressions and translate them to regular LPeg patterns.)

Functions

lpeg.match (pattern, subject [, init])

The matching function. It attempts to match the given pattern against the subject string. If the match succeeds, returns the index in the subject of the first character after the match, or the captured values (if the pattern captured any value).

An optional numeric argument init makes the match start at that position in the subject string. As in the Lua standard libraries, a negative value counts from the end.

Unlike typical pattern-matching functions, match works only in anchored mode; that is, it tries to match the pattern with a prefix of the given subject string (at position init), not with an arbitrary substring of the subject. So, if we want to find a pattern anywhere in a string, we must either write a loop in Lua or write a pattern that matches anywhere. This second approach is easy and quite efficient; see examples.

lpeg.type (value)

If the given value is a pattern, returns the string "pattern". Otherwise returns nil.

lpeg.version

A string (not a function) with the running version of LPeg.

lpeg.setmaxstack (max)

Sets a limit for the size of the backtrack stack used by LPeg to track calls and choices. (The default limit is 400.) Most well-written patterns need little backtrack levels and therefore you seldom need to change this limit; before changing it you should try to rewrite your pattern to avoid the need for extra space. Nevertheless, a few useful patterns may overflow. Also, with recursive grammars, subjects with deep recursion may also need larger limits.

Basic Constructions

The following operations build patterns. All operations that expect a pattern as an argument may receive also strings, tables, numbers, booleans, or functions, which are translated to patterns according to the rules of function lpeg.P.

lpeg.P (value)

Converts the given value into a proper pattern, according to the following rules:

  • If the argument is a pattern, it is returned unmodified.

  • If the argument is a string, it is translated to a pattern that matches the string literally.

  • If the argument is a non-negative number n, the result is a pattern that matches exactly n characters.

  • If the argument is a negative number -n, the result is a pattern that succeeds only if the input string has less than n characters left: lpeg.P(-n) is equivalent to -lpeg.P(n) (see the unary minus operation).

  • If the argument is a boolean, the result is a pattern that always succeeds or always fails (according to the boolean value), without consuming any input.

  • If the argument is a table, it is interpreted as a grammar (see Grammars).

  • If the argument is a function, returns a pattern equivalent to a match-time capture over the empty string.

lpeg.B(patt)

Returns a pattern that matches only if the input string at the current position is preceded by patt. Pattern patt must match only strings with some fixed length, and it cannot contain captures.

Like the and predicate, this pattern never consumes any input, independently of success or failure.

lpeg.R ({range})

Returns a pattern that matches any single character belonging to one of the given ranges. Each range is a string xy of length 2, representing all characters with code between the codes of x and y (both inclusive).

As an example, the pattern lpeg.R("09") matches any digit, and lpeg.R("az", "AZ") matches any ASCII letter.

lpeg.S (string)

Returns a pattern that matches any single character that appears in the given string. (The S stands for Set.)

As an example, the pattern lpeg.S("+-*/") matches any arithmetic operator.

Note that, if s is a character (that is, a string of length 1), then lpeg.P(s) is equivalent to lpeg.S(s) which is equivalent to lpeg.R(s..s). Note also that both lpeg.S("") and lpeg.R() are patterns that always fail.

lpeg.utfR (cp1, cp2)

Returns a pattern that matches a valid UTF-8 byte sequence representing a code point in the range [cp1, cp2]. The range is limited by the natural Unicode limit of 0x10FFFF, but may include surrogates.

lpeg.V (v)

This operation creates a non-terminal (a variable) for a grammar. The created non-terminal refers to the rule indexed by v in the enclosing grammar. (See Grammars for details.)

lpeg.locale ([table])

Returns a table with patterns for matching some character classes according to the current locale. The table has fields named alnum, alpha, cntrl, digit, graph, lower, print, punct, space, upper, and xdigit, each one containing a correspondent pattern. Each pattern matches any single character that belongs to its class.

If called with an argument table, then it creates those fields inside the given table and returns that table.

#patt

Returns a pattern that matches only if the input string matches patt, but without consuming any input, independently of success or failure. (This pattern is called an and predicate and it is equivalent to &patt in the original PEG notation.)

This pattern never produces any capture.

-patt

Returns a pattern that matches only if the input string does not match patt. It does not consume any input, independently of success or failure. (This pattern is equivalent to !patt in the original PEG notation.)

As an example, the pattern -lpeg.P(1) matches only the end of string.

This pattern never produces any captures, because either patt fails or -patt fails. (A failing pattern never produces captures.)

patt1 + patt2

Returns a pattern equivalent to an ordered choice of patt1 and patt2. (This is denoted by patt1 / patt2 in the original PEG notation, not to be confused with the / operation in LPeg.) It matches either patt1 or patt2, with no backtracking once one of them succeeds. The identity element for this operation is the pattern lpeg.P(false), which always fails.

If both patt1 and patt2 are character sets, this operation is equivalent to set union.

lower = lpeg.R("az")
upper = lpeg.R("AZ")
letter = lower + upper

patt1 - patt2

Returns a pattern equivalent to !patt2 patt1 in the origial PEG notation. This pattern asserts that the input does not match patt2 and then matches patt1.

When successful, this pattern produces all captures from patt1. It never produces any capture from patt2 (as either patt2 fails or patt1 - patt2 fails).

If both patt1 and patt2 are character sets, this operation is equivalent to set difference. Note that -patt is equivalent to "" - patt (or 0 - patt). If patt is a character set, 1 - patt is its complement.

patt1 * patt2

Returns a pattern that matches patt1 and then matches patt2, starting where patt1 finished. The identity element for this operation is the pattern lpeg.P(true), which always succeeds.

(LPeg uses the * operator [instead of the more obvious ..] both because it has the right priority and because in formal languages it is common to use a dot for denoting concatenation.)

patt^n

If n is nonnegative, this pattern is equivalent to pattn patt*: It matches n or more occurrences of patt.

Otherwise, when n is negative, this pattern is equivalent to (patt?)-n: It matches at most |n| occurrences of patt.

In particular, patt^0 is equivalent to patt*, patt^1 is equivalent to patt+, and patt^-1 is equivalent to patt? in the original PEG notation.

In all cases, the resulting pattern is greedy with no backtracking (also called a possessive repetition). That is, it matches only the longest possible sequence of matches for patt.

Grammars

With the use of Lua variables, it is possible to define patterns incrementally, with each new pattern using previously defined ones. However, this technique does not allow the definition of recursive patterns. For recursive patterns, we need real grammars.

LPeg represents grammars with tables, where each entry is a rule.

The call lpeg.V(v) creates a pattern that represents the nonterminal (or variable) with index v in a grammar. Because the grammar still does not exist when this function is evaluated, the result is an open reference to the respective rule.

A table is fixed when it is converted to a pattern (either by calling lpeg.P or by using it wherein a pattern is expected). Then every open reference created by lpeg.V(v) is corrected to refer to the rule indexed by v in the table.

When a table is fixed, the result is a pattern that matches its initial rule. The entry with index 1 in the table defines its initial rule. If that entry is a string, it is assumed to be the name of the initial rule. Otherwise, LPeg assumes that the entry 1 itself is the initial rule.

As an example, the following grammar matches strings of a's and b's that have the same number of a's and b's:

equalcount = lpeg.P{
  "S";   -- initial rule name
  S = "a" * lpeg.V"B" + "b" * lpeg.V"A" + "",
  A = "a" * lpeg.V"S" + "b" * lpeg.V"A" * lpeg.V"A",
  B = "b" * lpeg.V"S" + "a" * lpeg.V"B" * lpeg.V"B",
} * -1

It is equivalent to the following grammar in standard PEG notation:

  S <- 'a' B / 'b' A / ''
  A <- 'a' S / 'b' A A
  B <- 'b' S / 'a' B B

Captures

A capture is a pattern that produces values (the so called semantic information) according to what it matches. LPeg offers several kinds of captures, which produces values based on matches and combine these values to produce new values. Each capture may produce zero or more values.

The following table summarizes the basic captures:

OperationWhat it Produces
lpeg.C(patt) the match for patt plus all captures made by patt
lpeg.Carg(n) the value of the nth extra argument to lpeg.match (matches the empty string)
lpeg.Cb(key) the values produced by the previous group capture named key (matches the empty string)
lpeg.Cc(values) the given values (matches the empty string)
lpeg.Cf(patt, func) folding capture (deprecated)
lpeg.Cg(patt [, key]) the values produced by patt, optionally tagged with key
lpeg.Cp() the current position (matches the empty string)
lpeg.Cs(patt) the match for patt with the values from nested captures replacing their matches
lpeg.Ct(patt) a table with all captures from patt
patt / string string, with some marks replaced by captures of patt
patt / number the n-th value captured by patt, or no value when number is zero.
patt / table table[c], where c is the (first) capture of patt
patt / function the returns of function applied to the captures of patt
patt % function produces no value; it accummulates the captures from patt into the previous capture through function
lpeg.Cmt(patt, function) the returns of function applied to the captures of patt; the application is done at match time

A capture pattern produces its values only when it succeeds. For instance, the pattern lpeg.C(lpeg.P"a"^-1) produces the empty string when there is no "a" (because the pattern "a"? succeeds), while the pattern lpeg.C("a")^-1 does not produce any value when there is no "a" (because the pattern "a" fails). A pattern inside a loop or inside a recursive structure produces values for each match.

Usually, LPeg does not specify when (and if) it evaluates its captures. (As an example, consider the pattern lpeg.P"a" / func / 0. Because the "division" by 0 instructs LPeg to throw away the results from the pattern, it is not specified whether LPeg will call func.) Therefore, captures should avoid side effects. Moreover, captures cannot affect the way a pattern matches a subject. The only exception to this rule is the so-called match-time capture. When a match-time capture matches, it forces the immediate evaluation of all its nested captures and then calls its corresponding function, which defines whether the match succeeds and also what values are produced.

lpeg.C (patt)

Creates a simple capture, which captures the substring of the subject that matches patt. The captured value is a string. If patt has other captures, their values are returned after this one.

lpeg.Carg (n)

Creates an argument capture. This pattern matches the empty string and produces the value given as the nth extra argument given in the call to lpeg.match.

lpeg.Cb (key)

Creates a back capture. This pattern matches the empty string and produces the values produced by the most recent group capture named key (where key can be any Lua value).

Most recent means the last complete outermost group capture with the given key. A Complete capture means that the entire pattern corresponding to the capture has matched; in other words, the back capture is not nested inside the group. An Outermost capture means that the capture is not inside another complete capture that does not contain the back capture itself.

In the same way that LPeg does not specify when it evaluates captures, it does not specify whether it reuses values previously produced by the group or re-evaluates them.

lpeg.Cc ([value, ...])

Creates a constant capture. This pattern matches the empty string and produces all given values as its captured values.

lpeg.Cf (patt, func)

Creates a fold capture. This construction is deprecated; use an accumulator pattern instead. In general, a fold like lpeg.Cf(p1 * p2^0, func) can be translated to (p1 * (p2 % func)^0).

lpeg.Cg (patt [, key])

Creates a group capture. It groups all values returned by patt into a single capture. The group may be anonymous (if no key is given) or named with the given key (which can be any non-nil Lua value).

An anonymous group serves to join values from several captures into a single capture. A named group has a different behavior. In most situations, a named group returns no values at all. Its values are only relevant for a following back capture or when used inside a table capture.

lpeg.Cp ()

Creates a position capture. It matches the empty string and captures the position in the subject where the match occurs. The captured value is a number.

lpeg.Cs (patt)

Creates a substitution capture, which captures the substring of the subject that matches patt, with substitutions. For any capture inside patt with a value, the substring that matched the capture is replaced by the capture value (which should be a string). The final captured value is the string resulting from all replacements.

lpeg.Ct (patt)

Creates a table capture. This capture returns a table with all values from all anonymous captures made by patt inside this table in successive integer keys, starting at 1. Moreover, for each named capture group created by patt, the first value of the group is put into the table with the group key as its key. The captured value is only the table.

patt / string

Creates a string capture. It creates a capture string based on string. The captured value is a copy of string, except that the character % works as an escape character: any sequence in string of the form %n, with n between 1 and 9, stands for the match of the n-th capture in patt. The sequence %0 stands for the whole match. The sequence %% stands for a single %.

patt / number

Creates a numbered capture. For a non-zero number, the captured value is the n-th value captured by patt. When number is zero, there are no captured values.

patt / table

Creates a query capture. It indexes the given table using as key the first value captured by patt, or the whole match if patt produced no value. The value at that index is the final value of the capture. If the table does not have that key, there is no captured value.

patt / function

Creates a function capture. It calls the given function passing all captures made by patt as arguments, or the whole match if patt made no capture. The values returned by the function are the final values of the capture. In particular, if function returns no value, there is no captured value.

patt % function

Creates an accumulator capture. This pattern behaves similarly to a function capture, with the following differences: The last captured value before patt is added as a first argument to the call; the return of the function is adjusted to one single value; that value replaces the last captured value. Note that the capture itself produces no values; it only changes the value of its previous capture.

As an example, let us consider the problem of adding a list of numbers.

-- matches a numeral and captures its numerical value
number = lpeg.R"09"^1 / tonumber

-- auxiliary function to add two numbers
function add (acc, newvalue) return acc + newvalue end

-- matches a list of numbers, adding their values
sum = number * ("," * number % add)^0

-- example of use
print(sum:match("10,30,43"))   --> 83

First, the initial number captures a number; that first capture will play the role of an accumulator. Then, each time the sequence comma-number matches inside the loop there is an accumulator capture: It calls add with the current value of the accumulator—which is the last captured value, created by the first number— and the value of the new number, and the result of the call (the sum of the two numbers) replaces the value of the accumulator. At the end of the match, the accumulator with all sums is the final value.

As another example, consider the following code fragment:

local name = lpeg.C(lpeg.R("az")^1)
local p = name * (lpeg.P("^") % string.upper)^-1
print(p:match("count"))    --> count
print(p:match("count^"))   --> COUNT

In the match against "count", as there is no "^", the optional accumulator capture does not match; so, the match results in its sole capture, a name. In the match against "count^", the accumulator capture matches, so the function string.upper is called with the previous captured value (created by name) plus the string "^"; the function ignores its second argument and returns the first argument changed to upper case; that value then becomes the first and only capture value created by the match.

Due to the nature of this capture, you should avoid using it in places where it is not clear what is the "previous" capture, such as directly nested in a string capture or a numbered capture. (Note that these captures may not need to evaluate all their subcaptures to compute their results.) Moreover, due to implementation details, you should not use this capture directly nested in a substitution capture. You should also avoid a direct nesting of this capture inside a folding capture (deprecated), as the folding will try to fold each individual accumulator capture. A simple and effective way to avoid all these issues is to enclose the whole accumulation composition (including the capture that generates the initial value) into an anonymous group capture.

lpeg.Cmt(patt, function)

Creates a match-time capture. Unlike all other captures, this one is evaluated immediately when a match occurs (even if it is part of a larger pattern that fails later). It forces the immediate evaluation of all its nested captures and then calls function.

The given function gets as arguments the entire subject, the current position (after the match of patt), plus any capture values produced by patt.

The first value returned by function defines how the match happens. If the call returns a number, the match succeeds and the returned number becomes the new current position. (Assuming a subject s and current position i, the returned number must be in the range [i, len(s) + 1].) If the call returns true, the match succeeds without consuming any input. (So, to return true is equivalent to return i.) If the call returns false, nil, or no value, the match fails.

Any extra values returned by the function become the values produced by the capture.

Some Examples

Using a Pattern

This example shows a very simple but complete program that builds and uses a pattern:

local lpeg = require "lpeg"

-- matches a word followed by end-of-string
p = lpeg.R"az"^1 * -1

print(p:match("hello"))        --> 6
print(lpeg.match(p, "hello"))  --> 6
print(p:match("1 hello"))      --> nil

The pattern is simply a sequence of one or more lower-case letters followed by the end of string (-1). The program calls match both as a method and as a function. In both sucessful cases, the match returns the index of the first character after the match, which is the string length plus one.

Name-value lists

This example parses a list of name-value pairs and returns a table with those pairs:

lpeg.locale(lpeg)   -- adds locale entries into 'lpeg' table

local space = lpeg.space^0
local name = lpeg.C(lpeg.alpha^1) * space
local sep = lpeg.S(",;") * space
local pair = name * "=" * space * name * sep^-1
local list = lpeg.Ct("") * (pair % rawset)^0
t = list:match("a=b, c = hi; next = pi")
        --> { a = "b", c = "hi", next = "pi" }

Each pair has the format name = name followed by an optional separator (a comma or a semicolon). The list pattern then folds these captures. It starts with an empty table, created by a table capture matching an empty string; then for each a pair of names it applies rawset over the accumulator (the table) and the capture values (the pair of names). rawset returns the table itself, so the accumulator is always the table.

Splitting a string

The following code builds a pattern that splits a string using a given pattern sep as a separator:

function split (s, sep)
  sep = lpeg.P(sep)
  local elem = lpeg.C((1 - sep)^0)
  local p = elem * (sep * elem)^0
  return lpeg.match(p, s)
end

First the function ensures that sep is a proper pattern. The pattern elem is a repetition of zero of more arbitrary characters as long as there is not a match against the separator. It also captures its match. The pattern p matches a list of elements separated by sep.

If the split results in too many values, it may overflow the maximum number of values that can be returned by a Lua function. To avoid this problem, we can collect these values in a table:

function split (s, sep)
  sep = lpeg.P(sep)
  local elem = lpeg.C((1 - sep)^0)
  local p = lpeg.Ct(elem * (sep * elem)^0)   -- make a table capture
  return lpeg.match(p, s)
end

Searching for a pattern

The primitive match works only in anchored mode. If we want to find a pattern anywhere in a string, we must write a pattern that matches anywhere.

Because patterns are composable, we can write a function that, given any arbitrary pattern p, returns a new pattern that searches for p anywhere in a string. There are several ways to do the search. One way is like this:

function anywhere (p)
  return lpeg.P{ p + 1 * lpeg.V(1) }
end

This grammar has a straight reading: its sole rule matches p or skips one character and tries again.

If we want to know where the pattern is in the string (instead of knowing only that it is there somewhere), we can add position captures to the pattern:

local Cp = lpeg.Cp()
function anywhere (p)
  return lpeg.P{ Cp * p * Cp + 1 * lpeg.V(1) }
end

print(anywhere("world"):match("hello world!"))   --> 7   12

Another option for the search is like this:

local Cp = lpeg.Cp()
function anywhere (p)
  return (1 - lpeg.P(p))^0 * Cp * p * Cp
end

Again the pattern has a straight reading: it skips as many characters as possible while not matching p, and then matches p plus appropriate captures.

If we want to look for a pattern only at word boundaries, we can use the following transformer:

local t = lpeg.locale()

function atwordboundary (p)
  return lpeg.P{
    [1] = p + t.alpha^0 * (1 - t.alpha)^1 * lpeg.V(1)
  }
end

Balanced parentheses

The following pattern matches only strings with balanced parentheses:

b = lpeg.P{ "(" * ((1 - lpeg.S"()") + lpeg.V(1))^0 * ")" }

Reading the first (and only) rule of the given grammar, we have that a balanced string is an open parenthesis, followed by zero or more repetitions of either a non-parenthesis character or a balanced string (lpeg.V(1)), followed by a closing parenthesis.

Global substitution

The next example does a job somewhat similar to string.gsub. It receives a pattern and a replacement value, and substitutes the replacement value for all occurrences of the pattern in a given string:

function gsub (s, patt, repl)
  patt = lpeg.P(patt)
  patt = lpeg.Cs((patt / repl + 1)^0)
  return lpeg.match(patt, s)
end

As in string.gsub, the replacement value can be a string, a function, or a table.

Comma-Separated Values (CSV)

This example breaks a string into comma-separated values, returning all fields:

local field = '"' * lpeg.Cs(((lpeg.P(1) - '"') + lpeg.P'""' / '"')^0) * '"' +
                    lpeg.C((1 - lpeg.S',\n"')^0)

local record = field * (',' * field)^0 * (lpeg.P'\n' + -1)

function csv (s)
  return lpeg.match(record, s)
end

A field is either a quoted field (which may contain any character except an individual quote, which may be written as two quotes that are replaced by one) or an unquoted field (which cannot contain commas, newlines, or quotes). A record is a list of fields separated by commas, ending with a newline or the string end (-1).

As it is, the previous pattern returns each field as a separated result. If we add a table capture in the definition of record, the pattern will return instead a single table containing all fields:

local record = lpeg.Ct(field * (',' * field)^0) * (lpeg.P'\n' + -1)

Lua's long strings

A long string in Lua starts with the pattern [=*[ and ends at the first occurrence of ]=*] with exactly the same number of equal signs. If the opening brackets are followed by a newline, this newline is discarded (that is, it is not part of the string).

To match a long string in Lua, the pattern must capture the first repetition of equal signs and then, whenever it finds a candidate for closing the string, check whether it has the same number of equal signs.

equals = lpeg.P"="^0
open = "[" * lpeg.Cg(equals, "init") * "[" * lpeg.P"\n"^-1
close = "]" * lpeg.C(equals) * "]"
closeeq = lpeg.Cmt(close * lpeg.Cb("init"), function (s, i, a, b) return a == b end)
string = open * lpeg.C((lpeg.P(1) - closeeq)^0) * close / 1

The open pattern matches [=*[, capturing the repetitions of equal signs in a group named init; it also discharges an optional newline, if present. The close pattern matches ]=*], also capturing the repetitions of equal signs. The closeeq pattern first matches close; then it uses a back capture to recover the capture made by the previous open, which is named init; finally it uses a match-time capture to check whether both captures are equal. The string pattern starts with an open, then it goes as far as possible until matching closeeq, and then matches the final close. The final numbered capture simply discards the capture made by close.

Arithmetic expressions

This example is a complete parser and evaluator for simple arithmetic expressions. We write it in two styles. The first approach first builds a syntax tree and then traverses this tree to compute the expression value:

-- Lexical Elements
local Space = lpeg.S(" \n\t")^0
local Number = lpeg.C(lpeg.P"-"^-1 * lpeg.R("09")^1) * Space
local TermOp = lpeg.C(lpeg.S("+-")) * Space
local FactorOp = lpeg.C(lpeg.S("*/")) * Space
local Open = "(" * Space
local Close = ")" * Space

-- Grammar
local Exp, Term, Factor = lpeg.V"Exp", lpeg.V"Term", lpeg.V"Factor"
G = lpeg.P{ Exp,
  Exp = lpeg.Ct(Term * (TermOp * Term)^0);
  Term = lpeg.Ct(Factor * (FactorOp * Factor)^0);
  Factor = Number + Open * Exp * Close;
}

G = Space * G * -1

-- Evaluator
function eval (x)
  if type(x) == "string" then
    return tonumber(x)
  else
    local op1 = eval(x[1])
    for i = 2, #x, 2 do
      local op = x[i]
      local op2 = eval(x[i + 1])
      if (op == "+") then op1 = op1 + op2
      elseif (op == "-") then op1 = op1 - op2
      elseif (op == "*") then op1 = op1 * op2
      elseif (op == "/") then op1 = op1 / op2
      end
    end
    return op1
  end
end

-- Parser/Evaluator
function evalExp (s)
  local t = lpeg.match(G, s)
  if not t then error("syntax error", 2) end
  return eval(t)
end

-- small example
print(evalExp"3 + 5*9 / (1+1) - 12")   --> 13.5

The second style computes the expression value on the fly, without building the syntax tree. The following grammar takes this approach. (It assumes the same lexical elements as before.)

-- Auxiliary function
function eval (v1, op, v2)
  if (op == "+") then return v1 + v2
  elseif (op == "-") then return v1 - v2
  elseif (op == "*") then return v1 * v2
  elseif (op == "/") then return v1 / v2
  end
end

-- Grammar
local V = lpeg.V
G = lpeg.P{ "Exp",
  Exp = V"Term" * (TermOp * V"Term" % eval)^0;
  Term = V"Factor" * (FactorOp * V"Factor" % eval)^0;
  Factor = Number / tonumber + Open * V"Exp" * Close;
}

-- small example
print(lpeg.match(G, "3 + 5*9 / (1+1) - 12"))   --> 13.5

Note the use of the accumulator capture. To compute the value of an expression, the accumulator starts with the value of the first term, and then applies eval over the accumulator, the operator, and the new term for each repetition.

Download

LPeg source code.

Probably, the easiest way to install LPeg is with LuaRocks. If you have LuaRocks installed, the following command is all you need to install LPeg:

$ luarocks install lpeg

License

Copyright © 2007-2023 Lua.org, PUC-Rio.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

================================================ FILE: 3rd/lpeg/lpprint.c ================================================ #include #include #include #include "lptypes.h" #include "lpprint.h" #include "lpcode.h" #if defined(LPEG_DEBUG) /* ** {====================================================== ** Printing patterns (for debugging) ** ======================================================= */ void printcharset (const byte *st) { int i; printf("["); for (i = 0; i <= UCHAR_MAX; i++) { int first = i; while (i <= UCHAR_MAX && testchar(st, i)) i++; if (i - 1 == first) /* unary range? */ printf("(%02x)", first); else if (i - 1 > first) /* non-empty range? */ printf("(%02x-%02x)", first, i - 1); } printf("]"); } static void printIcharset (const Instruction *inst, const byte *buff) { byte cs[CHARSETSIZE]; int i; printf("(%02x-%d) ", inst->i.aux2.set.offset, inst->i.aux2.set.size); clearset(cs); for (i = 0; i < CHARSETSIZE * 8; i++) { if (charinset(inst, buff, i)) setchar(cs, i); } printcharset(cs); } static void printTcharset (TTree *tree) { byte cs[CHARSETSIZE]; int i; printf("(%02x-%d) ", tree->u.set.offset, tree->u.set.size); fillset(cs, tree->u.set.deflt); for (i = 0; i < tree->u.set.size; i++) cs[tree->u.set.offset + i] = treebuffer(tree)[i]; printcharset(cs); } static const char *capkind (int kind) { const char *const modes[] = { "close", "position", "constant", "backref", "argument", "simple", "table", "function", "accumulator", "query", "string", "num", "substitution", "fold", "runtime", "group"}; return modes[kind]; } static void printjmp (const Instruction *op, const Instruction *p) { printf("-> %d", (int)(p + (p + 1)->offset - op)); } void printinst (const Instruction *op, const Instruction *p) { const char *const names[] = { "any", "char", "set", "testany", "testchar", "testset", "span", "utf-range", "behind", "ret", "end", "choice", "jmp", "call", "open_call", "commit", "partial_commit", "back_commit", "failtwice", "fail", "giveup", "fullcapture", "opencapture", "closecapture", "closeruntime", "--" }; printf("%02ld: %s ", (long)(p - op), names[p->i.code]); switch ((Opcode)p->i.code) { case IChar: { printf("'%c' (%02x)", p->i.aux1, p->i.aux1); break; } case ITestChar: { printf("'%c' (%02x)", p->i.aux1, p->i.aux1); printjmp(op, p); break; } case IUTFR: { printf("%d - %d", p[1].offset, utf_to(p)); break; } case IFullCapture: { printf("%s (size = %d) (idx = %d)", capkind(getkind(p)), getoff(p), p->i.aux2.key); break; } case IOpenCapture: { printf("%s (idx = %d)", capkind(getkind(p)), p->i.aux2.key); break; } case ISet: { printIcharset(p, (p+1)->buff); break; } case ITestSet: { printIcharset(p, (p+2)->buff); printjmp(op, p); break; } case ISpan: { printIcharset(p, (p+1)->buff); break; } case IOpenCall: { printf("-> %d", (p + 1)->offset); break; } case IBehind: { printf("%d", p->i.aux1); break; } case IJmp: case ICall: case ICommit: case IChoice: case IPartialCommit: case IBackCommit: case ITestAny: { printjmp(op, p); break; } default: break; } printf("\n"); } void printpatt (Instruction *p) { Instruction *op = p; uint n = op[-1].codesize - 1; while (p < op + n) { printinst(op, p); p += sizei(p); } } static void printcap (Capture *cap, int ident) { while (ident--) printf(" "); printf("%s (idx: %d - size: %d) -> %lu (%p)\n", capkind(cap->kind), cap->idx, cap->siz, (long)cap->index, (void*)cap); } /* ** Print a capture and its nested captures */ static Capture *printcap2close (Capture *cap, int ident) { Capture *head = cap++; printcap(head, ident); /* print head capture */ while (capinside(head, cap)) cap = printcap2close(cap, ident + 2); /* print nested captures */ if (isopencap(head)) { assert(isclosecap(cap)); printcap(cap++, ident); /* print and skip close capture */ } return cap; } void printcaplist (Capture *cap) { { /* for debugging, print first a raw list of captures */ Capture *c = cap; while (c->index != MAXINDT) { printcap(c, 0); c++; } } printf(">======\n"); while (!isclosecap(cap)) cap = printcap2close(cap, 0); printf("=======\n"); } /* }====================================================== */ /* ** {====================================================== ** Printing trees (for debugging) ** ======================================================= */ static const char *tagnames[] = { "char", "set", "any", "true", "false", "utf8.range", "rep", "seq", "choice", "not", "and", "call", "opencall", "rule", "xinfo", "grammar", "behind", "capture", "run-time" }; void printtree (TTree *tree, int ident) { int i; int sibs = numsiblings[tree->tag]; for (i = 0; i < ident; i++) printf(" "); printf("%s", tagnames[tree->tag]); switch (tree->tag) { case TChar: { int c = tree->u.n; if (isprint(c)) printf(" '%c'\n", c); else printf(" (%02X)\n", c); break; } case TSet: { printTcharset(tree); printf("\n"); break; } case TUTFR: { assert(sib1(tree)->tag == TXInfo); printf(" %d (%02x %d) - %d (%02x %d) \n", tree->u.n, tree->key, tree->cap, sib1(tree)->u.n, sib1(tree)->key, sib1(tree)->cap); break; } case TOpenCall: case TCall: { assert(sib1(sib2(tree))->tag == TXInfo); printf(" key: %d (rule: %d)\n", tree->key, sib1(sib2(tree))->u.n); break; } case TBehind: { printf(" %d\n", tree->u.n); break; } case TCapture: { printf(" kind: '%s' key: %d\n", capkind(tree->cap), tree->key); break; } case TRule: { printf(" key: %d\n", tree->key); sibs = 1; /* do not print 'sib2' (next rule) as a sibling */ break; } case TXInfo: { printf(" n: %d\n", tree->u.n); break; } case TGrammar: { TTree *rule = sib1(tree); printf(" %d\n", tree->u.n); /* number of rules */ for (i = 0; i < tree->u.n; i++) { printtree(rule, ident + 2); rule = sib2(rule); } assert(rule->tag == TTrue); /* sentinel */ sibs = 0; /* siblings already handled */ break; } default: printf("\n"); break; } if (sibs >= 1) { printtree(sib1(tree), ident + 2); if (sibs >= 2) printtree(sib2(tree), ident + 2); } } void printktable (lua_State *L, int idx) { int n, i; lua_getuservalue(L, idx); if (lua_isnil(L, -1)) /* no ktable? */ return; n = lua_rawlen(L, -1); printf("["); for (i = 1; i <= n; i++) { printf("%d = ", i); lua_rawgeti(L, -1, i); if (lua_isstring(L, -1)) printf("%s ", lua_tostring(L, -1)); else printf("%s ", lua_typename(L, lua_type(L, -1))); lua_pop(L, 1); } printf("]\n"); /* leave ktable at the stack */ } /* }====================================================== */ #endif ================================================ FILE: 3rd/lpeg/lpprint.h ================================================ #if !defined(lpprint_h) #define lpprint_h #include "lptree.h" #include "lpvm.h" #if defined(LPEG_DEBUG) void printpatt (Instruction *p); void printtree (TTree *tree, int ident); void printktable (lua_State *L, int idx); void printcharset (const byte *st); void printcaplist (Capture *cap); void printinst (const Instruction *op, const Instruction *p); #else #define printktable(L,idx) \ luaL_error(L, "function only implemented in debug mode") #define printtree(tree,i) \ luaL_error(L, "function only implemented in debug mode") #define printpatt(p) \ luaL_error(L, "function only implemented in debug mode") #endif #endif ================================================ FILE: 3rd/lpeg/lptree.c ================================================ #include #include #include #include "lua.h" #include "lauxlib.h" #include "lptypes.h" #include "lpcap.h" #include "lpcode.h" #include "lpprint.h" #include "lptree.h" #include "lpcset.h" /* number of siblings for each tree */ const byte numsiblings[] = { 0, 0, 0, /* char, set, any */ 0, 0, 0, /* true, false, utf-range */ 1, /* acc */ 2, 2, /* seq, choice */ 1, 1, /* not, and */ 0, 0, 2, 1, 1, /* call, opencall, rule, prerule, grammar */ 1, /* behind */ 1, 1 /* capture, runtime capture */ }; static TTree *newgrammar (lua_State *L, int arg); /* ** returns a reasonable name for value at index 'idx' on the stack */ static const char *val2str (lua_State *L, int idx) { const char *k = lua_tostring(L, idx); if (k != NULL) return lua_pushfstring(L, "%s", k); else return lua_pushfstring(L, "(a %s)", luaL_typename(L, idx)); } /* ** Fix a TOpenCall into a TCall node, using table 'postable' to ** translate a key to its rule address in the tree. Raises an ** error if key does not exist. */ static void fixonecall (lua_State *L, int postable, TTree *g, TTree *t) { int n; lua_rawgeti(L, -1, t->key); /* get rule's name */ lua_gettable(L, postable); /* query name in position table */ n = lua_tonumber(L, -1); /* get (absolute) position */ lua_pop(L, 1); /* remove position */ if (n == 0) { /* no position? */ lua_rawgeti(L, -1, t->key); /* get rule's name again */ luaL_error(L, "rule '%s' undefined in given grammar", val2str(L, -1)); } t->tag = TCall; t->u.ps = n - (t - g); /* position relative to node */ assert(sib2(t)->tag == TRule); sib2(t)->key = t->key; /* fix rule's key */ } /* ** Transform left associative constructions into right ** associative ones, for sequence and choice; that is: ** (t11 + t12) + t2 => t11 + (t12 + t2) ** (t11 * t12) * t2 => t11 * (t12 * t2) ** (that is, Op (Op t11 t12) t2 => Op t11 (Op t12 t2)) */ static void correctassociativity (TTree *tree) { TTree *t1 = sib1(tree); assert(tree->tag == TChoice || tree->tag == TSeq); while (t1->tag == tree->tag) { int n1size = tree->u.ps - 1; /* t1 == Op t11 t12 */ int n11size = t1->u.ps - 1; int n12size = n1size - n11size - 1; memmove(sib1(tree), sib1(t1), n11size * sizeof(TTree)); /* move t11 */ tree->u.ps = n11size + 1; sib2(tree)->tag = tree->tag; sib2(tree)->u.ps = n12size + 1; } } /* ** Make final adjustments in a tree. Fix open calls in tree 't', ** making them refer to their respective rules or raising appropriate ** errors (if not inside a grammar). Correct associativity of associative ** constructions (making them right associative). Assume that tree's ** ktable is at the top of the stack (for error messages). */ static void finalfix (lua_State *L, int postable, TTree *g, TTree *t) { tailcall: switch (t->tag) { case TGrammar: /* subgrammars were already fixed */ return; case TOpenCall: { if (g != NULL) /* inside a grammar? */ fixonecall(L, postable, g, t); else { /* open call outside grammar */ lua_rawgeti(L, -1, t->key); luaL_error(L, "rule '%s' used outside a grammar", val2str(L, -1)); } break; } case TSeq: case TChoice: correctassociativity(t); break; } switch (numsiblings[t->tag]) { case 1: /* finalfix(L, postable, g, sib1(t)); */ t = sib1(t); goto tailcall; case 2: finalfix(L, postable, g, sib1(t)); t = sib2(t); goto tailcall; /* finalfix(L, postable, g, sib2(t)); */ default: assert(numsiblings[t->tag] == 0); break; } } /* ** {=================================================================== ** KTable manipulation ** ** - The ktable of a pattern 'p' can be shared by other patterns that ** contain 'p' and no other constants. Because of this sharing, we ** should not add elements to a 'ktable' unless it was freshly created ** for the new pattern. ** ** - The maximum index in a ktable is USHRT_MAX, because trees and ** patterns use unsigned shorts to store those indices. ** ==================================================================== */ /* ** Create a new 'ktable' to the pattern at the top of the stack. */ static void newktable (lua_State *L, int n) { lua_createtable(L, n, 0); /* create a fresh table */ lua_setuservalue(L, -2); /* set it as 'ktable' for pattern */ } /* ** Add element 'idx' to 'ktable' of pattern at the top of the stack; ** Return index of new element. ** If new element is nil, does not add it to table (as it would be ** useless) and returns 0, as ktable[0] is always nil. */ static int addtoktable (lua_State *L, int idx) { if (lua_isnil(L, idx)) /* nil value? */ return 0; else { int n; lua_getuservalue(L, -1); /* get ktable from pattern */ n = lua_rawlen(L, -1); if (n >= USHRT_MAX) luaL_error(L, "too many Lua values in pattern"); lua_pushvalue(L, idx); /* element to be added */ lua_rawseti(L, -2, ++n); lua_pop(L, 1); /* remove 'ktable' */ return n; } } /* ** Return the number of elements in the ktable at 'idx'. ** In Lua 5.2/5.3, default "environment" for patterns is nil, not ** a table. Treat it as an empty table. In Lua 5.1, assumes that ** the environment has no numeric indices (len == 0) */ static int ktablelen (lua_State *L, int idx) { if (!lua_istable(L, idx)) return 0; else return lua_rawlen(L, idx); } /* ** Concatentate the contents of table 'idx1' into table 'idx2'. ** (Assume that both indices are negative.) ** Return the original length of table 'idx2' (or 0, if no ** element was added, as there is no need to correct any index). */ static int concattable (lua_State *L, int idx1, int idx2) { int i; int n1 = ktablelen(L, idx1); int n2 = ktablelen(L, idx2); if (n1 + n2 > USHRT_MAX) luaL_error(L, "too many Lua values in pattern"); if (n1 == 0) return 0; /* nothing to correct */ for (i = 1; i <= n1; i++) { lua_rawgeti(L, idx1, i); lua_rawseti(L, idx2 - 1, n2 + i); /* correct 'idx2' */ } return n2; } /* ** When joining 'ktables', constants from one of the subpatterns must ** be renumbered; 'correctkeys' corrects their indices (adding 'n' ** to each of them) */ static void correctkeys (TTree *tree, int n) { if (n == 0) return; /* no correction? */ tailcall: switch (tree->tag) { case TOpenCall: case TCall: case TRunTime: case TRule: { if (tree->key > 0) tree->key += n; break; } case TCapture: { if (tree->key > 0 && tree->cap != Carg && tree->cap != Cnum) tree->key += n; break; } default: break; } switch (numsiblings[tree->tag]) { case 1: /* correctkeys(sib1(tree), n); */ tree = sib1(tree); goto tailcall; case 2: correctkeys(sib1(tree), n); tree = sib2(tree); goto tailcall; /* correctkeys(sib2(tree), n); */ default: assert(numsiblings[tree->tag] == 0); break; } } /* ** Join the ktables from p1 and p2 the ktable for the new pattern at the ** top of the stack, reusing them when possible. */ static void joinktables (lua_State *L, int p1, TTree *t2, int p2) { int n1, n2; lua_getuservalue(L, p1); /* get ktables */ lua_getuservalue(L, p2); n1 = ktablelen(L, -2); n2 = ktablelen(L, -1); if (n1 == 0 && n2 == 0) /* are both tables empty? */ lua_pop(L, 2); /* nothing to be done; pop tables */ else if (n2 == 0 || lp_equal(L, -2, -1)) { /* 2nd table empty or equal? */ lua_pop(L, 1); /* pop 2nd table */ lua_setuservalue(L, -2); /* set 1st ktable into new pattern */ } else if (n1 == 0) { /* first table is empty? */ lua_setuservalue(L, -3); /* set 2nd table into new pattern */ lua_pop(L, 1); /* pop 1st table */ } else { lua_createtable(L, n1 + n2, 0); /* create ktable for new pattern */ /* stack: new p; ktable p1; ktable p2; new ktable */ concattable(L, -3, -1); /* from p1 into new ktable */ concattable(L, -2, -1); /* from p2 into new ktable */ lua_setuservalue(L, -4); /* new ktable becomes 'p' environment */ lua_pop(L, 2); /* pop other ktables */ correctkeys(t2, n1); /* correction for indices from p2 */ } } /* ** copy 'ktable' of element 'idx' to new tree (on top of stack) */ static void copyktable (lua_State *L, int idx) { lua_getuservalue(L, idx); lua_setuservalue(L, -2); } /* ** merge 'ktable' from 'stree' at stack index 'idx' into 'ktable' ** from tree at the top of the stack, and correct corresponding ** tree. */ static void mergektable (lua_State *L, int idx, TTree *stree) { int n; lua_getuservalue(L, -1); /* get ktables */ lua_getuservalue(L, idx); n = concattable(L, -1, -2); lua_pop(L, 2); /* remove both ktables */ correctkeys(stree, n); } /* ** Create a new 'ktable' to the pattern at the top of the stack, adding ** all elements from pattern 'p' (if not 0) plus element 'idx' to it. ** Return index of new element. */ static int addtonewktable (lua_State *L, int p, int idx) { newktable(L, 1); if (p) mergektable(L, p, NULL); return addtoktable(L, idx); } /* }====================================================== */ /* ** {====================================================== ** Tree generation ** ======================================================= */ /* ** In 5.2, could use 'luaL_testudata'... */ static int testpattern (lua_State *L, int idx) { if (lua_touserdata(L, idx)) { /* value is a userdata? */ if (lua_getmetatable(L, idx)) { /* does it have a metatable? */ luaL_getmetatable(L, PATTERN_T); if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ lua_pop(L, 2); /* remove both metatables */ return 1; } } } return 0; } static Pattern *getpattern (lua_State *L, int idx) { return (Pattern *)luaL_checkudata(L, idx, PATTERN_T); } static int getsize (lua_State *L, int idx) { return (lua_rawlen(L, idx) - offsetof(Pattern, tree)) / sizeof(TTree); } static TTree *gettree (lua_State *L, int idx, int *len) { Pattern *p = getpattern(L, idx); if (len) *len = getsize(L, idx); return p->tree; } /* ** create a pattern followed by a tree with 'len' nodes. Set its ** uservalue (the 'ktable') equal to its metatable. (It could be any ** empty sequence; the metatable is at hand here, so we use it.) */ static TTree *newtree (lua_State *L, int len) { size_t size = offsetof(Pattern, tree) + len * sizeof(TTree); Pattern *p = (Pattern *)lua_newuserdata(L, size); luaL_getmetatable(L, PATTERN_T); lua_pushvalue(L, -1); lua_setuservalue(L, -3); lua_setmetatable(L, -2); p->code = NULL; return p->tree; } static TTree *newleaf (lua_State *L, int tag) { TTree *tree = newtree(L, 1); tree->tag = tag; return tree; } /* ** Create a tree for a charset, optimizing for special cases: empty set, ** full set, and singleton set. */ static TTree *newcharset (lua_State *L, byte *cs) { charsetinfo info; Opcode op = charsettype(cs, &info); switch (op) { case IFail: return newleaf(L, TFalse); /* empty set */ case IAny: return newleaf(L, TAny); /* full set */ case IChar: { /* singleton set */ TTree *tree =newleaf(L, TChar); tree->u.n = info.offset; return tree; } default: { /* regular set */ int i; int bsize = /* tree size in bytes */ (int)offsetof(TTree, u.set.bitmap) + info.size; TTree *tree = newtree(L, bytes2slots(bsize)); assert(op == ISet); tree->tag = TSet; tree->u.set.offset = info.offset; tree->u.set.size = info.size; tree->u.set.deflt = info.deflt; for (i = 0; i < info.size; i++) { assert(&treebuffer(tree)[i] < (byte*)tree + bsize); treebuffer(tree)[i] = cs[info.offset + i]; } return tree; } } } /* ** Add to tree a sequence where first sibling is 'sib' (with size ** 'sibsize'); return position for second sibling. */ static TTree *seqaux (TTree *tree, TTree *sib, int sibsize) { tree->tag = TSeq; tree->u.ps = sibsize + 1; memcpy(sib1(tree), sib, sibsize * sizeof(TTree)); return sib2(tree); } /* ** Build a sequence of 'n' nodes, each with tag 'tag' and 'u.n' got ** from the array 's' (or 0 if array is NULL). (TSeq is binary, so it ** must build a sequence of sequence of sequence...) */ static void fillseq (TTree *tree, int tag, int n, const char *s) { int i; for (i = 0; i < n - 1; i++) { /* initial n-1 copies of Seq tag; Seq ... */ tree->tag = TSeq; tree->u.ps = 2; sib1(tree)->tag = tag; sib1(tree)->u.n = s ? (byte)s[i] : 0; tree = sib2(tree); } tree->tag = tag; /* last one does not need TSeq */ tree->u.n = s ? (byte)s[i] : 0; } /* ** Numbers as patterns: ** 0 == true (always match); n == TAny repeated 'n' times; ** -n == not (TAny repeated 'n' times) */ static TTree *numtree (lua_State *L, int n) { if (n == 0) return newleaf(L, TTrue); else { TTree *tree, *nd; if (n > 0) tree = nd = newtree(L, 2 * n - 1); else { /* negative: code it as !(-n) */ n = -n; tree = newtree(L, 2 * n); tree->tag = TNot; nd = sib1(tree); } fillseq(nd, TAny, n, NULL); /* sequence of 'n' any's */ return tree; } } /* ** Convert value at index 'idx' to a pattern */ static TTree *getpatt (lua_State *L, int idx, int *len) { TTree *tree; switch (lua_type(L, idx)) { case LUA_TSTRING: { size_t slen; const char *s = lua_tolstring(L, idx, &slen); /* get string */ if (slen == 0) /* empty? */ tree = newleaf(L, TTrue); /* always match */ else { tree = newtree(L, 2 * (slen - 1) + 1); fillseq(tree, TChar, slen, s); /* sequence of 'slen' chars */ } break; } case LUA_TNUMBER: { int n = lua_tointeger(L, idx); tree = numtree(L, n); break; } case LUA_TBOOLEAN: { tree = (lua_toboolean(L, idx) ? newleaf(L, TTrue) : newleaf(L, TFalse)); break; } case LUA_TTABLE: { tree = newgrammar(L, idx); break; } case LUA_TFUNCTION: { tree = newtree(L, 2); tree->tag = TRunTime; tree->key = addtonewktable(L, 0, idx); sib1(tree)->tag = TTrue; break; } default: { return gettree(L, idx, len); } } lua_replace(L, idx); /* put new tree into 'idx' slot */ if (len) *len = getsize(L, idx); return tree; } /* ** create a new tree, whith a new root and one sibling. ** Sibling must be on the Lua stack, at index 1. */ static TTree *newroot1sib (lua_State *L, int tag) { int s1; TTree *tree1 = getpatt(L, 1, &s1); TTree *tree = newtree(L, 1 + s1); /* create new tree */ tree->tag = tag; memcpy(sib1(tree), tree1, s1 * sizeof(TTree)); copyktable(L, 1); return tree; } /* ** create a new tree, whith a new root and 2 siblings. ** Siblings must be on the Lua stack, first one at index 1. */ static TTree *newroot2sib (lua_State *L, int tag) { int s1, s2; TTree *tree1 = getpatt(L, 1, &s1); TTree *tree2 = getpatt(L, 2, &s2); TTree *tree = newtree(L, 1 + s1 + s2); /* create new tree */ tree->tag = tag; tree->u.ps = 1 + s1; memcpy(sib1(tree), tree1, s1 * sizeof(TTree)); memcpy(sib2(tree), tree2, s2 * sizeof(TTree)); joinktables(L, 1, sib2(tree), 2); return tree; } static int lp_P (lua_State *L) { luaL_checkany(L, 1); getpatt(L, 1, NULL); lua_settop(L, 1); return 1; } /* ** sequence operator; optimizations: ** false x => false, x true => x, true x => x ** (cannot do x . false => false because x may have runtime captures) */ static int lp_seq (lua_State *L) { TTree *tree1 = getpatt(L, 1, NULL); TTree *tree2 = getpatt(L, 2, NULL); if (tree1->tag == TFalse || tree2->tag == TTrue) lua_pushvalue(L, 1); /* false . x == false, x . true = x */ else if (tree1->tag == TTrue) lua_pushvalue(L, 2); /* true . x = x */ else newroot2sib(L, TSeq); return 1; } /* ** choice operator; optimizations: ** charset / charset => charset ** true / x => true, x / false => x, false / x => x ** (x / true is not equivalent to true) */ static int lp_choice (lua_State *L) { Charset st1, st2; TTree *t1 = getpatt(L, 1, NULL); TTree *t2 = getpatt(L, 2, NULL); if (tocharset(t1, &st1) && tocharset(t2, &st2)) { loopset(i, st1.cs[i] |= st2.cs[i]); newcharset(L, st1.cs); } else if (nofail(t1) || t2->tag == TFalse) lua_pushvalue(L, 1); /* true / x => true, x / false => x */ else if (t1->tag == TFalse) lua_pushvalue(L, 2); /* false / x => x */ else newroot2sib(L, TChoice); return 1; } /* ** p^n */ static int lp_star (lua_State *L) { int size1; int n = (int)luaL_checkinteger(L, 2); TTree *tree1 = getpatt(L, 1, &size1); if (n >= 0) { /* seq tree1 (seq tree1 ... (seq tree1 (rep tree1))) */ TTree *tree = newtree(L, (n + 1) * (size1 + 1)); if (nullable(tree1)) luaL_error(L, "loop body may accept empty string"); while (n--) /* repeat 'n' times */ tree = seqaux(tree, tree1, size1); tree->tag = TRep; memcpy(sib1(tree), tree1, size1 * sizeof(TTree)); } else { /* choice (seq tree1 ... choice tree1 true ...) true */ TTree *tree; n = -n; /* size = (choice + seq + tree1 + true) * n, but the last has no seq */ tree = newtree(L, n * (size1 + 3) - 1); for (; n > 1; n--) { /* repeat (n - 1) times */ tree->tag = TChoice; tree->u.ps = n * (size1 + 3) - 2; sib2(tree)->tag = TTrue; tree = sib1(tree); tree = seqaux(tree, tree1, size1); } tree->tag = TChoice; tree->u.ps = size1 + 1; sib2(tree)->tag = TTrue; memcpy(sib1(tree), tree1, size1 * sizeof(TTree)); } copyktable(L, 1); return 1; } /* ** #p == &p */ static int lp_and (lua_State *L) { newroot1sib(L, TAnd); return 1; } /* ** -p == !p */ static int lp_not (lua_State *L) { newroot1sib(L, TNot); return 1; } /* ** [t1 - t2] == Seq (Not t2) t1 ** If t1 and t2 are charsets, make their difference. */ static int lp_sub (lua_State *L) { Charset st1, st2; int s1, s2; TTree *t1 = getpatt(L, 1, &s1); TTree *t2 = getpatt(L, 2, &s2); if (tocharset(t1, &st1) && tocharset(t2, &st2)) { loopset(i, st1.cs[i] &= ~st2.cs[i]); newcharset(L, st1.cs); } else { TTree *tree = newtree(L, 2 + s1 + s2); tree->tag = TSeq; /* sequence of... */ tree->u.ps = 2 + s2; sib1(tree)->tag = TNot; /* ...not... */ memcpy(sib1(sib1(tree)), t2, s2 * sizeof(TTree)); /* ...t2 */ memcpy(sib2(tree), t1, s1 * sizeof(TTree)); /* ... and t1 */ joinktables(L, 1, sib1(tree), 2); } return 1; } static int lp_set (lua_State *L) { size_t l; const char *s = luaL_checklstring(L, 1, &l); byte buff[CHARSETSIZE]; clearset(buff); while (l--) { setchar(buff, (byte)(*s)); s++; } newcharset(L, buff); return 1; } static int lp_range (lua_State *L) { int arg; int top = lua_gettop(L); byte buff[CHARSETSIZE]; clearset(buff); for (arg = 1; arg <= top; arg++) { int c; size_t l; const char *r = luaL_checklstring(L, arg, &l); luaL_argcheck(L, l == 2, arg, "range must have two characters"); for (c = (byte)r[0]; c <= (byte)r[1]; c++) setchar(buff, c); } newcharset(L, buff); return 1; } /* ** Fills a tree node with basic information about the UTF-8 code point ** 'cpu': its value in 'n', its length in 'cap', and its first byte in ** 'key' */ static void codeutftree (lua_State *L, TTree *t, lua_Unsigned cpu, int arg) { int len, fb, cp; cp = (int)cpu; if (cp <= 0x7f) { /* one byte? */ len = 1; fb = cp; } else if (cp <= 0x7ff) { len = 2; fb = 0xC0 | (cp >> 6); } else if (cp <= 0xffff) { len = 3; fb = 0xE0 | (cp >> 12); } else { luaL_argcheck(L, cpu <= 0x10ffffu, arg, "invalid code point"); len = 4; fb = 0xF0 | (cp >> 18); } t->u.n = cp; t->cap = len; t->key = fb; } static int lp_utfr (lua_State *L) { lua_Unsigned from = (lua_Unsigned)luaL_checkinteger(L, 1); lua_Unsigned to = (lua_Unsigned)luaL_checkinteger(L, 2); luaL_argcheck(L, from <= to, 2, "empty range"); if (to <= 0x7f) { /* ascii range? */ uint f; byte buff[CHARSETSIZE]; /* code it as a regular charset */ clearset(buff); for (f = (int)from; f <= to; f++) setchar(buff, f); newcharset(L, buff); } else { /* multi-byte utf-8 range */ TTree *tree = newtree(L, 2); tree->tag = TUTFR; codeutftree(L, tree, from, 1); sib1(tree)->tag = TXInfo; codeutftree(L, sib1(tree), to, 2); } return 1; } /* ** Look-behind predicate */ static int lp_behind (lua_State *L) { TTree *tree; TTree *tree1 = getpatt(L, 1, NULL); int n = fixedlen(tree1); luaL_argcheck(L, n >= 0, 1, "pattern may not have fixed length"); luaL_argcheck(L, !hascaptures(tree1), 1, "pattern have captures"); luaL_argcheck(L, n <= MAXBEHIND, 1, "pattern too long to look behind"); tree = newroot1sib(L, TBehind); tree->u.n = n; return 1; } /* ** Create a non-terminal */ static int lp_V (lua_State *L) { TTree *tree = newleaf(L, TOpenCall); luaL_argcheck(L, !lua_isnoneornil(L, 1), 1, "non-nil value expected"); tree->key = addtonewktable(L, 0, 1); return 1; } /* ** Create a tree for a non-empty capture, with a body and ** optionally with an associated Lua value (at index 'labelidx' in the ** stack) */ static int capture_aux (lua_State *L, int cap, int labelidx) { TTree *tree = newroot1sib(L, TCapture); tree->cap = cap; tree->key = (labelidx == 0) ? 0 : addtonewktable(L, 1, labelidx); return 1; } /* ** Fill a tree with an empty capture, using an empty (TTrue) sibling. ** (The 'key' field must be filled by the caller to finish the tree.) */ static TTree *auxemptycap (TTree *tree, int cap) { tree->tag = TCapture; tree->cap = cap; sib1(tree)->tag = TTrue; return tree; } /* ** Create a tree for an empty capture. */ static TTree *newemptycap (lua_State *L, int cap, int key) { TTree *tree = auxemptycap(newtree(L, 2), cap); tree->key = key; return tree; } /* ** Create a tree for an empty capture with an associated Lua value. */ static TTree *newemptycapkey (lua_State *L, int cap, int idx) { TTree *tree = auxemptycap(newtree(L, 2), cap); tree->key = addtonewktable(L, 0, idx); return tree; } /* ** Captures with syntax p / v ** (function capture, query capture, string capture, or number capture) */ static int lp_divcapture (lua_State *L) { switch (lua_type(L, 2)) { case LUA_TFUNCTION: return capture_aux(L, Cfunction, 2); case LUA_TTABLE: return capture_aux(L, Cquery, 2); case LUA_TSTRING: return capture_aux(L, Cstring, 2); case LUA_TNUMBER: { int n = lua_tointeger(L, 2); TTree *tree = newroot1sib(L, TCapture); luaL_argcheck(L, 0 <= n && n <= SHRT_MAX, 1, "invalid number"); tree->cap = Cnum; tree->key = n; return 1; } default: return luaL_error(L, "unexpected %s as 2nd operand to LPeg '/'", luaL_typename(L, 2)); } } static int lp_acccapture (lua_State *L) { return capture_aux(L, Cacc, 2); } static int lp_substcapture (lua_State *L) { return capture_aux(L, Csubst, 0); } static int lp_tablecapture (lua_State *L) { return capture_aux(L, Ctable, 0); } static int lp_groupcapture (lua_State *L) { if (lua_isnoneornil(L, 2)) return capture_aux(L, Cgroup, 0); else return capture_aux(L, Cgroup, 2); } static int lp_foldcapture (lua_State *L) { luaL_checktype(L, 2, LUA_TFUNCTION); return capture_aux(L, Cfold, 2); } static int lp_simplecapture (lua_State *L) { return capture_aux(L, Csimple, 0); } static int lp_poscapture (lua_State *L) { newemptycap(L, Cposition, 0); return 1; } static int lp_argcapture (lua_State *L) { int n = (int)luaL_checkinteger(L, 1); luaL_argcheck(L, 0 < n && n <= SHRT_MAX, 1, "invalid argument index"); newemptycap(L, Carg, n); return 1; } static int lp_backref (lua_State *L) { luaL_checkany(L, 1); newemptycapkey(L, Cbackref, 1); return 1; } /* ** Constant capture */ static int lp_constcapture (lua_State *L) { int i; int n = lua_gettop(L); /* number of values */ if (n == 0) /* no values? */ newleaf(L, TTrue); /* no capture */ else if (n == 1) newemptycapkey(L, Cconst, 1); /* single constant capture */ else { /* create a group capture with all values */ TTree *tree = newtree(L, 1 + 3 * (n - 1) + 2); newktable(L, n); /* create a 'ktable' for new tree */ tree->tag = TCapture; tree->cap = Cgroup; tree->key = 0; tree = sib1(tree); for (i = 1; i <= n - 1; i++) { tree->tag = TSeq; tree->u.ps = 3; /* skip TCapture and its sibling */ auxemptycap(sib1(tree), Cconst); sib1(tree)->key = addtoktable(L, i); tree = sib2(tree); } auxemptycap(tree, Cconst); tree->key = addtoktable(L, i); } return 1; } static int lp_matchtime (lua_State *L) { TTree *tree; luaL_checktype(L, 2, LUA_TFUNCTION); tree = newroot1sib(L, TRunTime); tree->key = addtonewktable(L, 1, 2); return 1; } /* }====================================================== */ /* ** {====================================================== ** Grammar - Tree generation ** ======================================================= */ /* ** push on the stack the index and the pattern for the ** initial rule of grammar at index 'arg' in the stack; ** also add that index into position table. */ static void getfirstrule (lua_State *L, int arg, int postab) { lua_rawgeti(L, arg, 1); /* access first element */ if (lua_isstring(L, -1)) { /* is it the name of initial rule? */ lua_pushvalue(L, -1); /* duplicate it to use as key */ lua_gettable(L, arg); /* get associated rule */ } else { lua_pushinteger(L, 1); /* key for initial rule */ lua_insert(L, -2); /* put it before rule */ } if (!testpattern(L, -1)) { /* initial rule not a pattern? */ if (lua_isnil(L, -1)) luaL_error(L, "grammar has no initial rule"); else luaL_error(L, "initial rule '%s' is not a pattern", lua_tostring(L, -2)); } lua_pushvalue(L, -2); /* push key */ lua_pushinteger(L, 1); /* push rule position (after TGrammar) */ lua_settable(L, postab); /* insert pair at position table */ } /* ** traverse grammar at index 'arg', pushing all its keys and patterns ** into the stack. Create a new table (before all pairs key-pattern) to ** collect all keys and their associated positions in the final tree ** (the "position table"). ** Return the number of rules and (in 'totalsize') the total size ** for the new tree. */ static int collectrules (lua_State *L, int arg, int *totalsize) { int n = 1; /* to count number of rules */ int postab = lua_gettop(L) + 1; /* index of position table */ int size; /* accumulator for total size */ lua_newtable(L); /* create position table */ getfirstrule(L, arg, postab); size = 3 + getsize(L, postab + 2); /* TGrammar + TRule + TXInfo + rule */ lua_pushnil(L); /* prepare to traverse grammar table */ while (lua_next(L, arg) != 0) { if (lua_tonumber(L, -2) == 1 || lp_equal(L, -2, postab + 1)) { /* initial rule? */ lua_pop(L, 1); /* remove value (keep key for lua_next) */ continue; } if (!testpattern(L, -1)) /* value is not a pattern? */ luaL_error(L, "rule '%s' is not a pattern", val2str(L, -2)); luaL_checkstack(L, LUA_MINSTACK, "grammar has too many rules"); lua_pushvalue(L, -2); /* push key (to insert into position table) */ lua_pushinteger(L, size); lua_settable(L, postab); size += 2 + getsize(L, -1); /* add 'TRule + TXInfo + rule' to size */ lua_pushvalue(L, -2); /* push key (for next lua_next) */ n++; } *totalsize = size + 1; /* space for 'TTrue' finishing list of rules */ return n; } static void buildgrammar (lua_State *L, TTree *grammar, int frule, int n) { int i; TTree *nd = sib1(grammar); /* auxiliary pointer to traverse the tree */ for (i = 0; i < n; i++) { /* add each rule into new tree */ int ridx = frule + 2*i + 1; /* index of i-th rule */ int rulesize; TTree *rn = gettree(L, ridx, &rulesize); TTree *pr = sib1(nd); /* points to rule's prerule */ nd->tag = TRule; nd->key = 0; /* will be fixed when rule is used */ pr->tag = TXInfo; pr->u.n = i; /* rule number */ nd->u.ps = rulesize + 2; /* point to next rule */ memcpy(sib1(pr), rn, rulesize * sizeof(TTree)); /* copy rule */ mergektable(L, ridx, sib1(nd)); /* merge its ktable into new one */ nd = sib2(nd); /* move to next rule */ } nd->tag = TTrue; /* finish list of rules */ } /* ** Check whether a tree has potential infinite loops */ static int checkloops (TTree *tree) { tailcall: if (tree->tag == TRep && nullable(sib1(tree))) return 1; else if (tree->tag == TGrammar) return 0; /* sub-grammars already checked */ else { switch (numsiblings[tree->tag]) { case 1: /* return checkloops(sib1(tree)); */ tree = sib1(tree); goto tailcall; case 2: if (checkloops(sib1(tree))) return 1; /* else return checkloops(sib2(tree)); */ tree = sib2(tree); goto tailcall; default: assert(numsiblings[tree->tag] == 0); return 0; } } } /* ** Give appropriate error message for 'verifyrule'. If a rule appears ** twice in 'passed', there is path from it back to itself without ** advancing the subject. */ static int verifyerror (lua_State *L, unsigned short *passed, int npassed) { int i, j; for (i = npassed - 1; i >= 0; i--) { /* search for a repetition */ for (j = i - 1; j >= 0; j--) { if (passed[i] == passed[j]) { lua_rawgeti(L, -1, passed[i]); /* get rule's key */ return luaL_error(L, "rule '%s' may be left recursive", val2str(L, -1)); } } } return luaL_error(L, "too many left calls in grammar"); } /* ** Check whether a rule can be left recursive; raise an error in that ** case; otherwise return 1 iff pattern is nullable. ** The return value is used to check sequences, where the second pattern ** is only relevant if the first is nullable. ** Parameter 'nb' works as an accumulator, to allow tail calls in ** choices. ('nb' true makes function returns true.) ** Parameter 'passed' is a list of already visited rules, 'npassed' ** counts the elements in 'passed'. ** Assume ktable at the top of the stack. */ static int verifyrule (lua_State *L, TTree *tree, unsigned short *passed, int npassed, int nb) { tailcall: switch (tree->tag) { case TChar: case TSet: case TAny: case TFalse: case TUTFR: return nb; /* cannot pass from here */ case TTrue: case TBehind: /* look-behind cannot have calls */ return 1; case TNot: case TAnd: case TRep: /* return verifyrule(L, sib1(tree), passed, npassed, 1); */ tree = sib1(tree); nb = 1; goto tailcall; case TCapture: case TRunTime: case TXInfo: /* return verifyrule(L, sib1(tree), passed, npassed, nb); */ tree = sib1(tree); goto tailcall; case TCall: /* return verifyrule(L, sib2(tree), passed, npassed, nb); */ tree = sib2(tree); goto tailcall; case TSeq: /* only check 2nd child if first is nb */ if (!verifyrule(L, sib1(tree), passed, npassed, 0)) return nb; /* else return verifyrule(L, sib2(tree), passed, npassed, nb); */ tree = sib2(tree); goto tailcall; case TChoice: /* must check both children */ nb = verifyrule(L, sib1(tree), passed, npassed, nb); /* return verifyrule(L, sib2(tree), passed, npassed, nb); */ tree = sib2(tree); goto tailcall; case TRule: if (npassed >= MAXRULES) /* too many steps? */ return verifyerror(L, passed, npassed); /* error */ else { passed[npassed++] = tree->key; /* add rule to path */ /* return verifyrule(L, sib1(tree), passed, npassed); */ tree = sib1(tree); goto tailcall; } case TGrammar: return nullable(tree); /* sub-grammar cannot be left recursive */ default: assert(0); return 0; } } static void verifygrammar (lua_State *L, TTree *grammar) { unsigned short passed[MAXRULES]; TTree *rule; /* check left-recursive rules */ for (rule = sib1(grammar); rule->tag == TRule; rule = sib2(rule)) { if (rule->key == 0) continue; /* unused rule */ verifyrule(L, sib1(rule), passed, 0, 0); } assert(rule->tag == TTrue); /* check infinite loops inside rules */ for (rule = sib1(grammar); rule->tag == TRule; rule = sib2(rule)) { if (rule->key == 0) continue; /* unused rule */ if (checkloops(sib1(rule))) { lua_rawgeti(L, -1, rule->key); /* get rule's key */ luaL_error(L, "empty loop in rule '%s'", val2str(L, -1)); } } assert(rule->tag == TTrue); } /* ** Give a name for the initial rule if it is not referenced */ static void initialrulename (lua_State *L, TTree *grammar, int frule) { if (sib1(grammar)->key == 0) { /* initial rule is not referenced? */ int n = lua_rawlen(L, -1) + 1; /* index for name */ lua_pushvalue(L, frule); /* rule's name */ lua_rawseti(L, -2, n); /* ktable was on the top of the stack */ sib1(grammar)->key = n; } } static TTree *newgrammar (lua_State *L, int arg) { int treesize; int frule = lua_gettop(L) + 2; /* position of first rule's key */ int n = collectrules(L, arg, &treesize); TTree *g = newtree(L, treesize); luaL_argcheck(L, n <= MAXRULES, arg, "grammar has too many rules"); g->tag = TGrammar; g->u.n = n; lua_newtable(L); /* create 'ktable' */ lua_setuservalue(L, -2); buildgrammar(L, g, frule, n); lua_getuservalue(L, -1); /* get 'ktable' for new tree */ finalfix(L, frule - 1, g, sib1(g)); initialrulename(L, g, frule); verifygrammar(L, g); lua_pop(L, 1); /* remove 'ktable' */ lua_insert(L, -(n * 2 + 2)); /* move new table to proper position */ lua_pop(L, n * 2 + 1); /* remove position table + rule pairs */ return g; /* new table at the top of the stack */ } /* }====================================================== */ static Instruction *prepcompile (lua_State *L, Pattern *p, int idx) { lua_getuservalue(L, idx); /* push 'ktable' (may be used by 'finalfix') */ finalfix(L, 0, NULL, p->tree); lua_pop(L, 1); /* remove 'ktable' */ return compile(L, p, getsize(L, idx)); } static int lp_printtree (lua_State *L) { TTree *tree = getpatt(L, 1, NULL); int c = lua_toboolean(L, 2); if (c) { lua_getuservalue(L, 1); /* push 'ktable' (may be used by 'finalfix') */ finalfix(L, 0, NULL, tree); lua_pop(L, 1); /* remove 'ktable' */ } printktable(L, 1); printtree(tree, 0); return 0; } static int lp_printcode (lua_State *L) { Pattern *p = getpattern(L, 1); printktable(L, 1); if (p->code == NULL) /* not compiled yet? */ prepcompile(L, p, 1); printpatt(p->code); return 0; } /* ** Get the initial position for the match, interpreting negative ** values from the end of the subject */ static size_t initposition (lua_State *L, size_t len) { lua_Integer ii = luaL_optinteger(L, 3, 1); if (ii > 0) { /* positive index? */ if ((size_t)ii <= len) /* inside the string? */ return (size_t)ii - 1; /* return it (corrected to 0-base) */ else return len; /* crop at the end */ } else { /* negative index */ if ((size_t)(-ii) <= len) /* inside the string? */ return len - ((size_t)(-ii)); /* return position from the end */ else return 0; /* crop at the beginning */ } } /* ** Main match function */ static int lp_match (lua_State *L) { Capture capture[INITCAPSIZE]; const char *r; size_t l; Pattern *p = (getpatt(L, 1, NULL), getpattern(L, 1)); Instruction *code = (p->code != NULL) ? p->code : prepcompile(L, p, 1); const char *s = luaL_checklstring(L, SUBJIDX, &l); size_t i = initposition(L, l); int ptop = lua_gettop(L); luaL_argcheck(L, l < MAXINDT, SUBJIDX, "subject too long"); lua_pushnil(L); /* initialize subscache */ lua_pushlightuserdata(L, capture); /* initialize caplistidx */ lua_getuservalue(L, 1); /* initialize ktableidx */ r = match(L, s, s + i, s + l, code, capture, ptop); if (r == NULL) { lua_pushnil(L); return 1; } return getcaptures(L, s, r, ptop); } /* ** {====================================================== ** Library creation and functions not related to matching ** ======================================================= */ /* maximum limit for stack size */ #define MAXLIM (INT_MAX / 100) static int lp_setmax (lua_State *L) { lua_Integer lim = luaL_checkinteger(L, 1); luaL_argcheck(L, 0 < lim && lim <= MAXLIM, 1, "out of range"); lua_settop(L, 1); lua_setfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX); return 0; } static int lp_type (lua_State *L) { if (testpattern(L, 1)) lua_pushliteral(L, "pattern"); else lua_pushnil(L); return 1; } int lp_gc (lua_State *L) { Pattern *p = getpattern(L, 1); freecode(L, p); /* delete code block */ return 0; } /* ** Create a charset representing a category of characters, given by ** the predicate 'catf'. */ static void createcat (lua_State *L, const char *catname, int (catf) (int)) { int c; byte buff[CHARSETSIZE]; clearset(buff); for (c = 0; c <= UCHAR_MAX; c++) if (catf(c)) setchar(buff, c); newcharset(L, buff); lua_setfield(L, -2, catname); } static int lp_locale (lua_State *L) { if (lua_isnoneornil(L, 1)) { lua_settop(L, 0); lua_createtable(L, 0, 12); } else { luaL_checktype(L, 1, LUA_TTABLE); lua_settop(L, 1); } createcat(L, "alnum", isalnum); createcat(L, "alpha", isalpha); createcat(L, "cntrl", iscntrl); createcat(L, "digit", isdigit); createcat(L, "graph", isgraph); createcat(L, "lower", islower); createcat(L, "print", isprint); createcat(L, "punct", ispunct); createcat(L, "space", isspace); createcat(L, "upper", isupper); createcat(L, "xdigit", isxdigit); return 1; } static struct luaL_Reg pattreg[] = { {"ptree", lp_printtree}, {"pcode", lp_printcode}, {"match", lp_match}, {"B", lp_behind}, {"V", lp_V}, {"C", lp_simplecapture}, {"Cc", lp_constcapture}, {"Cmt", lp_matchtime}, {"Cb", lp_backref}, {"Carg", lp_argcapture}, {"Cp", lp_poscapture}, {"Cs", lp_substcapture}, {"Ct", lp_tablecapture}, {"Cf", lp_foldcapture}, {"Cg", lp_groupcapture}, {"P", lp_P}, {"S", lp_set}, {"R", lp_range}, {"utfR", lp_utfr}, {"locale", lp_locale}, {"version", NULL}, {"setmaxstack", lp_setmax}, {"type", lp_type}, {NULL, NULL} }; static struct luaL_Reg metareg[] = { {"__mul", lp_seq}, {"__add", lp_choice}, {"__pow", lp_star}, {"__gc", lp_gc}, {"__len", lp_and}, {"__div", lp_divcapture}, {"__mod", lp_acccapture}, {"__unm", lp_not}, {"__sub", lp_sub}, {NULL, NULL} }; int luaopen_lpeg (lua_State *L); int luaopen_lpeg (lua_State *L) { luaL_newmetatable(L, PATTERN_T); lua_pushnumber(L, MAXBACK); /* initialize maximum backtracking */ lua_setfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX); luaL_setfuncs(L, metareg, 0); luaL_newlib(L, pattreg); lua_pushvalue(L, -1); lua_setfield(L, -3, "__index"); lua_pushliteral(L, "LPeg " VERSION); lua_setfield(L, -2, "version"); return 1; } /* }====================================================== */ ================================================ FILE: 3rd/lpeg/lptree.h ================================================ #if !defined(lptree_h) #define lptree_h #include "lptypes.h" /* ** types of trees */ typedef enum TTag { TChar = 0, /* 'n' = char */ TSet, /* the set is encoded in 'u.set' and the next 'u.set.size' bytes */ TAny, TTrue, TFalse, TUTFR, /* range of UTF-8 codepoints; 'n' has initial codepoint; 'cap' has length; 'key' has first byte; extra info is similar for end codepoint */ TRep, /* 'sib1'* */ TSeq, /* 'sib1' 'sib2' */ TChoice, /* 'sib1' / 'sib2' */ TNot, /* !'sib1' */ TAnd, /* &'sib1' */ TCall, /* ktable[key] is rule's key; 'sib2' is rule being called */ TOpenCall, /* ktable[key] is rule's key */ TRule, /* ktable[key] is rule's key (but key == 0 for unused rules); 'sib1' is rule's pattern pre-rule; 'sib2' is next rule; extra info 'n' is rule's sequential number */ TXInfo, /* extra info */ TGrammar, /* 'sib1' is initial (and first) rule */ TBehind, /* 'sib1' is pattern, 'n' is how much to go back */ TCapture, /* captures: 'cap' is kind of capture (enum 'CapKind'); ktable[key] is Lua value associated with capture; 'sib1' is capture body */ TRunTime /* run-time capture: 'key' is Lua function; 'sib1' is capture body */ } TTag; /* ** Tree trees ** The first child of a tree (if there is one) is immediately after ** the tree. A reference to a second child (ps) is its position ** relative to the position of the tree itself. */ typedef struct TTree { byte tag; byte cap; /* kind of capture (if it is a capture) */ unsigned short key; /* key in ktable for Lua data (0 if no key) */ union { int ps; /* occasional second child */ int n; /* occasional counter */ struct { byte offset; /* compact set offset (in bytes) */ byte size; /* compact set size (in bytes) */ byte deflt; /* default value */ byte bitmap[1]; /* bitmap (open array) */ } set; /* for compact sets */ } u; } TTree; /* access to charset */ #define treebuffer(t) ((t)->u.set.bitmap) /* ** A complete pattern has its tree plus, if already compiled, ** its corresponding code */ typedef struct Pattern { union Instruction *code; TTree tree[1]; } Pattern; /* number of children for each tree */ extern const byte numsiblings[]; /* access to children */ #define sib1(t) ((t) + 1) #define sib2(t) ((t) + (t)->u.ps) #endif ================================================ FILE: 3rd/lpeg/lptypes.h ================================================ /* ** LPeg - PEG pattern matching for Lua ** Copyright 2007-2023, Lua.org & PUC-Rio (see 'lpeg.html' for license) ** written by Roberto Ierusalimschy */ #if !defined(lptypes_h) #define lptypes_h #include #include #include #include "lua.h" #define VERSION "1.1.0" #define PATTERN_T "lpeg-pattern" #define MAXSTACKIDX "lpeg-maxstack" /* ** compatibility with Lua 5.1 */ #if (LUA_VERSION_NUM == 501) #define lp_equal lua_equal #define lua_getuservalue lua_getfenv #define lua_setuservalue lua_setfenv #define lua_rawlen lua_objlen #define luaL_setfuncs(L,f,n) luaL_register(L,NULL,f) #define luaL_newlib(L,f) luaL_register(L,"lpeg",f) typedef size_t lua_Unsigned; #endif #if !defined(lp_equal) #define lp_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) #endif /* default maximum size for call/backtrack stack */ #if !defined(MAXBACK) #define MAXBACK 400 #endif /* maximum number of rules in a grammar (limited by 'unsigned short') */ #if !defined(MAXRULES) #define MAXRULES 1000 #endif /* initial size for capture's list */ #define INITCAPSIZE 32 /* index, on Lua stack, for subject */ #define SUBJIDX 2 /* number of fixed arguments to 'match' (before capture arguments) */ #define FIXEDARGS 3 /* index, on Lua stack, for capture list */ #define caplistidx(ptop) ((ptop) + 2) /* index, on Lua stack, for pattern's ktable */ #define ktableidx(ptop) ((ptop) + 3) /* index, on Lua stack, for backtracking stack */ #define stackidx(ptop) ((ptop) + 4) typedef unsigned char byte; typedef unsigned int uint; #define BITSPERCHAR 8 #define CHARSETSIZE ((UCHAR_MAX/BITSPERCHAR) + 1) typedef struct Charset { byte cs[CHARSETSIZE]; } Charset; #define loopset(v,b) { int v; for (v = 0; v < CHARSETSIZE; v++) {b;} } #define fillset(s,c) memset(s,c,CHARSETSIZE) #define clearset(s) fillset(s,0) /* number of slots needed for 'n' bytes */ #define bytes2slots(n) (((n) - 1u) / (uint)sizeof(TTree) + 1u) /* set 'b' bit in charset 'cs' */ #define setchar(cs,b) ((cs)[(b) >> 3] |= (1 << ((b) & 7))) /* ** in capture instructions, 'kind' of capture and its offset are ** packed in field 'aux', 4 bits for each */ #define getkind(op) ((op)->i.aux1 & 0xF) #define getoff(op) (((op)->i.aux1 >> 4) & 0xF) #define joinkindoff(k,o) ((k) | ((o) << 4)) #define MAXOFF 0xF #define MAXAUX 0xFF /* maximum number of bytes to look behind */ #define MAXBEHIND MAXAUX /* maximum size (in elements) for a pattern */ #define MAXPATTSIZE (SHRT_MAX - 10) /* size (in instructions) for l bytes (l > 0) */ #define instsize(l) ((int)(((l) + (uint)sizeof(Instruction) - 1u) \ / (uint)sizeof(Instruction))) /* size (in elements) for a ISet instruction */ #define CHARSETINSTSIZE (1 + instsize(CHARSETSIZE)) /* size (in elements) for a IFunc instruction */ #define funcinstsize(p) ((p)->i.aux + 2) #define testchar(st,c) ((((uint)(st)[((c) >> 3)]) >> ((c) & 7)) & 1) #endif ================================================ FILE: 3rd/lpeg/lpvm.c ================================================ #include #include #include "lua.h" #include "lauxlib.h" #include "lpcap.h" #include "lptypes.h" #include "lpvm.h" #include "lpprint.h" /* initial size for call/backtrack stack */ #if !defined(INITBACK) #define INITBACK MAXBACK #endif #define getoffset(p) (((p) + 1)->offset) static const Instruction giveup = {{IGiveup, 0, {0}}}; int charinset (const Instruction *i, const byte *buff, uint c) { c -= i->i.aux2.set.offset; if (c >= ((uint)i->i.aux2.set.size /* size in instructions... */ * (uint)sizeof(Instruction) /* in bytes... */ * 8u)) /* in bits */ return i->i.aux1; /* out of range; return default value */ return testchar(buff, c); } /* ** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid. */ static const char *utf8_decode (const char *o, int *val) { static const uint limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFFu}; const unsigned char *s = (const unsigned char *)o; uint c = s[0]; /* first byte */ uint res = 0; /* final result */ if (c < 0x80) /* ascii? */ res = c; else { int count = 0; /* to count number of continuation bytes */ while (c & 0x40) { /* still have continuation bytes? */ int cc = s[++count]; /* read next byte */ if ((cc & 0xC0) != 0x80) /* not a continuation byte? */ return NULL; /* invalid byte sequence */ res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ c <<= 1; /* to test next bit */ } res |= (c & 0x7F) << (count * 5); /* add first byte */ if (count > 3 || res > 0x10FFFFu || res <= limits[count]) return NULL; /* invalid byte sequence */ s += count; /* skip continuation bytes read */ } *val = res; return (const char *)s + 1; /* +1 to include first byte */ } /* ** {====================================================== ** Virtual Machine ** ======================================================= */ typedef struct Stack { const char *s; /* saved position (or NULL for calls) */ const Instruction *p; /* next instruction */ int caplevel; } Stack; #define getstackbase(L, ptop) ((Stack *)lua_touserdata(L, stackidx(ptop))) /* ** Ensures the size of array 'capture' (with size '*capsize' and ** 'captop' elements being used) is enough to accomodate 'n' extra ** elements plus one. (Because several opcodes add stuff to the capture ** array, it is simpler to ensure the array always has at least one free ** slot upfront and check its size later.) */ /* new size in number of elements cannot overflow integers, and new size in bytes cannot overflow size_t. */ #define MAXNEWSIZE \ (((size_t)INT_MAX) <= (~(size_t)0 / sizeof(Capture)) ? \ ((size_t)INT_MAX) : (~(size_t)0 / sizeof(Capture))) static Capture *growcap (lua_State *L, Capture *capture, int *capsize, int captop, int n, int ptop) { if (*capsize - captop > n) return capture; /* no need to grow array */ else { /* must grow */ Capture *newc; uint newsize = captop + n + 1; /* minimum size needed */ if (newsize < (MAXNEWSIZE / 3) * 2) newsize += newsize / 2; /* 1.5 that size, if not too big */ else if (newsize < (MAXNEWSIZE / 9) * 8) newsize += newsize / 8; /* else, try 9/8 that size */ else luaL_error(L, "too many captures"); newc = (Capture *)lua_newuserdata(L, newsize * sizeof(Capture)); memcpy(newc, capture, captop * sizeof(Capture)); *capsize = newsize; lua_replace(L, caplistidx(ptop)); return newc; } } /* ** Double the size of the stack */ static Stack *doublestack (lua_State *L, Stack **stacklimit, int ptop) { Stack *stack = getstackbase(L, ptop); Stack *newstack; int n = *stacklimit - stack; /* current stack size */ int max, newn; lua_getfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX); max = lua_tointeger(L, -1); /* maximum allowed size */ lua_pop(L, 1); if (n >= max) /* already at maximum size? */ luaL_error(L, "backtrack stack overflow (current limit is %d)", max); newn = 2 * n; /* new size */ if (newn > max) newn = max; newstack = (Stack *)lua_newuserdata(L, newn * sizeof(Stack)); memcpy(newstack, stack, n * sizeof(Stack)); lua_replace(L, stackidx(ptop)); *stacklimit = newstack + newn; return newstack + n; /* return next position */ } /* ** Interpret the result of a dynamic capture: false -> fail; ** true -> keep current position; number -> next position. ** Return new subject position. 'fr' is stack index where ** is the result; 'curr' is current subject position; 'limit' ** is subject's size. */ static int resdyncaptures (lua_State *L, int fr, int curr, int limit) { lua_Integer res; if (!lua_toboolean(L, fr)) { /* false value? */ lua_settop(L, fr - 1); /* remove results */ return -1; /* and fail */ } else if (lua_isboolean(L, fr)) /* true? */ res = curr; /* keep current position */ else { res = lua_tointeger(L, fr) - 1; /* new position */ if (res < curr || res > limit) luaL_error(L, "invalid position returned by match-time capture"); } lua_remove(L, fr); /* remove first result (offset) */ return res; } /* ** Add capture values returned by a dynamic capture to the list ** 'capture', nested inside a group. 'fd' indexes the first capture ** value, 'n' is the number of values (at least 1). The open group ** capture is already in 'capture', before the place for the new entries. */ static void adddyncaptures (Index_t index, Capture *capture, int n, int fd) { int i; assert(capture[-1].kind == Cgroup && capture[-1].siz == 0); capture[-1].idx = 0; /* make group capture an anonymous group */ for (i = 0; i < n; i++) { /* add runtime captures */ capture[i].kind = Cruntime; capture[i].siz = 1; /* mark it as closed */ capture[i].idx = fd + i; /* stack index of capture value */ capture[i].index = index; } capture[n].kind = Cclose; /* close group */ capture[n].siz = 1; capture[n].index = index; } /* ** Remove dynamic captures from the Lua stack (called in case of failure) */ static int removedyncap (lua_State *L, Capture *capture, int level, int last) { int id = finddyncap(capture + level, capture + last); /* index of 1st cap. */ int top = lua_gettop(L); if (id == 0) return 0; /* no dynamic captures? */ lua_settop(L, id - 1); /* remove captures */ return top - id + 1; /* number of values removed */ } /* ** Find the corresponding 'open' capture before 'cap', when that capture ** can become a full capture. If a full capture c1 is followed by an ** empty capture c2, there is no way to know whether c2 is inside ** c1. So, full captures can enclose only captures that start *before* ** its end. */ static Capture *findopen (Capture *cap, Index_t currindex) { int i; cap--; /* check last capture */ /* Must it be inside current one, but starts where current one ends? */ if (!isopencap(cap) && cap->index == currindex) return NULL; /* current one cannot be a full capture */ /* else, look for an 'open' capture */ for (i = 0; i < MAXLOP; i++, cap--) { if (currindex - cap->index >= UCHAR_MAX) return NULL; /* capture too long for a full capture */ else if (isopencap(cap)) /* open capture? */ return cap; /* that's the one to be closed */ else if (cap->kind == Cclose) return NULL; /* a full capture should not nest a non-full one */ } return NULL; /* not found within allowed search limit */ } /* ** Opcode interpreter */ const char *match (lua_State *L, const char *o, const char *s, const char *e, Instruction *op, Capture *capture, int ptop) { Stack stackbase[INITBACK]; Stack *stacklimit = stackbase + INITBACK; Stack *stack = stackbase; /* point to first empty slot in stack */ int capsize = INITCAPSIZE; int captop = 0; /* point to first empty slot in captures */ int ndyncap = 0; /* number of dynamic captures (in Lua stack) */ const Instruction *p = op; /* current instruction */ stack->p = &giveup; stack->s = s; stack->caplevel = 0; stack++; lua_pushlightuserdata(L, stackbase); for (;;) { #if defined(DEBUG) printf("-------------------------------------\n"); printcaplist(capture, capture + captop); printf("s: |%s| stck:%d, dyncaps:%d, caps:%d ", s, (int)(stack - getstackbase(L, ptop)), ndyncap, captop); printinst(op, p); #endif assert(stackidx(ptop) + ndyncap == lua_gettop(L) && ndyncap <= captop); switch ((Opcode)p->i.code) { case IEnd: { assert(stack == getstackbase(L, ptop) + 1); capture[captop].kind = Cclose; capture[captop].index = MAXINDT; return s; } case IGiveup: { assert(stack == getstackbase(L, ptop)); return NULL; } case IRet: { assert(stack > getstackbase(L, ptop) && (stack - 1)->s == NULL); p = (--stack)->p; continue; } case IAny: { if (s < e) { p++; s++; } else goto fail; continue; } case IUTFR: { int codepoint; if (s >= e) goto fail; s = utf8_decode (s, &codepoint); if (s && p[1].offset <= codepoint && codepoint <= utf_to(p)) p += 2; else goto fail; continue; } case ITestAny: { if (s < e) p += 2; else p += getoffset(p); continue; } case IChar: { if ((byte)*s == p->i.aux1 && s < e) { p++; s++; } else goto fail; continue; } case ITestChar: { if ((byte)*s == p->i.aux1 && s < e) p += 2; else p += getoffset(p); continue; } case ISet: { uint c = (byte)*s; if (charinset(p, (p+1)->buff, c) && s < e) { p += 1 + p->i.aux2.set.size; s++; } else goto fail; continue; } case ITestSet: { uint c = (byte)*s; if (charinset(p, (p + 2)->buff, c) && s < e) p += 2 + p->i.aux2.set.size; else p += getoffset(p); continue; } case IBehind: { int n = p->i.aux1; if (n > s - o) goto fail; s -= n; p++; continue; } case ISpan: { for (; s < e; s++) { uint c = (byte)*s; if (!charinset(p, (p+1)->buff, c)) break; } p += 1 + p->i.aux2.set.size; continue; } case IJmp: { p += getoffset(p); continue; } case IChoice: { if (stack == stacklimit) stack = doublestack(L, &stacklimit, ptop); stack->p = p + getoffset(p); stack->s = s; stack->caplevel = captop; stack++; p += 2; continue; } case ICall: { if (stack == stacklimit) stack = doublestack(L, &stacklimit, ptop); stack->s = NULL; stack->p = p + 2; /* save return address */ stack++; p += getoffset(p); continue; } case ICommit: { assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL); stack--; p += getoffset(p); continue; } case IPartialCommit: { assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL); (stack - 1)->s = s; (stack - 1)->caplevel = captop; p += getoffset(p); continue; } case IBackCommit: { assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL); s = (--stack)->s; if (ndyncap > 0) /* are there matchtime captures? */ ndyncap -= removedyncap(L, capture, stack->caplevel, captop); captop = stack->caplevel; p += getoffset(p); continue; } case IFailTwice: assert(stack > getstackbase(L, ptop)); stack--; /* FALLTHROUGH */ case IFail: fail: { /* pattern failed: try to backtrack */ do { /* remove pending calls */ assert(stack > getstackbase(L, ptop)); s = (--stack)->s; } while (s == NULL); if (ndyncap > 0) /* is there matchtime captures? */ ndyncap -= removedyncap(L, capture, stack->caplevel, captop); captop = stack->caplevel; p = stack->p; #if defined(DEBUG) printf("**FAIL**\n"); #endif continue; } case ICloseRunTime: { CapState cs; int rem, res, n; int fr = lua_gettop(L) + 1; /* stack index of first result */ cs.reclevel = 0; cs.L = L; cs.s = o; cs.ocap = capture; cs.ptop = ptop; n = runtimecap(&cs, capture + captop, s, &rem); /* call function */ captop -= n; /* remove nested captures */ ndyncap -= rem; /* update number of dynamic captures */ fr -= rem; /* 'rem' items were popped from Lua stack */ res = resdyncaptures(L, fr, s - o, e - o); /* get result */ if (res == -1) /* fail? */ goto fail; s = o + res; /* else update current position */ n = lua_gettop(L) - fr + 1; /* number of new captures */ ndyncap += n; /* update number of dynamic captures */ if (n == 0) /* no new captures? */ captop--; /* remove open group */ else { /* new captures; keep original open group */ if (fr + n >= SHRT_MAX) luaL_error(L, "too many results in match-time capture"); /* add new captures + close group to 'capture' list */ capture = growcap(L, capture, &capsize, captop, n + 1, ptop); adddyncaptures(s - o, capture + captop, n, fr); captop += n + 1; /* new captures + close group */ } p++; continue; } case ICloseCapture: { Capture *open = findopen(capture + captop, s - o); assert(captop > 0); if (open) { /* if possible, turn capture into a full capture */ open->siz = (s - o) - open->index + 1; p++; continue; } else { /* must create a close capture */ capture[captop].siz = 1; /* mark entry as closed */ capture[captop].index = s - o; goto pushcapture; } } case IOpenCapture: capture[captop].siz = 0; /* mark entry as open */ capture[captop].index = s - o; goto pushcapture; case IFullCapture: capture[captop].siz = getoff(p) + 1; /* save capture size */ capture[captop].index = s - o - getoff(p); /* goto pushcapture; */ pushcapture: { capture[captop].idx = p->i.aux2.key; capture[captop].kind = getkind(p); captop++; capture = growcap(L, capture, &capsize, captop, 0, ptop); p++; continue; } default: assert(0); return NULL; } } } /* }====================================================== */ ================================================ FILE: 3rd/lpeg/lpvm.h ================================================ #if !defined(lpvm_h) #define lpvm_h #include "lpcap.h" /* ** About Character sets in instructions: a set is a bit map with an ** initial offset, in bits, and a size, in number of instructions. ** aux1 has the default value for the bits outsize that range. */ /* Virtual Machine's instructions */ typedef enum Opcode { IAny, /* if no char, fail */ IChar, /* if char != aux1, fail */ ISet, /* if char not in set, fail */ ITestAny, /* in no char, jump to 'offset' */ ITestChar, /* if char != aux1, jump to 'offset' */ ITestSet, /* if char not in set, jump to 'offset' */ ISpan, /* read a span of chars in set */ IUTFR, /* if codepoint not in range [offset, utf_to], fail */ IBehind, /* walk back 'aux1' characters (fail if not possible) */ IRet, /* return from a rule */ IEnd, /* end of pattern */ IChoice, /* stack a choice; next fail will jump to 'offset' */ IJmp, /* jump to 'offset' */ ICall, /* call rule at 'offset' */ IOpenCall, /* call rule number 'key' (must be closed to a ICall) */ ICommit, /* pop choice and jump to 'offset' */ IPartialCommit, /* update top choice to current position and jump */ IBackCommit, /* backtrack like "fail" but jump to its own 'offset' */ IFailTwice, /* pop one choice and then fail */ IFail, /* go back to saved state on choice and jump to saved offset */ IGiveup, /* internal use */ IFullCapture, /* complete capture of last 'off' chars */ IOpenCapture, /* start a capture */ ICloseCapture, ICloseRunTime, IEmpty /* to fill empty slots left by optimizations */ } Opcode; /* ** All array of instructions has a 'codesize' as its first element ** and is referred by a pointer to its second element, which is the ** first actual opcode. */ typedef union Instruction { struct Inst { byte code; byte aux1; union { short key; struct { byte offset; byte size; } set; } aux2; } i; int offset; uint codesize; byte buff[1]; } Instruction; /* extract 24-bit value from an instruction */ #define utf_to(inst) (((inst)->i.aux2.key << 8) | (inst)->i.aux1) int charinset (const Instruction *i, const byte *buff, uint c); const char *match (lua_State *L, const char *o, const char *s, const char *e, Instruction *op, Capture *capture, int ptop); #endif ================================================ FILE: 3rd/lpeg/makefile ================================================ LIBNAME = lpeg LUADIR = ../lua/ COPT = -O2 -DNDEBUG # COPT = -O0 -DLPEG_DEBUG -g CWARNS = -Wall -Wextra -pedantic \ -Waggregate-return \ -Wcast-align \ -Wcast-qual \ -Wdisabled-optimization \ -Wpointer-arith \ -Wshadow \ -Wredundant-decls \ -Wsign-compare \ -Wundef \ -Wwrite-strings \ -Wbad-function-cast \ -Wdeclaration-after-statement \ -Wmissing-prototypes \ -Wmissing-declarations \ -Wnested-externs \ -Wstrict-prototypes \ -Wc++-compat \ # -Wunreachable-code \ CFLAGS = $(CWARNS) $(COPT) -std=c99 -I$(LUADIR) -fPIC CC = gcc FILES = lpvm.o lpcap.o lptree.o lpcode.o lpprint.o lpcset.o # For Linux linux: $(MAKE) lpeg.so "DLLFLAGS = -shared -fPIC" # For Mac OS macosx: $(MAKE) lpeg.so "DLLFLAGS = -bundle -undefined dynamic_lookup" lpeg.so: $(FILES) env $(CC) $(DLLFLAGS) $(FILES) -o lpeg.so $(FILES): makefile test: test.lua re.lua lpeg.so ./test.lua clean: rm -f $(FILES) lpeg.so lpcap.o: lpcap.c lpcap.h lptypes.h lpcode.o: lpcode.c lptypes.h lpcode.h lptree.h lpvm.h lpcap.h lpcset.h lpcset.o: lpcset.c lptypes.h lpcset.h lpcode.h lptree.h lpvm.h lpcap.h lpprint.o: lpprint.c lptypes.h lpprint.h lptree.h lpvm.h lpcap.h lpcode.h lptree.o: lptree.c lptypes.h lpcap.h lpcode.h lptree.h lpvm.h lpprint.h \ lpcset.h lpvm.o: lpvm.c lpcap.h lptypes.h lpvm.h lpprint.h lptree.h ================================================ FILE: 3rd/lpeg/re.html ================================================ LPeg.re - Regex syntax for LPEG
LPeg.re
Regex syntax for LPEG

The re Module

The re module (provided by file re.lua in the distribution) supports a somewhat conventional regex syntax for pattern usage within LPeg.

The next table summarizes re's syntax. A p represents an arbitrary pattern; num represents a number ([0-9]+); name represents an identifier ([a-zA-Z][a-zA-Z0-9_]*). Constructions are listed in order of decreasing precedence.
SyntaxDescription
( p ) grouping
& p and predicate
! p not predicate
p1 p2 concatenation
p1 / p2 ordered choice
p ? optional match
p * zero or more repetitions
p + one or more repetitions
p^num exactly num repetitions
p^+num at least num repetitions
p^-num at most num repetitions
(name <- p)+ grammar
'string' literal string
"string" literal string
[class] character class
. any character
%name pattern defs[name] or a pre-defined pattern
namenon terminal
<name>non terminal
{} position capture
{ p } simple capture
{: p :} anonymous group capture
{:name: p :} named group capture
{~ p ~} substitution capture
{| p |} table capture
=name back reference
p -> 'string' string capture
p -> "string" string capture
p -> num numbered capture
p -> name function/query/string capture equivalent to p / defs[name]
p => name match-time capture equivalent to lpeg.Cmt(p, defs[name])
p ~> name fold capture (deprecated)
p >> name accumulator capture equivalent to (p % defs[name])

Any space appearing in a syntax description can be replaced by zero or more space characters and Lua-style short comments (-- until end of line).

Character classes define sets of characters. An initial ^ complements the resulting set. A range x-y includes in the set all characters with codes between the codes of x and y. A pre-defined class %name includes all characters of that class. A simple character includes itself in the set. The only special characters inside a class are ^ (special only if it is the first character); ] (can be included in the set as the first character, after the optional ^); % (special only if followed by a letter); and - (can be included in the set as the first or the last character).

Currently the pre-defined classes are similar to those from the Lua's string library (%a for letters, %A for non letters, etc.). There is also a class %nl containing only the newline character, which is particularly handy for grammars written inside long strings, as long strings do not interpret escape sequences like \n.

Functions

re.compile (string, [, defs])

Compiles the given string and returns an equivalent LPeg pattern. The given string may define either an expression or a grammar. The optional defs table provides extra Lua values to be used by the pattern.

re.find (subject, pattern [, init])

Searches the given pattern in the given subject. If it finds a match, returns the index where this occurrence starts and the index where it ends. Otherwise, returns nil.

An optional numeric argument init makes the search starts at that position in the subject string. As usual in Lua libraries, a negative value counts from the end.

re.gsub (subject, pattern, replacement)

Does a global substitution, replacing all occurrences of pattern in the given subject by replacement.

re.match (subject, pattern)

Matches the given pattern against the given subject, returning all captures.

re.updatelocale ()

Updates the pre-defined character classes to the current locale.

Some Examples

A complete simple program

The next code shows a simple complete Lua program using the re module:

local re = require"re"

-- find the position of the first numeral in a string
print(re.find("the number 423 is odd", "[0-9]+"))  --> 12    14

-- returns all words in a string
print(re.match("the number 423 is odd", "({%a+} / .)*"))
--> the    number    is    odd

-- returns the first numeral in a string
print(re.match("the number 423 is odd", "s <- {%d+} / . s"))
--> 423

-- substitutes a dot for each vowel in a string
print(re.gsub("hello World", "[aeiou]", "."))
--> h.ll. W.rld

Balanced parentheses

The following call will produce the same pattern produced by the Lua expression in the balanced parentheses example:

b = re.compile[[  balanced <- "(" ([^()] / balanced)* ")"  ]]

String reversal

The next example reverses a string:

rev = re.compile[[ R <- (!.) -> '' / ({.} R) -> '%2%1']]
print(rev:match"0123456789")   --> 9876543210

CSV decoder

The next example replicates the CSV decoder:

record = re.compile[[
  record <- {| field (',' field)* |} (%nl / !.)
  field <- escaped / nonescaped
  nonescaped <- { [^,"%nl]* }
  escaped <- '"' {~ ([^"] / '""' -> '"')* ~} '"'
]]

Lua's long strings

The next example matches Lua long strings:

c = re.compile([[
  longstring <- ('[' {:eq: '='* :} '[' close)
  close <- ']' =eq ']' / . close
]])

print(c:match'[==[]]===]]]]==]===[]')   --> 17

Abstract Syntax Trees

This example shows a simple way to build an abstract syntax tree (AST) for a given grammar. To keep our example simple, let us consider the following grammar for lists of names:

p = re.compile[[
      listname <- (name s)*
      name <- [a-z][a-z]*
      s <- %s*
]]

Now, we will add captures to build a corresponding AST. As a first step, the pattern will build a table to represent each non terminal; terminals will be represented by their corresponding strings:

c = re.compile[[
      listname <- {| (name s)* |}
      name <- {| {[a-z][a-z]*} |}
      s <- %s*
]]

Now, a match against "hi hello bye" results in the table {{"hi"}, {"hello"}, {"bye"}}.

For such a simple grammar, this AST is more than enough; actually, the tables around each single name are already overkilling. More complex grammars, however, may need some more structure. Specifically, it would be useful if each table had a tag field telling what non terminal that table represents. We can add such a tag using named group captures:

x = re.compile[[
      listname <- {| {:tag: '' -> 'list':} (name s)* |}
      name <- {| {:tag: '' -> 'id':} {[a-z][a-z]*} |}
      s <- ' '*
]]

With these group captures, a match against "hi hello bye" results in the following table:

{tag="list",
  {tag="id", "hi"},
  {tag="id", "hello"},
  {tag="id", "bye"}
}

Indented blocks

This example breaks indented blocks into tables, respecting the indentation:

p = re.compile[[
  block <- {| {:ident:' '*:} line
           ((=ident !' ' line) / &(=ident ' ') block)* |}
  line <- {[^%nl]*} %nl
]]

As an example, consider the following text:

t = p:match[[
first line
  subline 1
  subline 2
second line
third line
  subline 3.1
    subline 3.1.1
  subline 3.2
]]

The resulting table t will be like this:

   {'first line'; {'subline 1'; 'subline 2'; ident = '  '};
    'second line';
    'third line'; { 'subline 3.1'; {'subline 3.1.1'; ident = '    '};
                    'subline 3.2'; ident = '  '};
    ident = ''}

Macro expander

This example implements a simple macro expander. Macros must be defined as part of the pattern, following some simple rules:

p = re.compile[[
      text <- {~ item* ~}
      item <- macro / [^()] / '(' item* ')'
      arg <- ' '* {~ (!',' item)* ~}
      args <- '(' arg (',' arg)* ')'
      -- now we define some macros
      macro <- ('apply' args) -> '%1(%2)'
             / ('add' args) -> '%1 + %2'
             / ('mul' args) -> '%1 * %2'
]]

print(p:match"add(mul(a,b), apply(f,x))")   --> a * b + f(x)

A text is a sequence of items, wherein we apply a substitution capture to expand any macros. An item is either a macro, any character different from parentheses, or a parenthesized expression. A macro argument (arg) is a sequence of items different from a comma. (Note that a comma may appear inside an item, e.g., inside a parenthesized expression.) Again we do a substitution capture to expand any macro in the argument before expanding the outer macro. args is a list of arguments separated by commas. Finally we define the macros. Each macro is a string substitution; it replaces the macro name and its arguments by its corresponding string, with each %n replaced by the n-th argument.

Patterns

This example shows the complete syntax of patterns accepted by re.

p = [=[

pattern         <- exp !.
exp             <- S (grammar / alternative)

alternative     <- seq ('/' S seq)*
seq             <- prefix*
prefix          <- '&' S prefix / '!' S prefix / suffix
suffix          <- primary S (([+*?]
                            / '^' [+-]? num
                            / '->' S (string / '{}' / name)
                            / '>>' S name
                            / '=>' S name) S)*

primary         <- '(' exp ')' / string / class / defined
                 / '{:' (name ':')? exp ':}'
                 / '=' name
                 / '{}'
                 / '{~' exp '~}'
                 / '{|' exp '|}'
                 / '{' exp '}'
                 / '.'
                 / name S !arrow
                 / '<' name '>'          -- old-style non terminals

grammar         <- definition+
definition      <- name S arrow exp

class           <- '[' '^'? item (!']' item)* ']'
item            <- defined / range / .
range           <- . '-' [^]]

S               <- (%s / '--' [^%nl]*)*   -- spaces and comments
name            <- [A-Za-z_][A-Za-z0-9_]*
arrow           <- '<-'
num             <- [0-9]+
string          <- '"' [^"]* '"' / "'" [^']* "'"
defined         <- '%' name

]=]

print(re.match(p, p))   -- a self description must match itself

License

This module is part of the LPeg package and shares its license.

================================================ FILE: 3rd/lpeg/re.lua ================================================ -- -- Copyright 2007-2023, Lua.org & PUC-Rio (see 'lpeg.html' for license) -- written by Roberto Ierusalimschy -- -- imported functions and modules local tonumber, type, print, error = tonumber, type, print, error local setmetatable = setmetatable local m = require"lpeg" -- 'm' will be used to parse expressions, and 'mm' will be used to -- create expressions; that is, 're' runs on 'm', creating patterns -- on 'mm' local mm = m -- patterns' metatable local mt = getmetatable(mm.P(0)) local version = _VERSION -- No more global accesses after this point _ENV = nil -- does no harm in Lua 5.1 local any = m.P(1) -- Pre-defined names local Predef = { nl = m.P"\n" } local mem local fmem local gmem local function updatelocale () mm.locale(Predef) Predef.a = Predef.alpha Predef.c = Predef.cntrl Predef.d = Predef.digit Predef.g = Predef.graph Predef.l = Predef.lower Predef.p = Predef.punct Predef.s = Predef.space Predef.u = Predef.upper Predef.w = Predef.alnum Predef.x = Predef.xdigit Predef.A = any - Predef.a Predef.C = any - Predef.c Predef.D = any - Predef.d Predef.G = any - Predef.g Predef.L = any - Predef.l Predef.P = any - Predef.p Predef.S = any - Predef.s Predef.U = any - Predef.u Predef.W = any - Predef.w Predef.X = any - Predef.x mem = {} -- restart memoization fmem = {} gmem = {} local mt = {__mode = "v"} setmetatable(mem, mt) setmetatable(fmem, mt) setmetatable(gmem, mt) end updatelocale() local I = m.P(function (s,i) print(i, s:sub(1, i-1)); return i end) local function patt_error (s, i) local msg = (#s < i + 20) and s:sub(i) or s:sub(i,i+20) .. "..." msg = ("pattern error near '%s'"):format(msg) error(msg, 2) end local function mult (p, n) local np = mm.P(true) while n >= 1 do if n%2 >= 1 then np = np * p end p = p * p n = n/2 end return np end local function equalcap (s, i, c) if type(c) ~= "string" then return nil end local e = #c + i if s:sub(i, e - 1) == c then return e else return nil end end local S = (Predef.space + "--" * (any - Predef.nl)^0)^0 local name = m.R("AZ", "az", "__") * m.R("AZ", "az", "__", "09")^0 local arrow = S * "<-" local seq_follow = m.P"/" + ")" + "}" + ":}" + "~}" + "|}" + (name * arrow) + -1 name = m.C(name) -- a defined name only have meaning in a given environment local Def = name * m.Carg(1) local function getdef (id, defs) local c = defs and defs[id] if not c then error("undefined name: " .. id) end return c end -- match a name and return a group of its corresponding definition -- and 'f' (to be folded in 'Suffix') local function defwithfunc (f) return m.Cg(Def / getdef * m.Cc(f)) end local num = m.C(m.R"09"^1) * S / tonumber local String = "'" * m.C((any - "'")^0) * "'" + '"' * m.C((any - '"')^0) * '"' local defined = "%" * Def / function (c,Defs) local cat = Defs and Defs[c] or Predef[c] if not cat then error ("name '" .. c .. "' undefined") end return cat end local Range = m.Cs(any * (m.P"-"/"") * (any - "]")) / mm.R local item = (defined + Range + m.C(any)) / m.P local Class = "[" * (m.C(m.P"^"^-1)) -- optional complement symbol * (item * ((item % mt.__add) - "]")^0) / function (c, p) return c == "^" and any - p or p end * "]" local function adddef (t, k, exp) if t[k] then error("'"..k.."' already defined as a rule") else t[k] = exp end return t end local function firstdef (n, r) return adddef({n}, n, r) end local function NT (n, b) if not b then error("rule '"..n.."' used outside a grammar") else return mm.V(n) end end local exp = m.P{ "Exp", Exp = S * ( m.V"Grammar" + m.V"Seq" * ("/" * S * m.V"Seq" % mt.__add)^0 ); Seq = (m.Cc(m.P"") * (m.V"Prefix" % mt.__mul)^0) * (#seq_follow + patt_error); Prefix = "&" * S * m.V"Prefix" / mt.__len + "!" * S * m.V"Prefix" / mt.__unm + m.V"Suffix"; Suffix = m.V"Primary" * S * ( ( m.P"+" * m.Cc(1, mt.__pow) + m.P"*" * m.Cc(0, mt.__pow) + m.P"?" * m.Cc(-1, mt.__pow) + "^" * ( m.Cg(num * m.Cc(mult)) + m.Cg(m.C(m.S"+-" * m.R"09"^1) * m.Cc(mt.__pow)) ) + "->" * S * ( m.Cg((String + num) * m.Cc(mt.__div)) + m.P"{}" * m.Cc(nil, m.Ct) + defwithfunc(mt.__div) ) + "=>" * S * defwithfunc(mm.Cmt) + ">>" * S * defwithfunc(mt.__mod) + "~>" * S * defwithfunc(mm.Cf) ) % function (a,b,f) return f(a,b) end * S )^0; Primary = "(" * m.V"Exp" * ")" + String / mm.P + Class + defined + "{:" * (name * ":" + m.Cc(nil)) * m.V"Exp" * ":}" / function (n, p) return mm.Cg(p, n) end + "=" * name / function (n) return mm.Cmt(mm.Cb(n), equalcap) end + m.P"{}" / mm.Cp + "{~" * m.V"Exp" * "~}" / mm.Cs + "{|" * m.V"Exp" * "|}" / mm.Ct + "{" * m.V"Exp" * "}" / mm.C + m.P"." * m.Cc(any) + (name * -arrow + "<" * name * ">") * m.Cb("G") / NT; Definition = name * arrow * m.V"Exp"; Grammar = m.Cg(m.Cc(true), "G") * ((m.V"Definition" / firstdef) * (m.V"Definition" % adddef)^0) / mm.P } local pattern = S * m.Cg(m.Cc(false), "G") * exp / mm.P * (-any + patt_error) local function compile (p, defs) if mm.type(p) == "pattern" then return p end -- already compiled local cp = pattern:match(p, 1, defs) if not cp then error("incorrect pattern", 3) end return cp end local function match (s, p, i) local cp = mem[p] if not cp then cp = compile(p) mem[p] = cp end return cp:match(s, i or 1) end local function find (s, p, i) local cp = fmem[p] if not cp then cp = compile(p) / 0 cp = mm.P{ mm.Cp() * cp * mm.Cp() + 1 * mm.V(1) } fmem[p] = cp end local i, e = cp:match(s, i or 1) if i then return i, e - 1 else return i end end local function gsub (s, p, rep) local g = gmem[p] or {} -- ensure gmem[p] is not collected while here gmem[p] = g local cp = g[rep] if not cp then cp = compile(p) cp = mm.Cs((cp / rep + 1)^0) g[rep] = cp end return cp:match(s) end -- exported names local re = { compile = compile, match = match, find = find, gsub = gsub, updatelocale = updatelocale, } if version == "Lua 5.1" then _G.re = re end return re ================================================ FILE: 3rd/lpeg/test.lua ================================================ #!/usr/bin/env lua -- require"strict" -- just to be pedantic local m = require"lpeg" -- for general use local a, b, c, d, e, f, g, p, t -- compatibility with Lua 5.2 local unpack = rawget(table, "unpack") or unpack local loadstring = rawget(_G, "loadstring") or load local any = m.P(1) local space = m.S" \t\n"^0 local function checkeq (x, y, p) if p then print(x,y) end if type(x) ~= "table" then assert(x == y) else for k,v in pairs(x) do checkeq(v, y[k], p) end for k,v in pairs(y) do checkeq(v, x[k], p) end end end local mt = getmetatable(m.P(1)) local allchar = {} for i=0,255 do allchar[i + 1] = i end allchar = string.char(unpack(allchar)) assert(#allchar == 256) local function cs2str (c) return m.match(m.Cs((c + m.P(1)/"")^0), allchar) end local function eqcharset (c1, c2) assert(cs2str(c1) == cs2str(c2)) end print"General tests for LPeg library" assert(type(m.version) == "string") print(m.version) assert(m.type("alo") ~= "pattern") assert(m.type(io.input) ~= "pattern") assert(m.type(m.P"alo") == "pattern") -- tests for some basic optimizations assert(m.match(m.P(false) + "a", "a") == 2) assert(m.match(m.P(true) + "a", "a") == 1) assert(m.match("a" + m.P(false), "b") == nil) assert(m.match("a" + m.P(true), "b") == 1) assert(m.match(m.P(false) * "a", "a") == nil) assert(m.match(m.P(true) * "a", "a") == 2) assert(m.match("a" * m.P(false), "a") == nil) assert(m.match("a" * m.P(true), "a") == 2) assert(m.match(#m.P(false) * "a", "a") == nil) assert(m.match(#m.P(true) * "a", "a") == 2) assert(m.match("a" * #m.P(false), "a") == nil) assert(m.match("a" * #m.P(true), "a") == 2) assert(m.match(m.P(1)^0, "abcd") == 5) assert(m.match(m.S("")^0, "abcd") == 1) -- tests for locale do assert(m.locale(m) == m) local t = {} assert(m.locale(t, m) == t) local x = m.locale() for n,v in pairs(x) do assert(type(n) == "string") eqcharset(v, m[n]) end end assert(m.match(3, "aaaa")) assert(m.match(4, "aaaa")) assert(not m.match(5, "aaaa")) assert(m.match(-3, "aa")) assert(not m.match(-3, "aaa")) assert(not m.match(-3, "aaaa")) assert(not m.match(-4, "aaaa")) assert(m.P(-5):match"aaaa") assert(m.match("a", "alo") == 2) assert(m.match("al", "alo") == 3) assert(not m.match("alu", "alo")) assert(m.match(true, "") == 1) local digit = m.S"0123456789" local upper = m.S"ABCDEFGHIJKLMNOPQRSTUVWXYZ" local lower = m.S"abcdefghijklmnopqrstuvwxyz" local letter = m.S"" + upper + lower local alpha = letter + digit + m.R() eqcharset(m.S"", m.P(false)) eqcharset(upper, m.R("AZ")) eqcharset(lower, m.R("az")) eqcharset(upper + lower, m.R("AZ", "az")) eqcharset(upper + lower, m.R("AZ", "cz", "aa", "bb", "90")) eqcharset(digit, m.S"01234567" + "8" + "9") eqcharset(upper, letter - lower) eqcharset(m.S(""), m.R()) assert(cs2str(m.S("")) == "") eqcharset(m.S"\0", "\0") eqcharset(m.S"\1\0\2", m.R"\0\2") eqcharset(m.S"\1\0\2", m.R"\1\2" + "\0") eqcharset(m.S"\1\0\2" - "\0", m.R"\1\2") eqcharset(m.S("\0\255"), m.P"\0" + "\255") -- charset extremes local word = alpha^1 * (1 - alpha)^0 assert((word^0 * -1):match"alo alo") assert(m.match(word^1 * -1, "alo alo")) assert(m.match(word^2 * -1, "alo alo")) assert(not m.match(word^3 * -1, "alo alo")) assert(not m.match(word^-1 * -1, "alo alo")) assert(m.match(word^-2 * -1, "alo alo")) assert(m.match(word^-3 * -1, "alo alo")) local eos = m.P(-1) assert(m.match(digit^0 * letter * digit * eos, "1298a1")) assert(not m.match(digit^0 * letter * eos, "1257a1")) b = { [1] = "(" * (((1 - m.S"()") + #m.P"(" * m.V(1))^0) * ")" } assert(m.match(b, "(al())()")) assert(not m.match(b * eos, "(al())()")) assert(m.match(b * eos, "((al())()(é))")) assert(not m.match(b, "(al()()")) assert(not m.match(letter^1 - "for", "foreach")) assert(m.match(letter^1 - ("for" * eos), "foreach")) assert(not m.match(letter^1 - ("for" * eos), "for")) function basiclookfor (p) return m.P { [1] = p + (1 * m.V(1)) } end function caplookfor (p) return basiclookfor(p:C()) end assert(m.match(caplookfor(letter^1), " 4achou123...") == "achou") a = {m.match(caplookfor(letter^1)^0, " two words, one more ")} checkeq(a, {"two", "words", "one", "more"}) assert(m.match( basiclookfor((#m.P(b) * 1) * m.Cp()), " ( (a)") == 7) a = {m.match(m.C(digit^1 * m.Cc"d") + m.C(letter^1 * m.Cc"l"), "123")} checkeq(a, {"123", "d"}) -- bug in LPeg 0.12 (nil value does not create a 'ktable') assert(m.match(m.Cc(nil), "") == nil) a = {m.match(m.C(digit^1 * m.Cc"d") + m.C(letter^1 * m.Cc"l"), "abcd")} checkeq(a, {"abcd", "l"}) a = {m.match(m.Cc(10,20,30) * 'a' * m.Cp(), 'aaa')} checkeq(a, {10,20,30,2}) a = {m.match(m.Cp() * m.Cc(10,20,30) * 'a' * m.Cp(), 'aaa')} checkeq(a, {1,10,20,30,2}) a = m.match(m.Ct(m.Cp() * m.Cc(10,20,30) * 'a' * m.Cp()), 'aaa') checkeq(a, {1,10,20,30,2}) a = m.match(m.Ct(m.Cp() * m.Cc(7,8) * m.Cc(10,20,30) * 'a' * m.Cp()), 'aaa') checkeq(a, {1,7,8,10,20,30,2}) a = {m.match(m.Cc() * m.Cc() * m.Cc(1) * m.Cc(2,3,4) * m.Cc() * 'a', 'aaa')} checkeq(a, {1,2,3,4}) a = {m.match(m.Cp() * letter^1 * m.Cp(), "abcd")} checkeq(a, {1, 5}) t = {m.match({[1] = m.C(m.C(1) * m.V(1) + -1)}, "abc")} checkeq(t, {"abc", "a", "bc", "b", "c", "c", ""}) -- bug in 0.12 ('hascapture' did not check for captures inside a rule) do local pat = m.P{ 'S'; S1 = m.C('abc') + 3, S = #m.V('S1') -- rule has capture, but '#' must ignore it } assert(pat:match'abc' == 1) end -- bug: loop in 'hascaptures' do local p = m.C(-m.P{m.P'x' * m.V(1) + m.P'y'}) assert(p:match("xxx") == "") end -- test for small capture boundary for i = 250,260 do assert(#m.match(m.C(i), string.rep('a', i)) == i) assert(#m.match(m.C(m.C(i)), string.rep('a', i)) == i) end -- tests for any*n and any*-n for n = 1, 550, 13 do local x_1 = string.rep('x', n - 1) local x = x_1 .. 'a' assert(not m.P(n):match(x_1)) assert(m.P(n):match(x) == n + 1) assert(n < 4 or m.match(m.P(n) + "xxx", x_1) == 4) assert(m.C(n):match(x) == x) assert(m.C(m.C(n)):match(x) == x) assert(m.P(-n):match(x_1) == 1) assert(not m.P(-n):match(x)) assert(n < 13 or m.match(m.Cc(20) * ((n - 13) * m.P(10)) * 3, x) == 20) local n3 = math.floor(n/3) assert(m.match(n3 * m.Cp() * n3 * n3, x) == n3 + 1) end -- true values assert(m.P(0):match("x") == 1) assert(m.P(0):match("") == 1) assert(m.C(0):match("x") == "") assert(m.match(m.Cc(0) * m.P(10) + m.Cc(1) * "xuxu", "xuxu") == 1) assert(m.match(m.Cc(0) * m.P(10) + m.Cc(1) * "xuxu", "xuxuxuxuxu") == 0) assert(m.match(m.C(m.P(2)^1), "abcde") == "abcd") p = m.Cc(0) * 1 + m.Cc(1) * 2 + m.Cc(2) * 3 + m.Cc(3) * 4 -- test for alternation optimization assert(m.match(m.P"a"^1 + "ab" + m.P"x"^0, "ab") == 2) assert(m.match((m.P"a"^1 + "ab" + m.P"x"^0 * 1)^0, "ab") == 3) assert(m.match(m.P"ab" + "cd" + "" + "cy" + "ak", "98") == 1) assert(m.match(m.P"ab" + "cd" + "ax" + "cy", "ax") == 3) assert(m.match("a" * m.P"b"^0 * "c" + "cd" + "ax" + "cy", "ax") == 3) assert(m.match((m.P"ab" + "cd" + "ax" + "cy")^0, "ax") == 3) assert(m.match(m.P(1) * "x" + m.S"" * "xu" + "ay", "ay") == 3) assert(m.match(m.P"abc" + "cde" + "aka", "aka") == 4) assert(m.match(m.S"abc" * "x" + "cde" + "aka", "ax") == 3) assert(m.match(m.S"abc" * "x" + "cde" + "aka", "aka") == 4) assert(m.match(m.S"abc" * "x" + "cde" + "aka", "cde") == 4) assert(m.match(m.S"abc" * "x" + "ide" + m.S"ab" * "ka", "aka") == 4) assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "cde" + "aka", "ax") == 3) assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "cde" + "aka", "aka") == 4) assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "cde" + "aka", "cde") == 4) assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "ide" + m.S"ab" * "ka", "aka") == 4) assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "ide" + m.S"ab" * "ka", "ax") == 3) assert(m.match(m.P(1) * "x" + "cde" + m.S"ab" * "ka", "aka") == 4) assert(m.match(m.P(1) * "x" + "cde" + m.P(1) * "ka", "aka") == 4) assert(m.match(m.P(1) * "x" + "cde" + m.P(1) * "ka", "cde") == 4) assert(m.match(m.P"eb" + "cd" + m.P"e"^0 + "x", "ee") == 3) assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x", "abcd") == 3) assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x", "eeex") == 4) assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x", "cd") == 3) assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x", "x") == 1) assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x" + "", "zee") == 1) assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x", "abcd") == 3) assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x", "eeex") == 4) assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x", "cd") == 3) assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x", "x") == 2) assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x" + "", "zee") == 1) assert(not m.match(("aa" * m.P"bc"^-1 + "aab") * "e", "aabe")) assert(m.match("alo" * (m.P"\n" + -1), "alo") == 4) -- bug in 0.12 (rc1) assert(m.match((m.P"\128\187\191" + m.S"abc")^0, "\128\187\191") == 4) assert(m.match(m.S"\0\128\255\127"^0, string.rep("\0\128\255\127", 10)) == 4*10 + 1) -- optimizations with optional parts assert(m.match(("ab" * -m.P"c")^-1, "abc") == 1) assert(m.match(("ab" * #m.P"c")^-1, "abd") == 1) assert(m.match(("ab" * m.B"c")^-1, "ab") == 1) assert(m.match(("ab" * m.P"cd"^0)^-1, "abcdcdc") == 7) assert(m.match(m.P"ab"^-1 - "c", "abcd") == 3) p = ('Aa' * ('Bb' * ('Cc' * m.P'Dd'^0)^0)^0)^-1 assert(p:match("AaBbCcDdBbCcDdDdDdBb") == 21) -- bug in 0.12.2 -- p = { ('ab' ('c' 'ef'?)*)? } p = m.C(('ab' * ('c' * m.P'ef'^-1)^0)^-1) s = "abcefccefc" assert(s == p:match(s)) pi = "3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510" assert(m.match(m.Cs((m.P"1" / "a" + m.P"5" / "b" + m.P"9" / "c" + 1)^0), pi) == m.match(m.Cs((m.P(1) / {["1"] = "a", ["5"] = "b", ["9"] = "c"})^0), pi)) print"+" -- tests for capture optimizations assert(m.match((m.P(3) + 4 * m.Cp()) * "a", "abca") == 5) t = {m.match(((m.P"a" + m.Cp()) * m.P"x")^0, "axxaxx")} checkeq(t, {3, 6}) -- tests for numbered captures p = m.C(1) assert(m.match(m.C(m.C(p * m.C(2)) * m.C(3)) / 3, "abcdefgh") == "a") assert(m.match(m.C(m.C(p * m.C(2)) * m.C(3)) / 1, "abcdefgh") == "abcdef") assert(m.match(m.C(m.C(p * m.C(2)) * m.C(3)) / 4, "abcdefgh") == "bc") assert(m.match(m.C(m.C(p * m.C(2)) * m.C(3)) / 0, "abcdefgh") == 7) a, b, c = m.match(p * (m.C(p * m.C(2)) * m.C(3) / 4) * p, "abcdefgh") assert(a == "a" and b == "efg" and c == "h") -- test for table captures t = m.match(m.Ct(letter^1), "alo") checkeq(t, {}) t, n = m.match(m.Ct(m.C(letter)^1) * m.Cc"t", "alo") assert(n == "t" and table.concat(t) == "alo") t = m.match(m.Ct(m.C(m.C(letter)^1)), "alo") assert(table.concat(t, ";") == "alo;a;l;o") t = m.match(m.Ct(m.C(m.C(letter)^1)), "alo") assert(table.concat(t, ";") == "alo;a;l;o") t = m.match(m.Ct(m.Ct((m.Cp() * letter * m.Cp())^1)), "alo") assert(table.concat(t[1], ";") == "1;2;2;3;3;4") t = m.match(m.Ct(m.C(m.C(1) * 1 * m.C(1))), "alo") checkeq(t, {"alo", "a", "o"}) -- tests for groups p = m.Cg(1) -- no capture assert(p:match('x') == 'x') p = m.Cg(m.P(true)/function () end * 1) -- no value assert(p:match('x') == 'x') p = m.Cg(m.Cg(m.Cg(m.C(1)))) assert(p:match('x') == 'x') p = m.Cg(m.Cg(m.Cg(m.C(1))^0) * m.Cg(m.Cc(1) * m.Cc(2))) t = {p:match'abc'} checkeq(t, {'a', 'b', 'c', 1, 2}) p = m.Ct(m.Cg(m.Cc(10), "hi") * m.C(1)^0 * m.Cg(m.Cc(20), "ho")) t = p:match'' checkeq(t, {hi = 10, ho = 20}) t = p:match'abc' checkeq(t, {hi = 10, ho = 20, 'a', 'b', 'c'}) -- non-string group names p = m.Ct(m.Cg(1, print) * m.Cg(1, 23.5) * m.Cg(1, io)) t = p:match('abcdefghij') assert(t[print] == 'a' and t[23.5] == 'b' and t[io] == 'c') -- test for error messages local function checkerr (msg, f, ...) local st, err = pcall(f, ...) assert(not st and m.match({ m.P(msg) + 1 * m.V(1) }, err)) end checkerr("rule '1' may be left recursive", m.match, { m.V(1) * 'a' }, "a") checkerr("rule '1' used outside a grammar", m.match, m.V(1), "") checkerr("rule 'hiii' used outside a grammar", m.match, m.V('hiii'), "") checkerr("rule 'hiii' undefined in given grammar", m.match, { m.V('hiii') }, "") checkerr("undefined in given grammar", m.match, { m.V{} }, "") checkerr("rule 'A' is not a pattern", m.P, { m.P(1), A = {} }) checkerr("grammar has no initial rule", m.P, { [print] = {} }) -- grammar with a long call chain before left recursion p = {'a', a = m.V'b' * m.V'c' * m.V'd' * m.V'a', b = m.V'c', c = m.V'd', d = m.V'e', e = m.V'f', f = m.V'g', g = m.P'' } checkerr("rule 'a' may be left recursive", m.match, p, "a") -- Bug in peephole optimization of LPeg 0.12 (IJmp -> ICommit) -- the next grammar has an original sequence IJmp -> ICommit -> IJmp L1 -- that is optimized to ICommit L1 p = m.P { (m.P {m.P'abc'} + 'ayz') * m.V'y'; y = m.P'x' } assert(p:match('abcx') == 5 and p:match('ayzx') == 5 and not p:match'abc') do print "testing large dynamic Cc" local lim = 2^16 - 1 local c = 0 local function seq (n) if n == 1 then c = c + 1; return m.Cc(c) else local m = math.floor(n / 2) return seq(m) * seq(n - m) end end p = m.Ct(seq(lim)) t = p:match('') assert(t[lim] == lim) checkerr("too many", function () p = p / print end) checkerr("too many", seq, lim + 1) end do -- nesting of captures too deep local p = m.C(1) for i = 1, 300 do p = m.Ct(p) end checkerr("too deep", p.match, p, "x") end -- tests for non-pattern as arguments to pattern functions p = { ('a' * m.V(1))^-1 } * m.P'b' * { 'a' * m.V(2); m.V(1)^-1 } assert(m.match(p, "aaabaac") == 7) p = m.P'abc' * 2 * -5 * true * 'de' -- mix of numbers and strings and booleans assert(p:match("abc01de") == 8) assert(p:match("abc01de3456") == nil) p = 'abc' * (2 * (-5 * (true * m.P'de'))) assert(p:match("abc01de") == 8) assert(p:match("abc01de3456") == nil) p = { m.V(2), m.P"abc" } * (m.P{ "xx", xx = m.P"xx" } + { "x", x = m.P"a" * m.V"x" + "" }) assert(p:match("abcaaaxx") == 7) assert(p:match("abcxx") == 6) -- a large table capture t = m.match(m.Ct(m.C('a')^0), string.rep("a", 10000)) assert(#t == 10000 and t[1] == 'a' and t[#t] == 'a') print('+') -- bug in 0.10 (rechecking a grammar, after tail-call optimization) m.P{ m.P { (m.P(3) + "xuxu")^0 * m.V"xuxu", xuxu = m.P(1) } } local V = m.V local Space = m.S(" \n\t")^0 local Number = m.C(m.R("09")^1) * Space local FactorOp = m.C(m.S("+-")) * Space local TermOp = m.C(m.S("*/")) * Space local Open = "(" * Space local Close = ")" * Space local function f_factor (v1, op, v2, d) assert(d == nil) if op == "+" then return v1 + v2 else return v1 - v2 end end local function f_term (v1, op, v2, d) assert(d == nil) if op == "*" then return v1 * v2 else return v1 / v2 end end G = m.P{ "Exp", Exp = V"Factor" * (FactorOp * V"Factor" % f_factor)^0; Factor = V"Term" * (TermOp * V"Term" % f_term)^0; Term = Number / tonumber + Open * V"Exp" * Close; } G = Space * G * -1 for _, s in ipairs{" 3 + 5*9 / (1+1) ", "3+4/2", "3+3-3- 9*2+3*9/1- 8"} do assert(m.match(G, s) == loadstring("return "..s)()) end -- test for grammars (errors deep in calling non-terminals) g = m.P{ [1] = m.V(2) + "a", [2] = "a" * m.V(3) * "x", [3] = "b" * m.V(3) + "c" } assert(m.match(g, "abbbcx") == 7) assert(m.match(g, "abbbbx") == 2) -- tests for \0 assert(m.match(m.R("\0\1")^1, "\0\1\0") == 4) assert(m.match(m.S("\0\1ab")^1, "\0\1\0a") == 5) assert(m.match(m.P(1)^3, "\0\1\0a") == 5) assert(not m.match(-4, "\0\1\0a")) assert(m.match("\0\1\0a", "\0\1\0a") == 5) assert(m.match("\0\0\0", "\0\0\0") == 4) assert(not m.match("\0\0\0", "\0\0")) -- tests for predicates assert(not m.match(-m.P("a") * 2, "alo")) assert(m.match(- -m.P("a") * 2, "alo") == 3) assert(m.match(#m.P("a") * 2, "alo") == 3) assert(m.match(##m.P("a") * 2, "alo") == 3) assert(not m.match(##m.P("c") * 2, "alo")) assert(m.match(m.Cs((##m.P("a") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") assert(m.match(m.Cs((#((#m.P"a")/"") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") assert(m.match(m.Cs((- -m.P("a") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") assert(m.match(m.Cs((-((-m.P"a")/"") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") -- fixed length do -- 'and' predicate using fixed length local p = m.C(#("a" * (m.P("bd") + "cd")) * 2) assert(p:match("acd") == "ac") p = #m.P{ "a" * m.V(2), m.P"b" } * 2 assert(p:match("abc") == 3) p = #(m.P"abc" * m.B"c") assert(p:match("abc") == 1 and not p:match("ab")) p = m.P{ "a" * m.V(2), m.P"b"^1 } checkerr("pattern may not have fixed length", m.B, p) p = "abc" * (m.P"b"^1 + m.P"a"^0) checkerr("pattern may not have fixed length", m.B, p) end p = -m.P'a' * m.Cc(1) + -m.P'b' * m.Cc(2) + -m.P'c' * m.Cc(3) assert(p:match('a') == 2 and p:match('') == 1 and p:match('b') == 1) p = -m.P'a' * m.Cc(10) + #m.P'a' * m.Cc(20) assert(p:match('a') == 20 and p:match('') == 10 and p:match('b') == 10) -- look-behind predicate assert(not m.match(m.B'a', 'a')) assert(m.match(1 * m.B'a', 'a') == 2) assert(not m.match(m.B(1), 'a')) assert(m.match(1 * m.B(1), 'a') == 2) assert(m.match(-m.B(1), 'a') == 1) assert(m.match(m.B(250), string.rep('a', 250)) == nil) assert(m.match(250 * m.B(250), string.rep('a', 250)) == 251) -- look-behind with an open call checkerr("pattern may not have fixed length", m.B, m.V'S1') checkerr("too long to look behind", m.B, 260) B = #letter * -m.B(letter) + -letter * m.B(letter) x = m.Ct({ (B * m.Cp())^-1 * (1 * m.V(1) + m.P(true)) }) checkeq(m.match(x, 'ar cal c'), {1,3,4,7,9,10}) checkeq(m.match(x, ' ar cal '), {2,4,5,8}) checkeq(m.match(x, ' '), {}) checkeq(m.match(x, 'aloalo'), {1,7}) assert(m.match(B, "a") == 1) assert(m.match(1 * B, "a") == 2) assert(not m.B(1 - letter):match("")) assert((-m.B(letter)):match("") == 1) assert((4 * m.B(letter, 4)):match("aaaaaaaa") == 5) assert(not (4 * m.B(#letter * 5)):match("aaaaaaaa")) assert((4 * -m.B(#letter * 5)):match("aaaaaaaa") == 5) -- look-behind with grammars assert(m.match('a' * m.B{'x', x = m.P(3)}, 'aaa') == nil) assert(m.match('aa' * m.B{'x', x = m.P('aaa')}, 'aaaa') == nil) assert(m.match('aaa' * m.B{'x', x = m.P('aaa')}, 'aaaaa') == 4) -- bug in 0.9 assert(m.match(('a' * #m.P'b'), "ab") == 2) assert(not m.match(('a' * #m.P'b'), "a")) assert(not m.match(#m.S'567', "")) assert(m.match(#m.S'567' * 1, "6") == 2) -- tests for Tail Calls p = m.P{ 'a' * m.V(1) + '' } assert(p:match(string.rep('a', 1000)) == 1001) -- create a grammar for a simple DFA for even number of 0s and 1s -- -- ->1 <---0---> 2 -- ^ ^ -- | | -- 1 1 -- | | -- V V -- 3 <---0---> 4 -- -- this grammar should keep no backtracking information p = m.P{ [1] = '0' * m.V(2) + '1' * m.V(3) + -1, [2] = '0' * m.V(1) + '1' * m.V(4), [3] = '0' * m.V(4) + '1' * m.V(1), [4] = '0' * m.V(3) + '1' * m.V(2), } assert(p:match(string.rep("00", 10000))) assert(p:match(string.rep("01", 10000))) assert(p:match(string.rep("011", 10000))) assert(not p:match(string.rep("011", 10000) .. "1")) assert(not p:match(string.rep("011", 10001))) -- this grammar does need backtracking info. local lim = 10000 p = m.P{ '0' * m.V(1) + '0' } checkerr("stack overflow", m.match, p, string.rep("0", lim)) m.setmaxstack(2*lim) checkerr("stack overflow", m.match, p, string.rep("0", lim)) m.setmaxstack(2*lim + 4) assert(m.match(p, string.rep("0", lim)) == lim + 1) -- this repetition should not need stack space (only the call does) p = m.P{ ('a' * m.V(1))^0 * 'b' + 'c' } m.setmaxstack(200) assert(p:match(string.rep('a', 180) .. 'c' .. string.rep('b', 180)) == 362) m.setmaxstack(100) -- restore low limit -- tests for optional start position assert(m.match("a", "abc", 1)) assert(m.match("b", "abc", 2)) assert(m.match("c", "abc", 3)) assert(not m.match(1, "abc", 4)) assert(m.match("a", "abc", -3)) assert(m.match("b", "abc", -2)) assert(m.match("c", "abc", -1)) assert(m.match("abc", "abc", -4)) -- truncate to position 1 assert(m.match("", "abc", 10)) -- empty string is everywhere! assert(m.match("", "", 10)) assert(not m.match(1, "", 1)) assert(not m.match(1, "", -1)) assert(not m.match(1, "", 0)) print("+") -- tests for argument captures checkerr("invalid argument", m.Carg, 0) checkerr("invalid argument", m.Carg, -1) checkerr("invalid argument", m.Carg, 2^18) checkerr("absent extra argument #1", m.match, m.Carg(1), 'a', 1) assert(m.match(m.Carg(1), 'a', 1, print) == print) x = {m.match(m.Carg(1) * m.Carg(2), '', 1, 10, 20)} checkeq(x, {10, 20}) assert(m.match(m.Cmt(m.Cg(m.Carg(3), "a") * m.Cmt(m.Cb("a"), function (s,i,x) assert(s == "a" and i == 1); return i, x+1 end) * m.Carg(2), function (s,i,a,b,c) assert(s == "a" and i == 1 and c == nil); return i, 2*a + 3*b end) * "a", "a", 1, false, 100, 1000) == 2*1001 + 3*100) -- tests for Lua functions t = {} s = "" p = m.P(function (s1, i) assert(s == s1); t[#t + 1] = i; return nil end) * false s = "hi, this is a test" assert(m.match(((p - m.P(-1)) + 2)^0, s) == string.len(s) + 1) assert(#t == string.len(s)/2 and t[1] == 1 and t[2] == 3) assert(not m.match(p, s)) p = mt.__add(function (s, i) return i end, function (s, i) return nil end) assert(m.match(p, "alo")) p = mt.__mul(function (s, i) return i end, function (s, i) return nil end) assert(not m.match(p, "alo")) t = {} p = function (s1, i) assert(s == s1); t[#t + 1] = i; return i end s = "hi, this is a test" assert(m.match((m.P(1) * p)^0, s) == string.len(s) + 1) assert(#t == string.len(s) and t[1] == 2 and t[2] == 3) t = {} p = m.P(function (s1, i) assert(s == s1); t[#t + 1] = i; return i <= s1:len() and i end) * 1 s = "hi, this is a test" assert(m.match(p^0, s) == string.len(s) + 1) assert(#t == string.len(s) + 1 and t[1] == 1 and t[2] == 2) p = function (s1, i) return m.match(m.P"a"^1, s1, i) end assert(m.match(p, "aaaa") == 5) assert(m.match(p, "abaa") == 2) assert(not m.match(p, "baaa")) checkerr("invalid position", m.match, function () return 2^20 end, s) checkerr("invalid position", m.match, function () return 0 end, s) checkerr("invalid position", m.match, function (s, i) return i - 1 end, s) checkerr("invalid position", m.match, m.P(1)^0 * function (_, i) return i - 1 end, s) assert(m.match(m.P(1)^0 * function (_, i) return i end * -1, s)) checkerr("invalid position", m.match, m.P(1)^0 * function (_, i) return i + 1 end, s) assert(m.match(m.P(function (s, i) return s:len() + 1 end) * -1, s)) checkerr("invalid position", m.match, m.P(function (s, i) return s:len() + 2 end) * -1, s) assert(not m.match(m.P(function (s, i) return s:len() end) * -1, s)) assert(m.match(m.P(1)^0 * function (_, i) return true end, s) == string.len(s) + 1) for i = 1, string.len(s) + 1 do assert(m.match(function (_, _) return i end, s) == i) end p = (m.P(function (s, i) return i%2 == 0 and i end) * 1 + m.P(function (s, i) return i%2 ~= 0 and i + 2 <= s:len() and i end) * 3)^0 * -1 assert(p:match(string.rep('a', 14000))) -- tests for Function Replacements f = function (a, ...) if a ~= "x" then return {a, ...} end end t = m.match(m.C(1)^0/f, "abc") checkeq(t, {"a", "b", "c"}) t = m.match(m.C(1)^0/f/f, "abc") checkeq(t, {{"a", "b", "c"}}) t = m.match(m.P(1)^0/f/f, "abc") -- no capture checkeq(t, {{"abc"}}) t = m.match((m.P(1)^0/f * m.Cp())/f, "abc") checkeq(t, {{"abc"}, 4}) t = m.match((m.C(1)^0/f * m.Cp())/f, "abc") checkeq(t, {{"a", "b", "c"}, 4}) t = m.match((m.C(1)^0/f * m.Cp())/f, "xbc") checkeq(t, {4}) t = m.match(m.C(m.C(1)^0)/f, "abc") checkeq(t, {"abc", "a", "b", "c"}) g = function (...) return 1, ... end t = {m.match(m.C(1)^0/g/g, "abc")} checkeq(t, {1, 1, "a", "b", "c"}) t = {m.match(m.Cc(nil,nil,4) * m.Cc(nil,3) * m.Cc(nil, nil) / g / g, "")} t1 = {1,1,nil,nil,4,nil,3,nil,nil} for i=1,10 do assert(t[i] == t1[i]) end -- bug in 0.12.2: ktable with only nil could be eliminated when joining -- with a pattern without ktable assert((m.P"aaa" * m.Cc(nil)):match"aaa" == nil) t = {m.match((m.C(1) / function (x) return x, x.."x" end)^0, "abc")} checkeq(t, {"a", "ax", "b", "bx", "c", "cx"}) t = m.match(m.Ct((m.C(1) / function (x,y) return y, x end * m.Cc(1))^0), "abc") checkeq(t, {nil, "a", 1, nil, "b", 1, nil, "c", 1}) -- tests for Query Replacements assert(m.match(m.C(m.C(1)^0)/{abc = 10}, "abc") == 10) assert(m.match(m.C(1)^0/{a = 10}, "abc") == 10) assert(m.match(m.S("ba")^0/{ab = 40}, "abc") == 40) t = m.match(m.Ct((m.S("ba")/{a = 40})^0), "abc") checkeq(t, {40}) assert(m.match(m.Cs((m.C(1)/{a=".", d=".."})^0), "abcdde") == ".bc....e") assert(m.match(m.Cs((m.C(1)/{f="."})^0), "abcdde") == "abcdde") assert(m.match(m.Cs((m.C(1)/{d="."})^0), "abcdde") == "abc..e") assert(m.match(m.Cs((m.C(1)/{e="."})^0), "abcdde") == "abcdd.") assert(m.match(m.Cs((m.C(1)/{e=".", f="+"})^0), "eefef") == "..+.+") assert(m.match(m.Cs((m.C(1))^0), "abcdde") == "abcdde") assert(m.match(m.Cs(m.C(m.C(1)^0)), "abcdde") == "abcdde") assert(m.match(1 * m.Cs(m.P(1)^0), "abcdde") == "bcdde") assert(m.match(m.Cs((m.C('0')/'x' + 1)^0), "abcdde") == "abcdde") assert(m.match(m.Cs((m.C('0')/'x' + 1)^0), "0ab0b0") == "xabxbx") assert(m.match(m.Cs((m.C('0')/'x' + m.P(1)/{b=3})^0), "b0a0b") == "3xax3") assert(m.match(m.P(1)/'%0%0'/{aa = -3} * 'x', 'ax') == -3) assert(m.match(m.C(1)/'%0%1'/{aa = 'z'}/{z = -3} * 'x', 'ax') == -3) assert(m.match(m.Cs(m.Cc(0) * (m.P(1)/"")), "4321") == "0") assert(m.match(m.Cs((m.P(1) / "%0")^0), "abcd") == "abcd") assert(m.match(m.Cs((m.P(1) / "%0.%0")^0), "abcd") == "a.ab.bc.cd.d") assert(m.match(m.Cs((m.P("a") / "%0.%0" + 1)^0), "abcad") == "a.abca.ad") assert(m.match(m.C("a") / "%1%%%0", "a") == "a%a") assert(m.match(m.Cs((m.P(1) / ".xx")^0), "abcd") == ".xx.xx.xx.xx") assert(m.match(m.Cp() * m.P(3) * m.Cp()/"%2%1%1 - %0 ", "abcde") == "411 - abc ") assert(m.match(m.P(1)/"%0", "abc") == "a") checkerr("invalid capture index", m.match, m.P(1)/"%1", "abc") checkerr("invalid capture index", m.match, m.P(1)/"%9", "abc") p = m.C(1) p = p * p; p = p * p; p = p * p * m.C(1) / "%9 - %1" assert(p:match("1234567890") == "9 - 1") assert(m.match(m.Cc(print), "") == print) -- too many captures (just ignore extra ones) p = m.C(1)^0 / "%2-%9-%0-%9" assert(p:match"01234567890123456789" == "1-8-01234567890123456789-8") s = string.rep("12345678901234567890", 20) assert(m.match(m.C(1)^0 / "%9-%1-%0-%3", s) == "9-1-" .. s .. "-3") -- string captures with non-string subcaptures p = m.Cc('alo') * m.C(1) / "%1 - %2 - %1" assert(p:match'x' == 'alo - x - alo') checkerr("invalid capture value (a boolean)", m.match, m.Cc(true) / "%1", "a") -- long strings for string capture l = 10000 s = string.rep('a', l) .. string.rep('b', l) .. string.rep('c', l) p = (m.C(m.P'a'^1) * m.C(m.P'b'^1) * m.C(m.P'c'^1)) / '%3%2%1' assert(p:match(s) == string.rep('c', l) .. string.rep('b', l) .. string.rep('a', l)) print"+" -- accumulator capture function f (x) return x + 1 end assert(m.match(m.Cf(m.Cc(0) * m.C(1)^0, f), "alo alo") == 7) assert(m.match(m.Cc(0) * (m.C(1) % f)^0, "alo alo") == 7) t = {m.match(m.Cf(m.Cc(1,2,3), error), "")} checkeq(t, {1}) p = m.Cf(m.Ct(true) * m.Cg(m.C(m.R"az"^1) * "=" * m.C(m.R"az"^1) * ";")^0, rawset) t = p:match("a=b;c=du;xux=yuy;") checkeq(t, {a="b", c="du", xux="yuy"}) -- errors in fold capture -- no initial capture checkerr("no initial value", m.match, m.Cf(m.P(5), print), 'aaaaaa') -- no initial capture (very long match forces fold to be a pair open-close) checkerr("no initial value", m.match, m.Cf(m.P(500), print), string.rep('a', 600)) -- errors in accumulator capture -- no initial capture checkerr("no previous value", m.match, m.P(5) % print, 'aaaaaa') -- no initial capture (very long match forces fold to be a pair open-close) checkerr("no previous value", m.match, m.P(500) % print, string.rep('a', 600)) -- tests for loop checker local function isnullable (p) checkerr("may accept empty string", function (p) return p^0 end, m.P(p)) end isnullable(m.P("x")^-4) assert(m.match(((m.P(0) + 1) * m.S"al")^0, "alo") == 3) assert(m.match((("x" + #m.P(1))^-4 * m.S"al")^0, "alo") == 3) isnullable("") isnullable(m.P("x")^0) isnullable(m.P("x")^-1) isnullable(m.P("x") + 1 + 2 + m.P("a")^-1) isnullable(-m.P("ab")) isnullable(- -m.P("ab")) isnullable(# #(m.P("ab") + "xy")) isnullable(- #m.P("ab")^0) isnullable(# -m.P("ab")^1) isnullable(#m.V(3)) isnullable(m.V(3) + m.V(1) + m.P('a')^-1) isnullable({[1] = m.V(2) * m.V(3), [2] = m.V(3), [3] = m.P(0)}) assert(m.match(m.P{[1] = m.V(2) * m.V(3), [2] = m.V(3), [3] = m.P(1)}^0, "abc") == 3) assert(m.match(m.P""^-3, "a") == 1) local function find (p, s) return m.match(basiclookfor(p), s) end local function badgrammar (g, expected) local stat, msg = pcall(m.P, g) assert(not stat) if expected then assert(find(expected, msg)) end end badgrammar({[1] = m.V(1)}, "rule '1'") badgrammar({[1] = m.V(2)}, "rule '2'") -- invalid non-terminal badgrammar({[1] = m.V"x"}, "rule 'x'") -- invalid non-terminal badgrammar({[1] = m.V{}}, "rule '(a table)'") -- invalid non-terminal badgrammar({[1] = #m.P("a") * m.V(1)}, "rule '1'") -- left-recursive badgrammar({[1] = -m.P("a") * m.V(1)}, "rule '1'") -- left-recursive badgrammar({[1] = -1 * m.V(1)}, "rule '1'") -- left-recursive badgrammar({[1] = -1 + m.V(1)}, "rule '1'") -- left-recursive badgrammar({[1] = 1 * m.V(2), [2] = m.V(2)}, "rule '2'") -- left-recursive badgrammar({[1] = 1 * m.V(2)^0, [2] = m.P(0)}, "rule '1'") -- inf. loop badgrammar({ m.V(2), m.V(3)^0, m.P"" }, "rule '2'") -- inf. loop badgrammar({ m.V(2) * m.V(3)^0, m.V(3)^0, m.P"" }, "rule '1'") -- inf. loop badgrammar({"x", x = #(m.V(1) * 'a') }, "rule '1'") -- inf. loop badgrammar({ -(m.V(1) * 'a') }, "rule '1'") -- inf. loop badgrammar({"x", x = m.P'a'^-1 * m.V"x"}, "rule 'x'") -- left recursive badgrammar({"x", x = m.P'a' * m.V"y"^1, y = #m.P(1)}, "rule 'x'") assert(m.match({'a' * -m.V(1)}, "aaa") == 2) assert(m.match({'a' * -m.V(1)}, "aaaa") == nil) -- good x bad grammars m.P{ ('a' * m.V(1))^-1 } m.P{ -('a' * m.V(1)) } m.P{ ('abc' * m.V(1))^-1 } m.P{ -('abc' * m.V(1)) } badgrammar{ #m.P('abc') * m.V(1) } badgrammar{ -('a' + m.V(1)) } m.P{ #('a' * m.V(1)) } badgrammar{ #('a' + m.V(1)) } m.P{ m.B{ m.P'abc' } * 'a' * m.V(1) } badgrammar{ m.B{ m.P'abc' } * m.V(1) } badgrammar{ ('a' + m.P'bcd')^-1 * m.V(1) } -- simple tests for maximum sizes: local p = m.P"a" for i=1,14 do p = p * p end p = {} for i=1,100 do p[i] = m.P"a" end p = m.P(p) -- strange values for rule labels p = m.P{ "print", print = m.V(print), [print] = m.V(_G), [_G] = m.P"a", } assert(p:match("a")) -- initial rule g = {} for i = 1, 10 do g["i"..i] = "a" * m.V("i"..i+1) end g.i11 = m.P"" for i = 1, 10 do g[1] = "i"..i local p = m.P(g) assert(p:match("aaaaaaaaaaa") == 11 - i + 1) end print "testing back references" checkerr("back reference 'x' not found", m.match, m.Cb('x'), '') checkerr("back reference 'b' not found", m.match, m.Cg(1, 'a') * m.Cb('b'), 'a') p = m.Cg(m.C(1) * m.C(1), "k") * m.Ct(m.Cb("k")) t = p:match("ab") checkeq(t, {"a", "b"}) do -- some basic cases assert(m.match(m.Cg(m.Cc(3), "a") * m.Cb("a"), "a") == 3) assert(m.match(m.Cg(m.C(1), 133) * m.Cb(133), "X") == "X") -- first reference to 'x' should not see the group enclosing it local p = m.Cg(m.Cb('x'), 'x') * m.Cb('x') checkerr("back reference 'x' not found", m.match, p, '') local p = m.Cg(m.Cb('x') * m.C(1), 'x') * m.Cb('x') checkerr("back reference 'x' not found", m.match, p, 'abc') -- reference to 'x' should not see the group enclosed in another capture local s = string.rep("a", 30) local p = (m.C(1)^-4 * m.Cg(m.C(1), 'x')) / {} * m.Cb('x') checkerr("back reference 'x' not found", m.match, p, s) local p = (m.C(1)^-20 * m.Cg(m.C(1), 'x')) / {} * m.Cb('x') checkerr("back reference 'x' not found", m.match, p, s) -- second reference 'k' should refer to 10 and first ref. 'k' p = m.Cg(m.Cc(20), 'k') * m.Cg(m.Cc(10) * m.Cb('k') * m.C(1), 'k') * (m.Cb('k') / function (a,b,c) return a*10 + b + tonumber(c) end) -- 10 * 10 (Cc) + 20 (Cb) + 7 (C) == 127 assert(p:match("756") == 127) end p = m.P(true) for i = 1, 10 do p = p * m.Cg(1, i) end for i = 1, 10 do local p = p * m.Cb(i) assert(p:match('abcdefghij') == string.sub('abcdefghij', i, i)) end t = {} function foo (p) t[#t + 1] = p; return p .. "x" end p = m.Cg(m.C(2) / foo, "x") * m.Cb"x" * m.Cg(m.Cb('x') / foo, "x") * m.Cb"x" * m.Cg(m.Cb('x') / foo, "x") * m.Cb"x" * m.Cg(m.Cb('x') / foo, "x") * m.Cb"x" x = {p:match'ab'} checkeq(x, {'abx', 'abxx', 'abxxx', 'abxxxx'}) checkeq(t, {'ab', 'ab', 'abx', 'ab', 'abx', 'abxx', 'ab', 'abx', 'abxx', 'abxxx'}) -- tests for match-time captures p = m.P'a' * (function (s, i) return (s:sub(i, i) == 'b') and i + 1 end) + 'acd' assert(p:match('abc') == 3) assert(p:match('acd') == 4) local function id (s, i, ...) return true, ... end do -- run-time capture in an end predicate (should discard its value) local x = 0 function foo (s, i) x = x + 1 return true, x end local p = #(m.Cmt("", foo) * "xx") * m.Cmt("", foo) assert(p:match("xx") == 2) end assert(m.Cmt(m.Cs((m.Cmt(m.S'abc' / { a = 'x', c = 'y' }, id) + m.R'09'^1 / string.char + m.P(1))^0), id):match"acb98+68c" == "xyb\98+\68y") p = m.P{'S', S = m.V'atom' * space + m.Cmt(m.Ct("(" * space * (m.Cmt(m.V'S'^1, id) + m.P(true)) * ")" * space), id), atom = m.Cmt(m.C(m.R("AZ", "az", "09")^1), id) } x = p:match"(a g () ((b) c) (d (e)))" checkeq(x, {'a', 'g', {}, {{'b'}, 'c'}, {'d', {'e'}}}); x = {(m.Cmt(1, id)^0):match(string.rep('a', 500))} assert(#x == 500) local function id(s, i, x) if x == 'a' then return i, 1, 3, 7 else return nil, 2, 4, 6, 8 end end p = ((m.P(id) * 1 + m.Cmt(2, id) * 1 + m.Cmt(1, id) * 1))^0 assert(table.concat{p:match('abababab')} == string.rep('137', 4)) local function ref (s, i, x) return m.match(x, s, i - x:len()) end assert(m.Cmt(m.P(1)^0, ref):match('alo') == 4) assert((m.P(1) * m.Cmt(m.P(1)^0, ref)):match('alo') == 4) assert(not (m.P(1) * m.Cmt(m.C(1)^0, ref)):match('alo')) ref = function (s,i,x) return i == tonumber(x) and i, 'xuxu' end assert(m.Cmt(1, ref):match'2') assert(not m.Cmt(1, ref):match'1') assert(m.Cmt(m.P(1)^0, ref):match'03') function ref (s, i, a, b) if a == b then return i, a:upper() end end p = m.Cmt(m.C(m.R"az"^1) * "-" * m.C(m.R"az"^1), ref) p = (any - p)^0 * p * any^0 * -1 assert(p:match'abbbc-bc ddaa' == 'BC') do -- match-time captures cannot be optimized away local touch = 0 f = m.P(function () touch = touch + 1; return true end) local function check(n) n = n or 1; assert(touch == n); touch = 0 end assert(m.match(f * false + 'b', 'a') == nil); check() assert(m.match(f * false + 'b', '') == nil); check() assert(m.match( (f * 'a')^0 * 'b', 'b') == 2); check() assert(m.match( (f * 'a')^0 * 'b', '') == nil); check() assert(m.match( (f * 'a')^-1 * 'b', 'b') == 2); check() assert(m.match( (f * 'a')^-1 * 'b', '') == nil); check() assert(m.match( ('b' + f * 'a')^-1 * 'b', '') == nil); check() assert(m.match( (m.P'b'^-1 * f * 'a')^-1 * 'b', '') == nil); check() assert(m.match( (-m.P(1) * m.P'b'^-1 * f * 'a')^-1 * 'b', '') == nil); check() assert(m.match( (f * 'a' + 'b')^-1 * 'b', '') == nil); check() assert(m.match(f * 'a' + f * 'b', 'b') == 2); check(2) assert(m.match(f * 'a' + f * 'b', 'a') == 2); check(1) assert(m.match(-f * 'a' + 'b', 'b') == 2); check(1) assert(m.match(-f * 'a' + 'b', '') == nil); check(1) end c = '[' * m.Cg(m.P'='^0, "init") * '[' * { m.Cmt(']' * m.C(m.P'='^0) * ']' * m.Cb("init"), function (_, _, s1, s2) return s1 == s2 end) + 1 * m.V(1) } / 0 assert(c:match'[==[]]====]]]]==]===[]' == 18) assert(c:match'[[]=]====]=]]]==]===[]' == 14) assert(not c:match'[[]=]====]=]=]==]===[]') -- old bug: optimization of concat with fail removed match-time capture p = m.Cmt(0, function (s) p = s end) * m.P(false) assert(not p:match('alo')) assert(p == 'alo') -- ensure that failed match-time captures are not kept on Lua stack do local t = {__mode = "kv"}; setmetatable(t,t) local c = 0 local function foo (s,i) collectgarbage(); assert(next(t) == "__mode" and next(t, "__mode") == nil) local x = {} t[x] = true c = c + 1 return i, x end local p = m.P{ m.Cmt(0, foo) * m.P(false) + m.P(1) * m.V(1) + m.P"" } p:match(string.rep('1', 10)) assert(c == 11) end -- Return a match-time capture that returns 'n' captures local function manyCmt (n) return m.Cmt("a", function () local a = {}; for i = 1, n do a[i] = n - i end return true, unpack(a) end) end -- bug in 1.0: failed match-time that used previous match-time results do local x local function aux (...) x = #{...}; return false end local res = {m.match(m.Cmt(manyCmt(20), aux) + manyCmt(10), "a")} assert(#res == 10 and res[1] == 9 and res[10] == 0) end -- bug in 1.0: problems with math-times returning too many captures if _VERSION >= "Lua 5.2" then local lim = 2^11 - 10 local res = {m.match(manyCmt(lim), "a")} assert(#res == lim and res[1] == lim - 1 and res[lim] == 0) checkerr("too many", m.match, manyCmt(2^15), "a") end p = (m.P(function () return true, "a" end) * 'a' + m.P(function (s, i) return i, "aa", 20 end) * 'b' + m.P(function (s,i) if i <= #s then return i, "aaa" end end) * 1)^0 t = {p:match('abacc')} checkeq(t, {'a', 'aa', 20, 'a', 'aaa', 'aaa'}) do print"testing large grammars" local lim = 1000 -- number of rules local t = {} for i = 3, lim do t[i] = m.V(i - 1) -- each rule calls previous one end t[1] = m.V(lim) -- start on last rule t[2] = m.C("alo") -- final rule local P = m.P(t) -- build grammar assert(P:match("alo") == "alo") t[#t + 1] = m.P("x") -- one more rule... checkerr("too many rules", m.P, t) end print "testing UTF-8 ranges" do -- a few typical UTF-8 ranges local p = m.utfR(0x410, 0x44f)^1 / "cyr: %0" + m.utfR(0x4e00, 0x9fff)^1 / "cjk: %0" + m.utfR(0x1F600, 0x1F64F)^1 / "emot: %0" + m.utfR(0, 0x7f)^1 / "ascii: %0" + m.utfR(0, 0x10ffff) / "other: %0" p = m.Ct(p^0) * -m.P(1) local cyr = "ждюя" local emot = "\240\159\152\128\240\159\153\128" -- 😀🙀 local cjk = "专举乸" local ascii = "alo" local last = "\244\143\191\191" -- U+10FFFF local s = cyr .. "—" .. emot .. "—" .. cjk .. "—" .. ascii .. last t = (p:match(s)) assert(t[1] == "cyr: " .. cyr and t[2] == "other: —" and t[3] == "emot: " .. emot and t[4] == "other: —" and t[5] == "cjk: " .. cjk and t[6] == "other: —" and t[7] == "ascii: " .. ascii and t[8] == "other: " .. last and t[9] == nil) -- failing UTF-8 matches and borders assert(not m.match(m.utfR(10, 0x2000), "\9")) assert(not m.match(m.utfR(10, 0x2000), "\226\128\129")) assert(m.match(m.utfR(10, 0x2000), "\10") == 2) assert(m.match(m.utfR(10, 0x2000), "\226\128\128") == 4) end do -- valid and invalid code points local p = m.utfR(0, 0x10ffff)^0 assert(p:match("汉字\128") == #"汉字" + 1) assert(p:match("\244\159\191") == 1) assert(p:match("\244\159\191\191") == 1) assert(p:match("\255") == 1) -- basic errors checkerr("empty range", m.utfR, 1, 0) checkerr("invalid code point", m.utfR, 1, 0x10ffff + 1) end do -- back references (fixed width) -- match a byte after a CJK point local p = m.B(m.utfR(0x4e00, 0x9fff)) * m.C(1) p = m.P{ p + m.P(1) * m.V(1) } -- search for 'p' assert(p:match("ab д 专X x") == "X") -- match a byte after a hebrew point local p = m.B(m.utfR(0x5d0, 0x5ea)) * m.C(1) p = m.P(#"ש") * p assert(p:match("שX") == "X") checkerr("fixed length", m.B, m.utfR(0, 0x10ffff)) end ------------------------------------------------------------------- -- Tests for 're' module ------------------------------------------------------------------- print"testing 're' module" local re = require "re" local match, compile = re.match, re.compile assert(match("a", ".") == 2) assert(match("a", "''") == 1) assert(match("", " ! . ") == 1) assert(not match("a", " ! . ")) assert(match("abcde", " ( . . ) * ") == 5) assert(match("abbcde", " [a-c] +") == 5) assert(match("0abbc1de", "'0' [a-c]+ '1'") == 7) assert(match("0zz1dda", "'0' [^a-c]+ 'a'") == 8) assert(match("abbc--", " [a-c] + +") == 5) assert(match("abbc--", " [ac-] +") == 2) assert(match("abbc--", " [-acb] + ") == 7) assert(not match("abbcde", " [b-z] + ")) assert(match("abb\"de", '"abb"["]"de"') == 7) assert(match("abceeef", "'ac' ? 'ab' * 'c' { 'e' * } / 'abceeef' ") == "eee") assert(match("abceeef", "'ac'? 'ab'* 'c' { 'f'+ } / 'abceeef' ") == 8) assert(re.match("aaand", "[a]^2") == 3) local t = {match("abceefe", "( ( & 'e' {} ) ? . ) * ")} checkeq(t, {4, 5, 7}) local t = {match("abceefe", "((&&'e' {})? .)*")} checkeq(t, {4, 5, 7}) local t = {match("abceefe", "( ( ! ! 'e' {} ) ? . ) *")} checkeq(t, {4, 5, 7}) local t = {match("abceefe", "(( & ! & ! 'e' {})? .)*")} checkeq(t, {4, 5, 7}) assert(match("cccx" , "'ab'? ('ccc' / ('cde' / 'cd'*)? / 'ccc') 'x'+") == 5) assert(match("cdx" , "'ab'? ('ccc' / ('cde' / 'cd'*)? / 'ccc') 'x'+") == 4) assert(match("abcdcdx" , "'ab'? ('ccc' / ('cde' / 'cd'*)? / 'ccc') 'x'+") == 8) assert(match("abc", "a <- (. a)?") == 4) b = "balanced <- '(' ([^()] / balanced)* ')'" assert(match("(abc)", b)) assert(match("(a(b)((c) (d)))", b)) assert(not match("(a(b ((c) (d)))", b)) b = compile[[ balanced <- "(" ([^()] / balanced)* ")" ]] assert(b == m.P(b)) assert(b:match"((((a))(b)))") local g = [[ S <- "0" B / "1" A / "" -- balanced strings A <- "0" S / "1" A A -- one more 0 B <- "1" S / "0" B B -- one more 1 ]] assert(match("00011011", g) == 9) local g = [[ S <- ("0" B / "1" A)* A <- "0" / "1" A A B <- "1" / "0" B B ]] assert(match("00011011", g) == 9) assert(match("000110110", g) == 9) assert(match("011110110", g) == 3) assert(match("000110010", g) == 1) s = "aaaaaaaaaaaaaaaaaaaaaaaa" assert(match(s, "'a'^3") == 4) assert(match(s, "'a'^0") == 1) assert(match(s, "'a'^+3") == s:len() + 1) assert(not match(s, "'a'^+30")) assert(match(s, "'a'^-30") == s:len() + 1) assert(match(s, "'a'^-5") == 6) for i = 1, s:len() do assert(match(s, string.format("'a'^+%d", i)) >= i + 1) assert(match(s, string.format("'a'^-%d", i)) <= i + 1) assert(match(s, string.format("'a'^%d", i)) == i + 1) end assert(match("01234567890123456789", "[0-9]^3+") == 19) assert(match("01234567890123456789", "({....}{...}) -> '%2%1'") == "4560123") t = match("0123456789", "{| {.}* |}") checkeq(t, {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}) assert(match("012345", "{| (..) -> '%0%0' |}")[1] == "0101") assert(match("abcdef", "( {.} {.} {.} {.} {.} ) -> 3") == "c") assert(match("abcdef", "( {:x: . :} {.} {.} {.} {.} ) -> 3") == "d") assert(match("abcdef", "( {:x: . :} {.} {.} {.} {.} ) -> 0") == 6) assert(not match("abcdef", "{:x: ({.} {.} {.}) -> 2 :} =x")) assert(match("abcbef", "{:x: ({.} {.} {.}) -> 2 :} =x")) eqcharset(compile"[]]", "]") eqcharset(compile"[][]", m.S"[]") eqcharset(compile"[]-]", m.S"-]") eqcharset(compile"[-]", m.S"-") eqcharset(compile"[az-]", m.S"a-z") eqcharset(compile"[-az]", m.S"a-z") eqcharset(compile"[a-z]", m.R"az") eqcharset(compile"[]['\"]", m.S[[]['"]]) eqcharset(compile"[^]]", any - "]") eqcharset(compile"[^][]", any - m.S"[]") eqcharset(compile"[^]-]", any - m.S"-]") eqcharset(compile"[^]-]", any - m.S"-]") eqcharset(compile"[^-]", any - m.S"-") eqcharset(compile"[^az-]", any - m.S"a-z") eqcharset(compile"[^-az]", any - m.S"a-z") eqcharset(compile"[^a-z]", any - m.R"az") eqcharset(compile"[^]['\"]", any - m.S[[]['"]]) -- tests for comments in 're' e = compile[[ A <- _B -- \t \n %nl .<> <- -> -- _B <- 'x' --]] assert(e:match'xy' == 2) -- tests for 're' with pre-definitions defs = {digits = m.R"09", letters = m.R"az", _=m.P"__"} e = compile("%letters (%letters / %digits)*", defs) assert(e:match"x123" == 5) e = compile("%_", defs) assert(e:match"__" == 3) e = compile([[ S <- A+ A <- %letters+ B B <- %digits+ ]], defs) e = compile("{[0-9]+'.'?[0-9]*} -> sin", math) assert(e:match("2.34") == math.sin(2.34)) e = compile("'pi' -> math", _G) assert(e:match("pi") == math.pi) e = compile("[ ]* 'version' -> _VERSION", _G) assert(e:match(" version") == _VERSION) function eq (_, _, a, b) return a == b end c = re.compile([[ longstring <- '[' {:init: '='* :} '[' close close <- ']' =init ']' / . close ]]) assert(c:match'[==[]]===]]]]==]===[]' == 17) assert(c:match'[[]=]====]=]]]==]===[]' == 14) assert(not c:match'[[]=]====]=]=]==]===[]') c = re.compile" '[' {:init: '='* :} '[' (!(']' =init ']') .)* ']' =init ']' !. " assert(c:match'[==[]]===]]]]==]') assert(c:match'[[]=]====]=][]==]===[]]') assert(not c:match'[[]=]====]=]=]==]===[]') assert(re.find("hi alalo", "{:x:..:} =x") == 4) assert(re.find("hi alalo", "{:x:..:} =x", 4) == 4) assert(not re.find("hi alalo", "{:x:..:} =x", 5)) assert(re.find("hi alalo", "{'al'}", 5) == 6) assert(re.find("hi aloalolo", "{:x:..:} =x") == 8) assert(re.find("alo alohi x x", "{:word:%w+:}%W*(=word)!%w") == 11) -- re.find discards any captures local a,b,c = re.find("alo", "{.}{'o'}") assert(a == 2 and b == 3 and c == nil) local function match (s,p) local i,e = re.find(s,p) if i then return s:sub(i, e) end end assert(match("alo alo", '[a-z]+') == "alo") assert(match("alo alo", '{:x: [a-z]+ :} =x') == nil) assert(match("alo alo", "{:x: [a-z]+ :} ' ' =x") == "alo alo") assert(re.gsub("alo alo", "[abc]", "x") == "xlo xlo") assert(re.gsub("alo alo", "%w+", ".") == ". .") assert(re.gsub("hi, how are you", "[aeiou]", string.upper) == "hI, hOw ArE yOU") s = 'hi [[a comment[=]=] ending here]] and [=[another]]=]]' c = re.compile" '[' {:i: '='* :} '[' (!(']' =i ']') .)* ']' { =i } ']' " assert(re.gsub(s, c, "%2") == 'hi and =]') assert(re.gsub(s, c, "%0") == s) assert(re.gsub('[=[hi]=]', c, "%2") == '=') assert(re.find("", "!.") == 1) assert(re.find("alo", "!.") == 4) function addtag (s, i, t, tag) t.tag = tag; return i, t end c = re.compile([[ doc <- block !. block <- (start {| (block / { [^<]+ })* |} end?) => addtag start <- '<' {:tag: [a-z]+ :} '>' end <- '' ]], {addtag = addtag}) x = c:match[[ hihellobuttotheend]] checkeq(x, {tag='x', 'hi', {tag = 'b', 'hello'}, 'but', {'totheend'}}) -- test for folding captures c = re.compile([[ S <- (number (%s+ number)*) ~> add number <- %d+ -> tonumber ]], {tonumber = tonumber, add = function (a,b) return a + b end}) assert(c:match("3 401 50") == 3 + 401 + 50) -- test for accumulator captures c = re.compile([[ S <- number (%s+ number >> add)* number <- %d+ -> tonumber ]], {tonumber = tonumber, add = function (a,b) return a + b end}) assert(c:match("3 401 50") == 3 + 401 + 50) -- tests for look-ahead captures x = {re.match("alo", "&(&{.}) !{'b'} {&(...)} &{..} {...} {!.}")} checkeq(x, {"", "alo", ""}) assert(re.match("aloalo", "{~ (((&'al' {.}) -> 'A%1' / (&%l {.}) -> '%1%1') / .)* ~}") == "AallooAalloo") -- bug in 0.9 (and older versions), due to captures in look-aheads x = re.compile[[ {~ (&(. ([a-z]* -> '*')) ([a-z]+ -> '+') ' '*)* ~} ]] assert(x:match"alo alo" == "+ +") -- valid capture in look-ahead (used inside the look-ahead itself) x = re.compile[[ S <- &({:two: .. :} . =two) {[a-z]+} / . S ]] assert(x:match("hello aloaLo aloalo xuxu") == "aloalo") p = re.compile[[ block <- {| {:ident:space*:} line ((=ident !space line) / &(=ident space) block)* |} line <- {[^%nl]*} %nl space <- '_' -- should be ' ', but '_' is simpler for editors ]] t= p:match[[ 1 __1.1 __1.2 ____1.2.1 ____ 2 __2.1 ]] checkeq(t, {"1", {"1.1", "1.2", {"1.2.1", "", ident = "____"}, ident = "__"}, "2", {"2.1", ident = "__"}, ident = ""}) -- nested grammars p = re.compile[[ s <- a b !. b <- ( x <- ('b' x)? ) a <- ( x <- 'a' x? ) ]] assert(p:match'aaabbb') assert(p:match'aaa') assert(not p:match'bbb') assert(not p:match'aaabbba') -- testing groups t = {re.match("abc", "{:S <- {:.:} {S} / '':}")} checkeq(t, {"a", "bc", "b", "c", "c", ""}) t = re.match("1234", "{| {:a:.:} {:b:.:} {:c:.{.}:} |}") checkeq(t, {a="1", b="2", c="4"}) t = re.match("1234", "{|{:a:.:} {:b:{.}{.}:} {:c:{.}:}|}") checkeq(t, {a="1", b="2", c="4"}) t = re.match("12345", "{| {:.:} {:b:{.}{.}:} {:{.}{.}:} |}") checkeq(t, {"1", b="2", "4", "5"}) t = re.match("12345", "{| {:.:} {:{:b:{.}{.}:}:} {:{.}{.}:} |}") checkeq(t, {"1", "23", "4", "5"}) t = re.match("12345", "{| {:.:} {{:b:{.}{.}:}} {:{.}{.}:} |}") checkeq(t, {"1", "23", "4", "5"}) -- testing pre-defined names assert(os.setlocale("C") == "C") function eqlpeggsub (p1, p2) local s1 = cs2str(re.compile(p1)) local s2 = string.gsub(allchar, "[^" .. p2 .. "]", "") -- if s1 ~= s2 then print(#s1,#s2) end assert(s1 == s2) end eqlpeggsub("%w", "%w") eqlpeggsub("%a", "%a") eqlpeggsub("%l", "%l") eqlpeggsub("%u", "%u") eqlpeggsub("%p", "%p") eqlpeggsub("%d", "%d") eqlpeggsub("%x", "%x") eqlpeggsub("%s", "%s") eqlpeggsub("%c", "%c") eqlpeggsub("%W", "%W") eqlpeggsub("%A", "%A") eqlpeggsub("%L", "%L") eqlpeggsub("%U", "%U") eqlpeggsub("%P", "%P") eqlpeggsub("%D", "%D") eqlpeggsub("%X", "%X") eqlpeggsub("%S", "%S") eqlpeggsub("%C", "%C") eqlpeggsub("[%w]", "%w") eqlpeggsub("[_%w]", "_%w") eqlpeggsub("[^%w]", "%W") eqlpeggsub("[%W%S]", "%W%S") re.updatelocale() -- testing nested substitutions x string captures p = re.compile[[ text <- {~ item* ~} item <- macro / [^()] / '(' item* ')' arg <- ' '* {~ (!',' item)* ~} args <- '(' arg (',' arg)* ')' macro <- ('apply' args) -> '%1(%2)' / ('add' args) -> '%1 + %2' / ('mul' args) -> '%1 * %2' ]] assert(p:match"add(mul(a,b), apply(f,x))" == "a * b + f(x)") rev = re.compile[[ R <- (!.) -> '' / ({.} R) -> '%2%1']] assert(rev:match"0123456789" == "9876543210") -- testing error messages in re local function errmsg (p, err) checkerr(err, re.compile, p) end errmsg('aaaa', "rule 'aaaa'") errmsg('a', 'outside') errmsg('b <- a', 'undefined') errmsg("x <- 'a' x <- 'b'", 'already defined') errmsg("'a' -", "near '-'") print"OK" ================================================ FILE: 3rd/lua/README ================================================ This is a modify version of lua 5.5 . For detail , Shared Proto : http://lua-users.org/lists/lua-l/2014-03/msg00489.html Shared short string : https://blog.codingnow.com/2019/06/string_comparison.html ================================================ FILE: 3rd/lua/README.md ================================================ # Lua This is the repository of Lua development code, as seen by the Lua team. It contains the full history of all commits but is mirrored irregularly. For complete information about Lua, visit [Lua.org](https://www.lua.org/). Please **do not** send pull requests. To report issues, post a message to the [Lua mailing list](https://www.lua.org/lua-l.html). Download official Lua releases from [Lua.org](https://www.lua.org/download.html). ================================================ FILE: 3rd/lua/lapi.c ================================================ /* ** $Id: lapi.c $ ** Lua API ** See Copyright Notice in lua.h */ #define lapi_c #define LUA_CORE #include "lprefix.h" #include #include #include #include "lua.h" #include "lapi.h" #include "ldebug.h" #include "ldo.h" #include "lfunc.h" #include "lgc.h" #include "lmem.h" #include "lobject.h" #include "lstate.h" #include "lstring.h" #include "ltable.h" #include "ltm.h" #include "lundump.h" #include "lvm.h" const char lua_ident[] = "$LuaVersion: " LUA_COPYRIGHT " $" "$LuaAuthors: " LUA_AUTHORS " $"; /* ** Test for a valid index (one that is not the 'nilvalue'). */ #define isvalid(L, o) ((o) != &G(L)->nilvalue) /* test for pseudo index */ #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) /* test for upvalue */ #define isupvalue(i) ((i) < LUA_REGISTRYINDEX) /* ** Convert an acceptable index to a pointer to its respective value. ** Non-valid indices return the special nil value 'G(L)->nilvalue'. */ static TValue *index2value (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { StkId o = ci->func.p + idx; api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index"); if (o >= L->top.p) return &G(L)->nilvalue; else return s2v(o); } else if (!ispseudo(idx)) { /* negative index */ api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1), "invalid index"); return s2v(L->top.p + idx); } else if (idx == LUA_REGISTRYINDEX) return &G(L)->l_registry; else { /* upvalues */ idx = LUA_REGISTRYINDEX - idx; api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); if (ttisCclosure(s2v(ci->func.p))) { /* C closure? */ CClosure *func = clCvalue(s2v(ci->func.p)); return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue; } else { /* light C function or Lua function (through a hook)?) */ api_check(L, ttislcf(s2v(ci->func.p)), "caller not a C function"); return &G(L)->nilvalue; /* no upvalues */ } } } /* ** Convert a valid actual index (not a pseudo-index) to its address. */ static StkId index2stack (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { StkId o = ci->func.p + idx; api_check(L, o < L->top.p, "invalid index"); return o; } else { /* non-positive index */ api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1), "invalid index"); api_check(L, !ispseudo(idx), "invalid index"); return L->top.p + idx; } } LUA_API int lua_checkstack (lua_State *L, int n) { int res; CallInfo *ci; lua_lock(L); ci = L->ci; api_check(L, n >= 0, "negative 'n'"); if (L->stack_last.p - L->top.p > n) /* stack large enough? */ res = 1; /* yes; check is OK */ else /* need to grow stack */ res = luaD_growstack(L, n, 0); if (res && ci->top.p < L->top.p + n) ci->top.p = L->top.p + n; /* adjust frame top */ lua_unlock(L); return res; } LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { int i; if (from == to) return; lua_lock(to); api_checkpop(from, n); api_check(from, G(from) == G(to), "moving among independent states"); api_check(from, to->ci->top.p - to->top.p >= n, "stack overflow"); from->top.p -= n; for (i = 0; i < n; i++) { setobjs2s(to, to->top.p, from->top.p + i); to->top.p++; /* stack already checked by previous 'api_check' */ } lua_unlock(to); } LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { lua_CFunction old; lua_lock(L); old = G(L)->panic; G(L)->panic = panicf; lua_unlock(L); return old; } LUA_API lua_Number lua_version (lua_State *L) { UNUSED(L); return LUA_VERSION_NUM; } /* ** basic stack manipulation */ /* ** convert an acceptable stack index into an absolute index */ LUA_API int lua_absindex (lua_State *L, int idx) { return (idx > 0 || ispseudo(idx)) ? idx : cast_int(L->top.p - L->ci->func.p) + idx; } LUA_API int lua_gettop (lua_State *L) { return cast_int(L->top.p - (L->ci->func.p + 1)); } LUA_API void lua_settop (lua_State *L, int idx) { CallInfo *ci; StkId func, newtop; ptrdiff_t diff; /* difference for new top */ lua_lock(L); ci = L->ci; func = ci->func.p; if (idx >= 0) { api_check(L, idx <= ci->top.p - (func + 1), "new top too large"); diff = ((func + 1) + idx) - L->top.p; for (; diff > 0; diff--) setnilvalue(s2v(L->top.p++)); /* clear new slots */ } else { api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top"); diff = idx + 1; /* will "subtract" index (as it is negative) */ } newtop = L->top.p + diff; if (diff < 0 && L->tbclist.p >= newtop) { lua_assert(ci->callstatus & CIST_TBC); newtop = luaF_close(L, newtop, CLOSEKTOP, 0); } L->top.p = newtop; /* correct top only after closing any upvalue */ lua_unlock(L); } LUA_API void lua_closeslot (lua_State *L, int idx) { StkId level; lua_lock(L); level = index2stack(L, idx); api_check(L, (L->ci->callstatus & CIST_TBC) && (L->tbclist.p == level), "no variable to close at given level"); level = luaF_close(L, level, CLOSEKTOP, 0); setnilvalue(s2v(level)); lua_unlock(L); } /* ** Reverse the stack segment from 'from' to 'to' ** (auxiliary to 'lua_rotate') ** Note that we move(copy) only the value inside the stack. ** (We do not move additional fields that may exist.) */ static void reverse (lua_State *L, StkId from, StkId to) { for (; from < to; from++, to--) { TValue temp; setobj(L, &temp, s2v(from)); setobjs2s(L, from, to); setobj2s(L, to, &temp); } } /* ** Let x = AB, where A is a prefix of length 'n'. Then, ** rotate x n == BA. But BA == (A^r . B^r)^r. */ LUA_API void lua_rotate (lua_State *L, int idx, int n) { StkId p, t, m; lua_lock(L); t = L->top.p - 1; /* end of stack segment being rotated */ p = index2stack(L, idx); /* start of segment */ api_check(L, L->tbclist.p < p, "moving a to-be-closed slot"); api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ reverse(L, p, m); /* reverse the prefix with length 'n' */ reverse(L, m + 1, t); /* reverse the suffix */ reverse(L, p, t); /* reverse the entire segment */ lua_unlock(L); } LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { TValue *fr, *to; lua_lock(L); fr = index2value(L, fromidx); to = index2value(L, toidx); api_check(L, isvalid(L, to), "invalid index"); setobj(L, to, fr); if (isupvalue(toidx)) /* function upvalue? */ luaC_barrier(L, clCvalue(s2v(L->ci->func.p)), fr); /* LUA_REGISTRYINDEX does not need gc barrier (collector revisits it before finishing collection) */ lua_unlock(L); } LUA_API void lua_pushvalue (lua_State *L, int idx) { lua_lock(L); setobj2s(L, L->top.p, index2value(L, idx)); api_incr_top(L); lua_unlock(L); } /* ** access functions (stack -> C) */ LUA_API int lua_type (lua_State *L, int idx) { const TValue *o = index2value(L, idx); return (isvalid(L, o) ? ttype(o) : LUA_TNONE); } LUA_API const char *lua_typename (lua_State *L, int t) { UNUSED(L); api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type"); return ttypename(t); } LUA_API int lua_iscfunction (lua_State *L, int idx) { const TValue *o = index2value(L, idx); return (ttislcf(o) || (ttisCclosure(o))); } LUA_API int lua_isinteger (lua_State *L, int idx) { const TValue *o = index2value(L, idx); return ttisinteger(o); } LUA_API int lua_isnumber (lua_State *L, int idx) { lua_Number n; const TValue *o = index2value(L, idx); return tonumber(o, &n); } LUA_API int lua_isstring (lua_State *L, int idx) { const TValue *o = index2value(L, idx); return (ttisstring(o) || cvt2str(o)); } LUA_API int lua_isuserdata (lua_State *L, int idx) { const TValue *o = index2value(L, idx); return (ttisfulluserdata(o) || ttislightuserdata(o)); } LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { const TValue *o1 = index2value(L, index1); const TValue *o2 = index2value(L, index2); return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0; } LUA_API void lua_arith (lua_State *L, int op) { lua_lock(L); if (op != LUA_OPUNM && op != LUA_OPBNOT) api_checkpop(L, 2); /* all other operations expect two operands */ else { /* for unary operations, add fake 2nd operand */ api_checkpop(L, 1); setobjs2s(L, L->top.p, L->top.p - 1); api_incr_top(L); } /* first operand at top - 2, second at top - 1; result go to top - 2 */ luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2); L->top.p--; /* pop second operand */ lua_unlock(L); } LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { const TValue *o1; const TValue *o2; int i = 0; lua_lock(L); /* may call tag method */ o1 = index2value(L, index1); o2 = index2value(L, index2); if (isvalid(L, o1) && isvalid(L, o2)) { switch (op) { case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break; default: api_check(L, 0, "invalid option"); } } lua_unlock(L); return i; } LUA_API unsigned (lua_numbertocstring) (lua_State *L, int idx, char *buff) { const TValue *o = index2value(L, idx); if (ttisnumber(o)) { unsigned len = luaO_tostringbuff(o, buff); buff[len++] = '\0'; /* add final zero */ return len; } else return 0; } LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { size_t sz = luaO_str2num(s, s2v(L->top.p)); if (sz != 0) api_incr_top(L); return sz; } LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) { lua_Number n = 0; const TValue *o = index2value(L, idx); int isnum = tonumber(o, &n); if (pisnum) *pisnum = isnum; return n; } LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { lua_Integer res = 0; const TValue *o = index2value(L, idx); int isnum = tointeger(o, &res); if (pisnum) *pisnum = isnum; return res; } LUA_API int lua_toboolean (lua_State *L, int idx) { const TValue *o = index2value(L, idx); return !l_isfalse(o); } LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { TValue *o; lua_lock(L); o = index2value(L, idx); if (!ttisstring(o)) { if (!cvt2str(o)) { /* not convertible? */ if (len != NULL) *len = 0; lua_unlock(L); return NULL; } luaO_tostring(L, o); luaC_checkGC(L); o = index2value(L, idx); /* previous call may reallocate the stack */ } lua_unlock(L); if (len != NULL) return getlstr(tsvalue(o), *len); else return getstr(tsvalue(o)); } LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) { const TValue *o = index2value(L, idx); switch (ttypetag(o)) { case LUA_VSHRSTR: return cast(lua_Unsigned, tsvalue(o)->shrlen); case LUA_VLNGSTR: return cast(lua_Unsigned, tsvalue(o)->u.lnglen); case LUA_VUSERDATA: return cast(lua_Unsigned, uvalue(o)->len); case LUA_VTABLE: { lua_Unsigned res; lua_lock(L); res = luaH_getn(L, hvalue(o)); lua_unlock(L); return res; } default: return 0; } } LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { const TValue *o = index2value(L, idx); if (ttislcf(o)) return fvalue(o); else if (ttisCclosure(o)) return clCvalue(o)->f; else return NULL; /* not a C function */ } l_sinline void *touserdata (const TValue *o) { switch (ttype(o)) { case LUA_TUSERDATA: return getudatamem(uvalue(o)); case LUA_TLIGHTUSERDATA: return pvalue(o); default: return NULL; } } LUA_API void *lua_touserdata (lua_State *L, int idx) { const TValue *o = index2value(L, idx); return touserdata(o); } LUA_API lua_State *lua_tothread (lua_State *L, int idx) { const TValue *o = index2value(L, idx); return (!ttisthread(o)) ? NULL : thvalue(o); } /* ** Returns a pointer to the internal representation of an object. ** Note that ISO C does not allow the conversion of a pointer to ** function to a 'void*', so the conversion here goes through ** a 'size_t'. (As the returned pointer is only informative, this ** conversion should not be a problem.) */ LUA_API const void *lua_topointer (lua_State *L, int idx) { const TValue *o = index2value(L, idx); switch (ttypetag(o)) { case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o))); case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA: return touserdata(o); default: { if (iscollectable(o)) return gcvalue(o); else return NULL; } } } /* ** push functions (C -> stack) */ LUA_API void lua_pushnil (lua_State *L) { lua_lock(L); setnilvalue(s2v(L->top.p)); api_incr_top(L); lua_unlock(L); } LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { lua_lock(L); setfltvalue(s2v(L->top.p), n); api_incr_top(L); lua_unlock(L); } LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { lua_lock(L); setivalue(s2v(L->top.p), n); api_incr_top(L); lua_unlock(L); } /* ** Pushes on the stack a string with given length. Avoid using 's' when ** 'len' == 0 (as 's' can be NULL in that case), due to later use of ** 'memcmp' and 'memcpy'. */ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { TString *ts; lua_lock(L); ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); setsvalue2s(L, L->top.p, ts); api_incr_top(L); luaC_checkGC(L); lua_unlock(L); return getstr(ts); } LUA_API const char *lua_pushexternalstring (lua_State *L, const char *s, size_t len, lua_Alloc falloc, void *ud) { TString *ts; lua_lock(L); api_check(L, len <= MAX_SIZE, "string too large"); api_check(L, s[len] == '\0', "string not ending with zero"); ts = luaS_newextlstr (L, s, len, falloc, ud); setsvalue2s(L, L->top.p, ts); api_incr_top(L); luaC_checkGC(L); lua_unlock(L); return getstr(ts); } LUA_API const char *lua_pushstring (lua_State *L, const char *s) { lua_lock(L); if (s == NULL) setnilvalue(s2v(L->top.p)); else { TString *ts; ts = luaS_new(L, s); setsvalue2s(L, L->top.p, ts); s = getstr(ts); /* internal copy's address */ } api_incr_top(L); luaC_checkGC(L); lua_unlock(L); return s; } LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, va_list argp) { const char *ret; lua_lock(L); ret = luaO_pushvfstring(L, fmt, argp); luaC_checkGC(L); lua_unlock(L); return ret; } LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { const char *ret; va_list argp; lua_lock(L); pushvfstring(L, argp, fmt, ret); luaC_checkGC(L); lua_unlock(L); return ret; } LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { lua_lock(L); if (n == 0) { setfvalue(s2v(L->top.p), fn); api_incr_top(L); } else { int i; CClosure *cl; api_checkpop(L, n); api_check(L, n <= MAXUPVAL, "upvalue index too large"); cl = luaF_newCclosure(L, n); cl->f = fn; for (i = 0; i < n; i++) { setobj2n(L, &cl->upvalue[i], s2v(L->top.p - n + i)); /* does not need barrier because closure is white */ lua_assert(iswhite(cl)); } L->top.p -= n; setclCvalue(L, s2v(L->top.p), cl); api_incr_top(L); luaC_checkGC(L); } lua_unlock(L); } LUA_API void lua_pushboolean (lua_State *L, int b) { lua_lock(L); if (b) setbtvalue(s2v(L->top.p)); else setbfvalue(s2v(L->top.p)); api_incr_top(L); lua_unlock(L); } LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { lua_lock(L); setpvalue(s2v(L->top.p), p); api_incr_top(L); lua_unlock(L); } LUA_API int lua_pushthread (lua_State *L) { lua_lock(L); setthvalue(L, s2v(L->top.p), L); api_incr_top(L); lua_unlock(L); return (mainthread(G(L)) == L); } /* ** get functions (Lua -> stack) */ static int auxgetstr (lua_State *L, const TValue *t, const char *k) { lu_byte tag; TString *str = luaS_new(L, k); luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, tag); if (!tagisempty(tag)) api_incr_top(L); else { setsvalue2s(L, L->top.p, str); api_incr_top(L); tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag); } lua_unlock(L); return novariant(tag); } /* ** The following function assumes that the registry cannot be a weak ** table; so, an emergency collection while using the global table ** cannot collect it. */ static void getGlobalTable (lua_State *L, TValue *gt) { Table *registry = hvalue(&G(L)->l_registry); lu_byte tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt); (void)tag; /* avoid not-used warnings when checks are off */ api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist"); } LUA_API int lua_getglobal (lua_State *L, const char *name) { TValue gt; lua_lock(L); getGlobalTable(L, >); return auxgetstr(L, >, name); } LUA_API int lua_gettable (lua_State *L, int idx) { lu_byte tag; TValue *t; lua_lock(L); api_checkpop(L, 1); t = index2value(L, idx); luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, tag); if (tagisempty(tag)) tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag); lua_unlock(L); return novariant(tag); } LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { lua_lock(L); return auxgetstr(L, index2value(L, idx), k); } LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { TValue *t; lu_byte tag; lua_lock(L); t = index2value(L, idx); luaV_fastgeti(t, n, s2v(L->top.p), tag); if (tagisempty(tag)) { TValue key; setivalue(&key, n); tag = luaV_finishget(L, t, &key, L->top.p, tag); } api_incr_top(L); lua_unlock(L); return novariant(tag); } static int finishrawget (lua_State *L, lu_byte tag) { if (tagisempty(tag)) /* avoid copying empty items to the stack */ setnilvalue(s2v(L->top.p)); api_incr_top(L); lua_unlock(L); return novariant(tag); } l_sinline Table *gettable (lua_State *L, int idx) { TValue *t = index2value(L, idx); api_check(L, ttistable(t), "table expected"); return hvalue(t); } LUA_API int lua_rawget (lua_State *L, int idx) { Table *t; lu_byte tag; lua_lock(L); api_checkpop(L, 1); t = gettable(L, idx); tag = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1)); L->top.p--; /* pop key */ return finishrawget(L, tag); } LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { Table *t; lu_byte tag; lua_lock(L); t = gettable(L, idx); luaH_fastgeti(t, n, s2v(L->top.p), tag); return finishrawget(L, tag); } LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { Table *t; TValue k; lua_lock(L); t = gettable(L, idx); setpvalue(&k, cast_voidp(p)); return finishrawget(L, luaH_get(t, &k, s2v(L->top.p))); } LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { Table *t; lua_lock(L); t = luaH_new(L); sethvalue2s(L, L->top.p, t); api_incr_top(L); if (narray > 0 || nrec > 0) luaH_resize(L, t, cast_uint(narray), cast_uint(nrec)); luaC_checkGC(L); lua_unlock(L); } LUA_API int lua_getmetatable (lua_State *L, int objindex) { const TValue *obj; Table *mt; int res = 0; lua_lock(L); obj = index2value(L, objindex); switch (ttype(obj)) { case LUA_TTABLE: mt = hvalue(obj)->metatable; break; case LUA_TUSERDATA: mt = uvalue(obj)->metatable; break; default: mt = G(L)->mt[ttype(obj)]; break; } if (mt != NULL) { sethvalue2s(L, L->top.p, mt); api_incr_top(L); res = 1; } lua_unlock(L); return res; } LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) { TValue *o; int t; lua_lock(L); o = index2value(L, idx); api_check(L, ttisfulluserdata(o), "full userdata expected"); if (n <= 0 || n > uvalue(o)->nuvalue) { setnilvalue(s2v(L->top.p)); t = LUA_TNONE; } else { setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv); t = ttype(s2v(L->top.p)); } api_incr_top(L); lua_unlock(L); return t; } /* ** set functions (stack -> Lua) */ /* ** t[k] = value at the top of the stack (where 'k' is a string) */ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { int hres; TString *str = luaS_new(L, k); api_checkpop(L, 1); luaV_fastset(t, str, s2v(L->top.p - 1), hres, luaH_psetstr); if (hres == HOK) { luaV_finishfastset(L, t, s2v(L->top.p - 1)); L->top.p--; /* pop value */ } else { setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */ api_incr_top(L); luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), hres); L->top.p -= 2; /* pop value and key */ } lua_unlock(L); /* lock done by caller */ } LUA_API void lua_setglobal (lua_State *L, const char *name) { TValue gt; lua_lock(L); /* unlock done in 'auxsetstr' */ getGlobalTable(L, >); auxsetstr(L, >, name); } LUA_API void lua_settable (lua_State *L, int idx) { TValue *t; int hres; lua_lock(L); api_checkpop(L, 2); t = index2value(L, idx); luaV_fastset(t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres, luaH_pset); if (hres == HOK) luaV_finishfastset(L, t, s2v(L->top.p - 1)); else luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres); L->top.p -= 2; /* pop index and value */ lua_unlock(L); } LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { lua_lock(L); /* unlock done in 'auxsetstr' */ auxsetstr(L, index2value(L, idx), k); } LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { TValue *t; int hres; lua_lock(L); api_checkpop(L, 1); t = index2value(L, idx); luaV_fastseti(t, n, s2v(L->top.p - 1), hres); if (hres == HOK) luaV_finishfastset(L, t, s2v(L->top.p - 1)); else { TValue temp; setivalue(&temp, n); luaV_finishset(L, t, &temp, s2v(L->top.p - 1), hres); } L->top.p--; /* pop value */ lua_unlock(L); } static void aux_rawset (lua_State *L, int idx, TValue *key, int n) { Table *t; lua_lock(L); api_checkpop(L, n); t = gettable(L, idx); luaH_set(L, t, key, s2v(L->top.p - 1)); invalidateTMcache(t); luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1)); L->top.p -= n; lua_unlock(L); } LUA_API void lua_rawset (lua_State *L, int idx) { aux_rawset(L, idx, s2v(L->top.p - 2), 2); } LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { TValue k; setpvalue(&k, cast_voidp(p)); aux_rawset(L, idx, &k, 1); } LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { Table *t; lua_lock(L); api_checkpop(L, 1); t = gettable(L, idx); luaH_setint(L, t, n, s2v(L->top.p - 1)); luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1)); L->top.p--; lua_unlock(L); } LUA_API int lua_setmetatable (lua_State *L, int objindex) { TValue *obj; Table *mt; lua_lock(L); api_checkpop(L, 1); obj = index2value(L, objindex); if (ttisnil(s2v(L->top.p - 1))) mt = NULL; else { api_check(L, ttistable(s2v(L->top.p - 1)), "table expected"); mt = hvalue(s2v(L->top.p - 1)); } switch (ttype(obj)) { case LUA_TTABLE: { if (l_unlikely(isshared(hvalue(obj)))) luaG_runerror(L, "can't setmetatable to shared table"); hvalue(obj)->metatable = mt; if (mt) { luaC_objbarrier(L, gcvalue(obj), mt); luaC_checkfinalizer(L, gcvalue(obj), mt); } break; } case LUA_TUSERDATA: { uvalue(obj)->metatable = mt; if (mt) { luaC_objbarrier(L, uvalue(obj), mt); luaC_checkfinalizer(L, gcvalue(obj), mt); } break; } default: { G(L)->mt[ttype(obj)] = mt; break; } } L->top.p--; lua_unlock(L); return 1; } LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { TValue *o; int res; lua_lock(L); api_checkpop(L, 1); o = index2value(L, idx); api_check(L, ttisfulluserdata(o), "full userdata expected"); if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue))) res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */ else { setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1)); luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1)); res = 1; } L->top.p--; lua_unlock(L); return res; } /* ** 'load' and 'call' functions (run Lua code) */ #define checkresults(L,na,nr) \ (api_check(L, (nr) == LUA_MULTRET \ || (L->ci->top.p - L->top.p >= (nr) - (na)), \ "results from function overflow current stack size"), \ api_check(L, LUA_MULTRET <= (nr) && (nr) <= MAXRESULTS, \ "invalid number of results")) LUA_API void lua_callk (lua_State *L, int nargs, int nresults, lua_KContext ctx, lua_KFunction k) { StkId func; lua_lock(L); api_check(L, k == NULL || !isLua(L->ci), "cannot use continuations inside hooks"); api_checkpop(L, nargs + 1); api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); func = L->top.p - (nargs+1); if (k != NULL && yieldable(L)) { /* need to prepare continuation? */ L->ci->u.c.k = k; /* save continuation */ L->ci->u.c.ctx = ctx; /* save context */ luaD_call(L, func, nresults); /* do the call */ } else /* no continuation or no yieldable */ luaD_callnoyield(L, func, nresults); /* just do the call */ adjustresults(L, nresults); lua_unlock(L); } /* ** Execute a protected call. */ struct CallS { /* data to 'f_call' */ StkId func; int nresults; }; static void f_call (lua_State *L, void *ud) { struct CallS *c = cast(struct CallS *, ud); luaD_callnoyield(L, c->func, c->nresults); } LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, lua_KContext ctx, lua_KFunction k) { struct CallS c; TStatus status; ptrdiff_t func; lua_lock(L); api_check(L, k == NULL || !isLua(L->ci), "cannot use continuations inside hooks"); api_checkpop(L, nargs + 1); api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); if (errfunc == 0) func = 0; else { StkId o = index2stack(L, errfunc); api_check(L, ttisfunction(s2v(o)), "error handler must be a function"); func = savestack(L, o); } c.func = L->top.p - (nargs+1); /* function to be called */ if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */ c.nresults = nresults; /* do a 'conventional' protected call */ status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); } else { /* prepare continuation (call is already protected by 'resume') */ CallInfo *ci = L->ci; ci->u.c.k = k; /* save continuation */ ci->u.c.ctx = ctx; /* save context */ /* save information for error recovery */ ci->u2.funcidx = cast_int(savestack(L, c.func)); ci->u.c.old_errfunc = L->errfunc; L->errfunc = func; setoah(ci, L->allowhook); /* save value of 'allowhook' */ ci->callstatus |= CIST_YPCALL; /* function can do error recovery */ luaD_call(L, c.func, nresults); /* do the call */ ci->callstatus &= ~CIST_YPCALL; L->errfunc = ci->u.c.old_errfunc; status = LUA_OK; /* if it is here, there were no errors */ } adjustresults(L, nresults); lua_unlock(L); return APIstatus(status); } static void set_env (lua_State *L, LClosure *f) { if (f->nupvalues >= 1) { /* does it have an upvalue? */ /* get global table from registry */ TValue gt; getGlobalTable(L, >); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ setobj(L, f->upvals[0]->v.p, >); luaC_barrier(L, f->upvals[0], >); } } LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char *chunkname, const char *mode) { ZIO z; TStatus status; lua_lock(L); if (!chunkname) chunkname = "?"; luaZ_init(L, &z, reader, data); status = luaD_protectedparser(L, &z, chunkname, mode); if (status == LUA_OK) { /* no errors? */ LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */ set_env(L,f); } lua_unlock(L); return APIstatus(status); } LUA_API void lua_clonefunction (lua_State *L, const void * fp) { LClosure *cl; LClosure *f = cast(LClosure *, fp); api_check(L, isshared(f->p), "Not a shared proto"); lua_lock(L); cl = luaF_newLclosure(L,f->nupvalues); setclLvalue2s(L,L->top.p,cl); api_incr_top(L); cl->p = f->p; luaF_initupvals(L, cl); set_env(L,cl); lua_unlock(L); } LUA_API void lua_sharefunction (lua_State *L, int index) { LClosure *f; if (!lua_isfunction(L,index) || lua_iscfunction(L,index)) luaG_runerror(L, "Only Lua function can share"); f = cast(LClosure *, lua_topointer(L, index)); luaF_shareproto(f->p); } LUA_API void lua_sharestring (lua_State *L, int index) { TString *ts; if (lua_type(L,index) != LUA_TSTRING) luaG_runerror(L, "need a string to share"); ts = tsvalue(index2value(L,index)); luaS_share(ts); } LUA_API void lua_clonetable(lua_State *L, const void * tp) { Table *t = cast(Table *, tp); if (l_unlikely(!isshared(t))) luaG_runerror(L, "Not a shared table"); lua_lock(L); sethvalue2s(L, L->top.p, t); api_incr_top(L); lua_unlock(L); } /* ** Dump a Lua function, calling 'writer' to write its parts. Ensure ** the stack returns with its original size. */ LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { int status; ptrdiff_t otop = savestack(L, L->top.p); /* original top */ TValue *f = s2v(L->top.p - 1); /* function to be dumped */ lua_lock(L); api_checkpop(L, 1); api_check(L, isLfunction(f), "Lua function expected"); status = luaU_dump(L, clLvalue(f)->p, writer, data, strip); L->top.p = restorestack(L, otop); /* restore top */ lua_unlock(L); return status; } LUA_API int lua_status (lua_State *L) { return APIstatus(L->status); } /* ** Garbage-collection function */ LUA_API int lua_gc (lua_State *L, int what, ...) { va_list argp; int res = 0; global_State *g = G(L); if (g->gcstp & (GCSTPGC | GCSTPCLS)) /* internal stop? */ return -1; /* all options are invalid when stopped */ lua_lock(L); va_start(argp, what); switch (what) { case LUA_GCSTOP: { g->gcstp = GCSTPUSR; /* stopped by the user */ break; } case LUA_GCRESTART: { luaE_setdebt(g, 0); g->gcstp = 0; /* (other bits must be zero here) */ break; } case LUA_GCCOLLECT: { luaC_fullgc(L, 0); break; } case LUA_GCCOUNT: { /* GC values are expressed in Kbytes: #bytes/2^10 */ res = cast_int(gettotalbytes(g) >> 10); break; } case LUA_GCCOUNTB: { res = cast_int(gettotalbytes(g) & 0x3ff); break; } case LUA_GCSTEP: { lu_byte oldstp = g->gcstp; l_mem n = cast(l_mem, va_arg(argp, size_t)); int work = 0; /* true if GC did some work */ g->gcstp = 0; /* allow GC to run (other bits must be zero here) */ if (n <= 0) n = g->GCdebt; /* force to run one basic step */ luaE_setdebt(g, g->GCdebt - n); luaC_condGC(L, (void)0, work = 1); if (work && g->gcstate == GCSpause) /* end of cycle? */ res = 1; /* signal it */ g->gcstp = oldstp; /* restore previous state */ break; } case LUA_GCISRUNNING: { res = gcrunning(g); break; } case LUA_GCGEN: { res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; luaC_changemode(L, KGC_GENMINOR); break; } case LUA_GCINC: { res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; luaC_changemode(L, KGC_INC); break; } case LUA_GCPARAM: { int param = va_arg(argp, int); int value = va_arg(argp, int); api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter"); res = cast_int(luaO_applyparam(g->gcparams[param], 100)); if (value >= 0) g->gcparams[param] = luaO_codeparam(cast_uint(value)); break; } default: res = -1; /* invalid option */ } va_end(argp); lua_unlock(L); return res; } /* ** miscellaneous functions */ LUA_API int lua_error (lua_State *L) { TValue *errobj; lua_lock(L); errobj = s2v(L->top.p - 1); api_checkpop(L, 1); /* error object is the memory error message? */ if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg)) luaM_error(L); /* raise a memory error */ else luaG_errormsg(L); /* raise a regular error */ /* code unreachable; will unlock when control actually leaves the kernel */ return 0; /* to avoid warnings */ } LUA_API int lua_next (lua_State *L, int idx) { Table *t; int more; lua_lock(L); api_checkpop(L, 1); t = gettable(L, idx); more = luaH_next(L, t, L->top.p - 1); if (more) api_incr_top(L); else /* no more elements */ L->top.p--; /* pop key */ lua_unlock(L); return more; } LUA_API void lua_toclose (lua_State *L, int idx) { StkId o; lua_lock(L); o = index2stack(L, idx); api_check(L, L->tbclist.p < o, "given index below or equal a marked one"); luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */ L->ci->callstatus |= CIST_TBC; /* mark that function has TBC slots */ lua_unlock(L); } LUA_API void lua_concat (lua_State *L, int n) { lua_lock(L); api_checknelems(L, n); if (n > 0) { luaV_concat(L, n); luaC_checkGC(L); } else { /* nothing to concatenate */ setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0)); /* push empty string */ api_incr_top(L); } lua_unlock(L); } LUA_API void lua_len (lua_State *L, int idx) { TValue *t; lua_lock(L); t = index2value(L, idx); luaV_objlen(L, L->top.p, t); api_incr_top(L); lua_unlock(L); } LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { lua_Alloc f; lua_lock(L); if (ud) *ud = G(L)->ud; f = G(L)->frealloc; lua_unlock(L); return f; } LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { lua_lock(L); G(L)->ud = ud; G(L)->frealloc = f; lua_unlock(L); } void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) { lua_lock(L); G(L)->ud_warn = ud; G(L)->warnf = f; lua_unlock(L); } void lua_warning (lua_State *L, const char *msg, int tocont) { lua_lock(L); luaE_warning(L, msg, tocont); lua_unlock(L); } LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { Udata *u; lua_lock(L); api_check(L, 0 <= nuvalue && nuvalue < SHRT_MAX, "invalid value"); u = luaS_newudata(L, size, cast(unsigned short, nuvalue)); setuvalue(L, s2v(L->top.p), u); api_incr_top(L); luaC_checkGC(L); lua_unlock(L); return getudatamem(u); } static const char *aux_upvalue (TValue *fi, int n, TValue **val, GCObject **owner) { switch (ttypetag(fi)) { case LUA_VCCL: { /* C closure */ CClosure *f = clCvalue(fi); if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues))) return NULL; /* 'n' not in [1, f->nupvalues] */ *val = &f->upvalue[n-1]; if (owner) *owner = obj2gco(f); return ""; } case LUA_VLCL: { /* Lua closure */ LClosure *f = clLvalue(fi); TString *name; Proto *p = f->p; if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues))) return NULL; /* 'n' not in [1, p->sizeupvalues] */ *val = f->upvals[n-1]->v.p; if (owner) *owner = obj2gco(f->upvals[n - 1]); name = p->upvalues[n-1].name; return (name == NULL) ? "(no name)" : getstr(name); } default: return NULL; /* not a closure */ } } LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { const char *name; TValue *val = NULL; /* to avoid warnings */ lua_lock(L); name = aux_upvalue(index2value(L, funcindex), n, &val, NULL); if (name) { setobj2s(L, L->top.p, val); api_incr_top(L); } lua_unlock(L); return name; } LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { const char *name; TValue *val = NULL; /* to avoid warnings */ GCObject *owner = NULL; /* to avoid warnings */ TValue *fi; lua_lock(L); fi = index2value(L, funcindex); api_checknelems(L, 1); name = aux_upvalue(fi, n, &val, &owner); if (name) { L->top.p--; setobj(L, val, s2v(L->top.p)); luaC_barrier(L, owner, val); } lua_unlock(L); return name; } static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { static const UpVal *const nullup = NULL; LClosure *f; TValue *fi = index2value(L, fidx); api_check(L, ttisLclosure(fi), "Lua function expected"); f = clLvalue(fi); if (pf) *pf = f; if (1 <= n && n <= f->p->sizeupvalues) return &f->upvals[n - 1]; /* get its upvalue pointer */ else return (UpVal**)&nullup; } LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { TValue *fi = index2value(L, fidx); switch (ttypetag(fi)) { case LUA_VLCL: { /* lua closure */ return *getupvalref(L, fidx, n, NULL); } case LUA_VCCL: { /* C closure */ CClosure *f = clCvalue(fi); if (1 <= n && n <= f->nupvalues) return &f->upvalue[n - 1]; /* else */ } /* FALLTHROUGH */ case LUA_VLCF: return NULL; /* light C functions have no upvalues */ default: { api_check(L, 0, "function expected"); return NULL; } } } LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, int fidx2, int n2) { LClosure *f1; UpVal **up1 = getupvalref(L, fidx1, n1, &f1); UpVal **up2 = getupvalref(L, fidx2, n2, NULL); api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index"); *up1 = *up2; luaC_objbarrier(L, f1, *up1); } ================================================ FILE: 3rd/lua/lapi.h ================================================ /* ** $Id: lapi.h $ ** Auxiliary functions from Lua API ** See Copyright Notice in lua.h */ #ifndef lapi_h #define lapi_h #include "llimits.h" #include "lstate.h" #if defined(LUA_USE_APICHECK) #include #define api_check(l,e,msg) assert(e) #else /* for testing */ #define api_check(l,e,msg) ((void)(l), lua_assert((e) && msg)) #endif /* Increments 'L->top.p', checking for stack overflows */ #define api_incr_top(L) \ (L->top.p++, api_check(L, L->top.p <= L->ci->top.p, "stack overflow")) /* ** macros that are executed whenever program enters the Lua core ** ('lua_lock') and leaves the core ('lua_unlock') */ #if !defined(lua_lock) #define lua_lock(L) ((void) 0) #define lua_unlock(L) ((void) 0) #endif /* ** If a call returns too many multiple returns, the callee may not have ** stack space to accommodate all results. In this case, this macro ** increases its stack space ('L->ci->top.p'). */ #define adjustresults(L,nres) \ { if ((nres) <= LUA_MULTRET && L->ci->top.p < L->top.p) \ L->ci->top.p = L->top.p; } /* Ensure the stack has at least 'n' elements */ #define api_checknelems(L,n) \ api_check(L, (n) < (L->top.p - L->ci->func.p), \ "not enough elements in the stack") /* Ensure the stack has at least 'n' elements to be popped. (Some ** functions only update a slot after checking it for popping, but that ** is only an optimization for a pop followed by a push.) */ #define api_checkpop(L,n) \ api_check(L, (n) < L->top.p - L->ci->func.p && \ L->tbclist.p < L->top.p - (n), \ "not enough free elements in the stack") #endif ================================================ FILE: 3rd/lua/lauxlib.c ================================================ /* ** $Id: lauxlib.c $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ #define lauxlib_c #define LUA_LIB #include "lprefix.h" #include #include #include #include #include /* ** This file uses only the official API of Lua. ** Any function declared here could be written as an application function. */ #include "lua.h" #include "lauxlib.h" #include "llimits.h" /* ** {====================================================== ** Traceback ** ======================================================= */ #define LEVELS1 10 /* size of the first part of the stack */ #define LEVELS2 11 /* size of the second part of the stack */ /* ** Search for 'objidx' in table at index -1. ('objidx' must be an ** absolute index.) Return 1 + string at top if it found a good name. */ static int findfield (lua_State *L, int objidx, int level) { if (level == 0 || !lua_istable(L, -1)) return 0; /* not found */ lua_pushnil(L); /* start 'next' loop */ while (lua_next(L, -2)) { /* for each pair in table */ if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */ if (lua_rawequal(L, objidx, -1)) { /* found object? */ lua_pop(L, 1); /* remove value (but keep name) */ return 1; } else if (findfield(L, objidx, level - 1)) { /* try recursively */ /* stack: lib_name, lib_table, field_name (top) */ lua_pushliteral(L, "."); /* place '.' between the two names */ lua_replace(L, -3); /* (in the slot occupied by table) */ lua_concat(L, 3); /* lib_name.field_name */ return 1; } } lua_pop(L, 1); /* remove value */ } return 0; /* not found */ } /* ** Search for a name for a function in all loaded modules */ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { int top = lua_gettop(L); lua_getinfo(L, "f", ar); /* push function */ lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); luaL_checkstack(L, 6, "not enough stack"); /* slots for 'findfield' */ if (findfield(L, top + 1, 2)) { const char *name = lua_tostring(L, -1); if (strncmp(name, LUA_GNAME ".", 3) == 0) { /* name start with '_G.'? */ lua_pushstring(L, name + 3); /* push name without prefix */ lua_remove(L, -2); /* remove original name */ } lua_copy(L, -1, top + 1); /* copy name to proper place */ lua_settop(L, top + 1); /* remove table "loaded" and name copy */ return 1; } else { lua_settop(L, top); /* remove function and global table */ return 0; } } static void pushfuncname (lua_State *L, lua_Debug *ar) { if (*ar->namewhat != '\0') /* is there a name from code? */ lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */ else if (*ar->what == 'm') /* main? */ lua_pushliteral(L, "main chunk"); else if (pushglobalfuncname(L, ar)) { /* try a global name */ lua_pushfstring(L, "function '%s'", lua_tostring(L, -1)); lua_remove(L, -2); /* remove name */ } else if (*ar->what != 'C') /* for Lua functions, use */ lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined); else /* nothing left... */ lua_pushliteral(L, "?"); } static int lastlevel (lua_State *L) { lua_Debug ar; int li = 1, le = 1; /* find an upper bound */ while (lua_getstack(L, le, &ar)) { li = le; le *= 2; } /* do a binary search */ while (li < le) { int m = (li + le)/2; if (lua_getstack(L, m, &ar)) li = m + 1; else le = m; } return le - 1; } LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level) { luaL_Buffer b; lua_Debug ar; int last = lastlevel(L1); int limit2show = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1; luaL_buffinit(L, &b); if (msg) { luaL_addstring(&b, msg); luaL_addchar(&b, '\n'); } luaL_addstring(&b, "stack traceback:"); while (lua_getstack(L1, level++, &ar)) { if (limit2show-- == 0) { /* too many levels? */ int n = last - level - LEVELS2 + 1; /* number of levels to skip */ lua_pushfstring(L, "\n\t...\t(skipping %d levels)", n); luaL_addvalue(&b); /* add warning about skip */ level += n; /* and skip to last levels */ } else { lua_getinfo(L1, "Slnt", &ar); if (ar.currentline <= 0) lua_pushfstring(L, "\n\t%s: in ", ar.short_src); else lua_pushfstring(L, "\n\t%s:%d: in ", ar.short_src, ar.currentline); luaL_addvalue(&b); pushfuncname(L, &ar); luaL_addvalue(&b); if (ar.istailcall) luaL_addstring(&b, "\n\t(...tail calls...)"); } } luaL_pushresult(&b); } /* }====================================================== */ /* ** {====================================================== ** Error-report functions ** ======================================================= */ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) { lua_Debug ar; const char *argword; if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ return luaL_error(L, "bad argument #%d (%s)", arg, extramsg); lua_getinfo(L, "nt", &ar); if (arg <= ar.extraargs) /* error in an extra argument? */ argword = "extra argument"; else { arg -= ar.extraargs; /* do not count extra arguments */ if (strcmp(ar.namewhat, "method") == 0) { /* colon syntax? */ arg--; /* do not count (extra) self argument */ if (arg == 0) /* error in self argument? */ return luaL_error(L, "calling '%s' on bad self (%s)", ar.name, extramsg); /* else go through; error in a regular argument */ } argword = "argument"; } if (ar.name == NULL) ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?"; return luaL_error(L, "bad %s #%d to '%s' (%s)", argword, arg, ar.name, extramsg); } LUALIB_API int luaL_typeerror (lua_State *L, int arg, const char *tname) { const char *msg; const char *typearg; /* name for the type of the actual argument */ if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING) typearg = lua_tostring(L, -1); /* use the given type name */ else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA) typearg = "light userdata"; /* special name for messages */ else typearg = luaL_typename(L, arg); /* standard name */ msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg); return luaL_argerror(L, arg, msg); } static void tag_error (lua_State *L, int arg, int tag) { luaL_typeerror(L, arg, lua_typename(L, tag)); } /* ** The use of 'lua_pushfstring' ensures this function does not ** need reserved stack space when called. */ LUALIB_API void luaL_where (lua_State *L, int level) { lua_Debug ar; if (lua_getstack(L, level, &ar)) { /* check function at level */ lua_getinfo(L, "Sl", &ar); /* get info about it */ if (ar.currentline > 0) { /* is there info? */ lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); return; } } lua_pushfstring(L, ""); /* else, no information available... */ } /* ** Again, the use of 'lua_pushvfstring' ensures this function does ** not need reserved stack space when called. (At worst, it generates ** a memory error instead of the given message.) */ LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { va_list argp; va_start(argp, fmt); luaL_where(L, 1); lua_pushvfstring(L, fmt, argp); va_end(argp); lua_concat(L, 2); return lua_error(L); } LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { int en = errno; /* calls to Lua API may change this value */ if (stat) { lua_pushboolean(L, 1); return 1; } else { const char *msg; luaL_pushfail(L); msg = (en != 0) ? strerror(en) : "(no extra info)"; if (fname) lua_pushfstring(L, "%s: %s", fname, msg); else lua_pushstring(L, msg); lua_pushinteger(L, en); return 3; } } #if !defined(l_inspectstat) /* { */ #if defined(LUA_USE_POSIX) #include /* ** use appropriate macros to interpret 'pclose' return status */ #define l_inspectstat(stat,what) \ if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \ else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; } #else #define l_inspectstat(stat,what) /* no op */ #endif #endif /* } */ LUALIB_API int luaL_execresult (lua_State *L, int stat) { if (stat != 0 && errno != 0) /* error with an 'errno'? */ return luaL_fileresult(L, 0, NULL); else { const char *what = "exit"; /* type of termination */ l_inspectstat(stat, what); /* interpret result */ if (*what == 'e' && stat == 0) /* successful termination? */ lua_pushboolean(L, 1); else luaL_pushfail(L); lua_pushstring(L, what); lua_pushinteger(L, stat); return 3; /* return true/fail,what,code */ } } /* }====================================================== */ /* ** {====================================================== ** Userdata's metatable manipulation ** ======================================================= */ LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { if (luaL_getmetatable(L, tname) != LUA_TNIL) /* name already in use? */ return 0; /* leave previous value on top, but return 0 */ lua_pop(L, 1); lua_createtable(L, 0, 2); /* create metatable */ lua_pushstring(L, tname); lua_setfield(L, -2, "__name"); /* metatable.__name = tname */ lua_pushvalue(L, -1); lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ return 1; } LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) { luaL_getmetatable(L, tname); lua_setmetatable(L, -2); } LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) { void *p = lua_touserdata(L, ud); if (p != NULL) { /* value is a userdata? */ if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ luaL_getmetatable(L, tname); /* get correct metatable */ if (!lua_rawequal(L, -1, -2)) /* not the same? */ p = NULL; /* value is a userdata with wrong metatable */ lua_pop(L, 2); /* remove both metatables */ return p; } } return NULL; /* value is not a userdata with a metatable */ } LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { void *p = luaL_testudata(L, ud, tname); luaL_argexpected(L, p != NULL, ud, tname); return p; } /* }====================================================== */ /* ** {====================================================== ** Argument check functions ** ======================================================= */ LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def, const char *const lst[]) { const char *name = (def) ? luaL_optstring(L, arg, def) : luaL_checkstring(L, arg); int i; for (i=0; lst[i]; i++) if (strcmp(lst[i], name) == 0) return i; return luaL_argerror(L, arg, lua_pushfstring(L, "invalid option '%s'", name)); } /* ** Ensures the stack has at least 'space' extra slots, raising an error ** if it cannot fulfill the request. (The error handling needs a few ** extra slots to format the error message. In case of an error without ** this extra space, Lua will generate the same 'stack overflow' error, ** but without 'msg'.) */ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { if (l_unlikely(!lua_checkstack(L, space))) { if (msg) luaL_error(L, "stack overflow (%s)", msg); else luaL_error(L, "stack overflow"); } } LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) { if (l_unlikely(lua_type(L, arg) != t)) tag_error(L, arg, t); } LUALIB_API void luaL_checkany (lua_State *L, int arg) { if (l_unlikely(lua_type(L, arg) == LUA_TNONE)) luaL_argerror(L, arg, "value expected"); } LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) { const char *s = lua_tolstring(L, arg, len); if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING); return s; } LUALIB_API const char *luaL_optlstring (lua_State *L, int arg, const char *def, size_t *len) { if (lua_isnoneornil(L, arg)) { if (len) *len = (def ? strlen(def) : 0); return def; } else return luaL_checklstring(L, arg, len); } LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) { int isnum; lua_Number d = lua_tonumberx(L, arg, &isnum); if (l_unlikely(!isnum)) tag_error(L, arg, LUA_TNUMBER); return d; } LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) { return luaL_opt(L, luaL_checknumber, arg, def); } static void interror (lua_State *L, int arg) { if (lua_isnumber(L, arg)) luaL_argerror(L, arg, "number has no integer representation"); else tag_error(L, arg, LUA_TNUMBER); } LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) { int isnum; lua_Integer d = lua_tointegerx(L, arg, &isnum); if (l_unlikely(!isnum)) { interror(L, arg); } return d; } LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg, lua_Integer def) { return luaL_opt(L, luaL_checkinteger, arg, def); } /* }====================================================== */ /* ** {====================================================== ** Generic Buffer manipulation ** ======================================================= */ /* userdata to box arbitrary data */ typedef struct UBox { void *box; size_t bsize; } UBox; /* Resize the buffer used by a box. Optimize for the common case of ** resizing to the old size. (For instance, __gc will resize the box ** to 0 even after it was closed. 'pushresult' may also resize it to a ** final size that is equal to the one set when the buffer was created.) */ static void *resizebox (lua_State *L, int idx, size_t newsize) { UBox *box = (UBox *)lua_touserdata(L, idx); if (box->bsize == newsize) /* not changing size? */ return box->box; /* keep the buffer */ else { void *ud; lua_Alloc allocf = lua_getallocf(L, &ud); void *temp = allocf(ud, box->box, box->bsize, newsize); if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */ lua_pushliteral(L, "not enough memory"); lua_error(L); /* raise a memory error */ } box->box = temp; box->bsize = newsize; return temp; } } static int boxgc (lua_State *L) { resizebox(L, 1, 0); return 0; } static const luaL_Reg boxmt[] = { /* box metamethods */ {"__gc", boxgc}, {"__close", boxgc}, {NULL, NULL} }; static void newbox (lua_State *L) { UBox *box = (UBox *)lua_newuserdatauv(L, sizeof(UBox), 0); box->box = NULL; box->bsize = 0; if (luaL_newmetatable(L, "_UBOX*")) /* creating metatable? */ luaL_setfuncs(L, boxmt, 0); /* set its metamethods */ lua_setmetatable(L, -2); } /* ** check whether buffer is using a userdata on the stack as a temporary ** buffer */ #define buffonstack(B) ((B)->b != (B)->init.b) /* ** Whenever buffer is accessed, slot 'idx' must either be a box (which ** cannot be NULL) or it is a placeholder for the buffer. */ #define checkbufferlevel(B,idx) \ lua_assert(buffonstack(B) ? lua_touserdata(B->L, idx) != NULL \ : lua_touserdata(B->L, idx) == (void*)B) /* ** Compute new size for buffer 'B', enough to accommodate extra 'sz' ** bytes plus one for a terminating zero. */ static size_t newbuffsize (luaL_Buffer *B, size_t sz) { size_t newsize = B->size; if (l_unlikely(sz >= MAX_SIZE - B->n)) return cast_sizet(luaL_error(B->L, "resulting string too large")); /* else B->n + sz + 1 <= MAX_SIZE */ if (newsize <= MAX_SIZE/3 * 2) /* no overflow? */ newsize += (newsize >> 1); /* new size *= 1.5 */ if (newsize < B->n + sz + 1) /* not big enough? */ newsize = B->n + sz + 1; return newsize; } /* ** Returns a pointer to a free area with at least 'sz' bytes in buffer ** 'B'. 'boxidx' is the relative position in the stack where is the ** buffer's box or its placeholder. */ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { checkbufferlevel(B, boxidx); if (B->size - B->n >= sz) /* enough space? */ return B->b + B->n; else { lua_State *L = B->L; char *newbuff; size_t newsize = newbuffsize(B, sz); /* create larger buffer */ if (buffonstack(B)) /* buffer already has a box? */ newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */ else { /* no box yet */ lua_remove(L, boxidx); /* remove placeholder */ newbox(L); /* create a new box */ lua_insert(L, boxidx); /* move box to its intended position */ lua_toclose(L, boxidx); newbuff = (char *)resizebox(L, boxidx, newsize); memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ } B->b = newbuff; B->size = newsize; return newbuff + B->n; } } /* ** returns a pointer to a free area with at least 'sz' bytes */ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { return prepbuffsize(B, sz, -1); } LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { if (l > 0) { /* avoid 'memcpy' when 's' can be NULL */ char *b = prepbuffsize(B, l, -1); memcpy(b, s, l * sizeof(char)); luaL_addsize(B, l); } } LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { luaL_addlstring(B, s, strlen(s)); } LUALIB_API void luaL_pushresult (luaL_Buffer *B) { lua_State *L = B->L; checkbufferlevel(B, -1); if (!buffonstack(B)) /* using static buffer? */ lua_pushlstring(L, B->b, B->n); /* save result as regular string */ else { /* reuse buffer already allocated */ UBox *box = (UBox *)lua_touserdata(L, -1); void *ud; lua_Alloc allocf = lua_getallocf(L, &ud); /* function to free buffer */ size_t len = B->n; /* final string length */ char *s; resizebox(L, -1, len + 1); /* adjust box size to content size */ s = (char*)box->box; /* final buffer address */ s[len] = '\0'; /* add ending zero */ /* clear box, as Lua will take control of the buffer */ box->bsize = 0; box->box = NULL; lua_pushexternalstring(L, s, len, allocf, ud); lua_closeslot(L, -2); /* close the box */ lua_gc(L, LUA_GCSTEP, len); } lua_remove(L, -2); /* remove box or placeholder from the stack */ } LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) { luaL_addsize(B, sz); luaL_pushresult(B); } /* ** 'luaL_addvalue' is the only function in the Buffer system where the ** box (if existent) is not on the top of the stack. So, instead of ** calling 'luaL_addlstring', it replicates the code using -2 as the ** last argument to 'prepbuffsize', signaling that the box is (or will ** be) below the string being added to the buffer. (Box creation can ** trigger an emergency GC, so we should not remove the string from the ** stack before we have the space guaranteed.) */ LUALIB_API void luaL_addvalue (luaL_Buffer *B) { lua_State *L = B->L; size_t len; const char *s = lua_tolstring(L, -1, &len); char *b = prepbuffsize(B, len, -2); memcpy(b, s, len * sizeof(char)); luaL_addsize(B, len); lua_pop(L, 1); /* pop string */ } LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { B->L = L; B->b = B->init.b; B->n = 0; B->size = LUAL_BUFFERSIZE; lua_pushlightuserdata(L, (void*)B); /* push placeholder */ } LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) { luaL_buffinit(L, B); return prepbuffsize(B, sz, -1); } /* }====================================================== */ /* ** {====================================================== ** Reference system ** ======================================================= */ /* ** The previously freed references form a linked list: t[1] is the index ** of a first free index, t[t[1]] is the index of the second element, ** etc. A zero signals the end of the list. */ LUALIB_API int luaL_ref (lua_State *L, int t) { int ref; if (lua_isnil(L, -1)) { lua_pop(L, 1); /* remove from stack */ return LUA_REFNIL; /* 'nil' has a unique fixed reference */ } t = lua_absindex(L, t); if (lua_rawgeti(L, t, 1) == LUA_TNUMBER) /* already initialized? */ ref = (int)lua_tointeger(L, -1); /* ref = t[1] */ else { /* first access */ lua_assert(!lua_toboolean(L, -1)); /* must be nil or false */ ref = 0; /* list is empty */ lua_pushinteger(L, 0); /* initialize as an empty list */ lua_rawseti(L, t, 1); /* ref = t[1] = 0 */ } lua_pop(L, 1); /* remove element from stack */ if (ref != 0) { /* any free element? */ lua_rawgeti(L, t, ref); /* remove it from list */ lua_rawseti(L, t, 1); /* (t[1] = t[ref]) */ } else /* no free elements */ ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */ lua_rawseti(L, t, ref); return ref; } LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { if (ref >= 0) { t = lua_absindex(L, t); lua_rawgeti(L, t, 1); lua_assert(lua_isinteger(L, -1)); lua_rawseti(L, t, ref); /* t[ref] = t[1] */ lua_pushinteger(L, ref); lua_rawseti(L, t, 1); /* t[1] = ref */ } } /* }====================================================== */ /* ** {====================================================== ** Load functions ** ======================================================= */ typedef struct LoadF { unsigned n; /* number of pre-read characters */ FILE *f; /* file being read */ char buff[BUFSIZ]; /* area for reading file */ } LoadF; static const char *getF (lua_State *L, void *ud, size_t *size) { LoadF *lf = (LoadF *)ud; UNUSED(L); if (lf->n > 0) { /* are there pre-read characters to be read? */ *size = lf->n; /* return them (chars already in buffer) */ lf->n = 0; /* no more pre-read characters */ } else { /* read a block from file */ /* 'fread' can return > 0 *and* set the EOF flag. If next call to 'getF' called 'fread', it might still wait for user input. The next check avoids this problem. */ if (feof(lf->f)) return NULL; *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */ } return lf->buff; } static int errfile (lua_State *L, const char *what, int fnameindex) { int err = errno; const char *filename = lua_tostring(L, fnameindex) + 1; if (err != 0) lua_pushfstring(L, "cannot %s %s: %s", what, filename, strerror(err)); else lua_pushfstring(L, "cannot %s %s", what, filename); lua_remove(L, fnameindex); return LUA_ERRFILE; } /* ** Skip an optional BOM at the start of a stream. If there is an ** incomplete BOM (the first character is correct but the rest is ** not), returns the first character anyway to force an error ** (as no chunk can start with 0xEF). */ static int skipBOM (FILE *f) { int c = getc(f); /* read first character */ if (c == 0xEF && getc(f) == 0xBB && getc(f) == 0xBF) /* correct BOM? */ return getc(f); /* ignore BOM and return next char */ else /* no (valid) BOM */ return c; /* return first character */ } /* ** reads the first character of file 'f' and skips an optional BOM mark ** in its beginning plus its first line if it starts with '#'. Returns ** true if it skipped the first line. In any case, '*cp' has the ** first "valid" character of the file (after the optional BOM and ** a first-line comment). */ static int skipcomment (FILE *f, int *cp) { int c = *cp = skipBOM(f); if (c == '#') { /* first line is a comment (Unix exec. file)? */ do { /* skip first line */ c = getc(f); } while (c != EOF && c != '\n'); *cp = getc(f); /* next character after comment, if present */ return 1; /* there was a comment */ } else return 0; /* no comment */ } LUALIB_API int luaL_loadfilex_ (lua_State *L, const char *filename, const char *mode) { LoadF lf; int status, readstatus; int c; int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ if (filename == NULL) { lua_pushliteral(L, "=stdin"); lf.f = stdin; } else { lua_pushfstring(L, "@%s", filename); errno = 0; lf.f = fopen(filename, "r"); if (lf.f == NULL) return errfile(L, "open", fnameindex); } lf.n = 0; if (skipcomment(lf.f, &c)) /* read initial portion */ lf.buff[lf.n++] = '\n'; /* add newline to correct line numbers */ if (c == LUA_SIGNATURE[0]) { /* binary file? */ lf.n = 0; /* remove possible newline */ if (filename) { /* "real" file? */ errno = 0; lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ if (lf.f == NULL) return errfile(L, "reopen", fnameindex); skipcomment(lf.f, &c); /* re-read initial portion */ } } if (c != EOF) lf.buff[lf.n++] = cast_char(c); /* 'c' is the first character */ status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); readstatus = ferror(lf.f); errno = 0; /* no useful error number until here */ if (filename) fclose(lf.f); /* close file (even in case of errors) */ if (readstatus) { lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ return errfile(L, "read", fnameindex); } lua_remove(L, fnameindex); return status; } typedef struct LoadS { const char *s; size_t size; } LoadS; static const char *getS (lua_State *L, void *ud, size_t *size) { LoadS *ls = (LoadS *)ud; UNUSED(L); if (ls->size == 0) return NULL; *size = ls->size; ls->size = 0; return ls->s; } LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size, const char *name, const char *mode) { LoadS ls; ls.s = buff; ls.size = size; return lua_load(L, getS, &ls, name, mode); } LUALIB_API int luaL_loadstring (lua_State *L, const char *s) { return luaL_loadbuffer(L, s, strlen(s), s); } /* }====================================================== */ LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { if (!lua_getmetatable(L, obj)) /* no metatable? */ return LUA_TNIL; else { int tt; lua_pushstring(L, event); tt = lua_rawget(L, -2); if (tt == LUA_TNIL) /* is metafield nil? */ lua_pop(L, 2); /* remove metatable and metafield */ else lua_remove(L, -2); /* remove only metatable */ return tt; /* return metafield type */ } } LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { obj = lua_absindex(L, obj); if (luaL_getmetafield(L, obj, event) == LUA_TNIL) /* no metafield? */ return 0; lua_pushvalue(L, obj); lua_call(L, 1, 1); return 1; } LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) { lua_Integer l; int isnum; lua_len(L, idx); l = lua_tointegerx(L, -1, &isnum); if (l_unlikely(!isnum)) luaL_error(L, "object length is not an integer"); lua_pop(L, 1); /* remove object */ return l; } LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { idx = lua_absindex(L,idx); if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */ if (!lua_isstring(L, -1)) luaL_error(L, "'__tostring' must return a string"); } else { switch (lua_type(L, idx)) { case LUA_TNUMBER: { char buff[LUA_N2SBUFFSZ]; lua_numbertocstring(L, idx, buff); lua_pushstring(L, buff); break; } case LUA_TSTRING: lua_pushvalue(L, idx); break; case LUA_TBOOLEAN: lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false")); break; case LUA_TNIL: lua_pushliteral(L, "nil"); break; default: { int tt = luaL_getmetafield(L, idx, "__name"); /* try name */ const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : luaL_typename(L, idx); lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx)); if (tt != LUA_TNIL) lua_remove(L, -2); /* remove '__name' */ break; } } } return lua_tolstring(L, -1, len); } /* ** set functions from list 'l' into table at top - 'nup'; each ** function gets the 'nup' elements at the top as upvalues. ** Returns with only the table at the stack. */ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { luaL_checkstack(L, nup, "too many upvalues"); for (; l->name != NULL; l++) { /* fill the table with given functions */ if (l->func == NULL) /* placeholder? */ lua_pushboolean(L, 0); else { int i; for (i = 0; i < nup; i++) /* copy upvalues to the top */ lua_pushvalue(L, -nup); lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ } lua_setfield(L, -(nup + 2), l->name); } lua_pop(L, nup); /* remove upvalues */ } /* ** ensure that stack[idx][fname] has a table and push that table ** into the stack */ LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) { if (lua_getfield(L, idx, fname) == LUA_TTABLE) return 1; /* table already there */ else { lua_pop(L, 1); /* remove previous result */ idx = lua_absindex(L, idx); lua_newtable(L); lua_pushvalue(L, -1); /* copy to be left at top */ lua_setfield(L, idx, fname); /* assign new table to field */ return 0; /* false, because did not find table there */ } } /* ** Stripped-down 'require': After checking "loaded" table, calls 'openf' ** to open a module, registers the result in 'package.loaded' table and, ** if 'glb' is true, also registers the result in the global table. ** Leaves resulting module on the top. */ LUALIB_API void luaL_requiref (lua_State *L, const char *modname, lua_CFunction openf, int glb) { luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); lua_getfield(L, -1, modname); /* LOADED[modname] */ if (!lua_toboolean(L, -1)) { /* package not already loaded? */ lua_pop(L, 1); /* remove field */ lua_pushcfunction(L, openf); lua_pushstring(L, modname); /* argument to open function */ lua_call(L, 1, 1); /* call 'openf' to open module */ lua_pushvalue(L, -1); /* make copy of module (call result) */ lua_setfield(L, -3, modname); /* LOADED[modname] = module */ } lua_remove(L, -2); /* remove LOADED table */ if (glb) { lua_pushvalue(L, -1); /* copy of module */ lua_setglobal(L, modname); /* _G[modname] = module */ } } LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s, const char *p, const char *r) { const char *wild; size_t l = strlen(p); while ((wild = strstr(s, p)) != NULL) { luaL_addlstring(b, s, ct_diff2sz(wild - s)); /* push prefix */ luaL_addstring(b, r); /* push replacement in place of pattern */ s = wild + l; /* continue after 'p' */ } luaL_addstring(b, s); /* push last suffix */ } LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, const char *r) { luaL_Buffer b; luaL_buffinit(L, &b); luaL_addgsub(&b, s, p, r); luaL_pushresult(&b); return lua_tostring(L, -1); } void *luaL_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { UNUSED(ud); UNUSED(osize); if (nsize == 0) { free(ptr); return NULL; } else return realloc(ptr, nsize); } /* ** Standard panic function just prints an error message. The test ** with 'lua_type' avoids possible memory errors in 'lua_tostring'. */ static int panic (lua_State *L) { const char *msg = (lua_type(L, -1) == LUA_TSTRING) ? lua_tostring(L, -1) : "error object is not a string"; lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", msg); return 0; /* return to Lua to abort */ } /* ** Warning functions: ** warnfoff: warning system is off ** warnfon: ready to start a new message ** warnfcont: previous message is to be continued */ static void warnfoff (void *ud, const char *message, int tocont); static void warnfon (void *ud, const char *message, int tocont); static void warnfcont (void *ud, const char *message, int tocont); /* ** Check whether message is a control message. If so, execute the ** control or ignore it if unknown. */ static int checkcontrol (lua_State *L, const char *message, int tocont) { if (tocont || *(message++) != '@') /* not a control message? */ return 0; else { if (strcmp(message, "off") == 0) lua_setwarnf(L, warnfoff, L); /* turn warnings off */ else if (strcmp(message, "on") == 0) lua_setwarnf(L, warnfon, L); /* turn warnings on */ return 1; /* it was a control message */ } } static void warnfoff (void *ud, const char *message, int tocont) { checkcontrol((lua_State *)ud, message, tocont); } /* ** Writes the message and handle 'tocont', finishing the message ** if needed and setting the next warn function. */ static void warnfcont (void *ud, const char *message, int tocont) { lua_State *L = (lua_State *)ud; lua_writestringerror("%s", message); /* write message */ if (tocont) /* not the last part? */ lua_setwarnf(L, warnfcont, L); /* to be continued */ else { /* last part */ lua_writestringerror("%s", "\n"); /* finish message with end-of-line */ lua_setwarnf(L, warnfon, L); /* next call is a new message */ } } static void warnfon (void *ud, const char *message, int tocont) { if (checkcontrol((lua_State *)ud, message, tocont)) /* control message? */ return; /* nothing else to be done */ lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ warnfcont(ud, message, tocont); /* finish processing */ } /* ** A function to compute an unsigned int with some level of ** randomness. Rely on Address Space Layout Randomization (if present) ** and the current time. */ #if !defined(luai_makeseed) #include /* Size for the buffer, in bytes */ #define BUFSEEDB (sizeof(void*) + sizeof(time_t)) /* Size for the buffer in int's, rounded up */ #define BUFSEED ((BUFSEEDB + sizeof(int) - 1) / sizeof(int)) /* ** Copy the contents of variable 'v' into the buffer pointed by 'b'. ** (The '&b[0]' disguises 'b' to fix an absurd warning from clang.) */ #define addbuff(b,v) (memcpy(&b[0], &(v), sizeof(v)), b += sizeof(v)) static unsigned int luai_makeseed (void) { unsigned int buff[BUFSEED]; unsigned int res; unsigned int i; time_t t = time(NULL); char *b = (char*)buff; addbuff(b, b); /* local variable's address */ addbuff(b, t); /* time */ /* fill (rare but possible) remain of the buffer with zeros */ memset(b, 0, sizeof(buff) - BUFSEEDB); res = buff[0]; for (i = 1; i < BUFSEED; i++) res ^= (res >> 3) + (res << 7) + buff[i]; return res; } #endif LUALIB_API unsigned int luaL_makeseed (lua_State *L) { UNUSED(L); return luai_makeseed(); } /* ** Use the name with parentheses so that headers can redefine it ** as a macro. */ LUALIB_API lua_State *(luaL_newstate) (void) { lua_State *L = lua_newstate(luaL_alloc, NULL, luaL_makeseed(NULL)); if (l_likely(L)) { lua_atpanic(L, &panic); lua_setwarnf(L, warnfon, L); } return L; } LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { lua_Number v = lua_version(L); if (sz != LUAL_NUMSIZES) /* check numeric types */ luaL_error(L, "core and library have incompatible numeric types"); else if (v != ver) luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f", (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)v); } // use clonefunction #include "spinlock.h" #include "lstate.h" struct codecache { struct spinlock lock; lua_State *L; }; static struct codecache CC; static lua_State * newState(lua_State *fromL) { lua_State *L = lua_newstate(luaL_alloc, NULL, G(fromL)->seed); return L; } static void clearcache(void) { if (CC.L == NULL) return; SPIN_LOCK(&CC) lua_State *fromL = CC.L; CC.L = newState(fromL); lua_close(fromL); SPIN_UNLOCK(&CC) } static void init(lua_State *L) { CC.L = newState(L); } LUALIB_API void luaL_initcodecache(void) { SPIN_INIT(&CC); } static const void * load_proto(const char *key) { lua_State *L; const void * result; if (CC.L == NULL) return NULL; SPIN_LOCK(&CC) L = CC.L; lua_pushstring(L, key); lua_rawget(L, LUA_REGISTRYINDEX); result = lua_touserdata(L, -1); lua_pop(L, 1); SPIN_UNLOCK(&CC) return result; } static const void * save_proto(lua_State *fromL, const char *key, const void * proto) { lua_State *L; const void * result = NULL; SPIN_LOCK(&CC) if (CC.L == NULL) { init(fromL); } L = CC.L; lua_pushstring(L, key); lua_pushvalue(L, -1); lua_rawget(L, LUA_REGISTRYINDEX); result = lua_touserdata(L, -1); /* stack: key oldvalue */ if (result == NULL) { lua_pop(L,1); lua_pushlightuserdata(L, (void *)proto); lua_rawset(L, LUA_REGISTRYINDEX); } else { lua_pop(L,2); } SPIN_UNLOCK(&CC) return result; } #define CACHE_OFF 0 #define CACHE_EXIST 1 #define CACHE_ON 2 static int cache_key = 0; static int cache_level(lua_State *L) { int t = lua_rawgetp(L, LUA_REGISTRYINDEX, &cache_key); int r = lua_tointeger(L, -1); lua_pop(L,1); if (t == LUA_TNUMBER) { return r; } return CACHE_ON; } static int cache_mode(lua_State *L) { static const char * lst[] = { "OFF", "EXIST", "ON", NULL, }; int t,r; if (lua_isnoneornil(L,1)) { t = lua_rawgetp(L, LUA_REGISTRYINDEX, &cache_key); r = lua_tointeger(L, -1); if (t == LUA_TNUMBER) { if (r < 0 || r >= CACHE_ON) { r = CACHE_ON; } } else { r = CACHE_ON; } lua_pushstring(L, lst[r]); return 1; } t = luaL_checkoption(L, 1, "OFF" , lst); lua_pushinteger(L, t); lua_rawsetp(L, LUA_REGISTRYINDEX, &cache_key); return 0; } LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) { int level = cache_level(L); const void * proto; lua_State * eL; int err; const void * oldv; if (level == CACHE_OFF || filename == NULL) { return luaL_loadfilex_(L, filename, mode); } proto = load_proto(filename); if (proto) { lua_clonefunction(L, proto); return LUA_OK; } if (level == CACHE_EXIST) { return luaL_loadfilex_(L, filename, mode); } eL = newState(L); if (eL == NULL) { lua_pushliteral(L, "New state failed"); return LUA_ERRMEM; } err = luaL_loadfilex_(eL, filename, mode); if (err != LUA_OK) { size_t sz = 0; const char * msg = lua_tolstring(eL, -1, &sz); lua_pushlstring(L, msg, sz); lua_close(eL); return err; } lua_sharefunction(eL, -1); proto = lua_topointer(eL, -1); oldv = save_proto(L, filename, proto); if (oldv) { lua_close(eL); lua_clonefunction(L, oldv); } else { lua_clonefunction(L, proto); /* Never close it. notice: memory leak */ } return LUA_OK; } static int cache_clear(lua_State *L) { (void)(L); clearcache(); return 0; } LUAMOD_API int luaopen_cache(lua_State *L) { luaL_Reg l[] = { { "clear", cache_clear }, { "mode", cache_mode }, { NULL, NULL }, }; luaL_newlib(L,l); lua_getglobal(L, "loadfile"); lua_setfield(L, -2, "loadfile"); return 1; } ================================================ FILE: 3rd/lua/lauxlib.h ================================================ /* ** $Id: lauxlib.h $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ #ifndef lauxlib_h #define lauxlib_h #include #include #include "luaconf.h" #include "lua.h" /* global table */ #define LUA_GNAME "_G" typedef struct luaL_Buffer luaL_Buffer; /* extra error code for 'luaL_loadfilex' */ #define LUA_ERRFILE (LUA_ERRERR+1) /* key, in the registry, for table of loaded modules */ #define LUA_LOADED_TABLE "_LOADED" /* key, in the registry, for table of preloaded loaders */ #define LUA_PRELOAD_TABLE "_PRELOAD" typedef struct luaL_Reg { const char *name; lua_CFunction func; } luaL_Reg; #define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); #define luaL_checkversion(L) \ luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); LUALIB_API int (luaL_typeerror) (lua_State *L, int arg, const char *tname); LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, size_t *l); LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, const char *def, size_t *l); LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg); LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def); LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg); LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg, lua_Integer def); LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t); LUALIB_API void (luaL_checkany) (lua_State *L, int arg); LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); LUALIB_API void (luaL_where) (lua_State *L, int lvl); LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, const char *const lst[]); LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); LUALIB_API int (luaL_execresult) (lua_State *L, int stat); LUALIB_API void *luaL_alloc (void *ud, void *ptr, size_t osize, size_t nsize); /* predefined references */ #define LUA_NOREF (-2) #define LUA_REFNIL (-1) LUALIB_API int (luaL_ref) (lua_State *L, int t); LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, const char *mode); LUALIB_API int (luaL_loadfilex_) (lua_State *L, const char *filename, const char *mode); #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode); LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); LUALIB_API lua_State *(luaL_newstate) (void); LUALIB_API unsigned luaL_makeseed (lua_State *L); LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s, const char *p, const char *r); LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, const char *r); LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, const char *msg, int level); LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, lua_CFunction openf, int glb); /* ** =============================================================== ** some useful macros ** =============================================================== */ #define luaL_newlibtable(L,l) \ lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) #define luaL_newlib(L,l) \ (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) #define luaL_argcheck(L, cond,arg,extramsg) \ ((void)(luai_likely(cond) || luaL_argerror(L, (arg), (extramsg)))) #define luaL_argexpected(L,cond,arg,tname) \ ((void)(luai_likely(cond) || luaL_typeerror(L, (arg), (tname)))) #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) #define luaL_dofile(L, fn) \ (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) #define luaL_dostring(L, s) \ (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) /* ** Perform arithmetic operations on lua_Integer values with wrap-around ** semantics, as the Lua core does. */ #define luaL_intop(op,v1,v2) \ ((lua_Integer)((lua_Unsigned)(v1) op (lua_Unsigned)(v2))) /* push the value used to represent failure/error */ #if defined(LUA_FAILISFALSE) #define luaL_pushfail(L) lua_pushboolean(L, 0) #else #define luaL_pushfail(L) lua_pushnil(L) #endif /* ** {====================================================== ** Generic Buffer manipulation ** ======================================================= */ struct luaL_Buffer { char *b; /* buffer address */ size_t size; /* buffer size */ size_t n; /* number of characters in buffer */ lua_State *L; union { LUAI_MAXALIGN; /* ensure maximum alignment for buffer */ char b[LUAL_BUFFERSIZE]; /* initial buffer */ } init; }; #define luaL_bufflen(bf) ((bf)->n) #define luaL_buffaddr(bf) ((bf)->b) #define luaL_addchar(B,c) \ ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ ((B)->b[(B)->n++] = (c))) #define luaL_addsize(B,s) ((B)->n += (s)) #define luaL_buffsub(B,s) ((B)->n -= (s)) LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) /* }====================================================== */ /* ** {====================================================== ** File handles for IO library ** ======================================================= */ /* ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and ** initial structure 'luaL_Stream' (it may contain other fields ** after that initial structure). */ #define LUA_FILEHANDLE "FILE*" typedef struct luaL_Stream { FILE *f; /* stream (NULL for incompletely created streams) */ lua_CFunction closef; /* to close stream (NULL for closed streams) */ } luaL_Stream; /* }====================================================== */ /* ** {============================================================ ** Compatibility with deprecated conversions ** ============================================================= */ #if defined(LUA_COMPAT_APIINTCASTS) #define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a)) #define luaL_optunsigned(L,a,d) \ ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d))) #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) #endif /* }============================================================ */ #endif ================================================ FILE: 3rd/lua/lbaselib.c ================================================ /* ** $Id: lbaselib.c $ ** Basic library ** See Copyright Notice in lua.h */ #define lbaselib_c #define LUA_LIB #include "lprefix.h" #include #include #include #include #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "llimits.h" static int luaB_print (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ int i; for (i = 1; i <= n; i++) { /* for each argument */ size_t l; const char *s = luaL_tolstring(L, i, &l); /* convert it to string */ if (i > 1) /* not the first element? */ lua_writestring("\t", 1); /* add a tab before it */ lua_writestring(s, l); /* print it */ lua_pop(L, 1); /* pop result */ } lua_writeline(); return 0; } /* ** Creates a warning with all given arguments. ** Check first for errors; otherwise an error may interrupt ** the composition of a warning, leaving it unfinished. */ static int luaB_warn (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ int i; luaL_checkstring(L, 1); /* at least one argument */ for (i = 2; i <= n; i++) luaL_checkstring(L, i); /* make sure all arguments are strings */ for (i = 1; i < n; i++) /* compose warning */ lua_warning(L, lua_tostring(L, i), 1); lua_warning(L, lua_tostring(L, n), 0); /* close warning */ return 0; } #define SPACECHARS " \f\n\r\t\v" static const char *b_str2int (const char *s, unsigned base, lua_Integer *pn) { lua_Unsigned n = 0; int neg = 0; s += strspn(s, SPACECHARS); /* skip initial spaces */ if (*s == '-') { s++; neg = 1; } /* handle sign */ else if (*s == '+') s++; if (!isalnum(cast_uchar(*s))) /* no digit? */ return NULL; do { unsigned digit = cast_uint(isdigit(cast_uchar(*s)) ? *s - '0' : (toupper(cast_uchar(*s)) - 'A') + 10); if (digit >= base) return NULL; /* invalid numeral */ n = n * base + digit; s++; } while (isalnum(cast_uchar(*s))); s += strspn(s, SPACECHARS); /* skip trailing spaces */ *pn = (lua_Integer)((neg) ? (0u - n) : n); return s; } static int luaB_tonumber (lua_State *L) { if (lua_isnoneornil(L, 2)) { /* standard conversion? */ if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */ lua_settop(L, 1); /* yes; return it */ return 1; } else { size_t l; const char *s = lua_tolstring(L, 1, &l); if (s != NULL && lua_stringtonumber(L, s) == l + 1) return 1; /* successful conversion to number */ /* else not a number */ luaL_checkany(L, 1); /* (but there must be some parameter) */ } } else { size_t l; const char *s; lua_Integer n = 0; /* to avoid warnings */ lua_Integer base = luaL_checkinteger(L, 2); luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */ s = lua_tolstring(L, 1, &l); luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); if (b_str2int(s, cast_uint(base), &n) == s + l) { lua_pushinteger(L, n); return 1; } /* else not a number */ } /* else not a number */ luaL_pushfail(L); /* not a number */ return 1; } static int luaB_error (lua_State *L) { int level = (int)luaL_optinteger(L, 2, 1); lua_settop(L, 1); if (lua_type(L, 1) == LUA_TSTRING && level > 0) { luaL_where(L, level); /* add extra information */ lua_pushvalue(L, 1); lua_concat(L, 2); } return lua_error(L); } static int luaB_getmetatable (lua_State *L) { luaL_checkany(L, 1); if (!lua_getmetatable(L, 1)) { lua_pushnil(L); return 1; /* no metatable */ } luaL_getmetafield(L, 1, "__metatable"); return 1; /* returns either __metatable field (if present) or metatable */ } static int luaB_setmetatable (lua_State *L) { int t = lua_type(L, 2); luaL_checktype(L, 1, LUA_TTABLE); luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table"); if (l_unlikely(luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL)) return luaL_error(L, "cannot change a protected metatable"); lua_settop(L, 2); lua_setmetatable(L, 1); return 1; } static int luaB_rawequal (lua_State *L) { luaL_checkany(L, 1); luaL_checkany(L, 2); lua_pushboolean(L, lua_rawequal(L, 1, 2)); return 1; } static int luaB_rawlen (lua_State *L) { int t = lua_type(L, 1); luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1, "table or string"); lua_pushinteger(L, l_castU2S(lua_rawlen(L, 1))); return 1; } static int luaB_rawget (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); luaL_checkany(L, 2); lua_settop(L, 2); lua_rawget(L, 1); return 1; } static int luaB_rawset (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); luaL_checkany(L, 2); luaL_checkany(L, 3); lua_settop(L, 3); lua_rawset(L, 1); return 1; } static int pushmode (lua_State *L, int oldmode) { if (oldmode == -1) luaL_pushfail(L); /* invalid call to 'lua_gc' */ else lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" : "generational"); return 1; } /* ** check whether call to 'lua_gc' was valid (not inside a finalizer) */ #define checkvalres(res) { if (res == -1) break; } static int luaB_collectgarbage (lua_State *L) { static const char *const opts[] = {"stop", "restart", "collect", "count", "step", "isrunning", "generational", "incremental", "param", NULL}; static const char optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, LUA_GCCOUNT, LUA_GCSTEP, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC, LUA_GCPARAM}; int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; switch (o) { case LUA_GCCOUNT: { int k = lua_gc(L, o); int b = lua_gc(L, LUA_GCCOUNTB); checkvalres(k); lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024)); return 1; } case LUA_GCSTEP: { lua_Integer n = luaL_optinteger(L, 2, 0); int res = lua_gc(L, o, cast_sizet(n)); checkvalres(res); lua_pushboolean(L, res); return 1; } case LUA_GCISRUNNING: { int res = lua_gc(L, o); checkvalres(res); lua_pushboolean(L, res); return 1; } case LUA_GCGEN: { return pushmode(L, lua_gc(L, o)); } case LUA_GCINC: { return pushmode(L, lua_gc(L, o)); } case LUA_GCPARAM: { static const char *const params[] = { "minormul", "majorminor", "minormajor", "pause", "stepmul", "stepsize", NULL}; static const char pnum[] = { LUA_GCPMINORMUL, LUA_GCPMAJORMINOR, LUA_GCPMINORMAJOR, LUA_GCPPAUSE, LUA_GCPSTEPMUL, LUA_GCPSTEPSIZE}; int p = pnum[luaL_checkoption(L, 2, NULL, params)]; lua_Integer value = luaL_optinteger(L, 3, -1); lua_pushinteger(L, lua_gc(L, o, p, (int)value)); return 1; } default: { int res = lua_gc(L, o); checkvalres(res); lua_pushinteger(L, res); return 1; } } luaL_pushfail(L); /* invalid call (inside a finalizer) */ return 1; } static int luaB_type (lua_State *L) { int t = lua_type(L, 1); luaL_argcheck(L, t != LUA_TNONE, 1, "value expected"); lua_pushstring(L, lua_typename(L, t)); return 1; } static int luaB_next (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); lua_settop(L, 2); /* create a 2nd argument if there isn't one */ if (lua_next(L, 1)) return 2; else { lua_pushnil(L); return 1; } } static int pairscont (lua_State *L, int status, lua_KContext k) { (void)L; (void)status; (void)k; /* unused */ return 4; /* __pairs did all the work, just return its results */ } static int luaB_pairs (lua_State *L) { luaL_checkany(L, 1); if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */ lua_pushcfunction(L, luaB_next); /* will return generator and */ lua_pushvalue(L, 1); /* state */ lua_pushnil(L); /* initial value */ lua_pushnil(L); /* to-be-closed object */ } else { lua_pushvalue(L, 1); /* argument 'self' to metamethod */ lua_callk(L, 1, 4, 0, pairscont); /* get 4 values from metamethod */ } return 4; } /* ** Traversal function for 'ipairs' */ static int ipairsaux (lua_State *L) { lua_Integer i = luaL_checkinteger(L, 2); i = luaL_intop(+, i, 1); lua_pushinteger(L, i); return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2; } /* ** 'ipairs' function. Returns 'ipairsaux', given "table", 0. ** (The given "table" may not be a table.) */ static int luaB_ipairs (lua_State *L) { luaL_checkany(L, 1); lua_pushcfunction(L, ipairsaux); /* iteration function */ lua_pushvalue(L, 1); /* state */ lua_pushinteger(L, 0); /* initial value */ return 3; } static int load_aux (lua_State *L, int status, int envidx) { if (l_likely(status == LUA_OK)) { if (envidx != 0) { /* 'env' parameter? */ lua_pushvalue(L, envidx); /* environment for loaded function */ if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ lua_pop(L, 1); /* remove 'env' if not used by previous call */ } return 1; } else { /* error (message is on top of the stack) */ luaL_pushfail(L); lua_insert(L, -2); /* put before error message */ return 2; /* return fail plus error message */ } } static const char *getMode (lua_State *L, int idx) { const char *mode = luaL_optstring(L, idx, "bt"); if (strchr(mode, 'B') != NULL) /* Lua code cannot use fixed buffers */ luaL_argerror(L, idx, "invalid mode"); return mode; } static int luaB_loadfile (lua_State *L) { const char *fname = luaL_optstring(L, 1, NULL); const char *mode = getMode(L, 2); int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ int status = luaL_loadfilex(L, fname, mode); return load_aux(L, status, env); } /* ** {====================================================== ** Generic Read function ** ======================================================= */ /* ** reserved slot, above all arguments, to hold a copy of the returned ** string to avoid it being collected while parsed. 'load' has four ** optional arguments (chunk, source name, mode, and environment). */ #define RESERVEDSLOT 5 /* ** Reader for generic 'load' function: 'lua_load' uses the ** stack for internal stuff, so the reader cannot change the ** stack top. Instead, it keeps its resulting string in a ** reserved slot inside the stack. */ static const char *generic_reader (lua_State *L, void *ud, size_t *size) { (void)(ud); /* not used */ luaL_checkstack(L, 2, "too many nested functions"); lua_pushvalue(L, 1); /* get function */ lua_call(L, 0, 1); /* call it */ if (lua_isnil(L, -1)) { lua_pop(L, 1); /* pop result */ *size = 0; return NULL; } else if (l_unlikely(!lua_isstring(L, -1))) luaL_error(L, "reader function must return a string"); lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ return lua_tolstring(L, RESERVEDSLOT, size); } static int luaB_load (lua_State *L) { int status; size_t l; const char *s = lua_tolstring(L, 1, &l); const char *mode = getMode(L, 3); int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */ if (s != NULL) { /* loading a string? */ const char *chunkname = luaL_optstring(L, 2, s); status = luaL_loadbufferx(L, s, l, chunkname, mode); } else { /* loading from a reader function */ const char *chunkname = luaL_optstring(L, 2, "=(load)"); luaL_checktype(L, 1, LUA_TFUNCTION); lua_settop(L, RESERVEDSLOT); /* create reserved slot */ status = lua_load(L, generic_reader, NULL, chunkname, mode); } return load_aux(L, status, env); } /* }====================================================== */ static int dofilecont (lua_State *L, int d1, lua_KContext d2) { (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */ return lua_gettop(L) - 1; } static int luaB_dofile (lua_State *L) { const char *fname = luaL_optstring(L, 1, NULL); lua_settop(L, 1); if (l_unlikely(luaL_loadfile(L, fname) != LUA_OK)) return lua_error(L); lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); return dofilecont(L, 0, 0); } static int luaB_assert (lua_State *L) { if (l_likely(lua_toboolean(L, 1))) /* condition is true? */ return lua_gettop(L); /* return all arguments */ else { /* error */ luaL_checkany(L, 1); /* there must be a condition */ lua_remove(L, 1); /* remove it */ lua_pushliteral(L, "assertion failed!"); /* default message */ lua_settop(L, 1); /* leave only message (default if no other one) */ return luaB_error(L); /* call 'error' */ } } static int luaB_select (lua_State *L) { int n = lua_gettop(L); if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { lua_pushinteger(L, n-1); return 1; } else { lua_Integer i = luaL_checkinteger(L, 1); if (i < 0) i = n + i; else if (i > n) i = n; luaL_argcheck(L, 1 <= i, 1, "index out of range"); return n - (int)i; } } /* ** Continuation function for 'pcall' and 'xpcall'. Both functions ** already pushed a 'true' before doing the call, so in case of success ** 'finishpcall' only has to return everything in the stack minus ** 'extra' values (where 'extra' is exactly the number of items to be ** ignored). */ static int finishpcall (lua_State *L, int status, lua_KContext extra) { if (l_unlikely(status != LUA_OK && status != LUA_YIELD)) { /* error? */ lua_pushboolean(L, 0); /* first result (false) */ lua_pushvalue(L, -2); /* error message */ return 2; /* return false, msg */ } else return lua_gettop(L) - (int)extra; /* return all results */ } static int luaB_pcall (lua_State *L) { int status; luaL_checkany(L, 1); lua_pushboolean(L, 1); /* first result if no errors */ lua_insert(L, 1); /* put it in place */ status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall); return finishpcall(L, status, 0); } /* ** Do a protected call with error handling. After 'lua_rotate', the ** stack will have ; so, the function passes ** 2 to 'finishpcall' to skip the 2 first values when returning results. */ static int luaB_xpcall (lua_State *L) { int status; int n = lua_gettop(L); luaL_checktype(L, 2, LUA_TFUNCTION); /* check error function */ lua_pushboolean(L, 1); /* first result */ lua_pushvalue(L, 1); /* function */ lua_rotate(L, 3, 2); /* move them below function's arguments */ status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall); return finishpcall(L, status, 2); } static int luaB_tostring (lua_State *L) { luaL_checkany(L, 1); luaL_tolstring(L, 1, NULL); return 1; } static const luaL_Reg base_funcs[] = { {"assert", luaB_assert}, {"collectgarbage", luaB_collectgarbage}, {"dofile", luaB_dofile}, {"error", luaB_error}, {"getmetatable", luaB_getmetatable}, {"ipairs", luaB_ipairs}, {"loadfile", luaB_loadfile}, {"load", luaB_load}, {"next", luaB_next}, {"pairs", luaB_pairs}, {"pcall", luaB_pcall}, {"print", luaB_print}, {"warn", luaB_warn}, {"rawequal", luaB_rawequal}, {"rawlen", luaB_rawlen}, {"rawget", luaB_rawget}, {"rawset", luaB_rawset}, {"select", luaB_select}, {"setmetatable", luaB_setmetatable}, {"tonumber", luaB_tonumber}, {"tostring", luaB_tostring}, {"type", luaB_type}, {"xpcall", luaB_xpcall}, /* placeholders */ {LUA_GNAME, NULL}, {"_VERSION", NULL}, {NULL, NULL} }; LUAMOD_API int luaopen_base (lua_State *L) { /* open lib into global table */ lua_pushglobaltable(L); luaL_setfuncs(L, base_funcs, 0); /* set global _G */ lua_pushvalue(L, -1); lua_setfield(L, -2, LUA_GNAME); /* set global _VERSION */ lua_pushliteral(L, LUA_VERSION); lua_setfield(L, -2, "_VERSION"); return 1; } ================================================ FILE: 3rd/lua/lcode.c ================================================ /* ** $Id: lcode.c $ ** Code generator for Lua ** See Copyright Notice in lua.h */ #define lcode_c #define LUA_CORE #include "lprefix.h" #include #include #include #include #include "lua.h" #include "lcode.h" #include "ldebug.h" #include "ldo.h" #include "lgc.h" #include "llex.h" #include "lmem.h" #include "lobject.h" #include "lopcodes.h" #include "lparser.h" #include "lstring.h" #include "ltable.h" #include "lvm.h" /* (note that expressions VJMP also have jumps.) */ #define hasjumps(e) ((e)->t != (e)->f) static int codesJ (FuncState *fs, OpCode o, int sj, int k); /* semantic error */ l_noret luaK_semerror (LexState *ls, const char *fmt, ...) { const char *msg; va_list argp; pushvfstring(ls->L, argp, fmt, msg); ls->t.token = 0; /* remove "near " from final message */ ls->linenumber = ls->lastline; /* back to line of last used token */ luaX_syntaxerror(ls, msg); } /* ** If expression is a numeric constant, fills 'v' with its value ** and returns 1. Otherwise, returns 0. */ static int tonumeral (const expdesc *e, TValue *v) { if (hasjumps(e)) return 0; /* not a numeral */ switch (e->k) { case VKINT: if (v) setivalue(v, e->u.ival); return 1; case VKFLT: if (v) setfltvalue(v, e->u.nval); return 1; default: return 0; } } /* ** Get the constant value from a constant expression */ static TValue *const2val (FuncState *fs, const expdesc *e) { lua_assert(e->k == VCONST); return &fs->ls->dyd->actvar.arr[e->u.info].k; } /* ** If expression is a constant, fills 'v' with its value ** and returns 1. Otherwise, returns 0. */ int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) { if (hasjumps(e)) return 0; /* not a constant */ switch (e->k) { case VFALSE: setbfvalue(v); return 1; case VTRUE: setbtvalue(v); return 1; case VNIL: setnilvalue(v); return 1; case VKSTR: { setsvalue(fs->ls->L, v, e->u.strval); return 1; } case VCONST: { setobj(fs->ls->L, v, const2val(fs, e)); return 1; } default: return tonumeral(e, v); } } /* ** Return the previous instruction of the current code. If there ** may be a jump target between the current instruction and the ** previous one, return an invalid instruction (to avoid wrong ** optimizations). */ static Instruction *previousinstruction (FuncState *fs) { static const Instruction invalidinstruction = ~(Instruction)0; if (fs->pc > fs->lasttarget) return &fs->f->code[fs->pc - 1]; /* previous instruction */ else return cast(Instruction*, &invalidinstruction); } /* ** Create a OP_LOADNIL instruction, but try to optimize: if the previous ** instruction is also OP_LOADNIL and ranges are compatible, adjust ** range of previous instruction instead of emitting a new one. (For ** instance, 'local a; local b' will generate a single opcode.) */ void luaK_nil (FuncState *fs, int from, int n) { int l = from + n - 1; /* last register to set nil */ Instruction *previous = previousinstruction(fs); if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */ int pfrom = GETARG_A(*previous); /* get previous range */ int pl = pfrom + GETARG_B(*previous); if ((pfrom <= from && from <= pl + 1) || (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */ if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */ if (pl > l) l = pl; /* l = max(l, pl) */ SETARG_A(*previous, from); SETARG_B(*previous, l - from); return; } /* else go through */ } luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */ } /* ** Gets the destination address of a jump instruction. Used to traverse ** a list of jumps. */ static int getjump (FuncState *fs, int pc) { int offset = GETARG_sJ(fs->f->code[pc]); if (offset == NO_JUMP) /* point to itself represents end of list */ return NO_JUMP; /* end of list */ else return (pc+1)+offset; /* turn offset into absolute position */ } /* ** Fix jump instruction at position 'pc' to jump to 'dest'. ** (Jump addresses are relative in Lua) */ static void fixjump (FuncState *fs, int pc, int dest) { Instruction *jmp = &fs->f->code[pc]; int offset = dest - (pc + 1); lua_assert(dest != NO_JUMP); if (!(-OFFSET_sJ <= offset && offset <= MAXARG_sJ - OFFSET_sJ)) luaX_syntaxerror(fs->ls, "control structure too long"); lua_assert(GET_OPCODE(*jmp) == OP_JMP); SETARG_sJ(*jmp, offset); } /* ** Concatenate jump-list 'l2' into jump-list 'l1' */ void luaK_concat (FuncState *fs, int *l1, int l2) { if (l2 == NO_JUMP) return; /* nothing to concatenate? */ else if (*l1 == NO_JUMP) /* no original list? */ *l1 = l2; /* 'l1' points to 'l2' */ else { int list = *l1; int next; while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ list = next; fixjump(fs, list, l2); /* last element links to 'l2' */ } } /* ** Create a jump instruction and return its position, so its destination ** can be fixed later (with 'fixjump'). */ int luaK_jump (FuncState *fs) { return codesJ(fs, OP_JMP, NO_JUMP, 0); } /* ** Code a 'return' instruction */ void luaK_ret (FuncState *fs, int first, int nret) { OpCode op; switch (nret) { case 0: op = OP_RETURN0; break; case 1: op = OP_RETURN1; break; default: op = OP_RETURN; break; } luaY_checklimit(fs, nret + 1, MAXARG_B, "returns"); luaK_codeABC(fs, op, first, nret + 1, 0); } /* ** Code a "conditional jump", that is, a test or comparison opcode ** followed by a jump. Return jump position. */ static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) { luaK_codeABCk(fs, op, A, B, C, k); return luaK_jump(fs); } /* ** returns current 'pc' and marks it as a jump target (to avoid wrong ** optimizations with consecutive instructions not in the same basic block). */ int luaK_getlabel (FuncState *fs) { fs->lasttarget = fs->pc; return fs->pc; } /* ** Returns the position of the instruction "controlling" a given ** jump (that is, its condition), or the jump itself if it is ** unconditional. */ static Instruction *getjumpcontrol (FuncState *fs, int pc) { Instruction *pi = &fs->f->code[pc]; if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) return pi-1; else return pi; } /* ** Patch destination register for a TESTSET instruction. ** If instruction in position 'node' is not a TESTSET, return 0 ("fails"). ** Otherwise, if 'reg' is not 'NO_REG', set it as the destination ** register. Otherwise, change instruction to a simple 'TEST' (produces ** no register value) */ static int patchtestreg (FuncState *fs, int node, int reg) { Instruction *i = getjumpcontrol(fs, node); if (GET_OPCODE(*i) != OP_TESTSET) return 0; /* cannot patch other instructions */ if (reg != NO_REG && reg != GETARG_B(*i)) SETARG_A(*i, reg); else { /* no register to put value or register already has the value; change instruction to simple test */ *i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, 0, GETARG_k(*i)); } return 1; } /* ** Traverse a list of tests ensuring no one produces a value */ static void removevalues (FuncState *fs, int list) { for (; list != NO_JUMP; list = getjump(fs, list)) patchtestreg(fs, list, NO_REG); } /* ** Traverse a list of tests, patching their destination address and ** registers: tests producing values jump to 'vtarget' (and put their ** values in 'reg'), other tests jump to 'dtarget'. */ static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, int dtarget) { while (list != NO_JUMP) { int next = getjump(fs, list); if (patchtestreg(fs, list, reg)) fixjump(fs, list, vtarget); else fixjump(fs, list, dtarget); /* jump to default target */ list = next; } } /* ** Path all jumps in 'list' to jump to 'target'. ** (The assert means that we cannot fix a jump to a forward address ** because we only know addresses once code is generated.) */ void luaK_patchlist (FuncState *fs, int list, int target) { lua_assert(target <= fs->pc); patchlistaux(fs, list, target, NO_REG, target); } void luaK_patchtohere (FuncState *fs, int list) { int hr = luaK_getlabel(fs); /* mark "here" as a jump target */ luaK_patchlist(fs, list, hr); } /* limit for difference between lines in relative line info. */ #define LIMLINEDIFF 0x80 /* ** Save line info for a new instruction. If difference from last line ** does not fit in a byte, of after that many instructions, save a new ** absolute line info; (in that case, the special value 'ABSLINEINFO' ** in 'lineinfo' signals the existence of this absolute information.) ** Otherwise, store the difference from last line in 'lineinfo'. */ static void savelineinfo (FuncState *fs, Proto *f, int line) { int linedif = line - fs->previousline; int pc = fs->pc - 1; /* last instruction coded */ if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ >= MAXIWTHABS) { luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo, f->sizeabslineinfo, AbsLineInfo, INT_MAX, "lines"); f->abslineinfo[fs->nabslineinfo].pc = pc; f->abslineinfo[fs->nabslineinfo++].line = line; linedif = ABSLINEINFO; /* signal that there is absolute information */ fs->iwthabs = 1; /* restart counter */ } luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte, INT_MAX, "opcodes"); f->lineinfo[pc] = cast(ls_byte, linedif); fs->previousline = line; /* last line saved */ } /* ** Remove line information from the last instruction. ** If line information for that instruction is absolute, set 'iwthabs' ** above its max to force the new (replacing) instruction to have ** absolute line info, too. */ static void removelastlineinfo (FuncState *fs) { Proto *f = fs->f; int pc = fs->pc - 1; /* last instruction coded */ if (f->lineinfo[pc] != ABSLINEINFO) { /* relative line info? */ fs->previousline -= f->lineinfo[pc]; /* correct last line saved */ fs->iwthabs--; /* undo previous increment */ } else { /* absolute line information */ lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == pc); fs->nabslineinfo--; /* remove it */ fs->iwthabs = MAXIWTHABS + 1; /* force next line info to be absolute */ } } /* ** Remove the last instruction created, correcting line information ** accordingly. */ static void removelastinstruction (FuncState *fs) { removelastlineinfo(fs); fs->pc--; } /* ** Emit instruction 'i', checking for array sizes and saving also its ** line information. Return 'i' position. */ int luaK_code (FuncState *fs, Instruction i) { Proto *f = fs->f; /* put new instruction in code array */ luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, INT_MAX, "opcodes"); f->code[fs->pc++] = i; savelineinfo(fs, f, fs->ls->lastline); return fs->pc - 1; /* index of new instruction */ } /* ** Format and emit an 'iABC' instruction. (Assertions check consistency ** of parameters versus opcode.) */ int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) { lua_assert(getOpMode(o) == iABC); lua_assert(A <= MAXARG_A && B <= MAXARG_B && C <= MAXARG_C && (k & ~1) == 0); return luaK_code(fs, CREATE_ABCk(o, A, B, C, k)); } int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) { lua_assert(getOpMode(o) == ivABC); lua_assert(A <= MAXARG_A && B <= MAXARG_vB && C <= MAXARG_vC && (k & ~1) == 0); return luaK_code(fs, CREATE_vABCk(o, A, B, C, k)); } /* ** Format and emit an 'iABx' instruction. */ int luaK_codeABx (FuncState *fs, OpCode o, int A, int Bc) { lua_assert(getOpMode(o) == iABx); lua_assert(A <= MAXARG_A && Bc <= MAXARG_Bx); return luaK_code(fs, CREATE_ABx(o, A, Bc)); } /* ** Format and emit an 'iAsBx' instruction. */ static int codeAsBx (FuncState *fs, OpCode o, int A, int Bc) { int b = Bc + OFFSET_sBx; lua_assert(getOpMode(o) == iAsBx); lua_assert(A <= MAXARG_A && b <= MAXARG_Bx); return luaK_code(fs, CREATE_ABx(o, A, b)); } /* ** Format and emit an 'isJ' instruction. */ static int codesJ (FuncState *fs, OpCode o, int sj, int k) { int j = sj + OFFSET_sJ; lua_assert(getOpMode(o) == isJ); lua_assert(j <= MAXARG_sJ && (k & ~1) == 0); return luaK_code(fs, CREATE_sJ(o, j, k)); } /* ** Emit an "extra argument" instruction (format 'iAx') */ static int codeextraarg (FuncState *fs, int A) { lua_assert(A <= MAXARG_Ax); return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, A)); } /* ** Emit a "load constant" instruction, using either 'OP_LOADK' ** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX' ** instruction with "extra argument". */ static int luaK_codek (FuncState *fs, int reg, int k) { if (k <= MAXARG_Bx) return luaK_codeABx(fs, OP_LOADK, reg, k); else { int p = luaK_codeABx(fs, OP_LOADKX, reg, 0); codeextraarg(fs, k); return p; } } /* ** Check register-stack level, keeping track of its maximum size ** in field 'maxstacksize' */ void luaK_checkstack (FuncState *fs, int n) { int newstack = fs->freereg + n; if (newstack > fs->f->maxstacksize) { luaY_checklimit(fs, newstack, MAX_FSTACK, "registers"); fs->f->maxstacksize = cast_byte(newstack); } } /* ** Reserve 'n' registers in register stack */ void luaK_reserveregs (FuncState *fs, int n) { luaK_checkstack(fs, n); fs->freereg = cast_byte(fs->freereg + n); } /* ** Free register 'reg', if it is neither a constant index nor ** a local variable. ) */ static void freereg (FuncState *fs, int reg) { if (reg >= luaY_nvarstack(fs)) { fs->freereg--; lua_assert(reg == fs->freereg); } } /* ** Free two registers in proper order */ static void freeregs (FuncState *fs, int r1, int r2) { if (r1 > r2) { freereg(fs, r1); freereg(fs, r2); } else { freereg(fs, r2); freereg(fs, r1); } } /* ** Free register used by expression 'e' (if any) */ static void freeexp (FuncState *fs, expdesc *e) { if (e->k == VNONRELOC) freereg(fs, e->u.info); } /* ** Free registers used by expressions 'e1' and 'e2' (if any) in proper ** order. */ static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) { int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1; int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1; freeregs(fs, r1, r2); } /* ** Add constant 'v' to prototype's list of constants (field 'k'). */ static int addk (FuncState *fs, Proto *f, TValue *v) { lua_State *L = fs->ls->L; int oldsize = f->sizek; int k = fs->nk; luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); setobj(L, &f->k[k], v); fs->nk++; luaC_barrier(L, f, v); return k; } /* ** Use scanner's table to cache position of constants in constant list ** and try to reuse constants. Because some values should not be used ** as keys (nil cannot be a key, integer keys can collapse with float ** keys), the caller must provide a useful 'key' for indexing the cache. */ static int k2proto (FuncState *fs, TValue *key, TValue *v) { TValue val; Proto *f = fs->f; int tag = luaH_get(fs->kcache, key, &val); /* query scanner table */ if (!tagisempty(tag)) { /* is there an index there? */ int k = cast_int(ivalue(&val)); /* collisions can happen only for float keys */ lua_assert(ttisfloat(key) || luaV_rawequalobj(&f->k[k], v)); return k; /* reuse index */ } else { /* constant not found; create a new entry */ int k = addk(fs, f, v); /* cache it for reuse; numerical value does not need GC barrier; table is not a metatable, so it does not need to invalidate cache */ setivalue(&val, k); luaH_set(fs->ls->L, fs->kcache, key, &val); return k; } } /* ** Add a string to list of constants and return its index. */ static int stringK (FuncState *fs, TString *s) { TValue o; setsvalue(fs->ls->L, &o, s); return k2proto(fs, &o, &o); /* use string itself as key */ } /* ** Add an integer to list of constants and return its index. */ static int luaK_intK (FuncState *fs, lua_Integer n) { TValue o; setivalue(&o, n); return k2proto(fs, &o, &o); /* use integer itself as key */ } /* ** Add a float to list of constants and return its index. Floats ** with integral values need a different key, to avoid collision ** with actual integers. To that end, we add to the number its smaller ** power-of-two fraction that is still significant in its scale. ** (For doubles, the fraction would be 2^-52). ** This method is not bulletproof: different numbers may generate the ** same key (e.g., very large numbers will overflow to 'inf') and for ** floats larger than 2^53 the result is still an integer. For those ** cases, just generate a new entry. At worst, this only wastes an entry ** with a duplicate. */ static int luaK_numberK (FuncState *fs, lua_Number r) { TValue o, kv; setfltvalue(&o, r); /* value as a TValue */ if (r == 0) { /* handle zero as a special case */ setpvalue(&kv, fs); /* use FuncState as index */ return k2proto(fs, &kv, &o); /* cannot collide */ } else { const int nbm = l_floatatt(MANT_DIG); const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1); const lua_Number k = r * (1 + q); /* key */ lua_Integer ik; setfltvalue(&kv, k); /* key as a TValue */ if (!luaV_flttointeger(k, &ik, F2Ieq)) { /* not an integer value? */ int n = k2proto(fs, &kv, &o); /* use key */ if (luaV_rawequalobj(&fs->f->k[n], &o)) /* correct value? */ return n; } /* else, either key is still an integer or there was a collision; anyway, do not try to reuse constant; instead, create a new one */ return addk(fs, fs->f, &o); } } /* ** Add a false to list of constants and return its index. */ static int boolF (FuncState *fs) { TValue o; setbfvalue(&o); return k2proto(fs, &o, &o); /* use boolean itself as key */ } /* ** Add a true to list of constants and return its index. */ static int boolT (FuncState *fs) { TValue o; setbtvalue(&o); return k2proto(fs, &o, &o); /* use boolean itself as key */ } /* ** Add nil to list of constants and return its index. */ static int nilK (FuncState *fs) { TValue k, v; setnilvalue(&v); /* cannot use nil as key; instead use table itself */ sethvalue(fs->ls->L, &k, fs->kcache); return k2proto(fs, &k, &v); } /* ** Check whether 'i' can be stored in an 'sC' operand. Equivalent to ** (0 <= int2sC(i) && int2sC(i) <= MAXARG_C) but without risk of ** overflows in the hidden addition inside 'int2sC'. */ static int fitsC (lua_Integer i) { return (l_castS2U(i) + OFFSET_sC <= cast_uint(MAXARG_C)); } /* ** Check whether 'i' can be stored in an 'sBx' operand. */ static int fitsBx (lua_Integer i) { return (-OFFSET_sBx <= i && i <= MAXARG_Bx - OFFSET_sBx); } void luaK_int (FuncState *fs, int reg, lua_Integer i) { if (fitsBx(i)) codeAsBx(fs, OP_LOADI, reg, cast_int(i)); else luaK_codek(fs, reg, luaK_intK(fs, i)); } static void luaK_float (FuncState *fs, int reg, lua_Number f) { lua_Integer fi; if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi)) codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); else luaK_codek(fs, reg, luaK_numberK(fs, f)); } /* ** Get the value of 'var' in a register and generate an opcode to check ** whether that register is nil. 'k' is the index of the variable name ** in the list of constants. If its value cannot be encoded in Bx, a 0 ** will use '?' for the name. */ void luaK_codecheckglobal (FuncState *fs, expdesc *var, int k, int line) { luaK_exp2anyreg(fs, var); luaK_fixline(fs, line); k = (k >= MAXARG_Bx) ? 0 : k + 1; luaK_codeABx(fs, OP_ERRNNIL, var->u.info, k); luaK_fixline(fs, line); freeexp(fs, var); } /* ** Convert a constant in 'v' into an expression description 'e' */ static void const2exp (TValue *v, expdesc *e) { switch (ttypetag(v)) { case LUA_VNUMINT: e->k = VKINT; e->u.ival = ivalue(v); break; case LUA_VNUMFLT: e->k = VKFLT; e->u.nval = fltvalue(v); break; case LUA_VFALSE: e->k = VFALSE; break; case LUA_VTRUE: e->k = VTRUE; break; case LUA_VNIL: e->k = VNIL; break; case LUA_VSHRSTR: case LUA_VLNGSTR: e->k = VKSTR; e->u.strval = tsvalue(v); break; default: lua_assert(0); } } /* ** Fix an expression to return the number of results 'nresults'. ** 'e' must be a multi-ret expression (function call or vararg). */ void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { Instruction *pc = &getinstruction(fs, e); luaY_checklimit(fs, nresults + 1, MAXARG_C, "multiple results"); if (e->k == VCALL) /* expression is an open function call? */ SETARG_C(*pc, nresults + 1); else { lua_assert(e->k == VVARARG); SETARG_C(*pc, nresults + 1); SETARG_A(*pc, fs->freereg); luaK_reserveregs(fs, 1); } } /* ** Convert a VKSTR to a VK */ static int str2K (FuncState *fs, expdesc *e) { lua_assert(e->k == VKSTR); e->u.info = stringK(fs, e->u.strval); e->k = VK; return e->u.info; } /* ** Fix an expression to return one result. ** If expression is not a multi-ret expression (function call or ** vararg), it already returns one result, so nothing needs to be done. ** Function calls become VNONRELOC expressions (as its result comes ** fixed in the base register of the call), while vararg expressions ** become VRELOC (as OP_VARARG puts its results where it wants). ** (Calls are created returning one result, so that does not need ** to be fixed.) */ void luaK_setoneret (FuncState *fs, expdesc *e) { if (e->k == VCALL) { /* expression is an open function call? */ /* already returns 1 value */ lua_assert(GETARG_C(getinstruction(fs, e)) == 2); e->k = VNONRELOC; /* result has fixed position */ e->u.info = GETARG_A(getinstruction(fs, e)); } else if (e->k == VVARARG) { SETARG_C(getinstruction(fs, e), 2); e->k = VRELOC; /* can relocate its simple result */ } } /* ** Change a vararg parameter into a regular local variable */ void luaK_vapar2local (FuncState *fs, expdesc *var) { needvatab(fs->f); /* function will need a vararg table */ /* now a vararg parameter is equivalent to a regular local variable */ var->k = VLOCAL; } /* ** Ensure that expression 'e' is not a variable (nor a ). ** (Expression still may have jump lists.) */ void luaK_dischargevars (FuncState *fs, expdesc *e) { switch (e->k) { case VCONST: { const2exp(const2val(fs, e), e); break; } case VVARGVAR: { luaK_vapar2local(fs, e); /* turn it into a local variable */ } /* FALLTHROUGH */ case VLOCAL: { /* already in a register */ int temp = e->u.var.ridx; e->u.info = temp; /* (can't do a direct assignment; values overlap) */ e->k = VNONRELOC; /* becomes a non-relocatable value */ break; } case VUPVAL: { /* move value to some (pending) register */ e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0); e->k = VRELOC; break; } case VINDEXUP: { e->u.info = luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx); e->k = VRELOC; break; } case VINDEXI: { freereg(fs, e->u.ind.t); e->u.info = luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx); e->k = VRELOC; break; } case VINDEXSTR: { freereg(fs, e->u.ind.t); e->u.info = luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx); e->k = VRELOC; break; } case VINDEXED: { freeregs(fs, e->u.ind.t, e->u.ind.idx); e->u.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx); e->k = VRELOC; break; } case VVARGIND: { freeregs(fs, e->u.ind.t, e->u.ind.idx); e->u.info = luaK_codeABC(fs, OP_GETVARG, 0, e->u.ind.t, e->u.ind.idx); e->k = VRELOC; break; } case VVARARG: case VCALL: { luaK_setoneret(fs, e); break; } default: break; /* there is one value available (somewhere) */ } } /* ** Ensure expression value is in register 'reg', making 'e' a ** non-relocatable expression. ** (Expression still may have jump lists.) */ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { luaK_dischargevars(fs, e); switch (e->k) { case VNIL: { luaK_nil(fs, reg, 1); break; } case VFALSE: { luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0); break; } case VTRUE: { luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0); break; } case VKSTR: { str2K(fs, e); } /* FALLTHROUGH */ case VK: { luaK_codek(fs, reg, e->u.info); break; } case VKFLT: { luaK_float(fs, reg, e->u.nval); break; } case VKINT: { luaK_int(fs, reg, e->u.ival); break; } case VRELOC: { Instruction *pc = &getinstruction(fs, e); SETARG_A(*pc, reg); /* instruction will put result in 'reg' */ break; } case VNONRELOC: { if (reg != e->u.info) luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0); break; } default: { lua_assert(e->k == VJMP); return; /* nothing to do... */ } } e->u.info = reg; e->k = VNONRELOC; } /* ** Ensure expression value is in a register, making 'e' a ** non-relocatable expression. ** (Expression still may have jump lists.) */ static void discharge2anyreg (FuncState *fs, expdesc *e) { if (e->k != VNONRELOC) { /* no fixed register yet? */ luaK_reserveregs(fs, 1); /* get a register */ discharge2reg(fs, e, fs->freereg-1); /* put value there */ } } static int code_loadbool (FuncState *fs, int A, OpCode op) { luaK_getlabel(fs); /* those instructions may be jump targets */ return luaK_codeABC(fs, op, A, 0, 0); } /* ** check whether list has any jump that do not produce a value ** or produce an inverted value */ static int need_value (FuncState *fs, int list) { for (; list != NO_JUMP; list = getjump(fs, list)) { Instruction i = *getjumpcontrol(fs, list); if (GET_OPCODE(i) != OP_TESTSET) return 1; } return 0; /* not found */ } /* ** Ensures final expression result (which includes results from its ** jump lists) is in register 'reg'. ** If expression has jumps, need to patch these jumps either to ** its final position or to "load" instructions (for those tests ** that do not produce values). */ static void exp2reg (FuncState *fs, expdesc *e, int reg) { discharge2reg(fs, e, reg); if (e->k == VJMP) /* expression itself is a test? */ luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */ if (hasjumps(e)) { int final; /* position after whole expression */ int p_f = NO_JUMP; /* position of an eventual LOAD false */ int p_t = NO_JUMP; /* position of an eventual LOAD true */ if (need_value(fs, e->t) || need_value(fs, e->f)) { int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); p_f = code_loadbool(fs, reg, OP_LFALSESKIP); /* skip next inst. */ p_t = code_loadbool(fs, reg, OP_LOADTRUE); /* jump around these booleans if 'e' is not a test */ luaK_patchtohere(fs, fj); } final = luaK_getlabel(fs); patchlistaux(fs, e->f, final, reg, p_f); patchlistaux(fs, e->t, final, reg, p_t); } e->f = e->t = NO_JUMP; e->u.info = reg; e->k = VNONRELOC; } /* ** Ensures final expression result is in next available register. */ void luaK_exp2nextreg (FuncState *fs, expdesc *e) { luaK_dischargevars(fs, e); freeexp(fs, e); luaK_reserveregs(fs, 1); exp2reg(fs, e, fs->freereg - 1); } /* ** Ensures final expression result is in some (any) register ** and return that register. */ int luaK_exp2anyreg (FuncState *fs, expdesc *e) { luaK_dischargevars(fs, e); if (e->k == VNONRELOC) { /* expression already has a register? */ if (!hasjumps(e)) /* no jumps? */ return e->u.info; /* result is already in a register */ if (e->u.info >= luaY_nvarstack(fs)) { /* reg. is not a local? */ exp2reg(fs, e, e->u.info); /* put final result in it */ return e->u.info; } /* else expression has jumps and cannot change its register to hold the jump values, because it is a local variable. Go through to the default case. */ } luaK_exp2nextreg(fs, e); /* default: use next available register */ return e->u.info; } /* ** Ensures final expression result is either in a register, ** in an upvalue, or it is the vararg parameter. */ void luaK_exp2anyregup (FuncState *fs, expdesc *e) { if ((e->k != VUPVAL && e->k != VVARGVAR) || hasjumps(e)) luaK_exp2anyreg(fs, e); } /* ** Ensures final expression result is either in a register ** or it is a constant. */ void luaK_exp2val (FuncState *fs, expdesc *e) { if (e->k == VJMP || hasjumps(e)) luaK_exp2anyreg(fs, e); else luaK_dischargevars(fs, e); } /* ** Try to make 'e' a K expression with an index in the range of R/K ** indices. Return true iff succeeded. */ static int luaK_exp2K (FuncState *fs, expdesc *e) { if (!hasjumps(e)) { int info; switch (e->k) { /* move constants to 'k' */ case VTRUE: info = boolT(fs); break; case VFALSE: info = boolF(fs); break; case VNIL: info = nilK(fs); break; case VKINT: info = luaK_intK(fs, e->u.ival); break; case VKFLT: info = luaK_numberK(fs, e->u.nval); break; case VKSTR: info = stringK(fs, e->u.strval); break; case VK: info = e->u.info; break; default: return 0; /* not a constant */ } if (info <= MAXINDEXRK) { /* does constant fit in 'argC'? */ e->k = VK; /* make expression a 'K' expression */ e->u.info = info; return 1; } } /* else, expression doesn't fit; leave it unchanged */ return 0; } /* ** Ensures final expression result is in a valid R/K index ** (that is, it is either in a register or in 'k' with an index ** in the range of R/K indices). ** Returns 1 iff expression is K. */ static int exp2RK (FuncState *fs, expdesc *e) { if (luaK_exp2K(fs, e)) return 1; else { /* not a constant in the right range: put it in a register */ luaK_exp2anyreg(fs, e); return 0; } } static void codeABRK (FuncState *fs, OpCode o, int A, int B, expdesc *ec) { int k = exp2RK(fs, ec); luaK_codeABCk(fs, o, A, B, ec->u.info, k); } /* ** Generate code to store result of expression 'ex' into variable 'var'. */ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { switch (var->k) { case VLOCAL: { freeexp(fs, ex); exp2reg(fs, ex, var->u.var.ridx); /* compute 'ex' into proper place */ return; } case VUPVAL: { int e = luaK_exp2anyreg(fs, ex); luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0); break; } case VINDEXUP: { codeABRK(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, ex); break; } case VINDEXI: { codeABRK(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, ex); break; } case VINDEXSTR: { codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex); break; } case VVARGIND: { needvatab(fs->f); /* function will need a vararg table */ /* now, assignment is to a regular table */ } /* FALLTHROUGH */ case VINDEXED: { codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex); break; } default: lua_assert(0); /* invalid var kind to store */ } freeexp(fs, ex); } /* ** Negate condition 'e' (where 'e' is a comparison). */ static void negatecondition (FuncState *fs, expdesc *e) { Instruction *pc = getjumpcontrol(fs, e->u.info); lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && GET_OPCODE(*pc) != OP_TEST); SETARG_k(*pc, (GETARG_k(*pc) ^ 1)); } /* ** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond' ** is true, code will jump if 'e' is true.) Return jump position. ** Optimize when 'e' is 'not' something, inverting the condition ** and removing the 'not'. */ static int jumponcond (FuncState *fs, expdesc *e, int cond) { if (e->k == VRELOC) { Instruction ie = getinstruction(fs, e); if (GET_OPCODE(ie) == OP_NOT) { removelastinstruction(fs); /* remove previous OP_NOT */ return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond); } /* else go through */ } discharge2anyreg(fs, e); freeexp(fs, e); return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond); } /* ** Emit code to go through if 'e' is true, jump otherwise. */ void luaK_goiftrue (FuncState *fs, expdesc *e) { int pc; /* pc of new jump */ luaK_dischargevars(fs, e); switch (e->k) { case VJMP: { /* condition? */ negatecondition(fs, e); /* jump when it is false */ pc = e->u.info; /* save jump position */ break; } case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: { pc = NO_JUMP; /* always true; do nothing */ break; } default: { pc = jumponcond(fs, e, 0); /* jump when false */ break; } } luaK_concat(fs, &e->f, pc); /* insert new jump in false list */ luaK_patchtohere(fs, e->t); /* true list jumps to here (to go through) */ e->t = NO_JUMP; } /* ** Emit code to go through if 'e' is false, jump otherwise. */ static void luaK_goiffalse (FuncState *fs, expdesc *e) { int pc; /* pc of new jump */ luaK_dischargevars(fs, e); switch (e->k) { case VJMP: { pc = e->u.info; /* already jump if true */ break; } case VNIL: case VFALSE: { pc = NO_JUMP; /* always false; do nothing */ break; } default: { pc = jumponcond(fs, e, 1); /* jump if true */ break; } } luaK_concat(fs, &e->t, pc); /* insert new jump in 't' list */ luaK_patchtohere(fs, e->f); /* false list jumps to here (to go through) */ e->f = NO_JUMP; } /* ** Code 'not e', doing constant folding. */ static void codenot (FuncState *fs, expdesc *e) { switch (e->k) { case VNIL: case VFALSE: { e->k = VTRUE; /* true == not nil == not false */ break; } case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: { e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */ break; } case VJMP: { negatecondition(fs, e); break; } case VRELOC: case VNONRELOC: { discharge2anyreg(fs, e); freeexp(fs, e); e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0); e->k = VRELOC; break; } default: lua_assert(0); /* cannot happen */ } /* interchange true and false lists */ { int temp = e->f; e->f = e->t; e->t = temp; } removevalues(fs, e->f); /* values are useless when negated */ removevalues(fs, e->t); } /* ** Check whether expression 'e' is a short literal string */ static int isKstr (FuncState *fs, expdesc *e) { return (e->k == VK && !hasjumps(e) && e->u.info <= MAXINDEXRK && ttisshrstring(&fs->f->k[e->u.info])); } /* ** Check whether expression 'e' is a literal integer. */ static int isKint (expdesc *e) { return (e->k == VKINT && !hasjumps(e)); } /* ** Check whether expression 'e' is a literal integer in ** proper range to fit in register C */ static int isCint (expdesc *e) { return isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); } /* ** Check whether expression 'e' is a literal integer in ** proper range to fit in register sC */ static int isSCint (expdesc *e) { return isKint(e) && fitsC(e->u.ival); } /* ** Check whether expression 'e' is a literal integer or float in ** proper range to fit in a register (sB or sC). */ static int isSCnumber (expdesc *e, int *pi, int *isfloat) { lua_Integer i; if (e->k == VKINT) i = e->u.ival; else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq)) *isfloat = 1; else return 0; /* not a number */ if (!hasjumps(e) && fitsC(i)) { *pi = int2sC(cast_int(i)); return 1; } else return 0; } /* ** Emit SELF instruction or equivalent: the code will convert ** expression 'e' into 'e.key(e,'. */ void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { int ereg, base; luaK_exp2anyreg(fs, e); ereg = e->u.info; /* register where 'e' (the receiver) was placed */ freeexp(fs, e); base = e->u.info = fs->freereg; /* base register for op_self */ e->k = VNONRELOC; /* self expression has a fixed register */ luaK_reserveregs(fs, 2); /* method and 'self' produced by op_self */ lua_assert(key->k == VKSTR); /* is method name a short string in a valid K index? */ if (strisshr(key->u.strval) && luaK_exp2K(fs, key)) { /* can use 'self' opcode */ luaK_codeABCk(fs, OP_SELF, base, ereg, key->u.info, 0); } else { /* cannot use 'self' opcode; use move+gettable */ luaK_exp2anyreg(fs, key); /* put method name in a register */ luaK_codeABC(fs, OP_MOVE, base + 1, ereg, 0); /* copy self to base+1 */ luaK_codeABC(fs, OP_GETTABLE, base, ereg, key->u.info); /* get method */ } freeexp(fs, key); } /* auxiliary function to define indexing expressions */ static void fillidxk (expdesc *t, int idx, expkind k) { t->u.ind.idx = cast_byte(idx); t->k = k; } /* ** Create expression 't[k]'. 't' must have its final result already in a ** register or upvalue. Upvalues can only be indexed by literal strings. ** Keys can be literal strings in the constant table or arbitrary ** values in registers. */ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { int keystr = -1; if (k->k == VKSTR) keystr = str2K(fs, k); lua_assert(!hasjumps(t) && (t->k == VLOCAL || t->k == VVARGVAR || t->k == VNONRELOC || t->k == VUPVAL)); if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */ luaK_exp2anyreg(fs, t); /* put it in a register */ if (t->k == VUPVAL) { lu_byte temp = cast_byte(t->u.info); /* upvalue index */ t->u.ind.t = temp; /* (can't do a direct assignment; values overlap) */ lua_assert(isKstr(fs, k)); fillidxk(t, k->u.info, VINDEXUP); /* literal short string */ } else if (t->k == VVARGVAR) { /* indexing the vararg parameter? */ int kreg = luaK_exp2anyreg(fs, k); /* put key in some register */ lu_byte vreg = cast_byte(t->u.var.ridx); /* register with vararg param. */ lua_assert(vreg == fs->f->numparams); t->u.ind.t = vreg; /* (avoid a direct assignment; values may overlap) */ fillidxk(t, kreg, VVARGIND); /* 't' represents 'vararg[k]' */ } else { /* register index of the table */ t->u.ind.t = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info); if (isKstr(fs, k)) fillidxk(t, k->u.info, VINDEXSTR); /* literal short string */ else if (isCint(k)) /* int. constant in proper range? */ fillidxk(t, cast_int(k->u.ival), VINDEXI); else fillidxk(t, luaK_exp2anyreg(fs, k), VINDEXED); /* register */ } t->u.ind.keystr = keystr; /* string index in 'k' */ t->u.ind.ro = 0; /* by default, not read-only */ } /* ** Return false if folding can raise an error. ** Bitwise operations need operands convertible to integers; division ** operations cannot have 0 as divisor. */ static int validop (int op, TValue *v1, TValue *v2) { switch (op) { case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */ lua_Integer i; return (luaV_tointegerns(v1, &i, LUA_FLOORN2I) && luaV_tointegerns(v2, &i, LUA_FLOORN2I)); } case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */ return (nvalue(v2) != 0); default: return 1; /* everything else is valid */ } } /* ** Try to "constant-fold" an operation; return 1 iff successful. ** (In this case, 'e1' has the final result.) */ static int constfolding (FuncState *fs, int op, expdesc *e1, const expdesc *e2) { TValue v1, v2, res; if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) return 0; /* non-numeric operands or not safe to fold */ luaO_rawarith(fs->ls->L, op, &v1, &v2, &res); /* does operation */ if (ttisinteger(&res)) { e1->k = VKINT; e1->u.ival = ivalue(&res); } else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */ lua_Number n = fltvalue(&res); if (luai_numisnan(n) || n == 0) return 0; e1->k = VKFLT; e1->u.nval = n; } return 1; } /* ** Convert a BinOpr to an OpCode (ORDER OPR - ORDER OP) */ l_sinline OpCode binopr2op (BinOpr opr, BinOpr baser, OpCode base) { lua_assert(baser <= opr && ((baser == OPR_ADD && opr <= OPR_SHR) || (baser == OPR_LT && opr <= OPR_LE))); return cast(OpCode, (cast_int(opr) - cast_int(baser)) + cast_int(base)); } /* ** Convert a UnOpr to an OpCode (ORDER OPR - ORDER OP) */ l_sinline OpCode unopr2op (UnOpr opr) { return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) + cast_int(OP_UNM)); } /* ** Convert a BinOpr to a tag method (ORDER OPR - ORDER TM) */ l_sinline TMS binopr2TM (BinOpr opr) { lua_assert(OPR_ADD <= opr && opr <= OPR_SHR); return cast(TMS, (cast_int(opr) - cast_int(OPR_ADD)) + cast_int(TM_ADD)); } /* ** Emit code for unary expressions that "produce values" ** (everything but 'not'). ** Expression to produce final result will be encoded in 'e'. */ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */ freeexp(fs, e); e->u.info = luaK_codeABC(fs, op, 0, r, 0); /* generate opcode */ e->k = VRELOC; /* all those operations are relocatable */ luaK_fixline(fs, line); } /* ** Emit code for binary expressions that "produce values" ** (everything but logical operators 'and'/'or' and comparison ** operators). ** Expression to produce final result will be encoded in 'e1'. */ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, OpCode op, int v2, int flip, int line, OpCode mmop, TMS event) { int v1 = luaK_exp2anyreg(fs, e1); int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0); freeexps(fs, e1, e2); e1->u.info = pc; e1->k = VRELOC; /* all those operations are relocatable */ luaK_fixline(fs, line); luaK_codeABCk(fs, mmop, v1, v2, cast_int(event), flip); /* metamethod */ luaK_fixline(fs, line); } /* ** Emit code for binary expressions that "produce values" over ** two registers. */ static void codebinexpval (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { OpCode op = binopr2op(opr, OPR_ADD, OP_ADD); int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */ /* 'e1' must be already in a register or it is a constant */ lua_assert((VNIL <= e1->k && e1->k <= VKSTR) || e1->k == VNONRELOC || e1->k == VRELOC); lua_assert(OP_ADD <= op && op <= OP_SHR); finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr)); } /* ** Code binary operators with immediate operands. */ static void codebini (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int flip, int line, TMS event) { int v2 = int2sC(cast_int(e2->u.ival)); /* immediate operand */ lua_assert(e2->k == VKINT); finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event); } /* ** Code binary operators with K operand. */ static void codebinK (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int flip, int line) { TMS event = binopr2TM(opr); int v2 = e2->u.info; /* K index */ OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK); finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event); } /* Try to code a binary operator negating its second operand. ** For the metamethod, 2nd operand must keep its original value. */ static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2, OpCode op, int line, TMS event) { if (!isKint(e2)) return 0; /* not an integer constant */ else { lua_Integer i2 = e2->u.ival; if (!(fitsC(i2) && fitsC(-i2))) return 0; /* not in the proper range */ else { /* operating a small integer constant */ int v2 = cast_int(i2); finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event); /* correct metamethod argument */ SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2)); return 1; /* successfully coded */ } } } static void swapexps (expdesc *e1, expdesc *e2) { expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */ } /* ** Code binary operators with no constant operand. */ static void codebinNoK (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int flip, int line) { if (flip) swapexps(e1, e2); /* back to original order */ codebinexpval(fs, opr, e1, e2, line); /* use standard operators */ } /* ** Code arithmetic operators ('+', '-', ...). If second operand is a ** constant in the proper range, use variant opcodes with K operands. */ static void codearith (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int flip, int line) { if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) /* K operand? */ codebinK(fs, opr, e1, e2, flip, line); else /* 'e2' is neither an immediate nor a K operand */ codebinNoK(fs, opr, e1, e2, flip, line); } /* ** Code commutative operators ('+', '*'). If first operand is a ** numeric constant, change order of operands to try to use an ** immediate or K operator. */ static void codecommutative (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2, int line) { int flip = 0; if (tonumeral(e1, NULL)) { /* is first operand a numeric constant? */ swapexps(e1, e2); /* change order */ flip = 1; } if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */ codebini(fs, OP_ADDI, e1, e2, flip, line, TM_ADD); else codearith(fs, op, e1, e2, flip, line); } /* ** Code bitwise operations; they are all commutative, so the function ** tries to put an integer constant as the 2nd operand (a K operand). */ static void codebitwise (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { int flip = 0; if (e1->k == VKINT) { swapexps(e1, e2); /* 'e2' will be the constant operand */ flip = 1; } if (e2->k == VKINT && luaK_exp2K(fs, e2)) /* K operand? */ codebinK(fs, opr, e1, e2, flip, line); else /* no constants */ codebinNoK(fs, opr, e1, e2, flip, line); } /* ** Emit code for order comparisons. When using an immediate operand, ** 'isfloat' tells whether the original value was a float. */ static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { int r1, r2; int im; int isfloat = 0; OpCode op; if (isSCnumber(e2, &im, &isfloat)) { /* use immediate operand */ r1 = luaK_exp2anyreg(fs, e1); r2 = im; op = binopr2op(opr, OPR_LT, OP_LTI); } else if (isSCnumber(e1, &im, &isfloat)) { /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */ r1 = luaK_exp2anyreg(fs, e2); r2 = im; op = binopr2op(opr, OPR_LT, OP_GTI); } else { /* regular case, compare two registers */ r1 = luaK_exp2anyreg(fs, e1); r2 = luaK_exp2anyreg(fs, e2); op = binopr2op(opr, OPR_LT, OP_LT); } freeexps(fs, e1, e2); e1->u.info = condjump(fs, op, r1, r2, isfloat, 1); e1->k = VJMP; } /* ** Emit code for equality comparisons ('==', '~='). ** 'e1' was already put as RK by 'luaK_infix'. */ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { int r1, r2; int im; int isfloat = 0; /* not needed here, but kept for symmetry */ OpCode op; if (e1->k != VNONRELOC) { lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT); swapexps(e1, e2); } r1 = luaK_exp2anyreg(fs, e1); /* 1st expression must be in register */ if (isSCnumber(e2, &im, &isfloat)) { op = OP_EQI; r2 = im; /* immediate operand */ } else if (exp2RK(fs, e2)) { /* 2nd expression is constant? */ op = OP_EQK; r2 = e2->u.info; /* constant index */ } else { op = OP_EQ; /* will compare two registers */ r2 = luaK_exp2anyreg(fs, e2); } freeexps(fs, e1, e2); e1->u.info = condjump(fs, op, r1, r2, isfloat, (opr == OPR_EQ)); e1->k = VJMP; } /* ** Apply prefix operation 'op' to expression 'e'. */ void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) { static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; luaK_dischargevars(fs, e); switch (opr) { case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */ if (constfolding(fs, cast_int(opr + LUA_OPUNM), e, &ef)) break; /* else */ /* FALLTHROUGH */ case OPR_LEN: codeunexpval(fs, unopr2op(opr), e, line); break; case OPR_NOT: codenot(fs, e); break; default: lua_assert(0); } } /* ** Process 1st operand 'v' of binary operation 'op' before reading ** 2nd operand. */ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { luaK_dischargevars(fs, v); switch (op) { case OPR_AND: { luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */ break; } case OPR_OR: { luaK_goiffalse(fs, v); /* go ahead only if 'v' is false */ break; } case OPR_CONCAT: { luaK_exp2nextreg(fs, v); /* operand must be on the stack */ break; } case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: case OPR_BAND: case OPR_BOR: case OPR_BXOR: case OPR_SHL: case OPR_SHR: { if (!tonumeral(v, NULL)) luaK_exp2anyreg(fs, v); /* else keep numeral, which may be folded or used as an immediate operand */ break; } case OPR_EQ: case OPR_NE: { if (!tonumeral(v, NULL)) exp2RK(fs, v); /* else keep numeral, which may be an immediate operand */ break; } case OPR_LT: case OPR_LE: case OPR_GT: case OPR_GE: { int dummy, dummy2; if (!isSCnumber(v, &dummy, &dummy2)) luaK_exp2anyreg(fs, v); /* else keep numeral, which may be an immediate operand */ break; } default: lua_assert(0); } } /* ** Create code for '(e1 .. e2)'. ** For '(e1 .. e2.1 .. e2.2)' (which is '(e1 .. (e2.1 .. e2.2))', ** because concatenation is right associative), merge both CONCATs. */ static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) { Instruction *ie2 = previousinstruction(fs); if (GET_OPCODE(*ie2) == OP_CONCAT) { /* is 'e2' a concatenation? */ int n = GETARG_B(*ie2); /* # of elements concatenated in 'e2' */ lua_assert(e1->u.info + 1 == GETARG_A(*ie2)); freeexp(fs, e2); SETARG_A(*ie2, e1->u.info); /* correct first element ('e1') */ SETARG_B(*ie2, n + 1); /* will concatenate one more element */ } else { /* 'e2' is not a concatenation */ luaK_codeABC(fs, OP_CONCAT, e1->u.info, 2, 0); /* new concat opcode */ freeexp(fs, e2); luaK_fixline(fs, line); } } /* ** Finalize code for binary operation, after reading 2nd operand. */ void luaK_posfix (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { luaK_dischargevars(fs, e2); if (foldbinop(opr) && constfolding(fs, cast_int(opr + LUA_OPADD), e1, e2)) return; /* done by folding */ switch (opr) { case OPR_AND: { lua_assert(e1->t == NO_JUMP); /* list closed by 'luaK_infix' */ luaK_concat(fs, &e2->f, e1->f); *e1 = *e2; break; } case OPR_OR: { lua_assert(e1->f == NO_JUMP); /* list closed by 'luaK_infix' */ luaK_concat(fs, &e2->t, e1->t); *e1 = *e2; break; } case OPR_CONCAT: { /* e1 .. e2 */ luaK_exp2nextreg(fs, e2); codeconcat(fs, e1, e2, line); break; } case OPR_ADD: case OPR_MUL: { codecommutative(fs, opr, e1, e2, line); break; } case OPR_SUB: { if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB)) break; /* coded as (r1 + -I) */ /* ELSE */ } /* FALLTHROUGH */ case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: { codearith(fs, opr, e1, e2, 0, line); break; } case OPR_BAND: case OPR_BOR: case OPR_BXOR: { codebitwise(fs, opr, e1, e2, line); break; } case OPR_SHL: { if (isSCint(e1)) { swapexps(e1, e2); codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL); /* I << r2 */ } else if (finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL)) { /* coded as (r1 >> -I) */; } else /* regular case (two registers) */ codebinexpval(fs, opr, e1, e2, line); break; } case OPR_SHR: { if (isSCint(e2)) codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */ else /* regular case (two registers) */ codebinexpval(fs, opr, e1, e2, line); break; } case OPR_EQ: case OPR_NE: { codeeq(fs, opr, e1, e2); break; } case OPR_GT: case OPR_GE: { /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */ swapexps(e1, e2); opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT); } /* FALLTHROUGH */ case OPR_LT: case OPR_LE: { codeorder(fs, opr, e1, e2); break; } default: lua_assert(0); } } /* ** Change line information associated with current position, by removing ** previous info and adding it again with new line. */ void luaK_fixline (FuncState *fs, int line) { removelastlineinfo(fs); savelineinfo(fs, fs->f, line); } void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) { Instruction *inst = &fs->f->code[pc]; int extra = asize / (MAXARG_vC + 1); /* higher bits of array size */ int rc = asize % (MAXARG_vC + 1); /* lower bits of array size */ int k = (extra > 0); /* true iff needs extra argument */ hsize = (hsize != 0) ? luaO_ceillog2(cast_uint(hsize)) + 1 : 0; *inst = CREATE_vABCk(OP_NEWTABLE, ra, hsize, rc, k); *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra); } /* ** Emit a SETLIST instruction. ** 'base' is register that keeps table; ** 'nelems' is #table plus those to be stored now; ** 'tostore' is number of values (in registers 'base + 1',...) to add to ** table (or LUA_MULTRET to add up to stack top). */ void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { lua_assert(tostore != 0); if (tostore == LUA_MULTRET) tostore = 0; if (nelems <= MAXARG_vC) luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 0); else { int extra = nelems / (MAXARG_vC + 1); nelems %= (MAXARG_vC + 1); luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 1); codeextraarg(fs, extra); } fs->freereg = cast_byte(base + 1); /* free registers with list values */ } /* ** return the final target of a jump (skipping jumps to jumps) */ static int finaltarget (Instruction *code, int i) { int count; for (count = 0; count < 100; count++) { /* avoid infinite loops */ Instruction pc = code[i]; if (GET_OPCODE(pc) != OP_JMP) break; else i += GETARG_sJ(pc) + 1; } return i; } /* ** Do a final pass over the code of a function, doing small peephole ** optimizations and adjustments. */ #include "lopnames.h" void luaK_finish (FuncState *fs) { int i; Proto *p = fs->f; if (p->flag & PF_VATAB) /* will it use a vararg table? */ p->flag &= cast_byte(~PF_VAHID); /* then it will not use hidden args. */ for (i = 0; i < fs->pc; i++) { Instruction *pc = &p->code[i]; /* avoid "not used" warnings when assert is off (for 'onelua.c') */ (void)luaP_isOT; (void)luaP_isIT; lua_assert(i == 0 || luaP_isOT(*(pc - 1)) == luaP_isIT(*pc)); switch (GET_OPCODE(*pc)) { case OP_RETURN0: case OP_RETURN1: { if (!(fs->needclose || (p->flag & PF_VAHID))) break; /* no extra work */ /* else use OP_RETURN to do the extra work */ SET_OPCODE(*pc, OP_RETURN); } /* FALLTHROUGH */ case OP_RETURN: case OP_TAILCALL: { if (fs->needclose) SETARG_k(*pc, 1); /* signal that it needs to close */ if (p->flag & PF_VAHID) /* does it use hidden arguments? */ SETARG_C(*pc, p->numparams + 1); /* signal that */ break; } case OP_GETVARG: { if (p->flag & PF_VATAB) /* function has a vararg table? */ SET_OPCODE(*pc, OP_GETTABLE); /* must get vararg there */ break; } case OP_VARARG: { if (p->flag & PF_VATAB) /* function has a vararg table? */ SETARG_k(*pc, 1); /* must get vararg there */ break; } case OP_JMP: { /* to optimize jumps to jumps */ int target = finaltarget(p->code, i); fixjump(fs, i, target); /* jump directly to final target */ break; } default: break; } } } ================================================ FILE: 3rd/lua/lcode.h ================================================ /* ** $Id: lcode.h $ ** Code generator for Lua ** See Copyright Notice in lua.h */ #ifndef lcode_h #define lcode_h #include "llex.h" #include "lobject.h" #include "lopcodes.h" #include "lparser.h" /* ** Marks the end of a patch list. It is an invalid value both as an absolute ** address, and as a list link (would link an element to itself). */ #define NO_JUMP (-1) /* ** grep "ORDER OPR" if you change these enums (ORDER OP) */ typedef enum BinOpr { /* arithmetic operators */ OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, OPR_DIV, OPR_IDIV, /* bitwise operators */ OPR_BAND, OPR_BOR, OPR_BXOR, OPR_SHL, OPR_SHR, /* string operator */ OPR_CONCAT, /* comparison operators */ OPR_EQ, OPR_LT, OPR_LE, OPR_NE, OPR_GT, OPR_GE, /* logical operators */ OPR_AND, OPR_OR, OPR_NOBINOPR } BinOpr; /* true if operation is foldable (that is, it is arithmetic or bitwise) */ #define foldbinop(op) ((op) <= OPR_SHR) #define luaK_codeABC(fs,o,a,b,c) luaK_codeABCk(fs,o,a,b,c,0) typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; /* get (pointer to) instruction of given 'expdesc' */ #define getinstruction(fs,e) ((fs)->f->code[(e)->u.info]) #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) LUAI_FUNC int luaK_code (FuncState *fs, Instruction i); LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, int Bx); LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k); LUAI_FUNC int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, int k); LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v); LUAI_FUNC void luaK_fixline (FuncState *fs, int line); LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); LUAI_FUNC void luaK_codecheckglobal (FuncState *fs, expdesc *var, int k, int line); LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n); LUAI_FUNC void luaK_vapar2local (FuncState *fs, expdesc *var); LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); LUAI_FUNC int luaK_jump (FuncState *fs); LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); LUAI_FUNC int luaK_getlabel (FuncState *fs); LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2, int line); LUAI_FUNC void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize); LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); LUAI_FUNC void luaK_finish (FuncState *fs); LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *fmt, ...); #endif ================================================ FILE: 3rd/lua/lcorolib.c ================================================ /* ** $Id: lcorolib.c $ ** Coroutine Library ** See Copyright Notice in lua.h */ #define lcorolib_c #define LUA_LIB #include "lprefix.h" #include #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "llimits.h" static lua_State *getco (lua_State *L) { lua_State *co = lua_tothread(L, 1); luaL_argexpected(L, co, 1, "thread"); return co; } /* ** Resumes a coroutine. Returns the number of results for non-error ** cases or -1 for errors. */ static int auxresume (lua_State *L, lua_State *co, int narg) { int status, nres; if (l_unlikely(!lua_checkstack(co, narg))) { lua_pushliteral(L, "too many arguments to resume"); return -1; /* error flag */ } lua_xmove(L, co, narg); status = lua_resume(co, L, narg, &nres); if (l_likely(status == LUA_OK || status == LUA_YIELD)) { if (l_unlikely(!lua_checkstack(L, nres + 1))) { lua_pop(co, nres); /* remove results anyway */ lua_pushliteral(L, "too many results to resume"); return -1; /* error flag */ } lua_xmove(co, L, nres); /* move yielded values */ return nres; } else { lua_xmove(co, L, 1); /* move error message */ return -1; /* error flag */ } } static int luaB_coresume (lua_State *L) { lua_State *co = getco(L); int r; r = auxresume(L, co, lua_gettop(L) - 1); if (l_unlikely(r < 0)) { lua_pushboolean(L, 0); lua_insert(L, -2); return 2; /* return false + error message */ } else { lua_pushboolean(L, 1); lua_insert(L, -(r + 1)); return r + 1; /* return true + 'resume' returns */ } } static int luaB_auxwrap (lua_State *L) { lua_State *co = lua_tothread(L, lua_upvalueindex(1)); int r = auxresume(L, co, lua_gettop(L)); if (l_unlikely(r < 0)) { /* error? */ int stat = lua_status(co); if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */ stat = lua_closethread(co, L); /* close its tbc variables */ lua_assert(stat != LUA_OK); lua_xmove(co, L, 1); /* move error message to the caller */ } if (stat != LUA_ERRMEM && /* not a memory error and ... */ lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */ luaL_where(L, 1); /* add extra info, if available */ lua_insert(L, -2); lua_concat(L, 2); } return lua_error(L); /* propagate error */ } return r; } static int luaB_cocreate (lua_State *L) { lua_State *NL; luaL_checktype(L, 1, LUA_TFUNCTION); NL = lua_newthread(L); lua_pushvalue(L, 1); /* move function to top */ lua_xmove(L, NL, 1); /* move function from L to NL */ return 1; } static int luaB_cowrap (lua_State *L) { luaB_cocreate(L); lua_pushcclosure(L, luaB_auxwrap, 1); return 1; } static int luaB_yield (lua_State *L) { return lua_yield(L, lua_gettop(L)); } #define COS_RUN 0 #define COS_DEAD 1 #define COS_YIELD 2 #define COS_NORM 3 static const char *const statname[] = {"running", "dead", "suspended", "normal"}; static int auxstatus (lua_State *L, lua_State *co) { if (L == co) return COS_RUN; else { switch (lua_status(co)) { case LUA_YIELD: return COS_YIELD; case LUA_OK: { lua_Debug ar; if (lua_getstack(co, 0, &ar)) /* does it have frames? */ return COS_NORM; /* it is running */ else if (lua_gettop(co) == 0) return COS_DEAD; else return COS_YIELD; /* initial state */ } default: /* some error occurred */ return COS_DEAD; } } } static int luaB_costatus (lua_State *L) { lua_State *co = getco(L); lua_pushstring(L, statname[auxstatus(L, co)]); return 1; } static lua_State *getoptco (lua_State *L) { return (lua_isnone(L, 1) ? L : getco(L)); } static int luaB_yieldable (lua_State *L) { lua_State *co = getoptco(L); lua_pushboolean(L, lua_isyieldable(co)); return 1; } static int luaB_corunning (lua_State *L) { int ismain = lua_pushthread(L); lua_pushboolean(L, ismain); return 2; } static int luaB_close (lua_State *L) { lua_State *co = getoptco(L); int status = auxstatus(L, co); switch (status) { case COS_DEAD: case COS_YIELD: { status = lua_closethread(co, L); if (status == LUA_OK) { lua_pushboolean(L, 1); return 1; } else { lua_pushboolean(L, 0); lua_xmove(co, L, 1); /* move error message */ return 2; } } case COS_NORM: return luaL_error(L, "cannot close a %s coroutine", statname[status]); case COS_RUN: lua_geti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); /* get main */ if (lua_tothread(L, -1) == co) return luaL_error(L, "cannot close main thread"); lua_closethread(co, L); /* close itself */ /* previous call does not return *//* FALLTHROUGH */ default: lua_assert(0); return 0; } } static const luaL_Reg co_funcs[] = { {"create", luaB_cocreate}, {"resume", luaB_coresume}, {"running", luaB_corunning}, {"status", luaB_costatus}, {"wrap", luaB_cowrap}, {"yield", luaB_yield}, {"isyieldable", luaB_yieldable}, {"close", luaB_close}, {NULL, NULL} }; LUAMOD_API int luaopen_coroutine (lua_State *L) { luaL_newlib(L, co_funcs); return 1; } ================================================ FILE: 3rd/lua/lctype.c ================================================ /* ** $Id: lctype.c $ ** 'ctype' functions for Lua ** See Copyright Notice in lua.h */ #define lctype_c #define LUA_CORE #include "lprefix.h" #include "lctype.h" #if !LUA_USE_CTYPE /* { */ #include #if defined (LUA_UCID) /* accept UniCode IDentifiers? */ /* consider all non-ASCII codepoints to be alphabetic */ #define NONA 0x01 #else #define NONA 0x00 /* default */ #endif LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 0x00, /* EOZ */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 8. */ NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 9. */ NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* a. */ NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* b. */ NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, 0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, /* c. */ NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* d. */ NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* e. */ NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, 0x00, 0x00, 0x00, /* f. */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; #endif /* } */ ================================================ FILE: 3rd/lua/lctype.h ================================================ /* ** $Id: lctype.h $ ** 'ctype' functions for Lua ** See Copyright Notice in lua.h */ #ifndef lctype_h #define lctype_h #include "lua.h" /* ** WARNING: the functions defined here do not necessarily correspond ** to the similar functions in the standard C ctype.h. They are ** optimized for the specific needs of Lua. */ #if !defined(LUA_USE_CTYPE) #if 'A' == 65 && '0' == 48 /* ASCII case: can use its own tables; faster and fixed */ #define LUA_USE_CTYPE 0 #else /* must use standard C ctype */ #define LUA_USE_CTYPE 1 #endif #endif #if !LUA_USE_CTYPE /* { */ #include #include "llimits.h" #define ALPHABIT 0 #define DIGITBIT 1 #define PRINTBIT 2 #define SPACEBIT 3 #define XDIGITBIT 4 #define MASK(B) (1 << (B)) /* ** add 1 to char to allow index -1 (EOZ) */ #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) /* ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' */ #define lislalpha(c) testprop(c, MASK(ALPHABIT)) #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) #define lisdigit(c) testprop(c, MASK(DIGITBIT)) #define lisspace(c) testprop(c, MASK(SPACEBIT)) #define lisprint(c) testprop(c, MASK(PRINTBIT)) #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) /* ** In ASCII, this 'ltolower' is correct for alphabetic characters and ** for '.'. That is enough for Lua needs. ('check_exp' ensures that ** the character either is an upper-case letter or is unchanged by ** the transformation, which holds for lower-case letters and '.'.) */ #define ltolower(c) \ check_exp(('A' <= (c) && (c) <= 'Z') || (c) == ((c) | ('A' ^ 'a')), \ (c) | ('A' ^ 'a')) /* one entry for each character and for -1 (EOZ) */ LUAI_DDEC(const lu_byte luai_ctype_[UCHAR_MAX + 2];) #else /* }{ */ /* ** use standard C ctypes */ #include #define lislalpha(c) (isalpha(c) || (c) == '_') #define lislalnum(c) (isalnum(c) || (c) == '_') #define lisdigit(c) (isdigit(c)) #define lisspace(c) (isspace(c)) #define lisprint(c) (isprint(c)) #define lisxdigit(c) (isxdigit(c)) #define ltolower(c) (tolower(c)) #endif /* } */ #endif ================================================ FILE: 3rd/lua/ldblib.c ================================================ /* ** $Id: ldblib.c $ ** Interface from Lua to its debug API ** See Copyright Notice in lua.h */ #define ldblib_c #define LUA_LIB #include "lprefix.h" #include #include #include #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "llimits.h" /* ** The hook table at registry[HOOKKEY] maps threads to their current ** hook function. */ static const char *const HOOKKEY = "_HOOKKEY"; /* ** If L1 != L, L1 can be in any state, and therefore there are no ** guarantees about its stack space; any push in L1 must be ** checked. */ static void checkstack (lua_State *L, lua_State *L1, int n) { if (l_unlikely(L != L1 && !lua_checkstack(L1, n))) luaL_error(L, "stack overflow"); } static int db_getregistry (lua_State *L) { lua_pushvalue(L, LUA_REGISTRYINDEX); return 1; } static int db_getmetatable (lua_State *L) { luaL_checkany(L, 1); if (!lua_getmetatable(L, 1)) { lua_pushnil(L); /* no metatable */ } return 1; } static int db_setmetatable (lua_State *L) { int t = lua_type(L, 2); luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table"); lua_settop(L, 2); lua_setmetatable(L, 1); return 1; /* return 1st argument */ } static int db_getuservalue (lua_State *L) { int n = (int)luaL_optinteger(L, 2, 1); if (lua_type(L, 1) != LUA_TUSERDATA) luaL_pushfail(L); else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) { lua_pushboolean(L, 1); return 2; } return 1; } static int db_setuservalue (lua_State *L) { int n = (int)luaL_optinteger(L, 3, 1); luaL_checktype(L, 1, LUA_TUSERDATA); luaL_checkany(L, 2); lua_settop(L, 2); if (!lua_setiuservalue(L, 1, n)) luaL_pushfail(L); return 1; } /* ** Auxiliary function used by several library functions: check for ** an optional thread as function's first argument and set 'arg' with ** 1 if this argument is present (so that functions can skip it to ** access their other arguments) */ static lua_State *getthread (lua_State *L, int *arg) { if (lua_isthread(L, 1)) { *arg = 1; return lua_tothread(L, 1); } else { *arg = 0; return L; /* function will operate over current thread */ } } /* ** Variations of 'lua_settable', used by 'db_getinfo' to put results ** from 'lua_getinfo' into result table. Key is always a string; ** value can be a string, an int, or a boolean. */ static void settabss (lua_State *L, const char *k, const char *v) { lua_pushstring(L, v); lua_setfield(L, -2, k); } static void settabsi (lua_State *L, const char *k, int v) { lua_pushinteger(L, v); lua_setfield(L, -2, k); } static void settabsb (lua_State *L, const char *k, int v) { lua_pushboolean(L, v); lua_setfield(L, -2, k); } /* ** In function 'db_getinfo', the call to 'lua_getinfo' may push ** results on the stack; later it creates the result table to put ** these objects. Function 'treatstackoption' puts the result from ** 'lua_getinfo' on top of the result table so that it can call ** 'lua_setfield'. */ static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { if (L == L1) lua_rotate(L, -2, 1); /* exchange object and table */ else lua_xmove(L1, L, 1); /* move object to the "main" stack */ lua_setfield(L, -2, fname); /* put object into table */ } /* ** Calls 'lua_getinfo' and collects all results in a new table. ** L1 needs stack space for an optional input (function) plus ** two optional outputs (function and line table) from function ** 'lua_getinfo'. */ static int db_getinfo (lua_State *L) { lua_Debug ar; int arg; lua_State *L1 = getthread(L, &arg); const char *options = luaL_optstring(L, arg+2, "flnSrtu"); checkstack(L, L1, 3); luaL_argcheck(L, options[0] != '>', arg + 2, "invalid option '>'"); if (lua_isfunction(L, arg + 1)) { /* info about a function? */ options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */ lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */ lua_xmove(L, L1, 1); } else { /* stack level */ if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) { luaL_pushfail(L); /* level out of range */ return 1; } } if (!lua_getinfo(L1, options, &ar)) return luaL_argerror(L, arg+2, "invalid option"); lua_newtable(L); /* table to collect results */ if (strchr(options, 'S')) { lua_pushlstring(L, ar.source, ar.srclen); lua_setfield(L, -2, "source"); settabss(L, "short_src", ar.short_src); settabsi(L, "linedefined", ar.linedefined); settabsi(L, "lastlinedefined", ar.lastlinedefined); settabss(L, "what", ar.what); } if (strchr(options, 'l')) settabsi(L, "currentline", ar.currentline); if (strchr(options, 'u')) { settabsi(L, "nups", ar.nups); settabsi(L, "nparams", ar.nparams); settabsb(L, "isvararg", ar.isvararg); } if (strchr(options, 'n')) { settabss(L, "name", ar.name); settabss(L, "namewhat", ar.namewhat); } if (strchr(options, 'r')) { settabsi(L, "ftransfer", ar.ftransfer); settabsi(L, "ntransfer", ar.ntransfer); } if (strchr(options, 't')) { settabsb(L, "istailcall", ar.istailcall); settabsi(L, "extraargs", ar.extraargs); } if (strchr(options, 'L')) treatstackoption(L, L1, "activelines"); if (strchr(options, 'f')) treatstackoption(L, L1, "func"); return 1; /* return table */ } static int db_getlocal (lua_State *L) { int arg; lua_State *L1 = getthread(L, &arg); int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */ if (lua_isfunction(L, arg + 1)) { /* function argument? */ lua_pushvalue(L, arg + 1); /* push function */ lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */ return 1; /* return only name (there is no value) */ } else { /* stack-level argument */ lua_Debug ar; const char *name; int level = (int)luaL_checkinteger(L, arg + 1); if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */ return luaL_argerror(L, arg+1, "level out of range"); checkstack(L, L1, 1); name = lua_getlocal(L1, &ar, nvar); if (name) { lua_xmove(L1, L, 1); /* move local value */ lua_pushstring(L, name); /* push name */ lua_rotate(L, -2, 1); /* re-order */ return 2; } else { luaL_pushfail(L); /* no name (nor value) */ return 1; } } } static int db_setlocal (lua_State *L) { int arg; const char *name; lua_State *L1 = getthread(L, &arg); lua_Debug ar; int level = (int)luaL_checkinteger(L, arg + 1); int nvar = (int)luaL_checkinteger(L, arg + 2); if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */ return luaL_argerror(L, arg+1, "level out of range"); luaL_checkany(L, arg+3); lua_settop(L, arg+3); checkstack(L, L1, 1); lua_xmove(L, L1, 1); name = lua_setlocal(L1, &ar, nvar); if (name == NULL) lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */ lua_pushstring(L, name); return 1; } /* ** get (if 'get' is true) or set an upvalue from a closure */ static int auxupvalue (lua_State *L, int get) { const char *name; int n = (int)luaL_checkinteger(L, 2); /* upvalue index */ luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */ name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); if (name == NULL) return 0; lua_pushstring(L, name); lua_insert(L, -(get+1)); /* no-op if get is false */ return get + 1; } static int db_getupvalue (lua_State *L) { return auxupvalue(L, 1); } static int db_setupvalue (lua_State *L) { luaL_checkany(L, 3); return auxupvalue(L, 0); } /* ** Check whether a given upvalue from a given closure exists and ** returns its index */ static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) { void *id; int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */ luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */ id = lua_upvalueid(L, argf, nup); if (pnup) { luaL_argcheck(L, id != NULL, argnup, "invalid upvalue index"); *pnup = nup; } return id; } static int db_upvalueid (lua_State *L) { void *id = checkupval(L, 1, 2, NULL); if (id != NULL) lua_pushlightuserdata(L, id); else luaL_pushfail(L); return 1; } static int db_upvaluejoin (lua_State *L) { int n1, n2; checkupval(L, 1, 2, &n1); checkupval(L, 3, 4, &n2); luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected"); luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected"); lua_upvaluejoin(L, 1, n1, 3, n2); return 0; } /* ** Call hook function registered at hook table for the current ** thread (if there is one) */ static void hookf (lua_State *L, lua_Debug *ar) { static const char *const hooknames[] = {"call", "return", "line", "count", "tail call"}; lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY); lua_pushthread(L); if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ if (ar->currentline >= 0) lua_pushinteger(L, ar->currentline); /* push current line */ else lua_pushnil(L); lua_assert(lua_getinfo(L, "lS", ar)); lua_call(L, 2, 0); /* call hook function */ } } /* ** Convert a string mask (for 'sethook') into a bit mask */ static int makemask (const char *smask, int count) { int mask = 0; if (strchr(smask, 'c')) mask |= LUA_MASKCALL; if (strchr(smask, 'r')) mask |= LUA_MASKRET; if (strchr(smask, 'l')) mask |= LUA_MASKLINE; if (count > 0) mask |= LUA_MASKCOUNT; return mask; } /* ** Convert a bit mask (for 'gethook') into a string mask */ static char *unmakemask (int mask, char *smask) { int i = 0; if (mask & LUA_MASKCALL) smask[i++] = 'c'; if (mask & LUA_MASKRET) smask[i++] = 'r'; if (mask & LUA_MASKLINE) smask[i++] = 'l'; smask[i] = '\0'; return smask; } static int db_sethook (lua_State *L) { int arg, mask, count; lua_Hook func; lua_State *L1 = getthread(L, &arg); if (lua_isnoneornil(L, arg+1)) { /* no hook? */ lua_settop(L, arg+1); func = NULL; mask = 0; count = 0; /* turn off hooks */ } else { const char *smask = luaL_checkstring(L, arg+2); luaL_checktype(L, arg+1, LUA_TFUNCTION); count = (int)luaL_optinteger(L, arg + 3, 0); func = hookf; mask = makemask(smask, count); } if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) { /* table just created; initialize it */ lua_pushliteral(L, "k"); lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ lua_pushvalue(L, -1); lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */ } checkstack(L, L1, 1); lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */ lua_pushvalue(L, arg + 1); /* value (hook function) */ lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */ lua_sethook(L1, func, mask, count); return 0; } static int db_gethook (lua_State *L) { int arg; lua_State *L1 = getthread(L, &arg); char buff[5]; int mask = lua_gethookmask(L1); lua_Hook hook = lua_gethook(L1); if (hook == NULL) { /* no hook? */ luaL_pushfail(L); return 1; } else if (hook != hookf) /* external hook? */ lua_pushliteral(L, "external hook"); else { /* hook table must exist */ lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY); checkstack(L, L1, 1); lua_pushthread(L1); lua_xmove(L1, L, 1); lua_rawget(L, -2); /* 1st result = hooktable[L1] */ lua_remove(L, -2); /* remove hook table */ } lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */ lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */ return 3; } static int db_debug (lua_State *L) { for (;;) { char buffer[250]; lua_writestringerror("%s", "lua_debug> "); if (fgets(buffer, sizeof(buffer), stdin) == NULL || strcmp(buffer, "cont\n") == 0) return 0; if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || lua_pcall(L, 0, 0, 0)) lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL)); lua_settop(L, 0); /* remove eventual returns */ } } static int db_traceback (lua_State *L) { int arg; lua_State *L1 = getthread(L, &arg); const char *msg = lua_tostring(L, arg + 1); if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */ lua_pushvalue(L, arg + 1); /* return it untouched */ else { int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0); luaL_traceback(L, L1, msg, level); } return 1; } static const luaL_Reg dblib[] = { {"debug", db_debug}, {"getuservalue", db_getuservalue}, {"gethook", db_gethook}, {"getinfo", db_getinfo}, {"getlocal", db_getlocal}, {"getregistry", db_getregistry}, {"getmetatable", db_getmetatable}, {"getupvalue", db_getupvalue}, {"upvaluejoin", db_upvaluejoin}, {"upvalueid", db_upvalueid}, {"setuservalue", db_setuservalue}, {"sethook", db_sethook}, {"setlocal", db_setlocal}, {"setmetatable", db_setmetatable}, {"setupvalue", db_setupvalue}, {"traceback", db_traceback}, {NULL, NULL} }; LUAMOD_API int luaopen_debug (lua_State *L) { luaL_newlib(L, dblib); return 1; } ================================================ FILE: 3rd/lua/ldebug.c ================================================ /* ** $Id: ldebug.c $ ** Debug Interface ** See Copyright Notice in lua.h */ #define ldebug_c #define LUA_CORE #include "lprefix.h" #include #include #include #include "lua.h" #include "lapi.h" #include "lcode.h" #include "ldebug.h" #include "ldo.h" #include "lfunc.h" #include "lobject.h" #include "lopcodes.h" #include "lstate.h" #include "lstring.h" #include "ltable.h" #include "ltm.h" #include "lvm.h" #define LuaClosure(f) ((f) != NULL && (f)->c.tt == LUA_VLCL) static const char strlocal[] = "local"; static const char strupval[] = "upvalue"; static const char *funcnamefromcall (lua_State *L, CallInfo *ci, const char **name); static int currentpc (CallInfo *ci) { lua_assert(isLua(ci)); return pcRel(ci->u.l.savedpc, ci_func(ci)->p); } /* ** Get a "base line" to find the line corresponding to an instruction. ** Base lines are regularly placed at MAXIWTHABS intervals, so usually ** an integer division gets the right place. When the source file has ** large sequences of empty/comment lines, it may need extra entries, ** so the original estimate needs a correction. ** If the original estimate is -1, the initial 'if' ensures that the ** 'while' will run at least once. ** The assertion that the estimate is a lower bound for the correct base ** is valid as long as the debug info has been generated with the same ** value for MAXIWTHABS or smaller. (Previous releases use a little ** smaller value.) */ static int getbaseline (const Proto *f, int pc, int *basepc) { if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) { *basepc = -1; /* start from the beginning */ return f->linedefined; } else { int i = pc / MAXIWTHABS - 1; /* get an estimate */ /* estimate must be a lower bound of the correct base */ lua_assert(i < 0 || (i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc)); while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc) i++; /* low estimate; adjust it */ *basepc = f->abslineinfo[i].pc; return f->abslineinfo[i].line; } } /* ** Get the line corresponding to instruction 'pc' in function 'f'; ** first gets a base line and from there does the increments until ** the desired instruction. */ int luaG_getfuncline (const Proto *f, int pc) { if (f->lineinfo == NULL) /* no debug information? */ return -1; else { int basepc; int baseline = getbaseline(f, pc, &basepc); while (basepc++ < pc) { /* walk until given instruction */ lua_assert(f->lineinfo[basepc] != ABSLINEINFO); baseline += f->lineinfo[basepc]; /* correct line */ } return baseline; } } static int getcurrentline (CallInfo *ci) { return luaG_getfuncline(ci_func(ci)->p, currentpc(ci)); } /* ** Set 'trap' for all active Lua frames. ** This function can be called during a signal, under "reasonable" ** assumptions. A new 'ci' is completely linked in the list before it ** becomes part of the "active" list, and we assume that pointers are ** atomic; see comment in next function. ** (A compiler doing interprocedural optimizations could, theoretically, ** reorder memory writes in such a way that the list could be ** temporarily broken while inserting a new element. We simply assume it ** has no good reasons to do that.) */ static void settraps (CallInfo *ci) { for (; ci != NULL; ci = ci->previous) if (isLua(ci)) ci->u.l.trap = 1; } /* ** This function can be called during a signal, under "reasonable" ** assumptions. ** Fields 'basehookcount' and 'hookcount' (set by 'resethookcount') ** are for debug only, and it is no problem if they get arbitrary ** values (causes at most one wrong hook call). 'hookmask' is an atomic ** value. We assume that pointers are atomic too (e.g., gcc ensures that ** for all platforms where it runs). Moreover, 'hook' is always checked ** before being called (see 'luaD_hook'). */ LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { if (func == NULL || mask == 0) { /* turn off hooks? */ mask = 0; func = NULL; } L->hook = func; L->basehookcount = count; resethookcount(L); L->hookmask = cast_byte(mask); if (mask) settraps(L->ci); /* to trace inside 'luaV_execute' */ } LUA_API lua_Hook lua_gethook (lua_State *L) { return L->hook; } LUA_API int lua_gethookmask (lua_State *L) { return L->hookmask; } LUA_API int lua_gethookcount (lua_State *L) { return L->basehookcount; } LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { int status; CallInfo *ci; if (level < 0) return 0; /* invalid (negative) level */ lua_lock(L); for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous) level--; if (level == 0 && ci != &L->base_ci) { /* level found? */ status = 1; ar->i_ci = ci; } else status = 0; /* no such level */ lua_unlock(L); return status; } static const char *upvalname (const Proto *p, int uv) { TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name); if (s == NULL) return "?"; else return getstr(s); } static const char *findvararg (CallInfo *ci, int n, StkId *pos) { if (clLvalue(s2v(ci->func.p))->p->flag & PF_VAHID) { int nextra = ci->u.l.nextraargs; if (n >= -nextra) { /* 'n' is negative */ *pos = ci->func.p - nextra - (n + 1); return "(vararg)"; /* generic name for any vararg */ } } return NULL; /* no such vararg */ } const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { StkId base = ci->func.p + 1; const char *name = NULL; if (isLua(ci)) { if (n < 0) /* access to vararg values? */ return findvararg(ci, n, pos); else name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); } if (name == NULL) { /* no 'standard' name? */ StkId limit = (ci == L->ci) ? L->top.p : ci->next->func.p; if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */ /* generic name for any valid slot */ name = isLua(ci) ? "(temporary)" : "(C temporary)"; } else return NULL; /* no name */ } if (pos) *pos = base + (n - 1); return name; } LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { const char *name; lua_lock(L); if (ar == NULL) { /* information about non-active function? */ if (!isLfunction(s2v(L->top.p - 1))) /* not a Lua function? */ name = NULL; else /* consider live variables at function start (parameters) */ name = luaF_getlocalname(clLvalue(s2v(L->top.p - 1))->p, n, 0); } else { /* active function; get information through 'ar' */ StkId pos = NULL; /* to avoid warnings */ name = luaG_findlocal(L, ar->i_ci, n, &pos); if (name) { setobjs2s(L, L->top.p, pos); api_incr_top(L); } } lua_unlock(L); return name; } LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { StkId pos = NULL; /* to avoid warnings */ const char *name; lua_lock(L); name = luaG_findlocal(L, ar->i_ci, n, &pos); if (name) { api_checkpop(L, 1); setobjs2s(L, pos, L->top.p - 1); L->top.p--; /* pop value */ } lua_unlock(L); return name; } static void funcinfo (lua_Debug *ar, Closure *cl) { if (!LuaClosure(cl)) { ar->source = "=[C]"; ar->srclen = LL("=[C]"); ar->linedefined = -1; ar->lastlinedefined = -1; ar->what = "C"; } else { const Proto *p = cl->l.p; if (p->source) { ar->source = getlstr(p->source, ar->srclen); } else { ar->source = "=?"; ar->srclen = LL("=?"); } ar->linedefined = p->linedefined; ar->lastlinedefined = p->lastlinedefined; ar->what = (ar->linedefined == 0) ? "main" : "Lua"; } luaO_chunkid(ar->short_src, ar->source, ar->srclen); } static int nextline (const Proto *p, int currentline, int pc) { if (p->lineinfo[pc] != ABSLINEINFO) return currentline + p->lineinfo[pc]; else return luaG_getfuncline(p, pc); } static void collectvalidlines (lua_State *L, Closure *f) { if (!LuaClosure(f)) { setnilvalue(s2v(L->top.p)); api_incr_top(L); } else { const Proto *p = f->l.p; int currentline = p->linedefined; Table *t = luaH_new(L); /* new table to store active lines */ sethvalue2s(L, L->top.p, t); /* push it on stack */ api_incr_top(L); if (p->lineinfo != NULL) { /* proto with debug information? */ int i; TValue v; setbtvalue(&v); /* boolean 'true' to be the value of all indices */ if (!(isvararg(p))) /* regular function? */ i = 0; /* consider all instructions */ else { /* vararg function */ lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP); currentline = nextline(p, currentline, 0); i = 1; /* skip first instruction (OP_VARARGPREP) */ } for (; i < p->sizelineinfo; i++) { /* for each instruction */ currentline = nextline(p, currentline, i); /* get its line */ luaH_setint(L, t, currentline, &v); /* table[line] = true */ } } } } static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { /* calling function is a known function? */ if (ci != NULL && !(ci->callstatus & CIST_TAIL)) return funcnamefromcall(L, ci->previous, name); else return NULL; /* no way to find a name */ } static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, Closure *f, CallInfo *ci) { int status = 1; for (; *what; what++) { switch (*what) { case 'S': { funcinfo(ar, f); break; } case 'l': { ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1; break; } case 'u': { ar->nups = (f == NULL) ? 0 : f->c.nupvalues; if (!LuaClosure(f)) { ar->isvararg = 1; ar->nparams = 0; } else { ar->isvararg = (isvararg(f->l.p)) ? 1 : 0; ar->nparams = f->l.p->numparams; } break; } case 't': { if (ci != NULL) { ar->istailcall = !!(ci->callstatus & CIST_TAIL); ar->extraargs = cast_uchar((ci->callstatus & MAX_CCMT) >> CIST_CCMT); } else { ar->istailcall = 0; ar->extraargs = 0; } break; } case 'n': { ar->namewhat = getfuncname(L, ci, &ar->name); if (ar->namewhat == NULL) { ar->namewhat = ""; /* not found */ ar->name = NULL; } break; } case 'r': { if (ci == NULL || !(ci->callstatus & CIST_HOOKED)) ar->ftransfer = ar->ntransfer = 0; else { ar->ftransfer = L->transferinfo.ftransfer; ar->ntransfer = L->transferinfo.ntransfer; } break; } case 'L': case 'f': /* handled by lua_getinfo */ break; default: status = 0; /* invalid option */ } } return status; } LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { int status; Closure *cl; CallInfo *ci; TValue *func; lua_lock(L); if (*what == '>') { ci = NULL; func = s2v(L->top.p - 1); api_check(L, ttisfunction(func), "function expected"); what++; /* skip the '>' */ L->top.p--; /* pop function */ } else { ci = ar->i_ci; func = s2v(ci->func.p); lua_assert(ttisfunction(func)); } cl = ttisclosure(func) ? clvalue(func) : NULL; status = auxgetinfo(L, what, ar, cl, ci); if (strchr(what, 'f')) { setobj2s(L, L->top.p, func); api_incr_top(L); } if (strchr(what, 'L')) collectvalidlines(L, cl); lua_unlock(L); return status; } /* ** {====================================================== ** Symbolic Execution ** ======================================================= */ static int filterpc (int pc, int jmptarget) { if (pc < jmptarget) /* is code conditional (inside a jump)? */ return -1; /* cannot know who sets that register */ else return pc; /* current position sets that register */ } /* ** Try to find last instruction before 'lastpc' that modified register 'reg'. */ static int findsetreg (const Proto *p, int lastpc, int reg) { int pc; int setreg = -1; /* keep last instruction that changed 'reg' */ int jmptarget = 0; /* any code before this address is conditional */ if (testMMMode(GET_OPCODE(p->code[lastpc]))) lastpc--; /* previous instruction was not actually executed */ for (pc = 0; pc < lastpc; pc++) { Instruction i = p->code[pc]; OpCode op = GET_OPCODE(i); int a = GETARG_A(i); int change; /* true if current instruction changed 'reg' */ switch (op) { case OP_LOADNIL: { /* set registers from 'a' to 'a+b' */ int b = GETARG_B(i); change = (a <= reg && reg <= a + b); break; } case OP_TFORCALL: { /* affect all regs above its base */ change = (reg >= a + 2); break; } case OP_CALL: case OP_TAILCALL: { /* affect all registers above base */ change = (reg >= a); break; } case OP_JMP: { /* doesn't change registers, but changes 'jmptarget' */ int b = GETARG_sJ(i); int dest = pc + 1 + b; /* jump does not skip 'lastpc' and is larger than current one? */ if (dest <= lastpc && dest > jmptarget) jmptarget = dest; /* update 'jmptarget' */ change = 0; break; } default: /* any instruction that sets A */ change = (testAMode(op) && reg == a); break; } if (change) setreg = filterpc(pc, jmptarget); } return setreg; } /* ** Find a "name" for the constant 'c'. */ static const char *kname (const Proto *p, int index, const char **name) { TValue *kvalue = &p->k[index]; if (ttisstring(kvalue)) { *name = getstr(tsvalue(kvalue)); return "constant"; } else { *name = "?"; return NULL; } } static const char *basicgetobjname (const Proto *p, int *ppc, int reg, const char **name) { int pc = *ppc; *name = luaF_getlocalname(p, reg + 1, pc); if (*name) /* is a local? */ return strlocal; /* else try symbolic execution */ *ppc = pc = findsetreg(p, pc, reg); if (pc != -1) { /* could find instruction? */ Instruction i = p->code[pc]; OpCode op = GET_OPCODE(i); switch (op) { case OP_MOVE: { int b = GETARG_B(i); /* move from 'b' to 'a' */ if (b < GETARG_A(i)) return basicgetobjname(p, ppc, b, name); /* get name for 'b' */ break; } case OP_GETUPVAL: { *name = upvalname(p, GETARG_B(i)); return strupval; } case OP_LOADK: return kname(p, GETARG_Bx(i), name); case OP_LOADKX: return kname(p, GETARG_Ax(p->code[pc + 1]), name); default: break; } } return NULL; /* could not find reasonable name */ } /* ** Find a "name" for the register 'c'. */ static void rname (const Proto *p, int pc, int c, const char **name) { const char *what = basicgetobjname(p, &pc, c, name); /* search for 'c' */ if (!(what && *what == 'c')) /* did not find a constant name? */ *name = "?"; } /* ** Check whether table being indexed by instruction 'i' is the ** environment '_ENV' */ static const char *isEnv (const Proto *p, int pc, Instruction i, int isup) { int t = GETARG_B(i); /* table index */ const char *name; /* name of indexed variable */ if (isup) /* is 't' an upvalue? */ name = upvalname(p, t); else { /* 't' is a register */ const char *what = basicgetobjname(p, &pc, t, &name); /* 'name' must be the name of a local variable (at the current level or an upvalue) */ if (what != strlocal && what != strupval) name = NULL; /* cannot be the variable _ENV */ } return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field"; } /* ** Extend 'basicgetobjname' to handle table accesses */ static const char *getobjname (const Proto *p, int lastpc, int reg, const char **name) { const char *kind = basicgetobjname(p, &lastpc, reg, name); if (kind != NULL) return kind; else if (lastpc != -1) { /* could find instruction? */ Instruction i = p->code[lastpc]; OpCode op = GET_OPCODE(i); switch (op) { case OP_GETTABUP: { int k = GETARG_C(i); /* key index */ kname(p, k, name); return isEnv(p, lastpc, i, 1); } case OP_GETTABLE: { int k = GETARG_C(i); /* key index */ rname(p, lastpc, k, name); return isEnv(p, lastpc, i, 0); } case OP_GETI: { *name = "integer index"; return "field"; } case OP_GETFIELD: { int k = GETARG_C(i); /* key index */ kname(p, k, name); return isEnv(p, lastpc, i, 0); } case OP_SELF: { int k = GETARG_C(i); /* key index */ kname(p, k, name); return "method"; } default: break; /* go through to return NULL */ } } return NULL; /* could not find reasonable name */ } /* ** Try to find a name for a function based on the code that called it. ** (Only works when function was called by a Lua function.) ** Returns what the name is (e.g., "for iterator", "method", ** "metamethod") and sets '*name' to point to the name. */ static const char *funcnamefromcode (lua_State *L, const Proto *p, int pc, const char **name) { TMS tm = (TMS)0; /* (initial value avoids warnings) */ Instruction i = p->code[pc]; /* calling instruction */ switch (GET_OPCODE(i)) { case OP_CALL: case OP_TAILCALL: return getobjname(p, pc, GETARG_A(i), name); /* get function name */ case OP_TFORCALL: { /* for iterator */ *name = "for iterator"; return "for iterator"; } /* other instructions can do calls through metamethods */ case OP_SELF: case OP_GETTABUP: case OP_GETTABLE: case OP_GETI: case OP_GETFIELD: tm = TM_INDEX; break; case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD: tm = TM_NEWINDEX; break; case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: { tm = cast(TMS, GETARG_C(i)); break; } case OP_UNM: tm = TM_UNM; break; case OP_BNOT: tm = TM_BNOT; break; case OP_LEN: tm = TM_LEN; break; case OP_CONCAT: tm = TM_CONCAT; break; case OP_EQ: tm = TM_EQ; break; /* no cases for OP_EQI and OP_EQK, as they don't call metamethods */ case OP_LT: case OP_LTI: case OP_GTI: tm = TM_LT; break; case OP_LE: case OP_LEI: case OP_GEI: tm = TM_LE; break; case OP_CLOSE: case OP_RETURN: tm = TM_CLOSE; break; default: return NULL; /* cannot find a reasonable name */ } *name = getshrstr(G(L)->tmname[tm]) + 2; return "metamethod"; } /* ** Try to find a name for a function based on how it was called. */ static const char *funcnamefromcall (lua_State *L, CallInfo *ci, const char **name) { if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ *name = "?"; return "hook"; } else if (ci->callstatus & CIST_FIN) { /* was it called as a finalizer? */ *name = "__gc"; return "metamethod"; /* report it as such */ } else if (isLua(ci)) return funcnamefromcode(L, ci_func(ci)->p, currentpc(ci), name); else return NULL; } /* }====================================================== */ /* ** Check whether pointer 'o' points to some value in the stack frame of ** the current function and, if so, returns its index. Because 'o' may ** not point to a value in this stack, we cannot compare it with the ** region boundaries (undefined behavior in ISO C). */ static int instack (CallInfo *ci, const TValue *o) { int pos; StkId base = ci->func.p + 1; for (pos = 0; base + pos < ci->top.p; pos++) { if (o == s2v(base + pos)) return pos; } return -1; /* not found */ } /* ** Checks whether value 'o' came from an upvalue. (That can only happen ** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on ** upvalues.) */ static const char *getupvalname (CallInfo *ci, const TValue *o, const char **name) { LClosure *c = ci_func(ci); int i; for (i = 0; i < c->nupvalues; i++) { if (c->upvals[i]->v.p == o) { *name = upvalname(c->p, i); return strupval; } } return NULL; } static const char *formatvarinfo (lua_State *L, const char *kind, const char *name) { if (kind == NULL) return ""; /* no information */ else return luaO_pushfstring(L, " (%s '%s')", kind, name); } /* ** Build a string with a "description" for the value 'o', such as ** "variable 'x'" or "upvalue 'y'". */ static const char *varinfo (lua_State *L, const TValue *o) { CallInfo *ci = L->ci; const char *name = NULL; /* to avoid warnings */ const char *kind = NULL; if (isLua(ci)) { kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ if (!kind) { /* not an upvalue? */ int reg = instack(ci, o); /* try a register */ if (reg >= 0) /* is 'o' a register? */ kind = getobjname(ci_func(ci)->p, currentpc(ci), reg, &name); } } return formatvarinfo(L, kind, name); } /* ** Raise a type error */ static l_noret typeerror (lua_State *L, const TValue *o, const char *op, const char *extra) { const char *t = luaT_objtypename(L, o); luaG_runerror(L, "attempt to %s a %s value%s", op, t, extra); } /* ** Raise a type error with "standard" information about the faulty ** object 'o' (using 'varinfo'). */ l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { typeerror(L, o, op, varinfo(L, o)); } /* ** Raise an error for calling a non-callable object. Try to find a name ** for the object based on how it was called ('funcnamefromcall'); if it ** cannot get a name there, try 'varinfo'. */ l_noret luaG_callerror (lua_State *L, const TValue *o) { CallInfo *ci = L->ci; const char *name = NULL; /* to avoid warnings */ const char *kind = funcnamefromcall(L, ci, &name); const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o); typeerror(L, o, "call", extra); } l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) { luaG_runerror(L, "bad 'for' %s (number expected, got %s)", what, luaT_objtypename(L, o)); } l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) { if (ttisstring(p1) || cvt2str(p1)) p1 = p2; luaG_typeerror(L, p1, "concatenate"); } l_noret luaG_opinterror (lua_State *L, const TValue *p1, const TValue *p2, const char *msg) { if (!ttisnumber(p1)) /* first operand is wrong? */ p2 = p1; /* now second is wrong */ luaG_typeerror(L, p2, msg); } /* ** Error when both values are convertible to numbers, but not to integers */ l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { lua_Integer temp; if (!luaV_tointegerns(p1, &temp, LUA_FLOORN2I)) p2 = p1; luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2)); } l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { const char *t1 = luaT_objtypename(L, p1); const char *t2 = luaT_objtypename(L, p2); if (strcmp(t1, t2) == 0) luaG_runerror(L, "attempt to compare two %s values", t1); else luaG_runerror(L, "attempt to compare %s with %s", t1, t2); } l_noret luaG_errnnil (lua_State *L, LClosure *cl, int k) { const char *globalname = "?"; /* default name if k == 0 */ if (k > 0) kname(cl->p, k - 1, &globalname); luaG_runerror(L, "global '%s' already defined", globalname); } /* add src:line information to 'msg' */ const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, int line) { if (src == NULL) /* no debug information? */ return luaO_pushfstring(L, "?:?: %s", msg); else { char buff[LUA_IDSIZE]; size_t idlen; const char *id = getlstr(src, idlen); luaO_chunkid(buff, id, idlen); return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); } } l_noret luaG_errormsg (lua_State *L) { if (L->errfunc != 0) { /* is there an error handling function? */ StkId errfunc = restorestack(L, L->errfunc); lua_assert(ttisfunction(s2v(errfunc))); setobjs2s(L, L->top.p, L->top.p - 1); /* move argument */ setobjs2s(L, L->top.p - 1, errfunc); /* push function */ L->top.p++; /* assume EXTRA_STACK */ luaD_callnoyield(L, L->top.p - 2, 1); /* call it */ } if (ttisnil(s2v(L->top.p - 1))) { /* error object is nil? */ /* change it to a proper message */ setsvalue2s(L, L->top.p - 1, luaS_newliteral(L, "")); } luaD_throw(L, LUA_ERRRUN); } l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { CallInfo *ci = L->ci; const char *msg; va_list argp; luaC_checkGC(L); /* error message uses memory */ pushvfstring(L, argp, fmt, msg); if (isLua(ci)) { /* Lua function? */ /* add source:line information */ luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */ L->top.p--; } luaG_errormsg(L); } /* ** Check whether new instruction 'newpc' is in a different line from ** previous instruction 'oldpc'. More often than not, 'newpc' is only ** one or a few instructions after 'oldpc' (it must be after, see ** caller), so try to avoid calling 'luaG_getfuncline'. If they are ** too far apart, there is a good chance of a ABSLINEINFO in the way, ** so it goes directly to 'luaG_getfuncline'. */ static int changedline (const Proto *p, int oldpc, int newpc) { if (p->lineinfo == NULL) /* no debug information? */ return 0; if (newpc - oldpc < MAXIWTHABS / 2) { /* not too far apart? */ int delta = 0; /* line difference */ int pc = oldpc; for (;;) { int lineinfo = p->lineinfo[++pc]; if (lineinfo == ABSLINEINFO) break; /* cannot compute delta; fall through */ delta += lineinfo; if (pc == newpc) return (delta != 0); /* delta computed successfully */ } } /* either instructions are too far apart or there is an absolute line info in the way; compute line difference explicitly */ return (luaG_getfuncline(p, oldpc) != luaG_getfuncline(p, newpc)); } /* ** Traces Lua calls. If code is running the first instruction of a function, ** and function is not vararg, and it is not coming from an yield, ** calls 'luaD_hookcall'. (Vararg functions will call 'luaD_hookcall' ** after adjusting its variable arguments; otherwise, they could call ** a line/count hook before the call hook. Functions coming from ** an yield already called 'luaD_hookcall' before yielding.) */ int luaG_tracecall (lua_State *L) { CallInfo *ci = L->ci; Proto *p = ci_func(ci)->p; ci->u.l.trap = 1; /* ensure hooks will be checked */ if (ci->u.l.savedpc == p->code) { /* first instruction (not resuming)? */ if (isvararg(p)) return 0; /* hooks will start at VARARGPREP instruction */ else if (!(ci->callstatus & CIST_HOOKYIELD)) /* not yielded? */ luaD_hookcall(L, ci); /* check 'call' hook */ } return 1; /* keep 'trap' on */ } /* ** Traces the execution of a Lua function. Called before the execution ** of each opcode, when debug is on. 'L->oldpc' stores the last ** instruction traced, to detect line changes. When entering a new ** function, 'npci' will be zero and will test as a new line whatever ** the value of 'oldpc'. Some exceptional conditions may return to ** a function without setting 'oldpc'. In that case, 'oldpc' may be ** invalid; if so, use zero as a valid value. (A wrong but valid 'oldpc' ** at most causes an extra call to a line hook.) ** This function is not "Protected" when called, so it should correct ** 'L->top.p' before calling anything that can run the GC. */ int luaG_traceexec (lua_State *L, const Instruction *pc) { CallInfo *ci = L->ci; lu_byte mask = cast_byte(L->hookmask); const Proto *p = ci_func(ci)->p; int counthook; if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */ ci->u.l.trap = 0; /* don't need to stop again */ return 0; /* turn off 'trap' */ } pc++; /* reference is always next instruction */ ci->u.l.savedpc = pc; /* save 'pc' */ counthook = (mask & LUA_MASKCOUNT) && (--L->hookcount == 0); if (counthook) resethookcount(L); /* reset count */ else if (!(mask & LUA_MASKLINE)) return 1; /* no line hook and count != 0; nothing to be done now */ if (ci->callstatus & CIST_HOOKYIELD) { /* hook yielded last time? */ ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ return 1; /* do not call hook again (VM yielded, so it did not move) */ } if (!luaP_isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */ L->top.p = ci->top.p; /* correct top */ if (counthook) luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */ if (mask & LUA_MASKLINE) { /* 'L->oldpc' may be invalid; use zero in this case */ int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0; int npci = pcRel(pc, p); if (npci <= oldpc || /* call hook when jump back (loop), */ changedline(p, oldpc, npci)) { /* or when enter new line */ int newline = luaG_getfuncline(p, npci); luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */ } L->oldpc = npci; /* 'pc' of last call to line hook */ } if (L->status == LUA_YIELD) { /* did hook yield? */ if (counthook) L->hookcount = 1; /* undo decrement to zero */ ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ luaD_throw(L, LUA_YIELD); } return 1; /* keep 'trap' on */ } ================================================ FILE: 3rd/lua/ldebug.h ================================================ /* ** $Id: ldebug.h $ ** Auxiliary functions from Debug Interface module ** See Copyright Notice in lua.h */ #ifndef ldebug_h #define ldebug_h #include "lstate.h" #define pcRel(pc, p) (cast_int((pc) - (p)->code) - 1) /* Active Lua function (given call info) */ #define ci_func(ci) (clLvalue(s2v((ci)->func.p))) #define resethookcount(L) (L->hookcount = L->basehookcount) /* ** mark for entries in 'lineinfo' array that has absolute information in ** 'abslineinfo' array */ #define ABSLINEINFO (-0x80) /* ** MAXimum number of successive Instructions WiTHout ABSolute line ** information. (A power of two allows fast divisions.) */ #if !defined(MAXIWTHABS) #define MAXIWTHABS 128 #endif LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc); LUAI_FUNC const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos); LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *opname); LUAI_FUNC l_noret luaG_callerror (lua_State *L, const TValue *o); LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what); LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2); LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1, const TValue *p2, const char *msg); LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2); LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2); LUAI_FUNC l_noret luaG_errnnil (lua_State *L, LClosure *cl, int k); LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, int line); LUAI_FUNC l_noret luaG_errormsg (lua_State *L); LUAI_FUNC int luaG_traceexec (lua_State *L, const Instruction *pc); LUAI_FUNC int luaG_tracecall (lua_State *L); #endif ================================================ FILE: 3rd/lua/ldo.c ================================================ /* ** $Id: ldo.c $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ #define ldo_c #define LUA_CORE #include "lprefix.h" #include #include #include #include "lua.h" #include "lapi.h" #include "ldebug.h" #include "ldo.h" #include "lfunc.h" #include "lgc.h" #include "lmem.h" #include "lobject.h" #include "lopcodes.h" #include "lparser.h" #include "lstate.h" #include "lstring.h" #include "ltable.h" #include "ltm.h" #include "lundump.h" #include "lvm.h" #include "lzio.h" #define errorstatus(s) ((s) > LUA_YIELD) /* ** these macros allow user-specific actions when a thread is ** resumed/yielded. */ #if !defined(luai_userstateresume) #define luai_userstateresume(L,n) ((void)L) #endif #if !defined(luai_userstateyield) #define luai_userstateyield(L,n) ((void)L) #endif /* ** {====================================================== ** Error-recovery functions ** ======================================================= */ /* chained list of long jump buffers */ typedef struct lua_longjmp { struct lua_longjmp *previous; jmp_buf b; volatile TStatus status; /* error code */ } lua_longjmp; /* ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By ** default, Lua handles errors with exceptions when compiling as ** C++ code, with _longjmp/_setjmp when available (POSIX), and with ** longjmp/setjmp otherwise. */ #if !defined(LUAI_THROW) /* { */ #if defined(__cplusplus) && !defined(LUA_USE_LONGJMP) /* { */ /* C++ exceptions */ #define LUAI_THROW(L,c) throw(c) static void LUAI_TRY (lua_State *L, lua_longjmp *c, Pfunc f, void *ud) { try { f(L, ud); /* call function protected */ } catch (lua_longjmp *c1) { /* Lua error */ if (c1 != c) /* not the correct level? */ throw; /* rethrow to upper level */ } catch (...) { /* non-Lua exception */ c->status = -1; /* create some error code */ } } #elif defined(LUA_USE_POSIX) /* }{ */ /* in POSIX, use _longjmp/_setjmp (more efficient) */ #define LUAI_THROW(L,c) _longjmp((c)->b, 1) #define LUAI_TRY(L,c,f,ud) if (_setjmp((c)->b) == 0) ((f)(L, ud)) #else /* }{ */ /* ISO C handling with long jumps */ #define LUAI_THROW(L,c) longjmp((c)->b, 1) #define LUAI_TRY(L,c,f,ud) if (setjmp((c)->b) == 0) ((f)(L, ud)) #endif /* } */ #endif /* } */ void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) { if (errcode == LUA_ERRMEM) { /* memory error? */ setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ } else { lua_assert(errorstatus(errcode)); /* must be a real error */ lua_assert(!ttisnil(s2v(L->top.p - 1))); /* with a non-nil object */ setobjs2s(L, oldtop, L->top.p - 1); /* move it to 'oldtop' */ } L->top.p = oldtop + 1; /* top goes back to old top plus error object */ } l_noret luaD_throw (lua_State *L, TStatus errcode) { if (L->errorJmp) { /* thread has an error handler? */ L->errorJmp->status = errcode; /* set status */ LUAI_THROW(L, L->errorJmp); /* jump to it */ } else { /* thread has no error handler */ global_State *g = G(L); lua_State *mainth = mainthread(g); errcode = luaE_resetthread(L, errcode); /* close all upvalues */ L->status = errcode; if (mainth->errorJmp) { /* main thread has a handler? */ setobjs2s(L, mainth->top.p++, L->top.p - 1); /* copy error obj. */ luaD_throw(mainth, errcode); /* re-throw in main thread */ } else { /* no handler at all; abort */ if (g->panic) { /* panic function? */ lua_unlock(L); g->panic(L); /* call panic function (last chance to jump out) */ } abort(); } } } l_noret luaD_throwbaselevel (lua_State *L, TStatus errcode) { if (L->errorJmp) { /* unroll error entries up to the first level */ while (L->errorJmp->previous != NULL) L->errorJmp = L->errorJmp->previous; } luaD_throw(L, errcode); } TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { l_uint32 oldnCcalls = L->nCcalls; lua_longjmp lj; lj.status = LUA_OK; lj.previous = L->errorJmp; /* chain new error handler */ L->errorJmp = &lj; LUAI_TRY(L, &lj, f, ud); /* call 'f' catching errors */ L->errorJmp = lj.previous; /* restore old error handler */ L->nCcalls = oldnCcalls; return lj.status; } /* }====================================================== */ /* ** {================================================================== ** Stack reallocation ** =================================================================== */ /* some stack space for error handling */ #define STACKERRSPACE 200 /* ** LUAI_MAXSTACK limits the size of the Lua stack. ** It must fit into INT_MAX/2. */ #if !defined(LUAI_MAXSTACK) #if 1000000 < (INT_MAX / 2) #define LUAI_MAXSTACK 1000000 #else #define LUAI_MAXSTACK (INT_MAX / 2u) #endif #endif /* maximum stack size that respects size_t */ #define MAXSTACK_BYSIZET ((MAX_SIZET / sizeof(StackValue)) - STACKERRSPACE) /* ** Minimum between LUAI_MAXSTACK and MAXSTACK_BYSIZET ** (Maximum size for the stack must respect size_t.) */ #define MAXSTACK cast_int(LUAI_MAXSTACK < MAXSTACK_BYSIZET \ ? LUAI_MAXSTACK : MAXSTACK_BYSIZET) /* stack size with extra space for error handling */ #define ERRORSTACKSIZE (MAXSTACK + STACKERRSPACE) /* raise a stack error while running the message handler */ l_noret luaD_errerr (lua_State *L) { TString *msg = luaS_newliteral(L, "error in error handling"); setsvalue2s(L, L->top.p, msg); L->top.p++; /* assume EXTRA_STACK */ luaD_throw(L, LUA_ERRERR); } /* ** Check whether stack has enough space to run a simple function (such ** as a finalizer): At least BASIC_STACK_SIZE in the Lua stack and ** 2 slots in the C stack. */ int luaD_checkminstack (lua_State *L) { return ((stacksize(L) < MAXSTACK - BASIC_STACK_SIZE) && (getCcalls(L) < LUAI_MAXCCALLS - 2)); } /* ** In ISO C, any pointer use after the pointer has been deallocated is ** undefined behavior. So, before a stack reallocation, all pointers ** should be changed to offsets, and after the reallocation they should ** be changed back to pointers. As during the reallocation the pointers ** are invalid, the reallocation cannot run emergency collections. ** Alternatively, we can use the old address after the deallocation. ** That is not strict ISO C, but seems to work fine everywhere. ** The following macro chooses how strict is the code. */ #if !defined(LUAI_STRICT_ADDRESS) #define LUAI_STRICT_ADDRESS 1 #endif #if LUAI_STRICT_ADDRESS /* ** Change all pointers to the stack into offsets. */ static void relstack (lua_State *L) { CallInfo *ci; UpVal *up; L->top.offset = savestack(L, L->top.p); L->tbclist.offset = savestack(L, L->tbclist.p); for (up = L->openupval; up != NULL; up = up->u.open.next) up->v.offset = savestack(L, uplevel(up)); for (ci = L->ci; ci != NULL; ci = ci->previous) { ci->top.offset = savestack(L, ci->top.p); ci->func.offset = savestack(L, ci->func.p); } } /* ** Change back all offsets into pointers. */ static void correctstack (lua_State *L, StkId oldstack) { CallInfo *ci; UpVal *up; UNUSED(oldstack); L->top.p = restorestack(L, L->top.offset); L->tbclist.p = restorestack(L, L->tbclist.offset); for (up = L->openupval; up != NULL; up = up->u.open.next) up->v.p = s2v(restorestack(L, up->v.offset)); for (ci = L->ci; ci != NULL; ci = ci->previous) { ci->top.p = restorestack(L, ci->top.offset); ci->func.p = restorestack(L, ci->func.offset); if (isLua(ci)) ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ } } #else /* ** Assume that it is fine to use an address after its deallocation, ** as long as we do not dereference it. */ static void relstack (lua_State *L) { UNUSED(L); } /* do nothing */ /* ** Correct pointers into 'oldstack' to point into 'L->stack'. */ static void correctstack (lua_State *L, StkId oldstack) { CallInfo *ci; UpVal *up; StkId newstack = L->stack.p; if (oldstack == newstack) return; L->top.p = L->top.p - oldstack + newstack; L->tbclist.p = L->tbclist.p - oldstack + newstack; for (up = L->openupval; up != NULL; up = up->u.open.next) up->v.p = s2v(uplevel(up) - oldstack + newstack); for (ci = L->ci; ci != NULL; ci = ci->previous) { ci->top.p = ci->top.p - oldstack + newstack; ci->func.p = ci->func.p - oldstack + newstack; if (isLua(ci)) ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ } } #endif /* ** Reallocate the stack to a new size, correcting all pointers into it. ** In case of allocation error, raise an error or return false according ** to 'raiseerror'. */ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { int oldsize = stacksize(L); int i; StkId newstack; StkId oldstack = L->stack.p; lu_byte oldgcstop = G(L)->gcstopem; lua_assert(newsize <= MAXSTACK || newsize == ERRORSTACKSIZE); relstack(L); /* change pointers to offsets */ G(L)->gcstopem = 1; /* stop emergency collection */ newstack = luaM_reallocvector(L, oldstack, oldsize + EXTRA_STACK, newsize + EXTRA_STACK, StackValue); G(L)->gcstopem = oldgcstop; /* restore emergency collection */ if (l_unlikely(newstack == NULL)) { /* reallocation failed? */ correctstack(L, oldstack); /* change offsets back to pointers */ if (raiseerror) luaM_error(L); else return 0; /* do not raise an error */ } L->stack.p = newstack; correctstack(L, oldstack); /* change offsets back to pointers */ L->stack_last.p = L->stack.p + newsize; for (i = oldsize + EXTRA_STACK; i < newsize + EXTRA_STACK; i++) setnilvalue(s2v(newstack + i)); /* erase new segment */ return 1; } /* ** Try to grow the stack by at least 'n' elements. When 'raiseerror' ** is true, raises any error; otherwise, return 0 in case of errors. */ int luaD_growstack (lua_State *L, int n, int raiseerror) { int size = stacksize(L); if (l_unlikely(size > MAXSTACK)) { /* if stack is larger than maximum, thread is already using the extra space reserved for errors, that is, thread is handling a stack error; cannot grow further than that. */ lua_assert(stacksize(L) == ERRORSTACKSIZE); if (raiseerror) luaD_errerr(L); /* stack error inside message handler */ return 0; /* if not 'raiseerror', just signal it */ } else if (n < MAXSTACK) { /* avoids arithmetic overflows */ int newsize = size + (size >> 1); /* tentative new size (size * 1.5) */ int needed = cast_int(L->top.p - L->stack.p) + n; if (newsize > MAXSTACK) /* cannot cross the limit */ newsize = MAXSTACK; if (newsize < needed) /* but must respect what was asked for */ newsize = needed; if (l_likely(newsize <= MAXSTACK)) return luaD_reallocstack(L, newsize, raiseerror); } /* else stack overflow */ /* add extra size to be able to handle the error message */ luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); if (raiseerror) luaG_runerror(L, "stack overflow"); return 0; } /* ** Compute how much of the stack is being used, by computing the ** maximum top of all call frames in the stack and the current top. */ static int stackinuse (lua_State *L) { CallInfo *ci; int res; StkId lim = L->top.p; for (ci = L->ci; ci != NULL; ci = ci->previous) { if (lim < ci->top.p) lim = ci->top.p; } lua_assert(lim <= L->stack_last.p + EXTRA_STACK); res = cast_int(lim - L->stack.p) + 1; /* part of stack in use */ if (res < LUA_MINSTACK) res = LUA_MINSTACK; /* ensure a minimum size */ return res; } /* ** If stack size is more than 3 times the current use, reduce that size ** to twice the current use. (So, the final stack size is at most 2/3 the ** previous size, and half of its entries are empty.) ** As a particular case, if stack was handling a stack overflow and now ** it is not, 'max' (limited by MAXSTACK) will be smaller than ** stacksize (equal to ERRORSTACKSIZE in this case), and so the stack ** will be reduced to a "regular" size. */ void luaD_shrinkstack (lua_State *L) { int inuse = stackinuse(L); int max = (inuse > MAXSTACK / 3) ? MAXSTACK : inuse * 3; /* if thread is currently not handling a stack overflow and its size is larger than maximum "reasonable" size, shrink it */ if (inuse <= MAXSTACK && stacksize(L) > max) { int nsize = (inuse > MAXSTACK / 2) ? MAXSTACK : inuse * 2; luaD_reallocstack(L, nsize, 0); /* ok if that fails */ } else /* don't change stack */ condmovestack(L,(void)0,(void)0); /* (change only for debugging) */ luaE_shrinkCI(L); /* shrink CI list */ } void luaD_inctop (lua_State *L) { L->top.p++; luaD_checkstack(L, 1); } /* }================================================================== */ /* ** Call a hook for the given event. Make sure there is a hook to be ** called. (Both 'L->hook' and 'L->hookmask', which trigger this ** function, can be changed asynchronously by signals.) */ void luaD_hook (lua_State *L, int event, int line, int ftransfer, int ntransfer) { lua_Hook hook = L->hook; if (hook && L->allowhook) { /* make sure there is a hook */ CallInfo *ci = L->ci; ptrdiff_t top = savestack(L, L->top.p); /* preserve original 'top' */ ptrdiff_t ci_top = savestack(L, ci->top.p); /* idem for 'ci->top' */ lua_Debug ar; ar.event = event; ar.currentline = line; ar.i_ci = ci; L->transferinfo.ftransfer = ftransfer; L->transferinfo.ntransfer = ntransfer; if (isLua(ci) && L->top.p < ci->top.p) L->top.p = ci->top.p; /* protect entire activation register */ luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ if (ci->top.p < L->top.p + LUA_MINSTACK) ci->top.p = L->top.p + LUA_MINSTACK; L->allowhook = 0; /* cannot call hooks inside a hook */ ci->callstatus |= CIST_HOOKED; lua_unlock(L); (*hook)(L, &ar); lua_lock(L); lua_assert(!L->allowhook); L->allowhook = 1; ci->top.p = restorestack(L, ci_top); L->top.p = restorestack(L, top); ci->callstatus &= ~CIST_HOOKED; } } /* ** Executes a call hook for Lua functions. This function is called ** whenever 'hookmask' is not zero, so it checks whether call hooks are ** active. */ void luaD_hookcall (lua_State *L, CallInfo *ci) { L->oldpc = 0; /* set 'oldpc' for new function */ if (L->hookmask & LUA_MASKCALL) { /* is call hook on? */ int event = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL : LUA_HOOKCALL; Proto *p = ci_func(ci)->p; ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ luaD_hook(L, event, -1, 1, p->numparams); ci->u.l.savedpc--; /* correct 'pc' */ } } /* ** Executes a return hook for Lua and C functions and sets/corrects ** 'oldpc'. (Note that this correction is needed by the line hook, so it ** is done even when return hooks are off.) */ static void rethook (lua_State *L, CallInfo *ci, int nres) { if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ StkId firstres = L->top.p - nres; /* index of first result */ int delta = 0; /* correction for vararg functions */ int ftransfer; if (isLua(ci)) { Proto *p = ci_func(ci)->p; if (p->flag & PF_VAHID) delta = ci->u.l.nextraargs + p->numparams + 1; } ci->func.p += delta; /* if vararg, back to virtual 'func' */ ftransfer = cast_int(firstres - ci->func.p); luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ ci->func.p -= delta; } if (isLua(ci = ci->previous)) L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* set 'oldpc' */ } /* ** Check whether 'func' has a '__call' metafield. If so, put it in the ** stack, below original 'func', so that 'luaD_precall' can call it. ** Raise an error if there is no '__call' metafield. ** Bits CIST_CCMT in status count how many _call metamethods were ** invoked and how many corresponding extra arguments were pushed. ** (This count will be saved in the 'callstatus' of the call). ** Raise an error if this counter overflows. */ static unsigned tryfuncTM (lua_State *L, StkId func, unsigned status) { const TValue *tm; StkId p; tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); if (l_unlikely(ttisnil(tm))) /* no metamethod? */ luaG_callerror(L, s2v(func)); for (p = L->top.p; p > func; p--) /* open space for metamethod */ setobjs2s(L, p, p-1); L->top.p++; /* stack space pre-allocated by the caller */ setobj2s(L, func, tm); /* metamethod is the new function to be called */ if ((status & MAX_CCMT) == MAX_CCMT) /* is counter full? */ luaG_runerror(L, "'__call' chain too long"); return status + (1u << CIST_CCMT); /* increment counter */ } /* Generic case for 'moveresult' */ l_sinline void genmoveresults (lua_State *L, StkId res, int nres, int wanted) { StkId firstresult = L->top.p - nres; /* index of first result */ int i; if (nres > wanted) /* extra results? */ nres = wanted; /* don't need them */ for (i = 0; i < nres; i++) /* move all results to correct place */ setobjs2s(L, res + i, firstresult + i); for (; i < wanted; i++) /* complete wanted number of results */ setnilvalue(s2v(res + i)); L->top.p = res + wanted; /* top points after the last result */ } /* ** Given 'nres' results at 'firstResult', move 'fwanted-1' of them ** to 'res'. Handle most typical cases (zero results for commands, ** one result for expressions, multiple results for tail calls/single ** parameters) separated. The flag CIST_TBC in 'fwanted', if set, ** forces the switch to go to the default case. */ l_sinline void moveresults (lua_State *L, StkId res, int nres, l_uint32 fwanted) { switch (fwanted) { /* handle typical cases separately */ case 0 + 1: /* no values needed */ L->top.p = res; return; case 1 + 1: /* one value needed */ if (nres == 0) /* no results? */ setnilvalue(s2v(res)); /* adjust with nil */ else /* at least one result */ setobjs2s(L, res, L->top.p - nres); /* move it to proper place */ L->top.p = res + 1; return; case LUA_MULTRET + 1: genmoveresults(L, res, nres, nres); /* we want all results */ break; default: { /* two/more results and/or to-be-closed variables */ int wanted = get_nresults(fwanted); if (fwanted & CIST_TBC) { /* to-be-closed variables? */ L->ci->u2.nres = nres; L->ci->callstatus |= CIST_CLSRET; /* in case of yields */ res = luaF_close(L, res, CLOSEKTOP, 1); L->ci->callstatus &= ~CIST_CLSRET; if (L->hookmask) { /* if needed, call hook after '__close's */ ptrdiff_t savedres = savestack(L, res); rethook(L, L->ci, nres); res = restorestack(L, savedres); /* hook can move stack */ } if (wanted == LUA_MULTRET) wanted = nres; /* we want all results */ } genmoveresults(L, res, nres, wanted); break; } } } /* ** Finishes a function call: calls hook if necessary, moves current ** number of results to proper place, and returns to previous call ** info. If function has to close variables, hook must be called after ** that. */ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { l_uint32 fwanted = ci->callstatus & (CIST_TBC | CIST_NRESULTS); if (l_unlikely(L->hookmask) && !(fwanted & CIST_TBC)) rethook(L, ci, nres); /* move results to proper place */ moveresults(L, ci->func.p, nres, fwanted); /* function cannot be in any of these cases when returning */ lua_assert(!(ci->callstatus & (CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_CLSRET))); L->ci = ci->previous; /* back to caller (after closing variables) */ } #define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L)) /* ** Allocate and initialize CallInfo structure. At this point, the ** only valid fields in the call status are number of results, ** CIST_C (if it's a C function), and number of extra arguments. ** (All these bit-fields fit in 16-bit values.) */ l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, unsigned status, StkId top) { CallInfo *ci = L->ci = next_ci(L); /* new frame */ ci->func.p = func; lua_assert((status & ~(CIST_NRESULTS | CIST_C | MAX_CCMT)) == 0); ci->callstatus = status; ci->top.p = top; return ci; } /* ** precall for C functions */ l_sinline int precallC (lua_State *L, StkId func, unsigned status, lua_CFunction f) { int n; /* number of returns */ CallInfo *ci; checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ L->ci = ci = prepCallInfo(L, func, status | CIST_C, L->top.p + LUA_MINSTACK); lua_assert(ci->top.p <= L->stack_last.p); if (l_unlikely(L->hookmask & LUA_MASKCALL)) { int narg = cast_int(L->top.p - func) - 1; luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); } lua_unlock(L); n = (*f)(L); /* do the actual call */ lua_lock(L); api_checknelems(L, n); luaD_poscall(L, ci, n); return n; } /* ** Prepare a function for a tail call, building its call info on top ** of the current call info. 'narg1' is the number of arguments plus 1 ** (so that it includes the function itself). Return the number of ** results, if it was a C function, or -1 for a Lua function. */ int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1, int delta) { unsigned status = LUA_MULTRET + 1; retry: switch (ttypetag(s2v(func))) { case LUA_VCCL: /* C closure */ return precallC(L, func, status, clCvalue(s2v(func))->f); case LUA_VLCF: /* light C function */ return precallC(L, func, status, fvalue(s2v(func))); case LUA_VLCL: { /* Lua function */ Proto *p = clLvalue(s2v(func))->p; int fsize = p->maxstacksize; /* frame size */ int nfixparams = p->numparams; int i; checkstackp(L, fsize - delta, func); ci->func.p -= delta; /* restore 'func' (if vararg) */ for (i = 0; i < narg1; i++) /* move down function and arguments */ setobjs2s(L, ci->func.p + i, func + i); func = ci->func.p; /* moved-down function */ for (; narg1 <= nfixparams; narg1++) setnilvalue(s2v(func + narg1)); /* complete missing arguments */ ci->top.p = func + 1 + fsize; /* top for new function */ lua_assert(ci->top.p <= L->stack_last.p); ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus |= CIST_TAIL; L->top.p = func + narg1; /* set top */ return -1; } default: { /* not a function */ checkstackp(L, 1, func); /* space for metamethod */ status = tryfuncTM(L, func, status); /* try '__call' metamethod */ narg1++; goto retry; /* try again */ } } } /* ** Prepares the call to a function (C or Lua). For C functions, also do ** the call. The function to be called is at '*func'. The arguments ** are on the stack, right after the function. Returns the CallInfo ** to be executed, if it was a Lua function. Otherwise (a C function) ** returns NULL, with all the results on the stack, starting at the ** original function position. */ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { unsigned status = cast_uint(nresults + 1); lua_assert(status <= MAXRESULTS + 1); retry: switch (ttypetag(s2v(func))) { case LUA_VCCL: /* C closure */ precallC(L, func, status, clCvalue(s2v(func))->f); return NULL; case LUA_VLCF: /* light C function */ precallC(L, func, status, fvalue(s2v(func))); return NULL; case LUA_VLCL: { /* Lua function */ CallInfo *ci; Proto *p = clLvalue(s2v(func))->p; int narg = cast_int(L->top.p - func) - 1; /* number of real arguments */ int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ checkstackp(L, fsize, func); L->ci = ci = prepCallInfo(L, func, status, func + 1 + fsize); ci->u.l.savedpc = p->code; /* starting point */ for (; narg < nfixparams; narg++) setnilvalue(s2v(L->top.p++)); /* complete missing arguments */ lua_assert(ci->top.p <= L->stack_last.p); return ci; } default: { /* not a function */ checkstackp(L, 1, func); /* space for metamethod */ status = tryfuncTM(L, func, status); /* try '__call' metamethod */ goto retry; /* try again with metamethod */ } } } /* ** Call a function (C or Lua) through C. 'inc' can be 1 (increment ** number of recursive invocations in the C stack) or nyci (the same ** plus increment number of non-yieldable calls). ** This function can be called with some use of EXTRA_STACK, so it should ** check the stack before doing anything else. 'luaD_precall' already ** does that. */ l_sinline void ccall (lua_State *L, StkId func, int nResults, l_uint32 inc) { CallInfo *ci; L->nCcalls += inc; if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) { checkstackp(L, 0, func); /* free any use of EXTRA_STACK */ luaE_checkcstack(L); } if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */ ci->callstatus |= CIST_FRESH; /* mark that it is a "fresh" execute */ luaV_execute(L, ci); /* call it */ } L->nCcalls -= inc; } /* ** External interface for 'ccall' */ void luaD_call (lua_State *L, StkId func, int nResults) { ccall(L, func, nResults, 1); } /* ** Similar to 'luaD_call', but does not allow yields during the call. */ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { ccall(L, func, nResults, nyci); } /* ** Finish the job of 'lua_pcallk' after it was interrupted by an yield. ** (The caller, 'finishCcall', does the final call to 'adjustresults'.) ** The main job is to complete the 'luaD_pcall' called by 'lua_pcallk'. ** If a '__close' method yields here, eventually control will be back ** to 'finishCcall' (when that '__close' method finally returns) and ** 'finishpcallk' will run again and close any still pending '__close' ** methods. Similarly, if a '__close' method errs, 'precover' calls ** 'unroll' which calls ''finishCcall' and we are back here again, to ** close any pending '__close' methods. ** Note that, up to the call to 'luaF_close', the corresponding ** 'CallInfo' is not modified, so that this repeated run works like the ** first one (except that it has at least one less '__close' to do). In ** particular, field CIST_RECST preserves the error status across these ** multiple runs, changing only if there is a new error. */ static TStatus finishpcallk (lua_State *L, CallInfo *ci) { TStatus status = getcistrecst(ci); /* get original status */ if (l_likely(status == LUA_OK)) /* no error? */ status = LUA_YIELD; /* was interrupted by an yield */ else { /* error */ StkId func = restorestack(L, ci->u2.funcidx); L->allowhook = getoah(ci); /* restore 'allowhook' */ func = luaF_close(L, func, status, 1); /* can yield or raise an error */ luaD_seterrorobj(L, status, func); luaD_shrinkstack(L); /* restore stack size in case of overflow */ setcistrecst(ci, LUA_OK); /* clear original status */ } ci->callstatus &= ~CIST_YPCALL; L->errfunc = ci->u.c.old_errfunc; /* if it is here, there were errors or yields; unlike 'lua_pcallk', do not change status */ return status; } /* ** Completes the execution of a C function interrupted by an yield. ** The interruption must have happened while the function was either ** closing its tbc variables in 'moveresults' or executing ** 'lua_callk'/'lua_pcallk'. In the first case, it just redoes ** 'luaD_poscall'. In the second case, the call to 'finishpcallk' ** finishes the interrupted execution of 'lua_pcallk'. After that, it ** calls the continuation of the interrupted function and finally it ** completes the job of the 'luaD_call' that called the function. In ** the call to 'adjustresults', we do not know the number of results ** of the function called by 'lua_callk'/'lua_pcallk', so we are ** conservative and use LUA_MULTRET (always adjust). */ static void finishCcall (lua_State *L, CallInfo *ci) { int n; /* actual number of results from C function */ if (ci->callstatus & CIST_CLSRET) { /* was closing TBC variable? */ lua_assert(ci->callstatus & CIST_TBC); n = ci->u2.nres; /* just redo 'luaD_poscall' */ /* don't need to reset CIST_CLSRET, as it will be set again anyway */ } else { TStatus status = LUA_YIELD; /* default if there were no errors */ lua_KFunction kf = ci->u.c.k; /* continuation function */ /* must have a continuation and must be able to call it */ lua_assert(kf != NULL && yieldable(L)); if (ci->callstatus & CIST_YPCALL) /* was inside a 'lua_pcallk'? */ status = finishpcallk(L, ci); /* finish it */ adjustresults(L, LUA_MULTRET); /* finish 'lua_callk' */ lua_unlock(L); n = (*kf)(L, APIstatus(status), ci->u.c.ctx); /* call continuation */ lua_lock(L); api_checknelems(L, n); } luaD_poscall(L, ci, n); /* finish 'luaD_call' */ } /* ** Executes "full continuation" (everything in the stack) of a ** previously interrupted coroutine until the stack is empty (or another ** interruption long-jumps out of the loop). */ static void unroll (lua_State *L, void *ud) { CallInfo *ci; UNUSED(ud); while ((ci = L->ci) != &L->base_ci) { /* something in the stack */ if (!isLua(ci)) /* C function? */ finishCcall(L, ci); /* complete its execution */ else { /* Lua function */ luaV_finishOp(L); /* finish interrupted instruction */ luaV_execute(L, ci); /* execute down to higher C 'boundary' */ } } } /* ** Try to find a suspended protected call (a "recover point") for the ** given thread. */ static CallInfo *findpcall (lua_State *L) { CallInfo *ci; for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */ if (ci->callstatus & CIST_YPCALL) return ci; } return NULL; /* no pending pcall */ } /* ** Signal an error in the call to 'lua_resume', not in the execution ** of the coroutine itself. (Such errors should not be handled by any ** coroutine error handler and should not kill the coroutine.) */ static int resume_error (lua_State *L, const char *msg, int narg) { api_checkpop(L, narg); L->top.p -= narg; /* remove args from the stack */ setsvalue2s(L, L->top.p, luaS_new(L, msg)); /* push error message */ api_incr_top(L); lua_unlock(L); return LUA_ERRRUN; } /* ** Do the work for 'lua_resume' in protected mode. Most of the work ** depends on the status of the coroutine: initial state, suspended ** inside a hook, or regularly suspended (optionally with a continuation ** function), plus erroneous cases: non-suspended coroutine or dead ** coroutine. */ static void resume (lua_State *L, void *ud) { int n = *(cast(int*, ud)); /* number of arguments */ StkId firstArg = L->top.p - n; /* first argument */ CallInfo *ci = L->ci; if (L->status == LUA_OK) /* starting a coroutine? */ ccall(L, firstArg - 1, LUA_MULTRET, 0); /* just call its body */ else { /* resuming from previous yield */ lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ if (isLua(ci)) { /* yielded inside a hook? */ /* undo increment made by 'luaG_traceexec': instruction was not executed yet */ lua_assert(ci->callstatus & CIST_HOOKYIELD); ci->u.l.savedpc--; L->top.p = firstArg; /* discard arguments */ luaV_execute(L, ci); /* just continue running Lua code */ } else { /* 'common' yield */ if (ci->u.c.k != NULL) { /* does it have a continuation function? */ lua_unlock(L); n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */ lua_lock(L); api_checknelems(L, n); } luaD_poscall(L, ci, n); /* finish 'luaD_call' */ } unroll(L, NULL); /* run continuation */ } } /* ** Unrolls a coroutine in protected mode while there are recoverable ** errors, that is, errors inside a protected call. (Any error ** interrupts 'unroll', and this loop protects it again so it can ** continue.) Stops with a normal end (status == LUA_OK), an yield ** (status == LUA_YIELD), or an unprotected error ('findpcall' doesn't ** find a recover point). */ static TStatus precover (lua_State *L, TStatus status) { CallInfo *ci; while (errorstatus(status) && (ci = findpcall(L)) != NULL) { L->ci = ci; /* go down to recovery functions */ setcistrecst(ci, status); /* status to finish 'pcall' */ status = luaD_rawrunprotected(L, unroll, NULL); } return status; } LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, int *nresults) { TStatus status; lua_lock(L); if (L->status == LUA_OK) { /* may be starting a coroutine */ if (L->ci != &L->base_ci) /* not in base level? */ return resume_error(L, "cannot resume non-suspended coroutine", nargs); else if (L->top.p - (L->ci->func.p + 1) == nargs) /* no function? */ return resume_error(L, "cannot resume dead coroutine", nargs); } else if (L->status != LUA_YIELD) /* ended with errors? */ return resume_error(L, "cannot resume dead coroutine", nargs); L->nCcalls = (from) ? getCcalls(from) : 0; if (getCcalls(L) >= LUAI_MAXCCALLS) return resume_error(L, "C stack overflow", nargs); L->nCcalls++; luai_userstateresume(L, nargs); api_checkpop(L, (L->status == LUA_OK) ? nargs + 1 : nargs); status = luaD_rawrunprotected(L, resume, &nargs); /* continue running after recoverable errors */ status = precover(L, status); if (l_likely(!errorstatus(status))) lua_assert(status == L->status); /* normal end or yield */ else { /* unrecoverable error */ L->status = status; /* mark thread as 'dead' */ luaD_seterrorobj(L, status, L->top.p); /* push error message */ L->ci->top.p = L->top.p; } *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield : cast_int(L->top.p - (L->ci->func.p + 1)); lua_unlock(L); return APIstatus(status); } LUA_API int lua_isyieldable (lua_State *L) { return yieldable(L); } LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, lua_KFunction k) { CallInfo *ci; luai_userstateyield(L, nresults); lua_lock(L); ci = L->ci; api_checkpop(L, nresults); if (l_unlikely(!yieldable(L))) { if (L != mainthread(G(L))) luaG_runerror(L, "attempt to yield across a C-call boundary"); else luaG_runerror(L, "attempt to yield from outside a coroutine"); } L->status = LUA_YIELD; ci->u2.nyield = nresults; /* save number of results */ if (isLua(ci)) { /* inside a hook? */ lua_assert(!isLuacode(ci)); api_check(L, nresults == 0, "hooks cannot yield values"); api_check(L, k == NULL, "hooks cannot continue after yielding"); } else { if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ ci->u.c.ctx = ctx; /* save context */ luaD_throw(L, LUA_YIELD); } lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ lua_unlock(L); return 0; /* return to 'luaD_hook' */ } /* ** Auxiliary structure to call 'luaF_close' in protected mode. */ struct CloseP { StkId level; TStatus status; }; /* ** Auxiliary function to call 'luaF_close' in protected mode. */ static void closepaux (lua_State *L, void *ud) { struct CloseP *pcl = cast(struct CloseP *, ud); luaF_close(L, pcl->level, pcl->status, 0); } /* ** Calls 'luaF_close' in protected mode. Return the original status ** or, in case of errors, the new status. */ TStatus luaD_closeprotected (lua_State *L, ptrdiff_t level, TStatus status) { CallInfo *old_ci = L->ci; lu_byte old_allowhooks = L->allowhook; for (;;) { /* keep closing upvalues until no more errors */ struct CloseP pcl; pcl.level = restorestack(L, level); pcl.status = status; status = luaD_rawrunprotected(L, &closepaux, &pcl); if (l_likely(status == LUA_OK)) /* no more errors? */ return pcl.status; else { /* an error occurred; restore saved state and repeat */ L->ci = old_ci; L->allowhook = old_allowhooks; } } } /* ** Call the C function 'func' in protected mode, restoring basic ** thread information ('allowhook', etc.) and in particular ** its stack level in case of errors. */ TStatus luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_top, ptrdiff_t ef) { TStatus status; CallInfo *old_ci = L->ci; lu_byte old_allowhooks = L->allowhook; ptrdiff_t old_errfunc = L->errfunc; L->errfunc = ef; status = luaD_rawrunprotected(L, func, u); if (l_unlikely(status != LUA_OK)) { /* an error occurred? */ L->ci = old_ci; L->allowhook = old_allowhooks; status = luaD_closeprotected(L, old_top, status); luaD_seterrorobj(L, status, restorestack(L, old_top)); luaD_shrinkstack(L); /* restore stack size in case of overflow */ } L->errfunc = old_errfunc; return status; } /* ** Execute a protected parser. */ struct SParser { /* data to 'f_parser' */ ZIO *z; Mbuffer buff; /* dynamic structure used by the scanner */ Dyndata dyd; /* dynamic structures used by the parser */ const char *mode; const char *name; }; static void checkmode (lua_State *L, const char *mode, const char *x) { if (strchr(mode, x[0]) == NULL) { luaO_pushfstring(L, "attempt to load a %s chunk (mode is '%s')", x, mode); luaD_throw(L, LUA_ERRSYNTAX); } } static void f_parser (lua_State *L, void *ud) { LClosure *cl; struct SParser *p = cast(struct SParser *, ud); const char *mode = p->mode ? p->mode : "bt"; int c = zgetc(p->z); /* read first character */ if (c == LUA_SIGNATURE[0]) { int fixed = 0; if (strchr(mode, 'B') != NULL) fixed = 1; else checkmode(L, mode, "binary"); cl = luaU_undump(L, p->z, p->name, fixed); } else { checkmode(L, mode, "text"); cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); } lua_assert(cl->nupvalues == cl->p->sizeupvalues); luaF_initupvals(L, cl); } TStatus luaD_protectedparser (lua_State *L, ZIO *z, const char *name, const char *mode) { struct SParser p; TStatus status; incnny(L); /* cannot yield during parsing */ p.z = z; p.name = name; p.mode = mode; p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0; p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; p.dyd.label.arr = NULL; p.dyd.label.size = 0; luaZ_initbuffer(L, &p.buff); status = luaD_pcall(L, f_parser, &p, savestack(L, L->top.p), L->errfunc); luaZ_freebuffer(L, &p.buff); luaM_freearray(L, p.dyd.actvar.arr, cast_sizet(p.dyd.actvar.size)); luaM_freearray(L, p.dyd.gt.arr, cast_sizet(p.dyd.gt.size)); luaM_freearray(L, p.dyd.label.arr, cast_sizet(p.dyd.label.size)); decnny(L); return status; } ================================================ FILE: 3rd/lua/ldo.h ================================================ /* ** $Id: ldo.h $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ #ifndef ldo_h #define ldo_h #include "llimits.h" #include "lobject.h" #include "lstate.h" #include "lzio.h" /* ** Macro to check stack size and grow stack if needed. Parameters ** 'pre'/'pos' allow the macro to preserve a pointer into the ** stack across reallocations, doing the work only when needed. ** It also allows the running of one GC step when the stack is ** reallocated. ** 'condmovestack' is used in heavy tests to force a stack reallocation ** at every check. */ #if !defined(HARDSTACKTESTS) #define condmovestack(L,pre,pos) ((void)0) #else /* realloc stack keeping its size */ #define condmovestack(L,pre,pos) \ { int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; } #endif #define luaD_checkstackaux(L,n,pre,pos) \ if (l_unlikely(L->stack_last.p - L->top.p <= (n))) \ { pre; luaD_growstack(L, n, 1); pos; } \ else { condmovestack(L,pre,pos); } /* In general, 'pre'/'pos' are empty (nothing to save) */ #define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) #define savestack(L,pt) (cast_charp(pt) - cast_charp(L->stack.p)) #define restorestack(L,n) cast(StkId, cast_charp(L->stack.p) + (n)) /* macro to check stack size, preserving 'p' */ #define checkstackp(L,n,p) \ luaD_checkstackaux(L, n, \ ptrdiff_t t__ = savestack(L, p), /* save 'p' */ \ p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ /* ** Maximum depth for nested C calls, syntactical nested non-terminals, ** and other features implemented through recursion in C. (Value must ** fit in a 16-bit unsigned integer. It must also be compatible with ** the size of the C stack.) */ #if !defined(LUAI_MAXCCALLS) #define LUAI_MAXCCALLS 200 #endif /* type of protected functions, to be ran by 'runprotected' */ typedef void (*Pfunc) (lua_State *L, void *ud); LUAI_FUNC l_noret luaD_errerr (lua_State *L); LUAI_FUNC void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop); LUAI_FUNC TStatus luaD_protectedparser (lua_State *L, ZIO *z, const char *name, const char *mode); LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, int fTransfer, int nTransfer); LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1, int delta); LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); LUAI_FUNC TStatus luaD_closeprotected (lua_State *L, ptrdiff_t level, TStatus status); LUAI_FUNC TStatus luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres); LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror); LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror); LUAI_FUNC void luaD_shrinkstack (lua_State *L); LUAI_FUNC void luaD_inctop (lua_State *L); LUAI_FUNC int luaD_checkminstack (lua_State *L); LUAI_FUNC l_noret luaD_throw (lua_State *L, TStatus errcode); LUAI_FUNC l_noret luaD_throwbaselevel (lua_State *L, TStatus errcode); LUAI_FUNC TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); #endif ================================================ FILE: 3rd/lua/ldump.c ================================================ /* ** $Id: ldump.c $ ** save precompiled Lua chunks ** See Copyright Notice in lua.h */ #define ldump_c #define LUA_CORE #include "lprefix.h" #include #include #include "lua.h" #include "lapi.h" #include "lgc.h" #include "lobject.h" #include "lstate.h" #include "ltable.h" #include "lundump.h" typedef struct { lua_State *L; lua_Writer writer; void *data; size_t offset; /* current position relative to beginning of dump */ int strip; int status; Table *h; /* table to track saved strings */ lua_Unsigned nstr; /* counter for counting saved strings */ } DumpState; /* ** All high-level dumps go through dumpVector; you can change it to ** change the endianness of the result */ #define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0])) #define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char)) /* ** Dump the block of memory pointed by 'b' with given 'size'. ** 'b' should not be NULL, except for the last call signaling the end ** of the dump. */ static void dumpBlock (DumpState *D, const void *b, size_t size) { if (D->status == 0) { /* do not write anything after an error */ lua_unlock(D->L); D->status = (*D->writer)(D->L, b, size, D->data); lua_lock(D->L); D->offset += size; } } /* ** Dump enough zeros to ensure that current position is a multiple of ** 'align'. */ static void dumpAlign (DumpState *D, unsigned align) { unsigned padding = align - cast_uint(D->offset % align); if (padding < align) { /* padding == align means no padding */ static lua_Integer paddingContent = 0; lua_assert(align <= sizeof(lua_Integer)); dumpBlock(D, &paddingContent, padding); } lua_assert(D->offset % align == 0); } #define dumpVar(D,x) dumpVector(D,&x,1) static void dumpByte (DumpState *D, int y) { lu_byte x = (lu_byte)y; dumpVar(D, x); } /* ** size for 'dumpVarint' buffer: each byte can store up to 7 bits. ** (The "+6" rounds up the division.) */ #define DIBS ((l_numbits(lua_Unsigned) + 6) / 7) /* ** Dumps an unsigned integer using the MSB Varint encoding */ static void dumpVarint (DumpState *D, lua_Unsigned x) { lu_byte buff[DIBS]; unsigned n = 1; buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */ while ((x >>= 7) != 0) /* fill other bytes in reverse order */ buff[DIBS - (++n)] = cast_byte((x & 0x7f) | 0x80); dumpVector(D, buff + DIBS - n, n); } static void dumpSize (DumpState *D, size_t sz) { dumpVarint(D, cast(lua_Unsigned, sz)); } static void dumpInt (DumpState *D, int x) { lua_assert(x >= 0); dumpVarint(D, cast_uint(x)); } static void dumpNumber (DumpState *D, lua_Number x) { dumpVar(D, x); } /* ** Signed integers are coded to keep small values small. (Coding -1 as ** 0xfff...fff would use too many bytes to save a quite common value.) ** A non-negative x is coded as 2x; a negative x is coded as -2x - 1. ** (0 => 0; -1 => 1; 1 => 2; -2 => 3; 2 => 4; ...) */ static void dumpInteger (DumpState *D, lua_Integer x) { lua_Unsigned cx = (x >= 0) ? 2u * l_castS2U(x) : (2u * ~l_castS2U(x)) + 1; dumpVarint(D, cx); } /* ** Dump a String. First dump its "size": ** size==0 is followed by an index and means "reuse saved string with ** that index"; index==0 means NULL. ** size>=1 is followed by the string contents with real size==size-1 and ** means that string, which will be saved with the next available index. ** The real size does not include the ending '\0' (which is not dumped), ** so adding 1 to it cannot overflow a size_t. */ static void dumpString (DumpState *D, TString *ts) { if (ts == NULL) { dumpVarint(D, 0); /* will "reuse" NULL */ dumpVarint(D, 0); /* special index for NULL */ } else { TValue idx; int tag = luaH_getstr(D->h, ts, &idx); if (!tagisempty(tag)) { /* string already saved? */ dumpVarint(D, 0); /* reuse a saved string */ dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */ } else { /* must write and save the string */ TValue key, value; /* to save the string in the hash */ size_t size; const char *s = getlstr(ts, size); dumpSize(D, size + 1); dumpVector(D, s, size + 1); /* include ending '\0' */ D->nstr++; /* one more saved string */ setsvalue(D->L, &key, ts); /* the string is the key */ setivalue(&value, l_castU2S(D->nstr)); /* its index is the value */ luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */ /* integer value does not need barrier */ } } } static void dumpCode (DumpState *D, const Proto *f) { dumpInt(D, f->sizecode); dumpAlign(D, sizeof(f->code[0])); lua_assert(f->code != NULL); dumpVector(D, f->code, cast_uint(f->sizecode)); } static void dumpFunction (DumpState *D, const Proto *f); static void dumpConstants (DumpState *D, const Proto *f) { int i; int n = f->sizek; dumpInt(D, n); for (i = 0; i < n; i++) { const TValue *o = &f->k[i]; int tt = ttypetag(o); dumpByte(D, tt); switch (tt) { case LUA_VNUMFLT: dumpNumber(D, fltvalue(o)); break; case LUA_VNUMINT: dumpInteger(D, ivalue(o)); break; case LUA_VSHRSTR: case LUA_VLNGSTR: dumpString(D, tsvalue(o)); break; default: lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE); } } } static void dumpProtos (DumpState *D, const Proto *f) { int i; int n = f->sizep; dumpInt(D, n); for (i = 0; i < n; i++) dumpFunction(D, f->p[i]); } static void dumpUpvalues (DumpState *D, const Proto *f) { int i, n = f->sizeupvalues; dumpInt(D, n); for (i = 0; i < n; i++) { dumpByte(D, f->upvalues[i].instack); dumpByte(D, f->upvalues[i].idx); dumpByte(D, f->upvalues[i].kind); } } static void dumpDebug (DumpState *D, const Proto *f) { int i, n; n = (D->strip) ? 0 : f->sizelineinfo; dumpInt(D, n); if (f->lineinfo != NULL) dumpVector(D, f->lineinfo, cast_uint(n)); n = (D->strip) ? 0 : f->sizeabslineinfo; dumpInt(D, n); if (n > 0) { /* 'abslineinfo' is an array of structures of int's */ dumpAlign(D, sizeof(int)); dumpVector(D, f->abslineinfo, cast_uint(n)); } n = (D->strip) ? 0 : f->sizelocvars; dumpInt(D, n); for (i = 0; i < n; i++) { dumpString(D, f->locvars[i].varname); dumpInt(D, f->locvars[i].startpc); dumpInt(D, f->locvars[i].endpc); } n = (D->strip) ? 0 : f->sizeupvalues; dumpInt(D, n); for (i = 0; i < n; i++) dumpString(D, f->upvalues[i].name); } static void dumpFunction (DumpState *D, const Proto *f) { dumpInt(D, f->linedefined); dumpInt(D, f->lastlinedefined); dumpByte(D, f->numparams); dumpByte(D, f->flag); dumpByte(D, f->maxstacksize); dumpCode(D, f); dumpConstants(D, f); dumpUpvalues(D, f); dumpProtos(D, f); dumpString(D, D->strip ? NULL : f->source); dumpDebug(D, f); } #define dumpNumInfo(D, tvar, value) \ { tvar i = value; dumpByte(D, sizeof(tvar)); dumpVar(D, i); } static void dumpHeader (DumpState *D) { dumpLiteral(D, LUA_SIGNATURE); dumpByte(D, LUAC_VERSION); dumpByte(D, LUAC_FORMAT); dumpLiteral(D, LUAC_DATA); dumpNumInfo(D, int, LUAC_INT); dumpNumInfo(D, Instruction, LUAC_INST); dumpNumInfo(D, lua_Integer, LUAC_INT); dumpNumInfo(D, lua_Number, LUAC_NUM); } /* ** dump Lua function as precompiled chunk */ int luaU_dump (lua_State *L, const Proto *f, lua_Writer w, void *data, int strip) { DumpState D; D.h = luaH_new(L); /* aux. table to keep strings already dumped */ sethvalue2s(L, L->top.p, D.h); /* anchor it */ L->top.p++; D.L = L; D.writer = w; D.offset = 0; D.data = data; D.strip = strip; D.status = 0; D.nstr = 0; dumpHeader(&D); dumpByte(&D, f->sizeupvalues); dumpFunction(&D, f); dumpBlock(&D, NULL, 0); /* signal end of dump */ return D.status; } ================================================ FILE: 3rd/lua/lfunc.c ================================================ /* ** $Id: lfunc.c $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ #define lfunc_c #define LUA_CORE #include "lprefix.h" #include #include "lua.h" #include "ldebug.h" #include "ldo.h" #include "lfunc.h" #include "lgc.h" #include "lmem.h" #include "lobject.h" #include "lstate.h" #include "lstring.h" CClosure *luaF_newCclosure (lua_State *L, int nupvals) { GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals)); CClosure *c = gco2ccl(o); c->nupvalues = cast_byte(nupvals); return c; } LClosure *luaF_newLclosure (lua_State *L, int nupvals) { GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals)); LClosure *c = gco2lcl(o); c->p = NULL; c->nupvalues = cast_byte(nupvals); while (nupvals--) c->upvals[nupvals] = NULL; return c; } /* ** fill a closure with new closed upvalues */ void luaF_initupvals (lua_State *L, LClosure *cl) { int i; for (i = 0; i < cl->nupvalues; i++) { GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); UpVal *uv = gco2upv(o); uv->v.p = &uv->u.value; /* make it closed */ setnilvalue(uv->v.p); cl->upvals[i] = uv; luaC_objbarrier(L, cl, uv); } } /* ** Create a new upvalue at the given level, and link it to the list of ** open upvalues of 'L' after entry 'prev'. **/ static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) { GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); UpVal *uv = gco2upv(o); UpVal *next = *prev; uv->v.p = s2v(level); /* current value lives in the stack */ uv->u.open.next = next; /* link it to list of open upvalues */ uv->u.open.previous = prev; if (next) next->u.open.previous = &uv->u.open.next; *prev = uv; if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ L->twups = G(L)->twups; /* link it to the list */ G(L)->twups = L; } return uv; } /* ** Find and reuse, or create if it does not exist, an upvalue ** at the given level. */ UpVal *luaF_findupval (lua_State *L, StkId level) { UpVal **pp = &L->openupval; UpVal *p; lua_assert(isintwups(L) || L->openupval == NULL); while ((p = *pp) != NULL && uplevel(p) >= level) { /* search for it */ lua_assert(!isdead(G(L), p)); if (uplevel(p) == level) /* corresponding upvalue? */ return p; /* return it */ pp = &p->u.open.next; } /* not found: create a new upvalue after 'pp' */ return newupval(L, level, pp); } /* ** Call closing method for object 'obj' with error object 'err'. The ** boolean 'yy' controls whether the call is yieldable. ** (This function assumes EXTRA_STACK.) */ static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) { StkId top = L->top.p; StkId func = top; const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); setobj2s(L, top++, tm); /* will call metamethod... */ setobj2s(L, top++, obj); /* with 'self' as the 1st argument */ if (err != NULL) /* if there was an error... */ setobj2s(L, top++, err); /* then error object will be 2nd argument */ L->top.p = top; /* add function and arguments */ if (yy) luaD_call(L, func, 0); else luaD_callnoyield(L, func, 0); } /* ** Check whether object at given level has a close metamethod and raise ** an error if not. */ static void checkclosemth (lua_State *L, StkId level) { const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE); if (ttisnil(tm)) { /* no metamethod? */ int idx = cast_int(level - L->ci->func.p); /* variable index */ const char *vname = luaG_findlocal(L, L->ci, idx, NULL); if (vname == NULL) vname = "?"; luaG_runerror(L, "variable '%s' got a non-closable value", vname); } } /* ** Prepare and call a closing method. ** If status is CLOSEKTOP, the call to the closing method will be pushed ** at the top of the stack. Otherwise, values can be pushed right after ** the 'level' of the upvalue being closed, as everything after that ** won't be used again. */ static void prepcallclosemth (lua_State *L, StkId level, TStatus status, int yy) { TValue *uv = s2v(level); /* value being closed */ TValue *errobj; switch (status) { case LUA_OK: L->top.p = level + 1; /* call will be at this level */ /* FALLTHROUGH */ case CLOSEKTOP: /* don't need to change top */ errobj = NULL; /* no error object */ break; default: /* 'luaD_seterrorobj' will set top to level + 2 */ errobj = s2v(level + 1); /* error object goes after 'uv' */ luaD_seterrorobj(L, status, level + 1); /* set error object */ break; } callclosemethod(L, uv, errobj, yy); } /* Maximum value for deltas in 'tbclist' */ #define MAXDELTA USHRT_MAX /* ** Insert a variable in the list of to-be-closed variables. */ void luaF_newtbcupval (lua_State *L, StkId level) { lua_assert(level > L->tbclist.p); if (l_isfalse(s2v(level))) return; /* false doesn't need to be closed */ checkclosemth(L, level); /* value must have a close method */ while (cast_uint(level - L->tbclist.p) > MAXDELTA) { L->tbclist.p += MAXDELTA; /* create a dummy node at maximum delta */ L->tbclist.p->tbclist.delta = 0; } level->tbclist.delta = cast(unsigned short, level - L->tbclist.p); L->tbclist.p = level; } void luaF_unlinkupval (UpVal *uv) { lua_assert(upisopen(uv)); *uv->u.open.previous = uv->u.open.next; if (uv->u.open.next) uv->u.open.next->u.open.previous = uv->u.open.previous; } /* ** Close all upvalues up to the given stack level. */ void luaF_closeupval (lua_State *L, StkId level) { UpVal *uv; while ((uv = L->openupval) != NULL && uplevel(uv) >= level) { TValue *slot = &uv->u.value; /* new position for value */ lua_assert(uplevel(uv) < L->top.p); luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */ setobj(L, slot, uv->v.p); /* move value to upvalue slot */ uv->v.p = slot; /* now current value lives here */ if (!iswhite(uv)) { /* neither white nor dead? */ nw2black(uv); /* closed upvalues cannot be gray */ luaC_barrier(L, uv, slot); } } } /* ** Remove first element from the tbclist plus its dummy nodes. */ static void poptbclist (lua_State *L) { StkId tbc = L->tbclist.p; lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */ tbc -= tbc->tbclist.delta; while (tbc > L->stack.p && tbc->tbclist.delta == 0) tbc -= MAXDELTA; /* remove dummy nodes */ L->tbclist.p = tbc; } /* ** Close all upvalues and to-be-closed variables up to the given stack ** level. Return restored 'level'. */ StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy) { ptrdiff_t levelrel = savestack(L, level); luaF_closeupval(L, level); /* first, close the upvalues */ while (L->tbclist.p >= level) { /* traverse tbc's down to that level */ StkId tbc = L->tbclist.p; /* get variable index */ poptbclist(L); /* remove it from list */ prepcallclosemth(L, tbc, status, yy); /* close variable */ level = restorestack(L, levelrel); } return level; } Proto *luaF_newproto (lua_State *L) { GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto)); Proto *f = gco2p(o); f->k = NULL; f->sizek = 0; f->p = NULL; f->sizep = 0; f->code = NULL; f->sizecode = 0; f->lineinfo = NULL; f->sizelineinfo = 0; f->abslineinfo = NULL; f->sizeabslineinfo = 0; f->upvalues = NULL; f->sizeupvalues = 0; f->numparams = 0; f->flag = 0; f->maxstacksize = 0; f->locvars = NULL; f->sizelocvars = 0; f->linedefined = 0; f->lastlinedefined = 0; f->source = NULL; return f; } lu_mem luaF_protosize (Proto *p) { lu_mem sz = cast(lu_mem, sizeof(Proto)) + cast_uint(p->sizep) * sizeof(Proto*) + cast_uint(p->sizek) * sizeof(TValue) + cast_uint(p->sizelocvars) * sizeof(LocVar) + cast_uint(p->sizeupvalues) * sizeof(Upvaldesc); if (!(p->flag & PF_FIXED)) { sz += cast_uint(p->sizecode) * sizeof(Instruction); sz += cast_uint(p->sizelineinfo) * sizeof(lu_byte); sz += cast_uint(p->sizeabslineinfo) * sizeof(AbsLineInfo); } return sz; } void luaF_freeproto (lua_State *L, Proto *f) { if (!(f->flag & PF_FIXED)) { luaM_freearray(L, f->code, cast_sizet(f->sizecode)); luaM_freearray(L, f->lineinfo, cast_sizet(f->sizelineinfo)); luaM_freearray(L, f->abslineinfo, cast_sizet(f->sizeabslineinfo)); } luaM_freearray(L, f->p, cast_sizet(f->sizep)); luaM_freearray(L, f->k, cast_sizet(f->sizek)); luaM_freearray(L, f->locvars, cast_sizet(f->sizelocvars)); luaM_freearray(L, f->upvalues, cast_sizet(f->sizeupvalues)); luaM_free(L, f); } /* ** Look for n-th local variable at line 'line' in function 'func'. ** Returns NULL if not found. */ const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { int i; for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { if (pc < f->locvars[i].endpc) { /* is variable active? */ local_number--; if (local_number == 0) return getstr(f->locvars[i].varname); } } return NULL; /* not found */ } void luaF_shareproto (Proto *f) { int i; if (f == NULL || isshared(f)) return; makeshared(f); luaS_share(f->source); for (i = 0; i < f->sizek; i++) { if (ttype(&f->k[i]) == LUA_TSTRING) luaS_share(tsvalue(&f->k[i])); } for (i = 0; i < f->sizeupvalues; i++) luaS_share(f->upvalues[i].name); for (i = 0; i < f->sizelocvars; i++) luaS_share(f->locvars[i].varname); for (i = 0; i < f->sizep; i++) luaF_shareproto(f->p[i]); } ================================================ FILE: 3rd/lua/lfunc.h ================================================ /* ** $Id: lfunc.h $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ #ifndef lfunc_h #define lfunc_h #include "lobject.h" #define sizeCclosure(n) \ (offsetof(CClosure, upvalue) + sizeof(TValue) * cast_uint(n)) #define sizeLclosure(n) \ (offsetof(LClosure, upvals) + sizeof(UpVal *) * cast_uint(n)) /* test whether thread is in 'twups' list */ #define isintwups(L) (L->twups != L) /* ** maximum number of upvalues in a closure (both C and Lua). (Value ** must fit in a VM register.) */ #define MAXUPVAL 255 #define upisopen(up) ((up)->v.p != &(up)->u.value) #define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v.p)) /* ** maximum number of misses before giving up the cache of closures ** in prototypes */ #define MAXMISS 10 /* special status to close upvalues preserving the top of the stack */ #define CLOSEKTOP (LUA_ERRERR + 1) LUAI_FUNC Proto *luaF_newproto (lua_State *L); LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nupvals); LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals); LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level); LUAI_FUNC StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy); LUAI_FUNC void luaF_unlinkupval (UpVal *uv); LUAI_FUNC lu_mem luaF_protosize (Proto *p); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, int pc); LUAI_FUNC void luaF_shareproto (Proto *func); #endif ================================================ FILE: 3rd/lua/lgc.c ================================================ /* ** $Id: lgc.c $ ** Garbage Collector ** See Copyright Notice in lua.h */ #define lgc_c #define LUA_CORE #include "lprefix.h" #include #include "lua.h" #include "ldebug.h" #include "ldo.h" #include "lfunc.h" #include "lgc.h" #include "lmem.h" #include "lobject.h" #include "lstate.h" #include "lstring.h" #include "ltable.h" #include "ltm.h" /* ** Maximum number of elements to sweep in each single step. ** (Large enough to dissipate fixed overheads but small enough ** to allow small steps for the collector.) */ #define GCSWEEPMAX 20 /* ** Cost (in work units) of running one finalizer. */ #define CWUFIN 10 /* mask with all color bits */ #define maskcolors (bitmask(BLACKBIT) | WHITEBITS) /* mask with all GC bits */ #define maskgcbits (maskcolors | AGEBITS) /* macro to erase all color bits then set only the current white bit */ #define makewhite(g,x) \ (x->marked = cast_byte((x->marked & ~maskcolors) | luaC_white(g))) /* make an object gray (neither white nor black) */ #define set2gray(x) resetbits(x->marked, maskcolors) /* make an object black (coming from any color) */ #define set2black(x) \ (x->marked = cast_byte((x->marked & ~WHITEBITS) | bitmask(BLACKBIT))) #define valiswhite(x) (iscollectable(x) && ispurewhite(gcvalue(x))) #define keyiswhite(n) (keyiscollectable(n) && ispurewhite(gckey(n))) /* ** Protected access to objects in values */ #define gcvalueN(o) (iscollectable(o) ? gcvalue(o) : NULL) /* ** Access to collectable objects in array part of tables */ #define gcvalarr(t,i) \ ((*getArrTag(t,i) & BIT_ISCOLLECTABLE) ? getArrVal(t,i)->gc : NULL) #define markvalue(g,o) { checkliveness(mainthread(g),o); \ if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); } #define markkey(g, n) { if keyiswhite(n) reallymarkobject(g,gckey(n)); } #define markobject(g,t) { if (ispurewhite(t)) reallymarkobject(g, obj2gco(t)); } /* ** mark an object that can be NULL (either because it is really optional, ** or it was stripped as debug info, or inside an uncompleted structure) */ #define markobjectN(g,t) { if (t) markobject(g,t); } static void reallymarkobject (global_State *g, GCObject *o); static void atomic (lua_State *L); static void entersweep (lua_State *L); /* ** {====================================================== ** Generic functions ** ======================================================= */ /* ** one after last element in a hash array */ #define gnodelast(h) gnode(h, cast_sizet(sizenode(h))) static l_mem objsize (GCObject *o) { lu_mem res; switch (o->tt) { case LUA_VTABLE: { res = luaH_size(gco2t(o)); break; } case LUA_VLCL: { LClosure *cl = gco2lcl(o); res = sizeLclosure(cl->nupvalues); break; } case LUA_VCCL: { CClosure *cl = gco2ccl(o); res = sizeCclosure(cl->nupvalues); break; } case LUA_VUSERDATA: { Udata *u = gco2u(o); res = sizeudata(u->nuvalue, u->len); break; } case LUA_VPROTO: { res = luaF_protosize(gco2p(o)); break; } case LUA_VTHREAD: { res = luaE_threadsize(gco2th(o)); break; } case LUA_VSHRSTR: { TString *ts = gco2ts(o); res = sizestrshr(cast_uint(ts->shrlen)); break; } case LUA_VLNGSTR: { TString *ts = gco2ts(o); res = luaS_sizelngstr(ts->u.lnglen, ts->shrlen); break; } case LUA_VUPVAL: { res = sizeof(UpVal); break; } default: res = 0; lua_assert(0); } return cast(l_mem, res); } static GCObject **getgclist (GCObject *o) { switch (o->tt) { case LUA_VTABLE: return &gco2t(o)->gclist; case LUA_VLCL: return &gco2lcl(o)->gclist; case LUA_VCCL: return &gco2ccl(o)->gclist; case LUA_VTHREAD: return &gco2th(o)->gclist; case LUA_VPROTO: return &gco2p(o)->gclist; case LUA_VUSERDATA: { Udata *u = gco2u(o); lua_assert(u->nuvalue > 0); return &u->gclist; } default: lua_assert(0); return 0; } } /* ** Link a collectable object 'o' with a known type into the list 'p'. ** (Must be a macro to access the 'gclist' field in different types.) */ #define linkgclist(o,p) linkgclist_(obj2gco(o), &(o)->gclist, &(p)) static void linkgclist_ (GCObject *o, GCObject **pnext, GCObject **list) { lua_assert(!isgray(o)); /* cannot be in a gray list */ *pnext = *list; *list = o; set2gray(o); /* now it is */ } /* ** Link a generic collectable object 'o' into the list 'p'. */ #define linkobjgclist(o,p) linkgclist_(obj2gco(o), getgclist(o), &(p)) /* ** Clear keys for empty entries in tables. If entry is empty, mark its ** entry as dead. This allows the collection of the key, but keeps its ** entry in the table: its removal could break a chain and could break ** a table traversal. Other places never manipulate dead keys, because ** its associated empty value is enough to signal that the entry is ** logically empty. */ static void clearkey (Node *n) { lua_assert(isempty(gval(n))); if (keyiscollectable(n)) setdeadkey(n); /* unused key; remove it */ } /* ** tells whether a key or value can be cleared from a weak ** table. Non-collectable objects are never removed from weak ** tables. Strings behave as 'values', so are never removed too. for ** other objects: if really collected, cannot keep them; for objects ** being finalized, keep them in keys, but not in values */ static int iscleared (global_State *g, const GCObject *o) { if (o == NULL) return 0; /* non-collectable value */ else if (novariant(o->tt) == LUA_TSTRING) { markobject(g, o); /* strings are 'values', so are never weak */ return 0; } else return ispurewhite(o); } /* ** Barrier that moves collector forward, that is, marks the white object ** 'v' being pointed by the black object 'o'. In the generational ** mode, 'v' must also become old, if 'o' is old; however, it cannot ** be changed directly to OLD, because it may still point to non-old ** objects. So, it is marked as OLD0. In the next cycle it will become ** OLD1, and in the next it will finally become OLD (regular old). By ** then, any object it points to will also be old. If called in the ** incremental sweep phase, it clears the black object to white (sweep ** it) to avoid other barrier calls for this same object. (That cannot ** be done is generational mode, as its sweep does not distinguish ** white from dead.) */ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { global_State *g = G(L); lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o) && !isshared(o)); if (keepinvariant(g)) { /* must keep invariant? */ reallymarkobject(g, v); /* restore invariant */ if (isold(o)) { lua_assert(!isold(v)); /* white object could not be old */ setage(v, G_OLD0); /* restore generational invariant */ } } else { /* sweep phase */ lua_assert(issweepphase(g)); if (g->gckind != KGC_GENMINOR) /* incremental mode? */ makewhite(g, o); /* mark 'o' as white to avoid other barriers */ } } /* ** barrier that moves collector backward, that is, mark the black object ** pointing to a white object as gray again. */ void luaC_barrierback_ (lua_State *L, GCObject *o) { global_State *g = G(L); lua_assert(isblack(o) && !isdead(g, o)); lua_assert((g->gckind != KGC_GENMINOR) || (isold(o) && getage(o) != G_TOUCHED1)); if (getage(o) == G_TOUCHED2) /* already in gray list? */ set2gray(o); /* make it gray to become touched1 */ else /* link it in 'grayagain' and paint it gray */ linkobjgclist(o, g->grayagain); if (isold(o)) /* generational mode? */ setage(o, G_TOUCHED1); /* touched in current cycle */ } void luaC_fix (lua_State *L, GCObject *o) { global_State *g = G(L); lua_assert(g->allgc == o); /* object must be 1st in 'allgc' list! */ set2gray(o); /* they will be gray forever */ setage(o, G_OLD); /* and old forever */ g->allgc = o->next; /* remove object from 'allgc' list */ o->next = g->fixedgc; /* link it to 'fixedgc' list */ g->fixedgc = o; } /* ** create a new collectable object (with given type, size, and offset) ** and link it to 'allgc' list. */ GCObject *luaC_newobjdt (lua_State *L, lu_byte tt, size_t sz, size_t offset) { global_State *g = G(L); char *p = cast_charp(luaM_newobject(L, novariant(tt), sz)); GCObject *o = cast(GCObject *, p + offset); o->marked = luaC_white(g); o->tt = tt; o->next = g->allgc; g->allgc = o; return o; } /* ** create a new collectable object with no offset. */ GCObject *luaC_newobj (lua_State *L, lu_byte tt, size_t sz) { return luaC_newobjdt(L, tt, sz, 0); } /* }====================================================== */ /* ** {====================================================== ** Mark functions ** ======================================================= */ /* ** Mark an object. Userdata with no user values, strings, and closed ** upvalues are visited and turned black here. Open upvalues are ** already indirectly linked through their respective threads in the ** 'twups' list, so they don't go to the gray list; nevertheless, they ** are kept gray to avoid barriers, as their values will be revisited ** by the thread or by 'remarkupvals'. Other objects are added to the ** gray list to be visited (and turned black) later. Both userdata and ** upvalues can call this function recursively, but this recursion goes ** for at most two levels: An upvalue cannot refer to another upvalue ** (only closures can), and a userdata's metatable must be a table. */ static void reallymarkobject (global_State *g, GCObject *o) { g->GCmarked += objsize(o); switch (o->tt) { case LUA_VSHRSTR: case LUA_VLNGSTR: { set2black(o); /* nothing to visit */ break; } case LUA_VUPVAL: { UpVal *uv = gco2upv(o); if (upisopen(uv)) set2gray(uv); /* open upvalues are kept gray */ else set2black(uv); /* closed upvalues are visited here */ markvalue(g, uv->v.p); /* mark its content */ break; } case LUA_VUSERDATA: { Udata *u = gco2u(o); if (u->nuvalue == 0) { /* no user values? */ markobjectN(g, u->metatable); /* mark its metatable */ set2black(u); /* nothing else to mark */ break; } /* else... */ } /* FALLTHROUGH */ case LUA_VLCL: case LUA_VCCL: case LUA_VTABLE: case LUA_VTHREAD: case LUA_VPROTO: { linkobjgclist(o, g->gray); /* to be visited later */ break; } default: lua_assert(0); break; } } /* ** mark metamethods for basic types */ static void markmt (global_State *g) { int i; for (i=0; i < LUA_NUMTYPES; i++) markobjectN(g, g->mt[i]); } /* ** mark all objects in list of being-finalized */ static void markbeingfnz (global_State *g) { GCObject *o; for (o = g->tobefnz; o != NULL; o = o->next) markobject(g, o); } /* ** For each non-marked thread, simulates a barrier between each open ** upvalue and its value. (If the thread is collected, the value will be ** assigned to the upvalue, but then it can be too late for the barrier ** to act. The "barrier" does not need to check colors: A non-marked ** thread must be young; upvalues cannot be older than their threads; so ** any visited upvalue must be young too.) Also removes the thread from ** the list, as it was already visited. Removes also threads with no ** upvalues, as they have nothing to be checked. (If the thread gets an ** upvalue later, it will be linked in the list again.) */ static void remarkupvals (global_State *g) { lua_State *thread; lua_State **p = &g->twups; while ((thread = *p) != NULL) { if (!iswhite(thread) && thread->openupval != NULL) p = &thread->twups; /* keep marked thread with upvalues in the list */ else { /* thread is not marked or without upvalues */ UpVal *uv; lua_assert(!isold(thread) || thread->openupval == NULL); *p = thread->twups; /* remove thread from the list */ thread->twups = thread; /* mark that it is out of list */ for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) { lua_assert(getage(uv) <= getage(thread)); if (!iswhite(uv)) { /* upvalue already visited? */ lua_assert(upisopen(uv) && isgray(uv)); markvalue(g, uv->v.p); /* mark its value */ } } } } } static void cleargraylists (global_State *g) { g->gray = g->grayagain = NULL; g->weak = g->allweak = g->ephemeron = NULL; } /* ** mark root set and reset all gray lists, to start a new collection. ** 'GCmarked' is initialized to count the total number of live bytes ** during a cycle. */ static void restartcollection (global_State *g) { cleargraylists(g); g->GCmarked = 0; markobject(g, mainthread(g)); markvalue(g, &g->l_registry); markmt(g); markbeingfnz(g); /* mark any finalizing object left from previous cycle */ } /* }====================================================== */ /* ** {====================================================== ** Traverse functions ** ======================================================= */ /* ** Check whether object 'o' should be kept in the 'grayagain' list for ** post-processing by 'correctgraylist'. (It could put all old objects ** in the list and leave all the work to 'correctgraylist', but it is ** more efficient to avoid adding elements that will be removed.) Only ** TOUCHED1 objects need to be in the list. TOUCHED2 doesn't need to go ** back to a gray list, but then it must become OLD. (That is what ** 'correctgraylist' does when it finds a TOUCHED2 object.) ** This function is a no-op in incremental mode, as objects cannot be ** marked as touched in that mode. */ static void genlink (global_State *g, GCObject *o) { lua_assert(isblack(o)); if (getage(o) == G_TOUCHED1) { /* touched in this cycle? */ linkobjgclist(o, g->grayagain); /* link it back in 'grayagain' */ } /* everything else do not need to be linked back */ else if (getage(o) == G_TOUCHED2) setage(o, G_OLD); /* advance age */ } /* ** Traverse a table with weak values and link it to proper list. During ** propagate phase, keep it in 'grayagain' list, to be revisited in the ** atomic phase. In the atomic phase, if table has any white value, ** put it in 'weak' list, to be cleared; otherwise, call 'genlink' ** to check table age in generational mode. */ static void traverseweakvalue (global_State *g, Table *h) { Node *n, *limit = gnodelast(h); /* if there is array part, assume it may have white values (it is not worth traversing it now just to check) */ int hasclears = (h->asize > 0); for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ if (isempty(gval(n))) /* entry is empty? */ clearkey(n); /* clear its key */ else { lua_assert(!keyisnil(n)); markkey(g, n); if (!hasclears && iscleared(g, gcvalueN(gval(n)))) /* a white value? */ hasclears = 1; /* table will have to be cleared */ } } if (g->gcstate == GCSpropagate) linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ else if (hasclears) linkgclist(h, g->weak); /* has to be cleared later */ else genlink(g, obj2gco(h)); } /* ** Traverse the array part of a table. */ static int traversearray (global_State *g, Table *h) { unsigned asize = h->asize; int marked = 0; /* true if some object is marked in this traversal */ unsigned i; for (i = 0; i < asize; i++) { GCObject *o = gcvalarr(h, i); if (o != NULL && iswhite(o)) { marked = 1; reallymarkobject(g, o); } } return marked; } /* ** Traverse an ephemeron table and link it to proper list. Returns true ** iff any object was marked during this traversal (which implies that ** convergence has to continue). During propagation phase, keep table ** in 'grayagain' list, to be visited again in the atomic phase. In ** the atomic phase, if table has any white->white entry, it has to ** be revisited during ephemeron convergence (as that key may turn ** black). Otherwise, if it has any white key, table has to be cleared ** (in the atomic phase). In generational mode, some tables ** must be kept in some gray list for post-processing; this is done ** by 'genlink'. */ static int traverseephemeron (global_State *g, Table *h, int inv) { int hasclears = 0; /* true if table has white keys */ int hasww = 0; /* true if table has entry "white-key -> white-value" */ unsigned int i; unsigned int nsize = sizenode(h); int marked = traversearray(g, h); /* traverse array part */ /* traverse hash part; if 'inv', traverse descending (see 'convergeephemerons') */ for (i = 0; i < nsize; i++) { Node *n = inv ? gnode(h, nsize - 1 - i) : gnode(h, i); if (isempty(gval(n))) /* entry is empty? */ clearkey(n); /* clear its key */ else if (iscleared(g, gckeyN(n))) { /* key is not marked (yet)? */ hasclears = 1; /* table must be cleared */ if (valiswhite(gval(n))) /* value not marked yet? */ hasww = 1; /* white-white entry */ } else if (valiswhite(gval(n))) { /* value not marked yet? */ marked = 1; reallymarkobject(g, gcvalue(gval(n))); /* mark it now */ } } /* link table into proper list */ if (g->gcstate == GCSpropagate) linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ else if (hasww) /* table has white->white entries? */ linkgclist(h, g->ephemeron); /* have to propagate again */ else if (hasclears) /* table has white keys? */ linkgclist(h, g->allweak); /* may have to clean white keys */ else genlink(g, obj2gco(h)); /* check whether collector still needs to see it */ return marked; } static void traversestrongtable (global_State *g, Table *h) { Node *n, *limit = gnodelast(h); traversearray(g, h); for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ if (isempty(gval(n))) /* entry is empty? */ clearkey(n); /* clear its key */ else { lua_assert(!keyisnil(n)); markkey(g, n); markvalue(g, gval(n)); } } genlink(g, obj2gco(h)); } /* ** (result & 1) iff weak values; (result & 2) iff weak keys. */ static int getmode (global_State *g, Table *h) { const TValue *mode = gfasttm(g, h->metatable, TM_MODE); if (mode == NULL || !ttisstring(mode)) return 0; /* ignore non-string modes */ else { const char *smode = getstr(tsvalue(mode)); const char *weakkey = strchr(smode, 'k'); const char *weakvalue = strchr(smode, 'v'); return ((weakkey != NULL) << 1) | (weakvalue != NULL); } } static l_mem traversetable (global_State *g, Table *h) { markobjectN(g, h->metatable); switch (getmode(g, h)) { case 0: /* not weak */ traversestrongtable(g, h); break; case 1: /* weak values */ traverseweakvalue(g, h); break; case 2: /* weak keys */ traverseephemeron(g, h, 0); break; case 3: /* all weak; nothing to traverse */ if (g->gcstate == GCSpropagate) linkgclist(h, g->grayagain); /* must visit again its metatable */ else linkgclist(h, g->allweak); /* must clear collected entries */ break; } return cast(l_mem, 1 + 2*sizenode(h) + h->asize); } static l_mem traverseudata (global_State *g, Udata *u) { int i; markobjectN(g, u->metatable); /* mark its metatable */ for (i = 0; i < u->nuvalue; i++) markvalue(g, &u->uv[i].uv); genlink(g, obj2gco(u)); return 1 + u->nuvalue; } /* ** Traverse a prototype. (While a prototype is being build, its ** arrays can be larger than needed; the extra slots are filled with ** NULL, so the use of 'markobjectN') */ static l_mem traverseproto (global_State *g, Proto *f) { int i; markobjectN(g, f->source); for (i = 0; i < f->sizek; i++) /* mark literals */ markvalue(g, &f->k[i]); for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */ markobjectN(g, f->upvalues[i].name); for (i = 0; i < f->sizep; i++) /* mark nested protos */ markobjectN(g, f->p[i]); for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ markobjectN(g, f->locvars[i].varname); return 1 + f->sizek + f->sizeupvalues + f->sizep + f->sizelocvars; } static l_mem traverseCclosure (global_State *g, CClosure *cl) { int i; for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */ markvalue(g, &cl->upvalue[i]); return 1 + cl->nupvalues; } /* ** Traverse a Lua closure, marking its prototype and its upvalues. ** (Both can be NULL while closure is being created.) */ static l_mem traverseLclosure (global_State *g, LClosure *cl) { int i; markobjectN(g, cl->p); /* mark its prototype */ for (i = 0; i < cl->nupvalues; i++) { /* visit its upvalues */ UpVal *uv = cl->upvals[i]; markobjectN(g, uv); /* mark upvalue */ } return 1 + cl->nupvalues; } /* ** Traverse a thread, marking the elements in the stack up to its top ** and cleaning the rest of the stack in the final traversal. That ** ensures that the entire stack have valid (non-dead) objects. ** Threads have no barriers. In gen. mode, old threads must be visited ** at every cycle, because they might point to young objects. In inc. ** mode, the thread can still be modified before the end of the cycle, ** and therefore it must be visited again in the atomic phase. To ensure ** these visits, threads must return to a gray list if they are not new ** (which can only happen in generational mode) or if the traverse is in ** the propagate phase (which can only happen in incremental mode). */ static l_mem traversethread (global_State *g, lua_State *th) { UpVal *uv; StkId o = th->stack.p; if (isold(th) || g->gcstate == GCSpropagate) linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ if (o == NULL) return 0; /* stack not completely built yet */ lua_assert(g->gcstate == GCSatomic || th->openupval == NULL || isintwups(th)); for (; o < th->top.p; o++) /* mark live elements in the stack */ markvalue(g, s2v(o)); for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) markobject(g, uv); /* open upvalues cannot be collected */ if (g->gcstate == GCSatomic) { /* final traversal? */ if (!g->gcemergency) luaD_shrinkstack(th); /* do not change stack in emergency cycle */ for (o = th->top.p; o < th->stack_last.p + EXTRA_STACK; o++) setnilvalue(s2v(o)); /* clear dead stack slice */ /* 'remarkupvals' may have removed thread from 'twups' list */ if (!isintwups(th) && th->openupval != NULL) { th->twups = g->twups; /* link it back to the list */ g->twups = th; } } return 1 + (th->top.p - th->stack.p); } /* ** traverse one gray object, turning it to black. Return an estimate ** of the number of slots traversed. */ static l_mem propagatemark (global_State *g) { GCObject *o = g->gray; nw2black(o); g->gray = *getgclist(o); /* remove from 'gray' list */ switch (o->tt) { case LUA_VTABLE: return traversetable(g, gco2t(o)); case LUA_VUSERDATA: return traverseudata(g, gco2u(o)); case LUA_VLCL: return traverseLclosure(g, gco2lcl(o)); case LUA_VCCL: return traverseCclosure(g, gco2ccl(o)); case LUA_VPROTO: return traverseproto(g, gco2p(o)); case LUA_VTHREAD: return traversethread(g, gco2th(o)); default: lua_assert(0); return 0; } } static void propagateall (global_State *g) { while (g->gray) propagatemark(g); } /* ** Traverse all ephemeron tables propagating marks from keys to values. ** Repeat until it converges, that is, nothing new is marked. 'dir' ** inverts the direction of the traversals, trying to speed up ** convergence on chains in the same table. */ static void convergeephemerons (global_State *g) { int changed; int dir = 0; do { GCObject *w; GCObject *next = g->ephemeron; /* get ephemeron list */ g->ephemeron = NULL; /* tables may return to this list when traversed */ changed = 0; while ((w = next) != NULL) { /* for each ephemeron table */ Table *h = gco2t(w); next = h->gclist; /* list is rebuilt during loop */ nw2black(h); /* out of the list (for now) */ if (traverseephemeron(g, h, dir)) { /* marked some value? */ propagateall(g); /* propagate changes */ changed = 1; /* will have to revisit all ephemeron tables */ } } dir = !dir; /* invert direction next time */ } while (changed); /* repeat until no more changes */ } /* }====================================================== */ /* ** {====================================================== ** Sweep Functions ** ======================================================= */ /* ** clear entries with unmarked keys from all weaktables in list 'l' */ static void clearbykeys (global_State *g, GCObject *l) { for (; l; l = gco2t(l)->gclist) { Table *h = gco2t(l); Node *limit = gnodelast(h); Node *n; for (n = gnode(h, 0); n < limit; n++) { if (iscleared(g, gckeyN(n))) /* unmarked key? */ setempty(gval(n)); /* remove entry */ if (isempty(gval(n))) /* is entry empty? */ clearkey(n); /* clear its key */ } } } /* ** clear entries with unmarked values from all weaktables in list 'l' up ** to element 'f' */ static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) { for (; l != f; l = gco2t(l)->gclist) { Table *h = gco2t(l); Node *n, *limit = gnodelast(h); unsigned int i; unsigned int asize = h->asize; for (i = 0; i < asize; i++) { GCObject *o = gcvalarr(h, i); if (iscleared(g, o)) /* value was collected? */ *getArrTag(h, i) = LUA_VEMPTY; /* remove entry */ } for (n = gnode(h, 0); n < limit; n++) { if (iscleared(g, gcvalueN(gval(n)))) /* unmarked value? */ setempty(gval(n)); /* remove entry */ if (isempty(gval(n))) /* is entry empty? */ clearkey(n); /* clear its key */ } } } static void freeupval (lua_State *L, UpVal *uv) { if (upisopen(uv)) luaF_unlinkupval(uv); luaM_free(L, uv); } static void freeobj (lua_State *L, GCObject *o) { assert_code(l_mem newmem = gettotalbytes(G(L)) - objsize(o)); switch (o->tt) { case LUA_VPROTO: luaF_freeproto(L, gco2p(o)); break; case LUA_VUPVAL: freeupval(L, gco2upv(o)); break; case LUA_VLCL: { LClosure *cl = gco2lcl(o); luaM_freemem(L, cl, sizeLclosure(cl->nupvalues)); break; } case LUA_VCCL: { CClosure *cl = gco2ccl(o); luaM_freemem(L, cl, sizeCclosure(cl->nupvalues)); break; } case LUA_VTABLE: luaH_free(L, gco2t(o)); break; case LUA_VTHREAD: luaE_freethread(L, gco2th(o)); break; case LUA_VUSERDATA: { Udata *u = gco2u(o); luaM_freemem(L, o, sizeudata(u->nuvalue, u->len)); break; } case LUA_VSHRSTR: { TString *ts = gco2ts(o); luaS_remove(L, ts); /* remove it from hash table */ luaM_freemem(L, ts, sizestrshr(cast_uint(ts->shrlen))); break; } case LUA_VLNGSTR: { TString *ts = gco2ts(o); if (ts->shrlen == LSTRMEM) /* must free external string? */ (*ts->falloc)(ts->ud, ts->contents, ts->u.lnglen + 1, 0); luaM_freemem(L, ts, luaS_sizelngstr(ts->u.lnglen, ts->shrlen)); break; } default: lua_assert(0); } lua_assert(gettotalbytes(G(L)) == newmem); } /* ** sweep at most 'countin' elements from a list of GCObjects erasing dead ** objects, where a dead object is one marked with the old (non current) ** white; change all non-dead objects back to white (and new), preparing ** for next collection cycle. Return where to continue the traversal or ** NULL if list is finished. */ static GCObject **sweeplist (lua_State *L, GCObject **p, l_mem countin) { global_State *g = G(L); int ow = otherwhite(g); int white = luaC_white(g); /* current white */ while (*p != NULL && countin-- > 0) { GCObject *curr = *p; int marked = curr->marked; if (isshared(curr)) p = &curr->next; else if (isdeadm(ow, marked)) { /* is 'curr' dead? */ *p = curr->next; /* remove 'curr' from list */ freeobj(L, curr); /* erase 'curr' */ } else { /* change mark to 'white' and age to 'new' */ curr->marked = cast_byte((marked & ~maskgcbits) | white | G_NEW); p = &curr->next; /* go to next element */ } } return (*p == NULL) ? NULL : p; } /* ** sweep a list until a live object (or end of list) */ static GCObject **sweeptolive (lua_State *L, GCObject **p) { GCObject **old = p; do { p = sweeplist(L, p, 1); } while (p == old); return p; } /* }====================================================== */ /* ** {====================================================== ** Finalization ** ======================================================= */ /* ** If possible, shrink string table. */ static void checkSizes (lua_State *L, global_State *g) { if (!g->gcemergency) { if (g->strt.nuse < g->strt.size / 4) /* string table too big? */ luaS_resize(L, g->strt.size / 2); } } /* ** Get the next udata to be finalized from the 'tobefnz' list, and ** link it back into the 'allgc' list. */ static GCObject *udata2finalize (global_State *g) { GCObject *o = g->tobefnz; /* get first element */ lua_assert(tofinalize(o)); g->tobefnz = o->next; /* remove it from 'tobefnz' list */ o->next = g->allgc; /* return it to 'allgc' list */ g->allgc = o; resetbit(o->marked, FINALIZEDBIT); /* object is "normal" again */ if (issweepphase(g)) makewhite(g, o); /* "sweep" object */ else if (getage(o) == G_OLD1) g->firstold1 = o; /* it is the first OLD1 object in the list */ return o; } static void dothecall (lua_State *L, void *ud) { UNUSED(ud); luaD_callnoyield(L, L->top.p - 2, 0); } static void GCTM (lua_State *L) { global_State *g = G(L); const TValue *tm; TValue v; lua_assert(!g->gcemergency); setgcovalue(L, &v, udata2finalize(g)); tm = luaT_gettmbyobj(L, &v, TM_GC); if (!notm(tm)) { /* is there a finalizer? */ TStatus status; lu_byte oldah = L->allowhook; lu_byte oldgcstp = g->gcstp; g->gcstp |= GCSTPGC; /* avoid GC steps */ L->allowhook = 0; /* stop debug hooks during GC metamethod */ setobj2s(L, L->top.p++, tm); /* push finalizer... */ setobj2s(L, L->top.p++, &v); /* ... and its argument */ L->ci->callstatus |= CIST_FIN; /* will run a finalizer */ status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top.p - 2), 0); L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */ L->allowhook = oldah; /* restore hooks */ g->gcstp = oldgcstp; /* restore state */ if (l_unlikely(status != LUA_OK)) { /* error while running __gc? */ luaE_warnerror(L, "__gc"); L->top.p--; /* pops error object */ } } } /* ** call all pending finalizers */ static void callallpendingfinalizers (lua_State *L) { global_State *g = G(L); while (g->tobefnz) GCTM(L); } /* ** find last 'next' field in list 'p' list (to add elements in its end) */ static GCObject **findlast (GCObject **p) { while (*p != NULL) p = &(*p)->next; return p; } /* ** Move all unreachable objects (or 'all' objects) that need ** finalization from list 'finobj' to list 'tobefnz' (to be finalized). ** (Note that objects after 'finobjold1' cannot be white, so they ** don't need to be traversed. In incremental mode, 'finobjold1' is NULL, ** so the whole list is traversed.) */ static void separatetobefnz (global_State *g, int all) { GCObject *curr; GCObject **p = &g->finobj; GCObject **lastnext = findlast(&g->tobefnz); while ((curr = *p) != g->finobjold1) { /* traverse all finalizable objects */ lua_assert(tofinalize(curr)); if (!(iswhite(curr) || all)) /* not being collected? */ p = &curr->next; /* don't bother with it */ else { if (curr == g->finobjsur) /* removing 'finobjsur'? */ g->finobjsur = curr->next; /* correct it */ *p = curr->next; /* remove 'curr' from 'finobj' list */ curr->next = *lastnext; /* link at the end of 'tobefnz' list */ *lastnext = curr; lastnext = &curr->next; } } } /* ** If pointer 'p' points to 'o', move it to the next element. */ static void checkpointer (GCObject **p, GCObject *o) { if (o == *p) *p = o->next; } /* ** Correct pointers to objects inside 'allgc' list when ** object 'o' is being removed from the list. */ static void correctpointers (global_State *g, GCObject *o) { checkpointer(&g->survival, o); checkpointer(&g->old1, o); checkpointer(&g->reallyold, o); checkpointer(&g->firstold1, o); } /* ** if object 'o' has a finalizer, remove it from 'allgc' list (must ** search the list to find it) and link it in 'finobj' list. */ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { global_State *g = G(L); if (tofinalize(o) || /* obj. is already marked... */ gfasttm(g, mt, TM_GC) == NULL || /* or has no finalizer... */ (g->gcstp & GCSTPCLS)) /* or closing state? */ return; /* nothing to be done */ else { /* move 'o' to 'finobj' list */ GCObject **p; if (issweepphase(g)) { makewhite(g, o); /* "sweep" object 'o' */ if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */ g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */ } else correctpointers(g, o); /* search for pointer pointing to 'o' */ for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ } *p = o->next; /* remove 'o' from 'allgc' list */ o->next = g->finobj; /* link it in 'finobj' list */ g->finobj = o; l_setbit(o->marked, FINALIZEDBIT); /* mark it as such */ } } /* }====================================================== */ /* ** {====================================================== ** Generational Collector ** ======================================================= */ /* ** Fields 'GCmarked' and 'GCmajorminor' are used to control the pace and ** the mode of the collector. They play several roles, depending on the ** mode of the collector: ** * KGC_INC: ** GCmarked: number of marked bytes during a cycle. ** GCmajorminor: not used. ** * KGC_GENMINOR ** GCmarked: number of bytes that became old since last major collection. ** GCmajorminor: number of bytes marked in last major collection. ** * KGC_GENMAJOR ** GCmarked: number of bytes that became old since last major collection. ** GCmajorminor: number of bytes marked in last major collection. */ /* ** Set the "time" to wait before starting a new incremental cycle; ** cycle will start when number of bytes in use hits the threshold of ** approximately (marked * pause / 100). */ static void setpause (global_State *g) { l_mem threshold = applygcparam(g, PAUSE, g->GCmarked); l_mem debt = threshold - gettotalbytes(g); if (debt < 0) debt = 0; luaE_setdebt(g, debt); } /* ** Sweep a list of objects to enter generational mode. Deletes dead ** objects and turns the non dead to old. All non-dead threads---which ** are now old---must be in a gray list. Everything else is not in a ** gray list. Open upvalues are also kept gray. */ static void sweep2old (lua_State *L, GCObject **p) { GCObject *curr; global_State *g = G(L); while ((curr = *p) != NULL) { if (isshared(curr)) p = &curr->next; /* go to next element */ else if (iswhite(curr)) { /* is 'curr' dead? */ lua_assert(isdead(g, curr)); *p = curr->next; /* remove 'curr' from list */ freeobj(L, curr); /* erase 'curr' */ } else { /* all surviving objects become old */ setage(curr, G_OLD); if (curr->tt == LUA_VTHREAD) { /* threads must be watched */ lua_State *th = gco2th(curr); linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ } else if (curr->tt == LUA_VUPVAL && upisopen(gco2upv(curr))) set2gray(curr); /* open upvalues are always gray */ else /* everything else is black */ nw2black(curr); p = &curr->next; /* go to next element */ } } } /* ** Sweep for generational mode. Delete dead objects. (Because the ** collection is not incremental, there are no "new white" objects ** during the sweep. So, any white object must be dead.) For ** non-dead objects, advance their ages and clear the color of ** new objects. (Old objects keep their colors.) ** The ages of G_TOUCHED1 and G_TOUCHED2 objects cannot be advanced ** here, because these old-generation objects are usually not swept ** here. They will all be advanced in 'correctgraylist'. That function ** will also remove objects turned white here from any gray list. */ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, GCObject *limit, GCObject **pfirstold1, l_mem *paddedold) { static const lu_byte nextage[] = { G_SURVIVAL, /* from G_NEW */ G_OLD1, /* from G_SURVIVAL */ G_OLD1, /* from G_OLD0 */ G_OLD, /* from G_OLD1 */ G_OLD, /* from G_OLD (do not change) */ G_TOUCHED1, /* from G_TOUCHED1 (do not change) */ G_TOUCHED2 /* from G_TOUCHED2 (do not change) */ }; l_mem addedold = 0; int white = luaC_white(g); GCObject *curr; while ((curr = *p) != limit) { if (isshared(curr)) p = &curr->next; /* go to next element */ else if (iswhite(curr)) { /* is 'curr' dead? */ lua_assert(!isold(curr) && isdead(g, curr)); *p = curr->next; /* remove 'curr' from list */ freeobj(L, curr); /* erase 'curr' */ } else { /* correct mark and age */ int age = getage(curr); if (age == G_NEW) { /* new objects go back to white */ int marked = curr->marked & ~maskgcbits; /* erase GC bits */ curr->marked = cast_byte(marked | G_SURVIVAL | white); } else { /* all other objects will be old, and so keep their color */ lua_assert(age != G_OLD1); /* advanced in 'markold' */ setage(curr, nextage[age]); if (getage(curr) == G_OLD1) { addedold += objsize(curr); /* bytes becoming old */ if (*pfirstold1 == NULL) *pfirstold1 = curr; /* first OLD1 object in the list */ } } p = &curr->next; /* go to next element */ } } *paddedold += addedold; return p; } /* ** Correct a list of gray objects. Return a pointer to the last element ** left on the list, so that we can link another list to the end of ** this one. ** Because this correction is done after sweeping, young objects might ** be turned white and still be in the list. They are only removed. ** 'TOUCHED1' objects are advanced to 'TOUCHED2' and remain on the list; ** Non-white threads also remain on the list. 'TOUCHED2' objects and ** anything else become regular old, are marked black, and are removed ** from the list. */ static GCObject **correctgraylist (GCObject **p) { GCObject *curr; while ((curr = *p) != NULL) { GCObject **next = getgclist(curr); if (iswhite(curr)) goto remove; /* remove all white objects */ else if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */ lua_assert(isgray(curr)); nw2black(curr); /* make it black, for next barrier */ setage(curr, G_TOUCHED2); goto remain; /* keep it in the list and go to next element */ } else if (curr->tt == LUA_VTHREAD) { lua_assert(isgray(curr)); goto remain; /* keep non-white threads on the list */ } else { /* everything else is removed */ lua_assert(isold(curr)); /* young objects should be white here */ if (getage(curr) == G_TOUCHED2) /* advance from TOUCHED2... */ setage(curr, G_OLD); /* ... to OLD */ nw2black(curr); /* make object black (to be removed) */ goto remove; } remove: *p = *next; continue; remain: p = next; continue; } return p; } /* ** Correct all gray lists, coalescing them into 'grayagain'. */ static void correctgraylists (global_State *g) { GCObject **list = correctgraylist(&g->grayagain); *list = g->weak; g->weak = NULL; list = correctgraylist(list); *list = g->allweak; g->allweak = NULL; list = correctgraylist(list); *list = g->ephemeron; g->ephemeron = NULL; correctgraylist(list); } /* ** Mark black 'OLD1' objects when starting a new young collection. ** Gray objects are already in some gray list, and so will be visited in ** the atomic step. */ static void markold (global_State *g, GCObject *from, GCObject *to) { GCObject *p; for (p = from; p != to; p = p->next) { if (getage(p) == G_OLD1) { lua_assert(!iswhite(p)); setage(p, G_OLD); /* now they are old */ if (isblack(p)) reallymarkobject(g, p); } } } /* ** Finish a young-generation collection. */ static void finishgencycle (lua_State *L, global_State *g) { correctgraylists(g); checkSizes(L, g); g->gcstate = GCSpropagate; /* skip restart */ if (!g->gcemergency && luaD_checkminstack(L)) callallpendingfinalizers(L); } /* ** Shifts from a minor collection to major collections. It starts in ** the "sweep all" state to clear all objects, which are mostly black ** in generational mode. */ static void minor2inc (lua_State *L, global_State *g, lu_byte kind) { g->GCmajorminor = g->GCmarked; /* number of live bytes */ g->gckind = kind; g->reallyold = g->old1 = g->survival = NULL; g->finobjrold = g->finobjold1 = g->finobjsur = NULL; entersweep(L); /* continue as an incremental cycle */ /* set a debt equal to the step size */ luaE_setdebt(g, applygcparam(g, STEPSIZE, 100)); } /* ** Decide whether to shift to major mode. It shifts if the accumulated ** number of added old bytes (counted in 'GCmarked') is larger than ** 'minormajor'% of the number of lived bytes after the last major ** collection. (This number is kept in 'GCmajorminor'.) */ static int checkminormajor (global_State *g) { l_mem limit = applygcparam(g, MINORMAJOR, g->GCmajorminor); if (limit == 0) return 0; /* special case: 'minormajor' 0 stops major collections */ return (g->GCmarked >= limit); } /* ** Does a young collection. First, mark 'OLD1' objects. Then does the ** atomic step. Then, check whether to continue in minor mode. If so, ** sweep all lists and advance pointers. Finally, finish the collection. */ static void youngcollection (lua_State *L, global_State *g) { l_mem addedold1 = 0; l_mem marked = g->GCmarked; /* preserve 'g->GCmarked' */ GCObject **psurvival; /* to point to first non-dead survival object */ GCObject *dummy; /* dummy out parameter to 'sweepgen' */ lua_assert(g->gcstate == GCSpropagate); if (g->firstold1) { /* are there regular OLD1 objects? */ markold(g, g->firstold1, g->reallyold); /* mark them */ g->firstold1 = NULL; /* no more OLD1 objects (for now) */ } markold(g, g->finobj, g->finobjrold); markold(g, g->tobefnz, NULL); atomic(L); /* will lose 'g->marked' */ /* sweep nursery and get a pointer to its last live element */ g->gcstate = GCSswpallgc; psurvival = sweepgen(L, g, &g->allgc, g->survival, &g->firstold1, &addedold1); /* sweep 'survival' */ sweepgen(L, g, psurvival, g->old1, &g->firstold1, &addedold1); g->reallyold = g->old1; g->old1 = *psurvival; /* 'survival' survivals are old now */ g->survival = g->allgc; /* all news are survivals */ /* repeat for 'finobj' lists */ dummy = NULL; /* no 'firstold1' optimization for 'finobj' lists */ psurvival = sweepgen(L, g, &g->finobj, g->finobjsur, &dummy, &addedold1); /* sweep 'survival' */ sweepgen(L, g, psurvival, g->finobjold1, &dummy, &addedold1); g->finobjrold = g->finobjold1; g->finobjold1 = *psurvival; /* 'survival' survivals are old now */ g->finobjsur = g->finobj; /* all news are survivals */ sweepgen(L, g, &g->tobefnz, NULL, &dummy, &addedold1); /* keep total number of added old1 bytes */ g->GCmarked = marked + addedold1; /* decide whether to shift to major mode */ if (checkminormajor(g)) { minor2inc(L, g, KGC_GENMAJOR); /* go to major mode */ g->GCmarked = 0; /* avoid pause in first major cycle (see 'setpause') */ } else finishgencycle(L, g); /* still in minor mode; finish it */ } /* ** Clears all gray lists, sweeps objects, and prepare sublists to enter ** generational mode. The sweeps remove dead objects and turn all ** surviving objects to old. Threads go back to 'grayagain'; everything ** else is turned black (not in any gray list). */ static void atomic2gen (lua_State *L, global_State *g) { cleargraylists(g); /* sweep all elements making them old */ g->gcstate = GCSswpallgc; sweep2old(L, &g->allgc); /* everything alive now is old */ g->reallyold = g->old1 = g->survival = g->allgc; g->firstold1 = NULL; /* there are no OLD1 objects anywhere */ /* repeat for 'finobj' lists */ sweep2old(L, &g->finobj); g->finobjrold = g->finobjold1 = g->finobjsur = g->finobj; sweep2old(L, &g->tobefnz); g->gckind = KGC_GENMINOR; g->GCmajorminor = g->GCmarked; /* "base" for number of bytes */ g->GCmarked = 0; /* to count the number of added old1 bytes */ finishgencycle(L, g); } /* ** Set debt for the next minor collection, which will happen when ** total number of bytes grows 'genminormul'% in relation to ** the base, GCmajorminor, which is the number of bytes being used ** after the last major collection. */ static void setminordebt (global_State *g) { luaE_setdebt(g, applygcparam(g, MINORMUL, g->GCmajorminor)); } /* ** Enter generational mode. Must go until the end of an atomic cycle ** to ensure that all objects are correctly marked and weak tables ** are cleared. Then, turn all objects into old and finishes the ** collection. */ static void entergen (lua_State *L, global_State *g) { luaC_runtilstate(L, GCSpause, 1); /* prepare to start a new cycle */ luaC_runtilstate(L, GCSpropagate, 1); /* start new cycle */ atomic(L); /* propagates all and then do the atomic stuff */ atomic2gen(L, g); setminordebt(g); /* set debt assuming next cycle will be minor */ } /* ** Change collector mode to 'newmode'. */ void luaC_changemode (lua_State *L, int newmode) { global_State *g = G(L); if (g->gckind == KGC_GENMAJOR) /* doing major collections? */ g->gckind = KGC_INC; /* already incremental but in name */ if (newmode != g->gckind) { /* does it need to change? */ if (newmode == KGC_INC) /* entering incremental mode? */ minor2inc(L, g, KGC_INC); /* entering incremental mode */ else { lua_assert(newmode == KGC_GENMINOR); entergen(L, g); } } } /* ** Does a full collection in generational mode. */ static void fullgen (lua_State *L, global_State *g) { minor2inc(L, g, KGC_INC); entergen(L, g); } /* ** After an atomic incremental step from a major collection, ** check whether collector could return to minor collections. ** It checks whether the number of bytes 'tobecollected' ** is greater than 'majorminor'% of the number of bytes added ** since the last collection ('addedbytes'). */ static int checkmajorminor (lua_State *L, global_State *g) { if (g->gckind == KGC_GENMAJOR) { /* generational mode? */ l_mem numbytes = gettotalbytes(g); l_mem addedbytes = numbytes - g->GCmajorminor; l_mem limit = applygcparam(g, MAJORMINOR, addedbytes); l_mem tobecollected = numbytes - g->GCmarked; if (tobecollected > limit) { atomic2gen(L, g); /* return to generational mode */ setminordebt(g); return 1; /* exit incremental collection */ } } g->GCmajorminor = g->GCmarked; /* prepare for next collection */ return 0; /* stay doing incremental collections */ } /* }====================================================== */ /* ** {====================================================== ** GC control ** ======================================================= */ /* ** Enter first sweep phase. ** The call to 'sweeptolive' makes the pointer point to an object ** inside the list (instead of to the header), so that the real sweep do ** not need to skip objects created between "now" and the start of the ** real sweep. */ static void entersweep (lua_State *L) { global_State *g = G(L); g->gcstate = GCSswpallgc; lua_assert(g->sweepgc == NULL); g->sweepgc = sweeptolive(L, &g->allgc); } /* ** Delete all objects in list 'p' until (but not including) object ** 'limit'. */ static void deletelist (lua_State *L, GCObject *p, GCObject *limit) { while (p != limit) { GCObject *next = p->next; freeobj(L, p); p = next; } } /* ** Call all finalizers of the objects in the given Lua state, and ** then free all objects, except for the main thread. */ void luaC_freeallobjects (lua_State *L) { global_State *g = G(L); g->gcstp = GCSTPCLS; /* no extra finalizers after here */ luaC_changemode(L, KGC_INC); separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); callallpendingfinalizers(L); deletelist(L, g->allgc, obj2gco(mainthread(g))); lua_assert(g->finobj == NULL); /* no new finalizers */ deletelist(L, g->fixedgc, NULL); /* collect fixed objects */ lua_assert(g->strt.nuse == 0); } static void atomic (lua_State *L) { global_State *g = G(L); GCObject *origweak, *origall; GCObject *grayagain = g->grayagain; /* save original list */ g->grayagain = NULL; lua_assert(g->ephemeron == NULL && g->weak == NULL); lua_assert(!iswhite(mainthread(g))); g->gcstate = GCSatomic; markobject(g, L); /* mark running thread */ /* registry and global metatables may be changed by API */ markvalue(g, &g->l_registry); markmt(g); /* mark global metatables */ propagateall(g); /* empties 'gray' list */ /* remark occasional upvalues of (maybe) dead threads */ remarkupvals(g); propagateall(g); /* propagate changes */ g->gray = grayagain; propagateall(g); /* traverse 'grayagain' list */ convergeephemerons(g); /* at this point, all strongly accessible objects are marked. */ /* Clear values from weak tables, before checking finalizers */ clearbyvalues(g, g->weak, NULL); clearbyvalues(g, g->allweak, NULL); origweak = g->weak; origall = g->allweak; separatetobefnz(g, 0); /* separate objects to be finalized */ markbeingfnz(g); /* mark objects that will be finalized */ propagateall(g); /* remark, to propagate 'resurrection' */ convergeephemerons(g); /* at this point, all resurrected objects are marked. */ /* remove dead objects from weak tables */ clearbykeys(g, g->ephemeron); /* clear keys from all ephemeron */ clearbykeys(g, g->allweak); /* clear keys from all 'allweak' */ /* clear values from resurrected weak tables */ clearbyvalues(g, g->weak, origweak); clearbyvalues(g, g->allweak, origall); luaS_clearcache(g); g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ lua_assert(g->gray == NULL); } /* ** Do a sweep step. The normal case (not fast) sweeps at most GCSWEEPMAX ** elements. The fast case sweeps the whole list. */ static void sweepstep (lua_State *L, global_State *g, lu_byte nextstate, GCObject **nextlist, int fast) { if (g->sweepgc) g->sweepgc = sweeplist(L, g->sweepgc, fast ? MAX_LMEM : GCSWEEPMAX); else { /* enter next state */ g->gcstate = nextstate; g->sweepgc = nextlist; } } /* ** Performs one incremental "step" in an incremental garbage collection. ** For indivisible work, a step goes to the next state. When marking ** (propagating), a step traverses one object. When sweeping, a step ** sweeps GCSWEEPMAX objects, to avoid a big overhead for sweeping ** objects one by one. (Sweeping is inexpensive, no matter the ** object.) When 'fast' is true, 'singlestep' tries to finish a state ** "as fast as possible". In particular, it skips the propagation ** phase and leaves all objects to be traversed by the atomic phase: ** That avoids traversing twice some objects, such as threads and ** weak tables. */ #define step2pause -3 /* finished collection; entered pause state */ #define atomicstep -2 /* atomic step */ #define step2minor -1 /* moved to minor collections */ static l_mem singlestep (lua_State *L, int fast) { global_State *g = G(L); l_mem stepresult; lua_assert(!g->gcstopem); /* collector is not reentrant */ g->gcstopem = 1; /* no emergency collections while collecting */ switch (g->gcstate) { case GCSpause: { restartcollection(g); g->gcstate = GCSpropagate; stepresult = 1; break; } case GCSpropagate: { if (fast || g->gray == NULL) { g->gcstate = GCSenteratomic; /* finish propagate phase */ stepresult = 1; } else stepresult = propagatemark(g); /* traverse one gray object */ break; } case GCSenteratomic: { atomic(L); if (checkmajorminor(L, g)) stepresult = step2minor; else { entersweep(L); stepresult = atomicstep; } break; } case GCSswpallgc: { /* sweep "regular" objects */ sweepstep(L, g, GCSswpfinobj, &g->finobj, fast); stepresult = GCSWEEPMAX; break; } case GCSswpfinobj: { /* sweep objects with finalizers */ sweepstep(L, g, GCSswptobefnz, &g->tobefnz, fast); stepresult = GCSWEEPMAX; break; } case GCSswptobefnz: { /* sweep objects to be finalized */ sweepstep(L, g, GCSswpend, NULL, fast); stepresult = GCSWEEPMAX; break; } case GCSswpend: { /* finish sweeps */ checkSizes(L, g); g->gcstate = GCScallfin; stepresult = GCSWEEPMAX; break; } case GCScallfin: { /* call finalizers */ if (g->tobefnz && !g->gcemergency && luaD_checkminstack(L)) { g->gcstopem = 0; /* ok collections during finalizers */ GCTM(L); /* call one finalizer */ stepresult = CWUFIN; } else { /* no more finalizers or emergency mode or no enough stack to run finalizers */ g->gcstate = GCSpause; /* finish collection */ stepresult = step2pause; } break; } default: lua_assert(0); return 0; } g->gcstopem = 0; return stepresult; } /* ** Advances the garbage collector until it reaches the given state. ** (The option 'fast' is only for testing; in normal code, 'fast' ** here is always true.) */ void luaC_runtilstate (lua_State *L, int state, int fast) { global_State *g = G(L); lua_assert(g->gckind == KGC_INC); while (state != g->gcstate) singlestep(L, fast); } /* ** Performs a basic incremental step. The step size is ** converted from bytes to "units of work"; then the function loops ** running single steps until adding that many units of work or ** finishing a cycle (pause state). Finally, it sets the debt that ** controls when next step will be performed. */ static void incstep (lua_State *L, global_State *g) { l_mem stepsize = applygcparam(g, STEPSIZE, 100); l_mem work2do = applygcparam(g, STEPMUL, stepsize / cast_int(sizeof(void*))); l_mem stres; int fast = (work2do == 0); /* special case: do a full collection */ do { /* repeat until enough work */ stres = singlestep(L, fast); /* perform one single step */ if (stres == step2minor) /* returned to minor collections? */ return; /* nothing else to be done here */ else if (stres == step2pause || (stres == atomicstep && !fast)) break; /* end of cycle or atomic */ else work2do -= stres; } while (fast || work2do > 0); if (g->gcstate == GCSpause) setpause(g); /* pause until next cycle */ else luaE_setdebt(g, stepsize); } #if !defined(luai_tracegc) #define luai_tracegc(L,f) ((void)0) #endif /* ** Performs a basic GC step if collector is running. (If collector was ** stopped by the user, set a reasonable debt to avoid it being called ** at every single check.) */ void luaC_step (lua_State *L) { global_State *g = G(L); lua_assert(!g->gcemergency); if (!gcrunning(g)) { /* not running? */ if (g->gcstp & GCSTPUSR) /* stopped by the user? */ luaE_setdebt(g, 20000); } else { luai_tracegc(L, 1); /* for internal debugging */ switch (g->gckind) { case KGC_INC: case KGC_GENMAJOR: incstep(L, g); break; case KGC_GENMINOR: youngcollection(L, g); setminordebt(g); break; } luai_tracegc(L, 0); /* for internal debugging */ } } /* ** Perform a full collection in incremental mode. ** Before running the collection, check 'keepinvariant'; if it is true, ** there may be some objects marked as black, so the collector has ** to sweep all objects to turn them back to white (as white has not ** changed, nothing will be collected). */ static void fullinc (lua_State *L, global_State *g) { if (keepinvariant(g)) /* black objects? */ entersweep(L); /* sweep everything to turn them back to white */ /* finish any pending sweep phase to start a new cycle */ luaC_runtilstate(L, GCSpause, 1); luaC_runtilstate(L, GCScallfin, 1); /* run up to finalizers */ luaC_runtilstate(L, GCSpause, 1); /* finish collection */ setpause(g); } /* ** Performs a full GC cycle; if 'isemergency', set a flag to avoid ** some operations which could change the interpreter state in some ** unexpected ways (running finalizers and shrinking some structures). */ void luaC_fullgc (lua_State *L, int isemergency) { global_State *g = G(L); lua_assert(!g->gcemergency); g->gcemergency = cast_byte(isemergency); /* set flag */ switch (g->gckind) { case KGC_GENMINOR: fullgen(L, g); break; case KGC_INC: fullinc(L, g); break; case KGC_GENMAJOR: g->gckind = KGC_INC; fullinc(L, g); g->gckind = KGC_GENMAJOR; break; } g->gcemergency = 0; } /* }====================================================== */ ================================================ FILE: 3rd/lua/lgc.h ================================================ /* ** $Id: lgc.h $ ** Garbage Collector ** See Copyright Notice in lua.h */ #ifndef lgc_h #define lgc_h #include #include "lobject.h" #include "lstate.h" /* ** Collectable objects may have one of three colors: white, which means ** the object is not marked; gray, which means the object is marked, but ** its references may be not marked; and black, which means that the ** object and all its references are marked. The main invariant of the ** garbage collector, while marking objects, is that a black object can ** never point to a white one. Moreover, any gray object must be in a ** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it ** can be visited again before finishing the collection cycle. (Open ** upvalues are an exception to this rule, as they are attached to ** a corresponding thread.) These lists have no meaning when the ** invariant is not being enforced (e.g., sweep phase). */ /* ** Possible states of the Garbage Collector */ #define GCSpropagate 0 #define GCSenteratomic 1 #define GCSatomic 2 #define GCSswpallgc 3 #define GCSswpfinobj 4 #define GCSswptobefnz 5 #define GCSswpend 6 #define GCScallfin 7 #define GCSpause 8 #define issweepphase(g) \ (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend) /* ** macro to tell when main invariant (white objects cannot point to black ** ones) must be kept. During a collection, the sweep phase may break ** the invariant, as objects turned white may point to still-black ** objects. The invariant is restored when sweep ends and all objects ** are white again. */ #define keepinvariant(g) ((g)->gcstate <= GCSatomic) /* ** some useful bit tricks */ #define resetbits(x,m) ((x) &= cast_byte(~(m))) #define setbits(x,m) ((x) |= (m)) #define testbits(x,m) ((x) & (m)) #define bitmask(b) (1<<(b)) #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) #define l_setbit(x,b) setbits(x, bitmask(b)) #define resetbit(x,b) resetbits(x, bitmask(b)) #define testbit(x,b) testbits(x, bitmask(b)) /* ** Layout for bit use in 'marked' field. First three bits are ** used for object "age" in generational mode. Last bit is used ** by tests. */ #define WHITE0BIT 3 /* object is white (type 0) */ #define WHITE1BIT 4 /* object is white (type 1) */ #define BLACKBIT 5 /* object is black */ #define FINALIZEDBIT 6 /* object has been marked for finalization */ #define TESTBIT 7 #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) #define iswhite(x) testbits((x)->marked, WHITEBITS) #define isblack(x) testbit((x)->marked, BLACKBIT) #define isgray(x) /* neither white nor black */ \ (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) #define isdeadm(ow,m) ((m) & (ow)) #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) #define changewhite(x) ((x)->marked ^= WHITEBITS) #define nw2black(x) \ check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT)) #define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS) /* object age in generational mode */ #define G_NEW 0 /* created in current cycle */ #define G_SURVIVAL 1 /* created in previous cycle */ #define G_OLD0 2 /* marked old by frw. barrier in this cycle */ #define G_OLD1 3 /* first full cycle as old */ #define G_OLD 4 /* really old object (not to be visited) */ #define G_TOUCHED1 5 /* old object touched this cycle */ #define G_TOUCHED2 6 /* old object touched in previous cycle */ #define G_SHARED 7 /* object is shared */ #define AGEBITS 7 /* all age bits (111) */ #define getage(o) ((o)->marked & AGEBITS) #define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a)) #define isold(o) (getage(o) > G_SURVIVAL) #define isshared(x) (getage(x) == G_SHARED) #define makeshared(x) setage(x, G_SHARED) #define ispurewhite(x) (iswhite(x) && !isshared(x)) /* ** In generational mode, objects are created 'new'. After surviving one ** cycle, they become 'survival'. Both 'new' and 'survival' can point ** to any other object, as they are traversed at the end of the cycle. ** We call them both 'young' objects. ** If a survival object survives another cycle, it becomes 'old1'. ** 'old1' objects can still point to survival objects (but not to ** new objects), so they still must be traversed. After another cycle ** (that, being old, 'old1' objects will "survive" no matter what) ** finally the 'old1' object becomes really 'old', and then they ** are no more traversed. ** ** To keep its invariants, the generational mode uses the same barriers ** also used by the incremental mode. If a young object is caught in a ** forward barrier, it cannot become old immediately, because it can ** still point to other young objects. Instead, it becomes 'old0', ** which in the next cycle becomes 'old1'. So, 'old0' objects is ** old but can point to new and survival objects; 'old1' is old ** but cannot point to new objects; and 'old' cannot point to any ** young object. ** ** If any old object ('old0', 'old1', 'old') is caught in a back ** barrier, it becomes 'touched1' and goes into a gray list, to be ** visited at the end of the cycle. There it evolves to 'touched2', ** which can point to survivals but not to new objects. In yet another ** cycle then it becomes 'old' again. ** ** The generational mode must also control the colors of objects, ** because of the barriers. While the mutator is running, young objects ** are kept white. 'old', 'old1', and 'touched2' objects are kept black, ** as they cannot point to new objects; exceptions are threads and open ** upvalues, which age to 'old1' and 'old' but are kept gray. 'old0' ** objects may be gray or black, as in the incremental mode. 'touched1' ** objects are kept gray, as they must be visited again at the end of ** the cycle. */ /* ** {====================================================== ** Default Values for GC parameters ** ======================================================= */ /* ** Minor collections will shift to major ones after LUAI_MINORMAJOR% ** bytes become old. */ #define LUAI_MINORMAJOR 70 /* ** Major collections will shift to minor ones after a collection ** collects at least LUAI_MAJORMINOR% of the new bytes. */ #define LUAI_MAJORMINOR 50 /* ** A young (minor) collection will run after creating LUAI_GENMINORMUL% ** new bytes. */ #define LUAI_GENMINORMUL 20 /* incremental */ /* Number of bytes must be LUAI_GCPAUSE% before starting new cycle */ #define LUAI_GCPAUSE 250 /* ** Step multiplier: The collector handles LUAI_GCMUL% work units for ** each new allocated word. (Each "work unit" corresponds roughly to ** sweeping one object or traversing one slot.) */ #define LUAI_GCMUL 200 /* How many bytes to allocate before next GC step */ #define LUAI_GCSTEPSIZE (200 * sizeof(Table)) #define setgcparam(g,p,v) (g->gcparams[LUA_GCP##p] = luaO_codeparam(v)) #define applygcparam(g,p,x) luaO_applyparam(g->gcparams[LUA_GCP##p], x) /* }====================================================== */ /* ** Control when GC is running: */ #define GCSTPUSR 1 /* bit true when GC stopped by user */ #define GCSTPGC 2 /* bit true when GC stopped by itself */ #define GCSTPCLS 4 /* bit true when closing Lua state */ #define gcrunning(g) ((g)->gcstp == 0) /* ** Does one step of collection when debt becomes zero. 'pre'/'pos' ** allows some adjustments to be done only when needed. macro ** 'condchangemem' is used only for heavy tests (forcing a full ** GC cycle on every opportunity) */ #if !defined(HARDMEMTESTS) #define condchangemem(L,pre,pos,emg) ((void)0) #else #define condchangemem(L,pre,pos,emg) \ { if (gcrunning(G(L))) { pre; luaC_fullgc(L, emg); pos; } } #endif #define luaC_condGC(L,pre,pos) \ { if (G(L)->GCdebt <= 0) { pre; luaC_step(L); pos;}; \ condchangemem(L,pre,pos,0); } /* more often than not, 'pre'/'pos' are empty */ #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) #define luaC_objbarrier(L,p,o) ( \ (isblack(p) && ispurewhite(o)) ? \ luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) #define luaC_barrier(L,p,v) ( \ iscollectable(v) ? luaC_objbarrier(L,p,gcvalue(v)) : cast_void(0)) #define luaC_objbarrierback(L,p,o) ( \ (isblack(p) && ispurewhite(o)) ? luaC_barrierback_(L,p) : cast_void(0)) #define luaC_barrierback(L,p,v) ( \ iscollectable(v) ? luaC_objbarrierback(L, p, gcvalue(v)) : cast_void(0)) LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); LUAI_FUNC void luaC_freeallobjects (lua_State *L); LUAI_FUNC void luaC_step (lua_State *L); LUAI_FUNC void luaC_runtilstate (lua_State *L, int state, int fast); LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); LUAI_FUNC GCObject *luaC_newobj (lua_State *L, lu_byte tt, size_t sz); LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, lu_byte tt, size_t sz, size_t offset); LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); LUAI_FUNC void luaC_changemode (lua_State *L, int newmode); #endif ================================================ FILE: 3rd/lua/linit.c ================================================ /* ** $Id: linit.c $ ** Initialization of libraries for lua.c and other clients ** See Copyright Notice in lua.h */ #define linit_c #define LUA_LIB #include "lprefix.h" #include #include "lua.h" #include "lualib.h" #include "lauxlib.h" #include "llimits.h" /* ** Standard Libraries. (Must be listed in the same ORDER of their ** respective constants LUA_K.) */ static const luaL_Reg stdlibs[] = { {LUA_GNAME, luaopen_base}, {LUA_LOADLIBNAME, luaopen_package}, {LUA_COLIBNAME, luaopen_coroutine}, {LUA_DBLIBNAME, luaopen_debug}, {LUA_IOLIBNAME, luaopen_io}, {LUA_MATHLIBNAME, luaopen_math}, {LUA_OSLIBNAME, luaopen_os}, {LUA_STRLIBNAME, luaopen_string}, {LUA_TABLIBNAME, luaopen_table}, {LUA_UTF8LIBNAME, luaopen_utf8}, {NULL, NULL} }; /* ** require and preload selected standard libraries */ LUALIB_API void luaL_openselectedlibs (lua_State *L, int load, int preload) { int mask; const luaL_Reg *lib; luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); for (lib = stdlibs, mask = 1; lib->name != NULL; lib++, mask <<= 1) { if (load & mask) { /* selected? */ luaL_requiref(L, lib->name, lib->func, 1); /* require library */ lua_pop(L, 1); /* remove result from the stack */ } else if (preload & mask) { /* selected? */ lua_pushcfunction(L, lib->func); lua_setfield(L, -2, lib->name); /* add library to PRELOAD table */ } } lua_assert((mask >> 1) == LUA_UTF8LIBK); lua_pop(L, 1); /* remove PRELOAD table */ } ================================================ FILE: 3rd/lua/liolib.c ================================================ /* ** $Id: liolib.c $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ #define liolib_c #define LUA_LIB #include "lprefix.h" #include #include #include #include #include #include #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "llimits.h" /* ** Change this macro to accept other modes for 'fopen' besides ** the standard ones. */ #if !defined(l_checkmode) /* accepted extensions to 'mode' in 'fopen' */ #if !defined(L_MODEEXT) #define L_MODEEXT "b" #endif /* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */ static int l_checkmode (const char *mode) { return (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && (*mode != '+' || ((void)(++mode), 1)) && /* skip if char is '+' */ (strspn(mode, L_MODEEXT) == strlen(mode))); /* check extensions */ } #endif /* ** {====================================================== ** l_popen spawns a new process connected to the current ** one through the file streams. ** ======================================================= */ #if !defined(l_popen) /* { */ #if defined(LUA_USE_POSIX) /* { */ #define l_popen(L,c,m) (fflush(NULL), popen(c,m)) #define l_pclose(L,file) (pclose(file)) #elif defined(LUA_USE_WINDOWS) /* }{ */ #define l_popen(L,c,m) (_popen(c,m)) #define l_pclose(L,file) (_pclose(file)) #if !defined(l_checkmodep) /* Windows accepts "[rw][bt]?" as valid modes */ #define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && \ (m[1] == '\0' || ((m[1] == 'b' || m[1] == 't') && m[2] == '\0'))) #endif #else /* }{ */ /* ISO C definitions */ #define l_popen(L,c,m) \ ((void)c, (void)m, \ luaL_error(L, "'popen' not supported"), \ (FILE*)0) #define l_pclose(L,file) ((void)L, (void)file, -1) #endif /* } */ #endif /* } */ #if !defined(l_checkmodep) /* By default, Lua accepts only "r" or "w" as valid modes */ #define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && m[1] == '\0') #endif /* }====================================================== */ #if !defined(l_getc) /* { */ #if defined(LUA_USE_POSIX) #define l_getc(f) getc_unlocked(f) #define l_lockfile(f) flockfile(f) #define l_unlockfile(f) funlockfile(f) #else #define l_getc(f) getc(f) #define l_lockfile(f) ((void)0) #define l_unlockfile(f) ((void)0) #endif #endif /* } */ /* ** {====================================================== ** l_fseek: configuration for longer offsets ** ======================================================= */ #if !defined(l_fseek) /* { */ #if defined(LUA_USE_POSIX) || defined(LUA_USE_OFF_T) /* { */ #include #define l_fseek(f,o,w) fseeko(f,o,w) #define l_ftell(f) ftello(f) #define l_seeknum off_t #elif defined(LUA_USE_WINDOWS) && !defined(_CRTIMP_TYPEINFO) \ && defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */ /* Windows (but not DDK) and Visual C++ 2005 or higher */ #define l_fseek(f,o,w) _fseeki64(f,o,w) #define l_ftell(f) _ftelli64(f) #define l_seeknum __int64 #else /* }{ */ /* ISO C definitions */ #define l_fseek(f,o,w) fseek(f,o,w) #define l_ftell(f) ftell(f) #define l_seeknum long #endif /* } */ #endif /* } */ /* }====================================================== */ #define IO_PREFIX "_IO_" #define IOPREF_LEN (sizeof(IO_PREFIX)/sizeof(char) - 1) #define IO_INPUT (IO_PREFIX "input") #define IO_OUTPUT (IO_PREFIX "output") typedef luaL_Stream LStream; #define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE)) #define isclosed(p) ((p)->closef == NULL) static int io_type (lua_State *L) { LStream *p; luaL_checkany(L, 1); p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); if (p == NULL) luaL_pushfail(L); /* not a file */ else if (isclosed(p)) lua_pushliteral(L, "closed file"); else lua_pushliteral(L, "file"); return 1; } static int f_tostring (lua_State *L) { LStream *p = tolstream(L); if (isclosed(p)) lua_pushliteral(L, "file (closed)"); else lua_pushfstring(L, "file (%p)", p->f); return 1; } static FILE *tofile (lua_State *L) { LStream *p = tolstream(L); if (l_unlikely(isclosed(p))) luaL_error(L, "attempt to use a closed file"); lua_assert(p->f); return p->f; } /* ** When creating file handles, always creates a 'closed' file handle ** before opening the actual file; so, if there is a memory error, the ** handle is in a consistent state. */ static LStream *newprefile (lua_State *L) { LStream *p = (LStream *)lua_newuserdatauv(L, sizeof(LStream), 0); p->closef = NULL; /* mark file handle as 'closed' */ luaL_setmetatable(L, LUA_FILEHANDLE); return p; } /* ** Calls the 'close' function from a file handle. The 'volatile' avoids ** a bug in some versions of the Clang compiler (e.g., clang 3.0 for ** 32 bits). */ static int aux_close (lua_State *L) { LStream *p = tolstream(L); volatile lua_CFunction cf = p->closef; p->closef = NULL; /* mark stream as closed */ return (*cf)(L); /* close it */ } static int f_close (lua_State *L) { tofile(L); /* make sure argument is an open stream */ return aux_close(L); } static int io_close (lua_State *L) { if (lua_isnone(L, 1)) /* no argument? */ lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use default output */ return f_close(L); } static int f_gc (lua_State *L) { LStream *p = tolstream(L); if (!isclosed(p) && p->f != NULL) aux_close(L); /* ignore closed and incompletely open files */ return 0; } /* ** function to close regular files */ static int io_fclose (lua_State *L) { LStream *p = tolstream(L); errno = 0; return luaL_fileresult(L, (fclose(p->f) == 0), NULL); } static LStream *newfile (lua_State *L) { LStream *p = newprefile(L); p->f = NULL; p->closef = &io_fclose; return p; } static void opencheck (lua_State *L, const char *fname, const char *mode) { LStream *p = newfile(L); p->f = fopen(fname, mode); if (l_unlikely(p->f == NULL)) luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); } static int io_open (lua_State *L) { const char *filename = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newfile(L); const char *md = mode; /* to traverse/check mode */ luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); errno = 0; p->f = fopen(filename, mode); return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; } /* ** function to close 'popen' files */ static int io_pclose (lua_State *L) { LStream *p = tolstream(L); errno = 0; return luaL_execresult(L, l_pclose(L, p->f)); } static int io_popen (lua_State *L) { const char *filename = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newprefile(L); luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode"); errno = 0; p->f = l_popen(L, filename, mode); p->closef = &io_pclose; return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; } static int io_tmpfile (lua_State *L) { LStream *p = newfile(L); errno = 0; p->f = tmpfile(); return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; } static FILE *getiofile (lua_State *L, const char *findex) { LStream *p; lua_getfield(L, LUA_REGISTRYINDEX, findex); p = (LStream *)lua_touserdata(L, -1); if (l_unlikely(isclosed(p))) luaL_error(L, "default %s file is closed", findex + IOPREF_LEN); return p->f; } static int g_iofile (lua_State *L, const char *f, const char *mode) { if (!lua_isnoneornil(L, 1)) { const char *filename = lua_tostring(L, 1); if (filename) opencheck(L, filename, mode); else { tofile(L); /* check that it's a valid file handle */ lua_pushvalue(L, 1); } lua_setfield(L, LUA_REGISTRYINDEX, f); } /* return current value */ lua_getfield(L, LUA_REGISTRYINDEX, f); return 1; } static int io_input (lua_State *L) { return g_iofile(L, IO_INPUT, "r"); } static int io_output (lua_State *L) { return g_iofile(L, IO_OUTPUT, "w"); } static int io_readline (lua_State *L); /* ** maximum number of arguments to 'f:lines'/'io.lines' (it + 3 must fit ** in the limit for upvalues of a closure) */ #define MAXARGLINE 250 /* ** Auxiliary function to create the iteration function for 'lines'. ** The iteration function is a closure over 'io_readline', with ** the following upvalues: ** 1) The file being read (first value in the stack) ** 2) the number of arguments to read ** 3) a boolean, true iff file has to be closed when finished ('toclose') ** *) a variable number of format arguments (rest of the stack) */ static void aux_lines (lua_State *L, int toclose) { int n = lua_gettop(L) - 1; /* number of arguments to read */ luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments"); lua_pushvalue(L, 1); /* file */ lua_pushinteger(L, n); /* number of arguments to read */ lua_pushboolean(L, toclose); /* close/not close file when finished */ lua_rotate(L, 2, 3); /* move the three values to their positions */ lua_pushcclosure(L, io_readline, 3 + n); } static int f_lines (lua_State *L) { tofile(L); /* check that it's a valid file handle */ aux_lines(L, 0); return 1; } /* ** Return an iteration function for 'io.lines'. If file has to be ** closed, also returns the file itself as a second result (to be ** closed as the state at the exit of a generic for). */ static int io_lines (lua_State *L) { int toclose; if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */ if (lua_isnil(L, 1)) { /* no file name? */ lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */ lua_replace(L, 1); /* put it at index 1 */ tofile(L); /* check that it's a valid file handle */ toclose = 0; /* do not close it after iteration */ } else { /* open a new file */ const char *filename = luaL_checkstring(L, 1); opencheck(L, filename, "r"); lua_replace(L, 1); /* put file at index 1 */ toclose = 1; /* close it after iteration */ } aux_lines(L, toclose); /* push iteration function */ if (toclose) { lua_pushnil(L); /* state */ lua_pushnil(L); /* control */ lua_pushvalue(L, 1); /* file is the to-be-closed variable (4th result) */ return 4; } else return 1; } /* ** {====================================================== ** READ ** ======================================================= */ /* maximum length of a numeral */ #if !defined (L_MAXLENNUM) #define L_MAXLENNUM 200 #endif /* auxiliary structure used by 'read_number' */ typedef struct { FILE *f; /* file being read */ int c; /* current character (look ahead) */ int n; /* number of elements in buffer 'buff' */ char buff[L_MAXLENNUM + 1]; /* +1 for ending '\0' */ } RN; /* ** Add current char to buffer (if not out of space) and read next one */ static int nextc (RN *rn) { if (l_unlikely(rn->n >= L_MAXLENNUM)) { /* buffer overflow? */ rn->buff[0] = '\0'; /* invalidate result */ return 0; /* fail */ } else { rn->buff[rn->n++] = cast_char(rn->c); /* save current char */ rn->c = l_getc(rn->f); /* read next one */ return 1; } } /* ** Accept current char if it is in 'set' (of size 2) */ static int test2 (RN *rn, const char *set) { if (rn->c == set[0] || rn->c == set[1]) return nextc(rn); else return 0; } /* ** Read a sequence of (hex)digits */ static int readdigits (RN *rn, int hex) { int count = 0; while ((hex ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn)) count++; return count; } /* ** Read a number: first reads a valid prefix of a numeral into a buffer. ** Then it calls 'lua_stringtonumber' to check whether the format is ** correct and to convert it to a Lua number. */ static int read_number (lua_State *L, FILE *f) { RN rn; int count = 0; int hex = 0; char decp[2]; rn.f = f; rn.n = 0; decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */ decp[1] = '.'; /* always accept a dot */ l_lockfile(rn.f); do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */ test2(&rn, "-+"); /* optional sign */ if (test2(&rn, "00")) { if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */ else count = 1; /* count initial '0' as a valid digit */ } count += readdigits(&rn, hex); /* integral part */ if (test2(&rn, decp)) /* decimal point? */ count += readdigits(&rn, hex); /* fractional part */ if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */ test2(&rn, "-+"); /* exponent sign */ readdigits(&rn, 0); /* exponent digits */ } ungetc(rn.c, rn.f); /* unread look-ahead char */ l_unlockfile(rn.f); rn.buff[rn.n] = '\0'; /* finish string */ if (l_likely(lua_stringtonumber(L, rn.buff))) return 1; /* ok, it is a valid number */ else { /* invalid format */ lua_pushnil(L); /* "result" to be removed */ return 0; /* read fails */ } } static int test_eof (lua_State *L, FILE *f) { int c = getc(f); ungetc(c, f); /* no-op when c == EOF */ lua_pushliteral(L, ""); return (c != EOF); } static int read_line (lua_State *L, FILE *f, int chop) { luaL_Buffer b; int c; luaL_buffinit(L, &b); do { /* may need to read several chunks to get whole line */ char *buff = luaL_prepbuffer(&b); /* preallocate buffer space */ unsigned i = 0; l_lockfile(f); /* no memory errors can happen inside the lock */ while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n') buff[i++] = cast_char(c); /* read up to end of line or buffer limit */ l_unlockfile(f); luaL_addsize(&b, i); } while (c != EOF && c != '\n'); /* repeat until end of line */ if (!chop && c == '\n') /* want a newline and have one? */ luaL_addchar(&b, '\n'); /* add ending newline to result */ luaL_pushresult(&b); /* close buffer */ /* return ok if read something (either a newline or something else) */ return (c == '\n' || lua_rawlen(L, -1) > 0); } static void read_all (lua_State *L, FILE *f) { size_t nr; luaL_Buffer b; luaL_buffinit(L, &b); do { /* read file in chunks of LUAL_BUFFERSIZE bytes */ char *p = luaL_prepbuffer(&b); nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f); luaL_addsize(&b, nr); } while (nr == LUAL_BUFFERSIZE); luaL_pushresult(&b); /* close buffer */ } static int read_chars (lua_State *L, FILE *f, size_t n) { size_t nr; /* number of chars actually read */ char *p; luaL_Buffer b; luaL_buffinit(L, &b); p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */ nr = fread(p, sizeof(char), n, f); /* try to read 'n' chars */ luaL_addsize(&b, nr); luaL_pushresult(&b); /* close buffer */ return (nr > 0); /* true iff read something */ } static int g_read (lua_State *L, FILE *f, int first) { int nargs = lua_gettop(L) - 1; int n, success; clearerr(f); errno = 0; if (nargs == 0) { /* no arguments? */ success = read_line(L, f, 1); n = first + 1; /* to return 1 result */ } else { /* ensure stack space for all results and for auxlib's buffer */ luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); success = 1; for (n = first; nargs-- && success; n++) { if (lua_type(L, n) == LUA_TNUMBER) { size_t l = (size_t)luaL_checkinteger(L, n); success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); } else { const char *p = luaL_checkstring(L, n); if (*p == '*') p++; /* skip optional '*' (for compatibility) */ switch (*p) { case 'n': /* number */ success = read_number(L, f); break; case 'l': /* line */ success = read_line(L, f, 1); break; case 'L': /* line with end-of-line */ success = read_line(L, f, 0); break; case 'a': /* file */ read_all(L, f); /* read entire file */ success = 1; /* always success */ break; default: return luaL_argerror(L, n, "invalid format"); } } } } if (ferror(f)) return luaL_fileresult(L, 0, NULL); if (!success) { lua_pop(L, 1); /* remove last result */ luaL_pushfail(L); /* push nil instead */ } return n - first; } static int io_read (lua_State *L) { return g_read(L, getiofile(L, IO_INPUT), 1); } static int f_read (lua_State *L) { return g_read(L, tofile(L), 2); } /* ** Iteration function for 'lines'. */ static int io_readline (lua_State *L) { LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1)); int i; int n = (int)lua_tointeger(L, lua_upvalueindex(2)); if (isclosed(p)) /* file is already closed? */ return luaL_error(L, "file is already closed"); lua_settop(L , 1); luaL_checkstack(L, n, "too many arguments"); for (i = 1; i <= n; i++) /* push arguments to 'g_read' */ lua_pushvalue(L, lua_upvalueindex(3 + i)); n = g_read(L, p->f, 2); /* 'n' is number of results */ lua_assert(n > 0); /* should return at least a nil */ if (lua_toboolean(L, -n)) /* read at least one value? */ return n; /* return them */ else { /* first result is false: EOF or error */ if (n > 1) { /* is there error information? */ /* 2nd result is error message */ return luaL_error(L, "%s", lua_tostring(L, -n + 1)); } if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ lua_settop(L, 0); /* clear stack */ lua_pushvalue(L, lua_upvalueindex(1)); /* push file at index 1 */ aux_close(L); /* close it */ } return 0; } } /* }====================================================== */ static int g_write (lua_State *L, FILE *f, int arg) { int nargs = lua_gettop(L) - arg; size_t totalbytes = 0; /* total number of bytes written */ errno = 0; for (; nargs--; arg++) { /* for each argument */ char buff[LUA_N2SBUFFSZ]; const char *s; size_t numbytes; /* bytes written in one call to 'fwrite' */ size_t len = lua_numbertocstring(L, arg, buff); /* try as a number */ if (len > 0) { /* did conversion work (value was a number)? */ s = buff; len--; } else /* must be a string */ s = luaL_checklstring(L, arg, &len); numbytes = fwrite(s, sizeof(char), len, f); totalbytes += numbytes; if (numbytes < len) { /* write error? */ int n = luaL_fileresult(L, 0, NULL); lua_pushinteger(L, cast_st2S(totalbytes)); return n + 1; /* return fail, error msg., error code, and counter */ } } return 1; /* no errors; file handle already on stack top */ } static int io_write (lua_State *L) { return g_write(L, getiofile(L, IO_OUTPUT), 1); } static int f_write (lua_State *L) { FILE *f = tofile(L); lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */ return g_write(L, f, 2); } static int f_seek (lua_State *L) { static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; static const char *const modenames[] = {"set", "cur", "end", NULL}; FILE *f = tofile(L); int op = luaL_checkoption(L, 2, "cur", modenames); lua_Integer p3 = luaL_optinteger(L, 3, 0); l_seeknum offset = (l_seeknum)p3; luaL_argcheck(L, (lua_Integer)offset == p3, 3, "not an integer in proper range"); errno = 0; op = l_fseek(f, offset, mode[op]); if (l_unlikely(op)) return luaL_fileresult(L, 0, NULL); /* error */ else { lua_pushinteger(L, (lua_Integer)l_ftell(f)); return 1; } } static int f_setvbuf (lua_State *L) { static const int mode[] = {_IONBF, _IOFBF, _IOLBF}; static const char *const modenames[] = {"no", "full", "line", NULL}; FILE *f = tofile(L); int op = luaL_checkoption(L, 2, NULL, modenames); lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); int res; errno = 0; res = setvbuf(f, NULL, mode[op], (size_t)sz); return luaL_fileresult(L, res == 0, NULL); } static int aux_flush (lua_State *L, FILE *f) { errno = 0; return luaL_fileresult(L, fflush(f) == 0, NULL); } static int f_flush (lua_State *L) { return aux_flush(L, tofile(L)); } static int io_flush (lua_State *L) { return aux_flush(L, getiofile(L, IO_OUTPUT)); } /* ** functions for 'io' library */ static const luaL_Reg iolib[] = { {"close", io_close}, {"flush", io_flush}, {"input", io_input}, {"lines", io_lines}, {"open", io_open}, {"output", io_output}, {"popen", io_popen}, {"read", io_read}, {"tmpfile", io_tmpfile}, {"type", io_type}, {"write", io_write}, {NULL, NULL} }; /* ** methods for file handles */ static const luaL_Reg meth[] = { {"read", f_read}, {"write", f_write}, {"lines", f_lines}, {"flush", f_flush}, {"seek", f_seek}, {"close", f_close}, {"setvbuf", f_setvbuf}, {NULL, NULL} }; /* ** metamethods for file handles */ static const luaL_Reg metameth[] = { {"__index", NULL}, /* placeholder */ {"__gc", f_gc}, {"__close", f_gc}, {"__tostring", f_tostring}, {NULL, NULL} }; static void createmeta (lua_State *L) { luaL_newmetatable(L, LUA_FILEHANDLE); /* metatable for file handles */ luaL_setfuncs(L, metameth, 0); /* add metamethods to new metatable */ luaL_newlibtable(L, meth); /* create method table */ luaL_setfuncs(L, meth, 0); /* add file methods to method table */ lua_setfield(L, -2, "__index"); /* metatable.__index = method table */ lua_pop(L, 1); /* pop metatable */ } /* ** function to (not) close the standard files stdin, stdout, and stderr */ static int io_noclose (lua_State *L) { LStream *p = tolstream(L); p->closef = &io_noclose; /* keep file opened */ luaL_pushfail(L); lua_pushliteral(L, "cannot close standard file"); return 2; } static void createstdfile (lua_State *L, FILE *f, const char *k, const char *fname) { LStream *p = newprefile(L); p->f = f; p->closef = &io_noclose; if (k != NULL) { lua_pushvalue(L, -1); lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */ } lua_setfield(L, -2, fname); /* add file to module */ } LUAMOD_API int luaopen_io (lua_State *L) { luaL_newlib(L, iolib); /* new module */ createmeta(L); /* create (and set) default files */ createstdfile(L, stdin, IO_INPUT, "stdin"); createstdfile(L, stdout, IO_OUTPUT, "stdout"); createstdfile(L, stderr, NULL, "stderr"); return 1; } ================================================ FILE: 3rd/lua/ljumptab.h ================================================ /* ** $Id: ljumptab.h $ ** Jump Table for the Lua interpreter ** See Copyright Notice in lua.h */ #undef vmdispatch #undef vmcase #undef vmbreak #define vmdispatch(x) goto *disptab[x]; #define vmcase(l) L_##l: #define vmbreak vmfetch(); vmdispatch(GET_OPCODE(i)); static const void *const disptab[NUM_OPCODES] = { #if 0 ** you can update the following list with this command: ** ** sed -n '/^OP_/!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h ** #endif &&L_OP_MOVE, &&L_OP_LOADI, &&L_OP_LOADF, &&L_OP_LOADK, &&L_OP_LOADKX, &&L_OP_LOADFALSE, &&L_OP_LFALSESKIP, &&L_OP_LOADTRUE, &&L_OP_LOADNIL, &&L_OP_GETUPVAL, &&L_OP_SETUPVAL, &&L_OP_GETTABUP, &&L_OP_GETTABLE, &&L_OP_GETI, &&L_OP_GETFIELD, &&L_OP_SETTABUP, &&L_OP_SETTABLE, &&L_OP_SETI, &&L_OP_SETFIELD, &&L_OP_NEWTABLE, &&L_OP_SELF, &&L_OP_ADDI, &&L_OP_ADDK, &&L_OP_SUBK, &&L_OP_MULK, &&L_OP_MODK, &&L_OP_POWK, &&L_OP_DIVK, &&L_OP_IDIVK, &&L_OP_BANDK, &&L_OP_BORK, &&L_OP_BXORK, &&L_OP_SHLI, &&L_OP_SHRI, &&L_OP_ADD, &&L_OP_SUB, &&L_OP_MUL, &&L_OP_MOD, &&L_OP_POW, &&L_OP_DIV, &&L_OP_IDIV, &&L_OP_BAND, &&L_OP_BOR, &&L_OP_BXOR, &&L_OP_SHL, &&L_OP_SHR, &&L_OP_MMBIN, &&L_OP_MMBINI, &&L_OP_MMBINK, &&L_OP_UNM, &&L_OP_BNOT, &&L_OP_NOT, &&L_OP_LEN, &&L_OP_CONCAT, &&L_OP_CLOSE, &&L_OP_TBC, &&L_OP_JMP, &&L_OP_EQ, &&L_OP_LT, &&L_OP_LE, &&L_OP_EQK, &&L_OP_EQI, &&L_OP_LTI, &&L_OP_LEI, &&L_OP_GTI, &&L_OP_GEI, &&L_OP_TEST, &&L_OP_TESTSET, &&L_OP_CALL, &&L_OP_TAILCALL, &&L_OP_RETURN, &&L_OP_RETURN0, &&L_OP_RETURN1, &&L_OP_FORLOOP, &&L_OP_FORPREP, &&L_OP_TFORPREP, &&L_OP_TFORCALL, &&L_OP_TFORLOOP, &&L_OP_SETLIST, &&L_OP_CLOSURE, &&L_OP_VARARG, &&L_OP_GETVARG, &&L_OP_ERRNNIL, &&L_OP_VARARGPREP, &&L_OP_EXTRAARG }; ================================================ FILE: 3rd/lua/llex.c ================================================ /* ** $Id: llex.c $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ #define llex_c #define LUA_CORE #include "lprefix.h" #include #include #include "lua.h" #include "lctype.h" #include "ldebug.h" #include "ldo.h" #include "lgc.h" #include "llex.h" #include "lobject.h" #include "lparser.h" #include "lstate.h" #include "lstring.h" #include "ltable.h" #include "lzio.h" #define next(ls) (ls->current = zgetc(ls->z)) /* minimum size for string buffer */ #if !defined(LUA_MINBUFFER) #define LUA_MINBUFFER 32 #endif #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') /* ORDER RESERVED */ static const char *const luaX_tokens [] = { "and", "break", "do", "else", "elseif", "end", "false", "for", "function", "global", "goto", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while", "//", "..", "...", "==", ">=", "<=", "~=", "<<", ">>", "::", "", "", "", "", "" }; #define save_and_next(ls) (save(ls, ls->current), next(ls)) static l_noret lexerror (LexState *ls, const char *msg, int token); static void save (LexState *ls, int c) { Mbuffer *b = ls->buff; if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) { size_t newsize = luaZ_sizebuffer(b); /* get old size */; if (newsize >= (MAX_SIZE/3 * 2)) /* larger than MAX_SIZE/1.5 ? */ lexerror(ls, "lexical element too long", 0); newsize += (newsize >> 1); /* new size is 1.5 times the old one */ luaZ_resizebuffer(ls->L, b, newsize); } b->buffer[luaZ_bufflen(b)++] = cast_char(c); } void luaX_init (lua_State *L) { int i; TString *e = luaS_newliteral(L, LUA_ENV); /* create env name */ luaC_fix(L, obj2gco(e)); /* never collect this name */ for (i=0; iextra = cast_byte(i+1); /* reserved word */ } } const char *luaX_token2str (LexState *ls, int token) { if (token < FIRST_RESERVED) { /* single-byte symbols? */ if (lisprint(token)) return luaO_pushfstring(ls->L, "'%c'", token); else /* control character */ return luaO_pushfstring(ls->L, "'<\\%d>'", token); } else { const char *s = luaX_tokens[token - FIRST_RESERVED]; if (token < TK_EOS) /* fixed format (symbols and reserved words)? */ return luaO_pushfstring(ls->L, "'%s'", s); else /* names, strings, and numerals */ return s; } } static const char *txtToken (LexState *ls, int token) { switch (token) { case TK_NAME: case TK_STRING: case TK_FLT: case TK_INT: save(ls, '\0'); return luaO_pushfstring(ls->L, "'%s'", luaZ_buffer(ls->buff)); default: return luaX_token2str(ls, token); } } static l_noret lexerror (LexState *ls, const char *msg, int token) { msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber); if (token) luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token)); luaD_throw(ls->L, LUA_ERRSYNTAX); } l_noret luaX_syntaxerror (LexState *ls, const char *msg) { lexerror(ls, msg, ls->t.token); } /* ** Anchors a string in scanner's table so that it will not be collected ** until the end of the compilation; by that time it should be anchored ** somewhere. It also internalizes long strings, ensuring there is only ** one copy of each unique string. */ static TString *anchorstr (LexState *ls, TString *ts) { lua_State *L = ls->L; TValue oldts; int tag = luaH_getstr(ls->h, ts, &oldts); if (!tagisempty(tag)) /* string already present? */ return tsvalue(&oldts); /* use stored value */ else { /* create a new entry */ TValue *stv = s2v(L->top.p++); /* reserve stack space for string */ setsvalue(L, stv, ts); /* push (anchor) the string on the stack */ luaH_set(L, ls->h, stv, stv); /* t[string] = string */ /* table is not a metatable, so it does not need to invalidate cache */ luaC_checkGC(L); L->top.p--; /* remove string from stack */ return ts; } } /* ** Creates a new string and anchors it in scanner's table. */ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { return anchorstr(ls, luaS_newlstr(ls->L, str, l)); } /* ** increment line number and skips newline sequence (any of ** \n, \r, \n\r, or \r\n) */ static void inclinenumber (LexState *ls) { int old = ls->current; lua_assert(currIsNewline(ls)); next(ls); /* skip '\n' or '\r' */ if (currIsNewline(ls) && ls->current != old) next(ls); /* skip '\n\r' or '\r\n' */ if (++ls->linenumber >= INT_MAX) lexerror(ls, "chunk has too many lines", 0); } void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, int firstchar) { ls->t.token = 0; ls->L = L; ls->current = firstchar; ls->lookahead.token = TK_EOS; /* no look-ahead token */ ls->z = z; ls->fs = NULL; ls->linenumber = 1; ls->lastline = 1; ls->source = source; /* all three strings here ("_ENV", "break", "global") were fixed, so they cannot be collected */ ls->envn = luaS_newliteral(L, LUA_ENV); /* get env string */ ls->brkn = luaS_newliteral(L, "break"); /* get "break" string */ #if defined(LUA_COMPAT_GLOBAL) /* compatibility mode: "global" is not a reserved word */ ls->glbn = luaS_newliteral(L, "global"); /* get "global" string */ ls->glbn->extra = 0; /* mark it as not reserved */ #endif luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ } /* ** ======================================================= ** LEXICAL ANALYZER ** ======================================================= */ static int check_next1 (LexState *ls, int c) { if (ls->current == c) { next(ls); return 1; } else return 0; } /* ** Check whether current char is in set 'set' (with two chars) and ** saves it */ static int check_next2 (LexState *ls, const char *set) { lua_assert(set[2] == '\0'); if (ls->current == set[0] || ls->current == set[1]) { save_and_next(ls); return 1; } else return 0; } /* LUA_NUMBER */ /* ** This function is quite liberal in what it accepts, as 'luaO_str2num' ** will reject ill-formed numerals. Roughly, it accepts the following ** pattern: ** ** %d(%x|%.|([Ee][+-]?))* | 0[Xx](%x|%.|([Pp][+-]?))* ** ** The only tricky part is to accept [+-] only after a valid exponent ** mark, to avoid reading '3-4' or '0xe+1' as a single number. ** ** The caller might have already read an initial dot. */ static int read_numeral (LexState *ls, SemInfo *seminfo) { TValue obj; const char *expo = "Ee"; int first = ls->current; lua_assert(lisdigit(ls->current)); save_and_next(ls); if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */ expo = "Pp"; for (;;) { if (check_next2(ls, expo)) /* exponent mark? */ check_next2(ls, "-+"); /* optional exponent sign */ else if (lisxdigit(ls->current) || ls->current == '.') /* '%x|%.' */ save_and_next(ls); else break; } if (lislalpha(ls->current)) /* is numeral touching a letter? */ save_and_next(ls); /* force an error */ save(ls, '\0'); if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */ lexerror(ls, "malformed number", TK_FLT); if (ttisinteger(&obj)) { seminfo->i = ivalue(&obj); return TK_INT; } else { lua_assert(ttisfloat(&obj)); seminfo->r = fltvalue(&obj); return TK_FLT; } } /* ** read a sequence '[=*[' or ']=*]', leaving the last bracket. If ** sequence is well formed, return its number of '='s + 2; otherwise, ** return 1 if it is a single bracket (no '='s and no 2nd bracket); ** otherwise (an unfinished '[==...') return 0. */ static size_t skip_sep (LexState *ls) { size_t count = 0; int s = ls->current; lua_assert(s == '[' || s == ']'); save_and_next(ls); while (ls->current == '=') { save_and_next(ls); count++; } return (ls->current == s) ? count + 2 : (count == 0) ? 1 : 0; } static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) { int line = ls->linenumber; /* initial line (for error message) */ save_and_next(ls); /* skip 2nd '[' */ if (currIsNewline(ls)) /* string starts with a newline? */ inclinenumber(ls); /* skip it */ for (;;) { switch (ls->current) { case EOZ: { /* error */ const char *what = (seminfo ? "string" : "comment"); const char *msg = luaO_pushfstring(ls->L, "unfinished long %s (starting at line %d)", what, line); lexerror(ls, msg, TK_EOS); break; /* to avoid warnings */ } case ']': { if (skip_sep(ls) == sep) { save_and_next(ls); /* skip 2nd ']' */ goto endloop; } break; } case '\n': case '\r': { save(ls, '\n'); inclinenumber(ls); if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */ break; } default: { if (seminfo) save_and_next(ls); else next(ls); } } } endloop: if (seminfo) seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep, luaZ_bufflen(ls->buff) - 2 * sep); } static void esccheck (LexState *ls, int c, const char *msg) { if (!c) { if (ls->current != EOZ) save_and_next(ls); /* add current to buffer for error message */ lexerror(ls, msg, TK_STRING); } } static int gethexa (LexState *ls) { save_and_next(ls); esccheck (ls, lisxdigit(ls->current), "hexadecimal digit expected"); return luaO_hexavalue(ls->current); } static int readhexaesc (LexState *ls) { int r = gethexa(ls); r = (r << 4) + gethexa(ls); luaZ_buffremove(ls->buff, 2); /* remove saved chars from buffer */ return r; } /* ** When reading a UTF-8 escape sequence, save everything to the buffer ** for error reporting in case of errors; 'i' counts the number of ** saved characters, so that they can be removed if case of success. */ static l_uint32 readutf8esc (LexState *ls) { l_uint32 r; int i = 4; /* number of chars to be removed: start with #"\u{X" */ save_and_next(ls); /* skip 'u' */ esccheck(ls, ls->current == '{', "missing '{'"); r = cast_uint(gethexa(ls)); /* must have at least one digit */ while (cast_void(save_and_next(ls)), lisxdigit(ls->current)) { i++; esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large"); r = (r << 4) + luaO_hexavalue(ls->current); } esccheck(ls, ls->current == '}', "missing '}'"); next(ls); /* skip '}' */ luaZ_buffremove(ls->buff, i); /* remove saved chars from buffer */ return r; } static void utf8esc (LexState *ls) { char buff[UTF8BUFFSZ]; int n = luaO_utf8esc(buff, readutf8esc(ls)); for (; n > 0; n--) /* add 'buff' to string */ save(ls, buff[UTF8BUFFSZ - n]); } static int readdecesc (LexState *ls) { int i; int r = 0; /* result accumulator */ for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */ r = 10*r + ls->current - '0'; save_and_next(ls); } esccheck(ls, r <= UCHAR_MAX, "decimal escape too large"); luaZ_buffremove(ls->buff, i); /* remove read digits from buffer */ return r; } static void read_string (LexState *ls, int del, SemInfo *seminfo) { save_and_next(ls); /* keep delimiter (for error messages) */ while (ls->current != del) { switch (ls->current) { case EOZ: lexerror(ls, "unfinished string", TK_EOS); break; /* to avoid warnings */ case '\n': case '\r': lexerror(ls, "unfinished string", TK_STRING); break; /* to avoid warnings */ case '\\': { /* escape sequences */ int c; /* final character to be saved */ save_and_next(ls); /* keep '\\' for error messages */ switch (ls->current) { case 'a': c = '\a'; goto read_save; case 'b': c = '\b'; goto read_save; case 'f': c = '\f'; goto read_save; case 'n': c = '\n'; goto read_save; case 'r': c = '\r'; goto read_save; case 't': c = '\t'; goto read_save; case 'v': c = '\v'; goto read_save; case 'x': c = readhexaesc(ls); goto read_save; case 'u': utf8esc(ls); goto no_save; case '\n': case '\r': inclinenumber(ls); c = '\n'; goto only_save; case '\\': case '\"': case '\'': c = ls->current; goto read_save; case EOZ: goto no_save; /* will raise an error next loop */ case 'z': { /* zap following span of spaces */ luaZ_buffremove(ls->buff, 1); /* remove '\\' */ next(ls); /* skip the 'z' */ while (lisspace(ls->current)) { if (currIsNewline(ls)) inclinenumber(ls); else next(ls); } goto no_save; } default: { esccheck(ls, lisdigit(ls->current), "invalid escape sequence"); c = readdecesc(ls); /* digital escape '\ddd' */ goto only_save; } } read_save: next(ls); /* go through */ only_save: luaZ_buffremove(ls->buff, 1); /* remove '\\' */ save(ls, c); /* go through */ no_save: break; } default: save_and_next(ls); } } save_and_next(ls); /* skip delimiter */ seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1, luaZ_bufflen(ls->buff) - 2); } static int llex (LexState *ls, SemInfo *seminfo) { luaZ_resetbuffer(ls->buff); for (;;) { switch (ls->current) { case '\n': case '\r': { /* line breaks */ inclinenumber(ls); break; } case ' ': case '\f': case '\t': case '\v': { /* spaces */ next(ls); break; } case '-': { /* '-' or '--' (comment) */ next(ls); if (ls->current != '-') return '-'; /* else is a comment */ next(ls); if (ls->current == '[') { /* long comment? */ size_t sep = skip_sep(ls); luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */ if (sep >= 2) { read_long_string(ls, NULL, sep); /* skip long comment */ luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */ break; } } /* else short comment */ while (!currIsNewline(ls) && ls->current != EOZ) next(ls); /* skip until end of line (or end of file) */ break; } case '[': { /* long string or simply '[' */ size_t sep = skip_sep(ls); if (sep >= 2) { read_long_string(ls, seminfo, sep); return TK_STRING; } else if (sep == 0) /* '[=...' missing second bracket? */ lexerror(ls, "invalid long string delimiter", TK_STRING); return '['; } case '=': { next(ls); if (check_next1(ls, '=')) return TK_EQ; /* '==' */ else return '='; } case '<': { next(ls); if (check_next1(ls, '=')) return TK_LE; /* '<=' */ else if (check_next1(ls, '<')) return TK_SHL; /* '<<' */ else return '<'; } case '>': { next(ls); if (check_next1(ls, '=')) return TK_GE; /* '>=' */ else if (check_next1(ls, '>')) return TK_SHR; /* '>>' */ else return '>'; } case '/': { next(ls); if (check_next1(ls, '/')) return TK_IDIV; /* '//' */ else return '/'; } case '~': { next(ls); if (check_next1(ls, '=')) return TK_NE; /* '~=' */ else return '~'; } case ':': { next(ls); if (check_next1(ls, ':')) return TK_DBCOLON; /* '::' */ else return ':'; } case '"': case '\'': { /* short literal strings */ read_string(ls, ls->current, seminfo); return TK_STRING; } case '.': { /* '.', '..', '...', or number */ save_and_next(ls); if (check_next1(ls, '.')) { if (check_next1(ls, '.')) return TK_DOTS; /* '...' */ else return TK_CONCAT; /* '..' */ } else if (!lisdigit(ls->current)) return '.'; else return read_numeral(ls, seminfo); } case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { return read_numeral(ls, seminfo); } case EOZ: { return TK_EOS; } default: { if (lislalpha(ls->current)) { /* identifier or reserved word? */ TString *ts; do { save_and_next(ls); } while (lislalnum(ls->current)); /* find or create string */ ts = luaS_newlstr(ls->L, luaZ_buffer(ls->buff), luaZ_bufflen(ls->buff)); if (isreserved(ts)) /* reserved word? */ return ts->extra - 1 + FIRST_RESERVED; else { seminfo->ts = anchorstr(ls, ts); return TK_NAME; } } else { /* single-char tokens ('+', '*', '%', '{', '}', ...) */ int c = ls->current; next(ls); return c; } } } } } void luaX_next (LexState *ls) { ls->lastline = ls->linenumber; if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */ ls->t = ls->lookahead; /* use this one */ ls->lookahead.token = TK_EOS; /* and discharge it */ } else ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */ } int luaX_lookahead (LexState *ls) { lua_assert(ls->lookahead.token == TK_EOS); ls->lookahead.token = llex(ls, &ls->lookahead.seminfo); return ls->lookahead.token; } ================================================ FILE: 3rd/lua/llex.h ================================================ /* ** $Id: llex.h $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ #ifndef llex_h #define llex_h #include #include "lobject.h" #include "lzio.h" /* ** Single-char tokens (terminal symbols) are represented by their own ** numeric code. Other tokens start at the following value. */ #define FIRST_RESERVED (UCHAR_MAX + 1) #if !defined(LUA_ENV) #define LUA_ENV "_ENV" #endif /* * WARNING: if you change the order of this enumeration, * grep "ORDER RESERVED" */ enum RESERVED { /* terminal symbols denoted by reserved words */ TK_AND = FIRST_RESERVED, TK_BREAK, TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, TK_GLOBAL, TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, /* other terminal symbols */ TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_SHL, TK_SHR, TK_DBCOLON, TK_EOS, TK_FLT, TK_INT, TK_NAME, TK_STRING }; /* number of reserved words */ #define NUM_RESERVED (cast_int(TK_WHILE-FIRST_RESERVED + 1)) typedef union { lua_Number r; lua_Integer i; TString *ts; } SemInfo; /* semantics information */ typedef struct Token { int token; SemInfo seminfo; } Token; /* state of the scanner plus state of the parser when shared by all functions */ typedef struct LexState { int current; /* current character (charint) */ int linenumber; /* input line counter */ int lastline; /* line of last token 'consumed' */ Token t; /* current token */ Token lookahead; /* look ahead token */ struct FuncState *fs; /* current function (parser) */ struct lua_State *L; ZIO *z; /* input stream */ Mbuffer *buff; /* buffer for tokens */ Table *h; /* to avoid collection/reuse strings */ struct Dyndata *dyd; /* dynamic structures used by the parser */ TString *source; /* current source name */ TString *envn; /* environment variable name */ TString *brkn; /* "break" name (used as a label) */ TString *glbn; /* "global" name (when not a reserved word) */ } LexState; LUAI_FUNC void luaX_init (lua_State *L); LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, int firstchar); LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); LUAI_FUNC void luaX_next (LexState *ls); LUAI_FUNC int luaX_lookahead (LexState *ls); LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); #endif ================================================ FILE: 3rd/lua/llimits.h ================================================ /* ** $Id: llimits.h $ ** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ #ifndef llimits_h #define llimits_h #include #include #include "lua.h" #define l_numbits(t) cast_int(sizeof(t) * CHAR_BIT) /* ** 'l_mem' is a signed integer big enough to count the total memory ** used by Lua. (It is signed due to the use of debt in several ** computations.) 'lu_mem' is a corresponding unsigned type. Usually, ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. */ #if defined(LUAI_MEM) /* { external definitions? */ typedef LUAI_MEM l_mem; typedef LUAI_UMEM lu_mem; #elif LUAI_IS32INT /* }{ */ typedef ptrdiff_t l_mem; typedef size_t lu_mem; #else /* 16-bit ints */ /* }{ */ typedef long l_mem; typedef unsigned long lu_mem; #endif /* } */ #define MAX_LMEM \ cast(l_mem, (cast(lu_mem, 1) << (l_numbits(l_mem) - 1)) - 1) /* chars used as small naturals (so that 'char' is reserved for characters) */ typedef unsigned char lu_byte; typedef signed char ls_byte; /* Type for thread status/error codes */ typedef lu_byte TStatus; /* The C API still uses 'int' for status/error codes */ #define APIstatus(st) cast_int(st) /* maximum value for size_t */ #define MAX_SIZET ((size_t)(~(size_t)0)) /* ** Maximum size for strings and userdata visible for Lua; should be ** representable as a lua_Integer and as a size_t. */ #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ : cast_sizet(LUA_MAXINTEGER)) /* ** test whether an unsigned value is a power of 2 (or zero) */ #define ispow2(x) (((x) & ((x) - 1)) == 0) /* number of chars of a literal string without the ending \0 */ #define LL(x) (sizeof(x)/sizeof(char) - 1) /* ** conversion of pointer to unsigned integer: this is for hashing only; ** there is no problem if the integer cannot hold the whole pointer ** value. (In strict ISO C this may cause undefined behavior, but no ** actual machine seems to bother.) */ #if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ __STDC_VERSION__ >= 199901L #include #if defined(UINTPTR_MAX) /* even in C99 this type is optional */ #define L_P2I uintptr_t #else /* no 'intptr'? */ #define L_P2I uintmax_t /* use the largest available integer */ #endif #else /* C89 option */ #define L_P2I size_t #endif #define point2uint(p) cast_uint((L_P2I)(p) & UINT_MAX) /* types of 'usual argument conversions' for lua_Number and lua_Integer */ typedef LUAI_UACNUMBER l_uacNumber; typedef LUAI_UACINT l_uacInt; /* ** Internal assertions for in-house debugging */ #if defined LUAI_ASSERT #undef NDEBUG #include #define lua_assert(c) assert(c) #define assert_code(c) c #endif #if defined(lua_assert) #else #define lua_assert(c) ((void)0) #define assert_code(c) ((void)0) #endif #define check_exp(c,e) (lua_assert(c), (e)) /* to avoid problems with conditions too long */ #define lua_longassert(c) assert_code((c) ? (void)0 : lua_assert(0)) /* macro to avoid warnings about unused variables */ #if !defined(UNUSED) #define UNUSED(x) ((void)(x)) #endif /* type casts (a macro highlights casts in the code) */ #define cast(t, exp) ((t)(exp)) #define cast_void(i) cast(void, (i)) #define cast_voidp(i) cast(void *, (i)) #define cast_num(i) cast(lua_Number, (i)) #define cast_int(i) cast(int, (i)) #define cast_short(i) cast(short, (i)) #define cast_uint(i) cast(unsigned int, (i)) #define cast_byte(i) cast(lu_byte, (i)) #define cast_uchar(i) cast(unsigned char, (i)) #define cast_char(i) cast(char, (i)) #define cast_charp(i) cast(char *, (i)) #define cast_sizet(i) cast(size_t, (i)) #define cast_Integer(i) cast(lua_Integer, (i)) #define cast_Inst(i) cast(Instruction, (i)) /* cast a signed lua_Integer to lua_Unsigned */ #if !defined(l_castS2U) #define l_castS2U(i) ((lua_Unsigned)(i)) #endif /* ** cast a lua_Unsigned to a signed lua_Integer; this cast is ** not strict ISO C, but two-complement architectures should ** work fine. */ #if !defined(l_castU2S) #define l_castU2S(i) ((lua_Integer)(i)) #endif /* ** cast a size_t to lua_Integer: These casts are always valid for ** sizes of Lua objects (see MAX_SIZE) */ #define cast_st2S(sz) ((lua_Integer)(sz)) /* Cast a ptrdiff_t to size_t, when it is known that the minuend ** comes from the subtrahend (the base) */ #define ct_diff2sz(df) ((size_t)(df)) /* ptrdiff_t to lua_Integer */ #define ct_diff2S(df) cast_st2S(ct_diff2sz(df)) /* ** Special type equivalent to '(void*)' for functions (to suppress some ** warnings when converting function pointers) */ typedef void (*voidf)(void); /* ** Macro to convert pointer-to-void* to pointer-to-function. This cast ** is undefined according to ISO C, but POSIX assumes that it works. ** (The '__extension__' in gnu compilers is only to avoid warnings.) */ #if defined(__GNUC__) #define cast_func(p) (__extension__ (voidf)(p)) #else #define cast_func(p) ((voidf)(p)) #endif /* ** non-return type */ #if !defined(l_noret) #if defined(__GNUC__) #define l_noret void __attribute__((noreturn)) #elif defined(_MSC_VER) && _MSC_VER >= 1200 #define l_noret void __declspec(noreturn) #else #define l_noret void #endif #endif /* ** Inline functions */ #if !defined(LUA_USE_C89) #define l_inline inline #elif defined(__GNUC__) #define l_inline __inline__ #else #define l_inline /* empty */ #endif #define l_sinline static l_inline /* ** An unsigned with (at least) 4 bytes */ #if LUAI_IS32INT typedef unsigned int l_uint32; #else typedef unsigned long l_uint32; #endif /* ** The luai_num* macros define the primitive operations over numbers. */ /* floor division (defined as 'floor(a/b)') */ #if !defined(luai_numidiv) #define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b))) #endif /* float division */ #if !defined(luai_numdiv) #define luai_numdiv(L,a,b) ((a)/(b)) #endif /* ** modulo: defined as 'a - floor(a/b)*b'; the direct computation ** using this definition has several problems with rounding errors, ** so it is better to use 'fmod'. 'fmod' gives the result of ** 'a - trunc(a/b)*b', and therefore must be corrected when ** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a ** non-integer negative result: non-integer result is equivalent to ** a non-zero remainder 'm'; negative result is equivalent to 'a' and ** 'b' with different signs, or 'm' and 'b' with different signs ** (as the result 'm' of 'fmod' has the same sign of 'a'). */ #if !defined(luai_nummod) #define luai_nummod(L,a,b,m) \ { (void)L; (m) = l_mathop(fmod)(a,b); \ if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); } #endif /* exponentiation */ #if !defined(luai_numpow) #define luai_numpow(L,a,b) \ ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b)) #endif /* the others are quite standard operations */ #if !defined(luai_numadd) #define luai_numadd(L,a,b) ((a)+(b)) #define luai_numsub(L,a,b) ((a)-(b)) #define luai_nummul(L,a,b) ((a)*(b)) #define luai_numunm(L,a) (-(a)) #define luai_numeq(a,b) ((a)==(b)) #define luai_numlt(a,b) ((a)<(b)) #define luai_numle(a,b) ((a)<=(b)) #define luai_numgt(a,b) ((a)>(b)) #define luai_numge(a,b) ((a)>=(b)) #define luai_numisnan(a) (!luai_numeq((a), (a))) #endif /* ** lua_numbertointeger converts a float number with an integral value ** to an integer, or returns 0 if the float is not within the range of ** a lua_Integer. (The range comparisons are tricky because of ** rounding. The tests here assume a two-complement representation, ** where MININTEGER always has an exact representation as a float; ** MAXINTEGER may not have one, and therefore its conversion to float ** may have an ill-defined value.) */ #define lua_numbertointeger(n,p) \ ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ (*(p) = (LUA_INTEGER)(n), 1)) /* ** LUAI_FUNC is a mark for all extern functions that are not to be ** exported to outside modules. ** LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables, ** none of which to be exported to outside modules (LUAI_DDEF for ** definitions and LUAI_DDEC for declarations). ** Elf and MACH/gcc (versions 3.2 and later) mark them as "hidden" to ** optimize access when Lua is compiled as a shared library. Not all elf ** targets support this attribute. Unfortunately, gcc does not offer ** a way to check whether the target offers that support, and those ** without support give a warning about it. To avoid these warnings, ** change to the default definition. */ #if !defined(LUAI_FUNC) #if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ (defined(__ELF__) || defined(__MACH__)) #define LUAI_FUNC __attribute__((visibility("internal"))) extern #else #define LUAI_FUNC extern #endif #define LUAI_DDEC(dec) LUAI_FUNC dec #define LUAI_DDEF /* empty */ #endif /* Give these macros simpler names for internal use */ #define l_likely(x) luai_likely(x) #define l_unlikely(x) luai_unlikely(x) /* ** {================================================================== ** "Abstraction Layer" for basic report of messages and errors ** =================================================================== */ /* print a string */ #if !defined(lua_writestring) #define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) #endif /* print a newline and flush the output */ #if !defined(lua_writeline) #define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) #endif /* print an error message */ #if !defined(lua_writestringerror) #define lua_writestringerror(s,p) \ (fprintf(stderr, (s), (p)), fflush(stderr)) #endif /* }================================================================== */ #endif ================================================ FILE: 3rd/lua/lmathlib.c ================================================ /* ** $Id: lmathlib.c $ ** Standard mathematical library ** See Copyright Notice in lua.h */ #define lmathlib_c #define LUA_LIB #include "lprefix.h" #include #include #include #include #include #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "llimits.h" #undef PI #define PI (l_mathop(3.141592653589793238462643383279502884)) static int math_abs (lua_State *L) { if (lua_isinteger(L, 1)) { lua_Integer n = lua_tointeger(L, 1); if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n); lua_pushinteger(L, n); } else lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); return 1; } static int math_sin (lua_State *L) { lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1))); return 1; } static int math_cos (lua_State *L) { lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1))); return 1; } static int math_tan (lua_State *L) { lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1))); return 1; } static int math_asin (lua_State *L) { lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1))); return 1; } static int math_acos (lua_State *L) { lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1))); return 1; } static int math_atan (lua_State *L) { lua_Number y = luaL_checknumber(L, 1); lua_Number x = luaL_optnumber(L, 2, 1); lua_pushnumber(L, l_mathop(atan2)(y, x)); return 1; } static int math_toint (lua_State *L) { int valid; lua_Integer n = lua_tointegerx(L, 1, &valid); if (l_likely(valid)) lua_pushinteger(L, n); else { luaL_checkany(L, 1); luaL_pushfail(L); /* value is not convertible to integer */ } return 1; } static void pushnumint (lua_State *L, lua_Number d) { lua_Integer n; if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */ lua_pushinteger(L, n); /* result is integer */ else lua_pushnumber(L, d); /* result is float */ } static int math_floor (lua_State *L) { if (lua_isinteger(L, 1)) lua_settop(L, 1); /* integer is its own floor */ else { lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1)); pushnumint(L, d); } return 1; } static int math_ceil (lua_State *L) { if (lua_isinteger(L, 1)) lua_settop(L, 1); /* integer is its own ceiling */ else { lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1)); pushnumint(L, d); } return 1; } static int math_fmod (lua_State *L) { if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) { lua_Integer d = lua_tointeger(L, 2); if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */ luaL_argcheck(L, d != 0, 2, "zero"); lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */ } else lua_pushinteger(L, lua_tointeger(L, 1) % d); } else lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); return 1; } /* ** next function does not use 'modf', avoiding problems with 'double*' ** (which is not compatible with 'float*') when lua_Number is not ** 'double'. */ static int math_modf (lua_State *L) { if (lua_isinteger(L ,1)) { lua_settop(L, 1); /* number is its own integer part */ lua_pushnumber(L, 0); /* no fractional part */ } else { lua_Number n = luaL_checknumber(L, 1); /* integer part (rounds toward zero) */ lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n); pushnumint(L, ip); /* fractional part (test needed for inf/-inf) */ lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip)); } return 2; } static int math_sqrt (lua_State *L) { lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1))); return 1; } static int math_ult (lua_State *L) { lua_Integer a = luaL_checkinteger(L, 1); lua_Integer b = luaL_checkinteger(L, 2); lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b); return 1; } static int math_log (lua_State *L) { lua_Number x = luaL_checknumber(L, 1); lua_Number res; if (lua_isnoneornil(L, 2)) res = l_mathop(log)(x); else { lua_Number base = luaL_checknumber(L, 2); #if !defined(LUA_USE_C89) if (base == l_mathop(2.0)) res = l_mathop(log2)(x); else #endif if (base == l_mathop(10.0)) res = l_mathop(log10)(x); else res = l_mathop(log)(x)/l_mathop(log)(base); } lua_pushnumber(L, res); return 1; } static int math_exp (lua_State *L) { lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); return 1; } static int math_deg (lua_State *L) { lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI)); return 1; } static int math_rad (lua_State *L) { lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0))); return 1; } static int math_frexp (lua_State *L) { lua_Number x = luaL_checknumber(L, 1); int ep; lua_pushnumber(L, l_mathop(frexp)(x, &ep)); lua_pushinteger(L, ep); return 2; } static int math_ldexp (lua_State *L) { lua_Number x = luaL_checknumber(L, 1); int ep = (int)luaL_checkinteger(L, 2); lua_pushnumber(L, l_mathop(ldexp)(x, ep)); return 1; } static int math_min (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ int imin = 1; /* index of current minimum value */ int i; luaL_argcheck(L, n >= 1, 1, "value expected"); for (i = 2; i <= n; i++) { if (lua_compare(L, i, imin, LUA_OPLT)) imin = i; } lua_pushvalue(L, imin); return 1; } static int math_max (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ int imax = 1; /* index of current maximum value */ int i; luaL_argcheck(L, n >= 1, 1, "value expected"); for (i = 2; i <= n; i++) { if (lua_compare(L, imax, i, LUA_OPLT)) imax = i; } lua_pushvalue(L, imax); return 1; } static int math_type (lua_State *L) { if (lua_type(L, 1) == LUA_TNUMBER) lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float"); else { luaL_checkany(L, 1); luaL_pushfail(L); } return 1; } /* ** {================================================================== ** Pseudo-Random Number Generator based on 'xoshiro256**'. ** =================================================================== */ /* ** This code uses lots of shifts. ISO C does not allow shifts greater ** than or equal to the width of the type being shifted, so some shifts ** are written in convoluted ways to match that restriction. For ** preprocessor tests, it assumes a width of 32 bits, so the maximum ** shift there is 31 bits. */ /* number of binary digits in the mantissa of a float */ #define FIGS l_floatatt(MANT_DIG) #if FIGS > 64 /* there are only 64 random bits; use them all */ #undef FIGS #define FIGS 64 #endif /* ** LUA_RAND32 forces the use of 32-bit integers in the implementation ** of the PRN generator (mainly for testing). */ #if !defined(LUA_RAND32) && !defined(Rand64) /* try to find an integer type with at least 64 bits */ #if ((ULONG_MAX >> 31) >> 31) >= 3 /* 'long' has at least 64 bits */ #define Rand64 unsigned long #define SRand64 long #elif !defined(LUA_USE_C89) && defined(LLONG_MAX) /* there is a 'long long' type (which must have at least 64 bits) */ #define Rand64 unsigned long long #define SRand64 long long #elif ((LUA_MAXUNSIGNED >> 31) >> 31) >= 3 /* 'lua_Unsigned' has at least 64 bits */ #define Rand64 lua_Unsigned #define SRand64 lua_Integer #endif #endif #if defined(Rand64) /* { */ /* ** Standard implementation, using 64-bit integers. ** If 'Rand64' has more than 64 bits, the extra bits do not interfere ** with the 64 initial bits, except in a right shift. Moreover, the ** final result has to discard the extra bits. */ /* avoid using extra bits when needed */ #define trim64(x) ((x) & 0xffffffffffffffffu) /* rotate left 'x' by 'n' bits */ static Rand64 rotl (Rand64 x, int n) { return (x << n) | (trim64(x) >> (64 - n)); } static Rand64 nextrand (Rand64 *state) { Rand64 state0 = state[0]; Rand64 state1 = state[1]; Rand64 state2 = state[2] ^ state0; Rand64 state3 = state[3] ^ state1; Rand64 res = rotl(state1 * 5, 7) * 9; state[0] = state0 ^ state3; state[1] = state1 ^ state2; state[2] = state2 ^ (state1 << 17); state[3] = rotl(state3, 45); return res; } /* ** Convert bits from a random integer into a float in the ** interval [0,1), getting the higher FIG bits from the ** random unsigned integer and converting that to a float. ** Some old Microsoft compilers cannot cast an unsigned long ** to a floating-point number, so we use a signed long as an ** intermediary. When lua_Number is float or double, the shift ensures ** that 'sx' is non negative; in that case, a good compiler will remove ** the correction. */ /* must throw out the extra (64 - FIGS) bits */ #define shift64_FIG (64 - FIGS) /* 2^(-FIGS) == 2^-1 / 2^(FIGS-1) */ #define scaleFIG (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1))) static lua_Number I2d (Rand64 x) { SRand64 sx = (SRand64)(trim64(x) >> shift64_FIG); lua_Number res = (lua_Number)(sx) * scaleFIG; if (sx < 0) res += l_mathop(1.0); /* correct the two's complement if negative */ lua_assert(0 <= res && res < 1); return res; } /* convert a 'Rand64' to a 'lua_Unsigned' */ #define I2UInt(x) ((lua_Unsigned)trim64(x)) /* convert a 'lua_Unsigned' to a 'Rand64' */ #define Int2I(x) ((Rand64)(x)) #else /* no 'Rand64' }{ */ /* ** Use two 32-bit integers to represent a 64-bit quantity. */ typedef struct Rand64 { l_uint32 h; /* higher half */ l_uint32 l; /* lower half */ } Rand64; /* ** If 'l_uint32' has more than 32 bits, the extra bits do not interfere ** with the 32 initial bits, except in a right shift and comparisons. ** Moreover, the final result has to discard the extra bits. */ /* avoid using extra bits when needed */ #define trim32(x) ((x) & 0xffffffffu) /* ** basic operations on 'Rand64' values */ /* build a new Rand64 value */ static Rand64 packI (l_uint32 h, l_uint32 l) { Rand64 result; result.h = h; result.l = l; return result; } /* return i << n */ static Rand64 Ishl (Rand64 i, int n) { lua_assert(n > 0 && n < 32); return packI((i.h << n) | (trim32(i.l) >> (32 - n)), i.l << n); } /* i1 ^= i2 */ static void Ixor (Rand64 *i1, Rand64 i2) { i1->h ^= i2.h; i1->l ^= i2.l; } /* return i1 + i2 */ static Rand64 Iadd (Rand64 i1, Rand64 i2) { Rand64 result = packI(i1.h + i2.h, i1.l + i2.l); if (trim32(result.l) < trim32(i1.l)) /* carry? */ result.h++; return result; } /* return i * 5 */ static Rand64 times5 (Rand64 i) { return Iadd(Ishl(i, 2), i); /* i * 5 == (i << 2) + i */ } /* return i * 9 */ static Rand64 times9 (Rand64 i) { return Iadd(Ishl(i, 3), i); /* i * 9 == (i << 3) + i */ } /* return 'i' rotated left 'n' bits */ static Rand64 rotl (Rand64 i, int n) { lua_assert(n > 0 && n < 32); return packI((i.h << n) | (trim32(i.l) >> (32 - n)), (trim32(i.h) >> (32 - n)) | (i.l << n)); } /* for offsets larger than 32, rotate right by 64 - offset */ static Rand64 rotl1 (Rand64 i, int n) { lua_assert(n > 32 && n < 64); n = 64 - n; return packI((trim32(i.h) >> n) | (i.l << (32 - n)), (i.h << (32 - n)) | (trim32(i.l) >> n)); } /* ** implementation of 'xoshiro256**' algorithm on 'Rand64' values */ static Rand64 nextrand (Rand64 *state) { Rand64 res = times9(rotl(times5(state[1]), 7)); Rand64 t = Ishl(state[1], 17); Ixor(&state[2], state[0]); Ixor(&state[3], state[1]); Ixor(&state[1], state[2]); Ixor(&state[0], state[3]); Ixor(&state[2], t); state[3] = rotl1(state[3], 45); return res; } /* ** Converts a 'Rand64' into a float. */ /* an unsigned 1 with proper type */ #define UONE ((l_uint32)1) #if FIGS <= 32 /* 2^(-FIGS) */ #define scaleFIG (l_mathop(0.5) / (UONE << (FIGS - 1))) /* ** get up to 32 bits from higher half, shifting right to ** throw out the extra bits. */ static lua_Number I2d (Rand64 x) { lua_Number h = (lua_Number)(trim32(x.h) >> (32 - FIGS)); return h * scaleFIG; } #else /* 32 < FIGS <= 64 */ /* 2^(-FIGS) = 1.0 / 2^30 / 2^3 / 2^(FIGS-33) */ #define scaleFIG \ (l_mathop(1.0) / (UONE << 30) / l_mathop(8.0) / (UONE << (FIGS - 33))) /* ** use FIGS - 32 bits from lower half, throwing out the other ** (32 - (FIGS - 32)) = (64 - FIGS) bits */ #define shiftLOW (64 - FIGS) /* ** higher 32 bits go after those (FIGS - 32) bits: shiftHI = 2^(FIGS - 32) */ #define shiftHI ((lua_Number)(UONE << (FIGS - 33)) * l_mathop(2.0)) static lua_Number I2d (Rand64 x) { lua_Number h = (lua_Number)trim32(x.h) * shiftHI; lua_Number l = (lua_Number)(trim32(x.l) >> shiftLOW); return (h + l) * scaleFIG; } #endif /* convert a 'Rand64' to a 'lua_Unsigned' */ static lua_Unsigned I2UInt (Rand64 x) { return (((lua_Unsigned)trim32(x.h) << 31) << 1) | (lua_Unsigned)trim32(x.l); } /* convert a 'lua_Unsigned' to a 'Rand64' */ static Rand64 Int2I (lua_Unsigned n) { return packI((l_uint32)((n >> 31) >> 1), (l_uint32)n); } #endif /* } */ /* ** A state uses four 'Rand64' values. */ typedef struct { Rand64 s[4]; } RanState; /* ** Project the random integer 'ran' into the interval [0, n]. ** Because 'ran' has 2^B possible values, the projection can only be ** uniform when the size of the interval is a power of 2 (exact ** division). So, to get a uniform projection into [0, n], we ** first compute 'lim', the smallest Mersenne number not smaller than ** 'n'. We then project 'ran' into the interval [0, lim]. If the result ** is inside [0, n], we are done. Otherwise, we try with another 'ran', ** until we have a result inside the interval. */ static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, RanState *state) { lua_Unsigned lim = n; /* to compute the Mersenne number */ int sh; /* how much to spread bits to the right in 'lim' */ /* spread '1' bits in 'lim' until it becomes a Mersenne number */ for (sh = 1; (lim & (lim + 1)) != 0; sh *= 2) lim |= (lim >> sh); /* spread '1's to the right */ while ((ran &= lim) > n) /* project 'ran' into [0..lim] and test */ ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */ return ran; } static int math_random (lua_State *L) { lua_Integer low, up; lua_Unsigned p; RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); Rand64 rv = nextrand(state->s); /* next pseudo-random value */ switch (lua_gettop(L)) { /* check number of arguments */ case 0: { /* no arguments */ lua_pushnumber(L, I2d(rv)); /* float between 0 and 1 */ return 1; } case 1: { /* only upper limit */ low = 1; up = luaL_checkinteger(L, 1); if (up == 0) { /* single 0 as argument? */ lua_pushinteger(L, l_castU2S(I2UInt(rv))); /* full random integer */ return 1; } break; } case 2: { /* lower and upper limits */ low = luaL_checkinteger(L, 1); up = luaL_checkinteger(L, 2); break; } default: return luaL_error(L, "wrong number of arguments"); } /* random integer in the interval [low, up] */ luaL_argcheck(L, low <= up, 1, "interval is empty"); /* project random integer into the interval [0, up - low] */ p = project(I2UInt(rv), l_castS2U(up) - l_castS2U(low), state); lua_pushinteger(L, l_castU2S(p + l_castS2U(low))); return 1; } static void setseed (lua_State *L, Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) { int i; state[0] = Int2I(n1); state[1] = Int2I(0xff); /* avoid a zero state */ state[2] = Int2I(n2); state[3] = Int2I(0); for (i = 0; i < 16; i++) nextrand(state); /* discard initial values to "spread" seed */ lua_pushinteger(L, l_castU2S(n1)); lua_pushinteger(L, l_castU2S(n2)); } static int math_randomseed (lua_State *L) { RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); lua_Unsigned n1, n2; if (lua_isnone(L, 1)) { n1 = luaL_makeseed(L); /* "random" seed */ n2 = I2UInt(nextrand(state->s)); /* in case seed is not that random... */ } else { n1 = l_castS2U(luaL_checkinteger(L, 1)); n2 = l_castS2U(luaL_optinteger(L, 2, 0)); } setseed(L, state->s, n1, n2); return 2; /* return seeds */ } static const luaL_Reg randfuncs[] = { {"random", math_random}, {"randomseed", math_randomseed}, {NULL, NULL} }; /* ** Register the random functions and initialize their state. */ static void setrandfunc (lua_State *L) { RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); setseed(L, state->s, luaL_makeseed(L), 0); /* initialize with random seed */ lua_pop(L, 2); /* remove pushed seeds */ luaL_setfuncs(L, randfuncs, 1); } /* }================================================================== */ /* ** {================================================================== ** Deprecated functions (for compatibility only) ** =================================================================== */ #if defined(LUA_COMPAT_MATHLIB) static int math_cosh (lua_State *L) { lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1))); return 1; } static int math_sinh (lua_State *L) { lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1))); return 1; } static int math_tanh (lua_State *L) { lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); return 1; } static int math_pow (lua_State *L) { lua_Number x = luaL_checknumber(L, 1); lua_Number y = luaL_checknumber(L, 2); lua_pushnumber(L, l_mathop(pow)(x, y)); return 1; } static int math_log10 (lua_State *L) { lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); return 1; } #endif /* }================================================================== */ static const luaL_Reg mathlib[] = { {"abs", math_abs}, {"acos", math_acos}, {"asin", math_asin}, {"atan", math_atan}, {"ceil", math_ceil}, {"cos", math_cos}, {"deg", math_deg}, {"exp", math_exp}, {"tointeger", math_toint}, {"floor", math_floor}, {"fmod", math_fmod}, {"frexp", math_frexp}, {"ult", math_ult}, {"ldexp", math_ldexp}, {"log", math_log}, {"max", math_max}, {"min", math_min}, {"modf", math_modf}, {"rad", math_rad}, {"sin", math_sin}, {"sqrt", math_sqrt}, {"tan", math_tan}, {"type", math_type}, #if defined(LUA_COMPAT_MATHLIB) {"atan2", math_atan}, {"cosh", math_cosh}, {"sinh", math_sinh}, {"tanh", math_tanh}, {"pow", math_pow}, {"log10", math_log10}, #endif /* placeholders */ {"random", NULL}, {"randomseed", NULL}, {"pi", NULL}, {"huge", NULL}, {"maxinteger", NULL}, {"mininteger", NULL}, {NULL, NULL} }; /* ** Open math library */ LUAMOD_API int luaopen_math (lua_State *L) { luaL_newlib(L, mathlib); lua_pushnumber(L, PI); lua_setfield(L, -2, "pi"); lua_pushnumber(L, (lua_Number)HUGE_VAL); lua_setfield(L, -2, "huge"); lua_pushinteger(L, LUA_MAXINTEGER); lua_setfield(L, -2, "maxinteger"); lua_pushinteger(L, LUA_MININTEGER); lua_setfield(L, -2, "mininteger"); setrandfunc(L); return 1; } ================================================ FILE: 3rd/lua/lmem.c ================================================ /* ** $Id: lmem.c $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ #define lmem_c #define LUA_CORE #include "lprefix.h" #include #include "lua.h" #include "ldebug.h" #include "ldo.h" #include "lgc.h" #include "lmem.h" #include "lobject.h" #include "lstate.h" /* ** About the realloc function: ** void *frealloc (void *ud, void *ptr, size_t osize, size_t nsize); ** ('osize' is the old size, 'nsize' is the new size) ** ** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL. ** Particularly, frealloc(ud, NULL, 0, 0) does nothing, ** which is equivalent to free(NULL) in ISO C. ** ** - frealloc(ud, NULL, x, s) creates a new block of size 's' ** (no matter 'x'). Returns NULL if it cannot create the new block. ** ** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from ** size 'x' to size 'y'. Returns NULL if it cannot reallocate the ** block to the new size. */ /* ** Macro to call the allocation function. */ #define callfrealloc(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns)) /* ** When an allocation fails, it will try again after an emergency ** collection, except when it cannot run a collection. The GC should ** not be called while the state is not fully built, as the collector ** is not yet fully initialized. Also, it should not be called when ** 'gcstopem' is true, because then the interpreter is in the middle of ** a collection step. */ #define cantryagain(g) (completestate(g) && !g->gcstopem) #if defined(EMERGENCYGCTESTS) /* ** First allocation will fail except when freeing a block (frees never ** fail) and when it cannot try again; this fail will trigger 'tryagain' ** and a full GC cycle at every allocation. */ static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { if (ns > 0 && cantryagain(g)) return NULL; /* fail */ else /* normal allocation */ return callfrealloc(g, block, os, ns); } #else #define firsttry(g,block,os,ns) callfrealloc(g, block, os, ns) #endif /* ** {================================================================== ** Functions to allocate/deallocate arrays for the Parser ** =================================================================== */ /* ** Minimum size for arrays during parsing, to avoid overhead of ** reallocating to size 1, then 2, and then 4. All these arrays ** will be reallocated to exact sizes or erased when parsing ends. */ #define MINSIZEARRAY 4 void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, unsigned size_elems, int limit, const char *what) { void *newblock; int size = *psize; if (nelems + 1 <= size) /* does one extra element still fit? */ return block; /* nothing to be done */ if (size >= limit / 2) { /* cannot double it? */ if (l_unlikely(size >= limit)) /* cannot grow even a little? */ luaG_runerror(L, "too many %s (limit is %d)", what, limit); size = limit; /* still have at least one free place */ } else { size *= 2; if (size < MINSIZEARRAY) size = MINSIZEARRAY; /* minimum size */ } lua_assert(nelems + 1 <= size && size <= limit); /* 'limit' ensures that multiplication will not overflow */ newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems, cast_sizet(size) * size_elems); *psize = size; /* update only when everything else is OK */ return newblock; } /* ** In prototypes, the size of the array is also its number of ** elements (to save memory). So, if it cannot shrink an array ** to its number of elements, the only option is to raise an ** error. */ void *luaM_shrinkvector_ (lua_State *L, void *block, int *size, int final_n, unsigned size_elem) { void *newblock; size_t oldsize = cast_sizet(*size) * size_elem; size_t newsize = cast_sizet(final_n) * size_elem; lua_assert(newsize <= oldsize); newblock = luaM_saferealloc_(L, block, oldsize, newsize); *size = final_n; return newblock; } /* }================================================================== */ l_noret luaM_toobig (lua_State *L) { luaG_runerror(L, "memory allocation error: block too big"); } /* ** Free memory */ void luaM_free_ (lua_State *L, void *block, size_t osize) { global_State *g = G(L); lua_assert((osize == 0) == (block == NULL)); callfrealloc(g, block, osize, 0); g->GCdebt += cast(l_mem, osize); } /* ** In case of allocation fail, this function will do an emergency ** collection to free some memory and then try the allocation again. */ static void *tryagain (lua_State *L, void *block, size_t osize, size_t nsize) { global_State *g = G(L); if (cantryagain(g)) { luaC_fullgc(L, 1); /* try to free some memory... */ return callfrealloc(g, block, osize, nsize); /* try again */ } else return NULL; /* cannot run an emergency collection */ } /* ** Generic allocation routine. */ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *newblock; global_State *g = G(L); lua_assert((osize == 0) == (block == NULL)); newblock = firsttry(g, block, osize, nsize); if (l_unlikely(newblock == NULL && nsize > 0)) { newblock = tryagain(L, block, osize, nsize); if (newblock == NULL) /* still no memory? */ return NULL; /* do not update 'GCdebt' */ } lua_assert((nsize == 0) == (newblock == NULL)); g->GCdebt -= cast(l_mem, nsize) - cast(l_mem, osize); return newblock; } void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *newblock = luaM_realloc_(L, block, osize, nsize); if (l_unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */ luaM_error(L); return newblock; } void *luaM_malloc_ (lua_State *L, size_t size, int tag) { if (size == 0) return NULL; /* that's all */ else { global_State *g = G(L); void *newblock = firsttry(g, NULL, cast_sizet(tag), size); if (l_unlikely(newblock == NULL)) { newblock = tryagain(L, NULL, cast_sizet(tag), size); if (newblock == NULL) luaM_error(L); } g->GCdebt -= cast(l_mem, size); return newblock; } } ================================================ FILE: 3rd/lua/lmem.h ================================================ /* ** $Id: lmem.h $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ #ifndef lmem_h #define lmem_h #include #include "llimits.h" #include "lua.h" #define luaM_error(L) luaD_throw(L, LUA_ERRMEM) /* ** This macro tests whether it is safe to multiply 'n' by the size of ** type 't' without overflows. Because 'e' is always constant, it avoids ** the runtime division MAX_SIZET/(e). ** (The macro is somewhat complex to avoid warnings: The 'sizeof' ** comparison avoids a runtime comparison when overflow cannot occur. ** The compiler should be able to optimize the real test by itself, but ** when it does it, it may give a warning about "comparison is always ** false due to limited range of data type"; the +1 tricks the compiler, ** avoiding this warning but also this optimization.) */ #define luaM_testsize(n,e) \ (sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e)) #define luaM_checksize(L,n,e) \ (luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0)) /* ** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that ** the result is not larger than 'n' and cannot overflow a 'size_t' ** when multiplied by the size of type 't'. (Assumes that 'n' is an ** 'int' and that 'int' is not larger than 'size_t'.) */ #define luaM_limitN(n,t) \ ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \ cast_int((MAX_SIZET/sizeof(t)))) /* ** Arrays of chars do not need any test */ #define luaM_reallocvchar(L,b,on,n) \ cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) #define luaM_freemem(L, b, s) luaM_free_(L, (b), (s)) #define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b))) #define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) #define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0)) #define luaM_newvector(L,n,t) \ cast(t*, luaM_malloc_(L, cast_sizet(n)*sizeof(t), 0)) #define luaM_newvectorchecked(L,n,t) \ (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) #define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag) #define luaM_newblock(L, size) luaM_newvector(L, size, char) #define luaM_growvector(L,v,nelems,size,t,limit,e) \ ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \ luaM_limitN(limit,t),e))) #define luaM_reallocvector(L, v,oldn,n,t) \ (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \ cast_sizet(n) * sizeof(t)))) #define luaM_shrinkvector(L,v,size,fs,t) \ ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t)))) LUAI_FUNC l_noret luaM_toobig (lua_State *L); /* not to be called directly */ LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, size_t size); LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize, size_t size); LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *size, unsigned size_elem, int limit, const char *what); LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, int final_n, unsigned size_elem); LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag); #endif ================================================ FILE: 3rd/lua/loadlib.c ================================================ /* ** $Id: loadlib.c $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** ** This module contains an implementation of loadlib for Unix systems ** that have dlfcn, an implementation for Windows, and a stub for other ** systems. */ #define loadlib_c #define LUA_LIB #include "lprefix.h" #include #include #include #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "llimits.h" /* ** LUA_CSUBSEP is the character that replaces dots in submodule names ** when searching for a C loader. ** LUA_LSUBSEP is the character that replaces dots in submodule names ** when searching for a Lua loader. */ #if !defined(LUA_CSUBSEP) #define LUA_CSUBSEP LUA_DIRSEP #endif #if !defined(LUA_LSUBSEP) #define LUA_LSUBSEP LUA_DIRSEP #endif /* prefix for open functions in C libraries */ #define LUA_POF "luaopen_" /* separator for open functions in C libraries */ #define LUA_OFSEP "_" /* ** key for table in the registry that keeps handles ** for all loaded C libraries */ static const char *const CLIBS = "_CLIBS"; #define LIB_FAIL "open" #define setprogdir(L) ((void)0) /* cast void* to a Lua function */ #define cast_Lfunc(p) cast(lua_CFunction, cast_func(p)) /* ** system-dependent functions */ /* ** unload library 'lib' */ static void lsys_unloadlib (void *lib); /* ** load C library in file 'path'. If 'seeglb', load with all names in ** the library global. ** Returns the library; in case of error, returns NULL plus an ** error string in the stack. */ static void *lsys_load (lua_State *L, const char *path, int seeglb); /* ** Try to find a function named 'sym' in library 'lib'. ** Returns the function; in case of error, returns NULL plus an ** error string in the stack. */ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym); #if defined(LUA_USE_DLOPEN) /* { */ /* ** {======================================================================== ** This is an implementation of loadlib based on the dlfcn interface, ** which is available in all POSIX systems. ** ========================================================================= */ #include static void lsys_unloadlib (void *lib) { dlclose(lib); } static void *lsys_load (lua_State *L, const char *path, int seeglb) { void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL)); if (l_unlikely(lib == NULL)) lua_pushstring(L, dlerror()); return lib; } static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { lua_CFunction f = cast_Lfunc(dlsym(lib, sym)); if (l_unlikely(f == NULL)) lua_pushstring(L, dlerror()); return f; } /* }====================================================== */ #elif defined(LUA_DL_DLL) /* }{ */ /* ** {====================================================================== ** This is an implementation of loadlib for Windows using native functions. ** ======================================================================= */ #include /* ** optional flags for LoadLibraryEx */ #if !defined(LUA_LLE_FLAGS) #define LUA_LLE_FLAGS 0 #endif #undef setprogdir /* ** Replace in the path (on the top of the stack) any occurrence ** of LUA_EXEC_DIR with the executable's path. */ static void setprogdir (lua_State *L) { char buff[MAX_PATH + 1]; char *lb; DWORD nsize = sizeof(buff)/sizeof(char); DWORD n = GetModuleFileNameA(NULL, buff, nsize); /* get exec. name */ if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) luaL_error(L, "unable to get ModuleFileName"); else { *lb = '\0'; /* cut name on the last '\\' to get the path */ luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); lua_remove(L, -2); /* remove original string */ } } static void pusherror (lua_State *L) { int error = GetLastError(); char buffer[128]; if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, buffer, sizeof(buffer)/sizeof(char), NULL)) lua_pushstring(L, buffer); else lua_pushfstring(L, "system error %d\n", error); } static void lsys_unloadlib (void *lib) { FreeLibrary((HMODULE)lib); } static void *lsys_load (lua_State *L, const char *path, int seeglb) { HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS); (void)(seeglb); /* not used: symbols are 'global' by default */ if (lib == NULL) pusherror(L); return lib; } static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { lua_CFunction f = cast_Lfunc(GetProcAddress((HMODULE)lib, sym)); if (f == NULL) pusherror(L); return f; } /* }====================================================== */ #else /* }{ */ /* ** {====================================================== ** Fallback for other systems ** ======================================================= */ #undef LIB_FAIL #define LIB_FAIL "absent" #define DLMSG "dynamic libraries not enabled; check your Lua installation" static void lsys_unloadlib (void *lib) { (void)(lib); /* not used */ } static void *lsys_load (lua_State *L, const char *path, int seeglb) { (void)(path); (void)(seeglb); /* not used */ lua_pushliteral(L, DLMSG); return NULL; } static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { (void)(lib); (void)(sym); /* not used */ lua_pushliteral(L, DLMSG); return NULL; } /* }====================================================== */ #endif /* } */ /* ** {================================================================== ** Set Paths ** =================================================================== */ /* ** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment ** variables that Lua check to set its paths. */ #if !defined(LUA_PATH_VAR) #define LUA_PATH_VAR "LUA_PATH" #endif #if !defined(LUA_CPATH_VAR) #define LUA_CPATH_VAR "LUA_CPATH" #endif /* ** return registry.LUA_NOENV as a boolean */ static int noenv (lua_State *L) { int b; lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); b = lua_toboolean(L, -1); lua_pop(L, 1); /* remove value */ return b; } /* ** Set a path. (If using the default path, assume it is a string ** literal in C and create it as an external string.) */ static void setpath (lua_State *L, const char *fieldname, const char *envname, const char *dft) { const char *dftmark; const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX); const char *path = getenv(nver); /* try versioned name */ if (path == NULL) /* no versioned environment variable? */ path = getenv(envname); /* try unversioned name */ if (path == NULL || noenv(L)) /* no environment variable? */ lua_pushexternalstring(L, dft, strlen(dft), NULL, NULL); /* use default */ else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL) lua_pushstring(L, path); /* nothing to change */ else { /* path contains a ";;": insert default path in its place */ size_t len = strlen(path); luaL_Buffer b; luaL_buffinit(L, &b); if (path < dftmark) { /* is there a prefix before ';;'? */ luaL_addlstring(&b, path, ct_diff2sz(dftmark - path)); /* add it */ luaL_addchar(&b, *LUA_PATH_SEP); } luaL_addstring(&b, dft); /* add default */ if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */ luaL_addchar(&b, *LUA_PATH_SEP); luaL_addlstring(&b, dftmark + 2, ct_diff2sz((path + len - 2) - dftmark)); } luaL_pushresult(&b); } setprogdir(L); lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */ lua_pop(L, 1); /* pop versioned variable name ('nver') */ } /* }================================================================== */ /* ** External strings created by DLLs may need the DLL code to be ** deallocated. This implies that a DLL can only be unloaded after all ** its strings were deallocated. To ensure that, we create a 'library ** string' to represent each DLL, and when this string is deallocated ** it closes its corresponding DLL. ** (The string itself is irrelevant; its userdata is the DLL pointer.) */ /* ** return registry.CLIBS[path] */ static void *checkclib (lua_State *L, const char *path) { void *plib; lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); lua_getfield(L, -1, path); plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ lua_pop(L, 2); /* pop CLIBS table and 'plib' */ return plib; } /* ** Deallocate function for library strings. ** Unload the DLL associated with the string being deallocated. */ static void *freelib (void *ud, void *ptr, size_t osize, size_t nsize) { /* string itself is irrelevant and static */ (void)ptr; (void)osize; (void)nsize; lsys_unloadlib(ud); /* unload library represented by the string */ return NULL; } /* ** Create a library string that, when deallocated, will unload 'plib' */ static void createlibstr (lua_State *L, void *plib) { /* common content for all library strings */ static const char dummy[] = "01234567890"; lua_pushexternalstring(L, dummy, sizeof(dummy) - 1, freelib, plib); } /* ** registry.CLIBS[path] = plib -- for queries. ** Also create a reference to strlib, so that the library string will ** only be collected when registry.CLIBS is collected. */ static void addtoclib (lua_State *L, const char *path, void *plib) { lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); lua_pushlightuserdata(L, plib); lua_setfield(L, -2, path); /* CLIBS[path] = plib */ createlibstr(L, plib); luaL_ref(L, -2); /* keep library string in CLIBS */ lua_pop(L, 1); /* pop CLIBS table */ } /* error codes for 'lookforfunc' */ #define ERRLIB 1 #define ERRFUNC 2 /* ** Look for a C function named 'sym' in a dynamically loaded library ** 'path'. ** First, check whether the library is already loaded; if not, try ** to load it. ** Then, if 'sym' is '*', return true (as library has been loaded). ** Otherwise, look for symbol 'sym' in the library and push a ** C function with that symbol. ** Return 0 with 'true' or a function in the stack; in case of ** errors, return an error code with an error message in the stack. */ static int lookforfunc (lua_State *L, const char *path, const char *sym) { void *reg = checkclib(L, path); /* check loaded C libraries */ if (reg == NULL) { /* must load library? */ reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */ if (reg == NULL) return ERRLIB; /* unable to load library */ addtoclib(L, path, reg); } if (*sym == '*') { /* loading only library (no function)? */ lua_pushboolean(L, 1); /* return 'true' */ return 0; /* no errors */ } else { lua_CFunction f = lsys_sym(L, reg, sym); if (f == NULL) return ERRFUNC; /* unable to find function */ lua_pushcfunction(L, f); /* else create new function */ return 0; /* no errors */ } } static int ll_loadlib (lua_State *L) { const char *path = luaL_checkstring(L, 1); const char *init = luaL_checkstring(L, 2); int stat = lookforfunc(L, path, init); if (l_likely(stat == 0)) /* no errors? */ return 1; /* return the loaded function */ else { /* error; error message is on stack top */ luaL_pushfail(L); lua_insert(L, -2); lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); return 3; /* return fail, error message, and where */ } } /* ** {====================================================== ** 'require' function ** ======================================================= */ static int readable (const char *filename) { FILE *f = fopen(filename, "r"); /* try to open file */ if (f == NULL) return 0; /* open failed */ fclose(f); return 1; } /* ** Get the next name in '*path' = 'name1;name2;name3;...', changing ** the ending ';' to '\0' to create a zero-terminated string. Return ** NULL when list ends. */ static const char *getnextfilename (char **path, char *end) { char *sep; char *name = *path; if (name == end) return NULL; /* no more names */ else if (*name == '\0') { /* from previous iteration? */ *name = *LUA_PATH_SEP; /* restore separator */ name++; /* skip it */ } sep = strchr(name, *LUA_PATH_SEP); /* find next separator */ if (sep == NULL) /* separator not found? */ sep = end; /* name goes until the end */ *sep = '\0'; /* finish file name */ *path = sep; /* will start next search from here */ return name; } /* ** Given a path such as ";blabla.so;blublu.so", pushes the string ** ** no file 'blabla.so' ** no file 'blublu.so' */ static void pusherrornotfound (lua_State *L, const char *path) { luaL_Buffer b; luaL_buffinit(L, &b); luaL_addstring(&b, "no file '"); luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '"); luaL_addstring(&b, "'"); luaL_pushresult(&b); } static const char *searchpath (lua_State *L, const char *name, const char *path, const char *sep, const char *dirsep) { luaL_Buffer buff; char *pathname; /* path with name inserted */ char *endpathname; /* its end */ const char *filename; /* separator is non-empty and appears in 'name'? */ if (*sep != '\0' && strchr(name, *sep) != NULL) name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ luaL_buffinit(L, &buff); /* add path to the buffer, replacing marks ('?') with the file name */ luaL_addgsub(&buff, path, LUA_PATH_MARK, name); luaL_addchar(&buff, '\0'); pathname = luaL_buffaddr(&buff); /* writable list of file names */ endpathname = pathname + luaL_bufflen(&buff) - 1; while ((filename = getnextfilename(&pathname, endpathname)) != NULL) { if (readable(filename)) /* does file exist and is readable? */ return lua_pushstring(L, filename); /* save and return name */ } luaL_pushresult(&buff); /* push path to create error message */ pusherrornotfound(L, lua_tostring(L, -1)); /* create error message */ return NULL; /* not found */ } static int ll_searchpath (lua_State *L) { const char *f = searchpath(L, luaL_checkstring(L, 1), luaL_checkstring(L, 2), luaL_optstring(L, 3, "."), luaL_optstring(L, 4, LUA_DIRSEP)); if (f != NULL) return 1; else { /* error message is on top of the stack */ luaL_pushfail(L); lua_insert(L, -2); return 2; /* return fail + error message */ } } static const char *findfile (lua_State *L, const char *name, const char *pname, const char *dirsep) { const char *path; lua_getfield(L, lua_upvalueindex(1), pname); path = lua_tostring(L, -1); if (l_unlikely(path == NULL)) luaL_error(L, "'package.%s' must be a string", pname); return searchpath(L, name, path, ".", dirsep); } static int checkload (lua_State *L, int stat, const char *filename) { if (l_likely(stat)) { /* module loaded successfully? */ lua_pushstring(L, filename); /* will be 2nd argument to module */ return 2; /* return open function and file name */ } else return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s", lua_tostring(L, 1), filename, lua_tostring(L, -1)); } static int searcher_Lua (lua_State *L) { const char *filename; const char *name = luaL_checkstring(L, 1); filename = findfile(L, name, "path", LUA_LSUBSEP); if (filename == NULL) return 1; /* module not found in this path */ return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename); } /* ** Try to find a load function for module 'modname' at file 'filename'. ** First, change '.' to '_' in 'modname'; then, if 'modname' has ** the form X-Y (that is, it has an "ignore mark"), build a function ** name "luaopen_X" and look for it. (For compatibility, if that ** fails, it also tries "luaopen_Y".) If there is no ignore mark, ** look for a function named "luaopen_modname". */ static int loadfunc (lua_State *L, const char *filename, const char *modname) { const char *openfunc; const char *mark; modname = luaL_gsub(L, modname, ".", LUA_OFSEP); mark = strchr(modname, *LUA_IGMARK); if (mark) { int stat; openfunc = lua_pushlstring(L, modname, ct_diff2sz(mark - modname)); openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc); stat = lookforfunc(L, filename, openfunc); if (stat != ERRFUNC) return stat; modname = mark + 1; /* else go ahead and try old-style name */ } openfunc = lua_pushfstring(L, LUA_POF"%s", modname); return lookforfunc(L, filename, openfunc); } static int searcher_C (lua_State *L) { const char *name = luaL_checkstring(L, 1); const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP); if (filename == NULL) return 1; /* module not found in this path */ return checkload(L, (loadfunc(L, filename, name) == 0), filename); } static int searcher_Croot (lua_State *L) { const char *filename; const char *name = luaL_checkstring(L, 1); const char *p = strchr(name, '.'); int stat; if (p == NULL) return 0; /* is root */ lua_pushlstring(L, name, ct_diff2sz(p - name)); filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP); if (filename == NULL) return 1; /* root not found */ if ((stat = loadfunc(L, filename, name)) != 0) { if (stat != ERRFUNC) return checkload(L, 0, filename); /* real error */ else { /* open function not found */ lua_pushfstring(L, "no module '%s' in file '%s'", name, filename); return 1; } } lua_pushstring(L, filename); /* will be 2nd argument to module */ return 2; } static int searcher_preload (lua_State *L) { const char *name = luaL_checkstring(L, 1); lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */ lua_pushfstring(L, "no field package.preload['%s']", name); return 1; } else { lua_pushliteral(L, ":preload:"); return 2; } } static void findloader (lua_State *L, const char *name) { int i; luaL_Buffer msg; /* to build error message */ /* push 'package.searchers' to index 3 in the stack */ if (l_unlikely(lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE)) luaL_error(L, "'package.searchers' must be a table"); luaL_buffinit(L, &msg); luaL_addstring(&msg, "\n\t"); /* error-message prefix for first message */ /* iterate over available searchers to find a loader */ for (i = 1; ; i++) { if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */ lua_pop(L, 1); /* remove nil */ luaL_buffsub(&msg, 2); /* remove last prefix */ luaL_pushresult(&msg); /* create error message */ luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); } lua_pushstring(L, name); lua_call(L, 1, 2); /* call it */ if (lua_isfunction(L, -2)) /* did it find a loader? */ return; /* module loader found */ else if (lua_isstring(L, -2)) { /* searcher returned error message? */ lua_pop(L, 1); /* remove extra return */ luaL_addvalue(&msg); /* concatenate error message */ luaL_addstring(&msg, "\n\t"); /* prefix for next message */ } else /* no error message */ lua_pop(L, 2); /* remove both returns */ } } static int ll_require (lua_State *L) { const char *name = luaL_checkstring(L, 1); lua_settop(L, 1); /* LOADED table will be at index 2 */ lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); lua_getfield(L, 2, name); /* LOADED[name] */ if (lua_toboolean(L, -1)) /* is it there? */ return 1; /* package is already loaded */ /* else must load package */ lua_pop(L, 1); /* remove 'getfield' result */ findloader(L, name); lua_rotate(L, -2, 1); /* function <-> loader data */ lua_pushvalue(L, 1); /* name is 1st argument to module loader */ lua_pushvalue(L, -3); /* loader data is 2nd argument */ /* stack: ...; loader data; loader function; mod. name; loader data */ lua_call(L, 2, 1); /* run loader to load module */ /* stack: ...; loader data; result from loader */ if (!lua_isnil(L, -1)) /* non-nil return? */ lua_setfield(L, 2, name); /* LOADED[name] = returned value */ else lua_pop(L, 1); /* pop nil */ if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ lua_pushboolean(L, 1); /* use true as result */ lua_copy(L, -1, -2); /* replace loader result */ lua_setfield(L, 2, name); /* LOADED[name] = true */ } lua_rotate(L, -2, 1); /* loader data <-> module result */ return 2; /* return module result and loader data */ } /* }====================================================== */ static const luaL_Reg pk_funcs[] = { {"loadlib", ll_loadlib}, {"searchpath", ll_searchpath}, /* placeholders */ {"preload", NULL}, {"cpath", NULL}, {"path", NULL}, {"searchers", NULL}, {"loaded", NULL}, {NULL, NULL} }; static const luaL_Reg ll_funcs[] = { {"require", ll_require}, {NULL, NULL} }; static void createsearcherstable (lua_State *L) { static const lua_CFunction searchers[] = { searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL }; int i; /* create 'searchers' table */ lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); /* fill it with predefined searchers */ for (i=0; searchers[i] != NULL; i++) { lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */ lua_pushcclosure(L, searchers[i], 1); lua_rawseti(L, -2, i+1); } lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */ } LUAMOD_API int luaopen_package (lua_State *L) { luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */ lua_pop(L, 1); /* will not use it now */ luaL_newlib(L, pk_funcs); /* create 'package' table */ createsearcherstable(L); /* set paths */ setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT); setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT); /* store config information */ lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" LUA_EXEC_DIR "\n" LUA_IGMARK "\n"); lua_setfield(L, -2, "config"); /* set field 'loaded' */ luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); lua_setfield(L, -2, "loaded"); /* set field 'preload' */ luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); lua_setfield(L, -2, "preload"); lua_pushglobaltable(L); lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */ luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */ lua_pop(L, 1); /* pop global table */ return 1; /* return 'package' table */ } ================================================ FILE: 3rd/lua/lobject.c ================================================ /* ** $Id: lobject.c $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ #define lobject_c #define LUA_CORE #include "lprefix.h" #include #include #include #include #include #include #include #include "lua.h" #include "lctype.h" #include "ldebug.h" #include "ldo.h" #include "lmem.h" #include "lobject.h" #include "lstate.h" #include "lstring.h" #include "lvm.h" /* ** Computes ceil(log2(x)), which is the smallest integer n such that ** x <= (1 << n). */ lu_byte luaO_ceillog2 (unsigned int x) { static const lu_byte log_2[256] = { /* log_2[i - 1] = ceil(log2(i)) */ 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 }; int l = 0; x--; while (x >= 256) { l += 8; x >>= 8; } return cast_byte(l + log_2[x]); } /* ** Encodes 'p'% as a floating-point byte, represented as (eeeexxxx). ** The exponent is represented using excess-7. Mimicking IEEE 754, the ** representation normalizes the number when possible, assuming an extra ** 1 before the mantissa (xxxx) and adding one to the exponent (eeee) ** to signal that. So, the real value is (1xxxx) * 2^(eeee - 7 - 1) if ** eeee != 0, and (xxxx) * 2^-7 otherwise (subnormal numbers). */ lu_byte luaO_codeparam (unsigned int p) { if (p >= (cast(lu_mem, 0x1F) << (0xF - 7 - 1)) * 100u) /* overflow? */ return 0xFF; /* return maximum value */ else { p = (cast(l_uint32, p) * 128 + 99) / 100; /* round up the division */ if (p < 0x10) { /* subnormal number? */ /* exponent bits are already zero; nothing else to do */ return cast_byte(p); } else { /* p >= 0x10 implies ceil(log2(p + 1)) >= 5 */ /* preserve 5 bits in 'p' */ unsigned log = luaO_ceillog2(p + 1) - 5u; return cast_byte(((p >> log) - 0x10) | ((log + 1) << 4)); } } } /* ** Computes 'p' times 'x', where 'p' is a floating-point byte. Roughly, ** we have to multiply 'x' by the mantissa and then shift accordingly to ** the exponent. If the exponent is positive, both the multiplication ** and the shift increase 'x', so we have to care only about overflows. ** For negative exponents, however, multiplying before the shift keeps ** more significant bits, as long as the multiplication does not ** overflow, so we check which order is best. */ l_mem luaO_applyparam (lu_byte p, l_mem x) { int m = p & 0xF; /* mantissa */ int e = (p >> 4); /* exponent */ if (e > 0) { /* normalized? */ e--; /* correct exponent */ m += 0x10; /* correct mantissa; maximum value is 0x1F */ } e -= 7; /* correct excess-7 */ if (e >= 0) { if (x < (MAX_LMEM / 0x1F) >> e) /* no overflow? */ return (x * m) << e; /* order doesn't matter here */ else /* real overflow */ return MAX_LMEM; } else { /* negative exponent */ e = -e; if (x < MAX_LMEM / 0x1F) /* multiplication cannot overflow? */ return (x * m) >> e; /* multiplying first gives more precision */ else if ((x >> e) < MAX_LMEM / 0x1F) /* cannot overflow after shift? */ return (x >> e) * m; else /* real overflow */ return MAX_LMEM; } } static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, lua_Integer v2) { switch (op) { case LUA_OPADD: return intop(+, v1, v2); case LUA_OPSUB:return intop(-, v1, v2); case LUA_OPMUL:return intop(*, v1, v2); case LUA_OPMOD: return luaV_mod(L, v1, v2); case LUA_OPIDIV: return luaV_idiv(L, v1, v2); case LUA_OPBAND: return intop(&, v1, v2); case LUA_OPBOR: return intop(|, v1, v2); case LUA_OPBXOR: return intop(^, v1, v2); case LUA_OPSHL: return luaV_shiftl(v1, v2); case LUA_OPSHR: return luaV_shiftr(v1, v2); case LUA_OPUNM: return intop(-, 0, v1); case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1); default: lua_assert(0); return 0; } } static lua_Number numarith (lua_State *L, int op, lua_Number v1, lua_Number v2) { switch (op) { case LUA_OPADD: return luai_numadd(L, v1, v2); case LUA_OPSUB: return luai_numsub(L, v1, v2); case LUA_OPMUL: return luai_nummul(L, v1, v2); case LUA_OPDIV: return luai_numdiv(L, v1, v2); case LUA_OPPOW: return luai_numpow(L, v1, v2); case LUA_OPIDIV: return luai_numidiv(L, v1, v2); case LUA_OPUNM: return luai_numunm(L, v1); case LUA_OPMOD: return luaV_modf(L, v1, v2); default: lua_assert(0); return 0; } } int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2, TValue *res) { switch (op) { case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* operate only on integers */ lua_Integer i1; lua_Integer i2; if (tointegerns(p1, &i1) && tointegerns(p2, &i2)) { setivalue(res, intarith(L, op, i1, i2)); return 1; } else return 0; /* fail */ } case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */ lua_Number n1; lua_Number n2; if (tonumberns(p1, n1) && tonumberns(p2, n2)) { setfltvalue(res, numarith(L, op, n1, n2)); return 1; } else return 0; /* fail */ } default: { /* other operations */ lua_Number n1; lua_Number n2; if (ttisinteger(p1) && ttisinteger(p2)) { setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); return 1; } else if (tonumberns(p1, n1) && tonumberns(p2, n2)) { setfltvalue(res, numarith(L, op, n1, n2)); return 1; } else return 0; /* fail */ } } } void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, StkId res) { if (!luaO_rawarith(L, op, p1, p2, s2v(res))) { /* could not perform raw operation; try metamethod */ luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); } } lu_byte luaO_hexavalue (int c) { lua_assert(lisxdigit(c)); if (lisdigit(c)) return cast_byte(c - '0'); else return cast_byte((ltolower(c) - 'a') + 10); } static int isneg (const char **s) { if (**s == '-') { (*s)++; return 1; } else if (**s == '+') (*s)++; return 0; } /* ** {================================================================== ** Lua's implementation for 'lua_strx2number' ** =================================================================== */ #if !defined(lua_strx2number) /* maximum number of significant digits to read (to avoid overflows even with single floats) */ #define MAXSIGDIG 30 /* ** convert a hexadecimal numeric string to a number, following ** C99 specification for 'strtod' */ static lua_Number lua_strx2number (const char *s, char **endptr) { int dot = lua_getlocaledecpoint(); lua_Number r = l_mathop(0.0); /* result (accumulator) */ int sigdig = 0; /* number of significant digits */ int nosigdig = 0; /* number of non-significant digits */ int e = 0; /* exponent correction */ int neg; /* 1 if number is negative */ int hasdot = 0; /* true after seen a dot */ *endptr = cast_charp(s); /* nothing is valid yet */ while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ neg = isneg(&s); /* check sign */ if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ return l_mathop(0.0); /* invalid format (no '0x') */ for (s += 2; ; s++) { /* skip '0x' and read numeral */ if (*s == dot) { if (hasdot) break; /* second dot? stop loop */ else hasdot = 1; } else if (lisxdigit(cast_uchar(*s))) { if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */ nosigdig++; else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */ r = (r * l_mathop(16.0)) + luaO_hexavalue(*s); else e++; /* too many digits; ignore, but still count for exponent */ if (hasdot) e--; /* decimal digit? correct exponent */ } else break; /* neither a dot nor a digit */ } if (nosigdig + sigdig == 0) /* no digits? */ return l_mathop(0.0); /* invalid format */ *endptr = cast_charp(s); /* valid up to here */ e *= 4; /* each digit multiplies/divides value by 2^4 */ if (*s == 'p' || *s == 'P') { /* exponent part? */ int exp1 = 0; /* exponent value */ int neg1; /* exponent sign */ s++; /* skip 'p' */ neg1 = isneg(&s); /* sign */ if (!lisdigit(cast_uchar(*s))) return l_mathop(0.0); /* invalid; must have at least one digit */ while (lisdigit(cast_uchar(*s))) /* read exponent */ exp1 = exp1 * 10 + *(s++) - '0'; if (neg1) exp1 = -exp1; e += exp1; *endptr = cast_charp(s); /* valid up to here */ } if (neg) r = -r; return l_mathop(ldexp)(r, e); } #endif /* }====================================================== */ /* maximum length of a numeral to be converted to a number */ #if !defined (L_MAXLENNUM) #define L_MAXLENNUM 200 #endif /* ** Convert string 's' to a Lua number (put in 'result'). Return NULL on ** fail or the address of the ending '\0' on success. ('mode' == 'x') ** means a hexadecimal numeral. */ static const char *l_str2dloc (const char *s, lua_Number *result, int mode) { char *endptr; *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */ : lua_str2number(s, &endptr); if (endptr == s) return NULL; /* nothing recognized? */ while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */ return (*endptr == '\0') ? endptr : NULL; /* OK iff no trailing chars */ } /* ** Convert string 's' to a Lua number (put in 'result') handling the ** current locale. ** This function accepts both the current locale or a dot as the radix ** mark. If the conversion fails, it may mean number has a dot but ** locale accepts something else. In that case, the code copies 's' ** to a buffer (because 's' is read-only), changes the dot to the ** current locale radix mark, and tries to convert again. ** The variable 'mode' checks for special characters in the string: ** - 'n' means 'inf' or 'nan' (which should be rejected) ** - 'x' means a hexadecimal numeral ** - '.' just optimizes the search for the common case (no special chars) */ static const char *l_str2d (const char *s, lua_Number *result) { const char *endptr; const char *pmode = strpbrk(s, ".xXnN"); /* look for special chars */ int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0; if (mode == 'n') /* reject 'inf' and 'nan' */ return NULL; endptr = l_str2dloc(s, result, mode); /* try to convert */ if (endptr == NULL) { /* failed? may be a different locale */ char buff[L_MAXLENNUM + 1]; const char *pdot = strchr(s, '.'); if (pdot == NULL || strlen(s) > L_MAXLENNUM) return NULL; /* string too long or no dot; fail */ strcpy(buff, s); /* copy string to buffer */ buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */ endptr = l_str2dloc(buff, result, mode); /* try again */ if (endptr != NULL) endptr = s + (endptr - buff); /* make relative to 's' */ } return endptr; } #define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10) #define MAXLASTD cast_int(LUA_MAXINTEGER % 10) static const char *l_str2int (const char *s, lua_Integer *result) { lua_Unsigned a = 0; int empty = 1; int neg; while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ neg = isneg(&s); if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) { /* hex? */ s += 2; /* skip '0x' */ for (; lisxdigit(cast_uchar(*s)); s++) { a = a * 16 + luaO_hexavalue(*s); empty = 0; } } else { /* decimal */ for (; lisdigit(cast_uchar(*s)); s++) { int d = *s - '0'; if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */ return NULL; /* do not accept it (as integer) */ a = a * 10 + cast_uint(d); empty = 0; } } while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ if (empty || *s != '\0') return NULL; /* something wrong in the numeral */ else { *result = l_castU2S((neg) ? 0u - a : a); return s; } } size_t luaO_str2num (const char *s, TValue *o) { lua_Integer i; lua_Number n; const char *e; if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */ setivalue(o, i); } else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */ setfltvalue(o, n); } else return 0; /* conversion failed */ return ct_diff2sz(e - s) + 1; /* success; return string size */ } int luaO_utf8esc (char *buff, l_uint32 x) { int n = 1; /* number of bytes put in buffer (backwards) */ lua_assert(x <= 0x7FFFFFFFu); if (x < 0x80) /* ASCII? */ buff[UTF8BUFFSZ - 1] = cast_char(x); else { /* need continuation bytes */ unsigned int mfb = 0x3f; /* maximum that fits in first byte */ do { /* add continuation bytes */ buff[UTF8BUFFSZ - (n++)] = cast_char(0x80 | (x & 0x3f)); x >>= 6; /* remove added bits */ mfb >>= 1; /* now there is one less bit available in first byte */ } while (x > mfb); /* still needs continuation byte? */ buff[UTF8BUFFSZ - n] = cast_char((~mfb << 1) | x); /* add first byte */ } return n; } /* ** The size of the buffer for the conversion of a number to a string ** 'LUA_N2SBUFFSZ' must be enough to accommodate both LUA_INTEGER_FMT ** and LUA_NUMBER_FMT. For a long long int, this is 19 digits plus a ** sign and a final '\0', adding to 21. For a long double, it can go to ** a sign, the dot, an exponent letter, an exponent sign, 4 exponent ** digits, the final '\0', plus the significant digits, which are ** approximately the *_DIG attribute. */ #if LUA_N2SBUFFSZ < (20 + l_floatatt(DIG)) #error "invalid value for LUA_N2SBUFFSZ" #endif /* ** Convert a float to a string, adding it to a buffer. First try with ** a not too large number of digits, to avoid noise (for instance, ** 1.1 going to "1.1000000000000001"). If that lose precision, so ** that reading the result back gives a different number, then do the ** conversion again with extra precision. Moreover, if the numeral looks ** like an integer (without a decimal point or an exponent), add ".0" to ** its end. */ static int tostringbuffFloat (lua_Number n, char *buff) { /* first conversion */ int len = l_sprintf(buff, LUA_N2SBUFFSZ, LUA_NUMBER_FMT, (LUAI_UACNUMBER)n); lua_Number check = lua_str2number(buff, NULL); /* read it back */ if (check != n) { /* not enough precision? */ /* convert again with more precision */ len = l_sprintf(buff, LUA_N2SBUFFSZ, LUA_NUMBER_FMT_N, (LUAI_UACNUMBER)n); } /* looks like an integer? */ if (buff[strspn(buff, "-0123456789")] == '\0') { buff[len++] = lua_getlocaledecpoint(); buff[len++] = '0'; /* adds '.0' to result */ } return len; } /* ** Convert a number object to a string, adding it to a buffer. */ unsigned luaO_tostringbuff (const TValue *obj, char *buff) { int len; lua_assert(ttisnumber(obj)); if (ttisinteger(obj)) len = lua_integer2str(buff, LUA_N2SBUFFSZ, ivalue(obj)); else len = tostringbuffFloat(fltvalue(obj), buff); lua_assert(len < LUA_N2SBUFFSZ); return cast_uint(len); } /* ** Convert a number object to a Lua string, replacing the value at 'obj' */ void luaO_tostring (lua_State *L, TValue *obj) { char buff[LUA_N2SBUFFSZ]; unsigned len = luaO_tostringbuff(obj, buff); setsvalue(L, obj, luaS_newlstr(L, buff, len)); } /* ** {================================================================== ** 'luaO_pushvfstring' ** =================================================================== */ /* ** Size for buffer space used by 'luaO_pushvfstring'. It should be ** (LUA_IDSIZE + LUA_N2SBUFFSZ) + a minimal space for basic messages, ** so that 'luaG_addinfo' can work directly on the static buffer. */ #define BUFVFS cast_uint(LUA_IDSIZE + LUA_N2SBUFFSZ + 95) /* ** Buffer used by 'luaO_pushvfstring'. 'err' signals an error while ** building result (memory error [1] or buffer overflow [2]). */ typedef struct BuffFS { lua_State *L; char *b; size_t buffsize; size_t blen; /* length of string in 'buff' */ int err; char space[BUFVFS]; /* initial buffer */ } BuffFS; static void initbuff (lua_State *L, BuffFS *buff) { buff->L = L; buff->b = buff->space; buff->buffsize = sizeof(buff->space); buff->blen = 0; buff->err = 0; } /* ** Push final result from 'luaO_pushvfstring'. This function may raise ** errors explicitly or through memory errors, so it must run protected. */ static void pushbuff (lua_State *L, void *ud) { BuffFS *buff = cast(BuffFS*, ud); switch (buff->err) { case 1: /* memory error */ luaD_throw(L, LUA_ERRMEM); break; case 2: /* length overflow: Add "..." at the end of result */ if (buff->buffsize - buff->blen < 3) strcpy(buff->b + buff->blen - 3, "..."); /* 'blen' must be > 3 */ else { /* there is enough space left for the "..." */ strcpy(buff->b + buff->blen, "..."); buff->blen += 3; } /* FALLTHROUGH */ default: { /* no errors, but it can raise one creating the new string */ TString *ts = luaS_newlstr(L, buff->b, buff->blen); setsvalue2s(L, L->top.p, ts); L->top.p++; } } } static const char *clearbuff (BuffFS *buff) { lua_State *L = buff->L; const char *res; if (luaD_rawrunprotected(L, pushbuff, buff) != LUA_OK) /* errors? */ res = NULL; /* error message is on the top of the stack */ else res = getstr(tsvalue(s2v(L->top.p - 1))); if (buff->b != buff->space) /* using dynamic buffer? */ luaM_freearray(L, buff->b, buff->buffsize); /* free it */ return res; } static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { size_t left = buff->buffsize - buff->blen; /* space left in the buffer */ if (buff->err) /* do nothing else after an error */ return; if (slen > left) { /* new string doesn't fit into current buffer? */ if (slen > ((MAX_SIZE/2) - buff->blen)) { /* overflow? */ memcpy(buff->b + buff->blen, str, left); /* copy what it can */ buff->blen = buff->buffsize; buff->err = 2; /* doesn't add anything else */ return; } else { size_t newsize = buff->buffsize + slen; /* limited to MAX_SIZE/2 */ char *newb = (buff->b == buff->space) /* still using static space? */ ? luaM_reallocvector(buff->L, NULL, 0, newsize, char) : luaM_reallocvector(buff->L, buff->b, buff->buffsize, newsize, char); if (newb == NULL) { /* allocation error? */ buff->err = 1; /* signal a memory error */ return; } if (buff->b == buff->space) /* new buffer (not reallocated)? */ memcpy(newb, buff->b, buff->blen); /* copy previous content */ buff->b = newb; /* set new (larger) buffer... */ buff->buffsize = newsize; /* ...and its new size */ } } memcpy(buff->b + buff->blen, str, slen); /* copy new content */ buff->blen += slen; } /* ** Add a numeral to the buffer. */ static void addnum2buff (BuffFS *buff, TValue *num) { char numbuff[LUA_N2SBUFFSZ]; unsigned len = luaO_tostringbuff(num, numbuff); addstr2buff(buff, numbuff, len); } /* ** this function handles only '%d', '%c', '%f', '%p', '%s', and '%%' conventional formats, plus Lua-specific '%I' and '%U' */ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { BuffFS buff; /* holds last part of the result */ const char *e; /* points to next '%' */ initbuff(L, &buff); while ((e = strchr(fmt, '%')) != NULL) { addstr2buff(&buff, fmt, ct_diff2sz(e - fmt)); /* add 'fmt' up to '%' */ switch (*(e + 1)) { /* conversion specifier */ case 's': { /* zero-terminated string */ const char *s = va_arg(argp, char *); if (s == NULL) s = "(null)"; addstr2buff(&buff, s, strlen(s)); break; } case 'c': { /* an 'int' as a character */ char c = cast_char(va_arg(argp, int)); addstr2buff(&buff, &c, sizeof(char)); break; } case 'd': { /* an 'int' */ TValue num; setivalue(&num, va_arg(argp, int)); addnum2buff(&buff, &num); break; } case 'I': { /* a 'lua_Integer' */ TValue num; setivalue(&num, cast_Integer(va_arg(argp, l_uacInt))); addnum2buff(&buff, &num); break; } case 'f': { /* a 'lua_Number' */ TValue num; setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber))); addnum2buff(&buff, &num); break; } case 'p': { /* a pointer */ char bf[LUA_N2SBUFFSZ]; /* enough space for '%p' */ void *p = va_arg(argp, void *); int len = lua_pointer2str(bf, LUA_N2SBUFFSZ, p); addstr2buff(&buff, bf, cast_uint(len)); break; } case 'U': { /* an 'unsigned long' as a UTF-8 sequence */ char bf[UTF8BUFFSZ]; unsigned long arg = va_arg(argp, unsigned long); int len = luaO_utf8esc(bf, cast(l_uint32, arg)); addstr2buff(&buff, bf + UTF8BUFFSZ - len, cast_uint(len)); break; } case '%': { addstr2buff(&buff, "%", 1); break; } default: { addstr2buff(&buff, e, 2); /* keep unknown format in the result */ break; } } fmt = e + 2; /* skip '%' and the specifier */ } addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */ return clearbuff(&buff); /* empty buffer into a new string */ } const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { const char *msg; va_list argp; va_start(argp, fmt); msg = luaO_pushvfstring(L, fmt, argp); va_end(argp); if (msg == NULL) /* error? */ luaD_throw(L, LUA_ERRMEM); return msg; } /* }================================================================== */ #define RETS "..." #define PRE "[string \"" #define POS "\"]" #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) void luaO_chunkid (char *out, const char *source, size_t srclen) { size_t bufflen = LUA_IDSIZE; /* free space in buffer */ if (*source == '=') { /* 'literal' source */ if (srclen <= bufflen) /* small enough? */ memcpy(out, source + 1, srclen * sizeof(char)); else { /* truncate it */ addstr(out, source + 1, bufflen - 1); *out = '\0'; } } else if (*source == '@') { /* file name */ if (srclen <= bufflen) /* small enough? */ memcpy(out, source + 1, srclen * sizeof(char)); else { /* add '...' before rest of name */ addstr(out, RETS, LL(RETS)); bufflen -= LL(RETS); memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char)); } } else { /* string; format as [string "source"] */ const char *nl = strchr(source, '\n'); /* find first new line (if any) */ addstr(out, PRE, LL(PRE)); /* add prefix */ bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ if (srclen < bufflen && nl == NULL) { /* small one-line source? */ addstr(out, source, srclen); /* keep it */ } else { if (nl != NULL) srclen = ct_diff2sz(nl - source); /* stop at first newline */ if (srclen > bufflen) srclen = bufflen; addstr(out, source, srclen); addstr(out, RETS, LL(RETS)); } memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); } } ================================================ FILE: 3rd/lua/lobject.h ================================================ /* ** $Id: lobject.h $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ #ifndef lobject_h #define lobject_h #include #include "llimits.h" #include "lua.h" /* ** Extra types for collectable non-values */ #define LUA_TUPVAL LUA_NUMTYPES /* upvalues */ #define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */ #define LUA_TDEADKEY (LUA_NUMTYPES+2) /* removed keys in tables */ /* ** number of all possible types (including LUA_TNONE but excluding DEADKEY) */ #define LUA_TOTALTYPES (LUA_TPROTO + 2) /* ** tags for Tagged Values have the following use of bits: ** bits 0-3: actual tag (a LUA_T* constant) ** bits 4-5: variant bits ** bit 6: whether value is collectable */ /* add variant bits to a type */ #define makevariant(t,v) ((t) | ((v) << 4)) /* ** Union of all Lua values */ typedef union Value { struct GCObject *gc; /* collectable objects */ void *p; /* light userdata */ lua_CFunction f; /* light C functions */ lua_Integer i; /* integer numbers */ lua_Number n; /* float numbers */ /* not used, but may avoid warnings for uninitialized value */ lu_byte ub; } Value; /* ** Tagged Values. This is the basic representation of values in Lua: ** an actual value plus a tag with its type. */ #define TValuefields Value value_; lu_byte tt_ typedef struct TValue { TValuefields; } TValue; #define val_(o) ((o)->value_) #define valraw(o) (val_(o)) /* raw type tag of a TValue */ #define rawtt(o) ((o)->tt_) /* tag with no variants (bits 0-3) */ #define novariant(t) ((t) & 0x0F) /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */ #define withvariant(t) ((t) & 0x3F) #define ttypetag(o) withvariant(rawtt(o)) /* type of a TValue */ #define ttype(o) (novariant(rawtt(o))) /* Macros to test type */ #define checktag(o,t) (rawtt(o) == (t)) #define checktype(o,t) (ttype(o) == (t)) /* Macros for internal tests */ /* collectable object has the same tag as the original value */ #define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt) /* ** Any value being manipulated by the program either is non ** collectable, or the collectable object has the right tag ** and it is not dead. The option 'L == NULL' allows other ** macros using this one to be used where L is not available. */ #define checkliveness(L,obj) \ ((void)L, lua_longassert(!iscollectable(obj) || \ (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)) || isshared(gcvalue(obj)))))) /* Macros to set values */ /* set a value's tag */ #define settt_(o,t) ((o)->tt_=(t)) /* main macro to copy values (from 'obj2' to 'obj1') */ #define setobj(L,obj1,obj2) \ { TValue *io1=(obj1); const TValue *io2=(obj2); \ io1->value_ = io2->value_; settt_(io1, io2->tt_); \ checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); } /* ** Different types of assignments, according to source and destination. ** (They are mostly equal now, but may be different in the future.) */ /* from stack to stack */ #define setobjs2s(L,o1,o2) setobj(L,s2v(o1),s2v(o2)) /* to stack (not from same stack) */ #define setobj2s(L,o1,o2) setobj(L,s2v(o1),o2) /* from table to same table */ #define setobjt2t setobj /* to new object */ #define setobj2n setobj /* to table */ #define setobj2t setobj /* ** Entries in a Lua stack. Field 'tbclist' forms a list of all ** to-be-closed variables active in this stack. Dummy entries are ** used when the distance between two tbc variables does not fit ** in an unsigned short. They are represented by delta==0, and ** their real delta is always the maximum value that fits in ** that field. */ typedef union StackValue { TValue val; struct { TValuefields; unsigned short delta; } tbclist; } StackValue; /* index to stack elements */ typedef StackValue *StkId; /* ** When reallocating the stack, change all pointers to the stack into ** proper offsets. */ typedef union { StkId p; /* actual pointer */ ptrdiff_t offset; /* used while the stack is being reallocated */ } StkIdRel; /* convert a 'StackValue' to a 'TValue' */ #define s2v(o) (&(o)->val) /* ** {================================================================== ** Nil ** =================================================================== */ /* Standard nil */ #define LUA_VNIL makevariant(LUA_TNIL, 0) /* Empty slot (which might be different from a slot containing nil) */ #define LUA_VEMPTY makevariant(LUA_TNIL, 1) /* Value returned for a key not found in a table (absent key) */ #define LUA_VABSTKEY makevariant(LUA_TNIL, 2) /* Special variant to signal that a fast get is accessing a non-table */ #define LUA_VNOTABLE makevariant(LUA_TNIL, 3) /* macro to test for (any kind of) nil */ #define ttisnil(v) checktype((v), LUA_TNIL) /* ** Macro to test the result of a table access. Formally, it should ** distinguish between LUA_VEMPTY/LUA_VABSTKEY/LUA_VNOTABLE and ** other tags. As currently nil is equivalent to LUA_VEMPTY, it is ** simpler to just test whether the value is nil. */ #define tagisempty(tag) (novariant(tag) == LUA_TNIL) /* macro to test for a standard nil */ #define ttisstrictnil(o) checktag((o), LUA_VNIL) #define setnilvalue(obj) settt_(obj, LUA_VNIL) #define isabstkey(v) checktag((v), LUA_VABSTKEY) /* ** macro to detect non-standard nils (used only in assertions) */ #define isnonstrictnil(v) (ttisnil(v) && !ttisstrictnil(v)) /* ** By default, entries with any kind of nil are considered empty. ** (In any definition, values associated with absent keys must also ** be accepted as empty.) */ #define isempty(v) ttisnil(v) /* macro defining a value corresponding to an absent key */ #define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY /* mark an entry as empty */ #define setempty(v) settt_(v, LUA_VEMPTY) /* }================================================================== */ /* ** {================================================================== ** Booleans ** =================================================================== */ #define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0) #define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1) #define ttisboolean(o) checktype((o), LUA_TBOOLEAN) #define ttisfalse(o) checktag((o), LUA_VFALSE) #define ttistrue(o) checktag((o), LUA_VTRUE) #define l_isfalse(o) (ttisfalse(o) || ttisnil(o)) #define tagisfalse(t) ((t) == LUA_VFALSE || novariant(t) == LUA_TNIL) #define setbfvalue(obj) settt_(obj, LUA_VFALSE) #define setbtvalue(obj) settt_(obj, LUA_VTRUE) /* }================================================================== */ /* ** {================================================================== ** Threads ** =================================================================== */ #define LUA_VTHREAD makevariant(LUA_TTHREAD, 0) #define ttisthread(o) checktag((o), ctb(LUA_VTHREAD)) #define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc)) #define setthvalue(L,obj,x) \ { TValue *io = (obj); lua_State *x_ = (x); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \ checkliveness(L,io); } #define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t) /* }================================================================== */ /* ** {================================================================== ** Collectable Objects ** =================================================================== */ /* ** Common Header for all collectable objects (in macro form, to be ** included in other objects) */ #define CommonHeader struct GCObject *next; lu_byte tt; lu_byte marked /* Common type for all collectable objects */ typedef struct GCObject { CommonHeader; } GCObject; /* Bit mark for collectable types */ #define BIT_ISCOLLECTABLE (1 << 6) #define iscollectable(o) (rawtt(o) & BIT_ISCOLLECTABLE) /* mark a tag as collectable */ #define ctb(t) ((t) | BIT_ISCOLLECTABLE) #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) #define gcvalueraw(v) ((v).gc) #define setgcovalue(L,obj,x) \ { TValue *io = (obj); GCObject *i_g=(x); \ val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); } /* }================================================================== */ /* ** {================================================================== ** Numbers ** =================================================================== */ /* Variant tags for numbers */ #define LUA_VNUMINT makevariant(LUA_TNUMBER, 0) /* integer numbers */ #define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1) /* float numbers */ #define ttisnumber(o) checktype((o), LUA_TNUMBER) #define ttisfloat(o) checktag((o), LUA_VNUMFLT) #define ttisinteger(o) checktag((o), LUA_VNUMINT) #define nvalue(o) check_exp(ttisnumber(o), \ (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o))) #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n) #define ivalue(o) check_exp(ttisinteger(o), val_(o).i) #define fltvalueraw(v) ((v).n) #define ivalueraw(v) ((v).i) #define setfltvalue(obj,x) \ { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); } #define chgfltvalue(obj,x) \ { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); } #define setivalue(obj,x) \ { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); } #define chgivalue(obj,x) \ { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); } /* }================================================================== */ /* ** {================================================================== ** Strings ** =================================================================== */ /* Variant tags for strings */ #define LUA_VSHRSTR makevariant(LUA_TSTRING, 0) /* short strings */ #define LUA_VLNGSTR makevariant(LUA_TSTRING, 1) /* long strings */ #define ttisstring(o) checktype((o), LUA_TSTRING) #define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR)) #define ttislngstring(o) checktag((o), ctb(LUA_VLNGSTR)) #define tsvalueraw(v) (gco2ts((v).gc)) #define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc)) #define setsvalue(L,obj,x) \ { TValue *io = (obj); TString *x_ = (x); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \ checkliveness(L,io); } /* set a string to the stack */ #define setsvalue2s(L,o,s) setsvalue(L,s2v(o),s) /* set a string to a new object */ #define setsvalue2n setsvalue /* Kinds of long strings (stored in 'shrlen') */ #define LSTRREG -1 /* regular long string */ #define LSTRFIX -2 /* fixed external long string */ #define LSTRMEM -3 /* external long string with deallocation */ /* ** Header for a string value. */ typedef struct TString { CommonHeader; lu_byte extra; /* reserved words for short strings; "has hash" for longs */ ls_byte shrlen; /* length for short strings, negative for long strings */ unsigned int hash; size_t id; /* id for short strings */ union { size_t lnglen; /* length for long strings */ struct TString *hnext; /* linked list for hash table */ } u; char *contents; /* pointer to content in long strings */ lua_Alloc falloc; /* deallocation function for external strings */ void *ud; /* user data for external strings */ } TString; #define strisshr(ts) ((ts)->shrlen >= 0) #define isextstr(ts) (ttislngstring(ts) && tsvalue(ts)->shrlen != LSTRREG) /* ** Get the actual string (array of bytes) from a 'TString'. (Generic ** version and specialized versions for long and short strings.) */ #define rawgetshrstr(ts) (cast_charp(&(ts)->contents)) #define getshrstr(ts) check_exp(strisshr(ts), rawgetshrstr(ts)) #define getlngstr(ts) check_exp(!strisshr(ts), (ts)->contents) #define getstr(ts) (strisshr(ts) ? rawgetshrstr(ts) : (ts)->contents) /* get string length from 'TString *ts' */ #define tsslen(ts) \ (strisshr(ts) ? cast_sizet((ts)->shrlen) : (ts)->u.lnglen) /* ** Get string and length */ #define getlstr(ts, len) \ (strisshr(ts) \ ? (cast_void((len) = cast_sizet((ts)->shrlen)), rawgetshrstr(ts)) \ : (cast_void((len) = (ts)->u.lnglen), (ts)->contents)) /* }================================================================== */ /* ** {================================================================== ** Userdata ** =================================================================== */ /* ** Light userdata should be a variant of userdata, but for compatibility ** reasons they are also different types. */ #define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 0) #define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 0) #define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA) #define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA)) #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) #define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc)) #define pvalueraw(v) ((v).p) #define setpvalue(obj,x) \ { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); } #define setuvalue(L,obj,x) \ { TValue *io = (obj); Udata *x_ = (x); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \ checkliveness(L,io); } /* Ensures that addresses after this type are always fully aligned. */ typedef union UValue { TValue uv; LUAI_MAXALIGN; /* ensures maximum alignment for udata bytes */ } UValue; /* ** Header for userdata with user values; ** memory area follows the end of this structure. */ typedef struct Udata { CommonHeader; unsigned short nuvalue; /* number of user values */ size_t len; /* number of bytes */ struct Table *metatable; GCObject *gclist; UValue uv[1]; /* user values */ } Udata; /* ** Header for userdata with no user values. These userdata do not need ** to be gray during GC, and therefore do not need a 'gclist' field. ** To simplify, the code always use 'Udata' for both kinds of userdata, ** making sure it never accesses 'gclist' on userdata with no user values. ** This structure here is used only to compute the correct size for ** this representation. (The 'bindata' field in its end ensures correct ** alignment for binary data following this header.) */ typedef struct Udata0 { CommonHeader; unsigned short nuvalue; /* number of user values */ size_t len; /* number of bytes */ struct Table *metatable; union {LUAI_MAXALIGN;} bindata; } Udata0; /* compute the offset of the memory area of a userdata */ #define udatamemoffset(nuv) \ ((nuv) == 0 ? offsetof(Udata0, bindata) \ : offsetof(Udata, uv) + (sizeof(UValue) * (nuv))) /* get the address of the memory block inside 'Udata' */ #define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue)) /* compute the size of a userdata */ #define sizeudata(nuv,nb) (udatamemoffset(nuv) + (nb)) /* }================================================================== */ /* ** {================================================================== ** Prototypes ** =================================================================== */ #define LUA_VPROTO makevariant(LUA_TPROTO, 0) typedef l_uint32 Instruction; /* ** Description of an upvalue for function prototypes */ typedef struct Upvaldesc { TString *name; /* upvalue name (for debug information) */ lu_byte instack; /* whether it is in stack (register) */ lu_byte idx; /* index of upvalue (in stack or in outer function's list) */ lu_byte kind; /* kind of corresponding variable */ } Upvaldesc; /* ** Description of a local variable for function prototypes ** (used for debug information) */ typedef struct LocVar { TString *varname; int startpc; /* first point where variable is active */ int endpc; /* first point where variable is dead */ } LocVar; /* ** Associates the absolute line source for a given instruction ('pc'). ** The array 'lineinfo' gives, for each instruction, the difference in ** lines from the previous instruction. When that difference does not ** fit into a byte, Lua saves the absolute line for that instruction. ** (Lua also saves the absolute line periodically, to speed up the ** computation of a line number: we can use binary search in the ** absolute-line array, but we must traverse the 'lineinfo' array ** linearly to compute a line.) */ typedef struct AbsLineInfo { int pc; int line; } AbsLineInfo; /* ** Flags in Prototypes */ #define PF_VAHID 1 /* function has hidden vararg arguments */ #define PF_VATAB 2 /* function has vararg table */ #define PF_FIXED 4 /* prototype has parts in fixed memory */ /* a vararg function either has hidden args. or a vararg table */ #define isvararg(p) ((p)->flag & (PF_VAHID | PF_VATAB)) /* ** mark that a function needs a vararg table. (The flag PF_VAHID will ** be cleared later.) */ #define needvatab(p) ((p)->flag |= PF_VATAB) /* ** Function Prototypes */ typedef struct Proto { CommonHeader; lu_byte numparams; /* number of fixed (named) parameters */ lu_byte flag; lu_byte maxstacksize; /* number of registers needed by this function */ int sizeupvalues; /* size of 'upvalues' */ int sizek; /* size of 'k' */ int sizecode; int sizelineinfo; int sizep; /* size of 'p' */ int sizelocvars; int sizeabslineinfo; /* size of 'abslineinfo' */ int linedefined; /* debug information */ int lastlinedefined; /* debug information */ TValue *k; /* constants used by the function */ Instruction *code; /* opcodes */ struct Proto **p; /* functions defined inside the function */ Upvaldesc *upvalues; /* upvalue information */ ls_byte *lineinfo; /* information about source lines (debug information) */ AbsLineInfo *abslineinfo; /* idem */ LocVar *locvars; /* information about local variables (debug information) */ TString *source; /* used for debug information */ GCObject *gclist; } Proto; /* }================================================================== */ /* ** {================================================================== ** Functions ** =================================================================== */ #define LUA_VUPVAL makevariant(LUA_TUPVAL, 0) /* Variant tags for functions */ #define LUA_VLCL makevariant(LUA_TFUNCTION, 0) /* Lua closure */ #define LUA_VLCF makevariant(LUA_TFUNCTION, 1) /* light C function */ #define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */ #define ttisfunction(o) checktype(o, LUA_TFUNCTION) #define ttisLclosure(o) checktag((o), ctb(LUA_VLCL)) #define ttislcf(o) checktag((o), LUA_VLCF) #define ttisCclosure(o) checktag((o), ctb(LUA_VCCL)) #define ttisclosure(o) (ttisLclosure(o) || ttisCclosure(o)) #define isLfunction(o) ttisLclosure(o) #define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc)) #define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc)) #define fvalue(o) check_exp(ttislcf(o), val_(o).f) #define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc)) #define fvalueraw(v) ((v).f) #define setclLvalue(L,obj,x) \ { TValue *io = (obj); LClosure *x_ = (x); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \ checkliveness(L,io); } #define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl) #define setfvalue(obj,x) \ { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); } #define setclCvalue(L,obj,x) \ { TValue *io = (obj); CClosure *x_ = (x); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \ checkliveness(L,io); } /* ** Upvalues for Lua closures */ typedef struct UpVal { CommonHeader; union { TValue *p; /* points to stack or to its own value */ ptrdiff_t offset; /* used while the stack is being reallocated */ } v; union { struct { /* (when open) */ struct UpVal *next; /* linked list */ struct UpVal **previous; } open; TValue value; /* the value (when closed) */ } u; } UpVal; #define ClosureHeader \ CommonHeader; lu_byte nupvalues; GCObject *gclist typedef struct CClosure { ClosureHeader; lua_CFunction f; TValue upvalue[1]; /* list of upvalues */ } CClosure; typedef struct LClosure { ClosureHeader; struct Proto *p; UpVal *upvals[1]; /* list of upvalues */ } LClosure; typedef union Closure { CClosure c; LClosure l; } Closure; #define getproto(o) (clLvalue(o)->p) /* }================================================================== */ /* ** {================================================================== ** Tables ** =================================================================== */ #define LUA_VTABLE makevariant(LUA_TTABLE, 0) #define ttistable(o) checktag((o), ctb(LUA_VTABLE)) #define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc)) #define sethvalue(L,obj,x) \ { TValue *io = (obj); Table *x_ = (x); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \ checkliveness(L,io); } #define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h) /* ** Nodes for Hash tables: A pack of two TValue's (key-value pairs) ** plus a 'next' field to link colliding entries. The distribution ** of the key's fields ('key_tt' and 'key_val') not forming a proper ** 'TValue' allows for a smaller size for 'Node' both in 4-byte ** and 8-byte alignments. */ typedef union Node { struct NodeKey { TValuefields; /* fields for value */ lu_byte key_tt; /* key type */ int next; /* for chaining */ Value key_val; /* key value */ } u; TValue i_val; /* direct access to node's value as a proper 'TValue' */ } Node; /* copy a value into a key */ #define setnodekey(node,obj) \ { Node *n_=(node); const TValue *io_=(obj); \ n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; } /* copy a value from a key */ #define getnodekey(L,obj,node) \ { TValue *io_=(obj); const Node *n_=(node); \ io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \ checkliveness(L,io_); } typedef struct Table { CommonHeader; lu_byte flags; /* 1<

u.key_tt) #define keyval(node) ((node)->u.key_val) #define keyisnil(node) (keytt(node) == LUA_TNIL) #define keyisinteger(node) (keytt(node) == LUA_VNUMINT) #define keyival(node) (keyval(node).i) #define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR)) #define keystrval(node) (gco2ts(keyval(node).gc)) #define setnilkey(node) (keytt(node) = LUA_TNIL) #define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE) #define gckey(n) (keyval(n).gc) #define gckeyN(n) (keyiscollectable(n) ? gckey(n) : NULL) /* ** Dead keys in tables have the tag DEADKEY but keep their original ** gcvalue. This distinguishes them from regular keys but allows them to ** be found when searched in a special way. ('next' needs that to find ** keys removed from a table during a traversal.) */ #define setdeadkey(node) (keytt(node) = LUA_TDEADKEY) #define keyisdead(node) (keytt(node) == LUA_TDEADKEY) /* }================================================================== */ /* ** 'module' operation for hashing (size is always a power of 2) */ #define lmod(s,size) \ (check_exp((size&(size-1))==0, (cast_uint(s) & cast_uint((size)-1)))) #define twoto(x) (1u<<(x)) #define sizenode(t) (twoto((t)->lsizenode)) /* size of buffer for 'luaO_utf8esc' function */ #define UTF8BUFFSZ 8 /* macro to call 'luaO_pushvfstring' correctly */ #define pushvfstring(L, argp, fmt, msg) \ { va_start(argp, fmt); \ msg = luaO_pushvfstring(L, fmt, argp); \ va_end(argp); \ if (msg == NULL) luaD_throw(L, LUA_ERRMEM); /* only after 'va_end' */ } LUAI_FUNC int luaO_utf8esc (char *buff, l_uint32 x); LUAI_FUNC lu_byte luaO_ceillog2 (unsigned int x); LUAI_FUNC lu_byte luaO_codeparam (unsigned int p); LUAI_FUNC l_mem luaO_applyparam (lu_byte p, l_mem x); LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2, TValue *res); LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, StkId res); LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o); LUAI_FUNC unsigned luaO_tostringbuff (const TValue *obj, char *buff); LUAI_FUNC lu_byte luaO_hexavalue (int c); LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj); LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp); LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen); #endif ================================================ FILE: 3rd/lua/lopcodes.c ================================================ /* ** $Id: lopcodes.c $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ #define lopcodes_c #define LUA_CORE #include "lprefix.h" #include "lopcodes.h" #define opmode(mm,ot,it,t,a,m) \ (((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m)) /* ORDER OP */ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { /* MM OT IT T A mode opcode */ opmode(0, 0, 0, 0, 1, iABC) /* OP_MOVE */ ,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADI */ ,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADF */ ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADK */ ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADKX */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADFALSE */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LFALSESKIP */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADTRUE */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADNIL */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETUPVAL */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETUPVAL */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABUP */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABLE */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETI */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETFIELD */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABUP */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABLE */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETI */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETFIELD */ ,opmode(0, 0, 0, 0, 1, ivABC) /* OP_NEWTABLE */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SELF */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDI */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDK */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUBK */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MULK */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MODK */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_POWK */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIVK */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIVK */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BANDK */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BORK */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXORK */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHLI */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADD */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUB */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MUL */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MOD */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_POW */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIV */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIV */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BAND */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BOR */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXOR */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHL */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHR */ ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBIN */ ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINI */ ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINK */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_UNM */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BNOT */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_NOT */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LEN */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_CONCAT */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_CLOSE */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_TBC */ ,opmode(0, 0, 0, 0, 0, isJ) /* OP_JMP */ ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQ */ ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LT */ ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LE */ ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQK */ ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQI */ ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LTI */ ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LEI */ ,opmode(0, 0, 0, 1, 0, iABC) /* OP_GTI */ ,opmode(0, 0, 0, 1, 0, iABC) /* OP_GEI */ ,opmode(0, 0, 0, 1, 0, iABC) /* OP_TEST */ ,opmode(0, 0, 0, 1, 1, iABC) /* OP_TESTSET */ ,opmode(0, 1, 1, 0, 1, iABC) /* OP_CALL */ ,opmode(0, 1, 1, 0, 1, iABC) /* OP_TAILCALL */ ,opmode(0, 0, 1, 0, 0, iABC) /* OP_RETURN */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN0 */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN1 */ ,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORLOOP */ ,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORPREP */ ,opmode(0, 0, 0, 0, 0, iABx) /* OP_TFORPREP */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_TFORCALL */ ,opmode(0, 0, 0, 0, 1, iABx) /* OP_TFORLOOP */ ,opmode(0, 0, 1, 0, 0, ivABC) /* OP_SETLIST */ ,opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */ ,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETVARG */ ,opmode(0, 0, 0, 0, 0, iABx) /* OP_ERRNNIL */ ,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */ ,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */ }; /* ** Check whether instruction sets top for next instruction, that is, ** it results in multiple values. */ int luaP_isOT (Instruction i) { OpCode op = GET_OPCODE(i); switch (op) { case OP_TAILCALL: return 1; default: return testOTMode(op) && GETARG_C(i) == 0; } } /* ** Check whether instruction uses top from previous instruction, that is, ** it accepts multiple results. */ int luaP_isIT (Instruction i) { OpCode op = GET_OPCODE(i); switch (op) { case OP_SETLIST: return testITMode(GET_OPCODE(i)) && GETARG_vB(i) == 0; default: return testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0; } } ================================================ FILE: 3rd/lua/lopcodes.h ================================================ /* ** $Id: lopcodes.h $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ #ifndef lopcodes_h #define lopcodes_h #include "llimits.h" #include "lobject.h" /*=========================================================================== We assume that instructions are unsigned 32-bit integers. All instructions have an opcode in the first 7 bits. Instructions can have the following formats: 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 iABC C(8) | B(8) |k| A(8) | Op(7) | ivABC vC(10) | vB(6) |k| A(8) | Op(7) | iABx Bx(17) | A(8) | Op(7) | iAsBx sBx (signed)(17) | A(8) | Op(7) | iAx Ax(25) | Op(7) | isJ sJ (signed)(25) | Op(7) | ('v' stands for "variant", 's' for "signed", 'x' for "extended".) A signed argument is represented in excess K: The represented value is the written unsigned value minus K, where K is half (rounded down) the maximum value for the corresponding unsigned argument. ===========================================================================*/ /* basic instruction formats */ enum OpMode {iABC, ivABC, iABx, iAsBx, iAx, isJ}; /* ** size and position of opcode arguments. */ #define SIZE_C 8 #define SIZE_vC 10 #define SIZE_B 8 #define SIZE_vB 6 #define SIZE_Bx (SIZE_C + SIZE_B + 1) #define SIZE_A 8 #define SIZE_Ax (SIZE_Bx + SIZE_A) #define SIZE_sJ (SIZE_Bx + SIZE_A) #define SIZE_OP 7 #define POS_OP 0 #define POS_A (POS_OP + SIZE_OP) #define POS_k (POS_A + SIZE_A) #define POS_B (POS_k + 1) #define POS_vB (POS_k + 1) #define POS_C (POS_B + SIZE_B) #define POS_vC (POS_vB + SIZE_vB) #define POS_Bx POS_k #define POS_Ax POS_A #define POS_sJ POS_A /* ** limits for opcode arguments. ** we use (signed) 'int' to manipulate most arguments, ** so they must fit in ints. */ /* ** Check whether type 'int' has at least 'b' + 1 bits. ** 'b' < 32; +1 for the sign bit. */ #define L_INTHASBITS(b) ((UINT_MAX >> (b)) >= 1) #if L_INTHASBITS(SIZE_Bx) #define MAXARG_Bx ((1<>1) /* 'sBx' is signed */ #if L_INTHASBITS(SIZE_Ax) #define MAXARG_Ax ((1<> 1) #define MAXARG_A ((1<> 1) #define int2sC(i) ((i) + OFFSET_sC) #define sC2int(i) ((i) - OFFSET_sC) /* creates a mask with 'n' 1 bits at position 'p' */ #define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p)) /* creates a mask with 'n' 0 bits at position 'p' */ #define MASK0(n,p) (~MASK1(n,p)) /* ** the following macros help to manipulate instructions */ #define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0))) #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ ((cast_Inst(o)<>(pos)) & MASK1(size,0))) #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \ ((cast_Inst(v)<> sC */ OP_ADD,/* A B C R[A] := R[B] + R[C] */ OP_SUB,/* A B C R[A] := R[B] - R[C] */ OP_MUL,/* A B C R[A] := R[B] * R[C] */ OP_MOD,/* A B C R[A] := R[B] % R[C] */ OP_POW,/* A B C R[A] := R[B] ^ R[C] */ OP_DIV,/* A B C R[A] := R[B] / R[C] */ OP_IDIV,/* A B C R[A] := R[B] // R[C] */ OP_BAND,/* A B C R[A] := R[B] & R[C] */ OP_BOR,/* A B C R[A] := R[B] | R[C] */ OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */ OP_SHL,/* A B C R[A] := R[B] << R[C] */ OP_SHR,/* A B C R[A] := R[B] >> R[C] */ OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] */ OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */ OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */ OP_UNM,/* A B R[A] := -R[B] */ OP_BNOT,/* A B R[A] := ~R[B] */ OP_NOT,/* A B R[A] := not R[B] */ OP_LEN,/* A B R[A] := #R[B] (length operator) */ OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */ OP_CLOSE,/* A close all upvalues >= R[A] */ OP_TBC,/* A mark variable A "to be closed" */ OP_JMP,/* sJ pc += sJ */ OP_EQ,/* A B k if ((R[A] == R[B]) ~= k) then pc++ */ OP_LT,/* A B k if ((R[A] < R[B]) ~= k) then pc++ */ OP_LE,/* A B k if ((R[A] <= R[B]) ~= k) then pc++ */ OP_EQK,/* A B k if ((R[A] == K[B]) ~= k) then pc++ */ OP_EQI,/* A sB k if ((R[A] == sB) ~= k) then pc++ */ OP_LTI,/* A sB k if ((R[A] < sB) ~= k) then pc++ */ OP_LEI,/* A sB k if ((R[A] <= sB) ~= k) then pc++ */ OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */ OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */ OP_TEST,/* A k if (not R[A] == k) then pc++ */ OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] */ OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */ OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */ OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] */ OP_RETURN0,/* return */ OP_RETURN1,/* A return R[A] */ OP_FORLOOP,/* A Bx update counters; if loop continues then pc-=Bx; */ OP_FORPREP,/* A Bx ; if not to run then pc+=Bx+1; */ OP_TFORPREP,/* A Bx create upvalue for R[A + 3]; pc+=Bx */ OP_TFORCALL,/* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */ OP_TFORLOOP,/* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */ OP_SETLIST,/* A vB vC k R[A][vC+i] := R[A+i], 1 <= i <= vB */ OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */ OP_VARARG,/* A B C k R[A], ..., R[A+C-2] = varargs */ OP_GETVARG, /* A B C R[A] := R[B][R[C]], R[B] is vararg parameter */ OP_ERRNNIL,/* A Bx raise error if R[A] ~= nil (K[Bx - 1] is global name)*/ OP_VARARGPREP,/* (adjust varargs) */ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ } OpCode; #define NUM_OPCODES ((int)(OP_EXTRAARG) + 1) /*=========================================================================== Notes: (*) Opcode OP_LFALSESKIP is used to convert a condition to a boolean value, in a code equivalent to (not cond ? false : true). (It produces false and skips the next instruction producing true.) (*) Opcodes OP_MMBIN and variants follow each arithmetic and bitwise opcode. If the operation succeeds, it skips this next opcode. Otherwise, this opcode calls the corresponding metamethod. (*) Opcode OP_TESTSET is used in short-circuit expressions that need both to jump and to produce a value, such as (a = b or c). (*) In OP_CALL, if (B == 0) then B = top - A. If (C == 0), then 'top' is set to last_result+1, so next open instruction (OP_CALL, OP_RETURN*, OP_SETLIST) may use 'top'. (*) In OP_VARARG, if (C == 0) then use actual number of varargs and set top (like in OP_CALL with C == 0). 'k' means function has a vararg table, which is in R[B]. (*) In OP_RETURN, if (B == 0) then return up to 'top'. (*) In OP_LOADKX and OP_NEWTABLE, the next instruction is always OP_EXTRAARG. (*) In OP_SETLIST, if (B == 0) then real B = 'top'; if k, then real C = EXTRAARG _ C (the bits of EXTRAARG concatenated with the bits of C). (*) In OP_NEWTABLE, vB is log2 of the hash size (which is always a power of 2) plus 1, or zero for size zero. If not k, the array size is vC. Otherwise, the array size is EXTRAARG _ vC. (*) In OP_ERRNNIL, (Bx == 0) means index of global name doesn't fit in Bx. (So, that name is not available for the error message.) (*) For comparisons, k specifies what condition the test should accept (true or false). (*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped (the constant is the first operand). (*) All comparison and test instructions assume that the instruction being skipped (pc++) is a jump. (*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the function builds upvalues, which may need to be closed. C > 0 means the function has hidden vararg arguments, so that its 'func' must be corrected before returning; in this case, (C - 1) is its number of fixed parameters. (*) In comparisons with an immediate operand, C signals whether the original operand was a float. (It must be corrected in case of metamethods.) ===========================================================================*/ /* ** masks for instruction properties. The format is: ** bits 0-2: op mode ** bit 3: instruction set register A ** bit 4: operator is a test (next instruction must be a jump) ** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0) ** bit 6: instruction sets 'L->top' for next instruction (when C == 0) ** bit 7: instruction is an MM instruction (call a metamethod) */ LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];) #define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7)) #define testAMode(m) (luaP_opmodes[m] & (1 << 3)) #define testTMode(m) (luaP_opmodes[m] & (1 << 4)) #define testITMode(m) (luaP_opmodes[m] & (1 << 5)) #define testOTMode(m) (luaP_opmodes[m] & (1 << 6)) #define testMMMode(m) (luaP_opmodes[m] & (1 << 7)) LUAI_FUNC int luaP_isOT (Instruction i); LUAI_FUNC int luaP_isIT (Instruction i); #endif ================================================ FILE: 3rd/lua/lopnames.h ================================================ /* ** $Id: lopnames.h $ ** Opcode names ** See Copyright Notice in lua.h */ #if !defined(lopnames_h) #define lopnames_h #include /* ORDER OP */ static const char *const opnames[] = { "MOVE", "LOADI", "LOADF", "LOADK", "LOADKX", "LOADFALSE", "LFALSESKIP", "LOADTRUE", "LOADNIL", "GETUPVAL", "SETUPVAL", "GETTABUP", "GETTABLE", "GETI", "GETFIELD", "SETTABUP", "SETTABLE", "SETI", "SETFIELD", "NEWTABLE", "SELF", "ADDI", "ADDK", "SUBK", "MULK", "MODK", "POWK", "DIVK", "IDIVK", "BANDK", "BORK", "BXORK", "SHLI", "SHRI", "ADD", "SUB", "MUL", "MOD", "POW", "DIV", "IDIV", "BAND", "BOR", "BXOR", "SHL", "SHR", "MMBIN", "MMBINI", "MMBINK", "UNM", "BNOT", "NOT", "LEN", "CONCAT", "CLOSE", "TBC", "JMP", "EQ", "LT", "LE", "EQK", "EQI", "LTI", "LEI", "GTI", "GEI", "TEST", "TESTSET", "CALL", "TAILCALL", "RETURN", "RETURN0", "RETURN1", "FORLOOP", "FORPREP", "TFORPREP", "TFORCALL", "TFORLOOP", "SETLIST", "CLOSURE", "VARARG", "GETVARG", "ERRNNIL", "VARARGPREP", "EXTRAARG", NULL }; #endif ================================================ FILE: 3rd/lua/loslib.c ================================================ /* ** $Id: loslib.c $ ** Standard Operating System library ** See Copyright Notice in lua.h */ #define loslib_c #define LUA_LIB #include "lprefix.h" #include #include #include #include #include #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "llimits.h" /* ** {================================================================== ** List of valid conversion specifiers for the 'strftime' function; ** options are grouped by length; group of length 2 start with '||'. ** =================================================================== */ #if !defined(LUA_STRFTIMEOPTIONS) /* { */ #if defined(LUA_USE_WINDOWS) #define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYzZ%" \ "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */ #elif defined(LUA_USE_C89) /* C89 (only 1-char options) */ #define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYZ%" #else /* C99 specification */ #define LUA_STRFTIMEOPTIONS "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \ "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */ #endif #endif /* } */ /* }================================================================== */ /* ** {================================================================== ** Configuration for time-related stuff ** =================================================================== */ /* ** type to represent time_t in Lua */ #if !defined(LUA_NUMTIME) /* { */ #define l_timet lua_Integer #define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t)) #define l_gettime(L,arg) luaL_checkinteger(L, arg) #else /* }{ */ #define l_timet lua_Number #define l_pushtime(L,t) lua_pushnumber(L,(lua_Number)(t)) #define l_gettime(L,arg) luaL_checknumber(L, arg) #endif /* } */ #if !defined(l_gmtime) /* { */ /* ** By default, Lua uses gmtime/localtime, except when POSIX is available, ** where it uses gmtime_r/localtime_r */ #if defined(LUA_USE_POSIX) /* { */ #define l_gmtime(t,r) gmtime_r(t,r) #define l_localtime(t,r) localtime_r(t,r) #else /* }{ */ /* ISO C definitions */ #define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t)) #define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t)) #endif /* } */ #endif /* } */ /* }================================================================== */ /* ** {================================================================== ** Configuration for 'tmpnam': ** By default, Lua uses tmpnam except when POSIX is available, where ** it uses mkstemp. ** =================================================================== */ #if !defined(lua_tmpnam) /* { */ #if defined(LUA_USE_POSIX) /* { */ #include #define LUA_TMPNAMBUFSIZE 32 #if !defined(LUA_TMPNAMTEMPLATE) #define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX" #endif #define lua_tmpnam(b,e) { \ strcpy(b, LUA_TMPNAMTEMPLATE); \ e = mkstemp(b); \ if (e != -1) close(e); \ e = (e == -1); } #else /* }{ */ /* ISO C definitions */ #define LUA_TMPNAMBUFSIZE L_tmpnam #define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } #endif /* } */ #endif /* } */ /* }================================================================== */ #if !defined(l_system) #if defined(LUA_USE_IOS) /* Despite claiming to be ISO C, iOS does not implement 'system'. */ #define l_system(cmd) ((cmd) == NULL ? 0 : -1) #else #define l_system(cmd) system(cmd) /* default definition */ #endif #endif static int os_execute (lua_State *L) { const char *cmd = luaL_optstring(L, 1, NULL); int stat; errno = 0; stat = l_system(cmd); if (cmd != NULL) return luaL_execresult(L, stat); else { lua_pushboolean(L, stat); /* true if there is a shell */ return 1; } } static int os_remove (lua_State *L) { const char *filename = luaL_checkstring(L, 1); errno = 0; return luaL_fileresult(L, remove(filename) == 0, filename); } static int os_rename (lua_State *L) { const char *fromname = luaL_checkstring(L, 1); const char *toname = luaL_checkstring(L, 2); errno = 0; return luaL_fileresult(L, rename(fromname, toname) == 0, NULL); } static int os_tmpname (lua_State *L) { char buff[LUA_TMPNAMBUFSIZE]; int err; lua_tmpnam(buff, err); if (l_unlikely(err)) return luaL_error(L, "unable to generate a unique filename"); lua_pushstring(L, buff); return 1; } static int os_getenv (lua_State *L) { lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ return 1; } static int os_clock (lua_State *L) { lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); return 1; } /* ** {====================================================== ** Time/Date operations ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, ** wday=%w+1, yday=%j, isdst=? } ** ======================================================= */ /* ** About the overflow check: an overflow cannot occur when time ** is represented by a lua_Integer, because either lua_Integer is ** large enough to represent all int fields or it is not large enough ** to represent a time that cause a field to overflow. However, if ** times are represented as doubles and lua_Integer is int, then the ** time 0x1.e1853b0d184f6p+55 would cause an overflow when adding 1900 ** to compute the year. */ static void setfield (lua_State *L, const char *key, int value, int delta) { #if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX) if (l_unlikely(value > LUA_MAXINTEGER - delta)) luaL_error(L, "field '%s' is out-of-bound", key); #endif lua_pushinteger(L, (lua_Integer)value + delta); lua_setfield(L, -2, key); } static void setboolfield (lua_State *L, const char *key, int value) { if (value < 0) /* undefined? */ return; /* does not set field */ lua_pushboolean(L, value); lua_setfield(L, -2, key); } /* ** Set all fields from structure 'tm' in the table on top of the stack */ static void setallfields (lua_State *L, struct tm *stm) { setfield(L, "year", stm->tm_year, 1900); setfield(L, "month", stm->tm_mon, 1); setfield(L, "day", stm->tm_mday, 0); setfield(L, "hour", stm->tm_hour, 0); setfield(L, "min", stm->tm_min, 0); setfield(L, "sec", stm->tm_sec, 0); setfield(L, "yday", stm->tm_yday, 1); setfield(L, "wday", stm->tm_wday, 1); setboolfield(L, "isdst", stm->tm_isdst); } static int getboolfield (lua_State *L, const char *key) { int res; res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1); lua_pop(L, 1); return res; } static int getfield (lua_State *L, const char *key, int d, int delta) { int isnum; int t = lua_getfield(L, -1, key); /* get field and its type */ lua_Integer res = lua_tointegerx(L, -1, &isnum); if (!isnum) { /* field is not an integer? */ if (l_unlikely(t != LUA_TNIL)) /* some other value? */ return luaL_error(L, "field '%s' is not an integer", key); else if (l_unlikely(d < 0)) /* absent field; no default? */ return luaL_error(L, "field '%s' missing in date table", key); res = d; } else { if (!(res >= 0 ? res - delta <= INT_MAX : INT_MIN + delta <= res)) return luaL_error(L, "field '%s' is out-of-bound", key); res -= delta; } lua_pop(L, 1); return (int)res; } static const char *checkoption (lua_State *L, const char *conv, size_t convlen, char *buff) { const char *option = LUA_STRFTIMEOPTIONS; unsigned oplen = 1; /* length of options being checked */ for (; *option != '\0' && oplen <= convlen; option += oplen) { if (*option == '|') /* next block? */ oplen++; /* will check options with next length (+1) */ else if (memcmp(conv, option, oplen) == 0) { /* match? */ memcpy(buff, conv, oplen); /* copy valid option to buffer */ buff[oplen] = '\0'; return conv + oplen; /* return next item */ } } luaL_argerror(L, 1, lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv)); return conv; /* to avoid warnings */ } static time_t l_checktime (lua_State *L, int arg) { l_timet t = l_gettime(L, arg); luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds"); return (time_t)t; } /* maximum size for an individual 'strftime' item */ #define SIZETIMEFMT 250 static int os_date (lua_State *L) { size_t slen; const char *s = luaL_optlstring(L, 1, "%c", &slen); time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); const char *se = s + slen; /* 's' end */ struct tm tmr, *stm; if (*s == '!') { /* UTC? */ stm = l_gmtime(&t, &tmr); s++; /* skip '!' */ } else stm = l_localtime(&t, &tmr); if (stm == NULL) /* invalid date? */ return luaL_error(L, "date result cannot be represented in this installation"); if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setallfields(L, stm); } else { char cc[4]; /* buffer for individual conversion specifiers */ luaL_Buffer b; cc[0] = '%'; luaL_buffinit(L, &b); while (s < se) { if (*s != '%') /* not a conversion specifier? */ luaL_addchar(&b, *s++); else { size_t reslen; char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT); s++; /* skip '%' */ /* copy specifier to 'cc' */ s = checkoption(L, s, ct_diff2sz(se - s), cc + 1); reslen = strftime(buff, SIZETIMEFMT, cc, stm); luaL_addsize(&b, reslen); } } luaL_pushresult(&b); } return 1; } static int os_time (lua_State *L) { time_t t; if (lua_isnoneornil(L, 1)) /* called without args? */ t = time(NULL); /* get current time */ else { struct tm ts; luaL_checktype(L, 1, LUA_TTABLE); lua_settop(L, 1); /* make sure table is at the top */ ts.tm_year = getfield(L, "year", -1, 1900); ts.tm_mon = getfield(L, "month", -1, 1); ts.tm_mday = getfield(L, "day", -1, 0); ts.tm_hour = getfield(L, "hour", 12, 0); ts.tm_min = getfield(L, "min", 0, 0); ts.tm_sec = getfield(L, "sec", 0, 0); ts.tm_isdst = getboolfield(L, "isdst"); t = mktime(&ts); setallfields(L, &ts); /* update fields with normalized values */ } if (t != (time_t)(l_timet)t || t == (time_t)(-1)) return luaL_error(L, "time result cannot be represented in this installation"); l_pushtime(L, t); return 1; } static int os_difftime (lua_State *L) { time_t t1 = l_checktime(L, 1); time_t t2 = l_checktime(L, 2); lua_pushnumber(L, (lua_Number)difftime(t1, t2)); return 1; } /* }====================================================== */ static int os_setlocale (lua_State *L) { static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME}; static const char *const catnames[] = {"all", "collate", "ctype", "monetary", "numeric", "time", NULL}; const char *l = luaL_optstring(L, 1, NULL); int op = luaL_checkoption(L, 2, "all", catnames); lua_pushstring(L, setlocale(cat[op], l)); return 1; } static int os_exit (lua_State *L) { int status; if (lua_isboolean(L, 1)) status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE); else status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS); if (lua_toboolean(L, 2)) lua_close(L); if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */ return 0; } static const luaL_Reg syslib[] = { {"clock", os_clock}, {"date", os_date}, {"difftime", os_difftime}, {"execute", os_execute}, {"exit", os_exit}, {"getenv", os_getenv}, {"remove", os_remove}, {"rename", os_rename}, {"setlocale", os_setlocale}, {"time", os_time}, {"tmpname", os_tmpname}, {NULL, NULL} }; /* }====================================================== */ LUAMOD_API int luaopen_os (lua_State *L) { luaL_newlib(L, syslib); return 1; } ================================================ FILE: 3rd/lua/lparser.c ================================================ /* ** $Id: lparser.c $ ** Lua Parser ** See Copyright Notice in lua.h */ #define lparser_c #define LUA_CORE #include "lprefix.h" #include #include #include "lua.h" #include "lcode.h" #include "ldebug.h" #include "ldo.h" #include "lfunc.h" #include "llex.h" #include "lmem.h" #include "lobject.h" #include "lopcodes.h" #include "lparser.h" #include "lstate.h" #include "lstring.h" #include "ltable.h" /* maximum number of variable declarations per function (must be smaller than 250, due to the bytecode format) */ #define MAXVARS 200 #define hasmultret(k) ((k) == VCALL || (k) == VVARARG) /* because all strings are unified by the scanner, the parser can use pointer equality for string equality */ #define eqstr(a,b) ((a) == (b)) /* ** nodes for block list (list of active blocks) */ typedef struct BlockCnt { struct BlockCnt *previous; /* chain */ int firstlabel; /* index of first label in this block */ int firstgoto; /* index of first pending goto in this block */ short nactvar; /* number of active declarations at block entry */ lu_byte upval; /* true if some variable in the block is an upvalue */ lu_byte isloop; /* 1 if 'block' is a loop; 2 if it has pending breaks */ lu_byte insidetbc; /* true if inside the scope of a to-be-closed var. */ } BlockCnt; /* ** prototypes for recursive non-terminal functions */ static void statement (LexState *ls); static void expr (LexState *ls, expdesc *v); static l_noret error_expected (LexState *ls, int token) { luaX_syntaxerror(ls, luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token))); } static l_noret errorlimit (FuncState *fs, int limit, const char *what) { lua_State *L = fs->ls->L; const char *msg; int line = fs->f->linedefined; const char *where = (line == 0) ? "main function" : luaO_pushfstring(L, "function at line %d", line); msg = luaO_pushfstring(L, "too many %s (limit is %d) in %s", what, limit, where); luaX_syntaxerror(fs->ls, msg); } void luaY_checklimit (FuncState *fs, int v, int l, const char *what) { if (l_unlikely(v > l)) errorlimit(fs, l, what); } /* ** Test whether next token is 'c'; if so, skip it. */ static int testnext (LexState *ls, int c) { if (ls->t.token == c) { luaX_next(ls); return 1; } else return 0; } /* ** Check that next token is 'c'. */ static void check (LexState *ls, int c) { if (ls->t.token != c) error_expected(ls, c); } /* ** Check that next token is 'c' and skip it. */ static void checknext (LexState *ls, int c) { check(ls, c); luaX_next(ls); } #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } /* ** Check that next token is 'what' and skip it. In case of error, ** raise an error that the expected 'what' should match a 'who' ** in line 'where' (if that is not the current line). */ static void check_match (LexState *ls, int what, int who, int where) { if (l_unlikely(!testnext(ls, what))) { if (where == ls->linenumber) /* all in the same line? */ error_expected(ls, what); /* do not need a complex message */ else { luaX_syntaxerror(ls, luaO_pushfstring(ls->L, "%s expected (to close %s at line %d)", luaX_token2str(ls, what), luaX_token2str(ls, who), where)); } } } static TString *str_checkname (LexState *ls) { TString *ts; check(ls, TK_NAME); ts = ls->t.seminfo.ts; luaX_next(ls); return ts; } static void init_exp (expdesc *e, expkind k, int i) { e->f = e->t = NO_JUMP; e->k = k; e->u.info = i; } static void codestring (expdesc *e, TString *s) { e->f = e->t = NO_JUMP; e->k = VKSTR; e->u.strval = s; } static void codename (LexState *ls, expdesc *e) { codestring(e, str_checkname(ls)); } /* ** Register a new local variable in the active 'Proto' (for debug ** information). */ static short registerlocalvar (LexState *ls, FuncState *fs, TString *varname) { Proto *f = fs->f; int oldsize = f->sizelocvars; luaM_growvector(ls->L, f->locvars, fs->ndebugvars, f->sizelocvars, LocVar, SHRT_MAX, "local variables"); while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; f->locvars[fs->ndebugvars].varname = varname; f->locvars[fs->ndebugvars].startpc = fs->pc; luaC_objbarrier(ls->L, f, varname); return fs->ndebugvars++; } /* ** Create a new variable with the given 'name' and given 'kind'. ** Return its index in the function. */ static int new_varkind (LexState *ls, TString *name, lu_byte kind) { lua_State *L = ls->L; FuncState *fs = ls->fs; Dyndata *dyd = ls->dyd; Vardesc *var; luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, dyd->actvar.size, Vardesc, SHRT_MAX, "variable declarations"); var = &dyd->actvar.arr[dyd->actvar.n++]; var->vd.kind = kind; /* default */ var->vd.name = name; return dyd->actvar.n - 1 - fs->firstlocal; } /* ** Create a new local variable with the given 'name' and regular kind. */ static int new_localvar (LexState *ls, TString *name) { return new_varkind(ls, name, VDKREG); } #define new_localvarliteral(ls,v) \ new_localvar(ls, \ luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1)); /* ** Return the "variable description" (Vardesc) of a given variable. ** (Unless noted otherwise, all variables are referred to by their ** compiler indices.) */ static Vardesc *getlocalvardesc (FuncState *fs, int vidx) { return &fs->ls->dyd->actvar.arr[fs->firstlocal + vidx]; } /* ** Convert 'nvar', a compiler index level, to its corresponding ** register. For that, search for the highest variable below that level ** that is in a register and uses its register index ('ridx') plus one. */ static lu_byte reglevel (FuncState *fs, int nvar) { while (nvar-- > 0) { Vardesc *vd = getlocalvardesc(fs, nvar); /* get previous variable */ if (varinreg(vd)) /* is in a register? */ return cast_byte(vd->vd.ridx + 1); } return 0; /* no variables in registers */ } /* ** Return the number of variables in the register stack for the given ** function. */ lu_byte luaY_nvarstack (FuncState *fs) { return reglevel(fs, fs->nactvar); } /* ** Get the debug-information entry for current variable 'vidx'. */ static LocVar *localdebuginfo (FuncState *fs, int vidx) { Vardesc *vd = getlocalvardesc(fs, vidx); if (!varinreg(vd)) return NULL; /* no debug info. for constants */ else { int idx = vd->vd.pidx; lua_assert(idx < fs->ndebugvars); return &fs->f->locvars[idx]; } } /* ** Create an expression representing variable 'vidx' */ static void init_var (FuncState *fs, expdesc *e, int vidx) { e->f = e->t = NO_JUMP; e->k = VLOCAL; e->u.var.vidx = cast_short(vidx); e->u.var.ridx = getlocalvardesc(fs, vidx)->vd.ridx; } /* ** Raises an error if variable described by 'e' is read only; moreover, ** if 'e' is t[exp] where t is the vararg parameter, change it to index ** a real table. (Virtual vararg tables cannot be changed.) */ static void check_readonly (LexState *ls, expdesc *e) { FuncState *fs = ls->fs; TString *varname = NULL; /* to be set if variable is const */ switch (e->k) { case VCONST: { varname = ls->dyd->actvar.arr[e->u.info].vd.name; break; } case VLOCAL: case VVARGVAR: { Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx); if (vardesc->vd.kind != VDKREG) /* not a regular variable? */ varname = vardesc->vd.name; break; } case VUPVAL: { Upvaldesc *up = &fs->f->upvalues[e->u.info]; if (up->kind != VDKREG) varname = up->name; break; } case VVARGIND: { needvatab(fs->f); /* function will need a vararg table */ e->k = VINDEXED; } /* FALLTHROUGH */ case VINDEXUP: case VINDEXSTR: case VINDEXED: { /* global variable */ if (e->u.ind.ro) /* read-only? */ varname = tsvalue(&fs->f->k[e->u.ind.keystr]); break; } default: lua_assert(e->k == VINDEXI); /* this one doesn't need any check */ return; /* integer index cannot be read-only */ } if (varname) luaK_semerror(ls, "attempt to assign to const variable '%s'", getstr(varname)); } /* ** Start the scope for the last 'nvars' created variables. */ static void adjustlocalvars (LexState *ls, int nvars) { FuncState *fs = ls->fs; int reglevel = luaY_nvarstack(fs); int i; for (i = 0; i < nvars; i++) { int vidx = fs->nactvar++; Vardesc *var = getlocalvardesc(fs, vidx); var->vd.ridx = cast_byte(reglevel++); var->vd.pidx = registerlocalvar(ls, fs, var->vd.name); luaY_checklimit(fs, reglevel, MAXVARS, "local variables"); } } /* ** Close the scope for all variables up to level 'tolevel'. ** (debug info.) */ static void removevars (FuncState *fs, int tolevel) { fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel); while (fs->nactvar > tolevel) { LocVar *var = localdebuginfo(fs, --fs->nactvar); if (var) /* does it have debug information? */ var->endpc = fs->pc; } } /* ** Search the upvalues of the function 'fs' for one ** with the given 'name'. */ static int searchupvalue (FuncState *fs, TString *name) { int i; Upvaldesc *up = fs->f->upvalues; for (i = 0; i < fs->nups; i++) { if (eqstr(up[i].name, name)) return i; } return -1; /* not found */ } static Upvaldesc *allocupvalue (FuncState *fs) { Proto *f = fs->f; int oldsize = f->sizeupvalues; luaY_checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, Upvaldesc, MAXUPVAL, "upvalues"); while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL; return &f->upvalues[fs->nups++]; } static int newupvalue (FuncState *fs, TString *name, expdesc *v) { Upvaldesc *up = allocupvalue(fs); FuncState *prev = fs->prev; if (v->k == VLOCAL) { up->instack = 1; up->idx = v->u.var.ridx; up->kind = getlocalvardesc(prev, v->u.var.vidx)->vd.kind; lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->vd.name)); } else { up->instack = 0; up->idx = cast_byte(v->u.info); up->kind = prev->f->upvalues[v->u.info].kind; lua_assert(eqstr(name, prev->f->upvalues[v->u.info].name)); } up->name = name; luaC_objbarrier(fs->ls->L, fs->f, name); return fs->nups - 1; } /* ** Look for an active variable with the name 'n' in the ** function 'fs'. If found, initialize 'var' with it and return ** its expression kind; otherwise return -1. While searching, ** var->u.info==-1 means that the preambular global declaration is ** active (the default while there is no other global declaration); ** var->u.info==-2 means there is no active collective declaration ** (some previous global declaration but no collective declaration); ** and var->u.info>=0 points to the inner-most (the first one found) ** collective declaration, if there is one. */ static int searchvar (FuncState *fs, TString *n, expdesc *var) { int i; for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { Vardesc *vd = getlocalvardesc(fs, i); if (varglobal(vd)) { /* global declaration? */ if (vd->vd.name == NULL) { /* collective declaration? */ if (var->u.info < 0) /* no previous collective declaration? */ var->u.info = fs->firstlocal + i; /* this is the first one */ } else { /* global name */ if (eqstr(n, vd->vd.name)) { /* found? */ init_exp(var, VGLOBAL, fs->firstlocal + i); return VGLOBAL; } else if (var->u.info == -1) /* active preambular declaration? */ var->u.info = -2; /* invalidate preambular declaration */ } } else if (eqstr(n, vd->vd.name)) { /* found? */ if (vd->vd.kind == RDKCTC) /* compile-time constant? */ init_exp(var, VCONST, fs->firstlocal + i); else { /* local variable */ init_var(fs, var, i); if (vd->vd.kind == RDKVAVAR) /* vararg parameter? */ var->k = VVARGVAR; } return cast_int(var->k); } } return -1; /* not found */ } /* ** Mark block where variable at given level was defined ** (to emit close instructions later). */ static void markupval (FuncState *fs, int level) { BlockCnt *bl = fs->bl; while (bl->nactvar > level) bl = bl->previous; bl->upval = 1; fs->needclose = 1; } /* ** Mark that current block has a to-be-closed variable. */ static void marktobeclosed (FuncState *fs) { BlockCnt *bl = fs->bl; bl->upval = 1; bl->insidetbc = 1; fs->needclose = 1; } /* ** Find a variable with the given name 'n'. If it is an upvalue, add ** this upvalue into all intermediate functions. If it is a global, set ** 'var' as 'void' as a flag. */ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { int v = searchvar(fs, n, var); /* look up variables at current level */ if (v >= 0) { /* found? */ if (!base) { if (var->k == VVARGVAR) /* vararg parameter? */ luaK_vapar2local(fs, var); /* change it to a regular local */ if (var->k == VLOCAL) markupval(fs, var->u.var.vidx); /* will be used as an upvalue */ } /* else nothing else to be done */ } else { /* not found at current level; try upvalues */ int idx = searchupvalue(fs, n); /* try existing upvalues */ if (idx < 0) { /* not found? */ if (fs->prev != NULL) /* more levels? */ singlevaraux(fs->prev, n, var, 0); /* try upper levels */ if (var->k == VLOCAL || var->k == VUPVAL) /* local or upvalue? */ idx = newupvalue(fs, n, var); /* will be a new upvalue */ else /* it is a global or a constant */ return; /* don't need to do anything at this level */ } init_exp(var, VUPVAL, idx); /* new or old upvalue */ } } static void buildglobal (LexState *ls, TString *varname, expdesc *var) { FuncState *fs = ls->fs; expdesc key; init_exp(var, VGLOBAL, -1); /* global by default */ singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ if (var->k == VGLOBAL) luaK_semerror(ls, "%s is global when accessing variable '%s'", LUA_ENV, getstr(varname)); luaK_exp2anyregup(fs, var); /* _ENV could be a constant */ codestring(&key, varname); /* key is variable name */ luaK_indexed(fs, var, &key); /* 'var' represents _ENV[varname] */ } /* ** Find a variable with the given name 'n', handling global variables ** too. */ static void buildvar (LexState *ls, TString *varname, expdesc *var) { FuncState *fs = ls->fs; init_exp(var, VGLOBAL, -1); /* global by default */ singlevaraux(fs, varname, var, 1); if (var->k == VGLOBAL) { /* global name? */ int info = var->u.info; /* global by default in the scope of a global declaration? */ if (info == -2) luaK_semerror(ls, "variable '%s' not declared", getstr(varname)); buildglobal(ls, varname, var); if (info != -1 && ls->dyd->actvar.arr[info].vd.kind == GDKCONST) var->u.ind.ro = 1; /* mark variable as read-only */ else /* anyway must be a global */ lua_assert(info == -1 || ls->dyd->actvar.arr[info].vd.kind == GDKREG); } } static void singlevar (LexState *ls, expdesc *var) { buildvar(ls, str_checkname(ls), var); } /* ** Adjust the number of results from an expression list 'e' with 'nexps' ** expressions to 'nvars' values. */ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { FuncState *fs = ls->fs; int needed = nvars - nexps; /* extra values needed */ luaK_checkstack(fs, needed); if (hasmultret(e->k)) { /* last expression has multiple returns? */ int extra = needed + 1; /* discount last expression itself */ if (extra < 0) extra = 0; luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ } else { if (e->k != VVOID) /* at least one expression? */ luaK_exp2nextreg(fs, e); /* close last expression */ if (needed > 0) /* missing values? */ luaK_nil(fs, fs->freereg, needed); /* complete with nils */ } if (needed > 0) luaK_reserveregs(fs, needed); /* registers for extra values */ else /* adding 'needed' is actually a subtraction */ fs->freereg = cast_byte(fs->freereg + needed); /* remove extra values */ } #define enterlevel(ls) luaE_incCstack(ls->L) #define leavelevel(ls) ((ls)->L->nCcalls--) /* ** Generates an error that a goto jumps into the scope of some ** variable declaration. */ static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { TString *tsname = getlocalvardesc(ls->fs, gt->nactvar)->vd.name; const char *varname = (tsname != NULL) ? getstr(tsname) : "*"; luaK_semerror(ls, " at line %d jumps into the scope of '%s'", getstr(gt->name), gt->line, varname); /* raise the error */ } /* ** Closes the goto at index 'g' to given 'label' and removes it ** from the list of pending gotos. ** If it jumps into the scope of some variable, raises an error. ** The goto needs a CLOSE if it jumps out of a block with upvalues, ** or out of the scope of some variable and the block has upvalues ** (signaled by parameter 'bup'). */ static void closegoto (LexState *ls, int g, Labeldesc *label, int bup) { int i; FuncState *fs = ls->fs; Labellist *gl = &ls->dyd->gt; /* list of gotos */ Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ lua_assert(eqstr(gt->name, label->name)); if (l_unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ jumpscopeerror(ls, gt); if (gt->close || (label->nactvar < gt->nactvar && bup)) { /* needs close? */ lu_byte stklevel = reglevel(fs, label->nactvar); /* move jump to CLOSE position */ fs->f->code[gt->pc + 1] = fs->f->code[gt->pc]; /* put CLOSE instruction at original position */ fs->f->code[gt->pc] = CREATE_ABCk(OP_CLOSE, stklevel, 0, 0, 0); gt->pc++; /* must point to jump instruction */ } luaK_patchlist(ls->fs, gt->pc, label->pc); /* goto jumps to label */ for (i = g; i < gl->n - 1; i++) /* remove goto from pending list */ gl->arr[i] = gl->arr[i + 1]; gl->n--; } /* ** Search for an active label with the given name, starting at ** index 'ilb' (so that it can search for all labels in current block ** or all labels in current function). */ static Labeldesc *findlabel (LexState *ls, TString *name, int ilb) { Dyndata *dyd = ls->dyd; for (; ilb < dyd->label.n; ilb++) { Labeldesc *lb = &dyd->label.arr[ilb]; if (eqstr(lb->name, name)) /* correct label? */ return lb; } return NULL; /* label not found */ } /* ** Adds a new label/goto in the corresponding list. */ static int newlabelentry (LexState *ls, Labellist *l, TString *name, int line, int pc) { int n = l->n; luaM_growvector(ls->L, l->arr, n, l->size, Labeldesc, SHRT_MAX, "labels/gotos"); l->arr[n].name = name; l->arr[n].line = line; l->arr[n].nactvar = ls->fs->nactvar; l->arr[n].close = 0; l->arr[n].pc = pc; l->n = n + 1; return n; } /* ** Create an entry for the goto and the code for it. As it is not known ** at this point whether the goto may need a CLOSE, the code has a jump ** followed by an CLOSE. (As the CLOSE comes after the jump, it is a ** dead instruction; it works as a placeholder.) When the goto is closed ** against a label, if it needs a CLOSE, the two instructions swap ** positions, so that the CLOSE comes before the jump. */ static int newgotoentry (LexState *ls, TString *name, int line) { FuncState *fs = ls->fs; int pc = luaK_jump(fs); /* create jump */ luaK_codeABC(fs, OP_CLOSE, 0, 1, 0); /* spaceholder, marked as dead */ return newlabelentry(ls, &ls->dyd->gt, name, line, pc); } /* ** Create a new label with the given 'name' at the given 'line'. ** 'last' tells whether label is the last non-op statement in its ** block. Solves all pending gotos to this new label and adds ** a close instruction if necessary. ** Returns true iff it added a close instruction. */ static void createlabel (LexState *ls, TString *name, int line, int last) { FuncState *fs = ls->fs; Labellist *ll = &ls->dyd->label; int l = newlabelentry(ls, ll, name, line, luaK_getlabel(fs)); if (last) { /* label is last no-op statement in the block? */ /* assume that locals are already out of scope */ ll->arr[l].nactvar = fs->bl->nactvar; } } /* ** Traverse the pending gotos of the finishing block checking whether ** each match some label of that block. Those that do not match are ** "exported" to the outer block, to be solved there. In particular, ** its 'nactvar' is updated with the level of the inner block, ** as the variables of the inner block are now out of scope. */ static void solvegotos (FuncState *fs, BlockCnt *bl) { LexState *ls = fs->ls; Labellist *gl = &ls->dyd->gt; int outlevel = reglevel(fs, bl->nactvar); /* level outside the block */ int igt = bl->firstgoto; /* first goto in the finishing block */ while (igt < gl->n) { /* for each pending goto */ Labeldesc *gt = &gl->arr[igt]; /* search for a matching label in the current block */ Labeldesc *lb = findlabel(ls, gt->name, bl->firstlabel); if (lb != NULL) /* found a match? */ closegoto(ls, igt, lb, bl->upval); /* close and remove goto */ else { /* adjust 'goto' for outer block */ /* block has variables to be closed and goto escapes the scope of some variable? */ if (bl->upval && reglevel(fs, gt->nactvar) > outlevel) gt->close = 1; /* jump may need a close */ gt->nactvar = bl->nactvar; /* correct level for outer block */ igt++; /* go to next goto */ } } ls->dyd->label.n = bl->firstlabel; /* remove local labels */ } static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { bl->isloop = isloop; bl->nactvar = fs->nactvar; bl->firstlabel = fs->ls->dyd->label.n; bl->firstgoto = fs->ls->dyd->gt.n; bl->upval = 0; /* inherit 'insidetbc' from enclosing block */ bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc); bl->previous = fs->bl; /* link block in function's block list */ fs->bl = bl; lua_assert(fs->freereg == luaY_nvarstack(fs)); } /* ** generates an error for an undefined 'goto'. */ static l_noret undefgoto (LexState *ls, Labeldesc *gt) { /* breaks are checked when created, cannot be undefined */ lua_assert(!eqstr(gt->name, ls->brkn)); luaK_semerror(ls, "no visible label '%s' for at line %d", getstr(gt->name), gt->line); } static void leaveblock (FuncState *fs) { BlockCnt *bl = fs->bl; LexState *ls = fs->ls; lu_byte stklevel = reglevel(fs, bl->nactvar); /* level outside block */ if (bl->previous && bl->upval) /* need a 'close'? */ luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0); fs->freereg = stklevel; /* free registers */ removevars(fs, bl->nactvar); /* remove block locals */ lua_assert(bl->nactvar == fs->nactvar); /* back to level on entry */ if (bl->isloop == 2) /* has to fix pending breaks? */ createlabel(ls, ls->brkn, 0, 0); solvegotos(fs, bl); if (bl->previous == NULL) { /* was it the last block? */ if (bl->firstgoto < ls->dyd->gt.n) /* still pending gotos? */ undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ } fs->bl = bl->previous; /* current block now is previous one */ } /* ** adds a new prototype into list of prototypes */ static Proto *addprototype (LexState *ls) { Proto *clp; lua_State *L = ls->L; FuncState *fs = ls->fs; Proto *f = fs->f; /* prototype of current function */ if (fs->np >= f->sizep) { int oldsize = f->sizep; luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions"); while (oldsize < f->sizep) f->p[oldsize++] = NULL; } f->p[fs->np++] = clp = luaF_newproto(L); luaC_objbarrier(L, f, clp); return clp; } /* ** codes instruction to create new closure in parent function. ** The OP_CLOSURE instruction uses the last available register, ** so that, if it invokes the GC, the GC knows which registers ** are in use at that time. */ static void codeclosure (LexState *ls, expdesc *v) { FuncState *fs = ls->fs->prev; init_exp(v, VRELOC, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1)); luaK_exp2nextreg(fs, v); /* fix it at the last register */ } static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { lua_State *L = ls->L; Proto *f = fs->f; fs->prev = ls->fs; /* linked list of funcstates */ fs->ls = ls; ls->fs = fs; fs->pc = 0; fs->previousline = f->linedefined; fs->iwthabs = 0; fs->lasttarget = 0; fs->freereg = 0; fs->nk = 0; fs->nabslineinfo = 0; fs->np = 0; fs->nups = 0; fs->ndebugvars = 0; fs->nactvar = 0; fs->needclose = 0; fs->firstlocal = ls->dyd->actvar.n; fs->firstlabel = ls->dyd->label.n; fs->bl = NULL; f->source = ls->source; luaC_objbarrier(L, f, f->source); f->maxstacksize = 2; /* registers 0/1 are always valid */ fs->kcache = luaH_new(L); /* create table for function */ sethvalue2s(L, L->top.p, fs->kcache); /* anchor it */ luaD_inctop(L); enterblock(fs, bl, 0); } static void close_func (LexState *ls) { lua_State *L = ls->L; FuncState *fs = ls->fs; Proto *f = fs->f; luaK_ret(fs, luaY_nvarstack(fs), 0); /* final return */ leaveblock(fs); lua_assert(fs->bl == NULL); luaK_finish(fs); luaM_shrinkvector(L, f->code, f->sizecode, fs->pc, Instruction); luaM_shrinkvector(L, f->lineinfo, f->sizelineinfo, fs->pc, ls_byte); luaM_shrinkvector(L, f->abslineinfo, f->sizeabslineinfo, fs->nabslineinfo, AbsLineInfo); luaM_shrinkvector(L, f->k, f->sizek, fs->nk, TValue); luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *); luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar); luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); ls->fs = fs->prev; L->top.p--; /* pop kcache table */ luaC_checkGC(L); } /* ** {====================================================================== ** GRAMMAR RULES ** ======================================================================= */ /* ** check whether current token is in the follow set of a block. ** 'until' closes syntactical blocks, but do not close scope, ** so it is handled in separate. */ static int block_follow (LexState *ls, int withuntil) { switch (ls->t.token) { case TK_ELSE: case TK_ELSEIF: case TK_END: case TK_EOS: return 1; case TK_UNTIL: return withuntil; default: return 0; } } static void statlist (LexState *ls) { /* statlist -> { stat [';'] } */ while (!block_follow(ls, 1)) { if (ls->t.token == TK_RETURN) { statement(ls); return; /* 'return' must be last statement */ } statement(ls); } } static void fieldsel (LexState *ls, expdesc *v) { /* fieldsel -> ['.' | ':'] NAME */ FuncState *fs = ls->fs; expdesc key; luaK_exp2anyregup(fs, v); luaX_next(ls); /* skip the dot or colon */ codename(ls, &key); luaK_indexed(fs, v, &key); } static void yindex (LexState *ls, expdesc *v) { /* index -> '[' expr ']' */ luaX_next(ls); /* skip the '[' */ expr(ls, v); luaK_exp2val(ls->fs, v); checknext(ls, ']'); } /* ** {====================================================================== ** Rules for Constructors ** ======================================================================= */ typedef struct ConsControl { expdesc v; /* last list item read */ expdesc *t; /* table descriptor */ int nh; /* total number of 'record' elements */ int na; /* number of array elements already stored */ int tostore; /* number of array elements pending to be stored */ int maxtostore; /* maximum number of pending elements */ } ConsControl; /* ** Maximum number of elements in a constructor, to control the following: ** * counter overflows; ** * overflows in 'extra' for OP_NEWTABLE and OP_SETLIST; ** * overflows when adding multiple returns in OP_SETLIST. */ #define MAX_CNST (INT_MAX/2) #if MAX_CNST/(MAXARG_vC + 1) > MAXARG_Ax #undef MAX_CNST #define MAX_CNST (MAXARG_Ax * (MAXARG_vC + 1)) #endif static void recfield (LexState *ls, ConsControl *cc) { /* recfield -> (NAME | '['exp']') = exp */ FuncState *fs = ls->fs; lu_byte reg = ls->fs->freereg; expdesc tab, key, val; if (ls->t.token == TK_NAME) codename(ls, &key); else /* ls->t.token == '[' */ yindex(ls, &key); cc->nh++; checknext(ls, '='); tab = *cc->t; luaK_indexed(fs, &tab, &key); expr(ls, &val); luaK_storevar(fs, &tab, &val); fs->freereg = reg; /* free registers */ } static void closelistfield (FuncState *fs, ConsControl *cc) { lua_assert(cc->tostore > 0); luaK_exp2nextreg(fs, &cc->v); cc->v.k = VVOID; if (cc->tostore >= cc->maxtostore) { luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */ cc->na += cc->tostore; cc->tostore = 0; /* no more items pending */ } } static void lastlistfield (FuncState *fs, ConsControl *cc) { if (cc->tostore == 0) return; if (hasmultret(cc->v.k)) { luaK_setmultret(fs, &cc->v); luaK_setlist(fs, cc->t->u.info, cc->na, LUA_MULTRET); cc->na--; /* do not count last expression (unknown number of elements) */ } else { if (cc->v.k != VVOID) luaK_exp2nextreg(fs, &cc->v); luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); } cc->na += cc->tostore; } static void listfield (LexState *ls, ConsControl *cc) { /* listfield -> exp */ expr(ls, &cc->v); cc->tostore++; } static void field (LexState *ls, ConsControl *cc) { /* field -> listfield | recfield */ switch(ls->t.token) { case TK_NAME: { /* may be 'listfield' or 'recfield' */ if (luaX_lookahead(ls) != '=') /* expression? */ listfield(ls, cc); else recfield(ls, cc); break; } case '[': { recfield(ls, cc); break; } default: { listfield(ls, cc); break; } } } /* ** Compute a limit for how many registers a constructor can use before ** emitting a 'SETLIST' instruction, based on how many registers are ** available. */ static int maxtostore (FuncState *fs) { int numfreeregs = MAX_FSTACK - fs->freereg; if (numfreeregs >= 160) /* "lots" of registers? */ return numfreeregs / 5; /* use up to 1/5 of them */ else if (numfreeregs >= 80) /* still "enough" registers? */ return 10; /* one 'SETLIST' instruction for each 10 values */ else /* save registers for potential more nesting */ return 1; } static void constructor (LexState *ls, expdesc *t) { /* constructor -> '{' [ field { sep field } [sep] ] '}' sep -> ',' | ';' */ FuncState *fs = ls->fs; int line = ls->linenumber; int pc = luaK_codevABCk(fs, OP_NEWTABLE, 0, 0, 0, 0); ConsControl cc; luaK_code(fs, 0); /* space for extra arg. */ cc.na = cc.nh = cc.tostore = 0; cc.t = t; init_exp(t, VNONRELOC, fs->freereg); /* table will be at stack top */ luaK_reserveregs(fs, 1); init_exp(&cc.v, VVOID, 0); /* no value (yet) */ checknext(ls, '{' /*}*/); cc.maxtostore = maxtostore(fs); do { if (ls->t.token == /*{*/ '}') break; if (cc.v.k != VVOID) /* is there a previous list item? */ closelistfield(fs, &cc); /* close it */ field(ls, &cc); luaY_checklimit(fs, cc.tostore + cc.na + cc.nh, MAX_CNST, "items in a constructor"); } while (testnext(ls, ',') || testnext(ls, ';')); check_match(ls, /*{*/ '}', '{' /*}*/, line); lastlistfield(fs, &cc); luaK_settablesize(fs, pc, t->u.info, cc.na, cc.nh); } /* }====================================================================== */ static void setvararg (FuncState *fs) { fs->f->flag |= PF_VAHID; /* by default, use hidden vararg arguments */ luaK_codeABC(fs, OP_VARARGPREP, 0, 0, 0); } static void parlist (LexState *ls) { /* parlist -> [ {NAME ','} (NAME | '...') ] */ FuncState *fs = ls->fs; Proto *f = fs->f; int nparams = 0; int varargk = 0; if (ls->t.token != ')') { /* is 'parlist' not empty? */ do { switch (ls->t.token) { case TK_NAME: { new_localvar(ls, str_checkname(ls)); nparams++; break; } case TK_DOTS: { varargk = 1; luaX_next(ls); /* skip '...' */ if (ls->t.token == TK_NAME) new_varkind(ls, str_checkname(ls), RDKVAVAR); else new_localvarliteral(ls, "(vararg table)"); break; } default: luaX_syntaxerror(ls, " or '...' expected"); } } while (!varargk && testnext(ls, ',')); } adjustlocalvars(ls, nparams); f->numparams = cast_byte(fs->nactvar); if (varargk) { setvararg(fs); /* declared vararg */ adjustlocalvars(ls, 1); /* vararg parameter */ } /* reserve registers for parameters (plus vararg parameter, if present) */ luaK_reserveregs(fs, fs->nactvar); } static void body (LexState *ls, expdesc *e, int ismethod, int line) { /* body -> '(' parlist ')' block END */ FuncState new_fs; BlockCnt bl; new_fs.f = addprototype(ls); new_fs.f->linedefined = line; open_func(ls, &new_fs, &bl); checknext(ls, '('); if (ismethod) { new_localvarliteral(ls, "self"); /* create 'self' parameter */ adjustlocalvars(ls, 1); } parlist(ls); checknext(ls, ')'); statlist(ls); new_fs.f->lastlinedefined = ls->linenumber; check_match(ls, TK_END, TK_FUNCTION, line); codeclosure(ls, e); close_func(ls); } static int explist (LexState *ls, expdesc *v) { /* explist -> expr { ',' expr } */ int n = 1; /* at least one expression */ expr(ls, v); while (testnext(ls, ',')) { luaK_exp2nextreg(ls->fs, v); expr(ls, v); n++; } return n; } static void funcargs (LexState *ls, expdesc *f) { FuncState *fs = ls->fs; expdesc args; int base, nparams; int line = ls->linenumber; switch (ls->t.token) { case '(': { /* funcargs -> '(' [ explist ] ')' */ luaX_next(ls); if (ls->t.token == ')') /* arg list is empty? */ args.k = VVOID; else { explist(ls, &args); if (hasmultret(args.k)) luaK_setmultret(fs, &args); } check_match(ls, ')', '(', line); break; } case '{' /*}*/: { /* funcargs -> constructor */ constructor(ls, &args); break; } case TK_STRING: { /* funcargs -> STRING */ codestring(&args, ls->t.seminfo.ts); luaX_next(ls); /* must use 'seminfo' before 'next' */ break; } default: { luaX_syntaxerror(ls, "function arguments expected"); } } lua_assert(f->k == VNONRELOC); base = f->u.info; /* base register for call */ if (hasmultret(args.k)) nparams = LUA_MULTRET; /* open call */ else { if (args.k != VVOID) luaK_exp2nextreg(fs, &args); /* close last argument */ nparams = fs->freereg - (base+1); } init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); luaK_fixline(fs, line); /* call removes function and arguments and leaves one result (unless changed later) */ fs->freereg = cast_byte(base + 1); } /* ** {====================================================================== ** Expression parsing ** ======================================================================= */ static void primaryexp (LexState *ls, expdesc *v) { /* primaryexp -> NAME | '(' expr ')' */ switch (ls->t.token) { case '(': { int line = ls->linenumber; luaX_next(ls); expr(ls, v); check_match(ls, ')', '(', line); luaK_dischargevars(ls->fs, v); return; } case TK_NAME: { singlevar(ls, v); return; } default: { luaX_syntaxerror(ls, "unexpected symbol"); } } } static void suffixedexp (LexState *ls, expdesc *v) { /* suffixedexp -> primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */ FuncState *fs = ls->fs; primaryexp(ls, v); for (;;) { switch (ls->t.token) { case '.': { /* fieldsel */ fieldsel(ls, v); break; } case '[': { /* '[' exp ']' */ expdesc key; luaK_exp2anyregup(fs, v); yindex(ls, &key); luaK_indexed(fs, v, &key); break; } case ':': { /* ':' NAME funcargs */ expdesc key; luaX_next(ls); codename(ls, &key); luaK_self(fs, v, &key); funcargs(ls, v); break; } case '(': case TK_STRING: case '{' /*}*/: { /* funcargs */ luaK_exp2nextreg(fs, v); funcargs(ls, v); break; } default: return; } } } static void simpleexp (LexState *ls, expdesc *v) { /* simpleexp -> FLT | INT | STRING | NIL | TRUE | FALSE | ... | constructor | FUNCTION body | suffixedexp */ switch (ls->t.token) { case TK_FLT: { init_exp(v, VKFLT, 0); v->u.nval = ls->t.seminfo.r; break; } case TK_INT: { init_exp(v, VKINT, 0); v->u.ival = ls->t.seminfo.i; break; } case TK_STRING: { codestring(v, ls->t.seminfo.ts); break; } case TK_NIL: { init_exp(v, VNIL, 0); break; } case TK_TRUE: { init_exp(v, VTRUE, 0); break; } case TK_FALSE: { init_exp(v, VFALSE, 0); break; } case TK_DOTS: { /* vararg */ FuncState *fs = ls->fs; check_condition(ls, isvararg(fs->f), "cannot use '...' outside a vararg function"); init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, fs->f->numparams, 1)); break; } case '{' /*}*/: { /* constructor */ constructor(ls, v); return; } case TK_FUNCTION: { luaX_next(ls); body(ls, v, 0, ls->linenumber); return; } default: { suffixedexp(ls, v); return; } } luaX_next(ls); } static UnOpr getunopr (int op) { switch (op) { case TK_NOT: return OPR_NOT; case '-': return OPR_MINUS; case '~': return OPR_BNOT; case '#': return OPR_LEN; default: return OPR_NOUNOPR; } } static BinOpr getbinopr (int op) { switch (op) { case '+': return OPR_ADD; case '-': return OPR_SUB; case '*': return OPR_MUL; case '%': return OPR_MOD; case '^': return OPR_POW; case '/': return OPR_DIV; case TK_IDIV: return OPR_IDIV; case '&': return OPR_BAND; case '|': return OPR_BOR; case '~': return OPR_BXOR; case TK_SHL: return OPR_SHL; case TK_SHR: return OPR_SHR; case TK_CONCAT: return OPR_CONCAT; case TK_NE: return OPR_NE; case TK_EQ: return OPR_EQ; case '<': return OPR_LT; case TK_LE: return OPR_LE; case '>': return OPR_GT; case TK_GE: return OPR_GE; case TK_AND: return OPR_AND; case TK_OR: return OPR_OR; default: return OPR_NOBINOPR; } } /* ** Priority table for binary operators. */ static const struct { lu_byte left; /* left priority for each binary operator */ lu_byte right; /* right priority */ } priority[] = { /* ORDER OPR */ {10, 10}, {10, 10}, /* '+' '-' */ {11, 11}, {11, 11}, /* '*' '%' */ {14, 13}, /* '^' (right associative) */ {11, 11}, {11, 11}, /* '/' '//' */ {6, 6}, {4, 4}, {5, 5}, /* '&' '|' '~' */ {7, 7}, {7, 7}, /* '<<' '>>' */ {9, 8}, /* '..' (right associative) */ {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */ {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */ {2, 2}, {1, 1} /* and, or */ }; #define UNARY_PRIORITY 12 /* priority for unary operators */ /* ** subexpr -> (simpleexp | unop subexpr) { binop subexpr } ** where 'binop' is any binary operator with a priority higher than 'limit' */ static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { BinOpr op; UnOpr uop; enterlevel(ls); uop = getunopr(ls->t.token); if (uop != OPR_NOUNOPR) { /* prefix (unary) operator? */ int line = ls->linenumber; luaX_next(ls); /* skip operator */ subexpr(ls, v, UNARY_PRIORITY); luaK_prefix(ls->fs, uop, v, line); } else simpleexp(ls, v); /* expand while operators have priorities higher than 'limit' */ op = getbinopr(ls->t.token); while (op != OPR_NOBINOPR && priority[op].left > limit) { expdesc v2; BinOpr nextop; int line = ls->linenumber; luaX_next(ls); /* skip operator */ luaK_infix(ls->fs, op, v); /* read sub-expression with higher priority */ nextop = subexpr(ls, &v2, priority[op].right); luaK_posfix(ls->fs, op, v, &v2, line); op = nextop; } leavelevel(ls); return op; /* return first untreated operator */ } static void expr (LexState *ls, expdesc *v) { subexpr(ls, v, 0); } /* }==================================================================== */ /* ** {====================================================================== ** Rules for Statements ** ======================================================================= */ static void block (LexState *ls) { /* block -> statlist */ FuncState *fs = ls->fs; BlockCnt bl; enterblock(fs, &bl, 0); statlist(ls); leaveblock(fs); } /* ** structure to chain all variables in the left-hand side of an ** assignment */ struct LHS_assign { struct LHS_assign *prev; expdesc v; /* variable (global, local, upvalue, or indexed) */ }; /* ** check whether, in an assignment to an upvalue/local variable, the ** upvalue/local variable is begin used in a previous assignment to a ** table. If so, save original upvalue/local value in a safe place and ** use this safe copy in the previous assignment. */ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { FuncState *fs = ls->fs; lu_byte extra = fs->freereg; /* eventual position to save local variable */ int conflict = 0; for (; lh; lh = lh->prev) { /* check all previous assignments */ if (vkisindexed(lh->v.k)) { /* assignment to table field? */ if (lh->v.k == VINDEXUP) { /* is table an upvalue? */ if (v->k == VUPVAL && lh->v.u.ind.t == v->u.info) { conflict = 1; /* table is the upvalue being assigned now */ lh->v.k = VINDEXSTR; lh->v.u.ind.t = extra; /* assignment will use safe copy */ } } else { /* table is a register */ if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.ridx) { conflict = 1; /* table is the local being assigned now */ lh->v.u.ind.t = extra; /* assignment will use safe copy */ } /* is index the local being assigned? */ if (lh->v.k == VINDEXED && v->k == VLOCAL && lh->v.u.ind.idx == v->u.var.ridx) { conflict = 1; lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ } } } } if (conflict) { /* copy upvalue/local value to a temporary (in position 'extra') */ if (v->k == VLOCAL) luaK_codeABC(fs, OP_MOVE, extra, v->u.var.ridx, 0); else luaK_codeABC(fs, OP_GETUPVAL, extra, v->u.info, 0); luaK_reserveregs(fs, 1); } } /* Create code to store the "top" register in 'var' */ static void storevartop (FuncState *fs, expdesc *var) { expdesc e; init_exp(&e, VNONRELOC, fs->freereg - 1); luaK_storevar(fs, var, &e); /* will also free the top register */ } /* ** Parse and compile a multiple assignment. The first "variable" ** (a 'suffixedexp') was already read by the caller. ** ** assignment -> suffixedexp restassign ** restassign -> ',' suffixedexp restassign | '=' explist */ static void restassign (LexState *ls, struct LHS_assign *lh, int nvars) { expdesc e; check_condition(ls, vkisvar(lh->v.k), "syntax error"); check_readonly(ls, &lh->v); if (testnext(ls, ',')) { /* restassign -> ',' suffixedexp restassign */ struct LHS_assign nv; nv.prev = lh; suffixedexp(ls, &nv.v); if (!vkisindexed(nv.v.k)) check_conflict(ls, lh, &nv.v); enterlevel(ls); /* control recursion depth */ restassign(ls, &nv, nvars+1); leavelevel(ls); } else { /* restassign -> '=' explist */ int nexps; checknext(ls, '='); nexps = explist(ls, &e); if (nexps != nvars) adjust_assign(ls, nvars, nexps, &e); else { luaK_setoneret(ls->fs, &e); /* close last expression */ luaK_storevar(ls->fs, &lh->v, &e); return; /* avoid default */ } } storevartop(ls->fs, &lh->v); /* default assignment */ } static int cond (LexState *ls) { /* cond -> exp */ expdesc v; expr(ls, &v); /* read condition */ if (v.k == VNIL) v.k = VFALSE; /* 'falses' are all equal here */ luaK_goiftrue(ls->fs, &v); return v.f; } static void gotostat (LexState *ls, int line) { TString *name = str_checkname(ls); /* label's name */ newgotoentry(ls, name, line); } /* ** Break statement. Semantically equivalent to "goto break". */ static void breakstat (LexState *ls, int line) { BlockCnt *bl; /* to look for an enclosing loop */ for (bl = ls->fs->bl; bl != NULL; bl = bl->previous) { if (bl->isloop) /* found one? */ goto ok; } luaX_syntaxerror(ls, "break outside loop"); ok: bl->isloop = 2; /* signal that block has pending breaks */ luaX_next(ls); /* skip break */ newgotoentry(ls, ls->brkn, line); } /* ** Check whether there is already a label with the given 'name' at ** current function. */ static void checkrepeated (LexState *ls, TString *name) { Labeldesc *lb = findlabel(ls, name, ls->fs->firstlabel); if (l_unlikely(lb != NULL)) /* already defined? */ luaK_semerror(ls, "label '%s' already defined on line %d", getstr(name), lb->line); /* error */ } static void labelstat (LexState *ls, TString *name, int line) { /* label -> '::' NAME '::' */ checknext(ls, TK_DBCOLON); /* skip double colon */ while (ls->t.token == ';' || ls->t.token == TK_DBCOLON) statement(ls); /* skip other no-op statements */ checkrepeated(ls, name); /* check for repeated labels */ createlabel(ls, name, line, block_follow(ls, 0)); } static void whilestat (LexState *ls, int line) { /* whilestat -> WHILE cond DO block END */ FuncState *fs = ls->fs; int whileinit; int condexit; BlockCnt bl; luaX_next(ls); /* skip WHILE */ whileinit = luaK_getlabel(fs); condexit = cond(ls); enterblock(fs, &bl, 1); checknext(ls, TK_DO); block(ls); luaK_jumpto(fs, whileinit); check_match(ls, TK_END, TK_WHILE, line); leaveblock(fs); luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ } static void repeatstat (LexState *ls, int line) { /* repeatstat -> REPEAT block UNTIL cond */ int condexit; FuncState *fs = ls->fs; int repeat_init = luaK_getlabel(fs); BlockCnt bl1, bl2; enterblock(fs, &bl1, 1); /* loop block */ enterblock(fs, &bl2, 0); /* scope block */ luaX_next(ls); /* skip REPEAT */ statlist(ls); check_match(ls, TK_UNTIL, TK_REPEAT, line); condexit = cond(ls); /* read condition (inside scope block) */ leaveblock(fs); /* finish scope */ if (bl2.upval) { /* upvalues? */ int exit = luaK_jump(fs); /* normal exit must jump over fix */ luaK_patchtohere(fs, condexit); /* repetition must close upvalues */ luaK_codeABC(fs, OP_CLOSE, reglevel(fs, bl2.nactvar), 0, 0); condexit = luaK_jump(fs); /* repeat after closing upvalues */ luaK_patchtohere(fs, exit); /* normal exit comes to here */ } luaK_patchlist(fs, condexit, repeat_init); /* close the loop */ leaveblock(fs); /* finish loop */ } /* ** Read an expression and generate code to put its results in next ** stack slot. ** */ static void exp1 (LexState *ls) { expdesc e; expr(ls, &e); luaK_exp2nextreg(ls->fs, &e); lua_assert(e.k == VNONRELOC); } /* ** Fix for instruction at position 'pc' to jump to 'dest'. ** (Jump addresses are relative in Lua). 'back' true means ** a back jump. */ static void fixforjump (FuncState *fs, int pc, int dest, int back) { Instruction *jmp = &fs->f->code[pc]; int offset = dest - (pc + 1); if (back) offset = -offset; if (l_unlikely(offset > MAXARG_Bx)) luaX_syntaxerror(fs->ls, "control structure too long"); SETARG_Bx(*jmp, offset); } /* ** Generate code for a 'for' loop. */ static void forbody (LexState *ls, int base, int line, int nvars, int isgen) { /* forbody -> DO block */ static const OpCode forprep[2] = {OP_FORPREP, OP_TFORPREP}; static const OpCode forloop[2] = {OP_FORLOOP, OP_TFORLOOP}; BlockCnt bl; FuncState *fs = ls->fs; int prep, endfor; checknext(ls, TK_DO); prep = luaK_codeABx(fs, forprep[isgen], base, 0); fs->freereg--; /* both 'forprep' remove one register from the stack */ enterblock(fs, &bl, 0); /* scope for declared variables */ adjustlocalvars(ls, nvars); luaK_reserveregs(fs, nvars); block(ls); leaveblock(fs); /* end of scope for declared variables */ fixforjump(fs, prep, luaK_getlabel(fs), 0); if (isgen) { /* generic for? */ luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); luaK_fixline(fs, line); } endfor = luaK_codeABx(fs, forloop[isgen], base, 0); fixforjump(fs, endfor, prep + 1, 1); luaK_fixline(fs, line); } static void fornum (LexState *ls, TString *varname, int line) { /* fornum -> NAME = exp,exp[,exp] forbody */ FuncState *fs = ls->fs; int base = fs->freereg; new_localvarliteral(ls, "(for state)"); new_localvarliteral(ls, "(for state)"); new_varkind(ls, varname, RDKCONST); /* control variable */ checknext(ls, '='); exp1(ls); /* initial value */ checknext(ls, ','); exp1(ls); /* limit */ if (testnext(ls, ',')) exp1(ls); /* optional step */ else { /* default step = 1 */ luaK_int(fs, fs->freereg, 1); luaK_reserveregs(fs, 1); } adjustlocalvars(ls, 2); /* start scope for internal variables */ forbody(ls, base, line, 1, 0); } static void forlist (LexState *ls, TString *indexname) { /* forlist -> NAME {,NAME} IN explist forbody */ FuncState *fs = ls->fs; expdesc e; int nvars = 4; /* function, state, closing, control */ int line; int base = fs->freereg; /* create internal variables */ new_localvarliteral(ls, "(for state)"); /* iterator function */ new_localvarliteral(ls, "(for state)"); /* state */ new_localvarliteral(ls, "(for state)"); /* closing var. (after swap) */ new_varkind(ls, indexname, RDKCONST); /* control variable */ /* other declared variables */ while (testnext(ls, ',')) { new_localvar(ls, str_checkname(ls)); nvars++; } checknext(ls, TK_IN); line = ls->linenumber; adjust_assign(ls, 4, explist(ls, &e), &e); adjustlocalvars(ls, 3); /* start scope for internal variables */ marktobeclosed(fs); /* last internal var. must be closed */ luaK_checkstack(fs, 2); /* extra space to call iterator */ forbody(ls, base, line, nvars - 3, 1); } static void forstat (LexState *ls, int line) { /* forstat -> FOR (fornum | forlist) END */ FuncState *fs = ls->fs; TString *varname; BlockCnt bl; enterblock(fs, &bl, 1); /* scope for loop and control variables */ luaX_next(ls); /* skip 'for' */ varname = str_checkname(ls); /* first variable name */ switch (ls->t.token) { case '=': fornum(ls, varname, line); break; case ',': case TK_IN: forlist(ls, varname); break; default: luaX_syntaxerror(ls, "'=' or 'in' expected"); } check_match(ls, TK_END, TK_FOR, line); leaveblock(fs); /* loop scope ('break' jumps to this point) */ } static void test_then_block (LexState *ls, int *escapelist) { /* test_then_block -> [IF | ELSEIF] cond THEN block */ FuncState *fs = ls->fs; int condtrue; luaX_next(ls); /* skip IF or ELSEIF */ condtrue = cond(ls); /* read condition */ checknext(ls, TK_THEN); block(ls); /* 'then' part */ if (ls->t.token == TK_ELSE || ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */ luaK_concat(fs, escapelist, luaK_jump(fs)); /* must jump over it */ luaK_patchtohere(fs, condtrue); } static void ifstat (LexState *ls, int line) { /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ FuncState *fs = ls->fs; int escapelist = NO_JUMP; /* exit list for finished parts */ test_then_block(ls, &escapelist); /* IF cond THEN block */ while (ls->t.token == TK_ELSEIF) test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */ if (testnext(ls, TK_ELSE)) block(ls); /* 'else' part */ check_match(ls, TK_END, TK_IF, line); luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */ } static void localfunc (LexState *ls) { expdesc b; FuncState *fs = ls->fs; int fvar = fs->nactvar; /* function's variable index */ new_localvar(ls, str_checkname(ls)); /* new local variable */ adjustlocalvars(ls, 1); /* enter its scope */ body(ls, &b, 0, ls->linenumber); /* function created in next register */ /* debug information will only see the variable after this point! */ localdebuginfo(fs, fvar)->startpc = fs->pc; } static lu_byte getvarattribute (LexState *ls, lu_byte df) { /* attrib -> ['<' NAME '>'] */ if (testnext(ls, '<')) { TString *ts = str_checkname(ls); const char *attr = getstr(ts); checknext(ls, '>'); if (strcmp(attr, "const") == 0) return RDKCONST; /* read-only variable */ else if (strcmp(attr, "close") == 0) return RDKTOCLOSE; /* to-be-closed variable */ else luaK_semerror(ls, "unknown attribute '%s'", attr); } return df; /* return default value */ } static void checktoclose (FuncState *fs, int level) { if (level != -1) { /* is there a to-be-closed variable? */ marktobeclosed(fs); luaK_codeABC(fs, OP_TBC, reglevel(fs, level), 0, 0); } } static void localstat (LexState *ls) { /* stat -> LOCAL NAME attrib { ',' NAME attrib } ['=' explist] */ FuncState *fs = ls->fs; int toclose = -1; /* index of to-be-closed variable (if any) */ Vardesc *var; /* last variable */ int vidx; /* index of last variable */ int nvars = 0; int nexps; expdesc e; /* get prefixed attribute (if any); default is regular local variable */ lu_byte defkind = getvarattribute(ls, VDKREG); do { /* for each variable */ TString *vname = str_checkname(ls); /* get its name */ lu_byte kind = getvarattribute(ls, defkind); /* postfixed attribute */ vidx = new_varkind(ls, vname, kind); /* predeclare it */ if (kind == RDKTOCLOSE) { /* to-be-closed? */ if (toclose != -1) /* one already present? */ luaK_semerror(ls, "multiple to-be-closed variables in local list"); toclose = fs->nactvar + nvars; } nvars++; } while (testnext(ls, ',')); if (testnext(ls, '=')) /* initialization? */ nexps = explist(ls, &e); else { e.k = VVOID; nexps = 0; } var = getlocalvardesc(fs, vidx); /* retrieve last variable */ if (nvars == nexps && /* no adjustments? */ var->vd.kind == RDKCONST && /* last variable is const? */ luaK_exp2const(fs, &e, &var->k)) { /* compile-time constant? */ var->vd.kind = RDKCTC; /* variable is a compile-time constant */ adjustlocalvars(ls, nvars - 1); /* exclude last variable */ fs->nactvar++; /* but count it */ } else { adjust_assign(ls, nvars, nexps, &e); adjustlocalvars(ls, nvars); } checktoclose(fs, toclose); } static lu_byte getglobalattribute (LexState *ls, lu_byte df) { lu_byte kind = getvarattribute(ls, df); switch (kind) { case RDKTOCLOSE: luaK_semerror(ls, "global variables cannot be to-be-closed"); return kind; /* to avoid warnings */ case RDKCONST: return GDKCONST; /* adjust kind for global variable */ default: return kind; } } static void checkglobal (LexState *ls, TString *varname, int line) { FuncState *fs = ls->fs; expdesc var; int k; buildglobal(ls, varname, &var); /* create global variable in 'var' */ k = var.u.ind.keystr; /* index of global name in 'k' */ luaK_codecheckglobal(fs, &var, k, line); } /* ** Recursively traverse list of globals to be initalized. When ** going, generate table description for the global. In the end, ** after all indices have been generated, read list of initializing ** expressions. When returning, generate the assignment of the value on ** the stack to the corresponding table description. 'n' is the variable ** being handled, range [0, nvars - 1]. */ static void initglobal (LexState *ls, int nvars, int firstidx, int n, int line) { if (n == nvars) { /* traversed all variables? */ expdesc e; int nexps = explist(ls, &e); /* read list of expressions */ adjust_assign(ls, nvars, nexps, &e); } else { /* handle variable 'n' */ FuncState *fs = ls->fs; expdesc var; TString *varname = getlocalvardesc(fs, firstidx + n)->vd.name; buildglobal(ls, varname, &var); /* create global variable in 'var' */ enterlevel(ls); /* control recursion depth */ initglobal(ls, nvars, firstidx, n + 1, line); leavelevel(ls); checkglobal(ls, varname, line); storevartop(fs, &var); } } static void globalnames (LexState *ls, lu_byte defkind) { FuncState *fs = ls->fs; int nvars = 0; int lastidx; /* index of last registered variable */ do { /* for each name */ TString *vname = str_checkname(ls); lu_byte kind = getglobalattribute(ls, defkind); lastidx = new_varkind(ls, vname, kind); nvars++; } while (testnext(ls, ',')); if (testnext(ls, '=')) /* initialization? */ initglobal(ls, nvars, lastidx - nvars + 1, 0, ls->linenumber); fs->nactvar = cast_short(fs->nactvar + nvars); /* activate declaration */ } static void globalstat (LexState *ls) { /* globalstat -> (GLOBAL) attrib '*' globalstat -> (GLOBAL) attrib NAME attrib {',' NAME attrib} */ FuncState *fs = ls->fs; /* get prefixed attribute (if any); default is regular global variable */ lu_byte defkind = getglobalattribute(ls, GDKREG); if (!testnext(ls, '*')) globalnames(ls, defkind); else { /* use NULL as name to represent '*' entries */ new_varkind(ls, NULL, defkind); fs->nactvar++; /* activate declaration */ } } static void globalfunc (LexState *ls, int line) { /* globalfunc -> (GLOBAL FUNCTION) NAME body */ expdesc var, b; FuncState *fs = ls->fs; TString *fname = str_checkname(ls); new_varkind(ls, fname, GDKREG); /* declare global variable */ fs->nactvar++; /* enter its scope */ buildglobal(ls, fname, &var); body(ls, &b, 0, ls->linenumber); /* compile and return closure in 'b' */ checkglobal(ls, fname, line); luaK_storevar(fs, &var, &b); luaK_fixline(fs, line); /* definition "happens" in the first line */ } static void globalstatfunc (LexState *ls, int line) { /* stat -> GLOBAL globalfunc | GLOBAL globalstat */ luaX_next(ls); /* skip 'global' */ if (testnext(ls, TK_FUNCTION)) globalfunc(ls, line); else globalstat(ls); } static int funcname (LexState *ls, expdesc *v) { /* funcname -> NAME {fieldsel} [':' NAME] */ int ismethod = 0; singlevar(ls, v); while (ls->t.token == '.') fieldsel(ls, v); if (ls->t.token == ':') { ismethod = 1; fieldsel(ls, v); } return ismethod; } static void funcstat (LexState *ls, int line) { /* funcstat -> FUNCTION funcname body */ int ismethod; expdesc v, b; luaX_next(ls); /* skip FUNCTION */ ismethod = funcname(ls, &v); check_readonly(ls, &v); body(ls, &b, ismethod, line); luaK_storevar(ls->fs, &v, &b); luaK_fixline(ls->fs, line); /* definition "happens" in the first line */ } static void exprstat (LexState *ls) { /* stat -> func | assignment */ FuncState *fs = ls->fs; struct LHS_assign v; suffixedexp(ls, &v.v); if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */ v.prev = NULL; restassign(ls, &v, 1); } else { /* stat -> func */ Instruction *inst; check_condition(ls, v.v.k == VCALL, "syntax error"); inst = &getinstruction(fs, &v.v); SETARG_C(*inst, 1); /* call statement uses no results */ } } static void retstat (LexState *ls) { /* stat -> RETURN [explist] [';'] */ FuncState *fs = ls->fs; expdesc e; int nret; /* number of values being returned */ int first = luaY_nvarstack(fs); /* first slot to be returned */ if (block_follow(ls, 1) || ls->t.token == ';') nret = 0; /* return no values */ else { nret = explist(ls, &e); /* optional return values */ if (hasmultret(e.k)) { luaK_setmultret(fs, &e); if (e.k == VCALL && nret == 1 && !fs->bl->insidetbc) { /* tail call? */ SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL); lua_assert(GETARG_A(getinstruction(fs,&e)) == luaY_nvarstack(fs)); } nret = LUA_MULTRET; /* return all values */ } else { if (nret == 1) /* only one single value? */ first = luaK_exp2anyreg(fs, &e); /* can use original slot */ else { /* values must go to the top of the stack */ luaK_exp2nextreg(fs, &e); lua_assert(nret == fs->freereg - first); } } } luaK_ret(fs, first, nret); testnext(ls, ';'); /* skip optional semicolon */ } static void statement (LexState *ls) { int line = ls->linenumber; /* may be needed for error messages */ enterlevel(ls); switch (ls->t.token) { case ';': { /* stat -> ';' (empty statement) */ luaX_next(ls); /* skip ';' */ break; } case TK_IF: { /* stat -> ifstat */ ifstat(ls, line); break; } case TK_WHILE: { /* stat -> whilestat */ whilestat(ls, line); break; } case TK_DO: { /* stat -> DO block END */ luaX_next(ls); /* skip DO */ block(ls); check_match(ls, TK_END, TK_DO, line); break; } case TK_FOR: { /* stat -> forstat */ forstat(ls, line); break; } case TK_REPEAT: { /* stat -> repeatstat */ repeatstat(ls, line); break; } case TK_FUNCTION: { /* stat -> funcstat */ funcstat(ls, line); break; } case TK_LOCAL: { /* stat -> localstat */ luaX_next(ls); /* skip LOCAL */ if (testnext(ls, TK_FUNCTION)) /* local function? */ localfunc(ls); else localstat(ls); break; } case TK_GLOBAL: { /* stat -> globalstatfunc */ globalstatfunc(ls, line); break; } case TK_DBCOLON: { /* stat -> label */ luaX_next(ls); /* skip double colon */ labelstat(ls, str_checkname(ls), line); break; } case TK_RETURN: { /* stat -> retstat */ luaX_next(ls); /* skip RETURN */ retstat(ls); break; } case TK_BREAK: { /* stat -> breakstat */ breakstat(ls, line); break; } case TK_GOTO: { /* stat -> 'goto' NAME */ luaX_next(ls); /* skip 'goto' */ gotostat(ls, line); break; } #if defined(LUA_COMPAT_GLOBAL) case TK_NAME: { /* compatibility code to parse global keyword when "global" is not reserved */ if (ls->t.seminfo.ts == ls->glbn) { /* current = "global"? */ int lk = luaX_lookahead(ls); if (lk == '<' || lk == TK_NAME || lk == '*' || lk == TK_FUNCTION) { /* 'global ' or 'global name' or 'global *' or 'global function' */ globalstatfunc(ls, line); break; } } /* else... */ } #endif /* FALLTHROUGH */ default: { /* stat -> func | assignment */ exprstat(ls); break; } } lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && ls->fs->freereg >= luaY_nvarstack(ls->fs)); ls->fs->freereg = luaY_nvarstack(ls->fs); /* free registers */ leavelevel(ls); } /* }====================================================================== */ /* }====================================================================== */ /* ** compiles the main function, which is a regular vararg function with an ** upvalue named LUA_ENV */ static void mainfunc (LexState *ls, FuncState *fs) { BlockCnt bl; Upvaldesc *env; open_func(ls, fs, &bl); setvararg(fs); /* main function is always vararg */ env = allocupvalue(fs); /* ...set environment upvalue */ env->instack = 1; env->idx = 0; env->kind = VDKREG; env->name = ls->envn; luaC_objbarrier(ls->L, fs->f, env->name); luaX_next(ls); /* read first token */ statlist(ls); /* parse main body */ check(ls, TK_EOS); close_func(ls); } LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, Dyndata *dyd, const char *name, int firstchar) { LexState lexstate; FuncState funcstate; LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ setclLvalue2s(L, L->top.p, cl); /* anchor it (to avoid being collected) */ luaD_inctop(L); lexstate.h = luaH_new(L); /* create table for scanner */ sethvalue2s(L, L->top.p, lexstate.h); /* anchor it */ luaD_inctop(L); funcstate.f = cl->p = luaF_newproto(L); luaC_objbarrier(L, cl, cl->p); funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ luaC_objbarrier(L, funcstate.f, funcstate.f->source); lexstate.buff = buff; lexstate.dyd = dyd; dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar); mainfunc(&lexstate, &funcstate); lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); /* all scopes should be correctly finished */ lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0); L->top.p--; /* remove scanner's table */ return cl; /* closure is on the stack, too */ } ================================================ FILE: 3rd/lua/lparser.h ================================================ /* ** $Id: lparser.h $ ** Lua Parser ** See Copyright Notice in lua.h */ #ifndef lparser_h #define lparser_h #include "llimits.h" #include "lobject.h" #include "lzio.h" /* ** Expression and variable descriptor. ** Code generation for variables and expressions can be delayed to allow ** optimizations; An 'expdesc' structure describes a potentially-delayed ** variable/expression. It has a description of its "main" value plus a ** list of conditional jumps that can also produce its value (generated ** by short-circuit operators 'and'/'or'). */ /* kinds of variables/expressions */ typedef enum { VVOID, /* when 'expdesc' describes the last expression of a list, this kind means an empty list (so, no expression) */ VNIL, /* constant nil */ VTRUE, /* constant true */ VFALSE, /* constant false */ VK, /* constant in 'k'; info = index of constant in 'k' */ VKFLT, /* floating constant; nval = numerical float value */ VKINT, /* integer constant; ival = numerical integer value */ VKSTR, /* string constant; strval = TString address; (string is fixed by the scanner) */ VNONRELOC, /* expression has its value in a fixed register; info = result register */ VLOCAL, /* local variable; var.ridx = register index; var.vidx = relative index in 'actvar.arr' */ VVARGVAR, /* vararg parameter; var.ridx = register index; var.vidx = relative index in 'actvar.arr' */ VGLOBAL, /* global variable; info = relative index in 'actvar.arr' (or -1 for implicit declaration) */ VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ VCONST, /* compile-time variable; info = absolute index in 'actvar.arr' */ VINDEXED, /* indexed variable; ind.t = table register; ind.idx = key's R index; ind.ro = true if it represents a read-only global; ind.keystr = if key is a string, index in 'k' of that string; -1 if key is not a string */ VVARGIND, /* indexed vararg parameter; ind.* as in VINDEXED */ VINDEXUP, /* indexed upvalue; ind.idx = key's K index; ind.* as in VINDEXED */ VINDEXI, /* indexed variable with constant integer; ind.t = table register; ind.idx = key's value */ VINDEXSTR, /* indexed variable with literal string; ind.idx = key's K index; ind.* as in VINDEXED */ VJMP, /* expression is a test/comparison; info = pc of corresponding jump instruction */ VRELOC, /* expression can put result in any register; info = instruction pc */ VCALL, /* expression is a function call; info = instruction pc */ VVARARG /* vararg expression; info = instruction pc */ } expkind; #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXSTR) #define vkisindexed(k) (VINDEXED <= (k) && (k) <= VINDEXSTR) typedef struct expdesc { expkind k; union { lua_Integer ival; /* for VKINT */ lua_Number nval; /* for VKFLT */ TString *strval; /* for VKSTR */ int info; /* for generic use */ struct { /* for indexed variables */ short idx; /* index (R or "long" K) */ lu_byte t; /* table (register or upvalue) */ lu_byte ro; /* true if variable is read-only */ int keystr; /* index in 'k' of string key, or -1 if not a string */ } ind; struct { /* for local variables */ lu_byte ridx; /* register holding the variable */ short vidx; /* index in 'actvar.arr' */ } var; } u; int t; /* patch list of 'exit when true' */ int f; /* patch list of 'exit when false' */ } expdesc; /* kinds of variables */ #define VDKREG 0 /* regular local */ #define RDKCONST 1 /* local constant */ #define RDKVAVAR 2 /* vararg parameter */ #define RDKTOCLOSE 3 /* to-be-closed */ #define RDKCTC 4 /* local compile-time constant */ #define GDKREG 5 /* regular global */ #define GDKCONST 6 /* global constant */ /* variables that live in registers */ #define varinreg(v) ((v)->vd.kind <= RDKTOCLOSE) /* test for global variables */ #define varglobal(v) ((v)->vd.kind >= GDKREG) /* description of an active variable */ typedef union Vardesc { struct { TValuefields; /* constant value (if it is a compile-time constant) */ lu_byte kind; lu_byte ridx; /* register holding the variable */ short pidx; /* index of the variable in the Proto's 'locvars' array */ TString *name; /* variable name */ } vd; TValue k; /* constant value (if any) */ } Vardesc; /* description of pending goto statements and label statements */ typedef struct Labeldesc { TString *name; /* label identifier */ int pc; /* position in code */ int line; /* line where it appeared */ short nactvar; /* number of active variables in that position */ lu_byte close; /* true for goto that escapes upvalues */ } Labeldesc; /* list of labels or gotos */ typedef struct Labellist { Labeldesc *arr; /* array */ int n; /* number of entries in use */ int size; /* array size */ } Labellist; /* dynamic structures used by the parser */ typedef struct Dyndata { struct { /* list of all active local variables */ Vardesc *arr; int n; int size; } actvar; Labellist gt; /* list of pending gotos */ Labellist label; /* list of active labels */ } Dyndata; /* control of blocks */ struct BlockCnt; /* defined in lparser.c */ /* state needed to generate code for a given function */ typedef struct FuncState { Proto *f; /* current function header */ struct FuncState *prev; /* enclosing function */ struct LexState *ls; /* lexical state */ struct BlockCnt *bl; /* chain of current blocks */ Table *kcache; /* cache for reusing constants */ int pc; /* next position to code (equivalent to 'ncode') */ int lasttarget; /* 'label' of last 'jump label' */ int previousline; /* last line that was saved in 'lineinfo' */ int nk; /* number of elements in 'k' */ int np; /* number of elements in 'p' */ int nabslineinfo; /* number of elements in 'abslineinfo' */ int firstlocal; /* index of first local var (in Dyndata array) */ int firstlabel; /* index of first label (in 'dyd->label->arr') */ short ndebugvars; /* number of elements in 'f->locvars' */ short nactvar; /* number of active variable declarations */ lu_byte nups; /* number of upvalues */ lu_byte freereg; /* first free register */ lu_byte iwthabs; /* instructions issued since last absolute line info */ lu_byte needclose; /* function needs to close upvalues when returning */ } FuncState; LUAI_FUNC lu_byte luaY_nvarstack (FuncState *fs); LUAI_FUNC void luaY_checklimit (FuncState *fs, int v, int l, const char *what); LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, Dyndata *dyd, const char *name, int firstchar); #endif ================================================ FILE: 3rd/lua/lprefix.h ================================================ /* ** $Id: lprefix.h $ ** Definitions for Lua code that must come before any other header file ** See Copyright Notice in lua.h */ #ifndef lprefix_h #define lprefix_h /* ** Allows POSIX/XSI stuff */ #if !defined(LUA_USE_C89) /* { */ #if !defined(_XOPEN_SOURCE) #define _XOPEN_SOURCE 600 #elif _XOPEN_SOURCE == 0 #undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ #endif /* ** Allows manipulation of large files in gcc and some other compilers */ #if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) #define _LARGEFILE_SOURCE 1 #define _FILE_OFFSET_BITS 64 #endif #endif /* } */ /* ** Windows stuff */ #if defined(_WIN32) /* { */ #if !defined(_CRT_SECURE_NO_WARNINGS) #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ #endif #endif /* } */ #endif ================================================ FILE: 3rd/lua/lstate.c ================================================ /* ** $Id: lstate.c $ ** Global State ** See Copyright Notice in lua.h */ #define lstate_c #define LUA_CORE #include "lprefix.h" #include #include #include "lua.h" #include "lapi.h" #include "ldebug.h" #include "ldo.h" #include "lfunc.h" #include "lgc.h" #include "llex.h" #include "lmem.h" #include "lstate.h" #include "lstring.h" #include "ltable.h" #include "ltm.h" #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) /* ** these macros allow user-specific actions when a thread is ** created/deleted */ #if !defined(luai_userstateopen) #define luai_userstateopen(L) ((void)L) #endif #if !defined(luai_userstateclose) #define luai_userstateclose(L) ((void)L) #endif #if !defined(luai_userstatethread) #define luai_userstatethread(L,L1) ((void)L) #endif #if !defined(luai_userstatefree) #define luai_userstatefree(L,L1) ((void)L) #endif /* ** set GCdebt to a new value keeping the real number of allocated ** objects (GCtotalobjs - GCdebt) invariant and avoiding overflows in ** 'GCtotalobjs'. */ void luaE_setdebt (global_State *g, l_mem debt) { l_mem tb = gettotalbytes(g); lua_assert(tb > 0); if (debt > MAX_LMEM - tb) debt = MAX_LMEM - tb; /* will make GCtotalbytes == MAX_LMEM */ g->GCtotalbytes = tb + debt; g->GCdebt = debt; } CallInfo *luaE_extendCI (lua_State *L) { CallInfo *ci; lua_assert(L->ci->next == NULL); ci = luaM_new(L, CallInfo); lua_assert(L->ci->next == NULL); L->ci->next = ci; ci->previous = L->ci; ci->next = NULL; ci->u.l.trap = 0; L->nci++; return ci; } /* ** free all CallInfo structures not in use by a thread */ static void freeCI (lua_State *L) { CallInfo *ci = L->ci; CallInfo *next = ci->next; ci->next = NULL; while ((ci = next) != NULL) { next = ci->next; luaM_free(L, ci); L->nci--; } } /* ** free half of the CallInfo structures not in use by a thread, ** keeping the first one. */ void luaE_shrinkCI (lua_State *L) { CallInfo *ci = L->ci->next; /* first free CallInfo */ CallInfo *next; if (ci == NULL) return; /* no extra elements */ while ((next = ci->next) != NULL) { /* two extra elements? */ CallInfo *next2 = next->next; /* next's next */ ci->next = next2; /* remove next from the list */ L->nci--; luaM_free(L, next); /* free next */ if (next2 == NULL) break; /* no more elements */ else { next2->previous = ci; ci = next2; /* continue */ } } } /* ** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS. ** If equal, raises an overflow error. If value is larger than ** LUAI_MAXCCALLS (which means it is handling an overflow) but ** not much larger, does not report an error (to allow overflow ** handling to work). */ void luaE_checkcstack (lua_State *L) { if (getCcalls(L) == LUAI_MAXCCALLS) luaG_runerror(L, "C stack overflow"); else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11)) luaD_errerr(L); /* error while handling stack error */ } LUAI_FUNC void luaE_incCstack (lua_State *L) { L->nCcalls++; if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) luaE_checkcstack(L); } static void resetCI (lua_State *L) { CallInfo *ci = L->ci = &L->base_ci; ci->func.p = L->stack.p; setnilvalue(s2v(ci->func.p)); /* 'function' entry for basic 'ci' */ ci->top.p = ci->func.p + 1 + LUA_MINSTACK; /* +1 for 'function' entry */ ci->u.c.k = NULL; ci->callstatus = CIST_C; L->status = LUA_OK; L->errfunc = 0; /* stack unwind can "throw away" the error function */ } static void stack_init (lua_State *L1, lua_State *L) { int i; /* initialize stack array */ L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); L1->tbclist.p = L1->stack.p; for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) setnilvalue(s2v(L1->stack.p + i)); /* erase new stack */ L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE; /* initialize first ci */ resetCI(L1); L1->top.p = L1->stack.p + 1; /* +1 for 'function' entry */ } static void freestack (lua_State *L) { if (L->stack.p == NULL) return; /* stack not completely built yet */ L->ci = &L->base_ci; /* free the entire 'ci' list */ freeCI(L); lua_assert(L->nci == 0); /* free stack */ luaM_freearray(L, L->stack.p, cast_sizet(stacksize(L) + EXTRA_STACK)); } /* ** Create registry table and its predefined values */ static void init_registry (lua_State *L, global_State *g) { /* create registry */ TValue aux; Table *registry = luaH_new(L); sethvalue(L, &g->l_registry, registry); luaH_resize(L, registry, LUA_RIDX_LAST, 0); /* registry[1] = false */ setbfvalue(&aux); luaH_setint(L, registry, 1, &aux); /* registry[LUA_RIDX_MAINTHREAD] = L */ setthvalue(L, &aux, L); luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &aux); /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */ sethvalue(L, &aux, luaH_new(L)); luaH_setint(L, registry, LUA_RIDX_GLOBALS, &aux); } /* ** open parts of the state that may cause memory-allocation errors. */ static void f_luaopen (lua_State *L, void *ud) { global_State *g = G(L); UNUSED(ud); stack_init(L, L); /* init stack */ init_registry(L, g); luaS_init(L); luaT_init(L); luaX_init(L); g->gcstp = 0; /* allow gc */ setnilvalue(&g->nilvalue); /* now state is complete */ luai_userstateopen(L); } /* ** preinitialize a thread with consistent values without allocating ** any memory (to avoid errors) */ static void preinit_thread (lua_State *L, global_State *g) { G(L) = g; L->stack.p = NULL; L->ci = NULL; L->nci = 0; L->twups = L; /* thread has no upvalues */ L->nCcalls = 0; L->errorJmp = NULL; L->hook = NULL; L->hookmask = 0; L->basehookcount = 0; L->allowhook = 1; resethookcount(L); L->openupval = NULL; L->status = LUA_OK; L->errfunc = 0; L->oldpc = 0; L->base_ci.previous = L->base_ci.next = NULL; } lu_mem luaE_threadsize (lua_State *L) { lu_mem sz = cast(lu_mem, sizeof(LX)) + cast_uint(L->nci) * sizeof(CallInfo); if (L->stack.p != NULL) sz += cast_uint(stacksize(L) + EXTRA_STACK) * sizeof(StackValue); return sz; } static void close_state (lua_State *L) { global_State *g = G(L); if (!completestate(g)) /* closing a partially built state? */ luaC_freeallobjects(L); /* just collect its objects */ else { /* closing a fully built state */ resetCI(L); luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */ L->top.p = L->stack.p + 1; /* empty the stack to run finalizers */ luaC_freeallobjects(L); /* collect all objects */ luai_userstateclose(L); } luaM_freearray(L, G(L)->strt.hash, cast_sizet(G(L)->strt.size)); freestack(L); lua_assert(gettotalbytes(g) == sizeof(global_State)); (*g->frealloc)(g->ud, g, sizeof(global_State), 0); /* free main block */ } LUA_API lua_State *lua_newthread (lua_State *L) { global_State *g = G(L); GCObject *o; lua_State *L1; lua_lock(L); luaC_checkGC(L); /* create new thread */ o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l)); L1 = gco2th(o); /* anchor it on L stack */ setthvalue2s(L, L->top.p, L1); api_incr_top(L); preinit_thread(L1, g); L1->hookmask = L->hookmask; L1->basehookcount = L->basehookcount; L1->hook = L->hook; resethookcount(L1); /* initialize L1 extra space */ memcpy(lua_getextraspace(L1), lua_getextraspace(mainthread(g)), LUA_EXTRASPACE); luai_userstatethread(L, L1); stack_init(L1, L); /* init stack */ lua_unlock(L); return L1; } void luaE_freethread (lua_State *L, lua_State *L1) { LX *l = fromstate(L1); luaF_closeupval(L1, L1->stack.p); /* close all upvalues */ lua_assert(L1->openupval == NULL); luai_userstatefree(L, L1); freestack(L1); luaM_free(L, l); } TStatus luaE_resetthread (lua_State *L, TStatus status) { resetCI(L); if (status == LUA_YIELD) status = LUA_OK; status = luaD_closeprotected(L, 1, status); if (status != LUA_OK) /* errors? */ luaD_seterrorobj(L, status, L->stack.p + 1); else L->top.p = L->stack.p + 1; luaD_reallocstack(L, cast_int(L->ci->top.p - L->stack.p), 0); return status; } LUA_API int lua_closethread (lua_State *L, lua_State *from) { TStatus status; lua_lock(L); L->nCcalls = (from) ? getCcalls(from) : 0; status = luaE_resetthread(L, L->status); if (L == from) /* closing itself? */ luaD_throwbaselevel(L, status); lua_unlock(L); return APIstatus(status); } LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) { int i; lua_State *L; global_State *g = cast(global_State*, (*f)(ud, NULL, LUA_TTHREAD, sizeof(global_State))); if (g == NULL) return NULL; L = &g->mainth.l; L->tt = LUA_VTHREAD; g->currentwhite = bitmask(WHITE0BIT); L->marked = luaC_white(g); preinit_thread(L, g); g->allgc = obj2gco(L); /* by now, only object is the main thread */ L->next = NULL; incnny(L); /* main thread is always non yieldable */ g->frealloc = f; g->ud = ud; g->warnf = NULL; g->ud_warn = NULL; g->seed = seed; g->gcstp = GCSTPGC; /* no GC while building state */ g->strt.size = g->strt.nuse = 0; g->strt.hash = NULL; setnilvalue(&g->l_registry); g->panic = NULL; g->gcstate = GCSpause; g->gckind = KGC_INC; g->gcstopem = 0; g->gcemergency = 0; g->finobj = g->tobefnz = g->fixedgc = NULL; g->firstold1 = g->survival = g->old1 = g->reallyold = NULL; g->finobjsur = g->finobjold1 = g->finobjrold = NULL; g->sweepgc = NULL; g->gray = g->grayagain = NULL; g->weak = g->ephemeron = g->allweak = NULL; g->twups = NULL; g->GCtotalbytes = sizeof(global_State); g->GCmarked = 0; g->GCdebt = 0; setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ setgcparam(g, PAUSE, LUAI_GCPAUSE); setgcparam(g, STEPMUL, LUAI_GCMUL); setgcparam(g, STEPSIZE, LUAI_GCSTEPSIZE); setgcparam(g, MINORMUL, LUAI_GENMINORMUL); setgcparam(g, MINORMAJOR, LUAI_MINORMAJOR); setgcparam(g, MAJORMINOR, LUAI_MAJORMINOR); for (i=0; i < LUA_NUMTYPES; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { /* memory allocation error: free partial state */ close_state(L); L = NULL; } return L; } LUA_API void lua_close (lua_State *L) { lua_lock(L); L = mainthread(G(L)); /* only the main thread can be closed */ close_state(L); } void luaE_warning (lua_State *L, const char *msg, int tocont) { lua_WarnFunction wf = G(L)->warnf; if (wf != NULL) wf(G(L)->ud_warn, msg, tocont); } /* ** Generate a warning from an error message */ void luaE_warnerror (lua_State *L, const char *where) { TValue *errobj = s2v(L->top.p - 1); /* error object */ const char *msg = (ttisstring(errobj)) ? getstr(tsvalue(errobj)) : "error object is not a string"; /* produce warning "error in %s (%s)" (where, msg) */ luaE_warning(L, "error in ", 1); luaE_warning(L, where, 1); luaE_warning(L, " (", 1); luaE_warning(L, msg, 1); luaE_warning(L, ")", 0); } ================================================ FILE: 3rd/lua/lstate.h ================================================ /* ** $Id: lstate.h $ ** Global State ** See Copyright Notice in lua.h */ #ifndef lstate_h #define lstate_h #include "lua.h" /* Some header files included here need this definition */ typedef struct CallInfo CallInfo; #include "lobject.h" #include "ltm.h" #include "lzio.h" /* ** Some notes about garbage-collected objects: All objects in Lua must ** be kept somehow accessible until being freed, so all objects always ** belong to one (and only one) of these lists, using field 'next' of ** the 'CommonHeader' for the link: ** ** 'allgc': all objects not marked for finalization; ** 'finobj': all objects marked for finalization; ** 'tobefnz': all objects ready to be finalized; ** 'fixedgc': all objects that are not to be collected (currently ** only small strings, such as reserved words). ** ** For the generational collector, some of these lists have marks for ** generations. Each mark points to the first element in the list for ** that particular generation; that generation goes until the next mark. ** ** 'allgc' -> 'survival': new objects; ** 'survival' -> 'old': objects that survived one collection; ** 'old1' -> 'reallyold': objects that became old in last collection; ** 'reallyold' -> NULL: objects old for more than one cycle. ** ** 'finobj' -> 'finobjsur': new objects marked for finalization; ** 'finobjsur' -> 'finobjold1': survived """"; ** 'finobjold1' -> 'finobjrold': just old """"; ** 'finobjrold' -> NULL: really old """". ** ** All lists can contain elements older than their main ages, due ** to 'luaC_checkfinalizer' and 'udata2finalize', which move ** objects between the normal lists and the "marked for finalization" ** lists. Moreover, barriers can age young objects in young lists as ** OLD0, which then become OLD1. However, a list never contains ** elements younger than their main ages. ** ** The generational collector also uses a pointer 'firstold1', which ** points to the first OLD1 object in the list. It is used to optimize ** 'markold'. (Potentially OLD1 objects can be anywhere between 'allgc' ** and 'reallyold', but often the list has no OLD1 objects or they are ** after 'old1'.) Note the difference between it and 'old1': ** 'firstold1': no OLD1 objects before this point; there can be all ** ages after it. ** 'old1': no objects younger than OLD1 after this point. */ /* ** Moreover, there is another set of lists that control gray objects. ** These lists are linked by fields 'gclist'. (All objects that ** can become gray have such a field. The field is not the same ** in all objects, but it always has this name.) Any gray object ** must belong to one of these lists, and all objects in these lists ** must be gray (with two exceptions explained below): ** ** 'gray': regular gray objects, still waiting to be visited. ** 'grayagain': objects that must be revisited at the atomic phase. ** That includes ** - black objects got in a write barrier; ** - all kinds of weak tables during propagation phase; ** - all threads. ** 'weak': tables with weak values to be cleared; ** 'ephemeron': ephemeron tables with white->white entries; ** 'allweak': tables with weak keys and/or weak values to be cleared. ** ** The exceptions to that "gray rule" are: ** - TOUCHED2 objects in generational mode stay in a gray list (because ** they must be visited again at the end of the cycle), but they are ** marked black because assignments to them must activate barriers (to ** move them back to TOUCHED1). ** - Open upvalues are kept gray to avoid barriers, but they stay out ** of gray lists. (They don't even have a 'gclist' field.) */ /* ** About 'nCcalls': This count has two parts: the lower 16 bits counts ** the number of recursive invocations in the C stack; the higher ** 16 bits counts the number of non-yieldable calls in the stack. ** (They are together so that we can change and save both with one ** instruction.) */ /* true if this thread does not have non-yieldable calls in the stack */ #define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0) /* real number of C calls */ #define getCcalls(L) ((L)->nCcalls & 0xffff) /* Increment the number of non-yieldable calls */ #define incnny(L) ((L)->nCcalls += 0x10000) /* Decrement the number of non-yieldable calls */ #define decnny(L) ((L)->nCcalls -= 0x10000) /* Non-yieldable call increment */ #define nyci (0x10000 | 1) struct lua_longjmp; /* defined in ldo.c */ /* ** Atomic type (relative to signals) to better ensure that 'lua_sethook' ** is thread safe */ #if !defined(l_signalT) #include #define l_signalT sig_atomic_t #endif /* ** Extra stack space to handle TM calls and some other extras. This ** space is not included in 'stack_last'. It is used only to avoid stack ** checks, either because the element will be promptly popped or because ** there will be a stack check soon after the push. Function frames ** never use this extra space, so it does not need to be kept clean. */ #define EXTRA_STACK 5 /* ** Size of cache for strings in the API. 'N' is the number of ** sets (better be a prime) and "M" is the size of each set. ** (M == 1 makes a direct cache.) */ #if !defined(STRCACHE_N) #define STRCACHE_N 53 #define STRCACHE_M 2 #endif #define BASIC_STACK_SIZE (2*LUA_MINSTACK) #define stacksize(th) cast_int((th)->stack_last.p - (th)->stack.p) /* kinds of Garbage Collection */ #define KGC_INC 0 /* incremental gc */ #define KGC_GENMINOR 1 /* generational gc in minor (regular) mode */ #define KGC_GENMAJOR 2 /* generational in major mode */ typedef struct stringtable { TString **hash; /* array of buckets (linked lists of strings) */ int nuse; /* number of elements */ int size; /* number of buckets */ } stringtable; /* ** Information about a call. ** About union 'u': ** - field 'l' is used only for Lua functions; ** - field 'c' is used only for C functions. ** About union 'u2': ** - field 'funcidx' is used only by C functions while doing a ** protected call; ** - field 'nyield' is used only while a function is "doing" an ** yield (from the yield until the next resume); ** - field 'nres' is used only while closing tbc variables when ** returning from a function; */ struct CallInfo { StkIdRel func; /* function index in the stack */ StkIdRel top; /* top for this function */ struct CallInfo *previous, *next; /* dynamic call link */ union { struct { /* only for Lua functions */ const Instruction *savedpc; volatile l_signalT trap; /* function is tracing lines/counts */ int nextraargs; /* # of extra arguments in vararg functions */ } l; struct { /* only for C functions */ lua_KFunction k; /* continuation in case of yields */ ptrdiff_t old_errfunc; lua_KContext ctx; /* context info. in case of yields */ } c; } u; union { int funcidx; /* called-function index */ int nyield; /* number of values yielded */ int nres; /* number of values returned */ } u2; l_uint32 callstatus; }; /* ** Maximum expected number of results from a function ** (must fit in CIST_NRESULTS). */ #define MAXRESULTS 250 /* ** Bits in CallInfo status */ /* bits 0-7 are the expected number of results from this function + 1 */ #define CIST_NRESULTS 0xffu /* bits 8-11 count call metamethods (and their extra arguments) */ #define CIST_CCMT 8 /* the offset, not the mask */ #define MAX_CCMT (0xfu << CIST_CCMT) /* Bits 12-14 are used for CIST_RECST (see below) */ #define CIST_RECST 12 /* the offset, not the mask */ /* call is running a C function (still in first 16 bits) */ #define CIST_C (1u << (CIST_RECST + 3)) /* call is on a fresh "luaV_execute" frame */ #define CIST_FRESH (cast(l_uint32, CIST_C) << 1) /* function is closing tbc variables */ #define CIST_CLSRET (CIST_FRESH << 1) /* function has tbc variables to close */ #define CIST_TBC (CIST_CLSRET << 1) /* original value of 'allowhook' */ #define CIST_OAH (CIST_TBC << 1) /* call is running a debug hook */ #define CIST_HOOKED (CIST_OAH << 1) /* doing a yieldable protected call */ #define CIST_YPCALL (CIST_HOOKED << 1) /* call was tail called */ #define CIST_TAIL (CIST_YPCALL << 1) /* last hook called yielded */ #define CIST_HOOKYIELD (CIST_TAIL << 1) /* function "called" a finalizer */ #define CIST_FIN (CIST_HOOKYIELD << 1) #define get_nresults(cs) (cast_int((cs) & CIST_NRESULTS) - 1) /* ** Field CIST_RECST stores the "recover status", used to keep the error ** status while closing to-be-closed variables in coroutines, so that ** Lua can correctly resume after an yield from a __close method called ** because of an error. (Three bits are enough for error status.) */ #define getcistrecst(ci) (((ci)->callstatus >> CIST_RECST) & 7) #define setcistrecst(ci,st) \ check_exp(((st) & 7) == (st), /* status must fit in three bits */ \ ((ci)->callstatus = ((ci)->callstatus & ~(7u << CIST_RECST)) \ | (cast(l_uint32, st) << CIST_RECST))) /* active function is a Lua function */ #define isLua(ci) (!((ci)->callstatus & CIST_C)) /* call is running Lua code (not a hook) */ #define isLuacode(ci) (!((ci)->callstatus & (CIST_C | CIST_HOOKED))) #define setoah(ci,v) \ ((ci)->callstatus = ((v) ? (ci)->callstatus | CIST_OAH \ : (ci)->callstatus & ~CIST_OAH)) #define getoah(ci) (((ci)->callstatus & CIST_OAH) ? 1 : 0) /* ** 'per thread' state */ struct lua_State { CommonHeader; lu_byte allowhook; TStatus status; StkIdRel top; /* first free slot in the stack */ struct global_State *l_G; CallInfo *ci; /* call info for current function */ StkIdRel stack_last; /* end of stack (last element + 1) */ StkIdRel stack; /* stack base */ UpVal *openupval; /* list of open upvalues in this stack */ StkIdRel tbclist; /* list of to-be-closed variables */ GCObject *gclist; struct lua_State *twups; /* list of threads with open upvalues */ struct lua_longjmp *errorJmp; /* current error recover point */ CallInfo base_ci; /* CallInfo for first level (C host) */ volatile lua_Hook hook; ptrdiff_t errfunc; /* current error handling function (stack index) */ l_uint32 nCcalls; /* number of nested non-yieldable or C calls */ int oldpc; /* last pc traced */ int nci; /* number of items in 'ci' list */ int basehookcount; int hookcount; volatile l_signalT hookmask; struct { /* info about transferred values (for call/return hooks) */ int ftransfer; /* offset of first value transferred */ int ntransfer; /* number of values transferred */ } transferinfo; }; /* ** thread state + extra space */ typedef struct LX { lu_byte extra_[LUA_EXTRASPACE]; lua_State l; } LX; /* ** 'global state', shared by all threads of this state */ typedef struct global_State { lua_Alloc frealloc; /* function to reallocate memory */ void *ud; /* auxiliary data to 'frealloc' */ l_mem GCtotalbytes; /* number of bytes currently allocated + debt */ l_mem GCdebt; /* bytes counted but not yet allocated */ l_mem GCmarked; /* number of objects marked in a GC cycle */ l_mem GCmajorminor; /* auxiliary counter to control major-minor shifts */ stringtable strt; /* hash table for strings */ TValue l_registry; TValue nilvalue; /* a nil value */ unsigned int seed; /* randomized seed for hashes */ lu_byte gcparams[LUA_GCPN]; lu_byte currentwhite; lu_byte gcstate; /* state of garbage collector */ lu_byte gckind; /* kind of GC running */ lu_byte gcstopem; /* stops emergency collections */ lu_byte gcstp; /* control whether GC is running */ lu_byte gcemergency; /* true if this is an emergency collection */ GCObject *allgc; /* list of all collectable objects */ GCObject **sweepgc; /* current position of sweep in list */ GCObject *finobj; /* list of collectable objects with finalizers */ GCObject *gray; /* list of gray objects */ GCObject *grayagain; /* list of objects to be traversed atomically */ GCObject *weak; /* list of tables with weak values */ GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ GCObject *allweak; /* list of all-weak tables */ GCObject *tobefnz; /* list of userdata to be GC */ GCObject *fixedgc; /* list of objects not to be collected */ /* fields for generational collector */ GCObject *survival; /* start of objects that survived one GC cycle */ GCObject *old1; /* start of old1 objects */ GCObject *reallyold; /* objects more than one cycle old ("really old") */ GCObject *firstold1; /* first OLD1 object in the list (if any) */ GCObject *finobjsur; /* list of survival objects with finalizers */ GCObject *finobjold1; /* list of old1 objects with finalizers */ GCObject *finobjrold; /* list of really old objects with finalizers */ struct lua_State *twups; /* list of threads with open upvalues */ lua_CFunction panic; /* to be called in unprotected errors */ TString *memerrmsg; /* message for memory-allocation errors */ TString *tmname[TM_N]; /* array with tag-method names */ struct Table *mt[LUA_NUMTYPES]; /* metatables for basic types */ TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ lua_WarnFunction warnf; /* warning function */ void *ud_warn; /* auxiliary data to 'warnf' */ LX mainth; /* main thread of this state */ } global_State; #define G(L) (L->l_G) #define mainthread(G) (&(G)->mainth.l) /* ** 'g->nilvalue' being a nil value flags that the state was completely ** build. */ #define completestate(g) ttisnil(&g->nilvalue) /* ** Union of all collectable objects (only for conversions) ** ISO C99, 6.5.2.3 p.5: ** "if a union contains several structures that share a common initial ** sequence [...], and if the union object currently contains one ** of these structures, it is permitted to inspect the common initial ** part of any of them anywhere that a declaration of the complete type ** of the union is visible." */ union GCUnion { GCObject gc; /* common header */ struct TString ts; struct Udata u; union Closure cl; struct Table h; struct Proto p; struct lua_State th; /* thread */ struct UpVal upv; }; /* ** ISO C99, 6.7.2.1 p.14: ** "A pointer to a union object, suitably converted, points to each of ** its members [...], and vice versa." */ #define cast_u(o) cast(union GCUnion *, (o)) /* macros to convert a GCObject into a specific value */ #define gco2ts(o) \ check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts)) #define gco2u(o) check_exp((o)->tt == LUA_VUSERDATA, &((cast_u(o))->u)) #define gco2lcl(o) check_exp((o)->tt == LUA_VLCL, &((cast_u(o))->cl.l)) #define gco2ccl(o) check_exp((o)->tt == LUA_VCCL, &((cast_u(o))->cl.c)) #define gco2cl(o) \ check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl)) #define gco2t(o) check_exp((o)->tt == LUA_VTABLE, &((cast_u(o))->h)) #define gco2p(o) check_exp((o)->tt == LUA_VPROTO, &((cast_u(o))->p)) #define gco2th(o) check_exp((o)->tt == LUA_VTHREAD, &((cast_u(o))->th)) #define gco2upv(o) check_exp((o)->tt == LUA_VUPVAL, &((cast_u(o))->upv)) /* ** macro to convert a Lua object into a GCObject */ #define obj2gco(v) \ check_exp(novariant((v)->tt) >= LUA_TSTRING, &(cast_u(v)->gc)) /* actual number of total memory allocated */ #define gettotalbytes(g) ((g)->GCtotalbytes - (g)->GCdebt) LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); LUAI_FUNC lu_mem luaE_threadsize (lua_State *L); LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); LUAI_FUNC void luaE_shrinkCI (lua_State *L); LUAI_FUNC void luaE_checkcstack (lua_State *L); LUAI_FUNC void luaE_incCstack (lua_State *L); LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where); LUAI_FUNC TStatus luaE_resetthread (lua_State *L, TStatus status); #endif ================================================ FILE: 3rd/lua/lstring.c ================================================ /* ** $Id: lstring.c $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ #define lstring_c #define LUA_CORE #include "lprefix.h" #include #include "lua.h" #include "ldebug.h" #include "ldo.h" #include "lmem.h" #include "lobject.h" #include "lstate.h" #include "lstring.h" #include "atomic.h" static ATOM_SIZET STRID = 0; /* ** Maximum size for string table. */ #define MAXSTRTB cast_int(luaM_limitN(INT_MAX, TString*)) /* ** Initial size for the string table (must be power of 2). ** The Lua core alone registers ~50 strings (reserved words + ** metaevent keys + a few others). Libraries would typically add ** a few dozens more. */ #if !defined(MINSTRTABSIZE) #define MINSTRTABSIZE 128 #endif /* ** generic equality for strings */ int luaS_eqstr (TString *a, TString *b) { size_t len1, len2; const char *s1 = getlstr(a, len1); const char *s2 = getlstr(b, len2); return ((len1 == len2) && /* equal length and ... */ (memcmp(s1, s2, len1) == 0)); /* equal contents */ } int luaS_eqshrstr (TString *a, TString *b) { int r; ls_byte len = a->shrlen; r = len == b->shrlen && (memcmp(getshrstr(a), getshrstr(b), len) == 0); if (r) { if (a->id < b->id) { a->id = b->id; } else { b->id = a->id; } } return r; } void luaS_share (TString *ts) { if (ts == NULL || isshared(ts)) return; if (ts->tt == LUA_VLNGSTR) luaS_hashlongstr(ts); makeshared(ts); ts->id = ATOM_FDEC(&STRID)-1; } static unsigned luaS_hash (const char *str, size_t l, unsigned seed) { unsigned int h = seed ^ cast_uint(l); for (; l > 0; l--) h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); return h; } unsigned luaS_hashlongstr (TString *ts) { lua_assert(ts->tt == LUA_VLNGSTR); if (ts->extra == 0) { /* no hash? */ size_t len = ts->u.lnglen; ts->hash = luaS_hash(getlngstr(ts), len, ts->hash); ts->extra = 1; /* now it has its hash */ } return ts->hash; } static void tablerehash (TString **vect, int osize, int nsize) { int i; for (i = osize; i < nsize; i++) /* clear new elements */ vect[i] = NULL; for (i = 0; i < osize; i++) { /* rehash old part of the array */ TString *p = vect[i]; vect[i] = NULL; while (p) { /* for each string in the list */ TString *hnext = p->u.hnext; /* save next */ unsigned int h = lmod(p->hash, nsize); /* new position */ p->u.hnext = vect[h]; /* chain it into array */ vect[h] = p; p = hnext; } } } /* ** Resize the string table. If allocation fails, keep the current size. ** (This can degrade performance, but any non-zero size should work ** correctly.) */ void luaS_resize (lua_State *L, int nsize) { stringtable *tb = &G(L)->strt; int osize = tb->size; TString **newvect; if (nsize < osize) /* shrinking table? */ tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */ newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*); if (l_unlikely(newvect == NULL)) { /* reallocation failed? */ if (nsize < osize) /* was it shrinking table? */ tablerehash(tb->hash, nsize, osize); /* restore to original size */ /* leave table as it was */ } else { /* allocation succeeded */ tb->hash = newvect; tb->size = nsize; if (nsize > osize) tablerehash(newvect, osize, nsize); /* rehash for new size */ } } /* ** Clear API string cache. (Entries cannot be empty, so fill them with ** a non-collectable string.) */ void luaS_clearcache (global_State *g) { int i, j; for (i = 0; i < STRCACHE_N; i++) for (j = 0; j < STRCACHE_M; j++) { if (iswhite(g->strcache[i][j])) /* will entry be collected? */ g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ } } /* ** Initialize the string table and the string cache */ void luaS_init (lua_State *L) { global_State *g = G(L); int i, j; stringtable *tb = &G(L)->strt; tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*); tablerehash(tb->hash, 0, MINSTRTABSIZE); /* clear array */ tb->size = MINSTRTABSIZE; /* pre-create memory-error message */ g->memerrmsg = luaS_newliteral(L, MEMERRMSG); luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */ for (j = 0; j < STRCACHE_M; j++) g->strcache[i][j] = g->memerrmsg; } size_t luaS_sizelngstr (size_t len, int kind) { switch (kind) { case LSTRREG: /* regular long string */ /* don't need 'falloc'/'ud', but need space for content */ return offsetof(TString, falloc) + (len + 1) * sizeof(char); case LSTRFIX: /* fixed external long string */ /* don't need 'falloc'/'ud' */ return offsetof(TString, falloc); default: /* external long string with deallocation */ lua_assert(kind == LSTRMEM); return sizeof(TString); } } /* ** creates a new string object */ static TString *createstrobj (lua_State *L, size_t totalsize, lu_byte tag, unsigned h) { TString *ts; GCObject *o; o = luaC_newobj(L, tag, totalsize); ts = gco2ts(o); ts->hash = h; ts->extra = 0; ts->id = 0; return ts; } TString *luaS_createlngstrobj (lua_State *L, size_t l) { size_t totalsize = luaS_sizelngstr(l, LSTRREG); TString *ts = createstrobj(L, totalsize, LUA_VLNGSTR, G(L)->seed); ts->u.lnglen = l; ts->shrlen = LSTRREG; /* signals that it is a regular long string */ ts->contents = cast_charp(ts) + offsetof(TString, falloc); ts->contents[l] = '\0'; /* ending 0 */ return ts; } void luaS_remove (lua_State *L, TString *ts) { stringtable *tb = &G(L)->strt; TString **p = &tb->hash[lmod(ts->hash, tb->size)]; while (*p != ts) /* find previous element */ p = &(*p)->u.hnext; *p = (*p)->u.hnext; /* remove element from its list */ tb->nuse--; } static void growstrtab (lua_State *L, stringtable *tb) { if (l_unlikely(tb->nuse == INT_MAX)) { /* too many strings? */ luaC_fullgc(L, 1); /* try to free some... */ if (tb->nuse == INT_MAX) /* still too many? */ luaM_error(L); /* cannot even create a message... */ } if (tb->size <= MAXSTRTB / 2) /* can grow string table? */ luaS_resize(L, tb->size * 2); } /* ** Checks whether short string exists and reuses it or creates a new one. */ static TString *internshrstr (lua_State *L, const char *str, size_t l) { TString *ts; global_State *g = G(L); stringtable *tb = &g->strt; unsigned int h = luaS_hash(str, l, g->seed); TString **list = &tb->hash[lmod(h, tb->size)]; lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ for (ts = *list; ts != NULL; ts = ts->u.hnext) { if (l == cast_uint(ts->shrlen) && (memcmp(str, getshrstr(ts), l * sizeof(char)) == 0)) { /* found! */ if (isdead(g, ts)) /* dead (but not collected yet)? */ changewhite(ts); /* resurrect it */ return ts; } } /* else must create a new string */ if (tb->nuse >= tb->size) { /* need to grow string table? */ growstrtab(L, tb); list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */ } ts = createstrobj(L, sizestrshr(l), LUA_VSHRSTR, h); ts->shrlen = cast(ls_byte, l); getshrstr(ts)[l] = '\0'; /* ending 0 */ memcpy(getshrstr(ts), str, l * sizeof(char)); ts->u.hnext = *list; *list = ts; tb->nuse++; return ts; } /* ** new string (with explicit length) */ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { if (l <= LUAI_MAXSHORTLEN) /* short string? */ return internshrstr(L, str, l); else { TString *ts; if (l_unlikely(l * sizeof(char) >= (MAX_SIZE - sizeof(TString)))) luaM_toobig(L); ts = luaS_createlngstrobj(L, l); memcpy(getlngstr(ts), str, l * sizeof(char)); return ts; } } /* ** Create or reuse a zero-terminated string, first checking in the ** cache (using the string address as a key). The cache can contain ** only zero-terminated strings, so it is safe to use 'strcmp' to ** check hits. */ TString *luaS_new (lua_State *L, const char *str) { unsigned int i = point2uint(str) % STRCACHE_N; /* hash */ int j; TString **p = G(L)->strcache[i]; for (j = 0; j < STRCACHE_M; j++) { if (strcmp(str, getstr(p[j])) == 0) /* hit? */ return p[j]; /* that is it */ } /* normal route */ for (j = STRCACHE_M - 1; j > 0; j--) p[j] = p[j - 1]; /* move out last element */ /* new element is first in the list */ p[0] = luaS_newlstr(L, str, strlen(str)); return p[0]; } Udata *luaS_newudata (lua_State *L, size_t s, unsigned short nuvalue) { Udata *u; int i; GCObject *o; if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue))) luaM_toobig(L); o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s)); u = gco2u(o); u->len = s; u->nuvalue = nuvalue; u->metatable = NULL; for (i = 0; i < nuvalue; i++) setnilvalue(&u->uv[i].uv); return u; } struct NewExt { ls_byte kind; const char *s; size_t len; TString *ts; /* output */ }; static void f_newext (lua_State *L, void *ud) { struct NewExt *ne = cast(struct NewExt *, ud); size_t size = luaS_sizelngstr(0, ne->kind); ne->ts = createstrobj(L, size, LUA_VLNGSTR, G(L)->seed); } TString *luaS_newextlstr (lua_State *L, const char *s, size_t len, lua_Alloc falloc, void *ud) { struct NewExt ne; if (!falloc) { ne.kind = LSTRFIX; f_newext(L, &ne); /* just create header */ } else { ne.kind = LSTRMEM; if (luaD_rawrunprotected(L, f_newext, &ne) != LUA_OK) { /* mem. error? */ (*falloc)(ud, cast_voidp(s), len + 1, 0); /* free external string */ luaM_error(L); /* re-raise memory error */ } ne.ts->falloc = falloc; ne.ts->ud = ud; } ne.ts->shrlen = ne.kind; ne.ts->u.lnglen = len; ne.ts->contents = cast_charp(s); return ne.ts; } /* ** Normalize an external string: If it is short, internalize it. */ TString *luaS_normstr (lua_State *L, TString *ts) { size_t len = ts->u.lnglen; if (len > LUAI_MAXSHORTLEN) return ts; /* long string; keep the original */ else { const char *str = getlngstr(ts); return internshrstr(L, str, len); } } ================================================ FILE: 3rd/lua/lstring.h ================================================ /* ** $Id: lstring.h $ ** String table (keep all strings handled by Lua) ** See Copyright Notice in lua.h */ #ifndef lstring_h #define lstring_h #include "lgc.h" #include "lobject.h" #include "lstate.h" /* ** Memory-allocation error message must be preallocated (it cannot ** be created after memory is exhausted) */ #define MEMERRMSG "not enough memory" /* ** Maximum length for short strings, that is, strings that are ** internalized. (Cannot be smaller than reserved words or tags for ** metamethods, as these strings must be internalized; ** #("function") = 8, #("__newindex") = 10.) */ #if !defined(LUAI_MAXSHORTLEN) #define LUAI_MAXSHORTLEN 40 #endif /* ** Size of a short TString: Size of the header plus space for the string ** itself (including final '\0'). */ #define sizestrshr(l) \ (offsetof(TString, contents) + ((l) + 1) * sizeof(char)) #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ (sizeof(s)/sizeof(char))-1)) /* ** test whether a string is a reserved word */ #define isreserved(s) (strisshr(s) && (s)->extra > 0) /* ** equality for short strings, compare id first */ #define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b) || \ ( ((a)->id == (b)->id) ? ((a)->id != 0) : ((a)->hash == (b)->hash && luaS_eqshrstr(a,b)) ) ) LUAI_FUNC unsigned luaS_hashlongstr (TString *ts); LUAI_FUNC int luaS_eqstr (TString *a, TString *b); LUAI_FUNC void luaS_resize (lua_State *L, int newsize); LUAI_FUNC void luaS_clearcache (global_State *g); LUAI_FUNC void luaS_init (lua_State *L); LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, unsigned short nuvalue); LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); LUAI_FUNC TString *luaS_newextlstr (lua_State *L, const char *s, size_t len, lua_Alloc falloc, void *ud); LUAI_FUNC size_t luaS_sizelngstr (size_t len, int kind); LUAI_FUNC TString *luaS_normstr (lua_State *L, TString *ts); LUAI_FUNC void luaS_share(TString *ts); LUAI_FUNC int luaS_eqshrstr (TString *a, TString *b); #endif ================================================ FILE: 3rd/lua/lstrlib.c ================================================ /* ** $Id: lstrlib.c $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ #define lstrlib_c #define LUA_LIB #include "lprefix.h" #include #include #include #include #include #include #include #include #include #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "llimits.h" /* ** maximum number of captures that a pattern can do during ** pattern-matching. This limit is arbitrary, but must fit in ** an unsigned char. */ #if !defined(LUA_MAXCAPTURES) #define LUA_MAXCAPTURES 32 #endif static int str_len (lua_State *L) { size_t l; luaL_checklstring(L, 1, &l); lua_pushinteger(L, (lua_Integer)l); return 1; } /* ** translate a relative initial string position ** (negative means back from end): clip result to [1, inf). ** The length of any string in Lua must fit in a lua_Integer, ** so there are no overflows in the casts. ** The inverted comparison avoids a possible overflow ** computing '-pos'. */ static size_t posrelatI (lua_Integer pos, size_t len) { if (pos > 0) return (size_t)pos; else if (pos == 0) return 1; else if (pos < -(lua_Integer)len) /* inverted comparison */ return 1; /* clip to 1 */ else return len + (size_t)pos + 1; } /* ** Gets an optional ending string position from argument 'arg', ** with default value 'def'. ** Negative means back from end: clip result to [0, len] */ static size_t getendpos (lua_State *L, int arg, lua_Integer def, size_t len) { lua_Integer pos = luaL_optinteger(L, arg, def); if (pos > (lua_Integer)len) return len; else if (pos >= 0) return (size_t)pos; else if (pos < -(lua_Integer)len) return 0; else return len + (size_t)pos + 1; } static int str_sub (lua_State *L) { size_t l; const char *s = luaL_checklstring(L, 1, &l); size_t start = posrelatI(luaL_checkinteger(L, 2), l); size_t end = getendpos(L, 3, -1, l); if (start <= end) lua_pushlstring(L, s + start - 1, (end - start) + 1); else lua_pushliteral(L, ""); return 1; } static int str_reverse (lua_State *L) { size_t l, i; luaL_Buffer b; const char *s = luaL_checklstring(L, 1, &l); char *p = luaL_buffinitsize(L, &b, l); for (i = 0; i < l; i++) p[i] = s[l - i - 1]; luaL_pushresultsize(&b, l); return 1; } static int str_lower (lua_State *L) { size_t l; size_t i; luaL_Buffer b; const char *s = luaL_checklstring(L, 1, &l); char *p = luaL_buffinitsize(L, &b, l); for (i=0; i MAX_SIZE - lsep || cast_st2S(len + lsep) > cast_st2S(MAX_SIZE) / n)) return luaL_error(L, "resulting string too large"); else { size_t totallen = (cast_sizet(n) * (len + lsep)) - lsep; luaL_Buffer b; char *p = luaL_buffinitsize(L, &b, totallen); while (n-- > 1) { /* first n-1 copies (followed by separator) */ memcpy(p, s, len * sizeof(char)); p += len; if (lsep > 0) { /* empty 'memcpy' is not that cheap */ memcpy(p, sep, lsep * sizeof(char)); p += lsep; } } memcpy(p, s, len * sizeof(char)); /* last copy without separator */ luaL_pushresultsize(&b, totallen); } return 1; } static int str_byte (lua_State *L) { size_t l; const char *s = luaL_checklstring(L, 1, &l); lua_Integer pi = luaL_optinteger(L, 2, 1); size_t posi = posrelatI(pi, l); size_t pose = getendpos(L, 3, pi, l); int n, i; if (posi > pose) return 0; /* empty interval; return no values */ if (l_unlikely(pose - posi >= (size_t)INT_MAX)) /* arithmetic overflow? */ return luaL_error(L, "string slice too long"); n = (int)(pose - posi) + 1; luaL_checkstack(L, n, "string slice too long"); for (i=0; iinit) { state->init = 1; luaL_buffinit(L, &state->B); } if (b == NULL) { /* finishing dump? */ luaL_pushresult(&state->B); /* push result */ lua_replace(L, 1); /* move it to reserved slot */ } else luaL_addlstring(&state->B, (const char *)b, size); return 0; } static int str_dump (lua_State *L) { struct str_Writer state; int strip = lua_toboolean(L, 2); luaL_argcheck(L, lua_type(L, 1) == LUA_TFUNCTION && !lua_iscfunction(L, 1), 1, "Lua function expected"); /* ensure function is on the top of the stack and vacate slot 1 */ lua_pushvalue(L, 1); state.init = 0; lua_dump(L, writer, &state, strip); lua_settop(L, 1); /* leave final result on top */ return 1; } /* ** {====================================================== ** METAMETHODS ** ======================================================= */ #if defined(LUA_NOCVTS2N) /* { */ /* no coercion from strings to numbers */ static const luaL_Reg stringmetamethods[] = { {"__index", NULL}, /* placeholder */ {NULL, NULL} }; #else /* }{ */ static int tonum (lua_State *L, int arg) { if (lua_type(L, arg) == LUA_TNUMBER) { /* already a number? */ lua_pushvalue(L, arg); return 1; } else { /* check whether it is a numerical string */ size_t len; const char *s = lua_tolstring(L, arg, &len); return (s != NULL && lua_stringtonumber(L, s) == len + 1); } } /* ** To be here, either the first operand was a string or the first ** operand didn't have a corresponding metamethod. (Otherwise, that ** other metamethod would have been called.) So, if this metamethod ** doesn't work, the only other option would be for the second ** operand to have a different metamethod. */ static void trymt (lua_State *L, const char *mtkey, const char *opname) { lua_settop(L, 2); /* back to the original arguments */ if (l_unlikely(lua_type(L, 2) == LUA_TSTRING || !luaL_getmetafield(L, 2, mtkey))) luaL_error(L, "attempt to %s a '%s' with a '%s'", opname, luaL_typename(L, -2), luaL_typename(L, -1)); lua_insert(L, -3); /* put metamethod before arguments */ lua_call(L, 2, 1); /* call metamethod */ } static int arith (lua_State *L, int op, const char *mtname) { if (tonum(L, 1) && tonum(L, 2)) lua_arith(L, op); /* result will be on the top */ else trymt(L, mtname, mtname + 2); return 1; } static int arith_add (lua_State *L) { return arith(L, LUA_OPADD, "__add"); } static int arith_sub (lua_State *L) { return arith(L, LUA_OPSUB, "__sub"); } static int arith_mul (lua_State *L) { return arith(L, LUA_OPMUL, "__mul"); } static int arith_mod (lua_State *L) { return arith(L, LUA_OPMOD, "__mod"); } static int arith_pow (lua_State *L) { return arith(L, LUA_OPPOW, "__pow"); } static int arith_div (lua_State *L) { return arith(L, LUA_OPDIV, "__div"); } static int arith_idiv (lua_State *L) { return arith(L, LUA_OPIDIV, "__idiv"); } static int arith_unm (lua_State *L) { return arith(L, LUA_OPUNM, "__unm"); } static const luaL_Reg stringmetamethods[] = { {"__add", arith_add}, {"__sub", arith_sub}, {"__mul", arith_mul}, {"__mod", arith_mod}, {"__pow", arith_pow}, {"__div", arith_div}, {"__idiv", arith_idiv}, {"__unm", arith_unm}, {"__index", NULL}, /* placeholder */ {NULL, NULL} }; #endif /* } */ /* }====================================================== */ /* ** {====================================================== ** PATTERN MATCHING ** ======================================================= */ #define CAP_UNFINISHED (-1) #define CAP_POSITION (-2) typedef struct MatchState { const char *src_init; /* init of source string */ const char *src_end; /* end ('\0') of source string */ const char *p_end; /* end ('\0') of pattern */ lua_State *L; int matchdepth; /* control for recursive depth (to avoid C stack overflow) */ int level; /* total number of captures (finished or unfinished) */ struct { const char *init; ptrdiff_t len; /* length or special value (CAP_*) */ } capture[LUA_MAXCAPTURES]; } MatchState; /* recursive function */ static const char *match (MatchState *ms, const char *s, const char *p); /* maximum recursion depth for 'match' */ #if !defined(MAXCCALLS) #define MAXCCALLS 200 #endif #define L_ESC '%' #define SPECIALS "^$*+?.([%-" static int check_capture (MatchState *ms, int l) { l -= '1'; if (l_unlikely(l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)) return luaL_error(ms->L, "invalid capture index %%%d", l + 1); return l; } static int capture_to_close (MatchState *ms) { int level = ms->level; for (level--; level>=0; level--) if (ms->capture[level].len == CAP_UNFINISHED) return level; return luaL_error(ms->L, "invalid pattern capture"); } static const char *classend (MatchState *ms, const char *p) { switch (*p++) { case L_ESC: { if (l_unlikely(p == ms->p_end)) luaL_error(ms->L, "malformed pattern (ends with '%%')"); return p+1; } case '[': { if (*p == '^') p++; do { /* look for a ']' */ if (l_unlikely(p == ms->p_end)) luaL_error(ms->L, "malformed pattern (missing ']')"); if (*(p++) == L_ESC && p < ms->p_end) p++; /* skip escapes (e.g. '%]') */ } while (*p != ']'); return p+1; } default: { return p; } } } static int match_class (int c, int cl) { int res; switch (tolower(cl)) { case 'a' : res = isalpha(c); break; case 'c' : res = iscntrl(c); break; case 'd' : res = isdigit(c); break; case 'g' : res = isgraph(c); break; case 'l' : res = islower(c); break; case 'p' : res = ispunct(c); break; case 's' : res = isspace(c); break; case 'u' : res = isupper(c); break; case 'w' : res = isalnum(c); break; case 'x' : res = isxdigit(c); break; case 'z' : res = (c == 0); break; /* deprecated option */ default: return (cl == c); } return (islower(cl) ? res : !res); } static int matchbracketclass (int c, const char *p, const char *ec) { int sig = 1; if (*(p+1) == '^') { sig = 0; p++; /* skip the '^' */ } while (++p < ec) { if (*p == L_ESC) { p++; if (match_class(c, cast_uchar(*p))) return sig; } else if ((*(p+1) == '-') && (p+2 < ec)) { p+=2; if (cast_uchar(*(p-2)) <= c && c <= cast_uchar(*p)) return sig; } else if (cast_uchar(*p) == c) return sig; } return !sig; } static int singlematch (MatchState *ms, const char *s, const char *p, const char *ep) { if (s >= ms->src_end) return 0; else { int c = cast_uchar(*s); switch (*p) { case '.': return 1; /* matches any char */ case L_ESC: return match_class(c, cast_uchar(*(p+1))); case '[': return matchbracketclass(c, p, ep-1); default: return (cast_uchar(*p) == c); } } } static const char *matchbalance (MatchState *ms, const char *s, const char *p) { if (l_unlikely(p >= ms->p_end - 1)) luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')"); if (*s != *p) return NULL; else { int b = *p; int e = *(p+1); int cont = 1; while (++s < ms->src_end) { if (*s == e) { if (--cont == 0) return s+1; } else if (*s == b) cont++; } } return NULL; /* string ends out of balance */ } static const char *max_expand (MatchState *ms, const char *s, const char *p, const char *ep) { ptrdiff_t i = 0; /* counts maximum expand for item */ while (singlematch(ms, s + i, p, ep)) i++; /* keeps trying to match with the maximum repetitions */ while (i>=0) { const char *res = match(ms, (s+i), ep+1); if (res) return res; i--; /* else didn't match; reduce 1 repetition to try again */ } return NULL; } static const char *min_expand (MatchState *ms, const char *s, const char *p, const char *ep) { for (;;) { const char *res = match(ms, s, ep+1); if (res != NULL) return res; else if (singlematch(ms, s, p, ep)) s++; /* try with one more repetition */ else return NULL; } } static const char *start_capture (MatchState *ms, const char *s, const char *p, int what) { const char *res; int level = ms->level; if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures"); ms->capture[level].init = s; ms->capture[level].len = what; ms->level = level+1; if ((res=match(ms, s, p)) == NULL) /* match failed? */ ms->level--; /* undo capture */ return res; } static const char *end_capture (MatchState *ms, const char *s, const char *p) { int l = capture_to_close(ms); const char *res; ms->capture[l].len = s - ms->capture[l].init; /* close capture */ if ((res = match(ms, s, p)) == NULL) /* match failed? */ ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ return res; } static const char *match_capture (MatchState *ms, const char *s, int l) { size_t len; l = check_capture(ms, l); len = cast_sizet(ms->capture[l].len); if ((size_t)(ms->src_end-s) >= len && memcmp(ms->capture[l].init, s, len) == 0) return s+len; else return NULL; } static const char *match (MatchState *ms, const char *s, const char *p) { if (l_unlikely(ms->matchdepth-- == 0)) luaL_error(ms->L, "pattern too complex"); init: /* using goto to optimize tail recursion */ if (p != ms->p_end) { /* end of pattern? */ switch (*p) { case '(': { /* start capture */ if (*(p + 1) == ')') /* position capture? */ s = start_capture(ms, s, p + 2, CAP_POSITION); else s = start_capture(ms, s, p + 1, CAP_UNFINISHED); break; } case ')': { /* end capture */ s = end_capture(ms, s, p + 1); break; } case '$': { if ((p + 1) != ms->p_end) /* is the '$' the last char in pattern? */ goto dflt; /* no; go to default */ s = (s == ms->src_end) ? s : NULL; /* check end of string */ break; } case L_ESC: { /* escaped sequences not in the format class[*+?-]? */ switch (*(p + 1)) { case 'b': { /* balanced string? */ s = matchbalance(ms, s, p + 2); if (s != NULL) { p += 4; goto init; /* return match(ms, s, p + 4); */ } /* else fail (s == NULL) */ break; } case 'f': { /* frontier? */ const char *ep; char previous; p += 2; if (l_unlikely(*p != '[')) luaL_error(ms->L, "missing '[' after '%%f' in pattern"); ep = classend(ms, p); /* points to what is next */ previous = (s == ms->src_init) ? '\0' : *(s - 1); if (!matchbracketclass(cast_uchar(previous), p, ep - 1) && matchbracketclass(cast_uchar(*s), p, ep - 1)) { p = ep; goto init; /* return match(ms, s, ep); */ } s = NULL; /* match failed */ break; } case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { /* capture results (%0-%9)? */ s = match_capture(ms, s, cast_uchar(*(p + 1))); if (s != NULL) { p += 2; goto init; /* return match(ms, s, p + 2) */ } break; } default: goto dflt; } break; } default: dflt: { /* pattern class plus optional suffix */ const char *ep = classend(ms, p); /* points to optional suffix */ /* does not match at least once? */ if (!singlematch(ms, s, p, ep)) { if (*ep == '*' || *ep == '?' || *ep == '-') { /* accept empty? */ p = ep + 1; goto init; /* return match(ms, s, ep + 1); */ } else /* '+' or no suffix */ s = NULL; /* fail */ } else { /* matched once */ switch (*ep) { /* handle optional suffix */ case '?': { /* optional */ const char *res; if ((res = match(ms, s + 1, ep + 1)) != NULL) s = res; else { p = ep + 1; goto init; /* else return match(ms, s, ep + 1); */ } break; } case '+': /* 1 or more repetitions */ s++; /* 1 match already done */ /* FALLTHROUGH */ case '*': /* 0 or more repetitions */ s = max_expand(ms, s, p, ep); break; case '-': /* 0 or more repetitions (minimum) */ s = min_expand(ms, s, p, ep); break; default: /* no suffix */ s++; p = ep; goto init; /* return match(ms, s + 1, ep); */ } } break; } } } ms->matchdepth++; return s; } static const char *lmemfind (const char *s1, size_t l1, const char *s2, size_t l2) { if (l2 == 0) return s1; /* empty strings are everywhere */ else if (l2 > l1) return NULL; /* avoids a negative 'l1' */ else { const char *init; /* to search for a '*s2' inside 's1' */ l2--; /* 1st char will be checked by 'memchr' */ l1 = l1-l2; /* 's2' cannot be found after that */ while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) { init++; /* 1st char is already checked */ if (memcmp(init, s2+1, l2) == 0) return init-1; else { /* correct 'l1' and 's1' to try again */ l1 -= ct_diff2sz(init - s1); s1 = init; } } return NULL; /* not found */ } } /* ** get information about the i-th capture. If there are no captures ** and 'i==0', return information about the whole match, which ** is the range 's'..'e'. If the capture is a string, return ** its length and put its address in '*cap'. If it is an integer ** (a position), push it on the stack and return CAP_POSITION. */ static ptrdiff_t get_onecapture (MatchState *ms, int i, const char *s, const char *e, const char **cap) { if (i >= ms->level) { if (l_unlikely(i != 0)) luaL_error(ms->L, "invalid capture index %%%d", i + 1); *cap = s; return (e - s); } else { ptrdiff_t capl = ms->capture[i].len; *cap = ms->capture[i].init; if (l_unlikely(capl == CAP_UNFINISHED)) luaL_error(ms->L, "unfinished capture"); else if (capl == CAP_POSITION) lua_pushinteger(ms->L, ct_diff2S(ms->capture[i].init - ms->src_init) + 1); return capl; } } /* ** Push the i-th capture on the stack. */ static void push_onecapture (MatchState *ms, int i, const char *s, const char *e) { const char *cap; ptrdiff_t l = get_onecapture(ms, i, s, e, &cap); if (l != CAP_POSITION) lua_pushlstring(ms->L, cap, cast_sizet(l)); /* else position was already pushed */ } static int push_captures (MatchState *ms, const char *s, const char *e) { int i; int nlevels = (ms->level == 0 && s) ? 1 : ms->level; luaL_checkstack(ms->L, nlevels, "too many captures"); for (i = 0; i < nlevels; i++) push_onecapture(ms, i, s, e); return nlevels; /* number of strings pushed */ } /* check whether pattern has no special characters */ static int nospecials (const char *p, size_t l) { size_t upto = 0; do { if (strpbrk(p + upto, SPECIALS)) return 0; /* pattern has a special character */ upto += strlen(p + upto) + 1; /* may have more after \0 */ } while (upto <= l); return 1; /* no special chars found */ } static void prepstate (MatchState *ms, lua_State *L, const char *s, size_t ls, const char *p, size_t lp) { ms->L = L; ms->matchdepth = MAXCCALLS; ms->src_init = s; ms->src_end = s + ls; ms->p_end = p + lp; } static void reprepstate (MatchState *ms) { ms->level = 0; lua_assert(ms->matchdepth == MAXCCALLS); } static int str_find_aux (lua_State *L, int find) { size_t ls, lp; const char *s = luaL_checklstring(L, 1, &ls); const char *p = luaL_checklstring(L, 2, &lp); size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1; if (init > ls) { /* start after string's end? */ luaL_pushfail(L); /* cannot find anything */ return 1; } /* explicit request or no special characters? */ if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) { /* do a plain search */ const char *s2 = lmemfind(s + init, ls - init, p, lp); if (s2) { lua_pushinteger(L, ct_diff2S(s2 - s) + 1); lua_pushinteger(L, cast_st2S(ct_diff2sz(s2 - s) + lp)); return 2; } } else { MatchState ms; const char *s1 = s + init; int anchor = (*p == '^'); if (anchor) { p++; lp--; /* skip anchor character */ } prepstate(&ms, L, s, ls, p, lp); do { const char *res; reprepstate(&ms); if ((res=match(&ms, s1, p)) != NULL) { if (find) { lua_pushinteger(L, ct_diff2S(s1 - s) + 1); /* start */ lua_pushinteger(L, ct_diff2S(res - s)); /* end */ return push_captures(&ms, NULL, 0) + 2; } else return push_captures(&ms, s1, res); } } while (s1++ < ms.src_end && !anchor); } luaL_pushfail(L); /* not found */ return 1; } static int str_find (lua_State *L) { return str_find_aux(L, 1); } static int str_match (lua_State *L) { return str_find_aux(L, 0); } /* state for 'gmatch' */ typedef struct GMatchState { const char *src; /* current position */ const char *p; /* pattern */ const char *lastmatch; /* end of last match */ MatchState ms; /* match state */ } GMatchState; static int gmatch_aux (lua_State *L) { GMatchState *gm = (GMatchState *)lua_touserdata(L, lua_upvalueindex(3)); const char *src; gm->ms.L = L; for (src = gm->src; src <= gm->ms.src_end; src++) { const char *e; reprepstate(&gm->ms); if ((e = match(&gm->ms, src, gm->p)) != NULL && e != gm->lastmatch) { gm->src = gm->lastmatch = e; return push_captures(&gm->ms, src, e); } } return 0; /* not found */ } static int gmatch (lua_State *L) { size_t ls, lp; const char *s = luaL_checklstring(L, 1, &ls); const char *p = luaL_checklstring(L, 2, &lp); size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1; GMatchState *gm; lua_settop(L, 2); /* keep strings on closure to avoid being collected */ gm = (GMatchState *)lua_newuserdatauv(L, sizeof(GMatchState), 0); if (init > ls) /* start after string's end? */ init = ls + 1; /* avoid overflows in 's + init' */ prepstate(&gm->ms, L, s, ls, p, lp); gm->src = s + init; gm->p = p; gm->lastmatch = NULL; lua_pushcclosure(L, gmatch_aux, 3); return 1; } static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, const char *e) { size_t l; lua_State *L = ms->L; const char *news = lua_tolstring(L, 3, &l); const char *p; while ((p = (char *)memchr(news, L_ESC, l)) != NULL) { luaL_addlstring(b, news, ct_diff2sz(p - news)); p++; /* skip ESC */ if (*p == L_ESC) /* '%%' */ luaL_addchar(b, *p); else if (*p == '0') /* '%0' */ luaL_addlstring(b, s, ct_diff2sz(e - s)); else if (isdigit(cast_uchar(*p))) { /* '%n' */ const char *cap; ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap); if (resl == CAP_POSITION) luaL_addvalue(b); /* add position to accumulated result */ else luaL_addlstring(b, cap, cast_sizet(resl)); } else luaL_error(L, "invalid use of '%c' in replacement string", L_ESC); l -= ct_diff2sz(p + 1 - news); news = p + 1; } luaL_addlstring(b, news, l); } /* ** Add the replacement value to the string buffer 'b'. ** Return true if the original string was changed. (Function calls and ** table indexing resulting in nil or false do not change the subject.) */ static int add_value (MatchState *ms, luaL_Buffer *b, const char *s, const char *e, int tr) { lua_State *L = ms->L; switch (tr) { case LUA_TFUNCTION: { /* call the function */ int n; lua_pushvalue(L, 3); /* push the function */ n = push_captures(ms, s, e); /* all captures as arguments */ lua_call(L, n, 1); /* call it */ break; } case LUA_TTABLE: { /* index the table */ push_onecapture(ms, 0, s, e); /* first capture is the index */ lua_gettable(L, 3); break; } default: { /* LUA_TNUMBER or LUA_TSTRING */ add_s(ms, b, s, e); /* add value to the buffer */ return 1; /* something changed */ } } if (!lua_toboolean(L, -1)) { /* nil or false? */ lua_pop(L, 1); /* remove value */ luaL_addlstring(b, s, ct_diff2sz(e - s)); /* keep original text */ return 0; /* no changes */ } else if (l_unlikely(!lua_isstring(L, -1))) return luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); else { luaL_addvalue(b); /* add result to accumulator */ return 1; /* something changed */ } } static int str_gsub (lua_State *L) { size_t srcl, lp; const char *src = luaL_checklstring(L, 1, &srcl); /* subject */ const char *p = luaL_checklstring(L, 2, &lp); /* pattern */ const char *lastmatch = NULL; /* end of last match */ int tr = lua_type(L, 3); /* replacement type */ /* max replacements */ lua_Integer max_s = luaL_optinteger(L, 4, cast_st2S(srcl) + 1); int anchor = (*p == '^'); lua_Integer n = 0; /* replacement count */ int changed = 0; /* change flag */ MatchState ms; luaL_Buffer b; luaL_argexpected(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, "string/function/table"); luaL_buffinit(L, &b); if (anchor) { p++; lp--; /* skip anchor character */ } prepstate(&ms, L, src, srcl, p, lp); while (n < max_s) { const char *e; reprepstate(&ms); /* (re)prepare state for new match */ if ((e = match(&ms, src, p)) != NULL && e != lastmatch) { /* match? */ n++; changed = add_value(&ms, &b, src, e, tr) | changed; src = lastmatch = e; } else if (src < ms.src_end) /* otherwise, skip one character */ luaL_addchar(&b, *src++); else break; /* end of subject */ if (anchor) break; } if (!changed) /* no changes? */ lua_pushvalue(L, 1); /* return original string */ else { /* something changed */ luaL_addlstring(&b, src, ct_diff2sz(ms.src_end - src)); luaL_pushresult(&b); /* create and return new string */ } lua_pushinteger(L, n); /* number of substitutions */ return 2; } /* }====================================================== */ /* ** {====================================================== ** STRING FORMAT ** ======================================================= */ #if !defined(lua_number2strx) /* { */ /* ** Hexadecimal floating-point formatter */ #define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char)) /* ** Number of bits that goes into the first digit. It can be any value ** between 1 and 4; the following definition tries to align the number ** to nibble boundaries by making what is left after that first digit a ** multiple of 4. */ #define L_NBFD ((l_floatatt(MANT_DIG) - 1)%4 + 1) /* ** Add integer part of 'x' to buffer and return new 'x' */ static lua_Number adddigit (char *buff, unsigned n, lua_Number x) { lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */ int d = (int)dd; buff[n] = cast_char(d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */ return x - dd; /* return what is left */ } static int num2straux (char *buff, unsigned sz, lua_Number x) { /* if 'inf' or 'NaN', format it like '%g' */ if (x != x || x == (lua_Number)HUGE_VAL || x == -(lua_Number)HUGE_VAL) return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)x); else if (x == 0) { /* can be -0... */ /* create "0" or "-0" followed by exponent */ return l_sprintf(buff, sz, LUA_NUMBER_FMT "x0p+0", (LUAI_UACNUMBER)x); } else { int e; lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */ unsigned n = 0; /* character count */ if (m < 0) { /* is number negative? */ buff[n++] = '-'; /* add sign */ m = -m; /* make it positive */ } buff[n++] = '0'; buff[n++] = 'x'; /* add "0x" */ m = adddigit(buff, n++, m * (1 << L_NBFD)); /* add first digit */ e -= L_NBFD; /* this digit goes before the radix point */ if (m > 0) { /* more digits? */ buff[n++] = lua_getlocaledecpoint(); /* add radix point */ do { /* add as many digits as needed */ m = adddigit(buff, n++, m * 16); } while (m > 0); } n += cast_uint(l_sprintf(buff + n, sz - n, "p%+d", e)); /* add exponent */ lua_assert(n < sz); return cast_int(n); } } static int lua_number2strx (lua_State *L, char *buff, unsigned sz, const char *fmt, lua_Number x) { int n = num2straux(buff, sz, x); if (fmt[SIZELENMOD] == 'A') { int i; for (i = 0; i < n; i++) buff[i] = cast_char(toupper(cast_uchar(buff[i]))); } else if (l_unlikely(fmt[SIZELENMOD] != 'a')) return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); return n; } #endif /* } */ /* ** Maximum size for items formatted with '%f'. This size is produced ** by format('%.99f', -maxfloat), and is equal to 99 + 3 ('-', '.', ** and '\0') + number of decimal digits to represent maxfloat (which ** is maximum exponent + 1). (99+3+1, adding some extra, 110) */ #define MAX_ITEMF (110 + l_floatatt(MAX_10_EXP)) /* ** All formats except '%f' do not need that large limit. The other ** float formats use exponents, so that they fit in the 99 limit for ** significant digits; 's' for large strings and 'q' add items directly ** to the buffer; all integer formats also fit in the 99 limit. The ** worst case are floats: they may need 99 significant digits, plus ** '0x', '-', '.', 'e+XXXX', and '\0'. Adding some extra, 120. */ #define MAX_ITEM 120 /* valid flags in a format specification */ #if !defined(L_FMTFLAGSF) /* valid flags for a, A, e, E, f, F, g, and G conversions */ #define L_FMTFLAGSF "-+#0 " /* valid flags for o, x, and X conversions */ #define L_FMTFLAGSX "-#0" /* valid flags for d and i conversions */ #define L_FMTFLAGSI "-+0 " /* valid flags for u conversions */ #define L_FMTFLAGSU "-0" /* valid flags for c, p, and s conversions */ #define L_FMTFLAGSC "-" #endif /* ** Maximum size of each format specification (such as "%-099.99d"): ** Initial '%', flags (up to 5), width (2), period, precision (2), ** length modifier (8), conversion specifier, and final '\0', plus some ** extra. */ #define MAX_FORMAT 32 static void addquoted (luaL_Buffer *b, const char *s, size_t len) { luaL_addchar(b, '"'); while (len--) { if (*s == '"' || *s == '\\' || *s == '\n') { luaL_addchar(b, '\\'); luaL_addchar(b, *s); } else if (iscntrl(cast_uchar(*s))) { char buff[10]; if (!isdigit(cast_uchar(*(s+1)))) l_sprintf(buff, sizeof(buff), "\\%d", (int)cast_uchar(*s)); else l_sprintf(buff, sizeof(buff), "\\%03d", (int)cast_uchar(*s)); luaL_addstring(b, buff); } else luaL_addchar(b, *s); s++; } luaL_addchar(b, '"'); } /* ** Serialize a floating-point number in such a way that it can be ** scanned back by Lua. Use hexadecimal format for "common" numbers ** (to preserve precision); inf, -inf, and NaN are handled separately. ** (NaN cannot be expressed as a numeral, so we write '(0/0)' for it.) */ static int quotefloat (lua_State *L, char *buff, lua_Number n) { const char *s; /* for the fixed representations */ if (n == (lua_Number)HUGE_VAL) /* inf? */ s = "1e9999"; else if (n == -(lua_Number)HUGE_VAL) /* -inf? */ s = "-1e9999"; else if (n != n) /* NaN? */ s = "(0/0)"; else { /* format number as hexadecimal */ int nb = lua_number2strx(L, buff, MAX_ITEM, "%" LUA_NUMBER_FRMLEN "a", n); /* ensures that 'buff' string uses a dot as the radix character */ if (memchr(buff, '.', cast_uint(nb)) == NULL) { /* no dot? */ char point = lua_getlocaledecpoint(); /* try locale point */ char *ppoint = (char *)memchr(buff, point, cast_uint(nb)); if (ppoint) *ppoint = '.'; /* change it to a dot */ } return nb; } /* for the fixed representations */ return l_sprintf(buff, MAX_ITEM, "%s", s); } static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { switch (lua_type(L, arg)) { case LUA_TSTRING: { size_t len; const char *s = lua_tolstring(L, arg, &len); addquoted(b, s, len); break; } case LUA_TNUMBER: { char *buff = luaL_prepbuffsize(b, MAX_ITEM); int nb; if (!lua_isinteger(L, arg)) /* float? */ nb = quotefloat(L, buff, lua_tonumber(L, arg)); else { /* integers */ lua_Integer n = lua_tointeger(L, arg); const char *format = (n == LUA_MININTEGER) /* corner case? */ ? "0x%" LUA_INTEGER_FRMLEN "x" /* use hex */ : LUA_INTEGER_FMT; /* else use default format */ nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n); } luaL_addsize(b, cast_uint(nb)); break; } case LUA_TNIL: case LUA_TBOOLEAN: { luaL_tolstring(L, arg, NULL); luaL_addvalue(b); break; } default: { luaL_argerror(L, arg, "value has no literal form"); } } } static const char *get2digits (const char *s) { if (isdigit(cast_uchar(*s))) { s++; if (isdigit(cast_uchar(*s))) s++; /* (2 digits at most) */ } return s; } /* ** Check whether a conversion specification is valid. When called, ** first character in 'form' must be '%' and last character must ** be a valid conversion specifier. 'flags' are the accepted flags; ** 'precision' signals whether to accept a precision. */ static void checkformat (lua_State *L, const char *form, const char *flags, int precision) { const char *spec = form + 1; /* skip '%' */ spec += strspn(spec, flags); /* skip flags */ if (*spec != '0') { /* a width cannot start with '0' */ spec = get2digits(spec); /* skip width */ if (*spec == '.' && precision) { spec++; spec = get2digits(spec); /* skip precision */ } } if (!isalpha(cast_uchar(*spec))) /* did not go to the end? */ luaL_error(L, "invalid conversion specification: '%s'", form); } /* ** Get a conversion specification and copy it to 'form'. ** Return the address of its last character. */ static const char *getformat (lua_State *L, const char *strfrmt, char *form) { /* spans flags, width, and precision ('0' is included as a flag) */ size_t len = strspn(strfrmt, L_FMTFLAGSF "123456789."); len++; /* adds following character (should be the specifier) */ /* still needs space for '%', '\0', plus a length modifier */ if (len >= MAX_FORMAT - 10) luaL_error(L, "invalid format (too long)"); *(form++) = '%'; memcpy(form, strfrmt, len * sizeof(char)); *(form + len) = '\0'; return strfrmt + len - 1; } /* ** add length modifier into formats */ static void addlenmod (char *form, const char *lenmod) { size_t l = strlen(form); size_t lm = strlen(lenmod); char spec = form[l - 1]; strcpy(form + l - 1, lenmod); form[l + lm - 1] = spec; form[l + lm] = '\0'; } static int str_format (lua_State *L) { int top = lua_gettop(L); int arg = 1; size_t sfl; const char *strfrmt = luaL_checklstring(L, arg, &sfl); const char *strfrmt_end = strfrmt+sfl; const char *flags; luaL_Buffer b; luaL_buffinit(L, &b); while (strfrmt < strfrmt_end) { if (*strfrmt != L_ESC) luaL_addchar(&b, *strfrmt++); else if (*++strfrmt == L_ESC) luaL_addchar(&b, *strfrmt++); /* %% */ else { /* format item */ char form[MAX_FORMAT]; /* to store the format ('%...') */ unsigned maxitem = MAX_ITEM; /* maximum length for the result */ char *buff = luaL_prepbuffsize(&b, maxitem); /* to put result */ int nb = 0; /* number of bytes in result */ if (++arg > top) return luaL_argerror(L, arg, "no value"); strfrmt = getformat(L, strfrmt, form); switch (*strfrmt++) { case 'c': { checkformat(L, form, L_FMTFLAGSC, 0); nb = l_sprintf(buff, maxitem, form, (int)luaL_checkinteger(L, arg)); break; } case 'd': case 'i': flags = L_FMTFLAGSI; goto intcase; case 'u': flags = L_FMTFLAGSU; goto intcase; case 'o': case 'x': case 'X': flags = L_FMTFLAGSX; intcase: { lua_Integer n = luaL_checkinteger(L, arg); checkformat(L, form, flags, 1); addlenmod(form, LUA_INTEGER_FRMLEN); nb = l_sprintf(buff, maxitem, form, (LUAI_UACINT)n); break; } case 'a': case 'A': checkformat(L, form, L_FMTFLAGSF, 1); addlenmod(form, LUA_NUMBER_FRMLEN); nb = lua_number2strx(L, buff, maxitem, form, luaL_checknumber(L, arg)); break; case 'f': maxitem = MAX_ITEMF; /* extra space for '%f' */ buff = luaL_prepbuffsize(&b, maxitem); /* FALLTHROUGH */ case 'e': case 'E': case 'g': case 'G': { lua_Number n = luaL_checknumber(L, arg); checkformat(L, form, L_FMTFLAGSF, 1); addlenmod(form, LUA_NUMBER_FRMLEN); nb = l_sprintf(buff, maxitem, form, (LUAI_UACNUMBER)n); break; } case 'p': { const void *p = lua_topointer(L, arg); checkformat(L, form, L_FMTFLAGSC, 0); if (p == NULL) { /* avoid calling 'printf' with argument NULL */ p = "(null)"; /* result */ form[strlen(form) - 1] = 's'; /* format it as a string */ } nb = l_sprintf(buff, maxitem, form, p); break; } case 'q': { if (form[2] != '\0') /* modifiers? */ return luaL_error(L, "specifier '%%q' cannot have modifiers"); addliteral(L, &b, arg); break; } case 's': { size_t l; const char *s = luaL_tolstring(L, arg, &l); if (form[2] == '\0') /* no modifiers? */ luaL_addvalue(&b); /* keep entire string */ else { luaL_argcheck(L, l == strlen(s), arg, "string contains zeros"); checkformat(L, form, L_FMTFLAGSC, 1); if (strchr(form, '.') == NULL && l >= 100) { /* no precision and string is too long to be formatted */ luaL_addvalue(&b); /* keep entire string */ } else { /* format the string into 'buff' */ nb = l_sprintf(buff, maxitem, form, s); lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ } } break; } default: { /* also treat cases 'pnLlh' */ return luaL_error(L, "invalid conversion '%s' to 'format'", form); } } lua_assert(cast_uint(nb) < maxitem); luaL_addsize(&b, cast_uint(nb)); } } luaL_pushresult(&b); return 1; } /* }====================================================== */ /* ** {====================================================== ** PACK/UNPACK ** ======================================================= */ /* value used for padding */ #if !defined(LUAL_PACKPADBYTE) #define LUAL_PACKPADBYTE 0x00 #endif /* maximum size for the binary representation of an integer */ #define MAXINTSIZE 16 /* number of bits in a character */ #define NB CHAR_BIT /* mask for one character (NB 1's) */ #define MC ((1 << NB) - 1) /* size of a lua_Integer */ #define SZINT ((int)sizeof(lua_Integer)) /* dummy union to get native endianness */ static const union { int dummy; char little; /* true iff machine is little endian */ } nativeendian = {1}; /* ** information to pack/unpack stuff */ typedef struct Header { lua_State *L; int islittle; unsigned maxalign; } Header; /* ** options for pack/unpack */ typedef enum KOption { Kint, /* signed integers */ Kuint, /* unsigned integers */ Kfloat, /* single-precision floating-point numbers */ Knumber, /* Lua "native" floating-point numbers */ Kdouble, /* double-precision floating-point numbers */ Kchar, /* fixed-length strings */ Kstring, /* strings with prefixed length */ Kzstr, /* zero-terminated strings */ Kpadding, /* padding */ Kpaddalign, /* padding for alignment */ Knop /* no-op (configuration or spaces) */ } KOption; /* ** Read an integer numeral from string 'fmt' or return 'df' if ** there is no numeral */ static int digit (int c) { return '0' <= c && c <= '9'; } static size_t getnum (const char **fmt, size_t df) { if (!digit(**fmt)) /* no number? */ return df; /* return default value */ else { size_t a = 0; do { a = a*10 + cast_uint(*((*fmt)++) - '0'); } while (digit(**fmt) && a <= (MAX_SIZE - 9)/10); return a; } } /* ** Read an integer numeral and raises an error if it is larger ** than the maximum size of integers. */ static unsigned getnumlimit (Header *h, const char **fmt, size_t df) { size_t sz = getnum(fmt, df); if (l_unlikely((sz - 1u) >= MAXINTSIZE)) return cast_uint(luaL_error(h->L, "integral size (%d) out of limits [1,%d]", sz, MAXINTSIZE)); return cast_uint(sz); } /* ** Initialize Header */ static void initheader (lua_State *L, Header *h) { h->L = L; h->islittle = nativeendian.little; h->maxalign = 1; } /* ** Read and classify next option. 'size' is filled with option's size. */ static KOption getoption (Header *h, const char **fmt, size_t *size) { /* dummy structure to get native alignment requirements */ struct cD { char c; union { LUAI_MAXALIGN; } u; }; int opt = *((*fmt)++); *size = 0; /* default */ switch (opt) { case 'b': *size = sizeof(char); return Kint; case 'B': *size = sizeof(char); return Kuint; case 'h': *size = sizeof(short); return Kint; case 'H': *size = sizeof(short); return Kuint; case 'l': *size = sizeof(long); return Kint; case 'L': *size = sizeof(long); return Kuint; case 'j': *size = sizeof(lua_Integer); return Kint; case 'J': *size = sizeof(lua_Integer); return Kuint; case 'T': *size = sizeof(size_t); return Kuint; case 'f': *size = sizeof(float); return Kfloat; case 'n': *size = sizeof(lua_Number); return Knumber; case 'd': *size = sizeof(double); return Kdouble; case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint; case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint; case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring; case 'c': *size = getnum(fmt, cast_sizet(-1)); if (l_unlikely(*size == cast_sizet(-1))) luaL_error(h->L, "missing size for format option 'c'"); return Kchar; case 'z': return Kzstr; case 'x': *size = 1; return Kpadding; case 'X': return Kpaddalign; case ' ': break; case '<': h->islittle = 1; break; case '>': h->islittle = 0; break; case '=': h->islittle = nativeendian.little; break; case '!': { const size_t maxalign = offsetof(struct cD, u); h->maxalign = getnumlimit(h, fmt, maxalign); break; } default: luaL_error(h->L, "invalid format option '%c'", opt); } return Knop; } /* ** Read, classify, and fill other details about the next option. ** 'psize' is filled with option's size, 'notoalign' with its ** alignment requirements. ** Local variable 'size' gets the size to be aligned. (Kpadal option ** always gets its full alignment, other options are limited by ** the maximum alignment ('maxalign'). Kchar option needs no alignment ** despite its size. */ static KOption getdetails (Header *h, size_t totalsize, const char **fmt, size_t *psize, unsigned *ntoalign) { KOption opt = getoption(h, fmt, psize); size_t align = *psize; /* usually, alignment follows size */ if (opt == Kpaddalign) { /* 'X' gets alignment from following option */ if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0) luaL_argerror(h->L, 1, "invalid next option for option 'X'"); } if (align <= 1 || opt == Kchar) /* need no alignment? */ *ntoalign = 0; else { if (align > h->maxalign) /* enforce maximum alignment */ align = h->maxalign; if (l_unlikely(!ispow2(align))) { /* not a power of 2? */ *ntoalign = 0; /* to avoid warnings */ luaL_argerror(h->L, 1, "format asks for alignment not power of 2"); } else { /* 'szmoda' = totalsize % align */ unsigned szmoda = cast_uint(totalsize & (align - 1)); *ntoalign = cast_uint((align - szmoda) & (align - 1)); } } return opt; } /* ** Pack integer 'n' with 'size' bytes and 'islittle' endianness. ** The final 'if' handles the case when 'size' is larger than ** the size of a Lua integer, correcting the extra sign-extension ** bytes if necessary (by default they would be zeros). */ static void packint (luaL_Buffer *b, lua_Unsigned n, int islittle, unsigned size, int neg) { char *buff = luaL_prepbuffsize(b, size); unsigned i; buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */ for (i = 1; i < size; i++) { n >>= NB; buff[islittle ? i : size - 1 - i] = (char)(n & MC); } if (neg && size > SZINT) { /* negative number need sign extension? */ for (i = SZINT; i < size; i++) /* correct extra bytes */ buff[islittle ? i : size - 1 - i] = (char)MC; } luaL_addsize(b, size); /* add result to buffer */ } /* ** Copy 'size' bytes from 'src' to 'dest', correcting endianness if ** given 'islittle' is different from native endianness. */ static void copywithendian (char *dest, const char *src, unsigned size, int islittle) { if (islittle == nativeendian.little) memcpy(dest, src, size); else { dest += size - 1; while (size-- != 0) *(dest--) = *(src++); } } static int str_pack (lua_State *L) { luaL_Buffer b; Header h; const char *fmt = luaL_checkstring(L, 1); /* format string */ int arg = 1; /* current argument to pack */ size_t totalsize = 0; /* accumulate total size of result */ initheader(L, &h); lua_pushnil(L); /* mark to separate arguments from string buffer */ luaL_buffinit(L, &b); while (*fmt != '\0') { unsigned ntoalign; size_t size; KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); luaL_argcheck(L, size + ntoalign <= MAX_SIZE - totalsize, arg, "result too long"); totalsize += ntoalign + size; while (ntoalign-- > 0) luaL_addchar(&b, LUAL_PACKPADBYTE); /* fill alignment */ arg++; switch (opt) { case Kint: { /* signed integers */ lua_Integer n = luaL_checkinteger(L, arg); if (size < SZINT) { /* need overflow check? */ lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1); luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow"); } packint(&b, (lua_Unsigned)n, h.islittle, cast_uint(size), (n < 0)); break; } case Kuint: { /* unsigned integers */ lua_Integer n = luaL_checkinteger(L, arg); if (size < SZINT) /* need overflow check? */ luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)), arg, "unsigned overflow"); packint(&b, (lua_Unsigned)n, h.islittle, cast_uint(size), 0); break; } case Kfloat: { /* C float */ float f = (float)luaL_checknumber(L, arg); /* get argument */ char *buff = luaL_prepbuffsize(&b, sizeof(f)); /* move 'f' to final result, correcting endianness if needed */ copywithendian(buff, (char *)&f, sizeof(f), h.islittle); luaL_addsize(&b, size); break; } case Knumber: { /* Lua float */ lua_Number f = luaL_checknumber(L, arg); /* get argument */ char *buff = luaL_prepbuffsize(&b, sizeof(f)); /* move 'f' to final result, correcting endianness if needed */ copywithendian(buff, (char *)&f, sizeof(f), h.islittle); luaL_addsize(&b, size); break; } case Kdouble: { /* C double */ double f = (double)luaL_checknumber(L, arg); /* get argument */ char *buff = luaL_prepbuffsize(&b, sizeof(f)); /* move 'f' to final result, correcting endianness if needed */ copywithendian(buff, (char *)&f, sizeof(f), h.islittle); luaL_addsize(&b, size); break; } case Kchar: { /* fixed-size string */ size_t len; const char *s = luaL_checklstring(L, arg, &len); luaL_argcheck(L, len <= size, arg, "string longer than given size"); luaL_addlstring(&b, s, len); /* add string */ if (len < size) { /* does it need padding? */ size_t psize = size - len; /* pad size */ char *buff = luaL_prepbuffsize(&b, psize); memset(buff, LUAL_PACKPADBYTE, psize); luaL_addsize(&b, psize); } break; } case Kstring: { /* strings with length count */ size_t len; const char *s = luaL_checklstring(L, arg, &len); luaL_argcheck(L, size >= sizeof(lua_Unsigned) || len < ((lua_Unsigned)1 << (size * NB)), arg, "string length does not fit in given size"); /* pack length */ packint(&b, (lua_Unsigned)len, h.islittle, cast_uint(size), 0); luaL_addlstring(&b, s, len); totalsize += len; break; } case Kzstr: { /* zero-terminated string */ size_t len; const char *s = luaL_checklstring(L, arg, &len); luaL_argcheck(L, strlen(s) == len, arg, "string contains zeros"); luaL_addlstring(&b, s, len); luaL_addchar(&b, '\0'); /* add zero at the end */ totalsize += len + 1; break; } case Kpadding: luaL_addchar(&b, LUAL_PACKPADBYTE); /* FALLTHROUGH */ case Kpaddalign: case Knop: arg--; /* undo increment */ break; } } luaL_pushresult(&b); return 1; } static int str_packsize (lua_State *L) { Header h; const char *fmt = luaL_checkstring(L, 1); /* format string */ size_t totalsize = 0; /* accumulate total size of result */ initheader(L, &h); while (*fmt != '\0') { unsigned ntoalign; size_t size; KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1, "variable-length format"); size += ntoalign; /* total space used by option */ luaL_argcheck(L, totalsize <= LUA_MAXINTEGER - size, 1, "format result too large"); totalsize += size; } lua_pushinteger(L, cast_st2S(totalsize)); return 1; } /* ** Unpack an integer with 'size' bytes and 'islittle' endianness. ** If size is smaller than the size of a Lua integer and integer ** is signed, must do sign extension (propagating the sign to the ** higher bits); if size is larger than the size of a Lua integer, ** it must check the unread bytes to see whether they do not cause an ** overflow. */ static lua_Integer unpackint (lua_State *L, const char *str, int islittle, int size, int issigned) { lua_Unsigned res = 0; int i; int limit = (size <= SZINT) ? size : SZINT; for (i = limit - 1; i >= 0; i--) { res <<= NB; res |= (lua_Unsigned)(unsigned char)str[islittle ? i : size - 1 - i]; } if (size < SZINT) { /* real size smaller than lua_Integer? */ if (issigned) { /* needs sign extension? */ lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1); res = ((res ^ mask) - mask); /* do sign extension */ } } else if (size > SZINT) { /* must check unread bytes */ int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC; for (i = limit; i < size; i++) { if (l_unlikely((unsigned char)str[islittle ? i : size - 1 - i] != mask)) luaL_error(L, "%d-byte integer does not fit into Lua Integer", size); } } return (lua_Integer)res; } static int str_unpack (lua_State *L) { Header h; const char *fmt = luaL_checkstring(L, 1); size_t ld; const char *data = luaL_checklstring(L, 2, &ld); size_t pos = posrelatI(luaL_optinteger(L, 3, 1), ld) - 1; int n = 0; /* number of results */ luaL_argcheck(L, pos <= ld, 3, "initial position out of string"); initheader(L, &h); while (*fmt != '\0') { unsigned ntoalign; size_t size; KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign); luaL_argcheck(L, ntoalign + size <= ld - pos, 2, "data string too short"); pos += ntoalign; /* skip alignment */ /* stack space for item + next position */ luaL_checkstack(L, 2, "too many results"); n++; switch (opt) { case Kint: case Kuint: { lua_Integer res = unpackint(L, data + pos, h.islittle, cast_int(size), (opt == Kint)); lua_pushinteger(L, res); break; } case Kfloat: { float f; copywithendian((char *)&f, data + pos, sizeof(f), h.islittle); lua_pushnumber(L, (lua_Number)f); break; } case Knumber: { lua_Number f; copywithendian((char *)&f, data + pos, sizeof(f), h.islittle); lua_pushnumber(L, f); break; } case Kdouble: { double f; copywithendian((char *)&f, data + pos, sizeof(f), h.islittle); lua_pushnumber(L, (lua_Number)f); break; } case Kchar: { lua_pushlstring(L, data + pos, size); break; } case Kstring: { lua_Unsigned len = (lua_Unsigned)unpackint(L, data + pos, h.islittle, cast_int(size), 0); luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short"); lua_pushlstring(L, data + pos + size, cast_sizet(len)); pos += cast_sizet(len); /* skip string */ break; } case Kzstr: { size_t len = strlen(data + pos); luaL_argcheck(L, pos + len < ld, 2, "unfinished string for format 'z'"); lua_pushlstring(L, data + pos, len); pos += len + 1; /* skip string plus final '\0' */ break; } case Kpaddalign: case Kpadding: case Knop: n--; /* undo increment */ break; } pos += size; } lua_pushinteger(L, cast_st2S(pos) + 1); /* next position */ return n + 1; } /* }====================================================== */ static const luaL_Reg strlib[] = { {"byte", str_byte}, {"char", str_char}, {"dump", str_dump}, {"find", str_find}, {"format", str_format}, {"gmatch", gmatch}, {"gsub", str_gsub}, {"len", str_len}, {"lower", str_lower}, {"match", str_match}, {"rep", str_rep}, {"reverse", str_reverse}, {"sub", str_sub}, {"upper", str_upper}, {"pack", str_pack}, {"packsize", str_packsize}, {"unpack", str_unpack}, {NULL, NULL} }; static void createmetatable (lua_State *L) { /* table to be metatable for strings */ luaL_newlibtable(L, stringmetamethods); luaL_setfuncs(L, stringmetamethods, 0); lua_pushliteral(L, ""); /* dummy string */ lua_pushvalue(L, -2); /* copy table */ lua_setmetatable(L, -2); /* set table as metatable for strings */ lua_pop(L, 1); /* pop dummy string */ lua_pushvalue(L, -2); /* get string library */ lua_setfield(L, -2, "__index"); /* metatable.__index = string */ lua_pop(L, 1); /* pop metatable */ } /* ** Open string library */ LUAMOD_API int luaopen_string (lua_State *L) { luaL_newlib(L, strlib); createmetatable(L); return 1; } ================================================ FILE: 3rd/lua/ltable.c ================================================ /* ** $Id: ltable.c $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ #define ltable_c #define LUA_CORE #include "lprefix.h" /* ** Implementation of tables (aka arrays, objects, or hash tables). ** Tables keep its elements in two parts: an array part and a hash part. ** Non-negative integer keys are all candidates to be kept in the array ** part. The actual size of the array is the largest 'n' such that ** more than half the slots between 1 and n are in use. ** Hash uses a mix of chained scatter table with Brent's variation. ** A main invariant of these tables is that, if an element is not ** in its main position (i.e. the 'original' position that its hash gives ** to it), then the colliding element is in its own main position. ** Hence even when the load factor reaches 100%, performance remains good. */ #include #include #include #include "lua.h" #include "ldebug.h" #include "ldo.h" #include "lgc.h" #include "lmem.h" #include "lobject.h" #include "lstate.h" #include "lstring.h" #include "ltable.h" #include "lvm.h" /* ** Only hash parts with at least 2^LIMFORLAST have a 'lastfree' field ** that optimizes finding a free slot. That field is stored just before ** the array of nodes, in the same block. Smaller tables do a complete ** search when looking for a free slot. */ #define LIMFORLAST 3 /* log2 of real limit (8) */ /* ** The union 'Limbox' stores 'lastfree' and ensures that what follows it ** is properly aligned to store a Node. */ typedef struct { Node *dummy; Node follows_pNode; } Limbox_aux; typedef union { Node *lastfree; char padding[offsetof(Limbox_aux, follows_pNode)]; } Limbox; #define haslastfree(t) ((t)->lsizenode >= LIMFORLAST) #define getlastfree(t) ((cast(Limbox *, (t)->node) - 1)->lastfree) /* ** MAXABITS is the largest integer such that 2^MAXABITS fits in an ** unsigned int. */ #define MAXABITS (l_numbits(int) - 1) /* ** MAXASIZEB is the maximum number of elements in the array part such ** that the size of the array fits in 'size_t'. */ #define MAXASIZEB (MAX_SIZET/(sizeof(Value) + 1)) /* ** MAXASIZE is the maximum size of the array part. It is the minimum ** between 2^MAXABITS and MAXASIZEB. */ #define MAXASIZE \ (((1u << MAXABITS) < MAXASIZEB) ? (1u << MAXABITS) : cast_uint(MAXASIZEB)) /* ** MAXHBITS is the largest integer such that 2^MAXHBITS fits in a ** signed int. */ #define MAXHBITS (MAXABITS - 1) /* ** MAXHSIZE is the maximum size of the hash part. It is the minimum ** between 2^MAXHBITS and the maximum size such that, measured in bytes, ** it fits in a 'size_t'. */ #define MAXHSIZE luaM_limitN(1 << MAXHBITS, Node) /* ** When the original hash value is good, hashing by a power of 2 ** avoids the cost of '%'. */ #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) /* ** for other types, it is better to avoid modulo by power of 2, as ** they can have many 2 factors. */ #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1u)|1u)))) #define hashstr(t,str) hashpow2(t, (str)->hash) #define hashboolean(t,p) hashpow2(t, p) #define hashpointer(t,p) hashmod(t, point2uint(p)) #define dummynode (&dummynode_) /* ** Common hash part for tables with empty hash parts. That allows all ** tables to have a hash part, avoiding an extra check ("is there a hash ** part?") when indexing. Its sole node has an empty value and a key ** (DEADKEY, NULL) that is different from any valid TValue. */ static const Node dummynode_ = { {{NULL}, LUA_VEMPTY, /* value's value and type */ LUA_TDEADKEY, 0, {NULL}} /* key type, next, and key value */ }; static const TValue absentkey = {ABSTKEYCONSTANT}; /* ** Hash for integers. To allow a good hash, use the remainder operator ** ('%'). If integer fits as a non-negative int, compute an int ** remainder, which is faster. Otherwise, use an unsigned-integer ** remainder, which uses all bits and ensures a non-negative result. */ static Node *hashint (const Table *t, lua_Integer i) { lua_Unsigned ui = l_castS2U(i); if (ui <= cast_uint(INT_MAX)) return gnode(t, cast_int(ui) % cast_int((sizenode(t)-1) | 1)); else return hashmod(t, ui); } /* ** Hash for floating-point numbers. ** The main computation should be just ** n = frexp(n, &i); return (n * INT_MAX) + i ** but there are some numerical subtleties. ** In a two-complement representation, INT_MAX may not have an exact ** representation as a float, but INT_MIN does; because the absolute ** value of 'frexp' is smaller than 1 (unless 'n' is inf/NaN), the ** absolute value of the product 'frexp * -INT_MIN' is smaller or equal ** to INT_MAX. Next, the use of 'unsigned int' avoids overflows when ** adding 'i'; the use of '~u' (instead of '-u') avoids problems with ** INT_MIN. */ #if !defined(l_hashfloat) static unsigned l_hashfloat (lua_Number n) { int i; lua_Integer ni; n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN); if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */ lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL)); return 0; } else { /* normal case */ unsigned int u = cast_uint(i) + cast_uint(ni); return (u <= cast_uint(INT_MAX) ? u : ~u); } } #endif /* ** returns the 'main' position of an element in a table (that is, ** the index of its hash value). */ static Node *mainpositionTV (const Table *t, const TValue *key) { switch (ttypetag(key)) { case LUA_VNUMINT: { lua_Integer i = ivalue(key); return hashint(t, i); } case LUA_VNUMFLT: { lua_Number n = fltvalue(key); return hashmod(t, l_hashfloat(n)); } case LUA_VSHRSTR: { TString *ts = tsvalue(key); return hashstr(t, ts); } case LUA_VLNGSTR: { TString *ts = tsvalue(key); return hashpow2(t, luaS_hashlongstr(ts)); } case LUA_VFALSE: return hashboolean(t, 0); case LUA_VTRUE: return hashboolean(t, 1); case LUA_VLIGHTUSERDATA: { void *p = pvalue(key); return hashpointer(t, p); } case LUA_VLCF: { lua_CFunction f = fvalue(key); return hashpointer(t, f); } default: { GCObject *o = gcvalue(key); return hashpointer(t, o); } } } l_sinline Node *mainpositionfromnode (const Table *t, Node *nd) { TValue key; getnodekey(cast(lua_State *, NULL), &key, nd); return mainpositionTV(t, &key); } /* ** Check whether key 'k1' is equal to the key in node 'n2'. This ** equality is raw, so there are no metamethods. Floats with integer ** values have been normalized, so integers cannot be equal to ** floats. It is assumed that 'eqshrstr' is simply pointer equality, ** so that short strings are handled in the default case. The flag ** 'deadok' means to accept dead keys as equal to their original values. ** (Only collectable objects can produce dead keys.) Note that dead ** long strings are also compared by identity. Once a key is dead, ** its corresponding value may be collected, and then another value ** can be created with the same address. If this other value is given ** to 'next', 'equalkey' will signal a false positive. In a regular ** traversal, this situation should never happen, as all keys given to ** 'next' came from the table itself, and therefore could not have been ** collected. Outside a regular traversal, we have garbage in, garbage ** out. What is relevant is that this false positive does not break ** anything. (In particular, 'next' will return some other valid item ** on the table or nil.) */ static int equalkey (const TValue *k1, const Node *n2, int deadok) { if (rawtt(k1) != keytt(n2)) { /* not the same variants? */ if (keyisshrstr(n2) && ttislngstring(k1)) { /* an external string can be equal to a short-string key */ return luaS_eqstr(tsvalue(k1), keystrval(n2)); } else if (deadok && keyisdead(n2) && iscollectable(k1)) { /* a collectable value can be equal to a dead key */ return gcvalue(k1) == gcvalueraw(keyval(n2)); } else return 0; /* otherwise, different variants cannot be equal */ } else { /* equal variants */ switch (keytt(n2)) { case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: return 1; case LUA_VNUMINT: return (ivalue(k1) == keyival(n2)); case LUA_VNUMFLT: return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2))); case LUA_VLIGHTUSERDATA: return pvalue(k1) == pvalueraw(keyval(n2)); case LUA_VLCF: return fvalue(k1) == fvalueraw(keyval(n2)); case ctb(LUA_VLNGSTR): return luaS_eqstr(tsvalue(k1), keystrval(n2)); case ctb(LUA_VSHRSTR): /* short strings can be from other VM */ return eqshrstr(tsvalue(k1), keystrval(n2)); default: return gcvalue(k1) == gcvalueraw(keyval(n2)); } } } /* ** "Generic" get version. (Not that generic: not valid for integers, ** which may be in array part, nor for floats with integral values.) ** See explanation about 'deadok' in function 'equalkey'. */ static const TValue *getgeneric (Table *t, const TValue *key, int deadok) { Node *n = mainpositionTV(t, key); for (;;) { /* check whether 'key' is somewhere in the chain */ if (equalkey(key, n, deadok)) return gval(n); /* that's it */ else { int nx = gnext(n); if (nx == 0) return &absentkey; /* not found */ n += nx; } } } /* ** Return the index 'k' (converted to an unsigned) if it is inside ** the range [1, limit]. */ static unsigned checkrange (lua_Integer k, unsigned limit) { return (l_castS2U(k) - 1u < limit) ? cast_uint(k) : 0; } /* ** Return the index 'k' if 'k' is an appropriate key to live in the ** array part of a table, 0 otherwise. */ #define arrayindex(k) checkrange(k, MAXASIZE) /* ** Check whether an integer key is in the array part of a table and ** return its index there, or zero. */ #define ikeyinarray(t,k) checkrange(k, t->asize) /* ** Check whether a key is in the array part of a table and return its ** index there, or zero. */ static unsigned keyinarray (Table *t, const TValue *key) { return (ttisinteger(key)) ? ikeyinarray(t, ivalue(key)) : 0; } /* ** returns the index of a 'key' for table traversals. First goes all ** elements in the array part, then elements in the hash part. The ** beginning of a traversal is signaled by 0. */ static unsigned findindex (lua_State *L, Table *t, TValue *key, unsigned asize) { unsigned int i; if (ttisnil(key)) return 0; /* first iteration */ i = keyinarray(t, key); if (i != 0) /* is 'key' inside array part? */ return i; /* yes; that's the index */ else { const TValue *n = getgeneric(t, key, 1); if (l_unlikely(isabstkey(n))) luaG_runerror(L, "invalid key to 'next'"); /* key not found */ i = cast_uint(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ /* hash elements are numbered after array ones */ return (i + 1) + asize; } } int luaH_next (lua_State *L, Table *t, StkId key) { unsigned int asize = t->asize; unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */ for (; i < asize; i++) { /* try first array part */ lu_byte tag = *getArrTag(t, i); if (!tagisempty(tag)) { /* a non-empty entry? */ setivalue(s2v(key), cast_int(i) + 1); farr2val(t, i, tag, s2v(key + 1)); return 1; } } for (i -= asize; i < sizenode(t); i++) { /* hash part */ if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */ Node *n = gnode(t, i); getnodekey(L, s2v(key), n); setobj2s(L, key + 1, gval(n)); return 1; } } return 0; /* no more elements */ } /* Extra space in Node array if it has a lastfree entry */ #define extraLastfree(t) (haslastfree(t) ? sizeof(Limbox) : 0) /* 'node' size in bytes */ static size_t sizehash (Table *t) { return cast_sizet(sizenode(t)) * sizeof(Node) + extraLastfree(t); } static void freehash (lua_State *L, Table *t) { if (!isdummy(t)) { /* get pointer to the beginning of Node array */ char *arr = cast_charp(t->node) - extraLastfree(t); luaM_freearray(L, arr, sizehash(t)); } } /* ** {============================================================= ** Rehash ** ============================================================== */ static int insertkey (Table *t, const TValue *key, TValue *value); static void newcheckedkey (Table *t, const TValue *key, TValue *value); /* ** Structure to count the keys in a table. ** 'total' is the total number of keys in the table. ** 'na' is the number of *array indices* in the table (see 'arrayindex'). ** 'deleted' is true if there are deleted nodes in the hash part. ** 'nums' is a "count array" where 'nums[i]' is the number of integer ** keys between 2^(i - 1) + 1 and 2^i. Note that 'na' is the summation ** of 'nums'. */ typedef struct { unsigned total; unsigned na; int deleted; unsigned nums[MAXABITS + 1]; } Counters; /* ** Check whether it is worth to use 'na' array entries instead of 'nh' ** hash nodes. (A hash node uses ~3 times more memory than an array ** entry: Two values plus 'next' versus one value.) Evaluate with size_t ** to avoid overflows. */ #define arrayXhash(na,nh) (cast_sizet(na) <= cast_sizet(nh) * 3) /* ** Compute the optimal size for the array part of table 't'. ** This size maximizes the number of elements going to the array part ** while satisfying the condition 'arrayXhash' with the use of memory if ** all those elements went to the hash part. ** 'ct->na' enters with the total number of array indices in the table ** and leaves with the number of keys that will go to the array part; ** return the optimal size for the array part. */ static unsigned computesizes (Counters *ct) { int i; unsigned int twotoi; /* 2^i (candidate for optimal size) */ unsigned int a = 0; /* number of elements smaller than 2^i */ unsigned int na = 0; /* number of elements to go to array part */ unsigned int optimal = 0; /* optimal size for array part */ /* traverse slices while 'twotoi' does not overflow and total of array indices still can satisfy 'arrayXhash' against the array size */ for (i = 0, twotoi = 1; twotoi > 0 && arrayXhash(twotoi, ct->na); i++, twotoi *= 2) { unsigned nums = ct->nums[i]; a += nums; if (nums > 0 && /* grows array only if it gets more elements... */ arrayXhash(twotoi, a)) { /* ...while using "less memory" */ optimal = twotoi; /* optimal size (till now) */ na = a; /* all elements up to 'optimal' will go to array part */ } } ct->na = na; return optimal; } static void countint (lua_Integer key, Counters *ct) { unsigned int k = arrayindex(key); if (k != 0) { /* is 'key' an array index? */ ct->nums[luaO_ceillog2(k)]++; /* count as such */ ct->na++; } } l_sinline int arraykeyisempty (const Table *t, unsigned key) { int tag = *getArrTag(t, key - 1); return tagisempty(tag); } /* ** Count keys in array part of table 't'. */ static void numusearray (const Table *t, Counters *ct) { int lg; unsigned int ttlg; /* 2^lg */ unsigned int ause = 0; /* summation of 'nums' */ unsigned int i = 1; /* index to traverse all array keys */ unsigned int asize = t->asize; /* traverse each slice */ for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) { unsigned int lc = 0; /* counter */ unsigned int lim = ttlg; if (lim > asize) { lim = asize; /* adjust upper limit */ if (i > lim) break; /* no more elements to count */ } /* count elements in range (2^(lg - 1), 2^lg] */ for (; i <= lim; i++) { if (!arraykeyisempty(t, i)) lc++; } ct->nums[lg] += lc; ause += lc; } ct->total += ause; ct->na += ause; } /* ** Count keys in hash part of table 't'. As this only happens during ** a rehash, all nodes have been used. A node can have a nil value only ** if it was deleted after being created. */ static void numusehash (const Table *t, Counters *ct) { unsigned i = sizenode(t); unsigned total = 0; while (i--) { Node *n = &t->node[i]; if (isempty(gval(n))) { lua_assert(!keyisnil(n)); /* entry was deleted; key cannot be nil */ ct->deleted = 1; } else { total++; if (keyisinteger(n)) countint(keyival(n), ct); } } ct->total += total; } /* ** Convert an "abstract size" (number of slots in an array) to ** "concrete size" (number of bytes in the array). */ static size_t concretesize (unsigned int size) { if (size == 0) return 0; else /* space for the two arrays plus an unsigned in between */ return size * (sizeof(Value) + 1) + sizeof(unsigned); } /* ** Resize the array part of a table. If new size is equal to the old, ** do nothing. Else, if new size is zero, free the old array. (It must ** be present, as the sizes are different.) Otherwise, allocate a new ** array, move the common elements to new proper position, and then ** frees the old array. ** We could reallocate the array, but we still would need to move the ** elements to their new position, so the copy implicit in realloc is a ** waste. Moreover, most allocators will move the array anyway when the ** new size is double the old one (the most common case). */ static Value *resizearray (lua_State *L , Table *t, unsigned oldasize, unsigned newasize) { if (oldasize == newasize) return t->array; /* nothing to be done */ else if (newasize == 0) { /* erasing array? */ Value *op = t->array - oldasize; /* original array's real address */ luaM_freemem(L, op, concretesize(oldasize)); /* free it */ return NULL; } else { size_t newasizeb = concretesize(newasize); Value *np = cast(Value *, luaM_reallocvector(L, NULL, 0, newasizeb, lu_byte)); if (np == NULL) /* allocation error? */ return NULL; np += newasize; /* shift pointer to the end of value segment */ if (oldasize > 0) { /* move common elements to new position */ size_t oldasizeb = concretesize(oldasize); Value *op = t->array; /* original array */ unsigned tomove = (oldasize < newasize) ? oldasize : newasize; size_t tomoveb = (oldasize < newasize) ? oldasizeb : newasizeb; lua_assert(tomoveb > 0); memcpy(np - tomove, op - tomove, tomoveb); luaM_freemem(L, op - oldasize, oldasizeb); /* free old block */ } return np; } } /* ** Creates an array for the hash part of a table with the given ** size, or reuses the dummy node if size is zero. ** The computation for size overflow is in two steps: the first ** comparison ensures that the shift in the second one does not ** overflow. */ static void setnodevector (lua_State *L, Table *t, unsigned size) { if (size == 0) { /* no elements to hash part? */ t->node = cast(Node *, dummynode); /* use common 'dummynode' */ t->lsizenode = 0; setdummy(t); /* signal that it is using dummy node */ } else { int i; int lsize = luaO_ceillog2(size); if (lsize > MAXHBITS || (1 << lsize) > MAXHSIZE) luaG_runerror(L, "table overflow"); size = twoto(lsize); if (lsize < LIMFORLAST) /* no 'lastfree' field? */ t->node = luaM_newvector(L, size, Node); else { size_t bsize = size * sizeof(Node) + sizeof(Limbox); char *node = luaM_newblock(L, bsize); t->node = cast(Node *, node + sizeof(Limbox)); getlastfree(t) = gnode(t, size); /* all positions are free */ } t->lsizenode = cast_byte(lsize); setnodummy(t); for (i = 0; i < cast_int(size); i++) { Node *n = gnode(t, i); gnext(n) = 0; setnilkey(n); setempty(gval(n)); } } } /* ** (Re)insert all elements from the hash part of 'ot' into table 't'. */ static void reinserthash (lua_State *L, Table *ot, Table *t) { unsigned j; unsigned size = sizenode(ot); for (j = 0; j < size; j++) { Node *old = gnode(ot, j); if (!isempty(gval(old))) { /* doesn't need barrier/invalidate cache, as entry was already present in the table */ TValue k; getnodekey(L, &k, old); newcheckedkey(t, &k, gval(old)); } } } /* ** Exchange the hash part of 't1' and 't2'. (In 'flags', only the ** dummy bit must be exchanged: The 'isrealasize' is not related ** to the hash part, and the metamethod bits do not change during ** a resize, so the "real" table can keep their values.) */ static void exchangehashpart (Table *t1, Table *t2) { lu_byte lsizenode = t1->lsizenode; Node *node = t1->node; int bitdummy1 = t1->flags & BITDUMMY; t1->lsizenode = t2->lsizenode; t1->node = t2->node; t1->flags = cast_byte((t1->flags & NOTBITDUMMY) | (t2->flags & BITDUMMY)); t2->lsizenode = lsizenode; t2->node = node; t2->flags = cast_byte((t2->flags & NOTBITDUMMY) | bitdummy1); } /* ** Re-insert into the new hash part of a table the elements from the ** vanishing slice of the array part. */ static void reinsertOldSlice (Table *t, unsigned oldasize, unsigned newasize) { unsigned i; for (i = newasize; i < oldasize; i++) { /* traverse vanishing slice */ lu_byte tag = *getArrTag(t, i); if (!tagisempty(tag)) { /* a non-empty entry? */ TValue key, aux; setivalue(&key, l_castU2S(i) + 1); /* make the key */ farr2val(t, i, tag, &aux); /* copy value into 'aux' */ insertkey(t, &key, &aux); /* insert entry into the hash part */ } } } /* ** Clear new slice of the array. */ static void clearNewSlice (Table *t, unsigned oldasize, unsigned newasize) { for (; oldasize < newasize; oldasize++) *getArrTag(t, oldasize) = LUA_VEMPTY; } /* ** Resize table 't' for the new given sizes. Both allocations (for ** the hash part and for the array part) can fail, which creates some ** subtleties. If the first allocation, for the hash part, fails, an ** error is raised and that is it. Otherwise, it copies the elements from ** the shrinking part of the array (if it is shrinking) into the new ** hash. Then it reallocates the array part. If that fails, the table ** is in its original state; the function frees the new hash part and then ** raises the allocation error. Otherwise, it sets the new hash part ** into the table, initializes the new part of the array (if any) with ** nils and reinserts the elements of the old hash back into the new ** parts of the table. ** Note that if the new size for the array part ('newasize') is equal to ** the old one ('oldasize'), this function will do nothing with that ** part. */ void luaH_resize (lua_State *L, Table *t, unsigned newasize, unsigned nhsize) { Table newt; /* to keep the new hash part */ unsigned oldasize = t->asize; Value *newarray; if (newasize > MAXASIZE) luaG_runerror(L, "table overflow"); /* create new hash part with appropriate size into 'newt' */ newt.flags = 0; setnodevector(L, &newt, nhsize); if (newasize < oldasize) { /* will array shrink? */ /* re-insert into the new hash the elements from vanishing slice */ exchangehashpart(t, &newt); /* pretend table has new hash */ reinsertOldSlice(t, oldasize, newasize); exchangehashpart(t, &newt); /* restore old hash (in case of errors) */ } /* allocate new array */ newarray = resizearray(L, t, oldasize, newasize); if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ freehash(L, &newt); /* release new hash part */ luaM_error(L); /* raise error (with array unchanged) */ } /* allocation ok; initialize new part of the array */ exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */ t->array = newarray; /* set new array part */ t->asize = newasize; if (newarray != NULL) *lenhint(t) = newasize / 2u; /* set an initial hint */ clearNewSlice(t, oldasize, newasize); /* re-insert elements from old hash part into new parts */ reinserthash(L, &newt, t); /* 'newt' now has the old hash */ freehash(L, &newt); /* free old hash part */ } void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { unsigned nsize = allocsizenode(t); luaH_resize(L, t, nasize, nsize); } /* ** Rehash a table. First, count its keys. If there are array indices ** outside the array part, compute the new best size for that part. ** Then, resize the table. */ static void rehash (lua_State *L, Table *t, const TValue *ek) { unsigned asize; /* optimal size for array part */ Counters ct; unsigned i; unsigned nsize; /* size for the hash part */ /* reset counts */ for (i = 0; i <= MAXABITS; i++) ct.nums[i] = 0; ct.na = 0; ct.deleted = 0; ct.total = 1; /* count extra key */ if (ttisinteger(ek)) countint(ivalue(ek), &ct); /* extra key may go to array */ numusehash(t, &ct); /* count keys in hash part */ if (ct.na == 0) { /* no new keys to enter array part; keep it with the same size */ asize = t->asize; } else { /* compute best size for array part */ numusearray(t, &ct); /* count keys in array part */ asize = computesizes(&ct); /* compute new size for array part */ } /* all keys not in the array part go to the hash part */ nsize = ct.total - ct.na; if (ct.deleted) { /* table has deleted entries? */ /* insertion-deletion-insertion: give hash some extra size to avoid repeated resizings */ nsize += nsize >> 2; } /* resize the table to new computed sizes */ luaH_resize(L, t, asize, nsize); } /* ** }============================================================= */ Table *luaH_new (lua_State *L) { GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table)); Table *t = gco2t(o); t->metatable = NULL; t->flags = maskflags; /* table has no metamethod fields */ t->array = NULL; t->asize = 0; setnodevector(L, t, 0); return t; } lu_mem luaH_size (Table *t) { lu_mem sz = cast(lu_mem, sizeof(Table)) + concretesize(t->asize); if (!isdummy(t)) sz += sizehash(t); return sz; } /* ** Frees a table. */ void luaH_free (lua_State *L, Table *t) { freehash(L, t); resizearray(L, t, t->asize, 0); luaM_free(L, t); } static Node *getfreepos (Table *t) { if (haslastfree(t)) { /* does it have 'lastfree' information? */ /* look for a spot before 'lastfree', updating 'lastfree' */ while (getlastfree(t) > t->node) { Node *free = --getlastfree(t); if (keyisnil(free)) return free; } } else { /* no 'lastfree' information */ unsigned i = sizenode(t); while (i--) { /* do a linear search */ Node *free = gnode(t, i); if (keyisnil(free)) return free; } } return NULL; /* could not find a free place */ } /* ** Inserts a new key into a hash table; first, check whether key's main ** position is free. If not, check whether colliding node is in its main ** position or not: if it is not, move colliding node to an empty place ** and put new key in its main position; otherwise (colliding node is in ** its main position), new key goes to an empty position. Return 0 if ** could not insert key (could not find a free space). */ static int insertkey (Table *t, const TValue *key, TValue *value) { Node *mp = mainpositionTV(t, key); /* table cannot already contain the key */ lua_assert(isabstkey(getgeneric(t, key, 0))); if (!isempty(gval(mp)) || isdummy(t)) { /* main position is taken? */ Node *othern; Node *f = getfreepos(t); /* get a free place */ if (f == NULL) /* cannot find a free place? */ return 0; lua_assert(!isdummy(t)); othern = mainpositionfromnode(t, mp); if (othern != mp) { /* is colliding node out of its main position? */ /* yes; move colliding node into free position */ while (othern + gnext(othern) != mp) /* find previous */ othern += gnext(othern); gnext(othern) = cast_int(f - othern); /* rechain to point to 'f' */ *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */ if (gnext(mp) != 0) { gnext(f) += cast_int(mp - f); /* correct 'next' */ gnext(mp) = 0; /* now 'mp' is free */ } setempty(gval(mp)); } else { /* colliding node is in its own main position */ /* new node will go into free position */ if (gnext(mp) != 0) gnext(f) = cast_int((mp + gnext(mp)) - f); /* chain new position */ else lua_assert(gnext(f) == 0); gnext(mp) = cast_int(f - mp); mp = f; } } setnodekey(mp, key); lua_assert(isempty(gval(mp))); setobj2t(cast(lua_State *, 0), gval(mp), value); return 1; } /* ** Insert a key in a table where there is space for that key, the ** key is valid, and the value is not nil. */ static void newcheckedkey (Table *t, const TValue *key, TValue *value) { unsigned i = keyinarray(t, key); if (i > 0) /* is key in the array part? */ obj2arr(t, i - 1, value); /* set value in the array */ else { int done = insertkey(t, key, value); /* insert key in the hash part */ lua_assert(done); /* it cannot fail */ cast(void, done); /* to avoid warnings */ } } static void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { if (l_unlikely(isshared(t))) luaG_runerror(L, "attempt to change a shared table"); if (!ttisnil(value)) { /* do not insert nil values */ int done = insertkey(t, key, value); if (!done) { /* could not find a free place? */ rehash(L, t, key); /* grow table */ newcheckedkey(t, key, value); /* insert key in grown table */ } luaC_barrierback(L, obj2gco(t), key); /* for debugging only: any new key may force an emergency collection */ condchangemem(L, (void)0, (void)0, 1); } } static const TValue *getintfromhash (Table *t, lua_Integer key) { Node *n = hashint(t, key); lua_assert(!ikeyinarray(t, key)); for (;;) { /* check whether 'key' is somewhere in the chain */ if (keyisinteger(n) && keyival(n) == key) return gval(n); /* that's it */ else { int nx = gnext(n); if (nx == 0) break; n += nx; } } return &absentkey; } static int hashkeyisempty (Table *t, lua_Unsigned key) { const TValue *val = getintfromhash(t, l_castU2S(key)); return isempty(val); } static lu_byte finishnodeget (const TValue *val, TValue *res) { if (!ttisnil(val)) { setobj(((lua_State*)NULL), res, val); } return ttypetag(val); } lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res) { unsigned k = ikeyinarray(t, key); if (k > 0) { lu_byte tag = *getArrTag(t, k - 1); if (!tagisempty(tag)) farr2val(t, k - 1, tag, res); return tag; } else return finishnodeget(getintfromhash(t, key), res); } /* ** search function for short strings */ const TValue *luaH_Hgetshortstr (Table *t, TString *key) { Node *n = hashstr(t, key); lua_assert(strisshr(key)); for (;;) { /* check whether 'key' is somewhere in the chain */ if (keyisshrstr(n) && eqshrstr(keystrval(n), key)) return gval(n); /* that's it */ else { int nx = gnext(n); if (nx == 0) return &absentkey; /* not found */ n += nx; } } } lu_byte luaH_getshortstr (Table *t, TString *key, TValue *res) { return finishnodeget(luaH_Hgetshortstr(t, key), res); } static const TValue *Hgetlongstr (Table *t, TString *key) { TValue ko; lua_assert(!strisshr(key)); setsvalue(cast(lua_State *, NULL), &ko, key); return getgeneric(t, &ko, 0); /* for long strings, use generic case */ } static const TValue *Hgetstr (Table *t, TString *key) { if (strisshr(key)) return luaH_Hgetshortstr(t, key); else return Hgetlongstr(t, key); } lu_byte luaH_getstr (Table *t, TString *key, TValue *res) { return finishnodeget(Hgetstr(t, key), res); } /* ** main search function */ lu_byte luaH_get (Table *t, const TValue *key, TValue *res) { const TValue *slot; switch (ttypetag(key)) { case LUA_VSHRSTR: slot = luaH_Hgetshortstr(t, tsvalue(key)); break; case LUA_VNUMINT: return luaH_getint(t, ivalue(key), res); case LUA_VNIL: slot = &absentkey; break; case LUA_VNUMFLT: { lua_Integer k; if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ return luaH_getint(t, k, res); /* use specialized version */ /* else... */ } /* FALLTHROUGH */ default: slot = getgeneric(t, key, 0); break; } return finishnodeget(slot, res); } /* ** When a 'pset' cannot be completed, this function returns an encoding ** of its result, to be used by 'luaH_finishset'. */ static int retpsetcode (Table *t, const TValue *slot) { if (isabstkey(slot)) return HNOTFOUND; /* no slot with that key */ else /* return node encoded */ return cast_int((cast(Node*, slot) - t->node)) + HFIRSTNODE; } static int finishnodeset (Table *t, const TValue *slot, TValue *val) { if (!ttisnil(slot)) { setobj(((lua_State*)NULL), cast(TValue*, slot), val); return HOK; /* success */ } else return retpsetcode(t, slot); } static int rawfinishnodeset (const TValue *slot, TValue *val) { if (isabstkey(slot)) return 0; /* no slot with that key */ else { setobj(((lua_State*)NULL), cast(TValue*, slot), val); return 1; /* success */ } } int luaH_psetint (Table *t, lua_Integer key, TValue *val) { lua_assert(!ikeyinarray(t, key)); return finishnodeset(t, getintfromhash(t, key), val); } static int psetint (Table *t, lua_Integer key, TValue *val) { int hres; luaH_fastseti(t, key, val, hres); return hres; } /* ** This function could be just this: ** return finishnodeset(t, luaH_Hgetshortstr(t, key), val); ** However, it optimizes the common case created by constructors (e.g., ** {x=1, y=2}), which creates a key in a table that has no metatable, ** it is not old/black, and it already has space for the key. */ int luaH_psetshortstr (Table *t, TString *key, TValue *val) { const TValue *slot = luaH_Hgetshortstr(t, key); if (!ttisnil(slot)) { /* key already has a value? (all too common) */ setobj(((lua_State*)NULL), cast(TValue*, slot), val); /* update it */ return HOK; /* done */ } else if (checknoTM(t->metatable, TM_NEWINDEX)) { /* no metamethod? */ if (ttisnil(val)) /* new value is nil? */ return HOK; /* done (value is already nil/absent) */ if (isabstkey(slot) && /* key is absent? */ !(isblack(t) && iswhite(key))) { /* and don't need barrier? */ TValue tk; /* key as a TValue */ setsvalue(cast(lua_State *, NULL), &tk, key); if (insertkey(t, &tk, val)) { /* insert key, if there is space */ invalidateTMcache(t); return HOK; } } } /* Else, either table has new-index metamethod, or it needs barrier, or it needs to rehash for the new key. In any of these cases, the operation cannot be completed here. Return a code for the caller. */ return retpsetcode(t, slot); } int luaH_psetstr (Table *t, TString *key, TValue *val) { if (strisshr(key)) return luaH_psetshortstr(t, key, val); else return finishnodeset(t, Hgetlongstr(t, key), val); } int luaH_pset (Table *t, const TValue *key, TValue *val) { switch (ttypetag(key)) { case LUA_VSHRSTR: return luaH_psetshortstr(t, tsvalue(key), val); case LUA_VNUMINT: return psetint(t, ivalue(key), val); case LUA_VNIL: return HNOTFOUND; case LUA_VNUMFLT: { lua_Integer k; if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ return psetint(t, k, val); /* use specialized version */ /* else... */ } /* FALLTHROUGH */ default: return finishnodeset(t, getgeneric(t, key, 0), val); } } /* ** Finish a raw "set table" operation, where 'hres' encodes where the ** value should have been (the result of a previous 'pset' operation). ** Beware: when using this function the caller probably need to check a ** GC barrier and invalidate the TM cache. */ void luaH_finishset (lua_State *L, Table *t, const TValue *key, TValue *value, int hres) { lua_assert(hres != HOK); if (l_unlikely(isshared(t))) luaG_runerror(L, "attempt to change a shared table"); if (hres == HNOTFOUND) { TValue aux; if (l_unlikely(ttisnil(key))) luaG_runerror(L, "table index is nil"); else if (ttisfloat(key)) { lua_Number f = fltvalue(key); lua_Integer k; if (luaV_flttointeger(f, &k, F2Ieq)) { setivalue(&aux, k); /* key is equal to an integer */ key = &aux; /* insert it as an integer */ } else if (l_unlikely(luai_numisnan(f))) luaG_runerror(L, "table index is NaN"); } else if (isextstr(key)) { /* external string? */ /* If string is short, must internalize it to be used as table key */ TString *ts = luaS_normstr(L, tsvalue(key)); setsvalue2s(L, L->top.p++, ts); /* anchor 'ts' (EXTRA_STACK) */ luaH_newkey(L, t, s2v(L->top.p - 1), value); L->top.p--; return; } luaH_newkey(L, t, key, value); } else if (hres > 0) { /* regular Node? */ setobj2t(L, gval(gnode(t, hres - HFIRSTNODE)), value); } else { /* array entry */ hres = ~hres; /* real index */ obj2arr(t, cast_uint(hres), value); } } /* ** beware: when using this function you probably need to check a GC ** barrier and invalidate the TM cache. */ void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) { int hres = luaH_pset(t, key, value); if (hres != HOK) luaH_finishset(L, t, key, value, hres); } /* ** Ditto for a GC barrier. (No need to invalidate the TM cache, as ** integers cannot be keys to metamethods.) */ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { unsigned ik = ikeyinarray(t, key); if (l_unlikely(isshared(t))) luaG_runerror(L, "attempt to change a shared table"); if (ik > 0) obj2arr(t, ik - 1, value); else { int ok = rawfinishnodeset(getintfromhash(t, key), value); if (!ok) { TValue k; setivalue(&k, key); luaH_newkey(L, t, &k, value); } } } /* ** Try to find a boundary in the hash part of table 't'. From the ** caller, we know that 'asize + 1' is present. We want to find a larger ** key that is absent from the table, so that we can do a binary search ** between the two keys to find a boundary. We keep doubling 'j' until ** we get an absent index. If the doubling would overflow, we try ** LUA_MAXINTEGER. If it is absent, we are ready for the binary search. ** ('j', being max integer, is larger or equal to 'i', but it cannot be ** equal because it is absent while 'i' is present.) Otherwise, 'j' is a ** boundary. ('j + 1' cannot be a present integer key because it is not ** a valid integer in Lua.) ** About 'rnd': If we used a fixed algorithm, a bad actor could fill ** a table with only the keys that would be probed, in such a way that ** a small table could result in a huge length. To avoid that, we use ** the state's seed as a source of randomness. For the first probe, ** we "randomly double" 'i' by adding to it a random number roughly its ** width. */ static lua_Unsigned hash_search (lua_State *L, Table *t, unsigned asize) { lua_Unsigned i = asize + 1; /* caller ensures t[i] is present */ unsigned rnd = G(L)->seed; int n = (asize > 0) ? luaO_ceillog2(asize) : 0; /* width of 'asize' */ unsigned mask = (1u << n) - 1; /* 11...111 with the width of 'asize' */ unsigned incr = (rnd & mask) + 1; /* first increment (at least 1) */ lua_Unsigned j = (incr <= l_castS2U(LUA_MAXINTEGER) - i) ? i + incr : i + 1; rnd >>= n; /* used 'n' bits from 'rnd' */ while (!hashkeyisempty(t, j)) { /* repeat until an absent t[j] */ i = j; /* 'i' is a present index */ if (j <= l_castS2U(LUA_MAXINTEGER)/2 - 1) { j = j*2 + (rnd & 1); /* try again with 2j or 2j+1 */ rnd >>= 1; } else { j = LUA_MAXINTEGER; if (hashkeyisempty(t, j)) /* t[j] not present? */ break; /* 'j' now is an absent index */ else /* weird case */ return j; /* well, max integer is a boundary... */ } } /* i < j && t[i] present && t[j] absent */ while (j - i > 1u) { /* do a binary search between them */ lua_Unsigned m = (i + j) / 2; if (hashkeyisempty(t, m)) j = m; else i = m; } return i; } static unsigned int binsearch (Table *array, unsigned int i, unsigned int j) { lua_assert(i <= j); while (j - i > 1u) { /* binary search */ unsigned int m = (i + j) / 2; if (arraykeyisempty(array, m)) j = m; else i = m; } return i; } /* return a border, saving it as a hint for next call */ static lua_Unsigned newhint (Table *t, unsigned hint) { lua_assert(hint <= t->asize); *lenhint(t) = hint; return hint; } /* ** Try to find a border in table 't'. (A 'border' is an integer index ** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent, ** or 'maxinteger' if t[maxinteger] is present.) ** If there is an array part, try to find a border there. First try ** to find it in the vicinity of the previous result (hint), to handle ** cases like 't[#t + 1] = val' or 't[#t] = nil', that move the border ** by one entry. Otherwise, do a binary search to find the border. ** If there is no array part, or its last element is non empty, the ** border may be in the hash part. */ lua_Unsigned luaH_getn (lua_State *L, Table *t) { unsigned asize = t->asize; if (asize > 0) { /* is there an array part? */ const unsigned maxvicinity = 4; unsigned limit = *lenhint(t); /* start with the hint */ if (limit == 0) limit = 1; /* make limit a valid index in the array */ if (arraykeyisempty(t, limit)) { /* t[limit] empty? */ /* there must be a border before 'limit' */ unsigned i; /* look for a border in the vicinity of the hint */ for (i = 0; i < maxvicinity && limit > 1; i++) { limit--; if (!arraykeyisempty(t, limit)) return newhint(t, limit); /* 'limit' is a border */ } /* t[limit] still empty; search for a border in [0, limit) */ return newhint(t, binsearch(t, 0, limit)); } else { /* 'limit' is present in table; look for a border after it */ unsigned i; /* look for a border in the vicinity of the hint */ for (i = 0; i < maxvicinity && limit < asize; i++) { limit++; if (arraykeyisempty(t, limit)) return newhint(t, limit - 1); /* 'limit - 1' is a border */ } if (arraykeyisempty(t, asize)) { /* last element empty? */ /* t[limit] not empty; search for a border in [limit, asize) */ return newhint(t, binsearch(t, limit, asize)); } } /* last element non empty; set a hint to speed up finding that again */ /* (keys in the hash part cannot be hints) */ *lenhint(t) = asize; } /* no array part or t[asize] is not empty; check the hash part */ lua_assert(asize == 0 || !arraykeyisempty(t, asize)); if (isdummy(t) || hashkeyisempty(t, asize + 1)) return asize; /* 'asize + 1' is empty */ else /* 'asize + 1' is also non empty */ return hash_search(L, t, asize); } #if defined(LUA_DEBUG) /* export this function for the test library */ Node *luaH_mainposition (const Table *t, const TValue *key) { return mainpositionTV(t, key); } #endif ================================================ FILE: 3rd/lua/ltable.h ================================================ /* ** $Id: ltable.h $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ #ifndef ltable_h #define ltable_h #include "lobject.h" #define gnode(t,i) (&(t)->node[i]) #define gval(n) (&(n)->i_val) #define gnext(n) ((n)->u.next) /* ** Clear all bits of fast-access metamethods, which means that the table ** may have any of these metamethods. (First access that fails after the ** clearing will set the bit again.) */ #define invalidateTMcache(t) ((t)->flags &= cast_byte(~maskflags)) /* ** Bit BITDUMMY set in 'flags' means the table is using the dummy node ** for its hash part. */ #define BITDUMMY (1 << 6) #define NOTBITDUMMY cast_byte(~BITDUMMY) #define isdummy(t) ((t)->flags & BITDUMMY) #define setnodummy(t) ((t)->flags &= NOTBITDUMMY) #define setdummy(t) ((t)->flags |= BITDUMMY) /* allocated size for hash nodes */ #define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t)) /* returns the Node, given the value of a table entry */ #define nodefromval(v) cast(Node *, (v)) #define luaH_fastgeti(t,k,res,tag) \ { Table *h = t; lua_Unsigned u = l_castS2U(k) - 1u; \ if ((u < h->asize)) { \ tag = *getArrTag(h, u); \ if (!tagisempty(tag)) { farr2val(h, u, tag, res); }} \ else { tag = luaH_getint(h, (k), res); }} #define luaH_fastseti(t,k,val,hres) \ { Table *h = t; lua_Unsigned u = l_castS2U(k) - 1u; \ if ((u < h->asize)) { \ lu_byte *tag = getArrTag(h, u); \ if (checknoTM(h->metatable, TM_NEWINDEX) || !tagisempty(*tag)) \ { fval2arr(h, u, tag, val); hres = HOK; } \ else hres = ~cast_int(u); } \ else { hres = luaH_psetint(h, k, val); }} /* results from pset */ #define HOK 0 #define HNOTFOUND 1 #define HNOTATABLE 2 #define HFIRSTNODE 3 /* ** 'luaH_get*' operations set 'res', unless the value is absent, and ** return the tag of the result. ** The 'luaH_pset*' (pre-set) operations set the given value and return ** HOK, unless the original value is absent. In that case, if the key ** is really absent, they return HNOTFOUND. Otherwise, if there is a ** slot with that key but with no value, 'luaH_pset*' return an encoding ** of where the key is (usually called 'hres'). (pset cannot set that ** value because there might be a metamethod.) If the slot is in the ** hash part, the encoding is (HFIRSTNODE + hash index); if the slot is ** in the array part, the encoding is (~array index), a negative value. ** The value HNOTATABLE is used by the fast macros to signal that the ** value being indexed is not a table. ** (The size for the array part is limited by the maximum power of two ** that fits in an unsigned integer; that is INT_MAX+1. So, the C-index ** ranges from 0, which encodes to -1, to INT_MAX, which encodes to ** INT_MIN. The size of the hash part is limited by the maximum power of ** two that fits in a signed integer; that is (INT_MAX+1)/2. So, it is ** safe to add HFIRSTNODE to any index there.) */ /* ** The array part of a table is represented by an inverted array of ** values followed by an array of tags, to avoid wasting space with ** padding. In between them there is an unsigned int, explained later. ** The 'array' pointer points between the two arrays, so that values are ** indexed with negative indices and tags with non-negative indices. Values Tags -------------------------------------------------------- ... | Value 1 | Value 0 |unsigned|0|1|... -------------------------------------------------------- ^ t->array ** All accesses to 't->array' should be through the macros 'getArrTag' ** and 'getArrVal'. */ /* Computes the address of the tag for the abstract C-index 'k' */ #define getArrTag(t,k) (cast(lu_byte*, (t)->array) + sizeof(unsigned) + (k)) /* Computes the address of the value for the abstract C-index 'k' */ #define getArrVal(t,k) ((t)->array - 1 - (k)) /* ** The unsigned between the two arrays is used as a hint for #t; ** see luaH_getn. It is stored there to avoid wasting space in ** the structure Table for tables with no array part. */ #define lenhint(t) cast(unsigned*, (t)->array) /* ** Move TValues to/from arrays, using C indices */ #define arr2obj(h,k,val) \ ((val)->tt_ = *getArrTag(h,(k)), (val)->value_ = *getArrVal(h,(k))) #define obj2arr(h,k,val) \ (*getArrTag(h,(k)) = (val)->tt_, *getArrVal(h,(k)) = (val)->value_) /* ** Often, we need to check the tag of a value before moving it. The ** following macros also move TValues to/from arrays, but receive the ** precomputed tag value or address as an extra argument. */ #define farr2val(h,k,tag,res) \ ((res)->tt_ = tag, (res)->value_ = *getArrVal(h,(k))) #define fval2arr(h,k,tag,val) \ (*tag = (val)->tt_, *getArrVal(h,(k)) = (val)->value_) LUAI_FUNC lu_byte luaH_get (Table *t, const TValue *key, TValue *res); LUAI_FUNC lu_byte luaH_getshortstr (Table *t, TString *key, TValue *res); LUAI_FUNC lu_byte luaH_getstr (Table *t, TString *key, TValue *res); LUAI_FUNC lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res); /* Special get for metamethods */ LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key); LUAI_FUNC int luaH_psetint (Table *t, lua_Integer key, TValue *val); LUAI_FUNC int luaH_psetshortstr (Table *t, TString *key, TValue *val); LUAI_FUNC int luaH_psetstr (Table *t, TString *key, TValue *val); LUAI_FUNC int luaH_pset (Table *t, const TValue *key, TValue *val); LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value); LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value); LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key, TValue *value, int hres); LUAI_FUNC Table *luaH_new (lua_State *L); LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned nasize, unsigned nhsize); LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned nasize); LUAI_FUNC lu_mem luaH_size (Table *t); LUAI_FUNC void luaH_free (lua_State *L, Table *t); LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); LUAI_FUNC lua_Unsigned luaH_getn (lua_State *L, Table *t); #if defined(LUA_DEBUG) LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); #endif #endif ================================================ FILE: 3rd/lua/ltablib.c ================================================ /* ** $Id: ltablib.c $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ #define ltablib_c #define LUA_LIB #include "lprefix.h" #include #include #include #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "llimits.h" /* ** Operations that an object must define to mimic a table ** (some functions only need some of them) */ #define TAB_R 1 /* read */ #define TAB_W 2 /* write */ #define TAB_L 4 /* length */ #define TAB_RW (TAB_R | TAB_W) /* read/write */ #define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n)) static int checkfield (lua_State *L, const char *key, int n) { lua_pushstring(L, key); return (lua_rawget(L, -n) != LUA_TNIL); } /* ** Check that 'arg' either is a table or can behave like one (that is, ** has a metatable with the required metamethods) */ static void checktab (lua_State *L, int arg, int what) { if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */ int n = 1; /* number of elements to pop */ if (lua_getmetatable(L, arg) && /* must have metatable */ (!(what & TAB_R) || checkfield(L, "__index", ++n)) && (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) && (!(what & TAB_L) || checkfield(L, "__len", ++n))) { lua_pop(L, n); /* pop metatable and tested metamethods */ } else luaL_checktype(L, arg, LUA_TTABLE); /* force an error */ } } static int tcreate (lua_State *L) { lua_Unsigned sizeseq = (lua_Unsigned)luaL_checkinteger(L, 1); lua_Unsigned sizerest = (lua_Unsigned)luaL_optinteger(L, 2, 0); luaL_argcheck(L, sizeseq <= cast_uint(INT_MAX), 1, "out of range"); luaL_argcheck(L, sizerest <= cast_uint(INT_MAX), 2, "out of range"); lua_createtable(L, cast_int(sizeseq), cast_int(sizerest)); return 1; } static int tinsert (lua_State *L) { lua_Integer pos; /* where to insert new element */ lua_Integer e = aux_getn(L, 1, TAB_RW); e = luaL_intop(+, e, 1); /* first empty element */ switch (lua_gettop(L)) { case 2: { /* called with only 2 arguments */ pos = e; /* insert new element at the end */ break; } case 3: { lua_Integer i; pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ /* check whether 'pos' is in [1, e] */ luaL_argcheck(L, (lua_Unsigned)pos - 1u < (lua_Unsigned)e, 2, "position out of bounds"); for (i = e; i > pos; i--) { /* move up elements */ lua_geti(L, 1, i - 1); lua_seti(L, 1, i); /* t[i] = t[i - 1] */ } break; } default: { return luaL_error(L, "wrong number of arguments to 'insert'"); } } lua_seti(L, 1, pos); /* t[pos] = v */ return 0; } static int tremove (lua_State *L) { lua_Integer size = aux_getn(L, 1, TAB_RW); lua_Integer pos = luaL_optinteger(L, 2, size); if (pos != size) /* validate 'pos' if given */ /* check whether 'pos' is in [1, size + 1] */ luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 2, "position out of bounds"); lua_geti(L, 1, pos); /* result = t[pos] */ for ( ; pos < size; pos++) { lua_geti(L, 1, pos + 1); lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ } lua_pushnil(L); lua_seti(L, 1, pos); /* remove entry t[pos] */ return 1; } /* ** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever ** possible, copy in increasing order, which is better for rehashing. ** "possible" means destination after original range, or smaller ** than origin, or copying to another table. */ static int tmove (lua_State *L) { lua_Integer f = luaL_checkinteger(L, 2); lua_Integer e = luaL_checkinteger(L, 3); lua_Integer t = luaL_checkinteger(L, 4); int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ checktab(L, 1, TAB_R); checktab(L, tt, TAB_W); if (e >= f) { /* otherwise, nothing to move */ lua_Integer n, i; luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3, "too many elements to move"); n = e - f + 1; /* number of elements to move */ luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, "destination wrap around"); if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) { for (i = 0; i < n; i++) { lua_geti(L, 1, f + i); lua_seti(L, tt, t + i); } } else { for (i = n - 1; i >= 0; i--) { lua_geti(L, 1, f + i); lua_seti(L, tt, t + i); } } } lua_pushvalue(L, tt); /* return destination table */ return 1; } static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { lua_geti(L, 1, i); if (l_unlikely(!lua_isstring(L, -1))) luaL_error(L, "invalid value (%s) at index %I in table for 'concat'", luaL_typename(L, -1), (LUAI_UACINT)i); luaL_addvalue(b); } static int tconcat (lua_State *L) { luaL_Buffer b; lua_Integer last = aux_getn(L, 1, TAB_R); size_t lsep; const char *sep = luaL_optlstring(L, 2, "", &lsep); lua_Integer i = luaL_optinteger(L, 3, 1); last = luaL_optinteger(L, 4, last); luaL_buffinit(L, &b); for (; i < last; i++) { addfield(L, &b, i); luaL_addlstring(&b, sep, lsep); } if (i == last) /* add last value (if interval was not empty) */ addfield(L, &b, i); luaL_pushresult(&b); return 1; } /* ** {====================================================== ** Pack/unpack ** ======================================================= */ static int tpack (lua_State *L) { int i; int n = lua_gettop(L); /* number of elements to pack */ lua_createtable(L, n, 1); /* create result table */ lua_insert(L, 1); /* put it at index 1 */ for (i = n; i >= 1; i--) /* assign elements */ lua_seti(L, 1, i); lua_pushinteger(L, n); lua_setfield(L, 1, "n"); /* t.n = number of elements */ return 1; /* return table */ } static int tunpack (lua_State *L) { lua_Unsigned n; lua_Integer i = luaL_optinteger(L, 2, 1); lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); if (i > e) return 0; /* empty range */ n = l_castS2U(e) - l_castS2U(i); /* number of elements minus 1 */ if (l_unlikely(n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n)))) return luaL_error(L, "too many results to unpack"); for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */ lua_geti(L, 1, i); } lua_geti(L, 1, e); /* push last element */ return (int)n; } /* }====================================================== */ /* ** {====================================================== ** Quicksort ** (based on 'Algorithms in MODULA-3', Robert Sedgewick; ** Addison-Wesley, 1993.) ** ======================================================= */ /* ** Type for array indices. These indices are always limited by INT_MAX, ** so it is safe to cast them to lua_Integer even for Lua 32 bits. */ typedef unsigned int IdxT; /* Versions of lua_seti/lua_geti specialized for IdxT */ #define geti(L,idt,idx) lua_geti(L, idt, l_castU2S(idx)) #define seti(L,idt,idx) lua_seti(L, idt, l_castU2S(idx)) /* ** Produce a "random" 'unsigned int' to randomize pivot choice. This ** macro is used only when 'sort' detects a big imbalance in the result ** of a partition. (If you don't want/need this "randomness", ~0 is a ** good choice.) */ #if !defined(l_randomizePivot) #define l_randomizePivot(L) luaL_makeseed(L) #endif /* } */ /* arrays larger than 'RANLIMIT' may use randomized pivots */ #define RANLIMIT 100u static void set2 (lua_State *L, IdxT i, IdxT j) { seti(L, 1, i); seti(L, 1, j); } /* ** Return true iff value at stack index 'a' is less than the value at ** index 'b' (according to the order of the sort). */ static int sort_comp (lua_State *L, int a, int b) { if (lua_isnil(L, 2)) /* no function? */ return lua_compare(L, a, b, LUA_OPLT); /* a < b */ else { /* function */ int res; lua_pushvalue(L, 2); /* push function */ lua_pushvalue(L, a-1); /* -1 to compensate function */ lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */ lua_call(L, 2, 1); /* call function */ res = lua_toboolean(L, -1); /* get result */ lua_pop(L, 1); /* pop result */ return res; } } /* ** Does the partition: Pivot P is at the top of the stack. ** precondition: a[lo] <= P == a[up-1] <= a[up], ** so it only needs to do the partition from lo + 1 to up - 2. ** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up] ** returns 'i'. */ static IdxT partition (lua_State *L, IdxT lo, IdxT up) { IdxT i = lo; /* will be incremented before first use */ IdxT j = up - 1; /* will be decremented before first use */ /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ for (;;) { /* next loop: repeat ++i while a[i] < P */ while ((void)geti(L, 1, ++i), sort_comp(L, -1, -2)) { if (l_unlikely(i == up - 1)) /* a[up - 1] < P == a[up - 1] */ luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[i] */ } /* after the loop, a[i] >= P and a[lo .. i - 1] < P (a) */ /* next loop: repeat --j while P < a[j] */ while ((void)geti(L, 1, --j), sort_comp(L, -3, -1)) { if (l_unlikely(j < i)) /* j <= i - 1 and a[j] > P, contradicts (a) */ luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[j] */ } /* after the loop, a[j] <= P and a[j + 1 .. up] >= P */ if (j < i) { /* no elements out of place? */ /* a[lo .. i - 1] <= P <= a[j + 1 .. i .. up] */ lua_pop(L, 1); /* pop a[j] */ /* swap pivot (a[up - 1]) with a[i] to satisfy pos-condition */ set2(L, up - 1, i); return i; } /* otherwise, swap a[i] - a[j] to restore invariant and repeat */ set2(L, i, j); } } /* ** Choose an element in the middle (2nd-3th quarters) of [lo,up] ** "randomized" by 'rnd' */ static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) { IdxT r4 = (up - lo) / 4; /* range/4 */ IdxT p = (rnd ^ lo ^ up) % (r4 * 2) + (lo + r4); lua_assert(lo + r4 <= p && p <= up - r4); return p; } /* ** Quicksort algorithm (recursive function) */ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) { while (lo < up) { /* loop for tail recursion */ IdxT p; /* Pivot index */ IdxT n; /* to be used later */ /* sort elements 'lo', 'p', and 'up' */ geti(L, 1, lo); geti(L, 1, up); if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */ set2(L, lo, up); /* swap a[lo] - a[up] */ else lua_pop(L, 2); /* remove both values */ if (up - lo == 1) /* only 2 elements? */ return; /* already sorted */ if (up - lo < RANLIMIT || rnd == 0) /* small interval or no randomize? */ p = (lo + up)/2; /* middle element is a good pivot */ else /* for larger intervals, it is worth a random pivot */ p = choosePivot(lo, up, rnd); geti(L, 1, p); geti(L, 1, lo); if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */ set2(L, p, lo); /* swap a[p] - a[lo] */ else { lua_pop(L, 1); /* remove a[lo] */ geti(L, 1, up); if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */ set2(L, p, up); /* swap a[up] - a[p] */ else lua_pop(L, 2); } if (up - lo == 2) /* only 3 elements? */ return; /* already sorted */ geti(L, 1, p); /* get middle element (Pivot) */ lua_pushvalue(L, -1); /* push Pivot */ geti(L, 1, up - 1); /* push a[up - 1] */ set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */ p = partition(L, lo, up); /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */ if (p - lo < up - p) { /* lower interval is smaller? */ auxsort(L, lo, p - 1, rnd); /* call recursively for lower interval */ n = p - lo; /* size of smaller interval */ lo = p + 1; /* tail call for [p + 1 .. up] (upper interval) */ } else { auxsort(L, p + 1, up, rnd); /* call recursively for upper interval */ n = up - p; /* size of smaller interval */ up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */ } if ((up - lo) / 128 > n) /* partition too imbalanced? */ rnd = l_randomizePivot(L); /* try a new randomization */ } /* tail call auxsort(L, lo, up, rnd) */ } static int sort (lua_State *L) { lua_Integer n = aux_getn(L, 1, TAB_RW); if (n > 1) { /* non-trivial interval? */ luaL_argcheck(L, n < INT_MAX, 1, "array too big"); if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */ lua_settop(L, 2); /* make sure there are two arguments */ auxsort(L, 1, (IdxT)n, 0); } return 0; } /* }====================================================== */ static const luaL_Reg tab_funcs[] = { {"concat", tconcat}, {"create", tcreate}, {"insert", tinsert}, {"pack", tpack}, {"unpack", tunpack}, {"remove", tremove}, {"move", tmove}, {"sort", sort}, {NULL, NULL} }; LUAMOD_API int luaopen_table (lua_State *L) { luaL_newlib(L, tab_funcs); return 1; } ================================================ FILE: 3rd/lua/ltests.c ================================================ /* ** $Id: ltests.c $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ #define ltests_c #define LUA_CORE #include "lprefix.h" #include #include #include #include #include #include "lua.h" #include "lapi.h" #include "lauxlib.h" #include "lcode.h" #include "lctype.h" #include "ldebug.h" #include "ldo.h" #include "lfunc.h" #include "lmem.h" #include "lopcodes.h" #include "lopnames.h" #include "lstate.h" #include "lstring.h" #include "ltable.h" #include "lualib.h" /* ** The whole module only makes sense with LUA_DEBUG on */ #if defined(LUA_DEBUG) void *l_Trick = 0; #define obj_at(L,k) s2v(L->ci->func.p + (k)) static int runC (lua_State *L, lua_State *L1, const char *pc); static void setnameval (lua_State *L, const char *name, int val) { lua_pushinteger(L, val); lua_setfield(L, -2, name); } static void pushobject (lua_State *L, const TValue *o) { setobj2s(L, L->top.p, o); api_incr_top(L); } static void badexit (const char *fmt, const char *s1, const char *s2) { fprintf(stderr, fmt, s1); if (s2) fprintf(stderr, "extra info: %s\n", s2); /* avoid assertion failures when exiting */ l_memcontrol.numblocks = l_memcontrol.total = 0; exit(EXIT_FAILURE); } static int tpanic (lua_State *L) { const char *msg = (lua_type(L, -1) == LUA_TSTRING) ? lua_tostring(L, -1) : "error object is not a string"; return (badexit("PANIC: unprotected error in call to Lua API (%s)\n", msg, NULL), 0); /* do not return to Lua */ } /* ** Warning function for tests. First, it concatenates all parts of ** a warning in buffer 'buff'. Then, it has three modes: ** - 0.normal: messages starting with '#' are shown on standard output; ** - other messages abort the tests (they represent real warning ** conditions; the standard tests should not generate these conditions ** unexpectedly); ** - 1.allow: all messages are shown; ** - 2.store: all warnings go to the global '_WARN'; */ static void warnf (void *ud, const char *msg, int tocont) { lua_State *L = cast(lua_State *, ud); static char buff[200] = ""; /* should be enough for tests... */ static int onoff = 0; static int mode = 0; /* start in normal mode */ static int lasttocont = 0; if (!lasttocont && !tocont && *msg == '@') { /* control message? */ if (buff[0] != '\0') badexit("Control warning during warning: %s\naborting...\n", msg, buff); if (strcmp(msg, "@off") == 0) onoff = 0; else if (strcmp(msg, "@on") == 0) onoff = 1; else if (strcmp(msg, "@normal") == 0) mode = 0; else if (strcmp(msg, "@allow") == 0) mode = 1; else if (strcmp(msg, "@store") == 0) mode = 2; else badexit("Invalid control warning in test mode: %s\naborting...\n", msg, NULL); return; } lasttocont = tocont; if (strlen(msg) >= sizeof(buff) - strlen(buff)) badexit("warnf-buffer overflow (%s)\n", msg, buff); strcat(buff, msg); /* add new message to current warning */ if (!tocont) { /* message finished? */ lua_unlock(L); luaL_checkstack(L, 1, "warn stack space"); lua_getglobal(L, "_WARN"); if (!lua_toboolean(L, -1)) lua_pop(L, 1); /* ok, no previous unexpected warning */ else { badexit("Unhandled warning in store mode: %s\naborting...\n", lua_tostring(L, -1), buff); } lua_lock(L); switch (mode) { case 0: { /* normal */ if (buff[0] != '#' && onoff) /* unexpected warning? */ badexit("Unexpected warning in test mode: %s\naborting...\n", buff, NULL); } /* FALLTHROUGH */ case 1: { /* allow */ if (onoff) fprintf(stderr, "Lua warning: %s\n", buff); /* print warning */ break; } case 2: { /* store */ lua_unlock(L); luaL_checkstack(L, 1, "warn stack space"); lua_pushstring(L, buff); lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */ lua_lock(L); break; } } buff[0] = '\0'; /* prepare buffer for next warning */ } } /* ** {====================================================================== ** Controlled version for realloc. ** ======================================================================= */ #define MARK 0x55 /* 01010101 (a nice pattern) */ typedef union memHeader { LUAI_MAXALIGN; struct { size_t size; int type; } d; } memHeader; #if !defined(EXTERNMEMCHECK) /* full memory check */ #define MARKSIZE 16 /* size of marks after each block */ #define fillmem(mem,size) memset(mem, -MARK, size) #else /* external memory check: don't do it twice */ #define MARKSIZE 0 #define fillmem(mem,size) /* empty */ #endif Memcontrol l_memcontrol = {0, 0UL, 0UL, 0UL, 0UL, (~0UL), {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}}; static void freeblock (Memcontrol *mc, memHeader *block) { if (block) { size_t size = block->d.size; int i; for (i = 0; i < MARKSIZE; i++) /* check marks after block */ lua_assert(*(cast_charp(block + 1) + size + i) == MARK); mc->objcount[block->d.type]--; fillmem(block, sizeof(memHeader) + size + MARKSIZE); /* erase block */ free(block); /* actually free block */ mc->numblocks--; /* update counts */ mc->total -= size; } } void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { Memcontrol *mc = cast(Memcontrol *, ud); memHeader *block = cast(memHeader *, b); int type; if (mc->memlimit == 0) { /* first time? */ char *limit = getenv("MEMLIMIT"); /* initialize memory limit */ mc->memlimit = limit ? strtoul(limit, NULL, 10) : ULONG_MAX; } if (block == NULL) { type = (oldsize < LUA_NUMTYPES) ? cast_int(oldsize) : 0; oldsize = 0; } else { block--; /* go to real header */ type = block->d.type; lua_assert(oldsize == block->d.size); } if (size == 0) { freeblock(mc, block); return NULL; } if (mc->failnext) { mc->failnext = 0; return NULL; /* fake a single memory allocation error */ } if (mc->countlimit != ~0UL && size != oldsize) { /* count limit in use? */ if (mc->countlimit == 0) return NULL; /* fake a memory allocation error */ mc->countlimit--; } if (size > oldsize && mc->total+size-oldsize > mc->memlimit) return NULL; /* fake a memory allocation error */ else { memHeader *newblock; int i; size_t commonsize = (oldsize < size) ? oldsize : size; size_t realsize = sizeof(memHeader) + size + MARKSIZE; if (realsize < size) return NULL; /* arithmetic overflow! */ newblock = cast(memHeader *, malloc(realsize)); /* alloc a new block */ if (newblock == NULL) return NULL; /* really out of memory? */ if (block) { memcpy(newblock + 1, block + 1, commonsize); /* copy old contents */ freeblock(mc, block); /* erase (and check) old copy */ } /* initialize new part of the block with something weird */ fillmem(cast_charp(newblock + 1) + commonsize, size - commonsize); /* initialize marks after block */ for (i = 0; i < MARKSIZE; i++) *(cast_charp(newblock + 1) + size + i) = MARK; newblock->d.size = size; newblock->d.type = type; mc->total += size; if (mc->total > mc->maxmem) mc->maxmem = mc->total; mc->numblocks++; mc->objcount[type]++; return newblock + 1; } } /* }====================================================================== */ /* ** {===================================================================== ** Functions to check memory consistency. ** Most of these checks are done through asserts, so this code does ** not make sense with asserts off. For this reason, it uses 'assert' ** directly, instead of 'lua_assert'. ** ====================================================================== */ #include /* ** Check GC invariants. For incremental mode, a black object cannot ** point to a white one. For generational mode, really old objects ** cannot point to young objects. Both old1 and touched2 objects ** cannot point to new objects (but can point to survivals). ** (Threads and open upvalues, despite being marked "really old", ** continue to be visited in all collections, and therefore can point to ** new objects. They, and only they, are old but gray.) */ static int testobjref1 (global_State *g, GCObject *f, GCObject *t) { if (isdead(g,t)) return 0; if (issweepphase(g)) return 1; /* no invariants */ else if (g->gckind != KGC_GENMINOR) return !(isblack(f) && iswhite(t)); /* basic incremental invariant */ else { /* generational mode */ if ((getage(f) == G_OLD && isblack(f)) && !isold(t)) return 0; if ((getage(f) == G_OLD1 || getage(f) == G_TOUCHED2) && getage(t) == G_NEW) return 0; return 1; } } static void printobj (global_State *g, GCObject *o) { printf("||%s(%p)-%c%c(%02X)||", ttypename(novariant(o->tt)), (void *)o, isdead(g,o) ? 'd' : isblack(o) ? 'b' : iswhite(o) ? 'w' : 'g', "ns01oTt"[getage(o)], o->marked); if (o->tt == LUA_VSHRSTR || o->tt == LUA_VLNGSTR) printf(" '%s'", getstr(gco2ts(o))); } void lua_printobj (lua_State *L, struct GCObject *o) { printobj(G(L), o); } void lua_printvalue (TValue *v) { switch (ttypetag(v)) { case LUA_VNUMINT: case LUA_VNUMFLT: { char buff[LUA_N2SBUFFSZ]; unsigned len = luaO_tostringbuff(v, buff); buff[len] = '\0'; printf("%s", buff); break; } case LUA_VSHRSTR: printf("'%s'", getstr(tsvalue(v))); break; case LUA_VLNGSTR: printf("'%.30s...'", getstr(tsvalue(v))); break; case LUA_VFALSE: printf("%s", "false"); break; case LUA_VTRUE: printf("%s", "true"); break; case LUA_VLIGHTUSERDATA: printf("light udata: %p", pvalue(v)); break; case LUA_VUSERDATA: printf("full udata: %p", uvalue(v)); break; case LUA_VNIL: printf("nil"); break; case LUA_VLCF: printf("light C function: %p", fvalue(v)); break; case LUA_VCCL: printf("C closure: %p", clCvalue(v)); break; case LUA_VLCL: printf("Lua function: %p", clLvalue(v)); break; case LUA_VTHREAD: printf("thread: %p", thvalue(v)); break; case LUA_VTABLE: printf("table: %p", hvalue(v)); break; default: lua_assert(0); } } static int testobjref (global_State *g, GCObject *f, GCObject *t) { int r1 = testobjref1(g, f, t); if (!r1) { printf("%d(%02X) - ", g->gcstate, g->currentwhite); printobj(g, f); printf(" -> "); printobj(g, t); printf("\n"); } return r1; } static void checkobjref (global_State *g, GCObject *f, GCObject *t) { assert(testobjref(g, f, t)); } /* ** Version where 't' can be NULL. In that case, it should not apply the ** macro 'obj2gco' over the object. ('t' may have several types, so this ** definition must be a macro.) Most checks need this version, because ** the check may run while an object is still being created. */ #define checkobjrefN(g,f,t) { if (t) checkobjref(g,f,obj2gco(t)); } static void checkvalref (global_State *g, GCObject *f, const TValue *t) { assert(!iscollectable(t) || (righttt(t) && testobjref(g, f, gcvalue(t)))); } static void checktable (global_State *g, Table *h) { unsigned int i; unsigned int asize = h->asize; Node *n, *limit = gnode(h, sizenode(h)); GCObject *hgc = obj2gco(h); checkobjrefN(g, hgc, h->metatable); for (i = 0; i < asize; i++) { TValue aux; arr2obj(h, i, &aux); checkvalref(g, hgc, &aux); } for (n = gnode(h, 0); n < limit; n++) { if (!isempty(gval(n))) { TValue k; getnodekey(mainthread(g), &k, n); assert(!keyisnil(n)); checkvalref(g, hgc, &k); checkvalref(g, hgc, gval(n)); } } } static void checkudata (global_State *g, Udata *u) { int i; GCObject *hgc = obj2gco(u); checkobjrefN(g, hgc, u->metatable); for (i = 0; i < u->nuvalue; i++) checkvalref(g, hgc, &u->uv[i].uv); } static void checkproto (global_State *g, Proto *f) { int i; GCObject *fgc = obj2gco(f); checkobjrefN(g, fgc, f->source); for (i=0; isizek; i++) { if (iscollectable(f->k + i)) checkobjref(g, fgc, gcvalue(f->k + i)); } for (i=0; isizeupvalues; i++) checkobjrefN(g, fgc, f->upvalues[i].name); for (i=0; isizep; i++) checkobjrefN(g, fgc, f->p[i]); for (i=0; isizelocvars; i++) checkobjrefN(g, fgc, f->locvars[i].varname); } static void checkCclosure (global_State *g, CClosure *cl) { GCObject *clgc = obj2gco(cl); int i; for (i = 0; i < cl->nupvalues; i++) checkvalref(g, clgc, &cl->upvalue[i]); } static void checkLclosure (global_State *g, LClosure *cl) { GCObject *clgc = obj2gco(cl); int i; checkobjrefN(g, clgc, cl->p); for (i=0; inupvalues; i++) { UpVal *uv = cl->upvals[i]; if (uv) { checkobjrefN(g, clgc, uv); if (!upisopen(uv)) checkvalref(g, obj2gco(uv), uv->v.p); } } } static int lua_checkpc (CallInfo *ci) { if (!isLua(ci)) return 1; else { StkId f = ci->func.p; Proto *p = clLvalue(s2v(f))->p; return p->code <= ci->u.l.savedpc && ci->u.l.savedpc <= p->code + p->sizecode; } } static void check_stack (global_State *g, lua_State *L1) { StkId o; CallInfo *ci; UpVal *uv; assert(!isdead(g, L1)); if (L1->stack.p == NULL) { /* incomplete thread? */ assert(L1->openupval == NULL && L1->ci == NULL); return; } for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next) assert(upisopen(uv)); /* must be open */ assert(L1->top.p <= L1->stack_last.p); assert(L1->tbclist.p <= L1->top.p); for (ci = L1->ci; ci != NULL; ci = ci->previous) { assert(ci->top.p <= L1->stack_last.p); assert(lua_checkpc(ci)); } for (o = L1->stack.p; o < L1->stack_last.p; o++) checkliveness(L1, s2v(o)); /* entire stack must have valid values */ } static void checkrefs (global_State *g, GCObject *o) { switch (o->tt) { case LUA_VUSERDATA: { checkudata(g, gco2u(o)); break; } case LUA_VUPVAL: { checkvalref(g, o, gco2upv(o)->v.p); break; } case LUA_VTABLE: { checktable(g, gco2t(o)); break; } case LUA_VTHREAD: { check_stack(g, gco2th(o)); break; } case LUA_VLCL: { checkLclosure(g, gco2lcl(o)); break; } case LUA_VCCL: { checkCclosure(g, gco2ccl(o)); break; } case LUA_VPROTO: { checkproto(g, gco2p(o)); break; } case LUA_VSHRSTR: case LUA_VLNGSTR: { assert(!isgray(o)); /* strings are never gray */ break; } default: assert(0); } } /* ** Check consistency of an object: ** - Dead objects can only happen in the 'allgc' list during a sweep ** phase (controlled by the caller through 'maybedead'). ** - During pause, all objects must be white. ** - In generational mode: ** * objects must be old enough for their lists ('listage'). ** * old objects cannot be white. ** * old objects must be black, except for 'touched1', 'old0', ** threads, and open upvalues. ** * 'touched1' objects must be gray. */ static void checkobject (global_State *g, GCObject *o, int maybedead, int listage) { if (isdead(g, o)) assert(maybedead); else { assert(g->gcstate != GCSpause || iswhite(o)); if (g->gckind == KGC_GENMINOR) { /* generational mode? */ assert(getage(o) >= listage); if (isold(o)) { assert(!iswhite(o)); assert(isblack(o) || getage(o) == G_TOUCHED1 || getage(o) == G_OLD0 || o->tt == LUA_VTHREAD || (o->tt == LUA_VUPVAL && upisopen(gco2upv(o)))); } assert(getage(o) != G_TOUCHED1 || isgray(o)); } checkrefs(g, o); } } static l_mem checkgraylist (global_State *g, GCObject *o) { int total = 0; /* count number of elements in the list */ cast_void(g); /* better to keep it if we need to print an object */ while (o) { assert(!!isgray(o) ^ (getage(o) == G_TOUCHED2)); assert(!testbit(o->marked, TESTBIT)); if (keepinvariant(g)) l_setbit(o->marked, TESTBIT); /* mark that object is in a gray list */ total++; switch (o->tt) { case LUA_VTABLE: o = gco2t(o)->gclist; break; case LUA_VLCL: o = gco2lcl(o)->gclist; break; case LUA_VCCL: o = gco2ccl(o)->gclist; break; case LUA_VTHREAD: o = gco2th(o)->gclist; break; case LUA_VPROTO: o = gco2p(o)->gclist; break; case LUA_VUSERDATA: assert(gco2u(o)->nuvalue > 0); o = gco2u(o)->gclist; break; default: assert(0); /* other objects cannot be in a gray list */ } } return total; } /* ** Check objects in gray lists. */ static l_mem checkgrays (global_State *g) { l_mem total = 0; /* count number of elements in all lists */ if (!keepinvariant(g)) return total; total += checkgraylist(g, g->gray); total += checkgraylist(g, g->grayagain); total += checkgraylist(g, g->weak); total += checkgraylist(g, g->allweak); total += checkgraylist(g, g->ephemeron); return total; } /* ** Check whether 'o' should be in a gray list. If so, increment ** 'count' and check its TESTBIT. (It must have been previously set by ** 'checkgraylist'.) */ static void incifingray (global_State *g, GCObject *o, l_mem *count) { if (!keepinvariant(g)) return; /* gray lists not being kept in these phases */ if (o->tt == LUA_VUPVAL) { /* only open upvalues can be gray */ assert(!isgray(o) || upisopen(gco2upv(o))); return; /* upvalues are never in gray lists */ } /* these are the ones that must be in gray lists */ if (isgray(o) || getage(o) == G_TOUCHED2) { (*count)++; assert(testbit(o->marked, TESTBIT)); resetbit(o->marked, TESTBIT); /* prepare for next cycle */ } } static l_mem checklist (global_State *g, int maybedead, int tof, GCObject *newl, GCObject *survival, GCObject *old, GCObject *reallyold) { GCObject *o; l_mem total = 0; /* number of object that should be in gray lists */ for (o = newl; o != survival; o = o->next) { checkobject(g, o, maybedead, G_NEW); incifingray(g, o, &total); assert(!tof == !tofinalize(o)); } for (o = survival; o != old; o = o->next) { checkobject(g, o, 0, G_SURVIVAL); incifingray(g, o, &total); assert(!tof == !tofinalize(o)); } for (o = old; o != reallyold; o = o->next) { checkobject(g, o, 0, G_OLD1); incifingray(g, o, &total); assert(!tof == !tofinalize(o)); } for (o = reallyold; o != NULL; o = o->next) { checkobject(g, o, 0, G_OLD); incifingray(g, o, &total); assert(!tof == !tofinalize(o)); } return total; } int lua_checkmemory (lua_State *L) { global_State *g = G(L); GCObject *o; int maybedead; l_mem totalin; /* total of objects that are in gray lists */ l_mem totalshould; /* total of objects that should be in gray lists */ if (keepinvariant(g)) { assert(!iswhite(mainthread(g))); assert(!iswhite(gcvalue(&g->l_registry))); } assert(!isdead(g, gcvalue(&g->l_registry))); assert(g->sweepgc == NULL || issweepphase(g)); totalin = checkgrays(g); /* check 'fixedgc' list */ for (o = g->fixedgc; o != NULL; o = o->next) { assert(o->tt == LUA_VSHRSTR && isgray(o) && getage(o) == G_OLD); } /* check 'allgc' list */ maybedead = (GCSatomic < g->gcstate && g->gcstate <= GCSswpallgc); totalshould = checklist(g, maybedead, 0, g->allgc, g->survival, g->old1, g->reallyold); /* check 'finobj' list */ totalshould += checklist(g, 0, 1, g->finobj, g->finobjsur, g->finobjold1, g->finobjrold); /* check 'tobefnz' list */ for (o = g->tobefnz; o != NULL; o = o->next) { checkobject(g, o, 0, G_NEW); incifingray(g, o, &totalshould); assert(tofinalize(o)); assert(o->tt == LUA_VUSERDATA || o->tt == LUA_VTABLE); } if (keepinvariant(g)) assert(totalin == totalshould); return 0; } /* }====================================================== */ /* ** {====================================================== ** Disassembler ** ======================================================= */ static char *buildop (Proto *p, int pc, char *buff) { char *obuff = buff; Instruction i = p->code[pc]; OpCode o = GET_OPCODE(i); const char *name = opnames[o]; int line = luaG_getfuncline(p, pc); int lineinfo = (p->lineinfo != NULL) ? p->lineinfo[pc] : 0; if (lineinfo == ABSLINEINFO) buff += sprintf(buff, "(__"); else buff += sprintf(buff, "(%2d", lineinfo); buff += sprintf(buff, " - %4d) %4d - ", line, pc); switch (getOpMode(o)) { case iABC: sprintf(buff, "%-12s%4d %4d %4d%s", name, GETARG_A(i), GETARG_B(i), GETARG_C(i), GETARG_k(i) ? " (k)" : ""); break; case ivABC: sprintf(buff, "%-12s%4d %4d %4d%s", name, GETARG_A(i), GETARG_vB(i), GETARG_vC(i), GETARG_k(i) ? " (k)" : ""); break; case iABx: sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i)); break; case iAsBx: sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i)); break; case iAx: sprintf(buff, "%-12s%4d", name, GETARG_Ax(i)); break; case isJ: sprintf(buff, "%-12s%4d", name, GETARG_sJ(i)); break; } return obuff; } #if 0 void luaI_printcode (Proto *pt, int size) { int pc; for (pc=0; pcmaxstacksize); setnameval(L, "numparams", p->numparams); for (pc=0; pcsizecode; pc++) { char buff[100]; lua_pushinteger(L, pc+1); lua_pushstring(L, buildop(p, pc, buff)); lua_settable(L, -3); } return 1; } static int printcode (lua_State *L) { int pc; Proto *p; luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, "Lua function expected"); p = getproto(obj_at(L, 1)); printf("maxstack: %d\n", p->maxstacksize); printf("numparams: %d\n", p->numparams); for (pc=0; pcsizecode; pc++) { char buff[100]; printf("%s\n", buildop(p, pc, buff)); } return 0; } static int listk (lua_State *L) { Proto *p; int i; luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, "Lua function expected"); p = getproto(obj_at(L, 1)); lua_createtable(L, p->sizek, 0); for (i=0; isizek; i++) { pushobject(L, p->k+i); lua_rawseti(L, -2, i+1); } return 1; } static int listabslineinfo (lua_State *L) { Proto *p; int i; luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, "Lua function expected"); p = getproto(obj_at(L, 1)); luaL_argcheck(L, p->abslineinfo != NULL, 1, "function has no debug info"); lua_createtable(L, 2 * p->sizeabslineinfo, 0); for (i=0; i < p->sizeabslineinfo; i++) { lua_pushinteger(L, p->abslineinfo[i].pc); lua_rawseti(L, -2, 2 * i + 1); lua_pushinteger(L, p->abslineinfo[i].line); lua_rawseti(L, -2, 2 * i + 2); } return 1; } static int listlocals (lua_State *L) { Proto *p; int pc = cast_int(luaL_checkinteger(L, 2)) - 1; int i = 0; const char *name; luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, "Lua function expected"); p = getproto(obj_at(L, 1)); while ((name = luaF_getlocalname(p, ++i, pc)) != NULL) lua_pushstring(L, name); return i-1; } /* }====================================================== */ void lua_printstack (lua_State *L) { int i; int n = lua_gettop(L); printf("stack: >>\n"); for (i = 1; i <= n; i++) { printf("%3d: ", i); lua_printvalue(s2v(L->ci->func.p + i)); printf("\n"); } printf("<<\n"); } int lua_printallstack (lua_State *L) { StkId p; int i = 1; CallInfo *ci = &L->base_ci; printf("stack: >>\n"); for (p = L->stack.p; p < L->top.p; p++) { if (ci != NULL && p == ci->func.p) { printf(" ---\n"); if (ci == L->ci) ci = NULL; /* printed last frame */ else ci = ci->next; } printf("%3d: ", i++); lua_printvalue(s2v(p)); printf("\n"); } printf("<<\n"); return 0; } static int get_limits (lua_State *L) { lua_createtable(L, 0, 5); setnameval(L, "IS32INT", LUAI_IS32INT); setnameval(L, "MAXARG_Ax", MAXARG_Ax); setnameval(L, "MAXARG_Bx", MAXARG_Bx); setnameval(L, "OFFSET_sBx", OFFSET_sBx); setnameval(L, "NUM_OPCODES", NUM_OPCODES); return 1; } static int get_sizes (lua_State *L) { lua_newtable(L); setnameval(L, "Lua state", sizeof(lua_State)); setnameval(L, "global state", sizeof(global_State)); setnameval(L, "TValue", sizeof(TValue)); setnameval(L, "Node", sizeof(Node)); setnameval(L, "stack Value", sizeof(StackValue)); return 1; } static int mem_query (lua_State *L) { if (lua_isnone(L, 1)) { lua_pushinteger(L, cast_Integer(l_memcontrol.total)); lua_pushinteger(L, cast_Integer(l_memcontrol.numblocks)); lua_pushinteger(L, cast_Integer(l_memcontrol.maxmem)); return 3; } else if (lua_isnumber(L, 1)) { unsigned long limit = cast(unsigned long, luaL_checkinteger(L, 1)); if (limit == 0) limit = ULONG_MAX; l_memcontrol.memlimit = limit; return 0; } else { const char *t = luaL_checkstring(L, 1); int i; for (i = LUA_NUMTYPES - 1; i >= 0; i--) { if (strcmp(t, ttypename(i)) == 0) { lua_pushinteger(L, cast_Integer(l_memcontrol.objcount[i])); return 1; } } return luaL_error(L, "unknown type '%s'", t); } } static int alloc_count (lua_State *L) { if (lua_isnone(L, 1)) l_memcontrol.countlimit = cast(unsigned long, ~0L); else l_memcontrol.countlimit = cast(unsigned long, luaL_checkinteger(L, 1)); return 0; } static int alloc_failnext (lua_State *L) { UNUSED(L); l_memcontrol.failnext = 1; return 0; } static int settrick (lua_State *L) { if (ttisnil(obj_at(L, 1))) l_Trick = NULL; else l_Trick = gcvalue(obj_at(L, 1)); return 0; } static int gc_color (lua_State *L) { TValue *o; luaL_checkany(L, 1); o = obj_at(L, 1); if (!iscollectable(o)) lua_pushstring(L, "no collectable"); else { GCObject *obj = gcvalue(o); lua_pushstring(L, isdead(G(L), obj) ? "dead" : iswhite(obj) ? "white" : isblack(obj) ? "black" : "gray"); } return 1; } static int gc_age (lua_State *L) { TValue *o; luaL_checkany(L, 1); o = obj_at(L, 1); if (!iscollectable(o)) lua_pushstring(L, "no collectable"); else { static const char *gennames[] = {"new", "survival", "old0", "old1", "old", "touched1", "touched2"}; GCObject *obj = gcvalue(o); lua_pushstring(L, gennames[getage(obj)]); } return 1; } static int gc_printobj (lua_State *L) { TValue *o; luaL_checkany(L, 1); o = obj_at(L, 1); if (!iscollectable(o)) printf("no collectable\n"); else { GCObject *obj = gcvalue(o); printobj(G(L), obj); printf("\n"); } return 0; } static const char *const statenames[] = { "propagate", "enteratomic", "atomic", "sweepallgc", "sweepfinobj", "sweeptobefnz", "sweepend", "callfin", "pause", ""}; static int gc_state (lua_State *L) { static const int states[] = { GCSpropagate, GCSenteratomic, GCSatomic, GCSswpallgc, GCSswpfinobj, GCSswptobefnz, GCSswpend, GCScallfin, GCSpause, -1}; int option = states[luaL_checkoption(L, 1, "", statenames)]; global_State *g = G(L); if (option == -1) { lua_pushstring(L, statenames[g->gcstate]); return 1; } else { if (g->gckind != KGC_INC) luaL_error(L, "cannot change states in generational mode"); lua_lock(L); if (option < g->gcstate) { /* must cross 'pause'? */ luaC_runtilstate(L, GCSpause, 1); /* run until pause */ } luaC_runtilstate(L, option, 0); /* do not skip propagation state */ lua_assert(g->gcstate == option); lua_unlock(L); return 0; } } static int tracinggc = 0; void luai_tracegctest (lua_State *L, int first) { if (!tracinggc) return; else { global_State *g = G(L); lua_unlock(L); g->gcstp = GCSTPGC; lua_checkstack(L, 10); lua_getfield(L, LUA_REGISTRYINDEX, "tracegc"); lua_pushboolean(L, first); lua_call(L, 1, 0); g->gcstp = 0; lua_lock(L); } } static int tracegc (lua_State *L) { if (lua_isnil(L, 1)) tracinggc = 0; else { tracinggc = 1; lua_setfield(L, LUA_REGISTRYINDEX, "tracegc"); } return 0; } static int hash_query (lua_State *L) { if (lua_isnone(L, 2)) { TString *ts; luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected"); ts = tsvalue(obj_at(L, 1)); if (ts->tt == LUA_VLNGSTR) luaS_hashlongstr(ts); /* make sure long string has a hash */ lua_pushinteger(L, cast_int(ts->hash)); } else { TValue *o = obj_at(L, 1); Table *t; luaL_checktype(L, 2, LUA_TTABLE); t = hvalue(obj_at(L, 2)); lua_pushinteger(L, cast_Integer(luaH_mainposition(t, o) - t->node)); } return 1; } static int stacklevel (lua_State *L) { int a = 0; lua_pushinteger(L, cast_Integer(L->top.p - L->stack.p)); lua_pushinteger(L, stacksize(L)); lua_pushinteger(L, cast_Integer(L->nCcalls)); lua_pushinteger(L, L->nci); lua_pushinteger(L, (lua_Integer)(size_t)&a); return 5; } static int table_query (lua_State *L) { const Table *t; int i = cast_int(luaL_optinteger(L, 2, -1)); unsigned int asize; luaL_checktype(L, 1, LUA_TTABLE); t = hvalue(obj_at(L, 1)); asize = t->asize; if (i == -1) { lua_pushinteger(L, cast_Integer(asize)); lua_pushinteger(L, cast_Integer(allocsizenode(t))); lua_pushinteger(L, cast_Integer(asize > 0 ? *lenhint(t) : 0)); return 3; } else if (cast_uint(i) < asize) { lua_pushinteger(L, i); if (!tagisempty(*getArrTag(t, i))) arr2obj(t, cast_uint(i), s2v(L->top.p)); else setnilvalue(s2v(L->top.p)); api_incr_top(L); lua_pushnil(L); } else if (cast_uint(i -= cast_int(asize)) < sizenode(t)) { TValue k; getnodekey(L, &k, gnode(t, i)); if (!isempty(gval(gnode(t, i))) || ttisnil(&k) || ttisnumber(&k)) { pushobject(L, &k); } else lua_pushliteral(L, ""); if (!isempty(gval(gnode(t, i)))) pushobject(L, gval(gnode(t, i))); else lua_pushnil(L); lua_pushinteger(L, gnext(&t->node[i])); } return 3; } static int gc_query (lua_State *L) { global_State *g = G(L); lua_pushstring(L, g->gckind == KGC_INC ? "inc" : g->gckind == KGC_GENMAJOR ? "genmajor" : "genminor"); lua_pushstring(L, statenames[g->gcstate]); lua_pushinteger(L, cast_st2S(gettotalbytes(g))); lua_pushinteger(L, cast_st2S(g->GCdebt)); lua_pushinteger(L, cast_st2S(g->GCmarked)); lua_pushinteger(L, cast_st2S(g->GCmajorminor)); return 6; } static int test_codeparam (lua_State *L) { lua_Integer p = luaL_checkinteger(L, 1); lua_pushinteger(L, luaO_codeparam(cast_uint(p))); return 1; } static int test_applyparam (lua_State *L) { lua_Integer p = luaL_checkinteger(L, 1); lua_Integer x = luaL_checkinteger(L, 2); lua_pushinteger(L, cast_Integer(luaO_applyparam(cast_byte(p), x))); return 1; } static int string_query (lua_State *L) { stringtable *tb = &G(L)->strt; int s = cast_int(luaL_optinteger(L, 1, 0)) - 1; if (s == -1) { lua_pushinteger(L ,tb->size); lua_pushinteger(L ,tb->nuse); return 2; } else if (s < tb->size) { TString *ts; int n = 0; for (ts = tb->hash[s]; ts != NULL; ts = ts->u.hnext) { setsvalue2s(L, L->top.p, ts); api_incr_top(L); n++; } return n; } else return 0; } static int getreftable (lua_State *L) { if (lua_istable(L, 2)) /* is there a table as second argument? */ return 2; /* use it as the table */ else return LUA_REGISTRYINDEX; /* default is to use the register */ } static int tref (lua_State *L) { int t = getreftable(L); int level = lua_gettop(L); luaL_checkany(L, 1); lua_pushvalue(L, 1); lua_pushinteger(L, luaL_ref(L, t)); cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level+1); /* +1 for result */ return 1; } static int getref (lua_State *L) { int t = getreftable(L); int level = lua_gettop(L); lua_rawgeti(L, t, luaL_checkinteger(L, 1)); cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level+1); return 1; } static int unref (lua_State *L) { int t = getreftable(L); int level = lua_gettop(L); luaL_unref(L, t, cast_int(luaL_checkinteger(L, 1))); cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level); return 0; } static int upvalue (lua_State *L) { int n = cast_int(luaL_checkinteger(L, 2)); luaL_checktype(L, 1, LUA_TFUNCTION); if (lua_isnone(L, 3)) { const char *name = lua_getupvalue(L, 1, n); if (name == NULL) return 0; lua_pushstring(L, name); return 2; } else { const char *name = lua_setupvalue(L, 1, n); lua_pushstring(L, name); return 1; } } static int newuserdata (lua_State *L) { size_t size = cast_sizet(luaL_optinteger(L, 1, 0)); int nuv = cast_int(luaL_optinteger(L, 2, 0)); char *p = cast_charp(lua_newuserdatauv(L, size, nuv)); while (size--) *p++ = '\0'; return 1; } static int pushuserdata (lua_State *L) { lua_Integer u = luaL_checkinteger(L, 1); lua_pushlightuserdata(L, cast_voidp(cast_sizet(u))); return 1; } static int udataval (lua_State *L) { lua_pushinteger(L, cast_st2S(cast_sizet(lua_touserdata(L, 1)))); return 1; } static int doonnewstack (lua_State *L) { lua_State *L1 = lua_newthread(L); size_t l; const char *s = luaL_checklstring(L, 1, &l); int status = luaL_loadbuffer(L1, s, l, s); if (status == LUA_OK) status = lua_pcall(L1, 0, 0, 0); lua_pushinteger(L, status); return 1; } static int s2d (lua_State *L) { lua_pushnumber(L, cast_num(*cast(const double *, luaL_checkstring(L, 1)))); return 1; } static int d2s (lua_State *L) { double d = cast(double, luaL_checknumber(L, 1)); lua_pushlstring(L, cast_charp(&d), sizeof(d)); return 1; } static int num2int (lua_State *L) { lua_pushinteger(L, lua_tointeger(L, 1)); return 1; } static int makeseed (lua_State *L) { lua_pushinteger(L, cast_Integer(luaL_makeseed(L))); return 1; } static int newstate (lua_State *L) { void *ud; lua_Alloc f = lua_getallocf(L, &ud); lua_State *L1 = lua_newstate(f, ud, 0); if (L1) { lua_atpanic(L1, tpanic); lua_pushlightuserdata(L, L1); } else lua_pushnil(L); return 1; } static lua_State *getstate (lua_State *L) { lua_State *L1 = cast(lua_State *, lua_touserdata(L, 1)); luaL_argcheck(L, L1 != NULL, 1, "state expected"); return L1; } static int loadlib (lua_State *L) { lua_State *L1 = getstate(L); int load = cast_int(luaL_checkinteger(L, 2)); int preload = cast_int(luaL_checkinteger(L, 3)); luaL_openselectedlibs(L1, load, preload); luaL_requiref(L1, "T", luaB_opentests, 0); lua_assert(lua_type(L1, -1) == LUA_TTABLE); /* 'requiref' should not reload module already loaded... */ luaL_requiref(L1, "T", NULL, 1); /* seg. fault if it reloads */ /* ...but should return the same module */ lua_assert(lua_compare(L1, -1, -2, LUA_OPEQ)); return 0; } static int closestate (lua_State *L) { lua_State *L1 = getstate(L); lua_close(L1); return 0; } static int doremote (lua_State *L) { lua_State *L1 = getstate(L); size_t lcode; const char *code = luaL_checklstring(L, 2, &lcode); int status; lua_settop(L1, 0); status = luaL_loadbuffer(L1, code, lcode, code); if (status == LUA_OK) status = lua_pcall(L1, 0, LUA_MULTRET, 0); if (status != LUA_OK) { lua_pushnil(L); lua_pushstring(L, lua_tostring(L1, -1)); lua_pushinteger(L, status); return 3; } else { int i = 0; while (!lua_isnone(L1, ++i)) lua_pushstring(L, lua_tostring(L1, i)); lua_pop(L1, i-1); return i-1; } } static int log2_aux (lua_State *L) { unsigned int x = (unsigned int)luaL_checkinteger(L, 1); lua_pushinteger(L, luaO_ceillog2(x)); return 1; } struct Aux { jmp_buf jb; const char *paniccode; lua_State *L; }; /* ** does a long-jump back to "main program". */ static int panicback (lua_State *L) { struct Aux *b; lua_checkstack(L, 1); /* open space for 'Aux' struct */ lua_getfield(L, LUA_REGISTRYINDEX, "_jmpbuf"); /* get 'Aux' struct */ b = (struct Aux *)lua_touserdata(L, -1); lua_pop(L, 1); /* remove 'Aux' struct */ runC(b->L, L, b->paniccode); /* run optional panic code */ longjmp(b->jb, 1); return 1; /* to avoid warnings */ } static int checkpanic (lua_State *L) { struct Aux b; void *ud; lua_State *L1; const char *code = luaL_checkstring(L, 1); lua_Alloc f = lua_getallocf(L, &ud); b.paniccode = luaL_optstring(L, 2, ""); b.L = L; L1 = lua_newstate(f, ud, 0); /* create new state */ if (L1 == NULL) { /* error? */ lua_pushstring(L, MEMERRMSG); return 1; } lua_atpanic(L1, panicback); /* set its panic function */ lua_pushlightuserdata(L1, &b); lua_setfield(L1, LUA_REGISTRYINDEX, "_jmpbuf"); /* store 'Aux' struct */ if (setjmp(b.jb) == 0) { /* set jump buffer */ runC(L, L1, code); /* run code unprotected */ lua_pushliteral(L, "no errors"); } else { /* error handling */ /* move error message to original state */ lua_pushstring(L, lua_tostring(L1, -1)); } lua_close(L1); return 1; } static int externKstr (lua_State *L) { size_t len; const char *s = luaL_checklstring(L, 1, &len); lua_pushexternalstring(L, s, len, NULL, NULL); return 1; } /* ** Create a buffer with the content of a given string and then ** create an external string using that buffer. Use the allocation ** function from Lua to create and free the buffer. */ static int externstr (lua_State *L) { size_t len; const char *s = luaL_checklstring(L, 1, &len); void *ud; lua_Alloc allocf = lua_getallocf(L, &ud); /* get allocation function */ /* create the buffer */ char *buff = cast_charp((*allocf)(ud, NULL, 0, len + 1)); if (buff == NULL) { /* memory error? */ lua_pushliteral(L, "not enough memory"); lua_error(L); /* raise a memory error */ } /* copy string content to buffer, including ending 0 */ memcpy(buff, s, (len + 1) * sizeof(char)); /* create external string */ lua_pushexternalstring(L, buff, len, allocf, ud); return 1; } /* ** {==================================================================== ** function to test the API with C. It interprets a kind of assembler ** language with calls to the API, so the test can be driven by Lua code ** ===================================================================== */ static void sethookaux (lua_State *L, int mask, int count, const char *code); static const char *const delimits = " \t\n,;"; static void skip (const char **pc) { for (;;) { if (**pc != '\0' && strchr(delimits, **pc)) (*pc)++; else if (**pc == '#') { /* comment? */ while (**pc != '\n' && **pc != '\0') (*pc)++; /* until end-of-line */ } else break; } } static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) { int res = 0; int sig = 1; skip(pc); if (**pc == '.') { res = cast_int(lua_tointeger(L1, -1)); lua_pop(L1, 1); (*pc)++; return res; } else if (**pc == '*') { res = lua_gettop(L1); (*pc)++; return res; } else if (**pc == '!') { (*pc)++; if (**pc == 'G') res = LUA_RIDX_GLOBALS; else if (**pc == 'M') res = LUA_RIDX_MAINTHREAD; else lua_assert(0); (*pc)++; return res; } else if (**pc == '-') { sig = -1; (*pc)++; } if (!lisdigit(cast_uchar(**pc))) luaL_error(L, "number expected (%s)", *pc); while (lisdigit(cast_uchar(**pc))) res = res*10 + (*(*pc)++) - '0'; return sig*res; } static const char *getstring_aux (lua_State *L, char *buff, const char **pc) { int i = 0; skip(pc); if (**pc == '"' || **pc == '\'') { /* quoted string? */ int quote = *(*pc)++; while (**pc != quote) { if (**pc == '\0') luaL_error(L, "unfinished string in C script"); buff[i++] = *(*pc)++; } (*pc)++; } else { while (**pc != '\0' && !strchr(delimits, **pc)) buff[i++] = *(*pc)++; } buff[i] = '\0'; return buff; } static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) { skip(pc); switch (*(*pc)++) { case 'R': return LUA_REGISTRYINDEX; case 'U': return lua_upvalueindex(getnum_aux(L, L1, pc)); default: { int n; (*pc)--; /* to read again */ n = getnum_aux(L, L1, pc); if (n == 0) return 0; else return lua_absindex(L1, n); } } } static const char *const statcodes[] = {"OK", "YIELD", "ERRRUN", "ERRSYNTAX", MEMERRMSG, "ERRERR"}; /* ** Avoid these stat codes from being collected, to avoid possible ** memory error when pushing them. */ static void regcodes (lua_State *L) { unsigned int i; for (i = 0; i < sizeof(statcodes) / sizeof(statcodes[0]); i++) { lua_pushboolean(L, 1); lua_setfield(L, LUA_REGISTRYINDEX, statcodes[i]); } } #define EQ(s1) (strcmp(s1, inst) == 0) #define getnum (getnum_aux(L, L1, &pc)) #define getstring (getstring_aux(L, buff, &pc)) #define getindex (getindex_aux(L, L1, &pc)) static int testC (lua_State *L); static int Cfunck (lua_State *L, int status, lua_KContext ctx); /* ** arithmetic operation encoding for 'arith' instruction ** LUA_OPIDIV -> \ ** LUA_OPSHL -> < ** LUA_OPSHR -> > ** LUA_OPUNM -> _ ** LUA_OPBNOT -> ! */ static const char ops[] = "+-*%^/\\&|~<>_!"; static int runC (lua_State *L, lua_State *L1, const char *pc) { char buff[300]; int status = 0; if (pc == NULL) return luaL_error(L, "attempt to runC null script"); for (;;) { const char *inst = getstring; if EQ("") return 0; else if EQ("absindex") { lua_pushinteger(L1, getindex); } else if EQ("append") { int t = getindex; int i = cast_int(lua_rawlen(L1, t)); lua_rawseti(L1, t, i + 1); } else if EQ("arith") { int op; skip(&pc); op = cast_int(strchr(ops, *pc++) - ops); lua_arith(L1, op); } else if EQ("call") { int narg = getnum; int nres = getnum; lua_call(L1, narg, nres); } else if EQ("callk") { int narg = getnum; int nres = getnum; int i = getindex; lua_callk(L1, narg, nres, i, Cfunck); } else if EQ("checkstack") { int sz = getnum; const char *msg = getstring; if (*msg == '\0') msg = NULL; /* to test 'luaL_checkstack' with no message */ luaL_checkstack(L1, sz, msg); } else if EQ("rawcheckstack") { int sz = getnum; lua_pushboolean(L1, lua_checkstack(L1, sz)); } else if EQ("compare") { const char *opt = getstring; /* EQ, LT, or LE */ int op = (opt[0] == 'E') ? LUA_OPEQ : (opt[1] == 'T') ? LUA_OPLT : LUA_OPLE; int a = getindex; int b = getindex; lua_pushboolean(L1, lua_compare(L1, a, b, op)); } else if EQ("concat") { lua_concat(L1, getnum); } else if EQ("copy") { int f = getindex; lua_copy(L1, f, getindex); } else if EQ("func2num") { lua_CFunction func = lua_tocfunction(L1, getindex); lua_pushinteger(L1, cast_st2S(cast_sizet(func))); } else if EQ("getfield") { int t = getindex; int tp = lua_getfield(L1, t, getstring); lua_assert(tp == lua_type(L1, -1)); } else if EQ("getglobal") { lua_getglobal(L1, getstring); } else if EQ("getmetatable") { if (lua_getmetatable(L1, getindex) == 0) lua_pushnil(L1); } else if EQ("gettable") { int tp = lua_gettable(L1, getindex); lua_assert(tp == lua_type(L1, -1)); } else if EQ("gettop") { lua_pushinteger(L1, lua_gettop(L1)); } else if EQ("gsub") { int a = getnum; int b = getnum; int c = getnum; luaL_gsub(L1, lua_tostring(L1, a), lua_tostring(L1, b), lua_tostring(L1, c)); } else if EQ("insert") { lua_insert(L1, getnum); } else if EQ("iscfunction") { lua_pushboolean(L1, lua_iscfunction(L1, getindex)); } else if EQ("isfunction") { lua_pushboolean(L1, lua_isfunction(L1, getindex)); } else if EQ("isnil") { lua_pushboolean(L1, lua_isnil(L1, getindex)); } else if EQ("isnull") { lua_pushboolean(L1, lua_isnone(L1, getindex)); } else if EQ("isnumber") { lua_pushboolean(L1, lua_isnumber(L1, getindex)); } else if EQ("isstring") { lua_pushboolean(L1, lua_isstring(L1, getindex)); } else if EQ("istable") { lua_pushboolean(L1, lua_istable(L1, getindex)); } else if EQ("isudataval") { lua_pushboolean(L1, lua_islightuserdata(L1, getindex)); } else if EQ("isuserdata") { lua_pushboolean(L1, lua_isuserdata(L1, getindex)); } else if EQ("len") { lua_len(L1, getindex); } else if EQ("Llen") { lua_pushinteger(L1, luaL_len(L1, getindex)); } else if EQ("loadfile") { luaL_loadfile(L1, luaL_checkstring(L1, getnum)); } else if EQ("loadstring") { size_t slen; const char *s = luaL_checklstring(L1, getnum, &slen); const char *name = getstring; const char *mode = getstring; luaL_loadbufferx(L1, s, slen, name, mode); } else if EQ("newmetatable") { lua_pushboolean(L1, luaL_newmetatable(L1, getstring)); } else if EQ("newtable") { lua_newtable(L1); } else if EQ("newthread") { lua_newthread(L1); } else if EQ("resetthread") { lua_pushinteger(L1, lua_resetthread(L1)); /* deprecated */ } else if EQ("newuserdata") { lua_newuserdata(L1, cast_sizet(getnum)); } else if EQ("next") { lua_next(L1, -2); } else if EQ("objsize") { lua_pushinteger(L1, l_castU2S(lua_rawlen(L1, getindex))); } else if EQ("pcall") { int narg = getnum; int nres = getnum; status = lua_pcall(L1, narg, nres, getnum); } else if EQ("pcallk") { int narg = getnum; int nres = getnum; int i = getindex; status = lua_pcallk(L1, narg, nres, 0, i, Cfunck); } else if EQ("pop") { lua_pop(L1, getnum); } else if EQ("printstack") { int n = getnum; if (n != 0) { lua_printvalue(s2v(L->ci->func.p + n)); printf("\n"); } else lua_printstack(L1); } else if EQ("print") { const char *msg = getstring; printf("%s\n", msg); } else if EQ("warningC") { const char *msg = getstring; lua_warning(L1, msg, 1); } else if EQ("warning") { const char *msg = getstring; lua_warning(L1, msg, 0); } else if EQ("pushbool") { lua_pushboolean(L1, getnum); } else if EQ("pushcclosure") { lua_pushcclosure(L1, testC, getnum); } else if EQ("pushint") { lua_pushinteger(L1, getnum); } else if EQ("pushnil") { lua_pushnil(L1); } else if EQ("pushnum") { lua_pushnumber(L1, (lua_Number)getnum); } else if EQ("pushstatus") { lua_pushstring(L1, statcodes[status]); } else if EQ("pushstring") { lua_pushstring(L1, getstring); } else if EQ("pushupvalueindex") { lua_pushinteger(L1, lua_upvalueindex(getnum)); } else if EQ("pushvalue") { lua_pushvalue(L1, getindex); } else if EQ("pushfstringI") { lua_pushfstring(L1, lua_tostring(L, -2), (int)lua_tointeger(L, -1)); } else if EQ("pushfstringS") { lua_pushfstring(L1, lua_tostring(L, -2), lua_tostring(L, -1)); } else if EQ("pushfstringP") { lua_pushfstring(L1, lua_tostring(L, -2), lua_topointer(L, -1)); } else if EQ("rawget") { int t = getindex; lua_rawget(L1, t); } else if EQ("rawgeti") { int t = getindex; lua_rawgeti(L1, t, getnum); } else if EQ("rawgetp") { int t = getindex; lua_rawgetp(L1, t, cast_voidp(cast_sizet(getnum))); } else if EQ("rawset") { int t = getindex; lua_rawset(L1, t); } else if EQ("rawseti") { int t = getindex; lua_rawseti(L1, t, getnum); } else if EQ("rawsetp") { int t = getindex; lua_rawsetp(L1, t, cast_voidp(cast_sizet(getnum))); } else if EQ("remove") { lua_remove(L1, getnum); } else if EQ("replace") { lua_replace(L1, getindex); } else if EQ("resume") { int i = getindex; int nres; status = lua_resume(lua_tothread(L1, i), L, getnum, &nres); } else if EQ("traceback") { const char *msg = getstring; int level = getnum; luaL_traceback(L1, L1, msg, level); } else if EQ("threadstatus") { lua_pushstring(L1, statcodes[lua_status(L1)]); } else if EQ("alloccount") { l_memcontrol.countlimit = cast_uint(getnum); } else if EQ("return") { int n = getnum; if (L1 != L) { int i; for (i = 0; i < n; i++) { int idx = -(n - i); switch (lua_type(L1, idx)) { case LUA_TBOOLEAN: lua_pushboolean(L, lua_toboolean(L1, idx)); break; default: lua_pushstring(L, lua_tostring(L1, idx)); break; } } } return n; } else if EQ("rotate") { int i = getindex; lua_rotate(L1, i, getnum); } else if EQ("setfield") { int t = getindex; const char *s = getstring; lua_setfield(L1, t, s); } else if EQ("seti") { int t = getindex; lua_seti(L1, t, getnum); } else if EQ("setglobal") { const char *s = getstring; lua_setglobal(L1, s); } else if EQ("sethook") { int mask = getnum; int count = getnum; const char *s = getstring; sethookaux(L1, mask, count, s); } else if EQ("setmetatable") { int idx = getindex; lua_setmetatable(L1, idx); } else if EQ("settable") { lua_settable(L1, getindex); } else if EQ("settop") { lua_settop(L1, getnum); } else if EQ("testudata") { int i = getindex; lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL); } else if EQ("error") { lua_error(L1); } else if EQ("abort") { abort(); } else if EQ("throw") { #if defined(__cplusplus) static struct X { int x; } x; throw x; #else luaL_error(L1, "C++"); #endif break; } else if EQ("tobool") { lua_pushboolean(L1, lua_toboolean(L1, getindex)); } else if EQ("tocfunction") { lua_pushcfunction(L1, lua_tocfunction(L1, getindex)); } else if EQ("tointeger") { lua_pushinteger(L1, lua_tointeger(L1, getindex)); } else if EQ("tonumber") { lua_pushnumber(L1, lua_tonumber(L1, getindex)); } else if EQ("topointer") { lua_pushlightuserdata(L1, cast_voidp(lua_topointer(L1, getindex))); } else if EQ("touserdata") { lua_pushlightuserdata(L1, lua_touserdata(L1, getindex)); } else if EQ("tostring") { const char *s = lua_tostring(L1, getindex); const char *s1 = lua_pushstring(L1, s); cast_void(s1); /* to avoid warnings */ lua_longassert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0); } else if EQ("Ltolstring") { luaL_tolstring(L1, getindex, NULL); } else if EQ("type") { lua_pushstring(L1, luaL_typename(L1, getnum)); } else if EQ("xmove") { int f = getindex; int t = getindex; lua_State *fs = (f == 0) ? L1 : lua_tothread(L1, f); lua_State *ts = (t == 0) ? L1 : lua_tothread(L1, t); int n = getnum; if (n == 0) n = lua_gettop(fs); lua_xmove(fs, ts, n); } else if EQ("isyieldable") { lua_pushboolean(L1, lua_isyieldable(lua_tothread(L1, getindex))); } else if EQ("yield") { return lua_yield(L1, getnum); } else if EQ("yieldk") { int nres = getnum; int i = getindex; return lua_yieldk(L1, nres, i, Cfunck); } else if EQ("toclose") { lua_toclose(L1, getnum); } else if EQ("closeslot") { lua_closeslot(L1, getnum); } else if EQ("argerror") { int arg = getnum; luaL_argerror(L1, arg, getstring); } else luaL_error(L, "unknown instruction %s", buff); } return 0; } static int testC (lua_State *L) { lua_State *L1; const char *pc; if (lua_isuserdata(L, 1)) { L1 = getstate(L); pc = luaL_checkstring(L, 2); } else if (lua_isthread(L, 1)) { L1 = lua_tothread(L, 1); pc = luaL_checkstring(L, 2); } else { L1 = L; pc = luaL_checkstring(L, 1); } return runC(L, L1, pc); } static int Cfunc (lua_State *L) { return runC(L, L, lua_tostring(L, lua_upvalueindex(1))); } static int Cfunck (lua_State *L, int status, lua_KContext ctx) { lua_pushstring(L, statcodes[status]); lua_setglobal(L, "status"); lua_pushinteger(L, cast_Integer(ctx)); lua_setglobal(L, "ctx"); return runC(L, L, lua_tostring(L, cast_int(ctx))); } static int makeCfunc (lua_State *L) { luaL_checkstring(L, 1); lua_pushcclosure(L, Cfunc, lua_gettop(L)); return 1; } /* }====================================================== */ /* ** {====================================================== ** tests for C hooks ** ======================================================= */ /* ** C hook that runs the C script stored in registry.C_HOOK[L] */ static void Chook (lua_State *L, lua_Debug *ar) { const char *scpt; const char *const events [] = {"call", "ret", "line", "count", "tailcall"}; lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK"); lua_pushlightuserdata(L, L); lua_gettable(L, -2); /* get C_HOOK[L] (script saved by sethookaux) */ scpt = lua_tostring(L, -1); /* not very religious (string will be popped) */ lua_pop(L, 2); /* remove C_HOOK and script */ lua_pushstring(L, events[ar->event]); /* may be used by script */ lua_pushinteger(L, ar->currentline); /* may be used by script */ runC(L, L, scpt); /* run script from C_HOOK[L] */ } /* ** sets 'registry.C_HOOK[L] = scpt' and sets 'Chook' as a hook */ static void sethookaux (lua_State *L, int mask, int count, const char *scpt) { if (*scpt == '\0') { /* no script? */ lua_sethook(L, NULL, 0, 0); /* turn off hooks */ return; } lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* get C_HOOK table */ if (!lua_istable(L, -1)) { /* no hook table? */ lua_pop(L, 1); /* remove previous value */ lua_newtable(L); /* create new C_HOOK table */ lua_pushvalue(L, -1); lua_setfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* register it */ } lua_pushlightuserdata(L, L); lua_pushstring(L, scpt); lua_settable(L, -3); /* C_HOOK[L] = script */ lua_sethook(L, Chook, mask, count); } static int sethook (lua_State *L) { if (lua_isnoneornil(L, 1)) lua_sethook(L, NULL, 0, 0); /* turn off hooks */ else { const char *scpt = luaL_checkstring(L, 1); const char *smask = luaL_checkstring(L, 2); int count = cast_int(luaL_optinteger(L, 3, 0)); int mask = 0; if (strchr(smask, 'c')) mask |= LUA_MASKCALL; if (strchr(smask, 'r')) mask |= LUA_MASKRET; if (strchr(smask, 'l')) mask |= LUA_MASKLINE; if (count > 0) mask |= LUA_MASKCOUNT; sethookaux(L, mask, count, scpt); } return 0; } static int coresume (lua_State *L) { int status, nres; lua_State *co = lua_tothread(L, 1); luaL_argcheck(L, co, 1, "coroutine expected"); status = lua_resume(co, L, 0, &nres); if (status != LUA_OK && status != LUA_YIELD) { lua_pushboolean(L, 0); lua_insert(L, -2); return 2; /* return false + error message */ } else { lua_pushboolean(L, 1); return 1; } } #if !defined(LUA_USE_POSIX) #define nonblock NULL #else #include #include static int nonblock (lua_State *L) { FILE *f = cast(luaL_Stream*, luaL_checkudata(L, 1, LUA_FILEHANDLE))->f; int fd = fileno(f); int flags = fcntl(fd, F_GETFL, 0); flags |= O_NONBLOCK; fcntl(fd, F_SETFL, flags); return 0; } #endif /* }====================================================== */ static const struct luaL_Reg tests_funcs[] = { {"checkmemory", lua_checkmemory}, {"closestate", closestate}, {"d2s", d2s}, {"doonnewstack", doonnewstack}, {"doremote", doremote}, {"gccolor", gc_color}, {"gcage", gc_age}, {"gcstate", gc_state}, {"tracegc", tracegc}, {"pobj", gc_printobj}, {"getref", getref}, {"hash", hash_query}, {"log2", log2_aux}, {"limits", get_limits}, {"listcode", listcode}, {"printcode", printcode}, {"printallstack", lua_printallstack}, {"listk", listk}, {"listabslineinfo", listabslineinfo}, {"listlocals", listlocals}, {"loadlib", loadlib}, {"checkpanic", checkpanic}, {"newstate", newstate}, {"newuserdata", newuserdata}, {"num2int", num2int}, {"makeseed", makeseed}, {"pushuserdata", pushuserdata}, {"gcquery", gc_query}, {"querystr", string_query}, {"querytab", table_query}, {"codeparam", test_codeparam}, {"applyparam", test_applyparam}, {"ref", tref}, {"resume", coresume}, {"s2d", s2d}, {"sethook", sethook}, {"stacklevel", stacklevel}, {"sizes", get_sizes}, {"testC", testC}, {"makeCfunc", makeCfunc}, {"totalmem", mem_query}, {"alloccount", alloc_count}, {"allocfailnext", alloc_failnext}, {"trick", settrick}, {"udataval", udataval}, {"unref", unref}, {"upvalue", upvalue}, {"externKstr", externKstr}, {"externstr", externstr}, {"nonblock", nonblock}, {NULL, NULL} }; static void checkfinalmem (void) { lua_assert(l_memcontrol.numblocks == 0); lua_assert(l_memcontrol.total == 0); } int luaB_opentests (lua_State *L) { void *ud; lua_Alloc f = lua_getallocf(L, &ud); lua_atpanic(L, &tpanic); lua_setwarnf(L, &warnf, L); lua_pushboolean(L, 0); lua_setglobal(L, "_WARN"); /* _WARN = false */ regcodes(L); atexit(checkfinalmem); lua_assert(f == debug_realloc && ud == cast_voidp(&l_memcontrol)); lua_setallocf(L, f, ud); /* exercise this function */ luaL_newlib(L, tests_funcs); return 1; } #endif ================================================ FILE: 3rd/lua/ltests.h ================================================ /* ** $Id: ltests.h $ ** Internal Header for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ #ifndef ltests_h #define ltests_h #include #include /* test Lua with compatibility code */ #define LUA_COMPAT_MATHLIB #undef LUA_COMPAT_GLOBAL #define LUA_DEBUG /* turn on assertions */ #define LUAI_ASSERT /* to avoid warnings, and to make sure value is really unused */ #define UNUSED(x) (x=0, (void)(x)) /* test for sizes in 'l_sprintf' (make sure whole buffer is available) */ #undef l_sprintf #if !defined(LUA_USE_C89) #define l_sprintf(s,sz,f,i) (memset(s,0xAB,sz), snprintf(s,sz,f,i)) #else #define l_sprintf(s,sz,f,i) (memset(s,0xAB,sz), sprintf(s,f,i)) #endif /* get a chance to test code without jump tables */ #define LUA_USE_JUMPTABLE 0 /* use 32-bit integers in random generator */ #define LUA_RAND32 /* test stack reallocation without strict address use */ #define LUAI_STRICT_ADDRESS 0 /* memory-allocator control variables */ typedef struct Memcontrol { int failnext; unsigned long numblocks; unsigned long total; unsigned long maxmem; unsigned long memlimit; unsigned long countlimit; unsigned long objcount[LUA_NUMTYPES]; } Memcontrol; LUA_API Memcontrol l_memcontrol; #define luai_tracegc(L,f) luai_tracegctest(L, f) extern void luai_tracegctest (lua_State *L, int first); /* ** generic variable for debug tricks */ extern void *l_Trick; /* ** Function to traverse and check all memory used by Lua */ extern int lua_checkmemory (lua_State *L); /* ** Function to print an object GC-friendly */ struct GCObject; extern void lua_printobj (lua_State *L, struct GCObject *o); /* ** Function to print a value */ struct TValue; extern void lua_printvalue (struct TValue *v); /* ** Function to print the stack */ extern void lua_printstack (lua_State *L); extern int lua_printallstack (lua_State *L); /* test for lock/unlock */ struct L_EXTRA { int lock; int *plock; }; #undef LUA_EXTRASPACE #define LUA_EXTRASPACE sizeof(struct L_EXTRA) #define getlock(l) cast(struct L_EXTRA*, lua_getextraspace(l)) #define luai_userstateopen(l) \ (getlock(l)->lock = 0, getlock(l)->plock = &(getlock(l)->lock)) #define luai_userstateclose(l) \ lua_assert(getlock(l)->lock == 1 && getlock(l)->plock == &(getlock(l)->lock)) #define luai_userstatethread(l,l1) \ lua_assert(getlock(l1)->plock == getlock(l)->plock) #define luai_userstatefree(l,l1) \ lua_assert(getlock(l)->plock == getlock(l1)->plock) #define lua_lock(l) lua_assert((*getlock(l)->plock)++ == 0) #define lua_unlock(l) lua_assert(--(*getlock(l)->plock) == 0) LUA_API int luaB_opentests (lua_State *L); LUA_API void *debug_realloc (void *ud, void *block, size_t osize, size_t nsize); #define luaL_newstate() \ lua_newstate(debug_realloc, &l_memcontrol, luaL_makeseed(NULL)) #define luai_openlibs(L) \ { luaL_openlibs(L); \ luaL_requiref(L, "T", luaB_opentests, 1); \ lua_pop(L, 1); } /* change some sizes to give some bugs a chance */ #undef LUAL_BUFFERSIZE #define LUAL_BUFFERSIZE 23 #define MINSTRTABSIZE 2 #define MAXIWTHABS 3 #define STRCACHE_N 23 #define STRCACHE_M 5 #define MAXINDEXRK 1 /* ** Reduce maximum stack size to make stack-overflow tests run faster. ** (But value is still large enough to overflow smaller integers.) */ #define LUAI_MAXSTACK 68000 /* test mode uses more stack space */ #undef LUAI_MAXCCALLS #define LUAI_MAXCCALLS 180 /* force Lua to use its own implementations */ #undef lua_strx2number #undef lua_number2strx #endif ================================================ FILE: 3rd/lua/ltm.c ================================================ /* ** $Id: ltm.c $ ** Tag methods ** See Copyright Notice in lua.h */ #define ltm_c #define LUA_CORE #include "lprefix.h" #include #include "lua.h" #include "ldebug.h" #include "ldo.h" #include "lgc.h" #include "lobject.h" #include "lstate.h" #include "lstring.h" #include "ltable.h" #include "ltm.h" #include "lvm.h" static const char udatatypename[] = "userdata"; LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = { "no value", "nil", "boolean", udatatypename, "number", "string", "table", "function", udatatypename, "thread", "upvalue", "proto" /* these last cases are used for tests only */ }; void luaT_init (lua_State *L) { static const char *const luaT_eventname[] = { /* ORDER TM */ "__index", "__newindex", "__gc", "__mode", "__len", "__eq", "__add", "__sub", "__mul", "__mod", "__pow", "__div", "__idiv", "__band", "__bor", "__bxor", "__shl", "__shr", "__unm", "__bnot", "__lt", "__le", "__concat", "__call", "__close" }; int i; for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */ } } /* ** function to be used with macro "fasttm": optimized for absence of ** tag methods */ const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { const TValue *tm = luaH_Hgetshortstr(events, ename); lua_assert(event <= TM_EQ); if (notm(tm)) { /* no tag method? */ events->flags |= cast_byte(1u<metatable; break; case LUA_TUSERDATA: mt = uvalue(o)->metatable; break; default: mt = G(L)->mt[ttype(o)]; } return (mt ? luaH_Hgetshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue); } /* ** Return the name of the type of an object. For tables and userdata ** with metatable, use their '__name' metafield, if present. */ const char *luaT_objtypename (lua_State *L, const TValue *o) { Table *mt; if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { const TValue *name = luaH_Hgetshortstr(mt, luaS_new(L, "__name")); if (ttisstring(name)) /* is '__name' a string? */ return getstr(tsvalue(name)); /* use it as type name */ } return ttypename(ttype(o)); /* else use standard type name */ } void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, const TValue *p3) { StkId func = L->top.p; setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ setobj2s(L, func + 1, p1); /* 1st argument */ setobj2s(L, func + 2, p2); /* 2nd argument */ setobj2s(L, func + 3, p3); /* 3rd argument */ L->top.p = func + 4; /* metamethod may yield only when called from Lua code */ if (isLuacode(L->ci)) luaD_call(L, func, 0); else luaD_callnoyield(L, func, 0); } lu_byte luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, StkId res) { ptrdiff_t result = savestack(L, res); StkId func = L->top.p; setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ setobj2s(L, func + 1, p1); /* 1st argument */ setobj2s(L, func + 2, p2); /* 2nd argument */ L->top.p += 3; /* metamethod may yield only when called from Lua code */ if (isLuacode(L->ci)) luaD_call(L, func, 1); else luaD_callnoyield(L, func, 1); res = restorestack(L, result); setobjs2s(L, res, --L->top.p); /* move result to its place */ return ttypetag(s2v(res)); /* return tag of the result */ } static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event) { const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ if (notm(tm)) tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ if (notm(tm)) return -1; /* tag method not found */ else /* call tag method and return the tag of the result */ return luaT_callTMres(L, tm, p1, p2, res); } void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event) { if (l_unlikely(callbinTM(L, p1, p2, res, event) < 0)) { switch (event) { case TM_BAND: case TM_BOR: case TM_BXOR: case TM_SHL: case TM_SHR: case TM_BNOT: { if (ttisnumber(p1) && ttisnumber(p2)) luaG_tointerror(L, p1, p2); else luaG_opinterror(L, p1, p2, "perform bitwise operation on"); } /* calls never return, but to avoid warnings: *//* FALLTHROUGH */ default: luaG_opinterror(L, p1, p2, "perform arithmetic on"); } } } /* ** The use of 'p1' after 'callbinTM' is safe because, when a tag ** method is not found, 'callbinTM' cannot change the stack. */ void luaT_tryconcatTM (lua_State *L) { StkId p1 = L->top.p - 2; /* first argument */ if (l_unlikely(callbinTM(L, s2v(p1), s2v(p1 + 1), p1, TM_CONCAT) < 0)) luaG_concaterror(L, s2v(p1), s2v(p1 + 1)); } void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, int flip, StkId res, TMS event) { if (flip) luaT_trybinTM(L, p2, p1, res, event); else luaT_trybinTM(L, p1, p2, res, event); } void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, int flip, StkId res, TMS event) { TValue aux; setivalue(&aux, i2); luaT_trybinassocTM(L, p1, &aux, flip, res, event); } /* ** Calls an order tag method. */ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event) { int tag = callbinTM(L, p1, p2, L->top.p, event); /* try original event */ if (tag >= 0) /* found tag method? */ return !tagisfalse(tag); luaG_ordererror(L, p1, p2); /* no metamethod found */ return 0; /* to avoid warnings */ } int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, int flip, int isfloat, TMS event) { TValue aux; const TValue *p2; if (isfloat) { setfltvalue(&aux, cast_num(v2)); } else setivalue(&aux, v2); if (flip) { /* arguments were exchanged? */ p2 = p1; p1 = &aux; /* correct them */ } else p2 = &aux; return luaT_callorderTM(L, p1, p2, event); } /* ** Create a vararg table at the top of the stack, with 'n' elements ** starting at 'f'. */ static void createvarargtab (lua_State *L, StkId f, int n) { int i; TValue key, value; Table *t = luaH_new(L); sethvalue(L, s2v(L->top.p), t); L->top.p++; luaH_resize(L, t, cast_uint(n), 1); setsvalue(L, &key, luaS_new(L, "n")); /* key is "n" */ setivalue(&value, n); /* value is n */ /* No need to anchor the key: Due to the resize, the next operation cannot trigger a garbage collection */ luaH_set(L, t, &key, &value); /* t.n = n */ for (i = 0; i < n; i++) luaH_setint(L, t, i + 1, s2v(f + i)); luaC_checkGC(L); } /* ** initial stack: func arg1 ... argn extra1 ... ** ^ ci->func ^ L->top ** final stack: func nil ... nil extra1 ... func arg1 ... argn ** ^ ci->func */ static void buildhiddenargs (lua_State *L, CallInfo *ci, const Proto *p, int totalargs, int nfixparams, int nextra) { int i; ci->u.l.nextraargs = nextra; luaD_checkstack(L, p->maxstacksize + 1); /* copy function to the top of the stack, after extra arguments */ setobjs2s(L, L->top.p++, ci->func.p); /* move fixed parameters to after the copied function */ for (i = 1; i <= nfixparams; i++) { setobjs2s(L, L->top.p++, ci->func.p + i); setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */ } ci->func.p += totalargs + 1; /* 'func' now lives after hidden arguments */ ci->top.p += totalargs + 1; } void luaT_adjustvarargs (lua_State *L, CallInfo *ci, const Proto *p) { int totalargs = cast_int(L->top.p - ci->func.p) - 1; int nfixparams = p->numparams; int nextra = totalargs - nfixparams; /* number of extra arguments */ if (p->flag & PF_VATAB) { /* does it need a vararg table? */ lua_assert(!(p->flag & PF_VAHID)); createvarargtab(L, ci->func.p + nfixparams + 1, nextra); /* move table to proper place (last parameter) */ setobjs2s(L, ci->func.p + nfixparams + 1, L->top.p - 1); } else { /* no table */ lua_assert(p->flag & PF_VAHID); buildhiddenargs(L, ci, p, totalargs, nfixparams, nextra); /* set vararg parameter to nil */ setnilvalue(s2v(ci->func.p + nfixparams + 1)); lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p); } } void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc) { int nextra = ci->u.l.nextraargs; lua_Integer n; if (tointegerns(rc, &n)) { /* integral value? */ if (l_castS2U(n) - 1 < cast_uint(nextra)) { StkId slot = ci->func.p - nextra + cast_int(n) - 1; setobjs2s(((lua_State*)NULL), ra, slot); return; } } else if (ttisstring(rc)) { /* string value? */ size_t len; const char *s = getlstr(tsvalue(rc), len); if (len == 1 && s[0] == 'n') { /* key is "n"? */ setivalue(s2v(ra), nextra); return; } } setnilvalue(s2v(ra)); /* else produce nil */ } /* ** Get the number of extra arguments in a vararg function. If vararg ** table has been optimized away, that number is in the call info. ** Otherwise, get the field 'n' from the vararg table and check that it ** has a proper value (non-negative integer not larger than the stack ** limit). */ static int getnumargs (lua_State *L, CallInfo *ci, Table *h) { if (h == NULL) /* no vararg table? */ return ci->u.l.nextraargs; else { TValue res; if (luaH_getshortstr(h, luaS_new(L, "n"), &res) != LUA_VNUMINT || l_castS2U(ivalue(&res)) > cast_uint(INT_MAX/2)) luaG_runerror(L, "vararg table has no proper 'n'"); return cast_int(ivalue(&res)); } } /* ** Get 'wanted' vararg arguments and put them in 'where'. 'vatab' is ** the register of the vararg table or -1 if there is no vararg table. */ void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted, int vatab) { Table *h = (vatab < 0) ? NULL : hvalue(s2v(ci->func.p + vatab + 1)); int nargs = getnumargs(L, ci, h); /* number of available vararg args. */ int i, touse; /* 'touse' is minimum between 'wanted' and 'nargs' */ if (wanted < 0) { touse = wanted = nargs; /* get all extra arguments available */ checkstackp(L, nargs, where); /* ensure stack space */ L->top.p = where + nargs; /* next instruction will need top */ } else touse = (nargs > wanted) ? wanted : nargs; if (h == NULL) { /* no vararg table? */ for (i = 0; i < touse; i++) /* get vararg values from the stack */ setobjs2s(L, where + i, ci->func.p - nargs + i); } else { /* get vararg values from vararg table */ for (i = 0; i < touse; i++) { lu_byte tag = luaH_getint(h, i + 1, s2v(where + i)); if (tagisempty(tag)) setnilvalue(s2v(where + i)); } } for (; i < wanted; i++) /* complete required results with nil */ setnilvalue(s2v(where + i)); } ================================================ FILE: 3rd/lua/ltm.h ================================================ /* ** $Id: ltm.h $ ** Tag methods ** See Copyright Notice in lua.h */ #ifndef ltm_h #define ltm_h #include "lobject.h" /* * WARNING: if you change the order of this enumeration, * grep "ORDER TM" and "ORDER OP" */ typedef enum { TM_INDEX, TM_NEWINDEX, TM_GC, TM_MODE, TM_LEN, TM_EQ, /* last tag method with fast access */ TM_ADD, TM_SUB, TM_MUL, TM_MOD, TM_POW, TM_DIV, TM_IDIV, TM_BAND, TM_BOR, TM_BXOR, TM_SHL, TM_SHR, TM_UNM, TM_BNOT, TM_LT, TM_LE, TM_CONCAT, TM_CALL, TM_CLOSE, TM_N /* number of elements in the enum */ } TMS; /* ** Mask with 1 in all fast-access methods. A 1 in any of these bits ** in the flag of a (meta)table means the metatable does not have the ** corresponding metamethod field. (Bit 6 of the flag indicates that ** the table is using the dummy node; bit 7 is used for 'isrealasize'.) */ #define maskflags cast_byte(~(~0u << (TM_EQ + 1))) /* ** Test whether there is no tagmethod. ** (Because tagmethods use raw accesses, the result may be an "empty" nil.) */ #define notm(tm) ttisnil(tm) #define checknoTM(mt,e) ((mt) == NULL || (mt)->flags & (1u<<(e))) #define gfasttm(g,mt,e) \ (checknoTM(mt, e) ? NULL : luaT_gettm(mt, e, (g)->tmname[e])) #define fasttm(l,mt,e) gfasttm(G(l), mt, e) #define ttypename(x) luaT_typenames_[(x) + 1] LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];) LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event); LUAI_FUNC void luaT_init (lua_State *L); LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, const TValue *p3); LUAI_FUNC lu_byte luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, StkId p3); LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event); LUAI_FUNC void luaT_tryconcatTM (lua_State *L); LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, int inv, StkId res, TMS event); LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, int inv, StkId res, TMS event); LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event); LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, int inv, int isfloat, TMS event); LUAI_FUNC void luaT_adjustvarargs (lua_State *L, struct CallInfo *ci, const Proto *p); LUAI_FUNC void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc); LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, StkId where, int wanted, int vatab); #endif ================================================ FILE: 3rd/lua/lua.c ================================================ /* ** $Id: lua.c $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ #define lua_c #include "lprefix.h" #include #include #include #include #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "llimits.h" #if !defined(LUA_PROGNAME) #define LUA_PROGNAME "lua" #endif #if !defined(LUA_INIT_VAR) #define LUA_INIT_VAR "LUA_INIT" #endif #define LUA_INITVARVERSION LUA_INIT_VAR LUA_VERSUFFIX static lua_State *globalL = NULL; static const char *progname = LUA_PROGNAME; #if defined(LUA_USE_POSIX) /* { */ /* ** Use 'sigaction' when available. */ static void setsignal (int sig, void (*handler)(int)) { struct sigaction sa; sa.sa_handler = handler; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); /* do not mask any signal */ sigaction(sig, &sa, NULL); } #else /* }{ */ #define setsignal signal #endif /* } */ /* ** Hook set by signal function to stop the interpreter. */ static void lstop (lua_State *L, lua_Debug *ar) { (void)ar; /* unused arg. */ lua_sethook(L, NULL, 0, 0); /* reset hook */ luaL_error(L, "interrupted!"); } /* ** Function to be called at a C signal. Because a C signal cannot ** just change a Lua state (as there is no proper synchronization), ** this function only sets a hook that, when called, will stop the ** interpreter. */ static void laction (int i) { int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT; setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ lua_sethook(globalL, lstop, flag, 1); } static void print_usage (const char *badoption) { lua_writestringerror("%s: ", progname); if (badoption[1] == 'e' || badoption[1] == 'l') lua_writestringerror("'%s' needs argument\n", badoption); else lua_writestringerror("unrecognized option '%s'\n", badoption); lua_writestringerror( "usage: %s [options] [script [args]]\n" "Available options are:\n" " -e stat execute string 'stat'\n" " -i enter interactive mode after executing 'script'\n" " -l mod require library 'mod' into global 'mod'\n" " -l g=mod require library 'mod' into global 'g'\n" " -v show version information\n" " -E ignore environment variables\n" " -W turn warnings on\n" " -- stop handling options\n" " - stop handling options and execute stdin\n" , progname); } /* ** Prints an error message, adding the program name in front of it ** (if present) */ static void l_message (const char *pname, const char *msg) { if (pname) lua_writestringerror("%s: ", pname); lua_writestringerror("%s\n", msg); } /* ** Check whether 'status' is not OK and, if so, prints the error ** message on the top of the stack. */ static int report (lua_State *L, int status) { if (status != LUA_OK) { const char *msg = lua_tostring(L, -1); if (msg == NULL) msg = "(error message not a string)"; l_message(progname, msg); lua_pop(L, 1); /* remove message */ } return status; } /* ** Message handler used to run all chunks */ static int msghandler (lua_State *L) { const char *msg = lua_tostring(L, 1); if (msg == NULL) { /* is error object not a string? */ if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */ lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */ return 1; /* that is the message */ else msg = lua_pushfstring(L, "(error object is a %s value)", luaL_typename(L, 1)); } luaL_traceback(L, L, msg, 1); /* append a standard traceback */ return 1; /* return the traceback */ } /* ** Interface to 'lua_pcall', which sets appropriate message function ** and C-signal handler. Used to run all chunks. */ static int docall (lua_State *L, int narg, int nres) { int status; int base = lua_gettop(L) - narg; /* function index */ lua_pushcfunction(L, msghandler); /* push message handler */ lua_insert(L, base); /* put it under function and args */ globalL = L; /* to be available to 'laction' */ setsignal(SIGINT, laction); /* set C-signal handler */ status = lua_pcall(L, narg, nres, base); setsignal(SIGINT, SIG_DFL); /* reset C-signal handler */ lua_remove(L, base); /* remove message handler from the stack */ return status; } static void print_version (void) { lua_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT)); lua_writeline(); } /* ** Create the 'arg' table, which stores all arguments from the ** command line ('argv'). It should be aligned so that, at index 0, ** it has 'argv[script]', which is the script name. The arguments ** to the script (everything after 'script') go to positive indices; ** other arguments (before the script name) go to negative indices. ** If there is no script name, assume interpreter's name as base. ** (If there is no interpreter's name either, 'script' is -1, so ** table sizes are zero.) */ static void createargtable (lua_State *L, char **argv, int argc, int script) { int i, narg; narg = argc - (script + 1); /* number of positive indices */ lua_createtable(L, narg, script + 1); for (i = 0; i < argc; i++) { lua_pushstring(L, argv[i]); lua_rawseti(L, -2, i - script); } lua_setglobal(L, "arg"); } static int dochunk (lua_State *L, int status) { if (status == LUA_OK) status = docall(L, 0, 0); return report(L, status); } static int dofile (lua_State *L, const char *name) { return dochunk(L, luaL_loadfile(L, name)); } static int dostring (lua_State *L, const char *s, const char *name) { return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name)); } /* ** Receives 'globname[=modname]' and runs 'globname = require(modname)'. ** If there is no explicit modname and globname contains a '-', cut ** the suffix after '-' (the "version") to make the global name. */ static int dolibrary (lua_State *L, char *globname) { int status; char *suffix = NULL; char *modname = strchr(globname, '='); if (modname == NULL) { /* no explicit name? */ modname = globname; /* module name is equal to global name */ suffix = strchr(modname, *LUA_IGMARK); /* look for a suffix mark */ } else { *modname = '\0'; /* global name ends here */ modname++; /* module name starts after the '=' */ } lua_getglobal(L, "require"); lua_pushstring(L, modname); status = docall(L, 1, 1); /* call 'require(modname)' */ if (status == LUA_OK) { if (suffix != NULL) /* is there a suffix mark? */ *suffix = '\0'; /* remove suffix from global name */ lua_setglobal(L, globname); /* globname = require(modname) */ } return report(L, status); } /* ** Push on the stack the contents of table 'arg' from 1 to #arg */ static int pushargs (lua_State *L) { int i, n; if (lua_getglobal(L, "arg") != LUA_TTABLE) luaL_error(L, "'arg' is not a table"); n = (int)luaL_len(L, -1); luaL_checkstack(L, n + 3, "too many arguments to script"); for (i = 1; i <= n; i++) lua_rawgeti(L, -i, i); lua_remove(L, -i); /* remove table from the stack */ return n; } static int handle_script (lua_State *L, char **argv) { int status; const char *fname = argv[0]; if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) fname = NULL; /* stdin */ status = luaL_loadfile(L, fname); if (status == LUA_OK) { int n = pushargs(L); /* push arguments to script */ status = docall(L, n, LUA_MULTRET); } return report(L, status); } /* bits of various argument indicators in 'args' */ #define has_error 1 /* bad option */ #define has_i 2 /* -i */ #define has_v 4 /* -v */ #define has_e 8 /* -e */ #define has_E 16 /* -E */ /* ** Traverses all arguments from 'argv', returning a mask with those ** needed before running any Lua code or an error code if it finds any ** invalid argument. In case of error, 'first' is the index of the bad ** argument. Otherwise, 'first' is -1 if there is no program name, ** 0 if there is no script name, or the index of the script name. */ static int collectargs (char **argv, int *first) { int args = 0; int i; if (argv[0] != NULL) { /* is there a program name? */ if (argv[0][0]) /* not empty? */ progname = argv[0]; /* save it */ } else { /* no program name */ *first = -1; return 0; } for (i = 1; argv[i] != NULL; i++) { /* handle arguments */ *first = i; if (argv[i][0] != '-') /* not an option? */ return args; /* stop handling options */ switch (argv[i][1]) { /* else check option */ case '-': /* '--' */ if (argv[i][2] != '\0') /* extra characters after '--'? */ return has_error; /* invalid option */ /* if there is a script name, it comes after '--' */ *first = (argv[i + 1] != NULL) ? i + 1 : 0; return args; case '\0': /* '-' */ return args; /* script "name" is '-' */ case 'E': if (argv[i][2] != '\0') /* extra characters? */ return has_error; /* invalid option */ args |= has_E; break; case 'W': if (argv[i][2] != '\0') /* extra characters? */ return has_error; /* invalid option */ break; case 'i': args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ case 'v': if (argv[i][2] != '\0') /* extra characters? */ return has_error; /* invalid option */ args |= has_v; break; case 'e': args |= has_e; /* FALLTHROUGH */ case 'l': /* both options need an argument */ if (argv[i][2] == '\0') { /* no concatenated argument? */ i++; /* try next 'argv' */ if (argv[i] == NULL || argv[i][0] == '-') return has_error; /* no next argument or it is another option */ } break; default: /* invalid option */ return has_error; } } *first = 0; /* no script name */ return args; } /* ** Processes options 'e' and 'l', which involve running Lua code, and ** 'W', which also affects the state. ** Returns 0 if some code raises an error. */ static int runargs (lua_State *L, char **argv, int n) { int i; lua_warning(L, "@off", 0); /* by default, Lua stand-alone has warnings off */ for (i = 1; i < n; i++) { int option = argv[i][1]; lua_assert(argv[i][0] == '-'); /* already checked */ switch (option) { case 'e': case 'l': { int status; char *extra = argv[i] + 2; /* both options need an argument */ if (*extra == '\0') extra = argv[++i]; lua_assert(extra != NULL); status = (option == 'e') ? dostring(L, extra, "=(command line)") : dolibrary(L, extra); if (status != LUA_OK) return 0; break; } case 'W': lua_warning(L, "@on", 0); /* warnings on */ break; } } return 1; } static int handle_luainit (lua_State *L) { const char *name = "=" LUA_INITVARVERSION; const char *init = getenv(name + 1); if (init == NULL) { name = "=" LUA_INIT_VAR; init = getenv(name + 1); /* try alternative name */ } if (init == NULL) return LUA_OK; else if (init[0] == '@') return dofile(L, init+1); else return dostring(L, init, name); } /* ** {================================================================== ** Read-Eval-Print Loop (REPL) ** =================================================================== */ #if !defined(LUA_PROMPT) #define LUA_PROMPT "> " #define LUA_PROMPT2 ">> " #endif #if !defined(LUA_MAXINPUT) #define LUA_MAXINPUT 512 #endif /* ** lua_stdin_is_tty detects whether the standard input is a 'tty' (that ** is, whether we're running lua interactively). */ #if !defined(lua_stdin_is_tty) /* { */ #if defined(LUA_USE_POSIX) /* { */ #include #define lua_stdin_is_tty() isatty(0) #elif defined(LUA_USE_WINDOWS) /* }{ */ #include #include #define lua_stdin_is_tty() _isatty(_fileno(stdin)) #else /* }{ */ /* ISO C definition */ #define lua_stdin_is_tty() 1 /* assume stdin is a tty */ #endif /* } */ #endif /* } */ /* ** * lua_initreadline initializes the readline system. ** * lua_readline defines how to show a prompt and then read a line from ** the standard input. ** * lua_saveline defines how to "save" a read line in a "history". ** * lua_freeline defines how to free a line read by lua_readline. */ #if !defined(lua_readline) /* { */ /* Otherwise, all previously listed functions should be defined. */ #if defined(LUA_USE_READLINE) /* { */ /* Lua will be linked with '-lreadline' */ #include #include #define lua_initreadline(L) ((void)L, rl_readline_name="lua") #define lua_readline(buff,prompt) ((void)buff, readline(prompt)) #define lua_saveline(line) add_history(line) #define lua_freeline(line) free(line) #else /* }{ */ /* use dynamically loaded readline (or nothing) */ /* pointer to 'readline' function (if any) */ typedef char *(*l_readlineT) (const char *prompt); static l_readlineT l_readline = NULL; /* pointer to 'add_history' function (if any) */ typedef void (*l_addhistT) (const char *string); static l_addhistT l_addhist = NULL; static char *lua_readline (char *buff, const char *prompt) { if (l_readline != NULL) /* is there a 'readline'? */ return (*l_readline)(prompt); /* use it */ else { /* emulate 'readline' over 'buff' */ fputs(prompt, stdout); fflush(stdout); /* show prompt */ return fgets(buff, LUA_MAXINPUT, stdin); /* read line */ } } static void lua_saveline (const char *line) { if (l_addhist != NULL) /* is there an 'add_history'? */ (*l_addhist)(line); /* use it */ /* else nothing to be done */ } static void lua_freeline (char *line) { if (l_readline != NULL) /* is there a 'readline'? */ free(line); /* free line created by it */ /* else 'lua_readline' used an automatic buffer; nothing to free */ } #if defined(LUA_USE_DLOPEN) && defined(LUA_READLINELIB) /* { */ /* try to load 'readline' dynamically */ #include static void lua_initreadline (lua_State *L) { void *lib = dlopen(LUA_READLINELIB, RTLD_NOW | RTLD_LOCAL); if (lib == NULL) lua_warning(L, "library '" LUA_READLINELIB "' not found", 0); else { const char **name = cast(const char**, dlsym(lib, "rl_readline_name")); if (name != NULL) *name = "lua"; l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline"))); l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history"))); if (l_readline == NULL) lua_warning(L, "unable to load 'readline'", 0); } } #else /* }{ */ /* no dlopen or LUA_READLINELIB undefined */ /* Leave pointers with NULL */ #define lua_initreadline(L) ((void)L) #endif /* } */ #endif /* } */ #endif /* } */ /* ** Return the string to be used as a prompt by the interpreter. Leave ** the string (or nil, if using the default value) on the stack, to keep ** it anchored. */ static const char *get_prompt (lua_State *L, int firstline) { if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL) return (firstline ? LUA_PROMPT : LUA_PROMPT2); /* use the default */ else { /* apply 'tostring' over the value */ const char *p = luaL_tolstring(L, -1, NULL); lua_remove(L, -2); /* remove original value */ return p; } } /* mark in error messages for incomplete statements */ #define EOFMARK "" #define marklen (sizeof(EOFMARK)/sizeof(char) - 1) /* ** Check whether 'status' signals a syntax error and the error ** message at the top of the stack ends with the above mark for ** incomplete statements. */ static int incomplete (lua_State *L, int status) { if (status == LUA_ERRSYNTAX) { size_t lmsg; const char *msg = lua_tolstring(L, -1, &lmsg); if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) return 1; } return 0; /* else... */ } /* ** Prompt the user, read a line, and push it into the Lua stack. */ static int pushline (lua_State *L, int firstline) { char buffer[LUA_MAXINPUT]; size_t l; const char *prmt = get_prompt(L, firstline); char *b = lua_readline(buffer, prmt); lua_pop(L, 1); /* remove prompt */ if (b == NULL) return 0; /* no input */ l = strlen(b); if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ b[--l] = '\0'; /* remove it */ lua_pushlstring(L, b, l); lua_freeline(b); return 1; } /* ** Try to compile line on the stack as 'return ;'; on return, stack ** has either compiled chunk or original line (if compilation failed). */ static int addreturn (lua_State *L) { const char *line = lua_tostring(L, -1); /* original line */ const char *retline = lua_pushfstring(L, "return %s;", line); int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin"); if (status == LUA_OK) lua_remove(L, -2); /* remove modified line */ else lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */ return status; } static void checklocal (const char *line) { static const size_t szloc = sizeof("local") - 1; static const char space[] = " \t"; line += strspn(line, space); /* skip spaces */ if (strncmp(line, "local", szloc) == 0 && /* "local"? */ strchr(space, *(line + szloc)) != NULL) { /* followed by a space? */ lua_writestringerror("%s\n", "warning: locals do not survive across lines in interactive mode"); } } /* ** Read multiple lines until a complete Lua statement or an error not ** for an incomplete statement. Start with first line already read in ** the stack. */ static int multiline (lua_State *L) { size_t len; const char *line = lua_tolstring(L, 1, &len); /* get first line */ checklocal(line); for (;;) { /* repeat until gets a complete statement */ int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ if (!incomplete(L, status) || !pushline(L, 0)) return status; /* should not or cannot try to add continuation line */ lua_remove(L, -2); /* remove error message (from incomplete line) */ lua_pushliteral(L, "\n"); /* add newline... */ lua_insert(L, -2); /* ...between the two lines */ lua_concat(L, 3); /* join them */ line = lua_tolstring(L, 1, &len); /* get what is has */ } } /* ** Read a line and try to load (compile) it first as an expression (by ** adding "return " in front of it) and second as a statement. Return ** the final status of load/call with the resulting function (if any) ** in the top of the stack. */ static int loadline (lua_State *L) { const char *line; int status; lua_settop(L, 0); if (!pushline(L, 1)) return -1; /* no input */ if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ status = multiline(L); /* try as command, maybe with continuation lines */ line = lua_tostring(L, 1); if (line[0] != '\0') /* non empty? */ lua_saveline(line); /* keep history */ lua_remove(L, 1); /* remove line from the stack */ lua_assert(lua_gettop(L) == 1); return status; } /* ** Prints (calling the Lua 'print' function) any values on the stack */ static void l_print (lua_State *L) { int n = lua_gettop(L); if (n > 0) { /* any result to be printed? */ luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); lua_getglobal(L, "print"); lua_insert(L, 1); if (lua_pcall(L, n, 0, 0) != LUA_OK) l_message(progname, lua_pushfstring(L, "error calling 'print' (%s)", lua_tostring(L, -1))); } } /* ** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and ** print any results. */ static void doREPL (lua_State *L) { int status; const char *oldprogname = progname; progname = NULL; /* no 'progname' on errors in interactive mode */ lua_initreadline(L); while ((status = loadline(L)) != -1) { if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET); if (status == LUA_OK) l_print(L); else report(L, status); } lua_settop(L, 0); /* clear stack */ lua_writeline(); progname = oldprogname; } /* }================================================================== */ #if !defined(luai_openlibs) #define luai_openlibs(L) luaL_openselectedlibs(L, ~0, 0) #endif /* ** Main body of stand-alone interpreter (to be called in protected mode). ** Reads the options and handles them all. */ static int pmain (lua_State *L) { int argc = (int)lua_tointeger(L, 1); char **argv = (char **)lua_touserdata(L, 2); int script; int args = collectargs(argv, &script); int optlim = (script > 0) ? script : argc; /* first argv not an option */ luaL_checkversion(L); /* check that interpreter has correct version */ if (args == has_error) { /* bad arg? */ print_usage(argv[script]); /* 'script' has index of bad arg. */ return 0; } if (args & has_v) /* option '-v'? */ print_version(); if (args & has_E) { /* option '-E'? */ lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); } luai_openlibs(L); /* open standard libraries */ createargtable(L, argv, argc, script); /* create table 'arg' */ lua_gc(L, LUA_GCRESTART); /* start GC... */ lua_gc(L, LUA_GCGEN); /* ...in generational mode */ if (!(args & has_E)) { /* no option '-E'? */ if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ return 0; /* error running LUA_INIT */ } if (!runargs(L, argv, optlim)) /* execute arguments -e, -l, and -W */ return 0; /* something failed */ if (script > 0) { /* execute main script (if there is one) */ if (handle_script(L, argv + script) != LUA_OK) return 0; /* interrupt in case of error */ } if (args & has_i) /* -i option? */ doREPL(L); /* do read-eval-print loop */ else if (script < 1 && !(args & (has_e | has_v))) { /* no active option? */ if (lua_stdin_is_tty()) { /* running in interactive mode? */ print_version(); doREPL(L); /* do read-eval-print loop */ } else dofile(L, NULL); /* executes stdin as a file */ } lua_pushboolean(L, 1); /* signal no errors */ return 1; } int main (int argc, char **argv) { int status, result; lua_State *L = luaL_newstate(); /* create state */ if (L == NULL) { l_message(argv[0], "cannot create state: not enough memory"); return EXIT_FAILURE; } lua_gc(L, LUA_GCSTOP); /* stop GC while building state */ lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ lua_pushinteger(L, argc); /* 1st argument */ lua_pushlightuserdata(L, argv); /* 2nd argument */ status = lua_pcall(L, 2, 1, 0); /* do the call */ result = lua_toboolean(L, -1); /* get result */ report(L, status); lua_close(L); return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; } ================================================ FILE: 3rd/lua/lua.h ================================================ /* ** $Id: lua.h $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (www.lua.org) ** See Copyright Notice at the end of this file */ #ifndef lua_h #define lua_h #include #include #define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2025 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" #define LUA_VERSION_MAJOR_N 5 #define LUA_VERSION_MINOR_N 5 #define LUA_VERSION_RELEASE_N 0 #define LUA_VERSION_NUM (LUA_VERSION_MAJOR_N * 100 + LUA_VERSION_MINOR_N) #define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + LUA_VERSION_RELEASE_N) #include "luaconf.h" /* mark for precompiled code ('Lua') */ #define LUA_SIGNATURE "\x1bLua" /* option for multiple returns in 'lua_pcall' and 'lua_call' */ #define LUA_MULTRET (-1) /* ** Pseudo-indices ** (The stack size is limited to INT_MAX/2; we keep some free empty ** space after that to help overflow detection.) */ #define LUA_REGISTRYINDEX (-(INT_MAX/2 + 1000)) #define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) /* thread status */ #define LUA_OK 0 #define LUA_YIELD 1 #define LUA_ERRRUN 2 #define LUA_ERRSYNTAX 3 #define LUA_ERRMEM 4 #define LUA_ERRERR 5 typedef struct lua_State lua_State; /* ** basic types */ #define LUA_TNONE (-1) #define LUA_TNIL 0 #define LUA_TBOOLEAN 1 #define LUA_TLIGHTUSERDATA 2 #define LUA_TNUMBER 3 #define LUA_TSTRING 4 #define LUA_TTABLE 5 #define LUA_TFUNCTION 6 #define LUA_TUSERDATA 7 #define LUA_TTHREAD 8 #define LUA_NUMTYPES 9 /* minimum Lua stack available to a C function */ #define LUA_MINSTACK 20 /* predefined values in the registry */ /* index 1 is reserved for the reference mechanism */ #define LUA_RIDX_GLOBALS 2 #define LUA_RIDX_MAINTHREAD 3 #define LUA_RIDX_LAST 3 /* type of numbers in Lua */ typedef LUA_NUMBER lua_Number; /* type for integer functions */ typedef LUA_INTEGER lua_Integer; /* unsigned integer type */ typedef LUA_UNSIGNED lua_Unsigned; /* type for continuation-function contexts */ typedef LUA_KCONTEXT lua_KContext; /* ** Type for C functions registered with Lua */ typedef int (*lua_CFunction) (lua_State *L); /* ** Type for continuation functions */ typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx); /* ** Type for functions that read/write blocks when loading/dumping Lua chunks */ typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud); /* ** Type for memory-allocation functions */ typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); /* ** Type for warning functions */ typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont); /* ** Type used by the debug API to collect debug information */ typedef struct lua_Debug lua_Debug; /* ** Functions to be called by the debugger in specific events */ typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); /* ** generic extra include file */ #if defined(LUA_USER_H) #include LUA_USER_H #endif /* ** RCS ident string */ extern const char lua_ident[]; /* ** state manipulation */ LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud, unsigned seed); LUA_API void (lua_close) (lua_State *L); LUA_API lua_State *(lua_newthread) (lua_State *L); LUA_API int (lua_closethread) (lua_State *L, lua_State *from); LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); LUA_API lua_Number (lua_version) (lua_State *L); /* ** basic stack manipulation */ LUA_API int (lua_absindex) (lua_State *L, int idx); LUA_API int (lua_gettop) (lua_State *L); LUA_API void (lua_settop) (lua_State *L, int idx); LUA_API void (lua_pushvalue) (lua_State *L, int idx); LUA_API void (lua_rotate) (lua_State *L, int idx, int n); LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); LUA_API int (lua_checkstack) (lua_State *L, int n); LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); /* ** access functions (stack -> C) */ LUA_API int (lua_isnumber) (lua_State *L, int idx); LUA_API int (lua_isstring) (lua_State *L, int idx); LUA_API int (lua_iscfunction) (lua_State *L, int idx); LUA_API int (lua_isinteger) (lua_State *L, int idx); LUA_API int (lua_isuserdata) (lua_State *L, int idx); LUA_API int (lua_type) (lua_State *L, int idx); LUA_API const char *(lua_typename) (lua_State *L, int tp); LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum); LUA_API int (lua_toboolean) (lua_State *L, int idx); LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); LUA_API lua_Unsigned (lua_rawlen) (lua_State *L, int idx); LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); LUA_API void *(lua_touserdata) (lua_State *L, int idx); LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); LUA_API const void *(lua_topointer) (lua_State *L, int idx); /* ** Comparison and arithmetic functions */ #define LUA_OPADD 0 /* ORDER TM, ORDER OP */ #define LUA_OPSUB 1 #define LUA_OPMUL 2 #define LUA_OPMOD 3 #define LUA_OPPOW 4 #define LUA_OPDIV 5 #define LUA_OPIDIV 6 #define LUA_OPBAND 7 #define LUA_OPBOR 8 #define LUA_OPBXOR 9 #define LUA_OPSHL 10 #define LUA_OPSHR 11 #define LUA_OPUNM 12 #define LUA_OPBNOT 13 LUA_API void (lua_arith) (lua_State *L, int op); #define LUA_OPEQ 0 #define LUA_OPLT 1 #define LUA_OPLE 2 LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op); /* ** push functions (C -> stack) */ LUA_API void (lua_pushnil) (lua_State *L); LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len); LUA_API const char *(lua_pushexternalstring) (lua_State *L, const char *s, size_t len, lua_Alloc falloc, void *ud); LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, va_list argp); LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); LUA_API void (lua_pushboolean) (lua_State *L, int b); LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); LUA_API int (lua_pushthread) (lua_State *L); LUA_API void (lua_clonefunction) (lua_State *L, const void * fp); LUA_API void (lua_sharefunction) (lua_State *L, int index); LUA_API void (lua_sharestring) (lua_State *L, int index); LUA_API void (lua_clonetable) (lua_State *L, const void * t); /* ** get functions (Lua -> stack) */ LUA_API int (lua_getglobal) (lua_State *L, const char *name); LUA_API int (lua_gettable) (lua_State *L, int idx); LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k); LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n); LUA_API int (lua_rawget) (lua_State *L, int idx); LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n); LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p); LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); LUA_API void *(lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue); LUA_API int (lua_getmetatable) (lua_State *L, int objindex); LUA_API int (lua_getiuservalue) (lua_State *L, int idx, int n); /* ** set functions (stack -> Lua) */ LUA_API void (lua_setglobal) (lua_State *L, const char *name); LUA_API void (lua_settable) (lua_State *L, int idx); LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n); LUA_API void (lua_rawset) (lua_State *L, int idx); LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n); LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p); LUA_API int (lua_setmetatable) (lua_State *L, int objindex); LUA_API int (lua_setiuservalue) (lua_State *L, int idx, int n); /* ** 'load' and 'call' functions (load and run Lua code) */ LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, lua_KContext ctx, lua_KFunction k); #define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, lua_KContext ctx, lua_KFunction k); #define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, const char *chunkname, const char *mode); LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); /* ** coroutine functions */ LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx, lua_KFunction k); LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg, int *nres); LUA_API int (lua_status) (lua_State *L); LUA_API int (lua_isyieldable) (lua_State *L); #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) /* ** Warning-related functions */ LUA_API void (lua_setwarnf) (lua_State *L, lua_WarnFunction f, void *ud); LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont); /* ** garbage-collection options */ #define LUA_GCSTOP 0 #define LUA_GCRESTART 1 #define LUA_GCCOLLECT 2 #define LUA_GCCOUNT 3 #define LUA_GCCOUNTB 4 #define LUA_GCSTEP 5 #define LUA_GCISRUNNING 6 #define LUA_GCGEN 7 #define LUA_GCINC 8 #define LUA_GCPARAM 9 /* ** garbage-collection parameters */ /* parameters for generational mode */ #define LUA_GCPMINORMUL 0 /* control minor collections */ #define LUA_GCPMAJORMINOR 1 /* control shift major->minor */ #define LUA_GCPMINORMAJOR 2 /* control shift minor->major */ /* parameters for incremental mode */ #define LUA_GCPPAUSE 3 /* size of pause between successive GCs */ #define LUA_GCPSTEPMUL 4 /* GC "speed" */ #define LUA_GCPSTEPSIZE 5 /* GC granularity */ /* number of parameters */ #define LUA_GCPN 6 LUA_API int (lua_gc) (lua_State *L, int what, ...); /* ** miscellaneous functions */ LUA_API int (lua_error) (lua_State *L); LUA_API int (lua_next) (lua_State *L, int idx); LUA_API void (lua_concat) (lua_State *L, int n); LUA_API void (lua_len) (lua_State *L, int idx); #define LUA_N2SBUFFSZ 64 LUA_API unsigned (lua_numbertocstring) (lua_State *L, int idx, char *buff); LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); LUA_API void (lua_toclose) (lua_State *L, int idx); LUA_API void (lua_closeslot) (lua_State *L, int idx); /* ** {============================================================== ** some useful macros ** =============================================================== */ #define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE)) #define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL) #define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL) #define lua_pop(L,n) lua_settop(L, -(n)-1) #define lua_newtable(L) lua_createtable(L, 0, 0) #define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) #define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) #define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) #define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) #define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) #define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) #define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) #define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) #define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) #define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) #define lua_pushliteral(L, s) lua_pushstring(L, "" s) #define lua_pushglobaltable(L) \ ((void)lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)) #define lua_tostring(L,i) lua_tolstring(L, (i), NULL) #define lua_insert(L,idx) lua_rotate(L, (idx), 1) #define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1)) #define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1)) /* }============================================================== */ /* ** {============================================================== ** compatibility macros ** =============================================================== */ #define lua_newuserdata(L,s) lua_newuserdatauv(L,s,1) #define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1) #define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1) #define lua_resetthread(L) lua_closethread(L,NULL) /* }============================================================== */ /* ** {====================================================================== ** Debug API ** ======================================================================= */ /* ** Event codes */ #define LUA_HOOKCALL 0 #define LUA_HOOKRET 1 #define LUA_HOOKLINE 2 #define LUA_HOOKCOUNT 3 #define LUA_HOOKTAILCALL 4 /* ** Event masks */ #define LUA_MASKCALL (1 << LUA_HOOKCALL) #define LUA_MASKRET (1 << LUA_HOOKRET) #define LUA_MASKLINE (1 << LUA_HOOKLINE) #define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar); LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar); LUA_API const char *(lua_getlocal) (lua_State *L, const lua_Debug *ar, int n); LUA_API const char *(lua_setlocal) (lua_State *L, const lua_Debug *ar, int n); LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n); LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n); LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n); LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1, int fidx2, int n2); LUA_API void (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count); LUA_API lua_Hook (lua_gethook) (lua_State *L); LUA_API int (lua_gethookmask) (lua_State *L); LUA_API int (lua_gethookcount) (lua_State *L); struct lua_Debug { int event; const char *name; /* (n) */ const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */ const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */ const char *source; /* (S) */ size_t srclen; /* (S) */ int currentline; /* (l) */ int linedefined; /* (S) */ int lastlinedefined; /* (S) */ unsigned char nups; /* (u) number of upvalues */ unsigned char nparams;/* (u) number of parameters */ char isvararg; /* (u) */ unsigned char extraargs; /* (t) number of extra arguments */ char istailcall; /* (t) */ int ftransfer; /* (r) index of first value transferred */ int ntransfer; /* (r) number of transferred values */ char short_src[LUA_IDSIZE]; /* (S) */ /* private part */ struct CallInfo *i_ci; /* active function */ }; /* }====================================================================== */ #define LUAI_TOSTRAUX(x) #x #define LUAI_TOSTR(x) LUAI_TOSTRAUX(x) #define LUA_VERSION_MAJOR LUAI_TOSTR(LUA_VERSION_MAJOR_N) #define LUA_VERSION_MINOR LUAI_TOSTR(LUA_VERSION_MINOR_N) #define LUA_VERSION_RELEASE LUAI_TOSTR(LUA_VERSION_RELEASE_N) #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE /****************************************************************************** * Copyright (C) 1994-2025 Lua.org, PUC-Rio. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ #endif ================================================ FILE: 3rd/lua/lua.hpp ================================================ // lua.hpp // Lua header files for C++ // 'extern "C" not supplied automatically in lua.h and other headers // because Lua also compiles as C++ extern "C" { #include "lua.h" #include "lualib.h" #include "lauxlib.h" } ================================================ FILE: 3rd/lua/luac.c ================================================ /* ** $Id: luac.c $ ** Lua compiler (saves bytecodes to files; also lists bytecodes) ** See Copyright Notice in lua.h */ #define luac_c #define LUA_CORE #include "lprefix.h" #include #include #include #include #include #include "lua.h" #include "lauxlib.h" #include "lapi.h" #include "ldebug.h" #include "lobject.h" #include "lopcodes.h" #include "lopnames.h" #include "lstate.h" #include "lundump.h" static void PrintFunction(const Proto* f, int full); #define luaU_print PrintFunction #define PROGNAME "luac" /* default program name */ #define OUTPUT PROGNAME ".out" /* default output file */ static int listing=0; /* list bytecodes? */ static int dumping=1; /* dump bytecodes? */ static int stripping=0; /* strip debug information? */ static char Output[]={ OUTPUT }; /* default output file name */ static const char* output=Output; /* actual output file name */ static const char* progname=PROGNAME; /* actual program name */ static TString **tmname; static void fatal(const char* message) { fprintf(stderr,"%s: %s\n",progname,message); exit(EXIT_FAILURE); } static void cannot(const char* what) { fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno)); exit(EXIT_FAILURE); } static void usage(const char* message) { if (*message=='-') fprintf(stderr,"%s: unrecognized option '%s'\n",progname,message); else fprintf(stderr,"%s: %s\n",progname,message); fprintf(stderr, "usage: %s [options] [filenames]\n" "Available options are:\n" " -l list (use -l -l for full listing)\n" " -o name output to file 'name' (default is \"%s\")\n" " -p parse only\n" " -s strip debug information\n" " -v show version information\n" " -- stop handling options\n" " - stop handling options and process stdin\n" ,progname,Output); exit(EXIT_FAILURE); } #define IS(s) (strcmp(argv[i],s)==0) static int doargs(int argc, char* argv[]) { int i; int version=0; if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0]; for (i=1; itop.p+(i))) static const Proto* combine(lua_State* L, int n) { if (n==1) return toproto(L,-1); else { Proto* f; int i=n; if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1)); f=toproto(L,-1); for (i=0; ip[i]=toproto(L,i-n-1); if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0; } return f; } } static int writer(lua_State* L, const void* p, size_t size, void* u) { UNUSED(L); return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0); } static int pmain(lua_State* L) { int argc=(int)lua_tointeger(L,1); char** argv=(char**)lua_touserdata(L,2); const Proto* f; int i; tmname=G(L)->tmname; if (!lua_checkstack(L,argc)) fatal("too many input files"); for (i=0; i1); if (dumping) { FILE* D= (output==NULL) ? stdout : fopen(output,"wb"); if (D==NULL) cannot("open"); lua_lock(L); luaU_dump(L,f,writer,D,stripping); lua_unlock(L); if (ferror(D)) cannot("write"); if (fclose(D)) cannot("close"); } return 0; } int main(int argc, char* argv[]) { lua_State* L; int i=doargs(argc,argv); argc-=i; argv+=i; if (argc<=0) usage("no input files given"); L=luaL_newstate(); if (L==NULL) fatal("cannot create state: not enough memory"); lua_pushcfunction(L,&pmain); lua_pushinteger(L,argc); lua_pushlightuserdata(L,argv); if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1)); lua_close(L); return EXIT_SUCCESS; } /* ** print bytecodes */ #define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") #define VOID(p) ((const void*)(p)) #define eventname(i) (getstr(tmname[i])) static void PrintString(const TString* ts) { const char* s=getstr(ts); size_t i,n=tsslen(ts); printf("\""); for (i=0; ik[i]; switch (ttypetag(o)) { case LUA_VNIL: printf("N"); break; case LUA_VFALSE: case LUA_VTRUE: printf("B"); break; case LUA_VNUMFLT: printf("F"); break; case LUA_VNUMINT: printf("I"); break; case LUA_VSHRSTR: case LUA_VLNGSTR: printf("S"); break; default: /* cannot happen */ printf("?%d",ttypetag(o)); break; } printf("\t"); } static void PrintConstant(const Proto* f, int i) { const TValue* o=&f->k[i]; switch (ttypetag(o)) { case LUA_VNIL: printf("nil"); break; case LUA_VFALSE: printf("false"); break; case LUA_VTRUE: printf("true"); break; case LUA_VNUMFLT: { char buff[100]; sprintf(buff,LUA_NUMBER_FMT,fltvalue(o)); printf("%s",buff); if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0"); break; } case LUA_VNUMINT: printf(LUA_INTEGER_FMT,ivalue(o)); break; case LUA_VSHRSTR: case LUA_VLNGSTR: PrintString(tsvalue(o)); break; default: /* cannot happen */ printf("?%d",ttypetag(o)); break; } } #define COMMENT "\t; " #define EXTRAARG GETARG_Ax(code[pc+1]) #define EXTRAARGC (EXTRAARG*(MAXARG_C+1)) #define ISK (isk ? "k" : "") static void PrintCode(const Proto* f) { const Instruction* code=f->code; int pc,n=f->sizecode; for (pc=0; pc0) printf("[%d]\t",line); else printf("[-]\t"); printf("%-9s\t",opnames[o]); switch (o) { case OP_MOVE: printf("%d %d",a,b); break; case OP_LOADI: printf("%d %d",a,sbx); break; case OP_LOADF: printf("%d %d",a,sbx); break; case OP_LOADK: printf("%d %d",a,bx); printf(COMMENT); PrintConstant(f,bx); break; case OP_LOADKX: printf("%d",a); printf(COMMENT); PrintConstant(f,EXTRAARG); break; case OP_LOADFALSE: printf("%d",a); break; case OP_LFALSESKIP: printf("%d",a); break; case OP_LOADTRUE: printf("%d",a); break; case OP_LOADNIL: printf("%d %d",a,b); printf(COMMENT "%d out",b+1); break; case OP_GETUPVAL: printf("%d %d",a,b); printf(COMMENT "%s",UPVALNAME(b)); break; case OP_SETUPVAL: printf("%d %d",a,b); printf(COMMENT "%s",UPVALNAME(b)); break; case OP_GETTABUP: printf("%d %d %d",a,b,c); printf(COMMENT "%s",UPVALNAME(b)); printf(" "); PrintConstant(f,c); break; case OP_GETTABLE: printf("%d %d %d",a,b,c); break; case OP_GETI: printf("%d %d %d",a,b,c); break; case OP_GETFIELD: printf("%d %d %d",a,b,c); printf(COMMENT); PrintConstant(f,c); break; case OP_SETTABUP: printf("%d %d %d%s",a,b,c,ISK); printf(COMMENT "%s",UPVALNAME(a)); printf(" "); PrintConstant(f,b); if (isk) { printf(" "); PrintConstant(f,c); } break; case OP_SETTABLE: printf("%d %d %d%s",a,b,c,ISK); if (isk) { printf(COMMENT); PrintConstant(f,c); } break; case OP_SETI: printf("%d %d %d%s",a,b,c,ISK); if (isk) { printf(COMMENT); PrintConstant(f,c); } break; case OP_SETFIELD: printf("%d %d %d%s",a,b,c,ISK); printf(COMMENT); PrintConstant(f,b); if (isk) { printf(" "); PrintConstant(f,c); } break; case OP_NEWTABLE: printf("%d %d %d%s",a,vb,vc,ISK); printf(COMMENT "%d",vc+EXTRAARGC); break; case OP_SELF: printf("%d %d %d%s",a,b,c,ISK); if (isk) { printf(COMMENT); PrintConstant(f,c); } break; case OP_ADDI: printf("%d %d %d",a,b,sc); break; case OP_ADDK: printf("%d %d %d",a,b,c); printf(COMMENT); PrintConstant(f,c); break; case OP_SUBK: printf("%d %d %d",a,b,c); printf(COMMENT); PrintConstant(f,c); break; case OP_MULK: printf("%d %d %d",a,b,c); printf(COMMENT); PrintConstant(f,c); break; case OP_MODK: printf("%d %d %d",a,b,c); printf(COMMENT); PrintConstant(f,c); break; case OP_POWK: printf("%d %d %d",a,b,c); printf(COMMENT); PrintConstant(f,c); break; case OP_DIVK: printf("%d %d %d",a,b,c); printf(COMMENT); PrintConstant(f,c); break; case OP_IDIVK: printf("%d %d %d",a,b,c); printf(COMMENT); PrintConstant(f,c); break; case OP_BANDK: printf("%d %d %d",a,b,c); printf(COMMENT); PrintConstant(f,c); break; case OP_BORK: printf("%d %d %d",a,b,c); printf(COMMENT); PrintConstant(f,c); break; case OP_BXORK: printf("%d %d %d",a,b,c); printf(COMMENT); PrintConstant(f,c); break; case OP_SHLI: printf("%d %d %d",a,b,sc); break; case OP_SHRI: printf("%d %d %d",a,b,sc); break; case OP_ADD: printf("%d %d %d",a,b,c); break; case OP_SUB: printf("%d %d %d",a,b,c); break; case OP_MUL: printf("%d %d %d",a,b,c); break; case OP_MOD: printf("%d %d %d",a,b,c); break; case OP_POW: printf("%d %d %d",a,b,c); break; case OP_DIV: printf("%d %d %d",a,b,c); break; case OP_IDIV: printf("%d %d %d",a,b,c); break; case OP_BAND: printf("%d %d %d",a,b,c); break; case OP_BOR: printf("%d %d %d",a,b,c); break; case OP_BXOR: printf("%d %d %d",a,b,c); break; case OP_SHL: printf("%d %d %d",a,b,c); break; case OP_SHR: printf("%d %d %d",a,b,c); break; case OP_MMBIN: printf("%d %d %d",a,b,c); printf(COMMENT "%s",eventname(c)); break; case OP_MMBINI: printf("%d %d %d %d",a,sb,c,isk); printf(COMMENT "%s",eventname(c)); if (isk) printf(" flip"); break; case OP_MMBINK: printf("%d %d %d %d",a,b,c,isk); printf(COMMENT "%s ",eventname(c)); PrintConstant(f,b); if (isk) printf(" flip"); break; case OP_UNM: printf("%d %d",a,b); break; case OP_BNOT: printf("%d %d",a,b); break; case OP_NOT: printf("%d %d",a,b); break; case OP_LEN: printf("%d %d",a,b); break; case OP_CONCAT: printf("%d %d",a,b); break; case OP_CLOSE: printf("%d",a); break; case OP_TBC: printf("%d",a); break; case OP_JMP: printf("%d",GETARG_sJ(i)); printf(COMMENT "to %d",GETARG_sJ(i)+pc+2); break; case OP_EQ: printf("%d %d %d",a,b,isk); break; case OP_LT: printf("%d %d %d",a,b,isk); break; case OP_LE: printf("%d %d %d",a,b,isk); break; case OP_EQK: printf("%d %d %d",a,b,isk); printf(COMMENT); PrintConstant(f,b); break; case OP_EQI: printf("%d %d %d",a,sb,isk); break; case OP_LTI: printf("%d %d %d",a,sb,isk); break; case OP_LEI: printf("%d %d %d",a,sb,isk); break; case OP_GTI: printf("%d %d %d",a,sb,isk); break; case OP_GEI: printf("%d %d %d",a,sb,isk); break; case OP_TEST: printf("%d %d",a,isk); break; case OP_TESTSET: printf("%d %d %d",a,b,isk); break; case OP_CALL: printf("%d %d %d",a,b,c); printf(COMMENT); if (b==0) printf("all in "); else printf("%d in ",b-1); if (c==0) printf("all out"); else printf("%d out",c-1); break; case OP_TAILCALL: printf("%d %d %d%s",a,b,c,ISK); printf(COMMENT "%d in",b-1); break; case OP_RETURN: printf("%d %d %d%s",a,b,c,ISK); printf(COMMENT); if (b==0) printf("all out"); else printf("%d out",b-1); break; case OP_RETURN0: break; case OP_RETURN1: printf("%d",a); break; case OP_FORLOOP: printf("%d %d",a,bx); printf(COMMENT "to %d",pc-bx+2); break; case OP_FORPREP: printf("%d %d",a,bx); printf(COMMENT "exit to %d",pc+bx+3); break; case OP_TFORPREP: printf("%d %d",a,bx); printf(COMMENT "to %d",pc+bx+2); break; case OP_TFORCALL: printf("%d %d",a,c); break; case OP_TFORLOOP: printf("%d %d",a,bx); printf(COMMENT "to %d",pc-bx+2); break; case OP_SETLIST: printf("%d %d %d%s",a,vb,vc,ISK); if (isk) printf(COMMENT "%d",c+EXTRAARGC); break; case OP_CLOSURE: printf("%d %d",a,bx); printf(COMMENT "%p",VOID(f->p[bx])); break; case OP_VARARG: printf("%d %d %d%s",a,b,c,ISK); printf(COMMENT); if (c==0) printf("all out"); else printf("%d out",c-1); break; case OP_GETVARG: printf("%d %d %d",a,b,c); break; case OP_ERRNNIL: printf("%d %d",a,bx); printf(COMMENT); if (bx==0) printf("?"); else PrintConstant(f,bx-1); break; case OP_VARARGPREP: printf("%d",a); break; case OP_EXTRAARG: printf("%d",ax); break; #if 0 default: printf("%d %d %d",a,b,c); printf(COMMENT "not handled"); break; #endif } printf("\n"); } } #define SS(x) ((x==1)?"":"s") #define S(x) (int)(x),SS(x) static void PrintHeader(const Proto* f) { const char* s=f->source ? getstr(f->source) : "=?"; if (*s=='@' || *s=='=') s++; else if (*s==LUA_SIGNATURE[0]) s="(bstring)"; else s="(string)"; printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n", (f->linedefined==0)?"main":"function",s, f->linedefined,f->lastlinedefined, S(f->sizecode),VOID(f)); printf("%d%s param%s, %d slot%s, %d upvalue%s, ", (int)(f->numparams),isvararg(f)?"+":"",SS(f->numparams), S(f->maxstacksize),S(f->sizeupvalues)); printf("%d local%s, %d constant%s, %d function%s\n", S(f->sizelocvars),S(f->sizek),S(f->sizep)); } static void PrintDebug(const Proto* f) { int i,n; n=f->sizek; printf("constants (%d) for %p:\n",n,VOID(f)); for (i=0; isizelocvars; printf("locals (%d) for %p:\n",n,VOID(f)); for (i=0; ilocvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); } n=f->sizeupvalues; printf("upvalues (%d) for %p:\n",n,VOID(f)); for (i=0; iupvalues[i].instack,f->upvalues[i].idx); } } static void PrintFunction(const Proto* f, int full) { int i,n=f->sizep; PrintHeader(f); PrintCode(f); if (full) PrintDebug(f); for (i=0; ip[i],full); } ================================================ FILE: 3rd/lua/luaconf.h ================================================ /* ** $Id: luaconf.h $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ #ifndef luaconf_h #define luaconf_h #include #include /* ** =================================================================== ** General Configuration File for Lua ** ** Some definitions here can be changed externally, through the compiler ** (e.g., with '-D' options): They are commented out or protected ** by '#if !defined' guards. However, several other definitions ** should be changed directly here, either because they affect the ** Lua ABI (by making the changes here, you ensure that all software ** connected to Lua, such as C libraries, will be compiled with the same ** configuration); or because they are seldom changed. ** ** Search for "@@" to find all configurable definitions. ** =================================================================== */ /* ** {==================================================================== ** System Configuration: macros to adapt (if needed) Lua to some ** particular platform, for instance restricting it to C89. ** ===================================================================== */ /* @@ LUA_USE_C89 controls the use of non-ISO-C89 features. ** Define it if you want Lua to avoid the use of a few C99 features ** or Windows-specific features on Windows. */ /* #define LUA_USE_C89 */ /* ** By default, Lua on Windows use (some) specific Windows features */ #if !defined(LUA_USE_C89) && defined(_WIN32) && !defined(_WIN32_WCE) #define LUA_USE_WINDOWS /* enable goodies for regular Windows */ #endif #if defined(LUA_USE_WINDOWS) #define LUA_DL_DLL /* enable support for DLL */ #define LUA_USE_C89 /* broadly, Windows is C89 */ #endif /* ** When POSIX DLL ('LUA_USE_DLOPEN') is enabled, the Lua stand-alone ** application will try to dynamically link a 'readline' facility ** for its REPL. In that case, LUA_READLINELIB is the name of the ** library it will look for those facilities. If lua.c cannot open ** the specified library, it will generate a warning and then run ** without 'readline'. If that macro is not defined, lua.c will not ** use 'readline'. */ #if defined(LUA_USE_LINUX) #define LUA_USE_POSIX #define LUA_USE_DLOPEN /* needs an extra library: -ldl */ #define LUA_READLINELIB "libreadline.so" #endif #if defined(LUA_USE_MACOSX) #define LUA_USE_POSIX #define LUA_USE_DLOPEN /* macOS does not need -ldl */ #define LUA_READLINELIB "libedit.dylib" #endif #if defined(LUA_USE_IOS) #define LUA_USE_POSIX #define LUA_USE_DLOPEN #endif #if defined(LUA_USE_C89) && defined(LUA_USE_POSIX) #error "POSIX is not compatible with C89" #endif /* @@ LUAI_IS32INT is true iff 'int' has (at least) 32 bits. */ #define LUAI_IS32INT ((UINT_MAX >> 30) >= 3) /* }================================================================== */ /* ** {================================================================== ** Configuration for Number types. These options should not be ** set externally, because any other code connected to Lua must ** use the same configuration. ** =================================================================== */ /* @@ LUA_INT_TYPE defines the type for Lua integers. @@ LUA_FLOAT_TYPE defines the type for Lua floats. ** Lua should work fine with any mix of these options supported ** by your C compiler. The usual configurations are 64-bit integers ** and 'double' (the default), 32-bit integers and 'float' (for ** restricted platforms), and 'long'/'double' (for C compilers not ** compliant with C99, which may not have support for 'long long'). */ /* predefined options for LUA_INT_TYPE */ #define LUA_INT_INT 1 #define LUA_INT_LONG 2 #define LUA_INT_LONGLONG 3 /* predefined options for LUA_FLOAT_TYPE */ #define LUA_FLOAT_FLOAT 1 #define LUA_FLOAT_DOUBLE 2 #define LUA_FLOAT_LONGDOUBLE 3 /* Default configuration ('long long' and 'double', for 64-bit Lua) */ #define LUA_INT_DEFAULT LUA_INT_LONGLONG #define LUA_FLOAT_DEFAULT LUA_FLOAT_DOUBLE /* @@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. */ /* #define LUA_32BITS */ /* @@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for ** C89 ('long' and 'double'); Windows always has '__int64', so it does ** not need to use this case. */ #if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) #define LUA_C89_NUMBERS 1 #else #define LUA_C89_NUMBERS 0 #endif #if defined(LUA_32BITS) /* { */ /* ** 32-bit integers and 'float' */ #if LUAI_IS32INT /* use 'int' if big enough */ #define LUA_INT_TYPE LUA_INT_INT #else /* otherwise use 'long' */ #define LUA_INT_TYPE LUA_INT_LONG #endif #define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT #elif LUA_C89_NUMBERS /* }{ */ /* ** largest types available for C89 ('long' and 'double') */ #define LUA_INT_TYPE LUA_INT_LONG #define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE #else /* }{ */ /* use defaults */ #define LUA_INT_TYPE LUA_INT_DEFAULT #define LUA_FLOAT_TYPE LUA_FLOAT_DEFAULT #endif /* } */ /* }================================================================== */ /* ** {================================================================== ** Configuration for Paths. ** =================================================================== */ /* ** LUA_PATH_SEP is the character that separates templates in a path. ** LUA_PATH_MARK is the string that marks the substitution points in a ** template. ** LUA_EXEC_DIR in a Windows path is replaced by the executable's ** directory. */ #define LUA_PATH_SEP ";" #define LUA_PATH_MARK "?" #define LUA_EXEC_DIR "!" /* @@ LUA_PATH_DEFAULT is the default path that Lua uses to look for ** Lua libraries. @@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for ** C libraries. ** CHANGE them if your machine has a non-conventional directory ** hierarchy or if you want to install your libraries in ** non-conventional directories. */ #define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #if defined(_WIN32) /* { */ /* ** In Windows, any exclamation mark ('!') in the path is replaced by the ** path of the directory of the executable file of the current process. */ #define LUA_LDIR "!\\lua\\" #define LUA_CDIR "!\\" #define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" #if !defined(LUA_PATH_DEFAULT) #define LUA_PATH_DEFAULT \ LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ ".\\?.lua;" ".\\?\\init.lua" #endif #if !defined(LUA_CPATH_DEFAULT) #define LUA_CPATH_DEFAULT \ LUA_CDIR"?.dll;" \ LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ LUA_CDIR"loadall.dll;" ".\\?.dll" #endif #else /* }{ */ #define LUA_ROOT "/usr/local/" #define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" #define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" #if !defined(LUA_PATH_DEFAULT) #define LUA_PATH_DEFAULT \ LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ "./?.lua;" "./?/init.lua" #endif #if !defined(LUA_CPATH_DEFAULT) #define LUA_CPATH_DEFAULT \ LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" #endif #endif /* } */ /* @@ LUA_DIRSEP is the directory separator (for submodules). ** CHANGE it if your machine does not use "/" as the directory separator ** and is not Windows. (On Windows Lua automatically uses "\".) */ #if !defined(LUA_DIRSEP) #if defined(_WIN32) #define LUA_DIRSEP "\\" #else #define LUA_DIRSEP "/" #endif #endif /* ** LUA_IGMARK is a mark to ignore all after it when building the ** module name (e.g., used to build the luaopen_ function name). ** Typically, the suffix after the mark is the module version, ** as in "mod-v1.2.so". */ #define LUA_IGMARK "-" /* }================================================================== */ /* ** {================================================================== ** Marks for exported symbols in the C code ** =================================================================== */ /* @@ LUA_API is a mark for all core API functions. @@ LUALIB_API is a mark for all auxiliary library functions. @@ LUAMOD_API is a mark for all standard library opening functions. ** CHANGE them if you need to define those functions in some special way. ** For instance, if you want to create one Windows DLL with the core and ** the libraries, you may want to use the following definition (define ** LUA_BUILD_AS_DLL to get it). */ #if defined(LUA_BUILD_AS_DLL) /* { */ #if defined(LUA_CORE) || defined(LUA_LIB) /* { */ #define LUA_API __declspec(dllexport) #else /* }{ */ #define LUA_API __declspec(dllimport) #endif /* } */ #else /* }{ */ #define LUA_API extern #endif /* } */ /* ** More often than not the libs go together with the core. */ #define LUALIB_API LUA_API #if defined(__cplusplus) /* Lua uses the "C name" when calling open functions */ #define LUAMOD_API extern "C" #else #define LUAMOD_API LUA_API #endif /* }================================================================== */ /* ** {================================================================== ** Compatibility with previous versions ** =================================================================== */ /* @@ LUA_COMPAT_GLOBAL avoids 'global' being a reserved word */ #define LUA_COMPAT_GLOBAL /* @@ LUA_COMPAT_MATHLIB controls the presence of several deprecated ** functions in the mathematical library. ** (These functions were already officially removed in 5.3; ** nevertheless they are still available here.) */ /* #define LUA_COMPAT_MATHLIB */ /* @@ The following macros supply trivial compatibility for some ** changes in the API. The macros themselves document how to ** change your code to avoid using them. ** (Once more, these macros were officially removed in 5.3, but they are ** still available here.) */ #define lua_strlen(L,i) lua_rawlen(L, (i)) #define lua_objlen(L,i) lua_rawlen(L, (i)) #define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) #define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT) /* }================================================================== */ /* ** {================================================================== ** Configuration for Numbers (low-level part). ** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_* ** satisfy your needs. ** =================================================================== */ /* @@ LUAI_UACNUMBER is the result of a 'default argument promotion' @@ over a floating number. @@ l_floatatt(x) corrects float attribute 'x' to the proper float type ** by prefixing it with one of FLT/DBL/LDBL. @@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. @@ LUA_NUMBER_FMT is the format for writing floats with the maximum ** number of digits that respects tostring(tonumber(numeral)) == numeral. ** (That would be floor(log10(2^n)), where n is the number of bits in ** the float mantissa.) @@ LUA_NUMBER_FMT_N is the format for writing floats with the minimum ** number of digits that ensures tonumber(tostring(number)) == number. ** (That would be LUA_NUMBER_FMT+2.) @@ l_mathop allows the addition of an 'l' or 'f' to all math operations. @@ l_floor takes the floor of a float. @@ lua_str2number converts a decimal numeral to a number. */ /* The following definition is good for most cases here */ #define l_floor(x) (l_mathop(floor)(x)) /* now the variable definitions */ #if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */ #define LUA_NUMBER float #define l_floatatt(n) (FLT_##n) #define LUAI_UACNUMBER double #define LUA_NUMBER_FRMLEN "" #define LUA_NUMBER_FMT "%.7g" #define LUA_NUMBER_FMT_N "%.9g" #define l_mathop(op) op##f #define lua_str2number(s,p) strtof((s), (p)) #elif LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE /* }{ long double */ #define LUA_NUMBER long double #define l_floatatt(n) (LDBL_##n) #define LUAI_UACNUMBER long double #define LUA_NUMBER_FRMLEN "L" #define LUA_NUMBER_FMT "%.19Lg" #define LUA_NUMBER_FMT_N "%.21Lg" #define l_mathop(op) op##l #define lua_str2number(s,p) strtold((s), (p)) #elif LUA_FLOAT_TYPE == LUA_FLOAT_DOUBLE /* }{ double */ #define LUA_NUMBER double #define l_floatatt(n) (DBL_##n) #define LUAI_UACNUMBER double #define LUA_NUMBER_FRMLEN "" #define LUA_NUMBER_FMT "%.15g" #define LUA_NUMBER_FMT_N "%.17g" #define l_mathop(op) op #define lua_str2number(s,p) strtod((s), (p)) #else /* }{ */ #error "numeric float type not defined" #endif /* } */ /* @@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. @@ LUAI_UACINT is the result of a 'default argument promotion' @@ over a LUA_INTEGER. @@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. @@ LUA_INTEGER_FMT is the format for writing integers. @@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. @@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. @@ LUA_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED. @@ lua_integer2str converts an integer to a string. */ /* The following definitions are good for most cases here */ #define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" #define LUAI_UACINT LUA_INTEGER #define lua_integer2str(s,sz,n) \ l_sprintf((s), sz, LUA_INTEGER_FMT, (LUAI_UACINT)(n)) /* ** use LUAI_UACINT here to avoid problems with promotions (which ** can turn a comparison between unsigneds into a signed comparison) */ #define LUA_UNSIGNED unsigned LUAI_UACINT /* now the variable definitions */ #if LUA_INT_TYPE == LUA_INT_INT /* { int */ #define LUA_INTEGER int #define LUA_INTEGER_FRMLEN "" #define LUA_MAXINTEGER INT_MAX #define LUA_MININTEGER INT_MIN #define LUA_MAXUNSIGNED UINT_MAX #elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */ #define LUA_INTEGER long #define LUA_INTEGER_FRMLEN "l" #define LUA_MAXINTEGER LONG_MAX #define LUA_MININTEGER LONG_MIN #define LUA_MAXUNSIGNED ULONG_MAX #elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ /* use presence of macro LLONG_MAX as proxy for C99 compliance */ #if defined(LLONG_MAX) /* { */ /* use ISO C99 stuff */ #define LUA_INTEGER long long #define LUA_INTEGER_FRMLEN "ll" #define LUA_MAXINTEGER LLONG_MAX #define LUA_MININTEGER LLONG_MIN #define LUA_MAXUNSIGNED ULLONG_MAX #elif defined(LUA_USE_WINDOWS) /* }{ */ /* in Windows, can use specific Windows types */ #define LUA_INTEGER __int64 #define LUA_INTEGER_FRMLEN "I64" #define LUA_MAXINTEGER _I64_MAX #define LUA_MININTEGER _I64_MIN #define LUA_MAXUNSIGNED _UI64_MAX #else /* }{ */ #error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \ or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)" #endif /* } */ #else /* }{ */ #error "numeric integer type not defined" #endif /* } */ /* }================================================================== */ /* ** {================================================================== ** Dependencies with C99 and other C details ** =================================================================== */ /* @@ l_sprintf is equivalent to 'snprintf' or 'sprintf' in C89. ** (All uses in Lua have only one format item.) */ #if !defined(LUA_USE_C89) #define l_sprintf(s,sz,f,i) snprintf(s,sz,f,i) #else #define l_sprintf(s,sz,f,i) ((void)(sz), sprintf(s,f,i)) #endif /* @@ lua_strx2number converts a hexadecimal numeral to a number. ** In C99, 'strtod' does that conversion. Otherwise, you can ** leave 'lua_strx2number' undefined and Lua will provide its own ** implementation. */ #if !defined(LUA_USE_C89) #define lua_strx2number(s,p) lua_str2number(s,p) #endif /* @@ lua_pointer2str converts a pointer to a readable string in a ** non-specified way. */ #define lua_pointer2str(buff,sz,p) l_sprintf(buff,sz,"%p",p) /* @@ lua_number2strx converts a float to a hexadecimal numeral. ** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. ** Otherwise, you can leave 'lua_number2strx' undefined and Lua will ** provide its own implementation. */ #if !defined(LUA_USE_C89) #define lua_number2strx(L,b,sz,f,n) \ ((void)L, l_sprintf(b,sz,f,(LUAI_UACNUMBER)(n))) #endif /* ** 'strtof' and 'opf' variants for math functions are not valid in ** C89. Otherwise, the macro 'HUGE_VALF' is a good proxy for testing the ** availability of these variants. ('math.h' is already included in ** all files that use these macros.) */ #if defined(LUA_USE_C89) || (defined(HUGE_VAL) && !defined(HUGE_VALF)) #undef l_mathop /* variants not available */ #undef lua_str2number #define l_mathop(op) (lua_Number)op /* no variant */ #define lua_str2number(s,p) ((lua_Number)strtod((s), (p))) #endif /* @@ LUA_KCONTEXT is the type of the context ('ctx') for continuation ** functions. It must be a numerical type; Lua will use 'intptr_t' if ** available, otherwise it will use 'ptrdiff_t' (the nearest thing to ** 'intptr_t' in C89) */ #define LUA_KCONTEXT ptrdiff_t #if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ __STDC_VERSION__ >= 199901L #include #if defined(INTPTR_MAX) /* even in C99 this type is optional */ #undef LUA_KCONTEXT #define LUA_KCONTEXT intptr_t #endif #endif /* @@ lua_getlocaledecpoint gets the locale "radix character" (decimal point). ** Change that if you do not want to use C locales. (Code using this ** macro must include the header 'locale.h'.) */ #if !defined(lua_getlocaledecpoint) #define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) #endif /* ** macros to improve jump prediction, used mostly for error handling ** and debug facilities. (Some macros in the Lua API use these macros. ** Define LUA_NOBUILTIN if you do not want '__builtin_expect' in your ** code.) */ #if !defined(luai_likely) #if defined(__GNUC__) && !defined(LUA_NOBUILTIN) #define luai_likely(x) (__builtin_expect(((x) != 0), 1)) #define luai_unlikely(x) (__builtin_expect(((x) != 0), 0)) #else #define luai_likely(x) (x) #define luai_unlikely(x) (x) #endif #endif /* }================================================================== */ /* ** {================================================================== ** Language Variations ** ===================================================================== */ /* @@ LUA_NOCVTN2S/LUA_NOCVTS2N control how Lua performs some ** coercions. Define LUA_NOCVTN2S to turn off automatic coercion from ** numbers to strings. Define LUA_NOCVTS2N to turn off automatic ** coercion from strings to numbers. */ /* #define LUA_NOCVTN2S */ /* #define LUA_NOCVTS2N */ /* @@ LUA_USE_APICHECK turns on several consistency checks on the C API. ** Define it as a help when debugging C code. */ /* #define LUA_USE_APICHECK */ /* }================================================================== */ /* ** {================================================================== ** Macros that affect the API and must be stable (that is, must be the ** same when you compile Lua and when you compile code that links to ** Lua). ** ===================================================================== */ /* @@ LUA_EXTRASPACE defines the size of a raw memory area associated with ** a Lua state with very fast access. ** CHANGE it if you need a different size. */ #define LUA_EXTRASPACE (sizeof(void *)) /* @@ LUA_IDSIZE gives the maximum size for the description of the source ** of a function in debug information. ** CHANGE it if you want a different size. */ #define LUA_IDSIZE 60 /* @@ LUAL_BUFFERSIZE is the initial buffer size used by the lauxlib ** buffer system. */ #define LUAL_BUFFERSIZE ((int)(16 * sizeof(void*) * sizeof(lua_Number))) /* @@ LUAI_MAXALIGN defines fields that, when used in a union, ensure ** maximum alignment for the other items in that union. */ #define LUAI_MAXALIGN lua_Number n; double u; void *s; lua_Integer i; long l /* }================================================================== */ /* =================================================================== */ /* ** Local configuration. You can use this space to add your redefinitions ** without modifying the main part of the file. */ #endif ================================================ FILE: 3rd/lua/lualib.h ================================================ /* ** $Id: lualib.h $ ** Lua standard libraries ** See Copyright Notice in lua.h */ #ifndef lualib_h #define lualib_h #include "lua.h" /* version suffix for environment variable names */ #define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR #define LUA_GLIBK 1 LUAMOD_API int (luaopen_base) (lua_State *L); #define LUA_LOADLIBNAME "package" #define LUA_LOADLIBK (LUA_GLIBK << 1) LUAMOD_API int (luaopen_package) (lua_State *L); #define LUA_CACHELIB LUAMOD_API int (luaopen_cache) (lua_State *L); LUALIB_API void (luaL_initcodecache) (void); #define LUA_COLIBNAME "coroutine" #define LUA_COLIBK (LUA_LOADLIBK << 1) LUAMOD_API int (luaopen_coroutine) (lua_State *L); #define LUA_DBLIBNAME "debug" #define LUA_DBLIBK (LUA_COLIBK << 1) LUAMOD_API int (luaopen_debug) (lua_State *L); #define LUA_IOLIBNAME "io" #define LUA_IOLIBK (LUA_DBLIBK << 1) LUAMOD_API int (luaopen_io) (lua_State *L); #define LUA_MATHLIBNAME "math" #define LUA_MATHLIBK (LUA_IOLIBK << 1) LUAMOD_API int (luaopen_math) (lua_State *L); #define LUA_OSLIBNAME "os" #define LUA_OSLIBK (LUA_MATHLIBK << 1) LUAMOD_API int (luaopen_os) (lua_State *L); #define LUA_STRLIBNAME "string" #define LUA_STRLIBK (LUA_OSLIBK << 1) LUAMOD_API int (luaopen_string) (lua_State *L); #define LUA_TABLIBNAME "table" #define LUA_TABLIBK (LUA_STRLIBK << 1) LUAMOD_API int (luaopen_table) (lua_State *L); #define LUA_UTF8LIBNAME "utf8" #define LUA_UTF8LIBK (LUA_TABLIBK << 1) LUAMOD_API int (luaopen_utf8) (lua_State *L); /* open selected libraries */ LUALIB_API void (luaL_openselectedlibs) (lua_State *L, int load, int preload); /* open all libraries */ #define luaL_openlibs(L) luaL_openselectedlibs(L, ~0, 0) #endif ================================================ FILE: 3rd/lua/lundump.c ================================================ /* ** $Id: lundump.c $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ #define lundump_c #define LUA_CORE #include "lprefix.h" #include #include #include "lua.h" #include "ldebug.h" #include "ldo.h" #include "lfunc.h" #include "lmem.h" #include "lobject.h" #include "lstring.h" #include "ltable.h" #include "lundump.h" #include "lzio.h" #if !defined(luai_verifycode) #define luai_verifycode(L,f) /* empty */ #endif typedef struct { lua_State *L; ZIO *Z; const char *name; Table *h; /* list for string reuse */ size_t offset; /* current position relative to beginning of dump */ lua_Unsigned nstr; /* number of strings in the list */ lu_byte fixed; /* dump is fixed in memory */ } LoadState; static l_noret error (LoadState *S, const char *why) { luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why); luaD_throw(S->L, LUA_ERRSYNTAX); } /* ** All high-level loads go through loadVector; you can change it to ** adapt to the endianness of the input */ #define loadVector(S,b,n) loadBlock(S,b,cast_sizet(n)*sizeof((b)[0])) static void loadBlock (LoadState *S, void *b, size_t size) { if (luaZ_read(S->Z, b, size) != 0) error(S, "truncated chunk"); S->offset += size; } static void loadAlign (LoadState *S, unsigned align) { unsigned padding = align - cast_uint(S->offset % align); if (padding < align) { /* (padding == align) means no padding */ lua_Integer paddingContent; loadBlock(S, &paddingContent, padding); lua_assert(S->offset % align == 0); } } #define getaddr(S,n,t) cast(t *, getaddr_(S,cast_sizet(n) * sizeof(t))) static const void *getaddr_ (LoadState *S, size_t size) { const void *block = luaZ_getaddr(S->Z, size); S->offset += size; if (block == NULL) error(S, "truncated fixed buffer"); return block; } #define loadVar(S,x) loadVector(S,&x,1) static lu_byte loadByte (LoadState *S) { int b = zgetc(S->Z); if (b == EOZ) error(S, "truncated chunk"); S->offset++; return cast_byte(b); } static lua_Unsigned loadVarint (LoadState *S, lua_Unsigned limit) { lua_Unsigned x = 0; int b; limit >>= 7; do { b = loadByte(S); if (x > limit) error(S, "integer overflow"); x = (x << 7) | (b & 0x7f); } while ((b & 0x80) != 0); return x; } static size_t loadSize (LoadState *S) { return cast_sizet(loadVarint(S, MAX_SIZE)); } static int loadInt (LoadState *S) { return cast_int(loadVarint(S, cast_sizet(INT_MAX))); } static lua_Number loadNumber (LoadState *S) { lua_Number x; loadVar(S, x); return x; } static lua_Integer loadInteger (LoadState *S) { lua_Unsigned cx = loadVarint(S, LUA_MAXUNSIGNED); /* decode unsigned to signed */ if ((cx & 1) != 0) return l_castU2S(~(cx >> 1)); else return l_castU2S(cx >> 1); } /* ** Load a nullable string into slot 'sl' from prototype 'p'. The ** assignment to the slot and the barrier must be performed before any ** possible GC activity, to anchor the string. (Both 'loadVector' and ** 'luaH_setint' can call the GC.) */ static void loadString (LoadState *S, Proto *p, TString **sl) { lua_State *L = S->L; TString *ts; TValue sv; size_t size = loadSize(S); if (size == 0) { /* previously saved string? */ lua_Unsigned idx = loadVarint(S, LUA_MAXUNSIGNED); /* get its index */ TValue stv; if (idx == 0) { /* no string? */ lua_assert(*sl == NULL); /* must be prefilled */ return; } if (novariant(luaH_getint(S->h, l_castU2S(idx), &stv)) != LUA_TSTRING) error(S, "invalid string index"); *sl = ts = tsvalue(&stv); /* get its value */ luaC_objbarrier(L, p, ts); return; /* do not save it again */ } else if ((size -= 1) <= LUAI_MAXSHORTLEN) { /* short string? */ char buff[LUAI_MAXSHORTLEN + 1]; /* extra space for '\0' */ loadVector(S, buff, size + 1); /* load string into buffer */ *sl = ts = luaS_newlstr(L, buff, size); /* create string */ luaC_objbarrier(L, p, ts); } else if (S->fixed) { /* for a fixed buffer, use a fixed string */ const char *s = getaddr(S, size + 1, char); /* get content address */ *sl = ts = luaS_newextlstr(L, s, size, NULL, NULL); luaC_objbarrier(L, p, ts); } else { /* create internal copy */ *sl = ts = luaS_createlngstrobj(L, size); /* create string */ luaC_objbarrier(L, p, ts); loadVector(S, getlngstr(ts), size + 1); /* load directly in final place */ } /* add string to list of saved strings */ S->nstr++; setsvalue(L, &sv, ts); luaH_setint(L, S->h, l_castU2S(S->nstr), &sv); luaC_objbarrierback(L, obj2gco(S->h), ts); } static void loadCode (LoadState *S, Proto *f) { int n = loadInt(S); loadAlign(S, sizeof(f->code[0])); if (S->fixed) { f->code = getaddr(S, n, Instruction); f->sizecode = n; } else { f->code = luaM_newvectorchecked(S->L, n, Instruction); f->sizecode = n; loadVector(S, f->code, n); } } static void loadFunction(LoadState *S, Proto *f); static void loadConstants (LoadState *S, Proto *f) { int i; int n = loadInt(S); f->k = luaM_newvectorchecked(S->L, n, TValue); f->sizek = n; for (i = 0; i < n; i++) setnilvalue(&f->k[i]); for (i = 0; i < n; i++) { TValue *o = &f->k[i]; int t = loadByte(S); switch (t) { case LUA_VNIL: setnilvalue(o); break; case LUA_VFALSE: setbfvalue(o); break; case LUA_VTRUE: setbtvalue(o); break; case LUA_VNUMFLT: setfltvalue(o, loadNumber(S)); break; case LUA_VNUMINT: setivalue(o, loadInteger(S)); break; case LUA_VSHRSTR: case LUA_VLNGSTR: { lua_assert(f->source == NULL); loadString(S, f, &f->source); /* use 'source' to anchor string */ if (f->source == NULL) error(S, "bad format for constant string"); setsvalue2n(S->L, o, f->source); /* save it in the right place */ f->source = NULL; break; } default: error(S, "invalid constant"); } } } static void loadProtos (LoadState *S, Proto *f) { int i; int n = loadInt(S); f->p = luaM_newvectorchecked(S->L, n, Proto *); f->sizep = n; for (i = 0; i < n; i++) f->p[i] = NULL; for (i = 0; i < n; i++) { f->p[i] = luaF_newproto(S->L); luaC_objbarrier(S->L, f, f->p[i]); loadFunction(S, f->p[i]); } } /* ** Load the upvalues for a function. The names must be filled first, ** because the filling of the other fields can raise read errors and ** the creation of the error message can call an emergency collection; ** in that case all prototypes must be consistent for the GC. */ static void loadUpvalues (LoadState *S, Proto *f) { int i; int n = loadInt(S); f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc); f->sizeupvalues = n; for (i = 0; i < n; i++) /* make array valid for GC */ f->upvalues[i].name = NULL; for (i = 0; i < n; i++) { /* following calls can raise errors */ f->upvalues[i].instack = loadByte(S); f->upvalues[i].idx = loadByte(S); f->upvalues[i].kind = loadByte(S); } } static void loadDebug (LoadState *S, Proto *f) { int i; int n = loadInt(S); if (S->fixed) { f->lineinfo = getaddr(S, n, ls_byte); f->sizelineinfo = n; } else { f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte); f->sizelineinfo = n; loadVector(S, f->lineinfo, n); } n = loadInt(S); if (n > 0) { loadAlign(S, sizeof(int)); if (S->fixed) { f->abslineinfo = getaddr(S, n, AbsLineInfo); f->sizeabslineinfo = n; } else { f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo); f->sizeabslineinfo = n; loadVector(S, f->abslineinfo, n); } } n = loadInt(S); f->locvars = luaM_newvectorchecked(S->L, n, LocVar); f->sizelocvars = n; for (i = 0; i < n; i++) f->locvars[i].varname = NULL; for (i = 0; i < n; i++) { loadString(S, f, &f->locvars[i].varname); f->locvars[i].startpc = loadInt(S); f->locvars[i].endpc = loadInt(S); } n = loadInt(S); if (n != 0) /* does it have debug information? */ n = f->sizeupvalues; /* must be this many */ for (i = 0; i < n; i++) loadString(S, f, &f->upvalues[i].name); } static void loadFunction (LoadState *S, Proto *f) { f->linedefined = loadInt(S); f->lastlinedefined = loadInt(S); f->numparams = loadByte(S); /* get only the meaningful flags */ f->flag = cast_byte(loadByte(S) & ~PF_FIXED); if (S->fixed) f->flag |= PF_FIXED; /* signal that code is fixed */ f->maxstacksize = loadByte(S); loadCode(S, f); loadConstants(S, f); loadUpvalues(S, f); loadProtos(S, f); loadString(S, f, &f->source); loadDebug(S, f); } static void checkliteral (LoadState *S, const char *s, const char *msg) { char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */ size_t len = strlen(s); loadVector(S, buff, len); if (memcmp(s, buff, len) != 0) error(S, msg); } static l_noret numerror (LoadState *S, const char *what, const char *tname) { const char *msg = luaO_pushfstring(S->L, "%s %s mismatch", tname, what); error(S, msg); } static void checknumsize (LoadState *S, int size, const char *tname) { if (size != loadByte(S)) numerror(S, "size", tname); } static void checknumformat (LoadState *S, int eq, const char *tname) { if (!eq) numerror(S, "format", tname); } #define checknum(S,tvar,value,tname) \ { tvar i; checknumsize(S, sizeof(i), tname); \ loadVar(S, i); \ checknumformat(S, i == value, tname); } static void checkHeader (LoadState *S) { /* skip 1st char (already read and checked) */ checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk"); if (loadByte(S) != LUAC_VERSION) error(S, "version mismatch"); if (loadByte(S) != LUAC_FORMAT) error(S, "format mismatch"); checkliteral(S, LUAC_DATA, "corrupted chunk"); checknum(S, int, LUAC_INT, "int"); checknum(S, Instruction, LUAC_INST, "instruction"); checknum(S, lua_Integer, LUAC_INT, "Lua integer"); checknum(S, lua_Number, LUAC_NUM, "Lua number"); } /* ** Load precompiled chunk. */ LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name, int fixed) { LoadState S; LClosure *cl; if (*name == '@' || *name == '=') name = name + 1; else if (*name == LUA_SIGNATURE[0]) name = "binary string"; S.name = name; S.L = L; S.Z = Z; S.fixed = cast_byte(fixed); S.offset = 1; /* fist byte was already read */ checkHeader(&S); cl = luaF_newLclosure(L, loadByte(&S)); setclLvalue2s(L, L->top.p, cl); luaD_inctop(L); S.h = luaH_new(L); /* create list of saved strings */ S.nstr = 0; sethvalue2s(L, L->top.p, S.h); /* anchor it */ luaD_inctop(L); cl->p = luaF_newproto(L); luaC_objbarrier(L, cl, cl->p); loadFunction(&S, cl->p); if (cl->nupvalues != cl->p->sizeupvalues) error(&S, "corrupted chunk"); luai_verifycode(L, cl->p); L->top.p--; /* pop table */ return cl; } ================================================ FILE: 3rd/lua/lundump.h ================================================ /* ** $Id: lundump.h $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ #ifndef lundump_h #define lundump_h #include #include "llimits.h" #include "lobject.h" #include "lzio.h" /* data to catch conversion errors */ #define LUAC_DATA "\x19\x93\r\n\x1a\n" #define LUAC_INT -0x5678 #define LUAC_INST 0x12345678 #define LUAC_NUM cast_num(-370.5) /* ** Encode major-minor version in one byte, one nibble for each */ #define LUAC_VERSION (LUA_VERSION_MAJOR_N*16+LUA_VERSION_MINOR_N) #define LUAC_FORMAT 0 /* this is the official format */ /* load one chunk; from lundump.c */ LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name, int fixed); /* dump one chunk; from ldump.c */ LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); #endif ================================================ FILE: 3rd/lua/lutf8lib.c ================================================ /* ** $Id: lutf8lib.c $ ** Standard library for UTF-8 manipulation ** See Copyright Notice in lua.h */ #define lutf8lib_c #define LUA_LIB #include "lprefix.h" #include #include #include #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "llimits.h" #define MAXUNICODE 0x10FFFFu #define MAXUTF 0x7FFFFFFFu #define MSGInvalid "invalid UTF-8 code" #define iscont(c) (((c) & 0xC0) == 0x80) #define iscontp(p) iscont(*(p)) /* from strlib */ /* translate a relative string position: negative means back from end */ static lua_Integer u_posrelat (lua_Integer pos, size_t len) { if (pos >= 0) return pos; else if (0u - (size_t)pos > len) return 0; else return (lua_Integer)len + pos + 1; } /* ** Decode one UTF-8 sequence, returning NULL if byte sequence is ** invalid. The array 'limits' stores the minimum value for each ** sequence length, to check for overlong representations. Its first ** entry forces an error for non-ASCII bytes with no continuation ** bytes (count == 0). */ static const char *utf8_decode (const char *s, l_uint32 *val, int strict) { static const l_uint32 limits[] = {~(l_uint32)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u}; unsigned int c = (unsigned char)s[0]; l_uint32 res = 0; /* final result */ if (c < 0x80) /* ASCII? */ res = c; else { int count = 0; /* to count number of continuation bytes */ for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */ unsigned int cc = (unsigned char)s[++count]; /* read next byte */ if (!iscont(cc)) /* not a continuation byte? */ return NULL; /* invalid byte sequence */ res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ } res |= ((l_uint32)(c & 0x7F) << (count * 5)); /* add first byte */ if (count > 5 || res > MAXUTF || res < limits[count]) return NULL; /* invalid byte sequence */ s += count; /* skip continuation bytes read */ } if (strict) { /* check for invalid code points; too large or surrogates */ if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu)) return NULL; } if (val) *val = res; return s + 1; /* +1 to include first byte */ } /* ** utf8len(s [, i [, j [, lax]]]) --> number of characters that ** start in the range [i,j], or nil + current position if 's' is not ** well formed in that interval */ static int utflen (lua_State *L) { lua_Integer n = 0; /* counter for the number of characters */ size_t len; /* string length in bytes */ const char *s = luaL_checklstring(L, 1, &len); lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); int lax = lua_toboolean(L, 4); luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, "initial position out of bounds"); luaL_argcheck(L, --posj < (lua_Integer)len, 3, "final position out of bounds"); while (posi <= posj) { const char *s1 = utf8_decode(s + posi, NULL, !lax); if (s1 == NULL) { /* conversion error? */ luaL_pushfail(L); /* return fail ... */ lua_pushinteger(L, posi + 1); /* ... and current position */ return 2; } posi = ct_diff2S(s1 - s); n++; } lua_pushinteger(L, n); return 1; } /* ** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all ** characters that start in the range [i,j] */ static int codepoint (lua_State *L) { size_t len; const char *s = luaL_checklstring(L, 1, &len); lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len); int lax = lua_toboolean(L, 4); int n; const char *se; luaL_argcheck(L, posi >= 1, 2, "out of bounds"); luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds"); if (posi > pose) return 0; /* empty interval; return no values */ if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */ return luaL_error(L, "string slice too long"); n = (int)(pose - posi) + 1; /* upper bound for number of returns */ luaL_checkstack(L, n, "string slice too long"); n = 0; /* count the number of returns */ se = s + pose; /* string end */ for (s += posi - 1; s < se;) { l_uint32 code; s = utf8_decode(s, &code, !lax); if (s == NULL) return luaL_error(L, MSGInvalid); lua_pushinteger(L, l_castU2S(code)); n++; } return n; } static void pushutfchar (lua_State *L, int arg) { lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg); luaL_argcheck(L, code <= MAXUTF, arg, "value out of range"); lua_pushfstring(L, "%U", (long)code); } /* ** utfchar(n1, n2, ...) -> char(n1)..char(n2)... */ static int utfchar (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ if (n == 1) /* optimize common case of single char */ pushutfchar(L, 1); else { int i; luaL_Buffer b; luaL_buffinit(L, &b); for (i = 1; i <= n; i++) { pushutfchar(L, i); luaL_addvalue(&b); } luaL_pushresult(&b); } return 1; } /* ** offset(s, n, [i]) -> indices where n-th character counting from ** position 'i' starts and ends; 0 means character at 'i'. */ static int byteoffset (lua_State *L) { size_t len; const char *s = luaL_checklstring(L, 1, &len); lua_Integer n = luaL_checkinteger(L, 2); lua_Integer posi = (n >= 0) ? 1 : cast_st2S(len) + 1; posi = u_posrelat(luaL_optinteger(L, 3, posi), len); luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, "position out of bounds"); if (n == 0) { /* find beginning of current byte sequence */ while (posi > 0 && iscontp(s + posi)) posi--; } else { if (iscontp(s + posi)) return luaL_error(L, "initial position is a continuation byte"); if (n < 0) { while (n < 0 && posi > 0) { /* move back */ do { /* find beginning of previous character */ posi--; } while (posi > 0 && iscontp(s + posi)); n++; } } else { n--; /* do not move for 1st character */ while (n > 0 && posi < (lua_Integer)len) { do { /* find beginning of next character */ posi++; } while (iscontp(s + posi)); /* (cannot pass final '\0') */ n--; } } } if (n != 0) { /* did not find given character? */ luaL_pushfail(L); return 1; } lua_pushinteger(L, posi + 1); /* initial position */ if ((s[posi] & 0x80) != 0) { /* multi-byte character? */ if (iscont(s[posi])) return luaL_error(L, "initial position is a continuation byte"); while (iscontp(s + posi + 1)) posi++; /* skip to last continuation byte */ } /* else one-byte character: final position is the initial one */ lua_pushinteger(L, posi + 1); /* 'posi' now is the final position */ return 2; } static int iter_aux (lua_State *L, int strict) { size_t len; const char *s = luaL_checklstring(L, 1, &len); lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2); if (n < len) { while (iscontp(s + n)) n++; /* go to next character */ } if (n >= len) /* (also handles original 'n' being negative) */ return 0; /* no more codepoints */ else { l_uint32 code; const char *next = utf8_decode(s + n, &code, strict); if (next == NULL || iscontp(next)) return luaL_error(L, MSGInvalid); lua_pushinteger(L, l_castU2S(n + 1)); lua_pushinteger(L, l_castU2S(code)); return 2; } } static int iter_auxstrict (lua_State *L) { return iter_aux(L, 1); } static int iter_auxlax (lua_State *L) { return iter_aux(L, 0); } static int iter_codes (lua_State *L) { int lax = lua_toboolean(L, 2); const char *s = luaL_checkstring(L, 1); luaL_argcheck(L, !iscontp(s), 1, MSGInvalid); lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict); lua_pushvalue(L, 1); lua_pushinteger(L, 0); return 3; } /* pattern to match a single UTF-8 character */ #define UTF8PATT "[\0-\x7F\xC2-\xFD][\x80-\xBF]*" static const luaL_Reg funcs[] = { {"offset", byteoffset}, {"codepoint", codepoint}, {"char", utfchar}, {"len", utflen}, {"codes", iter_codes}, /* placeholders */ {"charpattern", NULL}, {NULL, NULL} }; LUAMOD_API int luaopen_utf8 (lua_State *L) { luaL_newlib(L, funcs); lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1); lua_setfield(L, -2, "charpattern"); return 1; } ================================================ FILE: 3rd/lua/lvm.c ================================================ /* ** $Id: lvm.c $ ** Lua virtual machine ** See Copyright Notice in lua.h */ #define lvm_c #define LUA_CORE #include "lprefix.h" #include #include #include #include #include #include #include "lua.h" #include "lapi.h" #include "ldebug.h" #include "ldo.h" #include "lfunc.h" #include "lgc.h" #include "lobject.h" #include "lopcodes.h" #include "lstate.h" #include "lstring.h" #include "ltable.h" #include "ltm.h" #include "lvm.h" /* ** By default, use jump tables in the main interpreter loop on gcc ** and compatible compilers. */ #if !defined(LUA_USE_JUMPTABLE) #if defined(__GNUC__) #define LUA_USE_JUMPTABLE 1 #else #define LUA_USE_JUMPTABLE 0 #endif #endif /* limit for table tag-method chains (to avoid infinite loops) */ #define MAXTAGLOOP 2000 /* ** 'l_intfitsf' checks whether a given integer is in the range that ** can be converted to a float without rounding. Used in comparisons. */ /* number of bits in the mantissa of a float */ #define NBM (l_floatatt(MANT_DIG)) /* ** Check whether some integers may not fit in a float, testing whether ** (maxinteger >> NBM) > 0. (That implies (1 << NBM) <= maxinteger.) ** (The shifts are done in parts, to avoid shifting by more than the size ** of an integer. In a worst case, NBM == 113 for long double and ** sizeof(long) == 32.) */ #if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \ >> (NBM - (3 * (NBM / 4)))) > 0 /* limit for integers that fit in a float */ #define MAXINTFITSF ((lua_Unsigned)1 << NBM) /* check whether 'i' is in the interval [-MAXINTFITSF, MAXINTFITSF] */ #define l_intfitsf(i) ((MAXINTFITSF + l_castS2U(i)) <= (2 * MAXINTFITSF)) #else /* all integers fit in a float precisely */ #define l_intfitsf(i) 1 #endif /* ** Try to convert a value from string to a number value. ** If the value is not a string or is a string not representing ** a valid numeral (or if coercions from strings to numbers ** are disabled via macro 'cvt2num'), do not modify 'result' ** and return 0. */ static int l_strton (const TValue *obj, TValue *result) { lua_assert(obj != result); if (!cvt2num(obj)) /* is object not a string? */ return 0; else { TString *st = tsvalue(obj); size_t stlen; const char *s = getlstr(st, stlen); return (luaO_str2num(s, result) == stlen + 1); } } /* ** Try to convert a value to a float. The float case is already handled ** by the macro 'tonumber'. */ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { TValue v; if (ttisinteger(obj)) { *n = cast_num(ivalue(obj)); return 1; } else if (l_strton(obj, &v)) { /* string coercible to number? */ *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ return 1; } else return 0; /* conversion failed */ } /* ** try to convert a float to an integer, rounding according to 'mode'. */ int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) { lua_Number f = l_floor(n); if (n != f) { /* not an integral value? */ if (mode == F2Ieq) return 0; /* fails if mode demands integral value */ else if (mode == F2Iceil) /* needs ceiling? */ f += 1; /* convert floor to ceiling (remember: n != f) */ } return lua_numbertointeger(f, p); } /* ** try to convert a value to an integer, rounding according to 'mode', ** without string coercion. ** ("Fast track" handled by macro 'tointegerns'.) */ int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) { if (ttisfloat(obj)) return luaV_flttointeger(fltvalue(obj), p, mode); else if (ttisinteger(obj)) { *p = ivalue(obj); return 1; } else return 0; } /* ** try to convert a value to an integer. */ int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) { TValue v; if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */ obj = &v; /* change it to point to its corresponding number */ return luaV_tointegerns(obj, p, mode); } /* ** Try to convert a 'for' limit to an integer, preserving the semantics ** of the loop. Return true if the loop must not run; otherwise, '*p' ** gets the integer limit. ** (The following explanation assumes a positive step; it is valid for ** negative steps mutatis mutandis.) ** If the limit is an integer or can be converted to an integer, ** rounding down, that is the limit. ** Otherwise, check whether the limit can be converted to a float. If ** the float is too large, clip it to LUA_MAXINTEGER. If the float ** is too negative, the loop should not run, because any initial ** integer value is greater than such limit; so, the function returns ** true to signal that. (For this latter case, no integer limit would be ** correct; even a limit of LUA_MININTEGER would run the loop once for ** an initial value equal to LUA_MININTEGER.) */ static int forlimit (lua_State *L, lua_Integer init, const TValue *lim, lua_Integer *p, lua_Integer step) { if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) { /* not coercible to in integer */ lua_Number flim; /* try to convert to float */ if (!tonumber(lim, &flim)) /* cannot convert to float? */ luaG_forerror(L, lim, "limit"); /* else 'flim' is a float out of integer bounds */ if (luai_numlt(0, flim)) { /* if it is positive, it is too large */ if (step < 0) return 1; /* initial value must be less than it */ *p = LUA_MAXINTEGER; /* truncate */ } else { /* it is less than min integer */ if (step > 0) return 1; /* initial value must be greater than it */ *p = LUA_MININTEGER; /* truncate */ } } return (step > 0 ? init > *p : init < *p); /* not to run? */ } /* ** Prepare a numerical for loop (opcode OP_FORPREP). ** Before execution, stack is as follows: ** ra : initial value ** ra + 1 : limit ** ra + 2 : step ** Return true to skip the loop. Otherwise, ** after preparation, stack will be as follows: ** ra : loop counter (integer loops) or limit (float loops) ** ra + 1 : step ** ra + 2 : control variable */ static int forprep (lua_State *L, StkId ra) { TValue *pinit = s2v(ra); TValue *plimit = s2v(ra + 1); TValue *pstep = s2v(ra + 2); if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */ lua_Integer init = ivalue(pinit); lua_Integer step = ivalue(pstep); lua_Integer limit; if (step == 0) luaG_runerror(L, "'for' step is zero"); if (forlimit(L, init, plimit, &limit, step)) return 1; /* skip the loop */ else { /* prepare loop counter */ lua_Unsigned count; if (step > 0) { /* ascending loop? */ count = l_castS2U(limit) - l_castS2U(init); if (step != 1) /* avoid division in the too common case */ count /= l_castS2U(step); } else { /* step < 0; descending loop */ count = l_castS2U(init) - l_castS2U(limit); /* 'step+1' avoids negating 'mininteger' */ count /= l_castS2U(-(step + 1)) + 1u; } /* use 'chgivalue' for places that for sure had integers */ chgivalue(s2v(ra), l_castU2S(count)); /* change init to count */ setivalue(s2v(ra + 1), step); /* change limit to step */ chgivalue(s2v(ra + 2), init); /* change step to init */ } } else { /* try making all values floats */ lua_Number init; lua_Number limit; lua_Number step; if (l_unlikely(!tonumber(plimit, &limit))) luaG_forerror(L, plimit, "limit"); if (l_unlikely(!tonumber(pstep, &step))) luaG_forerror(L, pstep, "step"); if (l_unlikely(!tonumber(pinit, &init))) luaG_forerror(L, pinit, "initial value"); if (step == 0) luaG_runerror(L, "'for' step is zero"); if (luai_numlt(0, step) ? luai_numlt(limit, init) : luai_numlt(init, limit)) return 1; /* skip the loop */ else { /* make sure all values are floats */ setfltvalue(s2v(ra), limit); setfltvalue(s2v(ra + 1), step); setfltvalue(s2v(ra + 2), init); /* control variable */ } } return 0; } /* ** Execute a step of a float numerical for loop, returning ** true iff the loop must continue. (The integer case is ** written online with opcode OP_FORLOOP, for performance.) */ static int floatforloop (StkId ra) { lua_Number step = fltvalue(s2v(ra + 1)); lua_Number limit = fltvalue(s2v(ra)); lua_Number idx = fltvalue(s2v(ra + 2)); /* control variable */ idx = luai_numadd(L, idx, step); /* increment index */ if (luai_numlt(0, step) ? luai_numle(idx, limit) : luai_numle(limit, idx)) { chgfltvalue(s2v(ra + 2), idx); /* update control variable */ return 1; /* jump back */ } else return 0; /* finish the loop */ } /* ** Finish the table access 'val = t[key]' and return the tag of the result. */ lu_byte luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, lu_byte tag) { int loop; /* counter to avoid infinite loops */ const TValue *tm; /* metamethod */ for (loop = 0; loop < MAXTAGLOOP; loop++) { if (tag == LUA_VNOTABLE) { /* 't' is not a table? */ lua_assert(!ttistable(t)); tm = luaT_gettmbyobj(L, t, TM_INDEX); if (l_unlikely(notm(tm))) luaG_typeerror(L, t, "index"); /* no metamethod */ /* else will try the metamethod */ } else { /* 't' is a table */ tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ if (tm == NULL) { /* no metamethod? */ setnilvalue(s2v(val)); /* result is nil */ return LUA_VNIL; } /* else will try the metamethod */ } if (ttisfunction(tm)) { /* is metamethod a function? */ tag = luaT_callTMres(L, tm, t, key, val); /* call it */ return tag; /* return tag of the result */ } t = tm; /* else try to access 'tm[key]' */ luaV_fastget(t, key, s2v(val), luaH_get, tag); if (!tagisempty(tag)) return tag; /* done */ /* else repeat (tail call 'luaV_finishget') */ } luaG_runerror(L, "'__index' chain too long; possible loop"); return 0; /* to avoid warnings */ } /* ** Finish a table assignment 't[key] = val'. ** About anchoring the table before the call to 'luaH_finishset': ** This call may trigger an emergency collection. When loop>0, ** the table being accessed is a field in some metatable. If this ** metatable is weak and the table is not anchored, this collection ** could collect that table while it is being updated. */ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, TValue *val, int hres) { int loop; /* counter to avoid infinite loops */ for (loop = 0; loop < MAXTAGLOOP; loop++) { const TValue *tm; /* '__newindex' metamethod */ if (hres != HNOTATABLE) { /* is 't' a table? */ Table *h = hvalue(t); /* save 't' table */ if (isshared(h)) luaG_typeerror(L, t, "change"); tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ if (tm == NULL) { /* no metamethod? */ sethvalue2s(L, L->top.p, h); /* anchor 't' */ L->top.p++; /* assume EXTRA_STACK */ luaH_finishset(L, h, key, val, hres); /* set new value */ L->top.p--; invalidateTMcache(h); luaC_barrierback(L, obj2gco(h), val); return; } /* else will try the metamethod */ } else { /* not a table; check metamethod */ tm = luaT_gettmbyobj(L, t, TM_NEWINDEX); if (l_unlikely(notm(tm))) luaG_typeerror(L, t, "index"); } /* try the metamethod */ if (ttisfunction(tm)) { luaT_callTM(L, tm, t, key, val); return; } t = tm; /* else repeat assignment over 'tm' */ luaV_fastset(t, key, val, hres, luaH_pset); if (hres == HOK) { luaV_finishfastset(L, t, val); return; /* done */ } /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */ } luaG_runerror(L, "'__newindex' chain too long; possible loop"); } /* ** Function to be used for 0-terminated string order comparison */ #if !defined(l_strcoll) #define l_strcoll strcoll #endif /* ** Compare two strings 'ts1' x 'ts2', returning an integer less-equal- ** -greater than zero if 'ts1' is less-equal-greater than 'ts2'. ** The code is a little tricky because it allows '\0' in the strings ** and it uses 'strcoll' (to respect locales) for each segment ** of the strings. Note that segments can compare equal but still ** have different lengths. */ static int l_strcmp (const TString *ts1, const TString *ts2) { size_t rl1; /* real length */ const char *s1 = getlstr(ts1, rl1); size_t rl2; const char *s2 = getlstr(ts2, rl2); for (;;) { /* for each segment */ int temp = l_strcoll(s1, s2); if (temp != 0) /* not equal? */ return temp; /* done */ else { /* strings are equal up to a '\0' */ size_t zl1 = strlen(s1); /* index of first '\0' in 's1' */ size_t zl2 = strlen(s2); /* index of first '\0' in 's2' */ if (zl2 == rl2) /* 's2' is finished? */ return (zl1 == rl1) ? 0 : 1; /* check 's1' */ else if (zl1 == rl1) /* 's1' is finished? */ return -1; /* 's1' is less than 's2' ('s2' is not finished) */ /* both strings longer than 'zl'; go on comparing after the '\0' */ zl1++; zl2++; s1 += zl1; rl1 -= zl1; s2 += zl2; rl2 -= zl2; } } } /* ** Check whether integer 'i' is less than float 'f'. If 'i' has an ** exact representation as a float ('l_intfitsf'), compare numbers as ** floats. Otherwise, use the equivalence 'i < f <=> i < ceil(f)'. ** If 'ceil(f)' is out of integer range, either 'f' is greater than ** all integers or less than all integers. ** (The test with 'l_intfitsf' is only for performance; the else ** case is correct for all values, but it is slow due to the conversion ** from float to int.) ** When 'f' is NaN, comparisons must result in false. */ l_sinline int LTintfloat (lua_Integer i, lua_Number f) { if (l_intfitsf(i)) return luai_numlt(cast_num(i), f); /* compare them as floats */ else { /* i < f <=> i < ceil(f) */ lua_Integer fi; if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */ return i < fi; /* compare them as integers */ else /* 'f' is either greater or less than all integers */ return f > 0; /* greater? */ } } /* ** Check whether integer 'i' is less than or equal to float 'f'. ** See comments on previous function. */ l_sinline int LEintfloat (lua_Integer i, lua_Number f) { if (l_intfitsf(i)) return luai_numle(cast_num(i), f); /* compare them as floats */ else { /* i <= f <=> i <= floor(f) */ lua_Integer fi; if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */ return i <= fi; /* compare them as integers */ else /* 'f' is either greater or less than all integers */ return f > 0; /* greater? */ } } /* ** Check whether float 'f' is less than integer 'i'. ** See comments on previous function. */ l_sinline int LTfloatint (lua_Number f, lua_Integer i) { if (l_intfitsf(i)) return luai_numlt(f, cast_num(i)); /* compare them as floats */ else { /* f < i <=> floor(f) < i */ lua_Integer fi; if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */ return fi < i; /* compare them as integers */ else /* 'f' is either greater or less than all integers */ return f < 0; /* less? */ } } /* ** Check whether float 'f' is less than or equal to integer 'i'. ** See comments on previous function. */ l_sinline int LEfloatint (lua_Number f, lua_Integer i) { if (l_intfitsf(i)) return luai_numle(f, cast_num(i)); /* compare them as floats */ else { /* f <= i <=> ceil(f) <= i */ lua_Integer fi; if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */ return fi <= i; /* compare them as integers */ else /* 'f' is either greater or less than all integers */ return f < 0; /* less? */ } } /* ** Return 'l < r', for numbers. */ l_sinline int LTnum (const TValue *l, const TValue *r) { lua_assert(ttisnumber(l) && ttisnumber(r)); if (ttisinteger(l)) { lua_Integer li = ivalue(l); if (ttisinteger(r)) return li < ivalue(r); /* both are integers */ else /* 'l' is int and 'r' is float */ return LTintfloat(li, fltvalue(r)); /* l < r ? */ } else { lua_Number lf = fltvalue(l); /* 'l' must be float */ if (ttisfloat(r)) return luai_numlt(lf, fltvalue(r)); /* both are float */ else /* 'l' is float and 'r' is int */ return LTfloatint(lf, ivalue(r)); } } /* ** Return 'l <= r', for numbers. */ l_sinline int LEnum (const TValue *l, const TValue *r) { lua_assert(ttisnumber(l) && ttisnumber(r)); if (ttisinteger(l)) { lua_Integer li = ivalue(l); if (ttisinteger(r)) return li <= ivalue(r); /* both are integers */ else /* 'l' is int and 'r' is float */ return LEintfloat(li, fltvalue(r)); /* l <= r ? */ } else { lua_Number lf = fltvalue(l); /* 'l' must be float */ if (ttisfloat(r)) return luai_numle(lf, fltvalue(r)); /* both are float */ else /* 'l' is float and 'r' is int */ return LEfloatint(lf, ivalue(r)); } } /* ** return 'l < r' for non-numbers. */ static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) { lua_assert(!ttisnumber(l) || !ttisnumber(r)); if (ttisstring(l) && ttisstring(r)) /* both are strings? */ return l_strcmp(tsvalue(l), tsvalue(r)) < 0; else return luaT_callorderTM(L, l, r, TM_LT); } /* ** Main operation less than; return 'l < r'. */ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ return LTnum(l, r); else return lessthanothers(L, l, r); } /* ** return 'l <= r' for non-numbers. */ static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) { lua_assert(!ttisnumber(l) || !ttisnumber(r)); if (ttisstring(l) && ttisstring(r)) /* both are strings? */ return l_strcmp(tsvalue(l), tsvalue(r)) <= 0; else return luaT_callorderTM(L, l, r, TM_LE); } /* ** Main operation less than or equal to; return 'l <= r'. */ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ return LEnum(l, r); else return lessequalothers(L, l, r); } /* ** Main operation for equality of Lua values; return 't1 == t2'. ** L == NULL means raw equality (no metamethods) */ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { const TValue *tm; if (ttype(t1) != ttype(t2)) /* not the same type? */ return 0; else if (ttypetag(t1) != ttypetag(t2)) { switch (ttypetag(t1)) { case LUA_VNUMINT: { /* integer == float? */ /* integer and float can only be equal if float has an integer value equal to the integer */ lua_Integer i2; return (luaV_flttointeger(fltvalue(t2), &i2, F2Ieq) && ivalue(t1) == i2); } case LUA_VNUMFLT: { /* float == integer? */ lua_Integer i1; /* see comment in previous case */ return (luaV_flttointeger(fltvalue(t1), &i1, F2Ieq) && i1 == ivalue(t2)); } case LUA_VSHRSTR: case LUA_VLNGSTR: { /* compare two strings with different variants: they can be equal when one string is a short string and the other is an external string */ return luaS_eqstr(tsvalue(t1), tsvalue(t2)); } default: /* only numbers (integer/float) and strings (long/short) can have equal values with different variants */ return 0; } } else { /* equal variants */ switch (ttypetag(t1)) { case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: return 1; case LUA_VNUMINT: return (ivalue(t1) == ivalue(t2)); case LUA_VNUMFLT: return (fltvalue(t1) == fltvalue(t2)); case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); case LUA_VSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); case LUA_VLNGSTR: return luaS_eqstr(tsvalue(t1), tsvalue(t2)); case LUA_VUSERDATA: { if (uvalue(t1) == uvalue(t2)) return 1; else if (L == NULL) return 0; tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); if (tm == NULL) tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); break; /* will try TM */ } case LUA_VTABLE: { if (hvalue(t1) == hvalue(t2)) return 1; else if (L == NULL) return 0; tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); if (tm == NULL) tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); break; /* will try TM */ } case LUA_VLCF: return (fvalue(t1) == fvalue(t2)); default: /* functions and threads */ return (gcvalue(t1) == gcvalue(t2)); } if (tm == NULL) /* no TM? */ return 0; /* objects are different */ else { int tag = luaT_callTMres(L, tm, t1, t2, L->top.p); /* call TM */ return !tagisfalse(tag); } } } /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */ #define tostring(L,o) \ (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1))) /* ** Check whether object is a short empty string to optimize concatenation. ** (External strings can be empty too; they will be concatenated like ** non-empty ones.) */ #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0) /* copy strings in stack from top - n up to top - 1 to buffer */ static void copy2buff (StkId top, int n, char *buff) { size_t tl = 0; /* size already copied */ do { TString *st = tsvalue(s2v(top - n)); size_t l; /* length of string being copied */ const char *s = getlstr(st, l); memcpy(buff + tl, s, l * sizeof(char)); tl += l; } while (--n > 0); } /* ** Main operation for concatenation: concat 'total' values in the stack, ** from 'L->top.p - total' up to 'L->top.p - 1'. */ void luaV_concat (lua_State *L, int total) { if (total == 1) return; /* "all" values already concatenated */ do { StkId top = L->top.p; int n = 2; /* number of elements handled in this pass (at least 2) */ if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) || !tostring(L, s2v(top - 1))) luaT_tryconcatTM(L); /* may invalidate 'top' */ else if (isemptystr(s2v(top - 1))) /* second operand is empty? */ cast_void(tostring(L, s2v(top - 2))); /* result is first operand */ else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */ setobjs2s(L, top - 2, top - 1); /* result is second op. */ } else { /* at least two string values; get as many as possible */ size_t tl = tsslen(tsvalue(s2v(top - 1))); /* total length */ TString *ts; /* collect total length and number of strings */ for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { size_t l = tsslen(tsvalue(s2v(top - n - 1))); if (l_unlikely(l >= MAX_SIZE - sizeof(TString) - tl)) { L->top.p = top - total; /* pop strings to avoid wasting stack */ luaG_runerror(L, "string length overflow"); } tl += l; } if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */ char buff[LUAI_MAXSHORTLEN]; copy2buff(top, n, buff); /* copy strings to buffer */ ts = luaS_newlstr(L, buff, tl); } else { /* long string; copy strings directly to final result */ ts = luaS_createlngstrobj(L, tl); copy2buff(top, n, getlngstr(ts)); } setsvalue2s(L, top - n, ts); /* create result */ } total -= n - 1; /* got 'n' strings to create one new */ L->top.p -= n - 1; /* popped 'n' strings and pushed one */ } while (total > 1); /* repeat until only 1 result left */ } /* ** Main operation 'ra = #rb'. */ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { const TValue *tm; switch (ttypetag(rb)) { case LUA_VTABLE: { Table *h = hvalue(rb); tm = fasttm(L, h->metatable, TM_LEN); if (tm) break; /* metamethod? break switch to call it */ setivalue(s2v(ra), l_castU2S(luaH_getn(L, h))); /* else primitive len */ return; } case LUA_VSHRSTR: { setivalue(s2v(ra), tsvalue(rb)->shrlen); return; } case LUA_VLNGSTR: { setivalue(s2v(ra), cast_st2S(tsvalue(rb)->u.lnglen)); return; } default: { /* try metamethod */ tm = luaT_gettmbyobj(L, rb, TM_LEN); if (l_unlikely(notm(tm))) /* no metamethod? */ luaG_typeerror(L, rb, "get length of"); break; } } luaT_callTMres(L, tm, rb, rb, ra); } /* ** Integer division; return 'm // n', that is, floor(m/n). ** C division truncates its result (rounds towards zero). ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer, ** otherwise 'floor(q) == trunc(q) - 1'. */ lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) { if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ if (n == 0) luaG_runerror(L, "attempt to divide by zero"); return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */ } else { lua_Integer q = m / n; /* perform C division */ if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */ q -= 1; /* correct result for different rounding */ return q; } } /* ** Integer modulus; return 'm % n'. (Assume that C '%' with ** negative operands follows C99 behavior. See previous comment ** about luaV_idiv.) */ lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ if (n == 0) luaG_runerror(L, "attempt to perform 'n%%0'"); return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ } else { lua_Integer r = m % n; if (r != 0 && (r ^ n) < 0) /* 'm/n' would be non-integer negative? */ r += n; /* correct result for different rounding */ return r; } } /* ** Float modulus */ lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) { lua_Number r; luai_nummod(L, m, n, r); return r; } /* number of bits in an integer */ #define NBITS l_numbits(lua_Integer) /* ** Shift left operation. (Shift right just negates 'y'.) */ lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { if (y < 0) { /* shift right? */ if (y <= -NBITS) return 0; else return intop(>>, x, -y); } else { /* shift left */ if (y >= NBITS) return 0; else return intop(<<, x, y); } } /* ** create a new Lua closure, push it in the stack, and initialize ** its upvalues. */ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, StkId ra) { int nup = p->sizeupvalues; Upvaldesc *uv = p->upvalues; int i; LClosure *ncl = luaF_newLclosure(L, nup); ncl->p = p; setclLvalue2s(L, ra, ncl); /* anchor new closure in stack */ for (i = 0; i < nup; i++) { /* fill in its upvalues */ if (uv[i].instack) /* upvalue refers to local variable? */ ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx); else /* get upvalue from enclosing function */ ncl->upvals[i] = encup[uv[i].idx]; luaC_objbarrier(L, ncl, ncl->upvals[i]); } } /* ** finish execution of an opcode interrupted by a yield */ void luaV_finishOp (lua_State *L) { CallInfo *ci = L->ci; StkId base = ci->func.p + 1; Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ OpCode op = GET_OPCODE(inst); switch (op) { /* finish its execution */ case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: { setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top.p); break; } case OP_UNM: case OP_BNOT: case OP_LEN: case OP_GETTABUP: case OP_GETTABLE: case OP_GETI: case OP_GETFIELD: case OP_SELF: { setobjs2s(L, base + GETARG_A(inst), --L->top.p); break; } case OP_LT: case OP_LE: case OP_LTI: case OP_LEI: case OP_GTI: case OP_GEI: case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */ int res = !l_isfalse(s2v(L->top.p - 1)); L->top.p--; lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); if (res != GETARG_k(inst)) /* condition failed? */ ci->u.l.savedpc++; /* skip jump instruction */ break; } case OP_CONCAT: { StkId top = L->top.p - 1; /* top when 'luaT_tryconcatTM' was called */ int a = GETARG_A(inst); /* first element to concatenate */ int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */ setobjs2s(L, top - 2, top); /* put TM result in proper position */ L->top.p = top - 1; /* top is one after last element (at top-2) */ luaV_concat(L, total); /* concat them (may yield again) */ break; } case OP_CLOSE: { /* yielded closing variables */ ci->u.l.savedpc--; /* repeat instruction to close other vars. */ break; } case OP_RETURN: { /* yielded closing variables */ StkId ra = base + GETARG_A(inst); /* adjust top to signal correct number of returns, in case the return is "up to top" ('isIT') */ L->top.p = ra + ci->u2.nres; /* repeat instruction to close other vars. and complete the return */ ci->u.l.savedpc--; break; } default: { /* only these other opcodes can yield */ lua_assert(op == OP_TFORCALL || op == OP_CALL || op == OP_TAILCALL || op == OP_SETTABUP || op == OP_SETTABLE || op == OP_SETI || op == OP_SETFIELD); break; } } } /* ** {================================================================== ** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute' ** ** All these macros are to be used exclusively inside the main ** iterpreter loop (function luaV_execute) and may access directly ** the local variables of that function (L, i, pc, ci, etc.). ** =================================================================== */ #define l_addi(L,a,b) intop(+, a, b) #define l_subi(L,a,b) intop(-, a, b) #define l_muli(L,a,b) intop(*, a, b) #define l_band(a,b) intop(&, a, b) #define l_bor(a,b) intop(|, a, b) #define l_bxor(a,b) intop(^, a, b) #define l_lti(a,b) (a < b) #define l_lei(a,b) (a <= b) #define l_gti(a,b) (a > b) #define l_gei(a,b) (a >= b) /* ** Arithmetic operations with immediate operands. 'iop' is the integer ** operation, 'fop' is the float operation. */ #define op_arithI(L,iop,fop) { \ TValue *ra = vRA(i); \ TValue *v1 = vRB(i); \ int imm = GETARG_sC(i); \ if (ttisinteger(v1)) { \ lua_Integer iv1 = ivalue(v1); \ pc++; setivalue(ra, iop(L, iv1, imm)); \ } \ else if (ttisfloat(v1)) { \ lua_Number nb = fltvalue(v1); \ lua_Number fimm = cast_num(imm); \ pc++; setfltvalue(ra, fop(L, nb, fimm)); \ }} /* ** Auxiliary function for arithmetic operations over floats and others ** with two operands. */ #define op_arithf_aux(L,v1,v2,fop) { \ lua_Number n1; lua_Number n2; \ if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ StkId ra = RA(i); \ pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ }} /* ** Arithmetic operations over floats and others with register operands. */ #define op_arithf(L,fop) { \ TValue *v1 = vRB(i); \ TValue *v2 = vRC(i); \ op_arithf_aux(L, v1, v2, fop); } /* ** Arithmetic operations with K operands for floats. */ #define op_arithfK(L,fop) { \ TValue *v1 = vRB(i); \ TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ op_arithf_aux(L, v1, v2, fop); } /* ** Arithmetic operations over integers and floats. */ #define op_arith_aux(L,v1,v2,iop,fop) { \ if (ttisinteger(v1) && ttisinteger(v2)) { \ StkId ra = RA(i); \ lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ } \ else op_arithf_aux(L, v1, v2, fop); } /* ** Arithmetic operations with register operands. */ #define op_arith(L,iop,fop) { \ TValue *v1 = vRB(i); \ TValue *v2 = vRC(i); \ op_arith_aux(L, v1, v2, iop, fop); } /* ** Arithmetic operations with K operands. */ #define op_arithK(L,iop,fop) { \ TValue *v1 = vRB(i); \ TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ op_arith_aux(L, v1, v2, iop, fop); } /* ** Bitwise operations with constant operand. */ #define op_bitwiseK(L,op) { \ TValue *v1 = vRB(i); \ TValue *v2 = KC(i); \ lua_Integer i1; \ lua_Integer i2 = ivalue(v2); \ if (tointegerns(v1, &i1)) { \ StkId ra = RA(i); \ pc++; setivalue(s2v(ra), op(i1, i2)); \ }} /* ** Bitwise operations with register operands. */ #define op_bitwise(L,op) { \ TValue *v1 = vRB(i); \ TValue *v2 = vRC(i); \ lua_Integer i1; lua_Integer i2; \ if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \ StkId ra = RA(i); \ pc++; setivalue(s2v(ra), op(i1, i2)); \ }} /* ** Order operations with register operands. 'opn' actually works ** for all numbers, but the fast track improves performance for ** integers. */ #define op_order(L,opi,opn,other) { \ TValue *ra = vRA(i); \ int cond; \ TValue *rb = vRB(i); \ if (ttisinteger(ra) && ttisinteger(rb)) { \ lua_Integer ia = ivalue(ra); \ lua_Integer ib = ivalue(rb); \ cond = opi(ia, ib); \ } \ else if (ttisnumber(ra) && ttisnumber(rb)) \ cond = opn(ra, rb); \ else \ Protect(cond = other(L, ra, rb)); \ docondjump(); } /* ** Order operations with immediate operand. (Immediate operand is ** always small enough to have an exact representation as a float.) */ #define op_orderI(L,opi,opf,inv,tm) { \ TValue *ra = vRA(i); \ int cond; \ int im = GETARG_sB(i); \ if (ttisinteger(ra)) \ cond = opi(ivalue(ra), im); \ else if (ttisfloat(ra)) { \ lua_Number fa = fltvalue(ra); \ lua_Number fim = cast_num(im); \ cond = opf(fa, fim); \ } \ else { \ int isf = GETARG_C(i); \ Protect(cond = luaT_callorderiTM(L, ra, im, inv, isf, tm)); \ } \ docondjump(); } /* }================================================================== */ /* ** {================================================================== ** Function 'luaV_execute': main interpreter loop ** =================================================================== */ /* ** some macros for common tasks in 'luaV_execute' */ #define RA(i) (base+GETARG_A(i)) #define vRA(i) s2v(RA(i)) #define RB(i) (base+GETARG_B(i)) #define vRB(i) s2v(RB(i)) #define KB(i) (k+GETARG_B(i)) #define RC(i) (base+GETARG_C(i)) #define vRC(i) s2v(RC(i)) #define KC(i) (k+GETARG_C(i)) #define RKC(i) ((TESTARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i))) #define updatetrap(ci) (trap = ci->u.l.trap) #define updatebase(ci) (base = ci->func.p + 1) #define updatestack(ci) \ { if (l_unlikely(trap)) { updatebase(ci); ra = RA(i); } } /* ** Execute a jump instruction. The 'updatetrap' allows signals to stop ** tight loops. (Without it, the local copy of 'trap' could never change.) */ #define dojump(ci,i,e) { pc += GETARG_sJ(i) + e; updatetrap(ci); } /* for test instructions, execute the jump instruction that follows it */ #define donextjump(ci) { Instruction ni = *pc; dojump(ci, ni, 1); } /* ** do a conditional jump: skip next instruction if 'cond' is not what ** was expected (parameter 'k'), else do next instruction, which must ** be a jump. */ #define docondjump() if (cond != GETARG_k(i)) pc++; else donextjump(ci); /* ** Correct global 'pc'. */ #define savepc(ci) (ci->u.l.savedpc = pc) /* ** Whenever code can raise errors, the global 'pc' and the global ** 'top' must be correct to report occasional errors. */ #define savestate(L,ci) (savepc(ci), L->top.p = ci->top.p) /* ** Protect code that, in general, can raise errors, reallocate the ** stack, and change the hooks. */ #define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci)) /* special version that does not change the top */ #define ProtectNT(exp) (savepc(ci), (exp), updatetrap(ci)) /* ** Protect code that can only raise errors. (That is, it cannot change ** the stack or hooks.) */ #define halfProtect(exp) (savestate(L,ci), (exp)) /* ** macro executed during Lua functions at points where the ** function can yield. */ #if !defined(luai_threadyield) #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} #endif /* 'c' is the limit of live values in the stack */ #define checkGC(L,c) \ { luaC_condGC(L, (savepc(ci), L->top.p = (c)), \ updatetrap(ci)); \ luai_threadyield(L); } /* fetch an instruction and prepare its execution */ #define vmfetch() { \ if (l_unlikely(trap)) { /* stack reallocation or hooks? */ \ trap = luaG_traceexec(L, pc); /* handle hooks */ \ updatebase(ci); /* correct stack */ \ } \ i = *(pc++); \ } #define vmdispatch(o) switch(o) #define vmcase(l) case l: #define vmbreak break void luaV_execute (lua_State *L, CallInfo *ci) { LClosure *cl; TValue *k; StkId base; const Instruction *pc; int trap; #if LUA_USE_JUMPTABLE #include "ljumptab.h" #endif startfunc: trap = L->hookmask; returning: /* trap already set */ cl = ci_func(ci); k = cl->p->k; pc = ci->u.l.savedpc; if (l_unlikely(trap)) trap = luaG_tracecall(L); base = ci->func.p + 1; /* main loop of interpreter */ for (;;) { Instruction i; /* instruction being executed */ vmfetch(); #if 0 { /* low-level line tracing for debugging Lua */ #include "lopnames.h" int pcrel = pcRel(pc, cl->p); printf("line: %d; %s (%d)\n", luaG_getfuncline(cl->p, pcrel), opnames[GET_OPCODE(i)], pcrel); } #endif lua_assert(base == ci->func.p + 1); lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p); /* for tests, invalidate top for instructions not expecting it */ lua_assert(luaP_isIT(i) || (cast_void(L->top.p = base), 1)); vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { StkId ra = RA(i); setobjs2s(L, ra, RB(i)); vmbreak; } vmcase(OP_LOADI) { StkId ra = RA(i); lua_Integer b = GETARG_sBx(i); setivalue(s2v(ra), b); vmbreak; } vmcase(OP_LOADF) { StkId ra = RA(i); int b = GETARG_sBx(i); setfltvalue(s2v(ra), cast_num(b)); vmbreak; } vmcase(OP_LOADK) { StkId ra = RA(i); TValue *rb = k + GETARG_Bx(i); setobj2s(L, ra, rb); vmbreak; } vmcase(OP_LOADKX) { StkId ra = RA(i); TValue *rb; rb = k + GETARG_Ax(*pc); pc++; setobj2s(L, ra, rb); vmbreak; } vmcase(OP_LOADFALSE) { StkId ra = RA(i); setbfvalue(s2v(ra)); vmbreak; } vmcase(OP_LFALSESKIP) { StkId ra = RA(i); setbfvalue(s2v(ra)); pc++; /* skip next instruction */ vmbreak; } vmcase(OP_LOADTRUE) { StkId ra = RA(i); setbtvalue(s2v(ra)); vmbreak; } vmcase(OP_LOADNIL) { StkId ra = RA(i); int b = GETARG_B(i); do { setnilvalue(s2v(ra++)); } while (b--); vmbreak; } vmcase(OP_GETUPVAL) { StkId ra = RA(i); int b = GETARG_B(i); setobj2s(L, ra, cl->upvals[b]->v.p); vmbreak; } vmcase(OP_SETUPVAL) { StkId ra = RA(i); UpVal *uv = cl->upvals[GETARG_B(i)]; setobj(L, uv->v.p, s2v(ra)); luaC_barrier(L, uv, s2v(ra)); vmbreak; } vmcase(OP_GETTABUP) { StkId ra = RA(i); TValue *upval = cl->upvals[GETARG_B(i)]->v.p; TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a short string */ lu_byte tag; luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, tag); if (tagisempty(tag)) Protect(luaV_finishget(L, upval, rc, ra, tag)); vmbreak; } vmcase(OP_GETTABLE) { StkId ra = RA(i); TValue *rb = vRB(i); TValue *rc = vRC(i); lu_byte tag; if (ttisinteger(rc)) { /* fast track for integers? */ luaV_fastgeti(rb, ivalue(rc), s2v(ra), tag); } else luaV_fastget(rb, rc, s2v(ra), luaH_get, tag); if (tagisempty(tag)) Protect(luaV_finishget(L, rb, rc, ra, tag)); vmbreak; } vmcase(OP_GETI) { StkId ra = RA(i); TValue *rb = vRB(i); int c = GETARG_C(i); lu_byte tag; luaV_fastgeti(rb, c, s2v(ra), tag); if (tagisempty(tag)) { TValue key; setivalue(&key, c); Protect(luaV_finishget(L, rb, &key, ra, tag)); } vmbreak; } vmcase(OP_GETFIELD) { StkId ra = RA(i); TValue *rb = vRB(i); TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a short string */ lu_byte tag; luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag); if (tagisempty(tag)) Protect(luaV_finishget(L, rb, rc, ra, tag)); vmbreak; } vmcase(OP_SETTABUP) { int hres; TValue *upval = cl->upvals[GETARG_A(i)]->v.p; TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a short string */ luaV_fastset(upval, key, rc, hres, luaH_psetshortstr); if (hres == HOK) luaV_finishfastset(L, upval, rc); else Protect(luaV_finishset(L, upval, rb, rc, hres)); vmbreak; } vmcase(OP_SETTABLE) { StkId ra = RA(i); int hres; TValue *rb = vRB(i); /* key (table is in 'ra') */ TValue *rc = RKC(i); /* value */ if (ttisinteger(rb)) { /* fast track for integers? */ luaV_fastseti(s2v(ra), ivalue(rb), rc, hres); } else { luaV_fastset(s2v(ra), rb, rc, hres, luaH_pset); } if (hres == HOK) luaV_finishfastset(L, s2v(ra), rc); else Protect(luaV_finishset(L, s2v(ra), rb, rc, hres)); vmbreak; } vmcase(OP_SETI) { StkId ra = RA(i); int hres; int b = GETARG_B(i); TValue *rc = RKC(i); luaV_fastseti(s2v(ra), b, rc, hres); if (hres == HOK) luaV_finishfastset(L, s2v(ra), rc); else { TValue key; setivalue(&key, b); Protect(luaV_finishset(L, s2v(ra), &key, rc, hres)); } vmbreak; } vmcase(OP_SETFIELD) { StkId ra = RA(i); int hres; TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a short string */ luaV_fastset(s2v(ra), key, rc, hres, luaH_psetshortstr); if (hres == HOK) luaV_finishfastset(L, s2v(ra), rc); else Protect(luaV_finishset(L, s2v(ra), rb, rc, hres)); vmbreak; } vmcase(OP_NEWTABLE) { StkId ra = RA(i); unsigned b = cast_uint(GETARG_vB(i)); /* log2(hash size) + 1 */ unsigned c = cast_uint(GETARG_vC(i)); /* array size */ Table *t; if (b > 0) b = 1u << (b - 1); /* hash size is 2^(b - 1) */ if (TESTARG_k(i)) { /* non-zero extra argument? */ lua_assert(GETARG_Ax(*pc) != 0); /* add it to array size */ c += cast_uint(GETARG_Ax(*pc)) * (MAXARG_vC + 1); } pc++; /* skip extra argument */ L->top.p = ra + 1; /* correct top in case of emergency GC */ t = luaH_new(L); /* memory allocation */ sethvalue2s(L, ra, t); if (b != 0 || c != 0) luaH_resize(L, t, c, b); /* idem */ checkGC(L, ra + 1); vmbreak; } vmcase(OP_SELF) { StkId ra = RA(i); lu_byte tag; TValue *rb = vRB(i); TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a short string */ setobj2s(L, ra + 1, rb); luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag); if (tagisempty(tag)) Protect(luaV_finishget(L, rb, rc, ra, tag)); vmbreak; } vmcase(OP_ADDI) { op_arithI(L, l_addi, luai_numadd); vmbreak; } vmcase(OP_ADDK) { op_arithK(L, l_addi, luai_numadd); vmbreak; } vmcase(OP_SUBK) { op_arithK(L, l_subi, luai_numsub); vmbreak; } vmcase(OP_MULK) { op_arithK(L, l_muli, luai_nummul); vmbreak; } vmcase(OP_MODK) { savestate(L, ci); /* in case of division by 0 */ op_arithK(L, luaV_mod, luaV_modf); vmbreak; } vmcase(OP_POWK) { op_arithfK(L, luai_numpow); vmbreak; } vmcase(OP_DIVK) { op_arithfK(L, luai_numdiv); vmbreak; } vmcase(OP_IDIVK) { savestate(L, ci); /* in case of division by 0 */ op_arithK(L, luaV_idiv, luai_numidiv); vmbreak; } vmcase(OP_BANDK) { op_bitwiseK(L, l_band); vmbreak; } vmcase(OP_BORK) { op_bitwiseK(L, l_bor); vmbreak; } vmcase(OP_BXORK) { op_bitwiseK(L, l_bxor); vmbreak; } vmcase(OP_SHLI) { StkId ra = RA(i); TValue *rb = vRB(i); int ic = GETARG_sC(i); lua_Integer ib; if (tointegerns(rb, &ib)) { pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib)); } vmbreak; } vmcase(OP_SHRI) { StkId ra = RA(i); TValue *rb = vRB(i); int ic = GETARG_sC(i); lua_Integer ib; if (tointegerns(rb, &ib)) { pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic)); } vmbreak; } vmcase(OP_ADD) { op_arith(L, l_addi, luai_numadd); vmbreak; } vmcase(OP_SUB) { op_arith(L, l_subi, luai_numsub); vmbreak; } vmcase(OP_MUL) { op_arith(L, l_muli, luai_nummul); vmbreak; } vmcase(OP_MOD) { savestate(L, ci); /* in case of division by 0 */ op_arith(L, luaV_mod, luaV_modf); vmbreak; } vmcase(OP_POW) { op_arithf(L, luai_numpow); vmbreak; } vmcase(OP_DIV) { /* float division (always with floats) */ op_arithf(L, luai_numdiv); vmbreak; } vmcase(OP_IDIV) { /* floor division */ savestate(L, ci); /* in case of division by 0 */ op_arith(L, luaV_idiv, luai_numidiv); vmbreak; } vmcase(OP_BAND) { op_bitwise(L, l_band); vmbreak; } vmcase(OP_BOR) { op_bitwise(L, l_bor); vmbreak; } vmcase(OP_BXOR) { op_bitwise(L, l_bxor); vmbreak; } vmcase(OP_SHL) { op_bitwise(L, luaV_shiftl); vmbreak; } vmcase(OP_SHR) { op_bitwise(L, luaV_shiftr); vmbreak; } vmcase(OP_MMBIN) { StkId ra = RA(i); Instruction pi = *(pc - 2); /* original arith. expression */ TValue *rb = vRB(i); TMS tm = (TMS)GETARG_C(i); StkId result = RA(pi); lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR); Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm)); vmbreak; } vmcase(OP_MMBINI) { StkId ra = RA(i); Instruction pi = *(pc - 2); /* original arith. expression */ int imm = GETARG_sB(i); TMS tm = (TMS)GETARG_C(i); int flip = GETARG_k(i); StkId result = RA(pi); Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm)); vmbreak; } vmcase(OP_MMBINK) { StkId ra = RA(i); Instruction pi = *(pc - 2); /* original arith. expression */ TValue *imm = KB(i); TMS tm = (TMS)GETARG_C(i); int flip = GETARG_k(i); StkId result = RA(pi); Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm)); vmbreak; } vmcase(OP_UNM) { StkId ra = RA(i); TValue *rb = vRB(i); lua_Number nb; if (ttisinteger(rb)) { lua_Integer ib = ivalue(rb); setivalue(s2v(ra), intop(-, 0, ib)); } else if (tonumberns(rb, nb)) { setfltvalue(s2v(ra), luai_numunm(L, nb)); } else Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); vmbreak; } vmcase(OP_BNOT) { StkId ra = RA(i); TValue *rb = vRB(i); lua_Integer ib; if (tointegerns(rb, &ib)) { setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib)); } else Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); vmbreak; } vmcase(OP_NOT) { StkId ra = RA(i); TValue *rb = vRB(i); if (l_isfalse(rb)) setbtvalue(s2v(ra)); else setbfvalue(s2v(ra)); vmbreak; } vmcase(OP_LEN) { StkId ra = RA(i); Protect(luaV_objlen(L, ra, vRB(i))); vmbreak; } vmcase(OP_CONCAT) { StkId ra = RA(i); int n = GETARG_B(i); /* number of elements to concatenate */ L->top.p = ra + n; /* mark the end of concat operands */ ProtectNT(luaV_concat(L, n)); checkGC(L, L->top.p); /* 'luaV_concat' ensures correct top */ vmbreak; } vmcase(OP_CLOSE) { StkId ra = RA(i); lua_assert(!GETARG_B(i)); /* 'close must be alive */ Protect(luaF_close(L, ra, LUA_OK, 1)); vmbreak; } vmcase(OP_TBC) { StkId ra = RA(i); /* create new to-be-closed upvalue */ halfProtect(luaF_newtbcupval(L, ra)); vmbreak; } vmcase(OP_JMP) { dojump(ci, i, 0); vmbreak; } vmcase(OP_EQ) { StkId ra = RA(i); int cond; TValue *rb = vRB(i); Protect(cond = luaV_equalobj(L, s2v(ra), rb)); docondjump(); vmbreak; } vmcase(OP_LT) { op_order(L, l_lti, LTnum, lessthanothers); vmbreak; } vmcase(OP_LE) { op_order(L, l_lei, LEnum, lessequalothers); vmbreak; } vmcase(OP_EQK) { StkId ra = RA(i); TValue *rb = KB(i); /* basic types do not use '__eq'; we can use raw equality */ int cond = luaV_rawequalobj(s2v(ra), rb); docondjump(); vmbreak; } vmcase(OP_EQI) { StkId ra = RA(i); int cond; int im = GETARG_sB(i); if (ttisinteger(s2v(ra))) cond = (ivalue(s2v(ra)) == im); else if (ttisfloat(s2v(ra))) cond = luai_numeq(fltvalue(s2v(ra)), cast_num(im)); else cond = 0; /* other types cannot be equal to a number */ docondjump(); vmbreak; } vmcase(OP_LTI) { op_orderI(L, l_lti, luai_numlt, 0, TM_LT); vmbreak; } vmcase(OP_LEI) { op_orderI(L, l_lei, luai_numle, 0, TM_LE); vmbreak; } vmcase(OP_GTI) { op_orderI(L, l_gti, luai_numgt, 1, TM_LT); vmbreak; } vmcase(OP_GEI) { op_orderI(L, l_gei, luai_numge, 1, TM_LE); vmbreak; } vmcase(OP_TEST) { StkId ra = RA(i); int cond = !l_isfalse(s2v(ra)); docondjump(); vmbreak; } vmcase(OP_TESTSET) { StkId ra = RA(i); TValue *rb = vRB(i); if (l_isfalse(rb) == GETARG_k(i)) pc++; else { setobj2s(L, ra, rb); donextjump(ci); } vmbreak; } vmcase(OP_CALL) { StkId ra = RA(i); CallInfo *newci; int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; if (b != 0) /* fixed number of arguments? */ L->top.p = ra + b; /* top signals number of arguments */ /* else previous instruction set top */ savepc(ci); /* in case of errors */ if ((newci = luaD_precall(L, ra, nresults)) == NULL) updatetrap(ci); /* C call; nothing else to be done */ else { /* Lua call: run function in this same C frame */ ci = newci; goto startfunc; } vmbreak; } vmcase(OP_TAILCALL) { StkId ra = RA(i); int b = GETARG_B(i); /* number of arguments + 1 (function) */ int n; /* number of results when calling a C function */ int nparams1 = GETARG_C(i); /* delta is virtual 'func' - real 'func' (vararg functions) */ int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; if (b != 0) L->top.p = ra + b; else /* previous instruction set top */ b = cast_int(L->top.p - ra); savepc(ci); /* several calls here can raise errors */ if (TESTARG_k(i)) { luaF_closeupval(L, base); /* close upvalues from current call */ lua_assert(L->tbclist.p < base); /* no pending tbc variables */ lua_assert(base == ci->func.p + 1); } if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0) /* Lua function? */ goto startfunc; /* execute the callee */ else { /* C function? */ ci->func.p -= delta; /* restore 'func' (if vararg) */ luaD_poscall(L, ci, n); /* finish caller */ updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; /* caller returns after the tail call */ } } vmcase(OP_RETURN) { StkId ra = RA(i); int n = GETARG_B(i) - 1; /* number of results */ int nparams1 = GETARG_C(i); if (n < 0) /* not fixed? */ n = cast_int(L->top.p - ra); /* get what is available */ savepc(ci); if (TESTARG_k(i)) { /* may there be open upvalues? */ ci->u2.nres = n; /* save number of returns */ if (L->top.p < ci->top.p) L->top.p = ci->top.p; luaF_close(L, base, CLOSEKTOP, 1); updatetrap(ci); updatestack(ci); } if (nparams1) /* vararg function? */ ci->func.p -= ci->u.l.nextraargs + nparams1; L->top.p = ra + n; /* set call for 'luaD_poscall' */ luaD_poscall(L, ci, n); updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; } vmcase(OP_RETURN0) { if (l_unlikely(L->hookmask)) { StkId ra = RA(i); L->top.p = ra; savepc(ci); luaD_poscall(L, ci, 0); /* no hurry... */ trap = 1; } else { /* do the 'poscall' here */ int nres = get_nresults(ci->callstatus); L->ci = ci->previous; /* back to caller */ L->top.p = base - 1; for (; l_unlikely(nres > 0); nres--) setnilvalue(s2v(L->top.p++)); /* all results are nil */ } goto ret; } vmcase(OP_RETURN1) { if (l_unlikely(L->hookmask)) { StkId ra = RA(i); L->top.p = ra + 1; savepc(ci); luaD_poscall(L, ci, 1); /* no hurry... */ trap = 1; } else { /* do the 'poscall' here */ int nres = get_nresults(ci->callstatus); L->ci = ci->previous; /* back to caller */ if (nres == 0) L->top.p = base - 1; /* asked for no results */ else { StkId ra = RA(i); setobjs2s(L, base - 1, ra); /* at least this result */ L->top.p = base; for (; l_unlikely(nres > 1); nres--) setnilvalue(s2v(L->top.p++)); /* complete missing results */ } } ret: /* return from a Lua function */ if (ci->callstatus & CIST_FRESH) return; /* end this frame */ else { ci = ci->previous; goto returning; /* continue running caller in this frame */ } } vmcase(OP_FORLOOP) { StkId ra = RA(i); if (ttisinteger(s2v(ra + 1))) { /* integer loop? */ lua_Unsigned count = l_castS2U(ivalue(s2v(ra))); if (count > 0) { /* still more iterations? */ lua_Integer step = ivalue(s2v(ra + 1)); lua_Integer idx = ivalue(s2v(ra + 2)); /* control variable */ chgivalue(s2v(ra), l_castU2S(count - 1)); /* update counter */ idx = intop(+, idx, step); /* add step to index */ chgivalue(s2v(ra + 2), idx); /* update control variable */ pc -= GETARG_Bx(i); /* jump back */ } } else if (floatforloop(ra)) /* float loop */ pc -= GETARG_Bx(i); /* jump back */ updatetrap(ci); /* allows a signal to break the loop */ vmbreak; } vmcase(OP_FORPREP) { StkId ra = RA(i); savestate(L, ci); /* in case of errors */ if (forprep(L, ra)) pc += GETARG_Bx(i) + 1; /* skip the loop */ vmbreak; } vmcase(OP_TFORPREP) { /* before: 'ra' has the iterator function, 'ra + 1' has the state, 'ra + 2' has the initial value for the control variable, and 'ra + 3' has the closing variable. This opcode then swaps the control and the closing variables and marks the closing variable as to-be-closed. */ StkId ra = RA(i); TValue temp; /* to swap control and closing variables */ setobj(L, &temp, s2v(ra + 3)); setobjs2s(L, ra + 3, ra + 2); setobj2s(L, ra + 2, &temp); /* create to-be-closed upvalue (if closing var. is not nil) */ halfProtect(luaF_newtbcupval(L, ra + 2)); pc += GETARG_Bx(i); /* go to end of the loop */ i = *(pc++); /* fetch next instruction */ lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i)); goto l_tforcall; } vmcase(OP_TFORCALL) { l_tforcall: { /* 'ra' has the iterator function, 'ra + 1' has the state, 'ra + 2' has the closing variable, and 'ra + 3' has the control variable. The call will use the stack starting at 'ra + 3', so that it preserves the first three values, and the first return will be the new value for the control variable. */ StkId ra = RA(i); setobjs2s(L, ra + 5, ra + 3); /* copy the control variable */ setobjs2s(L, ra + 4, ra + 1); /* copy state */ setobjs2s(L, ra + 3, ra); /* copy function */ L->top.p = ra + 3 + 3; ProtectNT(luaD_call(L, ra + 3, GETARG_C(i))); /* do the call */ updatestack(ci); /* stack may have changed */ i = *(pc++); /* go to next instruction */ lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i)); goto l_tforloop; }} vmcase(OP_TFORLOOP) { l_tforloop: { StkId ra = RA(i); if (!ttisnil(s2v(ra + 3))) /* continue loop? */ pc -= GETARG_Bx(i); /* jump back */ vmbreak; }} vmcase(OP_SETLIST) { StkId ra = RA(i); unsigned n = cast_uint(GETARG_vB(i)); unsigned last = cast_uint(GETARG_vC(i)); Table *h = hvalue(s2v(ra)); if (n == 0) n = cast_uint(L->top.p - ra) - 1; /* get up to the top */ else L->top.p = ci->top.p; /* correct top in case of emergency GC */ last += n; if (TESTARG_k(i)) { last += cast_uint(GETARG_Ax(*pc)) * (MAXARG_vC + 1); pc++; } /* when 'n' is known, table should have proper size */ if (last > h->asize) { /* needs more space? */ /* fixed-size sets should have space preallocated */ lua_assert(GETARG_vB(i) == 0); luaH_resizearray(L, h, last); /* preallocate it at once */ } for (; n > 0; n--) { TValue *val = s2v(ra + n); obj2arr(h, last - 1, val); last--; luaC_barrierback(L, obj2gco(h), val); } vmbreak; } vmcase(OP_CLOSURE) { StkId ra = RA(i); Proto *p = cl->p->p[GETARG_Bx(i)]; halfProtect(pushclosure(L, p, cl->upvals, base, ra)); checkGC(L, ra + 1); vmbreak; } vmcase(OP_VARARG) { StkId ra = RA(i); int n = GETARG_C(i) - 1; /* required results (-1 means all) */ int vatab = GETARG_k(i) ? GETARG_B(i) : -1; Protect(luaT_getvarargs(L, ci, ra, n, vatab)); vmbreak; } vmcase(OP_GETVARG) { StkId ra = RA(i); TValue *rc = vRC(i); luaT_getvararg(ci, ra, rc); vmbreak; } vmcase(OP_ERRNNIL) { TValue *ra = vRA(i); if (!ttisnil(ra)) halfProtect(luaG_errnnil(L, cl, GETARG_Bx(i))); vmbreak; } vmcase(OP_VARARGPREP) { ProtectNT(luaT_adjustvarargs(L, ci, cl->p)); if (l_unlikely(trap)) { /* previous "Protect" updated trap */ luaD_hookcall(L, ci); L->oldpc = 1; /* next opcode will be seen as a "new" line */ } updatebase(ci); /* function has new base after adjustment */ vmbreak; } vmcase(OP_EXTRAARG) { lua_assert(0); vmbreak; } } } } /* }================================================================== */ ================================================ FILE: 3rd/lua/lvm.h ================================================ /* ** $Id: lvm.h $ ** Lua virtual machine ** See Copyright Notice in lua.h */ #ifndef lvm_h #define lvm_h #include "ldo.h" #include "lobject.h" #include "ltm.h" #if !defined(LUA_NOCVTN2S) #define cvt2str(o) ttisnumber(o) #else #define cvt2str(o) 0 /* no conversion from numbers to strings */ #endif #if !defined(LUA_NOCVTS2N) #define cvt2num(o) ttisstring(o) #else #define cvt2num(o) 0 /* no conversion from strings to numbers */ #endif /* ** You can define LUA_FLOORN2I if you want to convert floats to integers ** by flooring them (instead of raising an error if they are not ** integral values) */ #if !defined(LUA_FLOORN2I) #define LUA_FLOORN2I F2Ieq #endif /* ** Rounding modes for float->integer coercion */ typedef enum { F2Ieq, /* no rounding; accepts only integral values */ F2Ifloor, /* takes the floor of the number */ F2Iceil /* takes the ceiling of the number */ } F2Imod; /* convert an object to a float (including string coercion) */ #define tonumber(o,n) \ (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) /* convert an object to a float (without string coercion) */ #define tonumberns(o,n) \ (ttisfloat(o) ? ((n) = fltvalue(o), 1) : \ (ttisinteger(o) ? ((n) = cast_num(ivalue(o)), 1) : 0)) /* convert an object to an integer (including string coercion) */ #define tointeger(o,i) \ (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \ : luaV_tointeger(o,i,LUA_FLOORN2I)) /* convert an object to an integer (without string coercion) */ #define tointegerns(o,i) \ (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \ : luaV_tointegerns(o,i,LUA_FLOORN2I)) #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) /* ** fast track for 'gettable' */ #define luaV_fastget(t,k,res,f, tag) \ (tag = (!ttistable(t) ? LUA_VNOTABLE : f(hvalue(t), k, res))) /* ** Special case of 'luaV_fastget' for integers, inlining the fast case ** of 'luaH_getint'. */ #define luaV_fastgeti(t,k,res,tag) \ if (!ttistable(t)) tag = LUA_VNOTABLE; \ else { luaH_fastgeti(hvalue(t), k, res, tag); } #define luaV_fastset(t,k,val,hres,f) \ (hres = ((!ttistable(t) || isshared(hvalue(t))) ? HNOTATABLE : f(hvalue(t), k, val))) #define luaV_fastseti(t,k,val,hres) \ if (!ttistable(t) || isshared(hvalue(t))) hres = HNOTATABLE; \ else { luaH_fastseti(hvalue(t), k, val, hres); } /* ** Finish a fast set operation (when fast set succeeds). */ #define luaV_finishfastset(L,t,v) luaC_barrierback(L, gcvalue(t), v) /* ** Shift right is the same as shift left with a negative 'y' */ #define luaV_shiftr(x,y) luaV_shiftl(x,intop(-, 0, y)) LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode); LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode); LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode); LUAI_FUNC lu_byte luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, lu_byte tag); LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, TValue *val, int aux); LUAI_FUNC void luaV_finishOp (lua_State *L); LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci); LUAI_FUNC void luaV_concat (lua_State *L, int total); LUAI_FUNC lua_Integer luaV_idiv (lua_State *L, lua_Integer x, lua_Integer y); LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); LUAI_FUNC lua_Number luaV_modf (lua_State *L, lua_Number x, lua_Number y); LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); #endif ================================================ FILE: 3rd/lua/lzio.c ================================================ /* ** $Id: lzio.c $ ** Buffered streams ** See Copyright Notice in lua.h */ #define lzio_c #define LUA_CORE #include "lprefix.h" #include #include "lua.h" #include "lapi.h" #include "llimits.h" #include "lmem.h" #include "lstate.h" #include "lzio.h" int luaZ_fill (ZIO *z) { size_t size; lua_State *L = z->L; const char *buff; lua_unlock(L); buff = z->reader(L, z->data, &size); lua_lock(L); if (buff == NULL || size == 0) return EOZ; z->n = size - 1; /* discount char being returned */ z->p = buff; return cast_uchar(*(z->p++)); } void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { z->L = L; z->reader = reader; z->data = data; z->n = 0; z->p = NULL; } /* --------------------------------------------------------------- read --- */ static int checkbuffer (ZIO *z) { if (z->n == 0) { /* no bytes in buffer? */ if (luaZ_fill(z) == EOZ) /* try to read more */ return 0; /* no more input */ else { z->n++; /* luaZ_fill consumed first byte; put it back */ z->p--; } } return 1; /* now buffer has something */ } size_t luaZ_read (ZIO *z, void *b, size_t n) { while (n) { size_t m; if (!checkbuffer(z)) return n; /* no more input; return number of missing bytes */ m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ memcpy(b, z->p, m); z->n -= m; z->p += m; b = (char *)b + m; n -= m; } return 0; } const void *luaZ_getaddr (ZIO* z, size_t n) { const void *res; if (!checkbuffer(z)) return NULL; /* no more input */ if (z->n < n) /* not enough bytes? */ return NULL; /* block not whole; cannot give an address */ res = z->p; /* get block address */ z->n -= n; /* consume these bytes */ z->p += n; return res; } ================================================ FILE: 3rd/lua/lzio.h ================================================ /* ** $Id: lzio.h $ ** Buffered streams ** See Copyright Notice in lua.h */ #ifndef lzio_h #define lzio_h #include "lua.h" #include "lmem.h" #define EOZ (-1) /* end of stream */ typedef struct Zio ZIO; #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) typedef struct Mbuffer { char *buffer; size_t n; size_t buffsize; } Mbuffer; #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) #define luaZ_buffer(buff) ((buff)->buffer) #define luaZ_sizebuffer(buff) ((buff)->buffsize) #define luaZ_bufflen(buff) ((buff)->n) #define luaZ_buffremove(buff,i) ((buff)->n -= cast_sizet(i)) #define luaZ_resetbuffer(buff) ((buff)->n = 0) #define luaZ_resizebuffer(L, buff, size) \ ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ (buff)->buffsize, size), \ (buff)->buffsize = size) #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data); LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ LUAI_FUNC const void *luaZ_getaddr (ZIO* z, size_t n); /* --------- Private Part ------------------ */ struct Zio { size_t n; /* bytes still unread */ const char *p; /* current position in buffer */ lua_Reader reader; /* reader function */ void *data; /* additional data */ lua_State *L; /* Lua state (for reader) */ }; LUAI_FUNC int luaZ_fill (ZIO *z); #endif ================================================ FILE: 3rd/lua/makefile ================================================ # Makefile for building Lua # See ../doc/readme.html for installation and customization instructions. # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= # Your platform. See PLATS for possible values. PLAT= guess CC= gcc -std=gnu99 CFLAGS= -O2 -Wall -Wextra $(SYSCFLAGS) $(MYCFLAGS) LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS) LIBS= -lm $(SYSLIBS) $(MYLIBS) AR= ar rcu RANLIB= ranlib RM= rm -f UNAME= uname SYSCFLAGS= SYSLDFLAGS= SYSLIBS= MYCFLAGS= -I../../skynet-src -g MYLDFLAGS= MYLIBS= MYOBJS= # Special flags for compiler modules; -Os reduces code size. CMCFLAGS= # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= PLATS= guess aix bsd c89 freebsd generic ios linux macosx mingw posix solaris LUA_A= liblua.a CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o LIB_O= lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o lmathlib.o loadlib.o loslib.o lstrlib.o ltablib.o lutf8lib.o linit.o BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS) LUA_T= lua LUA_O= lua.o LUAC_T= luac LUAC_O= luac.o ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O) ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) ALL_A= $(LUA_A) # Targets start here. default: $(PLAT) all: $(ALL_T) o: $(ALL_O) a: $(ALL_A) $(LUA_A): $(BASE_O) $(AR) $@ $(BASE_O) $(RANLIB) $@ $(LUA_T): $(LUA_O) $(LUA_A) $(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) $(LUAC_T): $(LUAC_O) $(LUA_A) $(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) test: ./$(LUA_T) -v clean: $(RM) $(ALL_T) $(ALL_O) depend: @$(CC) $(CFLAGS) -MM l*.c echo: @echo "PLAT= $(PLAT)" @echo "CC= $(CC)" @echo "CFLAGS= $(CFLAGS)" @echo "LDFLAGS= $(LDFLAGS)" @echo "LIBS= $(LIBS)" @echo "AR= $(AR)" @echo "RANLIB= $(RANLIB)" @echo "RM= $(RM)" @echo "UNAME= $(UNAME)" # Convenience targets for popular platforms. ALL= all help: @echo "Do 'make PLATFORM' where PLATFORM is one of these:" @echo " $(PLATS)" @echo "See doc/readme.html for complete instructions." guess: @echo Guessing `$(UNAME)` @$(MAKE) `$(UNAME)` AIX aix: $(MAKE) $(ALL) CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" SYSLDFLAGS="-brtl -bexpall" bsd: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-Wl,-E" c89: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_C89" CC="gcc -std=c89" @echo '' @echo '*** C89 does not guarantee 64-bit integers for Lua.' @echo '*** Make sure to compile all external Lua libraries' @echo '*** with LUA_USE_C89 to ensure consistency' @echo '' FreeBSD NetBSD OpenBSD freebsd: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE -I/usr/include/edit" SYSLIBS="-Wl,-E -ledit" CC="cc" generic: $(ALL) ios: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_IOS" Linux linux: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl" Darwin macos macosx: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX -DLUA_USE_READLINE" SYSLIBS="-lreadline" mingw: $(MAKE) "LUA_A=lua54.dll" "LUA_T=lua.exe" \ "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \ "SYSCFLAGS=-DLUA_BUILD_AS_DLL" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe $(MAKE) "LUAC_T=luac.exe" luac.exe posix: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX" SunOS solaris: $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_REENTRANT" SYSLIBS="-ldl" # Targets that do not create files (not all makes understand .PHONY). .PHONY: all $(PLATS) help test clean default o a depend echo # Compiler modules may use special flags. llex.o: $(CC) $(CFLAGS) $(CMCFLAGS) -c llex.c lparser.o: $(CC) $(CFLAGS) $(CMCFLAGS) -c lparser.c lcode.o: $(CC) $(CFLAGS) $(CMCFLAGS) -c lcode.c # DO NOT DELETE lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \ ltable.h lundump.h lvm.h lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h llimits.h lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ llimits.h lcode.o: lcode.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ ldo.h lgc.h lstring.h ltable.h lvm.h lopnames.h lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ llimits.h lctype.o: lctype.c lprefix.h lctype.h lua.h luaconf.h llimits.h ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h \ ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lvm.h ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \ lparser.h lstring.h ltable.h lundump.h lvm.h ldump.o: ldump.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h lgc.h ltable.h lundump.h lfunc.o: lfunc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h llimits.h liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldebug.h \ lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lgc.h llex.h lparser.h \ lstring.h ltable.h lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ llimits.h lmem.o: lmem.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ llimits.h lobject.o: lobject.c lprefix.h lua.h luaconf.h lctype.h llimits.h \ ldebug.h lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h \ lvm.h lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h \ lobject.h loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h lparser.o: lparser.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ ldo.h lfunc.h lstring.h lgc.h ltable.h lstate.o: lstate.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h \ lstring.h ltable.h lstring.o: lstring.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ llimits.h ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ llimits.h ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h luac.o: luac.c lprefix.h lua.h luaconf.h lauxlib.h lapi.h llimits.h \ lstate.h lobject.h ltm.h lzio.h lmem.h ldebug.h lopcodes.h lopnames.h \ lundump.h lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \ ltable.h lundump.h lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ llimits.h lvm.o: lvm.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \ lstring.h ltable.h lvm.h ljumptab.h lzio.o: lzio.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h # (end of Makefile) ================================================ FILE: 3rd/lua/onelua.c ================================================ /* ** Lua core, libraries, and interpreter in a single file. ** Compiling just this file generates a complete Lua stand-alone ** program: ** ** $ gcc -O2 -std=c99 -o lua onelua.c -lm ** ** or (for C89) ** ** $ gcc -O2 -std=c89 -DLUA_USE_C89 -o lua onelua.c -lm ** ** or (for Linux) ** ** gcc -O2 -o lua -DLUA_USE_LINUX -Wl,-E onelua.c -lm -ldl ** */ /* default is to build the full interpreter */ #ifndef MAKE_LIB #ifndef MAKE_LUAC #ifndef MAKE_LUA #define MAKE_LUA #endif #endif #endif /* ** Choose suitable platform-specific features. Default is no ** platform-specific features. Some of these options may need extra ** libraries such as -ldl -lreadline -lncurses */ #if 0 #define LUA_USE_LINUX #define LUA_USE_MACOSX #define LUA_USE_POSIX #endif /* ** Other specific features */ #if 0 #define LUA_32BITS #define LUA_USE_C89 #endif /* no need to change anything below this line ----------------------------- */ #include "lprefix.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* setup for luaconf.h */ #define LUA_CORE #define LUA_LIB #include "luaconf.h" /* do not export internal symbols */ #undef LUAI_FUNC #undef LUAI_DDEC #undef LUAI_DDEF #define LUAI_FUNC static #define LUAI_DDEC(def) /* empty */ #define LUAI_DDEF static /* core -- used by all */ #include "lzio.c" #include "lctype.c" #include "lopcodes.c" #include "lmem.c" #include "lundump.c" #include "ldump.c" #include "lstate.c" #include "lgc.c" #include "llex.c" #include "lcode.c" #include "lparser.c" #include "ldebug.c" #include "lfunc.c" #include "lobject.c" #include "ltm.c" #include "lstring.c" #include "ltable.c" #include "ldo.c" #include "lvm.c" #include "lapi.c" /* auxiliary library -- used by all */ #include "lauxlib.c" /* standard library -- not used by luac */ #ifndef MAKE_LUAC #include "lbaselib.c" #include "lcorolib.c" #include "ldblib.c" #include "liolib.c" #include "lmathlib.c" #include "loadlib.c" #include "loslib.c" #include "lstrlib.c" #include "ltablib.c" #include "lutf8lib.c" #include "linit.c" #endif /* test library -- used only for internal development */ #if defined(LUA_DEBUG) #include "ltests.c" #endif /* lua */ #ifdef MAKE_LUA #include "lua.c" #endif /* luac */ #ifdef MAKE_LUAC #include "luac.c" #endif ================================================ FILE: 3rd/lua-md5/README ================================================ MD5 - Cryptographic Library for Lua Copyright 2003 PUC-Rio http://www.keplerproject.org/md5 MD5 offers basic cryptographic facilities for Lua 5.1: a hash (digest) function, a pair crypt/decrypt based on MD5 and CFB, and a pair crypt/decrypt based on DES with 56-bit keys. MD5 current version is 1.1.2. This version is copy from https://github.com/keplerproject/md5 ================================================ FILE: 3rd/lua-md5/compat-5.2.c ================================================ #include "lua.h" #include "lauxlib.h" #include "compat-5.2.h" #if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501 /* ** Adapted from Lua 5.2.0 */ void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { luaL_checkstack(L, nup+1, "too many upvalues"); for (; l->name != NULL; l++) { /* fill the table with given functions */ int i; lua_pushstring(L, l->name); for (i = 0; i < nup; i++) /* copy upvalues to the top */ lua_pushvalue(L, -(nup + 1)); lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */ } lua_pop(L, nup); /* remove upvalues */ } #endif ================================================ FILE: 3rd/lua-md5/compat-5.2.h ================================================ #if !defined LUA_VERSION_NUM /* Lua 5.0 */ #define luaL_Reg luaL_reg #define luaL_addchar(B,c) \ ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ (*(B)->p++ = (char)(c))) #endif #if LUA_VERSION_NUM==501 /* Lua 5.1 */ #define lua_rawlen lua_objlen #endif void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup); ================================================ FILE: 3rd/lua-md5/md5.c ================================================ /** * $Id: md5.c,v 1.2 2008/03/24 20:59:12 mascarenhas Exp $ * Hash function MD5 * @author Marcela Ozorio Suarez, Roberto I. */ #include #include "md5.h" #define WORD 32 #define MASK 0xFFFFFFFF #if __STDC_VERSION__ >= 199901L #include typedef uint32_t WORD32; #else typedef unsigned int WORD32; #endif /** * md5 hash function. * @param message: aribtary string. * @param len: message length. * @param output: buffer to receive the hash value. Its size must be * (at least) HASHSIZE. */ void md5 (const char *message, long len, char *output); /* ** Realiza a rotacao no sentido horario dos bits da variavel 'D' do tipo WORD32. ** Os bits sao deslocados de 'num' posicoes */ #define rotate(D, num) (D<>(WORD-num)) /*Macros que definem operacoes relizadas pelo algoritmo md5 */ #define F(x, y, z) (((x) & (y)) | ((~(x)) & (z))) #define G(x, y, z) (((x) & (z)) | ((y) & (~(z)))) #define H(x, y, z) ((x) ^ (y) ^ (z)) #define I(x, y, z) ((y) ^ ((x) | (~(z)))) /*vetor de numeros utilizados pelo algoritmo md5 para embaralhar bits */ static const WORD32 T[64]={ 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 }; static void word32tobytes (const WORD32 *input, char *output) { int j = 0; while (j<4*4) { WORD32 v = *input++; output[j++] = (char)(v & 0xff); v >>= 8; output[j++] = (char)(v & 0xff); v >>= 8; output[j++] = (char)(v & 0xff); v >>= 8; output[j++] = (char)(v & 0xff); } } static void inic_digest(WORD32 *d) { d[0] = 0x67452301; d[1] = 0xEFCDAB89; d[2] = 0x98BADCFE; d[3] = 0x10325476; } /*funcao que implemeta os quatro passos principais do algoritmo MD5 */ static void digest(const WORD32 *m, WORD32 *d) { int j; /*MD5 PASSO1 */ for (j=0; j<4*4; j+=4) { d[0] = d[0]+ F(d[1], d[2], d[3])+ m[j] + T[j]; d[0]=rotate(d[0], 7); d[0]+=d[1]; d[3] = d[3]+ F(d[0], d[1], d[2])+ m[(j)+1] + T[j+1]; d[3]=rotate(d[3], 12); d[3]+=d[0]; d[2] = d[2]+ F(d[3], d[0], d[1])+ m[(j)+2] + T[j+2]; d[2]=rotate(d[2], 17); d[2]+=d[3]; d[1] = d[1]+ F(d[2], d[3], d[0])+ m[(j)+3] + T[j+3]; d[1]=rotate(d[1], 22); d[1]+=d[2]; } /*MD5 PASSO2 */ for (j=0; j<4*4; j+=4) { d[0] = d[0]+ G(d[1], d[2], d[3])+ m[(5*j+1)&0x0f] + T[(j-1)+17]; d[0] = rotate(d[0],5); d[0]+=d[1]; d[3] = d[3]+ G(d[0], d[1], d[2])+ m[((5*(j+1)+1)&0x0f)] + T[(j+0)+17]; d[3] = rotate(d[3], 9); d[3]+=d[0]; d[2] = d[2]+ G(d[3], d[0], d[1])+ m[((5*(j+2)+1)&0x0f)] + T[(j+1)+17]; d[2] = rotate(d[2], 14); d[2]+=d[3]; d[1] = d[1]+ G(d[2], d[3], d[0])+ m[((5*(j+3)+1)&0x0f)] + T[(j+2)+17]; d[1] = rotate(d[1], 20); d[1]+=d[2]; } /*MD5 PASSO3 */ for (j=0; j<4*4; j+=4) { d[0] = d[0]+ H(d[1], d[2], d[3])+ m[(3*j+5)&0x0f] + T[(j-1)+33]; d[0] = rotate(d[0], 4); d[0]+=d[1]; d[3] = d[3]+ H(d[0], d[1], d[2])+ m[(3*(j+1)+5)&0x0f] + T[(j+0)+33]; d[3] = rotate(d[3], 11); d[3]+=d[0]; d[2] = d[2]+ H(d[3], d[0], d[1])+ m[(3*(j+2)+5)&0x0f] + T[(j+1)+33]; d[2] = rotate(d[2], 16); d[2]+=d[3]; d[1] = d[1]+ H(d[2], d[3], d[0])+ m[(3*(j+3)+5)&0x0f] + T[(j+2)+33]; d[1] = rotate(d[1], 23); d[1]+=d[2]; } /*MD5 PASSO4 */ for (j=0; j<4*4; j+=4) { d[0] = d[0]+ I(d[1], d[2], d[3])+ m[(7*j)&0x0f] + T[(j-1)+49]; d[0] = rotate(d[0], 6); d[0]+=d[1]; d[3] = d[3]+ I(d[0], d[1], d[2])+ m[(7*(j+1))&0x0f] + T[(j+0)+49]; d[3] = rotate(d[3], 10); d[3]+=d[0]; d[2] = d[2]+ I(d[3], d[0], d[1])+ m[(7*(j+2))&0x0f] + T[(j+1)+49]; d[2] = rotate(d[2], 15); d[2]+=d[3]; d[1] = d[1]+ I(d[2], d[3], d[0])+ m[(7*(j+3))&0x0f] + T[(j+2)+49]; d[1] = rotate(d[1], 21); d[1]+=d[2]; } } static void bytestoword32 (WORD32 *x, const char *pt) { int i; for (i=0; i<16; i++) { int j=i*4; x[i] = (((WORD32)(unsigned char)pt[j+3] << 8 | (WORD32)(unsigned char)pt[j+2]) << 8 | (WORD32)(unsigned char)pt[j+1]) << 8 | (WORD32)(unsigned char)pt[j]; } } static void put_length(WORD32 *x, long len) { /* in bits! */ x[14] = (WORD32)((len<<3) & MASK); x[15] = (WORD32)(len>>(32-3) & 0x7); } /* ** returned status: * 0 - normal message (full 64 bytes) * 1 - enough room for 0x80, but not for message length (two 4-byte words) * 2 - enough room for 0x80 plus message length (at least 9 bytes free) */ static int converte (WORD32 *x, const char *pt, int num, int old_status) { int new_status = 0; char buff[64]; if (num<64) { memcpy(buff, pt, num); /* to avoid changing original string */ memset(buff+num, 0, 64-num); if (old_status == 0) buff[num] = '\200'; new_status = 1; pt = buff; } bytestoword32(x, pt); if (num <= (64 - 9)) new_status = 2; return new_status; } void md5 (const char *message, long len, char *output) { WORD32 d[4]; int status = 0; long i = 0; inic_digest(d); while (status != 2) { WORD32 d_old[4]; WORD32 wbuff[16]; int numbytes = (len-i >= 64) ? 64 : len-i; /*salva os valores do vetor digest*/ d_old[0]=d[0]; d_old[1]=d[1]; d_old[2]=d[2]; d_old[3]=d[3]; status = converte(wbuff, message+i, numbytes, status); if (status == 2) put_length(wbuff, len); digest(wbuff, d); d[0]+=d_old[0]; d[1]+=d_old[1]; d[2]+=d_old[2]; d[3]+=d_old[3]; i += numbytes; } word32tobytes(d, output); } ================================================ FILE: 3rd/lua-md5/md5.h ================================================ /** * $Id: md5.h,v 1.2 2006/03/03 15:04:49 tomas Exp $ * Cryptographic module for Lua. * @author Roberto Ierusalimschy */ #ifndef md5_h #define md5_h #include #define HASHSIZE 16 void md5 (const char *message, long len, char *output); int luaopen_md5_core (lua_State *L); #endif ================================================ FILE: 3rd/lua-md5/md5lib.c ================================================ /** * $Id: md5lib.c,v 1.10 2008/05/12 20:51:27 carregal Exp $ * Cryptographic and Hash functions for Lua * @author Roberto Ierusalimschy */ #include #include #include #include #include #include "md5.h" #include "compat-5.2.h" /** * Hash function. Returns a hash for a given string. * @param message: arbitrary binary string. * @return A 128-bit hash string. */ static int lmd5 (lua_State *L) { char buff[16]; size_t l; const char *message = luaL_checklstring(L, 1, &l); md5(message, l, buff); lua_pushlstring(L, buff, 16L); return 1; } /** * X-Or. Does a bit-a-bit exclusive-or of two strings. * @param s1: arbitrary binary string. * @param s2: arbitrary binary string with same length as s1. * @return a binary string with same length as s1 and s2, * where each bit is the exclusive-or of the corresponding bits in s1-s2. */ static int ex_or (lua_State *L) { size_t l1, l2; const char *s1 = luaL_checklstring(L, 1, &l1); const char *s2 = luaL_checklstring(L, 2, &l2); luaL_Buffer b; luaL_argcheck( L, l1 == l2, 2, "lengths must be equal" ); luaL_buffinit(L, &b); while (l1--) luaL_addchar(&b, (*s1++)^(*s2++)); luaL_pushresult(&b); return 1; } static void checkseed (lua_State *L) { if (lua_isnone(L, 3)) { /* no seed? */ time_t tm = time(NULL); /* for `random' seed */ lua_pushlstring(L, (char *)&tm, sizeof(tm)); } } #define MAXKEY 256 #define BLOCKSIZE 16 static int initblock (lua_State *L, const char *seed, int lseed, char *block) { size_t lkey; const char *key = luaL_checklstring(L, 2, &lkey); if (lkey > MAXKEY) luaL_error(L, "key too long (> %d)", MAXKEY); memset(block, 0, BLOCKSIZE); memcpy(block, seed, lseed); memcpy(block+BLOCKSIZE, key, lkey); return (int)lkey+BLOCKSIZE; } static void codestream (lua_State *L, const char *msg, size_t lmsg, char *block, int lblock) { luaL_Buffer b; luaL_buffinit(L, &b); while (lmsg > 0) { char code[BLOCKSIZE]; int i; md5(block, lblock, code); for (i=0; i 0; i++, lmsg--) code[i] ^= *msg++; luaL_addlstring(&b, code, i); memcpy(block, code, i); /* update seed */ } luaL_pushresult(&b); } static void decodestream (lua_State *L, const char *cypher, size_t lcypher, char *block, int lblock) { luaL_Buffer b; luaL_buffinit(L, &b); while (lcypher > 0) { char code[BLOCKSIZE]; int i; md5(block, lblock, code); /* update seed */ for (i=0; i 0; i++, lcypher--) code[i] ^= *cypher++; luaL_addlstring(&b, code, i); memcpy(block, cypher-i, i); } luaL_pushresult(&b); } /** * Encrypts a string. Uses the hash function md5 in CFB (Cipher-feedback * mode). * @param message: arbitrary binary string to be encrypted. * @param key: arbitrary binary string to be used as a key. * @param [seed]: optional arbitrary binary string to be used as a seed. * if no seed is provided, the function uses the result of * time() as a seed. * @return The cyphertext (as a binary string). */ static int crypt (lua_State *L) { size_t lmsg; const char *msg = luaL_checklstring(L, 1, &lmsg); size_t lseed; const char *seed; int lblock; char block[BLOCKSIZE+MAXKEY]; checkseed(L); seed = luaL_checklstring(L, 3, &lseed); if (lseed > BLOCKSIZE) luaL_error(L, "seed too long (> %d)", BLOCKSIZE); /* put seed and seed length at the beginning of result */ block[0] = (char)lseed; memcpy(block+1, seed, lseed); lua_pushlstring(L, block, lseed+1); /* to concat with result */ lblock = initblock(L, seed, lseed, block); codestream(L, msg, lmsg, block, lblock); lua_concat(L, 2); return 1; } /** * Decrypts a string. For any message, key, and seed, we have that * decrypt(crypt(msg, key, seed), key) == msg. * @param cyphertext: message to be decrypted (this must be the result of a previous call to crypt. * @param key: arbitrary binary string to be used as a key. * @return The plaintext. */ static int decrypt (lua_State *L) { size_t lcyphertext; const char *cyphertext = luaL_checklstring(L, 1, &lcyphertext); size_t lseed = cyphertext[0]; const char *seed = cyphertext+1; int lblock; char block[BLOCKSIZE+MAXKEY]; luaL_argcheck(L, lcyphertext >= lseed+1 && lseed <= BLOCKSIZE, 1, "invalid cyphered string"); cyphertext += lseed+1; lcyphertext -= lseed+1; lblock = initblock(L, seed, lseed, block); decodestream(L, cyphertext, lcyphertext, block, lblock); return 1; } /* ** Assumes the table is on top of the stack. */ static void set_info (lua_State *L) { lua_pushliteral (L, "_COPYRIGHT"); lua_pushliteral (L, "Copyright (C) 2003-2013 PUC-Rio"); lua_settable (L, -3); lua_pushliteral (L, "_DESCRIPTION"); lua_pushliteral (L, "Basic cryptographic facilities"); lua_settable (L, -3); lua_pushliteral (L, "_VERSION"); lua_pushliteral (L, "MD5 1.2"); lua_settable (L, -3); } static struct luaL_Reg md5lib[] = { {"sum", lmd5}, {"exor", ex_or}, {"crypt", crypt}, {"decrypt", decrypt}, {NULL, NULL} }; int luaopen_md5_core (lua_State *L) { lua_newtable(L); luaL_setfuncs(L, md5lib, 0); set_info (L); return 1; } ================================================ FILE: HISTORY.md ================================================ v1.8.0 (2025-1-14) ----------- * Update Lua to 5.4.7 * service sessions can be rewind * Improve: udp (ipv6 support) * Improve: debug console * Improve: http * Improve: mongo driver * Improve: mysql driver * Bugfix: socketchannel * Bugfix: cluster * Bugfix: ssl * Bugfix: websocket * Bugfix: redis cluster driver v1.7.0 (2023-11-13) ----------- * Update Lua to 5.4.6 * Update lpeg to 1.1.0 * Improve mongo driver * Fix service session rewind issue * Add websocket.is_closed v1.6.0 (2022-11-16) ----------- * Update Lua to 5.4.4 (github Nov 8, 2022) * Update jemalloc to 5.3.0 * Update lpeg to 1.0.2 (For sproto) * Update mongo driver to support the newest wire protocol * socket.listen()/cluster.open() returns ip address and port * Add service.close() v1.5.0 (2021-11-9) ----------- * Update Lua to 5.4.3 * Fix socket half close issues * Fix TLS issues * Improve websocket support * Improve redis support * Rework skynet.init/skynet.require * Add socket.onclose * Add httpc.request_stream v1.4.0 (2020-11-16) ----------- * Update Lua to 5.4.2 * Add skynet.select * Improve mysql driver (@zero-rp @xiaojin @yxt945) * Improve websocket and ssl (@lvzixun) * Improve sproto (double @lvzixun map @t0350) * Add padding mode PKCS7 for DES * Add jmem in debug console * Add skynet_socket_pause for net traffic control * Add timestamp to default logger v1.3.0 (2019-11-19) ----------- * Improve mysql driver (@yxt945) * Improve cluster * Improve lua shared proto (@hongling0) * Improve socket.write * Add lua sharetable * Add https support (@lvzixun) * Add websocket support (@lvzixun) * Fix bug in dns * Fix some memory leaks * jemalloc update to 5.2.1 v1.2.0 (2018-11-6) ----------- * Improve cluster support * Improve mongodb driver * Improve redis driver * Improve socket concurrent write * Improve socket channel * Improve service gate * Improve udp support * Add skynet.ignoreret * Add skynet.trace * Add skynet.context * Improve skynet.wait/wakeup * Add socket.netstat * Add socketchannel.overload * Fix memory leak for dead service * lua update to 5.3.5 * jemalloc update to 5.1.0 v1.1.0 (2017-10-31) ----------- * add socket.disconnected() * fix bugs : see comments for detail v1.1.0-rc (2017-7-18) ----------- * config file : support include * debug console : User config binding IP * debug console : Add call command * debug console : Report error message of inject code * debug console : Change response message * sharedata : Add sharedata.flush * sharedata : Add sharedata.deepcopy * cluster : Add cluster.send * cluster : Add API to update config table * skynet : Add skynet.state * skynet : Keep the order of skynet.wakeup * skynet : Add a MEMORY_CHECK macro for debugging * httpc : Add httpc.timeout * mongo driver : sort support multi-key * bson : Check utf8 string * bson : No longer support numeric key * daemon mode: Can output the error messages * sproto : Support decimal number * sproto: Support binary type * sproto: Support response nil * crypt: Add crypt.hmac64_md5 * redis: Add redis-cluster support * socket server : Optimize socket write (Try direct write from worker thread first) * Add prefix skynet to all skynet lua modules * datasheet : New module for replacement of sharedata * jemalloc : Update to 5.0.1 * lua : Update to 5.3.4 * lpeg : Update to 1.0.1 v1.0.0 (2016-7-11) ----------- * Version 1.0.0 Released v1.0.0-rc5 (2016-7-4) ----------- * MongoDB : Support auth_scram_sha1 * MongoDB : Auto determine primary host * Bugfix : memory leak in multicast * Bugfix : Lua 5.3.3 * Bson : support meta array v1.0.0-rc4 (2016-6-13) ----------- * Update lua to 5.3.3 * Update jemalloc to 4.2.1 * Add debug console command ping * Lua bson support __pairs * Add mongo.createIndexes and fix bug in old mongo.createIndex * Handle signal HUP to reopen log file (for logrotate) v1.0.0-rc3 (2016-5-9) ----------- * Update jemalloc 4.1.1 * Update lua 5.3.3 rc1 * Update sproto to support encoding empty table * Make skynet.init stable (keep order) * skynet.getenv can return empty string * Add lua VM memory warning * lua VM support memory limit * skynet.pcall support varargs * Bugfix : Global name query * Bugfix : snax.queryglobal v1.0.0-rc2 (2016-3-7) ----------- * Fix a bug in lua 5.3.2 * Update sproto (fix bugs and add ud for package) * Fix a bug in http * Fix a bug in harbor * Fix a bug in socket channel * Enhance remote debugger v1.0.0-rc (2015-12-28) ----------- * Update to lua 5.3.2 * Add skynet.coroutine lib * Add new debug api to show c memory used * httpc can use async dns query * Redis driver support pipeline * socket.send support string table, and rewrite redis driver * socket.shutdown would abandon unsend buffer * Improve some sproto api * c memory doesn't count the memory allocated by lua vm * some other bugfix (In multicast, socketchannel, etc) v1.0.0-beta (2015-11-10) ----------- * Improve and fix bug for sproto * Add global short string pool for lua vm * Add code cache mode * Add a callback for mysql auth * Add hmac_md5 * Sharedata support filename as a string * Fix a bug in socket.httpc * Fix a lua stack overflow bug in lua bson * Fix a socketchannel bug may block the data steam * Avoid dead loop when sending message to the service exiting * Fix memory leak in netpack * Improve DH key exchange implement * Minor fix for socket * Minor fix for multicast * Update jemalloc to 4.0.4 * Update lpeg to 1.0.0 v1.0.0-alpha10 (2015-8-17) ----------- * Remove the size limit of cluster RPC message. * Remove the size limit of local message. * Add cluster.query and clsuter.register. * Add an option of pthread mutex lock. * Add skynet.core.intcommand to optimize skynet.sleep etc. * Fix a memory leak bug in lua shared proto. * snax.msgserver use string instead of lightuserdata/size. * Remove some unused api in netpack. * Raise error when skynet.send to 0. v1.0.0-alpha9 (2015-8-10) ----------- * Improve lua serialization , support pairs metamethod. * Bugfix : sproto (See commits log of sproto) * Add user log service support (In config) * Other minor bugfix (See commits log) v1.0.0-alpha8 (2015-6-29) ----------- * Update lua 5.3.1 * Bugfix: skynet exit issue * Bugfix: timer race condition * Use atom increment in bson object id * remove assert when write to a listen fd * sproto encode doesn't use raw table api v1.0.0-alpha7 (2015-6-8) ----------- * console support launch snax service * Add cluster.snax * Add nodelay in clusterd * Merge sproto bugfix patch * Move some skynet api into skynet.manager * DNS support underscore * Add logservice in config file for user defined log service * skynet.fork returns coroutine * Fix a few of bugs , see the commits log v1.0.0-alpha6 (2015-5-18) ----------- * bugfix: httpc.get * bugfix: seri lib stack overflow * bugfix: udp send * bugfix: udp address * bugfix: sproto dump * add: sproto default * improve: skynet.wakeup (can wakeup skynet.call by raise an error) * improve: skynet.exit (raise error when uncall response) * remove: task overload warning * move: some skynet api move into skynet.manager v1.0.0-alpha5 (2015-4-27) ----------- * merge lua 5.3 official bugfix * improve sproto rpc api * fix a deadlock bug when service retire * improve cluster config reload * add skynet.pcall for calling a function with `require` * better error log in loginserver v1.0.0-alpha4 (2015-4-13) ----------- * sproto can share c struct between states * udp api changed (use lua string now) * fix memory leak in dns module v1.0.0-alpha3 (2015-3-30) ----------- * Update sproto (bugfix) * Add async dns query * improve httpc v1.0.0-alpha2 (2015-3-16) ----------- * Update examples client to lua 5.3 * Patch lua 5.3 to interrupt the dead loop (for debug) * Update sproto (fix some bugs and support unordered map) v1.0.0-alpha (2015-3-9) ----------- * Update lua from 5.2 to 5.3 * Add an online lua debugger * Add sharemap as an example use case of stm * Improve sproto for multi-state * Improve mongodb driver * Fix known bugs v0.9.3 (2015-1-5) ----------- * Add : mongo createIndex * Update : sproto * bugfix : sharedata check dirty flag when len/pairs metamethod * bugfix : multicast v0.9.2 (2014-12-8) ----------- * Simplify the message queue * Add create_index in mongo driver * Fix a bug in big-endian architecture (sproto) v0.9.0 / v0.9.1 (2014-11-17) ----------- * Add UDP support * Add IPv6 support * socket send package can define a release method * dispatch read before write in epoll * remove snax queue mode * Fix a bug in big-endian architecture v0.8.1 (2014-11-3) ----------- * Send to an invalid remote service will raise an error * Bugifx: socket open address string * Remove sha1 from mysqlaux * merge lua and sproto bugfix , use crypt lib instead * Fix a memory leak in socket * minor bugfix in http module v0.8.0 (2014-10-27) ----------- * Add mysql client driver * Bugfix : skynet.queue v0.7.4 (2014-10-13) ----------- * Bugfix : clear coroutine pool when GC * hotfix : A bug introduce by 0.7.3 v0.7.3 (2014-10-13) ----------- * Add some logs (warning) when overload * Bugfix: crash on exit v0.7.2 (2014-9-29) ----------- * Bugfix : datacenter.wait * Bugfix : error in forker coroutine * Add skynet.term * Accept socket report port * sharedata can be update more than once v0.7.1 (2014-9-22) ----------- * bugfix: wakeup sleep should return BREAK * bugfix: sharedatad load string * bugfix: dataserver forward error msg v0.7.0 (2014-9-8) ----------- * Use sproto instead of cjson * Add message logger * Add hmac-sha1 * Some minor bugfix v0.6.2 (2014-9-1) ----------- * bugfix: only skynet.call response PTYPE_ERROR v0.6.1 (2014-8-25) ----------- * bugfix: datacenter.wakeup * change struct msg name to avoid conflict in mac * improve seri library v0.6.0 (2014-8-18) ----------- * add sharedata * bugfix: service exit before init would not report back * add skynet.response and check multicall skynet.ret * skynet.newservice throw error when lanuch failed * Don't check imported function in snax.hotfix * snax service add change SERVICE_PATH and add it to package.path * skynet.redirect support string address * bugfix: skynet.harbor.link may block * add skynet.harbor.queryname to query globalname * add cluster.proxy * add DEBUG command exit (send a message to lua service by DEBUG) * add DEBUG command run (debug_console command inject) * bugfix : socketchannel connect once * bugfix : mongo driver v0.5.2 (2014-8-11) ----------- * Bugfix : httpd request * Bugifx : http chunked mode * Add : httpc * timer support more than 497 days v0.5.1 (2014-8-4) ----------- * Bugfix : http module * Bugfix : multicast local channel delete * Bugfix : socket.read(fd) v0.5.0 (2014-7-28) ----------- * skynet.exit will quit service immediately. * Add snax.gateserver, snax.loginserver, snax.msgserver * Simplify clientsocket lib * mongo driver support replica set * config file support read from ENV * add simple httpd (see examples/simpleweb.lua) v0.4.2 (2014-7-14) ----------- * Bugfix : invalid negative socket id * Add optional TCP_NODELAY support * Add worker thread weight * Add skynet.queue * Bugfix: socketchannel * cluster can throw error * Add readline and writeline to clientsocket lib * Add cluster.reload to reload config file * Add datacenter.wait v0.4.1 (2014-7-7) ----------- * Add SERVICE_NAME in loader * Throw error back when skynet.error * Add skynet.task * Bugfix for last version (harbor service bugs) v0.4.0 (2014-6-30) ----------- * Optimize redis driver `compose_message`. * Add module skynet.harbor for monitor harbor connect/disconnect, see test/testharborlink.lua . * cluster.open support cluster name. * Add new api skynet.packstring , and skynet.unpack support lua string * socket.listen support put port into address. (address:port) * Redesign harbor/master/dummy, remove lots of C code and rewrite in lua. * Remove block connect api, queue sending message during connecting now. * Add skynet.time() v0.3.2 (2014-6-23) ---------- * Bugfix : cluster (double free). * Add socket.header() to decode big-endian package header (and fix the bug in cluster). v0.3.1 (2014-6-16) ----------- * Bugfix: lua mongo driver . Hold reply string before decode bson data. * More check in bson decoding. * Use big-endian for encoding bson objectid. v0.3.0 (2014-6-2) ----------- * Add cluster support * Add single node mode * Add daemon mode * Bugfix: update lua-bson (signed 32bit int bug / check string length) * Optimize timer * Simplify message queue and optimize message dispatch * Use jemalloc release 3.6.0 v0.2.1 (2014-5-19) ----------- * Bugfix: check all the events already read after socket close * Bugfix: socket data in gate service * Bugfix: boundary problem in harbor service * Bugfix: stdin handle is 0 v0.2.0 (2014-5-12) ----------- * Rewrite malloc hook , use `pthread_getspecific` instead of `__thread` to get current service handle. * Optimize global unique service query, rewrite `service_mgr` . * Add some snax api, snax.uniqueservice (etc.) , use independent protocol `PTYPE_SNAX` . * Add bootstrap lua script , remove some code in C . * Use a lua loader to load lua service code (and set the lua environment), remove some code in C. * Support preload a file before each lua service start. * Add datacenter service. * Add multicast api. * Remove skynet.blockcall , simplify the implement of message queue. * When dropping message queue (at service exit) , dispatcher will post an error back to the source of each message. * Remove skynet.watch , monitor is not necessary for watching skynet.call . so simplemonitor.lua is move to examples. * Remove the limit of global queue size (64K actived service limit before). * Refactoring `skynet_command`. v0.1.1 (2014-4-28) ------------------ * Socket channel should clear request queue when reconnect. * Fix the issue that socket close may block the coroutine. * Fix the issue that jemalloc api may crash on macosx (disable jemalloc on macosx). v0.1.0 (2014-4-23) ------------------ * First release version. First public version (2012-8-1) ------------------ * Make skynet from a private project to public. ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2012-2025 codingnow.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: Makefile ================================================ include platform.mk LUA_CLIB_PATH ?= luaclib CSERVICE_PATH ?= cservice SKYNET_BUILD_PATH ?= . CFLAGS = -g -O2 -Wall -I$(LUA_INC) $(MYCFLAGS) # CFLAGS += -DUSE_PTHREAD_LOCK # lua LUA_STATICLIB := 3rd/lua/liblua.a LUA_LIB ?= $(LUA_STATICLIB) LUA_INC ?= 3rd/lua $(LUA_STATICLIB) : cd 3rd/lua && $(MAKE) CC='$(CC) -std=gnu99' $(PLAT) # https : turn on TLS_MODULE to add https support # TLS_MODULE=ltls TLS_LIB= TLS_INC= # jemalloc JEMALLOC_STATICLIB := 3rd/jemalloc/lib/libjemalloc_pic.a JEMALLOC_INC := 3rd/jemalloc/include/jemalloc all : jemalloc .PHONY : jemalloc update3rd MALLOC_STATICLIB := $(JEMALLOC_STATICLIB) $(JEMALLOC_STATICLIB) : 3rd/jemalloc/Makefile cd 3rd/jemalloc && $(MAKE) CC=$(CC) 3rd/jemalloc/autogen.sh : git submodule update --init 3rd/jemalloc/Makefile : | 3rd/jemalloc/autogen.sh cd 3rd/jemalloc && ./autogen.sh --with-jemalloc-prefix=je_ --enable-prof jemalloc : $(MALLOC_STATICLIB) update3rd : rm -rf 3rd/jemalloc && git submodule update --init # skynet CSERVICE = snlua logger gate harbor LUA_CLIB = skynet \ client \ bson md5 sproto lpeg $(TLS_MODULE) LUA_CLIB_SKYNET = \ lua-skynet.c lua-seri.c \ lua-socket.c \ lua-mongo.c \ lua-netpack.c \ lua-memory.c \ lua-multicast.c \ lua-cluster.c \ lua-crypt.c lsha1.c \ lua-sharedata.c \ lua-stm.c \ lua-debugchannel.c \ lua-datasheet.c \ lua-sharetable.c \ \ SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \ skynet_harbor.c skynet_env.c skynet_monitor.c skynet_socket.c socket_server.c \ mem_info.c malloc_hook.c skynet_daemon.c skynet_log.c all : \ $(SKYNET_BUILD_PATH)/skynet \ $(foreach v, $(CSERVICE), $(CSERVICE_PATH)/$(v).so) \ $(foreach v, $(LUA_CLIB), $(LUA_CLIB_PATH)/$(v).so) $(SKYNET_BUILD_PATH)/skynet : $(foreach v, $(SKYNET_SRC), skynet-src/$(v)) $(LUA_LIB) $(MALLOC_STATICLIB) $(CC) $(CFLAGS) -o $@ $^ -Iskynet-src -I$(JEMALLOC_INC) $(LDFLAGS) $(EXPORT) $(SKYNET_LIBS) $(SKYNET_DEFINES) $(LUA_CLIB_PATH) : mkdir $(LUA_CLIB_PATH) $(CSERVICE_PATH) : mkdir $(CSERVICE_PATH) define CSERVICE_TEMP $$(CSERVICE_PATH)/$(1).so : service-src/service_$(1).c | $$(CSERVICE_PATH) $$(CC) $$(CFLAGS) $$(SHARED) $$< -o $$@ -Iskynet-src endef $(foreach v, $(CSERVICE), $(eval $(call CSERVICE_TEMP,$(v)))) $(LUA_CLIB_PATH)/skynet.so : $(addprefix lualib-src/,$(LUA_CLIB_SKYNET)) | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src -Iservice-src -Ilualib-src $(LUA_CLIB_PATH)/bson.so : lualib-src/lua-bson.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@ $(LUA_CLIB_PATH)/md5.so : 3rd/lua-md5/md5.c 3rd/lua-md5/md5lib.c 3rd/lua-md5/compat-5.2.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -I3rd/lua-md5 $^ -o $@ $(LUA_CLIB_PATH)/client.so : lualib-src/lua-clientsocket.c lualib-src/lua-crypt.c lualib-src/lsha1.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -lpthread $(LUA_CLIB_PATH)/sproto.so : lualib-src/sproto/sproto.c lualib-src/sproto/lsproto.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Ilualib-src/sproto $^ -o $@ $(LUA_CLIB_PATH)/ltls.so : lualib-src/ltls.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -Iskynet-src -L$(TLS_LIB) -I$(TLS_INC) $^ -o $@ -lssl -lcrypto $(LUA_CLIB_PATH)/lpeg.so : 3rd/lpeg/lpcap.c 3rd/lpeg/lpcode.c 3rd/lpeg/lpprint.c 3rd/lpeg/lptree.c 3rd/lpeg/lpvm.c 3rd/lpeg/lpcset.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -I3rd/lpeg $^ -o $@ clean : rm -f $(SKYNET_BUILD_PATH)/skynet $(CSERVICE_PATH)/*.so $(LUA_CLIB_PATH)/*.so && \ rm -rf $(SKYNET_BUILD_PATH)/*.dSYM $(CSERVICE_PATH)/*.dSYM $(LUA_CLIB_PATH)/*.dSYM $(MAKE) clean -f mingw.mk cleanall: clean ifneq (,$(wildcard 3rd/jemalloc/Makefile)) cd 3rd/jemalloc && $(MAKE) clean && rm Makefile endif cd 3rd/lua && $(MAKE) clean rm -f $(LUA_STATICLIB) $(MAKE) cleanall -f mingw.mk ================================================ FILE: README.md ================================================ ## ![skynet logo](https://github.com/cloudwu/skynet/wiki/image/skynet_metro.jpg) Skynet is a multi-user Lua framework supporting the actor model, often used in games. [It is heavily used in the Chinese game industry](https://github.com/cloudwu/skynet/wiki/Uses), but is also now spreading to other industries, and to English-centric developers. To visit related sites, visit the Chinese pages using something like Google or Deepl translate. The community is friendly and almost all contributors can speak English, so English speakers are welcome to ask questions in [Discussion](https://github.com/cloudwu/skynet/discussions), or submit issues in English. ## Build For Linux, install autoconf first for jemalloc: ``` git clone https://github.com/cloudwu/skynet.git cd skynet make 'PLATFORM' # PLATFORM can be linux, macosx, freebsd now ``` Or: ``` export PLAT=linux make ``` For FreeBSD , use gmake instead of make. ## Test Run these in different consoles: ``` ./skynet examples/config # Launch first skynet node (Gate server) and a skynet-master (see config for standalone option) ./3rd/lua/lua examples/client.lua # Launch a client, and try to input hello. ``` ## About Lua version Skynet now uses a modified version of lua 5.5.0 ( https://github.com/ejoy/lua/tree/skynet55 ) for multiple lua states. Official Lua versions can also be used as long as the Makefile is edited. ## How To Use * Read Wiki for documents https://github.com/cloudwu/skynet/wiki (Written in both English and Chinese) * The FAQ in wiki https://github.com/cloudwu/skynet/wiki/FAQ (In Chinese, but you can visit them using something like Google or Deepl translate.) ================================================ FILE: examples/abort.lua ================================================ local skynet = require "skynet" require "skynet.manager" -- import skynet.abort skynet.abort() ================================================ FILE: examples/agent.lua ================================================ local skynet = require "skynet" local socket = require "skynet.socket" local sproto = require "sproto" local sprotoloader = require "sprotoloader" local WATCHDOG local host local send_request local CMD = {} local REQUEST = {} local client_fd function REQUEST:get() print("get", self.what) local r = skynet.call("SIMPLEDB", "lua", "get", self.what) return { result = r } end function REQUEST:set() print("set", self.what, self.value) local r = skynet.call("SIMPLEDB", "lua", "set", self.what, self.value) end function REQUEST:handshake() return { msg = "Welcome to skynet, I will send heartbeat every 5 sec." } end function REQUEST:quit() skynet.call(WATCHDOG, "lua", "close", client_fd) end local function request(name, args, response) local f = assert(REQUEST[name]) local r = f(args) if response then return response(r) end end local function send_package(pack) local package = string.pack(">s2", pack) socket.write(client_fd, package) end skynet.register_protocol { name = "client", id = skynet.PTYPE_CLIENT, unpack = function (msg, sz) return host:dispatch(msg, sz) end, dispatch = function (fd, _, type, ...) assert(fd == client_fd) -- You can use fd to reply message skynet.ignoreret() -- session is fd, don't call skynet.ret skynet.trace() if type == "REQUEST" then local ok, result = pcall(request, ...) if ok then if result then send_package(result) end else skynet.error(result) end else assert(type == "RESPONSE") error "This example doesn't support request client" end end } function CMD.start(conf) local fd = conf.client local gate = conf.gate WATCHDOG = conf.watchdog -- slot 1,2 set at main.lua host = sprotoloader.load(1):host "package" send_request = host:attach(sprotoloader.load(2)) skynet.fork(function() while true do send_package(send_request "heartbeat") skynet.sleep(500) end end) client_fd = fd skynet.call(gate, "lua", "forward", fd) end function CMD.disconnect() -- todo: do something before exit skynet.exit() end skynet.start(function() skynet.dispatch("lua", function(_,_, command, ...) skynet.trace() local f = CMD[command] skynet.ret(skynet.pack(f(...))) end) end) ================================================ FILE: examples/checkdeadloop.lua ================================================ local skynet = require "skynet" local list = {} local function timeout_check(ti) if not next(list) then return end skynet.sleep(ti) -- sleep 10 sec for k,v in pairs(list) do skynet.error("timeout",ti,k,v) end end skynet.start(function() skynet.error("ping all") local list_ret = skynet.call(".launcher", "lua", "LIST") for addr, desc in pairs(list_ret) do list[addr] = desc skynet.fork(function() skynet.call(addr,"debug","INFO") list[addr] = nil end) end skynet.sleep(0) timeout_check(100) timeout_check(400) timeout_check(500) skynet.exit() end) ================================================ FILE: examples/client.lua ================================================ package.cpath = "luaclib/?.so" package.path = "lualib/?.lua;examples/?.lua" if _VERSION ~= "Lua 5.4" then error "Use lua 5.4" end local socket = require "client.socket" local proto = require "proto" local sproto = require "sproto" local host = sproto.new(proto.s2c):host "package" local request = host:attach(sproto.new(proto.c2s)) local fd = assert(socket.connect("127.0.0.1", 8888)) local function send_package(fd, pack) local package = string.pack(">s2", pack) socket.send(fd, package) end local function unpack_package(text) local size = #text if size < 2 then return nil, text end local s = text:byte(1) * 256 + text:byte(2) if size < s+2 then return nil, text end return text:sub(3,2+s), text:sub(3+s) end local function recv_package(last) local result result, last = unpack_package(last) if result then return result, last end local r = socket.recv(fd) if not r then return nil, last end if r == "" then error "Server closed" end return recv_package(last .. r) end local session = 0 local function send_request(name, args) session = session + 1 local str = request(name, args, session) send_package(fd, str) print("Request:", session) end local last = "" local function print_request(name, args) print("REQUEST", name) if args then for k,v in pairs(args) do print(k,v) end end end local function print_response(session, args) print("RESPONSE", session) if args then for k,v in pairs(args) do print(k,v) end end end local function print_package(t, ...) if t == "REQUEST" then print_request(...) else assert(t == "RESPONSE") print_response(...) end end local function dispatch_package() while true do local v v, last = recv_package(last) if not v then break end print_package(host:dispatch(v)) end end send_request("handshake") send_request("set", { what = "hello", value = "world" }) while true do dispatch_package() local cmd = socket.readstdin() if cmd then if cmd == "quit" then send_request("quit") else send_request("get", { what = cmd }) end else socket.usleep(100) end end ================================================ FILE: examples/cluster1.lua ================================================ local skynet = require "skynet" local cluster = require "skynet.cluster" local snax = require "skynet.snax" skynet.start(function() cluster.reload { db = "127.0.0.1:2528", db2 = "127.0.0.1:2529", } local sdb = skynet.newservice("simpledb") -- register name "sdb" for simpledb, you can use cluster.query() later. -- See cluster2.lua cluster.register("sdb", sdb) cluster.unregister("sdb") cluster.register("sdb", sdb) print(skynet.call(sdb, "lua", "SET", "a", "foobar")) print(skynet.call(sdb, "lua", "SET", "b", "foobar2")) print(skynet.call(sdb, "lua", "GET", "a")) print(skynet.call(sdb, "lua", "GET", "b")) cluster.open "db" cluster.open "db2" -- unique snax service snax.uniqueservice "pingserver" end) ================================================ FILE: examples/cluster2.lua ================================================ local skynet = require "skynet" local cluster = require "skynet.cluster" skynet.start(function() local proxy = cluster.proxy "db@sdb" -- cluster.proxy("db", "@sdb") local largekey = string.rep("X", 128*1024) local largevalue = string.rep("R", 100 * 1024) skynet.call(proxy, "lua", "SET", largekey, largevalue) local v = skynet.call(proxy, "lua", "GET", largekey) assert(largevalue == v) skynet.send(proxy, "lua", "PING", "proxy") skynet.fork(function() skynet.trace("cluster") print(cluster.call("db", "@sdb", "GET", "a")) print(cluster.call("db2", "@sdb", "GET", "b")) cluster.send("db2", "@sdb", "PING", "db2:longstring" .. largevalue) end) -- test snax service skynet.timeout(300,function() cluster.reload { db = false, -- db is down db3 = "127.0.0.1:2529" } print(pcall(cluster.call, "db", "@sdb", "GET", "a")) -- db is down end) cluster.reload { __nowaiting = false } local pingserver = cluster.snax("db3", "pingserver") print(pingserver.req.ping "hello") end) ================================================ FILE: examples/clustername.lua ================================================ __nowaiting = true -- If you turn this flag off, cluster.call would block when node name is absent db = "127.0.0.1:2528" db2 = "127.0.0.1:2529" ================================================ FILE: examples/config ================================================ include "config.path" -- preload = "./examples/preload.lua" -- run preload.lua before every lua service run thread = 8 logger = nil logpath = "." harbor = 1 address = "127.0.0.1:2526" master = "127.0.0.1:2013" start = "main" -- main script bootstrap = "snlua bootstrap" -- The service for bootstrap standalone = "0.0.0.0:2013" -- snax_interface_g = "snax_g" cpath = root.."cservice/?.so" -- daemon = "./skynet.pid" ================================================ FILE: examples/config.c1 ================================================ thread = 8 logger = nil harbor = 0 start = "cluster1" bootstrap = "snlua bootstrap" -- The service for bootstrap luaservice = "./service/?.lua;./test/?.lua;./examples/?.lua" lualoader = "lualib/loader.lua" cpath = "./cservice/?.so" -- use cluster.reload instead, see cluster1.lua -- cluster = "./examples/clustername.lua" snax = "./test/?.lua" ================================================ FILE: examples/config.c2 ================================================ thread = 8 logger = nil harbor = 0 start = "cluster2" bootstrap = "snlua bootstrap" -- The service for bootstrap luaservice = "./service/?.lua;./test/?.lua;./examples/?.lua" lualoader = "lualib/loader.lua" cpath = "./cservice/?.so" cluster = "./examples/clustername.lua" snax = "./test/?.lua" ================================================ FILE: examples/config.handle ================================================ include "config.path" thread = 8 logger = "skynet.log" logpath = "." harbor = 0 start = "testhandle" -- main script bootstrap = "snlua bootstrap" -- The service for bootstrap cpath = root.."cservice/?.so" --daemon = "./skynet.pid" ================================================ FILE: examples/config.login ================================================ thread = 8 logger = nil harbor = 0 start = "main" bootstrap = "snlua bootstrap" -- The service for bootstrap luaservice = "./service/?.lua;./examples/login/?.lua" lualoader = "lualib/loader.lua" cpath = "./cservice/?.so" ================================================ FILE: examples/config.mc ================================================ root = "./" thread = 8 logger = nil harbor = 2 address = "127.0.0.1:2527" master = "127.0.0.1:2013" start = "testmulticast2" -- main script bootstrap = "snlua bootstrap" -- The service for bootstrap --standalone = "0.0.0.0:2013" luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" lualoader = "lualib/loader.lua" -- preload = "./examples/preload.lua" -- run preload.lua before every lua service run snax = root.."examples/?.lua;"..root.."test/?.lua" cpath = root.."cservice/?.so" ================================================ FILE: examples/config.mongodb ================================================ root = "./" thread = 8 logger = nil harbor = 0 start = "main_mongodb" -- main script bootstrap = "snlua bootstrap" -- The service for bootstrap luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" lualoader = "lualib/loader.lua" snax = root.."examples/?.lua;"..root.."test/?.lua" cpath = root.."cservice/?.so" -- daemon = "./skynet.pid" ================================================ FILE: examples/config.mysql ================================================ root = "./" thread = 8 logger = nil harbor = 0 start = "main_mysql" -- main script bootstrap = "snlua bootstrap" -- The service for bootstrap luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" lualoader = "lualib/loader.lua" snax = root.."examples/?.lua;"..root.."test/?.lua" cpath = root.."cservice/?.so" -- daemon = "./skynet.pid" ================================================ FILE: examples/config.path ================================================ root = "./" luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua;"..root.."test/?/init.lua" lualoader = root .. "lualib/loader.lua" lua_path = root.."lualib/?.lua;"..root.."lualib/?/init.lua" lua_cpath = root .. "luaclib/?.so" snax = root.."examples/?.lua;"..root.."test/?.lua" ================================================ FILE: examples/config.userlog ================================================ root = "./" thread = 8 logger = "userlog" logservice = "snlua" logpath = "." harbor = 0 start = "main" -- main script bootstrap = "snlua bootstrap" -- The service for bootstrap luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" lualoader = "lualib/loader.lua" -- preload = "./examples/preload.lua" -- run preload.lua before every lua service run snax = root.."examples/?.lua;"..root.."test/?.lua" -- snax_interface_g = "snax_g" cpath = root.."cservice/?.so" -- daemon = "./skynet.pid" ================================================ FILE: examples/config_log ================================================ thread = 8 mqueue = 256 cpath = "./cservice/?.so" logger = nil harbor = 2 address = "127.0.0.1:2527" master = "127.0.0.1:2013" start = "main_log" luaservice ="./service/?.lua;./test/?.lua;./examples/?.lua" snax = "./examples/?.lua;./test/?.lua" ================================================ FILE: examples/globallog.lua ================================================ local skynet = require "skynet" require "skynet.manager" -- import skynet.register skynet.start(function() skynet.dispatch("lua", function(session, address, ...) print("[GLOBALLOG]", skynet.address(address), ...) end) skynet.register ".log" skynet.register "LOG" end) ================================================ FILE: examples/injectlaunch.lua ================================================ if not _P then print[[ This file is examples to show how to inject code into lua service. It is used to inject into launcher service to change the command.LAUNCH to command.LOGLAUNCH. telnet the debug_console service (nc 127.0.0.1 8000), and run: inject 3 examples/injectlaunch.lua -- 3 means launcher service ]] return end local command = _P.lua.command if command.RAWLAUNCH then command.LAUNCH, command.RAWLAUNCH = command.RAWLAUNCH print "restore command.LAUNCH" else command.RAWLAUNCH = command.LAUNCH command.LAUNCH = command.LOGLAUNCH print "replace command.LAUNCH" end ================================================ FILE: examples/login/client.lua ================================================ package.cpath = "luaclib/?.so" local socket = require "client.socket" local crypt = require "client.crypt" if _VERSION ~= "Lua 5.4" then error "Use lua 5.4" end local fd = assert(socket.connect("127.0.0.1", 8001)) local function writeline(fd, text) socket.send(fd, text .. "\n") end local function unpack_line(text) local from = text:find("\n", 1, true) if from then return text:sub(1, from-1), text:sub(from+1) end return nil, text end local last = "" local function unpack_f(f) local function try_recv(fd, last) local result result, last = f(last) if result then return result, last end local r = socket.recv(fd) if not r then return nil, last end if r == "" then error "Server closed" end return f(last .. r) end return function() while true do local result result, last = try_recv(fd, last) if result then return result end socket.usleep(100) end end end local readline = unpack_f(unpack_line) local challenge = crypt.base64decode(readline()) local clientkey = crypt.randomkey() writeline(fd, crypt.base64encode(crypt.dhexchange(clientkey))) local secret = crypt.dhsecret(crypt.base64decode(readline()), clientkey) print("sceret is ", crypt.hexencode(secret)) local hmac = crypt.hmac64(challenge, secret) writeline(fd, crypt.base64encode(hmac)) local token = { server = "sample", user = "hello", pass = "password", } local function encode_token(token) return string.format("%s@%s:%s", crypt.base64encode(token.user), crypt.base64encode(token.server), crypt.base64encode(token.pass)) end local etoken = crypt.desencode(secret, encode_token(token)) writeline(fd, crypt.base64encode(etoken)) local result = readline() print(result) local code = tonumber(string.sub(result, 1, 3)) assert(code == 200) socket.close(fd) local subid = crypt.base64decode(string.sub(result, 5)) print("login ok, subid=", subid) ----- connect to game server local function send_request(v, session) local size = #v + 4 local package = string.pack(">I2", size)..v..string.pack(">I4", session) socket.send(fd, package) return v, session end local function recv_response(v) local size = #v - 5 local content, ok, session = string.unpack("c"..tostring(size).."B>I4", v) return ok ~=0 , content, session end local function unpack_package(text) local size = #text if size < 2 then return nil, text end local s = text:byte(1) * 256 + text:byte(2) if size < s+2 then return nil, text end return text:sub(3,2+s), text:sub(3+s) end local readpackage = unpack_f(unpack_package) local function send_package(fd, pack) local package = string.pack(">s2", pack) socket.send(fd, package) end local text = "echo" local index = 1 print("connect") fd = assert(socket.connect("127.0.0.1", 8888)) last = "" local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index) local hmac = crypt.hmac64(crypt.hashkey(handshake), secret) send_package(fd, handshake .. ":" .. crypt.base64encode(hmac)) print(readpackage()) print("===>",send_request(text,0)) -- don't recv response -- print("<===",recv_response(readpackage())) print("disconnect") socket.close(fd) index = index + 1 print("connect again") fd = assert(socket.connect("127.0.0.1", 8888)) last = "" local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index) local hmac = crypt.hmac64(crypt.hashkey(handshake), secret) send_package(fd, handshake .. ":" .. crypt.base64encode(hmac)) print(readpackage()) print("===>",send_request("fake",0)) -- request again (use last session 0, so the request message is fake) print("===>",send_request("again",1)) -- request again (use new session) print("<===",recv_response(readpackage())) print("<===",recv_response(readpackage())) print("disconnect") socket.close(fd) ================================================ FILE: examples/login/gated.lua ================================================ local msgserver = require "snax.msgserver" local crypt = require "skynet.crypt" local skynet = require "skynet" local loginservice = tonumber(...) local server = {} local users = {} local username_map = {} local internal_id = 0 -- login server disallow multi login, so login_handler never be reentry -- call by login server function server.login_handler(uid, secret) if users[uid] then error(string.format("%s is already login", uid)) end internal_id = internal_id + 1 local id = internal_id -- don't use internal_id directly local username = msgserver.username(uid, id, servername) -- you can use a pool to alloc new agent local agent = skynet.newservice "msgagent" local u = { username = username, agent = agent, uid = uid, subid = id, } -- trash subid (no used) skynet.call(agent, "lua", "login", uid, id, secret) users[uid] = u username_map[username] = u msgserver.login(username, secret) -- you should return unique subid return id end -- call by agent function server.logout_handler(uid, subid) local u = users[uid] if u then local username = msgserver.username(uid, subid, servername) assert(u.username == username) msgserver.logout(u.username) users[uid] = nil username_map[u.username] = nil skynet.call(loginservice, "lua", "logout",uid, subid) end end -- call by login server function server.kick_handler(uid, subid) local u = users[uid] if u then local username = msgserver.username(uid, subid, servername) assert(u.username == username) -- NOTICE: logout may call skynet.exit, so you should use pcall. pcall(skynet.call, u.agent, "lua", "logout") end end -- call by self (when socket disconnect) function server.disconnect_handler(username) local u = username_map[username] if u then skynet.call(u.agent, "lua", "afk") end end -- call by self (when recv a request from client) function server.request_handler(username, msg) local u = username_map[username] return skynet.tostring(skynet.rawcall(u.agent, "client", msg)) end -- call by self (when gate open) function server.register_handler(name) servername = name skynet.call(loginservice, "lua", "register_gate", servername, skynet.self()) end msgserver.start(server) ================================================ FILE: examples/login/logind.lua ================================================ local login = require "snax.loginserver" local crypt = require "skynet.crypt" local skynet = require "skynet" local server = { host = "127.0.0.1", port = 8001, multilogin = false, -- disallow multilogin name = "login_master", } local server_list = {} local user_online = {} local user_login = {} function server.auth_handler(token) -- the token is base64(user)@base64(server):base64(password) local user, server, password = token:match("([^@]+)@([^:]+):(.+)") user = crypt.base64decode(user) server = crypt.base64decode(server) password = crypt.base64decode(password) assert(password == "password", "Invalid password") return server, user end function server.login_handler(server, uid, secret) print(string.format("%s@%s is login, secret is %s", uid, server, crypt.hexencode(secret))) local gameserver = assert(server_list[server], "Unknown server") -- only one can login, because disallow multilogin local last = user_online[uid] if last then skynet.call(last.address, "lua", "kick", uid, last.subid) end if user_online[uid] then error(string.format("user %s is already online", uid)) end local subid = tostring(skynet.call(gameserver, "lua", "login", uid, secret)) user_online[uid] = { address = gameserver, subid = subid , server = server} return subid end local CMD = {} function CMD.register_gate(server, address) server_list[server] = address end function CMD.logout(uid, subid) local u = user_online[uid] if u then print(string.format("%s@%s is logout", uid, u.server)) user_online[uid] = nil end end function server.command_handler(command, ...) local f = assert(CMD[command]) return f(...) end login(server) ================================================ FILE: examples/login/main.lua ================================================ local skynet = require "skynet" skynet.start(function() local loginserver = skynet.newservice("logind") local gate = skynet.newservice("gated", loginserver) skynet.call(gate, "lua", "open" , { port = 8888, maxclient = 64, servername = "sample", }) end) ================================================ FILE: examples/login/msgagent.lua ================================================ local skynet = require "skynet" skynet.register_protocol { name = "client", id = skynet.PTYPE_CLIENT, unpack = skynet.tostring, } local gate local userid, subid local CMD = {} function CMD.login(source, uid, sid, secret) -- you may use secret to make a encrypted data stream skynet.error(string.format("%s is login", uid)) gate = source userid = uid subid = sid -- you may load user data from database end local function logout() if gate then skynet.call(gate, "lua", "logout", userid, subid) end skynet.exit() end function CMD.logout(source) -- NOTICE: The logout MAY be reentry skynet.error(string.format("%s is logout", userid)) logout() end function CMD.afk(source) -- the connection is broken, but the user may back skynet.error(string.format("AFK")) end skynet.start(function() -- If you want to fork a work thread , you MUST do it in CMD.login skynet.dispatch("lua", function(session, source, command, ...) local f = assert(CMD[command]) skynet.ret(skynet.pack(f(source, ...))) end) skynet.dispatch("client", function(_,_, msg) -- the simple echo service skynet.sleep(10) -- sleep a while skynet.ret(msg) end) end) ================================================ FILE: examples/main.lua ================================================ local skynet = require "skynet" local sprotoloader = require "sprotoloader" local max_client = 64 skynet.start(function() skynet.error("Server start") skynet.uniqueservice("protoloader") if not skynet.getenv "daemon" then local console = skynet.newservice("console") end skynet.newservice("debug_console",8000) skynet.newservice("simpledb") local watchdog = skynet.newservice("watchdog") local addr,port = skynet.call(watchdog, "lua", "start", { port = 8888, maxclient = max_client, nodelay = true, }) skynet.error("Watchdog listen on " .. addr .. ":" .. port) skynet.exit() end) ================================================ FILE: examples/main_log.lua ================================================ local skynet = require "skynet" local harbor = require "skynet.harbor" require "skynet.manager" -- import skynet.monitor local function monitor_master() harbor.linkmaster() print("master is down") skynet.exit() end skynet.start(function() print("Log server start") skynet.monitor "simplemonitor" local log = skynet.newservice("globallog") skynet.fork(monitor_master) end) ================================================ FILE: examples/main_mongodb.lua ================================================ local skynet = require "skynet" skynet.start(function() print("Main Server start") local console = skynet.newservice( "testmongodb", "127.0.0.1", 27017, "testdb", "test", "test" ) print("Main Server exit") skynet.exit() end) ================================================ FILE: examples/main_mysql.lua ================================================ local skynet = require "skynet" skynet.start(function() print("Main Server start") local console = skynet.newservice("testmysql") print("Main Server exit") skynet.exit() end) ================================================ FILE: examples/preload.lua ================================================ -- This file will execute before every lua service start -- See config print("PRELOAD", ...) ================================================ FILE: examples/proto.lua ================================================ local sprotoparser = require "sprotoparser" local proto = {} proto.c2s = sprotoparser.parse [[ .package { type 0 : integer session 1 : integer } handshake 1 { response { msg 0 : string } } get 2 { request { what 0 : string } response { result 0 : string } } set 3 { request { what 0 : string value 1 : string } } quit 4 {} ]] proto.s2c = sprotoparser.parse [[ .package { type 0 : integer session 1 : integer } heartbeat 1 {} ]] return proto ================================================ FILE: examples/protoloader.lua ================================================ -- module proto as examples/proto.lua package.path = "./examples/?.lua;" .. package.path local skynet = require "skynet" local sprotoparser = require "sprotoparser" local sprotoloader = require "sprotoloader" local proto = require "proto" skynet.start(function() sprotoloader.save(proto.c2s, 1) sprotoloader.save(proto.s2c, 2) -- don't call skynet.exit() , because sproto.core may unload and the global slot become invalid end) ================================================ FILE: examples/share.lua ================================================ local skynet = require "skynet" local sharedata = require "skynet.sharedata" local mode = ... if mode == "host" then skynet.start(function() skynet.error("new foobar") sharedata.new("foobar", { a=1, b= { "hello", "world" } }) skynet.fork(function() skynet.sleep(200) -- sleep 2s skynet.error("update foobar a = 2") sharedata.update("foobar", { a =2 }) skynet.sleep(200) -- sleep 2s skynet.error("update foobar a = 3") sharedata.update("foobar", { a = 3, b = { "change" } }) skynet.sleep(100) skynet.error("delete foobar") sharedata.delete "foobar" end) end) else skynet.start(function() skynet.newservice(SERVICE_NAME, "host") local obj = sharedata.query "foobar" local b = obj.b skynet.error(string.format("a=%d", obj.a)) for k,v in ipairs(b) do skynet.error(string.format("b[%d]=%s", k,v)) end -- test lua serialization local s = skynet.packstring(obj) local nobj = skynet.unpack(s) for k,v in pairs(nobj) do skynet.error(string.format("nobj[%s]=%s", k,v)) end for k,v in ipairs(nobj.b) do skynet.error(string.format("nobj.b[%d]=%s", k,v)) end for i = 1, 5 do skynet.sleep(100) skynet.error("second " ..i) for k,v in pairs(obj) do skynet.error(string.format("%s = %s", k , tostring(v))) end end local ok, err = pcall(function() local tmp = { b[1], b[2] } -- b is invalid , so pcall should failed end) if not ok then skynet.error(err) end -- obj. b is not the same with local b for k,v in ipairs(obj.b) do skynet.error(string.format("b[%d] = %s", k , tostring(v))) end collectgarbage() skynet.error("sleep") skynet.sleep(100) b = nil collectgarbage() skynet.error("sleep") skynet.sleep(100) skynet.exit() end) end ================================================ FILE: examples/simpledb.lua ================================================ local skynet = require "skynet" require "skynet.manager" -- import skynet.register local db = {} local command = {} function command.GET(key) return db[key] end function command.SET(key, value) local last = db[key] db[key] = value return last end skynet.start(function() skynet.dispatch("lua", function(session, address, cmd, ...) cmd = cmd:upper() if cmd == "PING" then assert(session == 0) local str = (...) if #str > 20 then str = str:sub(1,20) .. "...(" .. #str .. ")" end skynet.error(string.format("%s ping %s", skynet.address(address), str)) return end local f = command[cmd] if f then skynet.ret(skynet.pack(f(...))) else error(string.format("Unknown command %s", tostring(cmd))) end end) -- skynet.traceproto("lua", false) -- true off tracelog skynet.register "SIMPLEDB" end) ================================================ FILE: examples/simplemonitor.lua ================================================ local skynet = require "skynet" -- It's a simple service exit monitor, you can do something more when a service exit. local service_map = {} skynet.register_protocol { name = "client", id = skynet.PTYPE_CLIENT, -- PTYPE_CLIENT = 3 unpack = function() end, dispatch = function(_, address) local w = service_map[address] if w then for watcher in pairs(w) do skynet.redirect(watcher, address, "error", 0, "") end service_map[address] = false end end } local function monitor(session, watcher, command, service) assert(command, "WATCH") local w = service_map[service] if not w then if w == false then skynet.ret(skynet.pack(false)) return end w = {} service_map[service] = w end w[watcher] = true skynet.ret(skynet.pack(true)) end skynet.start(function() skynet.dispatch("lua", monitor) end) ================================================ FILE: examples/simpleweb.lua ================================================ local skynet = require "skynet" local socket = require "skynet.socket" local httpd = require "http.httpd" local sockethelper = require "http.sockethelper" local urllib = require "http.url" local table = table local string = string local mode, protocol = ... protocol = protocol or "http" if mode == "agent" then local function response(id, write, ...) local ok, err = httpd.write_response(write, ...) if not ok then -- if err == sockethelper.socket_error , that means socket closed. skynet.error(string.format("fd = %d, %s", id, err)) end end local SSLCTX_SERVER = nil local function gen_interface(protocol, fd) if protocol == "http" then return { init = nil, close = nil, read = sockethelper.readfunc(fd), write = sockethelper.writefunc(fd), } elseif protocol == "https" then local tls = require "http.tlshelper" if not SSLCTX_SERVER then SSLCTX_SERVER = tls.newctx() -- gen cert and key -- openssl req -x509 -newkey rsa:2048 -days 3650 -nodes -keyout server-key.pem -out server-cert.pem local certfile = skynet.getenv("certfile") or "./server-cert.pem" local keyfile = skynet.getenv("keyfile") or "./server-key.pem" print(certfile, keyfile) SSLCTX_SERVER:set_cert(certfile, keyfile) end local tls_ctx = tls.newtls("server", SSLCTX_SERVER) return { init = tls.init_responsefunc(fd, tls_ctx), close = tls.closefunc(tls_ctx), read = tls.readfunc(fd, tls_ctx), write = tls.writefunc(fd, tls_ctx), } else error(string.format("Invalid protocol: %s", protocol)) end end skynet.start(function() skynet.dispatch("lua", function (_,_,id) socket.start(id) local interface = gen_interface(protocol, id) if interface.init then interface.init() end -- limit request body size to 8192 (you can pass nil to unlimit) local code, url, method, header, body = httpd.read_request(interface.read, 8192) if code then if code ~= 200 then response(id, interface.write, code) else local tmp = {} if header.host then table.insert(tmp, string.format("host: %s", header.host)) end local path, query = urllib.parse(url) table.insert(tmp, string.format("path: %s", path)) if query then local q = urllib.parse_query(query) for k, v in pairs(q) do table.insert(tmp, string.format("query: %s= %s", k,v)) end end table.insert(tmp, "-----header----") for k,v in pairs(header) do table.insert(tmp, string.format("%s = %s",k,v)) end table.insert(tmp, "-----body----\n" .. body) response(id, interface.write, code, table.concat(tmp,"\n")) end else if url == sockethelper.socket_error then skynet.error("socket closed") else skynet.error(url) end end socket.close(id) if interface.close then interface.close() end end) end) else skynet.start(function() local agent = {} local protocol = "http" for i= 1, 20 do agent[i] = skynet.newservice(SERVICE_NAME, "agent", protocol) end local balance = 1 local id = socket.listen("0.0.0.0", 8001) skynet.error(string.format("Listen web port 8001 protocol:%s", protocol)) socket.start(id , function(id, addr) skynet.error(string.format("%s connected, pass it to agent :%08x", addr, agent[balance])) skynet.send(agent[balance], "lua", id) balance = balance + 1 if balance > #agent then balance = 1 end end) end) end ================================================ FILE: examples/simplewebsocket.lua ================================================ local skynet = require "skynet" local socket = require "skynet.socket" local service = require "skynet.service" local websocket = require "http.websocket" local handle = {} local MODE = ... if MODE == "agent" then function handle.connect(id) print("ws connect from: " .. tostring(id)) end function handle.handshake(id, header, url) local addr = websocket.addrinfo(id) print("ws handshake from: " .. tostring(id), "url", url, "addr:", addr) print("----header-----") for k,v in pairs(header) do print(k,v) end print("--------------") end function handle.message(id, msg, msg_type) assert(msg_type == "binary" or msg_type == "text") websocket.write(id, msg) end function handle.ping(id) print("ws ping from: " .. tostring(id) .. "\n") end function handle.pong(id) print("ws pong from: " .. tostring(id)) end function handle.close(id, code, reason) print("ws close from: " .. tostring(id), code, reason) end function handle.error(id) print("ws error from: " .. tostring(id)) end skynet.start(function () skynet.dispatch("lua", function (_,_, id, protocol, addr) local ok, err = websocket.accept(id, handle, protocol, addr) if not ok then print(err) end end) end) else local function simple_echo_client_service(protocol) local skynet = require "skynet" local websocket = require "http.websocket" local url = string.format("%s://127.0.0.1:9948/test_websocket", protocol) local ws_id = websocket.connect(url) while true do local msg = "hello world!" websocket.write(ws_id, msg) print(">: " .. msg) local resp, close_reason = websocket.read(ws_id) print("<: " .. (resp and resp or "[Close] " .. close_reason)) if not resp then print("echo server close.") break end websocket.ping(ws_id) skynet.sleep(100) end end skynet.start(function () local agent = {} for i= 1, 20 do agent[i] = skynet.newservice(SERVICE_NAME, "agent") end local balance = 1 local protocol = "ws" local id = socket.listen("0.0.0.0", 9948) skynet.error(string.format("Listen websocket port 9948 protocol:%s", protocol)) socket.start(id, function(id, addr) print(string.format("accept client socket_id: %s addr:%s", id, addr)) skynet.send(agent[balance], "lua", id, protocol, addr) balance = balance + 1 if balance > #agent then balance = 1 end end) -- test echo client service.new("websocket_echo_client", simple_echo_client_service, protocol) end) end ================================================ FILE: examples/userlog.lua ================================================ local skynet = require "skynet" require "skynet.manager" -- register protocol text before skynet.start would be better. skynet.register_protocol { name = "text", id = skynet.PTYPE_TEXT, unpack = skynet.tostring, dispatch = function(_, address, msg) print(string.format(":%08x(%.2f): %s", address, skynet.time(), msg)) end } skynet.register_protocol { name = "SYSTEM", id = skynet.PTYPE_SYSTEM, unpack = function(...) return ... end, dispatch = function() -- reopen signal print("SIGHUP") end } skynet.start(function() end) ================================================ FILE: examples/watchdog.lua ================================================ local skynet = require "skynet" local CMD = {} local SOCKET = {} local gate local agent = {} function SOCKET.open(fd, addr) skynet.error("New client from : " .. addr) agent[fd] = skynet.newservice("agent") skynet.call(agent[fd], "lua", "start", { gate = gate, client = fd, watchdog = skynet.self() }) end local function close_agent(fd) local a = agent[fd] agent[fd] = nil if a then skynet.call(gate, "lua", "kick", fd) -- disconnect never return skynet.send(a, "lua", "disconnect") end end function SOCKET.close(fd) print("socket close",fd) close_agent(fd) end function SOCKET.error(fd, msg) print("socket error",fd, msg) close_agent(fd) end function SOCKET.warning(fd, size) -- size K bytes havn't send out in fd print("socket warning", fd, size) end function SOCKET.data(fd, msg) end function CMD.start(conf) return skynet.call(gate, "lua", "open" , conf) end function CMD.close(fd) close_agent(fd) end skynet.start(function() skynet.dispatch("lua", function(session, source, cmd, subcmd, ...) if cmd == "socket" then local f = SOCKET[subcmd] f(...) -- socket api don't need return else local f = assert(CMD[cmd]) skynet.ret(skynet.pack(f(subcmd, ...))) end end) gate = skynet.newservice("gate") end) ================================================ FILE: lualib/compat10/cluster.lua ================================================ return require "skynet.cluster" ================================================ FILE: lualib/compat10/crypt.lua ================================================ return require "skynet.crypt" ================================================ FILE: lualib/compat10/datacenter.lua ================================================ return require "skynet.datacenter" ================================================ FILE: lualib/compat10/dns.lua ================================================ return require "skynet.dns" ================================================ FILE: lualib/compat10/memory.lua ================================================ return require "skynet.memory" ================================================ FILE: lualib/compat10/mongo.lua ================================================ return require "skynet.db.mongo" ================================================ FILE: lualib/compat10/mqueue.lua ================================================ return require "skynet.mqueue" ================================================ FILE: lualib/compat10/multicast.lua ================================================ return require "skynet.multicast" ================================================ FILE: lualib/compat10/mysql.lua ================================================ return require "skynet.db.mysql" ================================================ FILE: lualib/compat10/netpack.lua ================================================ return require "skynet.netpack" ================================================ FILE: lualib/compat10/profile.lua ================================================ return require "skynet.profile" ================================================ FILE: lualib/compat10/redis.lua ================================================ return require "skynet.db.redis" ================================================ FILE: lualib/compat10/sharedata.lua ================================================ return require "skynet.sharedata" ================================================ FILE: lualib/compat10/sharemap.lua ================================================ return require "skynet.sharemap" ================================================ FILE: lualib/compat10/snax.lua ================================================ return require "skynet.snax" ================================================ FILE: lualib/compat10/socket.lua ================================================ return require "skynet.socket" ================================================ FILE: lualib/compat10/socketchannel.lua ================================================ return require "skynet.socketchannel" ================================================ FILE: lualib/compat10/socketdriver.lua ================================================ return require "skynet.socketdriver" ================================================ FILE: lualib/compat10/stm.lua ================================================ return require "skynet.stm" ================================================ FILE: lualib/http/httpc.lua ================================================ local skynet = require "skynet" local socket = require "http.sockethelper" local internal = require "http.internal" local dns = require "skynet.dns" local string = string local table = table local pcall = pcall local error = error local pairs = pairs local httpc = {} local async_dns function httpc.dns(server,port) async_dns = true dns.server(server,port) end local default_port = { http = 80, https = 443, } local function hostname_port(host) if host:find ".*:.*:" then -- If host contains 2 or more ":", it's ipv6 address local ipv6, port = host:match "^%[(.-)%]:(%d+)$" if ipv6 then return ipv6, port else return host end end local hostname, port = host:match "(.-):(%d+)$" if hostname then return hostname, port end return host end local function check_protocol(host) local protocol, hostname = host:match "^(%a+)://(.*)" if protocol then protocol = string.lower(protocol) else protocol = "http" hostname = host end hostname, port = hostname_port(hostname) return protocol, hostname, port or default_port[protocol] or error("Invalid protocol: " .. protocol) end local SSLCTX_CLIENT = nil local function gen_interface(protocol, fd, hostname) if protocol == "http" then return { init = nil, close = nil, read = socket.readfunc(fd), write = socket.writefunc(fd), readall = function () return socket.readall(fd) end, } elseif protocol == "https" then local tls = require "http.tlshelper" SSLCTX_CLIENT = SSLCTX_CLIENT or tls.newctx() local tls_ctx = tls.newtls("client", SSLCTX_CLIENT, hostname) return { init = tls.init_requestfunc(fd, tls_ctx), close = tls.closefunc(tls_ctx), read = tls.readfunc(fd, tls_ctx), write = tls.writefunc(fd, tls_ctx), readall = tls.readallfunc(fd, tls_ctx), } else error(string.format("Invalid protocol: %s", protocol)) end end local function connect(host, timeout) local protocol, host, port = check_protocol(host) local hostaddr = host local hostname if host:find "^[^:]-%D$" then -- it's a hostname (not ip address), because -- ipv6 contains colons -- ipv4 end with a digit hostname = host if async_dns then local msg hostaddr, msg = dns.resolve(host) if not hostaddr then error(string.format("%s dns resolve failed msg:%s", host, msg)) end end end local fd = socket.connect(hostaddr, port, timeout) if not fd then error(string.format("%s connect error host:%s, port:%s, timeout:%s", protocol, host, port, timeout)) end local interface = gen_interface(protocol, fd, hostname) if timeout then skynet.timeout(timeout, function() if not interface.finish then socket.shutdown(fd) -- shutdown the socket fd, need close later. end end) end if interface.init then interface.init(host) end return fd, interface, host end local function close_interface(interface, fd) interface.finish = true socket.close(fd) if interface.close then interface.close() interface.close = nil end end function httpc.request(method, hostname, url, recvheader, header, content) local fd, interface, host = connect(hostname, httpc.timeout) local ok , statuscode, body , header = pcall(internal.request, interface, method, host, url, recvheader, header, content) if ok then ok, body = pcall(internal.response, interface, statuscode, body, header) end close_interface(interface, fd) if ok then return statuscode, body else error(body or statuscode) end end function httpc.head(hostname, url, recvheader, header, content) local fd, interface, host = connect(hostname, httpc.timeout) local ok , statuscode = pcall(internal.request, interface, "HEAD", host, url, recvheader, header, content) close_interface(interface, fd) if ok then return statuscode else error(statuscode) end end function httpc.request_stream(method, hostname, url, recvheader, header, content) local fd, interface, host = connect(hostname, httpc.timeout) local ok , statuscode, body , header = pcall(internal.request, interface, method, host, url, recvheader, header, content) interface.finish = true -- don't shutdown fd in timeout local function close_fd() close_interface(interface, fd) end if not ok then close_fd() error(statuscode) end -- todo: stream support timeout local stream = internal.response_stream(interface, statuscode, body, header) stream._onclose = close_fd return stream end function httpc.get(...) return httpc.request("GET", ...) end local function escape(s) return (string.gsub(s, "([^A-Za-z0-9_])", function(c) return string.format("%%%02X", string.byte(c)) end)) end function httpc.post(host, url, form, recvheader) local header = { ["content-type"] = "application/x-www-form-urlencoded" } local body = {} for k,v in pairs(form) do table.insert(body, string.format("%s=%s",escape(k),escape(v))) end return httpc.request("POST", host, url, recvheader, header, table.concat(body , "&")) end return httpc ================================================ FILE: lualib/http/httpd.lua ================================================ local internal = require "http.internal" local string = string local type = type local assert = assert local tonumber = tonumber local pcall = pcall local ipairs = ipairs local pairs = pairs local httpd = {} local http_status_msg = { [100] = "Continue", [101] = "Switching Protocols", [200] = "OK", [201] = "Created", [202] = "Accepted", [203] = "Non-Authoritative Information", [204] = "No Content", [205] = "Reset Content", [206] = "Partial Content", [300] = "Multiple Choices", [301] = "Moved Permanently", [302] = "Found", [303] = "See Other", [304] = "Not Modified", [305] = "Use Proxy", [307] = "Temporary Redirect", [400] = "Bad Request", [401] = "Unauthorized", [402] = "Payment Required", [403] = "Forbidden", [404] = "Not Found", [405] = "Method Not Allowed", [406] = "Not Acceptable", [407] = "Proxy Authentication Required", [408] = "Request Time-out", [409] = "Conflict", [410] = "Gone", [411] = "Length Required", [412] = "Precondition Failed", [413] = "Request Entity Too Large", [414] = "Request-URI Too Large", [415] = "Unsupported Media Type", [416] = "Requested range not satisfiable", [417] = "Expectation Failed", [500] = "Internal Server Error", [501] = "Not Implemented", [502] = "Bad Gateway", [503] = "Service Unavailable", [504] = "Gateway Time-out", [505] = "HTTP Version not supported", } local function readall(readbytes, bodylimit) local tmpline = {} local body = internal.recvheader(readbytes, tmpline, "") if not body then return 413 -- Request Entity Too Large end local request = assert(tmpline[1]) local method, url, httpver = request:match "^(%a+)%s+(.-)%s+HTTP/([%d%.]+)$" assert(method and url and httpver) httpver = assert(tonumber(httpver)) if httpver < 1.0 or httpver > 1.1 then return 505 -- HTTP Version not supported end local header = internal.parseheader(tmpline,2,{}) if not header then return 400 -- Bad request end local length = header["content-length"] if length then length = tonumber(length) end local mode = header["transfer-encoding"] if mode then if mode ~= "identity" and mode ~= "chunked" then return 501 -- Not Implemented end end if mode == "chunked" then body, header = internal.recvchunkedbody(readbytes, bodylimit, header, body) if not body then return 413 end else -- identity mode if length then if bodylimit and length > bodylimit then return 413 end if #body >= length then body = body:sub(1,length) else local padding = readbytes(length - #body) body = body .. padding end end end return 200, url, method, header, body end function httpd.read_request(...) local ok, code, url, method, header, body = pcall(readall, ...) if ok then return code, url, method, header, body else return nil, code end end local function writeall(writefunc, statuscode, bodyfunc, header) local statusline = string.format("HTTP/1.1 %03d %s\r\n", statuscode, http_status_msg[statuscode] or "") writefunc(statusline) if header then for k,v in pairs(header) do if type(v) == "table" then for _,v in ipairs(v) do writefunc(string.format("%s: %s\r\n", k,v)) end else writefunc(string.format("%s: %s\r\n", k,v)) end end end local t = type(bodyfunc) if t == "string" then writefunc(string.format("content-length: %d\r\n\r\n", #bodyfunc)) writefunc(bodyfunc) elseif t == "function" then writefunc("transfer-encoding: chunked\r\n") while true do local s = bodyfunc() if s then if s ~= "" then writefunc(string.format("\r\n%x\r\n", #s)) writefunc(s) end else writefunc("\r\n0\r\n\r\n") break end end else assert(t == "nil") writefunc("\r\n") end end function httpd.write_response(...) return pcall(writeall, ...) end return httpd ================================================ FILE: lualib/http/internal.lua ================================================ local table = table local type = type local string = string local tonumber = tonumber local pcall = pcall local assert = assert local error = error local pairs = pairs local M = {} local LIMIT = 8192 local function chunksize(readbytes, body) while true do local f,e = body:find("\r\n",1,true) if f then return tonumber(body:sub(1,f-1),16), body:sub(e+1) end if #body > 128 then -- pervent the attacker send very long stream without \r\n return end body = body .. readbytes() end end local function readcrln(readbytes, body) if #body >= 2 then if body:sub(1,2) ~= "\r\n" then return end return body:sub(3) else body = body .. readbytes(2-#body) if body ~= "\r\n" then return end return "" end end function M.recvheader(readbytes, lines, header) if #header >= 2 then if header:find "^\r\n" then return header:sub(3) end end local result local e = header:find("\r\n\r\n", 1, true) if e then result = header:sub(e+4) else while true do local bytes = readbytes() header = header .. bytes e = header:find("\r\n\r\n", -#bytes-3, true) if e then result = header:sub(e+4) break end if header:find "^\r\n" then return header:sub(3) end if #header > LIMIT then return end end end for v in header:gmatch("(.-)\r\n") do if v == "" then break end table.insert(lines, v) end return result end function M.parseheader(lines, from, header) local name, value for i=from,#lines do local line = lines[i] if line:byte(1) == 9 then -- tab, append last line if name == nil then return end header[name] = header[name] .. line:sub(2) else name, value = line:match "^(.-):%s*(.*)" if name == nil or value == nil then return end name = name:lower() if header[name] then local v = header[name] if type(v) == "table" then table.insert(v, value) else header[name] = { v , value } end else header[name] = value end end end return header end function M.recvchunkedbody(readbytes, bodylimit, header, body) local result = "" local size = 0 while true do local sz sz , body = chunksize(readbytes, body) if not sz then return end if sz == 0 then break end size = size + sz if bodylimit and size > bodylimit then return end if #body >= sz then result = result .. body:sub(1,sz) body = body:sub(sz+1) else result = result .. body .. readbytes(sz - #body) body = "" end body = readcrln(readbytes, body) if not body then return end end local tmpline = {} body = M.recvheader(readbytes, tmpline, body) if not body then return end header = M.parseheader(tmpline,1,header) return result, header end local function recvbody(interface, code, header, body) local length = header["content-length"] if length then length = tonumber(length) end if length then if #body >= length then body = body:sub(1,length) else local padding = interface.read(length - #body) body = body .. padding end elseif code == 204 or code == 304 or code < 200 then body = "" -- See https://stackoverflow.com/questions/15991173/is-the-content-length-header-required-for-a-http-1-0-response else -- no content-length, read all body = body .. interface.readall() end return body end function M.request(interface, method, host, url, recvheader, header, content) local read = interface.read local write = interface.write local header_content = "" if header then if not header.Host then header.Host = host end for k,v in pairs(header) do header_content = string.format("%s%s:%s\r\n", header_content, k, v) end else header_content = string.format("host:%s\r\n",host) end if content then local data if header and header["transfer-encoding"] == "chunked" then data = string.format("%s %s HTTP/1.1\r\n%s\r\n", method, url, header_content) else data = string.format("%s %s HTTP/1.1\r\n%sContent-length:%d\r\n\r\n", method, url, header_content, #content) end write(data) write(content) else local request_header = string.format("%s %s HTTP/1.1\r\n%sContent-length:0\r\n\r\n", method, url, header_content) write(request_header) end local tmpline = {} local body = M.recvheader(read, tmpline, "") if not body then error("Recv header failed") end local statusline = tmpline[1] local code, info = statusline:match "HTTP/[%d%.]+%s+([%d]+)%s+(.*)$" code = assert(tonumber(code)) local header = M.parseheader(tmpline,2,recvheader or {}) if not header then error("Invalid HTTP response header") end return code, body, header end function M.response(interface, code, body, header) local mode = header["transfer-encoding"] if mode then if mode ~= "identity" and mode ~= "chunked" then error ("Unsupport transfer-encoding") end end if mode == "chunked" then body, header = M.recvchunkedbody(interface.read, nil, header, body) if not body then error("Invalid response body") end else -- identity mode body = recvbody(interface, code, header, body) end return body end local stream = {}; stream.__index = stream function stream:close() if self._onclose then self._onclose(self) self._onclose = nil end end function stream:padding() return self._reading(self), self end stream.__close = stream.close stream.__call = stream.padding local function stream_nobody(stream) stream._reading = stream.close stream.connected = nil return "" end local function stream_length(length) return function(stream) local body = stream._body if body == nil then local ret, padding = stream._interface.read() if not ret then -- disconnected body = padding stream.connected = false else body = ret end end local n = #body if n >= length then stream._reading = stream.close stream.connected = nil return (body:sub(1,length)) else length = length - n stream._body = nil if not stream.connected then stream._reading = stream.close end return body end end end local function stream_read(stream) local ret, padding = stream._interface.read() if ret == "" or not ret then stream.connected = nil stream:close() if padding == "" then return end return padding end return ret end local function stream_all(stream) local body = stream._body stream._body = nil stream._reading = stream_read return body end local function stream_chunked(stream) local read = stream._interface.read local sz, body = chunksize(read, stream._body) if not sz then stream.connected = false stream:close() return end if sz == 0 then -- last chunk local tmpline = {} body = M.recvheader(read, tmpline, body) if not body then stream.connected = false stream:close() return end M.parseheader(tmpline,1, stream.header) stream._reading = stream.close stream.connected = nil return "" end local n = #body local remain if n >= sz then remain = body:sub(sz+1) body = body:sub(1,sz) else body = body .. read(sz - n) remain = "" end remain = readcrln(read, remain) if not remain then stream.connected = false stream:close() return end stream._body = remain return body end function M.response_stream(interface, code, body, header) local mode = header["transfer-encoding"] if mode then if mode ~= "identity" and mode ~= "chunked" then error ("Unsupport transfer-encoding") end end local read_func if mode == "chunked" then read_func = stream_chunked else -- identity mode local length = header["content-length"] if length then length = tonumber(length) end if length then read_func = stream_length(length) elseif code == 204 or code == 304 or code < 200 then read_func = stream_nobody else read_func = stream_all end end -- todo: timeout return setmetatable({ status = code, _body = body, _interface = interface, _reading = read_func, header = header, connected = true, }, stream) end return M ================================================ FILE: lualib/http/sockethelper.lua ================================================ local socket = require "skynet.socket" local skynet = require "skynet" local coroutine = coroutine local error = error local tostring = tostring local readbytes = socket.read local writebytes = socket.write local sockethelper = {} local socket_error = setmetatable({} , { __tostring = function(self) local info = self.err_info self.err_info = nil return info or "[Socket Error]" end, __call = function (self, info) self.err_info = "[Socket Error] : " .. tostring(info) return self end }) sockethelper.socket_error = socket_error local function preread(fd, str) return function (sz) if str then if sz == #str or sz == nil then local ret = str str = nil return ret else if sz < #str then local ret = str:sub(1,sz) str = str:sub(sz + 1) return ret else sz = sz - #str local ret = readbytes(fd, sz) if ret then return str .. ret else error(socket_error("read failed fd = " .. fd)) end end end else local ret = readbytes(fd, sz) if ret then return ret else error(socket_error("read failed fd = " .. fd)) end end end end function sockethelper.readfunc(fd, pre) if pre then return preread(fd, pre) end return function (sz) local ret = readbytes(fd, sz) if ret then return ret else error(socket_error("read failed fd = " .. fd)) end end end sockethelper.readall = socket.readall function sockethelper.writefunc(fd) return function(content) local ok = writebytes(fd, content) if not ok then error(socket_error("write failed fd = " .. fd)) end end end function sockethelper.connect(host, port, timeout) local fd, err local is_time_out = false if timeout then is_time_out = true local drop_fd local co = coroutine.running() -- asynchronous connect skynet.fork(function() fd, err = socket.open(host, port) if drop_fd then -- sockethelper.connect already return, and raise socket_error socket.close(fd) else -- socket.open before sleep, wakeup. is_time_out = false skynet.wakeup(co) end end) skynet.sleep(timeout) if not fd then -- not connect yet drop_fd = true end else is_time_out = false -- block connect fd = socket.open(host, port) end if fd then return fd end error(socket_error("connect failed host = " .. host .. ' port = '.. port .. ' timeout = ' .. tostring(timeout) .. ' err = ' .. tostring(err) .. ' is_time_out = '.. tostring(is_time_out))) end function sockethelper.close(fd) socket.close(fd) end function sockethelper.shutdown(fd) socket.shutdown(fd) end return sockethelper ================================================ FILE: lualib/http/tlshelper.lua ================================================ local socket = require "http.sockethelper" local c = require "ltls.c" local tlshelper = {} function tlshelper.init_requestfunc(fd, tls_ctx) local readfunc = socket.readfunc(fd) local writefunc = socket.writefunc(fd) return function (hostname) tls_ctx:set_ext_host_name(hostname) local ds1 = tls_ctx:handshake() writefunc(ds1) while not tls_ctx:finished() do local ds2 = readfunc() local ds3 = tls_ctx:handshake(ds2) if ds3 then writefunc(ds3) end end end end function tlshelper.init_responsefunc(fd, tls_ctx) local readfunc = socket.readfunc(fd) local writefunc = socket.writefunc(fd) return function () while not tls_ctx:finished() do local ds1 = readfunc() local ds2 = tls_ctx:handshake(ds1) if ds2 then writefunc(ds2) end end local ds3 = tls_ctx:write() writefunc(ds3) end end function tlshelper.closefunc(tls_ctx) return function () tls_ctx:close() end end function tlshelper.readfunc(fd, tls_ctx) local function readfunc() readfunc = socket.readfunc(fd) return "" end local read_buff = "" return function (sz) if not sz then local s = "" if #read_buff == 0 then local ds = readfunc() s = tls_ctx:read(ds) end s = read_buff .. s read_buff = "" return s else while #read_buff < sz do local ds = readfunc() local s = tls_ctx:read(ds) read_buff = read_buff .. s end local s = string.sub(read_buff, 1, sz) read_buff = string.sub(read_buff, sz+1, #read_buff) return s end end end function tlshelper.writefunc(fd, tls_ctx) local writefunc = socket.writefunc(fd) return function (s) local ds = tls_ctx:write(s) return writefunc(ds) end end function tlshelper.readallfunc(fd, tls_ctx) return function () local ds = socket.readall(fd) local s = tls_ctx:read(ds) return s end end function tlshelper.newctx() return c.newctx() end function tlshelper.newtls(method, ssl_ctx, hostname) return c.newtls(method, ssl_ctx, hostname) end return tlshelper ================================================ FILE: lualib/http/url.lua ================================================ local url = {} local function decode_func(c) return string.char(tonumber(c, 16)) end local function decode(str) local str = str:gsub('+', ' ') return str:gsub("%%(..)", decode_func) end function url.parse(u) local path,query = u:match "([^?]*)%??(.*)" if path then path = decode(path) end return path, query end function url.parse_query(q) local r = {} for k,v in q:gmatch "(.-)=([^&]*)&?" do local dk, dv = decode(k), decode(v) local oldv = r[dk] if oldv then if type(oldv) ~= "table" then r[dk] = {oldv, dv} else oldv[#oldv+1] = dv end else r[dk] = dv end end return r end return url ================================================ FILE: lualib/http/websocket.lua ================================================ local internal = require "http.internal" local socket = require "skynet.socket" local crypt = require "skynet.crypt" local httpd = require "http.httpd" local skynet = require "skynet" local sockethelper = require "http.sockethelper" local socket_error = sockethelper.socket_error local GLOBAL_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" local MAX_FRAME_SIZE = 256 * 1024 -- max frame is 256K local assert = assert local pairs = pairs local error = error local string = string local xpcall = xpcall local pcall = pcall local debug = debug local table = table local tonumber = tonumber local M = {} local ws_pool = {} local function _close_websocket(ws_obj) local id = ws_obj.id assert(ws_pool[id] == ws_obj) ws_pool[id] = nil ws_obj.close() end local function _isws_closed(id) return not ws_pool[id] end local function reader_with_payload(self, payload) local sz_payload = #payload if sz_payload == 0 then return end local read = self.read function self.read (sz) if sz == nil or sz == sz_payload then self.read = read return payload end if sz < sz_payload then local ret = payload:sub(1, sz) payload = payload:sub(sz + 1) sz_payload = #payload return ret end self.read = read return payload .. read(sz - sz_payload) end end local function write_handshake(self, host, url, header) local key = crypt.base64encode(crypt.randomkey()..crypt.randomkey()) local request_header = { ["Upgrade"] = "websocket", ["Connection"] = "Upgrade", ["Sec-WebSocket-Version"] = "13", ["Sec-WebSocket-Key"] = key } if header then for k,v in pairs(header) do assert(request_header[k] == nil, k) request_header[k] = v end end local recvheader = {} local code, payload = internal.request(self, "GET", host, url, recvheader, request_header) if code ~= 101 then error(string.format("websocket handshake error: code[%s] info:%s", code, payload)) end reader_with_payload(self, payload) if not recvheader["upgrade"] or recvheader["upgrade"]:lower() ~= "websocket" then error("websocket handshake upgrade must websocket") end if not recvheader["connection"] or recvheader["connection"]:lower() ~= "upgrade" then error("websocket handshake connection must upgrade") end local sw_key = recvheader["sec-websocket-accept"] if not sw_key then error("websocket handshake need Sec-WebSocket-Accept") end local guid = self.guid sw_key = crypt.base64decode(sw_key) if sw_key ~= crypt.sha1(key .. guid) then error("websocket handshake invalid Sec-WebSocket-Accept") end end local function read_handshake(self, upgrade_ops) local header, method, url if upgrade_ops then header, method, url = upgrade_ops.header, upgrade_ops.method, upgrade_ops.url else local tmpline = {} local payload = internal.recvheader(self.read, tmpline, "") if not payload then return 413 end reader_with_payload(self, payload) local request = assert(tmpline[1]) local httpver method, url, httpver = request:match "^(%a+)%s+(.-)%s+HTTP/([%d%.]+)$" assert(method and url and httpver) if method ~= "GET" then return 400, "need GET method" end httpver = assert(tonumber(httpver)) if httpver < 1.1 then return 505 -- HTTP Version not supported end header = internal.parseheader(tmpline, 2, {}) end if not header then return 400 -- Bad request end if not header["upgrade"] or header["upgrade"]:lower() ~= "websocket" then return 426, "Upgrade Required" end if not header["host"] then return 400, "host Required" end if not header["connection"] or not header["connection"]:lower():find("upgrade", 1,true) then return 400, "Connection must Upgrade" end local sw_key = header["sec-websocket-key"] if not sw_key then return 400, "Sec-WebSocket-Key Required" else local raw_key = crypt.base64decode(sw_key) if #raw_key ~= 16 then return 400, "Sec-WebSocket-Key invalid" end end if not header["sec-websocket-version"] or header["sec-websocket-version"] ~= "13" then return 400, "Sec-WebSocket-Version must 13" end local sw_protocol = header["sec-websocket-protocol"] local sub_pro = "" if sw_protocol then local has_chat = false for sub_protocol in string.gmatch(sw_protocol, "[^%s,]+") do if sub_protocol == "chat" then sub_pro = "Sec-WebSocket-Protocol: chat\r\n" has_chat = true break end end if not has_chat then return 400, "Sec-WebSocket-Protocol need include chat" end end -- read 'x-real-ip' header from nginx self.real_ip = header["x-real-ip"] -- response handshake local accept = crypt.base64encode(crypt.sha1(sw_key .. self.guid)) local resp = "HTTP/1.1 101 Switching Protocols\r\n".. "Upgrade: websocket\r\n".. "Connection: Upgrade\r\n".. string.format("Sec-WebSocket-Accept: %s\r\n", accept).. sub_pro .. "\r\n" self.write(resp) return nil, header, url end local function try_handle(self, method, ...) local handle = self.handle local f = handle and handle[method] if f then f(self.id, ...) end end local op_code = { ["frame"] = 0x00, ["text"] = 0x01, ["binary"] = 0x02, ["close"] = 0x08, ["ping"] = 0x09, ["pong"] = 0x0A, [0x00] = "frame", [0x01] = "text", [0x02] = "binary", [0x08] = "close", [0x09] = "ping", [0x0A] = "pong", } local function write_frame(self, op, payload_data, masking_key) payload_data = payload_data or "" local payload_len = #payload_data local op_v = assert(op_code[op]) local v1 = 0x80 | op_v -- fin is 1 with opcode local s local mask = masking_key and 0x80 or 0x00 -- mask set to 0 if payload_len < 126 then s = string.pack("I1I1", v1, mask | payload_len) elseif payload_len <= 0xffff then s = string.pack("I1I1>I2", v1, mask | 126, payload_len) else s = string.pack("I1I1>I8", v1, mask | 127, payload_len) end self.write(s) -- write masking_key if masking_key then s = string.pack(">I4", masking_key) self.write(s) payload_data = crypt.xor_str(payload_data, s) end if payload_len > 0 then self.write(payload_data) end end local function read_close(payload_data) local code, reason local payload_len = #payload_data if payload_len > 2 then local fmt = string.format(">I2c%d", payload_len - 2) code, reason = string.unpack(fmt, payload_data) end return code, reason end local function read_frame(self) local s = self.read(2) local v1, v2 = string.unpack("I1I1", s) local fin = (v1 & 0x80) ~= 0 -- unused flag -- local rsv1 = (v1 & 0x40) ~= 0 -- local rsv2 = (v1 & 0x20) ~= 0 -- local rsv3 = (v1 & 0x10) ~= 0 local op = v1 & 0x0f local mask = (v2 & 0x80) ~= 0 local payload_len = (v2 & 0x7f) if payload_len == 126 then s = self.read(2) payload_len = string.unpack(">I2", s) elseif payload_len == 127 then s = self.read(8) payload_len = string.unpack(">I8", s) end if self.mode == "server" and payload_len > MAX_FRAME_SIZE then error("payload_len is too large") end -- print(string.format("fin:%s, op:%s, mask:%s, payload_len:%s", fin, op_code[op], mask, payload_len)) local masking_key = mask and self.read(4) or false local payload_data = payload_len>0 and self.read(payload_len) or "" payload_data = masking_key and crypt.xor_str(payload_data, masking_key) or payload_data return fin, assert(op_code[op]), payload_data end local function resolve_accept(self, options) try_handle(self, "connect") local code, err, url = read_handshake(self, options and options.upgrade) if code then local ok, s = httpd.write_response(self.write, code, err) if not ok then error(s) end try_handle(self, "close") return end local header = err try_handle(self, "handshake", header, url) local recv_count = 0 local recv_buf = {} local first_op while true do if _isws_closed(self.id) then try_handle(self, "close") return end local fin, op, payload_data = read_frame(self) if op == "close" then local code, reason = read_close(payload_data) write_frame(self, "close") try_handle(self, "close", code, reason) break elseif op == "ping" then write_frame(self, "pong", payload_data) try_handle(self, "ping") elseif op == "pong" then try_handle(self, "pong") else if fin and #recv_buf == 0 then try_handle(self, "message", payload_data, op) else recv_buf[#recv_buf+1] = payload_data recv_count = recv_count + #payload_data if recv_count > MAX_FRAME_SIZE then error("payload_len is too large") end first_op = first_op or op if fin then local s = table.concat(recv_buf) try_handle(self, "message", s, first_op) recv_buf = {} -- clear recv_buf recv_count = 0 first_op = nil end end end end end local SSLCTX_CLIENT = nil local function _new_client_ws(socket_id, protocol, hostname) local obj if protocol == "ws" then obj = { close = function () socket.close(socket_id) end, read = sockethelper.readfunc(socket_id), write = sockethelper.writefunc(socket_id), readall = function () return socket.readall(socket_id) end, } elseif protocol == "wss" then local tls = require "http.tlshelper" SSLCTX_CLIENT = SSLCTX_CLIENT or tls.newctx() local tls_ctx = tls.newtls("client", SSLCTX_CLIENT, hostname) local init = tls.init_requestfunc(socket_id, tls_ctx) init() obj = { close = function () socket.close(socket_id) tls.closefunc(tls_ctx)() end, read = tls.readfunc(socket_id, tls_ctx), write = tls.writefunc(socket_id, tls_ctx), readall = tls.readallfunc(socket_id, tls_ctx), } else error(string.format("invalid websocket protocol:%s", tostring(protocol))) end obj.mode = "client" obj.id = assert(socket_id) obj.guid = GLOBAL_GUID ws_pool[socket_id] = obj return obj end local SSLCTX_SERVER = nil local function _new_server_ws(socket_id, handle, protocol) local obj if protocol == "ws" then obj = { close = function () socket.close(socket_id) end, read = sockethelper.readfunc(socket_id), write = sockethelper.writefunc(socket_id), } elseif protocol == "wss" then local tls = require "http.tlshelper" if not SSLCTX_SERVER then SSLCTX_SERVER = tls.newctx() -- gen cert and key -- openssl req -x509 -newkey rsa:2048 -days 3650 -nodes -keyout server-key.pem -out server-cert.pem local certfile = skynet.getenv("certfile") or "./server-cert.pem" local keyfile = skynet.getenv("keyfile") or "./server-key.pem" SSLCTX_SERVER:set_cert(certfile, keyfile) end local tls_ctx = tls.newtls("server", SSLCTX_SERVER) local init = tls.init_responsefunc(socket_id, tls_ctx) init() obj = { close = function () socket.close(socket_id) tls.closefunc(tls_ctx)() end, read = tls.readfunc(socket_id, tls_ctx), write = tls.writefunc(socket_id, tls_ctx), } else error(string.format("invalid websocket protocol:%s", tostring(protocol))) end obj.mode = "server" obj.id = assert(socket_id) obj.handle = handle obj.guid = GLOBAL_GUID ws_pool[socket_id] = obj return obj end -- handle interface -- connect / handshake / message / ping / pong / close / error function M.accept(socket_id, handle, protocol, addr, options) if not (options and options.upgrade) then local isok, err = socket.start(socket_id) if not isok then return false, err end end protocol = protocol or "ws" local ws_obj = _new_server_ws(socket_id, handle, protocol) ws_obj.addr = addr local on_warning = handle and handle["warning"] if on_warning then local isok = pcall(socket.warning, socket_id, function(id, sz) on_warning(ws_obj, sz) end) if not isok then if not _isws_closed(socket_id) then _close_websocket(ws_obj) end return false, "connect is closed " .. socket_id end end local ok, err = xpcall(resolve_accept, debug.traceback, ws_obj, options) local closed = _isws_closed(socket_id) if not closed then _close_websocket(ws_obj) end if not ok then if err == socket_error then if closed then try_handle(ws_obj, "close") else try_handle(ws_obj, "error", err) end else -- error(err) return false, err end end return true end function M.connect(url, header, timeout) local protocol, host, uri = string.match(url, "^(wss?)://([^/]+)(.*)$") if protocol ~= "wss" and protocol ~= "ws" then error(string.format("invalid protocol: %s", protocol)) end assert(host) local host_addr, host_port = string.match(host, "^([^:]+):?(%d*)$") assert(host_addr and host_port) if host_port == "" then host_port = protocol == "ws" and 80 or 443 end local hostname if not host_addr:match(".*%d+$") then hostname = host_addr end uri = uri == "" and "/" or uri local socket_id = sockethelper.connect(host_addr, host_port, timeout) local ws_obj = _new_client_ws(socket_id, protocol, hostname) ws_obj.addr = host local is_ok,err = pcall(write_handshake, ws_obj, host_addr, uri, header) if not is_ok then _close_websocket(ws_obj) error(err) end return socket_id end function M.read(id) local ws_obj = assert(ws_pool[id]) local recv_buf while true do local fin, op, payload_data = read_frame(ws_obj) if op == "close" then _close_websocket(ws_obj) return false, payload_data elseif op == "ping" then write_frame(ws_obj, "pong", payload_data) elseif op ~= "pong" then -- op is frame, text binary if fin and not recv_buf then return payload_data else recv_buf = recv_buf or {} recv_buf[#recv_buf+1] = payload_data if fin then local s = table.concat(recv_buf) return s end end end end end function M.write(id, data, fmt, masking_key) local ws_obj = assert(ws_pool[id]) fmt = fmt or "text" assert(fmt == "text" or fmt == "binary") write_frame(ws_obj, fmt, data, masking_key) end function M.ping(id) local ws_obj = assert(ws_pool[id]) write_frame(ws_obj, "ping") end function M.addrinfo(id) local ws_obj = assert(ws_pool[id]) return ws_obj.addr end function M.real_ip(id) local ws_obj = assert(ws_pool[id]) return ws_obj.real_ip end function M.close(id, code ,reason) local ws_obj = ws_pool[id] if not ws_obj then return end local ok, err = xpcall(function () reason = reason or "" local payload_data if code then local fmt =string.format(">I2c%d", #reason) payload_data = string.pack(fmt, code, reason) end write_frame(ws_obj, "close", payload_data) end, debug.traceback) _close_websocket(ws_obj) if not ok then skynet.error(err) end end M.is_close = _isws_closed return M ================================================ FILE: lualib/loader.lua ================================================ local args = {} for word in string.gmatch(..., "%S+") do table.insert(args, word) end SERVICE_NAME = args[1] local main, pattern local err = {} for pat in string.gmatch(LUA_SERVICE, "([^;]+);*") do local filename = string.gsub(pat, "?", SERVICE_NAME) local f, msg = loadfile(filename) if not f then table.insert(err, msg) else pattern = pat main = f break end end if not main then error(table.concat(err, "\n")) end LUA_SERVICE = nil package.path , LUA_PATH = LUA_PATH, nil package.cpath , LUA_CPATH = LUA_CPATH, nil local service_path = string.match(pattern, "(.*/)[^/?]+$") if service_path then service_path = string.gsub(service_path, "?", args[1]) package.path = service_path .. "?.lua;" .. package.path SERVICE_PATH = service_path else local p = string.match(pattern, "(.*/).+$") SERVICE_PATH = p end if LUA_PRELOAD then local f = assert(loadfile(LUA_PRELOAD)) f(table.unpack(args)) LUA_PRELOAD = nil end _G.require = (require "skynet.require").require main(select(2, table.unpack(args))) ================================================ FILE: lualib/md5.lua ================================================ ---------------------------------------------------------------------------- -- Modify version from https://github.com/keplerproject/md5 ---------------------------------------------------------------------------- local core = require "md5.core" ---------------------------------------------------------------------------- -- @param k String with original message. -- @return String with the md5 hash value converted to hexadecimal digits function core.sumhexa (k) k = core.sum(k) return (string.gsub(k, ".", function (c) return string.format("%02x", string.byte(c)) end)) end local function get_ipad(c) return string.char(c:byte() ~ 0x36) end local function get_opad(c) return string.char(c:byte() ~ 0x5c) end function core.hmacmd5(data,key) if #key>64 then key=core.sum(key) key=key:sub(1,16) end local ipad_s=key:gsub(".", get_ipad)..string.rep("6",64-#key) local opad_s=key:gsub(".", get_opad)..string.rep("\\",64-#key) local istr=core.sum(ipad_s..data) local ostr=core.sumhexa(opad_s..istr) return ostr end return core ================================================ FILE: lualib/skynet/cluster.lua ================================================ local skynet = require "skynet" local clusterd local cluster = {} local sender = {} local task_queue = {} local function repack(address, ...) return address, skynet.pack(...) end local function request_sender(q, node) local ok, c = pcall(skynet.call, clusterd, "lua", "sender", node) if not ok then skynet.error(c) c = nil end -- run tasks in queue local confirm = coroutine.running() q.confirm = confirm q.sender = c for _, task in ipairs(q) do if type(task) == "string" then if c then skynet.send(c, "lua", "push", repack(skynet.unpack(task))) end else skynet.wakeup(task) skynet.wait(confirm) end end task_queue[node] = nil sender[node] = c end local function get_queue(t, node) local q = {} t[node] = q skynet.fork(request_sender, q, node) return q end setmetatable(task_queue, { __index = get_queue } ) local function get_sender(node) local s = sender[node] if not s then local q = task_queue[node] local task = coroutine.running() table.insert(q, task) skynet.wait(task) skynet.wakeup(q.confirm) return q.sender end return s end cluster.get_sender = get_sender function cluster.call(node, address, ...) -- skynet.pack(...) will free by cluster.core.packrequest local s = sender[node] if not s then local task = skynet.packstring(address, ...) return skynet.call(get_sender(node), "lua", "req", repack(skynet.unpack(task))) end return skynet.call(s, "lua", "req", address, skynet.pack(...)) end function cluster.send(node, address, ...) -- push is the same with req, but no response local s = sender[node] if not s then table.insert(task_queue[node], skynet.packstring(address, ...)) else skynet.send(sender[node], "lua", "push", address, skynet.pack(...)) end end function cluster.open(port, maxclient) if type(port) == "string" then return skynet.call(clusterd, "lua", "listen", port, nil, maxclient) else return skynet.call(clusterd, "lua", "listen", "0.0.0.0", port, maxclient) end end function cluster.reload(config) skynet.call(clusterd, "lua", "reload", config) end function cluster.proxy(node, name) return skynet.call(clusterd, "lua", "proxy", node, name) end function cluster.snax(node, name, address) local snax = require "skynet.snax" if not address then address = cluster.call(node, ".service", "QUERY", "snaxd" , name) end local handle = skynet.call(clusterd, "lua", "proxy", node, address) return snax.bind(handle, name) end function cluster.register(name, addr) assert(type(name) == "string") assert(addr == nil or type(addr) == "number") return skynet.call(clusterd, "lua", "register", name, addr) end function cluster.unregister(name) assert(type(name) == "string") return skynet.call(clusterd, "lua", "unregister", name) end function cluster.query(node, name) return skynet.call(get_sender(node), "lua", "req", 0, skynet.pack(name)) end skynet.init(function() clusterd = skynet.uniqueservice("clusterd") end) return cluster ================================================ FILE: lualib/skynet/coroutine.lua ================================================ -- You should use this module (skynet.coroutine) instead of origin lua coroutine in skynet framework local coroutine = coroutine -- origin lua coroutine module local coroutine_resume = coroutine.resume local coroutine_yield = coroutine.yield local coroutine_status = coroutine.status local coroutine_running = coroutine.running local coroutine_close = coroutine.close local select = select local skynetco = {} skynetco.isyieldable = coroutine.isyieldable skynetco.running = coroutine.running skynetco.status = coroutine.status local skynet_coroutines = setmetatable({}, { __mode = "kv" }) -- true : skynet coroutine -- false : skynet suspend -- nil : exit function skynetco.create(f) local co = coroutine.create(f) -- mark co as a skynet coroutine skynet_coroutines[co] = true return co end do -- begin skynetco.resume local function unlock(co, ...) skynet_coroutines[co] = true return ... end local function skynet_yielding(co, ...) skynet_coroutines[co] = false return unlock(co, coroutine_resume(co, coroutine_yield(...))) end local function resume(co, ok, ...) if not ok then return ok, ... elseif coroutine_status(co) == "dead" then -- the main function exit skynet_coroutines[co] = nil return true, ... elseif (...) == "USER" then return true, select(2, ...) else -- blocked in skynet framework, so raise the yielding message return resume(co, skynet_yielding(co, ...)) end end -- record the root of coroutine caller (It should be a skynet thread) local coroutine_caller = setmetatable({} , { __mode = "kv" }) function skynetco.resume(co, ...) local co_status = skynet_coroutines[co] if not co_status then if co_status == false then -- is running return false, "cannot resume a skynet coroutine suspend by skynet framework" end if coroutine_status(co) == "dead" then -- always return false, "cannot resume dead coroutine" return coroutine_resume(co, ...) else return false, "cannot resume none skynet coroutine" end end local from = coroutine_running() local caller = coroutine_caller[from] or from coroutine_caller[co] = caller return resume(co, coroutine_resume(co, ...)) end function skynetco.thread(co) co = co or coroutine_running() if skynet_coroutines[co] ~= nil then return coroutine_caller[co] , false else return co, true end end end -- end of skynetco.resume function skynetco.status(co) local status = coroutine_status(co) if status == "suspended" then if skynet_coroutines[co] == false then return "blocked" else return "suspended" end else return status end end function skynetco.yield(...) return coroutine_yield("USER", ...) end do -- begin skynetco.wrap local function wrap_co(ok, ...) if ok then return ... else error(...) end end function skynetco.wrap(f) local co = skynetco.create(function(...) return f(...) end) return function(...) return wrap_co(skynetco.resume(co, ...)) end end end -- end of skynetco.wrap function skynetco.close(co) skynet_coroutines[co] = nil return coroutine_close(co) end return skynetco ================================================ FILE: lualib/skynet/datacenter.lua ================================================ local skynet = require "skynet" local datacenter = {} function datacenter.get(...) return skynet.call("DATACENTER", "lua", "QUERY", ...) end function datacenter.set(...) return skynet.call("DATACENTER", "lua", "UPDATE", ...) end function datacenter.wait(...) return skynet.call("DATACENTER", "lua", "WAIT", ...) end return datacenter ================================================ FILE: lualib/skynet/datasheet/builder.lua ================================================ local skynet = require "skynet" local dump = require "skynet.datasheet.dump" local core = require "skynet.datasheet.core" local service = require "skynet.service" local builder = {} local cache = {} local dataset = {} local address local unique_id = 0 local function unique_string(str) unique_id = unique_id + 1 return str .. tostring(unique_id) end local function monitor(pointer) skynet.fork(function() skynet.call(address, "lua", "collect", pointer) for k,v in pairs(cache) do if v == pointer then cache[k] = nil return end end end) end local function dumpsheet(v) if type(v) == "string" then return v else return dump.dump(v) end end function builder.new(name, v) assert(dataset[name] == nil) local datastring = unique_string(dumpsheet(v)) local pointer = core.stringpointer(datastring) skynet.call(address, "lua", "update", name, pointer) cache[datastring] = pointer dataset[name] = datastring monitor(pointer) end function builder.update(name, v) local lastversion = assert(dataset[name]) local newversion = dumpsheet(v) local diff = unique_string(dump.diff(lastversion, newversion)) local pointer = core.stringpointer(diff) skynet.call(address, "lua", "update", name, pointer) cache[diff] = pointer local lp = assert(cache[lastversion]) skynet.send(address, "lua", "release", lp) dataset[name] = diff monitor(pointer) end function builder.compile(v) return dump.dump(v) end local function datasheet_service() local skynet = require "skynet" local datasheet = {} local handles = {} -- handle:{ ref:count , name:name , collect:resp } local dataset = {} -- name:{ handle:handle, monitor:{monitors queue} } local customers = {} -- source: { handle:true } setmetatable(customers, { __index = function(c, source) local v = {} c[source] = v return v end } ) local function releasehandle(source, handle) local h = handles[handle] h.ref = h.ref - 1 if h.ref == 0 and h.collect then h.collect(true) h.collect = nil handles[handle] = nil end local t=dataset[h.name] t.monitor[source]=nil end -- from builder, create or update handle function datasheet.update(source, name, handle) local t = dataset[name] if not t then -- new datasheet t = { handle = handle, monitor = {} } dataset[name] = t handles[handle] = { ref = 1, name = name } else -- report update to customers handles[handle] = { ref = handles[t.handle].ref, name = name } t.handle = handle for k,v in pairs(t.monitor) do v(true, handle) t.monitor[k] = nil end end skynet.ret() end -- from customers function datasheet.query(source, name) local t = assert(dataset[name], "create data first") local handle = t.handle local h = handles[handle] h.ref = h.ref + 1 customers[source][handle] = true skynet.ret(skynet.pack(handle)) end -- from customers, monitor handle change function datasheet.monitor(source, handle) local h = assert(handles[handle], "Invalid data handle") local t = dataset[h.name] if t.handle ~= handle then -- already changes customers[source][t.handle] = true skynet.ret(skynet.pack(t.handle)) else assert(not t.monitor[source]) local resp = skynet.response() t.monitor[source]= function(ok, handle) if ok then customers[source][handle] = true end resp(ok, handle) end end end -- from customers, release handle , ref count - 1 function datasheet.release(source, handle) -- send message, don't ret customers[source][handle] = nil releasehandle(source, handle) end -- customer closed, clear all handles it queried function datasheet.close(source) for handle in pairs(customers[source]) do releasehandle(source, handle) end customers[source] = nil end -- from builder, monitor handle release function datasheet.collect(source, handle) local h = assert(handles[handle], "Invalid data handle") if h.ref == 0 then handles[handle] = nil skynet.ret() else assert(h.collect == nil, "Only one collect allows") h.collect = skynet.response() end end skynet.dispatch("lua", function(_,source,cmd,...) datasheet[cmd](source,...) end) skynet.info_func(function() local info = {} local tmp = {} for k,v in pairs(handles) do tmp[k] = v end for k,v in pairs(dataset) do local h = handles[v.handle] tmp[v.handle] = nil info[k] = { handle = v.handle, monitors = h.ref, } end for k,v in pairs(tmp) do info[k] = v.ref end return info end) end skynet.init(function() address=service.new("datasheet", datasheet_service) end) return builder ================================================ FILE: lualib/skynet/datasheet/dump.lua ================================================ --[[ file format document : int32 strtbloffset int32 n int32*n index table table*n strings table: int32 array int32 dict int8*(array+dict) type (align 4) value*array kvpair*dict kvpair: string k value v value: (union) int32 integer float real int32 boolean int32 table index int32 string offset type: (enum) 0 nil 1 integer 2 real 3 boolean 4 table 5 string ]] local ctd = {} local math = math local table = table local string = string function ctd.dump(root) local doc = { table_n = 0, table = {}, strings = {}, offset = 0, } local function dump_table(t) local index = doc.table_n + 1 doc.table_n = index doc.table[index] = false -- place holder local array_n = 0 local array = {} local kvs = {} local types = {} local function encode(v) local t = type(v) if t == "table" then local index = dump_table(v) return '\4', string.pack("= -(0x7FFFFFFF+1) then return '\1', string.pack(" 0 and ik <= array_n) end end -- encode table local typeset = table.concat(types) local align = string.rep("\0", (4 - #typeset & 3) & 3) local tmp = { string.pack(" 9999999 then transaction_id = 1 end return transaction_id end ---@param session_id table ---@param transaction_id integer local function genTransactionParams(session_id, transaction_id, startTransaction) return "lsid", bson_encode(session_id), "txnNumber", bson_int64(transaction_id) -- 这是此会话中的第一个事务,故从1开始 -- BSON field 'OperationSessionInfo.txnNumber' is the wrong type 'int', expected type 'long' ,"autocommit", false , startTransaction and "startTransaction" or nil, startTransaction and startTransaction or nil end -- 原始裸接口备用,考虑是否需要开放支持跨协程事务,但每个更新操作需手动额外调用runCommand来辅助实现,而不能调用原有相关接口(如safe_insert) -- ---@param session_id table -- ---@param transaction_id integer -- function mongo_client:commitTransaction_(session_id, transaction_id) -- commitTransaction may only be run against the admin database. -- return self:runCommand("commitTransaction", 1, "lsid", bson_encode(session_id), "txnNumber", bson_int64(transaction_id), "autocommit", false) -- end -- ---@param session_id table -- ---@param transaction_id integer -- function mongo_client:abortTransaction_(session_id, transaction_id) -- return self:runCommand("abortTransaction", 1, "lsid", bson_encode(session_id), "txnNumber", bson_int64(transaction_id), "autocommit", false) -- 如果不设置autocommit则报错:txnNumber may only be provided for multi-document transactions and retryable write commands. autocommit:false was not provided, and abortTransaction is not a retryable write command. -- end local cur_coroutine = coroutine.running local transaction = {} local function is_in_transaction() return transaction[cur_coroutine()] end local function get_cur_transaction_params() local TransactionParams = transaction[cur_coroutine()] assert(TransactionParams) return table.unpack(TransactionParams) end local function set_transaction_params(params) local TransactionParams = transaction[cur_coroutine()] assert(TransactionParams) table.move(TransactionParams, 1, #TransactionParams, #params + 1, params) -- startTransaction只设置一次为true while #TransactionParams > 6 do table.remove(TransactionParams) end -- table.insert(params, "writeConcern") -- writeConcern is not allowed within a multi-statement transaction -- table.insert(params, bson_encode({w=0})) return params end local debug_traceback = debug.traceback -- 只支持同协程事务接口,兼容老接口 ---@param fun_transaction fun() 包含多个更新语句,这些语句要么全部成功,要么全部取消,目前同时支持run和send机制,但send接口会自动去掉writeConcern选项 ---@return any failed error info | nil ok function mongo_db:run_transaction(fun_transaction) local ret = self:startSession_() transaction[cur_coroutine()] = {genTransactionParams(ret.id, startTransaction_(), true)} local ok, err = xpcall(fun_transaction, debug_traceback) local r if ok then r = self:commit_transaction_() -- 不能报异常,否则后续transaction无法清理 else r = self:abort_transaction_() -- 不能报异常,否则后续transaction无法清理 -- error(err, 0) end transaction[cur_coroutine()] = nil -- 这个位置很关键,必须在commit_transaction和abort_transaction之后,因为其仍依赖本数据 self:endSessions_(ret.id) -- 这个不是本事务的关键,所以不处理其返回结果 if not err then local _ _, err = werror(r) end return err -- 如果成功err应该为nil end function mongo_db:commit_transaction_() -- local TransactionParams = transaction[cur_coroutine()] -- assert(TransactionParams) -- transaction[cur_coroutine()] = nil -- return self.connection:runCommand("commitTransaction", 1, table.unpack(TransactionParams)) local ok, r = pcall(function() return self.connection:runCommand("commitTransaction") end) -- transaction[cur_coroutine()] = nil if not ok then r = { errmsg = r } -- 模拟mongo风格错误 end return r end function mongo_db:abort_transaction_() -- local TransactionParams = transaction[cur_coroutine()] -- assert(TransactionParams) -- transaction[cur_coroutine()] = nil -- return self.connection:runCommand("abortTransaction", 1, table.unpack(TransactionParams)) local ok, r = pcall(function() return self.connection:runCommand("abortTransaction") end) -- transaction[cur_coroutine()] = nil if not ok then r = { errmsg = r } -- 模拟mongo风格错误 end return r end local old_runCommand = mongo_db.runCommand local old_send_command = mongo_db.send_command function mongo_db:runCommand(cmd,cmd_v,...) if is_in_transaction() then cmd_v = cmd_v or 1 local params = {cmd,cmd_v,...} return old_runCommand(self, table.unpack(set_transaction_params(params))) end return old_runCommand(self, cmd,cmd_v,...) end function mongo_db:send_command(cmd,cmd_v,...) if is_in_transaction() then cmd_v = cmd_v or 1 local params = {...} -- {cmd,cmd_v,...} 这里要去掉下面重复的cmd和cmd_v -- return old_send_command(self, table.unpack(set_transaction_params(params))) -- 这里我要需要将writeConcern选项去掉,因为MongoDB 不允许在事务中使用 writeConcern local conn = self.connection local request_id = conn:genId() local sock = conn.__sock local bson_cmd if not cmd_v then -- ensure cmd remains in first place bson_cmd = bson_encode_order(cmd, 1, "$db", self.name, table.unpack(set_transaction_params(params))) else bson_cmd = bson_encode_order(cmd, cmd_v, "$db", self.name, table.unpack(set_transaction_params(params))) end local pack = driver.op_msg(request_id, 2, bson_cmd) sock:request(pack) return {ok=1} -- fake successful response end return old_send_command(self, cmd,cmd_v,...) end -- 测试及使用示例 function mongo_db:test_transaction_() local tb_name = "tb_test" self[tb_name]:drop() -- Transaction numbers are only allowed on a replica set member or mongos local db = self[tb_name] print(self:run_transaction(function() print("-----------------------1") print(db:safe_insert({_id = bson.objectid(), key = 1, d = 1})) -- Only servers in a sharded cluster can start a new transaction at the active transaction number print("-----------------------2") print(db:safe_insert({_id = bson.objectid(), key = 2, d = 2})) print(db:insert({_id = bson.objectid(), key = 4, d = 4, by = "send"})) -- 测试send_command接口混用 -- error("err") -- 如果报错,上面的更新将自动回滚 print("-----------------------3") print(db:safe_insert({_id = bson.objectid(), key = 3, d = 3})) end)) end end return M ================================================ FILE: lualib/skynet/db/mongo.lua ================================================ local bson = require "bson" require "skynet.socket" local socketchannel = require "skynet.socketchannel" local skynet = require "skynet" local driver = require "skynet.mongo.driver" local md5 = require "md5" local crypt = require "skynet.crypt" local rawget = rawget local assert = assert local table = table local bson_encode = bson.encode local bson_encode_order = bson.encode_order local bson_decode = bson.decode local bson_int64 = bson.int64 local empty_bson = bson_encode {} local mongo = {} mongo.null = assert(bson.null) mongo.maxkey = assert(bson.maxkey) mongo.minkey = assert(bson.minkey) mongo.type = assert(bson.type) local mongo_cursor = {} local cursor_meta = { __index = mongo_cursor, } local aggregate_cursor = {} local aggregate_cursor_meta = { __index = aggregate_cursor, } local mongo_client = {} local client_meta = { __index = function(self, key) return rawget(mongo_client, key) or self:getDB(key) end, __tostring = function (self) local port_string if self.port then port_string = ":" .. tostring(self.port) else port_string = "" end return "[mongo client : " .. self.host .. port_string .."]" end, -- DO NOT need disconnect, because channel will shutdown during gc } local mongo_db = {} local db_meta = { __index = function (self, key) return rawget(mongo_db, key) or self:getCollection(key) end, __tostring = function (self) return "[mongo db : " .. self.name .. "]" end } local mongo_collection = {} local collection_meta = { __index = function(self, key) return rawget(mongo_collection, key) or self:getCollection(key) end , __tostring = function (self) return "[mongo collection : " .. self.full_name .. "]" end } local function dispatch_reply(so) local len_reply = so:read(4) local reply = so:read(driver.length(len_reply)) local result = {} local succ, reply_id, document = driver.reply(reply) result.document = document result.data = reply return reply_id, succ, result end local function __parse_addr(addr) local host, port = string.match(addr, "([^:]+):(.+)") return host, tonumber(port) end local function werror(r) local ok = (r.ok == 1 and not r.writeErrors and not r.writeConcernError and not r.errmsg) local err if not ok then if r.writeErrors then err = r.writeErrors[1].errmsg elseif r.writeConcernError then err = r.writeConcernError.errmsg else err = r.errmsg end end return ok, err, r end local function wbulkerror(r) local ok, err = werror(r) local cursor if (r.nErrors and r.nErrors ~= 0) and r.cursor then cursor = {} err = err and err .. ";" or "" for i, v in ipairs(r.cursor.firstBatch) do if v.ok ~= 1 then cursor[#cursor+1] = i err = err .. v.errmsg .. ";" end end end return ok, err, r, cursor end local auth_method = {} local function mongo_auth(mongoc) local user = rawget(mongoc, "username") local pass = rawget(mongoc, "password") local authmod = rawget(mongoc, "authmod") or "scram_sha1" authmod = "auth_" .. authmod local authdb = rawget(mongoc, "authdb") if authdb then authdb = mongo_client.getDB(mongoc, authdb) -- mongoc has not set metatable yet end return function() if user ~= nil and pass ~= nil then -- autmod can be "mongodb_cr" or "scram_sha1" local auth_func = auth_method[authmod] assert(auth_func , "Invalid authmod") assert(auth_func(authdb or mongoc, user, pass)) end local rs_data = mongoc:runCommand("ismaster") if rs_data.ok == 1 then if rs_data.hosts then local backup = {} for _, v in ipairs(rs_data.hosts) do local host, port = __parse_addr(v) table.insert(backup, {host = host, port = port}) end mongoc.__sock:changebackup(backup) end if rs_data.ismaster then return elseif rs_data.primary then local host, port = __parse_addr(rs_data.primary) mongoc.host = host mongoc.port = port mongoc.__sock:changehost(host, port) else -- socketchannel would try the next host in backup list error ("No primary return : " .. tostring(rs_data.me)) end end end end function mongo.client( conf ) local first = conf local backup = nil if conf.rs then first = conf.rs[1] backup = conf.rs end local obj = { host = first.host, port = first.port or 27017, username = first.username, password = first.password, authmod = first.authmod, authdb = first.authdb, } obj.__id = 0 obj.__sock = socketchannel.channel { host = obj.host, port = obj.port, response = dispatch_reply, auth = mongo_auth(obj), backup = backup, nodelay = true, overload = conf.overload, } setmetatable(obj, client_meta) obj.__sock:connect(true) -- try connect only once return obj end function mongo_client:getDB(dbname) local db = { connection = self, name = dbname, full_name = dbname, database = false, __cmd = dbname .. "." .. "$cmd", } db.database = db return setmetatable(db, db_meta) end function mongo_client:disconnect() if self.__sock then local so = self.__sock self.__sock = false so:close() end end function mongo_client:genId() local id = self.__id + 1 self.__id = id return id end function mongo_client:runCommand(...) if not self.admin then self.admin = self:getDB "admin" end return self.admin:runCommand(...) end local function filter(tbl, key, value) if value == nil then return end tbl[#tbl+1] = key tbl[#tbl+1] = type(value) == "table" and bson_encode(value) or value end local bulkWrite = {} bulkWrite.insert = function(nsindex, doc) return bson_encode_order("insert", nsindex, "document", bson_encode(doc.insert)) end bulkWrite.delete = function(nsindex, doc) local args = {"delete", nsindex, "filter", bson_encode(doc.filter), "multi", doc.multi or false} filter(args, "hit", doc.hit) filter(args, "collation", doc.collation) return bson_encode_order(table.unpack(args)) end --update仅支持原子操作更新/聚合管道更新 详情阅读官方文档 --arrayFilters 过滤方式需要遍历encode 暂时不支持 bulkWrite.update = function(nsindex, doc) local args = {"update", nsindex, "filter", bson_encode(doc.filter), "updateMods", bson_encode(doc.update), "multi", doc.multi or false, "upsert", doc.upsert or false} filter(args, "hit", doc.hit) filter(args, "constants", doc.constants) filter(args, "collation", doc.collation) return bson_encode_order(table.unpack(args)) end --bulkWrite 依赖 mongo8.0x --忽略let,cursor,writeConcern 参数 ---@param datas table 待写入数据 --- - op string 写入数据方式 参见:bulkWrite --- - collection string 数据写入目标, 用于构建nsInfo; 注意:采用 空间.集合方式 --- - ... 操作具体参数 insert采用:insert填充document, update采用 update 填充updateMods --- 示例:{op = "insert", collection = "log.online", insert = {count=0}} --- {op = "update", collection = "log.online", update = {["$set"] = {count = 2}}, upsert = true}} function mongo_client:bulkWrite(datas, comment, ordered, verify, errorsOnly) local ops, ns, map = {}, {}, {} for i, v in ipairs(datas) do if not map[v.collection] then map[v.collection] = #ns --ops 索引从0开始 ns[#ns+1] = bson_encode({ns = v.collection}) end local f = assert(bulkWrite[v.op], v.op) ops[i] = f(map[v.collection], v) end local args = {"bulkWrite", 1, "ops", ops, "nsInfo", ns} filter(args, "ordered", ordered) --是否有序执行 默认有序 filter(args, "bypassDocumentValidation", verify) --是否验证 默认验证 filter(args, "comment", comment) --日志跟踪注释 filter(args, "errorsOnly",errorsOnly) -- 仅返回错误信息 return wbulkerror(self:runCommand(table.unpack(args))) end function auth_method:auth_mongodb_cr(user,password) local password = md5.sumhexa(string.format("%s:mongo:%s",user,password)) local result= self:runCommand "getnonce" if result.ok ~=1 then return false end local key = md5.sumhexa(string.format("%s%s%s",result.nonce,user,password)) local result= self:runCommand ("authenticate",1,"user",user,"nonce",result.nonce,"key",key) return result.ok == 1 end local function salt_password(password, salt, iter) salt = salt .. "\0\0\0\1" local output = crypt.hmac_sha1(password, salt) local inter = output for i=2,iter do inter = crypt.hmac_sha1(password, inter) output = crypt.xor_str(output, inter) end return output end function auth_method:auth_scram_sha1(username,password) local user = string.gsub(string.gsub(username, '=', '=3D'), ',' , '=2C') local nonce = crypt.base64encode(crypt.randomkey()) local first_bare = "n=" .. user .. ",r=" .. nonce local sasl_start_payload = crypt.base64encode("n,," .. first_bare) local r r = self:runCommand("saslStart",1,"autoAuthorize",1,"mechanism","SCRAM-SHA-1","payload",sasl_start_payload) if r.ok ~= 1 then return false end local conversationId = r['conversationId'] local server_first = r['payload'] local parsed_s = crypt.base64decode(server_first) local parsed_t = {} for k, v in string.gmatch(parsed_s, "(%w+)=([^,]*)") do parsed_t[k] = v end local iterations = tonumber(parsed_t['i']) local salt = parsed_t['s'] local rnonce = parsed_t['r'] if string.sub(rnonce, 1, 12) ~= nonce then skynet.error("Server returned an invalid nonce.") return false end local without_proof = "c=biws,r=" .. rnonce local pbkdf2_key = md5.sumhexa(string.format("%s:mongo:%s",username,password)) local salted_pass = salt_password(pbkdf2_key, crypt.base64decode(salt), iterations) local client_key = crypt.hmac_sha1(salted_pass, "Client Key") local stored_key = crypt.sha1(client_key) local auth_msg = first_bare .. ',' .. parsed_s .. ',' .. without_proof local client_sig = crypt.hmac_sha1(stored_key, auth_msg) local client_key_xor_sig = crypt.xor_str(client_key, client_sig) local client_proof = "p=" .. crypt.base64encode(client_key_xor_sig) local client_final = crypt.base64encode(without_proof .. ',' .. client_proof) local server_key = crypt.hmac_sha1(salted_pass, "Server Key") local server_sig = crypt.base64encode(crypt.hmac_sha1(server_key, auth_msg)) r = self:runCommand("saslContinue",1,"conversationId",conversationId,"payload",client_final) if r.ok ~= 1 then return false end parsed_s = crypt.base64decode(r['payload']) parsed_t = {} for k, v in string.gmatch(parsed_s, "(%w+)=([^,]*)") do parsed_t[k] = v end if parsed_t['v'] ~= server_sig then skynet.error("Server returned an invalid signature.") return false end if not r.done then r = self:runCommand("saslContinue",1,"conversationId",conversationId,"payload","") if r.ok ~= 1 then return false end if not r.done then skynet.error("SASL conversation failed to complete.") return false end end return true end function mongo_client:logout() local result = self:runCommand "logout" return result.ok == 1 end function mongo_db:auth(user, pass) local authmod = rawget(self.connection, "authmod") or "scram_sha1" local auth_func = auth_method["auth_" .. authmod] assert(auth_func , "Invalid authmod") return auth_func(self, user, pass) end function mongo_db:runCommand(cmd,cmd_v,...) local conn = self.connection local request_id = conn:genId() local sock = conn.__sock local bson_cmd if not cmd_v then -- ensure cmd remains in first place bson_cmd = bson_encode_order(cmd,1, "$db", self.name) else bson_cmd = bson_encode_order(cmd,cmd_v, "$db", self.name, ...) end local pack = driver.op_msg(request_id, 0, bson_cmd) -- we must hold req (req.data), because req.document is a lightuserdata, it's a pointer to the string (req.data) local req = sock:request(pack, request_id) local doc = req.document return bson_decode(doc) end --- send command without response function mongo_db:send_command(cmd, cmd_v, ...) local conn = self.connection local request_id = conn:genId() local sock = conn.__sock local bson_cmd if not cmd_v then -- ensure cmd remains in first place bson_cmd = bson_encode_order(cmd, 1, "$db", self.name, "writeConcern", {w=0}) else bson_cmd = bson_encode_order(cmd, cmd_v, "$db", self.name, "writeConcern", {w=0}, ...) end local pack = driver.op_msg(request_id, 2, bson_cmd) sock:request(pack) return {ok=1} -- fake successful response end function mongo_db:getCollection(collection) local col = { connection = self.connection, name = collection, full_name = self.full_name .. "." .. collection, database = self.database, } self[collection] = setmetatable(col, collection_meta) return col end mongo_collection.getCollection = mongo_db.getCollection function mongo_collection:insert(doc) if doc._id == nil then doc._id = bson.objectid() end self.database:send_command("insert", self.name, "documents", {bson_encode(doc)}) end function mongo_collection:safe_insert(doc) local r = self.database:runCommand("insert", self.name, "documents", {bson_encode(doc)}) return werror(r) end function mongo_collection:raw_safe_insert(doc) local r = self.database:runCommand("insert", self.name, "documents", {doc}) return werror(r) end function mongo_collection:batch_insert(docs) for i=1,#docs do if docs[i]._id == nil then docs[i]._id = bson.objectid() end docs[i] = bson_encode(docs[i]) end self.database:send_command("insert", self.name, "documents", docs) end mongo_collection.insert_many = mongo_collection.batch_insert function mongo_collection:safe_batch_insert(docs) for i = 1, #docs do if docs[i]._id == nil then docs[i]._id = bson.objectid() end docs[i] = bson_encode(docs[i]) end local r = self.database:runCommand("insert", self.name, "documents", docs) return werror(r) end mongo_collection.safe_insert_many = mongo_collection.safe_batch_insert function mongo_collection:update(query,update,upsert,multi) self.database:send_command("update", self.name, "updates", {bson_encode({ q = query, u = update, upsert = upsert, multi = multi })}) end function mongo_collection:safe_update(query, update, upsert, multi) local r = self.database:runCommand("update", self.name, "updates", {bson_encode({ q = query, u = update, upsert = upsert, multi = multi, })}) return werror(r) end function mongo_collection:batch_update(updates) local updates_tb = {} for i = 1, #updates do updates_tb[i] = bson_encode({ q = updates[i].query, u = updates[i].update, upsert = updates[i].upsert, multi = updates[i].multi, }) end self.database:send_command("update", self.name, "updates", updates_tb) end function mongo_collection:safe_batch_update(updates) local updates_tb = {} for i = 1, #updates do updates_tb[i] = bson_encode({ q = updates[i].query, u = updates[i].update, upsert = updates[i].upsert, multi = updates[i].multi, }) end local r = self.database:runCommand("update", self.name, "updates", updates_tb) return werror(r) end function mongo_collection:raw_safe_update(update) local r = self.database:runCommand("update", self.name, "updates", {update}) return werror(r) end function mongo_collection:delete(query, single) self.database:send_command("delete", self.name, "deletes", {bson_encode({ q = query, limit = single and 1 or 0, })}) end function mongo_collection:safe_delete(query, single) local r = self.database:runCommand("delete", self.name, "deletes", {bson_encode({ q = query, limit = single and 1 or 0, })}) return werror(r) end function mongo_collection:safe_batch_delete(deletes, single) local delete_tb = {} for i = 1, #deletes do delete_tb[i] = bson_encode({ q = deletes[i], limit = single and 1 or 0, }) end local r = self.database:runCommand("delete", self.name, "deletes", delete_tb) return werror(r) end function mongo_collection:raw_safe_delete(delete) local r = self.database:runCommand("delete", self.name, "deletes", {delete}) return werror(r) end function mongo_collection:findOne(query, projection) local r = self.database:runCommand("find", self.name, "filter", query and bson_encode(query) or empty_bson, "limit", 1, "projection", projection and bson_encode(projection) or empty_bson) if r.ok ~= 1 then error(r.errmsg or "Reply from mongod error") end return r.cursor.firstBatch[1] end function mongo_collection:find(query, projection) return setmetatable( { __collection = self, __query = query and bson_encode(query) or empty_bson, __projection = projection and bson_encode(projection) or empty_bson, __ptr = nil, __data = nil, __cursor = nil, __document = {}, __sort = empty_bson } , cursor_meta) end local function unfold(list, key, ...) if key == nil then return list end local next_func, t = pairs(key) local k, v = next_func(t) -- The first key pair table.insert(list, k) table.insert(list, v) return unfold(list, ...) end -- cursor:sort { key = 1 } or cursor:sort( {key1 = 1}, {key2 = -1}) function mongo_cursor:sort(key, key_v, ...) if key_v then local key_list = unfold({}, key, key_v , ...) key = bson_encode_order(table.unpack(key_list)) end self.__sort = key return self end function mongo_cursor:skip(amount) self.__skip = amount return self end function mongo_cursor:limit(amount) self.__limit = amount return self end function mongo_cursor:hint(indexName) self.__hint = indexName return self end function mongo_cursor:maxTimeMS(ms) self.__maxTimeMS = ms return self end local opt_func = {} local function opt_define(name) local key = "__" .. name opt_func[name] = function (self, ...) local v = self[key] if v ~= nil then return name, v, ... else return ... end end end opt_define "skip" opt_define "limit" opt_define "hint" opt_define "maxTimeMS" local function add_opt(self, opt, ...) if opt == nil then return end return opt_func[opt](self, add_opt(self, ...)) end function mongo_cursor:count(with_limit_and_skip) local ret if with_limit_and_skip then ret = self.__collection.database:runCommand('count', self.__collection.name, 'query', self.__query, add_opt(self, "skip", "limit", "hint", "maxTimeMS")) else ret = self.__collection.database:runCommand('count', self.__collection.name, 'query', self.__query, add_opt(self, "hint", "maxTimeMS")) end assert(ret.ok == 1, ret.errmsg) return ret.n end -- For compatibility. -- collection:createIndex({username = 1}, {unique = true}) local function createIndex_onekey(self, key, option) local doc = {} for k,v in pairs(option) do doc[k] = v end local k,v = next(key) -- support only one key assert(next(key,k) == nil, "Use new api for multi-keys") doc.name = doc.name or (k .. "_" .. v) doc.key = key return self.database:runCommand("createIndexes", self.name, "indexes", {doc}) end local function IndexModel(option) local doc = {} for k,v in pairs(option) do if type(k) == "string" then doc[k] = v end end local keys = {} local name for _, kv in ipairs(option) do local k,v if type(kv) == "string" then k = kv v = 1 else k,v = next(kv) end table.insert(keys, k) table.insert(keys, v) name = (name == nil) and k or (name .. "_" .. k) name = name .. "_" .. v end assert(name, "Need keys") doc.name = doc.name or name doc.key = bson_encode_order(table.unpack(keys)) return doc end -- collection:createIndex { { key1 = 1}, { key2 = 1 }, unique = true } -- or collection:createIndex { "key1", "key2", unique = true } -- or collection:createIndex( { key1 = 1} , { unique = true } ) -- For compatibility function mongo_collection:createIndex(arg1 , arg2) if arg2 then return createIndex_onekey(self, arg1, arg2) else return self.database:runCommand("createIndexes", self.name, "indexes", { IndexModel(arg1) }) end end function mongo_collection:createIndexes(...) local idx = { ... } for k,v in ipairs(idx) do idx[k] = IndexModel(v) end return self.database:runCommand("createIndexes", self.name, "indexes", idx) end mongo_collection.ensureIndex = mongo_collection.createIndex function mongo_collection:drop() return self.database:runCommand("drop", self.name) end -- collection:dropIndex("age_1") -- collection:dropIndex("*") function mongo_collection:dropIndex(indexName) return self.database:runCommand("dropIndexes", self.name, "index", indexName) end -- collection:findAndModify({query = {name = "userid"}, update = {["$inc"] = {nextid = 1}}, }) -- keys, value type -- query, table -- sort, table -- remove, bool -- update, table -- new, bool -- fields, bool -- upsert, boolean function mongo_collection:findAndModify(doc) assert(doc.query) assert(doc.update or doc.remove) local cmd = {"findAndModify", self.name}; for k, v in pairs(doc) do table.insert(cmd, k) table.insert(cmd, v) end return self.database:runCommand(table.unpack(cmd)) end -- https://docs.mongodb.com/manual/reference/command/aggregate/ -- collection:aggregate({ { ["$project"] = {tags = 1} } }, {cursor={}}) -- @param pipeline: array -- @param options: map -- @return function mongo_collection:aggregate(pipeline, options) assert(pipeline) local options_cmd if options then options_cmd = {} for k, v in pairs(options) do table.insert(options_cmd, k) table.insert(options_cmd, v) end end local len = #pipeline return setmetatable( { __collection = self, __pipeline = table.move(pipeline, 1, len, 1, {}), __pipeline_len = len, __options = options_cmd, __ptr = nil, __data = nil, __cursor = nil, __document = {}, __skip = 0, __limit = 0, } , aggregate_cursor_meta) end function mongo_cursor:hasNext() if self.__ptr == nil then if self.__document == nil then return false end local response local database = self.__collection.database if self.__data == nil then local name = self.__collection.name response = database:runCommand("find", name, "filter", self.__query, "sort", self.__sort, "projection", self.__projection, add_opt(self, "skip", "limit", "hint", "maxTimeMS")) else if self.__cursor and self.__cursor > 0 then local name = self.__collection.name response = database:runCommand("getMore", bson_int64(self.__cursor), "collection", name) else -- no more self.__document = nil self.__data = nil return false end end if response.ok ~= 1 then self.__document = nil self.__data = nil self.__cursor = nil error(response["errmsg"] or "Reply from mongod error") end local cursor = response.cursor self.__document = cursor.firstBatch or cursor.nextBatch self.__data = response self.__ptr = 1 self.__cursor = cursor.id local limit = self.__limit if limit and limit > 0 and cursor.id > 0 then limit = limit - #self.__document if limit <= 0 then -- reach limit self:close() end self.__limit = limit end if cursor.id == 0 and #self.__document == 0 then -- nomore return false end return true end return true end function mongo_cursor:next() if self.__ptr == nil then error "Call hasNext first" end local r = self.__document[self.__ptr] self.__ptr = self.__ptr + 1 if self.__ptr > #self.__document then self.__ptr = nil end return r end function mongo_cursor:close() if self.__cursor and self.__cursor > 0 then local coll = self.__collection coll.database:send_command("killCursors", coll.name, "cursors", {bson_int64(self.__cursor)}) self.__cursor = nil end end local sort_stage = { ["$sort"] = true } local skip_stage = { ["$skip"] = 0 } local limit_stage = { ["$limit"] = 0 } local count_stage = { ["$count"] = "__count" } local function format_pipeline(self, with_limit_and_skip, is_count) local len = self.__pipeline_len if self.__sort and not is_count then len = len + 1 sort_stage["$sort"] = self.__sort self.__pipeline[len] = sort_stage end if with_limit_and_skip then if self.__skip > 0 then len = len + 1 skip_stage["$skip"] = self.__skip self.__pipeline[len] = skip_stage end if self.__limit > 0 then len = len + 1 limit_stage["$limit"] = self.__limit self.__pipeline[len] = limit_stage end end if is_count then len = len + 1 self.__pipeline[len] = count_stage end for i = 1, 2 do self.__pipeline[len + i] = nil end return self.__pipeline end function aggregate_cursor:count(with_limit_and_skip) local ret local name = self.__collection.name local database = self.__collection.database if self.__options then ret = database:runCommand("aggregate", name, "pipeline", format_pipeline(self, with_limit_and_skip, true), table.unpack(self.__options)) else ret = database:runCommand("aggregate", name, "pipeline", format_pipeline(self, with_limit_and_skip, true), "cursor", empty_bson) end if ret.ok ~= 1 then error(ret["errmsg"] or "Reply from mongod error") end return ret.cursor.firstBatch[1].__count end function aggregate_cursor:hasNext() if self.__ptr == nil then if self.__document == nil then return false end local ret local name = self.__collection.name local database = self.__collection.database if self.__data == nil then if self.__options then ret = database:runCommand("aggregate", name, "pipeline", format_pipeline(self, true), table.unpack(self.__options)) else ret = database:runCommand("aggregate", name, "pipeline", format_pipeline(self, true), "cursor", empty_bson) end else if self.__cursor and self.__cursor > 0 then ret = database:runCommand("getMore", bson_int64(self.__cursor), "collection", name) else -- no more self.__document = nil self.__data = nil return false end end if ret.ok ~= 1 then self.__document = nil self.__data = nil self.__cursor = nil error(ret["errmsg"] or "Reply from mongod error") end local cursor = ret.cursor self.__document = cursor.firstBatch or cursor.nextBatch self.__data = ret self.__ptr = 1 self.__cursor = cursor.id local limit = self.__limit if cursor.id > 0 and limit > 0 then limit = limit - #self.__document if limit <= 0 then -- reach limit self:close() end self.__limit = limit end if cursor.id == 0 and #self.__document == 0 then -- nomore return false end return true end return true end aggregate_cursor.sort = mongo_cursor.sort aggregate_cursor.skip = mongo_cursor.skip aggregate_cursor.limit = mongo_cursor.limit aggregate_cursor.next = mongo_cursor.next aggregate_cursor.close = mongo_cursor.close --[[ 支持插件式扩展模式 比如要添加mongo会话事务接口支持功能可在事务接口使用前执行以下代码: local mongo = require "mongo" mongo.enable("transaction") ]] local inited_modules = {} -- 确保每个子模块仅能初始化一次 function mongo.enable(name) if inited_modules[name] then return end inited_modules[name] = true local m = require("skynet.db.mongo." .. name) m.init({ mongo_db = mongo_db }) -- 后续可增加更多的扩展模块支持 end return mongo ================================================ FILE: lualib/skynet/db/mysql.lua ================================================ -- Copyright (C) 2012 Yichun Zhang (agentzh) -- Copyright (C) 2014 Chang Feng -- This file is modified version from https://github.com/openresty/lua-resty-mysql -- The license is under the BSD license. -- Modified by Cloud Wu (remove bit32 for lua 5.3) -- protocol detail: https://mariadb.com/kb/en/clientserver-protocol/ local socketchannel = require "skynet.socketchannel" local crypt = require "skynet.crypt" local sub = string.sub local strgsub = string.gsub local strformat = string.format local strbyte = string.byte local strchar = string.char local strrep = string.rep local strunpack = string.unpack local strpack = string.pack local sha1 = crypt.sha1 local setmetatable = setmetatable local error = error local tonumber = tonumber local tointeger = math.tointeger local _M = {_VERSION = "0.14"} -- the following charset map is generated from the following mysql query: -- SELECT CHARACTER_SET_NAME, ID -- FROM information_schema.collations -- WHERE IS_DEFAULT = 'Yes' ORDER BY id; local CHARSET_MAP = { _default = 0, big5 = 1, dec8 = 3, cp850 = 4, hp8 = 6, koi8r = 7, latin1 = 8, latin2 = 9, swe7 = 10, ascii = 11, ujis = 12, sjis = 13, hebrew = 16, tis620 = 18, euckr = 19, koi8u = 22, gb2312 = 24, greek = 25, cp1250 = 26, gbk = 28, latin5 = 30, armscii8 = 32, utf8 = 33, ucs2 = 35, cp866 = 36, keybcs2 = 37, macce = 38, macroman = 39, cp852 = 40, latin7 = 41, utf8mb4 = 45, cp1251 = 51, utf16 = 54, utf16le = 56, cp1256 = 57, cp1257 = 59, utf32 = 60, binary = 63, geostd8 = 92, cp932 = 95, eucjpms = 97, gb18030 = 248 } -- constants local COM_QUERY = "\x03" local COM_PING = "\x0e" local COM_STMT_PREPARE = "\x16" local COM_STMT_EXECUTE = "\x17" local COM_STMT_CLOSE = "\x19" local COM_STMT_RESET = "\x1a" local CURSOR_TYPE_NO_CURSOR = 0x00 local SERVER_MORE_RESULTS_EXISTS = 8 local mt = {__index = _M} -- mysql field value type converters local converters = {} for i = 0x01, 0x05 do -- tiny, short, long, float, double converters[i] = tonumber end converters[0x08] = tonumber -- long long converters[0x09] = tonumber -- int24 converters[0x0d] = tonumber -- year converters[0xf6] = tonumber -- newdecimal local function _get_byte1(data, i) return strbyte(data, i), i + 1 end local function _get_int1(data, i, is_signed) if not is_signed then return strunpack("= 0 and first <= 250 then return first, pos + 1 end if first == 251 then return nil, pos + 1 end if first == 252 then pos = pos + 1 return _get_byte2(data, pos) end if first == 253 then pos = pos + 1 return _get_byte3(data, pos) end if first == 254 then pos = pos + 1 return _get_byte8(data, pos) end return false, pos + 1 end local function _set_length_coded_bin(n) if n < 251 then return strchar(n) end if n < (1 << 16) then return strpack(" 0 then local f, ts, vs local types_buf = "" local values_buf = "" --生成NULL位图 local null_count = (arg_num + 7) // 8 local null_map = "" local field_index = 1 for i = 1, null_count do local byte = 0 for j = 0, 7 do if field_index <= arg_num then if args[field_index] == nil then byte = byte | (1 << j) else byte = byte | (0 << j) end end field_index = field_index + 1 end null_map = null_map .. strchar(byte) end for i = 1, arg_num do local v = args[i] f = store_types[type(v)] if not f then error("invalid parameter type " .. type(v)) end ts, vs = f(v) types_buf = types_buf .. ts values_buf = values_buf .. vs end cmd_packet = cmd_packet .. null_map .. strchar(0x01) .. types_buf .. values_buf end return _compose_packet(self, cmd_packet) end local function read_result(self, sock) local packet, typ, err = _recv_packet(self, sock) if not packet then return nil, err --error( err ) end if typ == "ERR" then local errno, msg, sqlstate = _parse_err_packet(packet) return nil, msg, errno, sqlstate --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) end if typ == "OK" then local res = _parse_ok_packet(packet) if res and res.server_status & SERVER_MORE_RESULTS_EXISTS ~= 0 then return res, "again" end return res end if typ ~= "DATA" then return nil, "packet type " .. typ .. " not supported" --error( "packet type " .. typ .. " not supported" ) end -- typ == 'DATA' local field_count, extra = _parse_result_set_header_packet(packet) local cols = {} for i = 1, field_count do local col, err, errno, sqlstate = _recv_field_packet(self, sock) if not col then return nil, err, errno, sqlstate --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) end cols[i] = col end local packet, typ, err = _recv_packet(self, sock) if not packet then --error( err) return nil, err end if typ ~= "EOF" then --error ( "unexpected packet type " .. typ .. " while eof packet is ".. "expected" ) return nil, "unexpected packet type " .. typ .. " while eof packet is " .. "expected" end -- typ == 'EOF' local compact = self.compact local rows = {} local i = 0 while true do packet, typ, err = _recv_packet(self, sock) if not packet then --error (err) return nil, err end if typ == "EOF" then local warning_count, status_flags = _parse_eof_packet(packet) if status_flags & SERVER_MORE_RESULTS_EXISTS ~= 0 then return rows, "again" end break end -- if typ ~= 'DATA' then -- return nil, 'bad row packet type: ' .. typ -- end -- typ == 'DATA' i = i + 1 rows[i] = _parse_row_data_packet(packet, cols, compact) end return rows end local function _query_resp(self) return function(sock) local res, err, errno, sqlstate = read_result(self, sock) if not res then local badresult = {} badresult.badresult = true badresult.err = err badresult.errno = errno badresult.sqlstate = sqlstate return true, badresult end if err ~= "again" then return true, res end local multiresultset = {res} multiresultset.multiresultset = true local i = 2 while err == "again" do res, err, errno, sqlstate = read_result(self, sock) if not res then multiresultset.badresult = true multiresultset.err = err multiresultset.errno = errno multiresultset.sqlstate = sqlstate return true, multiresultset end multiresultset[i] = res i = i + 1 end return true, multiresultset end end function _M.connect(opts) local self = setmetatable({}, mt) local max_packet_size = opts.max_packet_size if not max_packet_size then max_packet_size = 1024 * 1024 -- default 1 MB end self._max_packet_size = max_packet_size self.compact = opts.compact_arrays local database = opts.database or "" local user = opts.user or "" local password = opts.password or "" local charset = CHARSET_MAP[opts.charset or "_default"] local channel = socketchannel.channel { host = opts.host, port = opts.port or 3306, auth = _mysql_login(self, user, password, charset, database, opts.on_connect), overload = opts.overload } self.sockchannel = channel -- try connect first only once channel:connect(true) return self end function _M.disconnect(self) self.sockchannel:close() setmetatable(self, nil) end function _M.query(self, query) local querypacket = _compose_query(self, query) local sockchannel = self.sockchannel if not self.query_resp then self.query_resp = _query_resp(self) end return sockchannel:request(querypacket, self.query_resp) end local function read_prepare_result(self, sock) local resp = {} local packet, typ, err = _recv_packet(self, sock) if not packet then resp.badresult = true resp.errno = 300101 resp.err = err return false, resp end if typ == "ERR" then local errno, msg, sqlstate = _parse_err_packet(packet) resp.badresult = true resp.errno = errno resp.err = msg resp.sqlstate = sqlstate return true, resp end --第一节只能是OK if typ ~= "OK" then resp.badresult = true resp.errno = 300201 resp.err = "first typ must be OK,now" .. typ return false, resp end resp.prepare_id, resp.field_count, resp.param_count, resp.warning_count = strunpack(" 0 then local param = _recv_field_packet(self, sock) while param do table.insert(resp.params, param) param = _recv_field_packet(self, sock) end end if resp.field_count > 0 then local field = _recv_field_packet(self, sock) while field do table.insert(resp.fields, field) field = _recv_field_packet(self, sock) end end return true, resp end local function _prepare_resp(self, sql) return function(sock) return read_prepare_result(self, sock) end end -- 注册预处理语句 function _M.prepare(self, sql) local querypacket = _compose_stmt_prepare(self, sql) local sockchannel = self.sockchannel if not self.prepare_resp then self.prepare_resp = _prepare_resp(self) end return sockchannel:request(querypacket, self.prepare_resp) end local function _get_datetime(data, pos) local len, year, month, day, hour, minute, second local value len, pos = _from_length_coded_bin(data, pos) if len == 7 then year, month, day, hour, minute, second, pos = string.unpack(" 2 then if byte & (1 << j) == 0 then null_fields[field_index - 2] = false else null_fields[field_index - 2] = true end end field_index = field_index + 1 end end local row = {} local parser for i = 1, ncols do local col = cols[i] local typ = col.type local name = col.name if not null_fields[i] then parser = _binary_parser[typ] if not parser then error("_parse_row_data_binary()error,unsupported field type " .. typ) end value, pos = parser(data, pos, col.is_signed) if compact then row[i] = value else row[name] = value end end end return row end local function read_execute_result(self, sock) local packet, typ, err = _recv_packet(self, sock) if not packet then return nil, err --error( err ) end if typ == "ERR" then local errno, msg, sqlstate = _parse_err_packet(packet) return nil, msg, errno, sqlstate --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) end if typ == "OK" then local res = _parse_ok_packet(packet) if res and res.server_status & SERVER_MORE_RESULTS_EXISTS ~= 0 then return res, "again" end return res end if typ ~= "DATA" then return nil, "packet type " .. typ .. " not supported" --error( "packet type " .. typ .. " not supported" ) end -- typ == 'DATA' -- local field_count, extra = _parse_result_set_header_packet(packet) local cols = {} local col while true do packet, typ, err = _recv_packet(self, sock) if typ == "EOF" then local warning_count, status_flags = _parse_eof_packet(packet) break end col = _parse_field_packet(packet) if not col then break --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) end table.insert(cols, col) end --没有记录集返回 if #cols < 1 then return {} end local compact = self.compact local rows = {} local row while true do packet, typ, err = _recv_packet(self, sock) if typ == "EOF" then local warning_count, status_flags = _parse_eof_packet(packet) if status_flags & SERVER_MORE_RESULTS_EXISTS ~= 0 then return rows, "again" end break end row = _parse_row_data_binary(packet, cols, compact) if not col then break end table.insert(rows, row) end return rows end local function _execute_resp(self) return function(sock) local res, err, errno, sqlstate = read_execute_result(self, sock) if not res then local badresult = {} badresult.badresult = true badresult.err = err badresult.errno = errno badresult.sqlstate = sqlstate return true, badresult end if err ~= "again" then return true, res end local mulitresultset = {res} mulitresultset.mulitresultset = true local i = 2 while err == "again" do res, err, errno, sqlstate = read_execute_result(self, sock) if not res then mulitresultset.badresult = true mulitresultset.err = err mulitresultset.errno = errno mulitresultset.sqlstate = sqlstate return true, mulitresultset end mulitresultset[i] = res i = i + 1 end return true, mulitresultset end end --[[ 执行预处理语句 失败返回字段 errno badresult sqlstate err ]] function _M.execute(self, stmt, ...) local querypacket, er = _compose_stmt_execute(self, stmt, CURSOR_TYPE_NO_CURSOR, table.pack(...)) if not querypacket then return { badresult = true, errno = 30902, err = er } end local sockchannel = self.sockchannel if not self.execute_resp then self.execute_resp = _execute_resp(self) end return sockchannel:request(querypacket, self.execute_resp) end local function _compose_stmt_reset(self, stmt) self.packet_no = -1 local cmd_packet = strpack("c1 instances -- map in order to initialize the @slots hash. function rediscluster:initialize_slots_cache() self.slots = {} self.nodes = {} for _,startup_node in ipairs(self.startup_nodes) do local ok = pcall(function () local conn = self:get_connection(startup_node) local list = conn:cluster("slots") for _,result in ipairs(list) do local ip,port = table.unpack(result[3]) assert(ip) port = assert(tonumber(port)) local master_node = { host = ip, port = port, slaves = {}, } self:set_node_name(master_node) for i=4,#result do local ip,port = table.unpack(result[i]) assert(ip) port = assert(tonumber(port)) local slave_node = { host = ip, port = port, } self:set_node_name(slave_node) table.insert(master_node.slaves,slave_node) end table.insert(self.nodes,master_node) for slot=tonumber(result[1]),tonumber(result[2]) do self.slots[slot] = master_node end end self.refresh_table_asap = false end) -- Exit the loop as long as the first node replies if ok then break end end end -- Flush the cache, mostly useful for debugging when we want to force -- redirection. function rediscluster:flush_slots_cache() self.slots = {} end -- Return the hash slot from the key. function rediscluster:keyslot(key) -- Only hash what is inside {...} if there is such a pattern in the key. -- Note that the specification requires the content that is between -- the first { and the first } after the first {. If we found {} without -- nothing in the middle, the whole key is hashed as usually. local startpos = string.find(key,"{",1,true) if startpos then local endpos = string.find(key,"}",startpos+1,true) if endpos and endpos ~= startpos + 1 then key = string.sub(key,startpos+1,endpos-1) end end return crc16(key) % RedisClusterHashSlots end -- Return the first key in the command arguments. -- -- Currently we just return argv[1], that is, the first argument -- after the command name. -- -- This is indeed the key for most commands, and when it is not true -- the cluster redirection will point us to the right node anyway. -- -- For commands we want to explicitly bad as they don't make sense -- in the context of cluster, nil is returned. function rediscluster:get_key_from_command(cmd, argv) local key = argv[2] -- argv[1] is cmd if cmd == "info" or cmd == "multi" or cmd == "exec" or cmd == "slaveof" or cmd == "config" or cmd == "shutdown" then return nil end if cmd == "eval" or cmd == "evalsha" then -- eval script numkeys key [key...] arg [arg...] local numkeys = argv[3] local firstkey_slot = nil for i=4,4+numkeys-1 do local slot = self:keyslot(argv[i]) if not firstkey_slot then firstkey_slot = slot elseif firstkey_slot ~= slot then error(string.format("%s - all keys must map to the same key slot",cmd)) end end -- numkeys <=0 will return nil return argv[4] end -- Unknown commands, and all the commands having the key -- as first argument are handled here: -- set, get, ... return key end -- If the current number of connections is already the maximum number -- allowed, close a random connection. This should be called every time -- we cache a new connection in the @connections hash. function rediscluster:close_existing_connection() local length = 0 for name,conn in pairs(self.connections) do length = length + 1 end if length >= self.max_connections then pcall(function () local name,conn = next(self.connections) self.connections[name] = nil conn:disconnect() end) end end function rediscluster:close_all_connection() local connections = self.connections self.connections = {} for name,conn in pairs(connections) do pcall(conn.disconnect,conn) end end function rediscluster:get_connection(node) if not node.name then node.name = nodename(node) end local name = node.name local ok,conn = sync.once.Do(name,function () local conn = self.connections[name] if not conn then self:close_existing_connection() conn = self:get_redis_link(node) self.connections[name] = conn end return conn end) assert(ok,conn) return conn end -- Return a link to a random node, or raise an error if no node can be -- contacted. This function is only called when we can't reach the node -- associated with a given hash slot, or when we don't know the right -- mapping. -- The function will try to get a successful reply to the PING command, -- otherwise the next node is tried. function rediscluster:get_random_connection() -- shuffle local shuffle_idx = {} local startpos = 1 local endpos = #self.nodes for i=startpos,endpos do shuffle_idx[i] = i end for i=startpos,endpos do local idx = math.random(i,endpos) local tmp = shuffle_idx[i] shuffle_idx[i] = shuffle_idx[idx] shuffle_idx[idx] = tmp end for i,idx in ipairs(shuffle_idx) do local ok,conn = pcall(function () local node = self.nodes[idx] local conn = self:get_connection(node) if conn:ping() == "PONG" then return conn else -- If the connection is not good close it ASAP in order -- to avoid waiting for the GC finalizer. File -- descriptors are a rare resource. self.connections[node.name] = nil conn:disconnect() end end) if ok and conn then return conn end end error("Can't reach a single startup node.") end -- Given a slot return the link (Redis instance) to the mapped node. -- Make sure to create a connection with the node if we don't have -- one. function rediscluster:get_connection_by_slot(slot) local node = self.slots[slot] -- If we don't know what the mapping is, return a random node. if not node then return self:get_random_connection() end local ok,conn = pcall(self.get_connection,self,node) if not ok then if self.opt.read_slave and node.slaves and #node.slaves > 0 then local slave_node = node.slaves[math.random(1,#node.slaves)] local ok2,conn = pcall(self.get_connection,self,slave_node) if ok2 then conn:readonly() -- allow this connection read-slave return conn end end -- This will probably never happen with recent redis-rb -- versions because the connection is enstablished in a lazy -- way only when a command is called. However it is wise to -- handle an instance creation error of some kind. return self:get_random_connection() end return conn end -- Dispatch commands. function rediscluster:call(...) local argv = table.pack(...) local cmd = string.lower(argv[1]) local key = self:get_key_from_command(cmd, argv) if not key then error("No way to dispatch this command to Redis Cluster: " .. tostring(argv[1])) end if cmd == "subscribe" or cmd == "unsubscribe" or cmd == "psubscribe" or cmd == "punsubscribe" then local slot = self:keyslot(key) local conn = self:get_watch_connection_by_slot(slot) local func = conn[cmd] return func(conn,table.unpack(argv,2)) end if self.refresh_table_asap then self:initialize_slots_cache() end local ttl = RedisClusterRequestTTL -- Max number of redirections local err local asking = false local try_random_node = false while ttl > 0 do ttl = ttl - 1 local conn local slot = self:keyslot(key) if asking then conn = self:get_connection(asking) elseif try_random_node then conn = self:get_random_connection() try_random_node = false else conn = self:get_connection_by_slot(slot) end local result if asking then -- use pipeline -- ensure ASKING and command execute sequentially and consecutively -- save one RTT local cmd_list = { { "asking" }, { ... }, } asking = false local func = conn["pipeline"] result = { pcall(func, conn, cmd_list) } else local func = conn[cmd] result = { pcall(func, conn, table.unpack(argv, 2)) } end local ok = result[1] if not ok then err = table.unpack(result,2) err = tostring(err) if err == "[Error: socket]" then -- may be never come here? try_random_node = true if ttl < RedisClusterRequestTTL/2 then skynet.sleep(10) end else -- err: ./lualib/skynet/socketchannel.lua:371: xxx err = string.match(err,".+:%d+:%s(.*)$") or err local errlist = {} for e in string.gmatch(err,"([^%s]+)%s?") do table.insert(errlist,e) end if (errlist[1] ~= "MOVED" and errlist[1] ~= "ASK") then error(err) else if errlist[1] == "ASK" then asking = true else -- Serve replied with MOVED. It's better for us to -- ask for CLUSTER SLOTS the next time. self.refresh_table_asap = true end local newslot = tonumber(errlist[2]) local node_ip,node_port = string.match(errlist[3],"^([^:]+):([^:]+)$") node_port = assert(tonumber(node_port)) local node = { host = node_ip, port = node_port, } if not asking then local oldnode = self.slots[newslot] if oldnode then node.slaves = oldnode.slaves end self:set_node_name(node) self.slots[newslot] = node else asking = node end end end else return table.unpack(result,2) end end error(string.format("Too many Cluster redirections?,maybe node is disconnected (last error: %q)",err)) end function rediscluster:get_watch_connection_by_slot(slot) if not self.slots[slot] then self:initialize_slots_cache() end local node = assert(self.slots[slot]) return self:get_watch_connection(node) end function rediscluster:get_watch_connection(node) local name = node.name local ok,conn = sync.once.Do(name,function () if not self.__watching[name] then local conf = { host = node.host, port = node.port, auth = self.opt.auth, db = self.opt.db or 0, } local db = redis.watch(conf) self.__watching[name] = db local onmessage = rawget(self,"__onmessage") skynet.fork(function () while true do local ok,data,channel,pchannel = pcall(db.message,db) if ok then if data and onmessage then pcall(onmessage,data,channel,pchannel) end end end end) end return self.__watching[name] end) assert(ok,conn) return conn end -- Currently we handle all the commands using method_missing for -- simplicity. For a Cluster client actually it will be better to have -- every single command as a method with the right arity and possibly -- additional checks (example: RPOPLPUSH with same src/dst key, SORT -- without GET or BY, and so forth). setmetatable(rediscluster,{ __index = function (t,cmd) t[cmd] = function (self,...) return self:call(cmd,...) end return t[cmd] end, }) return _M ================================================ FILE: lualib/skynet/db/redis/crc16.lua ================================================ --/* -- This is the CRC16 algorithm used by Redis Cluster to hash keys. -- Implementation according to CCITT standards. -- -- This is actually the XMODEM CRC 16 algorithm, using the -- following parameters: -- -- Name : "XMODEM", also known as "ZMODEM", "CRC-16/ACORN" -- Width : 16 bit -- Poly : 1021 (That is actually x^16 + x^12 + x^5 + 1) -- Initialization : 0000 -- Reflect Input byte : False -- Reflect Output CRC : False -- Xor constant to output CRC : 0000 -- Output for "123456789" : 31C3 --*/ local XMODEMCRC16Lookup = { 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7, 0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef, 0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6, 0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de, 0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485, 0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d, 0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4, 0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc, 0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823, 0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b, 0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12, 0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a, 0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41, 0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49, 0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70, 0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78, 0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f, 0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067, 0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e, 0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256, 0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d, 0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405, 0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c, 0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634, 0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab, 0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3, 0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a, 0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92, 0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9, 0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1, 0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8, 0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0 } return function(bytes) local crc = 0 for i=1,#bytes do local b = string.byte(bytes,i,i) crc = ((crc<<8) & 0xffff) ~ XMODEMCRC16Lookup[(((crc>>8)~b) & 0xff) + 1] end return tonumber(crc) end ================================================ FILE: lualib/skynet/db/redis.lua ================================================ local socketchannel = require "skynet.socketchannel" local tostring = tostring local tonumber = tonumber local table = table local string = string local assert = assert local setmetatable = setmetatable local ipairs = ipairs local type = type local select = select local pairs = pairs local redis = {} local command = {} local meta = { __index = command, -- DO NOT close channel in __gc } ---------- redis response local redcmd = {} redcmd[36] = function(fd, data) -- '$' local bytes = tonumber(data) if bytes < 0 then return true,nil end local firstline = fd:read(bytes+2) return true,string.sub(firstline,1,-3) end redcmd[43] = function(fd, data) -- '+' return true,data end redcmd[45] = function(fd, data) -- '-' return false,data end redcmd[58] = function(fd, data) -- ':' -- todo: return string later return true, tonumber(data) end local function read_response(fd) local result = fd:readline "\r\n" local firstchar = string.byte(result) local data = string.sub(result,2) return redcmd[firstchar](fd,data) end redcmd[42] = function(fd, data) -- '*' local n = tonumber(data) if n < 0 then return true, nil end local bulk = {} local noerr = true for i = 1,n do local ok, v = read_response(fd) if not ok then noerr = false end bulk[i] = v end return noerr, bulk end ------------------- function command:disconnect() self[1]:close() setmetatable(self, nil) end -- msg could be any type of value local function make_cache(f) return setmetatable({}, { __mode = "kv", __index = f, }) end local header_cache = make_cache(function(t,k) local s = "\r\n$" .. k .. "\r\n" t[k] = s return s end) local command_cache = make_cache(function(t,cmd) local s = "\r\n$"..#cmd.."\r\n"..cmd:upper() t[cmd] = s return s end) local count_cache = make_cache(function(t,k) local s = "*" .. k t[k] = s return s end) local command_np_cache = make_cache(function(t, cmd) local s = "*1" .. command_cache[cmd] .. "\r\n" t[cmd] = s return s end) local function compose_message(cmd, msg) if msg == nil then return command_np_cache[cmd] end local t = type(msg) local lines = {} if t == "table" then local n = msg.n or #msg lines[1] = count_cache[n+1] lines[2] = command_cache[cmd] local idx = 3 for i = 1, n do local v = msg[i] if v == nil then lines[idx] = "\r\n$-1" idx = idx + 1 else v= tostring(v) lines[idx] = header_cache[#v] lines[idx+1] = v idx = idx + 2 end end lines[idx] = "\r\n" else msg = tostring(msg) lines[1] = "*2" lines[2] = command_cache[cmd] lines[3] = header_cache[#msg] lines[4] = msg lines[5] = "\r\n" end return lines end local function redis_login(conf) local auth = conf.auth local db = conf.db if auth == nil and db == nil then return end if auth then if conf.username then auth = { conf.username, auth } end end return function(so) if auth then so:request(compose_message("AUTH", auth), read_response) end if db then so:request(compose_message("SELECT", db), read_response) end end end function redis.connect(db_conf) local channel = socketchannel.channel { host = db_conf.host, port = db_conf.port or 6379, auth = redis_login(db_conf), nodelay = true, overload = db_conf.overload, } -- try connect first only once channel:connect(true) return setmetatable( { channel }, meta ) end setmetatable(command, { __index = function(t,k) local cmd = string.upper(k) local f = function (self, v, ...) if v == nil then return self[1]:request(compose_message(cmd), read_response) elseif type(v) == "table" then return self[1]:request(compose_message(cmd, v), read_response) else return self[1]:request(compose_message(cmd, table.pack(v, ...)), read_response) end end t[k] = f return f end}) local function read_boolean(so) local ok, result = read_response(so) return ok, result ~= 0 end function command:exists(key) local fd = self[1] return fd:request(compose_message ("EXISTS", key), read_boolean) end function command:sismember(key, value) local fd = self[1] return fd:request(compose_message ("SISMEMBER", table.pack(key, value)), read_boolean) end local function compose_table(lines, msg) local tinsert = table.insert tinsert(lines, count_cache[#msg]) local val for _,v in ipairs(msg) do val = tostring(v) tinsert(lines,header_cache[#val]) tinsert(lines,val) end tinsert(lines, "\r\n") return lines end function command:pipeline(ops,resp) assert(ops and #ops > 0, "pipeline is null") local fd = self[1] local cmds = {} for _, cmd in ipairs(ops) do compose_table(cmds, cmd) end if resp then return fd:request(cmds, function (fd) for _=1, #ops do local ok, out = read_response(fd) table.insert(resp, {ok = ok, out = out}) end return true, resp end) else return fd:request(cmds, function (fd) local ok, out for _=1, #ops do ok, out = read_response(fd) end -- return last response return ok,out end) end end --- watch mode local watch = {} local watchmeta = { __index = watch, __gc = function(self) self.__sock:close() end, } local function watch_login(conf, obj) local login_auth = redis_login(conf) return function(so) if login_auth then login_auth(so) end for k in pairs(obj.__psubscribe) do so:request(compose_message ("PSUBSCRIBE", k)) end for k in pairs(obj.__subscribe) do so:request(compose_message("SUBSCRIBE", k)) end end end function redis.watch(db_conf) local obj = { __subscribe = {}, __psubscribe = {}, } local channel = socketchannel.channel { host = db_conf.host, port = db_conf.port or 6379, auth = watch_login(db_conf, obj), nodelay = true, } obj.__sock = channel -- try connect first only once channel:connect(true) return setmetatable( obj, watchmeta ) end function watch:disconnect() self.__sock:close() setmetatable(self, nil) end local function watch_func( name ) local NAME = string.upper(name) watch[name] = function(self, ...) local so = self.__sock for i = 1, select("#", ...) do local v = select(i, ...) so:request(compose_message(NAME, v)) end end end watch_func "subscribe" watch_func "psubscribe" watch_func "unsubscribe" watch_func "punsubscribe" function watch:message() local so = self.__sock while true do local ret = so:response(read_response) local ttype , channel, data , data2 = ret[1], ret[2], ret[3], ret[4] if ttype == "message" then return data, channel elseif ttype == "pmessage" then return data2, data, channel elseif ttype == "subscribe" then self.__subscribe[channel] = true elseif ttype == "psubscribe" then self.__psubscribe[channel] = true elseif ttype == "unsubscribe" then self.__subscribe[channel] = nil elseif ttype == "punsubscribe" then self.__psubscribe[channel] = nil end end end return redis ================================================ FILE: lualib/skynet/debug.lua ================================================ local table = table local extern_dbgcmd = {} local function init(skynet, export) local internal_info_func function skynet.info_func(func) internal_info_func = func end local dbgcmd local function init_dbgcmd() dbgcmd = {} function dbgcmd.MEM() local kb = collectgarbage "count" skynet.ret(skynet.pack(kb)) end local gcing = false function dbgcmd.GC() if gcing then return end gcing = true local before = collectgarbage "count" local before_time = skynet.now() collectgarbage "collect" -- skip subsequent GC message skynet.yield() local after = collectgarbage "count" local after_time = skynet.now() skynet.error(string.format("GC %.2f Kb -> %.2f Kb, cost %.2f sec", before, after, (after_time - before_time) / 100)) gcing = false end function dbgcmd.STAT() local stat = {} stat.task = skynet.task() stat.mqlen = skynet.stat "mqlen" stat.cpu = skynet.stat "cpu" stat.message = skynet.stat "message" skynet.ret(skynet.pack(stat)) end function dbgcmd.KILLTASK(threadname) local co = skynet.killthread(threadname) if co then skynet.error(string.format("Kill %s", co)) skynet.ret() else skynet.error(string.format("Kill %s : Not found", threadname)) skynet.ret(skynet.pack "Not found") end end function dbgcmd.TASK(session) if session then skynet.ret(skynet.pack(skynet.task(session))) else local task = {} skynet.task(task) skynet.ret(skynet.pack(task)) end end function dbgcmd.UNIQTASK() skynet.ret(skynet.pack(skynet.uniqtask())) end function dbgcmd.INFO(...) if internal_info_func then skynet.ret(skynet.pack(internal_info_func(...))) else skynet.ret(skynet.pack(nil)) end end function dbgcmd.EXIT() skynet.exit() end function dbgcmd.RUN(source, filename, ...) local inject = require "skynet.inject" local args = table.pack(...) local ok, output = inject(skynet, source, filename, args, export.dispatch, skynet.register_protocol) collectgarbage "collect" skynet.ret(skynet.pack(ok, table.concat(output, "\n"))) end function dbgcmd.TERM(service) skynet.term(service) end function dbgcmd.REMOTEDEBUG(...) local remotedebug = require "skynet.remotedebug" remotedebug.start(export, ...) end function dbgcmd.SUPPORT(pname) return skynet.ret(skynet.pack(skynet.dispatch(pname) ~= nil)) end function dbgcmd.PING() return skynet.ret() end function dbgcmd.LINK() skynet.response() -- get response , but not return. raise error when exit end function dbgcmd.TRACELOG(proto, flag) if type(proto) ~= "string" then flag = proto proto = "lua" end skynet.error(string.format("Turn trace log %s for %s", flag, proto)) skynet.traceproto(proto, flag) skynet.ret() end return dbgcmd end -- function init_dbgcmd local function _debug_dispatch(session, address, cmd, ...) dbgcmd = dbgcmd or init_dbgcmd() -- lazy init dbgcmd local f = dbgcmd[cmd] or extern_dbgcmd[cmd] assert(f, cmd) f(...) end skynet.register_protocol { name = "debug", id = assert(skynet.PTYPE_DEBUG), pack = assert(skynet.pack), unpack = assert(skynet.unpack), dispatch = _debug_dispatch, } end local function reg_debugcmd(name, fn) extern_dbgcmd[name] = fn end return { init = init, reg_debugcmd = reg_debugcmd, } ================================================ FILE: lualib/skynet/dns.lua ================================================ --[[ lua dns resolver library See https://github.com/xjdrew/levent/blob/master/levent/dns.lua for more detail -- resource record type: -- TYPE value and meaning -- A 1 a host address -- NS 2 an authoritative name server -- MD 3 a mail destination (Obsolete - use MX) -- MF 4 a mail forwarder (Obsolete - use MX) -- CNAME 5 the canonical name for an alias -- SOA 6 marks the start of a zone of authority -- MB 7 a mailbox domain name (EXPERIMENTAL) -- MG 8 a mail group member (EXPERIMENTAL) -- MR 9 a mail rename domain name (EXPERIMENTAL) -- NULL 10 a null RR (EXPERIMENTAL) -- WKS 11 a well known service description -- PTR 12 a domain name pointer -- HINFO 13 host information -- MINFO 14 mailbox or mail list information -- MX 15 mail exchange -- TXT 16 text strings -- AAAA 28 a ipv6 host address -- only appear in the question section: -- AXFR 252 A request for a transfer of an entire zone -- MAILB 253 A request for mailbox-related records (MB, MG or MR) -- MAILA 254 A request for mail agent RRs (Obsolete - see MX) -- * 255 A request for all records -- -- resource recode class: -- IN 1 the Internet -- CS 2 the CSNET class (Obsolete - used only for examples in some obsolete RFCs) -- CH 3 the CHAOS class -- HS 4 Hesiod [Dyer 87] -- only appear in the question section: -- * 255 any class -- ]] --[[ -- struct header { -- uint16_t tid # identifier assigned by the program that generates any kind of query. -- uint16_t flags # flags -- uint16_t qdcount # the number of entries in the question section. -- uint16_t ancount # the number of resource records in the answer section. -- uint16_t nscount # the number of name server resource records in the authority records section. -- uint16_t arcount # the number of resource records in the additional records section. -- } -- -- request body: -- struct request { -- string name -- uint16_t atype -- uint16_t class -- } -- -- response body: -- struct response { -- string name -- uint16_t atype -- uint16_t class -- uint16_t ttl -- uint16_t rdlength -- string rdata -- } --]] local skynet = require "skynet" local socket = require "skynet.socket" local MAX_DOMAIN_LEN = 1024 local MAX_LABEL_LEN = 63 local MAX_PACKET_LEN = 2048 local DNS_HEADER_LEN = 12 local TIMEOUT = 30 * 100 -- 30 seconds local QTYPE = { A = 1, CNAME = 5, AAAA = 28, } local QCLASS = { IN = 1, } local weak = {__mode = "kv"} local CACHE = {} local dns = {} local request_pool = {} local local_hosts -- local static table lookup for hostnames dns.DEFAULT_HOSTS = "/etc/hosts" dns.DEFAULT_RESOLV_CONF = "/etc/resolv.conf" -- return name type: 'ipv4', 'ipv6', or 'hostname' local function guess_name_type(name) if name:match("^[%d%.]+$") then return "ipv4" end if name:find(":") then return "ipv6" end return "hostname" end -- http://man7.org/linux/man-pages/man5/hosts.5.html local function parse_hosts() if not dns.DEFAULT_HOSTS then return end local f = io.open(dns.DEFAULT_HOSTS) if not f then return end local rts = {} for line in f:lines() do local ip, hosts = string.match(line, "^%s*([%[%]%x%.%:]+)%s+([^#;]+)") if not ip or not hosts then goto continue end local family = guess_name_type(ip) if family == "hostname" then goto continue end for host in hosts:gmatch("%S+") do local h = host:lower() local rt = rts[h] if not rt then rt = {} rts[h] = rt end if not rt[family] then rt[family] = {} end table.insert(rt[family], ip) end ::continue:: end return rts end -- http://man7.org/linux/man-pages/man5/resolv.conf.5.html local function parse_resolv_conf() if not dns.DEFAULT_RESOLV_CONF then return end local f = io.open(dns.DEFAULT_RESOLV_CONF) if not f then return end local server for line in f:lines() do server = line:match("^%s*nameserver%s+([^#;%s]+)") if server then break end end f:close() return server end function dns.flush() CACHE[QTYPE.A] = setmetatable({},weak) CACHE[QTYPE.AAAA] = setmetatable({},weak) end dns.flush() local function verify_domain_name(name) if #name > MAX_DOMAIN_LEN then return false end if not name:match("^[_%l%d%-%.]+$") then return false end for w in name:gmatch("([_%w%-]+)%.?") do if #w > MAX_LABEL_LEN then return false end end return true end local next_tid = 1 local function gen_tid() local tid = next_tid if request_pool[tid] then tid = nil for i = 1, 65535 do -- find available tid if not request_pool[i] then tid = i break end end assert(tid) end next_tid = tid + 1 if next_tid > 65535 then next_tid = 1 end return tid end local function pack_header(t) return string.pack(">HHHHHH", t.tid, t.flags, t.qdcount, t.ancount or 0, t.nscount or 0, t.arcount or 0) end local function pack_question(name, qtype, qclass) local labels = {} for w in name:gmatch("([_%w%-]+)%.?") do table.insert(labels, string.pack("s1",w)) end table.insert(labels, '\0') table.insert(labels, string.pack(">HH", qtype, qclass)) return table.concat(labels) end local function unpack_header(chunk) local tid, flags, qdcount, ancount, nscount, arcount, left = string.unpack(">HHHHHH", chunk) return { tid = tid, flags = flags, qdcount = qdcount, ancount = ancount, nscount = nscount, arcount = arcount }, left end -- unpack a resource name local function unpack_name(chunk, left) local t = {} local jump_pointer local tag, offset, label while true do tag, left = string.unpack("B", chunk, left) if tag & 0xc0 == 0xc0 then -- pointer offset,left = string.unpack(">H", chunk, left - 1) offset = offset & 0x3fff if not jump_pointer then jump_pointer = left end -- offset is base 0, need to plus 1 left = offset + 1 elseif tag == 0 then break else label, left = string.unpack("s1", chunk, left - 1) t[#t+1] = label end end return table.concat(t, "."), jump_pointer or left end local function unpack_question(chunk, left) local name, left = unpack_name(chunk, left) local atype, class, left = string.unpack(">HH", chunk, left) return { name = name, atype = atype, class = class }, left end local function unpack_answer(chunk, left) local name, left = unpack_name(chunk, left) local atype, class, ttl, rdata, left = string.unpack(">HHI4s2", chunk, left) return { name = name, atype = atype, class = class, ttl = ttl, rdata = rdata },left end local function unpack_rdata(qtype, chunk) if qtype == QTYPE.A then local a,b,c,d = string.unpack("BBBB", chunk) return string.format("%d.%d.%d.%d", a,b,c,d) elseif qtype == QTYPE.AAAA then local a,b,c,d,e,f,g,h = string.unpack(">HHHHHHHH", chunk) return string.format("%x:%x:%x:%x:%x:%x:%x:%x", a, b, c, d, e, f, g, h) else error("Error qtype " .. qtype) end end local dns_server = { fd = nil, address = nil, port = nil, retire = nil, } local function resolve(content) if #content < DNS_HEADER_LEN then -- drop skynet.error("Recv an invalid package when dns query") return end local answer_header,left = unpack_header(content) -- verify answer assert(answer_header.qdcount == 1, "malformed packet") local question,left = unpack_question(content, left) local ttl local answer local answers_ipv4 local answers_ipv6 for i=1, answer_header.ancount do answer, left = unpack_answer(content, left) local answers if answer.atype == QTYPE.A then answers_ipv4 = answers_ipv4 or {} answers = answers_ipv4 elseif answer.atype == QTYPE.AAAA then answers_ipv6 = answers_ipv6 or {} answers = answers_ipv6 end if answers then local ip = unpack_rdata(answer.atype, answer.rdata) ttl = ttl and math.min(ttl, answer.ttl) or answer.ttl answers[#answers+1] = ip end end if answers_ipv4 then CACHE[QTYPE.A][question.name] = { answers = answers_ipv4, ttl = skynet.now() + ttl * 100 } end if answers_ipv6 then CACHE[QTYPE.AAAA][question.name] = { answers = answers_ipv6, ttl = skynet.now() + ttl * 100 } end local resp = request_pool[answer_header.tid] if not resp then -- the resp may be timeout return end if question.name ~= resp.name then skynet.error("Recv an invalid name when dns query") end local r = CACHE[resp.qtype][resp.name] if r then resp.answers = r.answers end skynet.wakeup(resp.co) end local function connect_server() local fd = socket.udp(function(str, from) resolve(str) end) if not dns_server.address then dns_server.address = parse_resolv_conf() dns_server.port = 53 end assert(dns_server.address, "Call dns.server first") local ok, err = pcall(socket.udp_connect,fd, dns_server.address, dns_server.port) if not ok then socket.close(fd) error(err) end dns_server.fd = fd skynet.error(string.format("Udp server open %s:%s (%d)", dns_server.address, dns_server.port, fd)) end local DNS_SERVER_RETIRE = 60 * 100 local function touch_server() dns_server.retire = skynet.now() if dns_server.fd then return end connect_server() local function check_alive() if skynet.now() > dns_server.retire + DNS_SERVER_RETIRE then local fd = dns_server.fd if fd then dns_server.fd = nil socket.close(fd) skynet.error(string.format("Udp server close %s:%s (%d)", dns_server.address, dns_server.port, fd)) end else skynet.timeout( 2 * DNS_SERVER_RETIRE, check_alive) end end skynet.timeout( 2 * DNS_SERVER_RETIRE, check_alive) end function dns.server(server, port) dns_server.address = server dns_server.port = port or 53 end local function lookup_cache(name, qtype, ignorettl) local result = CACHE[qtype][name] if result then if ignorettl or (result.ttl > skynet.now()) then return result.answers end end end local function suspend(tid, name, qtype) local req = { name = name, tid = tid, qtype = qtype, co = coroutine.running(), } request_pool[tid] = req skynet.fork(function() skynet.sleep(TIMEOUT) local req = request_pool[tid] if req then -- cancel tid skynet.error(string.format("DNS query %s timeout", name)) request_pool[tid] = nil skynet.wakeup(req.co) end end) skynet.wait(req.co) request_pool[tid] = nil if not req.answers then local answers = lookup_cache(name, qtype, true) if answers then return answers[1], answers end error "timeout or no answer" end return req.answers[1], req.answers end -- lookup local static table local function local_resolve(name, ipv6) if not local_hosts then local_hosts = parse_hosts() end if not local_hosts then return end local family = ipv6 and "ipv6" or "ipv4" local t = local_hosts[name] if t then local answers = t[family] if answers then return answers[1], answers end end return nil end -- lookup dns server local function remote_resolve(name, ipv6) local qtype = ipv6 and QTYPE.AAAA or QTYPE.A local answers = lookup_cache(name, qtype) if answers then return answers[1], answers end local question_header = { tid = gen_tid(), flags = 0x100, -- flags: 00000001 00000000, set RD qdcount = 1, } local req = pack_header(question_header) .. pack_question(name, qtype, QCLASS.IN) touch_server() socket.write(dns_server.fd, req) return suspend(question_header.tid, name, qtype) end function dns.resolve(name, ipv6) local name = name:lower() local ntype = guess_name_type(name) if ntype ~= "hostname" then if (ipv6 and name == "ipv4") or (not ipv6 and name == "ipv6") then return nil, "illegal ip address" end return name end if not verify_domain_name(name) then return nil, "illegal name" end local answer, answers = local_resolve(name, ipv6) if answer then return answer, answers end return remote_resolve(name, ipv6) end return dns ================================================ FILE: lualib/skynet/harbor.lua ================================================ local skynet = require "skynet" local harbor = {} function harbor.globalname(name, handle) handle = handle or skynet.self() skynet.send(".cslave", "lua", "REGISTER", name, handle) end function harbor.queryname(name) return skynet.call(".cslave", "lua", "QUERYNAME", name) end function harbor.link(id) skynet.call(".cslave", "lua", "LINK", id) end function harbor.connect(id) skynet.call(".cslave", "lua", "CONNECT", id) end function harbor.linkmaster() skynet.call(".cslave", "lua", "LINKMASTER") end return harbor ================================================ FILE: lualib/skynet/inject.lua ================================================ local function getupvaluetable(u, func, unique) local i = 1 while true do local name, value = debug.getupvalue(func, i) if name == nil then return end local t = type(value) if t == "table" then u[name] = value elseif t == "function" then if not unique[value] then unique[value] = true getupvaluetable(u, value, unique) end end i=i+1 end end return function(skynet, source, filename, args, ...) if filename then filename = "@" .. filename else filename = "=(load)" end local output = {} local function print(...) local value = { ... } for k,v in ipairs(value) do value[k] = tostring(v) end table.insert(output, table.concat(value, "\t")) end local u = {} local unique = {} local funcs = { ... } for k, func in ipairs(funcs) do getupvaluetable(u, func, unique) end local p = {} local proto = u.proto if proto then for k,v in pairs(proto) do local name, dispatch = v.name, v.dispatch if name and dispatch and not p[name] then local pp = {} p[name] = pp getupvaluetable(pp, dispatch, unique) end end end local env = setmetatable( { print = print , _U = u, _P = p}, { __index = _ENV }) local func, err = load(source, filename, "bt", env) if not func then return false, { err } end local ok, err = skynet.pcall(func, table.unpack(args, 1, args.n)) if not ok then table.insert(output, err) return false, output end return true, output end ================================================ FILE: lualib/skynet/injectcode.lua ================================================ local debug = debug local table = table local FUNC_TEMP=[[ local $ARGS return function(...) $SOURCE end, function() return {$LOCALS} end ]] local temp = {} local function wrap_locals(co, source, level, ext_funcs) if co == coroutine.running() then level = level + 3 end local f = debug.getinfo(co, level,"f").func if f == nil then return false, "Invalid level" end local uv = {} local locals = {} local uv_id = {} local local_id = {} if ext_funcs then for k,v in pairs(ext_funcs) do table.insert(uv, k) end end local i = 1 while true do local name, value = debug.getlocal(co, level, i) if name == nil then break end if name:byte() ~= 40 then -- '(' table.insert(uv, name) table.insert(locals, ("[%d]=%s,"):format(i,name)) local_id[name] = value end i = i + 1 end local i = 1 while true do local name = debug.getupvalue(f, i) if name == nil then break end uv_id[name] = i table.insert(uv, name) i = i + 1 end temp.ARGS = table.concat(uv, ",") temp.SOURCE = source temp.LOCALS = table.concat(locals) local full_source = FUNC_TEMP:gsub("%$(%w+)",temp) local loader, err = load(full_source, "=(debug)") if loader == nil then return false, err end local func, update = loader() -- join func's upvalues local i = 1 while true do local name = debug.getupvalue(func, i) if name == nil then break end if ext_funcs then local v = ext_funcs[name] if v then debug.setupvalue(func, i, v) end end local local_value = local_id[name] if local_value then debug.setupvalue(func, i, local_value) end local upvalue_id = uv_id[name] if upvalue_id then debug.upvaluejoin(func, i, f, upvalue_id) end i=i+1 end local vararg, v = debug.getlocal(co, level, -1) if vararg then local vargs = { v } local i = 2 while true do local vararg,v = debug.getlocal(co, level, -i) if vararg then vargs[i] = v else break end i=i+1 end return func, update, table.unpack(vargs) else return func, update end end local function exec(co, level, func, update, ...) if not func then return false, update end if co == coroutine.running() then level = level + 2 end local rets = table.pack(pcall(func, ...)) if rets[1] then local needupdate = update() for k,v in pairs(needupdate) do debug.setlocal(co, level,k,v) end return table.unpack(rets, 1, rets.n) else return false, rets[2] end end return function (source, co, level, ext_funcs) co = co or coroutine.running() level = level or 0 return exec(co, level, wrap_locals(co, source, level, ext_funcs)) end ================================================ FILE: lualib/skynet/manager.lua ================================================ local skynet = require "skynet" local c = require "skynet.core" local function number_address(name) local t = type(name) if t == "number" then return name elseif t == "string" then local hex = name:match "^:(%x+)" if hex then return tonumber(hex, 16) end end end function skynet.launch(...) local addr = c.command("LAUNCH", table.concat({...}," ")) if addr then return tonumber(string.sub(addr , 2), 16) end end function skynet.kill(name) local addr = number_address(name) if addr then skynet.send(".launcher","lua","REMOVE", addr, true) name = skynet.address(addr) end c.command("KILL",name) end function skynet.abort() c.command("ABORT") end local function globalname(name, handle) local c = string.sub(name,1,1) assert(c ~= ':') if c == '.' then return false end assert(#name < 16) -- GLOBALNAME_LENGTH is 16, defined in skynet_harbor.h assert(tonumber(name) == nil) -- global name can't be number local harbor = require "skynet.harbor" harbor.globalname(name, handle) return true end function skynet.register(name) if not globalname(name) then c.command("REG", name) end end function skynet.name(name, handle) if not globalname(name, handle) then c.command("NAME", name .. " " .. skynet.address(handle)) end end local dispatch_message = skynet.dispatch_message function skynet.forward_type(map, start_func) c.callback(function(ptype, msg, sz, ...) local prototype = map[ptype] if prototype then dispatch_message(prototype, msg, sz, ...) else local ok, err = pcall(dispatch_message, ptype, msg, sz, ...) c.trash(msg, sz) if not ok then error(err) end end end, true) skynet.timeout(0, function() skynet.init_service(start_func) end) end function skynet.filter(f ,start_func) c.callback(function(...) dispatch_message(f(...)) end) skynet.timeout(0, function() skynet.init_service(start_func) end) end function skynet.monitor(service, query) local monitor if query then monitor = skynet.queryservice(true, service) else monitor = skynet.uniqueservice(true, service) end assert(monitor, "Monitor launch failed") c.command("MONITOR", string.format(":%08x", monitor)) return monitor end return skynet ================================================ FILE: lualib/skynet/mqueue.lua ================================================ -- This is a deprecated module, use skynet.queue instead. local skynet = require "skynet" local c = require "skynet.core" local mqueue = {} local init_once local thread_id local message_queue = {} skynet.register_protocol { name = "queue", -- please read skynet.h for magic number 8 id = 8, pack = skynet.pack, unpack = skynet.unpack, dispatch = function(session, from, ...) table.insert(message_queue, {session = session, addr = from, ... }) if thread_id then skynet.wakeup(thread_id) thread_id = nil end end } local function do_func(f, msg) return pcall(f, table.unpack(msg)) end local function message_dispatch(f) while true do if #message_queue==0 then thread_id = coroutine.running() skynet.wait() else local msg = table.remove(message_queue,1) local session = msg.session if session == 0 then local ok, msg = do_func(f, msg) if ok then if msg then skynet.fork(message_dispatch,f) error(string.format("[:%x] send a message to [:%x] return something", msg.addr, skynet.self())) end else skynet.fork(message_dispatch,f) error(string.format("[:%x] send a message to [:%x] throw an error : %s", msg.addr, skynet.self(),msg)) end else local data, size = skynet.pack(do_func(f,msg)) -- 1 means response c.send(msg.addr, 1, session, data, size) end end end end function mqueue.register(f) assert(init_once == nil) init_once = true skynet.fork(message_dispatch,f) end local function catch(succ, ...) if succ then return ... else error(...) end end function mqueue.call(addr, ...) return catch(skynet.call(addr, "queue", ...)) end function mqueue.send(addr, ...) return skynet.send(addr, "queue", ...) end function mqueue.size() return #message_queue end return mqueue ================================================ FILE: lualib/skynet/multicast.lua ================================================ local skynet = require "skynet" local mc = require "skynet.multicast.core" local multicastd local multicast = {} local dispatch = {} local chan = {} local chan_meta = { __index = chan, __gc = function(self) self:unsubscribe() end, __tostring = function (self) return string.format("[Multicast:%x]",self.channel) end, } function multicast.new(conf) assert(multicastd, "Init first") local self = {} conf = conf or self self.channel = conf.channel if self.channel == nil then self.channel = skynet.call(multicastd, "lua", "NEW") end self.__pack = conf.pack or skynet.pack self.__unpack = conf.unpack or skynet.unpack self.__dispatch = conf.dispatch return setmetatable(self, chan_meta) end function chan:delete() local c = assert(self.channel) skynet.send(multicastd, "lua", "DEL", c) self.channel = nil self.__subscribe = nil end function chan:publish(...) local c = assert(self.channel) skynet.call(multicastd, "lua", "PUB", c, mc.pack(self.__pack(...))) end function chan:subscribe() local c = assert(self.channel) if self.__subscribe then -- already subscribe return end skynet.call(multicastd, "lua", "SUB", c) self.__subscribe = true dispatch[c] = self end function chan:unsubscribe() if not self.__subscribe then -- already unsubscribe return end local c = assert(self.channel) skynet.send(multicastd, "lua", "USUB", c) self.__subscribe = nil dispatch[c] = nil end local function dispatch_subscribe(channel, source, pack, msg, sz) -- channel as session, do need response skynet.ignoreret() local self = dispatch[channel] if not self then mc.close(pack) -- This channel may unsubscribe first, see #1141 return end if self.__subscribe then local ok, err = pcall(self.__dispatch, self, source, self.__unpack(msg, sz)) mc.close(pack) assert(ok, err) else -- maybe unsubscribe first, but the message is send out. drop the message unneed mc.close(pack) end end local function init() multicastd = skynet.uniqueservice "multicastd" skynet.register_protocol { name = "multicast", id = skynet.PTYPE_MULTICAST, unpack = mc.unpack, dispatch = dispatch_subscribe, } end skynet.init(init) return multicast ================================================ FILE: lualib/skynet/queue.lua ================================================ local skynet = require "skynet" local coroutine = coroutine local xpcall = xpcall local traceback = debug.traceback local table = table function skynet.queue() local current_thread local ref = 0 local thread_queue = {} local function xpcall_ret(ok, ...) ref = ref - 1 if ref == 0 then current_thread = table.remove(thread_queue,1) if current_thread then skynet.wakeup(current_thread) end end assert(ok, (...)) return ... end return function(f, ...) local thread = coroutine.running() if current_thread and current_thread ~= thread then table.insert(thread_queue, thread) skynet.wait() assert(ref == 0) -- current_thread == thread end current_thread = thread ref = ref + 1 return xpcall_ret(xpcall(f, traceback, ...)) end end return skynet.queue ================================================ FILE: lualib/skynet/remotedebug.lua ================================================ local skynet = require "skynet" local debugchannel = require "skynet.debugchannel" local socketdriver = require "skynet.socketdriver" local injectrun = require "skynet.injectcode" local table = table local debug = debug local coroutine = coroutine local sethook = debugchannel.sethook local M = {} local HOOK_FUNC = "raw_dispatch_message" local raw_dispatcher local print = _G.print local skynet_suspend local skynet_resume local prompt local newline local function change_prompt(s) newline = true prompt = s end local function replace_upvalue(func, uvname, value) local i = 1 while true do local name, uv = debug.getupvalue(func, i) if name == nil then break end if name == uvname then if value then debug.setupvalue(func, i, value) end return uv end i = i + 1 end end local function remove_hook(dispatcher) assert(raw_dispatcher, "Not in debug mode") replace_upvalue(dispatcher, HOOK_FUNC, raw_dispatcher) raw_dispatcher = nil print = _G.print skynet.error "Leave debug mode" end local function gen_print(fd) -- redirect print to socket fd return function(...) local tmp = table.pack(...) for i=1,tmp.n do tmp[i] = tostring(tmp[i]) end table.insert(tmp, "\n") socketdriver.send(fd, table.concat(tmp, "\t")) end end local function run_exp(ok, ...) if ok then print(...) end return ok end local function run_cmd(cmd, env, co, level) if not run_exp(injectrun("return "..cmd, co, level, env)) then print(select(2, injectrun(cmd,co, level,env))) end end local ctx_skynet = debug.getinfo(skynet.start,"S").short_src -- skip when enter this source file local ctx_term = debug.getinfo(run_cmd, "S").short_src -- term when get here local ctx_active = {} local linehook local function skip_hook(mode) local co = coroutine.running() local ctx = ctx_active[co] if mode == "return" then ctx.level = ctx.level - 1 if ctx.level == 0 then ctx.needupdate = true sethook(linehook, "crl") end else ctx.level = ctx.level + 1 end end function linehook(mode, line) local co = coroutine.running() local ctx = ctx_active[co] if mode ~= "line" then ctx.needupdate = true if mode ~= "return" then if ctx.next_mode or debug.getinfo(2,"S").short_src == ctx_skynet then ctx.level = 1 sethook(skip_hook, "cr") end end else if ctx.needupdate then ctx.needupdate = false ctx.filename = debug.getinfo(2, "S").short_src if ctx.filename == ctx_term then ctx_active[co] = nil sethook() change_prompt(string.format(":%08x>", skynet.self())) return end end change_prompt(string.format("%s(%d)>",ctx.filename, line)) return true -- yield end end local function add_watch_hook() local co = coroutine.running() local ctx = {} ctx_active[co] = ctx local level = 1 sethook(function(mode) if mode == "return" then level = level - 1 else level = level + 1 if level == 0 then ctx.needupdate = true sethook(linehook, "crl") end end end, "cr") end local function watch_proto(protoname, cond) local proto = assert(replace_upvalue(skynet.register_protocol, "proto"), "Can't find proto table") local p = proto[protoname] if p == nil then return "No " .. protoname end local dispatch = p.dispatch_origin or p.dispatch if dispatch == nil then return "No dispatch" end p.dispatch_origin = dispatch p.dispatch = function(...) if not cond or cond(...) then p.dispatch = dispatch -- restore origin dispatch function add_watch_hook() end dispatch(...) end end local function remove_watch() for co in pairs(ctx_active) do sethook(co) end ctx_active = {} end local dbgcmd = {} function dbgcmd.s(co) local ctx = ctx_active[co] ctx.next_mode = false skynet_suspend(co, skynet_resume(co)) end function dbgcmd.n(co) local ctx = ctx_active[co] ctx.next_mode = true skynet_suspend(co, skynet_resume(co)) end function dbgcmd.c(co) sethook(co) ctx_active[co] = nil change_prompt(string.format(":%08x>", skynet.self())) skynet_suspend(co, skynet_resume(co)) end local function hook_dispatch(dispatcher, resp, fd, channel) change_prompt(string.format(":%08x>", skynet.self())) print = gen_print(fd) local env = { print = print, watch = watch_proto } local watch_env = { print = print } local function watch_cmd(cmd) local co = next(ctx_active) watch_env._CO = co if dbgcmd[cmd] then dbgcmd[cmd](co) else run_cmd(cmd, watch_env, co, 0) end end local function debug_hook() while true do if newline then socketdriver.send(fd, prompt) newline = false end local cmd = channel:read() if cmd then if cmd == "cont" then -- leave debug mode break end if cmd ~= "" then if next(ctx_active) then watch_cmd(cmd) else run_cmd(cmd, env, coroutine.running(),2) end end newline = true else -- no input return end end -- exit debug mode remove_watch() remove_hook(dispatcher) resp(true) end local func local function hook(...) debug_hook() return func(...) end func = replace_upvalue(dispatcher, HOOK_FUNC, hook) if func then local function idle() if raw_dispatcher then skynet.timeout(10,idle) -- idle every 0.1s end end skynet.timeout(0, idle) end return func end function M.start(import, fd, handle) local dispatcher = import.dispatch skynet_suspend = import.suspend skynet_resume = import.resume assert(raw_dispatcher == nil, "Already in debug mode") skynet.error "Enter debug mode" local channel = debugchannel.connect(handle) raw_dispatcher = hook_dispatch(dispatcher, skynet.response(), fd, channel) end return M ================================================ FILE: lualib/skynet/require.lua ================================================ -- skynet module two-step initialize . When you require a skynet module : -- 1. Run module main function as official lua module behavior. -- 2. Run the functions register by skynet.init() during the step 1, -- unless calling `require` in main thread . -- If you call `require` in main thread ( service main function ), the functions -- registered by skynet.init() do not execute immediately, they will be executed -- by skynet.start() before start function. local M = {} local mainthread, ismain = coroutine.running() assert(ismain, "skynet.require must initialize in main thread") local context = { [mainthread] = {}, } do local require = _G.require local loaded = package.loaded local loading = {} function M.require(name) local m = loaded[name] if m ~= nil then return m end local co, main = coroutine.running() if main then return require(name) end local filename = package.searchpath(name, package.path) if not filename then return require(name) end local modfunc = loadfile(filename) if not modfunc then return require(name) end local loading_queue = loading[name] if loading_queue then assert(loading_queue.co ~= co, "circular dependency") -- Module is in the init process (require the same mod at the same time in different coroutines) , waiting. local skynet = require "skynet" loading_queue[#loading_queue+1] = co skynet.wait(co) local m = loaded[name] if m == nil then error(string.format("require %s failed", name)) end return m end loading_queue = {co = co} loading[name] = loading_queue local old_init_list = context[co] local init_list = {} context[co] = init_list -- We should call modfunc in lua, because modfunc may yield by calling M.require recursive. local function execute_module() local m = modfunc(name, filename) for _, f in ipairs(init_list) do f() end if m == nil then m = true end loaded[name] = m end local ok, err = xpcall(execute_module, debug.traceback) context[co] = old_init_list local waiting = #loading_queue if waiting > 0 then local skynet = require "skynet" for i = 1, waiting do skynet.wakeup(loading_queue[i]) end end loading[name] = nil if ok then return loaded[name] else error(err) end end end function M.init_all() for _, f in ipairs(context[mainthread]) do f() end context[mainthread] = nil end function M.init(f) assert(type(f) == "function") local co = coroutine.running() table.insert(context[co], f) end return M ================================================ FILE: lualib/skynet/service.lua ================================================ local skynet = require "skynet" local service = {} local cache = {} local provider local function get_provider() provider = provider or skynet.uniqueservice "service_provider" return provider end local function check(func) local info = debug.getinfo(func, "u") assert(info.nups == 1) assert(debug.getupvalue(func,1) == "_ENV") end function service.new(name, mainfunc, ...) local p = get_provider() local addr, booting = skynet.call(p, "lua", "test", name) local address if addr then address = addr else if booting then address = skynet.call(p, "lua", "query", name) else check(mainfunc) local code = string.dump(mainfunc) address = skynet.call(p, "lua", "launch", name, code, ...) end end cache[name] = address return address end function service.close(name) local addr = skynet.call(get_provider(), "lua", "close", name) if addr then cache[name] = nil skynet.kill(addr) return true end return false end function service.query(name) if not cache[name] then cache[name] = skynet.call(get_provider(), "lua", "query", name) end return cache[name] end return service ================================================ FILE: lualib/skynet/sharedata/corelib.lua ================================================ local core = require "skynet.sharedata.core" local type = type local rawset = rawset local conf = {} conf.host = { new = core.new, delete = core.delete, getref = core.getref, markdirty = core.markdirty, incref = core.incref, decref = core.decref, } local meta = {} local isdirty = core.isdirty local index = core.index local needupdate = core.needupdate local len = core.len local core_nextkey = core.nextkey local function findroot(self) while self.__parent do self = self.__parent end return self end local function update(root, cobj, gcobj) root.__obj = cobj root.__gcobj = gcobj local children = root.__cache if children then for k,v in pairs(children) do local pointer = index(cobj, k) if type(pointer) == "userdata" then update(v, pointer, gcobj) else children[k] = nil end end end end local function genkey(self) local key = tostring(self.__key) while self.__parent do self = self.__parent key = self.__key .. "." .. key end return key end local function getcobj(self) local obj = self.__obj if isdirty(obj) then local newobj, newtbl = needupdate(self.__gcobj) if newobj then local newgcobj = newtbl.__gcobj local root = findroot(self) update(root, newobj, newgcobj) if obj == self.__obj then error ("The key [" .. genkey(self) .. "] doesn't exist after update") end obj = self.__obj end end return obj end function meta:__newindex(key, value) error ("Error newindex, the key [" .. genkey(self) .. "]") end function meta:__index(key) local obj = getcobj(self) local v = index(obj, key) if type(v) == "userdata" then local children = self.__cache if children == nil then children = {} rawset(self, "__cache", children) end local r = children[key] if r then return r end r = setmetatable({ __obj = v, __gcobj = self.__gcobj, __parent = self, __key = key, }, meta) children[key] = r return r else return v end end function meta:__len() return len(getcobj(self)) end function meta:__pairs() return conf.next, self, nil end function conf.next(obj, key) local cobj = getcobj(obj) local nextkey = core_nextkey(cobj, key) if nextkey then return nextkey, obj[nextkey] end end function conf.box(obj) local gcobj = core.box(obj) return setmetatable({ __parent = false, __obj = obj, __gcobj = gcobj, __key = "", } , meta) end function conf.update(self, pointer) local cobj = self.__obj assert(isdirty(cobj), "Only dirty object can be update") core.update(self.__gcobj, pointer, { __gcobj = core.box(pointer) }) end function conf.flush(obj) getcobj(obj) end local function clone_table(cobj) local obj = {} local key while true do key = core_nextkey(cobj, key) if key == nil then break end local v = index(cobj, key) if type(v) == "userdata" then v = clone_table(v) end obj[key] = v end return obj end local function find_node(cobj, key, ...) if key == nil then return cobj end local cobj = index(cobj, key) if cobj == nil then return nil end if type(cobj) == "userdata" then return find_node(cobj, ...) end return cobj end function conf.copy(cobj, ...) cobj = find_node(cobj, ...) if cobj then if type(cobj) == "userdata" then return clone_table(cobj) else return cobj end end end return conf ================================================ FILE: lualib/skynet/sharedata.lua ================================================ local skynet = require "skynet" local sd = require "skynet.sharedata.corelib" local service skynet.init(function() service = skynet.uniqueservice "sharedatad" end) local sharedata = {} local cache = setmetatable({}, { __mode = "kv" }) local function monitor(name, obj, cobj) local newobj = cobj while true do newobj = skynet.call(service, "lua", "monitor", name, newobj) if newobj == nil then break end sd.update(obj, newobj) skynet.send(service, "lua", "confirm" , newobj) end if cache[name] == obj then cache[name] = nil end end function sharedata.query(name) if cache[name] then return cache[name] end local obj = skynet.call(service, "lua", "query", name) if cache[name] and cache[name].__obj == obj then skynet.send(service, "lua", "confirm" , obj) return cache[name] end local r = sd.box(obj) skynet.send(service, "lua", "confirm" , obj) skynet.fork(monitor,name, r, obj) cache[name] = r return r end function sharedata.new(name, v, ...) skynet.call(service, "lua", "new", name, v, ...) end function sharedata.update(name, v, ...) skynet.call(service, "lua", "update", name, v, ...) end function sharedata.delete(name) skynet.call(service, "lua", "delete", name) end function sharedata.flush() for name, obj in pairs(cache) do sd.flush(obj) end collectgarbage() end function sharedata.deepcopy(name, ...) if cache[name] then local cobj = cache[name].__obj return sd.copy(cobj, ...) end local cobj = skynet.call(service, "lua", "query", name) local ret = sd.copy(cobj, ...) skynet.send(service, "lua", "confirm" , cobj) return ret end return sharedata ================================================ FILE: lualib/skynet/sharemap.lua ================================================ local stm = require "skynet.stm" local sprotoloader = require "sprotoloader" local sproto = require "sproto" local setmetatable = setmetatable local sharemap = {} function sharemap.register(protofile) -- use global slot 0 for type define sprotoloader.register(protofile, 0) end local sprotoobj local function loadsp() if sprotoobj == nil then sprotoobj = sprotoloader.load(0) end return sprotoobj end function sharemap:commit() self.__obj(sprotoobj:encode(self.__typename, self.__data)) end function sharemap:copy() return stm.copy(self.__obj) end function sharemap.writer(typename, obj) local sp = loadsp() obj = obj or {} local stmobj = stm.new(sp:encode(typename,obj)) local ret = { __typename = typename, __obj = stmobj, __data = obj, commit = sharemap.commit, copy = sharemap.copy, } return setmetatable(ret, { __index = obj, __newindex = obj }) end local function decode(msg, sz, self) local data = self.__data for k in pairs(data) do data[k] = nil end return sprotoobj:decode(self.__typename, msg, sz, data) end function sharemap:update() return self.__obj(decode, self) end function sharemap.reader(typename, stmcpy) local sp = loadsp() local stmobj = stm.newcopy(stmcpy) local _, data = stmobj(function(msg, sz) return sp:decode(typename, msg, sz) end) local obj = { __typename = typename, __obj = stmobj, __data = data, update = sharemap.update, } return setmetatable(obj, { __index = data, __newindex = error }) end return sharemap ================================================ FILE: lualib/skynet/sharetable.lua ================================================ local skynet = require "skynet" local service = require "skynet.service" local core = require "skynet.sharetable.core" local is_sharedtable = core.is_sharedtable local stackvalues = core.stackvalues local function sharetable_service() local skynet = require "skynet" local core = require "skynet.sharetable.core" local matrix = {} -- all the matrix local files = {} -- filename : matrix local clients = {} local sharetable = {} local function close_matrix(m) if m == nil then return end local ptr = m:getptr() local ref = matrix[ptr] if ref == nil or ref.count == 0 then matrix[ptr] = nil m:close() end end function sharetable.loadfile(source, filename, ...) close_matrix(files[filename]) local m = core.matrix("@" .. filename, ...) files[filename] = m skynet.ret() end function sharetable.loadstring(source, filename, datasource, ...) close_matrix(files[filename]) local m = core.matrix(datasource, ...) files[filename] = m skynet.ret() end local function loadtable(filename, ptr, len) close_matrix(files[filename]) local m = core.matrix([[ local unpack, ptr, len = ... return unpack(ptr, len) ]], skynet.unpack, ptr, len) files[filename] = m end function sharetable.loadtable(source, filename, ptr, len) local ok, err = pcall(loadtable, filename, ptr, len) skynet.trash(ptr, len) assert(ok, err) skynet.ret() end local function query_file(source, filename) local m = files[filename] local ptr = m:getptr() local ref = matrix[ptr] if ref == nil then ref = { filename = filename, count = 0, matrix = m, refs = {}, } matrix[ptr] = ref end if ref.refs[source] == nil then ref.refs[source] = true local list = clients[source] if not list then clients[source] = { ptr } else table.insert(list, ptr) end ref.count = ref.count + 1 end return ptr end function sharetable.query(source, filename) local m = files[filename] if m == nil then skynet.ret() return end local ptr = query_file(source, filename) skynet.ret(skynet.pack(ptr)) end local function querylist(source, filenamelist) local ptrList = {} for _, filename in ipairs(filenamelist) do if files[filename] then ptrList[filename] = query_file(source, filename) end end return ptrList end local function queryall(source) local ptrList = {} for filename in pairs(files) do ptrList[filename] = query_file(source, filename) end return ptrList end function sharetable.queryall(source, filenamelist) local queryFunc = filenamelist and querylist or queryall local ptrList = queryFunc(source, filenamelist) skynet.ret(skynet.pack(ptrList)) end function sharetable.close(source) local list = clients[source] if list then for _, ptr in ipairs(list) do local ref = matrix[ptr] if ref and ref.refs[source] then ref.refs[source] = nil ref.count = ref.count - 1 if ref.count == 0 then if files[ref.filename] ~= ref.matrix then -- It's a history version skynet.error(string.format("Delete a version (%s) of %s", ptr, ref.filename)) ref.matrix:close() matrix[ptr] = nil end end end end clients[source] = nil end -- no return end skynet.dispatch("lua", function(_,source,cmd,...) sharetable[cmd](source,...) end) skynet.info_func(function() local info = {} for filename, m in pairs(files) do info[filename] = { current = m:getptr(), size = m:size(), } end local function address(refs) local keys = {} for addr in pairs(refs) do table.insert(keys, skynet.address(addr)) end table.sort(keys) return table.concat(keys, ",") end for ptr, copy in pairs(matrix) do local v = info[copy.filename] local h = v.history if h == nil then h = {} v.history = h end table.insert(h, string.format("%s [%d]: (%s)", copy.matrix:getptr(), copy.matrix:size(), address(copy.refs))) end for _, v in pairs(info) do if v.history then v.history = table.concat(v.history, "\n\t") end end return info end) end local function load_service(t, key) if key == "address" then t.address = service.new("sharetable", sharetable_service) return t.address else return nil end end local function report_close(t) local addr = rawget(t, "address") if addr then skynet.send(addr, "lua", "close") end end local sharetable = setmetatable ( {} , { __index = load_service, __gc = report_close, }) function sharetable.loadfile(filename, ...) skynet.call(sharetable.address, "lua", "loadfile", filename, ...) end function sharetable.loadstring(filename, source, ...) skynet.call(sharetable.address, "lua", "loadstring", filename, source, ...) end function sharetable.loadtable(filename, tbl) assert(type(tbl) == "table") skynet.call(sharetable.address, "lua", "loadtable", filename, skynet.pack(tbl)) end local RECORD = {} function sharetable.query(filename) local newptr = skynet.call(sharetable.address, "lua", "query", filename) if newptr then local t = core.clone(newptr) local map = RECORD[filename] if not map then map = {} RECORD[filename] = map end map[t] = true return t end end function sharetable.queryall(filenamelist) local list, t, map = {} local ptrList = skynet.call(sharetable.address, "lua", "queryall", filenamelist) for filename, ptr in pairs(ptrList) do t = core.clone(ptr) map = RECORD[filename] if not map then map = {} RECORD[filename] = map end map[t] = true list[filename] = t end return list end local pairs = pairs local type = type local assert = assert local next = next local rawset = rawset local getuservalue = debug.getuservalue local setuservalue = debug.setuservalue local getupvalue = debug.getupvalue local setupvalue = debug.setupvalue local getlocal = debug.getlocal local setlocal = debug.setlocal local getinfo = debug.getinfo local NILOBJ = {} local function insert_replace(old_t, new_t, replace_map) for k, ov in pairs(old_t) do if type(ov) == "table" then local nv = new_t[k] if nv == nil then nv = NILOBJ end assert(replace_map[ov] == nil) replace_map[ov] = nv nv = type(nv) == "table" and nv or NILOBJ insert_replace(ov, nv, replace_map) end end replace_map[old_t] = new_t return replace_map end local function resolve_replace(replace_map) local match = {} local record_map = {} local function getnv(v) local nv = replace_map[v] if nv then if nv == NILOBJ then return nil end return nv end assert(false) end local function match_value(v) if v == nil or record_map[v] or is_sharedtable(v) then return end local tv = type(v) local f = match[tv] if f then record_map[v] = true return f(v) end end local function match_mt(v) local mt = debug.getmetatable(v) if mt then local nv = replace_map[mt] if nv then nv = getnv(mt) debug.setmetatable(v, nv) else return match_value(mt) end end end local function match_internmt() local internal_types = { pointer = debug.upvalueid(getnv, 1), boolean = false, str = "", number = 42, thread = coroutine.running(), func = getnv, } for _,v in pairs(internal_types) do match_mt(v) end return match_mt(nil) end local function match_table(t) local keys = false for k,v in next, t do local tk = type(k) if match[tk] then keys = keys or {} keys[#keys+1] = k end local nv = replace_map[v] if nv then nv = getnv(v) rawset(t, k, nv) else match_value(v) end end if keys then for _, old_k in ipairs(keys) do local new_k = replace_map[old_k] if new_k then local value = rawget(t, old_k) new_k = getnv(old_k) rawset(t, old_k, nil) if new_k then rawset(t, new_k, value) end else match_value(old_k) end end end return match_mt(t) end local function match_userdata(u) local uv = getuservalue(u) local nv = replace_map[uv] if nv then nv = getnv(uv) setuservalue(u, nv) end return match_mt(u) end local function match_funcinfo(info) local func = info.func local nups = info.nups for i=1,nups do local name, upv = getupvalue(func, i) local nv = replace_map[upv] if nv then nv = getnv(upv) setupvalue(func, i, nv) elseif upv then match_value(upv) end end local level = info.level local curco = info.curco if not level then return end local i = 1 while true do local name, v = getlocal(curco, level, i) if name == nil then break end if replace_map[v] then local nv = getnv(v) setlocal(curco, level, i, nv) elseif v then match_value(v) end i = i + 1 end end local function match_function(f) local info = getinfo(f, "uf") return match_funcinfo(info) end local function match_thread(co, level) -- match stackvalues local values = {} local n = stackvalues(co, values) for i=1,n do local v = values[i] match_value(v) end local uplevel = co == coroutine.running() and 1 or 0 level = level or 1 while true do local info = getinfo(co, level, "uf") if not info then break end info.level = level + uplevel info.curco = co match_funcinfo(info) level = level + 1 end end local function prepare_match() local co = coroutine.running() record_map[co] = true record_map[match] = true record_map[RECORD] = true record_map[record_map] = true record_map[replace_map] = true record_map[insert_replace] = true record_map[resolve_replace] = true assert(getinfo(co, 3, "f").func == sharetable.update) match_thread(co, 5) -- ignore match_thread and match_funcinfo frame end match["table"] = match_table match["function"] = match_function match["userdata"] = match_userdata match["thread"] = match_thread prepare_match() match_internmt() local root = debug.getregistry() assert(replace_map[root] == nil) match_table(root) end function sharetable.update(...) local names = {...} local replace_map = {} for _, name in ipairs(names) do local map = RECORD[name] if map then local new_t = sharetable.query(name) for old_t,_ in pairs(map) do if old_t ~= new_t then insert_replace(old_t, new_t, replace_map) map[old_t] = nil end end end end if next(replace_map) then resolve_replace(replace_map) end end return sharetable ================================================ FILE: lualib/skynet/snax.lua ================================================ local skynet = require "skynet" local snax_interface = require "snax.interface" local snax = {} local typeclass = {} local interface_g = skynet.getenv("snax_interface_g") local G = interface_g and require (interface_g) or { require = function() end } interface_g = nil skynet.register_protocol { name = "snax", id = skynet.PTYPE_SNAX, pack = skynet.pack, unpack = skynet.unpack, } function snax.interface(name) if typeclass[name] then return typeclass[name] end local si = snax_interface(name, G) local ret = { name = name, accept = {}, response = {}, system = {}, } for _,v in ipairs(si) do local id, group, name, f = table.unpack(v) ret[group][name] = id end typeclass[name] = ret return ret end local meta = { __tostring = function(v) return string.format("[%s:%x]", v.type, v.handle) end} local skynet_send = skynet.send local skynet_call = skynet.call local function gen_post(type, handle) return setmetatable({} , { __index = function( t, k ) local id = type.accept[k] if not id then error(string.format("post %s:%s no exist", type.name, k)) end return function(...) skynet_send(handle, "snax", id, ...) end end }) end local function gen_req(type, handle) return setmetatable({} , { __index = function( t, k ) local id = type.response[k] if not id then error(string.format("request %s:%s no exist", type.name, k)) end return function(...) return skynet_call(handle, "snax", id, ...) end end }) end local function wrapper(handle, name, type) return setmetatable ({ post = gen_post(type, handle), req = gen_req(type, handle), type = name, handle = handle, }, meta) end local handle_cache = setmetatable( {} , { __mode = "kv" } ) function snax.rawnewservice(name, ...) local t = snax.interface(name) local handle = skynet.newservice("snaxd", name) assert(handle_cache[handle] == nil) if t.system.init then skynet.call(handle, "snax", t.system.init, ...) end return handle end function snax.bind(handle, type) local ret = handle_cache[handle] if ret then assert(ret.type == type) return ret end local t = snax.interface(type) ret = wrapper(handle, type, t) handle_cache[handle] = ret return ret end function snax.newservice(name, ...) local handle = snax.rawnewservice(name, ...) return snax.bind(handle, name) end function snax.uniqueservice(name, ...) local handle = assert(skynet.call(".service", "lua", "LAUNCH", "snaxd", name, ...)) return snax.bind(handle, name) end function snax.globalservice(name, ...) local handle = assert(skynet.call(".service", "lua", "GLAUNCH", "snaxd", name, ...)) return snax.bind(handle, name) end function snax.queryservice(name) local handle = assert(skynet.call(".service", "lua", "QUERY", "snaxd", name)) return snax.bind(handle, name) end function snax.queryglobal(name) local handle = assert(skynet.call(".service", "lua", "GQUERY", "snaxd", name)) return snax.bind(handle, name) end function snax.kill(obj, ...) local t = snax.interface(obj.type) skynet_call(obj.handle, "snax", t.system.exit, ...) end function snax.self() return snax.bind(skynet.self(), SERVICE_NAME) end function snax.exit(...) snax.kill(snax.self(), ...) end local function test_result(ok, ...) if ok then return ... else error(...) end end function snax.hotfix(obj, source, ...) local t = snax.interface(obj.type) return test_result(skynet_call(obj.handle, "snax", t.system.hotfix, source, ...)) end function snax.printf(fmt, ...) skynet.error(string.format(fmt, ...)) end function snax.profile_info(obj) local t = snax.interface(obj.type) return skynet_call(obj.handle, "snax", t.system.profile) end return snax ================================================ FILE: lualib/skynet/socket.lua ================================================ local driver = require "skynet.socketdriver" local skynet = require "skynet" local skynet_core = require "skynet.core" local assert = assert local BUFFER_LIMIT = 128 * 1024 local socket = {} -- api local socket_pool = setmetatable( -- store all socket object {}, { __gc = function(p) for id,v in pairs(p) do driver.close(id) p[id] = nil end end } ) local socket_onclose = {} local socket_message = {} local function wakeup(s) local co = s.co if co then s.co = nil skynet.wakeup(co) end end local function pause_socket(s, size) if s.pause ~= nil then return end if size then skynet.error(string.format("Pause socket (%d) size : %d" , s.id, size)) else skynet.error(string.format("Pause socket (%d)" , s.id)) end driver.pause(s.id) s.pause = true skynet.yield() -- there are subsequent socket messages in mqueue, maybe. end local function suspend(s) assert(not s.co) s.co = coroutine.running() if s.pause then skynet.error(string.format("Resume socket (%d)", s.id)) driver.start(s.id) skynet.wait(s.co) s.pause = nil else skynet.wait(s.co) end -- wakeup closing corouting every time suspend, -- because socket.close() will wait last socket buffer operation before clear the buffer. if s.closing then skynet.wakeup(s.closing) end end -- read skynet_socket.h for these macro -- SKYNET_SOCKET_TYPE_DATA = 1 socket_message[1] = function(id, size, data) local s = socket_pool[id] if s == nil then skynet.error("socket: drop package from " .. id) driver.drop(data, size) return end local sz = driver.push(s.buffer, s.pool, data, size) local rr = s.read_required local rrt = type(rr) if rrt == "number" then -- read size if sz >= rr then s.read_required = nil if sz > BUFFER_LIMIT then pause_socket(s, sz) end wakeup(s) end else if s.buffer_limit and sz > s.buffer_limit then skynet.error(string.format("socket buffer overflow: fd=%d size=%d", id , sz)) driver.close(id) return end if rrt == "string" then -- read line if driver.readline(s.buffer,nil,rr) then s.read_required = nil if sz > BUFFER_LIMIT then pause_socket(s, sz) end wakeup(s) end elseif sz > BUFFER_LIMIT and not s.pause then pause_socket(s, sz) end end end -- SKYNET_SOCKET_TYPE_CONNECT = 2 socket_message[2] = function(id, ud , addr) local s = socket_pool[id] if s == nil then return end -- log remote addr if not s.connected then -- resume may also post connect message if s.listen then s.addr = addr s.port = ud end s.connected = true wakeup(s) end end -- SKYNET_SOCKET_TYPE_CLOSE = 3 socket_message[3] = function(id) local s = socket_pool[id] if s then s.connected = false wakeup(s) else driver.close(id) end local cb = socket_onclose[id] if cb then cb(id) socket_onclose[id] = nil end end -- SKYNET_SOCKET_TYPE_ACCEPT = 4 socket_message[4] = function(id, newid, addr) local s = socket_pool[id] if s == nil then driver.close(newid) return end s.callback(newid, addr) end -- SKYNET_SOCKET_TYPE_ERROR = 5 socket_message[5] = function(id, _, err) local s = socket_pool[id] if s == nil then driver.shutdown(id) skynet.error("socket: error on unknown", id, err) return end if s.callback then skynet.error("socket: accept error:", err) return end if s.connected then skynet.error("socket: error on", id, err) elseif s.connecting then s.connecting = err end s.connected = false driver.shutdown(id) wakeup(s) end -- SKYNET_SOCKET_TYPE_UDP = 6 socket_message[6] = function(id, size, data, address) local s = socket_pool[id] if s == nil or s.callback == nil then skynet.error("socket: drop udp package from " .. id) driver.drop(data, size) return end local str = skynet.tostring(data, size) skynet_core.trash(data, size) s.callback(str, address) end local function default_warning(id, size) local s = socket_pool[id] if not s then return end skynet.error(string.format("WARNING: %d K bytes need to send out (fd = %d)", size, id)) end -- SKYNET_SOCKET_TYPE_WARNING socket_message[7] = function(id, size) local s = socket_pool[id] if s then local warning = s.on_warning or default_warning warning(id, size) end end skynet.register_protocol { name = "socket", id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 unpack = driver.unpack, dispatch = function (_, _, t, ...) socket_message[t](...) end } local function connect(id, func) local newbuffer if func == nil then newbuffer = driver.buffer() end local s = { id = id, buffer = newbuffer, pool = newbuffer and {}, connected = false, connecting = true, read_required = false, co = false, callback = func, protocol = "TCP", } assert(not socket_onclose[id], "socket has onclose callback") local s2 = socket_pool[id] if s2 and not s2.listen then error("socket is not closed") end socket_pool[id] = s suspend(s) local err = s.connecting s.connecting = nil if s.connected then return id else socket_pool[id] = nil return nil, err end end function socket.open(addr, port) local id = driver.connect(addr,port) return connect(id) end function socket.bind(os_fd) local id = driver.bind(os_fd) return connect(id) end function socket.stdin() return socket.bind(0) end function socket.start(id, func) driver.start(id) return connect(id, func) end function socket.pause(id) local s = socket_pool[id] if s == nil then return end pause_socket(s) end function socket.shutdown(id) local s = socket_pool[id] if s then -- the framework would send SKYNET_SOCKET_TYPE_CLOSE , need close(id) later driver.shutdown(id) end end function socket.close_fd(id) assert(socket_pool[id] == nil,"Use socket.close instead") driver.close(id) end function socket.close(id) local s = socket_pool[id] if s == nil then return end driver.close(id) if s.connected then s.pause = false -- Do not resume this fd if it paused. if s.co then -- reading this socket on another coroutine, so don't shutdown (clear the buffer) immediately -- wait reading coroutine read the buffer. assert(not s.closing) s.closing = coroutine.running() skynet.wait(s.closing) else suspend(s) end s.connected = false end socket_pool[id] = nil end function socket.read(id, sz) local s = socket_pool[id] assert(s) if sz == nil then -- read some bytes local ret = driver.readall(s.buffer, s.pool) if ret ~= "" then return ret end if not s.connected then return false, ret end assert(not s.read_required) s.read_required = 0 suspend(s) ret = driver.readall(s.buffer, s.pool) if ret ~= "" then return ret else return false, ret end end local ret = driver.pop(s.buffer, s.pool, sz) if ret then return ret end if s.closing or not s.connected then return false, driver.readall(s.buffer, s.pool) end assert(not s.read_required) s.read_required = sz suspend(s) ret = driver.pop(s.buffer, s.pool, sz) if ret then return ret else return false, driver.readall(s.buffer, s.pool) end end function socket.readall(id) local s = socket_pool[id] assert(s) if not s.connected then local r = driver.readall(s.buffer, s.pool) return r ~= "" and r end assert(not s.read_required) s.read_required = true suspend(s) assert(s.connected == false) return driver.readall(s.buffer, s.pool) end function socket.readline(id, sep) sep = sep or "\n" local s = socket_pool[id] assert(s) local ret = driver.readline(s.buffer, s.pool, sep) if ret then return ret end if not s.connected then return false, driver.readall(s.buffer, s.pool) end assert(not s.read_required) s.read_required = sep suspend(s) if s.connected then return driver.readline(s.buffer, s.pool, sep) else return false, driver.readall(s.buffer, s.pool) end end function socket.block(id) local s = socket_pool[id] if not s or not s.connected then return false end assert(not s.read_required) s.read_required = 0 suspend(s) return s.connected end socket.write = assert(driver.send) socket.lwrite = assert(driver.lsend) socket.header = assert(driver.header) function socket.invalid(id) return socket_pool[id] == nil end function socket.disconnected(id) local s = socket_pool[id] if s then return not(s.connected or s.connecting) end end function socket.listen(host, port, backlog) local id = driver.listen(host, port, backlog) local s = { id = id, connected = false, listen = true, } assert(socket_pool[id] == nil) socket_pool[id] = s suspend(s) return id, s.addr, s.port end -- abandon use to forward socket id to other service -- you must call socket.start(id) later in other service function socket.abandon(id) local s = socket_pool[id] if s then s.connected = false wakeup(s) socket_onclose[id] = nil socket_pool[id] = nil end end function socket.limit(id, limit) local s = assert(socket_pool[id]) s.buffer_limit = limit end ---------------------- UDP local function create_udp_object(id, cb) assert(not socket_pool[id], "socket is not closed") socket_pool[id] = { id = id, connected = true, protocol = "UDP", callback = cb, } end function socket.udp(callback, host, port) local id = driver.udp(host, port) create_udp_object(id, callback) return id end function socket.udp_connect(id, addr, port, callback) local obj = socket_pool[id] if obj then assert(obj.protocol == "UDP") if callback then obj.callback = callback end else create_udp_object(id, callback) end driver.udp_connect(id, addr, port) end function socket.udp_listen(addr, port, callback) local id = driver.udp_listen(addr, port) create_udp_object(id, callback) return id end function socket.udp_dial(addr, port, callback) local id = driver.udp_dial(addr, port) create_udp_object(id, callback) return id end socket.sendto = assert(driver.udp_send) socket.udp_address = assert(driver.udp_address) socket.netstat = assert(driver.info) socket.resolve = assert(driver.resolve) function socket.warning(id, callback) local obj = socket_pool[id] assert(obj) obj.on_warning = callback end function socket.onclose(id, callback) socket_onclose[id] = callback end return socket ================================================ FILE: lualib/skynet/socketchannel.lua ================================================ local skynet = require "skynet" local socket = require "skynet.socket" local socketdriver = require "skynet.socketdriver" -- channel support auto reconnect , and capture socket error in request/response transaction -- { host = "", port = , auth = function(so) , response = function(so) session, data } local socket_channel = {} local channel = {} local channel_socket = {} local channel_meta = { __index = channel } local channel_socket_meta = { __index = channel_socket, __gc = function(cs) local fd = cs[1] cs[1] = false if fd then socket.shutdown(fd) end end } local socket_error = setmetatable({}, {__tostring = function() return "[Error: socket]" end }) -- alias for error object socket_channel.error = socket_error function socket_channel.channel(desc) local c = { __host = assert(desc.host), __port = desc.port, __backup = desc.backup, __auth = desc.auth, __response = desc.response, -- It's for session mode __request = {}, -- request seq { response func or session } -- It's for order mode __thread = {}, -- coroutine seq or session->coroutine map __result = {}, -- response result { coroutine -> result } __result_data = {}, __connecting = {}, __sock = false, __closed = false, __authcoroutine = false, __nodelay = desc.nodelay, __overload_notify = desc.overload, __overload = false, __socket_meta = channel_socket_meta, } if desc.socket_read or desc.socket_readline then c.__socket_meta = { __index = { read = desc.socket_read or channel_socket.read, readline = desc.socket_readline or channel_socket.readline, }, __gc = channel_socket_meta.__gc } end return setmetatable(c, channel_meta) end local function close_channel_socket(self) if self.__sock then local so = self.__sock self.__sock = false if self.__wait_response then skynet.wakeup(self.__wait_response) self.__wait_response = nil end -- never raise error pcall(socket.close,so[1]) end end local function wakeup_all(self, errmsg) if self.__response then for k,co in pairs(self.__thread) do self.__thread[k] = nil self.__result[co] = socket_error self.__result_data[co] = errmsg skynet.wakeup(co) end else for i = 1, #self.__request do self.__request[i] = nil end for i = 1, #self.__thread do local co = self.__thread[i] self.__thread[i] = nil if co then -- ignore the close signal self.__result[co] = socket_error self.__result_data[co] = errmsg skynet.wakeup(co) end end end end local function dispatch_by_session(self) local response = self.__response -- response() return session while self.__sock do local ok , session, result_ok, result_data, padding = pcall(response, self.__sock) if ok and session then local co = self.__thread[session] if co then if padding and result_ok then -- If padding is true, append result_data to a table (self.__result_data[co]) local result = self.__result_data[co] or {} self.__result_data[co] = result table.insert(result, result_data) else self.__thread[session] = nil self.__result[co] = result_ok if result_ok and self.__result_data[co] then table.insert(self.__result_data[co], result_data) else self.__result_data[co] = result_data end skynet.wakeup(co) end if not self.__sock then -- closed wakeup_all(self, "channel_closed") break end else self.__thread[session] = nil skynet.error("socket: unknown session :", session) end else close_channel_socket(self) local errormsg if session ~= socket_error then errormsg = session end wakeup_all(self, errormsg) end end end local function pop_response(self) while self.__sock do local func,co = table.remove(self.__request, 1), table.remove(self.__thread, 1) if func then return func, co end self.__wait_response = coroutine.running() skynet.wait(self.__wait_response) end end -- on close callback local function autoclose_cb(self, fd) local sock = self.__sock if self.__wait_response and sock and sock[1] == fd then -- closed by peer skynet.error("socket closed by peer : ", self.__host, self.__port) close_channel_socket(self) end end local function push_response(self, response, co) if self.__response then -- response is session self.__thread[response] = co else -- response is a function, push it to __request table.insert(self.__request, response) table.insert(self.__thread, co) if self.__wait_response then skynet.wakeup(self.__wait_response) self.__wait_response = nil end end end local function get_response(func, sock) local result_ok, result_data, padding = func(sock) if result_ok and padding then local result = { result_data } local index = 2 repeat result_ok, result_data, padding = func(sock) if not result_ok then return result_ok, result_data end result[index] = result_data index = index + 1 until not padding return true, result else return result_ok, result_data end end local function dispatch_by_order(self) while self.__sock do local func, co = pop_response(self) if not co then -- close signal wakeup_all(self, "channel_closed") break end local sock = self.__sock if not sock then -- closed by peer self.__result[co] = socket_error skynet.wakeup(co) wakeup_all(self) break end local ok, result_ok, result_data = pcall(get_response, func, sock) if ok then self.__result[co] = result_ok if result_ok and self.__result_data[co] then table.insert(self.__result_data[co], result_data) else self.__result_data[co] = result_data end skynet.wakeup(co) if not self.__sock then -- closed wakeup_all(self, "channel_closed") break end else close_channel_socket(self) local errmsg if result_ok ~= socket_error then errmsg = result_ok end self.__result[co] = socket_error self.__result_data[co] = errmsg skynet.wakeup(co) wakeup_all(self, errmsg) end end end local function dispatch_function(self) if self.__response then return dispatch_by_session else socket.onclose(self.__sock[1], function(fd) autoclose_cb(self, fd) end) return dispatch_by_order end end local function term_dispatch_thread(self) if not self.__response and self.__dispatch_thread then -- dispatch by order, send close signal to dispatch thread push_response(self, true, false) -- (true, false) is close signal end end local function connect_once(self) if self.__closed then return false end local addr_list = {} local addr_set = {} local function _add_backup() if self.__backup then for _, addr in ipairs(self.__backup) do local host, port if type(addr) == "table" then host,port = addr.host, addr.port else host = addr port = self.__port end -- don't add the same host local hostkey = host..":"..port if not addr_set[hostkey] then addr_set[hostkey] = true table.insert(addr_list, { host = host, port = port }) end end end end local function _next_addr() local addr = table.remove(addr_list,1) if addr then skynet.error("socket: connect to backup host", addr.host, addr.port) end return addr end local function _connect_once(self, addr) local fd,err = socket.open(addr.host, addr.port) if not fd then -- try next one addr = _next_addr() if addr == nil then return false, err end return _connect_once(self, addr) end self.__host = addr.host self.__port = addr.port assert(not self.__sock and not self.__authcoroutine) -- term current dispatch thread (send a signal) term_dispatch_thread(self) if self.__nodelay then socketdriver.nodelay(fd) end -- register overload warning local overload = self.__overload_notify if overload then local function overload_trigger(id, size) if id == self.__sock[1] then if size == 0 then if self.__overload then self.__overload = false overload(false) end else if not self.__overload then self.__overload = true overload(true) else skynet.error(string.format("WARNING: %d K bytes need to send out (fd = %d)", size, id), self.__host, self.__port) end end end end skynet.fork(overload_trigger, fd, 0) socket.warning(fd, overload_trigger) end while self.__dispatch_thread do -- wait for dispatch thread exit skynet.yield() end self.__sock = setmetatable( {fd} , self.__socket_meta ) self.__dispatch_thread = skynet.fork(function() if self.__sock then -- self.__sock can be false (socket closed) if error during connecting, See #1513 pcall(dispatch_function(self), self) end -- clear dispatch_thread self.__dispatch_thread = nil end) if self.__auth then self.__authcoroutine = coroutine.running() local ok , message = pcall(self.__auth, self) if not ok then close_channel_socket(self) if message ~= socket_error then self.__authcoroutine = false skynet.error("socket: auth failed", message) end end self.__authcoroutine = false if ok then if not self.__sock then -- auth may change host, so connect again return connect_once(self) end -- auth succ, go through else -- auth failed, try next addr _add_backup() -- auth may add new backup hosts addr = _next_addr() if addr == nil then return false, "no more backup host" end return _connect_once(self, addr) end end return true end _add_backup() return _connect_once(self, { host = self.__host, port = self.__port }) end local function try_connect(self , once) local t = 0 while not self.__closed do local ok, err = connect_once(self) if ok then if not once then skynet.error("socket: connect to", self.__host, self.__port) end return elseif once then return err else skynet.error("socket: connect", err) end if t > 1000 then skynet.error("socket: try to reconnect", self.__host, self.__port) skynet.sleep(t) t = 0 else skynet.sleep(t) end t = t + 100 end end local function check_connection(self) if self.__sock then local authco = self.__authcoroutine if socket.disconnected(self.__sock[1]) then -- closed by peer skynet.error("socket: disconnect detected ", self.__host, self.__port) close_channel_socket(self) if authco and authco == coroutine.running() then -- disconnected during auth, See #1513 return false end return end if not authco then return true end if authco == coroutine.running() then -- authing return true end end if self.__closed then return false end end local function block_connect(self, once) local r = check_connection(self) if r ~= nil then return r end local err if #self.__connecting > 0 then -- connecting in other coroutine local co = coroutine.running() table.insert(self.__connecting, co) skynet.wait(co) else self.__connecting[1] = true err = try_connect(self, once) for i=2, #self.__connecting do local co = self.__connecting[i] self.__connecting[i] = nil skynet.wakeup(co) end self.__connecting[1] = nil end r = check_connection(self) if r == nil then skynet.error("Connect failed", err, self.__host, self.__port) error(socket_error) else return r end end function channel:connect(once) self.__closed = false return block_connect(self, once) end local function wait_for_response(self, response) local co = coroutine.running() push_response(self, response, co) skynet.wait(co) local result = self.__result[co] self.__result[co] = nil local result_data = self.__result_data[co] self.__result_data[co] = nil if result == socket_error then if result_data then error(result_data) else error(socket_error) end else assert(result, result_data) return result_data end end local socket_write = socket.write local socket_lwrite = socket.lwrite local function sock_err(self) close_channel_socket(self) wakeup_all(self) error(socket_error) end function channel:request(request, response, padding) assert(block_connect(self, true)) -- connect once local fd = self.__sock[1] if padding then -- padding may be a table, to support multi part request -- multi part request use low priority socket write -- now socket_lwrite returns as socket_write if not socket_lwrite(fd , request) then sock_err(self) end for _,v in ipairs(padding) do if not socket_lwrite(fd, v) then sock_err(self) end end else if not socket_write(fd , request) then sock_err(self) end end if response == nil then -- no response return end return wait_for_response(self, response) end function channel:response(response) assert(block_connect(self)) return wait_for_response(self, response) end function channel:close() if not self.__closed then term_dispatch_thread(self) self.__closed = true close_channel_socket(self) end end function channel:changehost(host, port) self.__host = host if port then self.__port = port end if not self.__closed then close_channel_socket(self) end end function channel:changebackup(backup) self.__backup = backup end channel_meta.__gc = channel.close local function wrapper_socket_function(f) return function(self, ...) local result = f(self[1], ...) if not result then error(socket_error) else return result end end end channel_socket.read = wrapper_socket_function(socket.read) channel_socket.readline = wrapper_socket_function(socket.readline) return socket_channel ================================================ FILE: lualib/skynet.lua ================================================ -- read https://github.com/cloudwu/skynet/wiki/FAQ for the module "skynet.core" local c = require "skynet.core" local skynet_require = require "skynet.require" local tostring = tostring local coroutine = coroutine local assert = assert local error = error local pairs = pairs local pcall = pcall local table = table local next = next local tremove = table.remove local tinsert = table.insert local tpack = table.pack local tunpack = table.unpack local traceback = debug.traceback local cresume = coroutine.resume local running_thread = nil local init_thread = nil local function coroutine_resume(co, ...) running_thread = co return cresume(co, ...) end local coroutine_yield = coroutine.yield local coroutine_create = coroutine.create local proto = {} local skynet = { -- read skynet.h PTYPE_TEXT = 0, PTYPE_RESPONSE = 1, PTYPE_MULTICAST = 2, PTYPE_CLIENT = 3, PTYPE_SYSTEM = 4, PTYPE_HARBOR = 5, PTYPE_SOCKET = 6, PTYPE_ERROR = 7, PTYPE_QUEUE = 8, -- used in deprecated mqueue, use skynet.queue instead PTYPE_DEBUG = 9, PTYPE_LUA = 10, PTYPE_SNAX = 11, PTYPE_TRACE = 12, -- use for debug trace } -- code cache skynet.cache = require "skynet.codecache" skynet._proto = proto function skynet.register_protocol(class) local name = class.name local id = class.id assert(proto[name] == nil and proto[id] == nil) assert(type(name) == "string" and type(id) == "number" and id >=0 and id <=255) proto[name] = class proto[id] = class end local session_id_coroutine = {} local session_coroutine_id = {} local session_coroutine_address = {} local session_coroutine_tracetag = {} local unresponse = {} local wakeup_queue = {} local sleep_session = {} local watching_session = {} local error_queue = {} local fork_queue = { h = 1, t = 0 } local auxsend, auxtimeout, auxwait do ---- avoid session rewind conflict local csend = c.send local cintcommand = c.intcommand local dangerzone local dangerzone_size = 0x1000 local dangerzone_low = 0x70000000 local dangerzone_up = dangerzone_low + dangerzone_size local set_checkrewind -- set auxsend and auxtimeout for safezone local set_checkconflict -- set auxsend and auxtimeout for dangerzone local function reset_dangerzone(session) dangerzone_up = session dangerzone_low = session dangerzone = { [session] = true } for s in pairs(session_id_coroutine) do if s < dangerzone_low then dangerzone_low = s elseif s > dangerzone_up then dangerzone_up = s end dangerzone[s] = true end dangerzone_low = dangerzone_low - dangerzone_size end -- in dangerzone, we should check if the next session already exist. local function checkconflict(session) if session == nil then return end local next_session = session + 1 if next_session > dangerzone_up then -- leave dangerzone reset_dangerzone(session) assert(next_session > dangerzone_up) set_checkrewind() else while true do if not dangerzone[next_session] then break end if not session_id_coroutine[next_session] then reset_dangerzone(session) break end -- skip the session already exist. next_session = c.genid() + 1 end end -- session will rewind after 0x7fffffff if next_session == 0x80000000 and dangerzone[1] then assert(c.genid() == 1) return checkconflict(1) end end local function auxsend_checkconflict(addr, proto, msg, sz) local session = csend(addr, proto, nil, msg, sz) checkconflict(session) return session end local function auxtimeout_checkconflict(timeout) local session = cintcommand("TIMEOUT", timeout) checkconflict(session) return session end local function auxwait_checkconflict() local session = c.genid() checkconflict(session) return session end local function auxsend_checkrewind(addr, proto, msg, sz) local session = csend(addr, proto, nil, msg, sz) if session and session > dangerzone_low and session <= dangerzone_up then -- enter dangerzone set_checkconflict(session) end return session end local function auxtimeout_checkrewind(timeout) local session = cintcommand("TIMEOUT", timeout) if session and session > dangerzone_low and session <= dangerzone_up then -- enter dangerzone set_checkconflict(session) end return session end local function auxwait_checkrewind() local session = c.genid() if session > dangerzone_low and session <= dangerzone_up then -- enter dangerzone set_checkconflict(session) end return session end set_checkrewind = function() auxsend = auxsend_checkrewind auxtimeout = auxtimeout_checkrewind auxwait = auxwait_checkrewind end set_checkconflict = function(session) reset_dangerzone(session) auxsend = auxsend_checkconflict auxtimeout = auxtimeout_checkconflict auxwait = auxwait_checkconflict end -- in safezone at the beginning set_checkrewind() end do ---- request/select local function send_requests(self) local sessions = {} self._sessions = sessions local request_n = 0 local err for i = 1, #self do local req = self[i] local addr = req[1] local p = proto[req[2]] assert(p.unpack) local tag = session_coroutine_tracetag[running_thread] if tag then c.trace(tag, "call", 4) c.send(addr, skynet.PTYPE_TRACE, 0, tag) end local session = auxsend(addr, p.id , p.pack(tunpack(req, 3, req.n))) if session == nil then err = err or {} err[#err+1] = req else sessions[session] = req watching_session[session] = addr session_id_coroutine[session] = self._thread request_n = request_n + 1 end end self._request = request_n return err end local function request_thread(self) while true do local succ, msg, sz, session = coroutine_yield "SUSPEND" if session == self._timeout then self._timeout = nil self.timeout = true else watching_session[session] = nil local req = self._sessions[session] local p = proto[req[2]] if succ then self._resp[session] = tpack( p.unpack(msg, sz) ) else self._resp[session] = false end end skynet.wakeup(self) end end local function request_iter(self) return function() if self._error then -- invalid address local e = tremove(self._error) if e then return e end self._error = nil end local session, resp = next(self._resp) if session == nil then if self._request == 0 then return end if self.timeout then return end skynet.wait(self) if self.timeout then return end session, resp = next(self._resp) end self._request = self._request - 1 local req = self._sessions[session] self._resp[session] = nil self._sessions[session] = nil return req, resp end end local request_meta = {} ; request_meta.__index = request_meta function request_meta:add(obj) assert(type(obj) == "table" and not self._thread) self[#self+1] = obj return self end request_meta.__call = request_meta.add function request_meta:close() if self._request > 0 then local resp = self._resp for session, req in pairs(self._sessions) do if not resp[session] then session_id_coroutine[session] = "BREAK" watching_session[session] = nil end end self._request = 0 end if self._timeout then session_id_coroutine[self._timeout] = "BREAK" self._timeout = nil end end request_meta.__close = request_meta.close function request_meta:select(timeout) assert(self._thread == nil) self._thread = coroutine_create(request_thread) self._error = send_requests(self) self._resp = {} if timeout then self._timeout = auxtimeout(timeout) session_id_coroutine[self._timeout] = self._thread end local running = running_thread coroutine_resume(self._thread, self) running_thread = running return request_iter(self), nil, nil, self end function skynet.request(obj) local ret = setmetatable({}, request_meta) if obj then return ret(obj) end return ret end end -- suspend is function local suspend ----- monitor exit local function dispatch_error_queue() local session = tremove(error_queue,1) if session then local co = session_id_coroutine[session] session_id_coroutine[session] = nil return suspend(co, coroutine_resume(co, false, nil, nil, session)) end end local function _error_dispatch(error_session, error_source) skynet.ignoreret() -- don't return for error if error_session == 0 then -- error_source is down, clear unreponse set for resp, address in pairs(unresponse) do if error_source == address then unresponse[resp] = nil end end for session, srv in pairs(watching_session) do if srv == error_source then tinsert(error_queue, session) end end else -- capture an error for error_session if watching_session[error_session] then tinsert(error_queue, error_session) end end end -- coroutine reuse local coroutine_pool = setmetatable({}, { __mode = "kv" }) local function co_create(f) local co = tremove(coroutine_pool) if co == nil then co = coroutine_create(function(...) f(...) while true do local session = session_coroutine_id[co] if session and session ~= 0 then local source = debug.getinfo(f,"S") skynet.error(string.format("Maybe forgot response session %s from %s : %s:%d", session, skynet.address(session_coroutine_address[co]), source.source, source.linedefined)) end -- coroutine exit local tag = session_coroutine_tracetag[co] if tag ~= nil then if tag then c.trace(tag, "end") end session_coroutine_tracetag[co] = nil end local address = session_coroutine_address[co] if address then session_coroutine_id[co] = nil session_coroutine_address[co] = nil end -- recycle co into pool f = nil coroutine_pool[#coroutine_pool+1] = co -- recv new main function f f = coroutine_yield "SUSPEND" f(coroutine_yield()) end end) else -- pass the main function f to coroutine, and restore running thread local running = running_thread coroutine_resume(co, f) running_thread = running end return co end local function dispatch_wakeup() while true do local token = tremove(wakeup_queue,1) if token then local session = sleep_session[token] if session then local co = session_id_coroutine[session] local tag = session_coroutine_tracetag[co] if tag then c.trace(tag, "resume") end session_id_coroutine[session] = "BREAK" return suspend(co, coroutine_resume(co, false, "BREAK", nil, session)) end else break end end return dispatch_error_queue() end -- suspend is local function function suspend(co, result, command) if not result then local session = session_coroutine_id[co] if session then -- coroutine may fork by others (session is nil) local addr = session_coroutine_address[co] if session ~= 0 then -- only call response error local tag = session_coroutine_tracetag[co] if tag then c.trace(tag, "error") end c.send(addr, skynet.PTYPE_ERROR, session, "") end session_coroutine_id[co] = nil end session_coroutine_address[co] = nil session_coroutine_tracetag[co] = nil skynet.fork(function() end) -- trigger command "SUSPEND" local tb = traceback(co,tostring(command)) coroutine.close(co) error(tb) end if command == "SUSPEND" then return dispatch_wakeup() elseif command == "QUIT" then coroutine.close(co) -- service exit return elseif command == "USER" then -- See skynet.coutine for detail error("Call skynet.coroutine.yield out of skynet.coroutine.resume\n" .. traceback(co)) elseif command == nil then -- debug trace return else error("Unknown command : " .. command .. "\n" .. traceback(co)) end end local co_create_for_timeout local timeout_traceback function skynet.trace_timeout(on) local function trace_coroutine(func, ti) local co co = co_create(function() timeout_traceback[co] = nil func() end) local info = string.format("TIMER %d+%d : ", skynet.now(), ti) timeout_traceback[co] = traceback(info, 3) return co end if on then timeout_traceback = timeout_traceback or {} co_create_for_timeout = trace_coroutine else timeout_traceback = nil co_create_for_timeout = co_create end end skynet.trace_timeout(false) -- turn off by default function skynet.timeout(ti, func) local session = auxtimeout(ti) assert(session) local co = co_create_for_timeout(func, ti) assert(session_id_coroutine[session] == nil) session_id_coroutine[session] = co return co -- for debug end local function suspend_sleep(session, token) local tag = session_coroutine_tracetag[running_thread] if tag then c.trace(tag, "sleep", 2) end session_id_coroutine[session] = running_thread assert(sleep_session[token] == nil, "token duplicative") sleep_session[token] = session return coroutine_yield "SUSPEND" end function skynet.sleep(ti, token) local session = auxtimeout(ti) assert(session) token = token or coroutine.running() local succ, ret = suspend_sleep(session, token) sleep_session[token] = nil if succ then return end if ret == "BREAK" then return "BREAK" else error(ret) end end function skynet.yield() return skynet.sleep(0) end function skynet.wait(token) local session = auxwait() token = token or coroutine.running() suspend_sleep(session, token) sleep_session[token] = nil session_id_coroutine[session] = nil end function skynet.killthread(thread) local session -- find session if type(thread) == "string" then for k,v in pairs(session_id_coroutine) do local thread_string = tostring(v) if thread_string:find(thread) then session = k break end end else local t = fork_queue.t for i = fork_queue.h, t do if fork_queue[i] == thread then table.move(fork_queue, i+1, t, i) fork_queue[t] = nil fork_queue.t = t - 1 return thread end end for k,v in pairs(session_id_coroutine) do if v == thread then session = k break end end end local co = session_id_coroutine[session] if co == nil then return end local addr = session_coroutine_address[co] if addr then session_coroutine_address[co] = nil session_coroutine_tracetag[co] = nil local session = session_coroutine_id[co] if session > 0 then c.send(addr, skynet.PTYPE_ERROR, session, "") end session_coroutine_id[co] = nil end if watching_session[session] then session_id_coroutine[session] = "BREAK" watching_session[session] = nil else session_id_coroutine[session] = nil end for k,v in pairs(sleep_session) do if v == session then sleep_session[k] = nil break end end coroutine.close(co) return co end function skynet.self() return c.addresscommand "REG" end function skynet.localname(name) return c.addresscommand("QUERY", name) end skynet.now = c.now skynet.hpc = c.hpc -- high performance counter local traceid = 0 function skynet.trace(info) skynet.error("TRACE", session_coroutine_tracetag[running_thread]) if session_coroutine_tracetag[running_thread] == false then -- force off trace log return end traceid = traceid + 1 local tag = string.format(":%08x-%d",skynet.self(), traceid) session_coroutine_tracetag[running_thread] = tag if info then c.trace(tag, "trace " .. info) else c.trace(tag, "trace") end end function skynet.tracetag() return session_coroutine_tracetag[running_thread] end local starttime function skynet.starttime() if not starttime then starttime = c.intcommand("STARTTIME") end return starttime end function skynet.time() return skynet.now()/100 + (starttime or skynet.starttime()) end function skynet.exit() fork_queue = { h = 1, t = 0 } -- no fork coroutine can be execute after skynet.exit skynet.send(".launcher","lua","REMOVE",skynet.self(), false) -- report the sources that call me for co, session in pairs(session_coroutine_id) do local address = session_coroutine_address[co] if session~=0 and address then c.send(address, skynet.PTYPE_ERROR, session, "") end end for session, co in pairs(session_id_coroutine) do if type(co) == "thread" and co ~= running_thread then coroutine.close(co) end end for resp in pairs(unresponse) do resp(false) end -- report the sources I call but haven't return local tmp = {} for session, address in pairs(watching_session) do tmp[address] = true end for address in pairs(tmp) do c.send(address, skynet.PTYPE_ERROR, 0, "") end c.callback(function(prototype, msg, sz, session, source) if session ~= 0 and source ~= 0 then c.send(source, skynet.PTYPE_ERROR, session, "") end end) c.command("EXIT") -- quit service coroutine_yield "QUIT" end function skynet.getenv(key) return (c.command("GETENV",key)) end function skynet.setenv(key, value) assert(c.command("GETENV",key) == nil, "Can't setenv exist key : " .. key) c.command("SETENV",key .. " " ..value) end function skynet.send(addr, typename, ...) local p = proto[typename] return c.send(addr, p.id, 0 , p.pack(...)) end function skynet.rawsend(addr, typename, msg, sz) local p = proto[typename] return c.send(addr, p.id, 0 , msg, sz) end skynet.genid = assert(c.genid) skynet.redirect = function(dest,source,typename,...) return c.redirect(dest, source, proto[typename].id, ...) end skynet.pack = assert(c.pack) skynet.packstring = assert(c.packstring) skynet.unpack = assert(c.unpack) skynet.tostring = assert(c.tostring) skynet.trash = assert(c.trash) local function yield_call(service, session) watching_session[session] = service session_id_coroutine[session] = running_thread local succ, msg, sz = coroutine_yield "SUSPEND" watching_session[session] = nil if not succ then error "call failed" end return msg,sz end function skynet.call(addr, typename, ...) local tag = session_coroutine_tracetag[running_thread] if tag then c.trace(tag, "call", 2) c.send(addr, skynet.PTYPE_TRACE, 0, tag) end local p = proto[typename] local session = auxsend(addr, p.id , p.pack(...)) if session == nil then error("call to invalid address " .. skynet.address(addr)) end return p.unpack(yield_call(addr, session)) end function skynet.rawcall(addr, typename, msg, sz) local tag = session_coroutine_tracetag[running_thread] if tag then c.trace(tag, "call", 2) c.send(addr, skynet.PTYPE_TRACE, 0, tag) end local p = proto[typename] local session = assert(auxsend(addr, p.id , msg, sz), "call to invalid address") return yield_call(addr, session) end function skynet.tracecall(tag, addr, typename, msg, sz) c.trace(tag, "tracecall begin") c.send(addr, skynet.PTYPE_TRACE, 0, tag) local p = proto[typename] local session = assert(auxsend(addr, p.id , msg, sz), "call to invalid address") local msg, sz = yield_call(addr, session) c.trace(tag, "tracecall end") return msg, sz end function skynet.ret(msg, sz) msg = msg or "" local tag = session_coroutine_tracetag[running_thread] if tag then c.trace(tag, "response") end local co_session = session_coroutine_id[running_thread] if co_session == nil then error "No session" end session_coroutine_id[running_thread] = nil if co_session == 0 then if sz ~= nil then c.trash(msg, sz) end return false -- send don't need ret end local co_address = session_coroutine_address[running_thread] local ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, msg, sz) if ret then return true elseif ret == false then -- If the package is too large, returns false. so we should report error back c.send(co_address, skynet.PTYPE_ERROR, co_session, "") end return false end function skynet.context() local co_session = session_coroutine_id[running_thread] local co_address = session_coroutine_address[running_thread] return co_session, co_address end function skynet.ignoreret() -- We use session for other uses session_coroutine_id[running_thread] = nil end function skynet.response(pack) pack = pack or skynet.pack local co_session = assert(session_coroutine_id[running_thread], "no session") session_coroutine_id[running_thread] = nil local co_address = session_coroutine_address[running_thread] if co_session == 0 then -- do not response when session == 0 (send) return function() end end local function response(ok, ...) if ok == "TEST" then return unresponse[response] ~= nil end if not pack then error "Can't response more than once" end local ret if unresponse[response] then if ok then ret = c.send(co_address, skynet.PTYPE_RESPONSE, co_session, pack(...)) if ret == false then -- If the package is too large, returns false. so we should report error back c.send(co_address, skynet.PTYPE_ERROR, co_session, "") end else ret = c.send(co_address, skynet.PTYPE_ERROR, co_session, "") end unresponse[response] = nil ret = ret ~= nil else ret = false end pack = nil return ret end unresponse[response] = co_address return response end function skynet.retpack(...) return skynet.ret(skynet.pack(...)) end function skynet.wakeup(token) if sleep_session[token] then tinsert(wakeup_queue, token) return true end end function skynet.dispatch(typename, func) local p = proto[typename] if func then local ret = p.dispatch p.dispatch = func return ret else return p and p.dispatch end end local function unknown_request(session, address, msg, sz, prototype) skynet.error(string.format("Unknown request (%s): %s", prototype, c.tostring(msg,sz))) error(string.format("Unknown session : %d from %x", session, address)) end function skynet.dispatch_unknown_request(unknown) local prev = unknown_request unknown_request = unknown return prev end local function unknown_response(session, address, msg, sz) skynet.error(string.format("Response message : %s" , c.tostring(msg,sz))) error(string.format("Unknown session : %d from %x", session, address)) end function skynet.dispatch_unknown_response(unknown) local prev = unknown_response unknown_response = unknown return prev end function skynet.fork(func,...) local n = select("#", ...) local co if n == 0 then co = co_create(func) else local args = { ... } co = co_create(function() func(table.unpack(args,1,n)) end) end local t = fork_queue.t + 1 fork_queue.t = t fork_queue[t] = co return co end local trace_source = {} local function raw_dispatch_message(prototype, msg, sz, session, source) -- skynet.PTYPE_RESPONSE = 1, read skynet.h if prototype == 1 then local co = session_id_coroutine[session] if co == "BREAK" then session_id_coroutine[session] = nil elseif co == nil then unknown_response(session, source, msg, sz) else local tag = session_coroutine_tracetag[co] if tag then c.trace(tag, "resume") end session_id_coroutine[session] = nil suspend(co, coroutine_resume(co, true, msg, sz, session)) end else local p = proto[prototype] if p == nil then if prototype == skynet.PTYPE_TRACE then -- trace next request trace_source[source] = c.tostring(msg,sz) elseif session ~= 0 then c.send(source, skynet.PTYPE_ERROR, session, "") else unknown_request(session, source, msg, sz, prototype) end return end local f = p.dispatch if f then local co = co_create(f) session_coroutine_id[co] = session session_coroutine_address[co] = source local traceflag = p.trace if traceflag == false then -- force off trace_source[source] = nil session_coroutine_tracetag[co] = false else local tag = trace_source[source] if tag then trace_source[source] = nil c.trace(tag, "request") session_coroutine_tracetag[co] = tag elseif traceflag then -- set running_thread for trace running_thread = co skynet.trace() end end suspend(co, coroutine_resume(co, session,source, p.unpack(msg,sz))) else trace_source[source] = nil if session ~= 0 then c.send(source, skynet.PTYPE_ERROR, session, "") else unknown_request(session, source, msg, sz, proto[prototype].name) end end end end function skynet.dispatch_message(...) local succ, err = pcall(raw_dispatch_message,...) while true do if fork_queue.h > fork_queue.t then -- queue is empty fork_queue.h = 1 fork_queue.t = 0 break end -- pop queue local h = fork_queue.h local co = fork_queue[h] fork_queue[h] = nil fork_queue.h = h + 1 local fork_succ, fork_err = pcall(suspend,co,coroutine_resume(co)) if not fork_succ then if succ then succ = false err = tostring(fork_err) else err = tostring(err) .. "\n" .. tostring(fork_err) end end end assert(succ, tostring(err)) end function skynet.newservice(name, ...) return skynet.call(".launcher", "lua" , "LAUNCH", "snlua", name, ...) end function skynet.uniqueservice(name, ...) if name == true then return assert(skynet.call(".service", "lua", "GLAUNCH", ...)) else return assert(skynet.call(".service", "lua", "LAUNCH", name, ...)) end end function skynet.queryservice(name, ...) if name == true then return assert(skynet.call(".service", "lua", "GQUERY", ...)) else return assert(skynet.call(".service", "lua", "QUERY", name, ...)) end end function skynet.address(addr) if type(addr) == "number" then return string.format(":%08x",addr) else return tostring(addr) end end function skynet.harbor(addr) return c.harbor(addr) end skynet.error = c.error skynet.tracelog = c.trace -- true: force on -- false: force off -- nil: optional (use skynet.trace() to trace one message) function skynet.traceproto(prototype, flag) local p = assert(proto[prototype]) p.trace = flag end ----- register protocol do local REG = skynet.register_protocol REG { name = "lua", id = skynet.PTYPE_LUA, pack = skynet.pack, unpack = skynet.unpack, } REG { name = "response", id = skynet.PTYPE_RESPONSE, } REG { name = "error", id = skynet.PTYPE_ERROR, unpack = function(...) return ... end, dispatch = _error_dispatch, } end skynet.init = skynet_require.init -- skynet.pcall is deprecated, use pcall directly skynet.pcall = pcall function skynet.init_service(start) local function main() skynet_require.init_all() start() end local ok, err = xpcall(main, traceback) if not ok then skynet.error("init service failed: " .. tostring(err)) skynet.send(".launcher","lua", "ERROR") skynet.exit() else skynet.send(".launcher","lua", "LAUNCHOK") end end function skynet.start(start_func) c.callback(skynet.dispatch_message) init_thread = skynet.timeout(0, function() skynet.init_service(start_func) init_thread = nil end) end function skynet.endless() return (c.intcommand("STAT", "endless") == 1) end function skynet.mqlen() return c.intcommand("STAT", "mqlen") end function skynet.stat(what) return c.intcommand("STAT", what) end local function task_traceback(co) if co == "BREAK" then return co elseif timeout_traceback and timeout_traceback[co] then return timeout_traceback[co] else return traceback(co) end end function skynet.task(ret) if ret == nil then local t = 0 for _,co in pairs(session_id_coroutine) do if co ~= "BREAK" then t = t + 1 end end return t end if ret == "init" then if init_thread then return traceback(init_thread) else return end end local tt = type(ret) if tt == "table" then for session,co in pairs(session_id_coroutine) do local key = string.format("%s session: %d", tostring(co), session) ret[key] = task_traceback(co) end return elseif tt == "number" then local co = session_id_coroutine[ret] if co then return task_traceback(co) else return "No session" end elseif tt == "thread" then for session, co in pairs(session_id_coroutine) do if co == ret then return session end end return end end function skynet.uniqtask() local stacks = {} for session, co in pairs(session_id_coroutine) do local stack = task_traceback(co) local info = stacks[stack] or {count = 0, sessions = {}} info.count = info.count + 1 if info.count < 10 then info.sessions[#info.sessions+1] = session end stacks[stack] = info end local ret = {} for stack, info in pairs(stacks) do local count = info.count local sessions = table.concat(info.sessions, ",") if count > 10 then sessions = sessions .. "..." end local head_line = string.format("%d\tsessions:[%s]\n", count, sessions) ret[head_line] = stack end return ret end function skynet.term(service) return _error_dispatch(0, service) end function skynet.memlimit(bytes) debug.getregistry().memlimit = bytes skynet.memlimit = nil -- set only once end -- Inject internal debug framework local debug = require "skynet.debug" debug.init(skynet, { dispatch = skynet.dispatch_message, suspend = suspend, resume = coroutine_resume, }) return skynet ================================================ FILE: lualib/snax/gateserver.lua ================================================ local skynet = require "skynet" local netpack = require "skynet.netpack" local socketdriver = require "skynet.socketdriver" local gateserver = {} local socket -- listen socket local queue -- message queue local maxclient -- max client local client_number = 0 local CMD = setmetatable({}, { __gc = function() netpack.clear(queue) end }) local nodelay = false local connection = {} -- true : connected -- nil : closed -- false : close read function gateserver.openclient(fd) if connection[fd] then socketdriver.start(fd) end end function gateserver.closeclient(fd) local c = connection[fd] if c ~= nil then connection[fd] = nil socketdriver.close(fd) end end function gateserver.start(handler) assert(handler.message) assert(handler.connect) local listen_context = {} function CMD.open( source, conf ) assert(not socket) local address = conf.address or "0.0.0.0" local port = conf.port maxclient = conf.maxclient or 1024 nodelay = conf.nodelay skynet.error("Listen on", address, port) socket = socketdriver.listen(address, port, conf.backlog) listen_context.co = coroutine.running() listen_context.fd = socket skynet.wait(listen_context.co) conf.address = listen_context.addr conf.port = listen_context.port listen_context = nil socketdriver.start(socket) if handler.open then return handler.open(source, conf) end end function CMD.close() assert(socket) socketdriver.close(socket) end local MSG = {} local function dispatch_msg(fd, msg, sz) if connection[fd] then handler.message(fd, msg, sz) else skynet.error(string.format("Drop message from fd (%d) : %s", fd, netpack.tostring(msg,sz))) end end MSG.data = dispatch_msg local function dispatch_queue() local fd, msg, sz = netpack.pop(queue) if fd then -- may dispatch even the handler.message blocked -- If the handler.message never block, the queue should be empty, so only fork once and then exit. skynet.fork(dispatch_queue) dispatch_msg(fd, msg, sz) for fd, msg, sz in netpack.pop, queue do dispatch_msg(fd, msg, sz) end end end MSG.more = dispatch_queue function MSG.open(fd, msg) client_number = client_number + 1 if client_number >= maxclient then socketdriver.shutdown(fd) return end if nodelay then socketdriver.nodelay(fd) end connection[fd] = true handler.connect(fd, msg) end function MSG.close(fd) if fd ~= socket then client_number = client_number - 1 if connection[fd] then connection[fd] = false -- close read end if handler.disconnect then handler.disconnect(fd) end else socket = nil end end function MSG.error(fd, msg) if fd == socket then skynet.error("gateserver accept error:",msg) else socketdriver.shutdown(fd) if handler.error then handler.error(fd, msg) end end end function MSG.warning(fd, size) if handler.warning then handler.warning(fd, size) end end function MSG.init(id, addr, port) if listen_context then local co = listen_context.co if co then assert(id == listen_context.fd) listen_context.addr = addr listen_context.port = port skynet.wakeup(co) listen_context.co = nil end end end skynet.register_protocol { name = "socket", id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 unpack = function ( msg, sz ) return netpack.filter( queue, msg, sz) end, dispatch = function (_, _, q, type, ...) queue = q if type then MSG[type](...) end end } local function init() skynet.dispatch("lua", function (_, address, cmd, ...) local f = CMD[cmd] if f then skynet.ret(skynet.pack(f(address, ...))) else skynet.ret(skynet.pack(handler.command(cmd, address, ...))) end end) end if handler.embed then init() else skynet.start(init) end end return gateserver ================================================ FILE: lualib/snax/hotfix.lua ================================================ local si = require "snax.interface" local function envid(f) local i = 1 while true do local name, value = debug.getupvalue(f, i) if name == nil then return end if name == "_ENV" then return debug.upvalueid(f, i) end i = i + 1 end end local function collect_uv(f , uv, env) local i = 1 while true do local name, value = debug.getupvalue(f, i) if name == nil then break end local id = debug.upvalueid(f, i) if uv[name] then assert(uv[name].id == id, string.format("ambiguity local value %s", name)) else uv[name] = { func = f, index = i, id = id } if type(value) == "function" then if envid(value) == env then collect_uv(value, uv, env) end end end i = i + 1 end end local function collect_all_uv(funcs) local globals = {} for _, v in pairs(funcs) do if v[4] then collect_uv(v[4], globals, envid(v[4])) end end if not globals["_ENV"] then globals["_ENV"] = {func = collect_uv, index = 1} end return globals end local function loader(source) return function (path, name, G) return load(source, "=patch", "bt", G) end end local function find_func(funcs, group , name) for _, desc in pairs(funcs) do local _, g, n = table.unpack(desc) if group == g and name == n then return desc end end end local dummy_env = {} for k,v in pairs(_ENV) do dummy_env[k] = v end local function _patch(globals, f) local i = 1 while true do local name, value = debug.getupvalue(f, i) if name == nil then break elseif value == nil or value == dummy_env then local old_uv = globals[name] if old_uv then debug.upvaluejoin(f, i, old_uv.func, old_uv.index) end else if type(value) == "function" then _patch(globals, value) end end i = i + 1 end end local function patch_func(funcs, globals, group, name, f) local desc = assert(find_func(funcs, group, name) , string.format("Patch mismatch %s.%s", group, name)) _patch(globals, f) desc[4] = f end local function inject(funcs, source, ...) local patch = si("patch", dummy_env, loader(source)) local globals = collect_all_uv(funcs) for _, v in pairs(patch) do local _, group, name, f = table.unpack(v) if f then patch_func(funcs, globals, group, name, f) end end local hf = find_func(patch, "system", "hotfix") if hf and hf[4] then return hf[4](...) end end return function (funcs, source, ...) return pcall(inject, funcs, source, ...) end ================================================ FILE: lualib/snax/interface.lua ================================================ local skynet = require "skynet" local function dft_loader(path, name, G) local errlist = {} for pat in string.gmatch(path,"[^;]+") do local filename = string.gsub(pat, "?", name) local f , err = loadfile(filename, "bt", G) if f then return f, pat else table.insert(errlist, err) end end error(table.concat(errlist, "\n")) end return function (name , G, loader) loader = loader or dft_loader local function func_id(id, group) local tmp = {} local function count( _, name, func) if type(name) ~= "string" then error (string.format("%s method only support string", group)) end if type(func) ~= "function" then error (string.format("%s.%s must be function", group, name)) end if tmp[name] then error (string.format("%s.%s duplicate definition", group, name)) end tmp[name] = true table.insert(id, { #id + 1, group, name, func} ) end return setmetatable({}, { __newindex = count }) end do assert(getmetatable(G) == nil) assert(G.init == nil) assert(G.exit == nil) assert(G.accept == nil) assert(G.response == nil) end local temp_global = {} local env = setmetatable({} , { __index = temp_global }) local func = {} local system = { "init", "exit", "hotfix", "profile"} do for k, v in ipairs(system) do system[v] = k func[k] = { k , "system", v } end end env.accept = func_id(func, "accept") env.response = func_id(func, "response") local function init_system(t, name, f) local index = system[name] if index then if type(f) ~= "function" then error (string.format("%s must be a function", name)) end func[index][4] = f else temp_global[name] = f end end local path = assert(skynet.getenv "snax" , "please set snax in config file") local mainfunc, pattern = loader(path, name, G) setmetatable(G, { __index = env , __newindex = init_system }) local ok, err = xpcall(mainfunc, debug.traceback) setmetatable(G, nil) assert(ok,err) for k,v in pairs(temp_global) do G[k] = v end return func, pattern end ================================================ FILE: lualib/snax/loginserver.lua ================================================ local skynet = require "skynet" require "skynet.manager" local socket = require "skynet.socket" local crypt = require "skynet.crypt" local table = table local string = string local assert = assert --[[ Protocol: line (\n) based text protocol 1. Server->Client : base64(8bytes random challenge) 2. Client->Server : base64(8bytes handshake client key) 3. Server: Gen a 8bytes handshake server key 4. Server->Client : base64(DH-Exchange(server key)) 5. Server/Client secret := DH-Secret(client key/server key) 6. Client->Server : base64(HMAC(challenge, secret)) 7. Client->Server : DES(secret, base64(token)) 8. Server : call auth_handler(token) -> server, uid (A user defined method) 9. Server : call login_handler(server, uid, secret) ->subid (A user defined method) 10. Server->Client : 200 base64(subid) Error Code: 401 Unauthorized . unauthorized by auth_handler 403 Forbidden . login_handler failed 406 Not Acceptable . already in login (disallow multi login) Success: 200 base64(subid) ]] local socket_error = {} local function assert_socket(service, v, fd) if v then return v else skynet.error(string.format("%s failed: socket (fd = %d) closed", service, fd)) error(socket_error) end end local function write(service, fd, text) assert_socket(service, socket.write(fd, text), fd) end local function launch_slave(auth_handler) local function auth(fd, addr) -- set socket buffer limit (8K) -- If the attacker send large package, close the socket socket.limit(fd, 8192) local challenge = crypt.randomkey() write("auth", fd, crypt.base64encode(challenge).."\n") local handshake = assert_socket("auth", socket.readline(fd), fd) local clientkey = crypt.base64decode(handshake) if #clientkey ~= 8 then error "Invalid client key" end local serverkey = crypt.randomkey() write("auth", fd, crypt.base64encode(crypt.dhexchange(serverkey)).."\n") local secret = crypt.dhsecret(clientkey, serverkey) local response = assert_socket("auth", socket.readline(fd), fd) local hmac = crypt.hmac64(challenge, secret) if hmac ~= crypt.base64decode(response) then error "challenge failed" end local etoken = assert_socket("auth", socket.readline(fd),fd) local token = crypt.desdecode(secret, crypt.base64decode(etoken)) local ok, server, uid = pcall(auth_handler,token) return ok, server, uid, secret end local function ret_pack(ok, err, ...) if ok then return skynet.pack(err, ...) else if err == socket_error then return skynet.pack(nil, "socket error") else return skynet.pack(false, err) end end end local function auth_fd(fd, addr) skynet.error(string.format("connect from %s (fd = %d)", addr, fd)) socket.start(fd) -- may raise error here local msg, len = ret_pack(pcall(auth, fd, addr)) socket.abandon(fd) -- never raise error here return msg, len end skynet.dispatch("lua", function(_,_,...) local ok, msg, len = pcall(auth_fd, ...) if ok then skynet.ret(msg,len) else skynet.ret(skynet.pack(false, msg)) end end) end local user_login = {} local function accept(conf, s, fd, addr) -- call slave auth local ok, server, uid, secret = skynet.call(s, "lua", fd, addr) -- slave will accept(start) fd, so we can write to fd later if not ok then if ok ~= nil then write("response 401", fd, "401 Unauthorized\n") end error(server) end if not conf.multilogin then if user_login[uid] then write("response 406", fd, "406 Not Acceptable\n") error(string.format("User %s is already login", uid)) end user_login[uid] = true end local ok, err = pcall(conf.login_handler, server, uid, secret) -- unlock login user_login[uid] = nil if ok then err = err or "" write("response 200",fd, "200 "..crypt.base64encode(err).."\n") else write("response 403",fd, "403 Forbidden\n") error(err) end end local function launch_master(conf) local instance = conf.instance or 8 assert(instance > 0) local host = conf.host or "0.0.0.0" local port = assert(tonumber(conf.port)) local slave = {} local balance = 1 skynet.dispatch("lua", function(_,source,command, ...) skynet.ret(skynet.pack(conf.command_handler(command, ...))) end) for i=1,instance do table.insert(slave, skynet.newservice(SERVICE_NAME)) end skynet.error(string.format("login server listen at : %s %d", host, port)) local id = socket.listen(host, port) socket.start(id , function(fd, addr) local s = slave[balance] balance = balance + 1 if balance > #slave then balance = 1 end local ok, err = pcall(accept, conf, s, fd, addr) if not ok then if err ~= socket_error then skynet.error(string.format("invalid client (fd = %d) error = %s", fd, err)) end end socket.close(fd) end) end local function login(conf) local name = "." .. (conf.name or "login") skynet.start(function() local loginmaster = skynet.localname(name) if loginmaster then local auth_handler = assert(conf.auth_handler) launch_master = nil conf = nil launch_slave(auth_handler) else launch_slave = nil conf.auth_handler = nil assert(conf.login_handler) assert(conf.command_handler) skynet.register(name) launch_master(conf) end end) end return login ================================================ FILE: lualib/snax/msgserver.lua ================================================ local skynet = require "skynet" local gateserver = require "snax.gateserver" local netpack = require "skynet.netpack" local crypt = require "skynet.crypt" local socketdriver = require "skynet.socketdriver" local assert = assert local b64encode = crypt.base64encode local b64decode = crypt.base64decode --[[ Protocol: All the number type is big-endian Shakehands (The first package) Client -> Server : base64(uid)@base64(server)#base64(subid):index:base64(hmac) Server -> Client XXX ErrorCode 404 User Not Found 403 Index Expired 401 Unauthorized 400 Bad Request 200 OK Req-Resp Client -> Server : Request word size (Not include self) string content (size-4) dword session Server -> Client : Response word size (Not include self) string content (size-5) byte ok (1 is ok, 0 is error) dword session API: server.userid(username) return uid, subid, server server.username(uid, subid, server) return username server.login(username, secret) update user secret server.logout(username) user logout server.ip(username) return ip when connection establish, or nil server.start(conf) start server Supported skynet command: kick username (may used by loginserver) login username secret (used by loginserver) logout username (used by agent) Config for server.start: conf.expired_number : the number of the response message cached after sending out (default is 128) conf.login_handler(uid, secret) -> subid : the function when a new user login, alloc a subid for it. (may call by login server) conf.logout_handler(uid, subid) : the functon when a user logout. (may call by agent) conf.kick_handler(uid, subid) : the functon when a user logout. (may call by login server) conf.request_handler(username, session, msg) : the function when recv a new request. conf.register_handler(servername) : call when gate open conf.disconnect_handler(username) : call when a connection disconnect (afk) ]] local server = {} skynet.register_protocol { name = "client", id = skynet.PTYPE_CLIENT, } local user_online = {} local handshake = {} local connection = {} function server.userid(username) -- base64(uid)@base64(server)#base64(subid) local uid, servername, subid = username:match "([^@]*)@([^#]*)#(.*)" return b64decode(uid), b64decode(subid), b64decode(servername) end function server.username(uid, subid, servername) return string.format("%s@%s#%s", b64encode(uid), b64encode(servername), b64encode(tostring(subid))) end function server.logout(username) local u = user_online[username] user_online[username] = nil if u.fd then if connection[u.fd] then gateserver.closeclient(u.fd) connection[u.fd] = nil end end end function server.login(username, secret) assert(user_online[username] == nil) user_online[username] = { secret = secret, version = 0, index = 0, username = username, response = {}, -- response cache } end function server.ip(username) local u = user_online[username] if u and u.fd then return u.ip end end function server.start(conf) local expired_number = conf.expired_number or 128 local handler = {} local CMD = { login = assert(conf.login_handler), logout = assert(conf.logout_handler), kick = assert(conf.kick_handler), } function handler.command(cmd, source, ...) local f = assert(CMD[cmd]) return f(...) end function handler.open(source, gateconf) local servername = assert(gateconf.servername) return conf.register_handler(servername) end function handler.connect(fd, addr) handshake[fd] = addr gateserver.openclient(fd) end function handler.disconnect(fd) handshake[fd] = nil local c = connection[fd] if c then if conf.disconnect_handler then conf.disconnect_handler(c.username) end -- double check, conf.disconnect_handler may close fd if connection[fd] then c.fd = nil connection[fd] = nil gateserver.closeclient(fd) end end end handler.error = handler.disconnect -- atomic , no yield local function do_auth(fd, message, addr) local username, index, hmac = string.match(message, "([^:]*):([^:]*):([^:]*)") local u = user_online[username] if u == nil then return "404 User Not Found" end local idx = assert(tonumber(index)) hmac = b64decode(hmac) if idx <= u.version then return "403 Index Expired" end local text = string.format("%s:%s", username, index) local v = crypt.hmac_hash(u.secret, text) -- equivalent to crypt.hmac64(crypt.hashkey(text), u.secret) if v ~= hmac then return "401 Unauthorized" end u.version = idx u.fd = fd u.ip = addr connection[fd] = u end local function auth(fd, addr, msg, sz) local message = netpack.tostring(msg, sz) local ok, result = pcall(do_auth, fd, message, addr) if not ok then skynet.error(result) result = "400 Bad Request" end local close = result ~= nil if result == nil then result = "200 OK" end socketdriver.send(fd, netpack.pack(result)) if close then gateserver.closeclient(fd) end end local request_handler = assert(conf.request_handler) -- u.response is a struct { return_fd , response, version, index } local function retire_response(u) if u.index >= expired_number * 2 then local max = 0 local response = u.response for k,p in pairs(response) do if p[1] == nil then -- request complete, check expired if p[4] < expired_number then response[k] = nil else p[4] = p[4] - expired_number if p[4] > max then max = p[4] end end end end u.index = max + 1 end end local function do_request(fd, message) local u = assert(connection[fd], "invalid fd") local session = string.unpack(">I4", message, -4) message = message:sub(1,-5) local p = u.response[session] if p then -- session can be reuse in the same connection if p[3] == u.version then local last = u.response[session] u.response[session] = nil p = nil if last[2] == nil then local error_msg = string.format("Conflict session %s", crypt.hexencode(session)) skynet.error(error_msg) error(error_msg) end end end if p == nil then p = { fd } u.response[session] = p local ok, result = pcall(request_handler, u.username, message) -- NOTICE: YIELD here, socket may close. result = result or "" if not ok then skynet.error(result) result = string.pack(">BI4", 0, session) else result = result .. string.pack(">BI4", 1, session) end p[2] = string.pack(">s2",result) p[3] = u.version p[4] = u.index else -- update version/index, change return fd. -- resend response. p[1] = fd p[3] = u.version p[4] = u.index if p[2] == nil then -- already request, but response is not ready return end end u.index = u.index + 1 -- the return fd is p[1] (fd may change by multi request) check connect fd = p[1] if connection[fd] then socketdriver.send(fd, p[2]) end p[1] = nil retire_response(u) end local function request(fd, msg, sz) local message = netpack.tostring(msg, sz) local ok, err = pcall(do_request, fd, message) -- not atomic, may yield if not ok then skynet.error(string.format("Invalid package %s : %s", err, message)) if connection[fd] then gateserver.closeclient(fd) end end end function handler.message(fd, msg, sz) local addr = handshake[fd] if addr then auth(fd,addr,msg,sz) handshake[fd] = nil else request(fd, msg, sz) end end return gateserver.start(handler) end return server ================================================ FILE: lualib/sproto.lua ================================================ local core = require "sproto.core" local assert = assert local sproto = {} local host = {} local weak_mt = { __mode = "kv" } local sproto_mt = { __index = sproto } local sproto_nogc = { __index = sproto } local host_mt = { __index = host } function sproto_mt:__gc() core.deleteproto(self.__cobj) end function sproto.new(bin) local cobj = assert(core.newproto(bin)) local self = { __cobj = cobj, __tcache = setmetatable( {} , weak_mt ), __pcache = setmetatable( {} , weak_mt ), } return setmetatable(self, sproto_mt) end function sproto.sharenew(cobj) local self = { __cobj = cobj, __tcache = setmetatable( {} , weak_mt ), __pcache = setmetatable( {} , weak_mt ), } return setmetatable(self, sproto_nogc) end function sproto.parse(ptext) local parser = require "sprotoparser" local pbin = parser.parse(ptext) return sproto.new(pbin) end function sproto:host( packagename ) packagename = packagename or "package" local obj = { __proto = self, __package = assert(core.querytype(self.__cobj, packagename), "type package not found"), __session = {}, } return setmetatable(obj, host_mt) end local function querytype(self, typename) local v = self.__tcache[typename] if not v then v = assert(core.querytype(self.__cobj, typename), "type not found") self.__tcache[typename] = v end return v end function sproto:exist_type(typename) local v = self.__tcache[typename] if not v then return core.querytype(self.__cobj, typename) ~= nil else return true end end function sproto:encode(typename, tbl) local st = querytype(self, typename) return core.encode(st, tbl) end function sproto:decode(typename, ...) local st = querytype(self, typename) return core.decode(st, ...) end function sproto:pencode(typename, tbl) local st = querytype(self, typename) return core.pack(core.encode(st, tbl)) end function sproto:pdecode(typename, ...) local st = querytype(self, typename) return core.decode(st, core.unpack(...)) end local function queryproto(self, pname) local v = self.__pcache[pname] if not v then local tag, req, resp = core.protocol(self.__cobj, pname) assert(tag, pname .. " not found") if tonumber(pname) then pname, tag = tag, pname end v = { request = req, response =resp, name = pname, tag = tag, } self.__pcache[pname] = v self.__pcache[tag] = v end return v end sproto.queryproto = queryproto function sproto:exist_proto(pname) local v = self.__pcache[pname] if not v then return core.protocol(self.__cobj, pname) ~= nil else return true end end function sproto:request_encode(protoname, tbl) local p = queryproto(self, protoname) local request = p.request if request then return core.encode(request,tbl) , p.tag else return "" , p.tag end end function sproto:response_encode(protoname, tbl) local p = queryproto(self, protoname) local response = p.response if response then return core.encode(response,tbl) else return "" end end function sproto:request_decode(protoname, ...) local p = queryproto(self, protoname) local request = p.request if request then return core.decode(request,...) , p.name else return nil, p.name end end function sproto:response_decode(protoname, ...) local p = queryproto(self, protoname) local response = p.response if response then return core.decode(response,...) end end sproto.pack = core.pack sproto.unpack = core.unpack function sproto:default(typename, type) if type == nil then return core.default(querytype(self, typename)) else local p = queryproto(self, typename) if type == "REQUEST" then if p.request then return core.default(p.request) end elseif type == "RESPONSE" then if p.response then return core.default(p.response) end else error "Invalid type" end end end local header_tmp = {} local function gen_response(self, response, session) return function(args, ud) header_tmp.type = nil header_tmp.session = session header_tmp.ud = ud local header = core.encode(self.__package, header_tmp) if response then local content = core.encode(response, args) return core.pack(header .. content) else return core.pack(header) end end end function host:dispatch(...) local bin = core.unpack(...) header_tmp.type = nil header_tmp.session = nil header_tmp.ud = nil local header, size = core.decode(self.__package, bin, header_tmp) local content = bin:sub(size + 1) if header.type then -- request local proto = queryproto(self.__proto, header.type) local result if proto.request then result = core.decode(proto.request, content) end if header_tmp.session then return "REQUEST", proto.name, result, gen_response(self, proto.response, header_tmp.session), header.ud else return "REQUEST", proto.name, result, nil, header.ud end else -- response local session = assert(header_tmp.session, "session not found") local response = assert(self.__session[session], "Unknown session") self.__session[session] = nil if response == true then return "RESPONSE", session, nil, header.ud else local result = core.decode(response, content) return "RESPONSE", session, result, header.ud end end end function host:attach(sp) return function(name, args, session, ud) local proto = queryproto(sp, name) header_tmp.type = proto.tag header_tmp.session = session header_tmp.ud = ud local header = core.encode(self.__package, header_tmp) if session then self.__session[session] = proto.response or true end if proto.request then local content = core.encode(proto.request, args) return core.pack(header .. content) else return core.pack(header) end end end return sproto ================================================ FILE: lualib/sprotoloader.lua ================================================ local parser = require "sprotoparser" local core = require "sproto.core" local sproto = require "sproto" local loader = {} function loader.register(filename, index) local f = assert(io.open(filename), "Can't open sproto file") local data = f:read "a" f:close() local sp = core.newproto(parser.parse(data)) core.saveproto(sp, index) end function loader.save(bin, index) local sp = core.newproto(bin) core.saveproto(sp, index) end function loader.load(index) local sp = core.loadproto(index) -- no __gc in metatable return sproto.sharenew(sp) end return loader ================================================ FILE: lualib/sprotoparser.lua ================================================ local lpeg = require "lpeg" local table = require "table" local packbytes local packvalue local version = _VERSION:match "5.*" if version and tonumber(version) >= 5.3 then function packbytes(str) return string.pack("=0 and id < 65536) local a = id % 256 local b = math.floor(id / 256) return string.char(a) .. string.char(b) end end local P = lpeg.P local S = lpeg.S local R = lpeg.R local C = lpeg.C local Ct = lpeg.Ct local Cg = lpeg.Cg local Cc = lpeg.Cc local V = lpeg.V local function count_lines(_,pos, parser_state) if parser_state.pos < pos then parser_state.line = parser_state.line + 1 parser_state.pos = pos end return pos end local exception = lpeg.Cmt( lpeg.Carg(1) , function ( _ , pos, parser_state) error(string.format("syntax error at [%s] line (%d)", parser_state.file or "", parser_state.line)) return pos end) local eof = P(-1) local newline = lpeg.Cmt((P"\n" + "\r\n") * lpeg.Carg(1) ,count_lines) local line_comment = "#" * (1 - newline) ^0 * (newline + eof) local blank = S" \t" + newline + line_comment local blank0 = blank ^ 0 local blanks = blank ^ 1 local alpha = R"az" + R"AZ" + "_" local alnum = alpha + R"09" local word = alpha * alnum ^ 0 local name = C(word) local typename = C(word * ("." * word) ^ 0) local tag = R"09" ^ 1 / tonumber local mainkey = "(" * blank0 * C((word ^ 0)) * blank0 * ")" local decimal = "(" * blank0 * C(tag) * blank0 * ")" local function multipat(pat) return Ct(blank0 * (pat * blanks) ^ 0 * pat^0 * blank0) end local function namedpat(name, pat) return Ct(Cg(Cc(name), "type") * Cg(pat)) end local typedef = P { "ALL", FIELD = namedpat("field", name * blanks * tag * blank0 * ":" * blank0 * (C"*")^-1 * typename * (mainkey + decimal)^0), STRUCT = P"{" * multipat(V"FIELD" + V"TYPE") * P"}", TYPE = namedpat("type", P"." * name * blank0 * V"STRUCT" ), SUBPROTO = Ct((C"request" + C"response") * blanks * (typename + V"STRUCT")), PROTOCOL = namedpat("protocol", name * blanks * tag * blank0 * P"{" * multipat(V"SUBPROTO") * P"}"), ALL = multipat(V"TYPE" + V"PROTOCOL"), } local proto = blank0 * typedef * blank0 local convert = {} function convert.protocol(all, obj) local result = { tag = obj[2] } for _, p in ipairs(obj[3]) do local pt = p[1] if result[pt] ~= nil then error(string.format("redefine %s in protocol %s", pt, obj[1])) end local typename = p[2] if type(typename) == "table" then local struct = typename typename = obj[1] .. "." .. p[1] all.type[typename] = convert.type(all, { typename, struct }) end if typename == "nil" then if p[1] == "response" then result.confirm = true end else result[p[1]] = typename end end return result end local map_keytypes = { integer = true, string = true, } function convert.type(all, obj) local result = {} local typename = obj[1] local tags = {} local names = {} for _, f in ipairs(obj[2]) do if f.type == "field" then local name = f[1] if names[name] then error(string.format("redefine %s in type %s", name, typename)) end names[name] = true local tag = f[2] if tags[tag] then error(string.format("redefine tag %d in type %s", tag, typename)) end tags[tag] = true local field = { name = name, tag = tag } table.insert(result, field) local fieldtype = f[3] if fieldtype == "*" then field.array = true fieldtype = f[4] end local mainkey = f[5] if mainkey then if fieldtype == "integer" then field.decimal = mainkey else assert(field.array) field.key = mainkey end end field.typename = fieldtype else assert(f.type == "type") -- nest type local nesttypename = typename .. "." .. f[1] f[1] = nesttypename assert(all.type[nesttypename] == nil, "redefined " .. nesttypename) all.type[nesttypename] = convert.type(all, f) end end table.sort(result, function(a,b) return a.tag < b.tag end) return result end local function adjust(r) local result = { type = {} , protocol = {} } for _, obj in ipairs(r) do local set = result[obj.type] local name = obj[1] assert(set[name] == nil , "redefined " .. name) set[name] = convert[obj.type](result,obj) end return result end local buildin_types = { integer = 0, boolean = 1, string = 2, binary = 2, -- binary is a sub type of string double = 3, } local function checktype(types, ptype, t) if buildin_types[t] then return t end local fullname = ptype .. "." .. t if types[fullname] then return fullname else ptype = ptype:match "(.+)%..+$" if ptype then return checktype(types, ptype, t) elseif types[t] then return t end end end local function check_protocol(r) local map = {} local type = r.type for name, v in pairs(r.protocol) do local tag = v.tag local request = v.request local response = v.response local p = map[tag] if p then error(string.format("redefined protocol tag %d at %s", tag, name)) end if request and not type[request] then error(string.format("Undefined request type %s in protocol %s", request, name)) end if response and not type[response] then error(string.format("Undefined response type %s in protocol %s", response, name)) end map[tag] = v end return r end local function flattypename(r) for typename, t in pairs(r.type) do for _, f in pairs(t) do local ftype = f.typename local fullname = checktype(r.type, typename, ftype) if fullname == nil then error(string.format("Undefined type %s in type %s", ftype, typename)) end f.typename = fullname end end return r end local function parser(text,filename) local state = { file = filename, pos = 0, line = 1 } local r = lpeg.match(proto * -1 + exception , text , 1, state ) return flattypename(check_protocol(adjust(r))) end --[[ -- The protocol of sproto .type { .field { name 0 : string buildin 1 : integer type 2 : integer tag 3 : integer array 4 : boolean key 5 : integer # If key exists, array must be true map 6 : boolean # Interpret two fields struct as map when decoding } name 0 : string fields 1 : *field } .protocol { name 0 : string tag 1 : integer request 2 : integer # index response 3 : integer # index confirm 4 : boolean # true means response nil } .group { type 0 : *type protocol 1 : *protocol } ]] local function packfield(f) local strtbl = {} if f.array then if f.key then if f.map then table.insert(strtbl, "\7\0") -- 7 fields else table.insert(strtbl, "\6\0") -- 6 fields end else table.insert(strtbl, "\5\0") -- 5 fields end else table.insert(strtbl, "\4\0") -- 4 fields end table.insert(strtbl, "\0\0") -- name (tag = 0, ref an object) if f.buildin then table.insert(strtbl, packvalue(f.buildin)) -- buildin (tag = 1) if f.extra then table.insert(strtbl, packvalue(f.extra)) -- f.buildin can be integer or string else table.insert(strtbl, "\1\0") -- skip (tag = 2) end table.insert(strtbl, packvalue(f.tag)) -- tag (tag = 3) else table.insert(strtbl, "\1\0") -- skip (tag = 1) table.insert(strtbl, packvalue(f.type)) -- type (tag = 2) table.insert(strtbl, packvalue(f.tag)) -- tag (tag = 3) end if f.array then table.insert(strtbl, packvalue(1)) -- array = true (tag = 4) if f.key then table.insert(strtbl, packvalue(f.key)) -- key tag (tag = 5) if f.map then table.insert(strtbl, packvalue(f.map)) -- map tag (tag = 6) end end end table.insert(strtbl, packbytes(f.name)) -- external object (name) return packbytes(table.concat(strtbl)) end local function packtype(name, t, alltypes) local fields = {} local tmp = {} for _, f in ipairs(t) do tmp.array = f.array tmp.name = f.name tmp.tag = f.tag tmp.extra = f.decimal tmp.buildin = buildin_types[f.typename] if f.typename == "binary" then tmp.extra = 1 -- binary is sub type of string end local subtype if not tmp.buildin then subtype = assert(alltypes[f.typename]) tmp.type = subtype.id else tmp.type = nil end tmp.map = nil if f.key then assert(f.array) if f.key == "" then tmp.map = 1 local c = 0 local min_t = math.maxinteger for n, t in pairs(subtype.fields) do c = c + 1 if t.tag < min_t then min_t = t.tag f.key = n end end if c ~= 2 then error(string.format("Invalid map definition: %s, must only have two fields", tmp.name)) end end local stfield = subtype.fields[f.key] if not stfield or not stfield.buildin then error("Invalid map index :" .. f.key) end tmp.key = stfield.tag else tmp.key = nil end table.insert(fields, packfield(tmp)) end local data if #fields == 0 then data = { "\1\0", -- 1 fields "\0\0", -- name (id = 0, ref = 0) packbytes(name), } else data = { "\2\0", -- 2 fields "\0\0", -- name (tag = 0, ref = 0) "\0\0", -- field[] (tag = 1, ref = 1) packbytes(name), packbytes(table.concat(fields)), } end return packbytes(table.concat(data)) end local function packproto(name, p, alltypes) if p.request then local request = alltypes[p.request] if request == nil then error(string.format("Protocol %s request type %s not found", name, p.request)) end request = request.id end local tmp = { "\4\0", -- 4 fields "\0\0", -- name (id=0, ref=0) packvalue(p.tag), -- tag (tag=1) } if p.request == nil and p.response == nil and p.confirm == nil then tmp[1] = "\2\0" -- only two fields else if p.request then table.insert(tmp, packvalue(alltypes[p.request].id)) -- request typename (tag=2) else table.insert(tmp, "\1\0") -- skip this field (request) end if p.response then table.insert(tmp, packvalue(alltypes[p.response].id)) -- request typename (tag=3) elseif p.confirm then tmp[1] = "\5\0" -- add confirm field table.insert(tmp, "\1\0") -- skip this field (response) table.insert(tmp, packvalue(1)) -- confirm = true else tmp[1] = "\3\0" -- only three fields end end table.insert(tmp, packbytes(name)) return packbytes(table.concat(tmp)) end local function packgroup(t,p) if next(t) == nil then assert(next(p) == nil) return "\0\0" end local tt, tp local alltypes = {} for name in pairs(t) do table.insert(alltypes, name) end table.sort(alltypes) -- make result stable for idx, name in ipairs(alltypes) do local fields = {} for _, type_fields in ipairs(t[name]) do fields[type_fields.name] = { tag = type_fields.tag, buildin = buildin_types[type_fields.typename] } end alltypes[name] = { id = idx - 1, fields = fields } end tt = {} for _,name in ipairs(alltypes) do table.insert(tt, packtype(name, t[name], alltypes)) end tt = packbytes(table.concat(tt)) if next(p) then local tmp = {} for name, tbl in pairs(p) do table.insert(tmp, tbl) tbl.name = name end table.sort(tmp, function(a,b) return a.tag < b.tag end) tp = {} for _, tbl in ipairs(tmp) do table.insert(tp, packproto(tbl.name, tbl, alltypes)) end tp = packbytes(table.concat(tp)) end local result if tp == nil then result = { "\1\0", -- 1 field "\0\0", -- type[] (id = 0, ref = 0) tt, } else result = { "\2\0", -- 2fields "\0\0", -- type array (id = 0, ref = 0) "\0\0", -- protocol array (id = 1, ref =1) tt, tp, } end return table.concat(result) end local function encodeall(r) return packgroup(r.type, r.protocol) end local sparser = {} function sparser.dump(str) local tmp = "" for i=1,#str do tmp = tmp .. string.format("%02X ", string.byte(str,i)) if i % 8 == 0 then if i % 16 == 0 then print(tmp) tmp = "" else tmp = tmp .. "- " end end end print(tmp) end function sparser.parse(text, name) local r = parser(text, name or "=text") local data = encodeall(r) return data end return sparser ================================================ FILE: lualib-src/lsha1.c ================================================ /* SHA-1 in C By Steve Reid 100% Public Domain ----------------- Modified 7/98 By James H. Brown Still 100% Public Domain Corrected a problem which generated improper hash values on 16 bit machines Routine SHA1Update changed from void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len) to void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned long len) The 'len' parameter was declared an int which works fine on 32 bit machines. However, on 16 bit machines an int is too small for the shifts being done against it. This caused the hash function to generate incorrect values if len was greater than 8191 (8K - 1) due to the 'len << 3' on line 3 of SHA1Update(). Since the file IO in main() reads 16K at a time, any file 8K or larger would be guaranteed to generate the wrong hash (e.g. Test Vector #3, a million "a"s). I also changed the declaration of variables i & j in SHA1Update to unsigned long from unsigned int for the same reason. These changes should make no difference to any 32 bit implementations since an int and a long are the same size in those environments. -- I also corrected a few compiler warnings generated by Borland C. 1. Added #include for exit() prototype 2. Removed unused variable 'j' in SHA1Final 3. Changed exit(0) to return(0) at end of main. ALL changes I made can be located by searching for comments containing 'JHB' ----------------- Modified 8/98 By Steve Reid Still 100% public domain 1- Removed #include and used return() instead of exit() 2- Fixed overwriting of finalcount in SHA1Final() (discovered by Chris Hall) 3- Changed email address from steve@edmweb.com to sreid@sea-to-sky.net ----------------- Modified 4/01 By Saul Kravitz Still 100% PD Modified to run on Compaq Alpha hardware. ----------------- Modified 07/2002 By Ralph Giles Still 100% public domain modified for use with stdint types, autoconf code cleanup, removed attribution comments switched SHA1Final() argument order for consistency use SHA1_ prefix for public api move public api to sha1.h ----------------- Modufiled 08/2014 By Cloud Wu Still 100% PD Lua binding */ /* Test Vectors (from FIPS PUB 180-1) "abc" A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 A million repetitions of "a" 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F */ #define LUA_LIB #include #include #include typedef struct { uint32_t state[5]; uint32_t count[2]; uint8_t buffer[64]; } SHA1_CTX; #define SHA1_DIGEST_SIZE 20 static void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]); #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) /* blk0() and blk() perform the initial expand. */ /* I got the idea of expanding during the round function from SSLeay */ /* FIXME: can we do this in an endian-proof way? */ #ifdef WORDS_BIGENDIAN #define blk0(i) block.l[i] #else #define blk0(i) (block.l[i] = (rol(block.l[i],24)&0xFF00FF00) \ |(rol(block.l[i],8)&0x00FF00FF)) #endif #define blk(i) (block.l[i&15] = rol(block.l[(i+13)&15]^block.l[(i+8)&15] \ ^block.l[(i+2)&15]^block.l[i&15],1)) /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); /* Hash a single 512-bit block. This is the core of the algorithm. */ static void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]) { uint32_t a, b, c, d, e; typedef union { uint8_t c[64]; uint32_t l[16]; } CHAR64LONG16; CHAR64LONG16 block; memcpy(&block, buffer, 64); /* Copy context->state[] to working vars */ a = state[0]; b = state[1]; c = state[2]; d = state[3]; e = state[4]; /* 4 rounds of 20 operations each. Loop unrolled. */ R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); /* Add the working vars back into context.state[] */ state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[4] += e; /* Wipe variables */ a = b = c = d = e = 0; } /* SHA1Init - Initialize new context */ static void sat_SHA1_Init(SHA1_CTX* context) { /* SHA1 initialization constants */ context->state[0] = 0x67452301; context->state[1] = 0xEFCDAB89; context->state[2] = 0x98BADCFE; context->state[3] = 0x10325476; context->state[4] = 0xC3D2E1F0; context->count[0] = context->count[1] = 0; } /* Run your data through this. */ static void sat_SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len) { size_t i, j; #ifdef VERBOSE SHAPrintContext(context, "before"); #endif j = (context->count[0] >> 3) & 63; if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++; context->count[1] += (len >> 29); if ((j + len) > 63) { memcpy(&context->buffer[j], data, (i = 64-j)); SHA1_Transform(context->state, context->buffer); for ( ; i + 63 < len; i += 64) { SHA1_Transform(context->state, data + i); } j = 0; } else i = 0; memcpy(&context->buffer[j], &data[i], len - i); #ifdef VERBOSE SHAPrintContext(context, "after "); #endif } /* Add padding and return the message digest. */ static void sat_SHA1_Final(SHA1_CTX* context, uint8_t digest[SHA1_DIGEST_SIZE]) { uint32_t i; uint8_t finalcount[8]; for (i = 0; i < 8; i++) { finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ } sat_SHA1_Update(context, (uint8_t *)"\200", 1); while ((context->count[0] & 504) != 448) { sat_SHA1_Update(context, (uint8_t *)"\0", 1); } sat_SHA1_Update(context, finalcount, 8); /* Should cause a SHA1_Transform() */ for (i = 0; i < SHA1_DIGEST_SIZE; i++) { digest[i] = (uint8_t) ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); } /* Wipe variables */ i = 0; memset(context->buffer, 0, 64); memset(context->state, 0, 20); memset(context->count, 0, 8); memset(finalcount, 0, 8); /* SWR */ } #include #include int lsha1(lua_State *L) { size_t sz = 0; const uint8_t * buffer = (const uint8_t *)luaL_checklstring(L, 1, &sz); uint8_t digest[SHA1_DIGEST_SIZE]; SHA1_CTX ctx; sat_SHA1_Init(&ctx); sat_SHA1_Update(&ctx, buffer, sz); sat_SHA1_Final(&ctx, digest); lua_pushlstring(L, (const char *)digest, SHA1_DIGEST_SIZE); return 1; } #define BLOCKSIZE 64 static inline void xor_key(uint8_t key[BLOCKSIZE], uint32_t xor_) { int i; for (i=0;i BLOCKSIZE) { SHA1_CTX ctx; sat_SHA1_Init(&ctx); sat_SHA1_Update(&ctx, key, key_sz); sat_SHA1_Final(&ctx, rkey); key_sz = SHA1_DIGEST_SIZE; } else { memcpy(rkey, key, key_sz); } xor_key(rkey, 0x5c5c5c5c); sat_SHA1_Init(&ctx1); sat_SHA1_Update(&ctx1, rkey, BLOCKSIZE); xor_key(rkey, 0x5c5c5c5c ^ 0x36363636); sat_SHA1_Init(&ctx2); sat_SHA1_Update(&ctx2, rkey, BLOCKSIZE); sat_SHA1_Update(&ctx2, text, text_sz); sat_SHA1_Final(&ctx2, digest2); sat_SHA1_Update(&ctx1, digest2, SHA1_DIGEST_SIZE); sat_SHA1_Final(&ctx1, digest1); lua_pushlstring(L, (const char *)digest1, SHA1_DIGEST_SIZE); return 1; } ================================================ FILE: lualib-src/ltls.c ================================================ #include #include #include #include #include #include #include #include #include #include #include static bool TLS_IS_INIT = false; struct tls_context { SSL* ssl; BIO* in_bio; BIO* out_bio; bool is_server; bool is_close; }; struct ssl_ctx { SSL_CTX* ctx; }; // static int // _ssl_verify_peer(int ok, X509_STORE_CTX* ctx) { // return 1; // } static void _init_bio(lua_State* L, struct tls_context* tls_p, struct ssl_ctx* ctx_p) { tls_p->ssl = SSL_new(ctx_p->ctx); if(!tls_p->ssl) { luaL_error(L, "SSL_new failed"); } tls_p->in_bio = BIO_new(BIO_s_mem()); if(!tls_p->in_bio) { luaL_error(L, "new in bio failed"); } BIO_set_mem_eof_return(tls_p->in_bio, -1); /* see: https://www.openssl.org/docs/crypto/BIO_s_mem.html */ tls_p->out_bio = BIO_new(BIO_s_mem()); if(!tls_p->out_bio) { luaL_error(L, "new out bio failed"); } BIO_set_mem_eof_return(tls_p->out_bio, -1); /* see: https://www.openssl.org/docs/crypto/BIO_s_mem.html */ SSL_set_bio(tls_p->ssl, tls_p->in_bio, tls_p->out_bio); } static void _init_client_context(lua_State* L, struct tls_context* tls_p, struct ssl_ctx* ctx_p) { tls_p->is_server = false; _init_bio(L, tls_p, ctx_p); SSL_set_connect_state(tls_p->ssl); } static void _init_server_context(lua_State* L, struct tls_context* tls_p, struct ssl_ctx* ctx_p) { tls_p->is_server = true; _init_bio(L, tls_p, ctx_p); SSL_set_accept_state(tls_p->ssl); } static struct tls_context * _check_context(lua_State* L, int idx) { struct tls_context* tls_p = (struct tls_context*)lua_touserdata(L, idx); if(!tls_p) { luaL_error(L, "need tls context"); } if(tls_p->is_close) { luaL_error(L, "context is closed"); } return tls_p; } static struct ssl_ctx * _check_sslctx(lua_State* L, int idx) { struct ssl_ctx* ctx_p = (struct ssl_ctx*)lua_touserdata(L, idx); if(!ctx_p) { luaL_error(L, "need sslctx"); } return ctx_p; } static int _ltls_context_finished(lua_State* L) { struct tls_context* tls_p = _check_context(L, 1); int b = SSL_is_init_finished(tls_p->ssl); lua_pushboolean(L, b); return 1; } static int _ltls_context_close(lua_State* L) { struct tls_context* tls_p = lua_touserdata(L, 1); if(!tls_p->is_close) { SSL_free(tls_p->ssl); tls_p->ssl = NULL; tls_p->in_bio = NULL; //in_bio and out_bio will be free when SSL_free is called tls_p->out_bio = NULL; tls_p->is_close = true; } return 0; } static int _bio_read(lua_State* L, struct tls_context* tls_p) { char outbuff[4096]; int all_read = 0; int read = 0; int pending = BIO_ctrl_pending(tls_p->out_bio); if(pending >0) { luaL_Buffer b; luaL_buffinit(L, &b); while(pending > 0) { read = BIO_read(tls_p->out_bio, outbuff, sizeof(outbuff)); // printf("BIO_read read:%d pending:%d\n", read, pending); if(read <= 0) { luaL_error(L, "BIO_read error:%d", read); }else if(read <= sizeof(outbuff)) { all_read += read; luaL_addlstring(&b, (const char*)outbuff, read); }else { luaL_error(L, "invalid BIO_read:%d", read); } pending = BIO_ctrl_pending(tls_p->out_bio); } if(all_read>0) { luaL_pushresult(&b); } } return all_read; } static void _bio_write(lua_State* L, struct tls_context* tls_p, const char* s, size_t len) { char* p = (char*)s; size_t sz = len; while(sz > 0) { int written = BIO_write(tls_p->in_bio, p, sz); // printf("BIO_write written:%d sz:%zu\n", written, sz); if(written <= 0) { luaL_error(L, "BIO_write error:%d", written); }else if (written <= sz) { p += written; sz -= written; }else { luaL_error(L, "invalid BIO_write:%d", written); } } } static int _ltls_context_handshake(lua_State* L) { struct tls_context* tls_p = _check_context(L, 1); size_t slen = 0; const char* exchange = lua_tolstring(L, 2, &slen); // check handshake is finished if(SSL_is_init_finished(tls_p->ssl)) { luaL_error(L, "handshake is finished"); } // handshake exchange if(slen > 0 && exchange != NULL) { _bio_write(L, tls_p, exchange, slen); } // first handshake; initiated by client if(!SSL_is_init_finished(tls_p->ssl)) { int ret = SSL_do_handshake(tls_p->ssl); if(ret == 1) { return 0; } else if (ret < 0) { int err = SSL_get_error(tls_p->ssl, ret); ERR_clear_error(); if(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { int all_read = _bio_read(L, tls_p); if(all_read>0) { return 1; } } else { luaL_error(L, "SSL_do_handshake error:%d ret:%d", err, ret); } } else { int err = SSL_get_error(tls_p->ssl, ret); ERR_clear_error(); luaL_error(L, "SSL_do_handshake error:%d ret:%d", err, ret); } } return 0; } static int _ltls_context_read(lua_State* L) { struct tls_context* tls_p = _check_context(L, 1); size_t slen = 0; const char* encrypted_data = lua_tolstring(L, 2, &slen); // write encrypted data if(slen>0 && encrypted_data) { _bio_write(L, tls_p, encrypted_data, slen); } char outbuff[4096]; int read = 0; luaL_Buffer b; luaL_buffinit(L, &b); do { read = SSL_read(tls_p->ssl, outbuff, sizeof(outbuff)); if(read < 0) { int err = SSL_get_error(tls_p->ssl, read); ERR_clear_error(); if(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { break; } luaL_error(L, "SSL_read error:%d", err); }else if(read == 0){ break; } else if(read <= sizeof(outbuff)) { luaL_addlstring(&b, outbuff, read); }else { luaL_error(L, "invalid SSL_read:%d", read); } }while(true); luaL_pushresult(&b); return 1; } static int _ltls_context_write(lua_State* L) { struct tls_context* tls_p = _check_context(L, 1); size_t slen = 0; char* unencrypted_data = (char*)lua_tolstring(L, 2, &slen); while(slen > 0) { int written = SSL_write(tls_p->ssl, unencrypted_data, slen); if(written <= 0) { int err = SSL_get_error(tls_p->ssl, written); ERR_clear_error(); luaL_error(L, "SSL_write error:%d", err); }else if(written <= slen) { unencrypted_data += written; slen -= written; }else { luaL_error(L, "invalid SSL_write:%d", written); } } int all_read = _bio_read(L, tls_p); if(all_read <= 0) { lua_pushstring(L, ""); } return 1; } static int _lset_ext_host_name(lua_State* L) { struct tls_context* tls_p = _check_context(L, 1); size_t slen = 0; char* host = (char*)lua_tolstring(L, 2, &slen); int ret = SSL_set_tlsext_host_name(tls_p->ssl, host); lua_pushinteger(L, ret); return 1; } static int _lctx_gc(lua_State* L) { struct ssl_ctx* ctx_p = _check_sslctx(L, 1); if(ctx_p->ctx) { SSL_CTX_free(ctx_p->ctx); ctx_p->ctx = NULL; } return 0; } static int _lctx_cert(lua_State* L) { struct ssl_ctx* ctx_p = _check_sslctx(L, 1); const char* certfile = lua_tostring(L, 2); const char* key = lua_tostring(L, 3); if(!certfile) { luaL_error(L, "need certfile"); } if(!key) { luaL_error(L, "need private key"); } int ret = SSL_CTX_use_certificate_chain_file(ctx_p->ctx, certfile); if(ret != 1) { luaL_error(L, "SSL_CTX_use_certificate_chain_file error:%d", ret); } ret = SSL_CTX_use_PrivateKey_file(ctx_p->ctx, key, SSL_FILETYPE_PEM); if(ret != 1) { luaL_error(L, "SSL_CTX_use_PrivateKey_file error:%d", ret); } ret = SSL_CTX_check_private_key(ctx_p->ctx); if(ret != 1) { luaL_error(L, "SSL_CTX_check_private_key error:%d", ret); } return 0; } static int _lctx_ciphers(lua_State* L) { struct ssl_ctx* ctx_p = _check_sslctx(L, 1); const char* s = lua_tostring(L, 2); if(!s) { luaL_error(L, "need cipher list"); } int ret = SSL_CTX_set_tlsext_use_srtp(ctx_p->ctx, s); if(ret != 0) { luaL_error(L, "SSL_CTX_set_tlsext_use_srtp error:%d", ret); } return 0; } static int lnew_ctx(lua_State* L) { struct ssl_ctx* ctx_p = (struct ssl_ctx*)lua_newuserdatauv(L, sizeof(*ctx_p), 0); ctx_p->ctx = SSL_CTX_new(SSLv23_method()); if(!ctx_p->ctx) { unsigned int err = ERR_get_error(); char buf[256]; ERR_error_string_n(err, buf, sizeof(buf)); luaL_error(L, "SSL_CTX_new client failed. %s\n", buf); } if(luaL_newmetatable(L, "_TLS_SSLCTX_METATABLE_")) { luaL_Reg l[] = { {"set_ciphers", _lctx_ciphers}, {"set_cert", _lctx_cert}, {NULL, NULL}, }; luaL_newlib(L, l); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, _lctx_gc); lua_setfield(L, -2, "__gc"); } lua_setmetatable(L, -2); return 1; } static int lnew_tls(lua_State* L) { struct tls_context* tls_p = (struct tls_context*)lua_newuserdatauv(L, sizeof(*tls_p), 1); tls_p->is_close = false; const char* method = luaL_optstring(L, 1, "nil"); struct ssl_ctx* ctx_p = _check_sslctx(L, 2); lua_pushvalue(L, 2); lua_setiuservalue(L, -2, 1); // set ssl_ctx associated to tls_context if(strcmp(method, "client") == 0) { _init_client_context(L, tls_p, ctx_p); if (!lua_isnoneornil(L, 3)) { const char* hostname = luaL_checkstring(L, 3); SSL_set_tlsext_host_name(tls_p->ssl, hostname); } }else if(strcmp(method, "server") == 0) { _init_server_context(L, tls_p, ctx_p); } else { luaL_error(L, "invalid method:%s e.g[server, client]", method); } if(luaL_newmetatable(L, "_TLS_CONTEXT_METATABLE_")) { luaL_Reg l[] = { {"close", _ltls_context_close}, {"finished", _ltls_context_finished}, {"handshake", _ltls_context_handshake}, {"read", _ltls_context_read}, {"write", _ltls_context_write}, {"set_ext_host_name", _lset_ext_host_name}, {NULL, NULL}, }; luaL_newlib(L, l); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, _ltls_context_close); lua_setfield(L, -2, "__gc"); } lua_setmetatable(L, -2); return 1; } int luaopen_ltls_c(lua_State* L) { if(!TLS_IS_INIT) { luaL_error(L, "ltls need init, Put enablessl = true in you config file."); } luaL_Reg l[] = { {"newctx", lnew_ctx}, {"newtls", lnew_tls}, {NULL, NULL}, }; luaL_checkversion(L); luaL_newlib(L, l); return 1; } // for ltls init static int ltls_init_constructor(lua_State* L) { #ifndef OPENSSL_EXTERNAL_INITIALIZATION if(!TLS_IS_INIT) { SSL_library_init(); SSL_load_error_strings(); #if OPENSSL_VERSION_NUMBER < 0x30000000L ERR_load_BIO_strings(); #endif OpenSSL_add_all_algorithms(); } #endif TLS_IS_INIT = true; return 0; } static int ltls_init_destructor(lua_State* L) { #ifndef OPENSSL_EXTERNAL_INITIALIZATION if(TLS_IS_INIT) { ENGINE_cleanup(); CONF_modules_unload(1); ERR_free_strings(); EVP_cleanup(); sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); CRYPTO_cleanup_all_ex_data(); } #endif TLS_IS_INIT = false; return 0; } int luaopen_ltls_init_c(lua_State* L) { luaL_Reg l[] = { {"constructor", ltls_init_constructor}, {"destructor", ltls_init_destructor}, {NULL, NULL}, }; luaL_checkversion(L); luaL_newlib(L, l); return 1; } ================================================ FILE: lualib-src/lua-bson.c ================================================ #define LUA_LIB #include #include #include #include #include #include #include #include #include "atomic.h" #define DEFAULT_CAP 64 #define MAX_NUMBER 1024 // avoid circular reference while encodeing #define MAX_DEPTH 128 #define BSON_REAL 1 #define BSON_STRING 2 #define BSON_DOCUMENT 3 #define BSON_ARRAY 4 #define BSON_BINARY 5 #define BSON_UNDEFINED 6 #define BSON_OBJECTID 7 #define BSON_BOOLEAN 8 #define BSON_DATE 9 #define BSON_NULL 10 #define BSON_REGEX 11 #define BSON_DBPOINTER 12 #define BSON_JSCODE 13 #define BSON_SYMBOL 14 #define BSON_CODEWS 15 #define BSON_INT32 16 #define BSON_TIMESTAMP 17 #define BSON_INT64 18 #define BSON_MINKEY 255 #define BSON_MAXKEY 127 #define BSON_TYPE_SHIFT 5 static char bson_numstrs[MAX_NUMBER][4]; static int bson_numstr_len[MAX_NUMBER]; struct bson { int size; int cap; uint8_t *ptr; uint8_t buffer[DEFAULT_CAP]; }; struct bson_reader { const uint8_t * ptr; int size; }; static inline int32_t get_length(const uint8_t * data) { const uint8_t * b = (const uint8_t *)data; int32_t len = b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24; return len; } static inline void bson_destroy(struct bson *b) { if (b->ptr != b->buffer) { free(b->ptr); } } static inline void bson_create(struct bson *b) { b->size = 0; b->cap = DEFAULT_CAP; b->ptr = b->buffer; } static inline void bson_reserve(struct bson *b, int sz) { if (b->size + sz <= b->cap) return; do { b->cap *= 2; } while (b->cap <= b->size + sz); if (b->ptr == b->buffer) { b->ptr = (uint8_t*)malloc(b->cap); memcpy(b->ptr, b->buffer, b->size); } else { b->ptr = (uint8_t*)realloc(b->ptr, b->cap); } } static inline void check_reader(lua_State *L, struct bson_reader *br, int sz) { if (br->size < sz) { luaL_error(L, "Invalid bson block (%d:%d)", br->size, sz); } } static inline int read_byte(lua_State *L, struct bson_reader *br) { check_reader(L, br, 1); const uint8_t * b = br->ptr; int r = b[0]; ++br->ptr; --br->size; return r; } static inline int32_t read_int32(lua_State *L, struct bson_reader *br) { check_reader(L, br, 4); const uint8_t * b = br->ptr; uint32_t v = b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24; br->ptr+=4; br->size-=4; return (int32_t)v; } static inline int64_t read_int64(lua_State *L, struct bson_reader *br) { check_reader(L, br, 8); const uint8_t * b = br->ptr; uint32_t lo = b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24; uint32_t hi = b[4] | b[5]<<8 | b[6]<<16 | b[7]<<24; uint64_t v = (uint64_t)lo | (uint64_t)hi<<32; br->ptr+=8; br->size-=8; return (int64_t)v; } static inline lua_Number read_double(lua_State *L, struct bson_reader *br) { check_reader(L, br, 8); union { uint64_t i; double d; } v; const uint8_t * b = br->ptr; uint32_t lo = b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24; uint32_t hi = b[4] | b[5]<<8 | b[6]<<16 | b[7]<<24; v.i = (uint64_t)lo | (uint64_t)hi<<32; br->ptr+=8; br->size-=8; return v.d; } static inline const void * read_bytes(lua_State *L, struct bson_reader *br, int sz) { const void * r = br->ptr; check_reader(L, br, sz); br->ptr+=sz; br->size-=sz; return r; } static inline const char * read_cstring(lua_State *L, struct bson_reader *br, size_t *sz) { int i; for (i=0;;i++) { if (i==br->size) { luaL_error(L, "Invalid bson block : cstring"); } if (br->ptr[i] == '\0') { break; } } *sz = i; const char * r = (const char *)br->ptr; br->ptr += i+1; br->size -= i+1; return r; } static inline void write_byte(struct bson *b, uint8_t v) { bson_reserve(b,1); b->ptr[b->size++] = v; } static inline void write_int32(struct bson *b, int32_t v) { uint32_t uv = (uint32_t)v; bson_reserve(b,4); b->ptr[b->size++] = uv & 0xff; b->ptr[b->size++] = (uv >> 8)&0xff; b->ptr[b->size++] = (uv >> 16)&0xff; b->ptr[b->size++] = (uv >> 24)&0xff; } static inline void write_length(struct bson *b, int32_t v, int off) { uint32_t uv = (uint32_t)v; b->ptr[off++] = uv & 0xff; b->ptr[off++] = (uv >> 8)&0xff; b->ptr[off++] = (uv >> 16)&0xff; b->ptr[off++] = (uv >> 24)&0xff; } #define MAXUNICODE 0x10FFFF static int utf8_copy(const char *s, char *d, size_t limit) { static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF}; unsigned int c = s[0]; unsigned int res = 0; if (limit < 1) return 0; d[0] = s[0]; if (c < 0x80) { return 1; } else { int count = 0; while (c & 0x40) { int cc = s[++count]; if (limit <= count || (cc & 0xC0) != 0x80) return 0; d[count] = s[count]; res = (res << 6) | (cc & 0x3F); c <<= 1; } res |= ((c & 0x7F) << (count * 5)); if (count > 3 || res > MAXUNICODE || res <= limits[count]) return 0; return count+1; } } static void write_string(struct bson *b, lua_State *L, const char *key, size_t sz) { bson_reserve(b,sz+1); char *dst = (char *)(b->ptr + b->size); const char *src = key; size_t n = sz; while(n > 0) { int c = utf8_copy(src, dst, n); if (c == 0) { luaL_error(L, "Invalid utf8 string"); } src += c; dst += c; n -= c; } b->ptr[b->size+sz] = '\0'; b->size+=sz+1; } static inline int reserve_length(struct bson *b) { int sz = b->size; bson_reserve(b,4); b->size +=4; return sz; } static inline void write_int64(struct bson *b, int64_t v) { uint64_t uv = (uint64_t)v; int i; bson_reserve(b,8); for (i=0;i<64;i+=8) { b->ptr[b->size++] = (uv>>i) & 0xff; } } static inline void write_double(struct bson *b, lua_Number d) { union { double d; uint64_t i; } v; v.d = d; int i; bson_reserve(b,8); for (i=0;i<64;i+=8) { b->ptr[b->size++] = (v.i>>i) & 0xff; } } static inline void append_key(struct bson *bs, lua_State *L, int type, const char *key, size_t sz) { write_byte(bs, type); write_string(bs, L, key, sz); } static inline int is_32bit(int64_t v) { return v >= INT32_MIN && v <= INT32_MAX; } static void append_number(struct bson *bs, lua_State *L, const char *key, size_t sz) { if (lua_isinteger(L, -1)) { int64_t i = lua_tointeger(L, -1); if (is_32bit(i)) { append_key(bs, L, BSON_INT32, key, sz); write_int32(bs, i); } else { append_key(bs, L, BSON_INT64, key, sz); write_int64(bs, i); } } else { lua_Number d = lua_tonumber(L,-1); append_key(bs, L, BSON_REAL, key, sz); write_double(bs, d); } } static void append_table(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth); static void write_binary(struct bson *b, const void * buffer, size_t sz) { int length = reserve_length(b); bson_reserve(b,sz); memcpy(b->ptr + b->size, buffer, sz); // include sub type b->size+=sz; write_length(b, sz-1, length); // not include sub type } static void append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) { int vt = lua_type(L,-1); switch(vt) { case LUA_TNUMBER: append_number(bs, L, key, sz); break; case LUA_TLIGHTUSERDATA: case LUA_TUSERDATA: { append_key(bs, L, BSON_DOCUMENT, key, sz); int32_t * doc = (int32_t*)lua_touserdata(L,-1); int32_t sz = *doc; bson_reserve(bs,sz); memcpy(bs->ptr + bs->size, doc, sz); bs->size += sz; break; } case LUA_TSTRING: { size_t len; const char * str = lua_tolstring(L,-1,&len); if (len > 1 && str[0]==0) { int subt = (uint8_t)str[1]; append_key(bs, L, subt, key, sz); switch(subt) { case BSON_BINARY: write_binary(bs, str+2, len-2); break; case BSON_OBJECTID: if (len != 2+12) { luaL_error(L, "Invalid object id %s", str+2); } // go though case BSON_JSCODE: case BSON_DBPOINTER: case BSON_SYMBOL: case BSON_CODEWS: bson_reserve(bs,len-2); memcpy(bs->ptr + bs->size, str+2, len-2); bs->size += len-2; break; case BSON_DATE: { if (len != 2+4) { luaL_error(L, "Invalid date"); } const uint32_t * ts = (const uint32_t *)(str + 2); int64_t v = (int64_t)*ts * 1000; write_int64(bs, v); break; } case BSON_TIMESTAMP: { if (len != 2+8) { luaL_error(L, "Invalid timestamp"); } const uint32_t * inc = (const uint32_t *)(str + 2); const uint32_t * ts = (const uint32_t *)(str + 6); write_int32(bs, *inc); write_int32(bs, *ts); break; } case BSON_REGEX: { str+=2; len-=3; size_t i; for (i=0;isize - length, length); } static void pack_dict_data(lua_State *L, struct bson *b, int depth, int kt) { const char * key = NULL; size_t sz; switch(kt) { case LUA_TNUMBER: luaL_error(L, "Bson dictionary's key can't be number"); break; case LUA_TSTRING: key = lua_tolstring(L,-2,&sz); append_one(b, L, key, sz, depth); lua_pop(L,1); break; default: luaL_error(L, "Invalid key type : %s", lua_typename(L, kt)); return; } } static void pack_simple_dict(lua_State *L, struct bson *b, int depth) { int length = reserve_length(b); lua_pushnil(L); while(lua_next(L,-2) != 0) { int kt = lua_type(L, -2); pack_dict_data(L, b, depth, kt); } write_byte(b,0); write_length(b, b->size - length, length); } static void pack_meta_dict(lua_State *L, struct bson *b, int depth) { int length = reserve_length(b); lua_pushvalue(L, -2); // push meta_obj lua_call(L, 1, 3); // call __pairs_func => next_func, t_data, first_k for(;;) { lua_pushvalue(L, -2); // copy data lua_pushvalue(L, -2); // copy k lua_copy(L, -5, -3); // copy next_func replace old_k lua_call(L, 2, 2); // call next_func int kt = lua_type(L, -2); if (kt == LUA_TNIL) { lua_pop(L, 4); // pop all k, v, next_func, obj break; } pack_dict_data(L, b, depth, kt); } write_byte(b,0); write_length(b, b->size - length, length); } static bool is_rawarray(lua_State *L) { lua_pushnil(L); if (lua_next(L, -2) == 0) { // empty table return false; } lua_Integer firstkey = lua_isinteger(L, -2) ? lua_tointeger(L, -2) : 0; lua_pop(L, 2); if (firstkey <= 1) { return firstkey > 0; } return firstkey <= lua_rawlen(L, -1); } static void append_table(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) { if (depth > MAX_DEPTH) { luaL_error(L, "Too depth while encoding bson"); } luaL_checkstack(L, 16, NULL); // reserve enough stack space to pack table if (luaL_getmetafield(L, -1, "__len") != LUA_TNIL) { lua_pushvalue(L, -2); lua_call(L, 1, 1); if (!lua_isinteger(L, -1)) { luaL_error(L, "__len should return integer"); } size_t len = lua_tointeger(L, -1); lua_pop(L, 1); append_key(bs, L, BSON_ARRAY, key, sz); pack_array(L, bs, depth, len); } else if (luaL_getmetafield(L, -1, "__pairs") != LUA_TNIL) { append_key(bs, L, BSON_DOCUMENT, key, sz); pack_meta_dict(L, bs, depth); } else if (is_rawarray(L)) { append_key(bs, L, BSON_ARRAY, key, sz); pack_array(L, bs, depth, lua_rawlen(L, -1)); } else { append_key(bs, L, BSON_DOCUMENT, key, sz); pack_simple_dict(L, bs, depth); } } static void pack_ordered_dict(lua_State *L, struct bson *b, int n, int depth) { int length = reserve_length(b); int i; size_t sz; // the first key is at index n const char * key = lua_tolstring(L, n, &sz); for (i=0;isize - length, length); } static int ltostring(lua_State *L) { size_t sz = lua_rawlen(L, 1); void * ud = lua_touserdata(L,1); lua_pushlstring(L, (const char*)ud, sz); return 1; } static int llen(lua_State *L) { size_t sz = lua_rawlen(L, 1); lua_pushinteger(L, sz); return 1; } static void make_object(lua_State *L, int type, const void * ptr, size_t len) { luaL_Buffer b; luaL_buffinit(L, &b); luaL_addchar(&b, 0); luaL_addchar(&b, type); luaL_addlstring(&b, (const char*)ptr, len); luaL_pushresult(&b); } static void unpack_dict(lua_State *L, struct bson_reader *br, bool array) { luaL_checkstack(L, 16, NULL); // reserve enough stack space to unpack table int sz = read_int32(L, br); const void * bytes = read_bytes(L, br, sz-5); struct bson_reader t = { (const uint8_t*)bytes, sz-5 }; int end = read_byte(L, br); if (end != '\0') { luaL_error(L, "Invalid document end"); } lua_newtable(L); for (;;) { if (t.size == 0) break; int bt = read_byte(L, &t); size_t klen = 0; const char * key = read_cstring(L, &t, &klen); if (array) { int id = strtol(key, NULL, 10) + 1; lua_pushinteger(L,id); } else { lua_pushlstring(L, key, klen); } switch (bt) { case BSON_REAL: lua_pushnumber(L, read_double(L, &t)); break; case BSON_BOOLEAN: lua_pushboolean(L, read_byte(L, &t)); break; case BSON_STRING: { int sz = read_int32(L, &t); if (sz <= 0) { luaL_error(L, "Invalid bson string , length = %d", sz); } lua_pushlstring(L, (const char*)read_bytes(L, &t, sz), sz-1); break; } case BSON_DOCUMENT: unpack_dict(L, &t, false); break; case BSON_ARRAY: unpack_dict(L, &t, true); break; case BSON_BINARY: { int sz = read_int32(L, &t); int subtype = read_byte(L, &t); luaL_Buffer b; luaL_buffinit(L, &b); luaL_addchar(&b, 0); luaL_addchar(&b, BSON_BINARY); luaL_addchar(&b, subtype); luaL_addlstring(&b, (const char*)read_bytes(L, &t, sz), sz); luaL_pushresult(&b); break; } case BSON_OBJECTID: make_object(L, BSON_OBJECTID, read_bytes(L, &t, 12), 12); break; case BSON_DATE: { int64_t date = read_int64(L, &t); uint32_t v = date / 1000; make_object(L, BSON_DATE, &v, 4); break; } case BSON_MINKEY: case BSON_MAXKEY: case BSON_NULL: { char key[] = { 0, (char)bt }; lua_pushlstring(L, key, sizeof(key)); break; } case BSON_REGEX: { size_t rlen1=0; size_t rlen2=0; const char * r1 = read_cstring(L, &t, &rlen1); const char * r2 = read_cstring(L, &t, &rlen2); luaL_Buffer b; luaL_buffinit(L, &b); luaL_addchar(&b, 0); luaL_addchar(&b, BSON_REGEX); luaL_addlstring(&b, r1, rlen1); luaL_addchar(&b,0); luaL_addlstring(&b, r2, rlen2); luaL_addchar(&b,0); luaL_pushresult(&b); break; } case BSON_INT32: lua_pushinteger(L, read_int32(L, &t)); break; case BSON_TIMESTAMP: { int32_t inc = read_int32(L, &t); int32_t ts = read_int32(L, &t); luaL_Buffer b; luaL_buffinit(L, &b); luaL_addchar(&b, 0); luaL_addchar(&b, BSON_TIMESTAMP); luaL_addlstring(&b, (const char *)&inc, 4); luaL_addlstring(&b, (const char *)&ts, 4); luaL_pushresult(&b); break; } case BSON_INT64: lua_pushinteger(L, read_int64(L, &t)); break; case BSON_DBPOINTER: { const void * ptr = t.ptr; int sz = read_int32(L, &t); read_bytes(L, &t, sz+12); make_object(L, BSON_DBPOINTER, ptr, sz + 16); break; } case BSON_JSCODE: case BSON_SYMBOL: { const void * ptr = t.ptr; int sz = read_int32(L, &t); read_bytes(L, &t, sz); make_object(L, bt, ptr, sz + 4); break; } case BSON_CODEWS: { const void * ptr = t.ptr; int sz = read_int32(L, &t); read_bytes(L, &t, sz-4); make_object(L, bt, ptr, sz); break; } default: // unsupported luaL_error(L, "Invalid bson type : %d", bt); lua_pop(L,1); continue; } lua_rawset(L,-3); } } static int lmakeindex(lua_State *L) { int32_t *bson = (int32_t*)luaL_checkudata(L,1,"bson"); const uint8_t * start = (const uint8_t *)bson; struct bson_reader br = { start+4, get_length(start) - 5 }; lua_newtable(L); for (;;) { if (br.size == 0) break; int bt = read_byte(L, &br); size_t klen = 0; const char * key = read_cstring(L, &br, &klen); int field_size = 0; switch (bt) { case BSON_INT64: case BSON_TIMESTAMP: case BSON_DATE: case BSON_REAL: field_size = 8; break; case BSON_BOOLEAN: field_size = 1; break; case BSON_JSCODE: case BSON_SYMBOL: case BSON_STRING: { int sz = read_int32(L, &br); read_bytes(L, &br, sz); break; } case BSON_CODEWS: case BSON_ARRAY: case BSON_DOCUMENT: { int sz = read_int32(L, &br); read_bytes(L, &br, sz-4); break; } case BSON_BINARY: { int sz = read_int32(L, &br); read_bytes(L, &br, sz+1); break; } case BSON_OBJECTID: field_size = 12; break; case BSON_MINKEY: case BSON_MAXKEY: case BSON_NULL: break; case BSON_REGEX: { size_t rlen1=0; size_t rlen2=0; read_cstring(L, &br, &rlen1); read_cstring(L, &br, &rlen2); break; } case BSON_INT32: field_size = 4; break; case BSON_DBPOINTER: { int sz = read_int32(L, &br); read_bytes(L, &br, sz+12); break; } default: // unsupported luaL_error(L, "Invalid bson type : %d", bt); lua_pop(L,1); continue; } if (field_size > 0) { int id = bt | (int)(br.ptr - start) << BSON_TYPE_SHIFT; read_bytes(L, &br, field_size); lua_pushlstring(L, key, klen); lua_pushinteger(L,id); lua_rawset(L,-3); } } lua_setiuservalue(L,1,1); lua_settop(L,1); return 1; } static void replace_object(lua_State *L, int type, struct bson * bs) { size_t len = 0; const char * data = luaL_checklstring(L,3, &len); if (len < 6 || data[0] != 0 || data[1] != type) { luaL_error(L, "Type mismatch, need bson type %d", type); } switch (type) { case BSON_OBJECTID: if (len != 2+12) { luaL_error(L, "Invalid object id"); } memcpy(bs->ptr, data+2, 12); break; case BSON_DATE: { if (len != 2+4) { luaL_error(L, "Invalid date"); } const uint32_t * ts = (const uint32_t *)(data + 2); int64_t v = (int64_t)*ts * 1000; write_int64(bs, v); break; } case BSON_TIMESTAMP: { if (len != 2+8) { luaL_error(L, "Invalid timestamp"); } const uint32_t * inc = (const uint32_t *)(data + 2); const uint32_t * ts = (const uint32_t *)(data + 6); write_int32(bs, *inc); write_int32(bs, *ts); break; } } } static int lreplace(lua_State *L) { lua_getiuservalue(L,1,1); if (!lua_istable(L,-1)) { return luaL_error(L, "call makeindex first"); } lua_pushvalue(L,2); if (lua_rawget(L, -2) != LUA_TNUMBER) { return luaL_error(L, "Can't replace key : %s", lua_tostring(L,2)); } int id = lua_tointeger(L, -1); int type = id & ((1<<(BSON_TYPE_SHIFT)) - 1); int offset = id >> BSON_TYPE_SHIFT; uint8_t * start = (uint8_t*)lua_touserdata(L,1); struct bson b = { 0,16, start + offset }; switch (type) { case BSON_REAL: write_double(&b, luaL_checknumber(L, 3)); break; case BSON_BOOLEAN: write_byte(&b, lua_toboolean(L,3)); break; case BSON_OBJECTID: case BSON_DATE: case BSON_TIMESTAMP: replace_object(L, type, &b); break; case BSON_INT32: { if (!lua_isinteger(L, 3)) { luaL_error(L, "%f must be a 32bit integer ", lua_tonumber(L, 3)); } int32_t i = lua_tointeger(L,3); write_int32(&b, i); break; } case BSON_INT64: { if (!lua_isinteger(L, 3)) { luaL_error(L, "%f must be a 64bit integer ", lua_tonumber(L, 3)); } int64_t i = lua_tointeger(L,3); write_int64(&b, i); break; } default: luaL_error(L, "Can't replace type %d", type); break; } return 0; } static int ldecode(lua_State *L) { const int32_t * data = (const int32_t*)lua_touserdata(L,1); if (data == NULL) { return 0; } const uint8_t * b = (const uint8_t *)data; int32_t len = get_length(b); struct bson_reader br = { b , len }; unpack_dict(L, &br, false); return 1; } static void bson_meta(lua_State *L) { if (luaL_newmetatable(L, "bson")) { luaL_Reg l[] = { { "decode", ldecode }, { "makeindex", lmakeindex }, { NULL, NULL }, }; luaL_newlib(L,l); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, ltostring); lua_setfield(L, -2, "__tostring"); lua_pushcfunction(L, llen); lua_setfield(L, -2, "__len"); lua_pushcfunction(L, lreplace); lua_setfield(L, -2, "__newindex"); } lua_setmetatable(L, -2); } static int encode_bson(lua_State *L) { struct bson *b = (struct bson*)lua_touserdata(L, 2); lua_settop(L, 1); if (luaL_getmetafield(L, -1, "__pairs") != LUA_TNIL) { pack_meta_dict(L, b, 0); } else { pack_simple_dict(L, b, 0); } void * ud = lua_newuserdatauv(L, b->size, 1); memcpy(ud, b->ptr, b->size); return 1; } static int lencode(lua_State *L) { struct bson b; lua_settop(L,1); luaL_checktype(L, 1, LUA_TTABLE); bson_create(&b); lua_pushcfunction(L, encode_bson); lua_pushvalue(L, 1); lua_pushlightuserdata(L, &b); if (lua_pcall(L, 2, 1, 0) != LUA_OK) { bson_destroy(&b); return lua_error(L); } bson_destroy(&b); bson_meta(L); return 1; } static int lto_lightuserdata(lua_State *L) { void *p = lua_touserdata(L, 1); lua_pushlightuserdata(L, p); return 1; } static int encode_bson_byorder(lua_State *L) { int n = lua_gettop(L); struct bson *b = (struct bson*)lua_touserdata(L, n); lua_settop(L, --n); pack_ordered_dict(L, b, n, 0); lua_settop(L,0); void * ud = lua_newuserdatauv(L, b->size, 1); memcpy(ud, b->ptr, b->size); return 1; } static int lencode_order(lua_State *L) { struct bson b; int n = lua_gettop(L); if (n%2 != 0) { return luaL_error(L, "Invalid ordered dict"); } bson_create(&b); lua_pushvalue(L, 1); // copy the first arg to n lua_pushcfunction(L, encode_bson_byorder); lua_replace(L, 1); lua_pushlightuserdata(L, &b); if (lua_pcall(L, n+1, 1, 0) != LUA_OK) { bson_destroy(&b); return lua_error(L); } bson_destroy(&b); bson_meta(L); return 1; } static int ldate(lua_State *L) { int d = luaL_checkinteger(L,1); luaL_Buffer b; luaL_buffinit(L, &b); luaL_addchar(&b, 0); luaL_addchar(&b, BSON_DATE); luaL_addlstring(&b, (const char *)&d, sizeof(d)); luaL_pushresult(&b); return 1; } static int lint64(lua_State *L) { int64_t d = luaL_checkinteger(L, 1); luaL_Buffer b; luaL_buffinit(L, &b); luaL_addchar(&b, 0); luaL_addchar(&b, BSON_INT64); luaL_addlstring(&b, (const char *)&d, sizeof(d)); luaL_pushresult(&b); return 1; } static int ltimestamp(lua_State *L) { int d = luaL_checkinteger(L,1); luaL_Buffer b; luaL_buffinit(L, &b); luaL_addchar(&b, 0); luaL_addchar(&b, BSON_TIMESTAMP); if (lua_isnoneornil(L,2)) { static uint32_t inc = 0; luaL_addlstring(&b, (const char *)&inc, sizeof(inc)); ++inc; } else { uint32_t i = (uint32_t)lua_tointeger(L,2); luaL_addlstring(&b, (const char *)&i, sizeof(i)); } luaL_addlstring(&b, (const char *)&d, sizeof(d)); luaL_pushresult(&b); return 1; } static int lregex(lua_State *L) { luaL_checkstring(L,1); if (lua_gettop(L) < 2) { lua_pushliteral(L,""); } luaL_Buffer b; luaL_buffinit(L, &b); luaL_addchar(&b, 0); luaL_addchar(&b, BSON_REGEX); lua_pushvalue(L,1); luaL_addvalue(&b); luaL_addchar(&b,0); lua_pushvalue(L,2); luaL_addvalue(&b); luaL_addchar(&b,0); luaL_pushresult(&b); return 1; } static int lbinary(lua_State *L) { lua_settop(L,1); luaL_Buffer b; luaL_buffinit(L, &b); luaL_addchar(&b, 0); luaL_addchar(&b, BSON_BINARY); luaL_addchar(&b, 0); // sub type lua_pushvalue(L,1); luaL_addvalue(&b); luaL_pushresult(&b); return 1; } static int lsubtype(lua_State *L, int subtype, const uint8_t * buf, size_t sz) { switch(subtype) { case BSON_BINARY: lua_pushvalue(L, lua_upvalueindex(6)); lua_pushlstring(L, (const char *)buf+1, sz-1); lua_pushinteger(L, buf[0]); return 3; case BSON_OBJECTID: { if (sz != 12) { return luaL_error(L, "Invalid object id"); } char oid[24]; int i; const uint8_t * id = buf; static const char *hex = "0123456789abcdef"; for (i=0;i<12;i++) { oid[i*2] = hex[id[i] >> 4]; oid[i*2+1] = hex[id[i] & 0xf]; } lua_pushvalue(L, lua_upvalueindex(7)); lua_pushlstring(L, oid, 24); return 2; } case BSON_DATE: { if (sz != 4) { return luaL_error(L, "Invalid date"); } int d = *(const int *)buf; lua_pushvalue(L, lua_upvalueindex(9)); lua_pushinteger(L, d); return 2; } case BSON_TIMESTAMP: { if (sz != 8) { return luaL_error(L, "Invalid timestamp"); } const uint32_t * ts = (const uint32_t *)buf; lua_pushvalue(L, lua_upvalueindex(8)); lua_pushinteger(L, (lua_Integer)ts[1]); lua_pushinteger(L, (lua_Integer)ts[0]); return 3; } case BSON_REGEX: { --sz; size_t i; const uint8_t *str = buf; for (i=0;i= 2) { return lsubtype(L, (uint8_t)str[1], (const uint8_t *)str+2, len-2); } else { type = 5; break; } } default: return luaL_error(L, "Invalid type %s",lua_typename(L,t)); } lua_pushvalue(L, lua_upvalueindex(type)); lua_pushvalue(L,1); return 2; } static void typeclosure(lua_State *L) { static const char * typename_[] = { "number", // 1 "boolean", // 2 "table", // 3 "nil", // 4 "string", // 5 "binary", // 6 "objectid", // 7 "timestamp", // 8 "date", // 9 "regex", // 10 "minkey", // 11 "maxkey", // 12 "int64", // 13 "unsupported", // 14 }; int i; int n = sizeof(typename_)/sizeof(typename_[0]); for (i=0;i>2)+hostname[i]); } h ^= i; } oid_header[0] = h & 0xff; oid_header[1] = (h>>8) & 0xff; oid_header[2] = (h>>16) & 0xff; oid_header[3] = pid & 0xff; oid_header[4] = (pid >> 8) & 0xff; unsigned long c = h ^ time(NULL) ^ (uintptr_t)&h; if (c == 0) { c = 1; } ATOM_STORE(&oid_counter, c); } static inline int hextoint(char c) { if (c>='0' && c<='9') return c-'0'; if (c>='a' && c<='z') return c-'a'+10; if (c>='A' && c<='Z') return c-'A'+10; return 0; } static int lobjectid(lua_State *L) { uint8_t oid[14] = { 0, BSON_OBJECTID }; if (lua_isstring(L,1)) { size_t len; const char * str = lua_tolstring(L,1,&len); if (len != 24) { return luaL_error(L, "Invalid objectid %s", str); } int i; for (i=0;i<12;i++) { oid[i+2] = hextoint(str[i*2]) << 4 | hextoint(str[i*2+1]); } } else { time_t ti = time(NULL); // old_counter is a static var, use atom inc. uint32_t id = ATOM_FINC(&oid_counter); oid[2] = (ti>>24) & 0xff; oid[3] = (ti>>16) & 0xff; oid[4] = (ti>>8) & 0xff; oid[5] = ti & 0xff; memcpy(oid+6 , oid_header, 5); oid[11] = (id>>16) & 0xff; oid[12] = (id>>8) & 0xff; oid[13] = id & 0xff; } lua_pushlstring( L, (const char *)oid, 14); return 1; } LUAMOD_API int luaopen_bson(lua_State *L) { luaL_checkversion(L); int i; for (i=0;i #include #include #include #include #include #include #include #include #include #include #include #include #define CACHE_SIZE 0x1000 static int lconnect(lua_State *L) { const char * addr = luaL_checkstring(L, 1); int port = luaL_checkinteger(L, 2); int fd = socket(AF_INET,SOCK_STREAM,0); struct sockaddr_in my_addr; my_addr.sin_addr.s_addr=inet_addr(addr); my_addr.sin_family=AF_INET; my_addr.sin_port=htons(port); int r = connect(fd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr_in)); if (r == -1) { return luaL_error(L, "Connect %s %d failed", addr, port); } int flag = fcntl(fd, F_GETFL, 0); fcntl(fd, F_SETFL, flag | O_NONBLOCK); lua_pushinteger(L, fd); return 1; } static int lclose(lua_State *L) { int fd = luaL_checkinteger(L, 1); close(fd); return 0; } static void block_send(lua_State *L, int fd, const char * buffer, int sz) { while(sz > 0) { int r = send(fd, buffer, sz, 0); if (r < 0) { if (errno == EAGAIN || errno == EINTR) continue; luaL_error(L, "socket error: %s", strerror(errno)); } buffer += r; sz -= r; } } /* integer fd string message */ static int lsend(lua_State *L) { size_t sz = 0; int fd = luaL_checkinteger(L,1); const char * msg = luaL_checklstring(L, 2, &sz); block_send(L, fd, msg, (int)sz); return 0; } /* intger fd string last table result return boolean (true: data, false: block, nil: close) string last */ struct socket_buffer { void * buffer; int sz; }; static int lrecv(lua_State *L) { int fd = luaL_checkinteger(L,1); char buffer[CACHE_SIZE]; int r = recv(fd, buffer, CACHE_SIZE, 0); if (r == 0) { lua_pushliteral(L, ""); // close return 1; } if (r < 0) { if (errno == EAGAIN || errno == EINTR) { return 0; } luaL_error(L, "socket error: %s", strerror(errno)); } lua_pushlstring(L, buffer, r); return 1; } static int lusleep(lua_State *L) { int n = luaL_checknumber(L, 1); usleep(n); return 0; } // quick and dirty none block stdin readline #define QUEUE_SIZE 1024 struct queue { pthread_mutex_t lock; int head; int tail; char * queue[QUEUE_SIZE]; }; static void * readline_stdin(void * arg) { struct queue * q = arg; char tmp[1024]; while (!feof(stdin)) { if (fgets(tmp,sizeof(tmp),stdin) == NULL) { // read stdin failed exit(1); } int n = strlen(tmp) -1; char * str = malloc(n+1); memcpy(str, tmp, n); str[n] = 0; pthread_mutex_lock(&q->lock); q->queue[q->tail] = str; if (++q->tail >= QUEUE_SIZE) { q->tail = 0; } if (q->head == q->tail) { // queue overflow exit(1); } pthread_mutex_unlock(&q->lock); } return NULL; } static int lreadstdin(lua_State *L) { struct queue *q = lua_touserdata(L, lua_upvalueindex(1)); pthread_mutex_lock(&q->lock); if (q->head == q->tail) { pthread_mutex_unlock(&q->lock); return 0; } char * str = q->queue[q->head]; if (++q->head >= QUEUE_SIZE) { q->head = 0; } pthread_mutex_unlock(&q->lock); lua_pushstring(L, str); free(str); return 1; } static int lshutdown(lua_State *L) { int fd = luaL_checkinteger(L,1); const char *mode = luaL_checkstring(L,2); int v = 0; int i; int read = 1; int write = 2; for (i=0;mode[i];i++) { switch(mode[i]) { case 'r': v |= read; break; case 'w': v |= write; break; default: return luaL_error(L, "Invalid mode %c", mode[i]); } } if (v == 0) { return luaL_error(L, "mode should be r or/and w"); } if (v == read) v = SHUT_RD; else if (v == write) v = SHUT_WR; else v = SHUT_RDWR; printf("SHUTDOWN %d %d\n", fd, v); shutdown(fd, v); return 0; } LUAMOD_API int luaopen_client_socket(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "connect", lconnect }, { "recv", lrecv }, { "send", lsend }, { "shutdown", lshutdown }, { "close", lclose }, { "usleep", lusleep }, { NULL, NULL }, }; luaL_newlib(L, l); struct queue * q = lua_newuserdata(L, sizeof(*q)); memset(q, 0, sizeof(*q)); pthread_mutex_init(&q->lock, NULL); lua_pushcclosure(L, lreadstdin, 1); lua_setfield(L, -2, "readstdin"); pthread_t pid ; pthread_create(&pid, NULL, readline_stdin, q); return 1; } ================================================ FILE: lualib-src/lua-cluster.c ================================================ #define LUA_LIB #include #include #include #include #include #include "skynet.h" /* uint32_t/string addr uint32_t/session session lightuserdata msg uint32_t sz return string request uint32_t next_session */ #define TEMP_LENGTH 0x8200 #define MULTI_PART 0x8000 static void fill_uint32(uint8_t * buf, uint32_t n) { buf[0] = n & 0xff; buf[1] = (n >> 8) & 0xff; buf[2] = (n >> 16) & 0xff; buf[3] = (n >> 24) & 0xff; } static void fill_header(lua_State *L, uint8_t *buf, int sz) { assert(sz < 0x10000); buf[0] = (sz >> 8) & 0xff; buf[1] = sz & 0xff; } /* The request package : first WORD is size of the package with big-endian DWORD in content is small-endian size <= 0x8000 (32K) and address is id WORD sz+9 BYTE 0 DWORD addr DWORD session PADDING msg(sz) size > 0x8000 and address is id WORD 13 BYTE 1 ; multireq , 0x41: multi push DWORD addr DWORD session DWORD sz size <= 0x8000 (32K) and address is string WORD sz+6+namelen BYTE 0x80 BYTE namelen STRING name DWORD session PADDING msg(sz) size > 0x8000 and address is string WORD 10 + namelen BYTE 0x81 ; 0xc1 : multi push BYTE namelen STRING name DWORD session DWORD sz multi req WORD sz + 5 BYTE 2/3 ; 2:multipart, 3:multipart end DWORD SESSION PADDING msgpart(sz) trace WORD stringsz + 1 BYTE 4 STRING tag */ static int packreq_number(lua_State *L, int session, void * msg, uint32_t sz, int is_push) { uint32_t addr = (uint32_t)lua_tointeger(L,1); uint8_t buf[TEMP_LENGTH]; if (sz < MULTI_PART) { fill_header(L, buf, sz+9); buf[2] = 0; fill_uint32(buf+3, addr); fill_uint32(buf+7, is_push ? 0 : (uint32_t)session); memcpy(buf+11,msg,sz); lua_pushlstring(L, (const char *)buf, sz+11); return 0; } else { int part = (sz - 1) / MULTI_PART + 1; fill_header(L, buf, 13); buf[2] = is_push ? 0x41 : 1; // multi push or request fill_uint32(buf+3, addr); fill_uint32(buf+7, (uint32_t)session); fill_uint32(buf+11, sz); lua_pushlstring(L, (const char *)buf, 15); return part; } } static int packreq_string(lua_State *L, int session, void * msg, uint32_t sz, int is_push) { size_t namelen = 0; const char *name = lua_tolstring(L, 1, &namelen); if (name == NULL || namelen < 1 || namelen > 255) { skynet_free(msg); if (name == NULL) { luaL_error(L, "name is not a string, it's a %s", lua_typename(L, lua_type(L, 1))); } else { luaL_error(L, "name length is invalid, must be between 1 and 255 characters: %s", name); } } uint8_t buf[TEMP_LENGTH]; if (sz < MULTI_PART) { fill_header(L, buf, sz+6+namelen); buf[2] = 0x80; buf[3] = (uint8_t)namelen; memcpy(buf+4, name, namelen); fill_uint32(buf+4+namelen, is_push ? 0 : (uint32_t)session); memcpy(buf+8+namelen,msg,sz); lua_pushlstring(L, (const char *)buf, sz+8+namelen); return 0; } else { int part = (sz - 1) / MULTI_PART + 1; fill_header(L, buf, 10+namelen); buf[2] = is_push ? 0xc1 : 0x81; // multi push or request buf[3] = (uint8_t)namelen; memcpy(buf+4, name, namelen); fill_uint32(buf+4+namelen, (uint32_t)session); fill_uint32(buf+8+namelen, sz); lua_pushlstring(L, (const char *)buf, 12+namelen); return part; } } static void packreq_multi(lua_State *L, int session, void * msg, uint32_t sz) { uint8_t buf[TEMP_LENGTH]; int part = (sz - 1) / MULTI_PART + 1; int i; char *ptr = msg; for (i=0;i MULTI_PART) { s = MULTI_PART; buf[2] = 2; } else { s = sz; buf[2] = 3; // the last multi part } fill_header(L, buf, s+5); fill_uint32(buf+3, (uint32_t)session); memcpy(buf+7, ptr, s); lua_pushlstring(L, (const char *)buf, s+7); lua_rawseti(L, -2, i+1); sz -= s; ptr += s; } } static int packrequest(lua_State *L, int is_push) { void *msg = lua_touserdata(L,3); if (msg == NULL) { return luaL_error(L, "Invalid request message"); } uint32_t sz = (uint32_t)luaL_checkinteger(L,4); int session = luaL_checkinteger(L,2); if (session <= 0) { skynet_free(msg); return luaL_error(L, "Invalid request session %d", session); } int addr_type = lua_type(L,1); int multipak; if (addr_type == LUA_TNUMBER) { multipak = packreq_number(L, session, msg, sz, is_push); } else { multipak = packreq_string(L, session, msg, sz, is_push); } uint32_t new_session = (uint32_t)session + 1; if (new_session > INT32_MAX) { new_session = 1; } lua_pushinteger(L, new_session); if (multipak) { lua_createtable(L, multipak, 0); packreq_multi(L, session, msg, sz); skynet_free(msg); return 3; } else { skynet_free(msg); return 2; } } static int lpackrequest(lua_State *L) { return packrequest(L, 0); } static int lpackpush(lua_State *L) { return packrequest(L, 1); } static int lpacktrace(lua_State *L) { size_t sz; const char * tag = luaL_checklstring(L, 1, &sz); if (sz > 0x8000) { return luaL_error(L, "trace tag is too long : %d", (int) sz); } uint8_t buf[TEMP_LENGTH]; buf[2] = 4; fill_header(L, buf, sz+1); memcpy(buf+3, tag, sz); lua_pushlstring(L, (const char *)buf, sz+3); return 1; } /* string packed message return uint32_t or string addr int session lightuserdata msg int sz boolean padding boolean is_push */ static inline uint32_t unpack_uint32(const uint8_t * buf) { return buf[0] | buf[1]<<8 | buf[2]<<16 | buf[3]<<24; } static void return_buffer(lua_State *L, const char * buffer, int sz) { void * ptr = skynet_malloc(sz); memcpy(ptr, buffer, sz); lua_pushlightuserdata(L, ptr); lua_pushinteger(L, sz); } static int unpackreq_number(lua_State *L, const uint8_t * buf, int sz) { if (sz < 9) { return luaL_error(L, "Invalid cluster message (size=%d)", sz); } uint32_t address = unpack_uint32(buf+1); uint32_t session = unpack_uint32(buf+5); lua_pushinteger(L, address); lua_pushinteger(L, session); return_buffer(L, (const char *)buf+9, sz-9); if (session == 0) { lua_pushnil(L); lua_pushboolean(L,1); // is_push, no response return 6; } return 4; } static int unpackmreq_number(lua_State *L, const uint8_t * buf, int sz, int is_push) { if (sz != 13) { return luaL_error(L, "Invalid cluster message size %d (multi req must be 13)", sz); } uint32_t address = unpack_uint32(buf+1); uint32_t session = unpack_uint32(buf+5); uint32_t size = unpack_uint32(buf+9); lua_pushinteger(L, address); lua_pushinteger(L, session); lua_pushnil(L); lua_pushinteger(L, size); lua_pushboolean(L, 1); // padding multi part lua_pushboolean(L, is_push); return 6; } static int unpackmreq_part(lua_State *L, const uint8_t * buf, int sz) { if (sz < 5) { return luaL_error(L, "Invalid cluster multi part message"); } int padding = (buf[0] == 2); uint32_t session = unpack_uint32(buf+1); lua_pushboolean(L, 0); // no address lua_pushinteger(L, session); return_buffer(L, (const char *)buf+5, sz-5); lua_pushboolean(L, padding); return 5; } static int unpacktrace(lua_State *L, const char * buf, int sz) { lua_pushlstring(L, buf + 1, sz - 1); return 1; } static int unpackreq_string(lua_State *L, const uint8_t * buf, int sz) { if (sz < 2) { return luaL_error(L, "Invalid cluster message (size=%d)", sz); } size_t namesz = buf[1]; if (sz < namesz + 6) { return luaL_error(L, "Invalid cluster message (size=%d)", sz); } lua_pushlstring(L, (const char *)buf+2, namesz); uint32_t session = unpack_uint32(buf + namesz + 2); lua_pushinteger(L, (uint32_t)session); return_buffer(L, (const char *)buf+2+namesz+4, sz - namesz - 6); if (session == 0) { lua_pushnil(L); lua_pushboolean(L,1); // is_push, no response return 6; } return 4; } static int unpackmreq_string(lua_State *L, const uint8_t * buf, int sz, int is_push) { if (sz < 2) { return luaL_error(L, "Invalid cluster message (size=%d)", sz); } size_t namesz = buf[1]; if (sz < namesz + 10) { return luaL_error(L, "Invalid cluster message (size=%d)", sz); } lua_pushlstring(L, (const char *)buf+2, namesz); uint32_t session = unpack_uint32(buf + namesz + 2); uint32_t size = unpack_uint32(buf + namesz + 6); lua_pushinteger(L, session); lua_pushnil(L); lua_pushinteger(L, size); lua_pushboolean(L, 1); // padding multipart lua_pushboolean(L, is_push); return 6; } static int lunpackrequest(lua_State *L) { int sz; const char *msg; if (lua_type(L, 1) == LUA_TLIGHTUSERDATA) { msg = (const char *)lua_touserdata(L, 1); sz = luaL_checkinteger(L, 2); } else { size_t ssz; msg = luaL_checklstring(L,1,&ssz); sz = (int)ssz; } if (sz == 0) return luaL_error(L, "Invalid req package. size == 0"); switch (msg[0]) { case 0: return unpackreq_number(L, (const uint8_t *)msg, sz); case 1: return unpackmreq_number(L, (const uint8_t *)msg, sz, 0); // request case '\x41': return unpackmreq_number(L, (const uint8_t *)msg, sz, 1); // push case 2: case 3: return unpackmreq_part(L, (const uint8_t *)msg, sz); case 4: return unpacktrace(L, msg, sz); case '\x80': return unpackreq_string(L, (const uint8_t *)msg, sz); case '\x81': return unpackmreq_string(L, (const uint8_t *)msg, sz, 0 ); // request case '\xc1': return unpackmreq_string(L, (const uint8_t *)msg, sz, 1 ); // push default: return luaL_error(L, "Invalid req package type %d", msg[0]); } } /* The response package : WORD size (big endian) DWORD session BYTE type 0: error 1: ok 2: multi begin 3: multi part 4: multi end PADDING msg type = 0, error msg type = 1, msg type = 2, DWORD size type = 3/4, msg */ /* int session boolean ok lightuserdata msg int sz return string response */ static int lpackresponse(lua_State *L) { uint32_t session = (uint32_t)luaL_checkinteger(L,1); // clusterd.lua:command.socket call lpackresponse, // and the msg/sz is return by skynet.rawcall , so don't free(msg) int ok = lua_toboolean(L,2); void * msg; size_t sz; if (lua_type(L,3) == LUA_TSTRING) { msg = (void *)lua_tolstring(L, 3, &sz); } else { msg = lua_touserdata(L,3); sz = (size_t)luaL_checkinteger(L, 4); } if (!ok) { if (sz > MULTI_PART) { // truncate the error msg if too long sz = MULTI_PART; } } else { if (sz > MULTI_PART) { // return int part = (sz - 1) / MULTI_PART + 1; lua_createtable(L, part+1, 0); uint8_t buf[TEMP_LENGTH]; // multi part begin fill_header(L, buf, 9); fill_uint32(buf+2, session); buf[6] = 2; fill_uint32(buf+7, (uint32_t)sz); lua_pushlstring(L, (const char *)buf, 11); lua_rawseti(L, -2, 1); char * ptr = msg; int i; for (i=0;i MULTI_PART) { s = MULTI_PART; buf[6] = 3; } else { s = sz; buf[6] = 4; } fill_header(L, buf, s+5); fill_uint32(buf+2, session); memcpy(buf+7,ptr,s); lua_pushlstring(L, (const char *)buf, s+7); lua_rawseti(L, -2, i+2); sz -= s; ptr += s; } return 1; } } uint8_t buf[TEMP_LENGTH]; fill_header(L, buf, sz+5); fill_uint32(buf+2, session); buf[6] = ok; memcpy(buf+7,msg,sz); lua_pushlstring(L, (const char *)buf, sz+7); return 1; } /* string packed response return integer session boolean ok string msg boolean padding */ static int lunpackresponse(lua_State *L) { size_t sz; const char * buf = luaL_checklstring(L, 1, &sz); if (sz < 5) { return 0; } uint32_t session = unpack_uint32((const uint8_t *)buf); lua_pushinteger(L, (lua_Integer)session); switch(buf[4]) { case 0: // error lua_pushboolean(L, 0); lua_pushlstring(L, buf+5, sz-5); return 3; case 1: // ok case 4: // multi end lua_pushboolean(L, 1); lua_pushlstring(L, buf+5, sz-5); return 3; case 2: // multi begin if (sz != 9) { return 0; } sz = unpack_uint32((const uint8_t *)buf+5); lua_pushboolean(L, 1); lua_pushinteger(L, sz); lua_pushboolean(L, 1); return 4; case 3: // multi part lua_pushboolean(L, 1); lua_pushlstring(L, buf+5, sz-5); lua_pushboolean(L, 1); return 4; default: return 0; } } /* table pointer sz push (pointer/sz) as string into table, and free pointer */ static int lappend(lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); int n = lua_rawlen(L, 1); if (lua_isnil(L, 2)) { lua_settop(L, 3); lua_seti(L, 1, n + 1); return 0; } void * buffer = lua_touserdata(L, 2); if (buffer == NULL) return luaL_error(L, "Need lightuserdata"); int sz = luaL_checkinteger(L, 3); lua_pushlstring(L, (const char *)buffer, sz); skynet_free((void *)buffer); lua_seti(L, 1, n+1); return 0; } static int lconcat(lua_State *L) { if (!lua_istable(L,1)) return 0; if (lua_geti(L,1,1) != LUA_TNUMBER) return 0; int sz = lua_tointeger(L,-1); lua_pop(L,1); char * buff = skynet_malloc(sz); int idx = 2; int offset = 0; while(lua_geti(L,1,idx) == LUA_TSTRING) { size_t s; const char * str = lua_tolstring(L, -1, &s); if (s+offset > sz) { skynet_free(buff); return 0; } memcpy(buff+offset, str, s); lua_pop(L,1); offset += s; ++idx; } if (offset != sz) { skynet_free(buff); return 0; } // buff/sz will send to other service, See clusterd.lua lua_pushlightuserdata(L, buff); lua_pushinteger(L, sz); return 2; } static int lisname(lua_State *L) { const char * name = lua_tostring(L, 1); if (name && name[0] == '@') { lua_pushboolean(L, 1); return 1; } return 0; } static int lnodename(lua_State *L) { pid_t pid = getpid(); char hostname[256]; if (gethostname(hostname, sizeof(hostname))==0) { int i; for (i=0; hostname[i]; i++) { if (hostname[i] <= ' ') hostname[i] = '_'; } lua_pushfstring(L, "%s%d", hostname, (int)pid); } else { lua_pushfstring(L, "noname%d", (int)pid); } return 1; } LUAMOD_API int luaopen_skynet_cluster_core(lua_State *L) { luaL_Reg l[] = { { "packrequest", lpackrequest }, { "packpush", lpackpush }, { "packtrace", lpacktrace }, { "unpackrequest", lunpackrequest }, { "packresponse", lpackresponse }, { "unpackresponse", lunpackresponse }, { "append", lappend }, { "concat", lconcat }, { "isname", lisname }, { "nodename", lnodename }, { NULL, NULL }, }; luaL_checkversion(L); luaL_newlib(L,l); return 1; } ================================================ FILE: lualib-src/lua-crypt.c ================================================ #define LUA_LIB #include #include #include #include #include #include #include #define PADDING_MODE_ISO7816_4 0 #define PADDING_MODE_PKCS7 1 #define PADDING_MODE_COUNT 2 #define SMALL_CHUNK 256 /* the eight DES S-boxes */ static uint32_t SB1[64] = { 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004, 0x00010000, 0x00000400, 0x01010400, 0x01010404, 0x00000400, 0x01000404, 0x01010004, 0x01000000, 0x00000004, 0x00000404, 0x01000400, 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404, 0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404, 0x00010404, 0x01000000, 0x00010000, 0x01010404, 0x00000004, 0x01010000, 0x01010400, 0x01000000, 0x01000000, 0x00000400, 0x01010004, 0x00010000, 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404, 0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404, 0x00010404, 0x01010400, 0x00000404, 0x01000400, 0x01000400, 0x00000000, 0x00010004, 0x00010400, 0x00000000, 0x01010004 }; static uint32_t SB2[64] = { 0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020, 0x80100020, 0x80008020, 0x80000020, 0x80108020, 0x80108000, 0x80000000, 0x80008000, 0x00100000, 0x00000020, 0x80100020, 0x00108000, 0x00100020, 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000, 0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000, 0x80100000, 0x00008020, 0x00000000, 0x00108020, 0x80100020, 0x00100000, 0x80008020, 0x80100000, 0x80108000, 0x00008000, 0x80100000, 0x80008000, 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000, 0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020, 0x80000020, 0x00100020, 0x00108000, 0x00000000, 0x80008000, 0x00008020, 0x80000000, 0x80100020, 0x80108020, 0x00108000 }; static uint32_t SB3[64] = { 0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000, 0x00020208, 0x08000200, 0x00020008, 0x08000008, 0x08000008, 0x00020000, 0x08020208, 0x00020008, 0x08020000, 0x00000208, 0x08000000, 0x00000008, 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208, 0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208, 0x00000200, 0x08000000, 0x08020200, 0x08000000, 0x00020008, 0x00000208, 0x00020000, 0x08020200, 0x08000200, 0x00000000, 0x00000200, 0x00020008, 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008, 0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208, 0x00020200, 0x08000008, 0x08020000, 0x08000208, 0x00000208, 0x08020000, 0x00020208, 0x00000008, 0x08020008, 0x00020200 }; static uint32_t SB4[64] = { 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081, 0x00800001, 0x00002001, 0x00000000, 0x00802000, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00800080, 0x00800001, 0x00000001, 0x00002000, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080, 0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080, 0x00802081, 0x00000081, 0x00800080, 0x00800001, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00000000, 0x00802000, 0x00002080, 0x00800080, 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001, 0x00802080, 0x00800081, 0x00002001, 0x00002080, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002000, 0x00802080 }; static uint32_t SB5[64] = { 0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100, 0x40000000, 0x02080000, 0x40080100, 0x00080000, 0x02000100, 0x40080100, 0x42000100, 0x42080000, 0x00080100, 0x40000000, 0x02000000, 0x40080000, 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100, 0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000, 0x42000000, 0x00080100, 0x00080000, 0x42000100, 0x00000100, 0x02000000, 0x40000000, 0x02080000, 0x42000100, 0x40080100, 0x02000100, 0x40000000, 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000, 0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000, 0x00000000, 0x40080000, 0x42000000, 0x00080100, 0x02000100, 0x40000100, 0x00080000, 0x00000000, 0x40080000, 0x02080100, 0x40000100 }; static uint32_t SB6[64] = { 0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010, 0x20404010, 0x00400000, 0x20004000, 0x00404010, 0x00400000, 0x20000010, 0x00400010, 0x20004000, 0x20000000, 0x00004010, 0x00000000, 0x00400010, 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010, 0x20400010, 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000, 0x20404000, 0x20000000, 0x20004000, 0x00000010, 0x20400010, 0x00404000, 0x20404010, 0x00400000, 0x00004010, 0x20000010, 0x00400000, 0x20004000, 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000, 0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010, 0x00004000, 0x20400000, 0x00404010, 0x00004000, 0x00400010, 0x20004010, 0x00000000, 0x20404000, 0x20000000, 0x00400010, 0x20004010 }; static uint32_t SB7[64] = { 0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802, 0x00200802, 0x04200800, 0x04200802, 0x00200000, 0x00000000, 0x04000002, 0x00000002, 0x04000000, 0x04200002, 0x00000802, 0x04000800, 0x00200802, 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002, 0x04200000, 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002, 0x04000000, 0x00200800, 0x04000000, 0x00200800, 0x00200000, 0x04000802, 0x04000802, 0x04200002, 0x04200002, 0x00000002, 0x00200002, 0x04000000, 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800, 0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800, 0x00000000, 0x00000002, 0x04200802, 0x00000000, 0x00200802, 0x04200000, 0x00000800, 0x04000002, 0x04000800, 0x00000800, 0x00200002 }; static uint32_t SB8[64] = { 0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040, 0x00000040, 0x10000000, 0x00040040, 0x10040000, 0x10041040, 0x00041000, 0x10041000, 0x00041040, 0x00001000, 0x00000040, 0x10040000, 0x10000040, 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000, 0x00001040, 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000, 0x00041040, 0x00040000, 0x00041040, 0x00040000, 0x10041000, 0x00001000, 0x00000040, 0x10040040, 0x00001000, 0x00041040, 0x10001000, 0x00000040, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040, 0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0x00000000, 0x10041040, 0x00041000, 0x00041000, 0x00001040, 0x00001040, 0x00040040, 0x10000000, 0x10041000 }; /* PC1: left and right halves bit-swap */ static uint32_t LHs[16] = { 0x00000000, 0x00000001, 0x00000100, 0x00000101, 0x00010000, 0x00010001, 0x00010100, 0x00010101, 0x01000000, 0x01000001, 0x01000100, 0x01000101, 0x01010000, 0x01010001, 0x01010100, 0x01010101 }; static uint32_t RHs[16] = { 0x00000000, 0x01000000, 0x00010000, 0x01010000, 0x00000100, 0x01000100, 0x00010100, 0x01010100, 0x00000001, 0x01000001, 0x00010001, 0x01010001, 0x00000101, 0x01000101, 0x00010101, 0x01010101, }; /* platform-independant 32-bit integer manipulation macros */ #define GET_UINT32(n,b,i) \ { \ (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ | ( (uint32_t) (b)[(i) + 1] << 16 ) \ | ( (uint32_t) (b)[(i) + 2] << 8 ) \ | ( (uint32_t) (b)[(i) + 3] ); \ } #define PUT_UINT32(n,b,i) \ { \ (b)[(i) ] = (uint8_t) ( (n) >> 24 ); \ (b)[(i) + 1] = (uint8_t) ( (n) >> 16 ); \ (b)[(i) + 2] = (uint8_t) ( (n) >> 8 ); \ (b)[(i) + 3] = (uint8_t) ( (n) ); \ } /* Initial Permutation macro */ #define DES_IP(X,Y) \ { \ T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \ T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \ T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \ T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \ Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF; \ T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T; \ X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF; \ } /* Final Permutation macro */ #define DES_FP(X,Y) \ { \ X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF; \ T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T; \ Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF; \ T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \ T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \ T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \ T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \ } /* DES round macro */ #define DES_ROUND(X,Y) \ { \ T = *SK++ ^ X; \ Y ^= SB8[ (T ) & 0x3F ] ^ \ SB6[ (T >> 8) & 0x3F ] ^ \ SB4[ (T >> 16) & 0x3F ] ^ \ SB2[ (T >> 24) & 0x3F ]; \ \ T = *SK++ ^ ((X << 28) | (X >> 4)); \ Y ^= SB7[ (T ) & 0x3F ] ^ \ SB5[ (T >> 8) & 0x3F ] ^ \ SB3[ (T >> 16) & 0x3F ] ^ \ SB1[ (T >> 24) & 0x3F ]; \ } /* DES key schedule */ static void des_main_ks( uint32_t SK[32], const uint8_t key[8] ) { int i; uint32_t X, Y, T; GET_UINT32( X, key, 0 ); GET_UINT32( Y, key, 4 ); /* Permuted Choice 1 */ T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4); T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T ); X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2) | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] ) | (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6) | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4); Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2) | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] ) | (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6) | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4); X &= 0x0FFFFFFF; Y &= 0x0FFFFFFF; /* calculate subkeys */ for( i = 0; i < 16; i++ ) { if( i < 2 || i == 8 || i == 15 ) { X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF; Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF; } else { X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF; Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF; } *SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000) | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000) | ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000) | ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000) | ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000) | ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000) | ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400) | ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100) | ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010) | ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004) | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001); *SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000) | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000) | ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000) | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000) | ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000) | ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000) | ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000) | ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400) | ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100) | ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011) | ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002); } } /* DES 64-bit block encryption/decryption */ static void des_crypt( const uint32_t SK[32], const uint8_t input[8], uint8_t output[8] ) { uint32_t X, Y, T; GET_UINT32( X, input, 0 ); GET_UINT32( Y, input, 4 ); DES_IP( X, Y ); DES_ROUND( Y, X ); DES_ROUND( X, Y ); DES_ROUND( Y, X ); DES_ROUND( X, Y ); DES_ROUND( Y, X ); DES_ROUND( X, Y ); DES_ROUND( Y, X ); DES_ROUND( X, Y ); DES_ROUND( Y, X ); DES_ROUND( X, Y ); DES_ROUND( Y, X ); DES_ROUND( X, Y ); DES_ROUND( Y, X ); DES_ROUND( X, Y ); DES_ROUND( Y, X ); DES_ROUND( X, Y ); DES_FP( Y, X ); PUT_UINT32( Y, output, 0 ); PUT_UINT32( X, output, 4 ); } static int lrandomkey(lua_State *L) { char tmp[8]; int i; char x = 0; for (i=0;i<8;i++) { tmp[i] = random() & 0xff; x ^= tmp[i]; } if (x==0) { tmp[0] |= 1; // avoid 0 } lua_pushlstring(L, tmp, 8); return 1; } static void padding_mode_table(lua_State *L) { // see macros PADDING_MODE_ISO7816_4, etc. const char * mode[] = { "iso7816_4", "pkcs7", }; int n = sizeof(mode) / sizeof(mode[0]); int i; lua_createtable(L,0,n); for (i=0;i= PADDING_MODE_COUNT) luaL_error(L, "Invalid padding mode %d", mode); } static void add_padding(lua_State *L, uint8_t buf[8], const uint8_t *src, int offset, int mode) { check_padding_mode(L, mode); if (offset >= 8) luaL_error(L, "Invalid padding"); memcpy(buf, src, offset); padding_add_func[mode](buf, offset); } static int remove_padding(lua_State *L, const uint8_t *last, int mode) { check_padding_mode(L, mode); return padding_remove_func[mode](last); } static void des_key(lua_State *L, uint32_t SK[32]) { size_t keysz = 0; const void * key = luaL_checklstring(L, 1, &keysz); if (keysz != 8) { luaL_error(L, "Invalid key size %d, need 8 bytes", (int)keysz); } des_main_ks(SK, (const uint8_t*)key); } static int ldesencode(lua_State *L) { uint32_t SK[32]; des_key(L, SK); size_t textsz = 0; const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 2, &textsz); size_t chunksz = (textsz + 8) & ~7; int padding_mode = luaL_optinteger(L, 3, PADDING_MODE_ISO7816_4); uint8_t tmp[SMALL_CHUNK]; uint8_t *buffer = tmp; if (chunksz > SMALL_CHUNK) { buffer = (uint8_t*)lua_newuserdatauv(L, chunksz, 0); } int i; for (i=0;i<(int)textsz-7;i+=8) { des_crypt(SK, text+i, buffer+i); } uint8_t tail[8]; add_padding(L, tail, text+i, textsz - i, padding_mode); des_crypt(SK, tail, buffer+i); lua_pushlstring(L, (const char *)buffer, chunksz); return 1; } static int ldesdecode(lua_State *L) { uint32_t ESK[32]; des_key(L, ESK); uint32_t SK[32]; int i; for( i = 0; i < 32; i += 2 ) { SK[i] = ESK[30 - i]; SK[i + 1] = ESK[31 - i]; } size_t textsz = 0; const uint8_t *text = (const uint8_t *)luaL_checklstring(L, 2, &textsz); if ((textsz & 7) || textsz == 0) { return luaL_error(L, "Invalid des crypt text length %d", (int)textsz); } int padding_mode = luaL_optinteger(L, 3, PADDING_MODE_ISO7816_4); uint8_t tmp[SMALL_CHUNK]; uint8_t *buffer = tmp; if (textsz > SMALL_CHUNK) { buffer = (uint8_t*)lua_newuserdatauv(L, textsz, 0); } for (i=0;i 8) { return luaL_error(L, "Invalid des crypt text"); } lua_pushlstring(L, (const char *)buffer, textsz - padding); return 1; } static void Hash(const char * str, int sz, uint8_t key[8]) { uint32_t djb_hash = 5381L; uint32_t js_hash = 1315423911L; int i; for (i=0;i> 2)); } key[0] = djb_hash & 0xff; key[1] = (djb_hash >> 8) & 0xff; key[2] = (djb_hash >> 16) & 0xff; key[3] = (djb_hash >> 24) & 0xff; key[4] = js_hash & 0xff; key[5] = (js_hash >> 8) & 0xff; key[6] = (js_hash >> 16) & 0xff; key[7] = (js_hash >> 24) & 0xff; } static int lhashkey(lua_State *L) { size_t sz = 0; const char * key = luaL_checklstring(L, 1, &sz); uint8_t realkey[8]; Hash(key,(int)sz,realkey); lua_pushlstring(L, (const char *)realkey, 8); return 1; } static int ltohex(lua_State *L) { static char hex[] = "0123456789abcdef"; size_t sz = 0; const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 1, &sz); char tmp[SMALL_CHUNK]; char *buffer = tmp; if (sz > SMALL_CHUNK/2) { buffer = (char*)lua_newuserdatauv(L, sz * 2, 0); } int i; for (i=0;i> 4]; buffer[i*2+1] = hex[text[i] & 0xf]; } lua_pushlstring(L, buffer, sz * 2); return 1; } #define HEX(v,c) { char tmp = (char) c; if (tmp >= '0' && tmp <= '9') { v = tmp-'0'; } else { v = tmp - 'a' + 10; } } static int lfromhex(lua_State *L) { size_t sz = 0; const char * text = luaL_checklstring(L, 1, &sz); if (sz & 1) { return luaL_error(L, "Invalid hex text size %d", (int)sz); } char tmp[SMALL_CHUNK]; char *buffer = tmp; if (sz > SMALL_CHUNK*2) { buffer = (char*)lua_newuserdatauv(L, sz / 2, 0); } int i; for (i=0;i 16 || low > 16) { return luaL_error(L, "Invalid hex text", text); } buffer[i/2] = hi<<4 | low; } lua_pushlstring(L, buffer, i/2); return 1; } // Constants are the integer part of the sines of integers (in radians) * 2^32. static const uint32_t k[64] = { 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee , 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501 , 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be , 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821 , 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa , 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8 , 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed , 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a , 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c , 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70 , 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05 , 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665 , 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039 , 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1 , 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1 , 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 }; // r specifies the per-round shift amounts static const uint32_t r[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21}; // leftrotate function definition #define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c)))) static void digest_md5(uint32_t w[16], uint32_t result[4]) { uint32_t a, b, c, d, f, g, temp; int i; a = 0x67452301u; b = 0xefcdab89u; c = 0x98badcfeu; d = 0x10325476u; for(i = 0; i<64; i++) { if (i < 16) { f = (b & c) | ((~b) & d); g = i; } else if (i < 32) { f = (d & b) | ((~d) & c); g = (5*i + 1) % 16; } else if (i < 48) { f = b ^ c ^ d; g = (3*i + 5) % 16; } else { f = c ^ (b | (~d)); g = (7*i) % 16; } temp = d; d = c; c = b; b = b + LEFTROTATE((a + f + k[i] + w[g]), r[i]); a = temp; } result[0] = a; result[1] = b; result[2] = c; result[3] = d; } // hmac64 use md5 algorithm without padding, and the result is (c^d .. a^b) static void hmac(uint32_t x[2], uint32_t y[2], uint32_t result[2]) { uint32_t w[16]; uint32_t r[4]; int i; for (i=0;i<16;i+=4) { w[i] = x[1]; w[i+1] = x[0]; w[i+2] = y[1]; w[i+3] = y[0]; } digest_md5(w,r); result[0] = r[2]^r[3]; result[1] = r[0]^r[1]; } static void hmac_md5(uint32_t x[2], uint32_t y[2], uint32_t result[2]) { uint32_t w[16]; uint32_t r[4]; int i; for (i=0;i<12;i+=4) { w[i] = x[0]; w[i+1] = x[1]; w[i+2] = y[0]; w[i+3] = y[1]; } w[12] = 0x80; w[13] = 0; w[14] = 384; w[15] = 0; digest_md5(w,r); result[0] = (r[0] + 0x67452301u) ^ (r[2] + 0x98badcfeu); result[1] = (r[1] + 0xefcdab89u) ^ (r[3] + 0x10325476u); } static void read64(lua_State *L, uint32_t xx[2], uint32_t yy[2]) { size_t sz = 0; const uint8_t *x = (const uint8_t *)luaL_checklstring(L, 1, &sz); if (sz != 8) { luaL_error(L, "Invalid uint64 x"); } const uint8_t *y = (const uint8_t *)luaL_checklstring(L, 2, &sz); if (sz != 8) { luaL_error(L, "Invalid uint64 y"); } xx[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24; xx[1] = x[4] | x[5]<<8 | x[6]<<16 | x[7]<<24; yy[0] = y[0] | y[1]<<8 | y[2]<<16 | y[3]<<24; yy[1] = y[4] | y[5]<<8 | y[6]<<16 | y[7]<<24; } static int pushqword(lua_State *L, uint32_t result[2]) { uint8_t tmp[8]; tmp[0] = result[0] & 0xff; tmp[1] = (result[0] >> 8 )& 0xff; tmp[2] = (result[0] >> 16 )& 0xff; tmp[3] = (result[0] >> 24 )& 0xff; tmp[4] = result[1] & 0xff; tmp[5] = (result[1] >> 8 )& 0xff; tmp[6] = (result[1] >> 16 )& 0xff; tmp[7] = (result[1] >> 24 )& 0xff; lua_pushlstring(L, (const char *)tmp, 8); return 1; } static int lhmac64(lua_State *L) { uint32_t x[2], y[2]; read64(L, x, y); uint32_t result[2]; hmac(x,y,result); return pushqword(L, result); } /* h1 = crypt.hmac64_md5(a,b) m = md5.sum((a..b):rep(3)) h2 = crypt.xor_str(m:sub(1,8), m:sub(9,16)) assert(h1 == h2) */ static int lhmac64_md5(lua_State *L) { uint32_t x[2], y[2]; read64(L, x, y); uint32_t result[2]; hmac_md5(x,y,result); return pushqword(L, result); } /* 8bytes key string text */ static int lhmac_hash(lua_State *L) { uint32_t key[2]; size_t sz = 0; const uint8_t *x = (const uint8_t *)luaL_checklstring(L, 1, &sz); if (sz != 8) { luaL_error(L, "Invalid uint64 key"); } key[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24; key[1] = x[4] | x[5]<<8 | x[6]<<16 | x[7]<<24; const char * text = luaL_checklstring(L, 2, &sz); uint8_t h[8]; Hash(text,(int)sz,h); uint32_t htext[2]; htext[0] = h[0] | h[1]<<8 | h[2]<<16 | h[3]<<24; htext[1] = h[4] | h[5]<<8 | h[6]<<16 | h[7]<<24; uint32_t result[2]; hmac(htext,key,result); return pushqword(L, result); } // powmodp64 for DH-key exchange // The biggest 64bit prime #define P 0xffffffffffffffc5ull static inline uint64_t mul_mod_p(uint64_t a, uint64_t b) { uint64_t m = 0; while(b) { if(b&1) { uint64_t t = P-a; if ( m >= t) { m -= t; } else { m += a; } } if (a >= P - a) { a = a * 2 - P; } else { a = a * 2; } b>>=1; } return m; } static inline uint64_t pow_mod_p(uint64_t a, uint64_t b) { if (b==1) { return a; } uint64_t t = pow_mod_p(a, b>>1); t = mul_mod_p(t,t); if (b % 2) { t = mul_mod_p(t, a); } return t; } // calc a^b % p static uint64_t powmodp(uint64_t a, uint64_t b) { if (a > P) a%=P; return pow_mod_p(a,b); } static void push64(lua_State *L, uint64_t r) { uint8_t tmp[8]; tmp[0] = r & 0xff; tmp[1] = (r >> 8 )& 0xff; tmp[2] = (r >> 16 )& 0xff; tmp[3] = (r >> 24 )& 0xff; tmp[4] = (r >> 32 )& 0xff; tmp[5] = (r >> 40 )& 0xff; tmp[6] = (r >> 48 )& 0xff; tmp[7] = (r >> 56 )& 0xff; lua_pushlstring(L, (const char *)tmp, 8); } static int ldhsecret(lua_State *L) { uint32_t x[2], y[2]; read64(L, x, y); uint64_t xx = (uint64_t)x[0] | (uint64_t)x[1]<<32; uint64_t yy = (uint64_t)y[0] | (uint64_t)y[1]<<32; if (xx == 0 || yy == 0) return luaL_error(L, "Can't be 0"); uint64_t r = powmodp(xx, yy); push64(L, r); return 1; } #define G 5 static int ldhexchange(lua_State *L) { size_t sz = 0; const uint8_t *x = (const uint8_t *)luaL_checklstring(L, 1, &sz); if (sz != 8) { luaL_error(L, "Invalid dh uint64 key"); } uint32_t xx[2]; xx[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24; xx[1] = x[4] | x[5]<<8 | x[6]<<16 | x[7]<<24; uint64_t x64 = (uint64_t)xx[0] | (uint64_t)xx[1]<<32; if (x64 == 0) return luaL_error(L, "Can't be 0"); uint64_t r = powmodp(G, x64); push64(L, r); return 1; } // base64 static int lb64encode(lua_State *L) { static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; size_t sz = 0; const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 1, &sz); int encode_sz = (sz + 2)/3*4; char tmp[SMALL_CHUNK]; char *buffer = tmp; if (encode_sz > SMALL_CHUNK) { buffer = (char*)lua_newuserdatauv(L, encode_sz, 0); } int i,j; j=0; for (i=0;i<(int)sz-2;i+=3) { uint32_t v = text[i] << 16 | text[i+1] << 8 | text[i+2]; buffer[j] = encoding[v >> 18]; buffer[j+1] = encoding[(v >> 12) & 0x3f]; buffer[j+2] = encoding[(v >> 6) & 0x3f]; buffer[j+3] = encoding[(v) & 0x3f]; j+=4; } int padding = sz-i; uint32_t v; switch(padding) { case 1 : v = text[i]; buffer[j] = encoding[v >> 2]; buffer[j+1] = encoding[(v & 3) << 4]; buffer[j+2] = '='; buffer[j+3] = '='; break; case 2 : v = text[i] << 8 | text[i+1]; buffer[j] = encoding[v >> 10]; buffer[j+1] = encoding[(v >> 4) & 0x3f]; buffer[j+2] = encoding[(v & 0xf) << 2]; buffer[j+3] = '='; break; } lua_pushlstring(L, buffer, encode_sz); return 1; } static inline int b64index(uint8_t c) { static const int decoding[] = {62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,0,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,-1,-1,-1,-1,-1,-1,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}; int decoding_size = sizeof(decoding)/sizeof(decoding[0]); if (c<43) { return -1; } c -= 43; if (c>=decoding_size) return -1; return decoding[c]; } static int lb64decode(lua_State *L) { size_t sz = 0; const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 1, &sz); int decode_sz = (sz+3)/4*3; char tmp[SMALL_CHUNK]; char *buffer = tmp; if (decode_sz > SMALL_CHUNK) { buffer = (char*)lua_newuserdatauv(L, decode_sz, 0); } int i,j; int output = 0; for (i=0;i=sz && 4>j){ /*To improve compatibility, there may not be enough equal signs */ c[j] = -2; }else{ c[j] = b64index(text[i]); } if (c[j] == -1) { ++i; continue; } if (c[j] == -2) { ++padding; } ++i; ++j; } uint32_t v; switch (padding) { case 0: v = (unsigned)c[0] << 18 | c[1] << 12 | c[2] << 6 | c[3]; buffer[output] = v >> 16; buffer[output+1] = (v >> 8) & 0xff; buffer[output+2] = v & 0xff; output += 3; break; case 1: if (c[3] != -2 || (c[2] & 3)!=0) { return luaL_error(L, "Invalid base64 text"); } v = (unsigned)c[0] << 10 | c[1] << 4 | c[2] >> 2 ; buffer[output] = v >> 8; buffer[output+1] = v & 0xff; output += 2; break; case 2: if (c[3] != -2 || c[2] != -2 || (c[1] & 0xf) !=0) { return luaL_error(L, "Invalid base64 text"); } v = (unsigned)c[0] << 2 | c[1] >> 4; buffer[output] = v; ++ output; break; default: return luaL_error(L, "Invalid base64 text"); } } lua_pushlstring(L, buffer, output); return 1; } static int lxor_str(lua_State *L) { size_t len1,len2; const char *s1 = luaL_checklstring(L,1,&len1); const char *s2 = luaL_checklstring(L,2,&len2); if (len2 == 0) { return luaL_error(L, "Can't xor empty string"); } luaL_Buffer b; char * buffer = luaL_buffinitsize(L, &b, len1); int i; for (i=0;i #include #include #define NODECACHE "_ctable" #define PROXYCACHE "_proxy" #define TABLES "_ctables" #define VALUE_NIL 0 #define VALUE_INTEGER 1 #define VALUE_REAL 2 #define VALUE_BOOLEAN 3 #define VALUE_TABLE 4 #define VALUE_STRING 5 #define VALUE_INVALID 6 #define INVALID_OFFSET 0xffffffff struct proxy { const char * data; int index; }; struct document { uint32_t strtbl; uint32_t n; uint32_t index[1]; // table[n] // strings }; struct table { uint32_t array; uint32_t dict; uint8_t type[1]; // value[array] // kvpair[dict] }; static inline const struct table * gettable(const struct document *doc, int index) { if (doc->index[index] == INVALID_OFFSET) { return NULL; } return (const struct table *)((const char *)doc + sizeof(uint32_t) + sizeof(uint32_t) + doc->n * sizeof(uint32_t) + doc->index[index]); } static void create_proxy(lua_State *L, const void *data, int index) { const struct table * t = gettable(data, index); if (t == NULL) { luaL_error(L, "Invalid index %d", index); } lua_getfield(L, LUA_REGISTRYINDEX, NODECACHE); if (lua_rawgetp(L, -1, t) == LUA_TTABLE) { lua_replace(L, -2); return; } lua_pop(L, 1); lua_newtable(L); lua_pushvalue(L, lua_upvalueindex(1)); lua_setmetatable(L, -2); lua_pushvalue(L, -1); // NODECACHE, table, table lua_rawsetp(L, -3, t); // NODECACHE, table lua_getfield(L, LUA_REGISTRYINDEX, PROXYCACHE); // NODECACHE, table, PROXYCACHE lua_pushvalue(L, -2); // NODECACHE, table, PROXYCACHE, table struct proxy * p = lua_newuserdatauv(L, sizeof(struct proxy), 0); // NODECACHE, table, PROXYCACHE, table, proxy p->data = data; p->index = index; lua_rawset(L, -3); // NODECACHE, table, PROXYCACHE lua_pop(L, 1); // NODECACHE, table lua_replace(L, -2); // table } static void clear_table(lua_State *L) { int t = lua_gettop(L); // clear top table if (lua_type(L, t) != LUA_TTABLE) { luaL_error(L, "Invalid cache"); } lua_pushnil(L); while (lua_next(L, t) != 0) { // key value lua_pop(L, 1); lua_pushvalue(L, -1); lua_pushnil(L); // key key nil lua_rawset(L, t); // key } } static void update_cache(lua_State *L, const void *data, const void * newdata) { lua_getfield(L, LUA_REGISTRYINDEX, NODECACHE); int t = lua_gettop(L); lua_getfield(L, LUA_REGISTRYINDEX, PROXYCACHE); int pt = t + 1; lua_newtable(L); // temp table int nt = pt + 1; lua_pushnil(L); while (lua_next(L, t) != 0) { // pointer (-2) -> table (-1) lua_pushvalue(L, -1); if (lua_rawget(L, pt) == LUA_TUSERDATA) { // pointer, table, proxy struct proxy * p = lua_touserdata(L, -1); if (p->data == data) { // update to newdata p->data = newdata; const struct table * newt = gettable(newdata, p->index); lua_pop(L, 1); // pointer, table clear_table(L); lua_pushvalue(L, lua_upvalueindex(1)); // pointer, table, meta lua_setmetatable(L, -2); // pointer, table if (newt) { lua_rawsetp(L, nt, newt); } else { lua_pop(L, 1); } // pointer lua_pushvalue(L, -1); lua_pushnil(L); lua_rawset(L, t); } else { lua_pop(L, 2); } } else { lua_pop(L, 2); // pointer } } // copy nt to t lua_pushnil(L); while (lua_next(L, nt) != 0) { lua_pushvalue(L, -2); lua_insert(L, -2); // key key value lua_rawset(L, t); } // NODECACHE PROXYCACHE TEMP lua_pop(L, 3); } static int lupdate(lua_State *L) { lua_getfield(L, LUA_REGISTRYINDEX, PROXYCACHE); lua_pushvalue(L, 1); // PROXYCACHE, table if (lua_rawget(L, -2) != LUA_TUSERDATA) { luaL_error(L, "Invalid proxy table %p", lua_topointer(L, 1)); } struct proxy * p = lua_touserdata(L, -1); luaL_checktype(L, 2, LUA_TLIGHTUSERDATA); const char * newdata = lua_touserdata(L, 2); update_cache(L, p->data, newdata); return 1; } static inline uint32_t getuint32(const void *v) { union { uint32_t d; uint8_t t[4]; } test = { 1 }; if (test.t[0] == 0) { // big endian test.d = *(const uint32_t *)v; return test.t[0] | test.t[1] << 4 | test.t[2] << 8 | test.t[3] << 12; } else { return *(const uint32_t *)v; } } static inline float getfloat(const void *v) { union { uint32_t d; float f; uint8_t t[4]; } test = { 1 }; if (test.t[0] == 0) { // big endian test.d = *(const uint32_t *)v; test.d = test.t[0] | test.t[1] << 4 | test.t[2] << 8 | test.t[3] << 12; return test.f; } else { return *(const float *)v; } } static void pushvalue(lua_State *L, const void *v, int type, const struct document * doc) { switch (type) { case VALUE_NIL: lua_pushnil(L); break; case VALUE_INTEGER: lua_pushinteger(L, (int32_t)getuint32(v)); break; case VALUE_REAL: lua_pushnumber(L, getfloat(v)); break; case VALUE_BOOLEAN: lua_pushboolean(L, getuint32(v)); break; case VALUE_TABLE: create_proxy(L, doc, getuint32(v)); break; case VALUE_STRING: lua_pushstring(L, (const char *)doc + doc->strtbl + getuint32(v)); break; default: luaL_error(L, "Invalid type %d at %p", type, v); } } static void copytable(lua_State *L, int tbl, struct proxy *p) { const struct document * doc = (const struct document *)p->data; if (p->index < 0 || p->index >= doc->n) { luaL_error(L, "Invalid proxy (index = %d, total = %d)", p->index, (int)doc->n); } const struct table * t = gettable(doc, p->index); if (t == NULL) { luaL_error(L, "Invalid proxy (index = %d)", p->index); } const uint32_t * v = (const uint32_t *)((const char *)t + sizeof(uint32_t) + sizeof(uint32_t) + ((t->array + t->dict + 3) & ~3)); int i; for (i=0;iarray;i++) { pushvalue(L, v++, t->type[i], doc); lua_rawseti(L, tbl, i+1); } for (i=0;idict;i++) { pushvalue(L, v++, VALUE_STRING, doc); pushvalue(L, v++, t->type[t->array+i], doc); lua_rawset(L, tbl); } } static int lnew(lua_State *L) { luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); const char * data = lua_touserdata(L, 1); // hold ref to data lua_getfield(L, LUA_REGISTRYINDEX, TABLES); lua_pushvalue(L, 1); lua_rawsetp(L, -2, data); create_proxy(L, data, 0); return 1; } static void copyfromdata(lua_State *L) { lua_getfield(L, LUA_REGISTRYINDEX, PROXYCACHE); lua_pushvalue(L, 1); // PROXYCACHE, table if (lua_rawget(L, -2) != LUA_TUSERDATA) { luaL_error(L, "Invalid proxy table %p", lua_topointer(L, 1)); } struct proxy * p = lua_touserdata(L, -1); lua_pop(L, 2); copytable(L, 1, p); lua_pushnil(L); lua_setmetatable(L, 1); // remove metatable } static int lindex(lua_State *L) { copyfromdata(L); lua_rawget(L, 1); return 1; } static int lnext(lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); lua_settop(L, 2); /* create a 2nd argument if there isn't one */ if (lua_next(L, 1)) return 2; else { lua_pushnil(L); return 1; } } static int lpairs(lua_State *L) { copyfromdata(L); lua_pushcfunction(L, lnext); lua_pushvalue(L, 1); lua_pushnil(L); return 3; } static int llen(lua_State *L) { copyfromdata(L); lua_pushinteger(L, lua_rawlen(L, 1)); return 1; } static void new_weak_table(lua_State *L, const char *mode) { lua_newtable(L); // NODECACHE { pointer:table } lua_createtable(L, 0, 1); // weak meta table lua_pushstring(L, mode); lua_setfield(L, -2, "__mode"); lua_setmetatable(L, -2); // make NODECACHE weak } static void gen_metatable(lua_State *L) { new_weak_table(L, "kv"); // NODECACHE { pointer:table } lua_setfield(L, LUA_REGISTRYINDEX, NODECACHE); new_weak_table(L, "k"); // PROXYCACHE { table:userdata } lua_setfield(L, LUA_REGISTRYINDEX, PROXYCACHE); lua_newtable(L); lua_setfield(L, LUA_REGISTRYINDEX, TABLES); lua_createtable(L, 0, 1); // mod table lua_createtable(L, 0, 2); // metatable luaL_Reg l[] = { { "__index", lindex }, { "__pairs", lpairs }, { "__len", llen }, { NULL, NULL }, }; lua_pushvalue(L, -1); luaL_setfuncs(L, l, 1); } static int lstringpointer(lua_State *L) { const char * str = luaL_checkstring(L, 1); lua_pushlightuserdata(L, (void *)str); return 1; } LUAMOD_API int luaopen_skynet_datasheet_core(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "new", lnew }, { "update", lupdate }, { NULL, NULL }, }; luaL_newlibtable(L,l); gen_metatable(L); luaL_setfuncs(L, l, 1); lua_pushcfunction(L, lstringpointer); lua_setfield(L, -2, "stringpointer"); return 1; } ================================================ FILE: lualib-src/lua-debugchannel.c ================================================ #define LUA_LIB // only for debug use #include #include #include #include #include #include "spinlock.h" #define METANAME "debugchannel" struct command { struct command * next; size_t sz; }; struct channel { struct spinlock lock; int ref; struct command * head; struct command * tail; }; static struct channel * channel_new() { struct channel * c = malloc(sizeof(*c)); memset(c, 0 , sizeof(*c)); c->ref = 1; SPIN_INIT(c) return c; } static struct channel * channel_connect(struct channel *c) { struct channel * ret = NULL; SPIN_LOCK(c) if (c->ref == 1) { ++c->ref; ret = c; } SPIN_UNLOCK(c) return ret; } static struct channel * channel_release(struct channel *c) { SPIN_LOCK(c) --c->ref; if (c->ref > 0) { SPIN_UNLOCK(c) return c; } // never unlock while reference is 0 struct command * p = c->head; c->head = NULL; c->tail = NULL; while(p) { struct command *next = p->next; free(p); p = next; } SPIN_UNLOCK(c) SPIN_DESTROY(c) free(c); return NULL; } // call free after channel_read static struct command * channel_read(struct channel *c, double timeout) { struct command * ret = NULL; SPIN_LOCK(c) if (c->head == NULL) { SPIN_UNLOCK(c) int ti = (int)(timeout * 100000); usleep(ti); return NULL; } ret = c->head; c->head = ret->next; if (c->head == NULL) { c->tail = NULL; } SPIN_UNLOCK(c) return ret; } static void channel_write(struct channel *c, const char * s, size_t sz) { struct command * cmd = malloc(sizeof(*cmd)+ sz); cmd->sz = sz; cmd->next = NULL; memcpy(cmd+1, s, sz); SPIN_LOCK(c) if (c->tail == NULL) { c->head = c->tail = cmd; } else { c->tail->next = cmd; c->tail = cmd; } SPIN_UNLOCK(c) } struct channel_box { struct channel *c; }; static int lread(lua_State *L) { struct channel_box *cb = luaL_checkudata(L,1, METANAME); double ti = luaL_optnumber(L, 2, 0); struct command * c = channel_read(cb->c, ti); if (c == NULL) return 0; lua_pushlstring(L, (const char *)(c+1), c->sz); free(c); return 1; } static int lwrite(lua_State *L) { struct channel_box *cb = luaL_checkudata(L,1, METANAME); size_t sz; const char * str = luaL_checklstring(L, 2, &sz); channel_write(cb->c, str, sz); return 0; } static int lrelease(lua_State *L) { struct channel_box *cb = lua_touserdata(L, 1); if (cb) { if (channel_release(cb->c) == NULL) { cb->c = NULL; } } return 0; } static struct channel * new_channel(lua_State *L, struct channel *c) { if (c == NULL) { c = channel_new(); } else { c = channel_connect(c); } if (c == NULL) { luaL_error(L, "new channel failed"); // never go here } struct channel_box * cb = lua_newuserdatauv(L, sizeof(*cb), 0); cb->c = c; if (luaL_newmetatable(L, METANAME)) { luaL_Reg l[]={ { "read", lread }, { "write", lwrite }, { NULL, NULL }, }; luaL_newlib(L,l); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, lrelease); lua_setfield(L, -2, "__gc"); } lua_setmetatable(L, -2); return c; } static int lcreate(lua_State *L) { struct channel *c = new_channel(L, NULL); lua_pushlightuserdata(L, c); return 2; } static int lconnect(lua_State *L) { struct channel *c = lua_touserdata(L, 1); if (c == NULL) return luaL_error(L, "Invalid channel pointer"); new_channel(L, c); return 1; } static const int HOOKKEY = 0; /* ** Auxiliary function used by several library functions: check for ** an optional thread as function's first argument and set 'arg' with ** 1 if this argument is present (so that functions can skip it to ** access their other arguments) */ static lua_State *getthread (lua_State *L, int *arg) { if (lua_isthread(L, 1)) { *arg = 1; return lua_tothread(L, 1); } else { *arg = 0; return L; /* function will operate over current thread */ } } /* ** Call hook function registered at hook table for the current ** thread (if there is one) */ static void hookf (lua_State *L, lua_Debug *ar) { static const char *const hooknames[] = {"call", "return", "line", "count", "tail call"}; lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); lua_pushthread(L); if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ if (ar->currentline >= 0) lua_pushinteger(L, ar->currentline); /* push current line */ else lua_pushnil(L); lua_call(L, 2, 1); /* call hook function */ int yield = lua_toboolean(L, -1); lua_pop(L,1); if (yield) { lua_yield(L, 0); } } } /* ** Convert a string mask (for 'sethook') into a bit mask */ static int makemask (const char *smask, int count) { int mask = 0; if (strchr(smask, 'c')) mask |= LUA_MASKCALL; if (strchr(smask, 'r')) mask |= LUA_MASKRET; if (strchr(smask, 'l')) mask |= LUA_MASKLINE; if (count > 0) mask |= LUA_MASKCOUNT; return mask; } static int db_sethook (lua_State *L) { int arg, mask, count; lua_Hook func; lua_State *L1 = getthread(L, &arg); if (lua_isnoneornil(L, arg+1)) { /* no hook? */ lua_settop(L, arg+1); func = NULL; mask = 0; count = 0; /* turn off hooks */ } else { const char *smask = luaL_checkstring(L, arg+2); luaL_checktype(L, arg+1, LUA_TFUNCTION); count = (int)luaL_optinteger(L, arg + 3, 0); func = hookf; mask = makemask(smask, count); } if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) { lua_createtable(L, 0, 2); /* create a hook table */ lua_pushvalue(L, -1); lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */ lua_pushstring(L, "k"); lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ lua_pushvalue(L, -1); lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ } lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */ lua_pushvalue(L, arg + 1); /* value (hook function) */ lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */ lua_sethook(L1, func, mask, count); return 0; } LUAMOD_API int luaopen_skynet_debugchannel(lua_State *L) { luaL_Reg l[] = { { "create", lcreate }, // for write { "connect", lconnect }, // for read { "release", lrelease }, { "sethook", db_sethook }, { NULL, NULL }, }; luaL_checkversion(L); luaL_newlib(L,l); return 1; } ================================================ FILE: lualib-src/lua-memory.c ================================================ #define LUA_LIB #include #include #include "malloc_hook.h" static int ltotal(lua_State *L) { size_t t = malloc_used_memory(); lua_pushinteger(L, (lua_Integer)t); return 1; } static int lblock(lua_State *L) { size_t t = malloc_memory_block(); lua_pushinteger(L, (lua_Integer)t); return 1; } static int ldumpinfo(lua_State *L) { const char *opts = NULL; if (lua_isstring(L, 1)) { opts = luaL_checkstring(L,1); } memory_info_dump(opts); return 0; } static int ljestat(lua_State *L) { static const char* names[] = { "stats.allocated", "stats.resident", "stats.retained", "stats.mapped", "stats.active" }; static size_t flush = 1; mallctl_int64("epoch", &flush); // refresh je.stats.cache lua_newtable(L); int i; for (i = 0; i < (sizeof(names)/sizeof(names[0])); i++) { lua_pushstring(L, names[i]); lua_pushinteger(L, (lua_Integer) mallctl_int64(names[i], NULL)); lua_settable(L, -3); } return 1; } static int lmallctl(lua_State *L) { const char *name = luaL_checkstring(L,1); lua_pushinteger(L, (lua_Integer) mallctl_int64(name, NULL)); return 1; } static int ldump(lua_State *L) { dump_c_mem(); return 0; } static int lcurrent(lua_State *L) { lua_pushinteger(L, malloc_current_memory()); return 1; } static int ldumpheap(lua_State *L) { mallctl_cmd("prof.dump"); return 0; } static int lprofactive(lua_State *L) { bool *pval, active; if (lua_isnone(L, 1)) { pval = NULL; } else { active = lua_toboolean(L, 1) ? true : false; pval = &active; } bool ret = mallctl_bool("prof.active", pval); lua_pushboolean(L, ret); return 1; } LUAMOD_API int luaopen_skynet_memory(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "total", ltotal }, { "block", lblock }, { "dumpinfo", ldumpinfo }, { "jestat", ljestat }, { "mallctl", lmallctl }, { "dump", ldump }, { "info", dump_mem_lua }, { "current", lcurrent }, { "dumpheap", ldumpheap }, { "profactive", lprofactive }, { NULL, NULL }, }; luaL_newlib(L,l); return 1; } ================================================ FILE: lualib-src/lua-mongo.c ================================================ #define LUA_LIB #include "skynet_malloc.h" #include #include #include #include #include #include #define OP_COMPRESSED 2012 #define OP_MSG 2013 typedef enum { MSG_CHECKSUM_PRESENT = 1 << 0, MSG_MORE_TO_COME = 1 << 1, MSG_EXHAUST_ALLOWED = 1 << 16, } msg_flags_t; #define DEFAULT_CAP 128 struct connection { int sock; int id; }; struct response { int flags; int32_t cursor_id[2]; int starting_from; int number; }; struct buffer { int size; int cap; uint8_t * ptr; uint8_t buffer[DEFAULT_CAP]; }; static inline uint32_t little_endian(uint32_t v) { union { uint32_t v; uint8_t b[4]; } u; u.v = v; return u.b[0] | u.b[1] << 8 | u.b[2] << 16 | u.b[3] << 24; } typedef void * document; static inline uint32_t get_length(document buffer) { union { uint32_t v; uint8_t b[4]; } u; memcpy(&u.v, buffer, 4); return u.b[0] | u.b[1] << 8 | u.b[2] << 16 | u.b[3] << 24; } static inline void buffer_destroy(struct buffer *b) { if (b->ptr != b->buffer) { skynet_free(b->ptr); } } static inline void buffer_create(struct buffer *b) { b->size = 0; b->cap = DEFAULT_CAP; b->ptr = b->buffer; } static inline void buffer_reserve(struct buffer *b, int sz) { if (b->size + sz <= b->cap) return; do { b->cap *= 2; } while (b->cap <= b->size + sz); if (b->ptr == b->buffer) { b->ptr = (uint8_t*)malloc(b->cap); memcpy(b->ptr, b->buffer, b->size); } else { b->ptr = (uint8_t*)realloc(b->ptr, b->cap); } } static inline void write_int32(struct buffer *b, int32_t v) { uint32_t uv = (uint32_t)v; buffer_reserve(b,4); b->ptr[b->size++] = uv & 0xff; b->ptr[b->size++] = (uv >> 8)&0xff; b->ptr[b->size++] = (uv >> 16)&0xff; b->ptr[b->size++] = (uv >> 24)&0xff; } static inline void write_int8(struct buffer *b, int8_t v) { uint8_t uv = (uint8_t)v; buffer_reserve(b, 1); b->ptr[b->size++] = uv; } /* static inline void write_bytes(struct buffer *b, const void * buf, int sz) { buffer_reserve(b,sz); memcpy(b->ptr + b->size, buf, sz); b->size += sz; } static void write_string(struct buffer *b, const char *key, size_t sz) { buffer_reserve(b,sz+1); memcpy(b->ptr + b->size, key, sz); b->ptr[b->size+sz] = '\0'; b->size+=sz+1; } */ static inline int reserve_length(struct buffer *b) { int sz = b->size; buffer_reserve(b,4); b->size +=4; return sz; } static inline void write_length(struct buffer *b, int32_t v, int off) { uint32_t uv = (uint32_t)v; b->ptr[off++] = uv & 0xff; b->ptr[off++] = (uv >> 8)&0xff; b->ptr[off++] = (uv >> 16)&0xff; b->ptr[off++] = (uv >> 24)&0xff; } struct header_t { //int32_t message_length; // total message size, include this int32_t request_id; // identifier for this message int32_t response_to; // requestID from the original request(used in responses from the database) int32_t opcode; // message type int32_t flags; }; // 1 string data // 2 result document table // return boolean succ (false -> request id, error document) // number request_id // document first static int unpack_reply(lua_State *L) { size_t data_len = 0; const char * data = luaL_checklstring(L,1,&data_len); const struct header_t* h = (const struct header_t*)data; if (data_len < sizeof(*h)) { lua_pushboolean(L, 0); return 1; } int opcode = little_endian(h->opcode); if (opcode != OP_MSG) { return luaL_error(L, "Unsupported opcode:%d", opcode); } int id = little_endian(h->response_to); int flags = little_endian(h->flags); if (flags != 0) { if ((flags & MSG_CHECKSUM_PRESENT) != 0) { return luaL_error(L, "Unsupported OP_MSG flag checksumPresent"); } if ((flags ^ MSG_MORE_TO_COME) != 0) { return luaL_error(L, "Unsupported OP_MSG flag:%d", flags); } } int sz = (int)data_len - sizeof(*h); const uint8_t * section = (const uint8_t *)(h+1); uint8_t payload_type = *section; const uint8_t * doc = section+1; if (payload_type != 0) { return luaL_error(L, "Unsupported OP_MSG payload type: %d", payload_type); } int32_t doc_sz = get_length((document)(doc)); if ((sz - 1) != doc_sz) { return luaL_error(L, "Unsupported OP_MSG reply: >1 section"); } lua_pushboolean(L, 1); lua_pushinteger(L, id); lua_pushlightuserdata(L, (void *)(doc)); return 3; } // string 4 bytes length // return integer static int reply_length(lua_State *L) { const char * rawlen_str = luaL_checkstring(L, 1); int rawlen = 0; memcpy(&rawlen, rawlen_str, sizeof(int)); int length = little_endian(rawlen); lua_pushinteger(L, length - 4); return 1; } // @param 1 request_id int // @param 2 flags int // @param 3 command bson document // @return static int op_msg(lua_State *L) { int id = luaL_checkinteger(L, 1); int flags = luaL_checkinteger(L, 2); document cmd = lua_touserdata(L, 3); if (cmd == NULL) { return luaL_error(L, "opmsg require cmd document"); } luaL_Buffer b; luaL_buffinit(L, &b); struct buffer buf; buffer_create(&buf); int len = reserve_length(&buf); write_int32(&buf, id); write_int32(&buf, 0); write_int32(&buf, OP_MSG); write_int32(&buf, flags); write_int8(&buf, 0); int32_t cmd_len = get_length(cmd); int total = buf.size + cmd_len; write_length(&buf, total, len); luaL_addlstring(&b, (const char *)buf.ptr, buf.size); buffer_destroy(&buf); luaL_addlstring(&b, (const char *)cmd, cmd_len); luaL_pushresult(&b); return 1; } LUAMOD_API int luaopen_skynet_mongo_driver(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] ={ { "reply", unpack_reply }, // 接收响应 { "length", reply_length }, { "op_msg", op_msg}, { NULL, NULL }, }; luaL_newlib(L,l); return 1; } ================================================ FILE: lualib-src/lua-multicast.c ================================================ #define LUA_LIB #include "skynet.h" #include #include #include #include #include "atomic.h" struct mc_package { ATOM_INT reference; uint32_t size; void *data; }; static int pack(lua_State *L, void *data, size_t size) { struct mc_package * pack = skynet_malloc(sizeof(struct mc_package)); ATOM_INIT(&pack->reference, 0); pack->size = (uint32_t)size; pack->data = data; struct mc_package ** ret = skynet_malloc(sizeof(*ret)); *ret = pack; lua_pushlightuserdata(L, ret); lua_pushinteger(L, sizeof(ret)); return 2; } /* lightuserdata integer size return lightuserdata, sizeof(struct mc_package *) */ static int mc_packlocal(lua_State *L) { void * data = lua_touserdata(L, 1); size_t size = (size_t)luaL_checkinteger(L, 2); if (size != (uint32_t)size) { return luaL_error(L, "Size should be 32bit integer"); } return pack(L, data, size); } /* lightuserdata integer size return lightuserdata, sizeof(struct mc_package *) */ static int mc_packremote(lua_State *L) { void * data = lua_touserdata(L, 1); size_t size = (size_t)luaL_checkinteger(L, 2); if (size != (uint32_t)size) { return luaL_error(L, "Size should be 32bit integer"); } void * msg = skynet_malloc(size); memcpy(msg, data, size); return pack(L, msg, size); } /* lightuserdata struct mc_package ** integer size (must be sizeof(struct mc_package *) return package, lightuserdata, size */ static int mc_unpacklocal(lua_State *L) { struct mc_package ** pack = lua_touserdata(L,1); int sz = luaL_checkinteger(L,2); if (sz != sizeof(pack)) { return luaL_error(L, "Invalid multicast package size %d", sz); } lua_pushlightuserdata(L, *pack); lua_pushlightuserdata(L, (*pack)->data); lua_pushinteger(L, (lua_Integer)((*pack)->size)); return 3; } /* lightuserdata struct mc_package ** integer reference return mc_package * */ static int mc_bindrefer(lua_State *L) { struct mc_package ** pack = lua_touserdata(L,1); int ref = luaL_checkinteger(L,2); if (ATOM_LOAD(&(*pack)->reference) != 0) { return luaL_error(L, "Can't bind a multicast package more than once"); } ATOM_STORE(&(*pack)->reference , ref); lua_pushlightuserdata(L, *pack); skynet_free(pack); return 1; } /* lightuserdata struct mc_package * */ static int mc_closelocal(lua_State *L) { struct mc_package *pack = lua_touserdata(L,1); int ref = ATOM_FDEC(&pack->reference)-1; if (ref <= 0) { skynet_free(pack->data); skynet_free(pack); if (ref < 0) { return luaL_error(L, "Invalid multicast package reference %d", ref); } } return 0; } /* lightuserdata struct mc_package ** return lightuserdata/size */ static int mc_remote(lua_State *L) { struct mc_package **ptr = lua_touserdata(L,1); struct mc_package *pack = *ptr; lua_pushlightuserdata(L, pack->data); lua_pushinteger(L, (lua_Integer)(pack->size)); skynet_free(pack); skynet_free(ptr); return 2; } static int mc_nextid(lua_State *L) { uint32_t id = (uint32_t)luaL_checkinteger(L, 1); id += 256; // remove the highest bit, see #1139 lua_pushinteger(L, id & 0x7fffffffu); return 1; } LUAMOD_API int luaopen_skynet_multicast_core(lua_State *L) { luaL_Reg l[] = { { "pack", mc_packlocal }, { "unpack", mc_unpacklocal }, { "bind", mc_bindrefer }, { "close", mc_closelocal }, { "remote", mc_remote }, { "packremote", mc_packremote }, { "nextid", mc_nextid }, { NULL, NULL }, }; luaL_checkversion(L); luaL_newlib(L,l); return 1; } ================================================ FILE: lualib-src/lua-netpack.c ================================================ #define LUA_LIB #include "skynet_malloc.h" #include "skynet_socket.h" #include #include #include #include #include #include #define QUEUESIZE 1024 #define HASHSIZE 4096 #define SMALLSTRING 2048 #define TYPE_DATA 1 #define TYPE_MORE 2 #define TYPE_ERROR 3 #define TYPE_OPEN 4 #define TYPE_CLOSE 5 #define TYPE_WARNING 6 #define TYPE_INIT 7 /* Each package is uint16 + data , uint16 (serialized in big-endian) is the number of bytes comprising the data . */ struct netpack { int id; int size; void * buffer; }; struct uncomplete { struct netpack pack; struct uncomplete * next; int read; int header; }; struct queue { int cap; int head; int tail; struct uncomplete * hash[HASHSIZE]; struct netpack queue[QUEUESIZE]; }; static void clear_list(struct uncomplete * uc) { while (uc) { skynet_free(uc->pack.buffer); void * tmp = uc; uc = uc->next; skynet_free(tmp); } } static int lclear(lua_State *L) { struct queue * q = lua_touserdata(L, 1); if (q == NULL) { return 0; } int i; for (i=0;ihash[i]); q->hash[i] = NULL; } if (q->head > q->tail) { q->tail += q->cap; } for (i=q->head;itail;i++) { struct netpack *np = &q->queue[i % q->cap]; skynet_free(np->buffer); } q->head = q->tail = 0; return 0; } static inline int hash_fd(int fd) { int a = fd >> 24; int b = fd >> 12; int c = fd; return (int)(((uint32_t)(a + b + c)) % HASHSIZE); } static struct uncomplete * find_uncomplete(struct queue *q, int fd) { if (q == NULL) return NULL; int h = hash_fd(fd); struct uncomplete * uc = q->hash[h]; if (uc == NULL) return NULL; if (uc->pack.id == fd) { q->hash[h] = uc->next; return uc; } struct uncomplete * last = uc; while (last->next) { uc = last->next; if (uc->pack.id == fd) { last->next = uc->next; return uc; } last = uc; } return NULL; } static struct queue * get_queue(lua_State *L) { struct queue *q = lua_touserdata(L,1); if (q == NULL) { q = lua_newuserdatauv(L, sizeof(struct queue), 0); q->cap = QUEUESIZE; q->head = 0; q->tail = 0; int i; for (i=0;ihash[i] = NULL; } lua_replace(L, 1); } return q; } static void expand_queue(lua_State *L, struct queue *q) { struct queue *nq = lua_newuserdatauv(L, sizeof(struct queue) + q->cap * sizeof(struct netpack), 0); nq->cap = q->cap + QUEUESIZE; nq->head = 0; nq->tail = q->cap; memcpy(nq->hash, q->hash, sizeof(nq->hash)); memset(q->hash, 0, sizeof(q->hash)); int i; for (i=0;icap;i++) { int idx = (q->head + i) % q->cap; nq->queue[i] = q->queue[idx]; } q->head = q->tail = 0; lua_replace(L,1); } static void push_data(lua_State *L, int fd, void *buffer, int size, int clone) { if (clone) { void * tmp = skynet_malloc(size); memcpy(tmp, buffer, size); buffer = tmp; } struct queue *q = get_queue(L); struct netpack *np = &q->queue[q->tail]; if (++q->tail >= q->cap) q->tail -= q->cap; np->id = fd; np->buffer = buffer; np->size = size; if (q->head == q->tail) { expand_queue(L, q); } } static struct uncomplete * save_uncomplete(lua_State *L, int fd) { struct queue *q = get_queue(L); int h = hash_fd(fd); struct uncomplete * uc = skynet_malloc(sizeof(struct uncomplete)); memset(uc, 0, sizeof(*uc)); uc->next = q->hash[h]; uc->pack.id = fd; q->hash[h] = uc; return uc; } static inline int read_size(uint8_t * buffer) { int r = (int)buffer[0] << 8 | (int)buffer[1]; return r; } static void push_more(lua_State *L, int fd, uint8_t *buffer, int size) { if (size == 1) { struct uncomplete * uc = save_uncomplete(L, fd); uc->read = -1; uc->header = *buffer; return; } int pack_size = read_size(buffer); buffer += 2; size -= 2; if (size < pack_size) { struct uncomplete * uc = save_uncomplete(L, fd); uc->read = size; uc->pack.size = pack_size; uc->pack.buffer = skynet_malloc(pack_size); memcpy(uc->pack.buffer, buffer, size); return; } push_data(L, fd, buffer, pack_size, 1); buffer += pack_size; size -= pack_size; if (size > 0) { push_more(L, fd, buffer, size); } } static void close_uncomplete(lua_State *L, int fd) { struct queue *q = lua_touserdata(L,1); struct uncomplete * uc = find_uncomplete(q, fd); if (uc) { skynet_free(uc->pack.buffer); skynet_free(uc); } } static int filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) { struct queue *q = lua_touserdata(L,1); struct uncomplete * uc = find_uncomplete(q, fd); if (uc) { // fill uncomplete if (uc->read < 0) { // read size assert(uc->read == -1); int pack_size = *buffer; pack_size |= uc->header << 8 ; ++buffer; --size; uc->pack.size = pack_size; uc->pack.buffer = skynet_malloc(pack_size); uc->read = 0; } int need = uc->pack.size - uc->read; if (size < need) { memcpy(uc->pack.buffer + uc->read, buffer, size); uc->read += size; int h = hash_fd(fd); uc->next = q->hash[h]; q->hash[h] = uc; return 1; } memcpy(uc->pack.buffer + uc->read, buffer, need); buffer += need; size -= need; if (size == 0) { lua_pushvalue(L, lua_upvalueindex(TYPE_DATA)); lua_pushinteger(L, fd); lua_pushlightuserdata(L, uc->pack.buffer); lua_pushinteger(L, uc->pack.size); skynet_free(uc); return 5; } // more data push_data(L, fd, uc->pack.buffer, uc->pack.size, 0); skynet_free(uc); push_more(L, fd, buffer, size); lua_pushvalue(L, lua_upvalueindex(TYPE_MORE)); return 2; } else { if (size == 1) { struct uncomplete * uc = save_uncomplete(L, fd); uc->read = -1; uc->header = *buffer; return 1; } int pack_size = read_size(buffer); buffer+=2; size-=2; if (size < pack_size) { struct uncomplete * uc = save_uncomplete(L, fd); uc->read = size; uc->pack.size = pack_size; uc->pack.buffer = skynet_malloc(pack_size); memcpy(uc->pack.buffer, buffer, size); return 1; } if (size == pack_size) { // just one package lua_pushvalue(L, lua_upvalueindex(TYPE_DATA)); lua_pushinteger(L, fd); void * result = skynet_malloc(pack_size); memcpy(result, buffer, size); lua_pushlightuserdata(L, result); lua_pushinteger(L, size); return 5; } // more data push_data(L, fd, buffer, pack_size, 1); buffer += pack_size; size -= pack_size; push_more(L, fd, buffer, size); lua_pushvalue(L, lua_upvalueindex(TYPE_MORE)); return 2; } } static inline int filter_data(lua_State *L, int fd, uint8_t * buffer, int size) { int ret = filter_data_(L, fd, buffer, size); // buffer is the data of socket message, it malloc at socket_server.c : function forward_message . // it should be free before return, skynet_free(buffer); return ret; } static void pushstring(lua_State *L, const char * msg, int size) { if (msg) { lua_pushlstring(L, msg, size); } else { lua_pushliteral(L, ""); } } /* userdata queue lightuserdata msg integer size return userdata queue integer type integer fd string msg | lightuserdata/integer */ static int lfilter(lua_State *L) { struct skynet_socket_message *message = lua_touserdata(L,2); int size = luaL_checkinteger(L,3); char * buffer = message->buffer; if (buffer == NULL) { buffer = (char *)(message+1); size -= sizeof(*message); } else { size = -1; } lua_settop(L, 1); switch(message->type) { case SKYNET_SOCKET_TYPE_DATA: // ignore listen id (message->id) assert(size == -1); // never padding string return filter_data(L, message->id, (uint8_t *)buffer, message->ud); case SKYNET_SOCKET_TYPE_CONNECT: lua_pushvalue(L, lua_upvalueindex(TYPE_INIT)); lua_pushinteger(L, message->id); lua_pushlstring(L, buffer, size); lua_pushinteger(L, message->ud); return 5; case SKYNET_SOCKET_TYPE_CLOSE: // no more data in fd (message->id) close_uncomplete(L, message->id); lua_pushvalue(L, lua_upvalueindex(TYPE_CLOSE)); lua_pushinteger(L, message->id); return 3; case SKYNET_SOCKET_TYPE_ACCEPT: lua_pushvalue(L, lua_upvalueindex(TYPE_OPEN)); // ignore listen id (message->id); lua_pushinteger(L, message->ud); pushstring(L, buffer, size); return 4; case SKYNET_SOCKET_TYPE_ERROR: // no more data in fd (message->id) close_uncomplete(L, message->id); lua_pushvalue(L, lua_upvalueindex(TYPE_ERROR)); lua_pushinteger(L, message->id); pushstring(L, buffer, size); return 4; case SKYNET_SOCKET_TYPE_WARNING: lua_pushvalue(L, lua_upvalueindex(TYPE_WARNING)); lua_pushinteger(L, message->id); lua_pushinteger(L, message->ud); return 4; default: // never get here return 1; } } /* userdata queue return integer fd lightuserdata msg integer size */ static int lpop(lua_State *L) { struct queue * q = lua_touserdata(L, 1); if (q == NULL || q->head == q->tail) return 0; struct netpack *np = &q->queue[q->head]; if (++q->head >= q->cap) { q->head = 0; } lua_pushinteger(L, np->id); lua_pushlightuserdata(L, np->buffer); lua_pushinteger(L, np->size); return 3; } /* string msg | lightuserdata/integer lightuserdata/integer */ static const char * tolstring(lua_State *L, size_t *sz, int index) { const char * ptr; if (lua_isuserdata(L,index)) { ptr = (const char *)lua_touserdata(L,index); *sz = (size_t)luaL_checkinteger(L, index+1); } else { ptr = luaL_checklstring(L, index, sz); } return ptr; } static inline void write_size(uint8_t * buffer, int len) { buffer[0] = (len >> 8) & 0xff; buffer[1] = len & 0xff; } static int lpack(lua_State *L) { size_t len; const char * ptr = tolstring(L, &len, 1); if (len >= 0x10000) { return luaL_error(L, "Invalid size (too long) of data : %d", (int)len); } uint8_t * buffer = skynet_malloc(len + 2); write_size(buffer, len); memcpy(buffer+2, ptr, len); lua_pushlightuserdata(L, buffer); lua_pushinteger(L, len + 2); return 2; } static int ltostring(lua_State *L) { void * ptr = lua_touserdata(L, 1); int size = luaL_checkinteger(L, 2); if (ptr == NULL) { lua_pushliteral(L, ""); } else { lua_pushlstring(L, (const char *)ptr, size); skynet_free(ptr); } return 1; } LUAMOD_API int luaopen_skynet_netpack(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "pop", lpop }, { "pack", lpack }, { "clear", lclear }, { "tostring", ltostring }, { NULL, NULL }, }; luaL_newlib(L,l); // the order is same with macros : TYPE_* (defined top) lua_pushliteral(L, "data"); lua_pushliteral(L, "more"); lua_pushliteral(L, "error"); lua_pushliteral(L, "open"); lua_pushliteral(L, "close"); lua_pushliteral(L, "warning"); lua_pushliteral(L, "init"); lua_pushcclosure(L, lfilter, 7); lua_setfield(L, -2, "filter"); return 1; } ================================================ FILE: lualib-src/lua-seri.c ================================================ /* modify from https://github.com/cloudwu/lua-serialize */ #define LUA_LIB #include "skynet_malloc.h" #include #include #include #include #include #include #define TYPE_NIL 0 #define TYPE_BOOLEAN 1 // hibits 0 false 1 true #define TYPE_NUMBER 2 // hibits 0 : 0 , 1: byte, 2:word, 4: dword, 6: qword, 8 : double #define TYPE_NUMBER_ZERO 0 #define TYPE_NUMBER_BYTE 1 #define TYPE_NUMBER_WORD 2 #define TYPE_NUMBER_DWORD 4 #define TYPE_NUMBER_QWORD 6 #define TYPE_NUMBER_REAL 8 #define TYPE_USERDATA 3 #define TYPE_SHORT_STRING 4 // hibits 0~31 : len #define TYPE_LONG_STRING 5 #define TYPE_TABLE 6 #define MAX_COOKIE 32 #define COMBINE_TYPE(t,v) ((t) | (v) << 3) #define BLOCK_SIZE 128 #define MAX_DEPTH 32 struct block { struct block * next; char buffer[BLOCK_SIZE]; }; struct write_block { struct block * head; struct block * current; int len; int ptr; }; struct read_block { char * buffer; int len; int ptr; }; inline static struct block * blk_alloc(void) { struct block *b = skynet_malloc(sizeof(struct block)); b->next = NULL; return b; } inline static void wb_push(struct write_block *b, const void *buf, int sz) { const char * buffer = buf; if (b->ptr == BLOCK_SIZE) { _again: b->current = b->current->next = blk_alloc(); b->ptr = 0; } if (b->ptr <= BLOCK_SIZE - sz) { memcpy(b->current->buffer + b->ptr, buffer, sz); b->ptr+=sz; b->len+=sz; } else { int copy = BLOCK_SIZE - b->ptr; memcpy(b->current->buffer + b->ptr, buffer, copy); buffer += copy; b->len += copy; sz -= copy; goto _again; } } static void wb_init(struct write_block *wb , struct block *b) { wb->head = b; assert(b->next == NULL); wb->len = 0; wb->current = wb->head; wb->ptr = 0; } static void wb_free(struct write_block *wb) { struct block *blk = wb->head; blk = blk->next; // the first block is on stack while (blk) { struct block * next = blk->next; skynet_free(blk); blk = next; } wb->head = NULL; wb->current = NULL; wb->ptr = 0; wb->len = 0; } static void rball_init(struct read_block * rb, char * buffer, int size) { rb->buffer = buffer; rb->len = size; rb->ptr = 0; } static const void * rb_read(struct read_block *rb, int sz) { if (rb->len < sz) { return NULL; } int ptr = rb->ptr; rb->ptr += sz; rb->len -= sz; return rb->buffer + ptr; } static inline void wb_nil(struct write_block *wb) { uint8_t n = TYPE_NIL; wb_push(wb, &n, 1); } static inline void wb_boolean(struct write_block *wb, int boolean) { uint8_t n = COMBINE_TYPE(TYPE_BOOLEAN , boolean ? 1 : 0); wb_push(wb, &n, 1); } static inline void wb_integer(struct write_block *wb, lua_Integer v) { int type = TYPE_NUMBER; if (v == 0) { uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_ZERO); wb_push(wb, &n, 1); } else if (v != (int32_t)v) { uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_QWORD); int64_t v64 = v; wb_push(wb, &n, 1); wb_push(wb, &v64, sizeof(v64)); } else if (v < 0) { int32_t v32 = (int32_t)v; uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_DWORD); wb_push(wb, &n, 1); wb_push(wb, &v32, sizeof(v32)); } else if (v<0x100) { uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_BYTE); wb_push(wb, &n, 1); uint8_t byte = (uint8_t)v; wb_push(wb, &byte, sizeof(byte)); } else if (v<0x10000) { uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_WORD); wb_push(wb, &n, 1); uint16_t word = (uint16_t)v; wb_push(wb, &word, sizeof(word)); } else { uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_DWORD); wb_push(wb, &n, 1); uint32_t v32 = (uint32_t)v; wb_push(wb, &v32, sizeof(v32)); } } static inline void wb_real(struct write_block *wb, double v) { uint8_t n = COMBINE_TYPE(TYPE_NUMBER , TYPE_NUMBER_REAL); wb_push(wb, &n, 1); wb_push(wb, &v, sizeof(v)); } static inline void wb_pointer(struct write_block *wb, void *v) { uint8_t n = TYPE_USERDATA; wb_push(wb, &n, 1); wb_push(wb, &v, sizeof(v)); } static inline void wb_string(struct write_block *wb, const char *str, int len) { if (len < MAX_COOKIE) { uint8_t n = COMBINE_TYPE(TYPE_SHORT_STRING, len); wb_push(wb, &n, 1); if (len > 0) { wb_push(wb, str, len); } } else { uint8_t n; if (len < 0x10000) { n = COMBINE_TYPE(TYPE_LONG_STRING, 2); wb_push(wb, &n, 1); uint16_t x = (uint16_t) len; wb_push(wb, &x, 2); } else { n = COMBINE_TYPE(TYPE_LONG_STRING, 4); wb_push(wb, &n, 1); uint32_t x = (uint32_t) len; wb_push(wb, &x, 4); } wb_push(wb, str, len); } } static void pack_one(lua_State *L, struct write_block *b, int index, int depth); static int wb_table_array(lua_State *L, struct write_block * wb, int index, int depth) { int array_size = lua_rawlen(L,index); if (array_size >= MAX_COOKIE-1) { uint8_t n = COMBINE_TYPE(TYPE_TABLE, MAX_COOKIE-1); wb_push(wb, &n, 1); wb_integer(wb, array_size); } else { uint8_t n = COMBINE_TYPE(TYPE_TABLE, array_size); wb_push(wb, &n, 1); } int i; for (i=1;i<=array_size;i++) { lua_rawgeti(L,index,i); pack_one(L, wb, -1, depth); lua_pop(L,1); } return array_size; } static void wb_table_hash(lua_State *L, struct write_block * wb, int index, int depth, int array_size) { lua_pushnil(L); while (lua_next(L, index) != 0) { if (lua_type(L,-2) == LUA_TNUMBER) { if (lua_isinteger(L, -2)) { lua_Integer x = lua_tointeger(L,-2); if (x>0 && x<=array_size) { lua_pop(L,1); continue; } } } pack_one(L,wb,-2,depth); pack_one(L,wb,-1,depth); lua_pop(L, 1); } wb_nil(wb); } static int wb_table_metapairs(lua_State *L, struct write_block *wb, int index, int depth) { uint8_t n = COMBINE_TYPE(TYPE_TABLE, 0); wb_push(wb, &n, 1); lua_pushvalue(L, index); if (lua_pcall(L, 1, 3,0) != LUA_OK) return 1; for(;;) { lua_pushvalue(L, -2); lua_pushvalue(L, -2); lua_copy(L, -5, -3); if (lua_pcall(L, 2, 2, 0) != LUA_OK) return 1; int type = lua_type(L, -2); if (type == LUA_TNIL) { lua_pop(L, 4); break; } pack_one(L, wb, -2, depth); pack_one(L, wb, -1, depth); lua_pop(L, 1); } wb_nil(wb); return 0; } static int wb_table(lua_State *L, struct write_block *wb, int index, int depth) { if (!lua_checkstack(L, LUA_MINSTACK)) { lua_pushstring(L, "out of memory"); return 1; } if (index < 0) { index = lua_gettop(L) + index + 1; } if (luaL_getmetafield(L, index, "__pairs") != LUA_TNIL) { return wb_table_metapairs(L, wb, index, depth); } else { int array_size = wb_table_array(L, wb, index, depth); wb_table_hash(L, wb, index, depth, array_size); return 0; } } static void pack_one(lua_State *L, struct write_block *b, int index, int depth) { if (depth > MAX_DEPTH) { wb_free(b); luaL_error(L, "serialize can't pack too depth table"); } int type = lua_type(L,index); switch(type) { case LUA_TNIL: wb_nil(b); break; case LUA_TNUMBER: { if (lua_isinteger(L, index)) { lua_Integer x = lua_tointeger(L,index); wb_integer(b, x); } else { lua_Number n = lua_tonumber(L,index); wb_real(b,n); } break; } case LUA_TBOOLEAN: wb_boolean(b, lua_toboolean(L,index)); break; case LUA_TSTRING: { size_t sz = 0; const char *str = lua_tolstring(L,index,&sz); wb_string(b, str, (int)sz); break; } case LUA_TLIGHTUSERDATA: wb_pointer(b, lua_touserdata(L,index)); break; case LUA_TTABLE: { if (index < 0) { index = lua_gettop(L) + index + 1; } if (wb_table(L, b, index, depth+1)) { wb_free(b); lua_error(L); } break; } default: wb_free(b); luaL_error(L, "Unsupport type %s to serialize", lua_typename(L, type)); } } static void pack_from(lua_State *L, struct write_block *b, int from) { int n = lua_gettop(L) - from; int i; for (i=1;i<=n;i++) { pack_one(L, b , from + i, 0); } } static inline void invalid_stream_line(lua_State *L, struct read_block *rb, int line) { int len = rb->len; luaL_error(L, "Invalid serialize stream %d (line:%d)", len, line); } #define invalid_stream(L,rb) invalid_stream_line(L,rb,__LINE__) static lua_Integer get_integer(lua_State *L, struct read_block *rb, int cookie) { switch (cookie) { case TYPE_NUMBER_ZERO: return 0; case TYPE_NUMBER_BYTE: { uint8_t n; const uint8_t * pn = (const uint8_t *)rb_read(rb,sizeof(n)); if (pn == NULL) invalid_stream(L,rb); n = *pn; return n; } case TYPE_NUMBER_WORD: { uint16_t n; const void * pn = rb_read(rb,sizeof(n)); if (pn == NULL) invalid_stream(L,rb); memcpy(&n, pn, sizeof(n)); return n; } case TYPE_NUMBER_DWORD: { int32_t n; const void * pn = rb_read(rb,sizeof(n)); if (pn == NULL) invalid_stream(L,rb); memcpy(&n, pn, sizeof(n)); return n; } case TYPE_NUMBER_QWORD: { int64_t n; const void * pn = rb_read(rb,sizeof(n)); if (pn == NULL) invalid_stream(L,rb); memcpy(&n, pn, sizeof(n)); return n; } default: invalid_stream(L,rb); return 0; } } static double get_real(lua_State *L, struct read_block *rb) { double n; const void * pn = rb_read(rb,sizeof(n)); if (pn == NULL) invalid_stream(L,rb); memcpy(&n, pn, sizeof(n)); return n; } static void * get_pointer(lua_State *L, struct read_block *rb) { void * userdata = 0; const void * v = rb_read(rb,sizeof(userdata)); if (v == NULL) { invalid_stream(L,rb); } memcpy(&userdata, v, sizeof(userdata)); return userdata; } static void get_buffer(lua_State *L, struct read_block *rb, int len) { const char * p = (const char *)rb_read(rb,len); if (p == NULL) { invalid_stream(L,rb); } lua_pushlstring(L,p,len); } static void unpack_one(lua_State *L, struct read_block *rb); static void unpack_table(lua_State *L, struct read_block *rb, int array_size) { if (array_size == MAX_COOKIE-1) { uint8_t type; const uint8_t * t = (const uint8_t *)rb_read(rb, sizeof(type)); if (t==NULL) { invalid_stream(L,rb); } type = *t; int cookie = type >> 3; if ((type & 7) != TYPE_NUMBER || cookie == TYPE_NUMBER_REAL) { invalid_stream(L,rb); } array_size = get_integer(L,rb,cookie); } luaL_checkstack(L,LUA_MINSTACK,NULL); lua_createtable(L,array_size,0); int i; for (i=1;i<=array_size;i++) { unpack_one(L,rb); lua_rawseti(L,-2,i); } for (;;) { unpack_one(L,rb); if (lua_isnil(L,-1)) { lua_pop(L,1); return; } unpack_one(L,rb); lua_rawset(L,-3); } } static void push_value(lua_State *L, struct read_block *rb, int type, int cookie) { switch(type) { case TYPE_NIL: lua_pushnil(L); break; case TYPE_BOOLEAN: lua_pushboolean(L,cookie); break; case TYPE_NUMBER: if (cookie == TYPE_NUMBER_REAL) { lua_pushnumber(L,get_real(L,rb)); } else { lua_pushinteger(L, get_integer(L, rb, cookie)); } break; case TYPE_USERDATA: lua_pushlightuserdata(L,get_pointer(L,rb)); break; case TYPE_SHORT_STRING: get_buffer(L,rb,cookie); break; case TYPE_LONG_STRING: { if (cookie == 2) { const void * plen = rb_read(rb, 2); if (plen == NULL) { invalid_stream(L,rb); } uint16_t n; memcpy(&n, plen, sizeof(n)); get_buffer(L,rb,n); } else { if (cookie != 4) { invalid_stream(L,rb); } const void * plen = rb_read(rb, 4); if (plen == NULL) { invalid_stream(L,rb); } uint32_t n; memcpy(&n, plen, sizeof(n)); get_buffer(L,rb,n); } break; } case TYPE_TABLE: { unpack_table(L,rb,cookie); break; } default: { invalid_stream(L,rb); break; } } } static void unpack_one(lua_State *L, struct read_block *rb) { uint8_t type; const uint8_t * t = (const uint8_t *)rb_read(rb, sizeof(type)); if (t==NULL) { invalid_stream(L, rb); } type = *t; push_value(L, rb, type & 0x7, type>>3); } static void seri(lua_State *L, struct block *b, int len) { uint8_t * buffer = skynet_malloc(len); uint8_t * ptr = buffer; int sz = len; while(len>0) { if (len >= BLOCK_SIZE) { memcpy(ptr, b->buffer, BLOCK_SIZE); ptr += BLOCK_SIZE; len -= BLOCK_SIZE; b = b->next; } else { memcpy(ptr, b->buffer, len); break; } } lua_pushlightuserdata(L, buffer); lua_pushinteger(L, sz); } int luaseri_unpack(lua_State *L) { if (lua_isnoneornil(L,1)) { return 0; } void * buffer; int len; if (lua_type(L,1) == LUA_TSTRING) { size_t sz; buffer = (void *)lua_tolstring(L,1,&sz); len = (int)sz; } else { buffer = lua_touserdata(L,1); len = luaL_checkinteger(L,2); } if (len == 0) { return 0; } if (buffer == NULL) { return luaL_error(L, "deserialize null pointer"); } lua_settop(L,1); struct read_block rb; rball_init(&rb, buffer, len); int i; for (i=0;;i++) { if (i%8==7) { luaL_checkstack(L,LUA_MINSTACK,NULL); } uint8_t type = 0; const uint8_t * t = (const uint8_t *)rb_read(&rb, sizeof(type)); if (t==NULL) break; type = *t; push_value(L, &rb, type & 0x7, type>>3); } // Need not free buffer return lua_gettop(L) - 1; } LUAMOD_API int luaseri_pack(lua_State *L) { struct block temp; temp.next = NULL; struct write_block wb; wb_init(&wb, &temp); pack_from(L,&wb,0); assert(wb.head == &temp); seri(L, &temp, wb.len); wb_free(&wb); return 2; } ================================================ FILE: lualib-src/lua-seri.h ================================================ #ifndef LUA_SERIALIZE_H #define LUA_SERIALIZE_H #include int luaseri_pack(lua_State *L); int luaseri_unpack(lua_State *L); #endif ================================================ FILE: lualib-src/lua-sharedata.c ================================================ #define LUA_LIB #include #include #include #include #include #include #include "atomic.h" #define KEYTYPE_INTEGER 0 #define KEYTYPE_STRING 1 #define VALUETYPE_NIL 0 #define VALUETYPE_REAL 1 #define VALUETYPE_STRING 2 #define VALUETYPE_BOOLEAN 3 #define VALUETYPE_TABLE 4 #define VALUETYPE_INTEGER 5 struct table; union value { lua_Number n; lua_Integer d; struct table * tbl; int string; int boolean; }; struct node { union value v; int key; // integer key or index of string table int next; // next slot index uint32_t keyhash; uint8_t keytype; // key type must be integer or string uint8_t valuetype; // value type can be number/string/boolean/table uint8_t nocolliding; // 0 means colliding slot }; struct state { int dirty; ATOM_INT ref; struct table * root; }; struct table { int sizearray; int sizehash; uint8_t *arraytype; union value * array; struct node * hash; lua_State * L; }; struct context { lua_State * L; struct table * tbl; int string_index; }; struct ctrl { struct table * root; struct table * update; }; static int countsize(lua_State *L, int sizearray) { int n = 0; lua_pushnil(L); while (lua_next(L, 1) != 0) { int type = lua_type(L, -2); ++n; if (type == LUA_TNUMBER) { if (!lua_isinteger(L, -2)) { luaL_error(L, "Invalid key %f", lua_tonumber(L, -2)); } lua_Integer nkey = lua_tointeger(L, -2); if (nkey > 0 && nkey <= sizearray) { --n; } } else if (type != LUA_TSTRING && type != LUA_TTABLE) { luaL_error(L, "Invalid key type %s", lua_typename(L, type)); } lua_pop(L, 1); } return n; } static uint32_t calchash(const char * str, size_t l) { uint32_t h = (uint32_t)l; size_t l1; size_t step = (l >> 5) + 1; for (l1 = l; l1 >= step; l1 -= step) { h = h ^ ((h<<5) + (h>>2) + (uint8_t)(str[l1 - 1])); } return h; } static int stringindex(struct context *ctx, const char * str, size_t sz) { lua_State *L = ctx->L; lua_pushlstring(L, str, sz); lua_pushvalue(L, -1); lua_rawget(L, 1); int index; // stringmap(1) str index if (lua_isnil(L, -1)) { index = ++ctx->string_index; lua_pop(L, 1); lua_pushinteger(L, index); lua_rawset(L, 1); } else { index = lua_tointeger(L, -1); lua_pop(L, 2); } return index; } static int convtable(lua_State *L); static void setvalue(struct context * ctx, lua_State *L, int index, struct node *n) { int vt = lua_type(L, index); switch(vt) { case LUA_TNIL: n->valuetype = VALUETYPE_NIL; break; case LUA_TNUMBER: if (lua_isinteger(L, index)) { n->v.d = lua_tointeger(L, index); n->valuetype = VALUETYPE_INTEGER; } else { n->v.n = lua_tonumber(L, index); n->valuetype = VALUETYPE_REAL; } break; case LUA_TSTRING: { size_t sz = 0; const char * str = lua_tolstring(L, index, &sz); n->v.string = stringindex(ctx, str, sz); n->valuetype = VALUETYPE_STRING; break; } case LUA_TBOOLEAN: n->v.boolean = lua_toboolean(L, index); n->valuetype = VALUETYPE_BOOLEAN; break; case LUA_TTABLE: { struct table *tbl = ctx->tbl; ctx->tbl = (struct table *)malloc(sizeof(struct table)); if (ctx->tbl == NULL) { ctx->tbl = tbl; luaL_error(L, "memory error"); // never get here } memset(ctx->tbl, 0, sizeof(struct table)); int absidx = lua_absindex(L, index); lua_pushcfunction(L, convtable); lua_pushvalue(L, absidx); lua_pushlightuserdata(L, ctx); lua_call(L, 2, 0); n->v.tbl = ctx->tbl; n->valuetype = VALUETYPE_TABLE; ctx->tbl = tbl; break; } default: luaL_error(L, "Unsupport value type %s", lua_typename(L, vt)); break; } } static void setarray(struct context *ctx, lua_State *L, int index, int key) { struct node n; setvalue(ctx, L, index, &n); struct table *tbl = ctx->tbl; --key; // base 0 tbl->arraytype[key] = n.valuetype; tbl->array[key] = n.v; } static int ishashkey(struct context * ctx, lua_State *L, int index, int *key, uint32_t *keyhash, int *keytype) { int sizearray = ctx->tbl->sizearray; int kt = lua_type(L, index); if (kt == LUA_TNUMBER) { *key = lua_tointeger(L, index); if (*key > 0 && *key <= sizearray) { return 0; } *keyhash = (uint32_t)*key; *keytype = KEYTYPE_INTEGER; } else { size_t sz = 0; const char * s = lua_tolstring(L, index, &sz); *keyhash = calchash(s, sz); *key = stringindex(ctx, s, sz); *keytype = KEYTYPE_STRING; } return 1; } static void fillnocolliding(lua_State *L, struct context *ctx) { struct table * tbl = ctx->tbl; lua_pushnil(L); while (lua_next(L, 1) != 0) { int key; int keytype; uint32_t keyhash; if (!ishashkey(ctx, L, -2, &key, &keyhash, &keytype)) { setarray(ctx, L, -1, key); } else { struct node * n = &tbl->hash[keyhash % tbl->sizehash]; if (n->valuetype == VALUETYPE_NIL) { n->key = key; n->keytype = keytype; n->keyhash = keyhash; n->next = -1; n->nocolliding = 1; setvalue(ctx, L, -1, n); // set n->v , n->valuetype } } lua_pop(L,1); } } static void fillcolliding(lua_State *L, struct context *ctx) { struct table * tbl = ctx->tbl; int sizehash = tbl->sizehash; int emptyslot = 0; int i; lua_pushnil(L); while (lua_next(L, 1) != 0) { int key; int keytype; uint32_t keyhash; if (ishashkey(ctx, L, -2, &key, &keyhash, &keytype)) { struct node * mainpos = &tbl->hash[keyhash % tbl->sizehash]; if (!(mainpos->keytype == keytype && mainpos->key == key)) { // the key has not insert struct node * n = NULL; for (i=emptyslot;ihash[i].valuetype == VALUETYPE_NIL) { n = &tbl->hash[i]; emptyslot = i + 1; break; } } assert(n); n->next = mainpos->next; mainpos->next = n - tbl->hash; mainpos->nocolliding = 0; n->key = key; n->keytype = keytype; n->keyhash = keyhash; n->nocolliding = 0; setvalue(ctx, L, -1, n); // set n->v , n->valuetype } } lua_pop(L,1); } } // table need convert // struct context * ctx static int convtable(lua_State *L) { int i; struct context *ctx = lua_touserdata(L,2); struct table *tbl = ctx->tbl; tbl->L = ctx->L; int sizearray = lua_rawlen(L, 1); if (sizearray) { tbl->arraytype = (uint8_t *)malloc(sizearray * sizeof(uint8_t)); if (tbl->arraytype == NULL) { goto memerror; } for (i=0;iarraytype[i] = VALUETYPE_NIL; } tbl->array = (union value *)malloc(sizearray * sizeof(union value)); if (tbl->array == NULL) { goto memerror; } tbl->sizearray = sizearray; } int sizehash = countsize(L, sizearray); if (sizehash) { tbl->hash = (struct node *)malloc(sizehash * sizeof(struct node)); if (tbl->hash == NULL) { goto memerror; } for (i=0;ihash[i].valuetype = VALUETYPE_NIL; tbl->hash[i].nocolliding = 0; tbl->hash[i].next = -1; } tbl->sizehash = sizehash; fillnocolliding(L, ctx); fillcolliding(L, ctx); } else { int i; for (i=1;i<=sizearray;i++) { lua_rawgeti(L, 1, i); setarray(ctx, L, -1, i); lua_pop(L,1); } } return 0; memerror: return luaL_error(L, "memory error"); } static void delete_tbl(struct table *tbl) { int i; for (i=0;isizearray;i++) { if (tbl->arraytype[i] == VALUETYPE_TABLE) { delete_tbl(tbl->array[i].tbl); } } for (i=0;isizehash;i++) { if (tbl->hash[i].valuetype == VALUETYPE_TABLE) { delete_tbl(tbl->hash[i].v.tbl); } } free(tbl->arraytype); free(tbl->array); free(tbl->hash); free(tbl); } static int pconv(lua_State *L) { struct context *ctx = lua_touserdata(L,1); lua_State * pL = lua_touserdata(L, 2); int ret; lua_settop(L, 0); // init L (may throw memory error) // create a table for string map lua_newtable(L); lua_pushcfunction(pL, convtable); lua_pushvalue(pL,1); lua_pushlightuserdata(pL, ctx); ret = lua_pcall(pL, 2, 0, 0); if (ret != LUA_OK) { size_t sz = 0; const char * error = lua_tolstring(pL, -1, &sz); lua_pushlstring(L, error, sz); lua_error(L); // never get here } luaL_checkstack(L, ctx->string_index + 3, NULL); lua_settop(L,1); return 1; } static void convert_stringmap(struct context *ctx, struct table *tbl) { lua_State *L = ctx->L; lua_checkstack(L, ctx->string_index + LUA_MINSTACK); lua_settop(L, ctx->string_index + 1); lua_pushvalue(L, 1); struct state * s = lua_newuserdatauv(L, sizeof(*s), 1); s->dirty = 0; ATOM_INIT(&s->ref , 0); s->root = tbl; lua_replace(L, 1); lua_replace(L, -2); lua_pushnil(L); // ... stringmap nil while (lua_next(L, -2) != 0) { int idx = lua_tointeger(L, -1); lua_pop(L, 1); lua_pushvalue(L, -1); lua_replace(L, idx); } lua_pop(L, 1); lua_gc(L, LUA_GCCOLLECT, 0); } static int lnewconf(lua_State *L) { int ret; struct context ctx; struct table * tbl = NULL; luaL_checktype(L,1,LUA_TTABLE); ctx.L = luaL_newstate(); ctx.tbl = NULL; ctx.string_index = 1; // 1 reserved for dirty flag if (ctx.L == NULL) { lua_pushliteral(L, "memory error"); goto error; } tbl = (struct table *)malloc(sizeof(struct table)); if (tbl == NULL) { // lua_pushliteral may fail because of memory error, close first. lua_close(ctx.L); ctx.L = NULL; lua_pushliteral(L, "memory error"); goto error; } memset(tbl, 0, sizeof(struct table)); ctx.tbl = tbl; lua_pushcfunction(ctx.L, pconv); lua_pushlightuserdata(ctx.L , &ctx); lua_pushlightuserdata(ctx.L , L); ret = lua_pcall(ctx.L, 2, 1, 0); if (ret != LUA_OK) { size_t sz = 0; const char * error = lua_tolstring(ctx.L, -1, &sz); lua_pushlstring(L, error, sz); goto error; } convert_stringmap(&ctx, tbl); lua_pushlightuserdata(L, tbl); return 1; error: if (ctx.L) { lua_close(ctx.L); } if (tbl) { delete_tbl(tbl); } lua_error(L); return -1; } static struct table * get_table(lua_State *L, int index) { struct table *tbl = lua_touserdata(L,index); if (tbl == NULL) { luaL_error(L, "Need a conf object"); } return tbl; } static int ldeleteconf(lua_State *L) { struct table *tbl = get_table(L,1); lua_close(tbl->L); delete_tbl(tbl); return 0; } static void pushvalue(lua_State *L, lua_State *sL, uint8_t vt, union value *v) { switch(vt) { case VALUETYPE_REAL: lua_pushnumber(L, v->n); break; case VALUETYPE_INTEGER: lua_pushinteger(L, v->d); break; case VALUETYPE_STRING: { size_t sz = 0; const char *str = lua_tolstring(sL, v->string, &sz); lua_pushlstring(L, str, sz); break; } case VALUETYPE_BOOLEAN: lua_pushboolean(L, v->boolean); break; case VALUETYPE_TABLE: lua_pushlightuserdata(L, v->tbl); break; default: lua_pushnil(L); break; } } static struct node * lookup_key(struct table *tbl, uint32_t keyhash, int key, int keytype, const char *str, size_t sz) { if (tbl->sizehash == 0) return NULL; struct node *n = &tbl->hash[keyhash % tbl->sizehash]; if (keyhash != n->keyhash && n->nocolliding) return NULL; for (;;) { if (keyhash == n->keyhash) { if (n->keytype == KEYTYPE_INTEGER) { if (keytype == KEYTYPE_INTEGER && n->key == key) { return n; } } else { // n->keytype == KEYTYPE_STRING if (keytype == KEYTYPE_STRING) { size_t sz2 = 0; const char * str2 = lua_tolstring(tbl->L, n->key, &sz2); if (sz == sz2 && memcmp(str,str2,sz) == 0) { return n; } } } } if (n->next < 0) { return NULL; } n = &tbl->hash[n->next]; } } static int lindexconf(lua_State *L) { struct table *tbl = get_table(L,1); int kt = lua_type(L,2); uint32_t keyhash; int key = 0; int keytype; size_t sz = 0; const char * str = NULL; if (kt == LUA_TNIL) { return 0; } else if (kt == LUA_TNUMBER) { if (!lua_isinteger(L, 2)) { return luaL_error(L, "Invalid key %f", lua_tonumber(L, 2)); } key = (int)lua_tointeger(L, 2); if (key > 0 && key <= tbl->sizearray) { --key; pushvalue(L, tbl->L, tbl->arraytype[key], &tbl->array[key]); return 1; } keytype = KEYTYPE_INTEGER; keyhash = (uint32_t)key; } else { str = luaL_checklstring(L, 2, &sz); keyhash = calchash(str, sz); keytype = KEYTYPE_STRING; } struct node *n = lookup_key(tbl, keyhash, key, keytype, str, sz); if (n) { pushvalue(L, tbl->L, n->valuetype, &n->v); return 1; } else { return 0; } } static void pushkey(lua_State *L, lua_State *sL, struct node *n) { if (n->keytype == KEYTYPE_INTEGER) { lua_pushinteger(L, n->key); } else { size_t sz = 0; const char * str = lua_tolstring(sL, n->key, &sz); lua_pushlstring(L, str, sz); } } static int pushfirsthash(lua_State *L, struct table * tbl) { if (tbl->sizehash) { pushkey(L, tbl->L, &tbl->hash[0]); return 1; } else { return 0; } } static int lnextkey(lua_State *L) { struct table *tbl = get_table(L,1); if (lua_isnoneornil(L,2)) { if (tbl->sizearray > 0) { int i; for (i=0;isizearray;i++) { if (tbl->arraytype[i] != VALUETYPE_NIL) { lua_pushinteger(L, i+1); return 1; } } } return pushfirsthash(L, tbl); } int kt = lua_type(L,2); uint32_t keyhash; int key = 0; int keytype; size_t sz=0; const char *str = NULL; int sizearray = tbl->sizearray; if (kt == LUA_TNUMBER) { if (!lua_isinteger(L, 2)) { return 0; } key = (int)lua_tointeger(L, 2); if (key > 0 && key <= sizearray) { lua_Integer i; for (i=key;iarraytype[i] != VALUETYPE_NIL) { lua_pushinteger(L, i+1); return 1; } } return pushfirsthash(L, tbl); } keyhash = (uint32_t)key; keytype = KEYTYPE_INTEGER; } else { str = luaL_checklstring(L, 2, &sz); keyhash = calchash(str, sz); keytype = KEYTYPE_STRING; } struct node *n = lookup_key(tbl, keyhash, key, keytype, str, sz); if (n) { ++n; int index = n-tbl->hash; if (index == tbl->sizehash) { return 0; } pushkey(L, tbl->L, n); return 1; } else { return 0; } } static int llen(lua_State *L) { struct table *tbl = get_table(L,1); lua_pushinteger(L, tbl->sizearray); return 1; } static int lhashlen(lua_State *L) { struct table *tbl = get_table(L,1); lua_pushinteger(L, tbl->sizehash); return 1; } static int releaseobj(lua_State *L) { struct ctrl *c = lua_touserdata(L, 1); struct table *tbl = c->root; struct state *s = lua_touserdata(tbl->L, 1); ATOM_FDEC(&s->ref); c->root = NULL; c->update = NULL; return 0; } static int lboxconf(lua_State *L) { struct table * tbl = get_table(L,1); struct state * s = lua_touserdata(tbl->L, 1); ATOM_FINC(&s->ref); struct ctrl * c = lua_newuserdatauv(L, sizeof(*c), 1); c->root = tbl; c->update = NULL; if (luaL_newmetatable(L, "confctrl")) { lua_pushcfunction(L, releaseobj); lua_setfield(L, -2, "__gc"); } lua_setmetatable(L, -2); return 1; } static int lmarkdirty(lua_State *L) { struct table *tbl = get_table(L,1); struct state * s = lua_touserdata(tbl->L, 1); s->dirty = 1; return 0; } static int lisdirty(lua_State *L) { struct table *tbl = get_table(L,1); struct state * s = lua_touserdata(tbl->L, 1); int d = s->dirty; lua_pushboolean(L, d); return 1; } static int lgetref(lua_State *L) { struct table *tbl = get_table(L,1); struct state * s = lua_touserdata(tbl->L, 1); lua_pushinteger(L , ATOM_LOAD(&s->ref)); return 1; } static int lincref(lua_State *L) { struct table *tbl = get_table(L,1); struct state * s = lua_touserdata(tbl->L, 1); int ref = ATOM_FINC(&s->ref)+1; lua_pushinteger(L , ref); return 1; } static int ldecref(lua_State *L) { struct table *tbl = get_table(L,1); struct state * s = lua_touserdata(tbl->L, 1); int ref = ATOM_FDEC(&s->ref)-1; lua_pushinteger(L , ref); return 1; } static int lneedupdate(lua_State *L) { struct ctrl * c = lua_touserdata(L, 1); if (c->update) { lua_pushlightuserdata(L, c->update); lua_getiuservalue(L, 1, 1); return 2; } return 0; } static int lupdate(lua_State *L) { luaL_checktype(L, 1, LUA_TUSERDATA); luaL_checktype(L, 2, LUA_TLIGHTUSERDATA); luaL_checktype(L, 3, LUA_TTABLE); struct ctrl * c= lua_touserdata(L, 1); struct table *n = lua_touserdata(L, 2); if (c->root == n) { return luaL_error(L, "You should update a new object"); } lua_settop(L, 3); lua_setiuservalue(L, 1, 1); c->update = n; return 0; } LUAMOD_API int luaopen_skynet_sharedata_core(lua_State *L) { luaL_Reg l[] = { // used by host { "new", lnewconf }, { "delete", ldeleteconf }, { "markdirty", lmarkdirty }, { "getref", lgetref }, { "incref", lincref }, { "decref", ldecref }, // used by client { "box", lboxconf }, { "index", lindexconf }, { "nextkey", lnextkey }, { "len", llen }, { "hashlen", lhashlen }, { "isdirty", lisdirty }, { "needupdate", lneedupdate }, { "update", lupdate }, { NULL, NULL }, }; luaL_checkversion(L); luaL_newlib(L, l); return 1; } ================================================ FILE: lualib-src/lua-sharetable.c ================================================ #define LUA_LIB #include #include #include #include "lgc.h" #ifdef makeshared static void mark_shared(lua_State *L) { if (lua_type(L, -1) != LUA_TTABLE) { luaL_error(L, "Not a table, it's a %s.", lua_typename(L, lua_type(L, -1))); } Table * t = (Table *)lua_topointer(L, -1); if (isshared(t)) return; makeshared(t); luaL_checkstack(L, 4, NULL); if (lua_getmetatable(L, -1)) { luaL_error(L, "Can't share metatable"); } lua_pushnil(L); while (lua_next(L, -2) != 0) { int i; for (i=0;i<2;i++) { int idx = -i-1; int t = lua_type(L, idx); switch (t) { case LUA_TTABLE: mark_shared(L); break; case LUA_TNUMBER: case LUA_TBOOLEAN: case LUA_TLIGHTUSERDATA: break; case LUA_TFUNCTION: if (lua_getupvalue(L, idx, 1) != NULL) { luaL_error(L, "Invalid function with upvalue"); } else if (!lua_iscfunction(L, idx)) { LClosure *f = (LClosure *)lua_topointer(L, idx); makeshared(f); lua_sharefunction(L, idx); } break; case LUA_TSTRING: lua_sharestring(L, idx); break; default: luaL_error(L, "Invalid type [%s]", lua_typename(L, t)); break; } } lua_pop(L, 1); } } static int lis_sharedtable(lua_State* L) { int b = 0; if(lua_type(L, 1) == LUA_TTABLE) { Table * t = (Table *)lua_topointer(L, 1); b = isshared(t); } lua_pushboolean(L, b); return 1; } static int make_matrix(lua_State *L) { // turn off gc , because marking shared will prevent gc mark. lua_gc(L, LUA_GCSTOP, 0); mark_shared(L); Table * t = (Table *)lua_topointer(L, -1); lua_pushlightuserdata(L, t); return 1; } static int clone_table(lua_State *L) { lua_clonetable(L, lua_touserdata(L, 1)); return 1; } static int lco_stackvalues(lua_State* L) { lua_State *cL = lua_tothread(L, 1); luaL_argcheck(L, cL, 1, "thread expected"); int n = 0; if(cL != L) { luaL_checktype(L, 2, LUA_TTABLE); n = lua_gettop(cL); if(n > 0) { luaL_checkstack(L, n+1, NULL); int top = lua_gettop(L); lua_xmove(cL, L, n); int i=0; for(i=1; i<=n; i++) { lua_pushvalue(L, top+i); lua_seti(L, 2, i); } lua_xmove(L, cL, n); } } lua_pushinteger(L, n); return 1; } struct state_ud { lua_State *L; }; static int close_state(lua_State *L) { struct state_ud *ud = (struct state_ud *)luaL_checkudata(L, 1, "BOXMATRIXSTATE"); if (ud->L) { lua_close(ud->L); ud->L = NULL; } return 0; } static int get_matrix(lua_State *L) { struct state_ud *ud = (struct state_ud *)luaL_checkudata(L, 1, "BOXMATRIXSTATE"); if (ud->L) { const void * v = lua_topointer(ud->L, 1); lua_pushlightuserdata(L, (void *)v); return 1; } return 0; } static int get_size(lua_State *L) { struct state_ud *ud = (struct state_ud *)luaL_checkudata(L, 1, "BOXMATRIXSTATE"); if (ud->L) { lua_Integer sz = lua_gc(ud->L, LUA_GCCOUNT, 0); sz *= 1024; sz += lua_gc(ud->L, LUA_GCCOUNTB, 0); lua_pushinteger(L, sz); } else { lua_pushinteger(L, 0); } return 1; } static int box_state(lua_State *L, lua_State *mL) { struct state_ud *ud = (struct state_ud *)lua_newuserdatauv(L, sizeof(*ud), 0); ud->L = mL; if (luaL_newmetatable(L, "BOXMATRIXSTATE")) { lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, close_state); lua_setfield(L, -2, "close"); lua_pushcfunction(L, get_matrix); lua_setfield(L, -2, "getptr"); lua_pushcfunction(L, get_size); lua_setfield(L, -2, "size"); } lua_setmetatable(L, -2); return 1; } static int load_matrixfile(lua_State *L) { luaL_openlibs(L); const char * source = (const char *)lua_touserdata(L, 1); if (source[0] == '@') { if (luaL_loadfilex_(L, source+1, NULL) != LUA_OK) lua_error(L); } else { if (luaL_loadstring(L, source) != LUA_OK) lua_error(L); } lua_replace(L, 1); if (lua_pcall(L, lua_gettop(L) - 1, 1, 0) != LUA_OK) lua_error(L); lua_gc(L, LUA_GCCOLLECT, 0); lua_pushcfunction(L, make_matrix); lua_insert(L, -2); lua_call(L, 1, 1); return 1; } static int matrix_from_file(lua_State *L) { lua_State *mL = lua_newstate(luaL_alloc, NULL, G(L)->seed); if (mL == NULL) { return luaL_error(L, "luaL_newstate failed"); } const char * source = luaL_checkstring(L, 1); int top = lua_gettop(L); lua_pushcfunction(mL, load_matrixfile); lua_pushlightuserdata(mL, (void *)source); if (top > 1) { if (!lua_checkstack(mL, top + 1)) { return luaL_error(L, "Too many argument %d", top); } int i; for (i=2;i<=top;i++) { switch(lua_type(L, i)) { case LUA_TBOOLEAN: lua_pushboolean(mL, lua_toboolean(L, i)); break; case LUA_TNUMBER: if (lua_isinteger(L, i)) { lua_pushinteger(mL, lua_tointeger(L, i)); } else { lua_pushnumber(mL, lua_tonumber(L, i)); } break; case LUA_TLIGHTUSERDATA: lua_pushlightuserdata(mL, lua_touserdata(L, i)); break; case LUA_TFUNCTION: if (lua_iscfunction(L, i) && lua_getupvalue(L, i, 1) == NULL) { lua_pushcfunction(mL, lua_tocfunction(L, i)); break; } return luaL_argerror(L, i, "Only support light C function"); default: return luaL_argerror(L, i, "Type invalid"); } } } int ok = lua_pcall(mL, top, 1, 0); if (ok != LUA_OK) { lua_pushstring(L, lua_tostring(mL, -1)); lua_close(mL); lua_error(L); } return box_state(L, mL); } LUAMOD_API int luaopen_skynet_sharetable_core(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "clone", clone_table }, { "stackvalues", lco_stackvalues }, { "matrix", matrix_from_file }, { "is_sharedtable", lis_sharedtable }, { NULL, NULL }, }; luaL_newlib(L, l); return 1; } #else LUAMOD_API int luaopen_skynet_sharetable_core(lua_State *L) { return luaL_error(L, "No share string table support"); } #endif ================================================ FILE: lualib-src/lua-skynet.c ================================================ #define LUA_LIB #include "skynet.h" #include "lua-seri.h" #define KNRM "\x1B[0m" #define KRED "\x1B[31m" #include #include #include #include #include #include #include #if defined(__APPLE__) #include #endif #include "skynet.h" // return nsec static int64_t get_time() { #if !defined(__APPLE__) || defined(AVAILABLE_MAC_OS_X_VERSION_10_12_AND_LATER) struct timespec ti; clock_gettime(CLOCK_MONOTONIC, &ti); return (int64_t)1000000000 * ti.tv_sec + ti.tv_nsec; #else struct timeval tv; gettimeofday(&tv, NULL); return (int64_t)1000000000 * tv.tv_sec + tv.tv_usec * 1000; #endif } static int traceback (lua_State *L) { const char *msg = lua_tostring(L, 1); if (msg) luaL_traceback(L, L, msg, 1); else { lua_pushliteral(L, "(no error message)"); } return 1; } struct callback_context { lua_State *L; }; static int _cb(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { struct callback_context *cb_ctx = (struct callback_context *)ud; lua_State *L = cb_ctx->L; int trace = 1; int r; lua_pushvalue(L,2); lua_pushinteger(L, type); lua_pushlightuserdata(L, (void *)msg); lua_pushinteger(L,sz); lua_pushinteger(L, session); lua_pushinteger(L, source); r = lua_pcall(L, 5, 0 , trace); if (r == LUA_OK) { return 0; } const char * self = skynet_command(context, "REG", NULL); switch (r) { case LUA_ERRRUN: skynet_error(context, "lua call [%x to %s : %d msgsz = %d] error : " KRED "%s" KNRM, source , self, session, sz, lua_tostring(L,-1)); break; case LUA_ERRMEM: skynet_error(context, "lua memory error : [%x to %s : %d]", source , self, session); break; case LUA_ERRERR: skynet_error(context, "lua error in error : [%x to %s : %d]", source , self, session); break; }; lua_pop(L,1); return 0; } static int forward_cb(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { _cb(context, ud, type, session, source, msg, sz); // don't delete msg in forward mode. return 1; } static void clear_last_context(lua_State *L) { if (lua_getfield(L, LUA_REGISTRYINDEX, "callback_context") == LUA_TUSERDATA) { lua_pushnil(L); lua_setiuservalue(L, -2, 2); } lua_pop(L, 1); } static int _cb_pre(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { struct callback_context *cb_ctx = (struct callback_context *)ud; clear_last_context(cb_ctx->L); skynet_callback(context, ud, _cb); return _cb(context, cb_ctx, type, session, source, msg, sz); } static int _forward_pre(struct skynet_context *context, void *ud, int type, int session, uint32_t source, const void *msg, size_t sz) { struct callback_context *cb_ctx = (struct callback_context *)ud; clear_last_context(cb_ctx->L); skynet_callback(context, ud, forward_cb); return forward_cb(context, cb_ctx, type, session, source, msg, sz); } static int lcallback(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); int forward = lua_toboolean(L, 2); luaL_checktype(L,1,LUA_TFUNCTION); lua_settop(L,1); struct callback_context * cb_ctx = (struct callback_context *)lua_newuserdatauv(L, sizeof(*cb_ctx), 2); cb_ctx->L = lua_newthread(L); lua_pushcfunction(cb_ctx->L, traceback); lua_setiuservalue(L, -2, 1); lua_getfield(L, LUA_REGISTRYINDEX, "callback_context"); lua_setiuservalue(L, -2, 2); lua_setfield(L, LUA_REGISTRYINDEX, "callback_context"); lua_xmove(L, cb_ctx->L, 1); skynet_callback(context, cb_ctx, (forward)?(_forward_pre):(_cb_pre)); return 0; } static int lcommand(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); const char * cmd = luaL_checkstring(L,1); const char * result; const char * parm = NULL; if (lua_gettop(L) == 2) { parm = luaL_checkstring(L,2); } result = skynet_command(context, cmd, parm); if (result) { lua_pushstring(L, result); return 1; } return 0; } static int laddresscommand(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); const char * cmd = luaL_checkstring(L,1); const char * result; const char * parm = NULL; if (lua_gettop(L) == 2) { parm = luaL_checkstring(L,2); } result = skynet_command(context, cmd, parm); if (result && result[0] == ':') { int i; uint32_t addr = 0; for (i=1;result[i];i++) { int c = result[i]; if (c>='0' && c<='9') { c = c - '0'; } else if (c>='a' && c<='f') { c = c - 'a' + 10; } else if (c>='A' && c<='F') { c = c - 'A' + 10; } else { return 0; } addr = addr * 16 + c; } lua_pushinteger(L, addr); return 1; } return 0; } static int lintcommand(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); const char * cmd = luaL_checkstring(L,1); const char * result; const char * parm = NULL; char tmp[64]; // for integer parm if (lua_gettop(L) == 2) { if (lua_isnumber(L, 2)) { int32_t n = (int32_t)luaL_checkinteger(L,2); sprintf(tmp, "%d", n); parm = tmp; } else { parm = luaL_checkstring(L,2); } } result = skynet_command(context, cmd, parm); if (result) { char *endptr = NULL; lua_Integer r = strtoll(result, &endptr, 0); if (endptr == NULL || *endptr != '\0') { // may be real number double n = strtod(result, &endptr); if (endptr == NULL || *endptr != '\0') { return luaL_error(L, "Invalid result %s", result); } else { lua_pushnumber(L, n); } } else { lua_pushinteger(L, r); } return 1; } return 0; } static int lgenid(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); int session = skynet_send(context, 0, 0, PTYPE_TAG_ALLOCSESSION , 0 , NULL, 0); lua_pushinteger(L, session); return 1; } static const char * get_dest_string(lua_State *L, int index) { const char * dest_string = lua_tostring(L, index); if (dest_string == NULL) { luaL_error(L, "dest address type (%s) must be a string or number.", lua_typename(L, lua_type(L,index))); } return dest_string; } static int send_message(lua_State *L, int source, int idx_type) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); uint32_t dest = (uint32_t)lua_tointeger(L, 1); const char * dest_string = NULL; if (dest == 0) { if (lua_type(L,1) == LUA_TNUMBER) { return luaL_error(L, "Invalid service address 0"); } dest_string = get_dest_string(L, 1); } int type = luaL_checkinteger(L, idx_type+0); int session = 0; if (lua_isnil(L,idx_type+1)) { type |= PTYPE_TAG_ALLOCSESSION; } else { session = luaL_checkinteger(L,idx_type+1); } int mtype = lua_type(L,idx_type+2); switch (mtype) { case LUA_TSTRING: { size_t len = 0; void * msg = (void *)lua_tolstring(L,idx_type+2,&len); if (len == 0) { msg = NULL; } if (dest_string) { session = skynet_sendname(context, source, dest_string, type, session , msg, len); } else { session = skynet_send(context, source, dest, type, session , msg, len); } break; } case LUA_TLIGHTUSERDATA: { void * msg = lua_touserdata(L,idx_type+2); int size = luaL_checkinteger(L,idx_type+3); if (dest_string) { session = skynet_sendname(context, source, dest_string, type | PTYPE_TAG_DONTCOPY, session, msg, size); } else { session = skynet_send(context, source, dest, type | PTYPE_TAG_DONTCOPY, session, msg, size); } break; } default: luaL_error(L, "invalid param %s", lua_typename(L, lua_type(L,idx_type+2))); } if (session < 0) { if (session == -2) { // package is too large lua_pushboolean(L, 0); return 1; } // send to invalid address // todo: maybe throw an error would be better return 0; } lua_pushinteger(L,session); return 1; } /* uint32 address string address integer type integer session string message lightuserdata message_ptr integer len */ static int lsend(lua_State *L) { return send_message(L, 0, 2); } /* uint32 address string address integer source_address integer type integer session string message lightuserdata message_ptr integer len */ static int lredirect(lua_State *L) { uint32_t source = (uint32_t)luaL_checkinteger(L,2); return send_message(L, source, 3); } static int lerror(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); int n = lua_gettop(L); if (n <= 1) { lua_settop(L, 1); size_t len; const char *s = luaL_tolstring(L, 1, &len); skynet_error(context, "%*s", (int)len, s); return 0; } luaL_Buffer b; luaL_buffinit(L, &b); int i; for (i=1; i<=n; i++) { luaL_tolstring(L, i, NULL); luaL_addvalue(&b); if (i= 0) ++index; } while (index < MAX_LEVEL); switch (index) { case 1: skynet_error(context, " %" PRId64 " %s : %s:%d", tag, get_time(), user, si[0].source, si[0].line); break; case 2: skynet_error(context, " %" PRId64 " %s : %s:%d %s:%d", tag, get_time(), user, si[0].source, si[0].line, si[1].source, si[1].line ); break; case 3: skynet_error(context, " %" PRId64 " %s : %s:%d %s:%d %s:%d", tag, get_time(), user, si[0].source, si[0].line, si[1].source, si[1].line, si[2].source, si[2].line ); break; default: skynet_error(context, " %" PRId64 " %s", tag, get_time(), user); break; } return 0; } skynet_error(context, " %" PRId64 " %s", tag, get_time(), user); return 0; } LUAMOD_API int luaopen_skynet_core(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "send" , lsend }, { "genid", lgenid }, { "redirect", lredirect }, { "command" , lcommand }, { "intcommand", lintcommand }, { "addresscommand", laddresscommand }, { "error", lerror }, { "harbor", lharbor }, { "callback", lcallback }, { "trace", ltrace }, { NULL, NULL }, }; // functions without skynet_context luaL_Reg l2[] = { { "tostring", ltostring }, { "pack", luaseri_pack }, { "unpack", luaseri_unpack }, { "packstring", lpackstring }, { "trash" , ltrash }, { "now", lnow }, { "hpc", lhpc }, // getHPCounter { NULL, NULL }, }; lua_createtable(L, 0, sizeof(l)/sizeof(l[0]) + sizeof(l2)/sizeof(l2[0]) -2); lua_getfield(L, LUA_REGISTRYINDEX, "skynet_context"); struct skynet_context *ctx = lua_touserdata(L,-1); if (ctx == NULL) { return luaL_error(L, "Init skynet context first"); } luaL_setfuncs(L,l,1); luaL_setfuncs(L,l2,0); return 1; } ================================================ FILE: lualib-src/lua-socket.c ================================================ #define LUA_LIB #include "skynet_malloc.h" #include #include #include #include #include #include #include #include #include #include #include #include #include "skynet.h" #include "skynet_socket.h" #define BACKLOG 32 // 2 ** 12 == 4096 #define LARGE_PAGE_NODE 12 #define POOL_SIZE_WARNING 32 #define BUFFER_LIMIT (256 * 1024) struct buffer_node { char * msg; int sz; struct buffer_node *next; }; struct socket_buffer { int size; int offset; struct buffer_node *head; struct buffer_node *tail; }; static int lfreepool(lua_State *L) { struct buffer_node * pool = lua_touserdata(L, 1); int sz = lua_rawlen(L,1) / sizeof(*pool); int i; for (i=0;imsg) { skynet_free(node->msg); node->msg = NULL; } } return 0; } static int lnewpool(lua_State *L, int sz) { struct buffer_node * pool = lua_newuserdatauv(L, sizeof(struct buffer_node) * sz, 0); int i; for (i=0;isize = 0; sb->offset = 0; sb->head = NULL; sb->tail = NULL; return 1; } /* userdata send_buffer table pool lightuserdata msg int size return size Comment: The table pool record all the buffers chunk, and the first index [1] is a lightuserdata : free_node. We can always use this pointer for struct buffer_node . The following ([2] ...) userdatas in table pool is the buffer chunk (for struct buffer_node), we never free them until the VM closed. The size of first chunk ([2]) is 16 struct buffer_node, and the second size is 32 ... The largest size of chunk is LARGE_PAGE_NODE (4096) lpushbbuffer will get a free struct buffer_node from table pool, and then put the msg/size in it. lpopbuffer return the struct buffer_node back to table pool (By calling return_free_node). */ static int lpushbuffer(lua_State *L) { struct socket_buffer *sb = lua_touserdata(L,1); if (sb == NULL) { return luaL_error(L, "need buffer object at param 1"); } char * msg = lua_touserdata(L,3); if (msg == NULL) { return luaL_error(L, "need message block at param 3"); } int pool_index = 2; luaL_checktype(L,pool_index,LUA_TTABLE); int sz = luaL_checkinteger(L,4); lua_rawgeti(L,pool_index,1); struct buffer_node * free_node = lua_touserdata(L,-1); // sb poolt msg size free_node lua_pop(L,1); if (free_node == NULL) { int tsz = lua_rawlen(L,pool_index); if (tsz == 0) tsz++; int size = 8; if (tsz <= LARGE_PAGE_NODE-3) { size <<= tsz; } else { size <<= LARGE_PAGE_NODE-3; } lnewpool(L, size); free_node = lua_touserdata(L,-1); lua_rawseti(L, pool_index, tsz+1); if (tsz > POOL_SIZE_WARNING) { skynet_error(NULL, "Too many socket pool (%d)", tsz); } } lua_pushlightuserdata(L, free_node->next); lua_rawseti(L, pool_index, 1); // sb poolt msg size free_node->msg = msg; free_node->sz = sz; free_node->next = NULL; if (sb->head == NULL) { assert(sb->tail == NULL); sb->head = sb->tail = free_node; } else { sb->tail->next = free_node; sb->tail = free_node; } sb->size += sz; lua_pushinteger(L, sb->size); return 1; } static void return_free_node(lua_State *L, int pool, struct socket_buffer *sb) { struct buffer_node *free_node = sb->head; sb->offset = 0; sb->head = free_node->next; if (sb->head == NULL) { sb->tail = NULL; } lua_rawgeti(L,pool,1); free_node->next = lua_touserdata(L,-1); lua_pop(L,1); skynet_free(free_node->msg); free_node->msg = NULL; free_node->sz = 0; lua_pushlightuserdata(L, free_node); lua_rawseti(L, pool, 1); } static void pop_lstring(lua_State *L, struct socket_buffer *sb, int sz, int skip) { struct buffer_node * current = sb->head; if (sz < current->sz - sb->offset) { lua_pushlstring(L, current->msg + sb->offset, sz-skip); sb->offset+=sz; return; } if (sz == current->sz - sb->offset) { lua_pushlstring(L, current->msg + sb->offset, sz-skip); return_free_node(L,2,sb); return; } luaL_Buffer b; luaL_buffinitsize(L, &b, sz); for (;;) { int bytes = current->sz - sb->offset; if (bytes >= sz) { if (sz > skip) { luaL_addlstring(&b, current->msg + sb->offset, sz - skip); } sb->offset += sz; if (bytes == sz) { return_free_node(L,2,sb); } break; } int real_sz = sz - skip; if (real_sz > 0) { luaL_addlstring(&b, current->msg + sb->offset, (real_sz < bytes) ? real_sz : bytes); } return_free_node(L,2,sb); sz-=bytes; if (sz==0) break; current = sb->head; assert(current); } luaL_pushresult(&b); } static int lheader(lua_State *L) { size_t len; const uint8_t * s = (const uint8_t *)luaL_checklstring(L, 1, &len); if (len > 4 || len < 1) { return luaL_error(L, "Invalid read %s", s); } int i; size_t sz = 0; for (i=0;i<(int)len;i++) { sz <<= 8; sz |= s[i]; } lua_pushinteger(L, (lua_Integer)sz); return 1; } /* userdata send_buffer table pool integer sz */ static int lpopbuffer(lua_State *L) { struct socket_buffer * sb = lua_touserdata(L, 1); if (sb == NULL) { return luaL_error(L, "Need buffer object at param 1"); } luaL_checktype(L,2,LUA_TTABLE); int sz = luaL_checkinteger(L,3); if (sb->size < sz || sz == 0) { lua_pushnil(L); } else { pop_lstring(L,sb,sz,0); sb->size -= sz; } lua_pushinteger(L, sb->size); return 2; } /* userdata send_buffer table pool */ static int lclearbuffer(lua_State *L) { struct socket_buffer * sb = lua_touserdata(L, 1); if (sb == NULL) { if (lua_isnil(L, 1)) { return 0; } return luaL_error(L, "Need buffer object at param 1"); } luaL_checktype(L,2,LUA_TTABLE); while(sb->head) { return_free_node(L,2,sb); } sb->size = 0; return 0; } static int lreadall(lua_State *L) { struct socket_buffer * sb = lua_touserdata(L, 1); if (sb == NULL) { return luaL_error(L, "Need buffer object at param 1"); } luaL_checktype(L,2,LUA_TTABLE); luaL_Buffer b; luaL_buffinit(L, &b); while(sb->head) { struct buffer_node *current = sb->head; luaL_addlstring(&b, current->msg + sb->offset, current->sz - sb->offset); return_free_node(L,2,sb); } luaL_pushresult(&b); sb->size = 0; return 1; } static int ldrop(lua_State *L) { void * msg = lua_touserdata(L,1); luaL_checkinteger(L,2); skynet_free(msg); return 0; } static bool check_sep(struct buffer_node * node, int from, const char *sep, int seplen) { for (;;) { int sz = node->sz - from; if (sz >= seplen) { return memcmp(node->msg+from,sep,seplen) == 0; } if (sz > 0) { if (memcmp(node->msg + from, sep, sz)) { return false; } } node = node->next; sep += sz; seplen -= sz; from = 0; } } /* userdata send_buffer table pool , nil for check string sep */ static int lreadline(lua_State *L) { struct socket_buffer * sb = lua_touserdata(L, 1); if (sb == NULL) { return luaL_error(L, "Need buffer object at param 1"); } // only check bool check = !lua_istable(L, 2); size_t seplen = 0; const char *sep = luaL_checklstring(L,3,&seplen); int i; struct buffer_node *current = sb->head; if (current == NULL) return 0; int from = sb->offset; int bytes = current->sz - from; for (i=0;i<=sb->size - (int)seplen;i++) { if (check_sep(current, from, sep, seplen)) { if (check) { lua_pushboolean(L,true); } else { pop_lstring(L, sb, i+seplen, seplen); sb->size -= i+seplen; } return 1; } ++from; --bytes; if (bytes == 0) { current = current->next; from = 0; if (current == NULL) break; bytes = current->sz; } } return 0; } static int lstr2p(lua_State *L) { size_t sz = 0; const char * str = luaL_checklstring(L,1,&sz); void *ptr = skynet_malloc(sz); memcpy(ptr, str, sz); lua_pushlightuserdata(L, ptr); lua_pushinteger(L, (int)sz); return 2; } // for skynet socket /* lightuserdata msg integer size return type n1 n2 ptr_or_string */ static int lunpack(lua_State *L) { struct skynet_socket_message *message = lua_touserdata(L,1); int size = luaL_checkinteger(L,2); lua_pushinteger(L, message->type); lua_pushinteger(L, message->id); lua_pushinteger(L, message->ud); if (message->buffer == NULL) { lua_pushlstring(L, (char *)(message+1),size - sizeof(*message)); } else { lua_pushlightuserdata(L, message->buffer); } if (message->type == SKYNET_SOCKET_TYPE_UDP) { int addrsz = 0; const char * addrstring = skynet_socket_udp_address(message, &addrsz); if (addrstring) { lua_pushlstring(L, addrstring, addrsz); return 5; } } return 4; } static const char * address_port(lua_State *L, char *tmp, const char * addr, int port_index, int *port) { const char * host; if (lua_isnoneornil(L,port_index)) { host = strchr(addr, '['); if (host) { // is ipv6 ++host; const char * sep = strchr(addr,']'); if (sep == NULL) { luaL_error(L, "Invalid address %s.",addr); } memcpy(tmp, host, sep-host); tmp[sep-host] = '\0'; host = tmp; sep = strchr(sep + 1, ':'); if (sep == NULL) { luaL_error(L, "Invalid address %s.",addr); } *port = strtoul(sep+1,NULL,10); } else { // is ipv4 const char * sep = strchr(addr,':'); if (sep == NULL) { luaL_error(L, "Invalid address %s.",addr); } memcpy(tmp, addr, sep-addr); tmp[sep-addr] = '\0'; host = tmp; *port = strtoul(sep+1,NULL,10); } } else { host = addr; *port = luaL_optinteger(L,port_index, 0); } return host; } static int lconnect(lua_State *L) { size_t sz = 0; const char * addr = luaL_checklstring(L,1,&sz); char tmp[sz]; int port = 0; const char * host = address_port(L, tmp, addr, 2, &port); if (port == 0) { return luaL_error(L, "Invalid port"); } struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int id = skynet_socket_connect(ctx, host, port); lua_pushinteger(L, id); return 1; } static int lclose(lua_State *L) { int id = luaL_checkinteger(L,1); struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); skynet_socket_close(ctx, id); return 0; } static int lshutdown(lua_State *L) { int id = luaL_checkinteger(L,1); struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); skynet_socket_shutdown(ctx, id); return 0; } static int llisten(lua_State *L) { size_t sz = 0; const char * addr = luaL_checklstring(L, 1, &sz); char tmp[sz]; int port = 0; const char * host = address_port(L, tmp, addr, 2, &port); if (port == 0) { return luaL_error(L, "Invalid port"); } int backlog = luaL_optinteger(L,3,BACKLOG); struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int id = skynet_socket_listen(ctx, host,port,backlog); if (id < 0) { return luaL_error(L, "Listen error"); } lua_pushinteger(L,id); return 1; } static size_t count_size(lua_State *L, int index) { size_t tlen = 0; int i; for (i=1;lua_geti(L, index, i) != LUA_TNIL; ++i) { size_t len; luaL_checklstring(L, -1, &len); tlen += len; lua_pop(L,1); } lua_pop(L,1); return tlen; } static void concat_table(lua_State *L, int index, void *buffer, size_t tlen) { char *ptr = buffer; int i; for (i=1;lua_geti(L, index, i) != LUA_TNIL; ++i) { size_t len; const char * str = lua_tolstring(L, -1, &len); if (str == NULL || tlen < len) { break; } memcpy(ptr, str, len); ptr += len; tlen -= len; lua_pop(L,1); } if (tlen != 0) { skynet_free(buffer); luaL_error(L, "Invalid strings table"); } lua_pop(L,1); } static void get_buffer(lua_State *L, int index, struct socket_sendbuffer *buf) { void *buffer; switch(lua_type(L, index)) { size_t len; case LUA_TUSERDATA: // lua full useobject must be a raw pointer, it can't be a socket object or a memory object. buf->type = SOCKET_BUFFER_RAWPOINTER; buf->buffer = lua_touserdata(L, index); if (lua_isinteger(L, index+1)) { buf->sz = lua_tointeger(L, index+1); } else { buf->sz = lua_rawlen(L, index); } break; case LUA_TLIGHTUSERDATA: { int sz = -1; if (lua_isinteger(L, index+1)) { sz = lua_tointeger(L,index+1); } if (sz < 0) { buf->type = SOCKET_BUFFER_OBJECT; } else { buf->type = SOCKET_BUFFER_MEMORY; } buf->buffer = lua_touserdata(L,index); buf->sz = (size_t)sz; break; } case LUA_TTABLE: // concat the table as a string len = count_size(L, index); buffer = skynet_malloc(len); concat_table(L, index, buffer, len); buf->type = SOCKET_BUFFER_MEMORY; buf->buffer = buffer; buf->sz = len; break; default: buf->type = SOCKET_BUFFER_RAWPOINTER; buf->buffer = luaL_checklstring(L, index, &buf->sz); break; } } static int lsend(lua_State *L) { struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int id = luaL_checkinteger(L, 1); struct socket_sendbuffer buf; buf.id = id; get_buffer(L, 2, &buf); int err = skynet_socket_sendbuffer(ctx, &buf); lua_pushboolean(L, !err); return 1; } static int lsendlow(lua_State *L) { struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int id = luaL_checkinteger(L, 1); struct socket_sendbuffer buf; buf.id = id; get_buffer(L, 2, &buf); int err = skynet_socket_sendbuffer_lowpriority(ctx, &buf); lua_pushboolean(L, !err); return 1; } static int lbind(lua_State *L) { struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int fd = luaL_checkinteger(L, 1); int id = skynet_socket_bind(ctx,fd); lua_pushinteger(L,id); return 1; } static int lstart(lua_State *L) { struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int id = luaL_checkinteger(L, 1); skynet_socket_start(ctx,id); return 0; } static int lpause(lua_State *L) { struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int id = luaL_checkinteger(L, 1); skynet_socket_pause(ctx,id); return 0; } static int lnodelay(lua_State *L) { struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int id = luaL_checkinteger(L, 1); skynet_socket_nodelay(ctx,id); return 0; } static int ludp(lua_State *L) { struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); size_t sz = 0; const char * addr = lua_tolstring(L,1,&sz); char tmp[sz]; int port = 0; const char * host = NULL; if (addr) { host = address_port(L, tmp, addr, 2, &port); } int id = skynet_socket_udp(ctx, host, port); if (id < 0) { return luaL_error(L, "udp init failed"); } lua_pushinteger(L, id); return 1; } static int ludp_connect(lua_State *L) { struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int id = luaL_checkinteger(L, 1); size_t sz = 0; const char * addr = luaL_checklstring(L,2,&sz); char tmp[sz]; int port = 0; const char * host = NULL; if (addr) { host = address_port(L, tmp, addr, 3, &port); } if (skynet_socket_udp_connect(ctx, id, host, port)) { return luaL_error(L, "udp connect failed"); } return 0; } static int ludp_dial(lua_State *L){ struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); size_t sz = 0; const char * addr = luaL_checklstring(L, 1, &sz); char tmp[sz]; int port = 0; const char * host = address_port(L, tmp, addr, 2, &port); int id = skynet_socket_udp_dial(ctx, host, port); if (id < 0){ return luaL_error(L, "udp dial host failed"); } lua_pushinteger(L, id); return 1; } static int ludp_listen(lua_State *L){ struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); size_t sz = 0; const char * addr = luaL_checklstring(L, 1, &sz); char tmp[sz]; int port = 0; const char * host = address_port(L, tmp, addr, 2, &port); int id = skynet_socket_udp_listen(ctx, host, port); if (id < 0){ return luaL_error(L, "udp listen host failed"); } lua_pushinteger(L, id); return 1; } static int ludp_send(lua_State *L) { struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int id = luaL_checkinteger(L, 1); const char * address = luaL_checkstring(L, 2); struct socket_sendbuffer buf; buf.id = id; get_buffer(L, 3, &buf); int err = skynet_socket_udp_sendbuffer(ctx, address, &buf); lua_pushboolean(L, !err); return 1; } static int ludp_address(lua_State *L) { size_t sz = 0; const uint8_t * addr = (const uint8_t *)luaL_checklstring(L, 1, &sz); uint16_t port = 0; memcpy(&port, addr+1, sizeof(uint16_t)); port = ntohs(port); const void * src = addr+3; char tmp[256]; int family; if (sz == 1+2+4) { family = AF_INET; } else { if (sz != 1+2+16) { return luaL_error(L, "Invalid udp address"); } family = AF_INET6; } if (inet_ntop(family, src, tmp, sizeof(tmp)) == NULL) { return luaL_error(L, "Invalid udp address"); } lua_pushstring(L, tmp); lua_pushinteger(L, port); return 2; } static void getinfo(lua_State *L, struct socket_info *si) { lua_newtable(L); lua_pushinteger(L, si->id); lua_setfield(L, -2, "id"); lua_pushinteger(L, si->opaque); lua_setfield(L, -2, "address"); switch(si->type) { case SOCKET_INFO_LISTEN: lua_pushstring(L, "LISTEN"); lua_setfield(L, -2, "type"); lua_pushinteger(L, si->read); lua_setfield(L, -2, "accept"); lua_pushinteger(L, si->rtime); lua_setfield(L, -2, "rtime"); if (si->name[0]) { lua_pushstring(L, si->name); lua_setfield(L, -2, "sock"); } return; case SOCKET_INFO_TCP: lua_pushstring(L, "TCP"); break; case SOCKET_INFO_UDP: lua_pushstring(L, "UDP"); break; case SOCKET_INFO_BIND: lua_pushstring(L, "BIND"); break; case SOCKET_INFO_CLOSING: lua_pushstring(L, "CLOSING"); break; default: lua_pushstring(L, "UNKNOWN"); lua_setfield(L, -2, "type"); return; } lua_setfield(L, -2, "type"); lua_pushinteger(L, si->read); lua_setfield(L, -2, "read"); lua_pushinteger(L, si->write); lua_setfield(L, -2, "write"); lua_pushinteger(L, si->wbuffer); lua_setfield(L, -2, "wbuffer"); lua_pushinteger(L, si->rtime); lua_setfield(L, -2, "rtime"); lua_pushinteger(L, si->wtime); lua_setfield(L, -2, "wtime"); lua_pushboolean(L, si->reading); lua_setfield(L, -2, "reading"); lua_pushboolean(L, si->writing); lua_setfield(L, -2, "writing"); if (si->name[0]) { lua_pushstring(L, si->name); lua_setfield(L, -2, "peer"); } } static int linfo(lua_State *L) { lua_newtable(L); struct socket_info * si = skynet_socket_info(); struct socket_info * temp = si; int n = 0; while (temp) { getinfo(L, temp); lua_seti(L, -2, ++n); temp = temp->next; } socket_info_release(si); return 1; } static int lresolve(lua_State *L) { const char * host = luaL_checkstring(L, 1); int status; struct addrinfo ai_hints; struct addrinfo *ai_list = NULL; struct addrinfo *ai_ptr = NULL; memset( &ai_hints, 0, sizeof( ai_hints ) ); status = getaddrinfo( host, NULL, &ai_hints, &ai_list); if ( status != 0 ) { return luaL_error(L, gai_strerror(status)); } lua_newtable(L); int idx = 1; char tmp[128]; for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next ) { struct sockaddr * addr = ai_ptr->ai_addr; void * sin_addr = (ai_ptr->ai_family == AF_INET) ? (void*)&((struct sockaddr_in *)addr)->sin_addr : (void*)&((struct sockaddr_in6 *)addr)->sin6_addr; if (inet_ntop(ai_ptr->ai_family, sin_addr, tmp, sizeof(tmp))) { lua_pushstring(L, tmp); lua_rawseti(L, -2, idx++); } } freeaddrinfo(ai_list); return 1; } LUAMOD_API int luaopen_skynet_socketdriver(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "buffer", lnewbuffer }, { "push", lpushbuffer }, { "pop", lpopbuffer }, { "drop", ldrop }, { "readall", lreadall }, { "clear", lclearbuffer }, { "readline", lreadline }, { "str2p", lstr2p }, { "header", lheader }, { "info", linfo }, { "unpack", lunpack }, { NULL, NULL }, }; luaL_newlib(L,l); luaL_Reg l2[] = { { "connect", lconnect }, { "close", lclose }, { "shutdown", lshutdown }, { "listen", llisten }, { "send", lsend }, { "lsend", lsendlow }, { "bind", lbind }, { "start", lstart }, { "pause", lpause }, { "nodelay", lnodelay }, { "udp", ludp }, { "udp_connect", ludp_connect }, { "udp_dial", ludp_dial}, { "udp_listen", ludp_listen}, { "udp_send", ludp_send }, { "udp_address", ludp_address }, { "resolve", lresolve }, { NULL, NULL }, }; lua_getfield(L, LUA_REGISTRYINDEX, "skynet_context"); struct skynet_context *ctx = lua_touserdata(L,-1); if (ctx == NULL) { return luaL_error(L, "Init skynet context first"); } luaL_setfuncs(L,l2,1); return 1; } ================================================ FILE: lualib-src/lua-stm.c ================================================ #define LUA_LIB #include #include #include #include #include #include #include "rwlock.h" #include "skynet_malloc.h" #include "atomic.h" struct stm_object { struct rwlock lock; ATOM_INT reference; struct stm_copy * copy; }; struct stm_copy { ATOM_INT reference; uint32_t sz; void * msg; }; // msg should alloc by skynet_malloc static struct stm_copy * stm_newcopy(void * msg, int32_t sz) { struct stm_copy * copy = skynet_malloc(sizeof(*copy)); ATOM_INIT(©->reference, 1); copy->sz = sz; copy->msg = msg; return copy; } static struct stm_object * stm_new(void * msg, int32_t sz) { struct stm_object * obj = skynet_malloc(sizeof(*obj)); rwlock_init(&obj->lock); ATOM_INIT(&obj->reference , 1); obj->copy = stm_newcopy(msg, sz); return obj; } static void stm_releasecopy(struct stm_copy *copy) { if (copy == NULL) return; if (ATOM_FDEC(©->reference) <= 1) { skynet_free(copy->msg); skynet_free(copy); } } static void stm_release(struct stm_object *obj) { assert(obj->copy); rwlock_wlock(&obj->lock); // writer release the stm object, so release the last copy . stm_releasecopy(obj->copy); obj->copy = NULL; if (ATOM_FDEC(&obj->reference) > 1) { // stm object grab by readers, reset the copy to NULL. rwlock_wunlock(&obj->lock); return; } // no one grab the stm object, no need to unlock wlock. skynet_free(obj); } static void stm_releasereader(struct stm_object *obj) { rwlock_rlock(&obj->lock); if (ATOM_FDEC(&obj->reference) == 1) { // last reader, no writer. so no need to unlock assert(obj->copy == NULL); skynet_free(obj); return; } rwlock_runlock(&obj->lock); } static void stm_grab(struct stm_object *obj) { rwlock_rlock(&obj->lock); int ref = ATOM_FINC(&obj->reference); rwlock_runlock(&obj->lock); assert(ref > 0); } static struct stm_copy * stm_copy(struct stm_object *obj) { rwlock_rlock(&obj->lock); struct stm_copy * ret = obj->copy; if (ret) { int ref = ATOM_FINC(&ret->reference); assert(ref > 0); } rwlock_runlock(&obj->lock); return ret; } static void stm_update(struct stm_object *obj, void *msg, int32_t sz) { struct stm_copy *copy = stm_newcopy(msg, sz); rwlock_wlock(&obj->lock); struct stm_copy *oldcopy = obj->copy; obj->copy = copy; rwlock_wunlock(&obj->lock); stm_releasecopy(oldcopy); } // lua binding struct boxstm { struct stm_object * obj; }; static int lcopy(lua_State *L) { struct boxstm * box = lua_touserdata(L, 1); stm_grab(box->obj); lua_pushlightuserdata(L, box->obj); return 1; } static int lnewwriter(lua_State *L) { void * msg; size_t sz; if (lua_isuserdata(L,1)) { msg = lua_touserdata(L, 1); sz = (size_t)luaL_checkinteger(L, 2); } else { const char * tmp = luaL_checklstring(L,1,&sz); msg = skynet_malloc(sz); memcpy(msg, tmp, sz); } struct boxstm * box = lua_newuserdatauv(L, sizeof(*box), 0); box->obj = stm_new(msg,sz); lua_pushvalue(L, lua_upvalueindex(1)); lua_setmetatable(L, -2); return 1; } static int ldeletewriter(lua_State *L) { struct boxstm * box = lua_touserdata(L, 1); stm_release(box->obj); box->obj = NULL; return 0; } static int lupdate(lua_State *L) { struct boxstm * box = lua_touserdata(L, 1); void * msg; size_t sz; if (lua_isuserdata(L, 2)) { msg = lua_touserdata(L, 2); sz = (size_t)luaL_checkinteger(L, 3); } else { const char * tmp = luaL_checklstring(L,2,&sz); msg = skynet_malloc(sz); memcpy(msg, tmp, sz); } stm_update(box->obj, msg, sz); return 0; } struct boxreader { struct stm_object *obj; struct stm_copy *lastcopy; }; static int lnewreader(lua_State *L) { struct boxreader * box = lua_newuserdatauv(L, sizeof(*box), 0); box->obj = lua_touserdata(L, 1); box->lastcopy = NULL; lua_pushvalue(L, lua_upvalueindex(1)); lua_setmetatable(L, -2); return 1; } static int ldeletereader(lua_State *L) { struct boxreader * box = lua_touserdata(L, 1); stm_releasereader(box->obj); box->obj = NULL; stm_releasecopy(box->lastcopy); box->lastcopy = NULL; return 0; } static int lread(lua_State *L) { struct boxreader * box = lua_touserdata(L, 1); luaL_checktype(L, 2, LUA_TFUNCTION); struct stm_copy * copy = stm_copy(box->obj); if (copy == box->lastcopy) { // not update stm_releasecopy(copy); lua_pushboolean(L, 0); return 1; } stm_releasecopy(box->lastcopy); box->lastcopy = copy; if (copy) { lua_settop(L, 3); lua_replace(L, 1); lua_settop(L, 2); lua_pushlightuserdata(L, copy->msg); lua_pushinteger(L, copy->sz); lua_pushvalue(L, 1); lua_call(L, 3, LUA_MULTRET); lua_pushboolean(L, 1); lua_replace(L, 1); return lua_gettop(L); } else { lua_pushboolean(L, 0); return 1; } } LUAMOD_API int luaopen_skynet_stm(lua_State *L) { luaL_checkversion(L); lua_createtable(L, 0, 3); lua_pushcfunction(L, lcopy); lua_setfield(L, -2, "copy"); luaL_Reg writer[] = { { "new", lnewwriter }, { NULL, NULL }, }; lua_createtable(L, 0, 2); lua_pushcfunction(L, ldeletewriter), lua_setfield(L, -2, "__gc"); lua_pushcfunction(L, lupdate), lua_setfield(L, -2, "__call"); luaL_setfuncs(L, writer, 1); luaL_Reg reader[] = { { "newcopy", lnewreader }, { NULL, NULL }, }; lua_createtable(L, 0, 2); lua_pushcfunction(L, ldeletereader), lua_setfield(L, -2, "__gc"); lua_pushcfunction(L, lread), lua_setfield(L, -2, "__call"); luaL_setfuncs(L, reader, 1); return 1; } ================================================ FILE: lualib-src/sproto/README ================================================ Check https://github.com/cloudwu/sproto for more ================================================ FILE: lualib-src/sproto/README.md ================================================ Introduction ====== Sproto is an efficient serialization library for C, and focuses on lua binding. It's like Google protocol buffers, but much faster. The design is simple. It only supports a few types that lua supports. It can be easily bound to other dynamic languages, or be used directly in C. In my i5-2500 @3.3GHz CPU, the benchmark is below: The schema in sproto: ``` .Person { name 0 : string id 1 : integer email 2 : string .PhoneNumber { number 0 : string type 1 : integer } phone 3 : *PhoneNumber } .AddressBook { person 0 : *Person } ``` It's equal to: ``` message Person { required string name = 1; required int32 id = 2; optional string email = 3; message PhoneNumber { required string number = 1; optional int32 type = 2 ; } repeated PhoneNumber phone = 4; } message AddressBook { repeated Person person = 1; } ``` Use the data: ```lua local ab = { person = { { name = "Alice", id = 10000, phone = { { number = "123456789" , type = 1 }, { number = "87654321" , type = 2 }, } }, { name = "Bob", id = 20000, phone = { { number = "01234567890" , type = 3 }, } } } } ``` library| encode 1M times | decode 1M times | size -------| --------------- | --------------- | ---- sproto | 2.15s | 7.84s | 83 bytes sproto (nopack) |1.58s | 6.93s | 130 bytes pbc-lua | 6.94s | 16.9s | 69 bytes lua-cjson | 4.92s | 8.30s | 183 bytes * pbc-lua is a google protocol buffers library https://github.com/cloudwu/pbc * lua-cjson is a json library https://github.com/efelix/lua-cjson Parser ======= ```lua local parser = require "sprotoparser" ``` * `parser.parse` parses a sproto schema to a binary string. The parser is needed for parsing the sproto schema. You can use it to generate binary string offline. The schema text and the parser is not needed when your program is running. Lua API ======= ```lua local sproto = require "sproto" local sprotocore = require "sproto.core" -- optional ``` * `sproto.new(spbin)` creates a sproto object by a schema binary string (generates by parser). * `sprotocore.newproto(spbin)` creates a sproto c object by a schema binary string (generates by parser). * `sproto.sharenew(spbin)` share a sproto object from a sproto c object (generates by sprotocore.newproto). * `sproto.parse(schema)` creates a sproto object by a schema text string (by calling parser.parse) * `sproto:exist_type(typename)` detect whether a type exist in sproto object. * `sproto:encode(typename, luatable)` encodes a lua table with typename into a binary string. * `sproto:decode(typename, blob [,sz])` decodes a binary string generated by sproto.encode with typename. If blob is a lightuserdata (C ptr), sz (integer) is needed. * `sproto:pencode(typename, luatable)` The same with sproto:encode, but pack (compress) the results. * `sproto:pdecode(typename, blob [,sz])` The same with sproto.decode, but unpack the blob (generated by sproto:pencode) first. * `sproto:default(typename, type)` Create a table with default values of typename. Type can be nil , "REQUEST", or "RESPONSE". RPC API ======= There is a lua wrapper for the core API for RPC . `sproto:host([packagename])` creates a host object to deliver the rpc message. `host:dispatch(blob [,sz])` unpack and decode (sproto:pdecode) the binary string with type the host created (packagename). If .type is exist, it's a REQUEST message with .type , returns "REQUEST", protoname, message, responser, .ud. The responser is a function for encode the response message. The responser will be nil when .session is not exist. If .type is not exist, it's a RESPONSE message for .session . Returns "RESPONSE", .session, message, .ud . `host:attach(sprotoobj)` creates a function(protoname, message, session, ud) to pack and encode request message with sprotoobj. If you don't want to use host object, you can also use these following apis to encode and decode the rpc message: `sproto:request_encode(protoname, tbl)` encode a request message with protoname. `sproto:response_encode(protoname, tbl)` encode a response message with protoname. `sproto:request_decode(protoname, blob [,sz])` decode a request message with protoname. `sproto:response_decode(protoname, blob [,sz]` decode a response message with protoname. Read testrpc.lua for detail. Schema Language ========== Like Protocol Buffers (but unlike json), sproto messages are strongly-typed and are not self-describing. You must define your message structure in a special language. You can use sprotoparser library to parse the schema text to a binary string, so that the sproto library can use it. You can parse them offline and save the string, or you can parse them during your program running. The schema text is like this: ``` # This is a comment. .Person { # . means a user defined type name 0 : string # string is a build-in type. id 1 : integer email 2 : string .PhoneNumber { # user defined type can be nest. number 0 : string type 1 : integer } phone 3 : *PhoneNumber # *PhoneNumber means an array of PhoneNumber. height 4 : integer(2) # (2) means a 1/100 fixed-point number. data 5 : binary # Some binary data weight 6 : double # floating number } .AddressBook { person 0 : *Person(id) # (id) is optional, means Person.id is main index. } foobar 1 { # define a new protocol (for RPC used) with tag 1 request Person # Associate the type Person with foobar.request response { # define the foobar.response type ok 0 : boolean } } ``` A schema text can be self-described by the sproto schema language. ``` .type { .field { name 0 : string buildin 1 : integer type 2 : integer # type is fixed-point number precision when buildin is SPROTO_TINTEGER; When buildin is SPROTO_TSTRING, it means binary string when type is 1. tag 3 : integer array 4 : boolean key 5 : integer # If key exists, array must be true, and it's a map. } name 0 : string fields 1 : *field } .protocol { name 0 : string tag 1 : integer request 2 : integer # index response 3 : integer # index confirm 4 : boolean # response nil where confirm == true } .group { type 0 : *type protocol 1 : *protocol } ``` Types ======= * **string** : string * **binary** : binary string (it's a sub type of string) * **integer** : integer, the max length of an integer is signed 64bit. It can be a fixed-point number with specified precision. * **double** : double precision floating-point number, satisfy [the IEEE 754 standard](https://en.wikipedia.org/wiki/Double-precision_floating-point_format). * **boolean** : true or false You can add * before the typename to declare an array. You can also specify a main index with the syntax likes `*array(id)`, the array would be encode as an unordered map with the `id` field as key. For empty main index likes `*array()`, the array would be encoded as an unordered map with the first field as key and the second field as value. User defined type can be any name in alphanumeric characters except the build-in typenames, and nested types are supported. * Where are double or real types? I have been using Google protocol buffers for many years in many projects, and I found the real types were seldom used. If you really need it, you can use string to serialize the double numbers. When you need decimal, you can specify the fixed-point precision. **NOTE** : `double` is supported now. * Where is enum? In lua, enum types are not very useful. You can use integer to define an enum table in lua. Wire protocol ======== Each integer number must be serialized in little-endian format. The sproto message must be a user defined type struct, and a struct is encoded in three parts. The header, the field part, and the data part. The tag and small integer or boolean will be encoded in field part, and others are in data part. All the fields must be encoded in ascending order (by tag, base 0). The tags of fields can be discontinuous, if a field is nil. (default value in lua), don't encode it in message. The header is a 16bit integer. It is the number of fields. Each field in field part is a 16bit integer (n). If n is zero, that means the field data is encoded in data part ; If n is even (and not zero), the value of this field is n/2-1 , and the tag increases 1; If n is odd, that means the tags is not continuous, and we should add current tag by (n+1)/2 . Arrays are always encode in data part, 4 bytes header for the size, and the following bytes is the contents. See the example 2 for the struct array; example 3/4 for the integer array ; example 5 for the boolean array. For integer array, an additional byte (4 or 8) to indicate the value is 32bit or 64bit. Read the examples below to see more details. Notice: If the tag is not declared in schema, the decoder will simply ignore the field for protocol version compatibility. Notice more: all examples are tested in `test_wire_protocol.lua`, update `test_wire_protocol.lua` when update examples. ``` .Person { name 0 : string age 1 : integer marital 2 : boolean children 3 : *Person } .Data { numbers 0 : *integer bools 1 : *boolean number 2 : integer bignumber 3 : integer double 4 : double doubles 5 : *double fpn 6 : integer(2) } ``` Example 1: ``` person { name = "Alice" , age = 13, marital = false } 03 00 (fn = 3) 00 00 (id = 0, value in data part) 1C 00 (id = 1, value = 13) 02 00 (id = 2, value = false) 05 00 00 00 (sizeof "Alice") 41 6C 69 63 65 ("Alice") ``` Example 2: ``` person { name = "Bob", age = 40, children = { { name = "Alice" , age = 13 }, { name = "Carol" , age = 5 }, } } 04 00 (fn = 4) 00 00 (id = 0, value in data part) 52 00 (id = 1, value = 40) 01 00 (skip id = 2) 00 00 (id = 3, value in data part) 03 00 00 00 (sizeof "Bob") 42 6F 62 ("Bob") 26 00 00 00 (sizeof children) 0F 00 00 00 (sizeof child 1) 02 00 (fn = 2) 00 00 (id = 0, value in data part) 1C 00 (id = 1, value = 13) 05 00 00 00 (sizeof "Alice") 41 6C 69 63 65 ("Alice") 0F 00 00 00 (sizeof child 2) 02 00 (fn = 2) 00 00 (id = 0, value in data part) 0C 00 (id = 1, value = 5) 05 00 00 00 (sizeof "Carol") 43 61 72 6F 6C ("Carol") ``` Example 3: ``` data { numbers = { 1,2,3,4,5 } } 01 00 (fn = 1) 00 00 (id = 0, value in data part) 15 00 00 00 (sizeof numbers) 04 ( sizeof int32 ) 01 00 00 00 (1) 02 00 00 00 (2) 03 00 00 00 (3) 04 00 00 00 (4) 05 00 00 00 (5) ``` Example 4: ``` data { numbers = { (1<<32)+1, (1<<32)+2, (1<<32)+3, } } 01 00 (fn = 1) 00 00 (id = 0, value in data part) 19 00 00 00 (sizeof numbers) 08 ( sizeof int64 ) 01 00 00 00 01 00 00 00 ( (1<32) + 1) 02 00 00 00 01 00 00 00 ( (1<32) + 2) 03 00 00 00 01 00 00 00 ( (1<32) + 3) ``` Example 5: ``` data { bools = { false, true, false } } 02 00 (fn = 2) 01 00 (skip id = 0) 00 00 (id = 1, value in data part) 03 00 00 00 (sizeof bools) 00 (false) 01 (true) 00 (false) ``` Example 6: ``` data { number = 100000, bignumber = -10000000000, } 03 00 (fn = 3) 03 00 (skip id = 1) 00 00 (id = 2, value in data part) 00 00 (id = 3, value in data part) 04 00 00 00 (sizeof number, data part) A0 86 01 00 (100000, 32bit integer) 08 00 00 00 (sizeof bignumber, data part) 00 1C F4 AB FD FF FF FF (-10000000000, 64bit integer) ``` Example 7: ``` data { double = 0.01171875, doubles = {0.01171875, 23, 4} } 03 00 (fn = 3) 07 00 (skip id = 3) 00 00 (id = 4, value in data part) 00 00 (id = 5, value in data part) 08 00 00 00 (sizeof number, data part) 00 00 00 00 00 00 88 3f (0.01171875, 64bit double) 19 00 00 00 (sizeof doubles) 08 (sizeof double) 00 00 00 00 00 00 88 3f (0.01171875, 64bit double) 00 00 00 00 00 00 37 40 (23, 64bit double) 00 00 00 00 00 00 10 40 (4, 64bit double) ``` Example 8: ``` data { fpn = 1.82, } 02 00 (fn = 2) 0b 00 (skip id = 5) 6e 01 (id = 6, value = 0x16e/2 - 1 = 182) ``` 0 Packing ======= The algorithm is very similar to [Cap'n proto](http://kentonv.github.io/capnproto/), but 0x00 is not treated specially. In packed format, the message is padding to 8. Each 8 byte is reduced to a tag byte followed by zero to eight content bytes. The bits of the tag byte correspond to the bytes of the unpacked word, with the least-significant bit corresponding to the first byte. Each zero bit indicates that the corresponding byte is zero. The non-zero bytes are packed following the tag. For example: ``` unpacked (hex): 08 00 00 00 03 00 02 00 19 00 00 00 aa 01 00 00 packed (hex): 51 08 03 02 31 19 aa 01 ``` Tag 0xff is treated specially. A number N is following the 0xff tag. N means (N+1)\*8 bytes should be copied directly. The bytes may or may not contain zeros. Because of this rule, the worst-case space overhead of packing is 2 bytes per 2 KiB of input. For example: ``` unpacked (hex): 8a (x 30 bytes) packed (hex): ff 03 8a (x 30 bytes) 00 00 ``` C API ===== ```C struct sproto * sproto_create(const void * proto, size_t sz); ``` Create a sproto object with a schema string encoded by sprotoparser: ```C void sproto_release(struct sproto *); ``` Release the sproto object: ```C int sproto_prototag(struct sproto *, const char * name); const char * sproto_protoname(struct sproto *, int proto); // SPROTO_REQUEST(0) : request, SPROTO_RESPONSE(1): response struct sproto_type * sproto_protoquery(struct sproto *, int proto, int what); ``` Convert between tag and name of a protocol, and query the type object of it: ```C struct sproto_type * sproto_type(struct sproto *, const char * typename); ``` Query the type object from a sproto object: ```C struct sproto_arg { void *ud; const char *tagname; int tagid; int type; struct sproto_type *subtype; void *value; int length; int index; // array base 1 int mainindex; // for map int extra; // SPROTO_TINTEGER: fixed-point presision ; SPROTO_TSTRING 0:utf8 string 1:binary }; typedef int (*sproto_callback)(const struct sproto_arg *args); int sproto_decode(struct sproto_type *, const void * data, int size, sproto_callback cb, void *ud); int sproto_encode(struct sproto_type *, void * buffer, int size, sproto_callback cb, void *ud); ``` encode and decode the sproto message with a user defined callback function. Read the implementation of lsproto.c for more details. ```C int sproto_pack(const void * src, int srcsz, void * buffer, int bufsz); int sproto_unpack(const void * src, int srcsz, void * buffer, int bufsz); ``` pack and unpack the message with the 0 packing algorithm. Other Implementions and bindings ===== See Wiki https://github.com/cloudwu/sproto/wiki Question? ========== * Send me an email: http://www.codingnow.com/2000/gmail.gif * My Blog: http://blog.codingnow.com * Design: http://blog.codingnow.com/2014/07/ejoyproto.html (in Chinese) ================================================ FILE: lualib-src/sproto/lsproto.c ================================================ #define LUA_LIB #include #include #include #include "msvcint.h" #include "lua.h" #include "lauxlib.h" #include "sproto.h" #define MAX_GLOBALSPROTO 16 #define ENCODE_BUFFERSIZE 2050 #define ENCODE_MAXSIZE 0x1000000 #define ENCODE_DEEPLEVEL 64 #ifndef luaL_newlib /* using LuaJIT */ /* ** set functions from list 'l' into table at top - 'nup'; each ** function gets the 'nup' elements at the top as upvalues. ** Returns with only the table at the stack. */ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { #ifdef luaL_checkversion luaL_checkversion(L); #endif luaL_checkstack(L, nup, "too many upvalues"); for (; l->name != NULL; l++) { /* fill the table with given functions */ int i; for (i = 0; i < nup; i++) /* copy upvalues to the top */ lua_pushvalue(L, -nup); lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ lua_setfield(L, -(nup + 2), l->name); } lua_pop(L, nup); /* remove upvalues */ } #define luaL_newlibtable(L,l) \ lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) #define luaL_newlib(L,l) (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) #endif #if LUA_VERSION_NUM < 503 #if LUA_VERSION_NUM < 502 static int64_t lua_tointegerx(lua_State *L, int idx, int *isnum) { if (lua_isnumber(L, idx)) { if (isnum) *isnum = 1; return (int64_t)lua_tointeger(L, idx); } else { if (isnum) *isnum = 0; return 0; } } static int lua_absindex (lua_State *L, int idx) { if (idx > 0 || idx <= LUA_REGISTRYINDEX) return idx; return lua_gettop(L) + idx + 1; } #endif static void lua_geti(lua_State *L, int index, lua_Integer i) { index = lua_absindex(L, index); lua_pushinteger(L, i); lua_gettable(L, index); } static void lua_seti(lua_State *L, int index, lua_Integer n) { index = lua_absindex(L, index); lua_pushinteger(L, n); lua_insert(L, -2); lua_settable(L, index); } #endif #if defined(SPROTO_WEAK_TYPE) static int64_t tointegerx (lua_State *L, int idx, int *isnum) { int _isnum = 0; int64_t v = lua_tointegerx(L, idx, &_isnum); if (!_isnum){ double num = lua_tonumberx(L, idx, &_isnum); if(_isnum) { v = (int64_t)llround(num); } } if(isnum) *isnum = _isnum; return v; } static int tobooleanx (lua_State *L, int idx, int *isbool) { if (isbool) *isbool = 1; return lua_toboolean(L, idx); } static const char * tolstringx (lua_State *L, int idx, size_t *len, int *isstring) { const char * str = luaL_tolstring(L, idx, len); // call metamethod, '__tostring' must return a string if (isstring) { *isstring = 1; } lua_pop(L, 1); return str; } #else #define tointegerx(L, idx, isnum) lua_tointegerx((L), (idx), (isnum)) static int tobooleanx (lua_State *L, int idx, int *isbool) { if (isbool) *isbool = lua_isboolean(L, idx); return lua_toboolean(L, idx); } static const char * tolstringx (lua_State *L, int idx, size_t *len, int *isstring) { if (isstring) { *isstring = (lua_type(L, idx) == LUA_TSTRING); } const char * str = lua_tolstring(L, idx, len); return str; } #endif static int lnewproto(lua_State *L) { struct sproto * sp; size_t sz; void * buffer = (void *)luaL_checklstring(L,1,&sz); sp = sproto_create(buffer, sz); if (sp) { lua_pushlightuserdata(L, sp); return 1; } return 0; } static int ldeleteproto(lua_State *L) { struct sproto * sp = lua_touserdata(L,1); if (sp == NULL) { return luaL_argerror(L, 1, "Need a sproto object"); } sproto_release(sp); return 0; } static int lquerytype(lua_State *L) { const char * type_name; struct sproto *sp = lua_touserdata(L,1); struct sproto_type *st; if (sp == NULL) { return luaL_argerror(L, 1, "Need a sproto object"); } type_name = luaL_checkstring(L,2); st = sproto_type(sp, type_name); if (st) { lua_pushlightuserdata(L, st); return 1; } return 0; } struct encode_ud { lua_State *L; struct sproto_type *st; int tbl_index; const char * array_tag; int array_index; int deep; int map_entry; int iter_func; int iter_table; int iter_key; }; static int next_list(lua_State *L, struct encode_ud * self) { // todo: check the key is equal to mainindex value if (self->iter_func) { lua_pushvalue(L, self->iter_func); lua_pushvalue(L, self->iter_table); lua_pushvalue(L, self->iter_key); lua_call(L, 2, 2); if (lua_isnil(L, -2)) { lua_pop(L, 2); return 0; } return 1; } else { lua_pushvalue(L,self->iter_key); return lua_next(L, self->array_index); } } static int get_encodefield(const struct sproto_arg *args) { struct encode_ud *self = args->ud; lua_State *L = self->L; if (args->index > 0) { int map = args->ktagname != NULL; if (args->tagname != self->array_tag) { // a new array self->array_tag = args->tagname; lua_getfield(L, self->tbl_index, args->tagname); if (lua_isnil(L, -1)) { if (self->array_index) { lua_replace(L, self->array_index); } self->array_index = 0; return SPROTO_CB_NOARRAY; } if (self->array_index) { lua_replace(L, self->array_index); } else { self->array_index = lua_gettop(L); } if (map) { if (!self->map_entry) { lua_createtable(L, 0, 2); // key/value entry self->map_entry = lua_gettop(L); } } if (luaL_getmetafield(L, self->array_index, "__pairs")) { lua_pushvalue(L, self->array_index); lua_call(L, 1, 3); int top = lua_gettop(L); self->iter_func = top - 2; self->iter_table = top - 1; self->iter_key = top; } else if (!lua_istable(L,self->array_index)) { return luaL_error(L, "%s.%s(%d) should be a table or an userdata with metamethods (Is a %s)", sproto_name(self->st), args->tagname, args->index, lua_typename(L, lua_type(L, -1))); } else { lua_pushnil(L); self->iter_func = 0; self->iter_table = 0; self->iter_key = lua_gettop(L); } } if (args->mainindex >= 0) { // *type(mainindex) if (!next_list(L, self)) { // iterate end lua_pushnil(L); lua_replace(L, self->iter_key); return SPROTO_CB_NIL; } if (map) { lua_pushvalue(L, -2); lua_replace(L, self->iter_key); lua_setfield(L, self->map_entry, args->vtagname); lua_setfield(L, self->map_entry, args->ktagname); lua_pushvalue(L, self->map_entry); } else { lua_insert(L, -2); lua_replace(L, self->iter_key); } } else { lua_geti(L, self->array_index, args->index); } } else { lua_getfield(L, self->tbl_index, args->tagname); } return 0; } static int encode(const struct sproto_arg *args); static int encode_one(const struct sproto_arg *args, struct encode_ud *self) { lua_State *L = self->L; int type = args->type; switch (type) { case SPROTO_TINTEGER: { int64_t v; lua_Integer vh; int isnum; if (args->extra) { // It's decimal. lua_Number vn = lua_tonumber(L, -1); // use 64bit integer for 32bit architecture. v = (int64_t)(round(vn * args->extra)); } else { v = tointegerx(L, -1, &isnum); if(!isnum) { return luaL_error(L, "%s.%s[%d] is not an integer (Is a %s)", sproto_name(self->st), args->tagname, args->index, lua_typename(L, lua_type(L, -1))); } } lua_pop(L,1); // notice: in lua 5.2, lua_Integer maybe 52bit vh = v >> 31; if (vh == 0 || vh == -1) { *(uint32_t *)args->value = (uint32_t)v; return 4; } else { *(uint64_t *)args->value = (uint64_t)v; return 8; } } case SPROTO_TDOUBLE: { lua_Number v = lua_tonumber(L, -1); *(double*)args->value = (double)v; lua_pop(L,1); return 8; } case SPROTO_TBOOLEAN: { int isbool; int v = tobooleanx(L, -1, &isbool); if (!isbool) { return luaL_error(L, "%s.%s[%d] is not a boolean (Is a %s)", sproto_name(self->st), args->tagname, args->index, lua_typename(L, lua_type(L, -1))); } *(int *)args->value = v; lua_pop(L,1); return 4; } case SPROTO_TSTRING: { size_t sz = 0; int isstring; int type = lua_type(L, -1); // get the type firstly, lua_tolstring may convert value on stack to string const char * str = tolstringx(L, -1, &sz, &isstring); if (!isstring) { return luaL_error(L, "%s.%s[%d] is not a string (Is a %s)", sproto_name(self->st), args->tagname, args->index, lua_typename(L, type)); } if (sz > args->length) return SPROTO_CB_ERROR; memcpy(args->value, str, sz); lua_pop(L,1); return sz; } case SPROTO_TSTRUCT: { struct encode_ud sub; int r; int top = lua_gettop(L); sub.L = L; sub.st = args->subtype; sub.tbl_index = top; sub.array_tag = NULL; sub.array_index = 0; sub.deep = self->deep + 1; sub.map_entry = 0; sub.iter_func = 0; sub.iter_table = 0; sub.iter_key = 0; r = sproto_encode(args->subtype, args->value, args->length, encode, &sub); lua_settop(L, top-1); // pop the value if (r < 0) return SPROTO_CB_ERROR; return r; } default: return luaL_error(L, "Invalid field type %d", args->type); } } static int encode(const struct sproto_arg *args) { struct encode_ud *self = args->ud; lua_State *L = self->L; int code; luaL_checkstack(L, 12, NULL); if (self->deep >= ENCODE_DEEPLEVEL) return luaL_error(L, "The table is too deep"); code = get_encodefield(args); if (code < 0) { return code; } if (lua_isnil(L, -1)) { lua_pop(L,1); return SPROTO_CB_NIL; } return encode_one(args, self); } static void * expand_buffer(lua_State *L, int osz, int nsz) { // assert(osz > 0 && osz < nsz) if (luai_unlikely(nsz > ENCODE_MAXSIZE)) { luaL_error(L, "object is too large (>%d)", ENCODE_MAXSIZE); return NULL; } // osz = osz * 1.5 // factor > 1.618... never reuse alloced space // factor = 1.3, reuse after 3 steps // factor = 1.4, reuse after 4 steps // factor = 1.5, reuse after 5 steps // factor = 1.6, reuse after 8 steps osz += osz >> 1; if (osz < nsz) { osz = nsz; } else if (osz > ENCODE_MAXSIZE) { osz = ENCODE_MAXSIZE; } void *output = lua_newuserdata(L, osz); lua_replace(L, lua_upvalueindex(1)); lua_pushinteger(L, osz); lua_replace(L, lua_upvalueindex(2)); return output; } /* lightuserdata sproto_type table source return string */ static int lencode(lua_State *L) { struct encode_ud self; void * buffer = lua_touserdata(L, lua_upvalueindex(1)); int sz = lua_tointeger(L, lua_upvalueindex(2)); int tbl_index = 2; struct sproto_type * st = lua_touserdata(L, 1); if (st == NULL) { luaL_checktype(L, tbl_index, LUA_TNIL); lua_pushstring(L, ""); return 1; // response nil } self.L = L; self.st = st; self.tbl_index = tbl_index; for (;;) { int r; self.array_tag = NULL; self.array_index = 0; self.deep = 0; lua_settop(L, tbl_index); self.map_entry = 0; self.iter_func = 0; self.iter_table = 0; self.iter_key = 0; r = sproto_encode(st, buffer, sz, encode, &self); if (r<0) { // nsz > osz to double sz buffer = expand_buffer(L, sz, sz + 1); sz = lua_tointeger(L, lua_upvalueindex(2)); } else { lua_pushlstring(L, buffer, r); return 1; } } } struct decode_ud { lua_State *L; const char * array_tag; int array_index; int result_index; int deep; int mainindex_tag; int key_index; int map_entry; }; static int decode(const struct sproto_arg *args) { struct decode_ud * self = args->ud; lua_State *L = self->L; if (self->deep >= ENCODE_DEEPLEVEL) return luaL_error(L, "The table is too deep"); luaL_checkstack(L, 12, NULL); if (args->index != 0) { // It's array if (args->tagname != self->array_tag) { self->array_tag = args->tagname; lua_newtable(L); lua_pushvalue(L, -1); lua_setfield(L, self->result_index, args->tagname); if (self->array_index) { lua_replace(L, self->array_index); } else { self->array_index = lua_gettop(L); } if (args->index < 0) { // It's a empty array, return now. return 0; } } } switch (args->type) { case SPROTO_TINTEGER: { // notice: in lua 5.2, 52bit integer support (not 64) if (args->extra) { // lua_Integer is 32bit in small lua. int64_t v = *(int64_t*)args->value; lua_Number vn = (lua_Number)v; vn /= args->extra; lua_pushnumber(L, vn); } else { int64_t v = *(int64_t*)args->value; lua_pushinteger(L, v); } break; } case SPROTO_TDOUBLE: { double v = *(double*)args->value; lua_pushnumber(L, v); break; } case SPROTO_TBOOLEAN: { int v = *(uint64_t*)args->value; lua_pushboolean(L,v); break; } case SPROTO_TSTRING: { lua_pushlstring(L, args->value, args->length); break; } case SPROTO_TSTRUCT: { int map = args->ktagname != NULL; struct decode_ud sub; int r; sub.L = L; if (map) { if (!self->map_entry) { lua_newtable(L); self->map_entry = lua_gettop(L); } sub.result_index = self->map_entry; } else { lua_newtable(L); sub.result_index = lua_gettop(L); } sub.deep = self->deep + 1; sub.array_index = 0; sub.array_tag = NULL; sub.map_entry = 0; if (args->mainindex >= 0) { // This struct will set into a map, so mark the main index tag. sub.mainindex_tag = args->mainindex; lua_pushnil(L); sub.key_index = lua_gettop(L); r = sproto_decode(args->subtype, args->value, args->length, decode, &sub); if (r < 0) return SPROTO_CB_ERROR; if (r != args->length) return r; if (map) { lua_getfield(L, sub.result_index, args->ktagname); if (lua_isnil(L, -1)) { luaL_error(L, "Can't find key field in [%s]", args->tagname); } lua_getfield(L, sub.result_index, args->vtagname); if (lua_isnil(L, -1)) { luaL_error(L, "Can't find value field in [%s]", args->tagname); } lua_settable(L, self->array_index); lua_settop(L, sub.result_index); } else { lua_pushvalue(L, sub.key_index); if (lua_isnil(L, -1)) { luaL_error(L, "Can't find main index (tag=%d) in [%s]", args->mainindex, args->tagname); } lua_pushvalue(L, sub.result_index); lua_settable(L, self->array_index); lua_settop(L, sub.result_index-1); } return 0; } else { sub.mainindex_tag = -1; sub.key_index = 0; r = sproto_decode(args->subtype, args->value, args->length, decode, &sub); if (r < 0) return SPROTO_CB_ERROR; if (r != args->length) return r; lua_settop(L, sub.result_index); break; } } default: luaL_error(L, "Invalid type"); } if (args->index > 0) { lua_seti(L, self->array_index, args->index); } else { if (self->mainindex_tag == args->tagid) { // This tag is marked, save the value to key_index // assert(self->key_index > 0); lua_pushvalue(L,-1); lua_replace(L, self->key_index); } lua_setfield(L, self->result_index, args->tagname); } return 0; } static const void * getbuffer(lua_State *L, int index, size_t *sz) { const void * buffer = NULL; int t = lua_type(L, index); if (t == LUA_TSTRING) { buffer = lua_tolstring(L, index, sz); } else { if (t != LUA_TUSERDATA && t != LUA_TLIGHTUSERDATA) { luaL_argerror(L, index, "Need a string or userdata"); return NULL; } buffer = lua_touserdata(L, index); *sz = luaL_checkinteger(L, index+1); } return buffer; } /* lightuserdata sproto_type string source / (lightuserdata , integer) return table, sz(decoded bytes) */ static int ldecode(lua_State *L) { struct sproto_type * st = lua_touserdata(L, 1); const void * buffer; struct decode_ud self; size_t sz; int r; if (st == NULL) { // return nil return 0; } sz = 0; buffer = getbuffer(L, 2, &sz); if (!lua_istable(L, -1)) { lua_newtable(L); } self.L = L; self.result_index = lua_gettop(L); self.array_index = 0; self.array_tag = NULL; self.deep = 0; self.mainindex_tag = -1; self.key_index = 0; self.map_entry = 0; r = sproto_decode(st, buffer, (int)sz, decode, &self); if (r < 0) { return luaL_error(L, "decode error"); } lua_settop(L, self.result_index); lua_pushinteger(L, r); return 2; } static int ldumpproto(lua_State *L) { struct sproto * sp = lua_touserdata(L, 1); if (sp == NULL) { return luaL_argerror(L, 1, "Need a sproto_type object"); } sproto_dump(sp); return 0; } /* string source / (lightuserdata , integer) return string */ static int lpack(lua_State *L) { size_t sz=0; const void * buffer = getbuffer(L, 1, &sz); // the worst-case space overhead of packing is 2 bytes per 2 KiB of input (256 words = 2KiB). size_t maxsz = (sz + 2047) / 2048 * 2 + sz + 2; void * output = lua_touserdata(L, lua_upvalueindex(1)); int bytes; int osz = lua_tointeger(L, lua_upvalueindex(2)); if (osz < maxsz) { output = expand_buffer(L, osz, maxsz); } bytes = sproto_pack(buffer, sz, output, maxsz); if (bytes > maxsz) { return luaL_error(L, "packing error, return size = %d", bytes); } lua_pushlstring(L, output, bytes); return 1; } static int lunpack(lua_State *L) { size_t sz=0; const void * buffer = getbuffer(L, 1, &sz); void * output = lua_touserdata(L, lua_upvalueindex(1)); int osz = lua_tointeger(L, lua_upvalueindex(2)); int r = sproto_unpack(buffer, sz, output, osz); if (r < 0) return luaL_error(L, "Invalid unpack stream"); if (r > osz) { output = expand_buffer(L, osz, r); r = sproto_unpack(buffer, sz, output, r); if (r < 0) return luaL_error(L, "Invalid unpack stream"); } lua_pushlstring(L, output, r); return 1; } static void pushfunction_withbuffer(lua_State *L, const char * name, lua_CFunction func) { lua_newuserdata(L, ENCODE_BUFFERSIZE); lua_pushinteger(L, ENCODE_BUFFERSIZE); lua_pushcclosure(L, func, 2); lua_setfield(L, -2, name); } static int lprotocol(lua_State *L) { struct sproto * sp = lua_touserdata(L, 1); struct sproto_type * request; struct sproto_type * response; int t; int tag; if (sp == NULL) { return luaL_argerror(L, 1, "Need a sproto_type object"); } t = lua_type(L,2); if (t == LUA_TNUMBER) { const char * name; tag = lua_tointeger(L, 2); name = sproto_protoname(sp, tag); if (name == NULL) return 0; lua_pushstring(L, name); } else { const char * name = lua_tostring(L, 2); if (name == NULL) { return luaL_argerror(L, 2, "Should be number or string"); } tag = sproto_prototag(sp, name); if (tag < 0) return 0; lua_pushinteger(L, tag); } request = sproto_protoquery(sp, tag, SPROTO_REQUEST); if (request == NULL) { lua_pushnil(L); } else { lua_pushlightuserdata(L, request); } response = sproto_protoquery(sp, tag, SPROTO_RESPONSE); if (response == NULL) { if (sproto_protoresponse(sp, tag)) { lua_pushlightuserdata(L, NULL); // response nil } else { lua_pushnil(L); } } else { lua_pushlightuserdata(L, response); } return 3; } /* global sproto pointer for multi states NOTICE : It is not thread safe */ static struct sproto * G_sproto[MAX_GLOBALSPROTO]; static int lsaveproto(lua_State *L) { struct sproto * sp = lua_touserdata(L, 1); int index = luaL_optinteger(L, 2, 0); if (index < 0 || index >= MAX_GLOBALSPROTO) { return luaL_error(L, "Invalid global slot index %d", index); } /* TODO : release old object (memory leak now, but thread safe)*/ G_sproto[index] = sp; return 0; } static int lloadproto(lua_State *L) { int index = luaL_optinteger(L, 1, 0); struct sproto * sp; if (index < 0 || index >= MAX_GLOBALSPROTO) { return luaL_error(L, "Invalid global slot index %d", index); } sp = G_sproto[index]; if (sp == NULL) { return luaL_error(L, "nil sproto at index %d", index); } lua_pushlightuserdata(L, sp); return 1; } static void push_default(const struct sproto_arg *args, int table) { lua_State *L = args->ud; switch(args->type) { case SPROTO_TINTEGER: if (args->extra) lua_pushnumber(L, 0.0); else lua_pushinteger(L, 0); break; case SPROTO_TDOUBLE: lua_pushnumber(L, 0.0); break; case SPROTO_TBOOLEAN: lua_pushboolean(L, 0); break; case SPROTO_TSTRING: lua_pushliteral(L, ""); break; case SPROTO_TSTRUCT: if (table) { lua_pushstring(L, sproto_name(args->subtype)); } else { lua_createtable(L, 0, 1); lua_pushstring(L, sproto_name(args->subtype)); lua_setfield(L, -2, "__type"); } break; default: luaL_error(L, "Invalid type %d", args->type); break; } } static int encode_default(const struct sproto_arg *args) { lua_State *L = args->ud; lua_pushstring(L, args->tagname); if (args->index > 0) { lua_newtable(L); push_default(args, 1); lua_setfield(L, -2, "__array"); lua_rawset(L, -3); return SPROTO_CB_NOARRAY; } else { push_default(args, 0); lua_rawset(L, -3); return SPROTO_CB_NIL; } } /* lightuserdata sproto_type return default table */ static int ldefault(lua_State *L) { int ret; // 64 is always enough for dummy buffer, except the type has many fields ( > 27). char dummy[64]; struct sproto_type * st = lua_touserdata(L, 1); if (st == NULL) { return luaL_argerror(L, 1, "Need a sproto_type object"); } lua_newtable(L); ret = sproto_encode(st, dummy, sizeof(dummy), encode_default, L); if (ret<0) { // try again int sz = sizeof(dummy) * 2; void * tmp = lua_newuserdata(L, sz); lua_insert(L, -2); for (;;) { ret = sproto_encode(st, tmp, sz, encode_default, L); if (ret >= 0) break; sz *= 2; tmp = lua_newuserdata(L, sz); lua_replace(L, -3); } } return 1; } LUAMOD_API int luaopen_sproto_core(lua_State *L) { #ifdef luaL_checkversion luaL_checkversion(L); #endif luaL_Reg l[] = { { "newproto", lnewproto }, { "deleteproto", ldeleteproto }, { "dumpproto", ldumpproto }, { "querytype", lquerytype }, { "decode", ldecode }, { "protocol", lprotocol }, { "loadproto", lloadproto }, { "saveproto", lsaveproto }, { "default", ldefault }, { NULL, NULL }, }; luaL_newlib(L,l); pushfunction_withbuffer(L, "encode", lencode); pushfunction_withbuffer(L, "pack", lpack); pushfunction_withbuffer(L, "unpack", lunpack); return 1; } ================================================ FILE: lualib-src/sproto/msvcint.h ================================================ #ifndef msvc_int_h #define msvc_int_h #ifdef _MSC_VER # define inline __inline # ifndef _MSC_STDINT_H_ # if (_MSC_VER < 1300) typedef signed char int8_t; typedef signed short int16_t; typedef signed int int32_t; typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned int uint32_t; # else typedef signed __int8 int8_t; typedef signed __int16 int16_t; typedef signed __int32 int32_t; typedef unsigned __int8 uint8_t; typedef unsigned __int16 uint16_t; typedef unsigned __int32 uint32_t; # endif typedef signed __int64 int64_t; typedef unsigned __int64 uint64_t; # endif #else #include #endif #endif ================================================ FILE: lualib-src/sproto/sproto.c ================================================ #include #include #include #include #include "msvcint.h" #include "sproto.h" #define CHUNK_SIZE 1000 #define SIZEOF_LENGTH 4 #define SIZEOF_HEADER 2 #define SIZEOF_FIELD 2 #define SIZEOF_INT64 ((int)sizeof(uint64_t)) #define SIZEOF_INT32 ((int)sizeof(uint32_t)) struct field { int tag; int type; const char * name; struct sproto_type * st; int key; int map; // interpreted two fields struct as map int extra; }; struct sproto_type { const char * name; int n; int base; int maxn; struct field *f; }; struct protocol { const char *name; int tag; int confirm; // confirm == 1 where response nil struct sproto_type * p[2]; }; struct chunk { struct chunk * next; }; struct pool { struct chunk * header; struct chunk * current; int current_used; }; struct sproto { struct pool memory; int type_n; int protocol_n; struct sproto_type * type; struct protocol * proto; }; static void pool_init(struct pool *p) { p->header = NULL; p->current = NULL; p->current_used = 0; } static void pool_release(struct pool *p) { struct chunk * tmp = p->header; while (tmp) { struct chunk * n = tmp->next; free(tmp); tmp = n; } } static void * pool_newchunk(struct pool *p, size_t sz) { struct chunk * t = malloc(sz + sizeof(struct chunk)); if (t == NULL) return NULL; t->next = p->header; p->header = t; return t+1; } static void * pool_alloc(struct pool *p, size_t sz) { // align by 8 sz = (sz + 7) & ~7; if (sz >= CHUNK_SIZE) { return pool_newchunk(p, sz); } if (p->current == NULL) { if (pool_newchunk(p, CHUNK_SIZE) == NULL) return NULL; p->current = p->header; } if (sz + p->current_used <= CHUNK_SIZE) { void * ret = (char *)(p->current+1) + p->current_used; p->current_used += sz; return ret; } if (sz >= p->current_used) { return pool_newchunk(p, sz); } else { void * ret = pool_newchunk(p, CHUNK_SIZE); p->current = p->header; p->current_used = sz; return ret; } } static inline int toword(const uint8_t * p) { return p[0] | p[1]<<8; } static inline uint32_t todword(const uint8_t *p) { return p[0] | p[1]<<8 | p[2]<<16 | p[3]<<24; } static int count_array(const uint8_t * stream) { uint32_t length = todword(stream); int n = 0; stream += SIZEOF_LENGTH; while (length > 0) { uint32_t nsz; if (length < SIZEOF_LENGTH) return -1; nsz = todword(stream); nsz += SIZEOF_LENGTH; if (nsz > length) return -1; ++n; stream += nsz; length -= nsz; } return n; } static int struct_field(const uint8_t * stream, size_t sz) { const uint8_t * field; int fn, header, i; if (sz < SIZEOF_LENGTH) return -1; fn = toword(stream); header = SIZEOF_HEADER + SIZEOF_FIELD * fn; if (sz < header) return -1; field = stream + SIZEOF_HEADER; sz -= header; stream += header; for (i=0;imemory, sz+1); memcpy(buffer, stream+SIZEOF_LENGTH, sz); buffer[sz] = '\0'; return buffer; } static int calc_pow(int base, int n) { int r; if (n == 0) return 1; r = calc_pow(base * base , n / 2); if (n&1) { r *= base; } return r; } static const uint8_t * import_field(struct sproto *s, struct field *f, const uint8_t * stream) { uint32_t sz; const uint8_t * result; int fn; int i; int array = 0; int tag = -1; f->tag = -1; f->type = -1; f->name = NULL; f->st = NULL; f->key = -1; f->map = -1; f->extra = 0; sz = todword(stream); stream += SIZEOF_LENGTH; result = stream + sz; fn = struct_field(stream, sz); if (fn < 0) return NULL; stream += SIZEOF_HEADER; for (i=0;iname = import_string(s, stream + fn * SIZEOF_FIELD); continue; } if (value == 0) return NULL; value = value/2 - 1; switch(tag) { case 1: // buildin if (value >= SPROTO_TSTRUCT) return NULL; // invalid buildin type f->type = value; break; case 2: // type index if (f->type == SPROTO_TINTEGER) { f->extra = calc_pow(10, value); } else if (f->type == SPROTO_TSTRING) { f->extra = value; // string if 0 ; binary is 1 } else { if (value >= s->type_n) return NULL; // invalid type index if (f->type >= 0) return NULL; f->type = SPROTO_TSTRUCT; f->st = &s->type[value]; } break; case 3: // tag f->tag = value; break; case 4: // array if (value) array = SPROTO_TARRAY; break; case 5: // key f->key = value; break; case 6: // map if (value) f->map = 1; break; default: return NULL; } } if (f->tag < 0 || f->type < 0 || f->name == NULL) return NULL; f->type |= array; return result; } /* .type { .field { name 0 : string buildin 1 : integer type 2 : integer tag 3 : integer array 4 : boolean key 5 : integer map 6 : boolean // Interpreted two fields struct as map when decoding } name 0 : string fields 1 : *field } */ static const uint8_t * import_type(struct sproto *s, struct sproto_type *t, const uint8_t * stream) { const uint8_t * result; uint32_t sz = todword(stream); int i; int fn; int n; int maxn; int last; stream += SIZEOF_LENGTH; result = stream + sz; fn = struct_field(stream, sz); if (fn <= 0 || fn > 2) return NULL; for (i=0;iname = import_string(s, stream); if (fn == 1) { return result; } stream += todword(stream)+SIZEOF_LENGTH; // second data n = count_array(stream); if (n<0) return NULL; stream += SIZEOF_LENGTH; maxn = n; last = -1; t->n = n; t->f = pool_alloc(&s->memory, sizeof(struct field) * n); for (i=0;if[i]; stream = import_field(s, f, stream); if (stream == NULL) return NULL; tag = f->tag; if (tag <= last) return NULL; // tag must in ascending order if (tag > last+1) { ++maxn; } last = tag; } t->maxn = maxn; t->base = t->f[0].tag; n = t->f[n-1].tag - t->base + 1; if (n != t->n) { t->base = -1; } return result; } /* .protocol { name 0 : string tag 1 : integer request 2 : integer response 3 : integer } */ static const uint8_t * import_protocol(struct sproto *s, struct protocol *p, const uint8_t * stream) { const uint8_t * result; uint32_t sz = todword(stream); int fn; int i; int tag; stream += SIZEOF_LENGTH; result = stream + sz; fn = struct_field(stream, sz); stream += SIZEOF_HEADER; p->name = NULL; p->tag = -1; p->p[SPROTO_REQUEST] = NULL; p->p[SPROTO_RESPONSE] = NULL; p->confirm = 0; tag = 0; for (i=0;iname = import_string(s, stream + SIZEOF_FIELD *fn); break; case 1: // tag if (value < 0) { return NULL; } p->tag = value; break; case 2: // request if (value < 0 || value>=s->type_n) return NULL; p->p[SPROTO_REQUEST] = &s->type[value]; break; case 3: // response if (value < 0 || value>=s->type_n) return NULL; p->p[SPROTO_RESPONSE] = &s->type[value]; break; case 4: // confirm p->confirm = value; break; default: return NULL; } } if (p->name == NULL || p->tag<0) { return NULL; } return result; } static struct sproto * create_from_bundle(struct sproto *s, const uint8_t * stream, size_t sz) { const uint8_t * content; const uint8_t * typedata = NULL; const uint8_t * protocoldata = NULL; int fn = struct_field(stream, sz); int i; if (fn < 0 || fn > 2) return NULL; stream += SIZEOF_HEADER; content = stream + fn*SIZEOF_FIELD; for (i=0;itype_n = n; s->type = pool_alloc(&s->memory, n * sizeof(*s->type)); } else { protocoldata = content+SIZEOF_LENGTH; s->protocol_n = n; s->proto = pool_alloc(&s->memory, n * sizeof(*s->proto)); } content += todword(content) + SIZEOF_LENGTH; } for (i=0;itype_n;i++) { typedata = import_type(s, &s->type[i], typedata); if (typedata == NULL) { return NULL; } } for (i=0;iprotocol_n;i++) { protocoldata = import_protocol(s, &s->proto[i], protocoldata); if (protocoldata == NULL) { return NULL; } } return s; } struct sproto * sproto_create(const void * proto, size_t sz) { struct pool mem; struct sproto * s; pool_init(&mem); s = pool_alloc(&mem, sizeof(*s)); if (s == NULL) return NULL; memset(s, 0, sizeof(*s)); s->memory = mem; if (create_from_bundle(s, proto, sz) == NULL) { pool_release(&s->memory); return NULL; } return s; } void sproto_release(struct sproto * s) { if (s == NULL) return; pool_release(&s->memory); } static const char * get_typename(int type, struct field *f) { if (type == SPROTO_TSTRUCT) { return f->st->name; } else { switch (type) { case SPROTO_TINTEGER: if (f->extra) return "decimal"; else return "integer"; case SPROTO_TBOOLEAN: return "boolean"; case SPROTO_TSTRING: if (f->extra == SPROTO_TSTRING_BINARY) return "binary"; else return "string"; case SPROTO_TDOUBLE: return "double"; default: return "invalid"; } } } void sproto_dump(struct sproto *s) { int i,j; printf("=== %d types ===\n", s->type_n); for (i=0;itype_n;i++) { struct sproto_type *t = &s->type[i]; printf("%s\n", t->name); for (j=0;jn;j++) { char container[2] = { 0, 0 }; const char * typename = NULL; struct field *f = &t->f[j]; int type = f->type & ~SPROTO_TARRAY; if (f->type & SPROTO_TARRAY) { container[0] = '*'; } else { container[0] = 0; } typename = get_typename(type, f); printf("\t%s (%d) %s%s", f->name, f->tag, container, typename); if (type == SPROTO_TINTEGER && f->extra > 0) { printf("(%d)", f->extra); } if (f->key >= 0) { printf(" key[%d]", f->key); if (f->map >= 0) { printf(" value[%d]", f->st->f[1].tag); } } printf("\n"); } } printf("=== %d protocol ===\n", s->protocol_n); for (i=0;iprotocol_n;i++) { struct protocol *p = &s->proto[i]; if (p->p[SPROTO_REQUEST]) { printf("\t%s (%d) request:%s", p->name, p->tag, p->p[SPROTO_REQUEST]->name); } else { printf("\t%s (%d) request:(null)", p->name, p->tag); } if (p->p[SPROTO_RESPONSE]) { printf(" response:%s", p->p[SPROTO_RESPONSE]->name); } else if (p->confirm) { printf(" response nil"); } printf("\n"); } } // query int sproto_prototag(const struct sproto *sp, const char * name) { int i; for (i=0;iprotocol_n;i++) { if (strcmp(name, sp->proto[i].name) == 0) { return sp->proto[i].tag; } } return -1; } static struct protocol * query_proto(const struct sproto *sp, int tag) { int begin = 0, end = sp->protocol_n; while(beginproto[mid].tag; if (t==tag) { return &sp->proto[mid]; } if (tag > t) { begin = mid+1; } else { end = mid; } } return NULL; } struct sproto_type * sproto_protoquery(const struct sproto *sp, int proto, int what) { struct protocol * p; if (what <0 || what >1) { return NULL; } p = query_proto(sp, proto); if (p) { return p->p[what]; } return NULL; } int sproto_protoresponse(const struct sproto * sp, int proto) { struct protocol * p = query_proto(sp, proto); return (p!=NULL && (p->p[SPROTO_RESPONSE] || p->confirm)); } const char * sproto_protoname(const struct sproto *sp, int proto) { struct protocol * p = query_proto(sp, proto); if (p) { return p->name; } return NULL; } struct sproto_type * sproto_type(const struct sproto *sp, const char * type_name) { int i; for (i=0;itype_n;i++) { if (strcmp(type_name, sp->type[i].name) == 0) { return &sp->type[i]; } } return NULL; } const char * sproto_name(struct sproto_type * st) { return st->name; } static struct field * findtag(const struct sproto_type *st, int tag) { int begin, end; if (st->base >=0 ) { tag -= st->base; if (tag < 0 || tag >= st->n) return NULL; return &st->f[tag]; } begin = 0; end = st->n; while (begin < end) { int mid = (begin+end)/2; struct field *f = &st->f[mid]; int t = f->tag; if (t == tag) { return f; } if (tag > t) { begin = mid + 1; } else { end = mid; } } return NULL; } // encode & decode // sproto_callback(void *ud, int tag, int type, struct sproto_type *, void *value, int length) // return size, -1 means error static inline int fill_size(uint8_t * data, int sz) { data[0] = sz & 0xff; data[1] = (sz >> 8) & 0xff; data[2] = (sz >> 16) & 0xff; data[3] = (sz >> 24) & 0xff; return sz + SIZEOF_LENGTH; } static int encode_integer(uint32_t v, uint8_t * data, int size) { if (size < SIZEOF_LENGTH + sizeof(v)) return -1; data[4] = v & 0xff; data[5] = (v >> 8) & 0xff; data[6] = (v >> 16) & 0xff; data[7] = (v >> 24) & 0xff; return fill_size(data, sizeof(v)); } static int encode_uint64(uint64_t v, uint8_t * data, int size) { if (size < SIZEOF_LENGTH + sizeof(v)) return -1; data[4] = v & 0xff; data[5] = (v >> 8) & 0xff; data[6] = (v >> 16) & 0xff; data[7] = (v >> 24) & 0xff; data[8] = (v >> 32) & 0xff; data[9] = (v >> 40) & 0xff; data[10] = (v >> 48) & 0xff; data[11] = (v >> 56) & 0xff; return fill_size(data, sizeof(v)); } /* //#define CB(tagname,type,index,subtype,value,length) cb(ud, tagname,type,index,subtype,value,length) static int do_cb(sproto_callback cb, void *ud, const char *tagname, int type, int index, struct sproto_type *subtype, void *value, int length) { if (subtype) { if (type >= 0) { printf("callback: tag=%s[%d], subtype[%s]:%d\n",tagname,index, subtype->name, type); } else { printf("callback: tag=%s[%d], subtype[%s]\n",tagname,index, subtype->name); } } else if (index > 0) { printf("callback: tag=%s[%d]\n",tagname,index); } else if (index == 0) { printf("callback: tag=%s\n",tagname); } else { printf("callback: tag=%s [mainkey]\n",tagname); } return cb(ud, tagname,type,index,subtype,value,length); } #define CB(tagname,type,index,subtype,value,length) do_cb(cb,ud, tagname,type,index,subtype,value,length) */ static int encode_object(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int size) { int sz; if (size < SIZEOF_LENGTH) return -1; args->value = data+SIZEOF_LENGTH; args->length = size-SIZEOF_LENGTH; sz = cb(args); if (sz < 0) { if (sz == SPROTO_CB_NIL) return 0; return -1; // sz == SPROTO_CB_ERROR } assert(sz <= size-SIZEOF_LENGTH); // verify buffer overflow return fill_size(data, sz); } static inline void uint32_to_uint64(int negative, uint8_t *buffer) { if (negative) { buffer[4] = 0xff; buffer[5] = 0xff; buffer[6] = 0xff; buffer[7] = 0xff; } else { buffer[4] = 0; buffer[5] = 0; buffer[6] = 0; buffer[7] = 0; } } static uint8_t * encode_integer_array(sproto_callback cb, struct sproto_arg *args, uint8_t *buffer, int size, int *noarray) { uint8_t * header = buffer; int intlen; int index; if (size < 1) return NULL; buffer++; size--; intlen = SIZEOF_INT32; index = 1; *noarray = 0; for (;;) { int sz; union { uint64_t u64; uint32_t u32; } u; args->value = &u; args->length = sizeof(u); args->index = index; sz = cb(args); if (sz <= 0) { if (sz == SPROTO_CB_NIL) // nil object, end of array break; if (sz == SPROTO_CB_NOARRAY) { // no array, don't encode it *noarray = 1; break; } return NULL; // sz == SPROTO_CB_ERROR } // notice: sizeof(uint64_t) is size_t (unsigned) , size may be negative. See issue #75 // so use MACRO SIZOF_INT64 instead if (size < SIZEOF_INT64) return NULL; if (sz == SIZEOF_INT32) { uint32_t v = u.u32; buffer[0] = v & 0xff; buffer[1] = (v >> 8) & 0xff; buffer[2] = (v >> 16) & 0xff; buffer[3] = (v >> 24) & 0xff; if (intlen == SIZEOF_INT64) { uint32_to_uint64(v & 0x80000000, buffer); } } else { uint64_t v; if (sz != SIZEOF_INT64) return NULL; if (intlen == SIZEOF_INT32) { int i; // rearrange size -= (index-1) * SIZEOF_INT32; if (size < SIZEOF_INT64) return NULL; buffer += (index-1) * SIZEOF_INT32; for (i=index-2;i>=0;i--) { int negative; memcpy(header+1+i*SIZEOF_INT64, header+1+i*SIZEOF_INT32, SIZEOF_INT32); negative = header[1+i*SIZEOF_INT64+3] & 0x80; uint32_to_uint64(negative, header+1+i*SIZEOF_INT64); } intlen = SIZEOF_INT64; } v = u.u64; buffer[0] = v & 0xff; buffer[1] = (v >> 8) & 0xff; buffer[2] = (v >> 16) & 0xff; buffer[3] = (v >> 24) & 0xff; buffer[4] = (v >> 32) & 0xff; buffer[5] = (v >> 40) & 0xff; buffer[6] = (v >> 48) & 0xff; buffer[7] = (v >> 56) & 0xff; } size -= intlen; buffer += intlen; index++; } if (buffer == header + 1) { return header; } *header = (uint8_t)intlen; return buffer; } static uint8_t * encode_array_object(sproto_callback cb, struct sproto_arg *args, uint8_t *buffer, int size, int *noarray) { int sz; *noarray = 0; args->index = 1; for (;;) { if (size < SIZEOF_LENGTH) return NULL; size -= SIZEOF_LENGTH; args->value = buffer + SIZEOF_LENGTH; args->length = size; sz = cb(args); if (sz < 0) { if (sz == SPROTO_CB_NIL) { break; } if (sz == SPROTO_CB_NOARRAY) { // no array, don't encode it *noarray = 1; break; } return NULL; // sz == SPROTO_CB_ERROR } fill_size(buffer, sz); buffer += SIZEOF_LENGTH+sz; size -= sz; ++args->index; } return buffer; } static int encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int size) { uint8_t * buffer; int sz; if (size < SIZEOF_LENGTH) return -1; size -= SIZEOF_LENGTH; buffer = data + SIZEOF_LENGTH; switch (args->type) { case SPROTO_TDOUBLE: case SPROTO_TINTEGER: { int noarray; buffer = encode_integer_array(cb,args,buffer,size, &noarray); if (buffer == NULL) return -1; if (noarray) { return 0; } break; } case SPROTO_TBOOLEAN: args->index = 1; for (;;) { int v = 0; args->value = &v; args->length = sizeof(v); sz = cb(args); if (sz < 0) { if (sz == SPROTO_CB_NIL) // nil object , end of array break; if (sz == SPROTO_CB_NOARRAY) // no array, don't encode it return 0; return -1; // sz == SPROTO_CB_ERROR } if (size < 1) return -1; buffer[0] = v ? 1: 0; size -= 1; buffer += 1; ++args->index; } break; default: { int noarray; buffer = encode_array_object(cb, args, buffer, size, &noarray); if (buffer == NULL) return -1; if (noarray) return 0; break; } } sz = buffer - (data + SIZEOF_LENGTH); return fill_size(data, sz); } int sproto_encode(const struct sproto_type *st, void * buffer, int size, sproto_callback cb, void *ud) { struct sproto_arg args; uint8_t * header = buffer; uint8_t * data; int header_sz = SIZEOF_HEADER + st->maxn * SIZEOF_FIELD; int i; int index; int lasttag; int datasz; if (size < header_sz) return -1; args.ud = ud; data = header + header_sz; size -= header_sz; index = 0; lasttag = -1; for (i=0;in;i++) { struct field *f = &st->f[i]; int type = f->type; int value = 0; int sz = -1; args.tagname = f->name; args.tagid = f->tag; args.subtype = f->st; args.mainindex = f->key; args.extra = f->extra; args.ktagname = NULL; args.vtagname = NULL; if (type & SPROTO_TARRAY) { args.type = type & (~SPROTO_TARRAY); if (f->map > 0) { args.ktagname = f->st->f[0].name; args.vtagname = f->st->f[1].name; } sz = encode_array(cb, &args, data, size); } else { args.type = type; args.index = 0; switch(type) { case SPROTO_TDOUBLE: case SPROTO_TINTEGER: case SPROTO_TBOOLEAN: { union { uint64_t u64; uint32_t u32; } u; args.value = &u; args.length = sizeof(u); sz = cb(&args); if (sz < 0) { if (sz == SPROTO_CB_NIL) continue; if (sz == SPROTO_CB_NOARRAY) // no array, don't encode it return 0; return -1; // sz == SPROTO_CB_ERROR } if (sz == SIZEOF_INT32) { if (u.u32 < 0x7fff) { value = (u.u32+1) * 2; sz = 2; // sz can be any number > 0 } else { sz = encode_integer(u.u32, data, size); } } else if (sz == SIZEOF_INT64) { sz= encode_uint64(u.u64, data, size); } else { return -1; } break; } case SPROTO_TSTRUCT: case SPROTO_TSTRING: sz = encode_object(cb, &args, data, size); break; } } if (sz < 0) return -1; if (sz > 0) { uint8_t * record; int tag; if (value == 0) { data += sz; size -= sz; } record = header+SIZEOF_HEADER+SIZEOF_FIELD*index; tag = f->tag - lasttag - 1; if (tag > 0) { // skip tag tag = (tag - 1) * 2 + 1; if (tag > 0xffff) return -1; record[0] = tag & 0xff; record[1] = (tag >> 8) & 0xff; ++index; record += SIZEOF_FIELD; } ++index; record[0] = value & 0xff; record[1] = (value >> 8) & 0xff; lasttag = f->tag; } } header[0] = index & 0xff; header[1] = (index >> 8) & 0xff; datasz = data - (header + header_sz); data = header + header_sz; if (index != st->maxn) { memmove(header + SIZEOF_HEADER + index * SIZEOF_FIELD, data, datasz); } return SIZEOF_HEADER + index * SIZEOF_FIELD + datasz; } static int decode_array_object(sproto_callback cb, struct sproto_arg *args, uint8_t * stream, int sz) { uint32_t hsz; int index = 1; while (sz > 0) { if (sz < SIZEOF_LENGTH) return -1; hsz = todword(stream); stream += SIZEOF_LENGTH; sz -= SIZEOF_LENGTH; if (hsz > sz) return -1; args->index = index; args->value = stream; args->length = hsz; if (cb(args)) return -1; sz -= hsz; stream += hsz; ++index; } return 0; } static inline uint64_t expand64(uint32_t v) { uint64_t value = v; if (value & 0x80000000) { value |= (uint64_t)~0 << 32 ; } return value; } static int decode_empty_array(sproto_callback cb, struct sproto_arg *args) { // It's empty array, call cb with index == -1 to create the empty array. args->index = -1; args->value = NULL; args->length = 0; return cb(args); } static int decode_array(sproto_callback cb, struct sproto_arg *args, uint8_t * stream) { uint32_t sz = todword(stream); int type = args->type; int i; if (sz == 0) { return decode_empty_array(cb, args); } stream += SIZEOF_LENGTH; switch (type) { case SPROTO_TDOUBLE: case SPROTO_TINTEGER: { if (--sz == 0) { // An empty array but with a len prefix return decode_empty_array(cb, args); } int len = *stream; ++stream; if (len == SIZEOF_INT32) { if (sz % SIZEOF_INT32 != 0) return -1; for (i=0;iindex = i+1; args->value = &value; args->length = sizeof(value); cb(args); } } else if (len == SIZEOF_INT64) { if (sz % SIZEOF_INT64 != 0) return -1; for (i=0;iindex = i+1; args->value = &value; args->length = sizeof(value); cb(args); } } else { return -1; } break; } case SPROTO_TBOOLEAN: for (i=0;iindex = i+1; args->value = &value; args->length = sizeof(value); cb(args); } break; case SPROTO_TSTRING: case SPROTO_TSTRUCT: return decode_array_object(cb, args, stream, sz); default: return -1; } return 0; } int sproto_decode(const struct sproto_type *st, const void * data, int size, sproto_callback cb, void *ud) { struct sproto_arg args; int total = size; uint8_t * stream; uint8_t * datastream; int fn; int i; int tag; if (size < SIZEOF_HEADER) return -1; // debug print // printf("sproto_decode[%p] (%s)\n", ud, st->name); stream = (void *)data; fn = toword(stream); stream += SIZEOF_HEADER; size -= SIZEOF_HEADER ; if (size < fn * SIZEOF_FIELD) return -1; datastream = stream + fn * SIZEOF_FIELD; size -= fn * SIZEOF_FIELD; args.ud = ud; tag = -1; for (i=0;iname; args.tagid = f->tag; args.type = f->type; args.subtype = f->st; args.index = 0; args.mainindex = f->key; args.extra = f->extra; args.ktagname = NULL; args.vtagname = NULL; if (value < 0) { if (f->type & SPROTO_TARRAY) { args.type = f->type & (~SPROTO_TARRAY); if (f->map > 0) { args.ktagname = f->st->f[0].name; args.vtagname = f->st->f[1].name; } if (decode_array(cb, &args, currentdata)) { return -1; } } else { switch (f->type) { case SPROTO_TDOUBLE: case SPROTO_TINTEGER: { uint32_t sz = todword(currentdata); if (sz == SIZEOF_INT32) { uint64_t v = expand64(todword(currentdata + SIZEOF_LENGTH)); args.value = &v; args.length = sizeof(v); cb(&args); } else if (sz != SIZEOF_INT64) { return -1; } else { uint32_t low = todword(currentdata + SIZEOF_LENGTH); uint32_t hi = todword(currentdata + SIZEOF_LENGTH + SIZEOF_INT32); uint64_t v = (uint64_t)low | (uint64_t) hi << 32; args.value = &v; args.length = sizeof(v); cb(&args); } break; } case SPROTO_TSTRING: case SPROTO_TSTRUCT: { uint32_t sz = todword(currentdata); args.value = currentdata+SIZEOF_LENGTH; args.length = sz; if (cb(&args)) return -1; break; } default: return -1; } } } else if (f->type != SPROTO_TINTEGER && f->type != SPROTO_TBOOLEAN) { return -1; } else { uint64_t v = value; args.value = &v; args.length = sizeof(v); cb(&args); } } return total - size; } // 0 pack static int pack_seg(const uint8_t *src, uint8_t * buffer, int sz, int n) { uint8_t header = 0; int notzero = 0; int i; uint8_t * obuffer = buffer; ++buffer; --sz; if (sz < 0) obuffer = NULL; for (i=0;i<8;i++) { if (src[i] != 0) { notzero++; header |= 1< 0) { *buffer = src[i]; ++buffer; --sz; } } } if ((notzero == 7 || notzero == 6) && n > 0) { notzero = 8; } if (notzero == 8) { if (n > 0) { return 8; } else { return 10; } } if (obuffer) { *obuffer = header; } return notzero + 1; } static inline void write_ff(const uint8_t * src, const uint8_t * src_end, uint8_t * des, int n) { des[0] = 0xff; des[1] = n - 1; if (src + n * 8 <= src_end) { memcpy(des+2, src, n*8); } else { int sz = (int)(src_end - src); memcpy(des+2, src, sz); memset(des+2+sz, 0, n*8-sz); } } int sproto_pack(const void * srcv, int srcsz, void * bufferv, int bufsz) { uint8_t tmp[8]; int i; const uint8_t * ff_srcstart = NULL; uint8_t * ff_desstart = NULL; int ff_n = 0; int size = 0; const uint8_t * src = srcv; const uint8_t * src_end = (uint8_t *)srcv + srcsz; uint8_t * buffer = bufferv; for (i=0;i 0) { int j; memcpy(tmp, src, 8-padding); for (j=0;j0) { ++ff_n; if (ff_n == 256) { if (bufsz >= 0) { write_ff(ff_srcstart, src_end, ff_desstart, 256); } ff_n = 0; } } else { if (ff_n > 0) { if (bufsz >= 0) { write_ff(ff_srcstart, src_end, ff_desstart, ff_n); } ff_n = 0; } } src += 8; buffer += n; size += n; } if(bufsz >= 0 && ff_n > 0) { write_ff(ff_srcstart, src_end, ff_desstart, ff_n); } return size; } int sproto_unpack(const void * srcv, int srcsz, void * bufferv, int bufsz) { const uint8_t * src = srcv; uint8_t * buffer = bufferv; int size = 0; while (srcsz > 0) { uint8_t header = src[0]; --srcsz; ++src; if (header == 0xff) { int n; if (srcsz <= 0) { return -1; } n = (src[0] + 1) * 8; if (srcsz < n + 1) return -1; srcsz -= n + 1; ++src; if (bufsz >= n) { memcpy(buffer, src, n); } bufsz -= n; buffer += n; src += n; size += n; } else { int i; for (i=0;i<8;i++) { int nz = (header >> i) & 1; if (nz) { if (srcsz <= 0) return -1; if (bufsz > 0) { *buffer = *src; --bufsz; ++buffer; } ++src; --srcsz; } else { if (bufsz > 0) { *buffer = 0; --bufsz; ++buffer; } } ++size; } } } return size; } ================================================ FILE: lualib-src/sproto/sproto.h ================================================ #ifndef sproto_h #define sproto_h #include struct sproto; struct sproto_type; #define SPROTO_REQUEST 0 #define SPROTO_RESPONSE 1 // type (sproto_arg.type) #define SPROTO_TINTEGER 0 #define SPROTO_TBOOLEAN 1 #define SPROTO_TSTRING 2 #define SPROTO_TDOUBLE 3 #define SPROTO_TSTRUCT 4 // container type #define SPROTO_TARRAY 0x80 // sub type of string (sproto_arg.extra) #define SPROTO_TSTRING_STRING 0 #define SPROTO_TSTRING_BINARY 1 #define SPROTO_CB_ERROR -1 #define SPROTO_CB_NIL -2 #define SPROTO_CB_NOARRAY -3 struct sproto * sproto_create(const void * proto, size_t sz); void sproto_release(struct sproto *); int sproto_prototag(const struct sproto *, const char * name); const char * sproto_protoname(const struct sproto *, int proto); // SPROTO_REQUEST(0) : request, SPROTO_RESPONSE(1): response struct sproto_type * sproto_protoquery(const struct sproto *, int proto, int what); int sproto_protoresponse(const struct sproto *, int proto); struct sproto_type * sproto_type(const struct sproto *, const char * type_name); int sproto_pack(const void * src, int srcsz, void * buffer, int bufsz); int sproto_unpack(const void * src, int srcsz, void * buffer, int bufsz); struct sproto_arg { void *ud; const char *tagname; int tagid; int type; struct sproto_type *subtype; void *value; int length; int index; // array base 1, negative value indicates that it is a empty array int mainindex; // for map int extra; // SPROTO_TINTEGER: decimal ; SPROTO_TSTRING 0:utf8 string 1:binary // When interpretd two fields struct as map, the following fields must not be NULL. const char *ktagname; const char *vtagname; }; typedef int (*sproto_callback)(const struct sproto_arg *args); int sproto_decode(const struct sproto_type *, const void * data, int size, sproto_callback cb, void *ud); int sproto_encode(const struct sproto_type *, void * buffer, int size, sproto_callback cb, void *ud); // for debug use void sproto_dump(struct sproto *); const char * sproto_name(struct sproto_type *); #endif ================================================ FILE: mingw.mk ================================================ # Cross-compilation toolchain CC = x86_64-w64-mingw32-gcc AR = x86_64-w64-mingw32-ar RANLIB = x86_64-w64-mingw32-ranlib LUA_CLIB_PATH ?= luaclib CSERVICE_PATH ?= cservice SKYNET_BUILD_PATH ?= . COMPAT_MINGW_DIR = 3rd/compat-mingw LUA_DIR = 3rd/lua LUA_CFLAGS = -g -O2 -Wall -I. -std=gnu99 -I../../skynet-src LUA_STATICLIB := 3rd/lua/liblua.a LUA_DLL ?= 3rd/lua/lua54.dll SKYNET_DLL ?= $(SKYNET_BUILD_PATH)/skynet.dll LUA_INC ?= $(LUA_DIR) # Lua linking options LUA_LIBS = -L$(LUA_DIR) -llua54 LUA_STATIC_LIBS = $(LUA_STATICLIB) # Skynet linking and include options SKYNET_LINK_LIBS = -L$(SKYNET_BUILD_PATH) -lskynet SKYNET_INCLUDES = -Iskynet-src -I$(COMPAT_MINGW_DIR) -I$(LUA_INC) SHARED_BUILD = $(CC) $(CFLAGS) $(SHARED) # Compiler flags CFLAGS = -g -O2 -Wall -std=gnu99 -I$(LUA_INC) $(MYCFLAGS) CFLAGS += -I3rd/lua -Iskynet-src -I$(COMPAT_MINGW_DIR) CFLAGS += -include $(COMPAT_MINGW_DIR)/compat.h CFLAGS += -Wno-int-conversion -Wno-incompatible-pointer-types -Wno-pointer-sign -Wno-unused-function LDFLAGS = -Wl,--export-all-symbols,--out-implib,libskynet.a SHARED = -fPIC --shared -Wl,--export-all-symbols,--enable-auto-import,--allow-shlib-undefined,--unresolved-symbols=ignore-all SKYNET_LIBS = -static-libgcc -Wl,-Bstatic -lpthread -Wl,-Bdynamic -lm -lws2_32 -lgdi32 SKYNET_DEFINES = -DNOUSE_JEMALLOC COMPAT_LIB = $(COMPAT_MINGW_DIR)/libcompat.a CSERVICE = snlua logger gate harbor LUA_CLIB = skynet client bson md5 sproto lpeg LUA_CLIB_SKYNET = \ lua-skynet.c lua-seri.c \ lua-socket.c \ lua-mongo.c \ lua-netpack.c \ lua-memory.c \ lua-multicast.c \ lua-cluster.c \ lua-crypt.c lsha1.c \ lua-sharedata.c \ lua-stm.c \ lua-debugchannel.c \ lua-datasheet.c \ lua-sharetable.c \ \ SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \ skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \ skynet_harbor.c skynet_env.c skynet_monitor.c skynet_socket.c socket_server.c \ mem_info.c malloc_hook.c skynet_daemon.c skynet_log.c $(LUA_STATICLIB): @echo "Building Lua static library..." cd $(LUA_DIR) && $(CC) $(LUA_CFLAGS) -DMAKE_LIB -c onelua.c -o onelua.o cd $(LUA_DIR) && $(AR) rcs liblua.a onelua.o $(LUA_DLL) : $(LUA_STATICLIB) @echo "Building Lua DLL..." cd $(LUA_DIR) && $(CC) -shared -o lua54.dll onelua.o -Wl,--export-all-symbols,--out-implib,liblua54.a $(SKYNET_LIBS) @echo "Lua DLL and import library created successfully" $(LUA_DIR)/lua.exe : $(LUA_DLL) $(LUA_STATICLIB) @echo "Building Lua interpreter..." cd $(LUA_DIR) && $(CC) $(LUA_CFLAGS) -c lua.c -o lua.o cd $(LUA_DIR) && $(CC) $(LUA_CFLAGS) -o lua.exe lua.o -L. -llua54 $(SKYNET_LIBS) cp $(LUA_DLL) $(SKYNET_BUILD_PATH)/ $(LUA_DIR)/luac.exe : $(LUA_DLL) $(LUA_STATICLIB) @echo "Building Lua compiler..." cd $(LUA_DIR) && $(CC) $(LUA_CFLAGS) -DMAKE_LUAC -c onelua.c -o luac.o cd $(LUA_DIR) && $(CC) $(LUA_CFLAGS) -o luac.exe luac.o liblua.a $(SKYNET_LIBS) $(COMPAT_LIB): $(COMPAT_MINGW_DIR)/compat.c @echo "Building compatibility library..." $(CC) $(CFLAGS) -c $(COMPAT_MINGW_DIR)/compat.c -o $(COMPAT_MINGW_DIR)/compat.o cd $(COMPAT_MINGW_DIR) && $(AR) rcs libcompat.a compat.o cd $(COMPAT_MINGW_DIR) && $(RANLIB) libcompat.a # Build order: first build core libraries, then dependent modules all : core_libs modules tools .PHONY: core_libs modules tools core_libs: $(LUA_STATICLIB) $(LUA_DLL) $(SKYNET_DLL) modules: core_libs $(foreach v, $(CSERVICE), $(CSERVICE_PATH)/$(v).so) $(foreach v, $(LUA_CLIB), $(LUA_CLIB_PATH)/$(v).so) tools: core_libs $(LUA_DIR)/lua.exe $(LUA_DIR)/luac.exe $(SKYNET_BUILD_PATH)/skynet.exe SKYNET_LIB_SRC = $(filter-out skynet_main.c, $(SKYNET_SRC)) $(SKYNET_DLL) : $(foreach v, $(SKYNET_LIB_SRC), skynet-src/$(v)) $(COMPAT_LIB) $(LUA_DLL) @echo "Building skynet.dll..." $(CC) $(CFLAGS) -shared -o $@ $(foreach v, $(SKYNET_LIB_SRC), skynet-src/$(v)) $(COMPAT_LIB) $(LUA_LIBS) $(SKYNET_INCLUDES) $(SKYNET_LIBS) $(SKYNET_DEFINES) $(LDFLAGS) $(SKYNET_BUILD_PATH)/skynet.exe : skynet-src/skynet_main.c $(SKYNET_DLL) $(COMPAT_LIB) $(LUA_DLL) @echo "Building skynet.exe..." $(CC) $(CFLAGS) -o $@ skynet-src/skynet_main.c $(COMPAT_LIB) $(SKYNET_LINK_LIBS) $(LUA_LIBS) $(SKYNET_INCLUDES) $(SKYNET_LIBS) $(SKYNET_DEFINES) cp $(LUA_DLL) $(SKYNET_BUILD_PATH)/ $(LUA_CLIB_PATH) : mkdir $(LUA_CLIB_PATH) $(CSERVICE_PATH) : mkdir $(CSERVICE_PATH) define CSERVICE_TEMP $$(CSERVICE_PATH)/$(1).so : service-src/service_$(1).c $$(LUA_DLL) $$(SKYNET_DLL) | $$(CSERVICE_PATH) $$(CC) $$(CFLAGS) $$(SHARED) $$< -o $$@ $$(SKYNET_INCLUDES) $$(LUA_LIBS) $$(SKYNET_LINK_LIBS) endef $(foreach v, $(CSERVICE), $(eval $(call CSERVICE_TEMP,$(v)))) $(LUA_CLIB_PATH)/skynet.so : $(addprefix lualib-src/,$(LUA_CLIB_SKYNET)) $(SKYNET_DLL) $(LUA_DLL) | $(LUA_CLIB_PATH) $(SHARED_BUILD) $(addprefix lualib-src/,$(LUA_CLIB_SKYNET)) -o $@ $(SKYNET_INCLUDES) -Iservice-src -Ilualib-src $(SKYNET_LINK_LIBS) $(LUA_LIBS) $(SKYNET_LIBS) $(LUA_CLIB_PATH)/bson.so : lualib-src/lua-bson.c $(LUA_DLL) | $(LUA_CLIB_PATH) $(SHARED_BUILD) $^ -o $@ $(SKYNET_INCLUDES) $(LUA_LIBS) $(SKYNET_LIBS) $(LUA_CLIB_PATH)/md5.so : 3rd/lua-md5/md5.c 3rd/lua-md5/md5lib.c 3rd/lua-md5/compat-5.2.c $(LUA_DLL) | $(LUA_CLIB_PATH) $(SHARED_BUILD) -I3rd/lua-md5 -Wno-attributes $^ -o $@ $(LUA_LIBS) $(SKYNET_LIBS) $(LUA_CLIB_PATH)/client.so : lualib-src/lua-clientsocket.c lualib-src/lua-crypt.c lualib-src/lsha1.c $(COMPAT_LIB) $(LUA_DLL) | $(LUA_CLIB_PATH) $(SHARED_BUILD) $^ -o $@ $(LUA_LIBS) $(SKYNET_LIBS) $(LUA_CLIB_PATH)/sproto.so : lualib-src/sproto/sproto.c lualib-src/sproto/lsproto.c $(LUA_DLL) | $(LUA_CLIB_PATH) $(SHARED_BUILD) -Ilualib-src/sproto $^ -o $@ $(LUA_LIBS) $(SKYNET_LIBS) $(LUA_CLIB_PATH)/lpeg.so : 3rd/lpeg/lpcap.c 3rd/lpeg/lpcode.c 3rd/lpeg/lpprint.c 3rd/lpeg/lptree.c 3rd/lpeg/lpvm.c 3rd/lpeg/lpcset.c $(LUA_DLL) | $(LUA_CLIB_PATH) $(SHARED_BUILD) -I3rd/lpeg $^ -o $@ $(LUA_LIBS) $(SKYNET_LIBS) .PHONY: clean cleanall core_libs modules tools clean : rm -f $(SKYNET_BUILD_PATH)/*.exe $(SKYNET_BUILD_PATH)/*.dll $(SKYNET_BUILD_PATH)/*.a rm -f $(COMPAT_LIB) $(COMPAT_MINGW_DIR)/*.o rm -f $(CSERVICE_PATH)/*.so $(LUA_CLIB_PATH)/*.so rm -f $(LUA_DIR)/*.a cleanall: clean rm -f $(LUA_STATICLIB) rm -f $(LUA_DIR)/*.exe $(LUA_DIR)/*.dll $(LUA_DIR)/*.a $(LUA_DIR)/*.o ================================================ FILE: platform.mk ================================================ PLAT ?= none PLATS = linux freebsd macosx mingw CC ?= gcc .PHONY : none $(PLATS) clean all cleanall #ifneq ($(PLAT), none) .PHONY : default default : $(MAKE) $(PLAT) #endif none : @echo "Please do 'make PLATFORM' where PLATFORM is one of these:" @echo " $(PLATS)" SKYNET_LIBS := -lpthread -lm SHARED := -fPIC --shared EXPORT := -Wl,-E linux : PLAT = linux macosx : PLAT = macosx freebsd : PLAT = freebsd mingw : PLAT = mingw macosx : SHARED := -fPIC -dynamiclib -Wl,-undefined,dynamic_lookup macosx : EXPORT := macosx linux : SKYNET_LIBS += -ldl linux freebsd : SKYNET_LIBS += -lrt # Turn off jemalloc and malloc hook on macosx macosx : MALLOC_STATICLIB := macosx : SKYNET_DEFINES :=-DNOUSE_JEMALLOC linux macosx freebsd : $(MAKE) all PLAT=$@ SKYNET_LIBS="$(SKYNET_LIBS)" SHARED="$(SHARED)" EXPORT="$(EXPORT)" MALLOC_STATICLIB="$(MALLOC_STATICLIB)" SKYNET_DEFINES="$(SKYNET_DEFINES)" mingw : $(MAKE) -f mingw.mk all PLAT=$@ ================================================ FILE: service/bootstrap.lua ================================================ local service = require "skynet.service" local skynet = require "skynet.manager" -- import skynet.launch, ... skynet.start(function() local standalone = skynet.getenv "standalone" local launcher = assert(skynet.launch("snlua","launcher")) skynet.name(".launcher", launcher) local harbor_id = tonumber(skynet.getenv "harbor" or 0) if harbor_id == 0 then assert(standalone == nil) standalone = true skynet.setenv("standalone", "true") local ok, slave = pcall(skynet.newservice, "cdummy") if not ok then skynet.abort() end skynet.name(".cslave", slave) else if standalone then if not pcall(skynet.newservice,"cmaster") then skynet.abort() end end local ok, slave = pcall(skynet.newservice, "cslave") if not ok then skynet.abort() end skynet.name(".cslave", slave) end if standalone then local datacenter = skynet.newservice "datacenterd" skynet.name("DATACENTER", datacenter) end skynet.newservice "service_mgr" local enablessl = skynet.getenv "enablessl" if enablessl == "true" then service.new("ltls_holder", function () local c = require "ltls.init.c" c.constructor() end) end pcall(skynet.newservice,skynet.getenv "start" or "main") skynet.exit() end) ================================================ FILE: service/cdummy.lua ================================================ local skynet = require "skynet" require "skynet.manager" -- import skynet.launch, ... local globalname = {} local queryname = {} local harbor = {} local harbor_service skynet.register_protocol { name = "harbor", id = skynet.PTYPE_HARBOR, pack = function(...) return ... end, unpack = skynet.tostring, } skynet.register_protocol { name = "text", id = skynet.PTYPE_TEXT, pack = function(...) return ... end, unpack = skynet.tostring, } local function response_name(name) local address = globalname[name] if queryname[name] then local tmp = queryname[name] queryname[name] = nil for _,resp in ipairs(tmp) do resp(true, address) end end end function harbor.REGISTER(name, handle) assert(globalname[name] == nil) globalname[name] = handle response_name(name) skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name) end function harbor.QUERYNAME(name) if name:byte() == 46 then -- "." , local name skynet.ret(skynet.pack(skynet.localname(name))) return end local result = globalname[name] if result then skynet.ret(skynet.pack(result)) return end local queue = queryname[name] if queue == nil then queue = { skynet.response() } queryname[name] = queue else table.insert(queue, skynet.response()) end end function harbor.LINK(id) skynet.ret() end function harbor.CONNECT(id) skynet.error("Can't connect to other harbor in single node mode") end skynet.start(function() local harbor_id = tonumber(skynet.getenv "harbor") assert(harbor_id == 0) skynet.dispatch("lua", function (session,source,command,...) local f = assert(harbor[command]) f(...) end) skynet.dispatch("text", function(session,source,command) -- ignore all the command end) harbor_service = assert(skynet.launch("harbor", harbor_id, skynet.self())) end) ================================================ FILE: service/clusteragent.lua ================================================ local skynet = require "skynet" local socket = require "skynet.socket" local cluster = require "skynet.cluster.core" local ignoreret = skynet.ignoreret local clusterd, gate, fd = ... clusterd = tonumber(clusterd) gate = tonumber(gate) fd = tonumber(fd) local large_request = {} local inquery_name = {} local register_name local register_name_mt = { __index = function(self, name) local waitco = inquery_name[name] if waitco then local co = coroutine.running() table.insert(waitco, co) skynet.wait(co) return rawget(register_name, name) else waitco = {} inquery_name[name] = waitco local addr = skynet.call(clusterd, "lua", "queryname", name:sub(2)) -- name must be '@xxxx' if addr then register_name[name] = addr end inquery_name[name] = nil for _, co in ipairs(waitco) do skynet.wakeup(co) end return addr end end } local function new_register_name() register_name = setmetatable({}, register_name_mt) end new_register_name() local tracetag local function dispatch_request(_,_,addr, session, msg, sz, padding, is_push) ignoreret() -- session is fd, don't call skynet.ret if session == nil then -- trace tracetag = addr return end if padding then local req = large_request[session] or { addr = addr , is_push = is_push, tracetag = tracetag } tracetag = nil large_request[session] = req cluster.append(req, msg, sz) return else local req = large_request[session] if req then tracetag = req.tracetag large_request[session] = nil cluster.append(req, msg, sz) msg,sz = cluster.concat(req) addr = req.addr is_push = req.is_push end if not msg then tracetag = nil local response = cluster.packresponse(session, false, "Invalid large req") socket.write(fd, response) return end end local ok, response if addr == 0 then local name = skynet.unpack(msg, sz) skynet.trash(msg, sz) local addr = register_name["@" .. name] if addr then ok = true msg = skynet.packstring(addr) else ok = false msg = "name not found" end sz = nil else if cluster.isname(addr) then addr = register_name[addr] end if addr then if is_push then skynet.rawsend(addr, "lua", msg, sz) return -- no response else if tracetag then ok , msg, sz = pcall(skynet.tracecall, tracetag, addr, "lua", msg, sz) tracetag = nil else ok , msg, sz = pcall(skynet.rawcall, addr, "lua", msg, sz) end end else ok = false msg = "Invalid name" end end if ok then response = cluster.packresponse(session, true, msg, sz) if type(response) == "table" then for _, v in ipairs(response) do socket.lwrite(fd, v) end else socket.write(fd, response) end else response = cluster.packresponse(session, false, msg) socket.write(fd, response) end end skynet.start(function() skynet.register_protocol { name = "client", id = skynet.PTYPE_CLIENT, unpack = cluster.unpackrequest, dispatch = dispatch_request, } -- fd can write, but don't read fd, the data package will forward from gate though client protocol. -- forward may fail, see https://github.com/cloudwu/skynet/issues/1958 pcall(skynet.call,gate, "lua", "forward", fd) skynet.dispatch("lua", function(_,source, cmd, ...) if cmd == "exit" then socket.close_fd(fd) skynet.exit() elseif cmd == "namechange" then new_register_name() else skynet.error(string.format("Invalid command %s from %s", cmd, skynet.address(source))) end end) end) ================================================ FILE: service/clusterd.lua ================================================ local skynet = require "skynet" require "skynet.manager" local cluster = require "skynet.cluster.core" local config_name = skynet.getenv "cluster" local node_address = {} local node_sender = {} local node_sender_closed = {} local command = {} local config = {} local nodename = cluster.nodename() local connecting = {} local function open_channel(t, key) local ct = connecting[key] if ct then local co = coroutine.running() local channel while ct do table.insert(ct, co) skynet.wait(co) channel = ct.channel ct = connecting[key] -- reload again if ct ~= nil end return assert(node_address[key] and channel) end ct = {} connecting[key] = ct local address = node_address[key] if address == nil and not config.nowaiting then local co = coroutine.running() assert(ct.namequery == nil) ct.namequery = co skynet.error("Waiting for cluster node [".. key.."]") skynet.wait(co) address = node_address[key] end local succ, err, c if address then c = node_sender[key] if c == nil then c = skynet.newservice("clustersender", key, nodename, address) if node_sender[key] then -- double check skynet.kill(c) c = node_sender[key] else node_sender[key] = c end end succ = pcall(skynet.call, c, "lua", "changenode", address) if succ then t[key] = c ct.channel = c node_sender_closed[key] = nil else err = string.format("changenode [%s] (%s) failed", key, address) end elseif address == false then c = node_sender[key] if c == nil or node_sender_closed[key] then -- no sender or closed, always succ succ = true else -- turn off the sender succ, err = pcall(skynet.call, c, "lua", "changenode", false) if succ then --turn off failed, wait next index todo turn off node_sender_closed[key] = true end end else err = string.format("cluster node [%s] is absent.", key) end connecting[key] = nil for _, co in ipairs(ct) do skynet.wakeup(co) end if node_address[key] ~= address then return open_channel(t,key) end assert(succ, err) return c end local node_channel = setmetatable({}, { __index = open_channel }) local function loadconfig(tmp) if tmp == nil then tmp = {} if config_name then local f = assert(io.open(config_name)) local source = f:read "*a" f:close() assert(load(source, "@"..config_name, "t", tmp))() end end local reload = {} for name,address in pairs(tmp) do if name:sub(1,2) == "__" then local opt = name:sub(3) config[opt] = address skynet.error(string.format("Config %s = %s", opt, address)) else assert(address == false or type(address) == "string") if node_address[name] ~= address then -- address changed if node_sender[name] then -- reset connection if node_sender[name] exist node_channel[name] = nil table.insert(reload, name) end node_address[name] = address end local ct = connecting[name] if ct and ct.namequery and not config.nowaiting then skynet.error(string.format("Cluster node [%s] resolved : %s", name, address)) skynet.wakeup(ct.namequery) end end end if config.nowaiting then -- wakeup all connecting request for name, ct in pairs(connecting) do if ct.namequery then skynet.wakeup(ct.namequery) end end end for _, name in ipairs(reload) do -- open_channel would block skynet.fork(open_channel, node_channel, name) end end function command.reload(source, config) loadconfig(config) skynet.ret(skynet.pack(nil)) end function command.listen(source, addr, port, maxclient) local gate = skynet.newservice("gate") if port == nil then local address = assert(node_address[addr], addr .. " is down") skynet.call(gate, "lua", "open", { address = address, port = port, maxclient = maxclient }) skynet.ret(skynet.pack(addr, port)) else local realaddr, realport = skynet.call(gate, "lua", "open", { address = addr, port = port, maxclient = maxclient }) skynet.ret(skynet.pack(realaddr, realport)) end end function command.sender(source, node) skynet.ret(skynet.pack(node_channel[node])) end function command.senders(source) skynet.retpack(node_sender) end local proxy = {} function command.proxy(source, node, name) if name == nil then node, name = node:match "^([^@.]+)([@.].+)" if name == nil then error ("Invalid name " .. tostring(node)) end end local fullname = node .. "." .. name local p = proxy[fullname] if p == nil then p = skynet.newservice("clusterproxy", node, name) -- double check if proxy[fullname] then skynet.kill(p) p = proxy[fullname] else proxy[fullname] = p end end skynet.ret(skynet.pack(p)) end local cluster_agent = {} -- fd:service local register_name = {} local function clearnamecache() for fd, service in pairs(cluster_agent) do if type(service) == "number" then skynet.send(service, "lua", "namechange") end end end function command.register(source, name, addr) assert(register_name[name] == nil) addr = addr or source local old_name = register_name[addr] if old_name then register_name[old_name] = nil clearnamecache() end register_name[addr] = name register_name[name] = addr skynet.ret(nil) skynet.error(string.format("Register [%s] :%08x", name, addr)) end function command.unregister(_, name) if not register_name[name] then return skynet.ret(nil) end local addr = register_name[name] register_name[addr] = nil register_name[name] = nil clearnamecache() skynet.ret(nil) skynet.error(string.format("Unregister [%s] :%08x", name, addr)) end function command.queryname(source, name) skynet.ret(skynet.pack(register_name[name])) end function command.socket(source, subcmd, fd, msg) if subcmd == "open" then skynet.error(string.format("socket accept from %s", msg)) -- new cluster agent cluster_agent[fd] = false local agent = skynet.newservice("clusteragent", skynet.self(), source, fd) local closed = cluster_agent[fd] cluster_agent[fd] = agent if closed then skynet.send(agent, "lua", "exit") cluster_agent[fd] = nil end else if subcmd == "close" or subcmd == "error" then -- close cluster agent local agent = cluster_agent[fd] if type(agent) == "boolean" then cluster_agent[fd] = true elseif agent then skynet.send(agent, "lua", "exit") cluster_agent[fd] = nil end else skynet.error(string.format("socket %s %d %s", subcmd, fd, msg or "")) end end end skynet.start(function() loadconfig() skynet.dispatch("lua", function(session , source, cmd, ...) local f = assert(command[cmd]) f(source, ...) end) end) ================================================ FILE: service/clusterproxy.lua ================================================ local skynet = require "skynet" local cluster = require "skynet.cluster" require "skynet.manager" -- inject skynet.forward_type local node, address = ... skynet.register_protocol { name = "system", id = skynet.PTYPE_SYSTEM, unpack = function (...) return ... end, } local forward_map = { [skynet.PTYPE_SNAX] = skynet.PTYPE_SYSTEM, [skynet.PTYPE_LUA] = skynet.PTYPE_SYSTEM, [skynet.PTYPE_RESPONSE] = skynet.PTYPE_RESPONSE, -- don't free response message } skynet.forward_type( forward_map ,function() local clusterd = skynet.uniqueservice("clusterd") local n = tonumber(address) if n then address = n end local sender = skynet.call(clusterd, "lua", "sender", node) skynet.dispatch("system", function (session, source, msg, sz) if session == 0 then skynet.send(sender, "lua", "push", address, msg, sz) else skynet.ret(skynet.rawcall(sender, "lua", skynet.pack("req", address, msg, sz))) end end) end) ================================================ FILE: service/clustersender.lua ================================================ local skynet = require "skynet" local sc = require "skynet.socketchannel" local socket = require "skynet.socket" local cluster = require "skynet.cluster.core" local channel local session = 1 local node, nodename, init_host, init_port = ... local command = {} local function send_request(addr, msg, sz) -- msg is a local pointer, cluster.packrequest will free it local current_session = session local request, new_session, padding = cluster.packrequest(addr, session, msg, sz) session = new_session local tracetag = skynet.tracetag() if tracetag then if tracetag:sub(1,1) ~= "(" then -- add nodename local newtag = string.format("(%s-%s-%d)%s", nodename, node, session, tracetag) skynet.tracelog(tracetag, string.format("session %s", newtag)) tracetag = newtag end skynet.tracelog(tracetag, string.format("cluster %s", node)) channel:request(cluster.packtrace(tracetag)) end return channel:request(request, current_session, padding) end function command.req(...) local ok, msg = pcall(send_request, ...) if ok then if type(msg) == "table" then skynet.ret(cluster.concat(msg)) else skynet.ret(msg) end else skynet.error(msg) skynet.response()(false) end end function command.push(addr, msg, sz) local request, new_session, padding = cluster.packpush(addr, session, msg, sz) if padding then -- is multi push session = new_session end channel:request(request, nil, padding) end local function read_response(sock) local sz = socket.header(sock:read(2)) local msg = sock:read(sz) return cluster.unpackresponse(msg) -- session, ok, data, padding end function command.changenode(host, port) if not host then skynet.error("Close cluster sender", channel.__host, channel.__port) channel:close() else channel:changehost(host, port) channel:connect(true) end skynet.ret(skynet.pack(nil)) end skynet.start(function() channel = sc.channel { host = init_host, port = tonumber(init_port), response = read_response, nodelay = true, } skynet.dispatch("lua", function(session , source, cmd, ...) local f = assert(command[cmd]) f(...) end) end) ================================================ FILE: service/cmaster.lua ================================================ local skynet = require "skynet" local socket = require "skynet.socket" --[[ master manage data : 1. all the slaves address : id -> ipaddr:port 2. all the global names : name -> address master hold connections from slaves . protocol slave->master : package size 1 byte type 1 byte : 'H' : HANDSHAKE, report slave id, and address. 'R' : REGISTER name address 'Q' : QUERY name protocol master->slave: package size 1 byte type 1 byte : 'W' : WAIT n 'C' : CONNECT slave_id slave_address 'N' : NAME globalname address 'D' : DISCONNECT slave_id ]] local slave_node = {} local global_name = {} local function read_package(fd) local sz = socket.read(fd, 1) assert(sz, "closed") sz = string.byte(sz) local content = assert(socket.read(fd, sz), "closed") return skynet.unpack(content) end local function pack_package(...) local message = skynet.packstring(...) local size = #message assert(size <= 255 , "too long") return string.char(size) .. message end local function report_slave(fd, slave_id, slave_addr) local message = pack_package("C", slave_id, slave_addr) local n = 0 for k,v in pairs(slave_node) do if v.fd ~= 0 then socket.write(v.fd, message) n = n + 1 end end socket.write(fd, pack_package("W", n)) end local function handshake(fd) local t, slave_id, slave_addr = read_package(fd) assert(t=='H', "Invalid handshake type " .. t) assert(slave_id ~= 0 , "Invalid slave id 0") if slave_node[slave_id] then error(string.format("Slave %d already register on %s", slave_id, slave_node[slave_id].addr)) end report_slave(fd, slave_id, slave_addr) slave_node[slave_id] = { fd = fd, id = slave_id, addr = slave_addr, } return slave_id , slave_addr end local function dispatch_slave(fd) local t, name, address = read_package(fd) if t == 'R' then -- register name assert(type(address)=="number", "Invalid request") if not global_name[name] then global_name[name] = address end local message = pack_package("N", name, address) for k,v in pairs(slave_node) do socket.write(v.fd, message) end elseif t == 'Q' then -- query name local address = global_name[name] if address then socket.write(fd, pack_package("N", name, address)) end else skynet.error("Invalid slave message type " .. t) end end local function monitor_slave(slave_id, slave_address) local fd = slave_node[slave_id].fd skynet.error(string.format("Harbor %d (fd=%d) report %s", slave_id, fd, slave_address)) while pcall(dispatch_slave, fd) do end skynet.error("slave " ..slave_id .. " is down") local message = pack_package("D", slave_id) slave_node[slave_id].fd = 0 for k,v in pairs(slave_node) do socket.write(v.fd, message) end socket.close(fd) end skynet.start(function() local master_addr = skynet.getenv "standalone" skynet.error("master listen socket " .. tostring(master_addr)) local fd = socket.listen(master_addr) socket.start(fd , function(id, addr) skynet.error("connect from " .. addr .. " " .. id) socket.start(id) local ok, slave, slave_addr = pcall(handshake, id) if ok then skynet.fork(monitor_slave, slave, slave_addr) else skynet.error(string.format("disconnect fd = %d, error = %s", id, slave)) socket.close(id) end end) end) ================================================ FILE: service/cmemory.lua ================================================ local skynet = require "skynet" local memory = require "skynet.memory" memory.dumpinfo() --memory.dump() local info = memory.info() for k,v in pairs(info) do print(string.format(":%08x %gK",k,v/1024)) end print("Total memory:", memory.total()) print("Total block:", memory.block()) skynet.start(function() skynet.exit() end) ================================================ FILE: service/console.lua ================================================ local skynet = require "skynet" local snax = require "skynet.snax" local socket = require "skynet.socket" local function split_cmdline(cmdline) local split = {} for i in string.gmatch(cmdline, "%S+") do table.insert(split,i) end return split end local function console_main_loop() local stdin = socket.stdin() while true do local cmdline = socket.readline(stdin, "\n") local split = split_cmdline(cmdline) local command = split[1] if command == "snax" then pcall(snax.newservice, select(2, table.unpack(split))) elseif cmdline ~= "" then pcall(skynet.newservice, cmdline) end end end skynet.start(function() skynet.fork(console_main_loop) end) ================================================ FILE: service/cslave.lua ================================================ local skynet = require "skynet" local socket = require "skynet.socket" local socketdriver = require "skynet.socketdriver" require "skynet.manager" -- import skynet.launch, ... local table = table local slaves = {} local connect_queue = {} local globalname = {} local queryname = {} local harbor = {} local harbor_service local monitor = {} local monitor_master_set = {} local function read_package(fd) local sz = socket.read(fd, 1) assert(sz, "closed") sz = string.byte(sz) local content = assert(socket.read(fd, sz), "closed") return skynet.unpack(content) end local function pack_package(...) local message = skynet.packstring(...) local size = #message assert(size <= 255 , "too long") return string.char(size) .. message end local function monitor_clear(id) local v = monitor[id] if v then monitor[id] = nil for _, v in ipairs(v) do v(true) end end end local function connect_slave(slave_id, address) local ok, err = pcall(function() if slaves[slave_id] == nil then local fd = assert(socket.open(address), "Can't connect to "..address) socketdriver.nodelay(fd) skynet.error(string.format("Connect to harbor %d (fd=%d), %s", slave_id, fd, address)) slaves[slave_id] = fd monitor_clear(slave_id) socket.abandon(fd) skynet.send(harbor_service, "harbor", string.format("S %d %d",fd,slave_id)) end end) if not ok then skynet.error(err) end end local function ready() local queue = connect_queue connect_queue = nil for k,v in pairs(queue) do connect_slave(k,v) end for name,address in pairs(globalname) do skynet.redirect(harbor_service, address, "harbor", 0, "N " .. name) end end local function response_name(name) local address = globalname[name] if queryname[name] then local tmp = queryname[name] queryname[name] = nil for _,resp in ipairs(tmp) do resp(true, address) end end end local function monitor_master(master_fd) while true do local ok, t, id_name, address = pcall(read_package,master_fd) if ok then if t == 'C' then if connect_queue then connect_queue[id_name] = address else connect_slave(id_name, address) end elseif t == 'N' then globalname[id_name] = address response_name(id_name) if connect_queue == nil then skynet.redirect(harbor_service, address, "harbor", 0, "N " .. id_name) end elseif t == 'D' then local fd = slaves[id_name] slaves[id_name] = false if fd then monitor_clear(id_name) socket.close(fd) end end else skynet.error("Master disconnect") for _, v in ipairs(monitor_master_set) do v(true) end socket.close(master_fd) break end end end local function accept_slave(fd) socket.start(fd) local id = socket.read(fd, 1) if not id then skynet.error(string.format("Connection (fd =%d) closed", fd)) socket.close(fd) return end id = string.byte(id) if slaves[id] ~= nil then skynet.error(string.format("Slave %d exist (fd =%d)", id, fd)) socket.close(fd) return end slaves[id] = fd monitor_clear(id) socket.abandon(fd) skynet.error(string.format("Harbor %d connected (fd = %d)", id, fd)) skynet.send(harbor_service, "harbor", string.format("A %d %d", fd, id)) end skynet.register_protocol { name = "harbor", id = skynet.PTYPE_HARBOR, pack = function(...) return ... end, unpack = skynet.tostring, } skynet.register_protocol { name = "text", id = skynet.PTYPE_TEXT, pack = function(...) return ... end, unpack = skynet.tostring, } local function monitor_harbor(master_fd) return function(session, source, command) local t = string.sub(command, 1, 1) local arg = string.sub(command, 3) if t == 'Q' then -- query name if globalname[arg] then skynet.redirect(harbor_service, globalname[arg], "harbor", 0, "N " .. arg) else socket.write(master_fd, pack_package("Q", arg)) end elseif t == 'D' then -- harbor down local id = tonumber(arg) if slaves[id] then monitor_clear(id) end slaves[id] = false else skynet.error("Unknown command ", command) end end end function harbor.REGISTER(fd, name, handle) assert(globalname[name] == nil) globalname[name] = handle response_name(name) socket.write(fd, pack_package("R", name, handle)) skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name) end function harbor.LINK(fd, id) if slaves[id] then if monitor[id] == nil then monitor[id] = {} end table.insert(monitor[id], skynet.response()) else skynet.ret() end end function harbor.LINKMASTER() table.insert(monitor_master_set, skynet.response()) end function harbor.CONNECT(fd, id) if not slaves[id] then if monitor[id] == nil then monitor[id] = {} end table.insert(monitor[id], skynet.response()) else skynet.ret() end end function harbor.QUERYNAME(fd, name) if name:byte() == 46 then -- "." , local name skynet.ret(skynet.pack(skynet.localname(name))) return end local result = globalname[name] if result then skynet.ret(skynet.pack(result)) return end local queue = queryname[name] if queue == nil then socket.write(fd, pack_package("Q", name)) queue = { skynet.response() } queryname[name] = queue else table.insert(queue, skynet.response()) end end skynet.start(function() local master_addr = skynet.getenv "master" local harbor_id = tonumber(skynet.getenv "harbor") local slave_address = assert(skynet.getenv "address") local slave_fd = socket.listen(slave_address) skynet.error("slave connect to master " .. tostring(master_addr)) local master_fd = assert(socket.open(master_addr), "Can't connect to master") skynet.dispatch("lua", function (_,_,command,...) local f = assert(harbor[command]) f(master_fd, ...) end) skynet.dispatch("text", monitor_harbor(master_fd)) harbor_service = assert(skynet.launch("harbor", harbor_id, skynet.self())) local hs_message = pack_package("H", harbor_id, slave_address) socket.write(master_fd, hs_message) local t, n = read_package(master_fd) assert(t == "W" and type(n) == "number", "slave shakehand failed") skynet.error(string.format("Waiting for %d harbors", n)) skynet.fork(monitor_master, master_fd) if n > 0 then local co = coroutine.running() socket.start(slave_fd, function(fd, addr) skynet.error(string.format("New connection (fd = %d, %s)",fd, addr)) socketdriver.nodelay(fd) if pcall(accept_slave,fd) then local s = 0 for k,v in pairs(slaves) do s = s + 1 end if s >= n then skynet.wakeup(co) end end end) skynet.wait() end socket.close(slave_fd) skynet.error("Shakehand ready") skynet.fork(ready) end) ================================================ FILE: service/datacenterd.lua ================================================ local skynet = require "skynet" local command = {} local database = {} local wait_queue = {} local mode = {} local function query(db, key, ...) if db == nil or key == nil then return db else return query(db[key], ...) end end function command.QUERY(key, ...) local d = database[key] if d ~= nil then return query(d, ...) end end local function update(db, key, value, ...) if select("#",...) == 0 then local ret = db[key] db[key] = value return ret, value else if db[key] == nil then db[key] = {} end return update(db[key], value, ...) end end local function wakeup(db, key1, ...) if key1 == nil then return end local q = db[key1] if q == nil then return end if q[mode] == "queue" then db[key1] = nil if select("#", ...) ~= 1 then -- throw error because can't wake up a branch for _,response in ipairs(q) do response(false) end else return q end else -- it's branch return wakeup(q , ...) end end function command.UPDATE(...) local ret, value = update(database, ...) if ret ~= nil or value == nil then return ret end local q = wakeup(wait_queue, ...) if q then for _, response in ipairs(q) do response(true,value) end end end local function waitfor(db, key1, key2, ...) if key2 == nil then -- push queue local q = db[key1] if q == nil then q = { [mode] = "queue" } db[key1] = q else assert(q[mode] == "queue") end table.insert(q, skynet.response()) else local q = db[key1] if q == nil then q = { [mode] = "branch" } db[key1] = q else assert(q[mode] == "branch") end return waitfor(q, key2, ...) end end skynet.start(function() skynet.dispatch("lua", function (_, _, cmd, ...) if cmd == "WAIT" then local ret = command.QUERY(...) if ret ~= nil then skynet.ret(skynet.pack(ret)) else waitfor(wait_queue, ...) end else local f = assert(command[cmd]) skynet.ret(skynet.pack(f(...))) end end) end) ================================================ FILE: service/dbg.lua ================================================ local skynet = require "skynet" local cmd = { ... } local function format_table(t) local index = {} for k in pairs(t) do table.insert(index, k) end table.sort(index) local result = {} for _,v in ipairs(index) do table.insert(result, string.format("%s:%s",v,tostring(t[v]))) end return table.concat(result,"\t") end local function dump_line(key, value) if type(value) == "table" then print(key, format_table(value)) else print(key,tostring(value)) end end local function dump_list(list) local index = {} for k in pairs(list) do table.insert(index, k) end table.sort(index) for _,v in ipairs(index) do dump_line(v, list[v]) end end skynet.start(function() local list = skynet.call(".launcher","lua", table.unpack(cmd)) if list then dump_list(list) end skynet.exit() end) ================================================ FILE: service/debug_agent.lua ================================================ local skynet = require "skynet" local debugchannel = require "skynet.debugchannel" local CMD = {} local channel function CMD.start(address, fd) assert(channel == nil, "start more than once") skynet.error(string.format("Attach to :%08x", address)) local handle channel, handle = debugchannel.create() local ok, err = pcall(skynet.call, address, "debug", "REMOTEDEBUG", fd, handle) if not ok then skynet.ret(skynet.pack(false, "Debugger attach failed")) else -- todo hook skynet.ret(skynet.pack(true)) end skynet.exit() end function CMD.cmd(cmdline) channel:write(cmdline) end function CMD.ping() skynet.ret() end skynet.start(function() skynet.dispatch("lua", function(_,_,cmd,...) local f = CMD[cmd] f(...) end) end) ================================================ FILE: service/debug_console.lua ================================================ local skynet = require "skynet" local codecache = require "skynet.codecache" local core = require "skynet.core" local socket = require "skynet.socket" local snax = require "skynet.snax" local memory = require "skynet.memory" local httpd = require "http.httpd" local sockethelper = require "http.sockethelper" local arg = table.pack(...) assert(arg.n <= 2) local ip = (arg.n == 2 and arg[1] or "127.0.0.1") local port = tonumber(arg[arg.n]) local TIMEOUT = 300 -- 3 sec local COMMAND = {} local COMMANDX = {} local function format_table(t) local index = {} for k in pairs(t) do table.insert(index, k) end table.sort(index, function(a, b) return tostring(a) < tostring(b) end) local result = {} for _,v in ipairs(index) do table.insert(result, string.format("%s:%s",v,tostring(t[v]))) end return table.concat(result,"\t") end local function dump_line(print, key, value) if type(value) == "table" then print(key, format_table(value)) else print(key,tostring(value)) end end local function dump_list(print, list) local index = {} for k in pairs(list) do table.insert(index, k) end table.sort(index, function(a, b) return tostring(a) < tostring(b) end) for _,v in ipairs(index) do dump_line(print, v, list[v]) end end local function split_cmdline(cmdline) local split = {} for i in string.gmatch(cmdline, "%S+") do table.insert(split,i) end return split end local function docmd(cmdline, print, fd) local split = split_cmdline(cmdline) local command = split[1] local cmd = COMMAND[command] local ok, list if cmd then ok, list = pcall(cmd, table.unpack(split,2)) else cmd = COMMANDX[command] if cmd then split.fd = fd split[1] = cmdline ok, list = pcall(cmd, split) else print("Invalid command, type help for command list") end end if ok then if list then if type(list) == "string" then print(list) else dump_list(print, list) end end print("") else print(list) print("") end end local function console_main_loop(stdin, print, addr) print("Welcome to skynet console") skynet.error(addr, "connected") local ok, err = pcall(function() while true do local cmdline = socket.readline(stdin, "\n") if not cmdline then break end if cmdline:sub(1,4) == "GET " then -- http local code, url = httpd.read_request(sockethelper.readfunc(stdin, cmdline.. "\n"), 8192) local cmdline = url:sub(2):gsub("/"," ") docmd(cmdline, print, stdin) break elseif cmdline:sub(1,5) == "POST " then -- http post local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(stdin, cmdline.. "\n"), 8192) docmd(body, print, stdin) break end if cmdline ~= "" then docmd(cmdline, print, stdin) end end end) if not ok then skynet.error(stdin, err) end skynet.error(addr, "disconnect") socket.close(stdin) end skynet.start(function() local listen_socket, ip, port = socket.listen (ip, port) skynet.error("Start debug console at " .. ip .. ":" .. port) socket.start(listen_socket , function(id, addr) local function print(...) local t = { ... } for k,v in ipairs(t) do t[k] = tostring(v) end socket.write(id, table.concat(t,"\t")) socket.write(id, "\n") end socket.start(id) skynet.fork(console_main_loop, id , print, addr) end) end) function COMMAND.help() return { help = "This help message", list = "List all the service", stat = "Dump all stats", info = "info address : get service infomation", exit = "exit address : kill a lua service", kill = "kill address : kill service", mem = "mem : show memory status", gc = "gc : force every lua service do garbage collect", start = "lanuch a new lua service", snax = "lanuch a new snax service", clearcache = "clear lua code cache", service = "List unique service", task = "task address : show service task detail", uniqtask = "task address : show service unique task detail", inject = "inject address luascript.lua", logon = "logon address", logoff = "logoff address", log = "launch a new lua service with log", debug = "debug address : debug a lua service", signal = "signal address sig", cmem = "Show C memory info", jmem = "Show jemalloc mem stats", ping = "ping address", call = "call address ...", trace = "trace address [proto] [on|off]", netstat = "netstat : show netstat", profactive = "profactive [on|off] : active/deactive jemalloc heap profilling", dumpheap = "dumpheap : dump heap profilling", killtask = "killtask address threadname : threadname listed by task", dbgcmd = "run address debug command", getenv = "getenv name : skynet.getenv(name)", setenv = "setenv name value: skynet.setenv(name,value)", } end function COMMAND.clearcache() codecache.clear() end function COMMAND.start(...) local ok, addr = pcall(skynet.newservice, ...) if ok then if addr then return { [skynet.address(addr)] = ... } else return "Exit" end else return "Failed" end end function COMMAND.log(...) local ok, addr = pcall(skynet.call, ".launcher", "lua", "LOGLAUNCH", "snlua", ...) if ok then if addr then return { [skynet.address(addr)] = ... } else return "Failed" end else return "Failed" end end function COMMAND.snax(...) local ok, s = pcall(snax.newservice, ...) if ok then local addr = s.handle return { [skynet.address(addr)] = ... } else return "Failed" end end function COMMAND.service() return skynet.call("SERVICE", "lua", "LIST") end local function adjust_address(address) local prefix = address:sub(1,1) if prefix == '.' then return assert(skynet.localname(address), "Not a valid name") elseif prefix ~= ':' then address = assert(tonumber("0x" .. address), "Need an address") | (skynet.harbor(skynet.self()) << 24) end return address end function COMMAND.list() return skynet.call(".launcher", "lua", "LIST") end local function timeout(ti) if ti then ti = tonumber(ti) if ti <= 0 then ti = nil end else ti = TIMEOUT end return ti end function COMMAND.stat(ti) return skynet.call(".launcher", "lua", "STAT", timeout(ti)) end function COMMAND.mem(ti) return skynet.call(".launcher", "lua", "MEM", timeout(ti)) end function COMMAND.kill(address) return skynet.call(".launcher", "lua", "KILL", adjust_address(address)) end function COMMAND.gc(ti) return skynet.call(".launcher", "lua", "GC", timeout(ti)) end function COMMAND.exit(address) skynet.send(adjust_address(address), "debug", "EXIT") end function COMMAND.inject(address, filename, ...) address = adjust_address(address) local f = io.open(filename, "rb") if not f then return "Can't open " .. filename end local source = f:read "*a" f:close() local ok, output = skynet.call(address, "debug", "RUN", source, filename, ...) if ok == false then error(output) end return output end function COMMAND.dbgcmd(address, cmd, ...) address = adjust_address(address) return skynet.call(address, "debug", cmd, ...) end function COMMAND.task(address) return COMMAND.dbgcmd(address, "TASK") end function COMMAND.killtask(address, threadname) return COMMAND.dbgcmd(address, "KILLTASK", threadname) end function COMMAND.uniqtask(address) return COMMAND.dbgcmd(address, "UNIQTASK") end function COMMAND.info(address, ...) return COMMAND.dbgcmd(address, "INFO", ...) end function COMMANDX.debug(cmd) local address = adjust_address(cmd[2]) local agent = skynet.newservice "debug_agent" local stop local term_co = coroutine.running() local function forward_cmd() repeat -- notice : It's a bad practice to call socket.readline from two threads (this one and console_main_loop), be careful. skynet.call(agent, "lua", "ping") -- detect agent alive, if agent exit, raise error local cmdline = socket.readline(cmd.fd, "\n") cmdline = cmdline and cmdline:gsub("(.*)\r$", "%1") if not cmdline then skynet.send(agent, "lua", "cmd", "cont") break end skynet.send(agent, "lua", "cmd", cmdline) until stop or cmdline == "cont" end skynet.fork(function() pcall(forward_cmd) if not stop then -- block at skynet.call "start" term_co = nil else skynet.wakeup(term_co) end end) local ok, err = skynet.call(agent, "lua", "start", address, cmd.fd) stop = true if term_co then -- wait for fork coroutine exit. skynet.wait(term_co) end if not ok then error(err) end end function COMMAND.logon(address) address = adjust_address(address) core.command("LOGON", skynet.address(address)) end function COMMAND.logoff(address) address = adjust_address(address) core.command("LOGOFF", skynet.address(address)) end function COMMAND.signal(address, sig) address = skynet.address(adjust_address(address)) if sig then core.command("SIGNAL", string.format("%s %d",address,sig)) else core.command("SIGNAL", address) end end function COMMAND.cmem() local info = memory.info() local tmp = {} for k,v in pairs(info) do tmp[skynet.address(k)] = v end tmp.total = memory.total() tmp.block = memory.block() return tmp end function COMMAND.jmem() local info = memory.jestat() local tmp = {} for k,v in pairs(info) do tmp[k] = string.format("%11d %8.2f Mb", v, v/1048576) end return tmp end function COMMAND.ping(address) address = adjust_address(address) local ti = skynet.now() skynet.call(address, "debug", "PING") ti = skynet.now() - ti return tostring(ti) end local function toboolean(x) return x and (x == "true" or x == "on") end function COMMAND.trace(address, proto, flag) address = adjust_address(address) if flag == nil then if proto == "on" or proto == "off" then proto = toboolean(proto) end else flag = toboolean(flag) end skynet.call(address, "debug", "TRACELOG", proto, flag) end function COMMANDX.call(cmd) local address = adjust_address(cmd[2]) local cmdline = assert(cmd[1]:match("%S+%s+%S+%s(.+)") , "need arguments") local args_func = assert(load("return " .. cmdline, "debug console", "t", {}), "Invalid arguments") local args = table.pack(pcall(args_func)) if not args[1] then error(args[2]) end local rets = table.pack(skynet.call(address, "lua", table.unpack(args, 2, args.n))) return rets end local function bytes(size) if size == nil or size == 0 then return end if size < 1024 then return size end if size < 1024 * 1024 then return tostring(size/1024) .. "K" end return tostring(size/(1024*1024)) .. "M" end local function convert_stat(info) local now = skynet.now() local function time(t) if t == nil then return end t = now - t if t < 6000 then return tostring(t/100) .. "s" end local hour = t // (100*60*60) t = t - hour * 100 * 60 * 60 local min = t // (100*60) t = t - min * 100 * 60 local sec = t / 100 return string.format("%s%d:%.2gs",hour == 0 and "" or (hour .. ":"),min,sec) end info.address = skynet.address(info.address) info.read = bytes(info.read) info.write = bytes(info.write) info.wbuffer = bytes(info.wbuffer) info.rtime = time(info.rtime) info.wtime = time(info.wtime) end function COMMAND.netstat() local stat = socket.netstat() for _, info in ipairs(stat) do convert_stat(info) end return stat end function COMMAND.dumpheap() memory.dumpheap() end function COMMAND.profactive(flag) if flag ~= nil then if flag == "on" or flag == "off" then flag = toboolean(flag) end memory.profactive(flag) end local active = memory.profactive() return "heap profilling is ".. (active and "active" or "deactive") end function COMMAND.getenv(name) local value = skynet.getenv(name) return {[name]=tostring(value)} end function COMMAND.setenv(name,value) return skynet.setenv(name,value) end ================================================ FILE: service/gate.lua ================================================ local skynet = require "skynet" local gateserver = require "snax.gateserver" local watchdog local connection = {} -- fd -> connection : { fd , client, agent , ip, mode } skynet.register_protocol { name = "client", id = skynet.PTYPE_CLIENT, } local handler = {} function handler.open(source, conf) watchdog = conf.watchdog or source return conf.address, conf.port end function handler.message(fd, msg, sz) -- recv a package, forward it local c = connection[fd] local agent = c.agent if agent then -- It's safe to redirect msg directly , gateserver framework will not free msg. skynet.redirect(agent, c.client, "client", fd, msg, sz) else skynet.send(watchdog, "lua", "socket", "data", fd, skynet.tostring(msg, sz)) -- skynet.tostring will copy msg to a string, so we must free msg here. skynet.trash(msg,sz) end end function handler.connect(fd, addr) local c = { fd = fd, ip = addr, } connection[fd] = c skynet.send(watchdog, "lua", "socket", "open", fd, addr) end local function unforward(c) if c.agent then c.agent = nil c.client = nil end end local function close_fd(fd) local c = connection[fd] if c then unforward(c) connection[fd] = nil end end function handler.disconnect(fd) close_fd(fd) skynet.send(watchdog, "lua", "socket", "close", fd) end function handler.error(fd, msg) close_fd(fd) skynet.send(watchdog, "lua", "socket", "error", fd, msg) end function handler.warning(fd, size) skynet.send(watchdog, "lua", "socket", "warning", fd, size) end local CMD = {} function CMD.forward(source, fd, client, address) local c = assert(connection[fd]) unforward(c) c.client = client or 0 c.agent = address or source gateserver.openclient(fd) end function CMD.accept(source, fd) local c = assert(connection[fd]) unforward(c) gateserver.openclient(fd) end function CMD.kick(source, fd) gateserver.closeclient(fd) end function handler.command(cmd, source, ...) local f = assert(CMD[cmd]) return f(source, ...) end gateserver.start(handler) ================================================ FILE: service/launcher.lua ================================================ local skynet = require "skynet" local core = require "skynet.core" require "skynet.manager" -- import manager apis local string = string local services = {} local command = {} local instance = {} -- for confirm (function command.LAUNCH / command.ERROR / command.LAUNCHOK) local launch_session = {} -- for command.QUERY, service_address -> session local function handle_to_address(handle) return tonumber("0x" .. string.sub(handle , 2)) end local NORET = {} function command.LIST() local list = {} for k,v in pairs(services) do list[skynet.address(k)] = v end return list end local function list_srv(ti, fmt_func, ...) local list = {} local sessions = {} local req = skynet.request() for addr in pairs(services) do local r = { addr, "debug", ... } req:add(r) sessions[r] = addr end for req, resp in req:select(ti) do local addr = req[1] if resp then local stat = resp[1] list[skynet.address(addr)] = fmt_func(stat, addr) else list[skynet.address(addr)] = fmt_func("ERROR", addr) end sessions[req] = nil end for session, addr in pairs(sessions) do list[skynet.address(addr)] = fmt_func("TIMEOUT", addr) end return list end function command.STAT(addr, ti) return list_srv(ti, function(v) return v end, "STAT") end function command.KILL(_, handle) skynet.kill(handle) local ret = { [skynet.address(handle)] = tostring(services[handle]) } services[handle] = nil return ret end function command.MEM(addr, ti) return list_srv(ti, function(kb, addr) local v = services[addr] if type(kb) == "string" then return string.format("%s (%s)", kb, v) else return string.format("%.2f Kb (%s)",kb,v) end end, "MEM") end function command.GC(addr, ti) for k,v in pairs(services) do skynet.send(k,"debug","GC") end return command.MEM(addr, ti) end function command.REMOVE(_, handle, kill) services[handle] = nil local response = instance[handle] if response then -- instance is dead response(not kill) -- return nil to caller of newservice, when kill == false instance[handle] = nil launch_session[handle] = nil end -- don't return (skynet.ret) because the handle may exit return NORET end local function launch_service(service, ...) local param = table.concat({...}, " ") local inst = skynet.launch(service, param) local session = skynet.context() local response = skynet.response() if inst then services[inst] = service .. " " .. param instance[inst] = response launch_session[inst] = session else response(false) return end return inst end function command.LAUNCH(_, service, ...) launch_service(service, ...) return NORET end function command.LOGLAUNCH(_, service, ...) local inst = launch_service(service, ...) if inst then core.command("LOGON", skynet.address(inst)) end return NORET end function command.ERROR(address) -- see serivce-src/service_lua.c -- init failed local response = instance[address] if response then response(false) launch_session[address] = nil instance[address] = nil end services[address] = nil return NORET end function command.LAUNCHOK(address) -- init notice local response = instance[address] if response then response(true, address) instance[address] = nil launch_session[address] = nil end return NORET end function command.QUERY(_, request_session) for address, session in pairs(launch_session) do if session == request_session then return address end end end -- for historical reasons, launcher support text command (for C service) skynet.register_protocol { name = "text", id = skynet.PTYPE_TEXT, unpack = skynet.tostring, dispatch = function(session, address , cmd) if cmd == "" then command.LAUNCHOK(address) elseif cmd == "ERROR" then command.ERROR(address) else error ("Invalid text command " .. cmd) end end, } skynet.dispatch("lua", function(session, address, cmd , ...) cmd = string.upper(cmd) local f = command[cmd] if f then local ret = f(address, ...) if ret ~= NORET then skynet.ret(skynet.pack(ret)) end else skynet.ret(skynet.pack {"Unknown command"} ) end end) skynet.start(function() end) ================================================ FILE: service/multicastd.lua ================================================ local skynet = require "skynet" local mc = require "skynet.multicast.core" local datacenter = require "skynet.datacenter" local harbor_id = skynet.harbor(skynet.self()) local command = {} local channel = {} local channel_n = {} local channel_remote = {} local channel_id = harbor_id local NORET = {} local function get_address(t, id) local v = assert(datacenter.get("multicast", id)) t[id] = v return v end local node_address = setmetatable({}, { __index = get_address }) -- new LOCAL channel , The low 8bit is the same with harbor_id function command.NEW() while channel[channel_id] do channel_id = mc.nextid(channel_id) end channel[channel_id] = {} channel_n[channel_id] = 0 local ret = channel_id channel_id = mc.nextid(channel_id) return ret end -- MUST call by the owner node of channel, delete a remote channel function command.DELR(source, c) channel[c] = nil channel_n[c] = nil return NORET end -- delete a channel, if the channel is remote, forward the command to the owner node -- otherwise, delete the channel, and call all the remote node, DELR function command.DEL(source, c) local node = c % 256 if node ~= harbor_id then skynet.send(node_address[node], "lua", "DEL", c) return NORET end local remote = channel_remote[c] channel[c] = nil channel_n[c] = nil channel_remote[c] = nil if remote then for node in pairs(remote) do skynet.send(node_address[node], "lua", "DELR", c) end end return NORET end -- forward multicast message to a node (channel id use the session field) local function remote_publish(node, channel, source, ...) skynet.redirect(node_address[node], source, "multicast", channel, ...) end -- publish a message, for local node, use the message pointer (call mc.bind to add the reference) -- for remote node, call remote_publish. (call mc.unpack and skynet.tostring to convert message pointer to string) local function publish(c , source, pack, size) local remote = channel_remote[c] if remote then -- remote publish should unpack the pack, because we should not publish the pointer out. local _, msg, sz = mc.unpack(pack, size) local msg = skynet.tostring(msg,sz) for node in pairs(remote) do remote_publish(node, c, source, msg) end end local group = channel[c] if group == nil or next(group) == nil then -- dead channel, delete the pack. mc.bind returns the pointer in pack and free the pack (struct mc_package **) local pack = mc.bind(pack, 1) mc.close(pack) return end local msg = skynet.tostring(pack, size) -- copy (pack,size) to a string mc.bind(pack, channel_n[c]) -- mc.bind will free the pack(struct mc_package **) for k in pairs(group) do -- the msg is a pointer to the real message, publish pointer in local is ok. skynet.redirect(k, source, "multicast", c , msg) end end skynet.register_protocol { name = "multicast", id = skynet.PTYPE_MULTICAST, unpack = function(msg, sz) return mc.packremote(msg, sz) end, dispatch = function (...) skynet.ignoreret() publish(...) end, } -- publish a message, if the caller is remote, forward the message to the owner node (by remote_publish) -- If the caller is local, call publish function command.PUB(source, c, pack, size) assert(skynet.harbor(source) == harbor_id) local node = c % 256 if node ~= harbor_id then -- remote publish remote_publish(node, c, source, mc.remote(pack)) else publish(c, source, pack,size) end end -- the node (source) subscribe a channel -- MUST call by channel owner node (assert source is not local and channel is create by self) -- If channel is not exist, return true -- Else set channel_remote[channel] true function command.SUBR(source, c) local node = skynet.harbor(source) if not channel[c] then -- channel none exist return true end assert(node ~= harbor_id and c % 256 == harbor_id) local group = channel_remote[c] if group == nil then group = {} channel_remote[c] = group end group[node] = true end -- the service (source) subscribe a channel -- If the channel is remote, node subscribe it by send a SUBR to the owner . function command.SUB(source, c) local node = c % 256 if node ~= harbor_id then -- remote group if channel[c] == nil then if skynet.call(node_address[node], "lua", "SUBR", c) then return end if channel[c] == nil then -- double check, because skynet.call whould yield, other SUB may occur. channel[c] = {} channel_n[c] = 0 end end end local group = channel[c] if group and not group[source] then channel_n[c] = channel_n[c] + 1 group[source] = true end end -- MUST call by a node, unsubscribe a channel function command.USUBR(source, c) local node = skynet.harbor(source) assert(node ~= harbor_id) local group = assert(channel_remote[c]) group[node] = nil return NORET end -- Unsubscribe a channel, if the subscriber is empty and the channel is remote, send USUBR to the channel owner function command.USUB(source, c) local group = assert(channel[c]) if group[source] then group[source] = nil channel_n[c] = channel_n[c] - 1 if channel_n[c] == 0 then local node = c % 256 if node ~= harbor_id then -- remote group channel[c] = nil channel_n[c] = nil skynet.send(node_address[node], "lua", "USUBR", c) end end end return NORET end skynet.start(function() skynet.dispatch("lua", function(_,source, cmd, ...) local f = assert(command[cmd]) local result = f(source, ...) if result ~= NORET then skynet.ret(skynet.pack(result)) end end) local self = skynet.self() local id = skynet.harbor(self) assert(datacenter.set("multicast", id, self) == nil) end) ================================================ FILE: service/service_cell.lua ================================================ local skynet = require "skynet" local service_name = (...) local init = {} function init.init(code, ...) local start_func skynet.start = function(f) start_func = f end skynet.dispatch("lua", function() error("No dispatch function") end) local mainfunc = assert(load(code, service_name)) assert(skynet.pcall(mainfunc,...)) if start_func then start_func() end skynet.ret() end skynet.start(function() skynet.dispatch("lua", function(_,_,cmd,...) init[cmd](...) end) end) ================================================ FILE: service/service_mgr.lua ================================================ local skynet = require "skynet" require "skynet.manager" -- import skynet.register local snax = require "skynet.snax" local cmd = {} local service = {} local function request(name, func, ...) local ok, handle = pcall(func, ...) local s = service[name] assert(type(s) == "table") if ok then service[name] = handle else service[name] = tostring(handle) end for _,v in ipairs(s) do skynet.wakeup(v.co) end if ok then return handle else error(tostring(handle)) end end local function waitfor(name , func, ...) local s = service[name] if type(s) == "number" then return s end local co = coroutine.running() if s == nil then s = {} service[name] = s elseif type(s) == "string" then error(s) end assert(type(s) == "table") local session, source = skynet.context() if s.launch == nil and func then s.launch = { session = session, source = source, co = co, } return request(name, func, ...) end table.insert(s, { co = co, session = session, source = source, }) skynet.wait() s = service[name] if type(s) == "string" then error(s) end assert(type(s) == "number") return s end local function read_name(service_name) if string.byte(service_name) == 64 then -- '@' return string.sub(service_name , 2) else return service_name end end function cmd.LAUNCH(service_name, subname, ...) local realname = read_name(service_name) if realname == "snaxd" then return waitfor(service_name.."."..subname, snax.rawnewservice, subname, ...) else return waitfor(service_name, skynet.newservice, realname, subname, ...) end end function cmd.QUERY(service_name, subname) local realname = read_name(service_name) if realname == "snaxd" then return waitfor(service_name.."."..subname) else return waitfor(service_name) end end local function list_service() local val local result = {} for k,v in pairs(service) do if type(v) == "string" then val = "Error: " .. v elseif type(v) == "table" then local querying = {} if v.launch then local session = skynet.task(v.launch.co) local launching_address = skynet.call(".launcher", "lua", "QUERY", session) if launching_address then table.insert(querying, "Init as " .. skynet.address(launching_address)) table.insert(querying, skynet.call(launching_address, "debug", "TASK", "init")) table.insert(querying, "Launching from " .. skynet.address(v.launch.source)) table.insert(querying, skynet.call(v.launch.source, "debug", "TASK", v.launch.session)) end end if #v > 0 then table.insert(querying , "Querying:" ) for _, detail in ipairs(v) do table.insert(querying, skynet.address(detail.source) .. " " .. tostring(skynet.call(detail.source, "debug", "TASK", detail.session))) end end val = table.concat(querying, "\n") else val = skynet.address(v) end result[k] = val end return result end local function register_global() function cmd.GLAUNCH(name, ...) local global_name = "@" .. name return cmd.LAUNCH(global_name, ...) end function cmd.GQUERY(name, ...) local global_name = "@" .. name return cmd.QUERY(global_name, ...) end local mgr = {} function cmd.REPORT(m) mgr[m] = true end local function add_list(all, m) local harbor = "@" .. skynet.harbor(m) local result = skynet.call(m, "lua", "LIST") for k,v in pairs(result) do all[k .. harbor] = v end end function cmd.LIST() local result = {} for k in pairs(mgr) do pcall(add_list, result, k) end local l = list_service() for k, v in pairs(l) do result[k] = v end return result end end local function register_local() local function waitfor_remote(cmd, name, ...) local global_name = "@" .. name local local_name if name == "snaxd" then local_name = global_name .. "." .. (...) else local_name = global_name end return waitfor(local_name, skynet.call, "SERVICE", "lua", cmd, global_name, ...) end function cmd.GLAUNCH(...) return waitfor_remote("LAUNCH", ...) end function cmd.GQUERY(...) return waitfor_remote("QUERY", ...) end function cmd.LIST() return list_service() end skynet.call("SERVICE", "lua", "REPORT", skynet.self()) end skynet.start(function() skynet.dispatch("lua", function(session, address, command, ...) local f = cmd[command] if f == nil then skynet.ret(skynet.pack(nil, "Invalid command " .. command)) return end local ok, r = pcall(f, ...) if ok then skynet.ret(skynet.pack(r)) else skynet.ret(skynet.pack(nil, r)) end end) local handle = skynet.localname ".service" if handle then skynet.error(".service is already register by ", skynet.address(handle)) skynet.exit() else skynet.register(".service") end if skynet.getenv "standalone" then skynet.register("SERVICE") register_global() else register_local() end end) ================================================ FILE: service/service_provider.lua ================================================ local skynet = require "skynet" local provider = {} local function new_service(svr, name) local s = {} svr[name] = s s.queue = {} return s end local svr = setmetatable({}, { __index = new_service }) function provider.query(name) local s = svr[name] if s.queue then table.insert(s.queue, skynet.response()) else if s.address then return skynet.ret(skynet.pack(s.address)) else error(s.error) end end end local function boot(addr, name, code, ...) local s = svr[name] skynet.call(addr, "lua", "init", code, ...) local tmp = table.pack( ... ) for i=1,tmp.n do tmp[i] = tostring(tmp[i]) end if tmp.n > 0 then s.init = table.concat(tmp, ",") end s.time = skynet.time() end function provider.launch(name, code, ...) local s = svr[name] if s.address then return skynet.ret(skynet.pack(s.address)) end if s.booting then table.insert(s.queue, skynet.response()) else s.booting = true local err local ok, addr = pcall(skynet.newservice,"service_cell", name) if ok then ok, err = xpcall(boot, debug.traceback, addr, name, code, ...) else err = addr addr = nil end s.booting = nil if ok then s.address = addr for _, resp in ipairs(s.queue) do resp(true, addr) end s.queue = nil skynet.ret(skynet.pack(addr)) else if addr then skynet.send(addr, "debug", "EXIT") end s.error = err for _, resp in ipairs(s.queue) do resp(false) end s.queue = nil error(err) end end end function provider.test(name) local s = svr[name] if s.booting then skynet.ret(skynet.pack(nil, true)) -- booting elseif s.address then skynet.ret(skynet.pack(s.address)) elseif s.error then error(s.error) else skynet.ret() -- nil end end function provider.close(name) local s = svr[name] if not s or s.booting then return skynet.ret(skynet.pack(nil)) end svr[name] = nil skynet.ret(skynet.pack(s.address)) end skynet.start(function() skynet.dispatch("lua", function(session, address, cmd, ...) provider[cmd](...) end) skynet.info_func(function() local info = {} for k,v in pairs(svr) do local status if v.booting then status = "booting" elseif v.queue then status = "waiting(" .. #v.queue .. ")" end info[skynet.address(v.address)] = { init = v.init, name = k, time = os.date("%Y %b %d %T %z",math.floor(v.time)), status = status, } end return info end) end) ================================================ FILE: service/sharedatad.lua ================================================ local skynet = require "skynet" local sharedata = require "skynet.sharedata.corelib" local table = table local cache = require "skynet.codecache" cache.mode "OFF" -- turn off codecache, because CMD.new may load data file local NORET = {} local pool = {} local pool_count = {} local objmap = {} local collect_tick = 10 local function newobj(name, tbl) assert(pool[name] == nil) local cobj = sharedata.host.new(tbl) sharedata.host.incref(cobj) local v = {obj = cobj, watch = {} } objmap[cobj] = v pool[name] = v pool_count[name] = { n = 0, threshold = 16 } end local function collect1min() if collect_tick > 1 then collect_tick = 1 end end local function collectobj() while true do skynet.sleep(60*100) -- sleep 1min if collect_tick <= 0 then collect_tick = 10 -- reset tick count to 10 min collectgarbage() for obj, v in pairs(objmap) do if v == true then if sharedata.host.getref(obj) <= 0 then objmap[obj] = nil sharedata.host.delete(obj) end end end else collect_tick = collect_tick - 1 end end end local CMD = {} local env_mt = { __index = _ENV } function CMD.new(name, t, ...) local dt = type(t) local value if dt == "table" then value = t elseif dt == "string" then value = setmetatable({}, env_mt) local f if t:sub(1,1) == "@" then f = assert(loadfile(t:sub(2),"bt",value)) else f = assert(load(t, "=" .. name, "bt", value)) end local _, ret = assert(skynet.pcall(f, ...)) setmetatable(value, nil) if type(ret) == "table" then value = ret end elseif dt == "nil" then value = {} else error ("Unknown data type " .. dt) end newobj(name, value) end function CMD.delete(name) local v = assert(pool[name]) pool[name] = nil pool_count[name] = nil assert(objmap[v.obj]) objmap[v.obj] = true sharedata.host.decref(v.obj) for _,response in pairs(v.watch) do response(true) end end function CMD.query(name) local v = assert(pool[name], name) local obj = v.obj sharedata.host.incref(obj) return v.obj end function CMD.confirm(cobj) if objmap[cobj] then sharedata.host.decref(cobj) end return NORET end function CMD.update(name, t, ...) local v = pool[name] local watch, oldcobj if v then watch = v.watch oldcobj = v.obj objmap[oldcobj] = true sharedata.host.decref(oldcobj) pool[name] = nil pool_count[name] = nil end CMD.new(name, t, ...) local newobj = pool[name].obj if watch then sharedata.host.markdirty(oldcobj) for _,response in pairs(watch) do sharedata.host.incref(newobj) response(true, newobj) end end collect1min() -- collect in 1 min end local function check_watch(queue) local n = 0 for k,response in pairs(queue) do if not response "TEST" then queue[k] = nil n = n + 1 end end return n end function CMD.monitor(name, obj) local v = assert(pool[name]) if obj ~= v.obj then sharedata.host.incref(v.obj) return v.obj end local n = pool_count[name].n + 1 if n > pool_count[name].threshold then n = n - check_watch(v.watch) pool_count[name].threshold = n * 2 end pool_count[name].n = n table.insert(v.watch, skynet.response()) return NORET end skynet.start(function() skynet.fork(collectobj) skynet.dispatch("lua", function (session, source ,cmd, ...) local f = assert(CMD[cmd]) local r = f(...) if r ~= NORET then skynet.ret(skynet.pack(r)) end end) end) ================================================ FILE: service/snaxd.lua ================================================ local skynet = require "skynet" local c = require "skynet.core" local snax_interface = require "snax.interface" local profile = require "skynet.profile" local snax = require "skynet.snax" local snax_name = tostring(...) local loaderpath = skynet.getenv"snax_loader" local loader = loaderpath and assert(dofile(loaderpath)) local func, pattern = snax_interface(snax_name, _ENV, loader) local snax_path = pattern:sub(1,pattern:find("?", 1, true)-1) .. snax_name .. "/" package.path = snax_path .. "?.lua;" .. package.path SERVICE_NAME = snax_name SERVICE_PATH = snax_path local profile_table = {} local function update_stat(name, ti) local t = profile_table[name] if t == nil then t = { count = 0, time = 0 } profile_table[name] = t end t.count = t.count + 1 t.time = t.time + ti end local traceback = debug.traceback local function return_f(f, ...) return skynet.ret(skynet.pack(f(...))) end local function timing( method, ... ) local err, msg profile.start() if method[2] == "accept" then -- no return err,msg = xpcall(method[4], traceback, ...) else err,msg = xpcall(return_f, traceback, method[4], ...) end local ti = profile.stop() update_stat(method[3], ti) assert(err,msg) end skynet.start(function() local init = false local function dispatcher( session , source , id, ...) local method = func[id] if method[2] == "system" then local command = method[3] if command == "hotfix" then local hotfix = require "snax.hotfix" skynet.ret(skynet.pack(hotfix(func, ...))) elseif command == "profile" then skynet.ret(skynet.pack(profile_table)) elseif command == "init" then assert(not init, "Already init") local initfunc = method[4] or function() end initfunc(...) skynet.ret() skynet.info_func(function() return profile_table end) init = true else assert(init, "Never init") assert(command == "exit") local exitfunc = method[4] or function() end exitfunc(...) skynet.ret() init = false skynet.exit() end else assert(init, "Init first") timing(method, ...) end end skynet.dispatch("snax", dispatcher) -- set lua dispatcher function snax.enablecluster() skynet.dispatch("lua", dispatcher) end end) ================================================ FILE: service-src/databuffer.h ================================================ #ifndef skynet_databuffer_h #define skynet_databuffer_h #include #include #include #define MESSAGEPOOL 1023 struct message { char * buffer; int size; struct message * next; }; struct databuffer { int header; int offset; int size; struct message * head; struct message * tail; }; struct messagepool_list { struct messagepool_list *next; struct message pool[MESSAGEPOOL]; }; struct messagepool { struct messagepool_list * pool; struct message * freelist; }; // use memset init struct static void messagepool_free(struct messagepool *pool) { struct messagepool_list *p = pool->pool; while(p) { struct messagepool_list *tmp = p; p=p->next; skynet_free(tmp); } pool->pool = NULL; pool->freelist = NULL; } static inline void _return_message(struct databuffer *db, struct messagepool *mp) { struct message *m = db->head; if (m->next == NULL) { assert(db->tail == m); db->head = db->tail = NULL; } else { db->head = m->next; } skynet_free(m->buffer); m->buffer = NULL; m->size = 0; m->next = mp->freelist; mp->freelist = m; } static void databuffer_read(struct databuffer *db, struct messagepool *mp, char * buffer, int sz) { assert(db->size >= sz); db->size -= sz; for (;;) { struct message *current = db->head; int bsz = current->size - db->offset; if (bsz > sz) { memcpy(buffer, current->buffer + db->offset, sz); db->offset += sz; return; } if (bsz == sz) { memcpy(buffer, current->buffer + db->offset, sz); db->offset = 0; _return_message(db, mp); return; } else { memcpy(buffer, current->buffer + db->offset, bsz); _return_message(db, mp); db->offset = 0; buffer+=bsz; sz-=bsz; } } } static void databuffer_push(struct databuffer *db, struct messagepool *mp, void *data, int sz) { struct message * m; if (mp->freelist) { m = mp->freelist; mp->freelist = m->next; } else { struct messagepool_list * mpl = skynet_malloc(sizeof(*mpl)); struct message * temp = mpl->pool; int i; for (i=1;inext = mp->pool; mp->pool = mpl; m = &temp[0]; mp->freelist = &temp[1]; } m->buffer = data; m->size = sz; m->next = NULL; db->size += sz; if (db->head == NULL) { assert(db->tail == NULL); db->head = db->tail = m; } else { db->tail->next = m; db->tail = m; } } static int databuffer_readheader(struct databuffer *db, struct messagepool *mp, int header_size) { if (db->header == 0) { // parser header (2 or 4) if (db->size < header_size) { return -1; } uint8_t plen[4]; databuffer_read(db,mp,(char *)plen,header_size); // big-endian if (header_size == 2) { db->header = plen[0] << 8 | plen[1]; } else { db->header = plen[0] << 24 | plen[1] << 16 | plen[2] << 8 | plen[3]; } } if (db->size < db->header) return -1; return db->header; } static inline void databuffer_reset(struct databuffer *db) { db->header = 0; } static void databuffer_clear(struct databuffer *db, struct messagepool *mp) { while (db->head) { _return_message(db,mp); } memset(db, 0, sizeof(*db)); } #endif ================================================ FILE: service-src/hashid.h ================================================ #ifndef skynet_hashid_h #define skynet_hashid_h #include #include #include struct hashid_node { int id; struct hashid_node *next; }; struct hashid { int hashmod; int cap; int count; struct hashid_node *id; struct hashid_node **hash; }; static void hashid_init(struct hashid *hi, int max) { int i; int hashcap; hashcap = 16; while (hashcap < max) { hashcap *= 2; } hi->hashmod = hashcap - 1; hi->cap = max; hi->count = 0; hi->id = skynet_malloc(max * sizeof(struct hashid_node)); for (i=0;iid[i].id = -1; hi->id[i].next = NULL; } hi->hash = skynet_malloc(hashcap * sizeof(struct hashid_node *)); memset(hi->hash, 0, hashcap * sizeof(struct hashid_node *)); } static void hashid_clear(struct hashid *hi) { skynet_free(hi->id); skynet_free(hi->hash); hi->id = NULL; hi->hash = NULL; hi->hashmod = 1; hi->cap = 0; hi->count = 0; } static int hashid_lookup(struct hashid *hi, int id) { int h = id & hi->hashmod; struct hashid_node * c = hi->hash[h]; while(c) { if (c->id == id) return c - hi->id; c = c->next; } return -1; } static int hashid_remove(struct hashid *hi, int id) { int h = id & hi->hashmod; struct hashid_node * c = hi->hash[h]; if (c == NULL) return -1; if (c->id == id) { hi->hash[h] = c->next; goto _clear; } while(c->next) { if (c->next->id == id) { struct hashid_node * temp = c->next; c->next = temp->next; c = temp; goto _clear; } c = c->next; } return -1; _clear: c->id = -1; c->next = NULL; --hi->count; return c - hi->id; } static int hashid_insert(struct hashid * hi, int id) { struct hashid_node *c = NULL; int i; for (i=0;icap;i++) { int index = (i+id) % hi->cap; if (hi->id[index].id == -1) { c = &hi->id[index]; break; } } assert(c); ++hi->count; c->id = id; assert(c->next == NULL); int h = id & hi->hashmod; if (hi->hash[h]) { c->next = hi->hash[h]; } hi->hash[h] = c; return c - hi->id; } static inline int hashid_full(struct hashid *hi) { return hi->count == hi->cap; } #endif ================================================ FILE: service-src/service_gate.c ================================================ #include "skynet.h" #include "skynet_socket.h" #include "databuffer.h" #include "hashid.h" #include #include #include #include #include #include #define BACKLOG 128 struct connection { int id; // skynet_socket id uint32_t agent; uint32_t client; char remote_name[32]; struct databuffer buffer; }; struct gate { struct skynet_context *ctx; int listen_id; uint32_t watchdog; uint32_t broker; int client_tag; int header_size; int max_connection; struct hashid hash; struct connection *conn; // todo: save message pool ptr for release struct messagepool mp; }; struct gate * gate_create(void) { struct gate * g = skynet_malloc(sizeof(*g)); memset(g,0,sizeof(*g)); g->listen_id = -1; return g; } void gate_release(struct gate *g) { int i; struct skynet_context *ctx = g->ctx; for (i=0;imax_connection;i++) { struct connection *c = &g->conn[i]; if (c->id >=0) { skynet_socket_close(ctx, c->id); } } if (g->listen_id >= 0) { skynet_socket_close(ctx, g->listen_id); } messagepool_free(&g->mp); hashid_clear(&g->hash); skynet_free(g->conn); skynet_free(g); } static void _parm(char *msg, int sz, int command_sz) { while (command_sz < sz) { if (msg[command_sz] != ' ') break; ++command_sz; } int i; for (i=command_sz;ihash, fd); if (id >=0) { struct connection * agent = &g->conn[id]; agent->agent = agentaddr; agent->client = clientaddr; } } static void _ctrl(struct gate * g, const void * msg, int sz) { struct skynet_context * ctx = g->ctx; char tmp[sz+1]; memcpy(tmp, msg, sz); tmp[sz] = '\0'; char * command = tmp; int i; if (sz == 0) return; for (i=0;ihash, uid); if (id>=0) { skynet_socket_close(ctx, uid); } return; } if (memcmp(command,"forward",i)==0) { _parm(tmp, sz, i); char * client = tmp; char * idstr = strsep(&client, " "); if (client == NULL) { return; } int id = strtol(idstr , NULL, 10); char * agent = strsep(&client, " "); if (client == NULL) { return; } uint32_t agent_handle = strtoul(agent+1, NULL, 16); uint32_t client_handle = strtoul(client+1, NULL, 16); _forward_agent(g, id, agent_handle, client_handle); return; } if (memcmp(command,"broker",i)==0) { _parm(tmp, sz, i); g->broker = skynet_queryname(ctx, command); return; } if (memcmp(command,"start",i) == 0) { _parm(tmp, sz, i); int uid = strtol(command , NULL, 10); int id = hashid_lookup(&g->hash, uid); if (id>=0) { skynet_socket_start(ctx, uid); } return; } if (memcmp(command, "close", i) == 0) { if (g->listen_id >= 0) { skynet_socket_close(ctx, g->listen_id); g->listen_id = -1; } return; } skynet_error(ctx, "[gate] Unknown command : %s", command); } static void _report(struct gate * g, const char * data, ...) { if (g->watchdog == 0) { return; } struct skynet_context * ctx = g->ctx; va_list ap; va_start(ap, data); char tmp[1024]; int n = vsnprintf(tmp, sizeof(tmp), data, ap); va_end(ap); skynet_send(ctx, 0, g->watchdog, PTYPE_TEXT, 0, tmp, n); } static void _forward(struct gate *g, struct connection * c, int size) { struct skynet_context * ctx = g->ctx; int fd = c->id; if (fd <= 0) { // socket error return; } if (g->broker) { void * temp = skynet_malloc(size); databuffer_read(&c->buffer,&g->mp,(char *)temp, size); skynet_send(ctx, 0, g->broker, g->client_tag | PTYPE_TAG_DONTCOPY, fd, temp, size); return; } if (c->agent) { void * temp = skynet_malloc(size); databuffer_read(&c->buffer,&g->mp,(char *)temp, size); skynet_send(ctx, c->client, c->agent, g->client_tag | PTYPE_TAG_DONTCOPY, fd , temp, size); } else if (g->watchdog) { char * tmp = skynet_malloc(size + 32); int n = snprintf(tmp,32,"%d data ",c->id); databuffer_read(&c->buffer,&g->mp,tmp+n,size); skynet_send(ctx, 0, g->watchdog, PTYPE_TEXT | PTYPE_TAG_DONTCOPY, fd, tmp, size + n); } } static void dispatch_message(struct gate *g, struct connection *c, int id, void * data, int sz) { databuffer_push(&c->buffer,&g->mp, data, sz); for (;;) { int size = databuffer_readheader(&c->buffer, &g->mp, g->header_size); if (size < 0) { return; } else if (size > 0) { if (size >= 0x1000000) { struct skynet_context * ctx = g->ctx; databuffer_clear(&c->buffer,&g->mp); skynet_socket_close(ctx, id); skynet_error(ctx, "Recv socket message > 16M"); return; } else { _forward(g, c, size); databuffer_reset(&c->buffer); } } } } static void dispatch_socket_message(struct gate *g, const struct skynet_socket_message * message, int sz) { struct skynet_context * ctx = g->ctx; switch(message->type) { case SKYNET_SOCKET_TYPE_DATA: { int id = hashid_lookup(&g->hash, message->id); if (id>=0) { struct connection *c = &g->conn[id]; dispatch_message(g, c, message->id, message->buffer, message->ud); } else { skynet_error(ctx, "Drop unknown connection %d message", message->id); skynet_socket_close(ctx, message->id); skynet_free(message->buffer); } break; } case SKYNET_SOCKET_TYPE_CONNECT: { if (message->id == g->listen_id) { // start listening break; } int id = hashid_lookup(&g->hash, message->id); if (id<0) { skynet_error(ctx, "Close unknown connection %d", message->id); skynet_socket_close(ctx, message->id); } break; } case SKYNET_SOCKET_TYPE_CLOSE: case SKYNET_SOCKET_TYPE_ERROR: { int id = hashid_remove(&g->hash, message->id); if (id>=0) { struct connection *c = &g->conn[id]; databuffer_clear(&c->buffer,&g->mp); memset(c, 0, sizeof(*c)); c->id = -1; _report(g, "%d close", message->id); skynet_socket_close(ctx, message->id); } break; } case SKYNET_SOCKET_TYPE_ACCEPT: // report accept, then it will be get a SKYNET_SOCKET_TYPE_CONNECT message assert(g->listen_id == message->id); if (hashid_full(&g->hash)) { skynet_socket_close(ctx, message->ud); } else { struct connection *c = &g->conn[hashid_insert(&g->hash, message->ud)]; if (sz >= sizeof(c->remote_name)) { sz = sizeof(c->remote_name) - 1; } c->id = message->ud; memcpy(c->remote_name, message+1, sz); c->remote_name[sz] = '\0'; _report(g, "%d open %d %s:0",c->id, c->id, c->remote_name); skynet_error(ctx, "socket open: %x", c->id); } break; case SKYNET_SOCKET_TYPE_WARNING: skynet_error(ctx, "fd (%d) send buffer (%d)K", message->id, message->ud); break; } } static int _cb(struct skynet_context * ctx, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { struct gate *g = ud; switch(type) { case PTYPE_TEXT: _ctrl(g , msg , (int)sz); break; case PTYPE_CLIENT: { if (sz <=4 ) { skynet_error(ctx, "Invalid client message from %x",source); break; } // The last 4 bytes in msg are the id of socket, write following bytes to it const uint8_t * idbuf = msg + sz - 4; uint32_t uid = idbuf[0] | idbuf[1] << 8 | idbuf[2] << 16 | idbuf[3] << 24; int id = hashid_lookup(&g->hash, uid); if (id>=0) { // don't send id (last 4 bytes) skynet_socket_send(ctx, uid, (void*)msg, sz-4); // return 1 means don't free msg return 1; } else { skynet_error(ctx, "Invalid client id %d from %x",(int)uid,source); break; } } case PTYPE_SOCKET: // recv socket message from skynet_socket dispatch_socket_message(g, msg, (int)(sz-sizeof(struct skynet_socket_message))); break; } return 0; } static int start_listen(struct gate *g, char * listen_addr) { struct skynet_context * ctx = g->ctx; char * portstr = strrchr(listen_addr,':'); const char * host = ""; int port; if (portstr == NULL) { port = strtol(listen_addr, NULL, 10); if (port <= 0) { skynet_error(ctx, "Invalid gate address %s",listen_addr); return 1; } } else { port = strtol(portstr + 1, NULL, 10); if (port <= 0) { skynet_error(ctx, "Invalid gate address %s",listen_addr); return 1; } portstr[0] = '\0'; host = listen_addr; } g->listen_id = skynet_socket_listen(ctx, host, port, BACKLOG); if (g->listen_id < 0) { return 1; } skynet_socket_start(ctx, g->listen_id); return 0; } int gate_init(struct gate *g , struct skynet_context * ctx, char * parm) { if (parm == NULL) return 1; int max = 0; int sz = strlen(parm)+1; char watchdog[sz]; char binding[sz]; int client_tag = 0; char header; int n = sscanf(parm, "%c %s %s %d %d", &header, watchdog, binding, &client_tag, &max); if (n<4) { skynet_error(ctx, "Invalid gate parm %s",parm); return 1; } if (max <=0 ) { skynet_error(ctx, "Need max connection"); return 1; } if (header != 'S' && header !='L') { skynet_error(ctx, "Invalid data header style"); return 1; } if (client_tag == 0) { client_tag = PTYPE_CLIENT; } if (watchdog[0] == '!') { g->watchdog = 0; } else { g->watchdog = skynet_queryname(ctx, watchdog); if (g->watchdog == 0) { skynet_error(ctx, "Invalid watchdog %s",watchdog); return 1; } } g->ctx = ctx; hashid_init(&g->hash, max); g->conn = skynet_malloc(max * sizeof(struct connection)); memset(g->conn, 0, max *sizeof(struct connection)); g->max_connection = max; int i; for (i=0;iconn[i].id = -1; } g->client_tag = client_tag; g->header_size = header=='S' ? 2 : 4; skynet_callback(ctx,g,_cb); return start_listen(g,binding); } ================================================ FILE: service-src/service_harbor.c ================================================ #include "skynet.h" #include "skynet_harbor.h" #include "skynet_socket.h" #include "skynet_handle.h" /* harbor listen the PTYPE_HARBOR (in text) N name : update the global name S fd id: connect to new harbor , we should send self_id to fd first , and then recv a id (check it), and at last send queue. A fd id: accept new harbor , we should send self_id to fd , and then send queue. If the fd is disconnected, send message to slave in PTYPE_TEXT. D id If we don't known a globalname, send message to slave in PTYPE_TEXT. Q name */ #include #include #include #include #include #include #include #define HASH_SIZE 4096 #define DEFAULT_QUEUE_SIZE 1024 // 12 is sizeof(struct remote_message_header) #define HEADER_COOKIE_LENGTH 12 /* message type (8bits) is in destination high 8bits harbor id (8bits) is also in that place , but remote message doesn't need harbor id. */ struct remote_message_header { uint32_t source; uint32_t destination; uint32_t session; }; struct harbor_msg { struct remote_message_header header; void * buffer; size_t size; }; struct harbor_msg_queue { int size; int head; int tail; struct harbor_msg * data; }; struct keyvalue { struct keyvalue * next; char key[GLOBALNAME_LENGTH]; uint32_t hash; uint32_t value; struct harbor_msg_queue * queue; }; struct hashmap { struct keyvalue *node[HASH_SIZE]; }; #define STATUS_WAIT 0 #define STATUS_HANDSHAKE 1 #define STATUS_HEADER 2 #define STATUS_CONTENT 3 #define STATUS_DOWN 4 struct slave { int fd; struct harbor_msg_queue *queue; int status; int length; int read; uint8_t size[4]; char * recv_buffer; }; struct harbor { struct skynet_context *ctx; int id; uint32_t slave; struct hashmap * map; struct slave s[REMOTE_MAX]; }; // hash table static void push_queue_msg(struct harbor_msg_queue * queue, struct harbor_msg * m) { // If there is only 1 free slot which is reserved to distinguish full/empty // of circular buffer, expand it. if (((queue->tail + 1) % queue->size) == queue->head) { struct harbor_msg * new_buffer = skynet_malloc(queue->size * 2 * sizeof(struct harbor_msg)); int i; for (i=0;isize-1;i++) { new_buffer[i] = queue->data[(i+queue->head) % queue->size]; } skynet_free(queue->data); queue->data = new_buffer; queue->head = 0; queue->tail = queue->size - 1; queue->size *= 2; } struct harbor_msg * slot = &queue->data[queue->tail]; *slot = *m; queue->tail = (queue->tail + 1) % queue->size; } static void push_queue(struct harbor_msg_queue * queue, void * buffer, size_t sz, struct remote_message_header * header) { struct harbor_msg m; m.header = *header; m.buffer = buffer; m.size = sz; push_queue_msg(queue, &m); } static struct harbor_msg * pop_queue(struct harbor_msg_queue * queue) { if (queue->head == queue->tail) { return NULL; } struct harbor_msg * slot = &queue->data[queue->head]; queue->head = (queue->head + 1) % queue->size; return slot; } static struct harbor_msg_queue * new_queue() { struct harbor_msg_queue * queue = skynet_malloc(sizeof(*queue)); queue->size = DEFAULT_QUEUE_SIZE; queue->head = 0; queue->tail = 0; queue->data = skynet_malloc(DEFAULT_QUEUE_SIZE * sizeof(struct harbor_msg)); return queue; } static void release_queue(struct harbor_msg_queue *queue) { if (queue == NULL) return; struct harbor_msg * m; while ((m=pop_queue(queue)) != NULL) { skynet_free(m->buffer); } skynet_free(queue->data); skynet_free(queue); } static struct keyvalue * hash_search(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { uint32_t *ptr = (uint32_t*) name; uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; struct keyvalue * node = hash->node[h % HASH_SIZE]; while (node) { if (node->hash == h && strncmp(node->key, name, GLOBALNAME_LENGTH) == 0) { return node; } node = node->next; } return NULL; } /* // Don't support erase name yet static struct void hash_erase(struct hashmap * hash, char name[GLOBALNAME_LENGTH) { uint32_t *ptr = name; uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; struct keyvalue ** ptr = &hash->node[h % HASH_SIZE]; while (*ptr) { struct keyvalue * node = *ptr; if (node->hash == h && strncmp(node->key, name, GLOBALNAME_LENGTH) == 0) { _release_queue(node->queue); *ptr->next = node->next; skynet_free(node); return; } *ptr = &(node->next); } } */ static struct keyvalue * hash_insert(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) { uint32_t *ptr = (uint32_t *)name; uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]; struct keyvalue ** pkv = &hash->node[h % HASH_SIZE]; struct keyvalue * node = skynet_malloc(sizeof(*node)); memcpy(node->key, name, GLOBALNAME_LENGTH); node->next = *pkv; node->queue = NULL; node->hash = h; node->value = 0; *pkv = node; return node; } static struct hashmap * hash_new() { struct hashmap * h = skynet_malloc(sizeof(struct hashmap)); memset(h,0,sizeof(*h)); return h; } static void hash_delete(struct hashmap *hash) { int i; for (i=0;inode[i]; while (node) { struct keyvalue * next = node->next; release_queue(node->queue); skynet_free(node); node = next; } } skynet_free(hash); } /////////////// static void close_harbor(struct harbor *h, int id) { struct slave *s = &h->s[id]; s->status = STATUS_DOWN; if (s->fd) { skynet_socket_close(h->ctx, s->fd); s->fd = 0; } if (s->queue) { release_queue(s->queue); s->queue = NULL; } } static void report_harbor_down(struct harbor *h, int id) { char down[64]; int n = sprintf(down, "D %d",id); skynet_send(h->ctx, 0, h->slave, PTYPE_TEXT, 0, down, n); } struct harbor * harbor_create(void) { struct harbor * h = skynet_malloc(sizeof(*h)); memset(h,0,sizeof(*h)); h->map = hash_new(); return h; } static void close_all_remotes(struct harbor *h) { int i; for (i=1;imap); skynet_free(h); } static inline void to_bigendian(uint8_t *buffer, uint32_t n) { buffer[0] = (n >> 24) & 0xff; buffer[1] = (n >> 16) & 0xff; buffer[2] = (n >> 8) & 0xff; buffer[3] = n & 0xff; } static inline void header_to_message(const struct remote_message_header * header, uint8_t * message) { to_bigendian(message , header->source); to_bigendian(message+4 , header->destination); to_bigendian(message+8 , header->session); } static inline uint32_t from_bigendian(uint32_t n) { union { uint32_t big; uint8_t bytes[4]; } u; u.big = n; return u.bytes[0] << 24 | u.bytes[1] << 16 | u.bytes[2] << 8 | u.bytes[3]; } static inline void message_to_header(const uint32_t *message, struct remote_message_header *header) { header->source = from_bigendian(message[0]); header->destination = from_bigendian(message[1]); header->session = from_bigendian(message[2]); } // socket package static void forward_local_messsage(struct harbor *h, void *msg, int sz) { const char * cookie = msg; cookie += sz - HEADER_COOKIE_LENGTH; struct remote_message_header header; message_to_header((const uint32_t *)cookie, &header); uint32_t destination = header.destination; int type = destination >> HANDLE_REMOTE_SHIFT; destination = (destination & HANDLE_MASK) | ((uint32_t)h->id << HANDLE_REMOTE_SHIFT); if (skynet_send(h->ctx, header.source, destination, type | PTYPE_TAG_DONTCOPY , (int)header.session, (void *)msg, sz-HEADER_COOKIE_LENGTH) < 0) { if (type != PTYPE_ERROR) { // don't need report error when type is error skynet_send(h->ctx, destination, header.source , PTYPE_ERROR, (int)header.session, NULL, 0); } skynet_error(h->ctx, "Unknown destination :%x from :%x type(%d)", destination, header.source, type); } } static void send_remote(struct skynet_context * ctx, int fd, const char * buffer, size_t sz, struct remote_message_header * cookie) { size_t sz_header = sz+sizeof(*cookie); if (sz_header > UINT32_MAX) { skynet_error(ctx, "remote message from :%08x to :%08x is too large.", cookie->source, cookie->destination); return; } uint8_t sendbuf[sz_header+4]; to_bigendian(sendbuf, (uint32_t)sz_header); memcpy(sendbuf+4, buffer, sz); header_to_message(cookie, sendbuf+4+sz); struct socket_sendbuffer tmp; tmp.id = fd; tmp.type = SOCKET_BUFFER_RAWPOINTER; tmp.buffer = sendbuf; tmp.sz = sz_header+4; // ignore send error, because if the connection is broken, the mainloop will recv a message. skynet_socket_sendbuffer(ctx, &tmp); } static void dispatch_name_queue(struct harbor *h, struct keyvalue * node) { struct harbor_msg_queue * queue = node->queue; uint32_t handle = node->value; int harbor_id = handle >> HANDLE_REMOTE_SHIFT; struct skynet_context * context = h->ctx; struct slave *s = &h->s[harbor_id]; int fd = s->fd; if (fd == 0) { if (s->status == STATUS_DOWN) { char tmp [GLOBALNAME_LENGTH+1]; memcpy(tmp, node->key, GLOBALNAME_LENGTH); tmp[GLOBALNAME_LENGTH] = '\0'; skynet_error(context, "Drop message to %s (in harbor %d)",tmp,harbor_id); } else { if (s->queue == NULL) { s->queue = node->queue; node->queue = NULL; } else { struct harbor_msg * m; while ((m = pop_queue(queue))!=NULL) { push_queue_msg(s->queue, m); } } if (harbor_id == (h->slave >> HANDLE_REMOTE_SHIFT)) { // the harbor_id is local struct harbor_msg * m; while ((m = pop_queue(s->queue)) != NULL) { int type = m->header.destination >> HANDLE_REMOTE_SHIFT; skynet_send(context, m->header.source, handle , type | PTYPE_TAG_DONTCOPY, m->header.session, m->buffer, m->size); } release_queue(s->queue); s->queue = NULL; } } return; } struct harbor_msg * m; while ((m = pop_queue(queue)) != NULL) { m->header.destination |= (handle & HANDLE_MASK); send_remote(context, fd, m->buffer, m->size, &m->header); skynet_free(m->buffer); } } static void dispatch_queue(struct harbor *h, int id) { struct slave *s = &h->s[id]; int fd = s->fd; assert(fd != 0); struct harbor_msg_queue *queue = s->queue; if (queue == NULL) return; struct harbor_msg * m; while ((m = pop_queue(queue)) != NULL) { send_remote(h->ctx, fd, m->buffer, m->size, &m->header); skynet_free(m->buffer); } release_queue(queue); s->queue = NULL; } static void push_socket_data(struct harbor *h, const struct skynet_socket_message * message) { assert(message->type == SKYNET_SOCKET_TYPE_DATA); int fd = message->id; int i; int id = 0; struct slave * s = NULL; for (i=1;is[i].fd == fd) { s = &h->s[i]; id = i; break; } } if (s == NULL) { skynet_error(h->ctx, "Invalid socket fd (%d) data", fd); return; } uint8_t * buffer = (uint8_t *)message->buffer; int size = message->ud; for (;;) { switch(s->status) { case STATUS_HANDSHAKE: { // check id uint8_t remote_id = buffer[0]; if (remote_id != id) { skynet_error(h->ctx, "Invalid shakehand id (%d) from fd = %d , harbor = %d", id, fd, remote_id); close_harbor(h,id); return; } ++buffer; --size; s->status = STATUS_HEADER; dispatch_queue(h, id); if (size == 0) { break; } // go though } case STATUS_HEADER: { // big endian 4 bytes length, the first one must be 0. int need = 4 - s->read; if (size < need) { memcpy(s->size + s->read, buffer, size); s->read += size; return; } else { memcpy(s->size + s->read, buffer, need); buffer += need; size -= need; if (s->size[0] != 0) { skynet_error(h->ctx, "Message is too long from harbor %d", id); close_harbor(h,id); return; } s->length = s->size[1] << 16 | s->size[2] << 8 | s->size[3]; s->read = 0; s->recv_buffer = skynet_malloc(s->length); s->status = STATUS_CONTENT; if (size == 0) { return; } } } // go though case STATUS_CONTENT: { int need = s->length - s->read; if (size < need) { memcpy(s->recv_buffer + s->read, buffer, size); s->read += size; return; } memcpy(s->recv_buffer + s->read, buffer, need); forward_local_messsage(h, s->recv_buffer, s->length); s->length = 0; s->read = 0; s->recv_buffer = NULL; size -= need; buffer += need; s->status = STATUS_HEADER; if (size == 0) return; break; } default: return; } } } static void update_name(struct harbor *h, const char name[GLOBALNAME_LENGTH], uint32_t handle) { struct keyvalue * node = hash_search(h->map, name); if (node == NULL) { node = hash_insert(h->map, name); } node->value = handle; if (node->queue) { dispatch_name_queue(h, node); release_queue(node->queue); node->queue = NULL; } } static int remote_send_handle(struct harbor *h, uint32_t source, uint32_t destination, int type, int session, const char * msg, size_t sz) { int harbor_id = destination >> HANDLE_REMOTE_SHIFT; struct skynet_context * context = h->ctx; if (harbor_id == h->id) { // local message skynet_send(context, source, destination , type | PTYPE_TAG_DONTCOPY, session, (void *)msg, sz); return 1; } struct slave * s = &h->s[harbor_id]; if (s->fd == 0 || s->status == STATUS_HANDSHAKE) { if (s->status == STATUS_DOWN) { // throw an error return to source // report the destination is dead skynet_send(context, destination, source, PTYPE_ERROR, session, NULL, 0); skynet_error(context, "Drop message to harbor %d from %x to %x (session = %d, msgsz = %d)",harbor_id, source, destination,session,(int)sz); } else { if (s->queue == NULL) { s->queue = new_queue(); } struct remote_message_header header; header.source = source; header.destination = (type << HANDLE_REMOTE_SHIFT) | (destination & HANDLE_MASK); header.session = (uint32_t)session; push_queue(s->queue, (void *)msg, sz, &header); return 1; } } else { struct remote_message_header cookie; cookie.source = source; cookie.destination = (destination & HANDLE_MASK) | ((uint32_t)type << HANDLE_REMOTE_SHIFT); cookie.session = (uint32_t)session; send_remote(context, s->fd, msg,sz,&cookie); } return 0; } static int remote_send_name(struct harbor *h, uint32_t source, const char name[GLOBALNAME_LENGTH], int type, int session, const char * msg, size_t sz) { struct keyvalue * node = hash_search(h->map, name); if (node == NULL) { node = hash_insert(h->map, name); } if (node->value == 0) { if (node->queue == NULL) { node->queue = new_queue(); } struct remote_message_header header; header.source = source; header.destination = type << HANDLE_REMOTE_SHIFT; header.session = (uint32_t)session; push_queue(node->queue, (void *)msg, sz, &header); char query[2+GLOBALNAME_LENGTH+1] = "Q "; query[2+GLOBALNAME_LENGTH] = 0; memcpy(query+2, name, GLOBALNAME_LENGTH); skynet_send(h->ctx, 0, h->slave, PTYPE_TEXT, 0, query, strlen(query)); return 1; } else { return remote_send_handle(h, source, node->value, type, session, msg, sz); } } static void handshake(struct harbor *h, int id) { struct slave *s = &h->s[id]; uint8_t handshake[1] = { (uint8_t)h->id }; struct socket_sendbuffer tmp; tmp.id = s->fd; tmp.type = SOCKET_BUFFER_RAWPOINTER; tmp.buffer = handshake; tmp.sz = 1; skynet_socket_sendbuffer(h->ctx, &tmp); } static void harbor_command(struct harbor * h, const char * msg, size_t sz, int session, uint32_t source) { const char * name = msg + 2; int s = (int)sz; s -= 2; switch(msg[0]) { case 'N' : { if (s <=0 || s>= GLOBALNAME_LENGTH) { skynet_error(h->ctx, "Invalid global name %s", name); return; } struct remote_name rn; memset(&rn, 0, sizeof(rn)); memcpy(rn.name, name, s); rn.handle = source; update_name(h, rn.name, rn.handle); break; } case 'S' : case 'A' : { char buffer[s+1]; memcpy(buffer, name, s); buffer[s] = 0; int fd=0, id=0; sscanf(buffer, "%d %d",&fd,&id); if (fd == 0 || id <= 0 || id>=REMOTE_MAX) { skynet_error(h->ctx, "Invalid command %c %s", msg[0], buffer); return; } struct slave * slave = &h->s[id]; if (slave->fd != 0) { skynet_error(h->ctx, "Harbor %d alreay exist", id); return; } slave->fd = fd; skynet_socket_start(h->ctx, fd); handshake(h, id); if (msg[0] == 'S') { slave->status = STATUS_HANDSHAKE; } else { slave->status = STATUS_HEADER; dispatch_queue(h,id); } break; } default: skynet_error(h->ctx, "Unknown command %s", msg); return; } } static int harbor_id(struct harbor *h, int fd) { int i; for (i=1;is[i]; if (s->fd == fd) { return i; } } return 0; } static int mainloop(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { struct harbor * h = ud; switch (type) { case PTYPE_SOCKET: { const struct skynet_socket_message * message = msg; switch(message->type) { case SKYNET_SOCKET_TYPE_DATA: push_socket_data(h, message); skynet_free(message->buffer); break; case SKYNET_SOCKET_TYPE_ERROR: case SKYNET_SOCKET_TYPE_CLOSE: { int id = harbor_id(h, message->id); if (id) { report_harbor_down(h,id); } else { skynet_error(context, "Unknown fd (%d) closed", message->id); } break; } case SKYNET_SOCKET_TYPE_CONNECT: // fd forward to this service break; case SKYNET_SOCKET_TYPE_WARNING: { int id = harbor_id(h, message->id); if (id) { skynet_error(context, "message havn't send to Harbor (%d) reach %d K", id, message->ud); } break; } default: skynet_error(context, "recv invalid socket message type %d", type); break; } return 0; } case PTYPE_HARBOR: { harbor_command(h, msg,sz,session,source); return 0; } case PTYPE_SYSTEM : { // remote message out const struct remote_message *rmsg = msg; if (rmsg->destination.handle == 0) { if (remote_send_name(h, source , rmsg->destination.name, rmsg->type, session, rmsg->message, rmsg->sz)) { return 0; } } else { if (remote_send_handle(h, source , rmsg->destination.handle, rmsg->type, session, rmsg->message, rmsg->sz)) { return 0; } } skynet_free((void *)rmsg->message); return 0; } default: skynet_error(context, "recv invalid message from %x, type = %d", source, type); if (session != 0 && type != PTYPE_ERROR) { skynet_send(context,0,source,PTYPE_ERROR, session, NULL, 0); } return 0; } } int harbor_init(struct harbor *h, struct skynet_context *ctx, const char * args) { h->ctx = ctx; int harbor_id = 0; uint32_t slave = 0; sscanf(args,"%d %u", &harbor_id, &slave); if (slave == 0) { return 1; } h->id = harbor_id; h->slave = slave; if (harbor_id == 0) { close_all_remotes(h); } skynet_callback(ctx, h, mainloop); skynet_harbor_start(ctx); return 0; } ================================================ FILE: service-src/service_logger.c ================================================ #include "skynet.h" #include #include #include #include #include struct logger { FILE * handle; char * filename; uint32_t starttime; int close; }; struct logger * logger_create(void) { struct logger * inst = skynet_malloc(sizeof(*inst)); inst->handle = NULL; inst->close = 0; inst->filename = NULL; return inst; } void logger_release(struct logger * inst) { if (inst->close) { fclose(inst->handle); } skynet_free(inst->filename); skynet_free(inst); } #define SIZETIMEFMT 250 static int timestring(struct logger *inst, char tmp[SIZETIMEFMT]) { uint64_t now = skynet_now(); time_t ti = now/100 + inst->starttime; struct tm info; (void)localtime_r(&ti,&info); strftime(tmp, SIZETIMEFMT, "%d/%m/%y %H:%M:%S", &info); return now % 100; } static int logger_cb(struct skynet_context * context, void *ud, int type, int session, uint32_t source, const void * msg, size_t sz) { struct logger * inst = ud; switch (type) { case PTYPE_SYSTEM: if (inst->filename) { inst->handle = freopen(inst->filename, "a", inst->handle); } break; case PTYPE_TEXT: if (inst->filename) { char tmp[SIZETIMEFMT]; int csec = timestring(ud, tmp); fprintf(inst->handle, "%s.%02d ", tmp, csec); } fprintf(inst->handle, "[:%08x] ", source); fwrite(msg, sz , 1, inst->handle); fprintf(inst->handle, "\n"); fflush(inst->handle); break; } return 0; } int logger_init(struct logger * inst, struct skynet_context *ctx, const char * parm) { const char * r = skynet_command(ctx, "STARTTIME", NULL); inst->starttime = strtoul(r, NULL, 10); if (parm) { inst->handle = fopen(parm,"a"); if (inst->handle == NULL) { return 1; } inst->filename = skynet_malloc(strlen(parm)+1); strcpy(inst->filename, parm); inst->close = 1; } else { inst->handle = stdout; } if (inst->handle) { skynet_callback(ctx, inst, logger_cb); return 0; } return 1; } ================================================ FILE: service-src/service_snlua.c ================================================ #include "skynet.h" #include "atomic.h" #include #include #include #include #include #include #include #include #if defined(__APPLE__) #include #include #endif #define NANOSEC 1000000000 #define MICROSEC 1000000 // #define DEBUG_LOG #define MEMORY_WARNING_REPORT (1024 * 1024 * 32) struct snlua { lua_State * L; struct skynet_context * ctx; size_t mem; size_t mem_report; size_t mem_limit; lua_State * activeL; ATOM_INT trap; }; // LUA_CACHELIB may defined in patched lua for shared proto #ifdef LUA_CACHELIB #define codecache luaopen_cache #else static int cleardummy(lua_State *L) { return 0; } static int codecache(lua_State *L) { luaL_Reg l[] = { { "clear", cleardummy }, { "mode", cleardummy }, { NULL, NULL }, }; luaL_newlib(L,l); lua_getglobal(L, "loadfile"); lua_setfield(L, -2, "loadfile"); return 1; } #endif static void signal_hook(lua_State *L, lua_Debug *ar) { void *ud = NULL; lua_getallocf(L, &ud); struct snlua *l = (struct snlua *)ud; lua_sethook (L, NULL, 0, 0); if (ATOM_LOAD(&l->trap)) { ATOM_STORE(&l->trap , 0); luaL_error(L, "signal 0"); } } static void switchL(lua_State *L, struct snlua *l) { l->activeL = L; if (ATOM_LOAD(&l->trap)) { lua_sethook(L, signal_hook, LUA_MASKCOUNT, 1); } } static int lua_resumeX(lua_State *L, lua_State *from, int nargs, int *nresults) { void *ud = NULL; lua_getallocf(L, &ud); struct snlua *l = (struct snlua *)ud; switchL(L, l); int err = lua_resume(L, from, nargs, nresults); if (ATOM_LOAD(&l->trap)) { // wait for lua_sethook. (l->trap == -1) while (ATOM_LOAD(&l->trap) >= 0) ; } switchL(from, l); return err; } static double get_time() { #if !defined(__APPLE__) struct timespec ti; clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ti); int sec = ti.tv_sec & 0xffff; int nsec = ti.tv_nsec; return (double)sec + (double)nsec / NANOSEC; #else struct task_thread_times_info aTaskInfo; mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT; if (KERN_SUCCESS != task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t )&aTaskInfo, &aTaskInfoCount)) { return 0; } int sec = aTaskInfo.user_time.seconds & 0xffff; int msec = aTaskInfo.user_time.microseconds; return (double)sec + (double)msec / MICROSEC; #endif } static inline double diff_time(double start) { double now = get_time(); if (now < start) { return now + 0x10000 - start; } else { return now - start; } } // coroutine lib, add profile /* ** Resumes a coroutine. Returns the number of results for non-error ** cases or -1 for errors. */ static int auxresume (lua_State *L, lua_State *co, int narg) { int status, nres; if (!lua_checkstack(co, narg)) { lua_pushliteral(L, "too many arguments to resume"); return -1; /* error flag */ } lua_xmove(L, co, narg); status = lua_resumeX(co, L, narg, &nres); if (status == LUA_OK || status == LUA_YIELD) { if (!lua_checkstack(L, nres + 1)) { lua_pop(co, nres); /* remove results anyway */ lua_pushliteral(L, "too many results to resume"); return -1; /* error flag */ } lua_xmove(co, L, nres); /* move yielded values */ return nres; } else { lua_xmove(co, L, 1); /* move error message */ return -1; /* error flag */ } } static int timing_enable(lua_State *L, int co_index, lua_Number *start_time) { lua_pushvalue(L, co_index); lua_rawget(L, lua_upvalueindex(1)); if (lua_isnil(L, -1)) { // check start time lua_pop(L, 1); return 0; } *start_time = lua_tonumber(L, -1); lua_pop(L,1); return 1; } static double timing_total(lua_State *L, int co_index) { lua_pushvalue(L, co_index); lua_rawget(L, lua_upvalueindex(2)); double total_time = lua_tonumber(L, -1); lua_pop(L,1); return total_time; } static int timing_resume(lua_State *L, int co_index, int n) { lua_State *co = lua_tothread(L, co_index); lua_Number start_time = 0; if (timing_enable(L, co_index, &start_time)) { start_time = get_time(); #ifdef DEBUG_LOG double ti = diff_time(start_time); fprintf(stderr, "PROFILE [%p] resume %lf\n", co, ti); #endif lua_pushvalue(L, co_index); lua_pushnumber(L, start_time); lua_rawset(L, lua_upvalueindex(1)); // set start time } int r = auxresume(L, co, n); if (timing_enable(L, co_index, &start_time)) { double total_time = timing_total(L, co_index); double diff = diff_time(start_time); total_time += diff; #ifdef DEBUG_LOG fprintf(stderr, "PROFILE [%p] yield (%lf/%lf)\n", co, diff, total_time); #endif lua_pushvalue(L, co_index); lua_pushnumber(L, total_time); lua_rawset(L, lua_upvalueindex(2)); } return r; } static int luaB_coresume (lua_State *L) { luaL_checktype(L, 1, LUA_TTHREAD); int r = timing_resume(L, 1, lua_gettop(L) - 1); if (r < 0) { lua_pushboolean(L, 0); lua_insert(L, -2); return 2; /* return false + error message */ } else { lua_pushboolean(L, 1); lua_insert(L, -(r + 1)); return r + 1; /* return true + 'resume' returns */ } } static int luaB_auxwrap (lua_State *L) { lua_State *co = lua_tothread(L, lua_upvalueindex(3)); int r = timing_resume(L, lua_upvalueindex(3), lua_gettop(L)); if (r < 0) { int stat = lua_status(co); if (stat != LUA_OK && stat != LUA_YIELD) lua_closethread(co, L); /* close variables in case of errors */ if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ luaL_where(L, 1); /* add extra info, if available */ lua_insert(L, -2); lua_concat(L, 2); } return lua_error(L); /* propagate error */ } return r; } static int luaB_cocreate (lua_State *L) { lua_State *NL; luaL_checktype(L, 1, LUA_TFUNCTION); NL = lua_newthread(L); lua_pushvalue(L, 1); /* move function to top */ lua_xmove(L, NL, 1); /* move function from L to NL */ return 1; } static int luaB_cowrap (lua_State *L) { lua_pushvalue(L, lua_upvalueindex(1)); lua_pushvalue(L, lua_upvalueindex(2)); luaB_cocreate(L); lua_pushcclosure(L, luaB_auxwrap, 3); return 1; } // profile lib static int lstart(lua_State *L) { if (lua_gettop(L) != 0) { lua_settop(L,1); luaL_checktype(L, 1, LUA_TTHREAD); } else { lua_pushthread(L); } lua_Number start_time = 0; if (timing_enable(L, 1, &start_time)) { return luaL_error(L, "Thread %p start profile more than once", lua_topointer(L, 1)); } // reset total time lua_pushvalue(L, 1); lua_pushnumber(L, 0); lua_rawset(L, lua_upvalueindex(2)); // set start time lua_pushvalue(L, 1); start_time = get_time(); #ifdef DEBUG_LOG fprintf(stderr, "PROFILE [%p] start\n", L); #endif lua_pushnumber(L, start_time); lua_rawset(L, lua_upvalueindex(1)); return 0; } static int lstop(lua_State *L) { if (lua_gettop(L) != 0) { lua_settop(L,1); luaL_checktype(L, 1, LUA_TTHREAD); } else { lua_pushthread(L); } lua_Number start_time = 0; if (!timing_enable(L, 1, &start_time)) { return luaL_error(L, "Call profile.start() before profile.stop()"); } double ti = diff_time(start_time); double total_time = timing_total(L,1); lua_pushvalue(L, 1); // push coroutine lua_pushnil(L); lua_rawset(L, lua_upvalueindex(1)); lua_pushvalue(L, 1); // push coroutine lua_pushnil(L); lua_rawset(L, lua_upvalueindex(2)); total_time += ti; lua_pushnumber(L, total_time); #ifdef DEBUG_LOG fprintf(stderr, "PROFILE [%p] stop (%lf/%lf)\n", lua_tothread(L,1), ti, total_time); #endif return 1; } static int init_profile(lua_State *L) { luaL_Reg l[] = { { "start", lstart }, { "stop", lstop }, { "resume", luaB_coresume }, { "wrap", luaB_cowrap }, { NULL, NULL }, }; luaL_newlibtable(L,l); lua_newtable(L); // table thread->start time lua_newtable(L); // table thread->total time lua_newtable(L); // weak table lua_pushliteral(L, "kv"); lua_setfield(L, -2, "__mode"); lua_pushvalue(L, -1); lua_setmetatable(L, -3); lua_setmetatable(L, -3); luaL_setfuncs(L,l,2); return 1; } /// end of coroutine static int traceback (lua_State *L) { const char *msg = lua_tostring(L, 1); if (msg) luaL_traceback(L, L, msg, 1); else { lua_pushliteral(L, "(no error message)"); } return 1; } static void report_launcher_error(struct skynet_context *ctx) { // sizeof "ERROR" == 5 skynet_sendname(ctx, 0, ".launcher", PTYPE_TEXT, 0, "ERROR", 5); } static const char * optstring(struct skynet_context *ctx, const char *key, const char * str) { const char * ret = skynet_command(ctx, "GETENV", key); if (ret == NULL) { return str; } return ret; } static int init_cb(struct snlua *l, struct skynet_context *ctx, const char * args, size_t sz) { lua_State *L = l->L; l->ctx = ctx; lua_gc(L, LUA_GCSTOP, 0); lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); luaL_openlibs(L); luaL_requiref(L, "skynet.profile", init_profile, 0); int profile_lib = lua_gettop(L); // replace coroutine.resume / coroutine.wrap lua_getglobal(L, "coroutine"); lua_getfield(L, profile_lib, "resume"); lua_setfield(L, -2, "resume"); lua_getfield(L, profile_lib, "wrap"); lua_setfield(L, -2, "wrap"); lua_settop(L, profile_lib-1); lua_pushlightuserdata(L, ctx); lua_setfield(L, LUA_REGISTRYINDEX, "skynet_context"); luaL_requiref(L, "skynet.codecache", codecache , 0); lua_pop(L,1); lua_gc(L, LUA_GCGEN, 0, 0); const char *path = optstring(ctx, "lua_path","./lualib/?.lua;./lualib/?/init.lua"); lua_pushstring(L, path); lua_setglobal(L, "LUA_PATH"); const char *cpath = optstring(ctx, "lua_cpath","./luaclib/?.so"); lua_pushstring(L, cpath); lua_setglobal(L, "LUA_CPATH"); const char *service = optstring(ctx, "luaservice", "./service/?.lua"); lua_pushstring(L, service); lua_setglobal(L, "LUA_SERVICE"); const char *preload = skynet_command(ctx, "GETENV", "preload"); lua_pushstring(L, preload); lua_setglobal(L, "LUA_PRELOAD"); lua_pushcfunction(L, traceback); assert(lua_gettop(L) == 1); const char * loader = optstring(ctx, "lualoader", "./lualib/loader.lua"); int r = luaL_loadfile(L,loader); if (r != LUA_OK) { skynet_error(ctx, "Can't load %s : %s", loader, lua_tostring(L, -1)); report_launcher_error(ctx); return 1; } lua_pushlstring(L, args, sz); r = lua_pcall(L,1,0,1); if (r != LUA_OK) { skynet_error(ctx, "lua loader error : %s", lua_tostring(L, -1)); report_launcher_error(ctx); return 1; } lua_settop(L,0); if (lua_getfield(L, LUA_REGISTRYINDEX, "memlimit") == LUA_TNUMBER) { size_t limit = lua_tointeger(L, -1); l->mem_limit = limit; skynet_error(ctx, "Set memory limit to %.2f M", (float)limit / (1024 * 1024)); lua_pushnil(L); lua_setfield(L, LUA_REGISTRYINDEX, "memlimit"); } lua_pop(L, 1); lua_gc(L, LUA_GCRESTART, 0); return 0; } static int launch_cb(struct skynet_context * context, void *ud, int type, int session, uint32_t source , const void * msg, size_t sz) { assert(type == 0 && session == 0); struct snlua *l = ud; skynet_callback(context, NULL, NULL); int err = init_cb(l, context, msg, sz); if (err) { skynet_command(context, "EXIT", NULL); } return 0; } int snlua_init(struct snlua *l, struct skynet_context *ctx, const char * args) { int sz = strlen(args); char * tmp = skynet_malloc(sz); memcpy(tmp, args, sz); skynet_callback(ctx, l , launch_cb); const char * self = skynet_command(ctx, "REG", NULL); uint32_t handle_id = strtoul(self+1, NULL, 16); // it must be first message skynet_send(ctx, 0, handle_id, PTYPE_TAG_DONTCOPY,0, tmp, sz); return 0; } static void * lalloc(void * ud, void *ptr, size_t osize, size_t nsize) { struct snlua *l = ud; size_t mem = l->mem; l->mem += nsize; if (ptr) l->mem -= osize; if (l->mem_limit != 0 && l->mem > l->mem_limit) { if (ptr == NULL || nsize > osize) { l->mem = mem; return NULL; } } if (l->mem > l->mem_report) { l->mem_report *= 2; skynet_error(l->ctx, "Memory warning %.2f M", (float)l->mem / (1024 * 1024)); } return skynet_lalloc(ptr, osize, nsize); } static unsigned global_seed() { static ATOM_INT seed = 0; unsigned ret = ATOM_LOAD(&seed); while (ret == 0) { unsigned t = luaL_makeseed(NULL); if (t == 0) t = 1; ATOM_CAS(&seed, 0, t); ret = ATOM_LOAD(&seed); } return ret; } struct snlua * snlua_create(void) { struct snlua * l = skynet_malloc(sizeof(*l)); memset(l,0,sizeof(*l)); l->mem_report = MEMORY_WARNING_REPORT; l->mem_limit = 0; l->L = lua_newstate(lalloc, l, global_seed()); l->activeL = NULL; ATOM_INIT(&l->trap , 0); return l; } void snlua_release(struct snlua *l) { lua_close(l->L); skynet_free(l); } void snlua_signal(struct snlua *l, int signal) { skynet_error(l->ctx, "recv a signal %d", signal); if (signal == 0) { if (ATOM_LOAD(&l->trap) == 0) { // only one thread can set trap ( l->trap 0->1 ) if (!ATOM_CAS(&l->trap, 0, 1)) return; lua_sethook (l->activeL, signal_hook, LUA_MASKCOUNT, 1); // finish set ( l->trap 1 -> -1 ) ATOM_CAS(&l->trap, 1, -1); } } else if (signal == 1) { skynet_error(l->ctx, "Current Memory %.3fK", (float)l->mem / 1024); } } ================================================ FILE: skynet-src/atomic.h ================================================ #ifndef SKYNET_ATOMIC_H #define SKYNET_ATOMIC_H #include #include #ifdef __STDC_NO_ATOMICS__ #define ATOM_INT volatile int #define ATOM_POINTER volatile uintptr_t #define ATOM_SIZET volatile size_t #define ATOM_ULONG volatile unsigned long #define ATOM_INIT(ptr, v) (*(ptr) = v) #define ATOM_LOAD(ptr) (*(ptr)) #define ATOM_STORE(ptr, v) (*(ptr) = v) #define ATOM_CAS(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval) #define ATOM_CAS_ULONG(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval) #define ATOM_CAS_SIZET(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval) #define ATOM_CAS_POINTER(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval) #define ATOM_FINC(ptr) __sync_fetch_and_add(ptr, 1) #define ATOM_FDEC(ptr) __sync_fetch_and_sub(ptr, 1) #define ATOM_FADD(ptr,n) __sync_fetch_and_add(ptr, n) #define ATOM_FSUB(ptr,n) __sync_fetch_and_sub(ptr, n) #define ATOM_FAND(ptr,n) __sync_fetch_and_and(ptr, n) #else #if defined (__cplusplus) #include #define STD_ std:: #define atomic_value_type_(p, v) decltype((p)->load())(v) #else #include #define STD_ #define atomic_value_type_(p, v) v #endif #define ATOM_INT STD_ atomic_int #define ATOM_POINTER STD_ atomic_uintptr_t #define ATOM_SIZET STD_ atomic_size_t #define ATOM_ULONG STD_ atomic_ulong #define ATOM_INIT(ref, v) STD_ atomic_init(ref, v) #define ATOM_LOAD(ptr) STD_ atomic_load(ptr) #define ATOM_STORE(ptr, v) STD_ atomic_store(ptr, v) static inline int ATOM_CAS(STD_ atomic_int *ptr, int oval, int nval) { return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval); } static inline int ATOM_CAS_SIZET(STD_ atomic_size_t *ptr, size_t oval, size_t nval) { return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval); } static inline int ATOM_CAS_ULONG(STD_ atomic_ulong *ptr, unsigned long oval, unsigned long nval) { return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval); } static inline int ATOM_CAS_POINTER(STD_ atomic_uintptr_t *ptr, uintptr_t oval, uintptr_t nval) { return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval); } #define ATOM_FINC(ptr) STD_ atomic_fetch_add(ptr, atomic_value_type_(ptr,1)) #define ATOM_FDEC(ptr) STD_ atomic_fetch_sub(ptr, atomic_value_type_(ptr, 1)) #define ATOM_FADD(ptr,n) STD_ atomic_fetch_add(ptr, atomic_value_type_(ptr, n)) #define ATOM_FSUB(ptr,n) STD_ atomic_fetch_sub(ptr, atomic_value_type_(ptr, n)) #define ATOM_FAND(ptr,n) STD_ atomic_fetch_and(ptr, atomic_value_type_(ptr, n)) #endif #endif ================================================ FILE: skynet-src/malloc_hook.c ================================================ #include #include #include #include #include #include "skynet.h" #include "atomic.h" #include "malloc_hook.h" // turn on MEMORY_CHECK can do more memory check, such as double free // #define MEMORY_CHECK #define MEMORY_ALLOCTAG 0x20140605 #define MEMORY_FREETAG 0x0badf00d struct mem_data { alignas(CACHE_LINE_SIZE) ATOM_ULONG handle; AtomicMemInfo info; }; _Static_assert(sizeof(struct mem_data) % CACHE_LINE_SIZE == 0, "mem_data must be cache-line aligned"); struct mem_cookie { size_t size; uint32_t handle; #ifdef MEMORY_CHECK uint32_t dogtag; #endif uint32_t cookie_size; // should be the last }; #define SLOT_SIZE 0x10000 #define PREFIX_SIZE sizeof(struct mem_cookie) static struct mem_data mem_stats[SLOT_SIZE]; _Static_assert(alignof(mem_stats) % CACHE_LINE_SIZE == 0, "mem_stats must be cache-line aligned"); static struct mem_data * get_mem_stat(uint32_t handle) { int h = (int)(handle & (SLOT_SIZE - 1)); struct mem_data *data = &mem_stats[h]; return data; } #ifndef NOUSE_JEMALLOC #include "jemalloc.h" // for skynet_lalloc use #define raw_realloc je_realloc #define raw_free je_free inline static void update_xmalloc_stat_alloc(uint32_t handle, size_t __n) { struct mem_data *data = get_mem_stat(handle); // 当两个不同的 handle 被哈希到同一个槽位时, 新的服务会覆盖旧服务的数据 // 这种情况在实际运行中非常罕见, 因为同时存在的服务数量很难超过 65536 ATOM_STORE(&data->handle, handle); atomic_meminfo_alloc(&data->info, __n); } inline static void update_xmalloc_stat_free(uint32_t handle, size_t __n) { struct mem_data *data = get_mem_stat(handle); atomic_meminfo_free(&data->info, __n); } inline static void* fill_prefix(char* ptr, size_t sz, uint32_t cookie_size) { uint32_t handle = skynet_current_handle(); struct mem_cookie *p = (struct mem_cookie *)ptr; char * ret = ptr + cookie_size; sz += cookie_size; p->size = sz; p->handle = handle; #ifdef MEMORY_CHECK p->dogtag = MEMORY_ALLOCTAG; #endif update_xmalloc_stat_alloc(handle, sz); memcpy(ret - sizeof(uint32_t), &cookie_size, sizeof(cookie_size)); return ret; } inline static uint32_t get_cookie_size(char *ptr) { uint32_t cookie_size; memcpy(&cookie_size, ptr - sizeof(cookie_size), sizeof(cookie_size)); return cookie_size; } inline static void* clean_prefix(char* ptr) { uint32_t cookie_size = get_cookie_size(ptr); struct mem_cookie *p = (struct mem_cookie *)(ptr - cookie_size); uint32_t handle = p->handle; #ifdef MEMORY_CHECK uint32_t dogtag = p->dogtag; if (dogtag == MEMORY_FREETAG) { fprintf(stderr, "xmalloc: double free in :%08x\n", handle); } assert(dogtag == MEMORY_ALLOCTAG); // memory out of bounds p->dogtag = MEMORY_FREETAG; #endif update_xmalloc_stat_free(handle, p->size); return p; } static void malloc_oom(size_t size) { fprintf(stderr, "xmalloc: Out of memory trying to allocate %zu bytes\n", size); fflush(stderr); abort(); } void memory_info_dump(const char* opts) { je_malloc_stats_print(0,0, opts); } bool mallctl_bool(const char* name, bool* newval) { bool v = 0; size_t len = sizeof(v); if(newval) { je_mallctl(name, &v, &len, newval, sizeof(bool)); } else { je_mallctl(name, &v, &len, NULL, 0); } return v; } int mallctl_cmd(const char* name) { return je_mallctl(name, NULL, NULL, NULL, 0); } size_t mallctl_int64(const char* name, size_t* newval) { size_t v = 0; size_t len = sizeof(v); if(newval) { je_mallctl(name, &v, &len, newval, sizeof(size_t)); } else { je_mallctl(name, &v, &len, NULL, 0); } // skynet_error(NULL, "name: %s, value: %zd\n", name, v); return v; } int mallctl_opt(const char* name, int* newval) { int v = 0; size_t len = sizeof(v); if(newval) { int ret = je_mallctl(name, &v, &len, newval, sizeof(int)); if(ret == 0) { skynet_error(NULL, "set new value(%d) for (%s) succeed\n", *newval, name); } else { skynet_error(NULL, "set new value(%d) for (%s) failed: error -> %d\n", *newval, name, ret); } } else { je_mallctl(name, &v, &len, NULL, 0); } return v; } // hook : malloc, realloc, free, calloc void * skynet_malloc(size_t size) { void* ptr = je_malloc(size + PREFIX_SIZE); if(!ptr) malloc_oom(size); return fill_prefix(ptr, size, PREFIX_SIZE); } void * skynet_realloc(void *ptr, size_t size) { if (ptr == NULL) return skynet_malloc(size); uint32_t cookie_size = get_cookie_size(ptr); void* rawptr = clean_prefix(ptr); void *newptr = je_realloc(rawptr, size+cookie_size); if(!newptr) malloc_oom(size); return fill_prefix(newptr, size, cookie_size); } void skynet_free(void *ptr) { if (ptr == NULL) return; void* rawptr = clean_prefix(ptr); je_free(rawptr); } void * skynet_calloc(size_t nmemb, size_t size) { uint32_t cookie_n = (PREFIX_SIZE+size-1)/size; void* ptr = je_calloc(nmemb + cookie_n, size); if(!ptr) malloc_oom(nmemb * size); return fill_prefix(ptr, nmemb * size, cookie_n * size); } static inline uint32_t alignment_cookie_size(size_t alignment) { if (alignment >= PREFIX_SIZE) return alignment; switch (alignment) { case 4 : return (PREFIX_SIZE + 3) / 4 * 4; case 8 : return (PREFIX_SIZE + 7) / 8 * 8; case 16 : return (PREFIX_SIZE + 15) / 16 * 16; } return (PREFIX_SIZE + alignment - 1) / alignment * alignment; } void * skynet_memalign(size_t alignment, size_t size) { uint32_t cookie_size = alignment_cookie_size(alignment); void* ptr = je_memalign(alignment, size + cookie_size); if(!ptr) malloc_oom(size); return fill_prefix(ptr, size, cookie_size); } void * skynet_aligned_alloc(size_t alignment, size_t size) { uint32_t cookie_size = alignment_cookie_size(alignment); void* ptr = je_aligned_alloc(alignment, size + cookie_size); if(!ptr) malloc_oom(size); return fill_prefix(ptr, size, cookie_size); } int skynet_posix_memalign(void **memptr, size_t alignment, size_t size) { uint32_t cookie_size = alignment_cookie_size(alignment); int err = je_posix_memalign(memptr, alignment, size + cookie_size); if (err) malloc_oom(size); fill_prefix(*memptr, size, cookie_size); return err; } #else // for skynet_lalloc use #define raw_realloc realloc #define raw_free free void memory_info_dump(const char* opts) { skynet_error(NULL, "No jemalloc"); } size_t mallctl_int64(const char* name, size_t* newval) { skynet_error(NULL, "No jemalloc : mallctl_int64 %s.", name); return 0; } int mallctl_opt(const char* name, int* newval) { skynet_error(NULL, "No jemalloc : mallctl_opt %s.", name); return 0; } bool mallctl_bool(const char* name, bool* newval) { skynet_error(NULL, "No jemalloc : mallctl_bool %s.", name); return 0; } int mallctl_cmd(const char* name) { skynet_error(NULL, "No jemalloc : mallctl_cmd %s.", name); return 0; } #endif size_t malloc_used_memory(void) { MemInfo total = {}; for(int i = 0; i < SLOT_SIZE; i++) { struct mem_data* data = &mem_stats[i]; const uint32_t handle = ATOM_LOAD(&data->handle); if (handle != 0) { atomic_meminfo_merge(&total, &data->info); } } return total.alloc - total.free; } size_t malloc_memory_block(void) { MemInfo total = {}; for(int i = 0; i < SLOT_SIZE; i++) { struct mem_data* data = &mem_stats[i]; const uint32_t handle = ATOM_LOAD(&data->handle); if (handle != 0) { atomic_meminfo_merge(&total, &data->info); } } return total.alloc_count - total.free_count; } void dump_c_mem() { skynet_error(NULL, "dump all service mem:"); MemInfo total = {}; for(int i = 0; i < SLOT_SIZE; i++) { struct mem_data* data = &mem_stats[i]; const uint32_t handle = ATOM_LOAD(&data->handle); if (handle != 0) { MemInfo info = {}; atomic_meminfo_merge(&info, &data->info); meminfo_merge(&total, &info); const size_t using = info.alloc - info.free; skynet_error(NULL, ":%08x -> %zukb %zub", handle, using >> 10, using); } } const size_t using = total.alloc - total.free; skynet_error(NULL, "+total: %zukb", using >> 10); } char * skynet_strdup(const char *str) { size_t sz = strlen(str); char * ret = skynet_malloc(sz+1); memcpy(ret, str, sz+1); return ret; } void * skynet_lalloc(void *ptr, size_t osize, size_t nsize) { if (nsize == 0) { raw_free(ptr); return NULL; } else { return raw_realloc(ptr, nsize); } } int dump_mem_lua(lua_State *L) { int i; lua_newtable(L); for(i=0; ihandle); if (handle != 0) { MemInfo info = {}; atomic_meminfo_merge(&info, &data->info); lua_pushinteger(L, info.alloc - info.free); lua_rawseti(L, -2, handle); } } return 1; } size_t malloc_current_memory(void) { uint32_t handle = skynet_current_handle(); struct mem_data *data = get_mem_stat(handle); if (ATOM_LOAD(&data->handle) != handle) { return 0; } MemInfo info = {}; atomic_meminfo_merge(&info, &data->info); return info.alloc - info.free; } void skynet_debug_memory(const char *info) { // for debug use uint32_t handle = skynet_current_handle(); size_t mem = malloc_current_memory(); fprintf(stderr, "[:%08x] %s %p\n", handle, info, (void *)mem); } ================================================ FILE: skynet-src/malloc_hook.h ================================================ #ifndef SKYNET_MALLOC_HOOK_H #define SKYNET_MALLOC_HOOK_H #include #include #include #include "mem_info.h" extern size_t malloc_used_memory(void); extern size_t malloc_memory_block(void); extern void memory_info_dump(const char *opts); extern size_t mallctl_int64(const char* name, size_t* newval); extern int mallctl_opt(const char* name, int* newval); extern bool mallctl_bool(const char* name, bool* newval); extern int mallctl_cmd(const char* name); extern void dump_c_mem(void); extern int dump_mem_lua(lua_State *L); extern size_t malloc_current_memory(void); #endif /* SKYNET_MALLOC_HOOK_H */ ================================================ FILE: skynet-src/mem_info.c ================================================ #include #include "mem_info.h" void meminfo_init(MemInfo *info) { memset(info, 0, sizeof(*info)); } void atomic_meminfo_init(AtomicMemInfo *info) { ATOM_INIT(&info->alloc, 0); ATOM_INIT(&info->alloc_count, 0); ATOM_INIT(&info->free, 0); ATOM_INIT(&info->free_count, 0); } void meminfo_alloc(MemInfo *info, size_t size) { info->alloc += size; ++info->alloc_count; } void atomic_meminfo_alloc(AtomicMemInfo *info, size_t size) { ATOM_FADD(&info->alloc, size); ATOM_FADD(&info->alloc_count, 1); } void meminfo_free(MemInfo *info, size_t size) { info->free += size; ++info->free_count; } void atomic_meminfo_free(AtomicMemInfo *info, size_t size) { ATOM_FADD(&info->free, size); ATOM_FADD(&info->free_count, 1); } void meminfo_merge(MemInfo *dest, const MemInfo *src) { dest->alloc += src->alloc; dest->alloc_count += src->alloc_count; dest->free += src->free; dest->free_count += src->free_count; } void atomic_meminfo_merge(MemInfo *dest, const AtomicMemInfo *src) { MemInfo info; // 先加载 free 后加载 alloc 避免大小错乱 info.free_count = ATOM_LOAD(&src->free_count); info.free = ATOM_LOAD(&src->free); info.alloc_count = ATOM_LOAD(&src->alloc_count); info.alloc = ATOM_LOAD(&src->alloc); meminfo_merge(dest, &info); } ================================================ FILE: skynet-src/mem_info.h ================================================ #ifndef _MEM_INFO_H_ #define _MEM_INFO_H_ #include #include #include "atomic.h" #define CACHE_LINE_SIZE 64 typedef struct { size_t alloc; size_t alloc_count; size_t free; size_t free_count; } MemInfo; typedef struct { // alloc 与 free 可能不在一个线程 // 为了避免竞争,这里也拆成两个 CacheLine 大小 alignas(CACHE_LINE_SIZE) ATOM_SIZET alloc; ATOM_SIZET alloc_count; alignas(CACHE_LINE_SIZE) ATOM_SIZET free; ATOM_SIZET free_count; } AtomicMemInfo; void meminfo_init(MemInfo *info); void atomic_meminfo_init(AtomicMemInfo *info); void meminfo_alloc(MemInfo *info, size_t size); void atomic_meminfo_alloc(AtomicMemInfo *info, size_t size); void meminfo_free(MemInfo *info, size_t size); void atomic_meminfo_free(AtomicMemInfo *info, size_t size); void meminfo_merge(MemInfo *dest, const MemInfo *src); void atomic_meminfo_merge(MemInfo *dest, const AtomicMemInfo *src); #endif // _MEM_INFO_H_ ================================================ FILE: skynet-src/rwlock.h ================================================ #ifndef SKYNET_RWLOCK_H #define SKYNET_RWLOCK_H #ifndef USE_PTHREAD_LOCK #include "atomic.h" struct rwlock { ATOM_INT write; ATOM_INT read; }; static inline void rwlock_init(struct rwlock *lock) { ATOM_INIT(&lock->write, 0); ATOM_INIT(&lock->read, 0); } static inline void rwlock_rlock(struct rwlock *lock) { for (;;) { while(ATOM_LOAD(&lock->write)) {} ATOM_FINC(&lock->read); if (ATOM_LOAD(&lock->write)) { ATOM_FDEC(&lock->read); } else { break; } } } static inline void rwlock_wlock(struct rwlock *lock) { while (!ATOM_CAS(&lock->write,0,1)) {} while(ATOM_LOAD(&lock->read)) {} } static inline void rwlock_wunlock(struct rwlock *lock) { ATOM_STORE(&lock->write, 0); } static inline void rwlock_runlock(struct rwlock *lock) { ATOM_FDEC(&lock->read); } #else #include // only for some platform doesn't have __sync_* // todo: check the result of pthread api struct rwlock { pthread_rwlock_t lock; }; static inline void rwlock_init(struct rwlock *lock) { pthread_rwlock_init(&lock->lock, NULL); } static inline void rwlock_rlock(struct rwlock *lock) { pthread_rwlock_rdlock(&lock->lock); } static inline void rwlock_wlock(struct rwlock *lock) { pthread_rwlock_wrlock(&lock->lock); } static inline void rwlock_wunlock(struct rwlock *lock) { pthread_rwlock_unlock(&lock->lock); } static inline void rwlock_runlock(struct rwlock *lock) { pthread_rwlock_unlock(&lock->lock); } #endif #endif ================================================ FILE: skynet-src/skynet.h ================================================ #ifndef SKYNET_H #define SKYNET_H #include "skynet_malloc.h" #include #include #define PTYPE_TEXT 0 #define PTYPE_RESPONSE 1 #define PTYPE_MULTICAST 2 #define PTYPE_CLIENT 3 #define PTYPE_SYSTEM 4 #define PTYPE_HARBOR 5 #define PTYPE_SOCKET 6 // read lualib/skynet.lua examples/simplemonitor.lua #define PTYPE_ERROR 7 // read lualib/skynet.lua lualib/mqueue.lua lualib/snax.lua #define PTYPE_RESERVED_QUEUE 8 #define PTYPE_RESERVED_DEBUG 9 #define PTYPE_RESERVED_LUA 10 #define PTYPE_RESERVED_SNAX 11 #define PTYPE_TAG_DONTCOPY 0x10000 #define PTYPE_TAG_ALLOCSESSION 0x20000 struct skynet_context; void skynet_error(struct skynet_context * context, const char *msg, ...); const char * skynet_command(struct skynet_context * context, const char * cmd , const char * parm); uint32_t skynet_queryname(struct skynet_context * context, const char * name); int skynet_send(struct skynet_context * context, uint32_t source, uint32_t destination , int type, int session, void * msg, size_t sz); int skynet_sendname(struct skynet_context * context, uint32_t source, const char * destination , int type, int session, void * msg, size_t sz); int skynet_isremote(struct skynet_context *, uint32_t handle, int * harbor); typedef int (*skynet_cb)(struct skynet_context * context, void *ud, int type, int session, uint32_t source , const void * msg, size_t sz); void skynet_callback(struct skynet_context * context, void *ud, skynet_cb cb); uint32_t skynet_current_handle(void); uint64_t skynet_now(void); void skynet_debug_memory(const char *info); // for debug use, output current service memory to stderr #endif ================================================ FILE: skynet-src/skynet_daemon.c ================================================ #include #include #include #include #include #include #include #include #include "skynet_daemon.h" static int check_pid(const char *pidfile) { int pid = 0; FILE *f = fopen(pidfile,"r"); if (f == NULL) return 0; int n = fscanf(f,"%d", &pid); fclose(f); if (n !=1 || pid == 0 || pid == getpid()) { return 0; } if (kill(pid, 0) && errno == ESRCH) return 0; return pid; } static int write_pid(const char *pidfile) { FILE *f; int pid = 0; int fd = open(pidfile, O_RDWR|O_CREAT, 0644); if (fd == -1) { fprintf(stderr, "Can't create pidfile [%s].\n", pidfile); return 0; } f = fdopen(fd, "w+"); if (f == NULL) { fprintf(stderr, "Can't open pidfile [%s].\n", pidfile); return 0; } if (flock(fd, LOCK_EX|LOCK_NB) == -1) { int n = fscanf(f, "%d", &pid); fclose(f); if (n != 1) { fprintf(stderr, "Can't lock and read pidfile.\n"); } else { fprintf(stderr, "Can't lock pidfile, lock is held by pid %d.\n", pid); } return 0; } pid = getpid(); if (!fprintf(f,"%d\n", pid)) { fprintf(stderr, "Can't write pid.\n"); close(fd); return 0; } fflush(f); return pid; } static int redirect_fds() { int nfd = open("/dev/null", O_RDWR); if (nfd == -1) { perror("Unable to open /dev/null: "); return -1; } if (dup2(nfd, 0) < 0) { perror("Unable to dup2 stdin(0): "); return -1; } if (dup2(nfd, 1) < 0) { perror("Unable to dup2 stdout(1): "); return -1; } if (dup2(nfd, 2) < 0) { perror("Unable to dup2 stderr(2): "); return -1; } close(nfd); return 0; } int daemon_init(const char *pidfile) { int pid = check_pid(pidfile); if (pid) { fprintf(stderr, "Skynet is already running, pid = %d.\n", pid); return 1; } #ifdef __APPLE__ fprintf(stderr, "'daemon' is deprecated: first deprecated in OS X 10.5 , use launchd instead.\n"); #else if (daemon(1,1)) { fprintf(stderr, "Can't daemonize.\n"); return 1; } #endif pid = write_pid(pidfile); if (pid == 0) { return 1; } if (redirect_fds()) { return 1; } return 0; } int daemon_exit(const char *pidfile) { return unlink(pidfile); } ================================================ FILE: skynet-src/skynet_daemon.h ================================================ #ifndef skynet_daemon_h #define skynet_daemon_h int daemon_init(const char *pidfile); int daemon_exit(const char *pidfile); #endif ================================================ FILE: skynet-src/skynet_env.c ================================================ #include "skynet.h" #include "skynet_env.h" #include "spinlock.h" #include #include #include #include struct skynet_env { struct spinlock lock; lua_State *L; }; static struct skynet_env *E = NULL; const char * skynet_getenv(const char *key) { SPIN_LOCK(E) lua_State *L = E->L; lua_getglobal(L, key); const char * result = lua_tostring(L, -1); lua_pop(L, 1); SPIN_UNLOCK(E) return result; } void skynet_setenv(const char *key, const char *value) { SPIN_LOCK(E) lua_State *L = E->L; lua_getglobal(L, key); assert(lua_isnil(L, -1)); lua_pop(L,1); lua_pushstring(L,value); lua_setglobal(L,key); SPIN_UNLOCK(E) } void skynet_env_init() { E = skynet_malloc(sizeof(*E)); SPIN_INIT(E) E->L = luaL_newstate(); } ================================================ FILE: skynet-src/skynet_env.h ================================================ #ifndef SKYNET_ENV_H #define SKYNET_ENV_H const char * skynet_getenv(const char *key); void skynet_setenv(const char *key, const char *value); void skynet_env_init(); #endif ================================================ FILE: skynet-src/skynet_error.c ================================================ #include "skynet.h" #include "skynet_handle.h" #include "skynet_imp.h" #include "skynet_mq.h" #include "skynet_server.h" #include #include #include #define LOG_MESSAGE_SIZE 256 static int log_try_vasprintf(char **strp, const char *fmt, va_list ap) { if (strcmp(fmt, "%*s") == 0) { // for `lerror` in lua-skynet.c const int len = va_arg(ap, int); const char *tmp = va_arg(ap, const char*); *strp = skynet_strndup(tmp, len); return *strp != NULL ? len : -1; } char tmp[LOG_MESSAGE_SIZE]; int len = vsnprintf(tmp, LOG_MESSAGE_SIZE, fmt, ap); if (len >= 0 && len < LOG_MESSAGE_SIZE) { *strp = skynet_strndup(tmp, len); if (*strp == NULL) return -1; } return len; } void skynet_error(struct skynet_context * context, const char *msg, ...) { static uint32_t logger = 0; if (logger == 0) { logger = skynet_handle_findname("logger"); } if (logger == 0) { return; } char *data = NULL; va_list ap; va_start(ap, msg); int len = log_try_vasprintf(&data, msg, ap); va_end(ap); if (len < 0) { perror("vasprintf error :"); return; } if (data == NULL) { // unlikely data = skynet_malloc(len + 1); va_start(ap, msg); len = vsnprintf(data, len + 1, msg, ap); va_end(ap); if (len < 0) { skynet_free(data); perror("vsnprintf error :"); return; } } struct skynet_message smsg; if (context == NULL) { smsg.source = 0; } else { smsg.source = skynet_context_handle(context); } smsg.session = 0; smsg.data = data; smsg.sz = len | ((size_t)PTYPE_TEXT << MESSAGE_TYPE_SHIFT); skynet_context_push(logger, &smsg); } ================================================ FILE: skynet-src/skynet_handle.c ================================================ #include "skynet.h" #include "skynet_handle.h" #include "skynet_imp.h" #include "skynet_server.h" #include "rwlock.h" #include #include #include #define DEFAULT_SLOT_SIZE 4 #define MAX_SLOT_SIZE 0x40000000 struct handle_name { char * name; uint32_t handle; }; struct handle_storage { struct rwlock lock; uint32_t harbor; uint32_t handle_index; int slot_size; struct skynet_context ** slot; int name_cap; int name_count; struct handle_name *name; }; static struct handle_storage *H = NULL; uint32_t skynet_handle_register(struct skynet_context *ctx) { struct handle_storage *s = H; rwlock_wlock(&s->lock); for (;;) { int i; uint32_t handle = s->handle_index; for (i=0;islot_size;i++,handle++) { if (handle > HANDLE_MASK) { // 0 is reserved handle = 1; } int hash = handle & (s->slot_size-1); if (s->slot[hash] == NULL) { s->slot[hash] = ctx; s->handle_index = handle + 1; rwlock_wunlock(&s->lock); handle |= s->harbor; return handle; } } assert((s->slot_size*2 - 1) <= HANDLE_MASK); struct skynet_context ** new_slot = skynet_malloc(s->slot_size * 2 * sizeof(struct skynet_context *)); memset(new_slot, 0, s->slot_size * 2 * sizeof(struct skynet_context *)); for (i=0;islot_size;i++) { if (s->slot[i]) { int hash = skynet_context_handle(s->slot[i]) & (s->slot_size * 2 - 1); assert(new_slot[hash] == NULL); new_slot[hash] = s->slot[i]; } } skynet_free(s->slot); s->slot = new_slot; s->slot_size *= 2; } } int skynet_handle_retire(uint32_t handle) { int ret = 0; struct handle_storage *s = H; rwlock_wlock(&s->lock); uint32_t hash = handle & (s->slot_size-1); struct skynet_context * ctx = s->slot[hash]; if (ctx != NULL && skynet_context_handle(ctx) == handle) { s->slot[hash] = NULL; ret = 1; int i; int j=0, n=s->name_count; for (i=0; iname[i].handle == handle) { skynet_free(s->name[i].name); continue; } else if (i!=j) { s->name[j] = s->name[i]; } ++j; } s->name_count = j; } else { ctx = NULL; } rwlock_wunlock(&s->lock); if (ctx) { // release ctx may call skynet_handle_* , so wunlock first. skynet_context_release(ctx); } return ret; } void skynet_handle_retireall() { struct handle_storage *s = H; for (;;) { int n=0; int i; for (i=0;islot_size;i++) { rwlock_rlock(&s->lock); struct skynet_context * ctx = s->slot[i]; uint32_t handle = 0; if (ctx) { handle = skynet_context_handle(ctx); ++n; } rwlock_runlock(&s->lock); if (handle != 0) { skynet_handle_retire(handle); } } if (n==0) return; } } struct skynet_context * skynet_handle_grab(uint32_t handle) { struct handle_storage *s = H; struct skynet_context * result = NULL; rwlock_rlock(&s->lock); uint32_t hash = handle & (s->slot_size-1); struct skynet_context * ctx = s->slot[hash]; if (ctx && skynet_context_handle(ctx) == handle) { result = ctx; skynet_context_grab(result); } rwlock_runlock(&s->lock); return result; } uint32_t skynet_handle_findname(const char * name) { struct handle_storage *s = H; rwlock_rlock(&s->lock); uint32_t handle = 0; int begin = 0; int end = s->name_count - 1; while (begin<=end) { int mid = (begin+end)/2; struct handle_name *n = &s->name[mid]; int c = strcmp(n->name, name); if (c==0) { handle = n->handle; break; } if (c<0) { begin = mid + 1; } else { end = mid - 1; } } rwlock_runlock(&s->lock); return handle; } static void _insert_name_before(struct handle_storage *s, char *name, uint32_t handle, int before) { if (s->name_count >= s->name_cap) { s->name_cap *= 2; assert(s->name_cap <= MAX_SLOT_SIZE); struct handle_name * n = skynet_malloc(s->name_cap * sizeof(struct handle_name)); int i; for (i=0;iname[i]; } for (i=before;iname_count;i++) { n[i+1] = s->name[i]; } skynet_free(s->name); s->name = n; } else { int i; for (i=s->name_count;i>before;i--) { s->name[i] = s->name[i-1]; } } s->name[before].name = name; s->name[before].handle = handle; s->name_count ++; } static const char * _insert_name(struct handle_storage *s, const char * name, uint32_t handle) { int begin = 0; int end = s->name_count - 1; while (begin<=end) { int mid = (begin+end)/2; struct handle_name *n = &s->name[mid]; int c = strcmp(n->name, name); if (c==0) { return NULL; } if (c<0) { begin = mid + 1; } else { end = mid - 1; } } char * result = skynet_strdup(name); _insert_name_before(s, result, handle, begin); return result; } const char * skynet_handle_namehandle(uint32_t handle, const char *name) { rwlock_wlock(&H->lock); const char * ret = _insert_name(H, name, handle); rwlock_wunlock(&H->lock); return ret; } void skynet_handle_init(int harbor) { assert(H==NULL); struct handle_storage * s = skynet_malloc(sizeof(*H)); s->slot_size = DEFAULT_SLOT_SIZE; s->slot = skynet_malloc(s->slot_size * sizeof(struct skynet_context *)); memset(s->slot, 0, s->slot_size * sizeof(struct skynet_context *)); rwlock_init(&s->lock); // reserve 0 for system s->harbor = (uint32_t) (harbor & 0xff) << HANDLE_REMOTE_SHIFT; s->handle_index = 1; s->name_cap = 2; s->name_count = 0; s->name = skynet_malloc(s->name_cap * sizeof(struct handle_name)); H = s; // Don't need to free H } ================================================ FILE: skynet-src/skynet_handle.h ================================================ #ifndef SKYNET_CONTEXT_HANDLE_H #define SKYNET_CONTEXT_HANDLE_H #include // reserve high 8 bits for remote id #define HANDLE_MASK 0xffffff #define HANDLE_REMOTE_SHIFT 24 struct skynet_context; uint32_t skynet_handle_register(struct skynet_context *); int skynet_handle_retire(uint32_t handle); struct skynet_context * skynet_handle_grab(uint32_t handle); void skynet_handle_retireall(); uint32_t skynet_handle_findname(const char * name); const char * skynet_handle_namehandle(uint32_t handle, const char *name); void skynet_handle_init(int harbor); #endif ================================================ FILE: skynet-src/skynet_harbor.c ================================================ #include "skynet.h" #include "skynet_harbor.h" #include "skynet_server.h" #include "skynet_mq.h" #include "skynet_handle.h" #include #include #include static struct skynet_context * REMOTE = 0; static unsigned int HARBOR = ~0; static inline int invalid_type(int type) { return type != PTYPE_SYSTEM && type != PTYPE_HARBOR; } void skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int session) { assert(invalid_type(rmsg->type) && REMOTE); skynet_context_send(REMOTE, rmsg, sizeof(*rmsg) , source, PTYPE_SYSTEM , session); } int skynet_harbor_message_isremote(uint32_t handle) { assert(HARBOR != ~0); int h = (handle & ~HANDLE_MASK); return h != HARBOR && h !=0; } void skynet_harbor_init(int harbor) { HARBOR = (unsigned int)harbor << HANDLE_REMOTE_SHIFT; } void skynet_harbor_start(void *ctx) { // the HARBOR must be reserved to ensure the pointer is valid. // It will be released at last by calling skynet_harbor_exit skynet_context_reserve(ctx); REMOTE = ctx; } void skynet_harbor_exit() { struct skynet_context * ctx = REMOTE; REMOTE= NULL; if (ctx) { skynet_context_release(ctx); } } ================================================ FILE: skynet-src/skynet_harbor.h ================================================ #ifndef SKYNET_HARBOR_H #define SKYNET_HARBOR_H #include #include #define GLOBALNAME_LENGTH 16 #define REMOTE_MAX 256 struct remote_name { char name[GLOBALNAME_LENGTH]; uint32_t handle; }; struct remote_message { struct remote_name destination; const void * message; size_t sz; int type; }; void skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int session); int skynet_harbor_message_isremote(uint32_t handle); void skynet_harbor_init(int harbor); void skynet_harbor_start(void * ctx); void skynet_harbor_exit(); #endif ================================================ FILE: skynet-src/skynet_imp.h ================================================ #ifndef SKYNET_IMP_H #define SKYNET_IMP_H #include struct skynet_config { int thread; int harbor; int profile; const char * daemon; const char * module_path; const char * bootstrap; const char * logger; const char * logservice; }; #define THREAD_WORKER 0 #define THREAD_MAIN 1 #define THREAD_SOCKET 2 #define THREAD_TIMER 3 #define THREAD_MONITOR 4 void skynet_start(struct skynet_config * config); static inline char * skynet_strndup(const char *str, size_t size) { char * ret = skynet_malloc(size+1); if (ret == NULL) return NULL; memcpy(ret, str, size); ret[size] = '\0'; return ret; } static inline char * skynet_strdup(const char *str) { size_t sz = strlen(str); return skynet_strndup(str, sz); } #endif ================================================ FILE: skynet-src/skynet_log.c ================================================ #include "skynet_log.h" #include "skynet_timer.h" #include "skynet.h" #include "skynet_socket.h" #include #include FILE * skynet_log_open(struct skynet_context * ctx, uint32_t handle) { const char * logpath = skynet_getenv("logpath"); if (logpath == NULL) return NULL; size_t sz = strlen(logpath); char tmp[sz + 16]; sprintf(tmp, "%s/%08x.log", logpath, handle); FILE *f = fopen(tmp, "ab"); if (f) { uint32_t starttime = skynet_starttime(); uint64_t currenttime = skynet_now(); time_t ti = starttime + currenttime/100; skynet_error(ctx, "Open log file %s", tmp); fprintf(f, "open time: %u %s", (uint32_t)currenttime, ctime(&ti)); fflush(f); } else { skynet_error(ctx, "Open log file %s fail", tmp); } return f; } void skynet_log_close(struct skynet_context * ctx, FILE *f, uint32_t handle) { skynet_error(ctx, "Close log file :%08x", handle); fprintf(f, "close time: %u\n", (uint32_t)skynet_now()); fclose(f); } static void log_blob(FILE *f, void * buffer, size_t sz) { size_t i; uint8_t * buf = buffer; for (i=0;i!=sz;i++) { fprintf(f, "%02x", buf[i]); } } static void log_socket(FILE * f, struct skynet_socket_message * message, size_t sz) { fprintf(f, "[socket] %d %d %d ", message->type, message->id, message->ud); if (message->buffer == NULL) { const char *buffer = (const char *)(message + 1); sz -= sizeof(*message); const char * eol = memchr(buffer, '\0', sz); if (eol) { sz = eol - buffer; } fprintf(f, "[%*s]", (int)sz, (const char *)buffer); } else { sz = message->ud; log_blob(f, message->buffer, sz); } fprintf(f, "\n"); fflush(f); } void skynet_log_output(FILE *f, uint32_t source, int type, int session, void * buffer, size_t sz) { if (type == PTYPE_SOCKET) { log_socket(f, buffer, sz); } else { uint32_t ti = (uint32_t)skynet_now(); fprintf(f, ":%08x %d %d %u ", source, type, session, ti); log_blob(f, buffer, sz); fprintf(f,"\n"); fflush(f); } } ================================================ FILE: skynet-src/skynet_log.h ================================================ #ifndef skynet_log_h #define skynet_log_h #include "skynet_env.h" #include "skynet.h" #include #include FILE * skynet_log_open(struct skynet_context * ctx, uint32_t handle); void skynet_log_close(struct skynet_context * ctx, FILE *f, uint32_t handle); void skynet_log_output(FILE *f, uint32_t source, int type, int session, void * buffer, size_t sz); #endif ================================================ FILE: skynet-src/skynet_main.c ================================================ #include "skynet.h" #include "skynet_imp.h" #include "skynet_env.h" #include "skynet_server.h" #include #include #include #include #include #include #include #include #ifndef SKYNET_MAXTHREAD #define SKYNET_MAXTHREAD 1024 #endif static int optint(const char *key, int opt) { const char * str = skynet_getenv(key); if (str == NULL) { char tmp[20]; sprintf(tmp,"%d",opt); skynet_setenv(key, tmp); return opt; } return strtol(str, NULL, 10); } static int optboolean(const char *key, int opt) { const char * str = skynet_getenv(key); if (str == NULL) { skynet_setenv(key, opt ? "true" : "false"); return opt; } return strcmp(str,"true")==0; } static const char * optstring(const char *key,const char * opt) { const char * str = skynet_getenv(key); if (str == NULL) { if (opt) { skynet_setenv(key, opt); opt = skynet_getenv(key); } return opt; } return str; } static void _init_env(lua_State *L) { lua_pushnil(L); /* first key */ while (lua_next(L, -2) != 0) { int keyt = lua_type(L, -2); if (keyt != LUA_TSTRING) { fprintf(stderr, "Invalid config table\n"); exit(1); } const char * key = lua_tostring(L,-2); if (lua_type(L,-1) == LUA_TBOOLEAN) { int b = lua_toboolean(L,-1); skynet_setenv(key,b ? "true" : "false" ); } else { const char * value = lua_tostring(L,-1); if (value == NULL) { fprintf(stderr, "Invalid config table key = %s\n", key); exit(1); } skynet_setenv(key,value); } lua_pop(L,1); } lua_pop(L,1); } int sigign() { struct sigaction sa; sa.sa_handler = SIG_IGN; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); sigaction(SIGPIPE, &sa, 0); return 0; } static const char * load_config = "\ local result = {}\n\ local function getenv(name) return assert(os.getenv(name), [[os.getenv() failed: ]] .. name) end\n\ local sep = package.config:sub(1,1)\n\ local current_path = [[.]]..sep\n\ local function include(filename)\n\ local last_path = current_path\n\ local path, name = filename:match([[(.*]]..sep..[[)(.*)$]])\n\ if path then\n\ if path:sub(1,1) == sep then -- root\n\ current_path = path\n\ else\n\ current_path = current_path .. path\n\ end\n\ else\n\ name = filename\n\ end\n\ local f = assert(io.open(current_path .. name))\n\ local code = assert(f:read [[*a]])\n\ code = string.gsub(code, [[%$([%w_%d]+)]], getenv)\n\ f:close()\n\ assert(load(code,[[@]]..filename,[[t]],result))()\n\ current_path = last_path\n\ end\n\ setmetatable(result, { __index = { include = include } })\n\ local config_name = ...\n\ include(config_name)\n\ setmetatable(result, nil)\n\ return result\n\ "; int main(int argc, char *argv[]) { const char * config_file = NULL ; if (argc > 1) { config_file = argv[1]; } else { fprintf(stderr, "Need a config file. Please read skynet wiki : https://github.com/cloudwu/skynet/wiki/Config\n" "usage: skynet configfilename\n"); return 1; } skynet_globalinit(); skynet_env_init(); sigign(); struct skynet_config config; #ifdef LUA_CACHELIB // init the lock of code cache luaL_initcodecache(); #endif struct lua_State *L = luaL_newstate(); luaL_openlibs(L); // link lua lib int err = luaL_loadbufferx(L, load_config, strlen(load_config), "=[skynet config]", "t"); assert(err == LUA_OK); lua_pushstring(L, config_file); err = lua_pcall(L, 1, 1, 0); if (err) { fprintf(stderr,"%s\n",lua_tostring(L,-1)); lua_close(L); return 1; } _init_env(L); lua_close(L); config.thread = optint("thread",8); if (config.thread < 1 || config.thread > SKYNET_MAXTHREAD) { fprintf(stderr, "Invalid thread %d , should be in [1,%d]\n", config.thread, SKYNET_MAXTHREAD); return 1; } config.module_path = optstring("cpath","./cservice/?.so"); config.harbor = optint("harbor", 1); config.bootstrap = optstring("bootstrap","snlua bootstrap"); config.daemon = optstring("daemon", NULL); config.logger = optstring("logger", NULL); config.logservice = optstring("logservice", "logger"); config.profile = optboolean("profile", 1); skynet_start(&config); skynet_globalexit(); return 0; } ================================================ FILE: skynet-src/skynet_malloc.h ================================================ #ifndef skynet_malloc_h #define skynet_malloc_h #include #define skynet_malloc malloc #define skynet_calloc calloc #define skynet_realloc realloc #define skynet_free free #define skynet_memalign memalign #define skynet_aligned_alloc aligned_alloc #define skynet_posix_memalign posix_memalign void * skynet_malloc(size_t sz); void * skynet_calloc(size_t nmemb,size_t size); void * skynet_realloc(void *ptr, size_t size); void skynet_free(void *ptr); void * skynet_lalloc(void *ptr, size_t osize, size_t nsize); // use for lua void * skynet_memalign(size_t alignment, size_t size); void * skynet_aligned_alloc(size_t alignment, size_t size); int skynet_posix_memalign(void **memptr, size_t alignment, size_t size); #endif ================================================ FILE: skynet-src/skynet_module.c ================================================ #include "skynet.h" #include "skynet_imp.h" #include "skynet_module.h" #include "spinlock.h" #include #include #include #include #include #include #define MAX_MODULE_TYPE 32 struct modules { int count; struct spinlock lock; const char * path; struct skynet_module m[MAX_MODULE_TYPE]; }; static struct modules * M = NULL; static void * _try_open(struct modules *m, const char * name) { const char *l; const char * path = m->path; size_t path_size = strlen(path); size_t name_size = strlen(name); int sz = path_size + name_size; //search path void * dl = NULL; char tmp[sz]; do { memset(tmp,0,sz); while (*path == ';') path++; if (*path == '\0') break; l = strchr(path, ';'); if (l == NULL) l = path + strlen(path); int len = l - path; int i; for (i=0;path[i]!='?' && i < len ;i++) { tmp[i] = path[i]; } memcpy(tmp+i,name,name_size); if (path[i] == '?') { strncpy(tmp+i+name_size,path+i+1,len - i - 1); } else { fprintf(stderr,"Invalid C service path\n"); exit(1); } dl = dlopen(tmp, RTLD_NOW | RTLD_GLOBAL); path = l; }while(dl == NULL); if (dl == NULL) { fprintf(stderr, "try open %s failed : %s\n",name,dlerror()); } return dl; } static struct skynet_module * _query(const char * name) { int i; for (i=0;icount;i++) { if (strcmp(M->m[i].name,name)==0) { return &M->m[i]; } } return NULL; } static void * get_api(struct skynet_module *mod, const char *api_name) { size_t name_size = strlen(mod->name); size_t api_size = strlen(api_name); char tmp[name_size + api_size + 1]; memcpy(tmp, mod->name, name_size); memcpy(tmp+name_size, api_name, api_size+1); char *ptr = strrchr(tmp, '.'); if (ptr == NULL) { ptr = tmp; } else { ptr = ptr + 1; } return dlsym(mod->module, ptr); } static int open_sym(struct skynet_module *mod) { mod->create = get_api(mod, "_create"); mod->init = get_api(mod, "_init"); mod->release = get_api(mod, "_release"); mod->signal = get_api(mod, "_signal"); return mod->init == NULL; } struct skynet_module * skynet_module_query(const char * name) { struct skynet_module * result = _query(name); if (result) return result; SPIN_LOCK(M) result = _query(name); // double check if (result == NULL && M->count < MAX_MODULE_TYPE) { int index = M->count; void * dl = _try_open(M,name); if (dl) { M->m[index].name = name; M->m[index].module = dl; if (open_sym(&M->m[index]) == 0) { M->m[index].name = skynet_strdup(name); M->count ++; result = &M->m[index]; } } } SPIN_UNLOCK(M) return result; } void * skynet_module_instance_create(struct skynet_module *m) { if (m->create) { return m->create(); } else { return (void *)(intptr_t)(~0); } } int skynet_module_instance_init(struct skynet_module *m, void * inst, struct skynet_context *ctx, const char * parm) { return m->init(inst, ctx, parm); } void skynet_module_instance_release(struct skynet_module *m, void *inst) { if (m->release) { m->release(inst); } } void skynet_module_instance_signal(struct skynet_module *m, void *inst, int signal) { if (m->signal) { m->signal(inst, signal); } } void skynet_module_init(const char *path) { struct modules *m = skynet_malloc(sizeof(*m)); m->count = 0; m->path = skynet_strdup(path); SPIN_INIT(m) M = m; } ================================================ FILE: skynet-src/skynet_module.h ================================================ #ifndef SKYNET_MODULE_H #define SKYNET_MODULE_H struct skynet_context; typedef void * (*skynet_dl_create)(void); typedef int (*skynet_dl_init)(void * inst, struct skynet_context *, const char * parm); typedef void (*skynet_dl_release)(void * inst); typedef void (*skynet_dl_signal)(void * inst, int signal); struct skynet_module { const char * name; void * module; skynet_dl_create create; skynet_dl_init init; skynet_dl_release release; skynet_dl_signal signal; }; struct skynet_module * skynet_module_query(const char * name); void * skynet_module_instance_create(struct skynet_module *); int skynet_module_instance_init(struct skynet_module *, void * inst, struct skynet_context *ctx, const char * parm); void skynet_module_instance_release(struct skynet_module *, void *inst); void skynet_module_instance_signal(struct skynet_module *, void *inst, int signal); void skynet_module_init(const char *path); #endif ================================================ FILE: skynet-src/skynet_monitor.c ================================================ #include "skynet.h" #include "skynet_monitor.h" #include "skynet_server.h" #include "skynet.h" #include "atomic.h" #include #include struct skynet_monitor { ATOM_INT version; int check_version; uint32_t source; uint32_t destination; }; struct skynet_monitor * skynet_monitor_new() { struct skynet_monitor * ret = skynet_malloc(sizeof(*ret)); memset(ret, 0, sizeof(*ret)); return ret; } void skynet_monitor_delete(struct skynet_monitor *sm) { skynet_free(sm); } void skynet_monitor_trigger(struct skynet_monitor *sm, uint32_t source, uint32_t destination) { sm->source = source; sm->destination = destination; ATOM_FINC(&sm->version); } void skynet_monitor_check(struct skynet_monitor *sm) { if (sm->version == sm->check_version) { if (sm->destination) { skynet_context_endless(sm->destination); skynet_error(NULL, "error: A message from [ :%08x ] to [ :%08x ] maybe in an endless loop (version = %d)", sm->source , sm->destination, sm->version); } } else { sm->check_version = sm->version; } } ================================================ FILE: skynet-src/skynet_monitor.h ================================================ #ifndef SKYNET_MONITOR_H #define SKYNET_MONITOR_H #include struct skynet_monitor; struct skynet_monitor * skynet_monitor_new(); void skynet_monitor_delete(struct skynet_monitor *); void skynet_monitor_trigger(struct skynet_monitor *, uint32_t source, uint32_t destination); void skynet_monitor_check(struct skynet_monitor *); #endif ================================================ FILE: skynet-src/skynet_mq.c ================================================ #include "skynet.h" #include "skynet_mq.h" #include "skynet_handle.h" #include "spinlock.h" #include #include #include #include #include #define DEFAULT_QUEUE_SIZE 64 #define MAX_GLOBAL_MQ 0x10000 // 0 means mq is not in global mq. // 1 means mq is in global mq , or the message is dispatching. #define MQ_IN_GLOBAL 1 #define MQ_OVERLOAD 1024 struct message_queue { struct spinlock lock; uint32_t handle; int cap; int head; int tail; int release; int in_global; int overload; int overload_threshold; struct skynet_message *queue; struct message_queue *next; }; struct global_queue { struct message_queue *head; struct message_queue *tail; struct spinlock lock; }; static struct global_queue *Q = NULL; void skynet_globalmq_push(struct message_queue * queue) { struct global_queue *q= Q; SPIN_LOCK(q) assert(queue->next == NULL); if(q->tail) { q->tail->next = queue; q->tail = queue; } else { q->head = q->tail = queue; } SPIN_UNLOCK(q) } struct message_queue * skynet_globalmq_pop() { struct global_queue *q = Q; SPIN_LOCK(q) struct message_queue *mq = q->head; if(mq) { q->head = mq->next; if(q->head == NULL) { assert(mq == q->tail); q->tail = NULL; } mq->next = NULL; } SPIN_UNLOCK(q) return mq; } struct message_queue * skynet_mq_create(uint32_t handle) { struct message_queue *q = skynet_malloc(sizeof(*q)); q->handle = handle; q->cap = DEFAULT_QUEUE_SIZE; q->head = 0; q->tail = 0; SPIN_INIT(q) // When the queue is create (always between service create and service init) , // set in_global flag to avoid push it to global queue . // If the service init success, skynet_context_new will call skynet_mq_push to push it to global queue. q->in_global = MQ_IN_GLOBAL; q->release = 0; q->overload = 0; q->overload_threshold = MQ_OVERLOAD; q->queue = skynet_malloc(sizeof(struct skynet_message) * q->cap); q->next = NULL; return q; } static void _release(struct message_queue *q) { assert(q->next == NULL); SPIN_DESTROY(q) skynet_free(q->queue); skynet_free(q); } uint32_t skynet_mq_handle(struct message_queue *q) { return q->handle; } int skynet_mq_length(struct message_queue *q) { int head, tail,cap; SPIN_LOCK(q) head = q->head; tail = q->tail; cap = q->cap; SPIN_UNLOCK(q) if (head <= tail) { return tail - head; } return tail + cap - head; } int skynet_mq_overload(struct message_queue *q) { if (q->overload) { int overload = q->overload; q->overload = 0; return overload; } return 0; } int skynet_mq_pop(struct message_queue *q, struct skynet_message *message) { int ret = 1; SPIN_LOCK(q) if (q->head != q->tail) { *message = q->queue[q->head++]; ret = 0; int head = q->head; int tail = q->tail; int cap = q->cap; if (head >= cap) { q->head = head = 0; } int length = tail - head; if (length < 0) { length += cap; } while (length > q->overload_threshold) { q->overload = length; q->overload_threshold *= 2; } } else { // reset overload_threshold when queue is empty q->overload_threshold = MQ_OVERLOAD; } if (ret) { q->in_global = 0; } SPIN_UNLOCK(q) return ret; } static void expand_queue(struct message_queue *q) { struct skynet_message *new_queue = skynet_malloc(sizeof(struct skynet_message) * q->cap * 2); int i; for (i=0;icap;i++) { new_queue[i] = q->queue[(q->head + i) % q->cap]; } q->head = 0; q->tail = q->cap; q->cap *= 2; skynet_free(q->queue); q->queue = new_queue; } void skynet_mq_push(struct message_queue *q, struct skynet_message *message) { assert(message); SPIN_LOCK(q) q->queue[q->tail] = *message; if (++ q->tail >= q->cap) { q->tail = 0; } if (q->head == q->tail) { expand_queue(q); } if (q->in_global == 0) { q->in_global = MQ_IN_GLOBAL; skynet_globalmq_push(q); } SPIN_UNLOCK(q) } void skynet_mq_init() { struct global_queue *q = skynet_malloc(sizeof(*q)); memset(q,0,sizeof(*q)); SPIN_INIT(q); Q=q; } void skynet_mq_mark_release(struct message_queue *q) { SPIN_LOCK(q) assert(q->release == 0); q->release = 1; if (q->in_global != MQ_IN_GLOBAL) { skynet_globalmq_push(q); } SPIN_UNLOCK(q) } static void _drop_queue(struct message_queue *q, message_drop drop_func, void *ud) { struct skynet_message msg; while(!skynet_mq_pop(q, &msg)) { drop_func(&msg, ud); } _release(q); } void skynet_mq_release(struct message_queue *q, message_drop drop_func, void *ud) { SPIN_LOCK(q) if (q->release) { SPIN_UNLOCK(q) _drop_queue(q, drop_func, ud); } else { skynet_globalmq_push(q); SPIN_UNLOCK(q) } } ================================================ FILE: skynet-src/skynet_mq.h ================================================ #ifndef SKYNET_MESSAGE_QUEUE_H #define SKYNET_MESSAGE_QUEUE_H #include #include struct skynet_message { uint32_t source; int session; void * data; size_t sz; }; // type is encoding in skynet_message.sz high 8bit #define MESSAGE_TYPE_MASK (SIZE_MAX >> 8) #define MESSAGE_TYPE_SHIFT ((sizeof(size_t)-1) * 8) struct message_queue; void skynet_globalmq_push(struct message_queue * queue); struct message_queue * skynet_globalmq_pop(void); struct message_queue * skynet_mq_create(uint32_t handle); void skynet_mq_mark_release(struct message_queue *q); typedef void (*message_drop)(struct skynet_message *, void *); void skynet_mq_release(struct message_queue *q, message_drop drop_func, void *ud); uint32_t skynet_mq_handle(struct message_queue *); // 0 for success int skynet_mq_pop(struct message_queue *q, struct skynet_message *message); void skynet_mq_push(struct message_queue *q, struct skynet_message *message); // return the length of message queue, for debug int skynet_mq_length(struct message_queue *q); int skynet_mq_overload(struct message_queue *q); void skynet_mq_init(); #endif ================================================ FILE: skynet-src/skynet_server.c ================================================ #include "skynet.h" #include "skynet_server.h" #include "skynet_module.h" #include "skynet_handle.h" #include "skynet_mq.h" #include "skynet_timer.h" #include "skynet_harbor.h" #include "skynet_env.h" #include "skynet_monitor.h" #include "skynet_imp.h" #include "skynet_log.h" #include "spinlock.h" #include "atomic.h" #include #include #include #include #include #include #ifdef CALLING_CHECK #define CHECKCALLING_BEGIN(ctx) if (!(spinlock_trylock(&ctx->calling))) { assert(0); } #define CHECKCALLING_END(ctx) spinlock_unlock(&ctx->calling); #define CHECKCALLING_INIT(ctx) spinlock_init(&ctx->calling); #define CHECKCALLING_DESTROY(ctx) spinlock_destroy(&ctx->calling); #define CHECKCALLING_DECL struct spinlock calling; #else #define CHECKCALLING_BEGIN(ctx) #define CHECKCALLING_END(ctx) #define CHECKCALLING_INIT(ctx) #define CHECKCALLING_DESTROY(ctx) #define CHECKCALLING_DECL #endif struct skynet_context { void * instance; struct skynet_module * mod; void * cb_ud; skynet_cb cb; struct message_queue *queue; ATOM_POINTER logfile; uint64_t cpu_cost; // in microsec uint64_t cpu_start; // in microsec char result[32]; uint32_t handle; int session_id; ATOM_INT ref; size_t message_count; bool init; bool endless; bool profile; CHECKCALLING_DECL }; struct skynet_node { ATOM_INT total; int init; uint32_t monitor_exit; pthread_key_t handle_key; bool profile; // default is on }; static struct skynet_node G_NODE; int skynet_context_total() { return ATOM_LOAD(&G_NODE.total); } static void context_inc() { ATOM_FINC(&G_NODE.total); } static void context_dec() { ATOM_FDEC(&G_NODE.total); } uint32_t skynet_current_handle(void) { if (G_NODE.init) { void * handle = pthread_getspecific(G_NODE.handle_key); return (uint32_t)(uintptr_t)handle; } else { uint32_t v = (uint32_t)(-THREAD_MAIN); return v; } } static void id_to_hex(char * str, uint32_t id) { int i; static char hex[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; str[0] = ':'; for (i=0;i<8;i++) { str[i+1] = hex[(id >> ((7-i) * 4))&0xf]; } str[9] = '\0'; } struct drop_t { uint32_t handle; }; static void drop_message(struct skynet_message *msg, void *ud) { struct drop_t *d = ud; skynet_free(msg->data); uint32_t source = d->handle; assert(source); // report error to the message source skynet_send(NULL, source, msg->source, PTYPE_ERROR, msg->session, NULL, 0); } uint32_t skynet_context_new(const char * name, const char *param) { struct skynet_module * mod = skynet_module_query(name); if (mod == NULL) return 0; void *inst = skynet_module_instance_create(mod); if (inst == NULL) return 0; struct skynet_context * ctx = skynet_malloc(sizeof(*ctx)); CHECKCALLING_INIT(ctx) ctx->mod = mod; ctx->instance = inst; ATOM_INIT(&ctx->ref , 2); // skynet_handle_register + skynet_module_instance_init ctx->cb = NULL; ctx->cb_ud = NULL; ctx->session_id = 0; ATOM_INIT(&ctx->logfile, (uintptr_t)NULL); ctx->init = false; ctx->endless = false; ctx->cpu_cost = 0; ctx->cpu_start = 0; ctx->message_count = 0; ctx->profile = G_NODE.profile; // Should set to 0 first to avoid skynet_handle_retireall get an uninitialized handle ctx->handle = 0; const uint32_t handle = skynet_handle_register(ctx); ctx->handle = handle; struct message_queue * queue = ctx->queue = skynet_mq_create(handle); // init function maybe use ctx->handle, so it must init at last context_inc(); CHECKCALLING_BEGIN(ctx) int r = skynet_module_instance_init(mod, inst, ctx, param); CHECKCALLING_END(ctx) if (r == 0) { ctx->init = true; skynet_globalmq_push(queue); skynet_error(ctx, "LAUNCH %s %s", name, param ? param : ""); skynet_context_release(ctx); return handle; } else { skynet_error(ctx, "error: launch %s FAILED", name); uint32_t handle = ctx->handle; skynet_context_release(ctx); skynet_handle_retire(handle); struct drop_t d = { handle }; skynet_mq_release(queue, drop_message, &d); return 0; } } int skynet_context_newsession(struct skynet_context *ctx) { // session always be a positive number int session = ++ctx->session_id; if (session <= 0) { ctx->session_id = 1; return 1; } return session; } void skynet_context_grab(struct skynet_context *ctx) { ATOM_FINC(&ctx->ref); } void skynet_context_reserve(struct skynet_context *ctx) { skynet_context_grab(ctx); // don't count the context reserved, because skynet abort (the worker threads terminate) only when the total context is 0 . // the reserved context will be release at last. context_dec(); } static void delete_context(struct skynet_context *ctx) { FILE *f = (FILE *)ATOM_LOAD(&ctx->logfile); if (f) { fclose(f); } skynet_module_instance_release(ctx->mod, ctx->instance); skynet_mq_mark_release(ctx->queue); CHECKCALLING_DESTROY(ctx) skynet_free(ctx); context_dec(); } void skynet_context_release(struct skynet_context *ctx) { if (ATOM_FDEC(&ctx->ref) == 1) { delete_context(ctx); } } int skynet_context_push(uint32_t handle, struct skynet_message *message) { struct skynet_context * ctx = skynet_handle_grab(handle); if (ctx == NULL) { return -1; } skynet_mq_push(ctx->queue, message); skynet_context_release(ctx); return 0; } void skynet_context_endless(uint32_t handle) { struct skynet_context * ctx = skynet_handle_grab(handle); if (ctx == NULL) { return; } ctx->endless = true; skynet_context_release(ctx); } int skynet_isremote(struct skynet_context * ctx, uint32_t handle, int * harbor) { int ret = skynet_harbor_message_isremote(handle); if (harbor) { *harbor = (int)(handle >> HANDLE_REMOTE_SHIFT); } return ret; } static void dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) { assert(ctx->init); CHECKCALLING_BEGIN(ctx) pthread_setspecific(G_NODE.handle_key, (void *)(uintptr_t)(ctx->handle)); int type = msg->sz >> MESSAGE_TYPE_SHIFT; size_t sz = msg->sz & MESSAGE_TYPE_MASK; FILE *f = (FILE *)ATOM_LOAD(&ctx->logfile); if (f) { skynet_log_output(f, msg->source, type, msg->session, msg->data, sz); } ++ctx->message_count; int reserve_msg; if (ctx->profile) { ctx->cpu_start = skynet_thread_time(); reserve_msg = ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz); uint64_t cost_time = skynet_thread_time() - ctx->cpu_start; ctx->cpu_cost += cost_time; } else { reserve_msg = ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz); } if (!reserve_msg) { skynet_free(msg->data); } CHECKCALLING_END(ctx) } void skynet_context_dispatchall(struct skynet_context * ctx) { // for skynet_error struct skynet_message msg; struct message_queue *q = ctx->queue; while (!skynet_mq_pop(q,&msg)) { dispatch_message(ctx, &msg); } } struct message_queue * skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue *q, int weight) { if (q == NULL) { q = skynet_globalmq_pop(); if (q==NULL) return NULL; } uint32_t handle = skynet_mq_handle(q); struct skynet_context * ctx = skynet_handle_grab(handle); if (ctx == NULL) { struct drop_t d = { handle }; skynet_mq_release(q, drop_message, &d); return skynet_globalmq_pop(); } int i,n=1; struct skynet_message msg; for (i=0;i= 0) { n = skynet_mq_length(q); n >>= weight; } int overload = skynet_mq_overload(q); if (overload) { skynet_error(ctx, "error: May overload, message queue length = %d", overload); } skynet_monitor_trigger(sm, msg.source , handle); if (ctx->cb == NULL) { skynet_free(msg.data); } else { dispatch_message(ctx, &msg); } skynet_monitor_trigger(sm, 0,0); } assert(q == ctx->queue); struct message_queue *nq = skynet_globalmq_pop(); if (nq) { // If global mq is not empty , push q back, and return next queue (nq) // Else (global mq is empty or block, don't push q back, and return q again (for next dispatch) skynet_globalmq_push(q); q = nq; } skynet_context_release(ctx); return q; } static void copy_name(char name[GLOBALNAME_LENGTH], const char * addr) { int i; for (i=0;ihandle; skynet_error(context, "KILL self"); } else { skynet_error(context, "KILL :%0x", handle); } if (G_NODE.monitor_exit) { skynet_send(context, handle, G_NODE.monitor_exit, PTYPE_CLIENT, 0, NULL, 0); } skynet_handle_retire(handle); } // skynet command struct command_func { const char *name; const char * (*func)(struct skynet_context * context, const char * param); }; static const char * cmd_timeout(struct skynet_context * context, const char * param) { char * session_ptr = NULL; int ti = strtol(param, &session_ptr, 10); int session = skynet_context_newsession(context); skynet_timeout(context->handle, ti, session); sprintf(context->result, "%d", session); return context->result; } static const char * cmd_reg(struct skynet_context * context, const char * param) { if (param == NULL || param[0] == '\0') { sprintf(context->result, ":%x", context->handle); return context->result; } else if (param[0] == '.') { return skynet_handle_namehandle(context->handle, param + 1); } else { skynet_error(context, "error: Can't register global name %s in C", param); return NULL; } } static const char * cmd_query(struct skynet_context * context, const char * param) { if (param[0] == '.') { uint32_t handle = skynet_handle_findname(param+1); if (handle) { sprintf(context->result, ":%x", handle); return context->result; } } return NULL; } static const char * cmd_name(struct skynet_context * context, const char * param) { int size = strlen(param); char name[size+1]; char handle[size+1]; sscanf(param,"%s %s",name,handle); if (handle[0] != ':') { return NULL; } uint32_t handle_id = strtoul(handle+1, NULL, 16); if (handle_id == 0) { return NULL; } if (name[0] == '.') { return skynet_handle_namehandle(handle_id, name + 1); } else { skynet_error(context, "error: Can't set global name %s in C", name); } return NULL; } static const char * cmd_exit(struct skynet_context * context, const char * param) { handle_exit(context, 0); return NULL; } static uint32_t tohandle(struct skynet_context * context, const char * param) { uint32_t handle = 0; if (param[0] == ':') { handle = strtoul(param+1, NULL, 16); } else if (param[0] == '.') { handle = skynet_handle_findname(param+1); } else { skynet_error(context, "error: Can't convert %s to handle",param); } return handle; } static const char * cmd_kill(struct skynet_context * context, const char * param) { uint32_t handle = tohandle(context, param); if (handle) { handle_exit(context, handle); } return NULL; } static const char * cmd_launch(struct skynet_context * context, const char * param) { size_t sz = strlen(param); char tmp[sz+1]; strcpy(tmp,param); char * args = tmp; char * mod = strsep(&args, " \t\r\n"); args = strsep(&args, "\r\n"); const uint32_t handle = skynet_context_new(mod,args); if (handle == 0) { return NULL; } else { id_to_hex(context->result, handle); return context->result; } } static const char * cmd_getenv(struct skynet_context * context, const char * param) { return skynet_getenv(param); } static const char * cmd_setenv(struct skynet_context * context, const char * param) { size_t sz = strlen(param); char key[sz+1]; int i; for (i=0;param[i] != ' ' && param[i];i++) { key[i] = param[i]; } if (param[i] == '\0') return NULL; key[i] = '\0'; param += i+1; skynet_setenv(key,param); return NULL; } static const char * cmd_starttime(struct skynet_context * context, const char * param) { uint32_t sec = skynet_starttime(); sprintf(context->result,"%u",sec); return context->result; } static const char * cmd_abort(struct skynet_context * context, const char * param) { skynet_handle_retireall(); return NULL; } static const char * cmd_monitor(struct skynet_context * context, const char * param) { uint32_t handle=0; if (param == NULL || param[0] == '\0') { if (G_NODE.monitor_exit) { // return current monitor serivce sprintf(context->result, ":%x", G_NODE.monitor_exit); return context->result; } return NULL; } else { handle = tohandle(context, param); } G_NODE.monitor_exit = handle; return NULL; } static const char * cmd_stat(struct skynet_context * context, const char * param) { if (strcmp(param, "mqlen") == 0) { int len = skynet_mq_length(context->queue); sprintf(context->result, "%d", len); } else if (strcmp(param, "endless") == 0) { if (context->endless) { strcpy(context->result, "1"); context->endless = false; } else { strcpy(context->result, "0"); } } else if (strcmp(param, "cpu") == 0) { double t = (double)context->cpu_cost / 1000000.0; // microsec sprintf(context->result, "%lf", t); } else if (strcmp(param, "time") == 0) { if (context->profile) { uint64_t ti = skynet_thread_time() - context->cpu_start; double t = (double)ti / 1000000.0; // microsec sprintf(context->result, "%lf", t); } else { strcpy(context->result, "0"); } } else if (strcmp(param, "message") == 0) { sprintf(context->result, "%zu", context->message_count); } else { context->result[0] = '\0'; } return context->result; } static const char * cmd_logon(struct skynet_context * context, const char * param) { uint32_t handle = tohandle(context, param); if (handle == 0) return NULL; struct skynet_context * ctx = skynet_handle_grab(handle); if (ctx == NULL) return NULL; FILE *f = NULL; FILE * lastf = (FILE *)ATOM_LOAD(&ctx->logfile); if (lastf == NULL) { f = skynet_log_open(context, handle); if (f) { if (!ATOM_CAS_POINTER(&ctx->logfile, 0, (uintptr_t)f)) { // logfile opens in other thread, close this one. fclose(f); } } } skynet_context_release(ctx); return NULL; } static const char * cmd_logoff(struct skynet_context * context, const char * param) { uint32_t handle = tohandle(context, param); if (handle == 0) return NULL; struct skynet_context * ctx = skynet_handle_grab(handle); if (ctx == NULL) return NULL; FILE * f = (FILE *)ATOM_LOAD(&ctx->logfile); if (f) { // logfile may close in other thread if (ATOM_CAS_POINTER(&ctx->logfile, (uintptr_t)f, (uintptr_t)NULL)) { skynet_log_close(context, f, handle); } } skynet_context_release(ctx); return NULL; } static const char * cmd_signal(struct skynet_context * context, const char * param) { uint32_t handle = tohandle(context, param); if (handle == 0) return NULL; struct skynet_context * ctx = skynet_handle_grab(handle); if (ctx == NULL) return NULL; param = strchr(param, ' '); int sig = 0; if (param) { sig = strtol(param, NULL, 0); } // NOTICE: the signal function should be thread safe. skynet_module_instance_signal(ctx->mod, ctx->instance, sig); skynet_context_release(ctx); return NULL; } static struct command_func cmd_funcs[] = { { "TIMEOUT", cmd_timeout }, { "REG", cmd_reg }, { "QUERY", cmd_query }, { "NAME", cmd_name }, { "EXIT", cmd_exit }, { "KILL", cmd_kill }, { "LAUNCH", cmd_launch }, { "GETENV", cmd_getenv }, { "SETENV", cmd_setenv }, { "STARTTIME", cmd_starttime }, { "ABORT", cmd_abort }, { "MONITOR", cmd_monitor }, { "STAT", cmd_stat }, { "LOGON", cmd_logon }, { "LOGOFF", cmd_logoff }, { "SIGNAL", cmd_signal }, { NULL, NULL }, }; const char * skynet_command(struct skynet_context * context, const char * cmd , const char * param) { struct command_func * method = &cmd_funcs[0]; while(method->name) { if (strcmp(cmd, method->name) == 0) { return method->func(context, param); } ++method; } return NULL; } static void _filter_args(struct skynet_context * context, int type, int *session, void ** data, size_t * sz) { int needcopy = !(type & PTYPE_TAG_DONTCOPY); int allocsession = type & PTYPE_TAG_ALLOCSESSION; type &= 0xff; if (allocsession) { assert(*session == 0); *session = skynet_context_newsession(context); } if (needcopy && *data) { char * msg = skynet_malloc(*sz+1); memcpy(msg, *data, *sz); msg[*sz] = '\0'; *data = msg; } *sz |= (size_t)type << MESSAGE_TYPE_SHIFT; } int skynet_send(struct skynet_context * context, uint32_t source, uint32_t destination , int type, int session, void * data, size_t sz) { if ((sz & MESSAGE_TYPE_MASK) != sz) { skynet_error(context, "error: The message to %x is too large", destination); if (type & PTYPE_TAG_DONTCOPY) { skynet_free(data); } return -2; } _filter_args(context, type, &session, (void **)&data, &sz); if (source == 0) { source = context->handle; } if (destination == 0) { if (data) { skynet_error(context, "error: Destination address can't be 0"); skynet_free(data); return -1; } return session; } if (skynet_harbor_message_isremote(destination)) { struct remote_message * rmsg = skynet_malloc(sizeof(*rmsg)); rmsg->destination.handle = destination; rmsg->message = data; rmsg->sz = sz & MESSAGE_TYPE_MASK; rmsg->type = sz >> MESSAGE_TYPE_SHIFT; skynet_harbor_send(rmsg, source, session); } else { struct skynet_message smsg; smsg.source = source; smsg.session = session; smsg.data = data; smsg.sz = sz; if (skynet_context_push(destination, &smsg)) { skynet_free(data); return -1; } } return session; } int skynet_sendname(struct skynet_context * context, uint32_t source, const char * addr , int type, int session, void * data, size_t sz) { if (source == 0) { source = context->handle; } uint32_t des = 0; if (addr[0] == ':') { des = strtoul(addr+1, NULL, 16); } else if (addr[0] == '.') { des = skynet_handle_findname(addr + 1); if (des == 0) { if (type & PTYPE_TAG_DONTCOPY) { skynet_free(data); } return -1; } } else { if ((sz & MESSAGE_TYPE_MASK) != sz) { skynet_error(context, "error: The message to %s is too large", addr); if (type & PTYPE_TAG_DONTCOPY) { skynet_free(data); } return -2; } _filter_args(context, type, &session, (void **)&data, &sz); struct remote_message * rmsg = skynet_malloc(sizeof(*rmsg)); copy_name(rmsg->destination.name, addr); rmsg->destination.handle = 0; rmsg->message = data; rmsg->sz = sz & MESSAGE_TYPE_MASK; rmsg->type = sz >> MESSAGE_TYPE_SHIFT; skynet_harbor_send(rmsg, source, session); return session; } return skynet_send(context, source, des, type, session, data, sz); } uint32_t skynet_context_handle(struct skynet_context *ctx) { return ctx->handle; } void skynet_callback(struct skynet_context * context, void *ud, skynet_cb cb) { context->cb = cb; context->cb_ud = ud; } void skynet_context_send(struct skynet_context * ctx, void * msg, size_t sz, uint32_t source, int type, int session) { struct skynet_message smsg; smsg.source = source; smsg.session = session; smsg.data = msg; smsg.sz = sz | (size_t)type << MESSAGE_TYPE_SHIFT; skynet_mq_push(ctx->queue, &smsg); } void skynet_globalinit(void) { ATOM_INIT(&G_NODE.total , 0); G_NODE.monitor_exit = 0; G_NODE.init = 1; if (pthread_key_create(&G_NODE.handle_key, NULL)) { fprintf(stderr, "pthread_key_create failed"); exit(1); } // set mainthread's key skynet_initthread(THREAD_MAIN); } void skynet_globalexit(void) { pthread_key_delete(G_NODE.handle_key); } void skynet_initthread(int m) { uintptr_t v = (uint32_t)(-m); pthread_setspecific(G_NODE.handle_key, (void *)v); } void skynet_profile_enable(int enable) { G_NODE.profile = (bool)enable; } ================================================ FILE: skynet-src/skynet_server.h ================================================ #ifndef SKYNET_SERVER_H #define SKYNET_SERVER_H #include #include struct skynet_context; struct skynet_message; struct skynet_monitor; uint32_t skynet_context_new(const char * name, const char * parm); void skynet_context_grab(struct skynet_context *); void skynet_context_reserve(struct skynet_context *ctx); void skynet_context_release(struct skynet_context *); uint32_t skynet_context_handle(struct skynet_context *); int skynet_context_push(uint32_t handle, struct skynet_message *message); void skynet_context_send(struct skynet_context * context, void * msg, size_t sz, uint32_t source, int type, int session); int skynet_context_newsession(struct skynet_context *); struct message_queue * skynet_context_message_dispatch(struct skynet_monitor *, struct message_queue *, int weight); // return next queue int skynet_context_total(); void skynet_context_dispatchall(struct skynet_context * context); // for skynet_error output before exit void skynet_context_endless(uint32_t handle); // for monitor void skynet_globalinit(void); void skynet_globalexit(void); void skynet_initthread(int m); void skynet_profile_enable(int enable); #endif ================================================ FILE: skynet-src/skynet_socket.c ================================================ #include "skynet.h" #include "skynet_socket.h" #include "socket_server.h" #include "skynet_server.h" #include "skynet_mq.h" #include "skynet_harbor.h" #include #include #include #include static struct socket_server * SOCKET_SERVER = NULL; void skynet_socket_init() { SOCKET_SERVER = socket_server_create(skynet_now()); } void skynet_socket_exit() { socket_server_exit(SOCKET_SERVER); } void skynet_socket_free() { socket_server_release(SOCKET_SERVER); SOCKET_SERVER = NULL; } void skynet_socket_updatetime() { socket_server_updatetime(SOCKET_SERVER, skynet_now()); } // mainloop thread static void forward_message(int type, bool padding, struct socket_message * result) { struct skynet_socket_message *sm; size_t sz = sizeof(*sm); if (padding) { if (result->data) { size_t msg_sz = strlen(result->data); if (msg_sz > 128) { msg_sz = 128; } sz += msg_sz; } else { result->data = ""; } } sm = (struct skynet_socket_message *)skynet_malloc(sz); sm->type = type; sm->id = result->id; sm->ud = result->ud; if (padding) { sm->buffer = NULL; memcpy(sm+1, result->data, sz - sizeof(*sm)); } else { sm->buffer = result->data; } struct skynet_message message; message.source = 0; message.session = 0; message.data = sm; message.sz = sz | ((size_t)PTYPE_SOCKET << MESSAGE_TYPE_SHIFT); if (skynet_context_push((uint32_t)result->opaque, &message)) { // todo: report somewhere to close socket // don't call skynet_socket_close here (It will block mainloop) skynet_free(sm->buffer); skynet_free(sm); } } int skynet_socket_poll() { struct socket_server *ss = SOCKET_SERVER; assert(ss); struct socket_message result; int more = 1; int type = socket_server_poll(ss, &result, &more); switch (type) { case SOCKET_EXIT: return 0; case SOCKET_DATA: forward_message(SKYNET_SOCKET_TYPE_DATA, false, &result); break; case SOCKET_CLOSE: forward_message(SKYNET_SOCKET_TYPE_CLOSE, false, &result); break; case SOCKET_OPEN: forward_message(SKYNET_SOCKET_TYPE_CONNECT, true, &result); break; case SOCKET_ERR: forward_message(SKYNET_SOCKET_TYPE_ERROR, true, &result); break; case SOCKET_ACCEPT: forward_message(SKYNET_SOCKET_TYPE_ACCEPT, true, &result); break; case SOCKET_UDP: forward_message(SKYNET_SOCKET_TYPE_UDP, false, &result); break; case SOCKET_WARNING: forward_message(SKYNET_SOCKET_TYPE_WARNING, false, &result); break; default: skynet_error(NULL, "error: Unknown socket message type %d.",type); return -1; } if (more) { return -1; } return 1; } int skynet_socket_sendbuffer(struct skynet_context *ctx, struct socket_sendbuffer *buffer) { return socket_server_send(SOCKET_SERVER, buffer); } int skynet_socket_sendbuffer_lowpriority(struct skynet_context *ctx, struct socket_sendbuffer *buffer) { return socket_server_send_lowpriority(SOCKET_SERVER, buffer); } int skynet_socket_listen(struct skynet_context *ctx, const char *host, int port, int backlog) { uint32_t source = skynet_context_handle(ctx); return socket_server_listen(SOCKET_SERVER, source, host, port, backlog); } int skynet_socket_connect(struct skynet_context *ctx, const char *host, int port) { uint32_t source = skynet_context_handle(ctx); return socket_server_connect(SOCKET_SERVER, source, host, port); } int skynet_socket_bind(struct skynet_context *ctx, int fd) { uint32_t source = skynet_context_handle(ctx); return socket_server_bind(SOCKET_SERVER, source, fd); } void skynet_socket_close(struct skynet_context *ctx, int id) { uint32_t source = skynet_context_handle(ctx); socket_server_close(SOCKET_SERVER, source, id); } void skynet_socket_shutdown(struct skynet_context *ctx, int id) { uint32_t source = skynet_context_handle(ctx); socket_server_shutdown(SOCKET_SERVER, source, id); } void skynet_socket_start(struct skynet_context *ctx, int id) { uint32_t source = skynet_context_handle(ctx); socket_server_start(SOCKET_SERVER, source, id); } void skynet_socket_pause(struct skynet_context *ctx, int id) { uint32_t source = skynet_context_handle(ctx); socket_server_pause(SOCKET_SERVER, source, id); } void skynet_socket_nodelay(struct skynet_context *ctx, int id) { socket_server_nodelay(SOCKET_SERVER, id); } int skynet_socket_udp(struct skynet_context *ctx, const char * addr, int port) { uint32_t source = skynet_context_handle(ctx); return socket_server_udp(SOCKET_SERVER, source, addr, port); } int skynet_socket_udp_dial(struct skynet_context *ctx, const char * addr, int port){ uint32_t source = skynet_context_handle(ctx); return socket_server_udp_dial(SOCKET_SERVER, source, addr, port); } int skynet_socket_udp_listen(struct skynet_context *ctx, const char * addr, int port){ uint32_t source = skynet_context_handle(ctx); return socket_server_udp_listen(SOCKET_SERVER, source, addr, port); } int skynet_socket_udp_connect(struct skynet_context *ctx, int id, const char * addr, int port) { return socket_server_udp_connect(SOCKET_SERVER, id, addr, port); } int skynet_socket_udp_sendbuffer(struct skynet_context *ctx, const char * address, struct socket_sendbuffer *buffer) { return socket_server_udp_send(SOCKET_SERVER, (const struct socket_udp_address *)address, buffer); } const char * skynet_socket_udp_address(struct skynet_socket_message *msg, int *addrsz) { if (msg->type != SKYNET_SOCKET_TYPE_UDP) { return NULL; } struct socket_message sm; sm.id = msg->id; sm.opaque = 0; sm.ud = msg->ud; sm.data = msg->buffer; return (const char *)socket_server_udp_address(SOCKET_SERVER, &sm, addrsz); } struct socket_info * skynet_socket_info() { return socket_server_info(SOCKET_SERVER); } ================================================ FILE: skynet-src/skynet_socket.h ================================================ #ifndef skynet_socket_h #define skynet_socket_h #include "socket_info.h" #include "socket_buffer.h" struct skynet_context; #define SKYNET_SOCKET_TYPE_DATA 1 #define SKYNET_SOCKET_TYPE_CONNECT 2 #define SKYNET_SOCKET_TYPE_CLOSE 3 #define SKYNET_SOCKET_TYPE_ACCEPT 4 #define SKYNET_SOCKET_TYPE_ERROR 5 #define SKYNET_SOCKET_TYPE_UDP 6 #define SKYNET_SOCKET_TYPE_WARNING 7 struct skynet_socket_message { int type; int id; int ud; char * buffer; }; void skynet_socket_init(); void skynet_socket_exit(); void skynet_socket_free(); int skynet_socket_poll(); void skynet_socket_updatetime(); int skynet_socket_sendbuffer(struct skynet_context *ctx, struct socket_sendbuffer *buffer); int skynet_socket_sendbuffer_lowpriority(struct skynet_context *ctx, struct socket_sendbuffer *buffer); int skynet_socket_listen(struct skynet_context *ctx, const char *host, int port, int backlog); int skynet_socket_connect(struct skynet_context *ctx, const char *host, int port); int skynet_socket_bind(struct skynet_context *ctx, int fd); void skynet_socket_close(struct skynet_context *ctx, int id); void skynet_socket_shutdown(struct skynet_context *ctx, int id); void skynet_socket_start(struct skynet_context *ctx, int id); void skynet_socket_pause(struct skynet_context *ctx, int id); void skynet_socket_nodelay(struct skynet_context *ctx, int id); int skynet_socket_udp(struct skynet_context *ctx, const char * addr, int port); int skynet_socket_udp_connect(struct skynet_context *ctx, int id, const char * addr, int port); int skynet_socket_udp_dial(struct skynet_context *ctx, const char * addr, int port); int skynet_socket_udp_listen(struct skynet_context *ctx, const char * addr, int port); int skynet_socket_udp_sendbuffer(struct skynet_context *ctx, const char * address, struct socket_sendbuffer *buffer); const char * skynet_socket_udp_address(struct skynet_socket_message *, int *addrsz); struct socket_info * skynet_socket_info(); // legacy APIs static inline void sendbuffer_init_(struct socket_sendbuffer *buf, int id, const void *buffer, int sz) { buf->id = id; buf->buffer = buffer; if (sz < 0) { buf->type = SOCKET_BUFFER_OBJECT; } else { buf->type = SOCKET_BUFFER_MEMORY; } buf->sz = (size_t)sz; } static inline int skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz) { struct socket_sendbuffer tmp; sendbuffer_init_(&tmp, id, buffer, sz); return skynet_socket_sendbuffer(ctx, &tmp); } static inline int skynet_socket_send_lowpriority(struct skynet_context *ctx, int id, void *buffer, int sz) { struct socket_sendbuffer tmp; sendbuffer_init_(&tmp, id, buffer, sz); return skynet_socket_sendbuffer_lowpriority(ctx, &tmp); } static inline int skynet_socket_udp_send(struct skynet_context *ctx, int id, const char * address, const void *buffer, int sz) { struct socket_sendbuffer tmp; sendbuffer_init_(&tmp, id, buffer, sz); return skynet_socket_udp_sendbuffer(ctx, address, &tmp); } #endif ================================================ FILE: skynet-src/skynet_start.c ================================================ #include "skynet.h" #include "skynet_server.h" #include "skynet_imp.h" #include "skynet_mq.h" #include "skynet_handle.h" #include "skynet_module.h" #include "skynet_timer.h" #include "skynet_monitor.h" #include "skynet_socket.h" #include "skynet_daemon.h" #include "skynet_harbor.h" #include #include #include #include #include #include #include struct monitor { int count; struct skynet_monitor ** m; pthread_cond_t cond; pthread_mutex_t mutex; int sleep; int quit; }; struct worker_parm { struct monitor *m; int id; int weight; }; static volatile int SIG = 0; static void handle_hup(int signal) { if (signal == SIGHUP) { SIG = 1; } } #define CHECK_ABORT if (skynet_context_total()==0) break; static void create_thread(pthread_t *thread, void *(*start_routine) (void *), void *arg) { if (pthread_create(thread,NULL, start_routine, arg)) { fprintf(stderr, "Create thread failed"); exit(1); } } static void wakeup(struct monitor *m, int busy) { if (m->sleep >= m->count - busy) { // signal sleep worker, "spurious wakeup" is harmless pthread_cond_signal(&m->cond); } } static void * thread_socket(void *p) { struct monitor * m = p; skynet_initthread(THREAD_SOCKET); for (;;) { int r = skynet_socket_poll(); if (r==0) break; if (r<0) { CHECK_ABORT continue; } wakeup(m,0); } return NULL; } static void free_monitor(struct monitor *m) { int i; int n = m->count; for (i=0;im[i]); } pthread_mutex_destroy(&m->mutex); pthread_cond_destroy(&m->cond); skynet_free(m->m); skynet_free(m); } static void * thread_monitor(void *p) { struct monitor * m = p; int i; int n = m->count; skynet_initthread(THREAD_MONITOR); for (;;) { CHECK_ABORT for (i=0;im[i]); } for (i=0;i<5;i++) { CHECK_ABORT sleep(1); } } return NULL; } static void signal_hup() { // make log file reopen struct skynet_message smsg; smsg.source = 0; smsg.session = 0; smsg.data = NULL; smsg.sz = (size_t)PTYPE_SYSTEM << MESSAGE_TYPE_SHIFT; uint32_t logger = skynet_handle_findname("logger"); if (logger) { skynet_context_push(logger, &smsg); } } static void * thread_timer(void *p) { struct monitor * m = p; skynet_initthread(THREAD_TIMER); for (;;) { skynet_updatetime(); skynet_socket_updatetime(); CHECK_ABORT wakeup(m,m->count-1); usleep(2500); if (SIG) { signal_hup(); SIG = 0; } } // wakeup socket thread skynet_socket_exit(); // wakeup all worker thread pthread_mutex_lock(&m->mutex); m->quit = 1; pthread_cond_broadcast(&m->cond); pthread_mutex_unlock(&m->mutex); return NULL; } static void * thread_worker(void *p) { struct worker_parm *wp = p; int id = wp->id; int weight = wp->weight; struct monitor *m = wp->m; struct skynet_monitor *sm = m->m[id]; skynet_initthread(THREAD_WORKER); struct message_queue * q = NULL; while (!m->quit) { q = skynet_context_message_dispatch(sm, q, weight); if (q == NULL) { if (pthread_mutex_lock(&m->mutex) == 0) { ++ m->sleep; // "spurious wakeup" is harmless, // because skynet_context_message_dispatch() can be call at any time. if (!m->quit) pthread_cond_wait(&m->cond, &m->mutex); -- m->sleep; if (pthread_mutex_unlock(&m->mutex)) { fprintf(stderr, "unlock mutex error"); exit(1); } } } } return NULL; } static void start(int thread) { pthread_t pid[thread+3]; struct monitor *m = skynet_malloc(sizeof(*m)); memset(m, 0, sizeof(*m)); m->count = thread; m->sleep = 0; m->m = skynet_malloc(thread * sizeof(struct skynet_monitor *)); int i; for (i=0;im[i] = skynet_monitor_new(); } if (pthread_mutex_init(&m->mutex, NULL)) { fprintf(stderr, "Init mutex error"); exit(1); } if (pthread_cond_init(&m->cond, NULL)) { fprintf(stderr, "Init cond error"); exit(1); } create_thread(&pid[0], thread_monitor, m); create_thread(&pid[1], thread_timer, m); create_thread(&pid[2], thread_socket, m); static int weight[] = { -1, -1, -1, -1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, }; struct worker_parm wp[thread]; for (i=0;idaemon) { if (daemon_init(config->daemon)) { exit(1); } } skynet_harbor_init(config->harbor); skynet_handle_init(config->harbor); skynet_mq_init(); skynet_module_init(config->module_path); skynet_timer_init(); skynet_socket_init(); skynet_profile_enable(config->profile); const uint32_t logger_handle = skynet_context_new(config->logservice, config->logger); if (logger_handle == 0) { fprintf(stderr, "Can't launch %s service\n", config->logservice); exit(1); } skynet_handle_namehandle(logger_handle, "logger"); bootstrap(logger_handle, config->bootstrap); start(config->thread); // harbor_exit may call socket send, so it should exit before socket_free skynet_harbor_exit(); skynet_socket_free(); if (config->daemon) { daemon_exit(config->daemon); } } ================================================ FILE: skynet-src/skynet_timer.c ================================================ #include "skynet.h" #include "skynet_timer.h" #include "skynet_mq.h" #include "skynet_server.h" #include "skynet_handle.h" #include "spinlock.h" #include #include #include #include #include typedef void (*timer_execute_func)(void *ud,void *arg); #define TIME_NEAR_SHIFT 8 #define TIME_NEAR (1 << TIME_NEAR_SHIFT) #define TIME_LEVEL_SHIFT 6 #define TIME_LEVEL (1 << TIME_LEVEL_SHIFT) #define TIME_NEAR_MASK (TIME_NEAR-1) #define TIME_LEVEL_MASK (TIME_LEVEL-1) struct timer_event { uint32_t handle; int session; }; struct timer_node { struct timer_node *next; uint32_t expire; }; struct link_list { struct timer_node head; struct timer_node *tail; }; struct timer { struct link_list near[TIME_NEAR]; struct link_list t[4][TIME_LEVEL]; struct spinlock lock; uint32_t time; uint32_t starttime; uint64_t current; uint64_t current_point; }; static struct timer * TI = NULL; static inline struct timer_node * link_clear(struct link_list *list) { struct timer_node * ret = list->head.next; list->head.next = 0; list->tail = &(list->head); return ret; } static inline void link(struct link_list *list,struct timer_node *node) { list->tail->next = node; list->tail = node; node->next=0; } static void add_node(struct timer *T,struct timer_node *node) { uint32_t time=node->expire; uint32_t current_time=T->time; if ((time|TIME_NEAR_MASK)==(current_time|TIME_NEAR_MASK)) { link(&T->near[time&TIME_NEAR_MASK],node); } else { int i; uint32_t mask=TIME_NEAR << TIME_LEVEL_SHIFT; for (i=0;i<3;i++) { if ((time|(mask-1))==(current_time|(mask-1))) { break; } mask <<= TIME_LEVEL_SHIFT; } link(&T->t[i][((time>>(TIME_NEAR_SHIFT + i*TIME_LEVEL_SHIFT)) & TIME_LEVEL_MASK)],node); } } static void timer_add(struct timer *T,void *arg,size_t sz,int time) { struct timer_node *node = (struct timer_node *)skynet_malloc(sizeof(*node)+sz); memcpy(node+1,arg,sz); SPIN_LOCK(T); node->expire=time+T->time; add_node(T,node); SPIN_UNLOCK(T); } static void move_list(struct timer *T, int level, int idx) { struct timer_node *current = link_clear(&T->t[level][idx]); while (current) { struct timer_node *temp=current->next; add_node(T,current); current=temp; } } static void timer_shift(struct timer *T) { int mask = TIME_NEAR; uint32_t ct = ++T->time; if (ct == 0) { move_list(T, 3, 0); } else { uint32_t time = ct >> TIME_NEAR_SHIFT; int i=0; while ((ct & (mask-1))==0) { int idx=time & TIME_LEVEL_MASK; if (idx!=0) { move_list(T, i, idx); break; } mask <<= TIME_LEVEL_SHIFT; time >>= TIME_LEVEL_SHIFT; ++i; } } } static inline void dispatch_list(struct timer_node *current) { do { struct timer_event * event = (struct timer_event *)(current+1); struct skynet_message message; message.source = 0; message.session = event->session; message.data = NULL; message.sz = (size_t)PTYPE_RESPONSE << MESSAGE_TYPE_SHIFT; skynet_context_push(event->handle, &message); struct timer_node * temp = current; current=current->next; skynet_free(temp); } while (current); } static inline void timer_execute(struct timer *T) { int idx = T->time & TIME_NEAR_MASK; while (T->near[idx].head.next) { struct timer_node *current = link_clear(&T->near[idx]); SPIN_UNLOCK(T); // dispatch_list don't need lock T dispatch_list(current); SPIN_LOCK(T); } } static void timer_update(struct timer *T) { SPIN_LOCK(T); // try to dispatch timeout 0 (rare condition) timer_execute(T); // shift time first, and then dispatch timer message timer_shift(T); timer_execute(T); SPIN_UNLOCK(T); } static struct timer * timer_create_timer() { struct timer *r=(struct timer *)skynet_malloc(sizeof(struct timer)); memset(r,0,sizeof(*r)); int i,j; for (i=0;inear[i]); } for (i=0;i<4;i++) { for (j=0;jt[i][j]); } } SPIN_INIT(r) r->current = 0; return r; } int skynet_timeout(uint32_t handle, int time, int session) { if (time <= 0) { struct skynet_message message; message.source = 0; message.session = session; message.data = NULL; message.sz = (size_t)PTYPE_RESPONSE << MESSAGE_TYPE_SHIFT; if (skynet_context_push(handle, &message)) { return -1; } } else { struct timer_event event; event.handle = handle; event.session = session; timer_add(TI, &event, sizeof(event), time); } return session; } // centisecond: 1/100 second static void systime(uint32_t *sec, uint32_t *cs) { struct timespec ti; clock_gettime(CLOCK_REALTIME, &ti); *sec = (uint32_t)ti.tv_sec; *cs = (uint32_t)(ti.tv_nsec / 10000000); } static uint64_t gettime() { uint64_t t; struct timespec ti; clock_gettime(CLOCK_MONOTONIC, &ti); t = (uint64_t)ti.tv_sec * 100; t += ti.tv_nsec / 10000000; return t; } void skynet_updatetime(void) { uint64_t cp = gettime(); if(cp < TI->current_point) { skynet_error(NULL, "time diff error: change from %lld to %lld", cp, TI->current_point); TI->current_point = cp; } else if (cp != TI->current_point) { uint32_t diff = (uint32_t)(cp - TI->current_point); TI->current_point = cp; TI->current += diff; int i; for (i=0;istarttime; } uint64_t skynet_now(void) { return TI->current; } void skynet_timer_init(void) { TI = timer_create_timer(); uint32_t current = 0; systime(&TI->starttime, ¤t); TI->current = current; TI->current_point = gettime(); } // for profile #define NANOSEC 1000000000 #define MICROSEC 1000000 uint64_t skynet_thread_time(void) { struct timespec ti; clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ti); return (uint64_t)ti.tv_sec * MICROSEC + (uint64_t)ti.tv_nsec / (NANOSEC / MICROSEC); } ================================================ FILE: skynet-src/skynet_timer.h ================================================ #ifndef SKYNET_TIMER_H #define SKYNET_TIMER_H #include int skynet_timeout(uint32_t handle, int time, int session); void skynet_updatetime(void); uint32_t skynet_starttime(void); uint64_t skynet_thread_time(void); // for profile, in micro second void skynet_timer_init(void); #endif ================================================ FILE: skynet-src/socket_buffer.h ================================================ #ifndef socket_buffer_h #define socket_buffer_h #include #define SOCKET_BUFFER_MEMORY 0 #define SOCKET_BUFFER_OBJECT 1 #define SOCKET_BUFFER_RAWPOINTER 2 struct socket_sendbuffer { int id; int type; const void *buffer; size_t sz; }; #endif ================================================ FILE: skynet-src/socket_epoll.h ================================================ #ifndef poll_socket_epoll_h #define poll_socket_epoll_h #include #include #include #include #include #include #include #include static bool sp_invalid(int efd) { return efd == -1; } static int sp_create() { return epoll_create(1024); } static void sp_release(int efd) { close(efd); } static int sp_add(int efd, int sock, void *ud) { struct epoll_event ev; ev.events = EPOLLIN; ev.data.ptr = ud; if (epoll_ctl(efd, EPOLL_CTL_ADD, sock, &ev) == -1) { return 1; } return 0; } static void sp_del(int efd, int sock) { epoll_ctl(efd, EPOLL_CTL_DEL, sock , NULL); } static int sp_enable(int efd, int sock, void *ud, bool read_enable, bool write_enable) { struct epoll_event ev; ev.events = (read_enable ? EPOLLIN : 0) | (write_enable ? EPOLLOUT : 0); ev.data.ptr = ud; if (epoll_ctl(efd, EPOLL_CTL_MOD, sock, &ev) == -1) { return 1; } return 0; } static int sp_wait(int efd, struct event *e, int max) { struct epoll_event ev[max]; int n = epoll_wait(efd , ev, max, -1); int i; for (i=0;i struct socket_info { int id; int type; uint64_t opaque; uint64_t read; uint64_t write; uint64_t rtime; uint64_t wtime; int64_t wbuffer; uint8_t reading; uint8_t writing; char name[128]; struct socket_info *next; }; struct socket_info * socket_info_create(struct socket_info *last); void socket_info_release(struct socket_info *); #endif ================================================ FILE: skynet-src/socket_kqueue.h ================================================ #ifndef poll_socket_kqueue_h #define poll_socket_kqueue_h #include #include #include #include #include #include #include #include static bool sp_invalid(int kfd) { return kfd == -1; } static int sp_create() { return kqueue(); } static void sp_release(int kfd) { close(kfd); } static void sp_del(int kfd, int sock) { struct kevent ke; EV_SET(&ke, sock, EVFILT_READ, EV_DELETE, 0, 0, NULL); kevent(kfd, &ke, 1, NULL, 0, NULL); EV_SET(&ke, sock, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); kevent(kfd, &ke, 1, NULL, 0, NULL); } static int sp_add(int kfd, int sock, void *ud) { struct kevent ke; EV_SET(&ke, sock, EVFILT_READ, EV_ADD, 0, 0, ud); if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { return 1; } EV_SET(&ke, sock, EVFILT_WRITE, EV_ADD, 0, 0, ud); if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { EV_SET(&ke, sock, EVFILT_READ, EV_DELETE, 0, 0, NULL); kevent(kfd, &ke, 1, NULL, 0, NULL); return 1; } EV_SET(&ke, sock, EVFILT_WRITE, EV_DISABLE, 0, 0, ud); if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { sp_del(kfd, sock); return 1; } return 0; } static int sp_enable(int kfd, int sock, void *ud, bool read_enable, bool write_enable) { int ret = 0; struct kevent ke; EV_SET(&ke, sock, EVFILT_READ, read_enable ? EV_ENABLE : EV_DISABLE, 0, 0, ud); if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { ret |= 1; } EV_SET(&ke, sock, EVFILT_WRITE, write_enable ? EV_ENABLE : EV_DISABLE, 0, 0, ud); if (kevent(kfd, &ke, 1, NULL, 0, NULL) == -1 || ke.flags & EV_ERROR) { ret |= 1; } return ret; } static int sp_wait(int kfd, struct event *e, int max) { struct kevent ev[max]; int n = kevent(kfd, NULL, 0, ev, max, NULL); int i; for (i=0;i typedef int poll_fd; struct event { void * s; bool read; bool write; bool error; bool eof; }; static bool sp_invalid(poll_fd fd); static poll_fd sp_create(); static void sp_release(poll_fd fd); static int sp_add(poll_fd fd, int sock, void *ud); static void sp_del(poll_fd fd, int sock); static int sp_enable(poll_fd, int sock, void *ud, bool read_enable, bool write_enable); static int sp_wait(poll_fd, struct event *e, int max); static void sp_nonblocking(int sock); #ifdef __linux__ #include "socket_epoll.h" #endif #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined (__NetBSD__) #include "socket_kqueue.h" #endif #endif ================================================ FILE: skynet-src/socket_server.c ================================================ #include "skynet.h" #include "socket_server.h" #include "socket_poll.h" #include "atomic.h" #include "spinlock.h" #include #include #include #include #include #include #include #include #include #include #include #define MAX_INFO 128 // MAX_SOCKET will be 2^MAX_SOCKET_P #define MAX_SOCKET_P 16 #define MAX_EVENT 64 #define MIN_READ_BUFFER 64 #define SOCKET_TYPE_INVALID 0 #define SOCKET_TYPE_RESERVE 1 #define SOCKET_TYPE_PLISTEN 2 #define SOCKET_TYPE_LISTEN 3 #define SOCKET_TYPE_CONNECTING 4 #define SOCKET_TYPE_CONNECTED 5 #define SOCKET_TYPE_HALFCLOSE_READ 6 #define SOCKET_TYPE_HALFCLOSE_WRITE 7 #define SOCKET_TYPE_PACCEPT 8 #define SOCKET_TYPE_BIND 9 #define MAX_SOCKET (1<>MAX_SOCKET_P) & 0xffff) #define PROTOCOL_TCP 0 #define PROTOCOL_UDP 1 #define PROTOCOL_UDPv6 2 #define PROTOCOL_UNKNOWN 255 #define UDP_ADDRESS_SIZE 19 // ipv6 128bit + port 16bit + 1 byte type #define MAX_UDP_PACKAGE 65535 // EAGAIN and EWOULDBLOCK may be not the same value. #if (EAGAIN != EWOULDBLOCK) #define AGAIN_WOULDBLOCK EAGAIN : case EWOULDBLOCK #else #define AGAIN_WOULDBLOCK EAGAIN #endif #define WARNING_SIZE (1024*1024) #define USEROBJECT ((size_t)(-1)) struct write_buffer { struct write_buffer * next; const void *buffer; char *ptr; size_t sz; bool userobject; }; struct write_buffer_udp { struct write_buffer buffer; uint8_t udp_address[UDP_ADDRESS_SIZE]; }; struct wb_list { struct write_buffer * head; struct write_buffer * tail; }; struct socket_stat { uint64_t rtime; uint64_t wtime; uint64_t read; uint64_t write; }; struct socket { uintptr_t opaque; struct wb_list high; struct wb_list low; int64_t wb_size; struct socket_stat stat; ATOM_ULONG sending; int fd; int id; ATOM_INT type; uint8_t protocol; bool reading; bool writing; bool closing; ATOM_INT udpconnecting; int64_t warn_size; union { int size; uint8_t udp_address[UDP_ADDRESS_SIZE]; } p; struct spinlock dw_lock; int dw_offset; const void * dw_buffer; size_t dw_size; }; struct socket_server { volatile uint64_t time; int reserve_fd; // for EMFILE int recvctrl_fd; int sendctrl_fd; int checkctrl; poll_fd event_fd; ATOM_INT alloc_id; int event_n; int event_index; struct socket_object_interface soi; struct event ev[MAX_EVENT]; struct socket slot[MAX_SOCKET]; char buffer[MAX_INFO]; uint8_t udpbuffer[MAX_UDP_PACKAGE]; fd_set rfds; }; struct request_open { int id; int port; uintptr_t opaque; char host[1]; }; struct request_send { int id; size_t sz; const void * buffer; }; struct request_send_udp { struct request_send send; uint8_t address[UDP_ADDRESS_SIZE]; }; struct request_setudp { int id; uint8_t address[UDP_ADDRESS_SIZE]; }; struct request_close { int id; int shutdown; uintptr_t opaque; }; struct request_listen { int id; int fd; uintptr_t opaque; // char host[1]; }; struct request_bind { int id; int fd; uintptr_t opaque; }; struct request_resumepause { int id; uintptr_t opaque; }; struct request_setopt { int id; int what; int value; }; struct request_udp { int id; int fd; int family; uintptr_t opaque; }; struct request_dial_udp { int id; int fd; uintptr_t opaque; uint8_t address[UDP_ADDRESS_SIZE]; }; /* The first byte is TYPE R Resume socket S Pause socket B Bind socket L Listen socket K Close socket O Connect to (Open) X Exit socket thread W Enable write D Send package (high) P Send package (low) A Send UDP package C set udp address N client dial to UDP host port T Set opt U Create UDP socket */ struct request_package { uint8_t header[8]; // 6 bytes dummy union { char buffer[256]; struct request_open open; struct request_send send; struct request_send_udp send_udp; struct request_close close; struct request_listen listen; struct request_bind bind; struct request_resumepause resumepause; struct request_setopt setopt; struct request_udp udp; struct request_setudp set_udp; struct request_dial_udp dial_udp; } u; uint8_t dummy[256]; }; union sockaddr_all { struct sockaddr s; struct sockaddr_in v4; struct sockaddr_in6 v6; }; struct send_object { const void * buffer; size_t sz; void (*free_func)(void *); }; #define MALLOC skynet_malloc #define FREE skynet_free struct socket_lock { struct spinlock *lock; int count; }; static inline void socket_lock_init(struct socket *s, struct socket_lock *sl) { sl->lock = &s->dw_lock; sl->count = 0; } static inline void socket_lock(struct socket_lock *sl) { if (sl->count == 0) { spinlock_lock(sl->lock); } ++sl->count; } static inline int socket_trylock(struct socket_lock *sl) { if (sl->count == 0) { if (!spinlock_trylock(sl->lock)) return 0; // lock failed } ++sl->count; return 1; } static inline void socket_unlock(struct socket_lock *sl) { --sl->count; if (sl->count <= 0) { assert(sl->count == 0); spinlock_unlock(sl->lock); } } static inline int socket_invalid(struct socket *s, int id) { return (s->id != id || ATOM_LOAD(&s->type) == SOCKET_TYPE_INVALID); } static inline bool send_object_init(struct socket_server *ss, struct send_object *so, const void *object, size_t sz) { if (sz == USEROBJECT) { so->buffer = ss->soi.buffer(object); so->sz = ss->soi.size(object); so->free_func = ss->soi.free; return true; } else { so->buffer = object; so->sz = sz; so->free_func = FREE; return false; } } static void dummy_free(void *ptr) { (void)ptr; } static inline void send_object_init_from_sendbuffer(struct socket_server *ss, struct send_object *so, struct socket_sendbuffer *buf) { switch (buf->type) { case SOCKET_BUFFER_MEMORY: send_object_init(ss, so, buf->buffer, buf->sz); break; case SOCKET_BUFFER_OBJECT: send_object_init(ss, so, buf->buffer, USEROBJECT); break; case SOCKET_BUFFER_RAWPOINTER: so->buffer = buf->buffer; so->sz = buf->sz; so->free_func = dummy_free; break; default: // never get here so->buffer = NULL; so->sz = 0; so->free_func = NULL; break; } } static inline void write_buffer_free(struct socket_server *ss, struct write_buffer *wb) { if (wb->userobject) { ss->soi.free((void *)wb->buffer); } else { FREE((void *)wb->buffer); } FREE(wb); } static void socket_keepalive(int fd) { int keepalive = 1; setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive , sizeof(keepalive)); } static int reserve_id(struct socket_server *ss) { int i; for (i=0;ialloc_id))+1; if (id < 0) { id = ATOM_FAND(&(ss->alloc_id), 0x7fffffff) & 0x7fffffff; } struct socket *s = &ss->slot[HASH_ID(id)]; int type_invalid = ATOM_LOAD(&s->type); if (type_invalid == SOCKET_TYPE_INVALID) { if (ATOM_CAS(&s->type, type_invalid, SOCKET_TYPE_RESERVE)) { s->id = id; s->protocol = PROTOCOL_UNKNOWN; // socket_server_udp_connect may inc s->udpconncting directly (from other thread, before new_fd), // so reset it to 0 here rather than in new_fd. ATOM_INIT(&s->udpconnecting, 0); s->fd = -1; return id; } else { // retry --i; } } } return -1; } static inline void clear_wb_list(struct wb_list *list) { list->head = NULL; list->tail = NULL; } struct socket_server * socket_server_create(uint64_t time) { int i; int fd[2]; poll_fd efd = sp_create(); if (sp_invalid(efd)) { skynet_error(NULL, "socket-server error: create event pool failed."); return NULL; } if (pipe(fd)) { sp_release(efd); skynet_error(NULL, "socket-server error: create socket pair failed."); return NULL; } if (sp_add(efd, fd[0], NULL)) { // add recvctrl_fd to event poll skynet_error(NULL, "socket-server error: can't add server fd to event pool."); close(fd[0]); close(fd[1]); sp_release(efd); return NULL; } struct socket_server *ss = MALLOC(sizeof(*ss)); ss->time = time; ss->event_fd = efd; ss->recvctrl_fd = fd[0]; ss->sendctrl_fd = fd[1]; ss->checkctrl = 1; ss->reserve_fd = dup(1); // reserve an extra fd for EMFILE for (i=0;islot[i]; ATOM_INIT(&s->type, SOCKET_TYPE_INVALID); clear_wb_list(&s->high); clear_wb_list(&s->low); spinlock_init(&s->dw_lock); } ATOM_INIT(&ss->alloc_id , 0); ss->event_n = 0; ss->event_index = 0; memset(&ss->soi, 0, sizeof(ss->soi)); FD_ZERO(&ss->rfds); assert(ss->recvctrl_fd < FD_SETSIZE); return ss; } void socket_server_updatetime(struct socket_server *ss, uint64_t time) { ss->time = time; } static void free_wb_list(struct socket_server *ss, struct wb_list *list) { struct write_buffer *wb = list->head; while (wb) { struct write_buffer *tmp = wb; wb = wb->next; write_buffer_free(ss, tmp); } list->head = NULL; list->tail = NULL; } static void free_buffer(struct socket_server *ss, struct socket_sendbuffer *buf) { void *buffer = (void *)buf->buffer; switch (buf->type) { case SOCKET_BUFFER_MEMORY: FREE(buffer); break; case SOCKET_BUFFER_OBJECT: ss->soi.free(buffer); break; case SOCKET_BUFFER_RAWPOINTER: break; } } static const void * clone_buffer(struct socket_sendbuffer *buf, size_t *sz) { switch (buf->type) { case SOCKET_BUFFER_MEMORY: *sz = buf->sz; return buf->buffer; case SOCKET_BUFFER_OBJECT: *sz = USEROBJECT; return buf->buffer; case SOCKET_BUFFER_RAWPOINTER: // It's a raw pointer, we need make a copy *sz = buf->sz; void * tmp = MALLOC(*sz); memcpy(tmp, buf->buffer, *sz); return tmp; } // never get here *sz = 0; return NULL; } static void force_close(struct socket_server *ss, struct socket *s, struct socket_lock *l, struct socket_message *result) { result->id = s->id; result->ud = 0; result->data = NULL; result->opaque = s->opaque; uint8_t type = ATOM_LOAD(&s->type); if (type == SOCKET_TYPE_INVALID) { return; } assert(type != SOCKET_TYPE_RESERVE); free_wb_list(ss,&s->high); free_wb_list(ss,&s->low); sp_del(ss->event_fd, s->fd); socket_lock(l); if (type != SOCKET_TYPE_BIND) { if (close(s->fd) < 0) { perror("close socket:"); } } ATOM_STORE(&s->type, SOCKET_TYPE_INVALID); if (s->dw_buffer) { struct socket_sendbuffer tmp; tmp.buffer = s->dw_buffer; tmp.sz = s->dw_size; tmp.id = s->id; tmp.type = (tmp.sz == USEROBJECT) ? SOCKET_BUFFER_OBJECT : SOCKET_BUFFER_MEMORY; free_buffer(ss, &tmp); s->dw_buffer = NULL; } socket_unlock(l); } void socket_server_release(struct socket_server *ss) { int i; struct socket_message dummy; for (i=0;islot[i]; struct socket_lock l; socket_lock_init(s, &l); if (ATOM_LOAD(&s->type) != SOCKET_TYPE_RESERVE) { force_close(ss, s, &l, &dummy); } spinlock_destroy(&s->dw_lock); } close(ss->sendctrl_fd); close(ss->recvctrl_fd); sp_release(ss->event_fd); if (ss->reserve_fd >= 0) close(ss->reserve_fd); FREE(ss); } static inline void check_wb_list(struct wb_list *s) { assert(s->head == NULL); assert(s->tail == NULL); } static inline int enable_write(struct socket_server *ss, struct socket *s, bool enable) { if (s->writing != enable) { s->writing = enable; return sp_enable(ss->event_fd, s->fd, s, s->reading, enable); } return 0; } static inline int enable_read(struct socket_server *ss, struct socket *s, bool enable) { if (s->reading != enable) { s->reading = enable; return sp_enable(ss->event_fd, s->fd, s, enable, s->writing); } return 0; } static struct socket * new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, bool reading) { struct socket * s = &ss->slot[HASH_ID(id)]; assert(ATOM_LOAD(&s->type) == SOCKET_TYPE_RESERVE); if (sp_add(ss->event_fd, fd, s)) { ATOM_STORE(&s->type, SOCKET_TYPE_INVALID); return NULL; } s->id = id; s->fd = fd; s->reading = true; s->writing = false; s->closing = false; ATOM_INIT(&s->sending , ID_TAG16(id) << 16 | 0); s->protocol = protocol; s->p.size = MIN_READ_BUFFER; s->opaque = opaque; s->wb_size = 0; s->warn_size = 0; check_wb_list(&s->high); check_wb_list(&s->low); s->dw_buffer = NULL; s->dw_size = 0; memset(&s->stat, 0, sizeof(s->stat)); if (enable_read(ss, s, reading)) { ATOM_STORE(&s->type , SOCKET_TYPE_INVALID); return NULL; } return s; } static inline void stat_read(struct socket_server *ss, struct socket *s, int n) { s->stat.read += n; s->stat.rtime = ss->time; } static inline void stat_write(struct socket_server *ss, struct socket *s, int n) { s->stat.write += n; s->stat.wtime = ss->time; } // return -1 when connecting static int open_socket(struct socket_server *ss, struct request_open * request, struct socket_message *result) { int id = request->id; result->opaque = request->opaque; result->id = id; result->ud = 0; result->data = NULL; struct socket *ns; int status; struct addrinfo ai_hints; struct addrinfo *ai_list = NULL; struct addrinfo *ai_ptr = NULL; char port[16]; sprintf(port, "%d", request->port); memset(&ai_hints, 0, sizeof( ai_hints ) ); ai_hints.ai_family = AF_UNSPEC; ai_hints.ai_socktype = SOCK_STREAM; ai_hints.ai_protocol = IPPROTO_TCP; status = getaddrinfo( request->host, port, &ai_hints, &ai_list ); if ( status != 0 ) { result->data = (void *)gai_strerror(status); goto _failed_getaddrinfo; } int sock= -1; for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next ) { sock = socket( ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol ); if ( sock < 0 ) { continue; } socket_keepalive(sock); sp_nonblocking(sock); status = connect( sock, ai_ptr->ai_addr, ai_ptr->ai_addrlen); if ( status != 0 && errno != EINPROGRESS) { close(sock); sock = -1; continue; } break; } if (sock < 0) { result->data = strerror(errno); goto _failed; } ns = new_fd(ss, id, sock, PROTOCOL_TCP, request->opaque, true); if (ns == NULL) { result->data = "reach skynet socket number limit"; goto _failed; } if(status == 0) { ATOM_STORE(&ns->type , SOCKET_TYPE_CONNECTED); struct sockaddr * addr = ai_ptr->ai_addr; void * sin_addr = (ai_ptr->ai_family == AF_INET) ? (void*)&((struct sockaddr_in *)addr)->sin_addr : (void*)&((struct sockaddr_in6 *)addr)->sin6_addr; if (inet_ntop(ai_ptr->ai_family, sin_addr, ss->buffer, sizeof(ss->buffer))) { result->data = ss->buffer; } freeaddrinfo( ai_list ); return SOCKET_OPEN; } else { if (enable_write(ss, ns, true)) { result->data = "enable write failed"; goto _failed; } ATOM_STORE(&ns->type , SOCKET_TYPE_CONNECTING); } freeaddrinfo( ai_list ); return -1; _failed: if (sock >= 0) close(sock); freeaddrinfo( ai_list ); _failed_getaddrinfo: ATOM_STORE(&ss->slot[HASH_ID(id)].type, SOCKET_TYPE_INVALID); return SOCKET_ERR; } static int report_error(struct socket *s, struct socket_message *result, const char *err) { result->id = s->id; result->ud = 0; result->opaque = s->opaque; result->data = (char *)err; return SOCKET_ERR; } static int close_write(struct socket_server *ss, struct socket *s, struct socket_lock *l, struct socket_message *result) { if (s->closing) { force_close(ss,s,l,result); return SOCKET_RST; } else { int t = ATOM_LOAD(&s->type); if (t == SOCKET_TYPE_HALFCLOSE_READ) { // recv 0 before, ignore the error and close fd force_close(ss,s,l,result); return SOCKET_RST; } if (t == SOCKET_TYPE_HALFCLOSE_WRITE) { // already raise SOCKET_ERR return SOCKET_RST; } ATOM_STORE(&s->type, SOCKET_TYPE_HALFCLOSE_WRITE); shutdown(s->fd, SHUT_WR); enable_write(ss, s, false); return report_error(s, result, strerror(errno)); } } static int send_list_tcp(struct socket_server *ss, struct socket *s, struct wb_list *list, struct socket_lock *l, struct socket_message *result) { while (list->head) { struct write_buffer * tmp = list->head; for (;;) { ssize_t sz = write(s->fd, tmp->ptr, tmp->sz); if (sz < 0) { switch(errno) { case EINTR: continue; case AGAIN_WOULDBLOCK: return -1; } return close_write(ss, s, l, result); } stat_write(ss,s,(int)sz); s->wb_size -= sz; if (sz != tmp->sz) { tmp->ptr += sz; tmp->sz -= sz; return -1; } break; } list->head = tmp->next; write_buffer_free(ss,tmp); } list->tail = NULL; return -1; } static socklen_t udp_socket_address(struct socket *s, const uint8_t udp_address[UDP_ADDRESS_SIZE], union sockaddr_all *sa) { int type = (uint8_t)udp_address[0]; if (type != s->protocol) return 0; uint16_t port = 0; memcpy(&port, udp_address+1, sizeof(uint16_t)); switch (s->protocol) { case PROTOCOL_UDP: memset(&sa->v4, 0, sizeof(sa->v4)); sa->s.sa_family = AF_INET; sa->v4.sin_port = port; memcpy(&sa->v4.sin_addr, udp_address + 1 + sizeof(uint16_t), sizeof(sa->v4.sin_addr)); // ipv4 address is 32 bits return sizeof(sa->v4); case PROTOCOL_UDPv6: memset(&sa->v6, 0, sizeof(sa->v6)); sa->s.sa_family = AF_INET6; sa->v6.sin6_port = port; memcpy(&sa->v6.sin6_addr, udp_address + 1 + sizeof(uint16_t), sizeof(sa->v6.sin6_addr)); // ipv6 address is 128 bits return sizeof(sa->v6); } return 0; } static void drop_udp(struct socket_server *ss, struct socket *s, struct wb_list *list, struct write_buffer *tmp) { s->wb_size -= tmp->sz; list->head = tmp->next; if (list->head == NULL) list->tail = NULL; write_buffer_free(ss,tmp); } static int send_list_udp(struct socket_server *ss, struct socket *s, struct wb_list *list, struct socket_message *result) { while (list->head) { struct write_buffer * tmp = list->head; struct write_buffer_udp * udp = (struct write_buffer_udp *)tmp; union sockaddr_all sa; socklen_t sasz = udp_socket_address(s, udp->udp_address, &sa); if (sasz == 0) { skynet_error(NULL, "socket-server : udp (%d) error: type mismatch.", s->id); drop_udp(ss, s, list, tmp); return -1; } int err = sendto(s->fd, tmp->ptr, tmp->sz, 0, &sa.s, sasz); if (err < 0) { switch(errno) { case EINTR: case AGAIN_WOULDBLOCK: return -1; } skynet_error(NULL, "socket-server : udp (%d) sendto error %s.",s->id, strerror(errno)); drop_udp(ss, s, list, tmp); return -1; } stat_write(ss,s,tmp->sz); s->wb_size -= tmp->sz; list->head = tmp->next; write_buffer_free(ss,tmp); } list->tail = NULL; return -1; } static int send_list(struct socket_server *ss, struct socket *s, struct wb_list *list, struct socket_lock *l, struct socket_message *result) { if (s->protocol == PROTOCOL_TCP) { return send_list_tcp(ss, s, list, l, result); } else { return send_list_udp(ss, s, list, result); } } static inline int list_uncomplete(struct wb_list *s) { struct write_buffer *wb = s->head; if (wb == NULL) return 0; return (void *)wb->ptr != wb->buffer; } static void raise_uncomplete(struct socket * s) { struct wb_list *low = &s->low; struct write_buffer *tmp = low->head; low->head = tmp->next; if (low->head == NULL) { low->tail = NULL; } // move head of low list (tmp) to the empty high list struct wb_list *high = &s->high; assert(high->head == NULL); tmp->next = NULL; high->head = high->tail = tmp; } static inline int send_buffer_empty(struct socket *s) { return (s->high.head == NULL && s->low.head == NULL); } /* Each socket has two write buffer list, high priority and low priority. 1. send high list as far as possible. 2. If high list is empty, try to send low list. 3. If low list head is uncomplete (send a part before), move the head of low list to empty high list (call raise_uncomplete) . 4. If two lists are both empty, turn off the event. (call check_close) */ static int send_buffer_(struct socket_server *ss, struct socket *s, struct socket_lock *l, struct socket_message *result) { assert(!list_uncomplete(&s->low)); // step 1 int ret = send_list(ss,s,&s->high,l,result); if (ret != -1) { if (ret == SOCKET_ERR) { // HALFCLOSE_WRITE return SOCKET_ERR; } // SOCKET_RST (ignore) return -1; } if (s->high.head == NULL) { // step 2 if (s->low.head != NULL) { int ret = send_list(ss,s,&s->low,l,result); if (ret != -1) { if (ret == SOCKET_ERR) { // HALFCLOSE_WRITE return SOCKET_ERR; } // SOCKET_RST (ignore) return -1; } // step 3 if (list_uncomplete(&s->low)) { raise_uncomplete(s); return -1; } if (s->low.head) return -1; } // step 4 assert(send_buffer_empty(s) && s->wb_size == 0); if (s->closing) { // finish writing force_close(ss, s, l, result); return -1; } int err = enable_write(ss, s, false); if (err) { return report_error(s, result, "disable write failed"); } if(s->warn_size > 0){ s->warn_size = 0; result->opaque = s->opaque; result->id = s->id; result->ud = 0; result->data = NULL; return SOCKET_WARNING; } } return -1; } static int send_buffer(struct socket_server *ss, struct socket *s, struct socket_lock *l, struct socket_message *result) { if (!socket_trylock(l)) return -1; // blocked by direct write, send later. if (s->dw_buffer) { // add direct write buffer before high.head struct write_buffer * buf = MALLOC(sizeof(*buf)); struct send_object so; buf->userobject = send_object_init(ss, &so, (void *)s->dw_buffer, s->dw_size); buf->ptr = (char*)so.buffer+s->dw_offset; buf->sz = so.sz - s->dw_offset; buf->buffer = (void *)s->dw_buffer; s->wb_size+=buf->sz; if (s->high.head == NULL) { s->high.head = s->high.tail = buf; buf->next = NULL; } else { buf->next = s->high.head; s->high.head = buf; } s->dw_buffer = NULL; } int r = send_buffer_(ss,s,l,result); socket_unlock(l); return r; } static struct write_buffer * append_sendbuffer_(struct socket_server *ss, struct wb_list *s, struct request_send * request, int size) { struct write_buffer * buf = MALLOC(size); struct send_object so; buf->userobject = send_object_init(ss, &so, request->buffer, request->sz); buf->ptr = (char*)so.buffer; buf->sz = so.sz; buf->buffer = request->buffer; buf->next = NULL; if (s->head == NULL) { s->head = s->tail = buf; } else { assert(s->tail != NULL); assert(s->tail->next == NULL); s->tail->next = buf; s->tail = buf; } return buf; } static inline void append_sendbuffer_udp(struct socket_server *ss, struct socket *s, int priority, struct request_send * request, const uint8_t udp_address[UDP_ADDRESS_SIZE]) { struct wb_list *wl = (priority == PRIORITY_HIGH) ? &s->high : &s->low; struct write_buffer_udp *buf = (struct write_buffer_udp *)append_sendbuffer_(ss, wl, request, sizeof(*buf)); memcpy(buf->udp_address, udp_address, UDP_ADDRESS_SIZE); s->wb_size += buf->buffer.sz; } static inline void append_sendbuffer(struct socket_server *ss, struct socket *s, struct request_send * request) { struct write_buffer *buf = append_sendbuffer_(ss, &s->high, request, sizeof(*buf)); s->wb_size += buf->sz; } static inline void append_sendbuffer_low(struct socket_server *ss,struct socket *s, struct request_send * request) { struct write_buffer *buf = append_sendbuffer_(ss, &s->low, request, sizeof(*buf)); s->wb_size += buf->sz; } static int trigger_write(struct socket_server *ss, struct request_send * request, struct socket_message *result) { int id = request->id; struct socket * s = &ss->slot[HASH_ID(id)]; if (socket_invalid(s, id)) return -1; if (enable_write(ss, s, true)) { return report_error(s, result, "enable write failed"); } return -1; } /* When send a package , we can assign the priority : PRIORITY_HIGH or PRIORITY_LOW If socket buffer is empty, write to fd directly. If write a part, append the rest part to high list. (Even priority is PRIORITY_LOW) Else append package to high (PRIORITY_HIGH) or low (PRIORITY_LOW) list. */ static int send_socket(struct socket_server *ss, struct request_send * request, struct socket_message *result, int priority, const uint8_t *udp_address) { int id = request->id; struct socket * s = &ss->slot[HASH_ID(id)]; struct send_object so; send_object_init(ss, &so, request->buffer, request->sz); uint8_t type = ATOM_LOAD(&s->type); if (type == SOCKET_TYPE_INVALID || s->id != id || type == SOCKET_TYPE_HALFCLOSE_WRITE || type == SOCKET_TYPE_PACCEPT || s->closing) { so.free_func((void *)request->buffer); return -1; } if (type == SOCKET_TYPE_PLISTEN || type == SOCKET_TYPE_LISTEN) { skynet_error(NULL, "socket-server error: write to listen fd %d.", id); so.free_func((void *)request->buffer); return -1; } if (send_buffer_empty(s)) { if (s->protocol == PROTOCOL_TCP) { append_sendbuffer(ss, s, request); // add to high priority list, even priority == PRIORITY_LOW } else { // udp if (udp_address == NULL) { udp_address = s->p.udp_address; } union sockaddr_all sa; socklen_t sasz = udp_socket_address(s, udp_address, &sa); if (sasz == 0) { // udp type mismatch, just drop it. skynet_error(NULL, "socket-server: udp socket (%d) error: type mismatch.", id); so.free_func((void *)request->buffer); return -1; } int n = sendto(s->fd, so.buffer, so.sz, 0, &sa.s, sasz); if (n != so.sz) { append_sendbuffer_udp(ss,s,priority,request,udp_address); } else { stat_write(ss,s,n); so.free_func((void *)request->buffer); return -1; } } if (enable_write(ss, s, true)) { return report_error(s, result, "enable write failed"); } } else { if (s->protocol == PROTOCOL_TCP) { if (priority == PRIORITY_LOW) { append_sendbuffer_low(ss, s, request); } else { append_sendbuffer(ss, s, request); } } else { if (udp_address == NULL) { udp_address = s->p.udp_address; } append_sendbuffer_udp(ss,s,priority,request,udp_address); } } if (s->wb_size >= WARNING_SIZE && s->wb_size >= s->warn_size) { s->warn_size = s->warn_size == 0 ? WARNING_SIZE *2 : s->warn_size*2; result->opaque = s->opaque; result->id = s->id; result->ud = s->wb_size%1024 == 0 ? s->wb_size/1024 : s->wb_size/1024 + 1; result->data = NULL; return SOCKET_WARNING; } return -1; } static int listen_socket(struct socket_server *ss, struct request_listen * request, struct socket_message *result) { int id = request->id; int listen_fd = request->fd; struct socket *s = new_fd(ss, id, listen_fd, PROTOCOL_TCP, request->opaque, false); if (s == NULL) { goto _failed; } ATOM_STORE(&s->type , SOCKET_TYPE_PLISTEN); result->opaque = request->opaque; result->id = id; result->ud = 0; result->data = "listen"; union sockaddr_all u; socklen_t slen = sizeof(u); if (getsockname(listen_fd, &u.s, &slen) == 0) { void * sin_addr = (u.s.sa_family == AF_INET) ? (void*)&u.v4.sin_addr : (void *)&u.v6.sin6_addr; if (inet_ntop(u.s.sa_family, sin_addr, ss->buffer, sizeof(ss->buffer)) == 0) { result->data = strerror(errno); return SOCKET_ERR; } int sin_port = ntohs((u.s.sa_family == AF_INET) ? u.v4.sin_port : u.v6.sin6_port); result->data = ss->buffer; result->ud = sin_port; } else { result->data = strerror(errno); return SOCKET_ERR; } return SOCKET_OPEN; _failed: close(listen_fd); result->opaque = request->opaque; result->id = id; result->ud = 0; result->data = "reach skynet socket number limit"; ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; return SOCKET_ERR; } static inline int nomore_sending_data(struct socket *s) { return (send_buffer_empty(s) && s->dw_buffer == NULL && (ATOM_LOAD(&s->sending) & 0xffff) == 0) || (ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE_WRITE); } static void close_read(struct socket_server *ss, struct socket * s, struct socket_message *result) { // Don't read socket later ATOM_STORE(&s->type , SOCKET_TYPE_HALFCLOSE_READ); enable_read(ss,s,false); shutdown(s->fd, SHUT_RD); result->id = s->id; result->ud = 0; result->data = NULL; result->opaque = s->opaque; } static inline int halfclose_read(struct socket *s) { return ATOM_LOAD(&s->type) == SOCKET_TYPE_HALFCLOSE_READ; } // SOCKET_CLOSE can be raised (only once) in one of two conditions. // See https://github.com/cloudwu/skynet/issues/1346 for more discussion. // 1. close socket by self, See close_socket() // 2. recv 0 or eof event (close socket by remote), See forward_message_tcp() // It's able to write data after SOCKET_CLOSE (In condition 2), but if remote is closed, SOCKET_ERR may raised. static int close_socket(struct socket_server *ss, struct request_close *request, struct socket_message *result) { int id = request->id; struct socket * s = &ss->slot[HASH_ID(id)]; if (socket_invalid(s, id)) { // The socket is closed, ignore return -1; } struct socket_lock l; socket_lock_init(s, &l); int shutdown_read = halfclose_read(s); if (request->shutdown || nomore_sending_data(s)) { // If socket is SOCKET_TYPE_HALFCLOSE_READ, Do not raise SOCKET_CLOSE again. int r = shutdown_read ? -1 : SOCKET_CLOSE; force_close(ss,s,&l,result); return r; } s->closing = true; if (!shutdown_read) { // don't read socket after socket.close() close_read(ss, s, result); return SOCKET_CLOSE; } // recv 0 before (socket is SOCKET_TYPE_HALFCLOSE_READ) and waiting for sending data out. return -1; } static int bind_socket(struct socket_server *ss, struct request_bind *request, struct socket_message *result) { int id = request->id; result->id = id; result->opaque = request->opaque; result->ud = 0; struct socket *s = new_fd(ss, id, request->fd, PROTOCOL_TCP, request->opaque, true); if (s == NULL) { result->data = "reach skynet socket number limit"; return SOCKET_ERR; } sp_nonblocking(request->fd); ATOM_STORE(&s->type , SOCKET_TYPE_BIND); result->data = "binding"; return SOCKET_OPEN; } static int resume_socket(struct socket_server *ss, struct request_resumepause *request, struct socket_message *result) { int id = request->id; result->id = id; result->opaque = request->opaque; result->ud = 0; result->data = NULL; struct socket *s = &ss->slot[HASH_ID(id)]; if (socket_invalid(s, id)) { result->data = "invalid socket"; return SOCKET_ERR; } if (halfclose_read(s)) { // The closing socket may be in transit, so raise an error. See https://github.com/cloudwu/skynet/issues/1374 result->data = "socket closed"; return SOCKET_ERR; } struct socket_lock l; socket_lock_init(s, &l); if (enable_read(ss, s, true)) { result->data = "enable read failed"; return SOCKET_ERR; } uint8_t type = ATOM_LOAD(&s->type); if (type == SOCKET_TYPE_PACCEPT || type == SOCKET_TYPE_PLISTEN) { ATOM_STORE(&s->type , (type == SOCKET_TYPE_PACCEPT) ? SOCKET_TYPE_CONNECTED : SOCKET_TYPE_LISTEN); s->opaque = request->opaque; result->data = "start"; return SOCKET_OPEN; } else if (type == SOCKET_TYPE_CONNECTED) { // todo: maybe we should send a message SOCKET_TRANSFER to s->opaque s->opaque = request->opaque; result->data = "transfer"; return SOCKET_OPEN; } // if s->type == SOCKET_TYPE_HALFCLOSE_WRITE , SOCKET_CLOSE message will send later return -1; } static int pause_socket(struct socket_server *ss, struct request_resumepause *request, struct socket_message *result) { int id = request->id; struct socket *s = &ss->slot[HASH_ID(id)]; if (socket_invalid(s, id)) { return -1; } if (enable_read(ss, s, false)) { return report_error(s, result, "enable read failed"); } return -1; } static void setopt_socket(struct socket_server *ss, struct request_setopt *request) { int id = request->id; struct socket *s = &ss->slot[HASH_ID(id)]; if (socket_invalid(s, id)) { return; } int v = request->value; setsockopt(s->fd, IPPROTO_TCP, request->what, &v, sizeof(v)); } static void block_readpipe(int pipefd, void *buffer, int sz) { for (;;) { int n = read(pipefd, buffer, sz); if (n<0) { if (errno == EINTR) continue; skynet_error(NULL, "socket-server : read pipe error %s.",strerror(errno)); return; } // must atomic read from a pipe assert(n == sz); return; } } static int has_cmd(struct socket_server *ss) { struct timeval tv = {0,0}; int retval; FD_SET(ss->recvctrl_fd, &ss->rfds); retval = select(ss->recvctrl_fd+1, &ss->rfds, NULL, NULL, &tv); if (retval == 1) { return 1; } return 0; } static void add_udp_socket(struct socket_server *ss, struct request_udp *udp) { int id = udp->id; int protocol; if (udp->family == AF_INET6) { protocol = PROTOCOL_UDPv6; } else { protocol = PROTOCOL_UDP; } struct socket *ns = new_fd(ss, id, udp->fd, protocol, udp->opaque, true); if (ns == NULL) { close(udp->fd); ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; return; } ATOM_STORE(&ns->type , SOCKET_TYPE_CONNECTED); memset(ns->p.udp_address, 0, sizeof(ns->p.udp_address)); } static int set_udp_address(struct socket_server *ss, struct request_setudp *request, struct socket_message *result) { int id = request->id; struct socket *s = &ss->slot[HASH_ID(id)]; if (socket_invalid(s, id)) { return -1; } int type = request->address[0]; if (type != s->protocol) { // protocol mismatch return report_error(s, result, "protocol mismatch"); } if (type == PROTOCOL_UDP) { memcpy(s->p.udp_address, request->address, 1+2+4); // 1 type, 2 port, 4 ipv4 } else { memcpy(s->p.udp_address, request->address, 1+2+16); // 1 type, 2 port, 16 ipv6 } ATOM_FDEC(&s->udpconnecting); return -1; } static int dial_udp_socket(struct socket_server *ss, struct request_dial_udp *request, struct socket_message *result){ int id = request->id; int protocol = request->address[0]; struct socket *ns = new_fd(ss, id, request->fd, protocol, request->opaque, true); if (ns == NULL){ close(request->fd); ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID; return -1; } if (protocol == PROTOCOL_UDP){ memcpy(ns->p.udp_address, request->address, 1 + 2 + 4); } else { memcpy(ns->p.udp_address, request->address, 1 + 2 + 16); } ATOM_STORE(&ns->type , SOCKET_TYPE_CONNECTED); ATOM_FDEC(&ns->udpconnecting); return -1; } static inline void inc_sending_ref(struct socket *s, int id) { if (s->protocol != PROTOCOL_TCP) return; for (;;) { unsigned long sending = ATOM_LOAD(&s->sending); if ((sending >> 16) == ID_TAG16(id)) { if ((sending & 0xffff) == 0xffff) { // s->sending may overflow (rarely), so busy waiting here for socket thread dec it. see issue #794 continue; } // inc sending only matching the same socket id if (ATOM_CAS_ULONG(&s->sending, sending, sending + 1)) return; // atom inc failed, retry } else { // socket id changed, just return return; } } } static inline void dec_sending_ref(struct socket_server *ss, int id) { struct socket * s = &ss->slot[HASH_ID(id)]; // Notice: udp may inc sending while type == SOCKET_TYPE_RESERVE if (s->id == id && s->protocol == PROTOCOL_TCP) { assert((ATOM_LOAD(&s->sending) & 0xffff) != 0); ATOM_FDEC(&s->sending); } } // return type static int ctrl_cmd(struct socket_server *ss, struct socket_message *result) { int fd = ss->recvctrl_fd; // the length of message is one byte, so 256 buffer size is enough. uint8_t buffer[256]; uint8_t header[2]; block_readpipe(fd, header, sizeof(header)); int type = header[0]; int len = header[1]; block_readpipe(fd, buffer, len); // ctrl command only exist in local fd, so don't worry about endian. switch (type) { case 'R': return resume_socket(ss,(struct request_resumepause *)buffer, result); case 'S': return pause_socket(ss,(struct request_resumepause *)buffer, result); case 'B': return bind_socket(ss,(struct request_bind *)buffer, result); case 'L': return listen_socket(ss,(struct request_listen *)buffer, result); case 'K': return close_socket(ss,(struct request_close *)buffer, result); case 'O': return open_socket(ss, (struct request_open *)buffer, result); case 'X': result->opaque = 0; result->id = 0; result->ud = 0; result->data = NULL; return SOCKET_EXIT; case 'W': return trigger_write(ss, (struct request_send *)buffer, result); case 'D': case 'P': { int priority = (type == 'D') ? PRIORITY_HIGH : PRIORITY_LOW; struct request_send * request = (struct request_send *) buffer; int ret = send_socket(ss, request, result, priority, NULL); dec_sending_ref(ss, request->id); return ret; } case 'A': { struct request_send_udp * rsu = (struct request_send_udp *)buffer; return send_socket(ss, &rsu->send, result, PRIORITY_HIGH, rsu->address); } case 'C': return set_udp_address(ss, (struct request_setudp *)buffer, result); case 'N': return dial_udp_socket(ss, (struct request_dial_udp *)buffer, result); case 'T': setopt_socket(ss, (struct request_setopt *)buffer); return -1; case 'U': add_udp_socket(ss, (struct request_udp *)buffer); return -1; default: skynet_error(NULL, "socket-server error: Unknown ctrl %c.",type); return -1; }; return -1; } // return -1 (ignore) when error static int forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lock *l, struct socket_message * result) { int sz = s->p.size; char * buffer = MALLOC(sz); int n = (int)read(s->fd, buffer, sz); if (n<0) { FREE(buffer); switch(errno) { case EINTR: case AGAIN_WOULDBLOCK: break; default: return report_error(s, result, strerror(errno)); } return -1; } if (n==0) { FREE(buffer); if (s->closing) { // Rare case : if s->closing is true, reading event is disable, and SOCKET_CLOSE is raised. if (nomore_sending_data(s)) { force_close(ss,s,l,result); } return -1; } int t = ATOM_LOAD(&s->type); if (t == SOCKET_TYPE_HALFCLOSE_READ) { // Rare case : Already shutdown read. return -1; } if (t == SOCKET_TYPE_HALFCLOSE_WRITE) { // Remote shutdown read (write error) before. force_close(ss,s,l,result); } else { close_read(ss, s, result); } return SOCKET_CLOSE; } if (halfclose_read(s)) { // discard recv data (Rare case : if socket is HALFCLOSE_READ, reading event is disable.) FREE(buffer); return -1; } stat_read(ss,s,n); result->opaque = s->opaque; result->id = s->id; result->ud = n; result->data = buffer; if (n == sz) { s->p.size *= 2; return SOCKET_MORE; } else if (sz > MIN_READ_BUFFER && n*2 < sz) { s->p.size /= 2; } return SOCKET_DATA; } static int gen_udp_address(int protocol, union sockaddr_all *sa, uint8_t * udp_address) { int addrsz = 1; udp_address[0] = (uint8_t)protocol; if (protocol == PROTOCOL_UDP) { memcpy(udp_address+addrsz, &sa->v4.sin_port, sizeof(sa->v4.sin_port)); addrsz += sizeof(sa->v4.sin_port); memcpy(udp_address+addrsz, &sa->v4.sin_addr, sizeof(sa->v4.sin_addr)); addrsz += sizeof(sa->v4.sin_addr); } else { memcpy(udp_address+addrsz, &sa->v6.sin6_port, sizeof(sa->v6.sin6_port)); addrsz += sizeof(sa->v6.sin6_port); memcpy(udp_address+addrsz, &sa->v6.sin6_addr, sizeof(sa->v6.sin6_addr)); addrsz += sizeof(sa->v6.sin6_addr); } return addrsz; } static int forward_message_udp(struct socket_server *ss, struct socket *s, struct socket_lock *l, struct socket_message * result) { union sockaddr_all sa; socklen_t slen = sizeof(sa); int n = recvfrom(s->fd, ss->udpbuffer,MAX_UDP_PACKAGE,0,&sa.s,&slen); if (n<0) { switch(errno) { case EINTR: case AGAIN_WOULDBLOCK: return -1; } int error = errno; // close when error force_close(ss, s, l, result); result->data = strerror(error); return SOCKET_ERR; } stat_read(ss,s,n); uint8_t * data; if (slen == sizeof(sa.v4)) { if (s->protocol != PROTOCOL_UDP) return -1; data = MALLOC(n + 1 + 2 + 4); gen_udp_address(PROTOCOL_UDP, &sa, data + n); } else { if (s->protocol != PROTOCOL_UDPv6) return -1; data = MALLOC(n + 1 + 2 + 16); gen_udp_address(PROTOCOL_UDPv6, &sa, data + n); } memcpy(data, ss->udpbuffer, n); result->opaque = s->opaque; result->id = s->id; result->ud = n; result->data = (char *)data; return SOCKET_UDP; } static int report_connect(struct socket_server *ss, struct socket *s, struct socket_lock *l, struct socket_message *result) { int error; socklen_t len = sizeof(error); int code = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &error, &len); if (code < 0 || error) { error = code < 0 ? errno : error; force_close(ss, s, l, result); result->data = strerror(error); return SOCKET_ERR; } else { ATOM_STORE(&s->type , SOCKET_TYPE_CONNECTED); result->opaque = s->opaque; result->id = s->id; result->ud = 0; if (nomore_sending_data(s)) { if (enable_write(ss, s, false)) { force_close(ss,s,l, result); result->data = "disable write failed"; return SOCKET_ERR; } } union sockaddr_all u; socklen_t slen = sizeof(u); if (getpeername(s->fd, &u.s, &slen) == 0) { void * sin_addr = (u.s.sa_family == AF_INET) ? (void*)&u.v4.sin_addr : (void *)&u.v6.sin6_addr; if (inet_ntop(u.s.sa_family, sin_addr, ss->buffer, sizeof(ss->buffer))) { result->data = ss->buffer; return SOCKET_OPEN; } } result->data = NULL; return SOCKET_OPEN; } } static int getname(union sockaddr_all *u, char *buffer, size_t sz) { char tmp[INET6_ADDRSTRLEN]; void * sin_addr = (u->s.sa_family == AF_INET) ? (void*)&u->v4.sin_addr : (void *)&u->v6.sin6_addr; if (inet_ntop(u->s.sa_family, sin_addr, tmp, sizeof(tmp))) { int sin_port = ntohs((u->s.sa_family == AF_INET) ? u->v4.sin_port : u->v6.sin6_port); snprintf(buffer, sz, "%s:%d", tmp, sin_port); return 1; } else { buffer[0] = '\0'; return 0; } } // return 0 when failed, or -1 when file limit static int report_accept(struct socket_server *ss, struct socket *s, struct socket_message *result) { union sockaddr_all u; socklen_t len = sizeof(u); int client_fd = accept(s->fd, &u.s, &len); if (client_fd < 0) { if (errno == EMFILE || errno == ENFILE) { result->opaque = s->opaque; result->id = s->id; result->ud = 0; result->data = strerror(errno); // See https://stackoverflow.com/questions/47179793/how-to-gracefully-handle-accept-giving-emfile-and-close-the-connection if (ss->reserve_fd >= 0) { close(ss->reserve_fd); client_fd = accept(s->fd, &u.s, &len); if (client_fd >= 0) { close(client_fd); } ss->reserve_fd = dup(1); } return -1; } else { return 0; } } int id = reserve_id(ss); if (id < 0) { close(client_fd); return 0; } socket_keepalive(client_fd); sp_nonblocking(client_fd); struct socket *ns = new_fd(ss, id, client_fd, PROTOCOL_TCP, s->opaque, false); if (ns == NULL) { close(client_fd); return 0; } // accept new one connection stat_read(ss,s,1); ATOM_STORE(&ns->type , SOCKET_TYPE_PACCEPT); result->opaque = s->opaque; result->id = s->id; result->ud = id; result->data = NULL; if (getname(&u, ss->buffer, sizeof(ss->buffer))) { result->data = ss->buffer; } return 1; } static inline void clear_closed_event(struct socket_server *ss, struct socket_message * result, int type) { if (type == SOCKET_CLOSE || type == SOCKET_ERR) { int id = result->id; int i; for (i=ss->event_index; ievent_n; i++) { struct event *e = &ss->ev[i]; struct socket *s = e->s; if (s) { if (socket_invalid(s, id) && s->id == id) { e->s = NULL; break; } } } } } // return type int socket_server_poll(struct socket_server *ss, struct socket_message * result, int * more) { for (;;) { if (ss->checkctrl) { if (has_cmd(ss)) { int type = ctrl_cmd(ss, result); if (type != -1) { clear_closed_event(ss, result, type); return type; } else continue; } else { ss->checkctrl = 0; } } if (ss->event_index == ss->event_n) { ss->event_n = sp_wait(ss->event_fd, ss->ev, MAX_EVENT); ss->checkctrl = 1; if (more) { *more = 0; } ss->event_index = 0; if (ss->event_n <= 0) { ss->event_n = 0; int err = errno; if (err != EINTR) { skynet_error(NULL, "socket-server error: %s", strerror(err)); } continue; } } struct event *e = &ss->ev[ss->event_index++]; struct socket *s = e->s; if (s == NULL) { // dispatch pipe message at beginning continue; } struct socket_lock l; socket_lock_init(s, &l); switch (ATOM_LOAD(&s->type)) { case SOCKET_TYPE_CONNECTING: return report_connect(ss, s, &l, result); case SOCKET_TYPE_LISTEN: { int ok = report_accept(ss, s, result); if (ok > 0) { return SOCKET_ACCEPT; } if (ok < 0 ) { return SOCKET_ERR; } // when ok == 0, retry break; } case SOCKET_TYPE_INVALID: skynet_error(NULL, "socket-server error: invalid socket"); break; default: if (e->read) { int type; if (s->protocol == PROTOCOL_TCP) { type = forward_message_tcp(ss, s, &l, result); if (type == SOCKET_MORE) { --ss->event_index; return SOCKET_DATA; } } else { type = forward_message_udp(ss, s, &l, result); if (type == SOCKET_UDP) { // try read again --ss->event_index; return SOCKET_UDP; } } if (e->write && type != SOCKET_CLOSE && type != SOCKET_ERR) { // Try to dispatch write message next step if write flag set. e->read = false; --ss->event_index; } if (type == -1) break; return type; } if (e->write) { int type = send_buffer(ss, s, &l, result); if (type == -1) break; return type; } if (e->error) { int error; socklen_t len = sizeof(error); int code = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &error, &len); const char * err = NULL; if (code < 0) { err = strerror(errno); } else if (error != 0) { err = strerror(error); } else { err = "Unknown error"; } return report_error(s, result, err); } if (e->eof) { // For epoll (at least), FIN packets are exchanged both ways. // See: https://stackoverflow.com/questions/52976152/tcp-when-is-epollhup-generated int halfclose = halfclose_read(s); force_close(ss, s, &l, result); if (!halfclose) { return SOCKET_CLOSE; } } break; } } } static void send_request(struct socket_server *ss, struct request_package *request, char type, int len) { request->header[6] = (uint8_t)type; request->header[7] = (uint8_t)len; const char * req = (const char *)request + offsetof(struct request_package, header[6]); for (;;) { ssize_t n = write(ss->sendctrl_fd, req, len+2); if (n<0) { if (errno != EINTR) { skynet_error(NULL, "socket-server : send ctrl command error %s.", strerror(errno)); } continue; } assert(n == len+2); return; } } static int open_request(struct socket_server *ss, struct request_package *req, uintptr_t opaque, const char *addr, int port) { int len = strlen(addr); if (len + sizeof(req->u.open) >= 256) { skynet_error(NULL, "socket-server error: Invalid addr %s.",addr); return -1; } int id = reserve_id(ss); if (id < 0) return -1; req->u.open.opaque = opaque; req->u.open.id = id; req->u.open.port = port; memcpy(req->u.open.host, addr, len); req->u.open.host[len] = '\0'; return len; } static inline void request_init(struct request_package *req) { memset(req, 0, sizeof(*req)); } int socket_server_connect(struct socket_server *ss, uintptr_t opaque, const char * addr, int port) { struct request_package request; request_init(&request); int len = open_request(ss, &request, opaque, addr, port); if (len < 0) return -1; send_request(ss, &request, 'O', sizeof(request.u.open) + len); return request.u.open.id; } static inline int can_direct_write(struct socket *s, int id) { return s->id == id && nomore_sending_data(s) && ATOM_LOAD(&s->type) == SOCKET_TYPE_CONNECTED && ATOM_LOAD(&s->udpconnecting) == 0; } // return -1 when error, 0 when success int socket_server_send(struct socket_server *ss, struct socket_sendbuffer *buf) { int id = buf->id; struct socket * s = &ss->slot[HASH_ID(id)]; if (socket_invalid(s, id) || s->closing) { free_buffer(ss, buf); return -1; } struct socket_lock l; socket_lock_init(s, &l); if (can_direct_write(s,id) && socket_trylock(&l)) { // may be we can send directly, double check if (can_direct_write(s,id)) { // send directly struct send_object so; send_object_init_from_sendbuffer(ss, &so, buf); ssize_t n; if (s->protocol == PROTOCOL_TCP) { n = write(s->fd, so.buffer, so.sz); } else { union sockaddr_all sa; socklen_t sasz = udp_socket_address(s, s->p.udp_address, &sa); if (sasz == 0) { skynet_error(NULL, "socket-server : set udp (%d) error: address first.", id); socket_unlock(&l); so.free_func((void *)buf->buffer); return -1; } n = sendto(s->fd, so.buffer, so.sz, 0, &sa.s, sasz); } if (n<0) { // ignore error, let socket thread try again n = 0; } stat_write(ss,s,n); if (n == so.sz) { // write done socket_unlock(&l); so.free_func((void *)buf->buffer); return 0; } // write failed, put buffer into s->dw_* , and let socket thread send it. see send_buffer() s->dw_buffer = clone_buffer(buf, &s->dw_size); s->dw_offset = n; socket_unlock(&l); struct request_package request; request_init(&request); request.u.send.id = id; request.u.send.sz = 0; request.u.send.buffer = NULL; // let socket thread enable write event send_request(ss, &request, 'W', sizeof(request.u.send)); return 0; } socket_unlock(&l); } inc_sending_ref(s, id); struct request_package request; request_init(&request); request.u.send.id = id; request.u.send.buffer = clone_buffer(buf, &request.u.send.sz); send_request(ss, &request, 'D', sizeof(request.u.send)); return 0; } // return -1 when error, 0 when success int socket_server_send_lowpriority(struct socket_server *ss, struct socket_sendbuffer *buf) { int id = buf->id; struct socket * s = &ss->slot[HASH_ID(id)]; if (socket_invalid(s, id)) { free_buffer(ss, buf); return -1; } inc_sending_ref(s, id); struct request_package request; request_init(&request); request.u.send.id = id; request.u.send.buffer = clone_buffer(buf, &request.u.send.sz); send_request(ss, &request, 'P', sizeof(request.u.send)); return 0; } void socket_server_exit(struct socket_server *ss) { struct request_package request; request_init(&request); send_request(ss, &request, 'X', 0); } void socket_server_close(struct socket_server *ss, uintptr_t opaque, int id) { struct request_package request; request_init(&request); request.u.close.id = id; request.u.close.shutdown = 0; request.u.close.opaque = opaque; send_request(ss, &request, 'K', sizeof(request.u.close)); } void socket_server_shutdown(struct socket_server *ss, uintptr_t opaque, int id) { struct request_package request; request_init(&request); request.u.close.id = id; request.u.close.shutdown = 1; request.u.close.opaque = opaque; send_request(ss, &request, 'K', sizeof(request.u.close)); } // return -1 means failed // or return AF_INET or AF_INET6 static int do_bind(const char *host, int port, int protocol, int *family) { int fd; int status; int reuse = 1; struct addrinfo ai_hints; struct addrinfo *ai_list = NULL; char portstr[16]; if (host == NULL || host[0] == 0) { host = "0.0.0.0"; // INADDR_ANY } sprintf(portstr, "%d", port); memset( &ai_hints, 0, sizeof( ai_hints ) ); ai_hints.ai_family = AF_UNSPEC; if (protocol == IPPROTO_TCP) { ai_hints.ai_socktype = SOCK_STREAM; } else { assert(protocol == IPPROTO_UDP); ai_hints.ai_socktype = SOCK_DGRAM; } ai_hints.ai_protocol = protocol; status = getaddrinfo( host, portstr, &ai_hints, &ai_list ); if ( status != 0 ) { return -1; } *family = ai_list->ai_family; fd = socket(*family, ai_list->ai_socktype, 0); if (fd < 0) { goto _failed_fd; } if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(int))==-1) { goto _failed; } status = bind(fd, (struct sockaddr *)ai_list->ai_addr, ai_list->ai_addrlen); if (status != 0) goto _failed; freeaddrinfo( ai_list ); return fd; _failed: close(fd); _failed_fd: freeaddrinfo( ai_list ); return -1; } static int do_listen(const char * host, int port, int backlog) { int family = 0; int listen_fd = do_bind(host, port, IPPROTO_TCP, &family); if (listen_fd < 0) { return -1; } if (listen(listen_fd, backlog) == -1) { close(listen_fd); return -1; } return listen_fd; } int socket_server_listen(struct socket_server *ss, uintptr_t opaque, const char * addr, int port, int backlog) { int fd = do_listen(addr, port, backlog); if (fd < 0) { return -1; } struct request_package request; request_init(&request); int id = reserve_id(ss); if (id < 0) { close(fd); return id; } request.u.listen.opaque = opaque; request.u.listen.id = id; request.u.listen.fd = fd; send_request(ss, &request, 'L', sizeof(request.u.listen)); return id; } int socket_server_bind(struct socket_server *ss, uintptr_t opaque, int fd) { struct request_package request; request_init(&request); int id = reserve_id(ss); if (id < 0) return -1; request.u.bind.opaque = opaque; request.u.bind.id = id; request.u.bind.fd = fd; send_request(ss, &request, 'B', sizeof(request.u.bind)); return id; } void socket_server_start(struct socket_server *ss, uintptr_t opaque, int id) { struct request_package request; request_init(&request); request.u.resumepause.id = id; request.u.resumepause.opaque = opaque; send_request(ss, &request, 'R', sizeof(request.u.resumepause)); } void socket_server_pause(struct socket_server *ss, uintptr_t opaque, int id) { struct request_package request; request_init(&request); request.u.resumepause.id = id; request.u.resumepause.opaque = opaque; send_request(ss, &request, 'S', sizeof(request.u.resumepause)); } void socket_server_nodelay(struct socket_server *ss, int id) { struct request_package request; request_init(&request); request.u.setopt.id = id; request.u.setopt.what = TCP_NODELAY; request.u.setopt.value = 1; send_request(ss, &request, 'T', sizeof(request.u.setopt)); } void socket_server_userobject(struct socket_server *ss, struct socket_object_interface *soi) { ss->soi = *soi; } // UDP int socket_server_udp(struct socket_server *ss, uintptr_t opaque, const char * addr, int port) { int fd; int family; if (port != 0 || addr != NULL) { // bind fd = do_bind(addr, port, IPPROTO_UDP, &family); if (fd < 0) { return -1; } } else { family = AF_INET; fd = socket(family, SOCK_DGRAM, 0); if (fd < 0) { return -1; } } sp_nonblocking(fd); int id = reserve_id(ss); if (id < 0) { close(fd); return -1; } struct request_package request; request_init(&request); request.u.udp.id = id; request.u.udp.fd = fd; request.u.udp.opaque = opaque; request.u.udp.family = family; send_request(ss, &request, 'U', sizeof(request.u.udp)); return id; } int socket_server_udp_listen(struct socket_server *ss, uintptr_t opaque, const char* addr, int port){ int fd; if (port == 0){ return -1; } int family; // bind fd = do_bind(addr, port, IPPROTO_UDP, &family); if (fd < 0) { return -1; } sp_nonblocking(fd); int id = reserve_id(ss); if (id < 0) { close(fd); return -1; } struct request_package request; request_init(&request); request.u.udp.id = id; request.u.udp.fd = fd; request.u.udp.opaque = opaque; request.u.udp.family = family; send_request(ss, &request, 'U', sizeof(request.u.udp)); return id; } int socket_server_udp_dial(struct socket_server *ss, uintptr_t opaque, const char* addr, int port){ int status; struct addrinfo ai_hints; struct addrinfo *ai_list = NULL; char portstr[16]; sprintf(portstr, "%d", port); memset( &ai_hints, 0, sizeof( ai_hints ) ); ai_hints.ai_family = AF_UNSPEC; ai_hints.ai_socktype = SOCK_DGRAM; ai_hints.ai_protocol = IPPROTO_UDP; status = getaddrinfo(addr, portstr, &ai_hints, &ai_list ); if ( status != 0 ) { return -1; } int protocol; if (ai_list->ai_family == AF_INET) { protocol = PROTOCOL_UDP; } else if (ai_list->ai_family == AF_INET6) { protocol = PROTOCOL_UDPv6; } else { freeaddrinfo( ai_list ); return -1; } int fd = socket(ai_list->ai_family, SOCK_DGRAM, 0); if (fd < 0){ return -1; } sp_nonblocking(fd); int id = reserve_id(ss); if (id < 0){ close(fd); return -1; } struct request_package request; request_init(&request); request.u.dial_udp.id = id; request.u.dial_udp.fd = fd; request.u.dial_udp.opaque = opaque; int addrsz = gen_udp_address(protocol, (union sockaddr_all *)ai_list->ai_addr, request.u.dial_udp.address); freeaddrinfo( ai_list ); send_request(ss, &request, 'N', sizeof(request.u.dial_udp) - sizeof(request.u.dial_udp.address) + addrsz); return id; } int socket_server_udp_send(struct socket_server *ss, const struct socket_udp_address *addr, struct socket_sendbuffer *buf) { int id = buf->id; struct socket * s = &ss->slot[HASH_ID(id)]; if (socket_invalid(s, id)) { free_buffer(ss, buf); return -1; } const uint8_t *udp_address = (const uint8_t *)addr; int addrsz; switch (udp_address[0]) { case PROTOCOL_UDP: addrsz = 1+2+4; // 1 type, 2 port, 4 ipv4 break; case PROTOCOL_UDPv6: addrsz = 1+2+16; // 1 type, 2 port, 16 ipv6 break; default: free_buffer(ss, buf); return -1; } struct socket_lock l; socket_lock_init(s, &l); if (can_direct_write(s,id) && socket_trylock(&l)) { // may be we can send directly, double check if (can_direct_write(s,id)) { // send directly struct send_object so; send_object_init_from_sendbuffer(ss, &so, buf); union sockaddr_all sa; socklen_t sasz = udp_socket_address(s, udp_address, &sa); if (sasz == 0) { socket_unlock(&l); so.free_func((void *)buf->buffer); return -1; } int n = sendto(s->fd, so.buffer, so.sz, 0, &sa.s, sasz); if (n >= 0) { // sendto succ stat_write(ss,s,n); socket_unlock(&l); so.free_func((void *)buf->buffer); return 0; } } socket_unlock(&l); // let socket thread try again, udp doesn't care the order } struct request_package request; request_init(&request); request.u.send_udp.send.id = id; request.u.send_udp.send.buffer = clone_buffer(buf, &request.u.send_udp.send.sz); memcpy(request.u.send_udp.address, udp_address, addrsz); send_request(ss, &request, 'A', sizeof(request.u.send_udp.send)+addrsz); return 0; } int socket_server_udp_connect(struct socket_server *ss, int id, const char * addr, int port) { struct socket * s = &ss->slot[HASH_ID(id)]; if (socket_invalid(s, id)) { return -1; } struct socket_lock l; socket_lock_init(s, &l); socket_lock(&l); if (socket_invalid(s, id)) { socket_unlock(&l); return -1; } ATOM_FINC(&s->udpconnecting); socket_unlock(&l); int status; struct addrinfo ai_hints; struct addrinfo *ai_list = NULL; char portstr[16]; sprintf(portstr, "%d", port); memset( &ai_hints, 0, sizeof( ai_hints ) ); ai_hints.ai_family = AF_UNSPEC; ai_hints.ai_socktype = SOCK_DGRAM; ai_hints.ai_protocol = IPPROTO_UDP; status = getaddrinfo(addr, portstr, &ai_hints, &ai_list ); if ( status != 0 ) { return -1; } struct request_package request; request_init(&request); request.u.set_udp.id = id; int protocol; if (ai_list->ai_family == AF_INET) { protocol = PROTOCOL_UDP; } else if (ai_list->ai_family == AF_INET6) { protocol = PROTOCOL_UDPv6; } else { freeaddrinfo( ai_list ); return -1; } int addrsz = gen_udp_address(protocol, (union sockaddr_all *)ai_list->ai_addr, request.u.set_udp.address); freeaddrinfo( ai_list ); send_request(ss, &request, 'C', sizeof(request.u.set_udp) - sizeof(request.u.set_udp.address) +addrsz); return 0; } const struct socket_udp_address * socket_server_udp_address(struct socket_server *ss, struct socket_message *msg, int *addrsz) { uint8_t * address = (uint8_t *)(msg->data + msg->ud); int type = address[0]; switch(type) { case PROTOCOL_UDP: *addrsz = 1+2+4; break; case PROTOCOL_UDPv6: *addrsz = 1+2+16; break; default: return NULL; } return (const struct socket_udp_address *)address; } struct socket_info * socket_info_create(struct socket_info *last) { struct socket_info *si = skynet_malloc(sizeof(*si)); memset(si, 0 , sizeof(*si)); si->next = last; return si; } void socket_info_release(struct socket_info *si) { while (si) { struct socket_info *temp = si; si = si->next; skynet_free(temp); } } static int query_info(struct socket *s, struct socket_info *si) { union sockaddr_all u; socklen_t slen = sizeof(u); int closing = 0; switch (ATOM_LOAD(&s->type)) { case SOCKET_TYPE_BIND: si->type = SOCKET_INFO_BIND; si->name[0] = '\0'; break; case SOCKET_TYPE_LISTEN: si->type = SOCKET_INFO_LISTEN; if (getsockname(s->fd, &u.s, &slen) == 0) { getname(&u, si->name, sizeof(si->name)); } break; case SOCKET_TYPE_HALFCLOSE_READ: case SOCKET_TYPE_HALFCLOSE_WRITE: closing = 1; case SOCKET_TYPE_CONNECTED: if (s->protocol == PROTOCOL_TCP) { si->type = closing ? SOCKET_INFO_CLOSING : SOCKET_INFO_TCP; if (getpeername(s->fd, &u.s, &slen) == 0) { getname(&u, si->name, sizeof(si->name)); } } else { si->type = SOCKET_INFO_UDP; if (udp_socket_address(s, s->p.udp_address, &u)) { getname(&u, si->name, sizeof(si->name)); } } break; default: return 0; } si->id = s->id; si->opaque = (uint64_t)s->opaque; si->read = s->stat.read; si->write = s->stat.write; si->rtime = s->stat.rtime; si->wtime = s->stat.wtime; si->wbuffer = s->wb_size; si->reading = s->reading; si->writing = s->writing; return 1; } struct socket_info * socket_server_info(struct socket_server *ss) { int i; struct socket_info * si = NULL; for (i=0;islot[i]; int id = s->id; struct socket_info temp; if (query_info(s, &temp) && s->id == id) { // socket_server_info may call in different thread, so check socket id again si = socket_info_create(si); temp.next = si->next; *si = temp; } } return si; } ================================================ FILE: skynet-src/socket_server.h ================================================ #ifndef skynet_socket_server_h #define skynet_socket_server_h #include #include "socket_info.h" #include "socket_buffer.h" #define SOCKET_DATA 0 #define SOCKET_CLOSE 1 #define SOCKET_OPEN 2 #define SOCKET_ACCEPT 3 #define SOCKET_ERR 4 #define SOCKET_EXIT 5 #define SOCKET_UDP 6 #define SOCKET_WARNING 7 // Only for internal use #define SOCKET_RST 8 #define SOCKET_MORE 9 struct socket_server; struct socket_message { int id; uintptr_t opaque; int ud; // for accept, ud is new connection id ; for data, ud is size of data char * data; }; struct socket_server * socket_server_create(uint64_t time); void socket_server_release(struct socket_server *); void socket_server_updatetime(struct socket_server *, uint64_t time); int socket_server_poll(struct socket_server *, struct socket_message *result, int *more); void socket_server_exit(struct socket_server *); void socket_server_close(struct socket_server *, uintptr_t opaque, int id); void socket_server_shutdown(struct socket_server *, uintptr_t opaque, int id); void socket_server_start(struct socket_server *, uintptr_t opaque, int id); void socket_server_pause(struct socket_server *, uintptr_t opaque, int id); // return -1 when error int socket_server_send(struct socket_server *, struct socket_sendbuffer *buffer); int socket_server_send_lowpriority(struct socket_server *, struct socket_sendbuffer *buffer); // ctrl command below returns id int socket_server_listen(struct socket_server *, uintptr_t opaque, const char * addr, int port, int backlog); int socket_server_connect(struct socket_server *, uintptr_t opaque, const char * addr, int port); int socket_server_bind(struct socket_server *, uintptr_t opaque, int fd); // for tcp void socket_server_nodelay(struct socket_server *, int id); struct socket_udp_address; // create an udp socket handle, attach opaque with it . udp socket don't need call socket_server_start to recv message // if port != 0, bind the socket . if addr == NULL, bind ipv4 0.0.0.0 . If you want to use ipv6, addr can be "::" and port 0. int socket_server_udp(struct socket_server *, uintptr_t opaque, const char * addr, int port); // set default dest address, return 0 when success int socket_server_udp_connect(struct socket_server *, int id, const char * addr, int port); // create an udp client socket handle, and connect to server addr, return id when success int socket_server_udp_dial(struct socket_server *ss, uintptr_t opaque, const char* addr, int port); // create an udp server socket handle, and bind the host port, return id when success int socket_server_udp_listen(struct socket_server *ss, uintptr_t opaque, const char* addr, int port); // If the socket_udp_address is NULL, use last call socket_server_udp_connect address instead // You can also use socket_server_send int socket_server_udp_send(struct socket_server *, const struct socket_udp_address *, struct socket_sendbuffer *buffer); // extract the address of the message, struct socket_message * should be SOCKET_UDP const struct socket_udp_address * socket_server_udp_address(struct socket_server *, struct socket_message *, int *addrsz); struct socket_object_interface { const void * (*buffer)(const void *); size_t (*size)(const void *); void (*free)(void *); }; // if you send package with type SOCKET_BUFFER_OBJECT, use soi. void socket_server_userobject(struct socket_server *, struct socket_object_interface *soi); struct socket_info * socket_server_info(struct socket_server *); #endif ================================================ FILE: skynet-src/spinlock.h ================================================ #ifndef SKYNET_SPINLOCK_H #define SKYNET_SPINLOCK_H #define SPIN_INIT(q) spinlock_init(&(q)->lock); #define SPIN_LOCK(q) spinlock_lock(&(q)->lock); #define SPIN_UNLOCK(q) spinlock_unlock(&(q)->lock); #define SPIN_DESTROY(q) spinlock_destroy(&(q)->lock); #ifndef USE_PTHREAD_LOCK #ifdef __STDC_NO_ATOMICS__ #define atomic_flag_ int #define ATOMIC_FLAG_INIT_ 0 #define atomic_flag_test_and_set_(ptr) __sync_lock_test_and_set(ptr, 1) #define atomic_flag_clear_(ptr) __sync_lock_release(ptr) struct spinlock { atomic_flag_ lock; }; static inline void spinlock_init(struct spinlock *lock) { atomic_flag_ v = ATOMIC_FLAG_INIT_; lock->lock = v; } static inline void spinlock_lock(struct spinlock *lock) { while (atomic_flag_test_and_set_(&lock->lock)) {} } static inline int spinlock_trylock(struct spinlock *lock) { return atomic_flag_test_and_set_(&lock->lock) == 0; } static inline void spinlock_unlock(struct spinlock *lock) { atomic_flag_clear_(&lock->lock); } static inline void spinlock_destroy(struct spinlock *lock) { (void) lock; } #else // __STDC_NO_ATOMICS__ #include "atomic.h" #define atomic_test_and_set_(ptr) STD_ atomic_exchange_explicit(ptr, 1, STD_ memory_order_acquire) #define atomic_clear_(ptr) STD_ atomic_store_explicit(ptr, 0, STD_ memory_order_release); #define atomic_load_relaxed_(ptr) STD_ atomic_load_explicit(ptr, STD_ memory_order_relaxed) #if defined(__x86_64__) #include // For _mm_pause #define atomic_pause_() _mm_pause() #else #define atomic_pause_() ((void)0) #endif struct spinlock { STD_ atomic_int lock; }; static inline void spinlock_init(struct spinlock *lock) { STD_ atomic_init(&lock->lock, 0); } static inline void spinlock_lock(struct spinlock *lock) { for (;;) { if (!atomic_test_and_set_(&lock->lock)) return; while (atomic_load_relaxed_(&lock->lock)) atomic_pause_(); } } static inline int spinlock_trylock(struct spinlock *lock) { return !atomic_load_relaxed_(&lock->lock) && !atomic_test_and_set_(&lock->lock); } static inline void spinlock_unlock(struct spinlock *lock) { atomic_clear_(&lock->lock); } static inline void spinlock_destroy(struct spinlock *lock) { (void) lock; } #endif // __STDC_NO_ATOMICS__ #else #include // we use mutex instead of spinlock for some reason // you can also replace to pthread_spinlock struct spinlock { pthread_mutex_t lock; }; static inline void spinlock_init(struct spinlock *lock) { pthread_mutex_init(&lock->lock, NULL); } static inline void spinlock_lock(struct spinlock *lock) { pthread_mutex_lock(&lock->lock); } static inline int spinlock_trylock(struct spinlock *lock) { return pthread_mutex_trylock(&lock->lock) == 0; } static inline void spinlock_unlock(struct spinlock *lock) { pthread_mutex_unlock(&lock->lock); } static inline void spinlock_destroy(struct spinlock *lock) { pthread_mutex_destroy(&lock->lock); } #endif #endif ================================================ FILE: test/pingserver.lua ================================================ local skynet = require "skynet" local queue = require "skynet.queue" local snax = require "skynet.snax" local i = 0 local hello = "hello" function response.ping(hello) skynet.sleep(100) return hello end -- response.sleep and accept.hello share one lock local lock function accept.sleep(queue, n) if queue then lock( function() print("queue=",queue, n) skynet.sleep(n) end) else print("queue=",queue, n) skynet.sleep(n) end end function accept.hello() lock(function() i = i + 1 print (i, hello) end) end function accept.exit(...) snax.exit(...) end function response.error() error "throw an error" end function init( ... ) print ("ping server start:", ...) snax.enablecluster() -- enable cluster call -- init queue lock = queue() end function exit(...) print ("ping server exit:", ...) end ================================================ FILE: test/sharemap.sp ================================================ .foobar { x 0 : integer y 1 : integer s 2 : string } ================================================ FILE: test/testbson.lua ================================================ local bson = require "bson" local sub = bson.encode_order( "hello", 1, "world", 2 ) do -- check decode encode_order local d = bson.decode(sub) assert(d.hello == 1 ) assert(d.world == 2 ) end local function tbl_next(...) print("--- next.a", ...) local k, v = next(...) print("--- next.b", k, v) return k, v end local function tbl_pairs(obj) return tbl_next, obj.__data, nil end local obj_a = { __data = { ["1"] = 2, ["3"] = 4, ["5"] = 6, } } setmetatable( obj_a, { __index = obj_a.__data, __pairs = tbl_pairs, } ) local obj_b = { __data = { ["7"] = 8, ["9"] = 10, ["11"] = obj_a, } } setmetatable( obj_b, { __index = obj_b.__data, __pairs = tbl_pairs, } ) local metaarray = setmetatable({ n = 5 }, { __len = function(self) return self.n end, __index = function(self, idx) return tostring(idx) end, }) b = bson.encode { a = 1, b = true, c = bson.null, d = { 1,2,3,4 }, e = bson.binary "hello", f = bson.regex ("*","i"), g = bson.regex "hello", h = bson.date (os.time()), i = bson.timestamp(os.time()), j = bson.objectid(), k = { a = false, b = true }, l = {}, m = bson.minkey, n = bson.maxkey, o = sub, p = 2^32-1, q = obj_b, r = metaarray, } print "\n[before replace]" t = b:decode() for k, v in pairs(t) do print(k,type(v)) end for k,v in ipairs(t.r) do print(k,v) end b:makeindex() b.a = 2 b.b = false b.h = bson.date(os.time()) b.i = bson.timestamp(os.time()) b.j = bson.objectid() print "\n[after replace]" t = b:decode() print("o.hello", bson.type(t.o.hello)) ================================================ FILE: test/testcoroutine.lua ================================================ local skynet = require "skynet" -- You should use skynet.coroutine instead of origin coroutine in skynet local coroutine = require "skynet.coroutine" local profile = require "skynet.profile" local function status(co) repeat local status = coroutine.status(co) print("STATUS", status) skynet.sleep(100) until status == "suspended" repeat local ok, n = assert(coroutine.resume(co)) print("status thread", n) until not n skynet.exit() end local function test(n) local co = coroutine.running() print ("begin", co, coroutine.thread(co)) -- false skynet.fork(status, co) for i=1,n do skynet.sleep(100) coroutine.yield(i) end print ("end", co) end local function main() local f = coroutine.wrap(test) coroutine.yield "begin" for i=1,3 do local n = f(5) print("main thread",n) end coroutine.yield "end" print("main thread time:", profile.stop(coroutine.thread())) end skynet.start(function() print("Main thead :", coroutine.thread()) -- true print(coroutine.resume(coroutine.running())) -- always return false profile.start() local f = coroutine.wrap(main) print("main step", f()) print("main step", f()) print("main step", f()) -- print("main thread time:", profile.stop()) print("close", coroutine.close(coroutine.create(main))) end) ================================================ FILE: test/testcrypt.lua ================================================ local skynet = require "skynet" local crypt = require "skynet.crypt" local text = "hello world" local key = "12345678" local function desencode(key, text, padding) local c = crypt.desencode(key, text, crypt.padding[padding or "iso7816_4"]) return crypt.base64encode(c) end local function desdecode(key, text, padding) text = crypt.base64decode(text) return crypt.desdecode(key, text, crypt.padding[padding or "iso7816_4"]) end local etext = desencode(key, text) assert( etext == "KNugLrX23UcGtcVlk9y+LA==") assert(desdecode(key, etext) == text) local etext = desencode(key, text, "pkcs7") assert(desdecode(key, etext, "pkcs7") == text) assert(desencode(key, "","pkcs7")=="/rlZt9RkL8s=") assert(desencode(key, "1","pkcs7")=="g6AtgJul6q0=") assert(desencode(key, "12","pkcs7")=="NefFpG+m1O4=") assert(desencode(key, "123","pkcs7")=="LDiFUdf0iew=") assert(desencode(key, "1234","pkcs7")=="T9u7dzBdi+w=") assert(desencode(key, "12345","pkcs7")=="AGgKdx/Qic8=") assert(desencode(key, "123456","pkcs7")=="ED5wLgc3Mnw=") assert(desencode(key, "1234567","pkcs7")=="mYo+BYIT41M=") assert(desencode(key, "12345678","pkcs7")=="ltACiHjVjIn+uVm31GQvyw==") skynet.start(skynet.exit) ================================================ FILE: test/testdatacenter.lua ================================================ local skynet = require "skynet" local datacenter = require "skynet.datacenter" local function f1() print("====1==== wait hello") print("\t1>",datacenter.wait ("hello")) print("====1==== wait key.foobar") print("\t1>", pcall(datacenter.wait,"key")) -- will failed, because "key" is a branch print("\t1>",datacenter.wait ("key", "foobar")) end local function f2() skynet.sleep(10) print("====2==== set key.foobar") datacenter.set("key", "foobar", "bingo") end skynet.start(function() datacenter.set("hello", "world") print(datacenter.get "hello") skynet.fork(f1) skynet.fork(f2) end) ================================================ FILE: test/testdatasheet.lua ================================================ local skynet = require "skynet" local mode = ... local function dump(t, prefix) for k,v in pairs(t) do print(prefix, k, v) if type(v) == "table" then dump(v, prefix .. "." .. k) end end end if mode == "child" then local datasheet = require "skynet.datasheet" skynet.start(function() local t = datasheet.query("foobar") dump(t, "[CHILD]") skynet.sleep(100) skynet.exit() end) else local builder = require "skynet.datasheet.builder" local datasheet = require "skynet.datasheet" skynet.start(function() builder.new("foobar", {a = 1, b = 2 , c = {3} }) skynet.newservice(SERVICE_NAME, "child") local t = datasheet.query "foobar" local c = t.c dump(t, "[1]") builder.update("foobar", { b = 4, c = { 5 } }) print("sleep") skynet.sleep(100) dump(t, "[2]") dump(c, "[2.c]") builder.update("foobar", { a = 6, c = 7, d = 8 }) print("sleep") skynet.sleep(100) dump(t, "[3]") end) end ================================================ FILE: test/testdeadcall.lua ================================================ local skynet = require "skynet" local mode = ... if mode == "test" then skynet.start(function() skynet.dispatch("lua", function (...) print("====>", ...) skynet.exit() end) end) elseif mode == "dead" then skynet.start(function() skynet.dispatch("lua", function (...) skynet.sleep(100) print("return", skynet.ret "") end) end) else skynet.start(function() local test = skynet.newservice(SERVICE_NAME, "test") -- launch self in test mode print(pcall(function() skynet.call(test,"lua", "dead call") end)) local dead = skynet.newservice(SERVICE_NAME, "dead") -- launch self in dead mode skynet.timeout(0, skynet.exit) -- exit after a while, so the call never return skynet.call(dead, "lua", "would not return") end) end ================================================ FILE: test/testdeadloop.lua ================================================ local skynet = require "skynet" local function dead_loop() while true do skynet.sleep(0) end end skynet.start(function() skynet.fork(dead_loop) end) ================================================ FILE: test/testdns.lua ================================================ local skynet = require "skynet" local dns = require "skynet.dns" local resolve_list = { "github.com", "stackoverflow.com", "lua.com", } skynet.start(function() -- you can specify the server like dns.server("8.8.4.4", 53) for _ , name in ipairs(resolve_list) do local ip, ips = dns.resolve(name) for k,v in ipairs(ips) do print(name,v) end skynet.sleep(500) -- sleep 5 sec end end) ================================================ FILE: test/testecho.lua ================================================ local skynet = require "skynet" local mode = ... if mode == "slave" then skynet.start(function() skynet.dispatch("lua", function(_,_, ...) skynet.ret(skynet.pack(...)) end) end) else skynet.start(function() local slave = skynet.newservice(SERVICE_NAME, "slave") local n = 100000 local start = skynet.now() print("call salve", n, "times in queue") for i=1,n do skynet.call(slave, "lua") end print("qps = ", n/ (skynet.now() - start) * 100) start = skynet.now() local worker = 10 local task = n/worker print("call salve", n, "times in parallel, worker = ", worker) for i=1,worker do skynet.fork(function() for i=1,task do skynet.call(slave, "lua") end worker = worker -1 if worker == 0 then print("qps = ", n/ (skynet.now() - start) * 100) end end) end end) end ================================================ FILE: test/testendless.lua ================================================ local skynet = require "skynet" skynet.start(function() for i = 1, 1000000000 do -- very long loop if i%100000000 == 0 then print("Endless = ", skynet.stat "endless") print("Cost time = ", skynet.stat "time") end end skynet.exit() end) ================================================ FILE: test/testhandle.lua ================================================ local skynet = require "skynet" require "skynet.manager" local mod = ... if mod == "slave" then skynet.start(function() skynet.error("addr:", skynet.self()) end) else skynet.start(function() skynet.newservice("debug_console",8000) skynet.error("master addr:", skynet.self()) skynet.newservice("testhandle", "slave") skynet.newservice("testhandle", "slave") skynet.newservice("testhandle", "slave") skynet.newservice("testhandle", "slave") skynet.newservice("testhandle", "slave") skynet.newservice("testhandle", "slave") while true do local addr = skynet.newservice("testhandle", "slave") skynet.kill(addr) if addr > 0xfffff0 then break end end end) end ================================================ FILE: test/testharborlink.lua ================================================ local skynet = require "skynet" local harbor = require "skynet.harbor" skynet.start(function() print("wait for harbor 2") print("run skynet examples/config_log please") harbor.connect(2) print("harbor 2 connected") print("LOG =", skynet.address(harbor.queryname "LOG")) harbor.link(2) print("disconnected") end) ================================================ FILE: test/testhttp.lua ================================================ local skynet = require "skynet" local httpc = require "http.httpc" local httpurl = require "http.url" local dns = require "skynet.dns" local function http_test(protocol) --httpc.dns() -- set dns server httpc.timeout = 100 -- set timeout 1 second print("GET baidu.com") protocol = protocol or "http" local respheader = {} local host = string.format("%s://baidu.com", protocol) print("geting... ".. host) local status, body = httpc.get(host, "/", respheader) print("[header] =====>") for k,v in pairs(respheader) do print(k,v) end print("[body] =====>", status) print(body) local respheader = {} local ip = dns.resolve "baidu.com" print(string.format("GET %s (baidu.com)", ip)) local status, body = httpc.get(host, "/", respheader, { host = "baidu.com" }) print(status) end local function http_stream_test() for resp, stream in httpc.request_stream("GET", "http://baidu.com", "/") do print("STATUS", stream.status) for k,v in pairs(stream.header) do print("HEADER",k,v) end print("BODY", resp) end end local function http_head_test() httpc.timeout = 100 local respheader = {} local status = httpc.head("http://baidu.com", "/", respheader) for k,v in pairs(respheader) do print("HEAD", k, v) end end local function http_url_test() local url = "http://baidu.com/get?k1=1&k2=2&k4=a%20space&k5=b%20space&k5=b%20space&k5=b%20space" local path, query = httpurl.parse(url) print("url", path, query) local qret = httpurl.parse_query(query) for k, v in pairs(qret) do print(k, v) end assert(#qret["k5"] == 3) assert(qret[1] == qret[2]) assert(qret[1] == qret[3]) end local function main() dns.server() http_stream_test() http_head_test() http_url_test() http_test("http") if not pcall(require,"ltls.c") then print "No ltls module, https is not supported" else http_test("https") end end skynet.start(function() print(pcall(main)) skynet.exit() end) ================================================ FILE: test/testmemlimit.lua ================================================ local skynet = require "skynet" local names = { "cluster", "skynet.db.dns", "skynet.db.mongo", "skynet.db.mysql", "skynet.db.redis", "sharedata", "skynet.socket", "sproto" } -- set sandbox memory limit to 1M, must set here (at start, out of skynet.start) skynet.memlimit(1 * 1024 * 1024) skynet.start(function() local a = {} local limit local ok, err = pcall(function() for i=1, 12355 do limit = i table.insert(a, {}) end end) local libs = {} for k,v in ipairs(names) do local ok, m = pcall(require, v) if ok then libs[v] = m end end skynet.error(limit, err) skynet.exit() end) ================================================ FILE: test/testmongodb.lua ================================================ local skynet = require "skynet" local mongo = require "skynet.db.mongo" local bson = require "bson" local host, port, db_name, username, password = ... if port then port = math.tointeger(port) end host = '127.0.0.1' port = 27017 username = "admin" password = 123456 db_name = "admin" -- print(host, port, db_name, username, password) local function _create_client() return mongo.client( { host = host, port = port, username = username, password = password, authdb = "admin", } ) end local function test_auth() local ok, err, ret local c = mongo.client( { host = host, port = port, } ) local db = c[db_name] db:auth(username, password) db.testcoll:dropIndex("*") db.testcoll:drop() ok, err, ret = db.testcoll:safe_insert({test_key = 1}); assert(ok and ret and ret.n == 1, err) ok, err, ret = db.testcoll:safe_insert({test_key = 1}); assert(ok and ret and ret.n == 1, err) end local function test_insert_without_index() local ok, err, ret local c = _create_client() local db = c[db_name] db.testcoll:dropIndex("*") db.testcoll:drop() ok, err, ret = db.testcoll:safe_insert({test_key = 1}); assert(ok and ret and ret.n == 1, err) ok, err, ret = db.testcoll:safe_insert({test_key = 1}); assert(ok and ret and ret.n == 1, err) end local function test_insert_with_index() local ok, err, ret local c = _create_client() local db = c[db_name] db.testcoll:dropIndex("*") db.testcoll:drop() db.testcoll:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"}) ok, err, ret = db.testcoll:safe_insert({test_key = 1}) assert(ok and ret and ret.n == 1, err) ok, err, ret = db.testcoll:safe_insert({test_key = 1}) assert(ok == false and string.find(err, "duplicate key error")) end local function test_find_and_remove() local ok, err, ret local c = _create_client() local db = c[db_name] db.testcoll:dropIndex("*") db.testcoll:drop() local cursor = db.testcoll:find() assert(cursor:hasNext() == false) db.testcoll:ensureIndex({test_key = 1}, {test_key2 = -1}, {unique = true, name = "test_index"}) ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_key2 = 1}) assert(ok and ret and ret.n == 1, err) cursor = db.testcoll:find() assert(cursor:hasNext() == true) local v = cursor:next() assert(v) assert(v.test_key == 1) ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_key2 = 2}) assert(ok and ret and ret.n == 1, err) ok, err, ret = db.testcoll:safe_insert({test_key = 2, test_key2 = 3}) assert(ok and ret and ret.n == 1, err) ret = db.testcoll:findOne({test_key2 = 1}) assert(ret and ret.test_key2 == 1, err) ret = db.testcoll:find({test_key2 = {['$gt'] = 0}}):sort({test_key = 1}, {test_key2 = -1}):skip(1):limit(1) assert(ret:count() == 3) assert(ret:count(true) == 1) if ret:hasNext() then ret = ret:next() end assert(ret and ret.test_key2 == 1) db.testcoll:delete({test_key = 1}) db.testcoll:delete({test_key = 2}) ret = db.testcoll:findOne({test_key = 1}) assert(ret == nil) end local function test_runcommand() local ok, err, ret local c = _create_client() local db = c[db_name] db.testcoll:dropIndex("*") db.testcoll:drop() ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_key2 = 1}) assert(ok and ret and ret.n == 1, err) ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_key2 = 2}) assert(ok and ret and ret.n == 1, err) ok, err, ret = db.testcoll:safe_insert({test_key = 2, test_key2 = 3}) assert(ok and ret and ret.n == 1, err) local pipeline = { { ["$group"] = { _id = mongo.null, test_key_total = { ["$sum"] = "$test_key"}, test_key2_total = { ["$sum"] = "$test_key2" }, } } } ret = db:runCommand("aggregate", "testcoll", "pipeline", pipeline, "cursor", {}) assert(ret and ret.cursor.firstBatch[1].test_key_total == 4) assert(ret and ret.cursor.firstBatch[1].test_key2_total == 6) end local function test_expire_index() local ok, err, ret local c = _create_client() local db = c[db_name] db.testcoll:dropIndex("*") db.testcoll:drop() db.testcoll:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index", expireAfterSeconds = 1, }) db.testcoll:ensureIndex({test_date = 1}, {expireAfterSeconds = 1, }) ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_date = bson.date(os.time())}) assert(ok and ret and ret.n == 1, err) ret = db.testcoll:findOne({test_key = 1}) assert(ret and ret.test_key == 1) for i = 1, 60 do skynet.sleep(100); print("check expire", i) ret = db.testcoll:findOne({test_key = 1}) if ret == nil then return end end print("test expire index failed") assert(false, "test expire index failed"); end local function test_safe_batch_insert() local ok, err, ret local c = _create_client() local db = c[db_name] db.testcoll:drop() local docs, length = {}, 10 for i = 1, length do table.insert(docs, {test_key = i}) end db.testcoll:safe_batch_insert(docs) local ret = db.testcoll:find() assert(length == ret:count(), "test safe batch insert failed") end local function test_safe_batch_delete() local ok, err, ret local c = _create_client() local db = c[db_name] db.testcoll:drop() local docs, length = {}, 10 for i = 1, length do table.insert(docs, {test_key = i}) end db.testcoll:safe_batch_insert(docs) docs = {} local del_num = 5 for i = 1, del_num do table.insert(docs, {test_key = i}) end db.testcoll:safe_batch_delete(docs) local ret = db.testcoll:find() assert((length - del_num) == ret:count(), "test safe batch delete failed") end local function test_safe_update() local ok, err, ret local c = _create_client() local db = c[db_name] db.testcoll:drop() db.testcoll:safe_insert({test_key = 100, test_value = "hello mongo"}) db.testcoll:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"}) local query = {test_key = 100} local update = {test_value = "hi mongo"} ok, err = db.testcoll:safe_update(query, {['$set'] = update}) assert(ok, err) ret = db.testcoll:findOne(query) assert(ret.test_value == "hi mongo") end skynet.start(function() if username then print("Test auth") test_auth() end print("Test insert without index") test_insert_without_index() print("Test insert index") test_insert_with_index() print("Test find and remove") test_find_and_remove() print("Test runCommand") test_runcommand() print("Test expire index") test_expire_index() print("test safe batch insert") test_safe_batch_insert() print("test safe batch delete") test_safe_batch_delete() print("test_safe_update") test_safe_update() print("mongodb test finish."); end) ================================================ FILE: test/testmulticast.lua ================================================ local skynet = require "skynet" local mc = require "skynet.multicast" local dc = require "skynet.datacenter" local mode = ... if mode == "sub" then skynet.start(function() skynet.dispatch("lua", function (_,_, cmd, channel) assert(cmd == "init") local c = mc.new { channel = channel , dispatch = function (channel, source, ...) print(string.format("%s <=== %s %s",skynet.address(skynet.self()),skynet.address(source), channel), ...) end } print(skynet.address(skynet.self()), "sub", c) c:subscribe() skynet.ret(skynet.pack()) end) end) else skynet.start(function() local channel = mc.new() print("New channel", channel) for i=1,10 do local sub = skynet.newservice(SERVICE_NAME, "sub") skynet.call(sub, "lua", "init", channel.channel) end dc.set("MCCHANNEL", channel.channel) -- for multi node test print(skynet.address(skynet.self()), "===>", channel) channel:publish("Hello World") end) end ================================================ FILE: test/testmulticast2.lua ================================================ local skynet = require "skynet" local dc = require "skynet.datacenter" local mc = require "skynet.multicast" skynet.start(function() print("remote start") local console = skynet.newservice("console") local channel = dc.get "MCCHANNEL" if channel then print("remote channel", channel) else print("create local channel") end for i=1,10 do local sub = skynet.newservice("testmulticast", "sub") skynet.call(sub, "lua", "init", channel) end local c = mc.new { channel = channel , dispatch = function(...) print("======>", ...) end, } c:subscribe() c:publish("Remote message") c:unsubscribe() c:publish("Remote message2") c:delete() skynet.exit() end) ================================================ FILE: test/testmysql.lua ================================================ local skynet = require "skynet" local mysql = require "skynet.db.mysql" local function dump(obj) local getIndent, quoteStr, wrapKey, wrapVal, dumpObj getIndent = function(level) return string.rep("\t", level) end quoteStr = function(str) return '"' .. string.gsub(str, '"', '\\"') .. '"' end wrapKey = function(val) if type(val) == "number" then return "[" .. val .. "]" elseif type(val) == "string" then return "[" .. quoteStr(val) .. "]" else return "[" .. tostring(val) .. "]" end end wrapVal = function(val, level) if type(val) == "table" then return dumpObj(val, level) elseif type(val) == "number" then return val elseif type(val) == "string" then return quoteStr(val) else return tostring(val) end end dumpObj = function(obj, level) if type(obj) ~= "table" then return wrapVal(obj) end level = level + 1 local tokens = {} tokens[#tokens + 1] = "{" for k, v in pairs(obj) do tokens[#tokens + 1] = getIndent(level) .. wrapKey(k) .. " = " .. wrapVal(v, level) .. "," end tokens[#tokens + 1] = getIndent(level - 1) .. "}" return table.concat(tokens, "\n") end return dumpObj(obj, 0) end local function test2( db) local i=1 while true do local res = db:query("select * from cats order by id asc") print ( "test2 loop times=" ,i,"\n","query result=",dump( res ) ) res = db:query("select * from cats order by id asc") print ( "test2 loop times=" ,i,"\n","query result=",dump( res ) ) skynet.sleep(1000) i=i+1 end end local function test3( db) local i=1 while true do local res = db:query("select * from cats order by id asc") print ( "test3 loop times=" ,i,"\n","query result=",dump( res ) ) res = db:query("select * from cats order by id asc") print ( "test3 loop times=" ,i,"\n","query result=",dump( res ) ) skynet.sleep(1000) i=i+1 end end local function test4( db) local stmt = db:prepare("SELECT * FROM cats WHERE name=?") print ( "test4 prepare result=",dump( stmt ) ) local res = db:execute(stmt,'Bob') print ( "test4 query result=",dump( res ) ) db:stmt_close(stmt) end -- 测试存储过程和blob读写 local function test_sp_blob(db) print("test stored procedure") -- 创建测试表 db:query "DROP TABLE IF EXISTS `test`" db:query [[ CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `str` varchar(45) COLLATE utf8mb4_bin DEFAULT NULL, `dt` timestamp NULL DEFAULT NULL, `flt` double DEFAULT NULL, `blb` mediumblob, `num` int(11) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id_UNIQUE` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; ]] -- 创建测试存储过程 db:query "DROP PROCEDURE IF EXISTS `get_test`" db:query [[ CREATE PROCEDURE `get_test`(IN p_id int) BEGIN select * from test where id=p_id; END ]] local stmt_insert = db:prepare("INSERT test (str,dt,flt,num,blb) VALUES (?,?,?,?,?)") local stmt_csp = db:prepare("call get_test(?)") local test_blob = string.char(0xFF,0x8F,0x03,0x04,0x0a,0x0b,0x0d,0x0e,0x10,0x20,0x30,0x40) local r = db:execute(stmt_insert,'test_str','2020-3-20 15:30:40',3.1415,89,test_blob) print("insert result : insert_id",r.insert_id,"affected_rows",r.affected_rows ,"server_status",r.server_status,"warning_count",r.warning_count) r = db:execute(stmt_csp,1) local rs = r[1][1] print("call get_test() result : str",rs.str,"dt",rs.dt,"flt",rs.flt,"num",rs.num ,"blb len",#rs.blb,"equal",test_blob==rs.blb) print("test stored procedure ok") end local function test_signed(db) local res = db:query("drop table if exists test_i_u") res = db:query("create table test_i_u (i tinyint primary key, u tinyint unsigned)") print(dump(res)) res = db:query("insert into test_i_u (i,u) values (-1,1),(127,128),(-127,255)") print(dump(res)) local prep = "SELECT * FROM test_i_u" local stmt = db:prepare(prep) local res = db:execute(stmt) print("test_i_u: ", dump(res)) db:stmt_close(stmt) end skynet.start(function() local function on_connect(db) db:query("set charset utf8mb4"); end local db=mysql.connect({ host="127.0.0.1", port=3306, database="skynet", user="root", password="123456", charset="utf8mb4", max_packet_size = 1024 * 1024, on_connect = on_connect }) if not db then print("failed to connect") end print("testmysql success to connect to mysql server") local res = db:query("drop table if exists cats") res = db:query("create table cats " .."(id serial primary key, ".. "name varchar(5))") print( dump( res ) ) res = db:query("insert into cats (name) " .. "values (\'Bob\'),(\'\'),(null)") print ( dump( res ) ) res = db:query("select * from cats order by id asc") print ( dump( res ) ) -- 测试存储过程和二进制blob test_sp_blob(db) test_signed(db) -- test in another coroutine skynet.fork( test2, db) skynet.fork( test3, db) skynet.fork( test4, db) -- multiresultset test res = db:query("select * from cats order by id asc ; select * from cats") print ("multiresultset test result=", dump( res ) ) print ("escape string test result=", mysql.quote_sql_str([[\mysql escape %string test'test"]]) ) -- bad sql statement local res = db:query("select * from notexisttable" ) print( "bad query test result=" ,dump(res) ) local i=1 while true do local res = db:query("select * from cats order by id asc") print ( "test1 loop times=" ,i,"\n","query result=",dump( res ) ) res = db:query("select * from cats order by id asc") print ( "test1 loop times=" ,i,"\n","query result=",dump( res ) ) skynet.sleep(1000) i=i+1 end --db:disconnect() --skynet.exit() end) ================================================ FILE: test/testoverload.lua ================================================ local skynet = require "skynet" local mode = ... if mode == "slave" then local CMD = {} function CMD.sum(n) skynet.error("for loop begin") local s = 0 for i = 1, n do s = s + i end skynet.error("for loop end") end function CMD.blackhole() end skynet.start(function() skynet.dispatch("lua", function(_,_, cmd, ...) local f = CMD[cmd] f(...) end) end) else skynet.start(function() local slave = skynet.newservice(SERVICE_NAME, "slave") for step = 1, 20 do skynet.error("overload test ".. step) for i = 1, 512 * step do skynet.send(slave, "lua", "blackhole") end skynet.sleep(step) end local n = 1000000000 skynet.error(string.format("endless test n=%d", n)) skynet.send(slave, "lua", "sum", n) end) end ================================================ FILE: test/testping.lua ================================================ local skynet = require "skynet" local snax = require "skynet.snax" skynet.start(function() skynet.trace() local ps = snax.newservice ("pingserver", "hello world") print(ps.req.ping("foobar")) print(ps.post.hello()) print(pcall(ps.req.error)) print("Hotfix (i) :", snax.hotfix(ps, [[ local i local hello function accept.hello() i = i + 1 print ("fix", i, hello) end function hotfix(...) local temp = i i = 100 return temp end ]])) print(ps.post.hello()) local info = skynet.call(ps.handle, "debug", "INFO") for name,v in pairs(info) do print(string.format("%s\tcount:%d time:%f", name, v.count, v.time)) end print(ps.post.exit("exit")) -- == snax.kill(ps, "exit") skynet.exit() end) ================================================ FILE: test/testpipeline.lua ================================================ local skynet = require "skynet" local redis = require "skynet.db.redis" local conf = { host = "127.0.0.1", port = 6379, db = 0 } local function read_table(t) local result = { } for i = 1, #t, 2 do result[t[i]] = t[i + 1] end return result end skynet.start(function() local db = redis.connect(conf) db.pipelining = function (self, block) local ops = {} block(setmetatable({}, { __index = function (_, name) return function (_, ...) table.insert(ops, {name, ...}) end end })) return self:pipeline(ops) end do print("test function") local ret = db:pipelining(function (red) red:multi() red:hincrby("hello", 1, 1) red:del("hello") red:hmset("hello", 1, 1, 2, 2, 3, 3) red:hgetall("hello") red:exec() end) -- ret is the result of red:exec() for k, v in pairs(read_table(ret[4])) do print(k, v) end end do print("test table") local ret = db:pipeline({ {"hincrby", "hello", 1, 1}, {"del", "hello"}, {"hmset", "hello", 1, 1, 2, 2, 3, 3}, {"hgetall", "hello"}, }, {}) -- offer a {} for result print(ret[1].out) print(ret[2].out) print(ret[3].out) for k, v in pairs(read_table(ret[4].out)) do print(k, v) end end db:disconnect() skynet.exit() end) ================================================ FILE: test/testqueue.lua ================================================ local skynet = require "skynet" local snax = require "skynet.snax" skynet.start(function() local ps = snax.uniqueservice ("pingserver", "test queue") for i=1, 10 do ps.post.sleep(true,i*10) ps.post.hello() end for i=1, 10 do ps.post.sleep(false,i*10) ps.post.hello() end skynet.exit() end) ================================================ FILE: test/testredis.lua ================================================ local skynet = require "skynet" local redis = require "skynet.db.redis" local conf = { host = "127.0.0.1" , port = 6379 , db = 0 } local function watching() local w = redis.watch(conf) w:subscribe "foo" w:psubscribe "hello.*" while true do print("Watch", w:message()) end end skynet.start(function() skynet.fork(watching) local db = redis.connect(conf) db:del "C" db:set("A", "hello") db:set("B", "world") db:sadd("C", "one") print(db:get("A")) print(db:get("B")) db:del "D" for i=1,10 do db:hset("D",i,i) end local r = db:hvals "D" for k,v in pairs(r) do print(k,v) end db:multi() db:get "A" db:get "B" local t = db:exec() for k,v in ipairs(t) do print("Exec", v) end print(db:exists "A") print(db:get "A") print(db:set("A","hello world")) print(db:get("A")) print(db:sismember("C","one")) print(db:sismember("C","two")) print("===========publish============") for i=1,10 do db:publish("foo", i) end for i=11,20 do db:publish("hello.foo", i) end db:disconnect() -- skynet.exit() end) ================================================ FILE: test/testredis2.lua ================================================ local skynet = require "skynet" local redis = require "skynet.db.redis" local db function add1(key, count) local t = {} for i = 1, count do t[2*i -1] = "key" ..i t[2*i] = "value" .. i end db:hmset(key, table.unpack(t)) end function add2(key, count) local t = {} for i = 1, count do t[2*i -1] = "key" ..i t[2*i] = "value" .. i end table.insert(t, 1, key) db:hmset(t) end function __init__() db = redis.connect { host = "127.0.0.1", port = 6300, db = 0, auth = "foobared" } print("dbsize:", db:dbsize()) local ok, msg = xpcall(add1, debug.traceback, "test1", 250000) if not ok then print("add1 failed", msg) else print("add1 succeed") end local ok, msg = xpcall(add2, debug.traceback, "test2", 250000) if not ok then print("add2 failed", msg) else print("add2 succeed") end print("dbsize:", db:dbsize()) print("redistest launched") end skynet.start(__init__) ================================================ FILE: test/testrediscluster.lua ================================================ local skynet = require "skynet" local rediscluster = require "skynet.db.redis.cluster" local test_more = ... -- subscribe mode's callback local function onmessage(data,channel,pchannel) print("onmessage",data,channel,pchannel) end skynet.start(function () local db = rediscluster.new({ {host="127.0.0.1",port=7000}, {host="127.0.0.1",port=7001},}, {read_slave=true,auth=nil,db=0,}, onmessage ) db:del("list") db:del("map") db:rpush("list",1,2,3) local list = db:lrange("list",0,-1) for i,v in ipairs(list) do print(v) end db:hmset("map","key1",1,"key2",2) local map = db:hgetall("map") for i=1,#map,2 do local key = map[i] local val = map[i+1] print(key,val) end -- test MOVED db:flush_slots_cache() print(db:set("A",1)) print(db:get("A")) -- reconnect local cnt = 0 for name,conn in pairs(db.connections) do print(name,conn) cnt = cnt + 1 end print("cnt:",cnt) db:close_all_connection() print(db:set("A",1)) print(db:del("A")) local slot = db:keyslot("{foo}") local conn = db:get_connection_by_slot(slot) -- You must ensure keys at one slot: use same key or hash tags local ret = conn:pipeline({ {"hincrby", "{foo}hello", 1, 1}, {"del", "{foo}hello"}, {"hmset", "{foo}hello", 1, 1, 2, 2, 3, 3}, {"hgetall", "{foo}hello"}, },{}) print(ret[1].ok,ret[1].out) print(ret[2].ok,ret[2].out) print(ret[3].ok,ret[3].out) print(ret[4].ok) if ret[4].ok then for i,v in ipairs(ret[4].out) do print(v) end else print(ret[4].out) end -- dbsize/info/keys local conn = db:get_random_connection() print("dbsize:",conn:dbsize()) print("info:",conn:info()) local keys = conn:keys("list*") for i,key in ipairs(keys) do print(key) end print("cluster nodes") local nodes = db:cluster("nodes") print(nodes) print("cluster slots") local slots = db:cluster("slots") for i,slot_map in ipairs(slots) do local start_slot = slot_map[1] local end_slot = slot_map[2] local master_node = slot_map[3] print(start_slot,end_slot) for i,v in ipairs(master_node) do print(v) end for i=4,#slot_map do local slave_node = slot_map[i] for i,v in ipairs(slave_node) do print(v) end end end -- test subscribe/publish db:subscribe("world") db:subscribe("myteam") db:publish("world","hello,world") db:publish("myteam","hello,my team") -- low-version(such as 3.0.2) redis-server psubscribe is locally -- if publish and psubscribe not map to same node, -- we may lost message. so upgrade your redis or use tag to resolved! db:psubscribe("{tag}*team") db:publish("{tag}1team","hello,1team") db:publish("{tag}2team","hello,2team") -- i test in redis-4.0.9, it's ok db:psubscribe("*team") db:publish("1team","hello,1team") db:publish("2team","hello,2team") -- test eval db:set("A",1) local script = [[ if redis.call("get",KEYS[1]) == ARGV[1] then return "ok" else return "fail" end]] print("eval#get",db:eval(script,1,"A",1)) db:del("A") if not test_more then skynet.exit() return end local last = false while not last do last = db:get("__last__") if last == nil then last = 0 end last = tonumber(last) end for val=last,1000000000 do local ok,errmsg = pcall(function () local key = string.format("foo%s",val) db:set(key,val) print(key,db:get(key)) db:set("__last__",val) end) if not ok then print("error:",errmsg) end end skynet.exit() end) ================================================ FILE: test/testresponse.lua ================================================ local skynet = require "skynet" local mode = ... if mode == "TICK" then -- this service whould response the request every 1s. local response_queue = {} local function response() while true do skynet.sleep(100) -- sleep 1s for k,v in ipairs(response_queue) do v(true, skynet.now()) -- true means succ, false means error response_queue[k] = nil end end end skynet.start(function() skynet.fork(response) skynet.dispatch("lua", function() table.insert(response_queue, skynet.response()) end) end) else local function request(tick, i) print(i, "call", skynet.now()) print(i, "response", skynet.call(tick, "lua")) print(i, "end", skynet.now()) end skynet.start(function() local tick = skynet.newservice(SERVICE_NAME, "TICK") for i=1,5 do skynet.fork(request, tick, i) skynet.sleep(10) end end) end ================================================ FILE: test/testselect.lua ================================================ local skynet = require "skynet" local mode = ... if mode == "slave" then local COMMAND = {} function COMMAND.ping(ti, str) skynet.sleep(ti) return str end function COMMAND.error() error "ERROR" end function COMMAND.exit() skynet.exit() end skynet.start(function() skynet.dispatch("lua", function(_,_, cmd, ...) skynet.ret(skynet.pack(COMMAND[cmd](...))) end) end) else local function info(fmt, ...) skynet.error(string.format(fmt, ...)) end skynet.start(function() local slave = skynet.newservice(SERVICE_NAME, "slave") for req, resp in skynet.request { slave, "lua", "ping", 6, "SLEEP 6" } { slave, "lua", "ping", 5, "SLEEP 5" } { slave, "lua", "ping", 4, "SLEEP 4" } { slave, "lua", "ping", 3, "SLEEP 3" } { slave, "lua", "ping", 2, "SLEEP 2" } { slave, "lua", "ping", 1, "SLEEP 1" } :select() do info("RESP %s", resp[1]) end -- test timeout local reqs = skynet.request() for i = 1, 10 do reqs:add { slave, "lua", "ping", i*10, "SLEEP " .. i, token = i } end for req, resp in reqs:select(50) do info("RESP %s token<%s>", resp[1], req.token) end -- test error for req, resp in skynet.request { slave, "lua", "error" } { slave, "lua", "ping", 0, "PING" } : select() do if resp then info("Ping : %s", resp[1]) else info("Error") end end -- timeout call local reqs = skynet.request { slave, "lua", "ping", 100 , "PING" } for req, resp in reqs:select(10) do info("%s", resp[1]) end info("Timeout : %s", reqs.timeout) -- call in select for req, resp in skynet.request { slave, "lua", "ping", 20, "CALL 20" } { slave, "lua", "ping", 10, "CALL 10" } : select() do info("%s", skynet.call( slave, "lua", "ping", 0, "ping in " .. resp[1]) ) skynet.sleep(50) end skynet.send(slave, "lua", "exit") skynet.exit() end) end ================================================ FILE: test/testservice/init.lua ================================================ local skynet = require "skynet" local kvdb = require "kvdb" local function dbname(i) return "db"..i end skynet.start(function() for i=1,10 do kvdb.new(dbname(i)) end local idx=1 for i=1,10 do local db=dbname(i) kvdb.set(db,"A",idx) idx=idx+1 kvdb.set(db,"B",idx) idx=idx+1 end for i=1,10 do local db=dbname(i) print(db,kvdb.get(db,"A"),kvdb.get(db,"B")) end end) ================================================ FILE: test/testservice/kvdb.lua ================================================ local skynet = require "skynet" local service = require "skynet.service" local kvdb = {} function kvdb.get(db,key) return skynet.call(service.query(db), "lua", "get", key) end function kvdb.set(db,key, value) skynet.call(service.query(db), "lua", "set", key , value) end -- this function will be injected into an unique service, so don't refer any upvalues local function service_mainfunc(...) local skynet = require "skynet" skynet.error(...) -- (...) passed from service.new local db = {} local command = {} function command.get(key) return db[key] end function command.set(key, value) db[key] = value end -- skynet.start is compatible skynet.dispatch("lua", function(session, address, cmd, ...) skynet.ret(skynet.pack(command[cmd](...))) end) end function kvdb.new(db) return service.new(db, service_mainfunc, "Service Init") end return kvdb ================================================ FILE: test/testsha.lua ================================================ local skynet = require "skynet" local crypt = require "skynet.crypt" local function sha1(text) local c = crypt.sha1(text) return crypt.hexencode(c) end local function hmac_sha1(key, text) local c = crypt.hmac_sha1(key, text) return crypt.hexencode(c) end -- test case from http://regex.info/code/sha1.lua print(1) assert(sha1 "http://regex.info/blog/" == "7f103bf600de51dfe91062300c14738b32725db5", 1) print(2) assert(sha1(string.rep("a", 10000)) == "a080cbda64850abb7b7f67ee875ba068074ff6fe", 2) print(3) assert(sha1 "abc" == "a9993e364706816aba3e25717850c26c9cd0d89d", 3) print(4) assert(sha1 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" == "84983e441c3bd26ebaae4aa1f95129e5e54670f1", 4) print(5) assert(sha1 "The quick brown fox jumps over the lazy dog" == "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", 5) print(6) assert(sha1 "The quick brown fox jumps over the lazy cog" == "de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3", 6) print(7) assert("efb750130b6cc9adf4be219435e575442ec68b7c" == sha1(string.char(136,43,218,202,158,86,64,140,154,173,20,184,170,125,37,54,208,68,171,24,164,89,142,111,148,235,187,181,122):rep(76)), 7) print(8) assert("432dff9d4023e13194170287103d0377ed182d96" == sha1(string.char(20,174):rep(407)), 8) print(9) assert("ccba5c47946530726bb86034dbee1dbf0c203e99" == sha1(string.char(20,54,149,252,176,4,96,100,223):rep(753)), 9) print(10) assert("4d6fea4f8576cd6648ae2d2ee4dc5df0a8309115" == sha1(string.char(118,171,221,33,54,209,223,152,35,67,88,50):rep(985)), 10) print(11) assert("f560412aabf813d01f15fdc6650489584aabd266" == sha1(string.char(23,85,29,13,146,55,164,14,206,196,109,183,53,92,97,123,242,220,112,15,43,113,22,246,114,29,209,219,190):rep(177)), 11) print(12) assert("b56795e12f3857b3ba1cbbcedb4d92dd9c419328" == sha1(string.char(21,216):rep(131)), 12) print(13) assert("54f292ecb7561e8ce27984685b427234c9465095" == sha1(string.char(15,205,12,181,4,114,128,118,219):rep(818)), 13) print(14) assert("fe265c1b5a848e5f3ada94a7e1bb98a8ce319835" == sha1(string.char(136,165,10,46,167,184,86,255,58,206,237,21,255,15,198,211,145,112,228,146,26,69,92,158,182,165,244,39,152):rep(605)), 14) print(15) assert("96f186998825075d528646059edadb55fdd96659" == sha1(string.char(100,226,28,248,132,70,221,54,92,181,82,128,191,12,250,244):rep(94)), 15) print(16) assert("e29c68e4d6ffd3998b2180015be9caee59dd8c8a" == sha1(string.char(247,14,15,163,0,53,50,113,84,121):rep(967)), 16) print(17) assert("6d2332a82b3600cbc5d2417f944c38be9f1081ae" == sha1(string.char(93,98,119,201,41,27,89,144,25,141,117,26,111,132):rep(632)), 17) print(18) assert("d84a91da8fb3aa7cd59b99f347113939406ef8eb" == sha1(string.char(28,252,0,4,150,164,91):rep(568)), 18) print(19) assert("8edf1b92ad5a90ed762def9a873a799b4bda97f9" == sha1(string.char(166,67,113,111,161,253,169,195,158,97,96,150,49,219,103,16,186,184,37,109,228,111):rep(135)), 19) print(20) assert("d5b2c7019f9ff2f75c38dc040c827ab9d1a42157" == sha1(string.char(38,252,110,224,168,60,2,133,8,153,200,0,199,104,191,62,28,168,73,48,199,217,83):rep(168)), 20) print(21) assert("5aeb57041bfada3b72e3514f493d7b9f4ca96620" == sha1(string.char(57):rep(738)), 21) print(22) assert("4548238c8c2124c6398427ed447ae8abbb8ead27" == sha1(string.char(221,131,171):rep(230)), 22) print(23) assert("ed0960b87a790a24eb2890d8ea8b18043f1a87d5" == sha1(string.char(151,113,144,19,249,148,75,51,164,233,102,232,3,58,81,99,101,255,93,231,147,150,212,216,109,62):rep(110)), 23) print(24) assert("d7ac6233298783af55901907bb99c13b2afbca99" == sha1(string.char(44,239,189,203,196,79,82,143,99,21,125,75,167,26,108,161,9,193,72):rep(919)), 24) print(25) assert("43600b41a3c5267e625bbdfde95027429c330c60" == sha1(string.char(122,70,129,24,192,213,205,224,62,79,81,129,22,171):rep(578)), 25) print(26) assert("5816df09a78e4594c1b02b170aa57333162def38" == sha1(string.char(76,103,48,150,115,161,86,42,247,82,197,213,155,108,215,18,119):rep(480)), 26) print(27) assert("ef7903b1e811a086a9a5a5142242132e1367ae1d" == sha1(string.char(143,70):rep(65)), 27) print(28) assert("6e16b2dac71e338a4bd26f182fdd5a2de3c30e6c" == sha1(string.char(130,114,144,219,245,72,205,44,149,68,150,169,243):rep(197)), 28) print(29) assert("6cc772f978ca5ef257273f046030f84b170f90c9" == sha1(string.char(26,49,141,64,30,61,12):rep(362)), 29) print(30) assert("54231fc19a04a64fc1aa3dce0882678b04062012" == sha1(string.char(76,252,160,253,253,167,27,179,237,15,219,46,141,255,23,53,184,190,233,125,211,11):rep(741)), 30) print(31) assert("5511a993b808e572999e508c3ce27d5f12bb4730" == sha1(string.char(197,139,184,188,200,31,171,236,252,147,123,75,7,138,111,167,68,114,73,80,51,233,241,233,91):rep(528)), 31) print(32) assert("64c4577d4763c95f47ac5d21a292836a34b8b124" == sha1(string.char(177,114,100,216,18,57,2,110,108,60,81,80,253,144,179,47,228,42,105,72,86,18,30,167):rep(901)), 32) print(33) assert("bb0467117e3b630c2b3d9cdf063a7e6766a3eae1" == sha1(string.char(249,99,174,228,15,211,121,152,203,115,197,198,66,17,196,6,159,170,116):rep(800)), 33) print(34) assert("1f9c66fca93bc33a071eef7d25cf0b492861e679" == sha1(string.char(205,64,237,65,171,0,176,17,104,6,101,29,128,200,214,24,32,91,115,71,26,11,226,69,141,83,249,129):rep(288)), 34) print(35) assert("55d228d9bfd522105fe1e1f1b5b09a1e8ee9f782" == sha1(string.char(96,37,252,185,137,194,215,191,190,235,73,224,125,18,146,74,32,82,58,95,49,102,85,57,241,54,55):rep(352)), 35) print(36) assert("58c3167e666bf3b4062315a84a72172688ad08b1" == sha1(string.char(65,91,96,147,212,18,32,144,138,187,70,26,105,42,71,13,229,137,185,10,86,124,171,204,104,42,2,172):rep(413)), 36) print(37) assert("f97936ca990d1c11a9967fd12fc717dcd10b8e9e" == sha1(string.char(253,159,59,76,230,153,22,198,15,9,223,3,31):rep(518)), 37) print(38) assert("5d15229ad10d2276d45c54b83fc0879579c2828e" == sha1(string.char(149,20,176,144,39,216,82,80,56,38,152,49,167,120,222,20,26,51,157,131,160,52,6):rep(895)), 38) print(39) assert("3757d3e98d46205a6b129e09b0beefaa0e453e64" == sha1(string.char(120,131,113,78,7,19,59,120,210,220,73,118,36,240,64,46,149,3,120,223,80,232,255,212,250,76,109,108,133):rep(724)), 39) print(40) assert("5e62539caa6c16752739f4f9fd33ca9032fff7e1" == sha1(string.char(216,240,166,165,2,203,2,189,137,219,231,229):rep(61)), 40) print(41) assert("3ff1c031417e7e9a34ce21be6d26033f66cb72c9" == sha1(string.char(4,178,215,183,17,198,184,253,137,108,178,74,244,126,32):rep(942)), 41) print(42) assert("8c20831fc3c652e5ce53b9612878e0478ab11ee6" == sha1(string.char(115,157,59,188,221,67,52,151,147,233,84,30,243,250,109,103,101,0,219,13,176,38,21):rep(767)), 42) print(43) assert("09c7c977cb39893c096449770e1ed75eebb9e5a1" == sha1(string.char(184,131,17,61,201,164,19,25,36,141,173,74,134,132,104,23,104,136,121,232,12,203,115,111,54,114,251,223,61,126):rep(458)), 43) print(44) assert("9534d690768bc85d2919a059b05561ec94547fc2" == sha1(string.char(49,93,136,112,92,42,117,28,31):rep(187)), 44) print(45) assert("7dfca0671de92a62de78f63c0921ff087f2ba61d" == sha1(string.char(194,78,252,112,175,6,26,103,4,47,195,99,78,130,40,58,84,175,240,180,255,108,3,42,51,111,35,49,217,160):rep(72)), 45) print(46) assert("62bf20c51473c6a0f23752e369cabd6c167c9415" == sha1(string.char(28,126,243,196,155,31,158,50):rep(166)), 46) print(47) assert("2ece95e43aba523cdbf248d07c05f569ecd0bd12" == sha1(string.char(76,230,117,248,231,228):rep(294)), 47) print(48) assert("722752e863386b737f29a08f23a0ec21c4313519" == sha1(string.char(61,102,1,118):rep(470)), 48) print(49) assert("a638db01b5af10a828c6e5b73f4ca881974124a0" == sha1(string.char(130,8,4):rep(768)), 49) print(50) assert("54c7f932548703cc75877195276bc2aa9643cf9b" == sha1(string.char(193,232,122,142,213,243,224,29,201,6,127,45,4,36,92,200,148,111,106,110,221,235,197,51,66,221,123,155,222,186):rep(290)), 50) print(51) assert("ecfc29397445d85c0f6dd6dc50a1272accba0920" == sha1(string.char(221,76,21,148,12,109,232,113,230,110,96,82,36):rep(196)), 51) print(52) assert("31d966d9540f77b49598fa22be4b599c3ba307aa" == sha1(string.char(148,237,212,223,44,133,153):rep(53)), 52) print(53) assert("9f97c8ace98db9f61d173bf2b705404eb2e9e283" == sha1(string.char(190,233,29,208,161,231,248,214,210):rep(451)), 53) print(54) assert("63449cfce29849d882d9552947ebf82920392aea" == sha1(string.char(216,12,113,137,33,99,200,140,6,222,170,2,115,50,138,134,211,244,176,250,42,95):rep(721)), 54) print(55) assert("15dc62f4469fb9eae76cd86a84d576905c4bbfe7" == sha1(string.char(50,194,13,88,156,226,39,135,165,204):rep(417)), 55) print(56) assert("abbc342bcfdb67944466e7086d284500e25aa103" == sha1(string.char(35,39,227,31,206,163,148,163,172,253,98,21,215,43,226,227,130,151,236,177,176,63,30,47,74):rep(960)), 56) print(57) assert("77ae3a7d3948eaa2c60d6bc165ba2812122f0cce" == sha1(string.char(166,83,82,49,153,89,58,79,131,163,125,18,43,135,120,17,48,94,136):rep(599)), 57) print(58) assert("d62e1c4395c8657ab1b1c776b924b29a8e009de1" == sha1(string.char(196,199,71,80,10,233,9,229,91,72,73,205,75,77,122,243,219,152):rep(964)), 58) print(59) assert("15d98d5279651f4a2566eda6eec573812d050ff7" == sha1(string.char(78,70,7,229,21,78,164,158,114,232,100,102,92,18,133,160,56,177,160,49,168,149,13,39,249,214,54,41):rep(618)), 59) print(60) assert("c3d9bc6535736f09fbb018427c994e58bbcb98f6" == sha1(string.char(129,208,110,40,135,3):rep(618)), 60) print(61) assert("7dc6b3f309cbe9fa3b2947c2526870a39bf96dc4" == sha1(string.char(126,184,110,39,55,177,108,179,214,200,175,118,125,212,19,147,137,133,89,209,89,189,233,164,71,81,156,215,152):rep(908)), 61) print(62) assert("b2d4ca3e787a1e475f6608136e89134ae279be57" == sha1(string.char(182,80,145,53,128,194,228,155,53):rep(475)), 62) print(63) assert("fbe6b9c3a7442806c5b9491642c69f8e56fdd576" == sha1(string.char(9,208,72,179,222,245,140,143,123,111,236,241,40,36,49,68,61,16,169,124,104,42,136,82,172):rep(189)), 63) print(64) assert("f87d96380201954801004f6b82af1953427dfdcb" == sha1(string.char(153,133,37,2,24,150,93,242,223,68,202,54,118,141,76,35,100,137,13):rep(307)), 64) print(65) assert("a953e2d054dd77f75337dd9dfa58ec4d3978cfb4" == sha1(string.char(112,215,41,50,221,94):rep(155)), 65) print(66) assert("e5c3047f9abfdd60f0c386b9a820f11d7028bc70" == sha1(string.char(247,177,124,213,47,175,139,203,81,21,85):rep(766)), 66) print(67) assert("ee6fe88911a13abfc0006f8809f51b9de7f5920f" == sha1(string.char(81,84,151,242,186,133,39,245,175,79,66,170,246,216,0,88,100,190,137,2,146,58):rep(10)), 67) print(68) assert("091db84d93bb68349eecf6bfa9378251ecd85500" == sha1(string.char(180,71,122,187):rep(129)), 68) print(69) assert("d8041c7d65201cc5a77056a5c7eb94a524494754" == sha1(string.char(188,99,87,126,152,214,159,151,234,223,199,247,213,107,63,59,12,146,175,57,79,200,132,202):rep(81)), 69) print(70) assert("cca1d77faf56f70c82fcb7bc2c0ac9daf553adff" == sha1(string.char(199,1,184,22,113,25,95,87,169,114,130,205,125,159,170,99):rep(865)), 70) print(71) assert("c009245d9767d4f56464a7b3d6b8ad62eba5ddeb" == sha1(string.char(39,168,16,191,95,82,184,102,242,224,15,108):rep(175)), 71) print(72) assert("8ce115679600324b58dc8745e38d9bea0a9fb4d6" == sha1(string.char(164,247,250,142,139,131,158,241,27,36,81,239,26,233,105,64,38,249,151,75,142,17,56,217,125,74,132,213,213):rep(426)), 72) print(73) assert("75f1e55718fd7a3c27792634e70760c6a390d40f" == sha1(string.char(32,145,170,90,188,97,251,159,244,153,21,126,2,67,36,110,31,251,66):rep(659)), 73) print(74) assert("615722261d6ec8cef4a4e7698200582958193f26" == sha1(string.char(51,24,1,69,81,157,34,185,26,159,231,119):rep(994)), 74) print(75) assert("4f61d2c1b60d342c51bc47641e0993d42e89c0f9" == sha1(string.char(175,25,99,122,247,217,204,100,0,35,57,65,150,182,51,79,169,99,9,33,113,113,113):rep(211)), 75) print(76) assert("7d9842e115fc2af17a90a6d85416dbadb663cef3" == sha1(string.char(136,42,39,219,103,95,119,125,205,72,174,15,210,213,23,75,120,56,104,31,63,255,253,100,61,55):rep(668)), 76) print(77) assert("3eccab72b375de3ba95a9f0fa715ae13cae82c08" == sha1(string.char(190,254,173,227,195,41,49,122,135,57,100):rep(729)), 77) print(78) assert("7dd68f741731211e0442ce7251e56950e0d05f96" == sha1(string.char(101,155,117,8,143,40,192,100,162,121,142,191,92):rep(250)), 78) print(79) assert("5e98cdb7f6d7e4e2466f9be23ec20d9177c5ddff" == sha1(string.char(84,67,182,136,89):rep(551)), 79) print(80) assert("dba1c76e22e2c391bde336c50319fbff2d66c3bb" == sha1(string.char(99,226,185,1,192):rep(702)), 80) print(81) assert("4b160b8bfe24147b01a247bfdc7a5296b6354e38" == sha1(string.char(144,141,254,1,166,144,129,232,203,31,192,75,145,12):rep(724)), 81) print(82) assert("27625ad9833144f6b818ef1cf54245dd4897d8aa" == sha1(string.char(31,187,82,156,224,133,116,251,180,165,246,8):rep(661)), 82) print(83) assert("0ce5e059d22a7ba5e2af9f0c6551d010b08ba197" == sha1(string.char(228):rep(672)), 83) print(84) assert("290982513a7f67a876c043d3c7819facb9082ea6" == sha1(string.char(150,44,52,144,68,76,207,114,106,153,99,39,219,81,73,140,71,4,228,220,55,244,210,225,221,32):rep(881)), 84) print(85) assert("14cf924aafceea393a8eb5dd06616c1fe1ccd735" == sha1(string.char(140,194,247,253,117,121,184,216,249,84,41,12,199):rep(738)), 85) print(86) assert("91e9cc620573db9a692bb0171c5c11b5946ad5c3" == sha1(string.char(48,77,182,152,26,145,231,116,179,95,21,248,120,55,73,66):rep(971)), 86) print(87) assert("3eb85ec3db474d01acafcbc5ec6e942b3a04a382" == sha1(string.char(68,211):rep(184)), 87) print(88) assert("f9bd0e886c74d730cb2457c484d30ce6a47f7afa" == sha1(string.char(168,73,19,144,110,93,7,216,40,111,212,192,33,9,136,28,210,175,140,47,125,243,206,157,151,252,26):rep(511)), 88) print(89) assert("64461476eb8bba70e322e4b83db2beaee5b495d4" == sha1(string.char(33,141):rep(359)), 89) print(90) assert("761e44ffa4df3b4e28ca22020dee1e0018107d21" == sha1(string.char(254,172,185,30,245,135,14,5,186,42,47,22):rep(715)), 90) print(91) assert("41161168b99104087bae0f5287b10a15c805596f" == sha1(string.char(79):rep(625)), 91) print(92) assert("7088f4d88146e6e7784172c2ad1f59ec39fa7768" == sha1(string.char(20,138,80,102,138,182,54,210,38,214,125,123,157,209,215,37):rep(315)), 92) print(93) assert("2e498e938cb3126ac1291cee8c483a91479900c1" == sha1(string.char(140,197,97,112,205,97,134,190):rep(552)), 93) print(94) assert("81a2491b727ef2b46fb84e4da2ced84d43587f4e" == sha1(string.char(109,44,17,199,17,107,170,54,113,153,212,161,174):rep(136)), 94) print(95) assert("0e4f8a07072968fbc4fe32deccbb95f113b32df7" == sha1(string.char(181,247,203,166,34,61,180,48,46,77,98,251,72,210,217,242,135,133,38,12,177,163,249,31,1,162):rep(282)), 95) print(96) assert("8d15dddd48575a1a0330976b57e2104629afe559" == sha1(string.char(15,60,105,249,158,45,14,208,202,232,255,181,234,217):rep(769)), 96) print(97) assert("98a9dd7b57418937cbd42f758baac4754d5a4a4b" == sha1(string.char(115,121,91,76,175,110,149,190,56,178,191,157,101,220,190,251,62,41,190,37):rep(879)), 97) print(98) assert("578487979de082f69e657d165df5031f1fa84030" == sha1(string.char(189,240,198,207,102,142,241,154):rep(684)), 98) print(99) assert("3e6667b40afb6bcc052654dd64a653ad4b4f9689" == sha1(string.char(85,82,55,80,43,17,57,20,157,10,148,85,154,58,254,254,221,132,53,105,43,234,251,110,111):rep(712)), 99) print(100) assert("31285f3fa3c6a086d030cf0f06b07e7a96b5cbd0" == hmac_sha1("63xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "data"), 100) print(101) assert("2d183212abc09247e21282d366eeb14d0bc41fb4" == hmac_sha1("64xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "data"), 101) print(102) assert("ff825333e64e696fc13d82c19071fa46dc94a066" == hmac_sha1("65xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "data"), 102) print(103) assert("ecc8b5d3abb5182d3d570a6f060fef2ee6c11a44" == hmac_sha1(string.char(220,58,17,206,77,234,240,187,69,25,179,182,186,38,57,83,120,107,198,148,234,246,46,96,83,28,231,89,3,169,42,62,125,235,137), string.char(1,225,98,83,100,71,237,241,239,170,244,215,3,254,14,24,216,66,69,30,124,126,96,177,241,20,44,3,92,111,243,169,100,119,198,167,146,242,30,124,7,22,251,52,235,95,211,145,56,204,236,37,107,139,17,184,65,207,245,101,241,12,50,149,19,118,208,133,198,33,80,94,87,133,146,202,27,89,201,218,171,206,21,191,43,77,127,30,187,194,166,39,191,208,42,167,77,202,186,225,4,86,218,237,157,117,175,106,63,166,132,136,153,243,187)), 103) print(104) assert("bd1577a9417d804ee2969636fa9dde838beb967d" == hmac_sha1(string.char(216,76,38,50,235,99,92,110,245,5,134,195,113,7), string.char(144,3,250,84,145,227,206,87,188,42,169,182,106,31,207,205,33,76,52,158,255,49,129,169,9,145,203,225,90,228,163,33,49,99,29,135,60,112,152,5,200,121,35,77,56,116,68,109,190,136,184,248,144,172,47,107,30,16,105,232,146,137,24,81,245,94,28,76,27,82,105,146,252,219,119,164,21,14,74,192,209,208,156,56,172,124,89,218,51,108,44,174,193,161,228,147,219,129,172,210,248,239,22,11,62,128,1,50,98,233,141,224,102,152,44,68,66,46,210,114,138,113,121,90,7,70,125,191,192,222,225,200,217,48,22,10,132,29,236,71,108,140,102,96,51,142,51,220,4)), 104) print(105) assert("e8ddf90f0c117ee61b33db4df49d7fc31beca7f7" == hmac_sha1(string.char(189,213,184,86,24,160,198,82,138,223,71,149,249,70,183,47,0,106,27,39,85,102,57,190,237,182,2,10,21,253,252,3,68,73,154,75,79,247,28,132,229,50,2,197,204,213,170,213,255,83,100,213,60,67,202,61,137,125,16,215,254,157,106,5), string.char(78,103,249,15,43,214,87,62,52,57,135,84,44,92,142,209,120,139,76,216,33,223,203,96,218,12,6,126,241,195,47,203,196,22,113,138,228,44,122,37,215,166,184,195,91,97,175,153,115,243,37,225,82,250,54,240,20,237,149,183,5,43,142,113,214,130,100,149,83,232,70,106,152,110,25,74,46,60,159,239,193,173,235,146,173,142,110,71,1,97,54,217,52,228,250,42,223,53,105,24,87,98,117,245,101,189,147,238,48,111,68,52,169,78,151,38,21,249)), 105) print(106) assert("e33c987c31bb45b842868d49e7636bd840a1ffd3" == hmac_sha1(string.char(154,60,91,199,157,45,32,99,248,5,163,234,114,223,234,48,238,82,43,242,52,176,243,5,135,26,141,49,105,120,220,135,192,96,219,152,126,74,58,110,200,56,108,1,68,175,82,235,149,191,67,72,195,46,35,131,237,188,177,223,145,122,26,234,136,93,34,96,71,214,55,27,13,116,235,109,58,83,175,226,59,13,218,93,5,132,43,235), string.char(182,10,154)), 106) print(107) assert("f6039d217c16afadfcfae7c5e3d1859801a0fe22" == hmac_sha1(string.char(73,120,173,165,190,159,174,100,255,56,217,98,249,11,166,25,66,95,96,99,70,73,223,132,231,147,220,151,79,102,111,98), string.char(134,93,229,103)), 107) print(108) assert("f6056b19cf6b070e62a0917a46f4b3aefcf6262d" == hmac_sha1(string.char(49,40,62,214,208,180,147,74,162,196,193,9,118,47,100,9,235,94,3,122,226,163,145,175,233,148,251,88,49,216,208,110,13,97,255,189,120,252,223,241,55,210,77,4), string.char(92,185,37,240,36,97,21,132,188,80,103,36,162,245,63,104,19,106,242,44,96,28,197,26,46,168,73,46,76,105,30,167,101,64,67,119,65,140,177,226,223,108,206,60,59,0,182,125,42,200,101,159,109,6,62,85,67,66,88,137,92,234,61,19,22,177,144,127,129,129,195,230,180,149,128,148,45,18,94,163,196,55,70,184,251,3,200,162,16,210,188,61,186,218,173,227,212,8,125,20,138,82,68,170,24,158,90,98,228,166,246,96,74,24,217,93,91,102,246,221,121,115,157,243,45,158,45,90,186,11,127,179,59,72,37,71,148,123,62,150,114,167,248,197,18,251,92,164,158,83,129,58,127,162,181,92,85,121,13,118,250,221,220,150,231,5,230,28,213,25,251,17,71,68,234,173,10,206,111,72,123,205,49,73,62,209,173,46,94,91,151,122)), 108) print(109) assert("15ddfa1baa0a3723d8aff00aeefa27b5b02ede95" == hmac_sha1(string.char(91,117,129,150,117,169,221,182,193,130,126,206,129,177,184,171,174,224,91,234,60,197,243,158,59,0,122,240,145,106,192,179,96,84,46,239,170,123,120,51,142,101,45,34), string.char(27,71,38,183,232,93,23,174,151,181,121,91,142,138,180,125,75,243,160,220,225,205,205,72,13,162,104,218,47,162,77,23,16,154,31,156,122,67,227,25,190,102,200,57,116,190,131,8,208,76,174,53,204,88,227,239,71,67,208,32,57,72,165,121,80,139,80,29,90,32,229,208,115,169,238,189,206,82,180,68,157,124,119,1,75,27,122,246,197,178,90,225,138,244,73,12,226,104,191,70,53,210,245,250,239,238,3,229,196,22,28,199,122,158,9,33,109,182,109,87,25,159,159,146,217,77,37,25,173,211,38,173,70,252,18,193,7,167,160,135,63,190,149,14,221,143,173,152,184,143,157,245,137,219,74,239,185,40,14,0,29,87,169,84,67,75,255,252,201,131,75,108,43,129,210,193,108,139,60,228,29,36,150,15,86)), 109) print(110) assert("d7cba04970a79f5d04c772fbe74bcc0951ff9c67" == hmac_sha1(string.char(159,179,206,236,86,25,53,102,163,243,239,139,134,14,243), string.char(106,160,249,31,114,205,9,66,139,182,209,218,31,151,57,132,182,85,218,220,103,15,210,218,101,244,240,227,12,184,127,137,40,127,232,195,234,182,0,214,125,122,141,207,61,244,143,202,159,229,100,76,165,44,226,137,100,61,1,107,132,142,144,158,223,249,151,78,186,194,189,83,45,66,178,41,23,172,195,137,170,216,208,27,149,112,68,188,0)), 110) print(111) assert("84625a20dc9ada78ec9de910d37734a299f01c5d" == hmac_sha1(string.char(85,239,250,237,210,126,142,84,119,107,100,163,15,179,132,206,112,85,119,101,44,163,240,10,14,69,169,158,170,190,95,66,129,69,218,229), string.char(210,103,131,185,91,224,115,108,129,11,50,16,90,98,64,124,157,177,50,21,30,201,244,101,136,104,149,102,34,166,25,62,1,79,216,4,221,113,168,169,5,151,172,22,166,28,130,137,251,164,220,189,253,45,149,80,247,84,130,208,69,49,120,8,90,154,100,191,121,81,230,207,23,189,7,164,112,123,158,192,224,255,218,200,70,238,211,161,35,251,150,125,24,10,131,220,57,178,196,38,231,196,206,94,118,32,56,197,136,148,145,247,188,64,56,53,195,140,92,202,22,122,229,105,115,14,42,27,107,223,105)), 111) print(112) assert("c463318ac1878cd770ec93b2710b706296316894" == hmac_sha1(string.char(162,21,153,195,254,135,203,75,152,144,200,187,226,4,248,93,161,180,219,181,99,130,122,28,179,140,152,9,21,115,34,140,162,45,88,4,33,238,179,125,58,23,108,194,158,48,191,40,3,50,81,247,114,241,0,88,147,93,57,178,73,187,11,195,254,171,106,167,245,190,117,160,1,219,200,249,107,233,58,19,122), string.char(246,179,198,163,52,106,45,38,131,22,142,185,149,121,79,211,0,16,102,52,46,176,83,7,32,42,103,184,234,107,180,128,128,130,21,27,236)), 112) print(113) assert("e2d37df980593b3e52aadb0d99d61114fb2c1182" == hmac_sha1(string.char(43,84,125,158,182,211,26,238,222,247,6,171,184,54,70,44,169,4,74,34,98,71,118,189,138,53,8,164,117,22,76,171,57,255,122,230,110,122,228,22,252,123,174,218,222,77,80,150,159,43,236,137,234,48,122,138,100,137,112), string.char(249,234,226,86,109,2,157,76,229,42,178,223,196,247,42,194,17,17,117,3,45,6,80,202,22,105,44,242,84,25,21,189,5,216,35,200,220,192,110,81,215,145,109,179,48,44,40,35,216,240,48,240,33,210,79,35,64,189,81,15,135,228,83,14,254,32,211,229,158,79,188,230,84,106,78,126,226,106,203,59,67,134,186,52,21,48,2,142,231,116,241,167,177,175,74,188,232,234,56,41,181,118,232,190,184,76,64,109,167,178,123,118,3,50,46,254,253,83,156,116,220,247,69,27,160,167,210,205,79,60,28,253,17,219,32,44,217,223,77,153,229,55,113,75,234,154,247,13,133,49,220,200,241,111,136,205,14,78,222,55,181,250,160,143,224,37,63,227,155,12,39,173,209,45,171,93,93,70,36,129,111,173,183,112,31,231,22,146,129,171,75,128,45)), 113) print(114) assert("666f9c282cf544c9c28006713a7461c7efbbdbda" == hmac_sha1(string.char(71,33,203,83,173,199,175,245,206,13,237,187,54,61,85,15,153,125,168,223,231,56,46,250,173,192,247,189,45,166,225,223,109,254,15,79,144,188,71,201,70,25,218,205,13,184,204,219,221,82,133,189,144,179,242,125,211,108,100,1,132,110,231,87,107,91,169,101,241,105,30), string.char(98,122,51,157,136,38,149,9,82,27,218,155,76,61,254,154,43,172,59,105,123,45,97,146,191,58,50,153,139,40,37,116)), 114) print(115) assert("6904a4ce463002b03923c71cdac6c38f57315b63" == hmac_sha1(string.char(15,40,41,76,105,201,153,223,174,12,100,114,234,95,204,95,84,31,26,28,69,85,48,111,40,173,162,6,198,36,252,179,244,9,112,213,64,87,58,186,4,147,229,211,161,30,226,159,75,38,91,89,224,245,156,110,229,50,210), string.char(213,155,72,86,75,185,138,211,199,57,75,9,1,141,184,89,82,180,105,17,193,63,161,202,25,60,16,201,72,179,74,129,73,172,84,39,140,33,25,122,136,143,116,100,100,234,246,108,135,180,85,12,85,151,176,154,172,147,249,242,180,207,126,126,235,203,55,8,251,29,135,134,166,152,80,76,242,15,69,225,199,221,133,118,194,227,9,185,190,125,120,116,182,15,241,209,113,253,241,58,145,106,136,25,60,47,174,209,251,54,159,98,103,88,245,61,108,91,12,245,119,191,232,36)), 115) print(116) assert("c57c742c772f92cce60cf3a1ddc263b8df0950f3" == hmac_sha1(string.char(193,178,151,175,44,234,140,18,10,183,92,17,232,95,94,198,229,188,188,131,247,236,215,129,243,171,223,83,42,173,88,157,29,46,221,57,80,179,139,80), string.char(214,95,30,179,51,95,183,198,185,9,76,215,255,122,91,103,133,117,42,33,0,145,60,129,129,237,203,59,141,214,108,79,247,244,187,250,157,177,245,72,191,63,176,211)), 116) print(117) assert("092336f7f1767874098a54d5f27092fbb92b7c94" == hmac_sha1(string.char(208,10,217,108,72,232,56,67,6,179,160,172,29,67,115,171,118,82,124,118,61,111,21,132,209,92,212,166,181,129,43,55,198,96,169,112,86,212,68,214,81,239,122,216,104,73,114,209,60,182,248,118,74,100,26,1,176,3,166,188), string.char(42,4,235,56,198,119,175,244,93,77)), 117) print(118) assert("c1d58ad2b611c4d9d59e339da7952bdae4a70737" == hmac_sha1(string.char(100,129,37,207,188,116,232,140,204,90,90,93,124,132,140,81,102,156,67,254,228,192,150,161,10,62,143,218,237,111,151,98,78,168,188,87,170,190,35,228,169,228,143,252,213,85,11,124,92,91,18,27,122,141,98,6,77,106,205,209,2,185,249), string.char(58,190,56,255,176,80,220,228,0,251,108,108,220,197,51,131,164,162,60,181,21,54,122,174,31,13,4,84,198,203,105,11,64,230,1,30,218,208,252,219,44,147,2,108,227,66,49,71,21,95,248,93,193,180,165,240,224,226,13,9,208,186,12,118,243,28,113,49,122,192,212,164,43,41,151,183,187,126,115,85,174,40,125,9,54,193,164,95,21,77,200,226,50,115,187,122,34,141,255,224,239,12,132,191,48,102,205,248,128,164,116,48,39,191,98,53,169,230,249,215,231,152,45,27,226,10,143,15,209,157,208,181,15,210,195,5,29,48,29,125,62,59,191,216,170,179,226,110,40,46,32,130,235,48,234,233,17)), 118) print(119) assert("3de84c915278a7d5e7d5629ec88193fb5bbadd15" == hmac_sha1(string.char(89,175,111,33,154,12,173,230,28,117), string.char(188,233,41,180,142,157,96,76,105,212,92,202,155,167,179,28,12,156,64,73,32,253,253,166,9,240,7,0,248,43,101,135,226,231,173,221,180,43,160,217,6,38,183,186,214,217,137,83,148,148,40,141,217,98,209,12,167,102,95,166,136,231,232,84,59,112,148,201,166,104,135,124,189,85,160,183,143,122,200,190,144,205,25,254,180,188,108,225,171,131,240,185,86,243,192,173,130,50,150,57,242,180,132,193,11,110,247,121,25,24,110,199,156,121,233,149,79,103,15,173,184,143,45,125,164,242,125,10,183,189,189,135,121,59,148,106,77,9,16,123,176,100,142,246,156,180,202,87,43,102,113,123)), 119) print(120) assert("e8cc5a50f80e7d52edd7ae4ec037414b70798598" == hmac_sha1(string.char(139,243,17,238,11,156,138,122,212,86,201,213,100,167,65,199,92,182,255,36,221,82,192,213,199,162,69,42,222,95,65,170,146,48,39,185,147,18,140,122,168,141), string.char(141,216,25,29,235,207,123,188,85,53,131,180,125,226,97,244,250,138,64,39,30,250,146,101,219,171,213,158,164,172,137,22,136,86,46,77,95,213,66,198,15,24,164,148,29,166,169,195,196,111,122,147,247,215,148,9,129,40,133,178,69,242,171,235,181,83,241,208,237,17,37,30,188,152,47,214,69,229,114,225,75,172,163,184,38,158,230,177,187,124,33,240,238,148,197,235,237,98,33,237,59,205,147,128,108,254,95,5,6,49,10,224,78,139,164,235,220,78,224,15,213,136,198,217,114,156,130,247,167,64,36,10,183,221,193,220,195,80,125,143,248,228,254,6,221,137,170,211,66)), 120) print(121) assert("80d9a0bc9600e6a0130681158a1787ef6b9c91d6" == hmac_sha1(string.char(152,28,190,131,179,120,137,63,214,85,213,89,116,246,171,92,0,47,44,102,120,206,185,116,71,106,195), string.char(250,9,181,163,46,88,166,238,164,40,207,236,152,104,201,42,14,255,125,57,173,176,230,171,63,6,254,130,140,201,45,152,164,216,171,27,172,105,19,103,128,33,255,93,64,155,55,210,140,2,94,168,168,164,5,218,249,227,221,133,72,112,97,243,140,149,193,80)), 121) print(122) assert("cb4182de586a30d606a057c948fe33733145790a" == hmac_sha1(string.char(193,229,65,81,159,93,162,173,74,64,195,41,80,189,104,238,119,67,255,63,209,247,146,247,210,208,86,14,102,153,246,175,242,209,42,4,243,138,217,58,206,147,19,163,152,239,154,78,5,1,175,97,251,24,185,10,67,107,174,145,178,121,36,238,108,85,214,162,78,195,107,135,114,76,180,37,99,103,78,232,29,244,11,60,236,112,18,241,39,164,107,18), string.char(222,85,11,36,12,191,252,103,180,161,84,168,21,125,50,76,4,148,195,230,210,114,35,116,225,176,16,64,44,20,17,224,227,243,175,251,204,177,41,223,76,216,228,221,54,226,150,209,145,175,142,137,140,25,196,99,252,175,125,15,39,76,47,127,188,51,88,227,194,171,249,141,12,249,225,199,241,112,153,26,107,204,191,23,241,46,223,143,9,9,46,64,197,209,23,18,208,139,148,45,146,94,80,86,49,12,122,218,14,210,133,164,39,227,102,60,129,6,43)), 122) print(123) assert("864e291ec69124c3a43976bae4ba1788f181f027" == hmac_sha1(string.char(231,211,73,154,64,125,224,253,200,234,208,210,83,9), string.char(33,248,155,139,159,173,90,40,200,95,96,12,81,103,8,139,211,189,87,75,8,6,36,103,116,204,137,181,81,233,97,171,47,27,143,164,63,3,223,107,80,232,182,154,7,255,190,171,209,115,219,6,34,109,1,254,214,73,2)), 123) print(124) assert("bee434914e65818f81bd07e4e0bbb58328be3ca1" == hmac_sha1(string.char(222,123,148,175,157,117,104,212,169,13,252,113,231,104,37,8,147,119,92,27,100,132,250,105,63,13,195,90,251,122,185,97,159,68,36,7,75,163,179,3,6,170,164,152,86,30,49,239,201,245,43,220,78,62,154,206,95,24,149,138,138,15,24,68,58,255,99,88,143,192,12), string.char(97,76,73,171,140,99,57,217,56,73,182,189,58,19,177,104,85,85,45,31,170,132,53,51,155,143,70,169,189,5,41,231,34,132,235,228,114,58,100,105,146,27,67,89,32,34,78,133,155,222,88,26,83,198,128,183,19,243,27,186,192,77,184,54,142,57,24)), 124) print(125) assert("0268f79ef55c3651a66a64a21f47495c5beeb212" == hmac_sha1(string.char(181,166,229,31,47,25,64,129,95,242,221,96,73,42,243,170,15,33,14,198,78,194,235,139,177,112,191,190,52,224,93,95,107,125,245,17,170,161,53,148,19,180,83,154,45,98,76,170,51,178,114,71,34,242,184,75,1,130,199,40,197,88,203,86,169,109,206,67,86,175,201,135,101,76,130,144,248,80,212,14,119,186), string.char(53,208,22,216,93,88,59,64,135,112,49,253,34,11,210,160,117,40,219,36,226,45,207,163,134,133,199,101)), 125) print(126) assert("bcb2473c745d7dee1ec17207d1d804d401828035" == hmac_sha1(string.char(143,117,182,35), string.char(110,95,240,139,124,96,238,255,165,146,205,18,155,244,252,176,24,19,253,39,33,248,90,95,34,115,70,63,195,91,124,144,243,91,110,80,173,47,46,231,73,228,207,37,183,28,42,144,66,248,178,255,129,52,55,232,1,33,193,185,184,237,8)), 126) print(127) assert("402ed75be595f59519e3dd52323817a65e9ff95b" == hmac_sha1(string.char(247,77,247,124,27,156,148,227,12,21,17,94,56,14,118,47,140,239,200,249,216,139,7,172,16,211,237,94,62,201,227,42,250,8,179,21,39,85,186,145,147,106,127,67,111,250,163,89,152,115,166,254,8,246,141,238,43,45,194,131,191,202), string.char(138,227,90,40,221,37,181,54,26,20,24,125,19,101,12,120,111,20,104,10,225,140,64,218,8,33,179,95,215,142,203,127,243,231,166,76,234,159,59,160,132,56,215,143,103,75,155,158,56,126,71,252,229,54,34,240,30,133,110,67,146,213,116,73,14,160,186,169,155,196,84,132,171,200,171,56,92,38,136,90,57,155,145,67,190,198,235,112,242,120,150,191,216,41,21,36,205,42,68,145,226,246,178,70,60,157,254,206,70,84,189,36,197,48,208,249,144,223,6,72,17,253,236,106,205,245,30,1,80,121,165,103,12,202,115,227,192,64,42,223,113,200,195,156,120,202,110,131,130,39,64,217,108,23,133,140,56,144,167,77,45,236,145,161,150,229,85,231,203,159,203,85,125,221,226)), 127) print(128) assert("20231b6abcdd7e5d5e22f83706652acf1ae5255e" == hmac_sha1(string.char(132,122,252,170,107,28,195,210,121,194,245,252,42,64,42,103,220,18,61,68,19,78,182,234,93,130,175,20,4,199,66,91,247,247,87,107,89,205,68,125,19,254,59,47,228,134,200,145,148,78,52,236,51,255,152,231,47,168,213,124,228,34,48), string.char(61,232,69,199,207,49,72,159,37,230,105,207,63,141,245,127,142,77,168,111,126,92,145,26,202,215,116,19,125,81,203,11,86,36,19,218,90,255,4,10,21,185,151,111,188,125,123,2,137,151,171,178,195,199,106,55,42,42,20,164,35,177,237,41,207,24,102,218,213,223,204,186,212,30,59,121,52,34,84,76,142,179,75,6,84,8,72,210,72,177,45,103,218,70,165,69,36,152,50,228,147,192,61,104,209,76,155,147,169,162,151,178,72,109,241,41,76,0,60,251,107,99,92,37,95,215,172,154,172,93,254,65,176,43,99,242,151,78,48,58,35,49,23,174,115,224,227,106,49,30,9)), 128) print(129) assert("b5a24d7aadc1e8fb71f914e1fa581085a8114b27" == hmac_sha1(string.char(233,21,254,12,70,129,236,10,187,36,119,197,152,242,131,11,10,243,169,217,128,247,196,111,183,109,146,155,92,91,71,83,0,136,109,1,130,143,0,8,6,22,160,153,190,164,199,56,152,229), string.char(152,112,218,132,38,125,94,106,64,9,137,105)), 129) print(130) assert("7fd9169b441711bef04ea08ed87b1cd8f245aeb2" == hmac_sha1(string.char(108,226,105,159), string.char(103,218,254,85,50,58,111,74,108,60,174,27,113,65,100,217,97,208,125,38,170,151,72,125,82,114,185,58,4,143,39,223,250,168,66,182,37,216,130,111,103,157,59,0,245,222,8,65,240)), 130) print(131) assert("98038102f8476b28dd11b535b6c1b93eb8089360" == hmac_sha1(string.char(77,121,52,138,71,149,161,87,106,75,232,247,8,148,175,24,23,60,151,104,176,208,148,227,226,191,73,123,164,36,177,235,101,190,128,202,139,116,201,125,61,41,204,187,48,107,63,231,12,137,11,228,93,136,178,243,90), string.char(90,6,232,110,12,66,45,86,141,121,2,6,177,135,64,208,45,178,129,37,253,246,74,48,71,211,243,121,31,209,138,147,185,141,65,185,245,9,137,134,148,123,181,128,204,74,222,150,239,169,179,36,236,104,147,255,251,204,5,191,198,185,153,195,26,112,179,170,232,101,238,143,143)), 131) print(132) assert("ba43c9b8e98b4b7176a1bb63fcbed5b004dc4fcd" == hmac_sha1(string.char(133,107,178,183,204,2,203,106,242,180,87,58,134), string.char(211,208,255,34,198,99,119,185,171,52,118,94,65,241,45,24,40,6,2,203,126,216,21,51,245,154,92,198,201,220,190,135,182,137,113,68,250,94,126,233,140,94,118,244,244,98,252,97,217,204,239,218,178,240,65,150,246,217,231,142,157,33,113,135,135,214,137,103,211,213,48,132,171,43,96,51,241,45,42,237,53,247,134,102,163,214,96,147,131,74,180,19,7,12,174,60,37,60,78,85,92,186,121,27,234,128,132,64,47,106,127,218,52,30,59,230,85,240,164,54,168,232,131,110,145,125,255,238,145,237,134,196,197,138,105,74,34,134,29,249,222,119,194,104,230)), 132) print(133) assert("0c1ad0332f0c6c1f61e6cc86f0a343065635ecb3" == hmac_sha1(string.char(230,111,251,74,216,102,12,134,90,44,121,175,56,221,225,235,180,246,56,12,236,119,119,109,97,59,224,121,27,37,72,219,138,193,50,219,193,152,1,229,224,197,233,76,188,62,120,17,63,158,186,198,87,135,34,60,164,139,3,200,20,188,203,110,212,137,91,70,35,255,146,213,216,71,73,143,55,219,54,142,105,83,175,193,32,226,150,51,59), string.char(185,250,11,8,121,214,91,27,1,50,67,204,219,252,212,198,117,234,57,127,214,12,220,104,44,177,225,238,103,138,144,162,28,69,143,108,192,4,160,63,175,185,123,46,151,221,127,145,55,32,85,245,251,178,125,223,107,192,206,207,232,231,190,44,221,173,1,59,12,178,112,22,253,222,14,40,18,141,128,144,196,6,112,206,183,144,215,156,159,79,132,152,170,22,68,106,137,248,12,242,231,98,96,144,148,20,212,30,242,109,36,112,50,51,57,212,181,191,80,73,215,119,244,209,57,108,147,182,67,204,154,48,216,116)), 133) print(134) assert("466442888b6989fd380f918d0476a16ae26279cf" == hmac_sha1(string.char(250,60,248,215,154,159,2,121), string.char(236,249,8,10,180,146,36,144,83,196,49,166,17,81,51,72,28)), 134) print(135) assert("f9febfeda402b930ec1b95c1325788305cb9fde8" == hmac_sha1(string.char(124,195,165,216,158,253,119,86,162,36,185,72,162,141,160,225,183,25,54,123,73,61,40,165,101,154,157,200,113,14,117,144,185,64,236,219,3,87,90,49), string.char(73,1,223,35,5,159,39,85,95,170,227,61,14,227,142,222,255,253,152,123,104,35,229,6,195,106,30,59,5,36,2,173,71,161,217,189,47,193,3,216,34,10,25,30,212,255,141,94,25,72,52,58,50,56,180,99,173,155)), 135) print(136) assert("53fe70b7e825d017ef0bdecbacdda4e8a76cbc6f" == hmac_sha1(string.char(144,144,130,150,207,183,143,100,188,177,90,5,4,95,116,105,252,118,187,251,35,113,251,86,36,234,176,165,55,165,158,4,182,85,62,255,18,146,128,67,221,183,78,71,158,184,140,14,84,44,36,79,244,158,37,1,122,43,103,228,217,253,72,113,228,121,160,29,87,5,158,64,38,253,179,122,187,157,106,99,161,79,97,176,119,86,101,40,142,241,217,85), string.char(12,89,157,112,207,206,171,254,87,106,42,113,70,72,222,239,88,94,14,41,14,175,206,56,219,244,230,40,33,154,107,249,45,70,55,104,151,193,246,133,99,59,244,179,8,145,225,100,146,142,128,74,40,155,99,93,204,253,249,222,64,99,156,121,142,49,166,62,171,223,135,55,212,183,144,218,56,12,211,99,254,58,249,219,197,189,173,141,60,196,241,157,67,151,251,172,49,105,200,88,224,175,9,79,65,40,5,255,222,14,152,149,228,83,154,207,104,132,106,96,40,229,182,120,100,207,223,81,171,141,1,171)), 136) print(137) assert("81b1df7265954a8aee4e119800169660ff9e75c8" == hmac_sha1(string.char(232,167,4,53,90,111,79,91,163,125,230,202,52,242,160,135,66,211,246,17,131,109,109,235,91), string.char(111,177,251,180,63,13,33,205,231,130,203,167,177,156,87,70,33,148,229,163,158,224,123,37,18,204,185,89,235,2,30,252,229,202,170,157,175,157,208,254,135,190,34,14,42,211,154,243,31,100,71,53,169,76,36)), 137) print(138) assert("caf986508402b3feb70c5b44d902f4a9428a44d2" == hmac_sha1(string.char(127,2,162,53,6,172,189,169,176,254,107,46,57,207,23,25,182,72,34,228,164,113,12,179,149,45,169,19,3,204,202,10,126,153,130,41,143,200,198,47,215,15,58,124,225,60,171,98,8,211,72,235,211,187,172,37,119,96,55,243,98,198,225,191,79,207,94,215,255,21,101,128,153,233,88,101,57,195,70,194), string.char(97,24,129,203,10,176,242,39,165,95,51,30,187,106,207,160,18,154,16,130,178,80,206,128,148,56,213,55,46,85,76,100,50,131,41,167,130,12,81,161,158,55,181,12,12,38,89,193,136,220,81,114,191,157,20,221,171,245)), 138) print(139) assert("83bd91a9e8072010f3b54d215c56ada0cd810bb6" == hmac_sha1(string.char(215,33,57,193,51,126,38,114,7,29,93,101,78,152,222,121,42,0,236,169,48,137,202,48,90,249,235,46,44,126,78,251,15,84,200,235,43,108,8,196,210,57,152,86,254), string.char(153,35,68,230,52,95,243,220,32,101,148,76,80,168,187,172,170,254,10,70,31,117,40,131,61,155,200,189,25,198,120,41,181,53,14,175,64,89,126,94,242,61,52,1,188,255,121,254,74,88,19,10,126,225,89,31,21,104,199,82,149,60,110,194,123,236,13,38,7,213,176,182,202,211,178,185,99,8,173,220,168,166,108,208,71,152,174,5,134,167,220,47,74,177,231,90,114,208,168,47,112,74,58,73,94,224,101,64,128,255,186,179,119,159,117,110,2,188,38,73,230,39,50,73,162,22,117,185,182,168,70,252,11,242,243,165,32,104,220,211)), 139) print(140) assert("c123d92ee67689922dfb3b68a45584e7e5dc5dc3" == hmac_sha1(string.char(167,32,181,181,249,210), string.char(249,7,96,196,48,157,109,129,192,227,82,84,196,167,224,145,61,177,60,74,217,117,199,147,147,54,198,138,5,51,135,112,29,184,49,16,0,240,172,21,214,114,146,57,2,154,205,60,113,32,113,255,165,5,178,15,159)), 140) print(141) assert("454d9ba00ac4e0e90a2866ba2abefba40dac9aab" == hmac_sha1(string.char(116,2,170,84,236,37), string.char(219,144,86,39,237,166,110,90,213,70)), 141) print(142) assert("e919cce3908786205511fee0283e1eb41c0c45a1" == hmac_sha1(string.char(180,76,140,201,56,232,48,183,173,36,111,94,208,153,234,255,52,18,121,118,117,77,92,74,241,103,193,164,169,1,60,46,101,73,61,221,26,253,209,124,99,154,177,218,59,238,159,191,0,65,117,78,15,12,92,35,254,40,11,112,112,91,49,248,198,229,161,247,130,170,76,146,8,220,134,96,155,68,50,168,186,135,188,186,36), string.char(26,47,201,201,197,232,196,239,204,197,162,14,53,254,88,75,43,254,11,20,52,61,136,206,162,29,223,55,125,50,200,239,135,51,154,54,78,191,188,0,137,191,149,162,191,103,0,168,52,178,147,251,253,86,98,17,15,202,231,0,122,10,131,25,15,129,48,190,60,205,185,114,193,122,19,47,52,142,124,107,45,174,221,196,18,32,79,12,129,84,40)), 142) print(143) assert("fa770f6b76984edb0f7fa514a19d3c7ef9c82c51" == hmac_sha1(string.char(70,55,200,82,238,241,84,180,129,122,103,199,170,177), string.char(88,236,14,60,249,2,197,242,76,152,248,241,158,232,113,242,209,93,232,113,147,210,80,180,44,124,186,150,172,177,138,131,117,151,174,231,93,248,244,200,191,169,159,206,232,246,2,121,106,183,107,79,210,73,145,244,254,248,85,217,156,221,238,120,224,176,31,56,246,26,3,186,189,86,41,17,34,8,208,58,46,130,226,139,209,194,3,34,55,213,154,12,250,229,201,122,89,47,177,229,80,165,183,77,178,139,3,241,165,196,239,93,98,101,99,210,99,5,135,132,151,197,4,154,87,130,145,175,243,238,132,0,146,14,6,205,227,78,150,59,191,223,74,145,57,82,30,223,90,205,248,47,195,220,215,167,112,216,20,130,26,88,170,101,26,14,216,62,169,217,1,135,193,147,218)), 143) print(144) assert("d3418e6e6d0a96f8ea7c511273423651a63590c8" == hmac_sha1(string.char(61,175,211,89,77,91,143,76,18,30,252,16,181,45,211,37,207,204,174,174,60,208,36,85,23,106,141,215,62,8,158,98,209,49,96,143,129,99,11,159,79,116,98,244,51,237,227,250,73,196,36,216,181,157,159,87,248,108,29,22,181,175,72,92,160,127,46,204,202,175,61,108,172,50,225,48,84,167,116,67,183), string.char(174,62,218,81,85,89,4,35,26)), 144) print(145) assert("f2fbe13b442bff3c09dcdaeaf375cb2f5214f821" == hmac_sha1(string.char(188,80,129,208,85,53,8,122,92,99,56,251,59,108,97,145,1,97,157,181,156,243,30,204,227,88,205,151), string.char(137,30,19,71,50,239,11,212,91,234,79,248,244,118,108,245,251,78,53,136,249,55,210,231,109,207,153,83,10,63,98,225,79,113,124,64,100,16,195,9,136,28,50,215,93,93,153,25,97,77,4,13,46,34,164,59,33,206,62,125,8,79,219,42,155,39,241,96,18)), 145) print(146) assert("aeacb03e8284a01b93e51e483df58a9492035668" == hmac_sha1(string.char(62,67,97,44,112,68,107,91,105,4,183,154,9,14,164,180,87,140,195,231,62,49,250,154,193,186,219,18,100,156,176,124,223,164,68,64,105,214,144,200,65), string.char(221,20,36,192,59,234,90,174,232,184,75,15,92,229,59,191,36,51,4,190,226,35,237,189,21,100,135,138,171,202,163,227,176,231,72,185,43,197,134,157,44,86,26,42,230,251,2,124,220,245,242,116,77,35,197,154,237,73,117,131,126,242,167,242,8,72,163,171,60,29,77,152,142,69,25,234,195,2,159,49,178,63,48,56,131,143,165,22,142,116,109,11,57,71,101,99,84,204,56,233,173,65,180,228,211,10,197,204,26,70,198,81,217,83,130,70,79,54,93,96,116,32,21,219,193,70,55,84,161,89,47,206,13,167,46,104,205,218)), 146) print(147) assert("e163c334e08bd3fa537c1ea309ce99cfbcd97930" == hmac_sha1(string.char(238,187,29,13,100,125,10,129,46,251,49,62,207,74,243,25,25,73,196,100,114,64,141,173,143,144,70,13,162,227,33,255,83,58,254,138,23,146,90,86,17,33,152,227,15,100,200,147,81,114,166,234,47,60,70,190,208,28,16,44,71,150,101,197,182,57,143,79,29,135,45,211,194,62,234,28,244,74,56,104,187,94,146,174,190,198,55,143,134,60,186,144,190,100,102,0,171,151), string.char(177,90,20,162,64,145,130,87,137,70,153,237,51,138,248,29,166,134,168,68,133,121,161,26,228,137,145,116,243,224,201,229,61,107,250,198,214,208,72,169,248,155,247,54,163,167,71,248,69,106,105,139,224,138,85,94,31,143,4,215,83,239,21,198,28,1,120,83,91,92,58,150,110,77,111,166,222,236,170,129,2,121,86,66,42,129,146,232,46,248,136,175,130,118,126,205,184,136,6,35,180,174,194,59,89,243,199,38,97,48,80,98,45,111)), 147) print(148) assert("6a82b3a54ed409f612a934caf96e6c4799286f19" == hmac_sha1(string.char(11,59,108,100,113,56,239,211,2,138,219,101,50,50,111,31,171,212,228,55,89,196,44,158,77,252,212,134,106,7,250,223,219,46,231,87,9,62,213,104,169,49,1,75,3,10,50,238,5,150,47,14,47,45,171,183,40,245,194,249,228,216,85), string.char(97,131,91,189,9,52,88,203,250,245,46,96,7,95,176,61,57,192,91,231,193,32,41,169,96,101,39,240,29,143,24,56,57,177,70,135,225,90,42,192,134,103,239,175,243,246,76,137,14,237,82,215,7,76,246,219,132,50,175,25,164,4,216,65,102,0,114,216,159,80,58,32,4,100,22,35,71,140,49,16,216,182,91,80,181,129,151,55,130,7,77,84,211,111,45,51,192,123,213,192,11,81,138,60,225,21,37,182,180,223,170,163,110,9,47,214,200,249,164,98,215)), 148) print(149) assert("79caa9946fcf0ccdd882bc409abe77d3470d28f7" == hmac_sha1(string.char(202,142,21,201,45,72,11,211,67,134,42,192,96,255,210,211,252,6,101,131,25,195,101,76,250,161,148,242,30,129,241,85,68,173,236,140,120,80,71,62,55,4,33,104,52,160,143,118,252,72), string.char(100,188,93,165,75,34,224,223,6,130,52,160,83,157,253,27,200,125,117,249,41,203,19,242,4,188,209,213,211,20,162,252,215,238,221,30,219,252,246,110,117,77,89,204,221,246,69,62,160,186,37,144,43,151,39,133,254,134,24,223)), 149) print(150) assert("bd1e60a3df42e2687746766d7d67d57e262f3c9b" == hmac_sha1(string.char(57,212,172,224,206,230,13,50,80,54,19,87,147,166,245,67,118,37,101,214,207,60,66,29,197,236,146,140,192,15,36,155,108,121,215,211,210,118,22,20,56,245,42,153,150,32,224,201,171,2,238,41,70,140,73,60,215,243,86,20,169,152,220,104,95,215,187), string.char(245,123,43,109,29,108,197,27,55,167,255,177,226,205,111,126,146,75,93,70,180,90,139,218,243,232,221,247,129,77,115,101,144,42,13,249,223,127,200,213,140,113,23,10,149,107,244,86,41,133,147,26,167,85,189,76,176,12,190,52,90,54,78,29,97,204,161,224,59,243,128,64,133,95,192,19,137,220,96,228,97,161,51,21,157,172,127,76,53,38,83,126,178,26,203,206,165,241,101,130,79,122,75,29,205,240,1,68,222,48,101,7,29,6,82,240,133,112)), 150) print(151) assert("fe249fa66eb1e6228e1e5166d7862d119ffa0dcf" == hmac_sha1(string.char(152,13,170,129,7,65,32,194,85,196,85,12,170,227,139,215,70,137,246,105,100,111,252,221,54,174,90,169,59,11,198,33,110,94,246,195,174,13,212,47,18,94,17,67,68,56,251,213,92,67,243,114,181,166,24,182,238,146,48,196,11,238,91,213,46,184,187,199,15,221,193,36,73,244,30,222,175,132,165,177,162,54,215,130,171,58,26,144,218,62,172,120,188,20,182,242,47,178,120,175), string.char(129,126,32,58,251,104,186,25,18,204,5,240,65,194,32,205,194,18,151,173,185,249,237,55,145,8,41,18,171,28,59,234,62,163,32,173,197,176,206,224,95,176,218,168,183,82,53,172,205,6,223,190,189,16,3,34,212,201,54,152,89,231,194,95,8,238,133,56,139,157,115,35,74,58,21,162,111,36,105,135,151,30,207,33,198,186,122,221,98,36,196,251,27)), 151) print(152) assert("88aef9bb450b75bf96e8b5fd7831ed0d16d7fe7a" == hmac_sha1(string.char(162,225,9,204,87,95,132,152,211,133,93,120,105,156,131,55,245,224,33,115,182,10,28,49,80,175,241,67,66,52,21,55,174,108,66,100,77,20), string.char(55,61,250,187,157,98,212,245,5,54,170,60,235,228,197,182,230,24,195,215,202,55,56,153,9,94,164,107,67,93,143,1,235,195,133,201,103,57,130,177,71,73,169,80,21,251,130,247,21,61,228,39,166,173,211,203,149,55,11,224,70,52,248,118,112,219,39,188,147,5,203,101,158,134,53,250,103,73,154,249,71,181,53,171,227,26,79,200,145,178,24,80,102,26,90,101,70,143,233,17,150,181,170)), 152) print(153) assert("a057dd823478540bbe9a6fcdf2d4dcfcac663545" == hmac_sha1(string.char(83,17,204,150,211,96,88,52,225,90,61,59,28,127,135), string.char(49,226,129,162,111,75,221,186,24,219,219,178,226,132,19,126,192,69,124,114,214,31,7,127,194,134,202,147,45,176,215,141,41,94,69,38,199,231,185,223,10,104,108,50,237,20,76,194,33,43,117,117,176,3,117,117,55,68,112,207,74,72,163)), 153) print(154) assert("b11757ccfafc8feadc4e9402c820f4903f20032b" == hmac_sha1(string.char(34,11,53,219), string.char(225,194,251,106,223,229,212,105,164,172,25,25,224,199,225,172,197,42,167,1,227,165,17,247,75,250,10,208,99,124,254,158,103,74,89,60,78,141,201,44,253,87,248,239,74,86,75,64,249,189,21,170,249,18,139,21,130,226,66,200,231,35,227,147,95,213,133,35,47,236,52,64,122,115,80,170,27,50,182,151,180,106,120,85,103,255,143,27,233,43,217,152,70,82,8,229,103,42,153,38,130,184,108,160,199,69,38,184)), 154) print(155) assert("bfd0994350b2e1e0d6a1faed059f67f1dd8b361f" == hmac_sha1(string.char(221,156,128,63,215,109,55,123,41,27,110,127,209,144,178,143,120,12,226,47,56,212,131,228,3,40,186,208,233,135,33,206,92,214,153,77,12,220,72,240,176,53,22,37,130,58,180,4,111,124,135,31,192,71,117,87,11,41,231,101,37,164,112,3,55,141,250,131,191,117,90,159,224,244,9,251,154), string.char(35,88,113,69,231,5,150,40,123,13,197,27,49,72,161,1,212,222,252,74,197,170,137,72,231,245,117,236,29,12,36,59,225,103,44,119,136,34,241,17,142,239,46,152,129,163,107,17,165,112,187,15,122,217,67,13,215,157,212,26,103,226,237,161,213,3,152,88,247,236,130,133,84,200,132,191,166,8,96,247,4,142,14,81,179,64,88,202,156,242,181,95,162,19,97,223,51,76,176,218,22,116,24,238,204,128,241,236,204,2,56,86,77,211,4,52,221,82,94,76,9,21,182,112,199,161,29,39,183,120,13,0,124,136,95,182,241,3,73,167,51,237,152,149,84,147,41,42,241,174,12,30,226,245,139,230,127,34,205,41,166,228,242,105,251,4,150,29,106,42,27,239,89,249,230,246,242,149,4,60,240,93,13,166,102,239,81,216,137,196)), 155) print(156) assert("15c4527be05987a9b5c33b920be4a4402f7cf941" == hmac_sha1(string.char(132,158,227,135,167,32,19,192,144,48,232,68,79,78,135,122,136,140,97,67,34,91,225,48,126,67,77,115,154,189,13,45,8,234,59,233,245,77,34,76,95,78,3,250,179,224,20,25,6,202,214,115,165,230,85,115,250,236,135,34,32,158,196,45,207,61,71,51,93,27,187,232,175,233,147,68,231,110,12,198,233,165,24,209,206,25,16,252,127,72), string.char(210,162,121,23,181,21,75,54,110,47,15,166,133,206,100,217,57,133,228,32,112,208,186,28,140,8,207,74,185,17,128,29,181,172,20,156,64,192,112,8,207,131,53,10,182,147,224,71,218,94,71,86,215,2,133,46,160,27,51,189,38,130,224,120,185,144,253,117,193,24,154,229,247,132,62,103,65,163,106,172,22,5,2,32,129,38,213,118,110,125,156,14,48,240,170,225,158,89,92,190,254,231,51,220,8,174,188,233,226,106,224,99,218,82,64,219,206,75,166,111,25,7,87,248,145,251,109,106,243,241,89,200,85,184,155,183,249,61,70,87,115,182,81,80,155,24,245,123,184,102,214,255,122,83,61,214,88,235,169,28,147,196,247,97,28,82,193,72,71,96,243,176,35,189,236,44,184,52,151,249,186,189,150,184,142,238,88,113,120,68,234,50,136,104,80,145,179,57,6,186)), 156) print(157) assert("b9f87f5f23583bdd21b1ea18e7e647513b7b0596" == hmac_sha1(string.char(216,79,247,98,215,219,128,232,135,111,179,168,80,76,36,250,224,157,229,209,238,186,212,188,200,52,208,200,210,79,182,186,22,22,37,34,86,107,89,238,168,90,53,30,151,191,89,163,253,24,204,152,118,1,158,25,168,2,123,91,111,39,101,239,247,37,200,51,215,31,146,211,163,136,208), string.char(153,134,211,81,255,219,50,91,67,48,102,63,244,170,129,66,3,151,110,167,142,150,153,95,89,90,23,87,12,85,42,209,197,36,41,189,199,136,196,159,125,53,253,192,135,234,94,34,87,79,143,213,237,149,212,170,231,181,22,72,81,233,151,243,134,71,160,155,121,32,233,251,187,179,139,48,254,206,172,165,202,105,222,31,223,95,203,87,114,52,218,59,187,41,43,135,200,108,102,201,85,199,8,176,249,44,96,104,160,85,149,177,25,88,55,231,85,120,227,6,180,24)), 157) print(158) assert("0b586895674ab11d3b395c07ddb01151a6e562f5" == hmac_sha1(string.char(26,110,222,147,168,18,74,206,186,238,154,250,229,207,138,175,126,82,236,148,74,180,27,168,159,89,211,34,39,115,227,239,22,199,252,96,100,16,193,117,111,102,75,70,2,114,214,196,29,26,43,189,183,0,194,217,204), string.char(90,15,17,198,223,30,20,138,203,132,6,170,107,40,167,58,194,212,244,49,174,60,209,164,26,87,174,177,41,166,95,173,40,61,47,136,147,239,125,167,146,180,97,167,33,185,5,110,128,36,61,52,140,20,189,16,216,83,164,46,74,16,251,250,72,50,47,5,86,60,56,77,101,64,11,120,124,194,212,199,136,87,157,160,90,172,101,222,235,23,111,11,36,11,68,20,43,251,65,212,206,150,0,50,146,170,183,93,89,142,161,50,110,237,95,191,22,184,62,194,81)), 158) print(159) assert("38b25a1d9d927ba5e6b294d2aa69c0ab8ab0a0c5" == hmac_sha1(string.char(93,218,103,160,235,71,107,150,18,170,193,9,63,245,77,150,71,242,137,52,243,12,207,183,222,252,20,215,210,194,241,79), string.char(87,97,35,201,134,196,180)), 159) print(160) assert("a72ce76565c2876e78f86ce9e137a9881328fddc" == hmac_sha1(string.char(48,215,222,182,164,136,31,53,160,28,61,171,220,159,243,154,169,101,191,193,99,28,140,59,60,215,77,170,51,136,50,145,159,135,237,149,83,154,219,223,29,200,85,193,173,201,66,142,100,21,89,144,111,5,24,229,228,154,160,32,210,71,19,63,244,230,61,185,221,158,151,51), string.char(196,237,178,172,24,232,144,251,253,31,8,63,69,58,202,88,120,219,39,34,223,173,196,164,135,93,188,69,126,58,153,37,72,219,50,152,76,235,244,223,205,60,74,12,109,203,134,239,40,181,207,99,118,149,179,84,14,53,189,201,55,110,67,81,116,140,31,247,163,135,53,254,82,230,55,162,255,230,149,144,217,62,164,59,124,59,78,177,115,47,26,169,228,18,167,232,89,161,56,105,127,111,230,93,230,181,222,212,254,85,32,117,140,80,246,117,21,140,221,198,11,196,112,69,122,125,156)), 160) print(161) assert("cd324faf8204be01296bea85a22d991533fd353e" == hmac_sha1(string.char(25,136,140,2,222,149,164,20,148,15,90,33,106,111,10,252,67,120,57,49,251,171,99,173,114,16,102,240,206,188,231,174,51,124,38,1,217,142,4,15,78,4,5,128,207,82), string.char(84,0,42,233,157,150,105,1,216,32,170,11,100,98,103,220,137,102,32,185,55,211,207,179,252,190,248,117,253,189,196,71,112,32,4,198,87,5,133,125,238,107,210,227,149,206,12,124,31,59,213,66,105,100,240,237,149,232,174,140,127,9,65,204,162,243,100,120,6,229,71,56,140,128,228,102,121,14,43,166,252,230,172,93,96,25,214,174,151,2,50,64,152,164,132,115,109,176,25,144,171,252,231,72)), 161) print(162) assert("fb6e40ddbf3df49d4b44ccecf9e9bc74567df49e" == hmac_sha1(string.char(31,41,38,118,207,197,83,231,45,187,105,1,246,6,34,16,214,148,97,64,139,27,73,129,71,129,183,182,228,12,74,242,94,43,121,163,188,242,92,43,154,103,20,208,89,213,63,46,81,60,69,217,238,237,97,18), string.char(126,99,64,121,143,219,76,227,119,32,135,246,48,55,214,38,179,255,33,43,32,140,228,44,218,40,187,117,172,131,225,222,72,61,213,132,167,121,190,167,66,213,199,59,212,61,69,242,46,169,89,191,74,0,228,208,46,112,7,81,88,203,95,180,179,75,116,222,115,239,1,132,178,73,139,252,77,1,151,222,108,84,196,161,79,220,80,44,223,44,131,252,58,28,201,3,77,211,33,208,237,47,251,79,134,223,48,8,0,236,139,11,232,186,188,134,249,29,208,154,137,169,218,228,203,254,106,88,35,252,155,111,119,14,147,161,19,60,145,157,120,236,108,122,218,168,105,59,10,44,99,208,86)), 162) print(163) assert("373f21bf8fe4e855f882cc976ebed31717f4c791" == hmac_sha1(string.char(198,41,45,129,159,48,37,183,170,144,24,223,108,176,68,60,197,245,254,165,24,152,14,25,243,46,6,25,142,99,64,10,145,229,74,232,162,201,72,215,116,26,179,127,107,45,193,232,96,170,168,153,79,47), string.char(79,250,224,177,254,56,111,120,135,79,55,200,199,71,65,132,222,7,106,170,26,179,235,237,47,172,88,28,244,88,137,177,92,178,147,129,147,85,88,157,30,207,235,246,132,98,232,18,201,57,151,90,174,148,126,22,38,118,133,49,83,156,56,195,10,95,180,250,215,220,251,5,131,243,70,2,162,239,197,153,196,28,181,167,26,14,247,86,244,69,190,100,255,158,217,222,58,116,126,245,240,139,255,213,7,28,222,99,12,127,57,2,212,33,136,220,203,251,87,205,213,160,146,162,73,55,112,203,60,243,139,167,45,91,68,62,204,245,206,221,52,253,5,193,69,19,190,131,64,250,12,127,77,212,53,83,102,212,11,215,253,143,111,201)), 163) print(164) assert("df709285c8a1917aaa6d570bd1d4225ca916b110" == hmac_sha1(string.char(193,124,34,185,160,71,134,56,178,152,165,219,223,89,174,116,11,237), string.char(47,110,24,9,42,187,179,71,114,43,155,129,158,24,130,32,208,3,46,63,16,1,192,210,72,220,200,89,120,80,82,65,199,119,167,86,57,33,105,118,215,60,18,155,17,154,63,59,189,153,236,78,219,89,4,116,208,122,56,65,253,220,57,147,162,242,193,86,45,145,151,61,30,138,105,139,99,89)), 164) print(165) assert("66be81c24c87feeecfd805872c6cde41cb1dd732" == hmac_sha1(string.char(128,186,31,231,128,48,206,237,59), string.char(56,228,42,58,98,45,202,132,30,78,80,52,36,128,7,55,209,148,181,52,185,220,137,85,111,128,220,109,55,70,185,242,253,155,165,193,240,63,144,253,240,147,18,161,7,46,40,148,201,0,127,1,33,145,180,247,90,206,178,96,62,137,130,99,248,248,227,135,213,21,230,40,88,162,106,240,218,93,226,108,9,121,173,70,255,106,137,222,135,206,104,153,171,1,52,156,166,27,98,7,74,180,206,11,193,129,77,194,86,224,175,79,77,95,134,62,58,252,180,100,191,53,28,59,121,123,146,107,121,249,168,51,204,170,141,26,84,126,38,77,203,11,58,123,155,167,60,176,151,7,39,75,217,254,32,110,79,123,188,133,158,178,132,198,169,23,5,104,124,44,206,191,239,139,152,110,207,42)), 165) print(166) assert("2d1dd80c3f0fc323ca46ebd0f73c628caa03f88b" == hmac_sha1(string.char(142,87,50,47,146,180,251,164,35,75,53,50,101,115,90,97,66,12), string.char(174,244,41,143,173,27,117,79,53,73,0,189,83,91,13,71,117,85,96,32,199,168,244,72,218,249,207,107,244,1,113,203,160,156,89,244,132,5,4,220,254,112,77,156,158,105,180,98,40,99,198,236,134,209,13,237,93,220,177,70,97,76,178,106,60,60,92,248)), 166) print(167) assert("444ef9725523ad1aac3d1c20f4954cdf1550d706" == hmac_sha1(string.char(242,169,63,243,133,158,118,205,250,154,66,127,178,170,214,241,96,22,173,138,6,189,90,45,108,70,236,108,93,58,54,204,85,241,194,55,55,184), string.char(30,52,200,164,245,80,75)), 167) print(168) assert("2170bcfb79e4ab2164287a30ee26535681e34505" == hmac_sha1(string.char(121,111,11,221,34,216,169,179,73,247,222,122,96,168,61,168,201,230,139,158,110,154,74,83,118,254,237,255,99,212,46,218,83,69,185,10,249,78,46,76,206,206,137,133,118,57,241,114,228,54,51,68,71,224,188,188,0,119,173,94,120,1,25,13,237,4,181,170,222,92,106,28,9,111,128,137,228,2,110,4,137,171,219,70), string.char(108,95,232,156,70,235,149,211,52,84,86,25,251,127,119,231,109,144,54,177,96,118,213,3,8,119,95,192,187,221,115,83,67,39,207,82,215,85,156,198,179,246,177,202,115,103,79,97,222,133,113,201,27,92,185,48,227,223,142,96,143,181,175,238,115,112,29,30,126,59,109,252,136,153,28,112,50,138,155,249,223,114,93,107,122,114,163,226,53,219,44,15,172,233,76,98,175,107,246,181,249,112,230,173,152,211,183,120,227,165,187,10,240,94)), 168) print(169) assert("2bcd1eb6df83aed8bcdfbb36474651f466b1fb22" == hmac_sha1(string.char(178,135,218,237,192,188,60,157,46,65,76,100,66,248,152,23,200,194,67,107,95,127,67,111,149,64,60,158,199,115,159,64,45,192,212,179,46,13,171,104,139,49,185,254,235,183,65,52,140,30,89), string.char(106,32,155,133,229,130,33,200,31,51,242)), 169) print(170) assert("505910401632c487cd9482ecd6ad16928f21d6f4" == hmac_sha1(string.char(170,34,244,62,31,230,158,102,235,63,93,33,111,253,232,211,132,160,120,156,107), string.char(43,16,194,149,208,76,252,14,73,11,1,172,79,41,132,217,125,96,221,110,199,248,129,122,45,63,77,145,21,98,40,149,154,9,127,27,168,224,204,167,203,74,218,196,236,230,47,29,122,138,65,239,123,120,29,68,236,137,130,95,114,230,185,146,32,69,201,48,251,233,103,61,132,167,245,42,68,249,145,166,137,204,186)), 170) print(171) assert("6d5f9ae7e8a51f2e615ae3aa8525a41b0bf77282" == hmac_sha1(string.char(166,8,119,187,177,166,23,183,147,37,177,162,102,16,93,204,247,119,248,179,23,89,48,145,188,37,197,176,238,218,173,6,216,229,224,108,58,78,40,199,9,241,191,154,88,124,237,142,228,7,235,102,142,123,4,124,38,46,170,229,116,243,104,106,26,163,94,2,118,71,173,171,104), string.char(41,12,123,197,199,117,129,177,132,149,175,83,168,79,76,58,236,204,99,121,155,207,228,89,236,154,103,137,183,221,208,93,22,222,28,237,140,21,25,149,188,111,6,85,251,31,73,79,239,246,66,46,215,157,184,5,207,4)), 171) print(172) assert("8c20120cd131e07d9fa46467602d57c9017ca22d" == hmac_sha1(string.char(12,24,169,75,70,190,156,115,112,233,245,216,85,248,62,24,91,179,204,185,0,129,209,137,116,97,242,183,35,151,91,170,41,4,81,12,120,47,78,145,193,50,237,224,62,203,171,169,6,141,126,75,29,40), string.char(47,165,0,107,170,77,37,219,45,135,102,68,144,218,213,74,35,58,246,179,171,3,148,55,216,32,29,102,83,58,29,11,166,75,243,60,21,9,202,179,236,129,212,62,217,203,6,193,18,176,156,198,86,140,135,222,109,48,195,112,167,218,92,76,71,40,128,221,235,43,81,109,198,51,77,122,183,111,173,195,8,249,40,159,212,136,180,215,171,70,60,108,26,185,11,222,67,47,142,179,158,131,135,153,168,251,199,21,44,7,164,68,212,209,54,75,18,200,117,213,16,175,152,29,146,154,151,236,143,173,86,70,224,138,71,33,33,151,122,205,128,223,166,51,149,125,178,225,234,164,180)), 172) print(173) assert("1ef2a94a2e620a164e07590f006c5a46b722fe7e" == hmac_sha1(string.char(78,109,133,245,198,165,112,63,135,53,124,251,110,183,41,34,153,68,22,253,66,201,9,41,15,99,105,14,252,172,212,39,100,14,128,16,64,47,104,37,33,27,203,120,163,182,52,37,27,224,212,40,134,81,70,29,155,101,246,141,238,41,253,124,222,216,49,49,85,46,51,51,122,39,221,156,104,7,14,52,71,93,31,219), string.char(191,94,78,159,209,12,118,97,214,190,238,108,175,252,192,244,8,104,145,30,236,242,45,49,215,185,251,73,46,101,156,221,136,245,45,240,138,174,187,151,0,122,113,154,195,94,245,186,218,211,203,189,37,251,21,33,225,66,168,152,102,200,245,101,102,217,193,215,194,214,48,131,153,140,75,100,237,240,32,89,113,17,125,177,189,188,212,101,211,212,102,57,251,213,216,14,86,204,198,147,122,97,203,127,100,107,126,107,116,34,220,122,154,130,135,199,9,243,9,194,214)), 173) print(174) assert("ad924bedf84061c998029fb2f168877f0b3939bb" == hmac_sha1(string.char(174,37,249,22,190,230,152,74,215,14,1,180,201,164,238,160,59,157,213,35,36,43,116,160,158,144,131,30,213,232,29,183,205,87,35,85,252,120,126,5,54,113,176), string.char(25,233,197,224,170,150,207,83,58,255,63,236,20,13,179,143,48,248,212,16,220,143,14,123,207,59,28,124,19)), 174) print(175) assert("75a6cfbedde0f1b196209a282b25f5f8b9fef3d1" == hmac_sha1(string.char(131,192,35,70,146,214,224,190,220,240,195,81,129,33,207,253,47,230,111,169,187,136,52,244,217,178,143,140,225,154,105,95,112,201,136,90,221,255,44,31,48,178,90,178,218,122,228,165,90,189,107,45,217,249,89,213,136,139,91,187,202,204,26,250,112), string.char(41,13,202,37,1,104,116,166,245,50,221,59,115,68,187,115,80,93,202,147,10,96,209,213,76,103,143,237,217,21,124,152,82,54,147,207,164,43,89,83,118,67,43,179,79,64,127,252,9,26,125,101,80,192,111,224,104,243,45,67,54,251,238,146,154,212,193,181,138,168,231,171,203,62,93,97,46,55,109,253,214,79,60,62,126,183,235,63,121,46,99,3,223,174,223,2,208,78,7,15,161,56,160,59,205,122,138,77,47,250,107,136,65,199,98,86,131,217,96,226,11,166,185,131,231,76,28,83,140,7,146,87,158,20,102,224,86,5,232,96,34,129,172,236,146,1,139,63,252,1,48,196,242,41,145,97,254,174,88,107,169,70,158,165,246,160,4,16,212,109,236)), 175) print(176) assert("c6f51cc53b0a341dafa4607ee834cffaa8c7c2a2" == hmac_sha1(string.char(85,193,15,80,156,124,171,95,40,75,229,181,147,237,153,83,232,67,210,131,57,69,7,226,226,195,164,46,185,83,120,177,67,77,119,201,9,123,117,111,148,176,12,145,14,80,225,95,71,136,179,94,136,6,78,230,149,135,176,131,19,125,185,48,200,244,111,8,28,170,82,121,242,200,208,145,73,59,234,19,87,61,79,137,245,148,212,94,96,253,227,60,162,170,49,102), string.char(200,235,229,185,225,227,209,209,199,11,112,58,19,202,246,237,94,60,90,176,145,87,191,138,171,88,242,239,21,146,3,92,255,188,99,146,74,134,252,19,201,111,239,76,249,231,71,109,230,240,192,234,253,52,58,173,153,24,131,161,97,222,0,203,177,179,186,160,46,206,73,176,154,39,248,22,66,164,232,120,188,5,73,245,68,8,40,157,155,93,50,207)), 176) print(177) assert("a07ef6f8799ef46062ffea5a43bb3edd30c1fdde" == hmac_sha1(string.char(81,90,37,124,211,22,19,34,50,56,90,45,17,230,233,38,76,24,250), string.char(142,185,46,191,103,207,110,48,71,10,217,115,193,52,131,87,106,39,9,80,216,15,235,169,30,143,3,7,188,78,34,136,18,200,181,140,22,253,141,59,235,25,59,204,147,80,162,108,237,101,167,249,7,168,24,123,215,220,198,38,133,253,72,168,10,139,84,22,195,36,241,128,70,132,28,242,56,40,159,113,184,187,89,38,198,255,176,88,112,125,68,12,57,207,3,251,72,198,135,48,118,244,199,192,205,164,181,243,40,33,195,204,9,78,108,253,114,211,173,121,245,104,13,116,143,160,241,139,210,162,78,62,69,232,40,248,254,177,6,148,177,168,51,253,177,84,152,29,107,91,186,68,118,30,125,62,196,24,14,87,158,83,39,240,192,79,78,61,133,109,106,98,120,232,144,18,49,96,17,202,208,189,152,171,219,19,41,132,179,219,83,220,201,36,191)), 177) print(178) assert("63585793821f635534879a8576194f385f4a551a" == hmac_sha1(string.char(78,213,189,105,102,116,246,215), string.char(162,53,102,158,78,125,120,189,186,214,237,16,219,220,44,24,30,62,32,177,104,217,225,27,92,128,95,202,50,177,239,152,151,161,31,152,93,108,29,125,23,63,55,243,160,169,99,102,15,188,196,129,217,239,132,180,177,251,132,173,62,172,21,139,16,137,231,6,243,185,218,60,60,156,166,153,99,149,144,192,151,249,141,165,222,254,4,111,58,46,106,78,160,215,49,128,252,4,114,236,132,108,114,189,143,45,42,84,134,81,47,140,247,146,247,179,63,14,92,136,120,139)), 178) print(179) assert("95bb1229d26df63839ba4846a41505d48ff98290" == hmac_sha1(string.char(121,190,21,20,39,101,53,249,44,132,19,132,4,114,199,22,32,143,38,33,184,181,66,55,183,240,31,204,225,230,125,186,87,25,90,229,11,55,239,62,101,67,238,1,104,178,191,144,148,105,6,26,127,154,135,247,248,171,41,44,57,83,186,220,42,40,68,153,189,221), string.char(99,150,114,100,37,53,229,202,20,171,246,91,188,188,46,232,70,26,151,35,223,28,97,132,9,242,55,238,98,75,108,91,226,48,236,209,133,92,68,194,241,199,188,86,59,255,230,128,252,193,94,113,134,27,32,50,191,176,159,185,89,209,255,192,9,214,252,63,147,8,92,162,114,72,50,101,136,180,95,39,55,143,137,118,40,14,104,3,149,153,230,135,98,93,72,180,72,86,185,80,107,69,68,20,73)), 179) print(180) assert("9e7c9a258a32e6b3667549cef7a61f307f49b4b9" == hmac_sha1(string.char(105,200,69,23,156,169,151,107,30,139,196,145,128,108,52,7,181,248,255,83,176,169,152,51,158,236,49,191,65,208,155,204,105,179,148,226,142,194,63,253,217,184,69,11,135,185,203,150,146,57,129,80,87,84,231,168,63,186,149,85,128,236,135,217,246,255,121,70,251,68,139,215,199,143,87,13,196,20,50,92,55,116,34,141,117,89,228,94,63,142,250,182,58,91,68,214), string.char(38,52,86,63,109,91,84,11,33,250,194,192,178,94,37,17,65,111,173,17,37,54,163,168,142,91,191,197,161,229,44,163,163,125,81,204,167,185,244,119,75,28,211,87,159)), 180) print(181) assert("7fb01c88e7e519265c18e714efd38bc66f831071" == hmac_sha1(string.char(138,214,212,108,83,152,165,123), string.char(148,223,186,162,237,166,230,68,59,78,220,164,231,42,46,211,239,38,39,150,174,50,58,4,0,135,87,123,123,5,33,252,197,6,53,83,27,178,189,228,166,252,91,141,67,44,68,106,222,17,46,84,214,252,110,181,216,132,14,246,209,57,183,155,238,64,215,121,8,169,157,175,182,191,169,94,116,105,91,49,203,30,155,202,39,140,146,6,115,162,112,130,175,86,143,128,126,249,148,185,39,174,63,83,19,76,164,34,228,97,198,250,65,3,168,158,61,130,167,166,144,185,146,184,160,39,189,11,243,125,255,60,217,46)), 181) print(182) assert("15ab3da2a97592c5f2e22586b4c8a8653411e756" == hmac_sha1(string.char(6,242,91,101,37,118,105,53,170,56,114,144,117,49,53,203,169,216,9,232,9,170,204,129,82,41,210,45,86,176,139,80,152,168,1,154,32,111,89,165,143,205,21,236,105,62,231,106,232,205,7,189,245,52,145,100,78,140,200,183,209,232,196,88,109,175,70,153,150,231,252,7,189,119,97,176,216,201,186,245,24,128,33,57,113,188,184,0,146,248,69,255,77,144,101), string.char(186,246,72,178,189,127,22,42,22,217,81,156,253,142,152,86,42,41,164,77,150,11,208,155,154,185,18,81,156,185,140,224,215,98,1,97,33,194,137,206,144,219,207,210,52,203,198,1,109,182,65,138,122,227,168,207,213,110,206,102,12,181,198,195,133,218,23,187,117,190,56,140,154,239,105,62,96,148,126,187,47,24,139,194,18,211,225,252,195,49,102,138,165,160,90,153,100,140,105,154,242,59,133,226,19,68,125,59,83,140,48,240,226,129,151,79,130,59,96,111,27,73,198)), 182) print(183) assert("7b06792805f4e8062ee9dfbbaffccd787c6f98e1" == hmac_sha1(string.char(83,132,152,16,245,136,91,241,37,158,80,92,65,223,79,3,189,213,89,253,159,93,194,52,218), string.char(212,242,37,98,229,190,238,23,210,197,147,253,82,105,146,168,198,253,2,160,165,188,165,221,179,112,136,72,149,79,138,70,101,95,210,73,157,14,211,92,27,230,177,84,170,183,15,40,0,92,222,252,166,48,1,139,40,16,186,178,231,109,100,125,253,125,123,94,115,94,150,157,186,230,115,163,194,164,30,131,162,90,28,61,44,248,137,8,246,173,31,177,236,39,8,10,192,148,211,36,215,57)), 183) print(184) assert("a28fe22ba0845ae3d57273febbbf1d13e8fde928" == hmac_sha1(string.char(149), string.char(11,139,249,120,214,252,67,75)), 184) print(185) assert("87a9daa85402f6c4b02f1e9fd6edd7e5d178bb88" == hmac_sha1(string.char(114,92,75,169,228,153,182,206,125,139,162,232,77,38,72,129,132,81,11,153,91,92,152,253,10,79,138,142,17,191,161,194,147,213,43,214,39,32,146,46,132,40,77,192,82,54,55,239,50,47), string.char(114,8,110,153,77,155,178,113,203,30,21,41,136,111,176,255,74,214,117,12,189,194,198,37,73,152,196,108,207,188,187,31,227,237,17,1,66,200,73,83,31,61,161,61,31,149,1,152,140,202,163,175,140,136,51,59,121,28,125,192,238,96,192,65,33,177,136,135,173,13,52,101,42,55,189,201,157,244,104,235,148,114,84,154,10,99,153,41,208,213,41,83,193,129,216,46,4,226,81,184,1,249,70,93,183,173,9,29,57,75,209,67,227,49,253,132,129,13,157,55,66,191,136,67,95,108,155,116,17,125,151,217,242,204,110,143,189,201,118)), 185) print(186) assert("6300b2f6236b83de0c84ff1816d246231a5d3b79" == hmac_sha1(string.char(215,10,19,7,73,236,63,23,34,49,11,240,186,195,32,171,67,85,7,95,190,237,77,216,115,36,230,204,139,95,229,37,201,115,81,96,217,205,49,22,169,32,90,2,90,93,137,247,83,229,39,88,235,175,191,246,80,40,228,114,188,115,14,252,35,40,130,240,20,25,202,80,184,27,227,175,149,64,110,223,25,64,57,189,153,176,143,30,76,2,68), string.char(13,245,250,139,223,128,3,6,189,59,12,139,38,153,215,76,250,198,91,117,242,118,34,26,133,244,143,5,56,182,213,167,144,71,250,40,202,81,22,14,72,201,143,10,177,4,166,75,160,156,100,5,147,138,104,98,9,125,81,202,44,120,241,221,181,0,169,155,96,1,4,205,145,105,150,122,135,231,42,165,179,83,39,122,159,193,139,28,4,201,178,41,168,11,177,61,39,14,196,60,29,131,201,91,46,35,128,63,167,48,132,134,105,224,246,244,81,144,209,123,222,190,167,138,76,194,42,152,23,125,52,156,18,217,101,207,239,63,235,3,3,49,147,172,158,97,179,86,233,85,155,245,206,205,165,195,180,223,20,62,197,143,149,126,152)), 186) print(187) assert("35fe4e0cf2417a783d5bdbc17bbc0ab77d2699e7" == hmac_sha1(string.char(15,157,117,128,204,42,12,183,229,129,132,234,228,150,235,100,100,77,160,26,154,230,246,138,150,120,252,20,188,138,171,189,208,62,201,58,158,152,167,165,98,249,52,143,7,26,109,227,144,81,213,157,31,155,26,12,206,103,193,29,142,126,205,123,83,111,179,166,31,32,183,183,100,54,51,205,180,186,160,220,176,233,42,94,62,241,88,74,31,24,22,115,179,124,136,206,78,83), string.char(248,157,191,249,76,86,187,243,10,116,1,227,141,21,104,154,167,72,30,245,6,57,40,170,253,15,4,192,237,237,142,141,196,243,231,245,121,219,150,237,212,118,78,25,201,249,174,21,76,98,187,155,82,243,218,142,239,101,235,240,134,70,212,205,136,165,170,71,121,137,44,8,110,14,167,162,233,92,51,191,178,227,85,134,168,222,121,62,35,127,57,92,80,142,91,112,9,248,129,127,236,2,190,165,125,236,188,117,158,241,181,134,54,133,16,64,100,156,59,83,249,105,235,187,160,186,158,173,52,25,129,20,156,209,102,170,209,10,3,233,12,228,205,138,16,140,108,74,75,65,238,155,251,129,73,236,118,93,202,217,46,180,165,27,84,120,61,186,173,148,131,161,187,30,175,21,249,12,245,145,89,93,61,151,254,29,247,51,198,238,111,52,30,254)), 187) print(188) assert("83c2a380e55ecbc6190b9817bb291a00c36de5e8" == hmac_sha1(string.char(61,128,92,30,94,101,187,67,197,177,98,100,191,221,190,196,86,62,8,238,225,3,93,127,28,126,217,206,56,192,90,9,74,34,43,228,190,162,57,99,170,238,230,196,99,213,31,6,184,11,79,82,100,68,255,40,63), string.char(78,173,248,128,215,226,77,57,233,135,163,11,241,74,59,78,169,219,125,37,182,56,139,89,32,52,80,165,233,52,179,54,216,45,205,209,173,33,12,148,231,66,126,155,191,91,155,174,23,15,14,28,77,186,139,113,48,141,153,177,252,194,23,61,181,189,66,151,45,11,119,74,236,107,206,244,41,117,161,182,233,138,57,196,90,118,28,99,186,150,54,180,105,230,2,41,246,19,80,211,173,102,72,78,82,253,5,183,248,101,173,204,2,145,105,4,160,44,110,211,63,45,46,78)), 188) print(189) assert("bd59d0dcf812107a613a2d892322f532c16ec210" == hmac_sha1(string.char(161,183,101,160,169,208,82,58,88,198,190,85,53,240,56,6,20,136,86,18,216,63,190), string.char(39,227,142,194,37,34,121,168,239,99,70,39,179,128,26,113,47,95,70,129,255,219,63,233,44,33,19,127,22,24,208,50,19,141,185,19,54,82,147,138,44,69)), 189) print(190) assert("6aa92cb64b2e390fbf04dc7edfe3af068109ffba" == hmac_sha1(string.char(202,230,55,100,136,80,156,240,98,73,97,72,191,195,185,105,150,97,148,33,167,140,212,100,247,154,5,152,117,24,128,221,150,49,143,86,12,211,161,75,177,75,145,46,112,143,37,181,84,50,230,81), string.char(131,200,0,161,117,217,5,10,64,79,178,147,64,248,35,147,135,103,157,181,194,123,76,13,150,118,204,63,32,203,155,222,227,148,31,28,218,83,245,17,208,200,104,78,85,31,114,194,67,159,184,167,42,199,241,6,182,195,3,79,22,236,124)), 190) print(191) assert("a42cf08fa6d8ad6ae00251aeed0357dca80645c7" == hmac_sha1(string.char(170,52,245,172,122,225,164,248,23,35,54,243,118,35,165,16,6), string.char(245,115,59,162,179,146,39,213,64,240,80,108,190,127,116,16,154,211,33,171,150,46,77,213,99,88,160,101,236,202,70,26,61,83,79,110,127,74,97,34,18,243,140,34,244,249,225,88,132,102,7,99,220,140,46,111,96,48,93,44,47,186,86,246,244,127,236,150,35,69,111,238,54,24,71,157,254,21,2,194,69,47,190,87,72,235,60,243,40,153,111,55,220,25,91,17,156,195,57,207,107,145,6,82,104,232,209)), 191) print(192) assert("8dd62dcf68ef2d193005921161341839b8cac487" == hmac_sha1(string.char(136,5,195,241,18,208,11,46,252,182,96,183,164,87,248,13), string.char(78,49,162,9,47,79,26,139,66,194,248,90,171,207,167,43,201,223,102,192,49,145,83,42,1,52,250,116,216,133,224,224,17,143,41,153,65,214,231,109,160,5,31,85,17,186,199,226,214,92,21,194,148,98,68,209,50,74,26,150,97,59,223,8,131,173,145,6,31,126,248,86,226,2,94,15,3,202,239,99,177,237,61,99,14,253,26,246,0,151,2,7,159,40,249,166)), 192) print(193) assert("eff66e7a69ae7d2a90b2d80c64ce7c41fe560a19" == hmac_sha1(string.char(123,54,89,205,103,116,21,97,164,41,23,224,151,195,22,196,90,52,253,92), string.char(141,12,168,171,49,30,165,57,107,214,157,224,36,163,182,184,103,186,133,22,2,215,52,220,95,205,231,180,221,139,67,184,21,46,172,95,183,220,31,27,227,122,183,196,114,206,132,162,51,164,84,212,172,63,218,236,127,215,218,77,119,105,14,19,164,53,149,11,13,1,29,246,69,200,208,239,154,60,245,31,172,82,158,165,197,250,151,83,12,156,215,201,66,45,238,228,77,125,157,35,235,162,78,220,132,14,137,168,238,127,10,24,41,132,181,43,95,73,33,32,129,149,145,55,80,156,171,73,90,183,166,182,163,154,31,104,14,56,59,102,72,210,121,191,251,239,1,5,76,116,158,89,40,69,220,107,23,168,106,93,92,95,21,91,118,118,71,29,64,142,84,15,153,193,47,214,30,117,236,217)), 193) print(194) assert("0f1adc518afcca2ed7ad0adaaedd544835ddb76e" == hmac_sha1(string.char(142,36,199,16,52,54,62,146,153,25,162,85,251,247), string.char(110,70,53,237,120,53,50,196,24,82,199,210,156,178,110,124,7,24,229,89,39,1,10,96,74,211,36,79,166,4,33,238,64,220,239,129,48,232,244,172,142,79,154,73,36,107,182,208,191,25,201,19,233,23,60,236,180,76,116,53,232,181,101,62,160,252,213,171,166,113,193,73,75,13,209,63,76,92,95,176,119,0,125,174,42,138,77,23,212,81,180,10,193,118,244,242,163,246,206,150,162,92,20,208,51,71,254,197,11,222,203,153,208,251,243,193,124,141,215,171,55,244,230,78,158,32,223,138,156,43,98,39,191,108,111,6,117,130,195,100,221,233,17,98,180,49,132,139,171,20,180,7,206,35,54,17,48,253,130,185,249,2,52,17,207,73,24,33)), 194) print(195) assert("5e87f8d9a653312fd7b070a33a5d9e67b28b9a84" == hmac_sha1(string.char(255,237,33,255,4,170,95,90,88,77,217,10,94,118,134,20,33,208,17,136,77,18,169,205,112,36,203,231,67,47,186,91,7,17,29), string.char(97,211,98,53,135,238,42,101,144,12,75,185,119,223,114,81,201,94,21,118,16,152,74,215,56,61,131,151,83,96,83,59,49,199,255,207,153,90,231,39,181,4,59,9,2,150,207,124,209,120,37,236,195,66,189,209,137,9,165,78,172,142,209,243,237,145,204,210,222,153,52,127,39,174,133,17,186,219,87,142,209,212,223,60,249,57,244,251,44,254,73,238,52,99,94,237,189,185,237,1,101,166,220,170,103,209)), 195) print(196) assert("837c7cc92e0d2c725b1d1d2f02ef787be37896fa" == hmac_sha1(string.char(150,241,64,89,79,72,88,157,113,139,190,43,211,214,202,52,124,91,111,198,47,161,120,17,120,157,112,248,245,154,254,25,233,96,216), string.char(214,251,73,193,181,158,253,28,23,48,186,168,162,26,124,103,22,195,165,63,189,196,162,229,69,1,208,85,249,24,73,101,243,149,68,236,124,147,108,67,37,75,202,143,57,124,71,176,27,216,208,183,164,173,219,170,79,10,198,1,232,35,9,19,70,131,211,60,37,94,146,98,92,31,150,95,89,189,46,38,156,118,171,137,86,87,32,173,77,14,135,233,233,114,162,136,57,113,73,115,134,87,184)), 196) print(197) assert("64534553f76e55b890101380ad9149d3cacb5b76" == hmac_sha1(string.char(231,24,192,54,17,14,98,122,119,170,71,54,206,207,223,109,164,181,124,186,122,111,122,34,157,24,237,130,207,206,117,76,183,162,174,124,60,60,77,114,139,170,194,143,232,74,160,141,161,171,51,178,162,159,234,208,55), string.char(141,184,241,185,112,45,53,192,72,152,139,145,146,149,145,151,12,140,111,79,158,172,212,175,75,225,127,4,252,104,5,94,105,24,141,183,79,208,240,227,63,126,213,4,13,109,216,244,237,24,196,52,212,245,147,109,18,63,69,107,122,245,101,67,212,232,92,25,135,67,127,247,187,143,224,198,105,38,147,12,238,6,57,53,105,246,0,127,222,69,163,44,11,245,82,220,32,108,33,70,93,177,89,75,192,214,104,236,84,28,221,209,27,12,80,239,111,249,57,39,236,193,17,69,66,180,63,193,242,248,72,22,225,56,198,235,111,102,33,171,159,124,220,143,108,10,71,186,219,213,28,209,145,188,36,70,59,9,235,154,57,12,211,146,6,87,83)), 197) print(198) assert("b7efbbc21ac1a746e22368e814ef5921056331ac" == hmac_sha1(string.char(137,153,151,252,88,36,165,92,194,50,19,117), string.char(155,113,35,47,22,52,144,77,130,20,178,133,75,207,168,146,132,209,160,7,123,190,117,196,147,212,142,25,182,222,56,249,192,228,6,224,250,221,89,7,176,27,37,49,215,192,74,132,127,101,32,23,34,131,23,74,37,226,208,205,162,242,102)), 198) print(199) assert("950ad3222f4917f868d09feab237a909fb6d50b7" == hmac_sha1(string.char(78,46,85,132,231,4,243,255,22,45,240,155,151,119,94,213,50,111,10,83,40,204,49,52,17,69,132,44,213,83,54,251,211,159,123,55,17,58,162,170,210,3,35,237,165,181,217,27,7,249,158,22,158,207,77,121,37,63,37,39,204,68,99,158,78,175,73,183,47,99,134,65,74,234,154,33,14,117,126,98,167,242,106,112,145,82), string.char(144,133,184,16,9,8,227,98,190,60,141,255,87,69,63,214,12,67,14,206,32,120,59,232,176,82,32,194,115,52,148,143,126,86,82,101,167,249,17,169,9,105,228)), 199) skynet.start(skynet.exit) ================================================ FILE: test/testsharetable.lua ================================================ local skynet = require "skynet" local sharetable = require "skynet.sharetable" local function queryall_test() sharetable.loadtable("test_one", {["message"] = "hello one", x = 1, 1}) sharetable.loadtable("test_two", {["message"] = "hello two", x = 2, 2}) sharetable.loadtable("test_three", {["message"] = "hello three", x = 3, 3}) local list = sharetable.queryall({"test_one", "test_two"}) for filename, tbl in pairs(list) do for k, v in pairs(tbl) do print(filename, k, v) end end print("test queryall default") local defaultlist = sharetable.queryall() for filename, tbl in pairs(defaultlist) do for k, v in pairs(tbl) do print(filename, k, v) end end end skynet.start(function() -- You can also use sharetable.loadfile / sharetable.loadstring sharetable.loadtable ("test", { x=1,y={ 'hello world' },['hello world'] = true }) local t = sharetable.query("test") for k,v in pairs(t) do print(k,v) end sharetable.loadstring ("test", "return { ... }", 1,2,3) local t = sharetable.query("test") for k,v in pairs(t) do print(k,v) end queryall_test() end) ================================================ FILE: test/testsm.lua ================================================ local skynet = require "skynet" local sharemap = require "skynet.sharemap" local mode = ... if mode == "slave" then --slave local function dump(reader) reader:update() print("x=", reader.x) print("y=", reader.y) print("s=", reader.s) end skynet.start(function() local reader skynet.dispatch("lua", function(_,_,cmd,...) if cmd == "init" then reader = sharemap.reader(...) else assert(cmd == "ping") dump(reader) end skynet.ret() end) end) else -- master skynet.start(function() -- register share type schema sharemap.register("./test/sharemap.sp") local slave = skynet.newservice(SERVICE_NAME, "slave") local writer = sharemap.writer("foobar", { x=0,y=0,s="hello" }) skynet.call(slave, "lua", "init", "foobar", writer:copy()) writer.x = 1 writer:commit() skynet.call(slave, "lua", "ping") writer.y = 2 writer:commit() skynet.call(slave, "lua", "ping") writer.s = "world" writer:commit() skynet.call(slave, "lua", "ping") end) end ================================================ FILE: test/testsocket.lua ================================================ local skynet = require "skynet" local socket = require "skynet.socket" local mode , id = ... local function echo(id) socket.start(id) socket.write(id, "Hello Skynet\n") while true do local str = socket.read(id) if str then socket.write(id, str) else socket.close(id) return end end end if mode == "agent" then id = tonumber(id) skynet.start(function() skynet.fork(function() echo(id) skynet.exit() end) end) else local function accept(id) skynet.newservice(SERVICE_NAME, "agent", id) end skynet.start(function() local id = socket.listen("127.0.0.1", 8001) print("Listen socket :", "127.0.0.1", 8001) socket.start(id , function(id, addr) print("connect from " .. addr .. " " .. id) -- you have choices : -- 1. skynet.newservice("testsocket", "agent", id) -- 2. skynet.fork(echo, id) -- 3. accept(id) accept(id) end) end) end ================================================ FILE: test/teststm.lua ================================================ local skynet = require "skynet" local stm = require "skynet.stm" local mode = ... if mode == "slave" then skynet.start(function() skynet.dispatch("lua", function (_,_, obj) local obj = stm.newcopy(obj) print("read:", obj(skynet.unpack)) skynet.ret() skynet.error("sleep and read") for i=1,10 do skynet.sleep(10) print("read:", obj(skynet.unpack)) end skynet.exit() end) end) else skynet.start(function() local slave = skynet.newservice(SERVICE_NAME, "slave") local obj = stm.new(skynet.pack(1,2,3,4,5)) local copy = stm.copy(obj) skynet.call(slave, "lua", copy) for i=1,5 do skynet.sleep(20) print("write", i) obj(skynet.pack("hello world", i)) end skynet.exit() end) end ================================================ FILE: test/testterm.lua ================================================ local skynet = require "skynet" local function term() skynet.error("Sleep one second, and term the call to UNEXIST") skynet.sleep(100) local self = skynet.self() skynet.send(skynet.self(), "debug", "TERM", "UNEXIST") end skynet.start(function() skynet.fork(term) skynet.error("call an unexist named service UNEXIST, may block") pcall(skynet.call, "UNEXIST", "lua", "test") skynet.error("unblock the unexisted service call") end) ================================================ FILE: test/testtimeout.lua ================================================ local skynet = require "skynet" local service = require "skynet.service" local function test_service() local skynet = require "skynet" skynet.start(function() skynet.dispatch("lua", function() skynet.error("Wait for 1s") skynet.sleep(100) -- response after 1s for any request skynet.ret() end) end) end local function timeout_call(ti, ...) local token = {} local ret skynet.fork(function(...) ret = table.pack(pcall(skynet.call, ...)) skynet.wakeup(token) end, ...) skynet.sleep(ti, token) if ret then if ret[1] then return table.unpack(ret, 1, ret.n) else error(ret[2]) end else -- timeout return false end end skynet.start(function() local test = service.new("testtimeout", test_service) skynet.error("1", skynet.now()) skynet.call(test, "lua") skynet.error("2", skynet.now()) skynet.error(timeout_call(50, test, "lua")) skynet.error("3", skynet.now()) skynet.error(timeout_call(150, test, "lua")) skynet.error("4", skynet.now()) skynet.exit() end) ================================================ FILE: test/testtimer.lua ================================================ local skynet = require "skynet" local function timeout(t) print(t) end local function wakeup(co) for i=1,5 do skynet.sleep(50) skynet.wakeup(co) end end local function test() skynet.timeout(10, function() print("test timeout 10") end) local taskinfo = {} skynet.task(taskinfo) for session, info in pairs(taskinfo) do print("session = ", session, "trace = ", info) end for i=1,10 do print("test sleep",i,skynet.now()) skynet.sleep(1) end end skynet.start(function() skynet.trace_timeout(true) -- turn on trace for timeout, skynet.task will returns more info. test() skynet.fork(wakeup, coroutine.running()) skynet.timeout(300, function() timeout "Hello World" end) for i = 1, 10 do print(i, skynet.now()) print(skynet.sleep(100)) end skynet.exit() print("Test timer exit") end) ================================================ FILE: test/testtobeclosed.lua ================================================ local skynet = require "skynet" local function new_test(name) return setmetatable({}, { __close = function(...) skynet.error(...) end, __name = "closemeta:" .. name}) end local i = 0 skynet.dispatch("lua", function() i = i + 1 if i==2 then local c = new_test("dispatch_error") error("dispatch_error") else local c = new_test("dispatch_wait") skynet.wait() end end) skynet.start(function() local c = new_test("skynet.exit") skynet.fork(function() local a = new_test("stack_raise_error") error("raise error") end) skynet.fork(function() local a = new_test("session_id_coroutine_wait") skynet.wait() end) skynet.fork(function() local a = new_test("session_id_coroutine_call") skynet.call(skynet.self(), "lua") end) skynet.fork(function() skynet.call(skynet.self(), "lua") end) skynet.sleep(100) skynet.fork(function() local a = new_test("no_running") skynet.wait() end) skynet.exit() end) --[[ testtobeclosed ]] ================================================ FILE: test/testudp.lua ================================================ local skynet = require "skynet" local socket = require "skynet.socket" local function server() local host host = socket.udp(function(str, from) print("server v4 recv", str, socket.udp_address(from)) socket.sendto(host, from, "OK " .. str) end , "127.0.0.1", 8765) -- bind an address end local function client() local c = socket.udp(function(str, from) print("client v4 recv", str, socket.udp_address(from)) end) socket.udp_connect(c, "127.0.0.1", 8765) for i=1,20 do socket.write(c, "hello " .. i) -- write to the address by udp_connect binding end end local function server_v6() local server server = socket.udp_listen("::1", 8766, function(str, from) print(string.format("server_v6 recv str:%s from:%s", str, socket.udp_address(from))) socket.sendto(server, from, "OK " .. str) end) -- bind an address print("create server succeed. "..server) return server end local function client_v6() local c = socket.udp_dial("::1", 8766, function(str, from) print(string.format("client recv v6 response str:%s from:%s", str, socket.udp_address(from))) end) print("create client succeed. "..c) for i=1,20 do socket.write(c, "hello " .. i) -- write to the address by udp_connect binding end end skynet.start(function() skynet.fork(server) skynet.fork(client) skynet.fork(server_v6) skynet.fork(client_v6) end) ================================================ FILE: test/time.lua ================================================ local skynet = require "skynet" skynet.start(function() print(skynet.starttime()) print(skynet.now()) skynet.timeout(1, function() print("in 1", skynet.now()) end) skynet.timeout(2, function() print("in 2", skynet.now()) end) skynet.timeout(3, function() print("in 3", skynet.now()) end) skynet.timeout(4, function() print("in 4", skynet.now()) end) skynet.timeout(100, function() print("in 100", skynet.now()) end) end)