├── .gitignore ├── .gn ├── BUILD.gn ├── build ├── BUILD.gn ├── BUILDCONFIG.gn ├── gn.exe ├── ninja.exe └── toolchain │ ├── BUILD.gn │ ├── cp.py │ ├── msvc.gni │ └── win_find_msvc.py ├── config.gni ├── noumenon ├── BUILD.gn ├── lua │ ├── BUILD.gn │ ├── bin │ │ └── lua_static.lib │ ├── compile.md │ ├── ext │ │ ├── BUILD.gn │ │ ├── unidata.h │ │ └── utf8.c │ ├── lua_machine.cpp │ ├── lua_machine.h │ ├── src │ │ ├── BUILD.gn │ │ ├── lapi.c │ │ ├── lapi.h │ │ ├── lauxlib.c │ │ ├── lauxlib.h │ │ ├── lbaselib.c │ │ ├── lbitlib.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 │ │ ├── llex.c │ │ ├── llex.h │ │ ├── llimits.h │ │ ├── lmathlib.c │ │ ├── lmem.c │ │ ├── lmem.h │ │ ├── loadlib.c │ │ ├── lobject.c │ │ ├── lobject.h │ │ ├── lopcodes.c │ │ ├── lopcodes.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 │ │ ├── ltm.c │ │ ├── ltm.h │ │ ├── lua.h │ │ ├── luaconf.h │ │ ├── lualib.h │ │ ├── lundump.c │ │ ├── lundump.h │ │ ├── lutf8lib.c │ │ ├── lvm.c │ │ ├── lvm.h │ │ ├── lzio.c │ │ └── lzio.h │ └── test_lua_wrapper.cpp ├── main.cpp ├── ui │ ├── BUILD.gn │ ├── test_windows_wrapper.cpp │ ├── win.cpp │ └── win.h └── utils │ ├── BUILD.gn │ ├── codec.cpp │ └── codec.h ├── readme.md └── usage ├── Config └── config.lua ├── GameLive ├── Animation.lua ├── AnimationX.lua ├── Assets.lua ├── Bear.lua ├── Board.lua ├── Button.lua ├── Define.lua ├── EastHero.lua ├── FoodSaler.lua ├── Game.lua ├── Legend.lua ├── Life.lua ├── Map.lua ├── Monster.lua ├── MuMu.lua ├── NPC.lua ├── State.lua ├── TextBox.lua ├── UI.lua └── World.lua ├── GamePainter.exe ├── GameRes ├── Image │ ├── BearL.png │ ├── BearR.png │ ├── Box1.png │ ├── ClothSaler.PNG │ ├── FoodSaler.PNG │ ├── HeroRDL.png │ ├── HeroRDR.png │ ├── HeroRUL.png │ ├── HeroRUR.png │ ├── MumuL.png │ ├── MumuR.png │ ├── Saying.png │ ├── ToolBoard.png │ ├── button1.png │ ├── heroL.png │ ├── heroLS.png │ ├── heroR.png │ ├── heroRD.png │ ├── heroRS.png │ ├── heroRU.png │ ├── numbers1.png │ ├── numbers2.png │ ├── 武器.png │ ├── 武器run.png │ ├── 长安城.jpg │ ├── 长安城b.jpg │ └── 长安城c.jpg └── sound │ └── summer.mp3 ├── 效果图1.jpg └── 效果图2.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | out/ -------------------------------------------------------------------------------- /.gn: -------------------------------------------------------------------------------- 1 | # The location of the build configuration file. 2 | buildconfig = "//build/BUILDCONFIG.gn" 3 | -------------------------------------------------------------------------------- /BUILD.gn: -------------------------------------------------------------------------------- 1 | import("config.gni") 2 | 3 | group("usage") { 4 | deps = [ 5 | "//noumenon:GamePainter", 6 | "//noumenon/lua:lua_wrapper_test", 7 | "//noumenon/ui:test_windows_wrapper", 8 | # "//noumenon/lua/ext:utf8plus", 9 | # "//noumenon/lua/src:lua_static" 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /build/BUILD.gn: -------------------------------------------------------------------------------- 1 | # Copyright 2014 The Chromium Authors. All rights reserved. 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. 4 | import("toolchain/msvc.gni") 5 | 6 | config("compiler_defaults") { 7 | if (is_linux) { 8 | cflags = [ 9 | "-fPIC", 10 | "-pthread", 11 | ] 12 | } 13 | if (is_win) { 14 | cflags = [ 15 | "/bigobj", # Some of our files are bigger than the regular limits. 16 | "/Gy", # Enable function-level linking. 17 | "/FS", # Preserve previous PDB behavior. 18 | "/utf-8", # Assume UTF-8 by default to avoid code page dependencies. 19 | ] 20 | include_dirs = win_msvc_inc_dirs 21 | } 22 | } 23 | 24 | config("executable_ldconfig") { 25 | if (is_linux) { 26 | ldflags = [ 27 | "-Wl,-rpath=\$ORIGIN/", 28 | "-Wl,-rpath-link=", 29 | ] 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /build/BUILDCONFIG.gn: -------------------------------------------------------------------------------- 1 | if (target_os == "") { 2 | target_os = host_os 3 | } 4 | 5 | if (target_cpu == "") { 6 | target_cpu = host_cpu 7 | } 8 | 9 | if (current_os == "") { 10 | current_os = target_os 11 | } 12 | 13 | if (current_cpu == "") { 14 | current_cpu = target_cpu 15 | } 16 | 17 | # 这样设置系统标志,表示暂不考虑交叉编译 18 | is_win = host_os == "win" && current_os == "win" && target_os == "win" 19 | is_linux = host_os == "linux" && current_os == "linux" && target_os == "linux" 20 | is_mac = host_os == "mac" && current_os == "mac" && target_os == "mac" 21 | 22 | # 给部分目标类型指定默认配置 23 | _shared_binary_target_configs = [ "//build:compiler_defaults" ] 24 | 25 | set_defaults("executable") { 26 | configs = _shared_binary_target_configs 27 | configs += [ "//build:executable_ldconfig" ] # Executables get this additional configuration. 28 | } 29 | 30 | set_defaults("static_library") { 31 | configs = _shared_binary_target_configs 32 | } 33 | 34 | set_defaults("shared_library") { 35 | configs = _shared_binary_target_configs 36 | } 37 | 38 | set_defaults("source_set") { 39 | configs = _shared_binary_target_configs 40 | } 41 | 42 | # 设置默认编译工具链 43 | set_default_toolchain("//build/toolchain:msvc") 44 | -------------------------------------------------------------------------------- /build/gn.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/build/gn.exe -------------------------------------------------------------------------------- /build/ninja.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/build/ninja.exe -------------------------------------------------------------------------------- /build/toolchain/BUILD.gn: -------------------------------------------------------------------------------- 1 | import("msvc.gni") 2 | 3 | toolchain("msvc") { 4 | lib_switch = "" 5 | lib_dir_switch = "/LIBPATH:" 6 | sys_lib_flags = string_join(" ", win_msvc_sys_lib_flags) 7 | 8 | # cc = "clang-cl" 9 | # cxx = "clang-cl" 10 | # linker = "lld-link" 11 | cc = "${win_msvc_bin_dir}\\cl.exe" 12 | cxx = "${win_msvc_bin_dir}\\cl.exe" 13 | linker = "${win_msvc_bin_dir}\\link.exe" 14 | 15 | tool("cc") { 16 | # precompiled_header_type = "msvc" 17 | pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb" 18 | depsformat = "msvc" 19 | 20 | command = "$cc /nologo /Y- /EHa /FC {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} /c {{source}} /Fo{{output}} /Fd\"$pdbname\"" 21 | 22 | outputs = [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj" ] 23 | description = "compile {{source}}" 24 | } 25 | 26 | tool("cxx") { 27 | # precompiled_header_type = "msvc" 28 | pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb" 29 | depsformat = "msvc" 30 | 31 | command = "$cxx /nologo /Y- /EHa /FC {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} /c {{source}} /Fo{{output}} /Fd\"$pdbname\"" 32 | 33 | outputs = [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj" ] 34 | description = "compile {{source}}" 35 | } 36 | 37 | tool("alink") { 38 | default_output_dir = "{{root_out_dir}}" # "{{target_out_dir}}" 39 | default_output_extension = ".lib" 40 | libname = "{{output_dir}}/{{target_output_name}}{{output_extension}}" 41 | outputs = [ libname ] 42 | rspfile = "{{output}}.rsp" 43 | 44 | command = "$linker /lib /nologo /ignore:4221 {{arflags}} /OUT:{{output}} @$rspfile" 45 | 46 | rspfile_content = "{{inputs_newline}}" 47 | description = "build static library {{output}}" 48 | } 49 | 50 | tool("solink") { 51 | default_output_dir = "{{root_out_dir}}" 52 | default_output_extension = ".dll" 53 | dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}" 54 | libname = "${dllname}.lib" 55 | pdbname = "${dllname}.pdb" 56 | rspfile = "${dllname}.rsp" 57 | 58 | command = "$linker /nologo /IMPLIB:$libname ${sys_lib_flags} /DLL /OUT:$dllname /PDB:$pdbname @$rspfile" 59 | outputs = [ 60 | dllname, 61 | libname, 62 | pdbname, 63 | ] 64 | 65 | link_output = libname 66 | depend_output = libname 67 | runtime_outputs = [ 68 | dllname, 69 | pdbname, 70 | ] 71 | 72 | # Since the above commands only updates the .lib file when it changes, ask 73 | # Ninja to check if the timestamp actually changed to know if downstream 74 | # dependencies should be recompiled. 75 | restat = true 76 | 77 | # inputs_newline works around a fixed per-line buffer size in the linker. 78 | rspfile_content = "{{inputs_newline}} {{libs}} {{solibs}} {{ldflags}}" 79 | description = "build dynamic library {{output}}" 80 | } 81 | 82 | tool("link") { 83 | default_output_dir = "{{root_out_dir}}" 84 | default_output_extension = ".exe" 85 | exename = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}" 86 | pdbname = "$exename.pdb" 87 | rspfile = "$exename.rsp" 88 | outputs = [ exename ] 89 | 90 | command = "$linker /nologo /OUT:$exename ${sys_lib_flags} /PDB:$pdbname @$rspfile" 91 | 92 | # inputs_newline works around a fixed per-line buffer size in the linker. 93 | rspfile_content = "{{inputs_newline}} {{libs}} {{solibs}} {{ldflags}}" 94 | description = "build executable $exename" 95 | } 96 | 97 | tool("stamp") { 98 | command = "cmd /c type nul > \"{{output}}\"" 99 | description = "STAMP {{output}}" 100 | } 101 | 102 | tool("copy") { 103 | command = "cmd /c python cp.py {{source}} {{output}}" 104 | description = "copy {{source}} {{output}}" 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /build/toolchain/cp.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Copyright (C) 2020 The Android Open Source Project 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | import os 17 | import shutil 18 | import sys 19 | 20 | 21 | def main(): 22 | src, dst = sys.argv[1:] 23 | 24 | if os.path.exists(dst): 25 | if os.path.isdir(dst): 26 | shutil.rmtree(dst) 27 | else: 28 | os.remove(dst) 29 | 30 | if os.path.isdir(src): 31 | shutil.copytree(src, dst) 32 | else: 33 | shutil.copy2(src, dst) 34 | #work around https://github.com/ninja-build/ninja/issues/1554 35 | os.utime(dst, None) 36 | 37 | 38 | if __name__ == '__main__': 39 | sys.exit(main()) 40 | -------------------------------------------------------------------------------- /build/toolchain/msvc.gni: -------------------------------------------------------------------------------- 1 | if (is_win) { 2 | _find_msvc_out = exec_script("win_find_msvc.py", [], "list lines") 3 | 4 | # The output looks like this (without the line number "N:" part): 5 | # 1: C:\Program Files (x86)\Windows Kits\10 6 | # 2: 10.0.19041.0 7 | # 3: C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29333 8 | _win_sdk_base = _find_msvc_out[0] 9 | _win_sdk_ver = _find_msvc_out[1] 10 | _win_msvc_base = _find_msvc_out[2] 11 | 12 | # TODO(primiano): look into how to integrate this with the toolchain pulled by 13 | # depot tools. Also improve error reporting telling the user what to do. 14 | # For now this requires both: 15 | # 1. Build Tools for Visual Studio 2019 16 | # https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019 17 | # 2. Windows 10 SDK: 18 | # https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk/ 19 | 20 | # These variables are required both for clang-cl.exe and MSVC (cl.exe). 21 | win_sdk_lib_dir = _win_sdk_base + "\\Lib\\" + _win_sdk_ver 22 | win_msvc_lib_dir = _win_msvc_base + "\\lib\\${target_cpu}" 23 | 24 | # These variables are only required when building with MSVC. 25 | # Clang is clever enough to figure out the right include path by querying the 26 | # registry and detect the Windows SDK path (it still needs the /LIBPATH 27 | # though, hence the _lib_dir above). 28 | win_msvc_bin_dir = _win_msvc_base + "\\bin\\Host${host_cpu}\\${target_cpu}" 29 | win_msvc_inc_dirs = [ 30 | _win_msvc_base + "\\include", 31 | _win_sdk_base + "\\Include\\" + _win_sdk_ver + "\\ucrt", 32 | _win_sdk_base + "\\Include\\" + _win_sdk_ver + "\\um", 33 | _win_sdk_base + "\\Include\\" + _win_sdk_ver + "\\shared", 34 | ] 35 | win_msvc_sys_lib_flags = [ 36 | "/LIBPATH:\"${win_sdk_lib_dir}\\ucrt\\${target_cpu}\"", 37 | "/LIBPATH:\"${win_sdk_lib_dir}\\um\\${target_cpu}\"", 38 | "/LIBPATH:\"${win_msvc_lib_dir}\"", 39 | ] 40 | } else { 41 | win_sdk_lib_dir = "" 42 | win_msvc_lib_dir = "" 43 | win_msvc_bin_dir = "" 44 | win_msvc_inc_dirs = [] 45 | win_msvc_sys_lib_flags = [] 46 | } 47 | -------------------------------------------------------------------------------- /build/toolchain/win_find_msvc.py: -------------------------------------------------------------------------------- 1 | """ 2 | Finds and prints MSVC and Windows SDK paths. 3 | 4 | It outpus: 5 | Line 1: the base path of the Windows SDK. 6 | Line 2: the most recent version of the Windows SDK. 7 | Line 3: the path of the most recent MSVC. 8 | 9 | Example: 10 | C:\Program Files (x86)\Windows Kits\10 11 | 10.0.19041.0 12 | C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29333 13 | """ 14 | 15 | import os 16 | import subprocess 17 | import sys 18 | 19 | def ver_to_tuple(ver_str): 20 | """Turns '10.1.2' into [10,1,2] so it can be compared using > """ 21 | parts = [int(x) for x in ver_str.split('.')] 22 | assert (len(parts) == 4) 23 | return parts 24 | 25 | def find_max_subdir(base_dir, filter=lambda x: True): 26 | """Finds the max subdirectory in base_dir by comparing semantic versions.""" 27 | max_ver = None 28 | for ver in os.listdir(base_dir) if os.path.exists(base_dir) else []: 29 | cur = os.path.join(base_dir, ver) 30 | if not filter(cur): 31 | continue 32 | if max_ver is None or ver_to_tuple(ver) > ver_to_tuple(max_ver): 33 | max_ver = ver 34 | return max_ver 35 | 36 | def main(): 37 | out = ['', '', ''] 38 | winsdk_base = 'C:\\Program Files (x86)\\Windows Kits\\10' 39 | if os.path.exists(winsdk_base): 40 | out[0] = winsdk_base 41 | lib_base = winsdk_base + '\\Lib' 42 | filt = lambda x: os.path.exists(os.path.join(x, 'ucrt', 'x64', 'ucrt.lib')) 43 | out[1] = find_max_subdir(lib_base, filt) 44 | 45 | versions = [2022, 2019, 2017, 2015, 2013, 2008] 46 | variants = ['BuildTools', 'Community', 'Professional', 'Preview'] 47 | for version in versions: 48 | for variant in variants: 49 | msvc_base = ('C:\\Program Files\\Microsoft Visual Studio\\{}\\' 50 | '{}\\VC\\Tools\\MSVC').format(version, variant) 51 | if os.path.exists(msvc_base): 52 | filt = lambda x: os.path.exists( 53 | os.path.join(x, 'lib', 'x64', 'libcmt.lib')) 54 | max_msvc = find_max_subdir(msvc_base, filt) 55 | if max_msvc is not None: 56 | out[2] = os.path.join(msvc_base, max_msvc) 57 | break 58 | 59 | # Don't error in case of failure, GN scripts are supposed to deal with 60 | # failures and allow the user to override the dirs. 61 | print('\n'.join(out)) 62 | return 0 63 | 64 | if __name__ == '__main__': 65 | sys.exit(main()) 66 | -------------------------------------------------------------------------------- /config.gni: -------------------------------------------------------------------------------- 1 | declare_args() { 2 | use_thread_pool = false 3 | debug_mode = false 4 | } -------------------------------------------------------------------------------- /noumenon/BUILD.gn: -------------------------------------------------------------------------------- 1 | import("//config.gni") 2 | 3 | config("game_painter_config") { 4 | libs = [ 5 | "gdiplus.lib", 6 | "winmm.lib", 7 | "Imm32.lib", 8 | ] 9 | defines = [] 10 | } 11 | 12 | executable("GamePainter") { 13 | sources = [ "main.cpp" ] 14 | configs += [ ":game_painter_config" ] 15 | public_deps = [ 16 | "ui:windows_wrapper", 17 | "lua:lua_wrapper", 18 | "utils:utils", 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /noumenon/lua/BUILD.gn: -------------------------------------------------------------------------------- 1 | import("//config.gni") 2 | 3 | config("lua_wrapper_config") { 4 | include_dirs = [ "./" ] 5 | } 6 | 7 | source_set("lua_wrapper") { 8 | public_configs = [ ":lua_wrapper_config" ] 9 | sources = [ "lua_machine.cpp" ] 10 | public_deps = [ "src:lua" ] 11 | } 12 | 13 | executable("lua_wrapper_test") { 14 | sources = [ "test_lua_wrapper.cpp" ] 15 | public_deps = [ 16 | ":lua_wrapper", 17 | "../utils:utils", 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /noumenon/lua/bin/lua_static.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/noumenon/lua/bin/lua_static.lib -------------------------------------------------------------------------------- /noumenon/lua/compile.md: -------------------------------------------------------------------------------- 1 | # 手动编译 lua 依赖 2 | 3 | ### 编译 lua 静态库 4 | 5 | ```shell 6 | clang -c src/*.c 7 | lib *.o 8 | del *.o 9 | ``` 10 | 11 | 其中`lib`来自 msvc 工具链,请注意配置相关路径。 12 | 13 | ### 编译 lua c++ wrapper 测试程序 14 | 15 | ```shell 16 | clang++ *.cpp ../utils/*.cpp -o test_lua_wrapper.exe -I../utils/ -L./bin/ -llua_static 17 | ``` 18 | 19 | -------------------------------------------------------------------------------- /noumenon/lua/ext/BUILD.gn: -------------------------------------------------------------------------------- 1 | import("//config.gni") 2 | 3 | source_set("utf8_source") { 4 | sources = [ "utf8.c" ] 5 | public_deps = [ "../src:lua" ] 6 | } 7 | 8 | shared_library("utf8plus") { 9 | sources = [ "utf8.c" ] 10 | public_deps = [ "../src:lua" ] 11 | } 12 | -------------------------------------------------------------------------------- /noumenon/lua/lua_machine.cpp: -------------------------------------------------------------------------------- 1 | // 东方盛夏 2022 2 | // https://www.zhihu.com/people/da-xia-tian-60 3 | #include "lua_machine.h" 4 | 5 | #include 6 | #include 7 | 8 | LuaMachine::LuaMachine() 9 | { 10 | lua_state_ = luaL_newstate(); 11 | luaL_openlibs(lua_state_); 12 | } 13 | 14 | LuaMachine::~LuaMachine() 15 | { 16 | if (lua_state_) { 17 | lua_close(lua_state_); 18 | } 19 | } 20 | 21 | bool LuaMachine::LoadScript(const std::string& script) 22 | { 23 | int err = luaL_loadbuffer(lua_state_, script.c_str(), script.length(), "line") || 24 | lua_pcall(lua_state_, 0, 0, 0); 25 | if (err) { 26 | errorCode_ = Error::COMPILE_FAILED; 27 | errorInfo_ = lua_tostring(lua_state_, -1); 28 | lua_pop(lua_state_, 1); 29 | return false; 30 | } 31 | return true; 32 | } 33 | 34 | bool LuaMachine::LoadScriptFromFile(const std::string& filePath) 35 | { 36 | std::fstream file(filePath); 37 | std::stringstream ss; 38 | ss << file.rdbuf(); 39 | return LoadScript(ss.str()); 40 | } 41 | 42 | void LuaMachine::Top(int& value) 43 | { 44 | value = lua_tointeger(lua_state_, -1); 45 | } 46 | 47 | void LuaMachine::Top(double& value) 48 | { 49 | value = lua_tonumber(lua_state_, -1); 50 | } 51 | 52 | void LuaMachine::Top(std::string& value) 53 | { 54 | value = lua_tostring(lua_state_, -1); 55 | } 56 | 57 | bool LuaMachine::LoadSymbol(const std::string& name) 58 | { 59 | lua_getglobal(lua_state_, name.c_str()); 60 | if (!lua_gettop(lua_state_)) { // 堆栈为空,说明变量不存在 61 | errorCode_ = Error::SYMBOL_NOT_FOUND; 62 | errorInfo_ += "undefined variable " + name; 63 | return false; 64 | } 65 | return true; 66 | } 67 | 68 | void LuaMachine::Push() 69 | { 70 | return; 71 | } 72 | 73 | // 调用无参无返回值的空函数 74 | bool LuaMachine::Call(const std::string& name) 75 | { 76 | return Call(name, 0); 77 | } 78 | 79 | // 向 Lua 注册 C 函数 80 | void LuaMachine::RegisterFunction(const std::string& name, lua_CFunction function) 81 | { 82 | lua_register(lua_state_, name.c_str(), function); 83 | } 84 | 85 | // 向 Lua 注册 C 函数库 86 | void LuaMachine::RegisterLibrary(const std::string& name, const luaL_Reg* lib) 87 | { 88 | luaL_register(lua_state_, name.c_str(), lib); 89 | } 90 | -------------------------------------------------------------------------------- /noumenon/lua/lua_machine.h: -------------------------------------------------------------------------------- 1 | // 东方盛夏 2022 2 | // https://www.zhihu.com/people/da-xia-tian-60 3 | #ifndef LUA_MACHINE_H 4 | #define LUA_MACHINE_H 5 | 6 | #ifndef LUA_COMPAT_MODULE 7 | #define LUA_COMPAT_MODULE 8 | #endif 9 | extern "C" { 10 | #include "src/lua.h" 11 | #include "src/lualib.h" 12 | #include "src/lauxlib.h" 13 | } 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | class LuaMachine 20 | { 21 | enum class Error { 22 | OK, 23 | COMPILE_FAILED, 24 | SYMBOL_NOT_FOUND, 25 | CALL_FAILED 26 | }; 27 | 28 | public: 29 | lua_State* lua_state_ {nullptr}; 30 | Error errorCode_ {Error::OK}; 31 | std::string errorInfo_; 32 | 33 | public: 34 | LuaMachine(); 35 | ~LuaMachine(); 36 | 37 | bool LoadScript(const std::string& Code); 38 | bool LoadScriptFromFile(const std::string& filePath); 39 | bool LoadSymbol(const std::string& symbol); 40 | 41 | void Top(int& value); 42 | void Top(double& value); 43 | void Top(std::string& value); 44 | template 45 | void Pop(Type& value); 46 | template 47 | bool Get(const std::string& name, Type& value); 48 | 49 | void Push(); 50 | template 51 | void Push(int value, VarType&&... params); 52 | template 53 | void Push(const std::string& value, VarType&&... params); 54 | template 55 | void Push(double value, VarType&&... params); 56 | 57 | template 58 | void Push(const std::vector& list, VarType&&... params); 59 | template 60 | void Push(const std::map& map, VarType&&... params); 61 | 62 | template 63 | bool Call(const std::string& name, int returnCount, VarType&&... params); 64 | bool Call(const std::string& name); 65 | 66 | void RegisterFunction(const std::string& name, lua_CFunction function); 67 | void RegisterLibrary(const std::string& name, const luaL_Reg* lib); 68 | }; 69 | 70 | template 71 | void LuaMachine::Pop(Type& value) 72 | { 73 | Top(value); 74 | lua_pop(lua_state_, 1); 75 | } 76 | 77 | template 78 | bool LuaMachine::Get(const std::string& name, Type& value) 79 | { 80 | if (LoadSymbol(name)) { 81 | Pop(value); 82 | return true; 83 | } 84 | return false; 85 | } 86 | 87 | template 88 | void LuaMachine::Push(int value, VarType&&... params) 89 | { 90 | lua_pushinteger(lua_state_, value); 91 | Push(std::forward(params)...); 92 | } 93 | 94 | template 95 | void LuaMachine::Push(const std::string& value, VarType&&... params) 96 | { 97 | lua_pushstring(lua_state_, value.c_str()); 98 | Push(std::forward(params)...); 99 | } 100 | 101 | template 102 | void LuaMachine::Push(double value, VarType&&... params) 103 | { 104 | lua_pushnumber(lua_state_, value); 105 | Push(std::forward(params)...); 106 | } 107 | 108 | template 109 | void LuaMachine::Push(const std::vector& list, VarType&&... params) 110 | { 111 | lua_newtable(lua_state_); 112 | for (int i = 0; i < list.size(); ++i) { 113 | lua_pushinteger(lua_state_, i + 1); // key 114 | Push(list[i]); // value 115 | lua_settable(lua_state_, -3); 116 | } 117 | Push(std::forward(params)...); 118 | } 119 | 120 | template 121 | void LuaMachine::Push(const std::map& map, VarType&&... params) 122 | { 123 | lua_newtable(lua_state_); 124 | for (auto& item : map) { 125 | Push(item.first); // key 126 | Push(item.second); // value 127 | lua_settable(lua_state_, -3); 128 | } 129 | Push(std::forward(params)...); 130 | } 131 | 132 | template 133 | bool LuaMachine::Call(const std::string& name, int returnCount, VarType&&... params) 134 | { 135 | if (!LoadSymbol(name)) { 136 | return false; 137 | } 138 | Push(std::forward(params)...); 139 | int err = lua_pcall(lua_state_, sizeof...(params), returnCount, 0); 140 | if (err) { 141 | errorCode_ = Error::CALL_FAILED; 142 | errorInfo_ = lua_tostring(lua_state_, -1); 143 | lua_pop(lua_state_, 1); 144 | return false; 145 | } 146 | return true; 147 | } 148 | 149 | #endif -------------------------------------------------------------------------------- /noumenon/lua/src/BUILD.gn: -------------------------------------------------------------------------------- 1 | import("//config.gni") 2 | 3 | c_files = [ 4 | "lapi.c", 5 | "lauxlib.c", 6 | "lbaselib.c", 7 | "lbitlib.c", 8 | "lcode.c", 9 | "lcorolib.c", 10 | "lctype.c", 11 | "ldblib.c", 12 | "ldebug.c", 13 | "ldo.c", 14 | "ldump.c", 15 | "lfunc.c", 16 | "lgc.c", 17 | "linit.c", 18 | "liolib.c", 19 | "llex.c", 20 | "lmathlib.c", 21 | "lmem.c", 22 | "loadlib.c", 23 | "lobject.c", 24 | "lopcodes.c", 25 | "loslib.c", 26 | "lparser.c", 27 | "lstate.c", 28 | "lstring.c", 29 | "lstrlib.c", 30 | "ltable.c", 31 | "ltablib.c", 32 | "ltm.c", 33 | "lundump.c", 34 | "lutf8lib.c", 35 | "lvm.c", 36 | "lzio.c", 37 | ] 38 | 39 | config("lua_config") { 40 | include_dirs = [ "./" ] 41 | defines = [ "LUA_COMPAT_MODULE" ] 42 | } 43 | 44 | source_set("lua") { 45 | public_configs = [ ":lua_config" ] 46 | sources = c_files 47 | } 48 | 49 | static_library("lua_static") { 50 | public_configs = [ ":lua_config" ] 51 | sources = c_files 52 | } 53 | -------------------------------------------------------------------------------- /noumenon/lua/src/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.9.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "llimits.h" 12 | #include "lstate.h" 13 | 14 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ 15 | "stack overflow");} 16 | 17 | #define adjustresults(L,nres) \ 18 | { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 19 | 20 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ 21 | "not enough elements in the stack") 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /noumenon/lua/src/lauxlib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lauxlib.h,v 1.131.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Auxiliary functions for building Lua libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lauxlib_h 9 | #define lauxlib_h 10 | 11 | 12 | #include 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | 18 | 19 | /* extra error code for 'luaL_loadfilex' */ 20 | #define LUA_ERRFILE (LUA_ERRERR+1) 21 | 22 | 23 | /* key, in the registry, for table of loaded modules */ 24 | #define LUA_LOADED_TABLE "_LOADED" 25 | 26 | 27 | /* key, in the registry, for table of preloaded loaders */ 28 | #define LUA_PRELOAD_TABLE "_PRELOAD" 29 | 30 | 31 | typedef struct luaL_Reg { 32 | const char *name; 33 | lua_CFunction func; 34 | } luaL_Reg; 35 | 36 | 37 | #define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) 38 | 39 | LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); 40 | #define luaL_checkversion(L) \ 41 | luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) 42 | 43 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 44 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 45 | LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); 46 | LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); 47 | LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, 48 | size_t *l); 49 | LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, 50 | const char *def, size_t *l); 51 | LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg); 52 | LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def); 53 | 54 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg); 55 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg, 56 | lua_Integer def); 57 | 58 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 59 | LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t); 60 | LUALIB_API void (luaL_checkany) (lua_State *L, int arg); 61 | 62 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 63 | LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); 64 | LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); 65 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 66 | 67 | LUALIB_API void (luaL_where) (lua_State *L, int lvl); 68 | LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 69 | 70 | LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, 71 | const char *const lst[]); 72 | 73 | LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); 74 | LUALIB_API int (luaL_execresult) (lua_State *L, int stat); 75 | 76 | /* predefined references */ 77 | #define LUA_NOREF (-2) 78 | #define LUA_REFNIL (-1) 79 | 80 | LUALIB_API int (luaL_ref) (lua_State *L, int t); 81 | LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 82 | 83 | LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, 84 | const char *mode); 85 | 86 | #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) 87 | 88 | LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, 89 | const char *name, const char *mode); 90 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 91 | 92 | LUALIB_API lua_State *(luaL_newstate) (void); 93 | 94 | LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); 95 | 96 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, 97 | const char *r); 98 | 99 | LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); 100 | 101 | LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); 102 | 103 | LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, 104 | const char *msg, int level); 105 | 106 | LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, 107 | lua_CFunction openf, int glb); 108 | 109 | /* 110 | ** =============================================================== 111 | ** some useful macros 112 | ** =============================================================== 113 | */ 114 | 115 | 116 | #define luaL_newlibtable(L,l) \ 117 | lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) 118 | 119 | #define luaL_newlib(L,l) \ 120 | (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) 121 | 122 | #define luaL_argcheck(L, cond,arg,extramsg) \ 123 | ((void)((cond) || luaL_argerror(L, (arg), (extramsg)))) 124 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 125 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 126 | 127 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 128 | 129 | #define luaL_dofile(L, fn) \ 130 | (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 131 | 132 | #define luaL_dostring(L, s) \ 133 | (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 134 | 135 | #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 136 | 137 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 138 | 139 | #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) 140 | 141 | 142 | /* 143 | ** {====================================================== 144 | ** Generic Buffer manipulation 145 | ** ======================================================= 146 | */ 147 | 148 | typedef struct luaL_Buffer { 149 | char *b; /* buffer address */ 150 | size_t size; /* buffer size */ 151 | size_t n; /* number of characters in buffer */ 152 | lua_State *L; 153 | char initb[LUAL_BUFFERSIZE]; /* initial buffer */ 154 | } luaL_Buffer; 155 | 156 | 157 | #define luaL_addchar(B,c) \ 158 | ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ 159 | ((B)->b[(B)->n++] = (c))) 160 | 161 | #define luaL_addsize(B,s) ((B)->n += (s)) 162 | 163 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 164 | LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); 165 | LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 166 | LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 167 | LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 168 | LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 169 | LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); 170 | LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); 171 | 172 | #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) 173 | 174 | /* }====================================================== */ 175 | 176 | 177 | 178 | /* 179 | ** {====================================================== 180 | ** File handles for IO library 181 | ** ======================================================= 182 | */ 183 | 184 | /* 185 | ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and 186 | ** initial structure 'luaL_Stream' (it may contain other fields 187 | ** after that initial structure). 188 | */ 189 | 190 | #define LUA_FILEHANDLE "FILE*" 191 | 192 | 193 | typedef struct luaL_Stream { 194 | FILE *f; /* stream (NULL for incompletely created streams) */ 195 | lua_CFunction closef; /* to close stream (NULL for closed streams) */ 196 | } luaL_Stream; 197 | 198 | /* }====================================================== */ 199 | 200 | 201 | 202 | /* compatibility with old module system */ 203 | #if defined(LUA_COMPAT_MODULE) 204 | 205 | LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, 206 | int sizehint); 207 | LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, 208 | const luaL_Reg *l, int nup); 209 | 210 | #define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) 211 | 212 | #endif 213 | 214 | 215 | /* 216 | ** {================================================================== 217 | ** "Abstraction Layer" for basic report of messages and errors 218 | ** =================================================================== 219 | */ 220 | 221 | /* print a string */ 222 | #if !defined(lua_writestring) 223 | #define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) 224 | #endif 225 | 226 | /* print a newline and flush the output */ 227 | #if !defined(lua_writeline) 228 | #define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) 229 | #endif 230 | 231 | /* print an error message */ 232 | #if !defined(lua_writestringerror) 233 | #define lua_writestringerror(s,p) \ 234 | (fprintf(stderr, (s), (p)), fflush(stderr)) 235 | #endif 236 | 237 | /* }================================================================== */ 238 | 239 | 240 | /* 241 | ** {============================================================ 242 | ** Compatibility with deprecated conversions 243 | ** ============================================================= 244 | */ 245 | #if defined(LUA_COMPAT_APIINTCASTS) 246 | 247 | #define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a)) 248 | #define luaL_optunsigned(L,a,d) \ 249 | ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d))) 250 | 251 | #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 252 | #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 253 | 254 | #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 255 | #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 256 | 257 | #endif 258 | /* }============================================================ */ 259 | 260 | 261 | 262 | #endif 263 | 264 | 265 | -------------------------------------------------------------------------------- /noumenon/lua/src/lbitlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lbitlib.c,v 1.30.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Standard library for bitwise operations 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lbitlib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lua.h" 14 | 15 | #include "lauxlib.h" 16 | #include "lualib.h" 17 | 18 | 19 | #if defined(LUA_COMPAT_BITLIB) /* { */ 20 | 21 | 22 | #define pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) 23 | #define checkunsigned(L,i) ((lua_Unsigned)luaL_checkinteger(L,i)) 24 | 25 | 26 | /* number of bits to consider in a number */ 27 | #if !defined(LUA_NBITS) 28 | #define LUA_NBITS 32 29 | #endif 30 | 31 | 32 | /* 33 | ** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must 34 | ** be made in two parts to avoid problems when LUA_NBITS is equal to the 35 | ** number of bits in a lua_Unsigned.) 36 | */ 37 | #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) 38 | 39 | 40 | /* macro to trim extra bits */ 41 | #define trim(x) ((x) & ALLONES) 42 | 43 | 44 | /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ 45 | #define mask(n) (~((ALLONES << 1) << ((n) - 1))) 46 | 47 | 48 | 49 | static lua_Unsigned andaux (lua_State *L) { 50 | int i, n = lua_gettop(L); 51 | lua_Unsigned r = ~(lua_Unsigned)0; 52 | for (i = 1; i <= n; i++) 53 | r &= checkunsigned(L, i); 54 | return trim(r); 55 | } 56 | 57 | 58 | static int b_and (lua_State *L) { 59 | lua_Unsigned r = andaux(L); 60 | pushunsigned(L, r); 61 | return 1; 62 | } 63 | 64 | 65 | static int b_test (lua_State *L) { 66 | lua_Unsigned r = andaux(L); 67 | lua_pushboolean(L, r != 0); 68 | return 1; 69 | } 70 | 71 | 72 | static int b_or (lua_State *L) { 73 | int i, n = lua_gettop(L); 74 | lua_Unsigned r = 0; 75 | for (i = 1; i <= n; i++) 76 | r |= checkunsigned(L, i); 77 | pushunsigned(L, trim(r)); 78 | return 1; 79 | } 80 | 81 | 82 | static int b_xor (lua_State *L) { 83 | int i, n = lua_gettop(L); 84 | lua_Unsigned r = 0; 85 | for (i = 1; i <= n; i++) 86 | r ^= checkunsigned(L, i); 87 | pushunsigned(L, trim(r)); 88 | return 1; 89 | } 90 | 91 | 92 | static int b_not (lua_State *L) { 93 | lua_Unsigned r = ~checkunsigned(L, 1); 94 | pushunsigned(L, trim(r)); 95 | return 1; 96 | } 97 | 98 | 99 | static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) { 100 | if (i < 0) { /* shift right? */ 101 | i = -i; 102 | r = trim(r); 103 | if (i >= LUA_NBITS) r = 0; 104 | else r >>= i; 105 | } 106 | else { /* shift left */ 107 | if (i >= LUA_NBITS) r = 0; 108 | else r <<= i; 109 | r = trim(r); 110 | } 111 | pushunsigned(L, r); 112 | return 1; 113 | } 114 | 115 | 116 | static int b_lshift (lua_State *L) { 117 | return b_shift(L, checkunsigned(L, 1), luaL_checkinteger(L, 2)); 118 | } 119 | 120 | 121 | static int b_rshift (lua_State *L) { 122 | return b_shift(L, checkunsigned(L, 1), -luaL_checkinteger(L, 2)); 123 | } 124 | 125 | 126 | static int b_arshift (lua_State *L) { 127 | lua_Unsigned r = checkunsigned(L, 1); 128 | lua_Integer i = luaL_checkinteger(L, 2); 129 | if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1)))) 130 | return b_shift(L, r, -i); 131 | else { /* arithmetic shift for 'negative' number */ 132 | if (i >= LUA_NBITS) r = ALLONES; 133 | else 134 | r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ 135 | pushunsigned(L, r); 136 | return 1; 137 | } 138 | } 139 | 140 | 141 | static int b_rot (lua_State *L, lua_Integer d) { 142 | lua_Unsigned r = checkunsigned(L, 1); 143 | int i = d & (LUA_NBITS - 1); /* i = d % NBITS */ 144 | r = trim(r); 145 | if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ 146 | r = (r << i) | (r >> (LUA_NBITS - i)); 147 | pushunsigned(L, trim(r)); 148 | return 1; 149 | } 150 | 151 | 152 | static int b_lrot (lua_State *L) { 153 | return b_rot(L, luaL_checkinteger(L, 2)); 154 | } 155 | 156 | 157 | static int b_rrot (lua_State *L) { 158 | return b_rot(L, -luaL_checkinteger(L, 2)); 159 | } 160 | 161 | 162 | /* 163 | ** get field and width arguments for field-manipulation functions, 164 | ** checking whether they are valid. 165 | ** ('luaL_error' called without 'return' to avoid later warnings about 166 | ** 'width' being used uninitialized.) 167 | */ 168 | static int fieldargs (lua_State *L, int farg, int *width) { 169 | lua_Integer f = luaL_checkinteger(L, farg); 170 | lua_Integer w = luaL_optinteger(L, farg + 1, 1); 171 | luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); 172 | luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); 173 | if (f + w > LUA_NBITS) 174 | luaL_error(L, "trying to access non-existent bits"); 175 | *width = (int)w; 176 | return (int)f; 177 | } 178 | 179 | 180 | static int b_extract (lua_State *L) { 181 | int w; 182 | lua_Unsigned r = trim(checkunsigned(L, 1)); 183 | int f = fieldargs(L, 2, &w); 184 | r = (r >> f) & mask(w); 185 | pushunsigned(L, r); 186 | return 1; 187 | } 188 | 189 | 190 | static int b_replace (lua_State *L) { 191 | int w; 192 | lua_Unsigned r = trim(checkunsigned(L, 1)); 193 | lua_Unsigned v = trim(checkunsigned(L, 2)); 194 | int f = fieldargs(L, 3, &w); 195 | lua_Unsigned m = mask(w); 196 | r = (r & ~(m << f)) | ((v & m) << f); 197 | pushunsigned(L, r); 198 | return 1; 199 | } 200 | 201 | 202 | static const luaL_Reg bitlib[] = { 203 | {"arshift", b_arshift}, 204 | {"band", b_and}, 205 | {"bnot", b_not}, 206 | {"bor", b_or}, 207 | {"bxor", b_xor}, 208 | {"btest", b_test}, 209 | {"extract", b_extract}, 210 | {"lrotate", b_lrot}, 211 | {"lshift", b_lshift}, 212 | {"replace", b_replace}, 213 | {"rrotate", b_rrot}, 214 | {"rshift", b_rshift}, 215 | {NULL, NULL} 216 | }; 217 | 218 | 219 | 220 | LUAMOD_API int luaopen_bit32 (lua_State *L) { 221 | luaL_newlib(L, bitlib); 222 | return 1; 223 | } 224 | 225 | 226 | #else /* }{ */ 227 | 228 | 229 | LUAMOD_API int luaopen_bit32 (lua_State *L) { 230 | return luaL_error(L, "library 'bit32' has been deprecated"); 231 | } 232 | 233 | #endif /* } */ 234 | -------------------------------------------------------------------------------- /noumenon/lua/src/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.64.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums (ORDER OP) 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, 28 | OPR_DIV, 29 | OPR_IDIV, 30 | OPR_BAND, OPR_BOR, OPR_BXOR, 31 | OPR_SHL, OPR_SHR, 32 | OPR_CONCAT, 33 | OPR_EQ, OPR_LT, OPR_LE, 34 | OPR_NE, OPR_GT, OPR_GE, 35 | OPR_AND, OPR_OR, 36 | OPR_NOBINOPR 37 | } BinOpr; 38 | 39 | 40 | typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 41 | 42 | 43 | /* get (pointer to) instruction of given 'expdesc' */ 44 | #define getinstruction(fs,e) ((fs)->f->code[(e)->u.info]) 45 | 46 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 47 | 48 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 49 | 50 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 51 | 52 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 53 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 54 | LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); 55 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 56 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 57 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 58 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 59 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 60 | LUAI_FUNC int luaK_intK (FuncState *fs, lua_Integer n); 61 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 62 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 63 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); 64 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 65 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 66 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 67 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 68 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 69 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 70 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); 71 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 72 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 73 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 74 | LUAI_FUNC int luaK_jump (FuncState *fs); 75 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 76 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 77 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 78 | LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); 79 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 80 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 81 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); 82 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 83 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, 84 | expdesc *v2, int line); 85 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 86 | 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /noumenon/lua/src/lcorolib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcorolib.c,v 1.10.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Coroutine Library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lcorolib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lauxlib.h" 18 | #include "lualib.h" 19 | 20 | 21 | static lua_State *getco (lua_State *L) { 22 | lua_State *co = lua_tothread(L, 1); 23 | luaL_argcheck(L, co, 1, "thread expected"); 24 | return co; 25 | } 26 | 27 | 28 | static int auxresume (lua_State *L, lua_State *co, int narg) { 29 | int status; 30 | if (!lua_checkstack(co, narg)) { 31 | lua_pushliteral(L, "too many arguments to resume"); 32 | return -1; /* error flag */ 33 | } 34 | if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { 35 | lua_pushliteral(L, "cannot resume dead coroutine"); 36 | return -1; /* error flag */ 37 | } 38 | lua_xmove(L, co, narg); 39 | status = lua_resume(co, L, narg); 40 | if (status == LUA_OK || status == LUA_YIELD) { 41 | int nres = lua_gettop(co); 42 | if (!lua_checkstack(L, nres + 1)) { 43 | lua_pop(co, nres); /* remove results anyway */ 44 | lua_pushliteral(L, "too many results to resume"); 45 | return -1; /* error flag */ 46 | } 47 | lua_xmove(co, L, nres); /* move yielded values */ 48 | return nres; 49 | } 50 | else { 51 | lua_xmove(co, L, 1); /* move error message */ 52 | return -1; /* error flag */ 53 | } 54 | } 55 | 56 | 57 | static int luaB_coresume (lua_State *L) { 58 | lua_State *co = getco(L); 59 | int r; 60 | r = auxresume(L, co, lua_gettop(L) - 1); 61 | if (r < 0) { 62 | lua_pushboolean(L, 0); 63 | lua_insert(L, -2); 64 | return 2; /* return false + error message */ 65 | } 66 | else { 67 | lua_pushboolean(L, 1); 68 | lua_insert(L, -(r + 1)); 69 | return r + 1; /* return true + 'resume' returns */ 70 | } 71 | } 72 | 73 | 74 | static int luaB_auxwrap (lua_State *L) { 75 | lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 76 | int r = auxresume(L, co, lua_gettop(L)); 77 | if (r < 0) { 78 | if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ 79 | luaL_where(L, 1); /* add extra info */ 80 | lua_insert(L, -2); 81 | lua_concat(L, 2); 82 | } 83 | return lua_error(L); /* propagate error */ 84 | } 85 | return r; 86 | } 87 | 88 | 89 | static int luaB_cocreate (lua_State *L) { 90 | lua_State *NL; 91 | luaL_checktype(L, 1, LUA_TFUNCTION); 92 | NL = lua_newthread(L); 93 | lua_pushvalue(L, 1); /* move function to top */ 94 | lua_xmove(L, NL, 1); /* move function from L to NL */ 95 | return 1; 96 | } 97 | 98 | 99 | static int luaB_cowrap (lua_State *L) { 100 | luaB_cocreate(L); 101 | lua_pushcclosure(L, luaB_auxwrap, 1); 102 | return 1; 103 | } 104 | 105 | 106 | static int luaB_yield (lua_State *L) { 107 | return lua_yield(L, lua_gettop(L)); 108 | } 109 | 110 | 111 | static int luaB_costatus (lua_State *L) { 112 | lua_State *co = getco(L); 113 | if (L == co) lua_pushliteral(L, "running"); 114 | else { 115 | switch (lua_status(co)) { 116 | case LUA_YIELD: 117 | lua_pushliteral(L, "suspended"); 118 | break; 119 | case LUA_OK: { 120 | lua_Debug ar; 121 | if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 122 | lua_pushliteral(L, "normal"); /* it is running */ 123 | else if (lua_gettop(co) == 0) 124 | lua_pushliteral(L, "dead"); 125 | else 126 | lua_pushliteral(L, "suspended"); /* initial state */ 127 | break; 128 | } 129 | default: /* some error occurred */ 130 | lua_pushliteral(L, "dead"); 131 | break; 132 | } 133 | } 134 | return 1; 135 | } 136 | 137 | 138 | static int luaB_yieldable (lua_State *L) { 139 | lua_pushboolean(L, lua_isyieldable(L)); 140 | return 1; 141 | } 142 | 143 | 144 | static int luaB_corunning (lua_State *L) { 145 | int ismain = lua_pushthread(L); 146 | lua_pushboolean(L, ismain); 147 | return 2; 148 | } 149 | 150 | 151 | static const luaL_Reg co_funcs[] = { 152 | {"create", luaB_cocreate}, 153 | {"resume", luaB_coresume}, 154 | {"running", luaB_corunning}, 155 | {"status", luaB_costatus}, 156 | {"wrap", luaB_cowrap}, 157 | {"yield", luaB_yield}, 158 | {"isyieldable", luaB_yieldable}, 159 | {NULL, NULL} 160 | }; 161 | 162 | 163 | 164 | LUAMOD_API int luaopen_coroutine (lua_State *L) { 165 | luaL_newlib(L, co_funcs); 166 | return 1; 167 | } 168 | 169 | -------------------------------------------------------------------------------- /noumenon/lua/src/lctype.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.c,v 1.12.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lctype_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lctype.h" 14 | 15 | #if !LUA_USE_CTYPE /* { */ 16 | 17 | #include 18 | 19 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 20 | 0x00, /* EOZ */ 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 22 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 23 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 24 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 25 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 26 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 27 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 28 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 29 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 30 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 32 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 33 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 34 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 35 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 36 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ 50 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ 52 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 53 | }; 54 | 55 | #endif /* } */ 56 | -------------------------------------------------------------------------------- /noumenon/lua/src/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h,v 1.12.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | /* 65 | ** this 'ltolower' only works for alphabetic characters 66 | */ 67 | #define ltolower(c) ((c) | ('A' ^ 'a')) 68 | 69 | 70 | /* two more entries for 0 and -1 (EOZ) */ 71 | LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; 72 | 73 | 74 | #else /* }{ */ 75 | 76 | /* 77 | ** use standard C ctypes 78 | */ 79 | 80 | #include 81 | 82 | 83 | #define lislalpha(c) (isalpha(c) || (c) == '_') 84 | #define lislalnum(c) (isalnum(c) || (c) == '_') 85 | #define lisdigit(c) (isdigit(c)) 86 | #define lisspace(c) (isspace(c)) 87 | #define lisprint(c) (isprint(c)) 88 | #define lisxdigit(c) (isxdigit(c)) 89 | 90 | #define ltolower(c) (tolower(c)) 91 | 92 | #endif /* } */ 93 | 94 | #endif 95 | 96 | -------------------------------------------------------------------------------- /noumenon/lua/src/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.14.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | 21 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, 22 | const char *opname); 23 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, 24 | const TValue *p2); 25 | LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1, 26 | const TValue *p2, 27 | const char *msg); 28 | LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, 29 | const TValue *p2); 30 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, 31 | const TValue *p2); 32 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 33 | LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg, 34 | TString *src, int line); 35 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 36 | LUAI_FUNC void luaG_traceexec (lua_State *L); 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /noumenon/lua/src/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.29.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | /* 17 | ** Macro to check stack size and grow stack if needed. Parameters 18 | ** 'pre'/'pos' allow the macro to preserve a pointer into the 19 | ** stack across reallocations, doing the work only when needed. 20 | ** 'condmovestack' is used in heavy tests to force a stack reallocation 21 | ** at every check. 22 | */ 23 | #define luaD_checkstackaux(L,n,pre,pos) \ 24 | if (L->stack_last - L->top <= (n)) \ 25 | { pre; luaD_growstack(L, n); pos; } else { condmovestack(L,pre,pos); } 26 | 27 | /* In general, 'pre'/'pos' are empty (nothing to save) */ 28 | #define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) 29 | 30 | 31 | 32 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 33 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 34 | 35 | 36 | /* type of protected functions, to be ran by 'runprotected' */ 37 | typedef void (*Pfunc) (lua_State *L, void *ud); 38 | 39 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 40 | const char *mode); 41 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); 42 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 43 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); 44 | LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); 45 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 46 | ptrdiff_t oldtop, ptrdiff_t ef); 47 | LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, 48 | int nres); 49 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 50 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 51 | LUAI_FUNC void luaD_shrinkstack (lua_State *L); 52 | LUAI_FUNC void luaD_inctop (lua_State *L); 53 | 54 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); 55 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 56 | 57 | #endif 58 | 59 | -------------------------------------------------------------------------------- /noumenon/lua/src/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 2.37.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define ldump_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lobject.h" 18 | #include "lstate.h" 19 | #include "lundump.h" 20 | 21 | 22 | typedef struct { 23 | lua_State *L; 24 | lua_Writer writer; 25 | void *data; 26 | int strip; 27 | int status; 28 | } DumpState; 29 | 30 | 31 | /* 32 | ** All high-level dumps go through DumpVector; you can change it to 33 | ** change the endianness of the result 34 | */ 35 | #define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D) 36 | 37 | #define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D) 38 | 39 | 40 | static void DumpBlock (const void *b, size_t size, DumpState *D) { 41 | if (D->status == 0 && size > 0) { 42 | lua_unlock(D->L); 43 | D->status = (*D->writer)(D->L, b, size, D->data); 44 | lua_lock(D->L); 45 | } 46 | } 47 | 48 | 49 | #define DumpVar(x,D) DumpVector(&x,1,D) 50 | 51 | 52 | static void DumpByte (int y, DumpState *D) { 53 | lu_byte x = (lu_byte)y; 54 | DumpVar(x, D); 55 | } 56 | 57 | 58 | static void DumpInt (int x, DumpState *D) { 59 | DumpVar(x, D); 60 | } 61 | 62 | 63 | static void DumpNumber (lua_Number x, DumpState *D) { 64 | DumpVar(x, D); 65 | } 66 | 67 | 68 | static void DumpInteger (lua_Integer x, DumpState *D) { 69 | DumpVar(x, D); 70 | } 71 | 72 | 73 | static void DumpString (const TString *s, DumpState *D) { 74 | if (s == NULL) 75 | DumpByte(0, D); 76 | else { 77 | size_t size = tsslen(s) + 1; /* include trailing '\0' */ 78 | const char *str = getstr(s); 79 | if (size < 0xFF) 80 | DumpByte(cast_int(size), D); 81 | else { 82 | DumpByte(0xFF, D); 83 | DumpVar(size, D); 84 | } 85 | DumpVector(str, size - 1, D); /* no need to save '\0' */ 86 | } 87 | } 88 | 89 | 90 | static void DumpCode (const Proto *f, DumpState *D) { 91 | DumpInt(f->sizecode, D); 92 | DumpVector(f->code, f->sizecode, D); 93 | } 94 | 95 | 96 | static void DumpFunction(const Proto *f, TString *psource, DumpState *D); 97 | 98 | static void DumpConstants (const Proto *f, DumpState *D) { 99 | int i; 100 | int n = f->sizek; 101 | DumpInt(n, D); 102 | for (i = 0; i < n; i++) { 103 | const TValue *o = &f->k[i]; 104 | DumpByte(ttype(o), D); 105 | switch (ttype(o)) { 106 | case LUA_TNIL: 107 | break; 108 | case LUA_TBOOLEAN: 109 | DumpByte(bvalue(o), D); 110 | break; 111 | case LUA_TNUMFLT: 112 | DumpNumber(fltvalue(o), D); 113 | break; 114 | case LUA_TNUMINT: 115 | DumpInteger(ivalue(o), D); 116 | break; 117 | case LUA_TSHRSTR: 118 | case LUA_TLNGSTR: 119 | DumpString(tsvalue(o), D); 120 | break; 121 | default: 122 | lua_assert(0); 123 | } 124 | } 125 | } 126 | 127 | 128 | static void DumpProtos (const Proto *f, DumpState *D) { 129 | int i; 130 | int n = f->sizep; 131 | DumpInt(n, D); 132 | for (i = 0; i < n; i++) 133 | DumpFunction(f->p[i], f->source, D); 134 | } 135 | 136 | 137 | static void DumpUpvalues (const Proto *f, DumpState *D) { 138 | int i, n = f->sizeupvalues; 139 | DumpInt(n, D); 140 | for (i = 0; i < n; i++) { 141 | DumpByte(f->upvalues[i].instack, D); 142 | DumpByte(f->upvalues[i].idx, D); 143 | } 144 | } 145 | 146 | 147 | static void DumpDebug (const Proto *f, DumpState *D) { 148 | int i, n; 149 | n = (D->strip) ? 0 : f->sizelineinfo; 150 | DumpInt(n, D); 151 | DumpVector(f->lineinfo, n, D); 152 | n = (D->strip) ? 0 : f->sizelocvars; 153 | DumpInt(n, D); 154 | for (i = 0; i < n; i++) { 155 | DumpString(f->locvars[i].varname, D); 156 | DumpInt(f->locvars[i].startpc, D); 157 | DumpInt(f->locvars[i].endpc, D); 158 | } 159 | n = (D->strip) ? 0 : f->sizeupvalues; 160 | DumpInt(n, D); 161 | for (i = 0; i < n; i++) 162 | DumpString(f->upvalues[i].name, D); 163 | } 164 | 165 | 166 | static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { 167 | if (D->strip || f->source == psource) 168 | DumpString(NULL, D); /* no debug info or same source as its parent */ 169 | else 170 | DumpString(f->source, D); 171 | DumpInt(f->linedefined, D); 172 | DumpInt(f->lastlinedefined, D); 173 | DumpByte(f->numparams, D); 174 | DumpByte(f->is_vararg, D); 175 | DumpByte(f->maxstacksize, D); 176 | DumpCode(f, D); 177 | DumpConstants(f, D); 178 | DumpUpvalues(f, D); 179 | DumpProtos(f, D); 180 | DumpDebug(f, D); 181 | } 182 | 183 | 184 | static void DumpHeader (DumpState *D) { 185 | DumpLiteral(LUA_SIGNATURE, D); 186 | DumpByte(LUAC_VERSION, D); 187 | DumpByte(LUAC_FORMAT, D); 188 | DumpLiteral(LUAC_DATA, D); 189 | DumpByte(sizeof(int), D); 190 | DumpByte(sizeof(size_t), D); 191 | DumpByte(sizeof(Instruction), D); 192 | DumpByte(sizeof(lua_Integer), D); 193 | DumpByte(sizeof(lua_Number), D); 194 | DumpInteger(LUAC_INT, D); 195 | DumpNumber(LUAC_NUM, D); 196 | } 197 | 198 | 199 | /* 200 | ** dump Lua function as precompiled chunk 201 | */ 202 | int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, 203 | int strip) { 204 | DumpState D; 205 | D.L = L; 206 | D.writer = w; 207 | D.data = data; 208 | D.strip = strip; 209 | D.status = 0; 210 | DumpHeader(&D); 211 | DumpByte(f->sizeupvalues, &D); 212 | DumpFunction(f, NULL, &D); 213 | return D.status; 214 | } 215 | 216 | -------------------------------------------------------------------------------- /noumenon/lua/src/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 2.45.1.1 2017/04/19 17:39:34 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lfunc_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lfunc.h" 18 | #include "lgc.h" 19 | #include "lmem.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | 23 | 24 | 25 | CClosure *luaF_newCclosure (lua_State *L, int n) { 26 | GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n)); 27 | CClosure *c = gco2ccl(o); 28 | c->nupvalues = cast_byte(n); 29 | return c; 30 | } 31 | 32 | 33 | LClosure *luaF_newLclosure (lua_State *L, int n) { 34 | GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n)); 35 | LClosure *c = gco2lcl(o); 36 | c->p = NULL; 37 | c->nupvalues = cast_byte(n); 38 | while (n--) c->upvals[n] = NULL; 39 | return c; 40 | } 41 | 42 | /* 43 | ** fill a closure with new closed upvalues 44 | */ 45 | void luaF_initupvals (lua_State *L, LClosure *cl) { 46 | int i; 47 | for (i = 0; i < cl->nupvalues; i++) { 48 | UpVal *uv = luaM_new(L, UpVal); 49 | uv->refcount = 1; 50 | uv->v = &uv->u.value; /* make it closed */ 51 | setnilvalue(uv->v); 52 | cl->upvals[i] = uv; 53 | } 54 | } 55 | 56 | 57 | UpVal *luaF_findupval (lua_State *L, StkId level) { 58 | UpVal **pp = &L->openupval; 59 | UpVal *p; 60 | UpVal *uv; 61 | lua_assert(isintwups(L) || L->openupval == NULL); 62 | while (*pp != NULL && (p = *pp)->v >= level) { 63 | lua_assert(upisopen(p)); 64 | if (p->v == level) /* found a corresponding upvalue? */ 65 | return p; /* return it */ 66 | pp = &p->u.open.next; 67 | } 68 | /* not found: create a new upvalue */ 69 | uv = luaM_new(L, UpVal); 70 | uv->refcount = 0; 71 | uv->u.open.next = *pp; /* link it to list of open upvalues */ 72 | uv->u.open.touched = 1; 73 | *pp = uv; 74 | uv->v = level; /* current value lives in the stack */ 75 | if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ 76 | L->twups = G(L)->twups; /* link it to the list */ 77 | G(L)->twups = L; 78 | } 79 | return uv; 80 | } 81 | 82 | 83 | void luaF_close (lua_State *L, StkId level) { 84 | UpVal *uv; 85 | while (L->openupval != NULL && (uv = L->openupval)->v >= level) { 86 | lua_assert(upisopen(uv)); 87 | L->openupval = uv->u.open.next; /* remove from 'open' list */ 88 | if (uv->refcount == 0) /* no references? */ 89 | luaM_free(L, uv); /* free upvalue */ 90 | else { 91 | setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ 92 | uv->v = &uv->u.value; /* now current value lives here */ 93 | luaC_upvalbarrier(L, uv); 94 | } 95 | } 96 | } 97 | 98 | 99 | Proto *luaF_newproto (lua_State *L) { 100 | GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); 101 | Proto *f = gco2p(o); 102 | f->k = NULL; 103 | f->sizek = 0; 104 | f->p = NULL; 105 | f->sizep = 0; 106 | f->code = NULL; 107 | f->cache = NULL; 108 | f->sizecode = 0; 109 | f->lineinfo = NULL; 110 | f->sizelineinfo = 0; 111 | f->upvalues = NULL; 112 | f->sizeupvalues = 0; 113 | f->numparams = 0; 114 | f->is_vararg = 0; 115 | f->maxstacksize = 0; 116 | f->locvars = NULL; 117 | f->sizelocvars = 0; 118 | f->linedefined = 0; 119 | f->lastlinedefined = 0; 120 | f->source = NULL; 121 | return f; 122 | } 123 | 124 | 125 | void luaF_freeproto (lua_State *L, Proto *f) { 126 | luaM_freearray(L, f->code, f->sizecode); 127 | luaM_freearray(L, f->p, f->sizep); 128 | luaM_freearray(L, f->k, f->sizek); 129 | luaM_freearray(L, f->lineinfo, f->sizelineinfo); 130 | luaM_freearray(L, f->locvars, f->sizelocvars); 131 | luaM_freearray(L, f->upvalues, f->sizeupvalues); 132 | luaM_free(L, f); 133 | } 134 | 135 | 136 | /* 137 | ** Look for n-th local variable at line 'line' in function 'func'. 138 | ** Returns NULL if not found. 139 | */ 140 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 141 | int i; 142 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { 143 | if (pc < f->locvars[i].endpc) { /* is variable active? */ 144 | local_number--; 145 | if (local_number == 0) 146 | return getstr(f->locvars[i].varname); 147 | } 148 | } 149 | return NULL; /* not found */ 150 | } 151 | 152 | -------------------------------------------------------------------------------- /noumenon/lua/src/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.15.1.1 2017/04/19 17:39:34 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 15 | cast(int, sizeof(TValue)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TValue *)*((n)-1))) 19 | 20 | 21 | /* test whether thread is in 'twups' list */ 22 | #define isintwups(L) (L->twups != L) 23 | 24 | 25 | /* 26 | ** maximum number of upvalues in a closure (both C and Lua). (Value 27 | ** must fit in a VM register.) 28 | */ 29 | #define MAXUPVAL 255 30 | 31 | 32 | /* 33 | ** Upvalues for Lua closures 34 | */ 35 | struct UpVal { 36 | TValue *v; /* points to stack or to its own value */ 37 | lu_mem refcount; /* reference counter */ 38 | union { 39 | struct { /* (when open) */ 40 | UpVal *next; /* linked list */ 41 | int touched; /* mark to avoid cycles with dead threads */ 42 | } open; 43 | TValue value; /* the value (when closed) */ 44 | } u; 45 | }; 46 | 47 | #define upisopen(up) ((up)->v != &(up)->u.value) 48 | 49 | 50 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 51 | LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); 52 | LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); 53 | LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); 54 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 55 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 56 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 57 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 58 | int pc); 59 | 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /noumenon/lua/src/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.91.1.1 2017/04/19 17:39:34 roberto Exp $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | /* 15 | ** Collectable objects may have one of three colors: white, which 16 | ** means the object is not marked; gray, which means the 17 | ** object is marked, but its references may be not marked; and 18 | ** black, which means that the object and all its references are marked. 19 | ** The main invariant of the garbage collector, while marking objects, 20 | ** is that a black object can never point to a white one. Moreover, 21 | ** any gray object must be in a "gray list" (gray, grayagain, weak, 22 | ** allweak, ephemeron) so that it can be visited again before finishing 23 | ** the collection cycle. These lists have no meaning when the invariant 24 | ** is not being enforced (e.g., sweep phase). 25 | */ 26 | 27 | 28 | 29 | /* how much to allocate before next GC step */ 30 | #if !defined(GCSTEPSIZE) 31 | /* ~100 small strings */ 32 | #define GCSTEPSIZE (cast_int(100 * sizeof(TString))) 33 | #endif 34 | 35 | 36 | /* 37 | ** Possible states of the Garbage Collector 38 | */ 39 | #define GCSpropagate 0 40 | #define GCSatomic 1 41 | #define GCSswpallgc 2 42 | #define GCSswpfinobj 3 43 | #define GCSswptobefnz 4 44 | #define GCSswpend 5 45 | #define GCScallfin 6 46 | #define GCSpause 7 47 | 48 | 49 | #define issweepphase(g) \ 50 | (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend) 51 | 52 | 53 | /* 54 | ** macro to tell when main invariant (white objects cannot point to black 55 | ** ones) must be kept. During a collection, the sweep 56 | ** phase may break the invariant, as objects turned white may point to 57 | ** still-black objects. The invariant is restored when sweep ends and 58 | ** all objects are white again. 59 | */ 60 | 61 | #define keepinvariant(g) ((g)->gcstate <= GCSatomic) 62 | 63 | 64 | /* 65 | ** some useful bit tricks 66 | */ 67 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 68 | #define setbits(x,m) ((x) |= (m)) 69 | #define testbits(x,m) ((x) & (m)) 70 | #define bitmask(b) (1<<(b)) 71 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 72 | #define l_setbit(x,b) setbits(x, bitmask(b)) 73 | #define resetbit(x,b) resetbits(x, bitmask(b)) 74 | #define testbit(x,b) testbits(x, bitmask(b)) 75 | 76 | 77 | /* Layout for bit use in 'marked' field: */ 78 | #define WHITE0BIT 0 /* object is white (type 0) */ 79 | #define WHITE1BIT 1 /* object is white (type 1) */ 80 | #define BLACKBIT 2 /* object is black */ 81 | #define FINALIZEDBIT 3 /* object has been marked for finalization */ 82 | /* bit 7 is currently used by tests (luaL_checkmemory) */ 83 | 84 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 85 | 86 | 87 | #define iswhite(x) testbits((x)->marked, WHITEBITS) 88 | #define isblack(x) testbit((x)->marked, BLACKBIT) 89 | #define isgray(x) /* neither white nor black */ \ 90 | (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) 91 | 92 | #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) 93 | 94 | #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) 95 | #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) 96 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) 97 | 98 | #define changewhite(x) ((x)->marked ^= WHITEBITS) 99 | #define gray2black(x) l_setbit((x)->marked, BLACKBIT) 100 | 101 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 102 | 103 | 104 | /* 105 | ** Does one step of collection when debt becomes positive. 'pre'/'pos' 106 | ** allows some adjustments to be done only when needed. macro 107 | ** 'condchangemem' is used only for heavy tests (forcing a full 108 | ** GC cycle on every opportunity) 109 | */ 110 | #define luaC_condGC(L,pre,pos) \ 111 | { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \ 112 | condchangemem(L,pre,pos); } 113 | 114 | /* more often than not, 'pre'/'pos' are empty */ 115 | #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) 116 | 117 | 118 | #define luaC_barrier(L,p,v) ( \ 119 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ 120 | luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) 121 | 122 | #define luaC_barrierback(L,p,v) ( \ 123 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ 124 | luaC_barrierback_(L,p) : cast_void(0)) 125 | 126 | #define luaC_objbarrier(L,p,o) ( \ 127 | (isblack(p) && iswhite(o)) ? \ 128 | luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) 129 | 130 | #define luaC_upvalbarrier(L,uv) ( \ 131 | (iscollectable((uv)->v) && !upisopen(uv)) ? \ 132 | luaC_upvalbarrier_(L,uv) : cast_void(0)) 133 | 134 | LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); 135 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); 136 | LUAI_FUNC void luaC_step (lua_State *L); 137 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 138 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 139 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); 140 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 141 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o); 142 | LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv); 143 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 144 | LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv); 145 | 146 | 147 | #endif 148 | -------------------------------------------------------------------------------- /noumenon/lua/src/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.39.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Initialization of libraries for lua.c and other clients 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define linit_c 9 | #define LUA_LIB 10 | 11 | /* 12 | ** If you embed Lua in your program and need to open the standard 13 | ** libraries, call luaL_openlibs in your program. If you need a 14 | ** different set of libraries, copy this file to your project and edit 15 | ** it to suit your needs. 16 | ** 17 | ** You can also *preload* libraries, so that a later 'require' can 18 | ** open the library, which is already linked to the application. 19 | ** For that, do the following code: 20 | ** 21 | ** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); 22 | ** lua_pushcfunction(L, luaopen_modname); 23 | ** lua_setfield(L, -2, modname); 24 | ** lua_pop(L, 1); // remove PRELOAD table 25 | */ 26 | 27 | #include "lprefix.h" 28 | 29 | 30 | #include 31 | 32 | #include "lua.h" 33 | 34 | #include "lualib.h" 35 | #include "lauxlib.h" 36 | 37 | 38 | /* 39 | ** these libs are loaded by lua.c and are readily available to any Lua 40 | ** program 41 | */ 42 | static const luaL_Reg loadedlibs[] = { 43 | {"_G", luaopen_base}, 44 | {LUA_LOADLIBNAME, luaopen_package}, 45 | {LUA_COLIBNAME, luaopen_coroutine}, 46 | {LUA_TABLIBNAME, luaopen_table}, 47 | {LUA_IOLIBNAME, luaopen_io}, 48 | {LUA_OSLIBNAME, luaopen_os}, 49 | {LUA_STRLIBNAME, luaopen_string}, 50 | {LUA_MATHLIBNAME, luaopen_math}, 51 | {LUA_UTF8LIBNAME, luaopen_utf8}, 52 | {LUA_DBLIBNAME, luaopen_debug}, 53 | #if defined(LUA_COMPAT_BITLIB) 54 | {LUA_BITLIBNAME, luaopen_bit32}, 55 | #endif 56 | {NULL, NULL} 57 | }; 58 | 59 | 60 | LUALIB_API void luaL_openlibs (lua_State *L) { 61 | const luaL_Reg *lib; 62 | /* "require" functions from 'loadedlibs' and set results to global table */ 63 | for (lib = loadedlibs; lib->func; lib++) { 64 | luaL_requiref(L, lib->name, lib->func, 1); 65 | lua_pop(L, 1); /* remove lib */ 66 | } 67 | } 68 | 69 | -------------------------------------------------------------------------------- /noumenon/lua/src/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.79.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | 17 | #if !defined(LUA_ENV) 18 | #define LUA_ENV "_ENV" 19 | #endif 20 | 21 | 22 | /* 23 | * WARNING: if you change the order of this enumeration, 24 | * grep "ORDER RESERVED" 25 | */ 26 | enum RESERVED { 27 | /* terminal symbols denoted by reserved words */ 28 | TK_AND = FIRST_RESERVED, TK_BREAK, 29 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 30 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 31 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 32 | /* other terminal symbols */ 33 | TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, 34 | TK_SHL, TK_SHR, 35 | TK_DBCOLON, TK_EOS, 36 | TK_FLT, TK_INT, TK_NAME, TK_STRING 37 | }; 38 | 39 | /* number of reserved words */ 40 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 41 | 42 | 43 | typedef union { 44 | lua_Number r; 45 | lua_Integer i; 46 | TString *ts; 47 | } SemInfo; /* semantics information */ 48 | 49 | 50 | typedef struct Token { 51 | int token; 52 | SemInfo seminfo; 53 | } Token; 54 | 55 | 56 | /* state of the lexer plus state of the parser when shared by all 57 | functions */ 58 | typedef struct LexState { 59 | int current; /* current character (charint) */ 60 | int linenumber; /* input line counter */ 61 | int lastline; /* line of last token 'consumed' */ 62 | Token t; /* current token */ 63 | Token lookahead; /* look ahead token */ 64 | struct FuncState *fs; /* current function (parser) */ 65 | struct lua_State *L; 66 | ZIO *z; /* input stream */ 67 | Mbuffer *buff; /* buffer for tokens */ 68 | Table *h; /* to avoid collection/reuse strings */ 69 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 70 | TString *source; /* current source name */ 71 | TString *envn; /* environment variable name */ 72 | } LexState; 73 | 74 | 75 | LUAI_FUNC void luaX_init (lua_State *L); 76 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 77 | TString *source, int firstchar); 78 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 79 | LUAI_FUNC void luaX_next (LexState *ls); 80 | LUAI_FUNC int luaX_lookahead (LexState *ls); 81 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); 82 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 83 | 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /noumenon/lua/src/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.141.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Limits, basic types, and some other 'installation-dependent' definitions 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llimits_h 8 | #define llimits_h 9 | 10 | 11 | #include 12 | #include 13 | 14 | 15 | #include "lua.h" 16 | 17 | /* 18 | ** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count 19 | ** the total memory used by Lua (in bytes). Usually, 'size_t' and 20 | ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. 21 | */ 22 | #if defined(LUAI_MEM) /* { external definitions? */ 23 | typedef LUAI_UMEM lu_mem; 24 | typedef LUAI_MEM l_mem; 25 | #elif LUAI_BITSINT >= 32 /* }{ */ 26 | typedef size_t lu_mem; 27 | typedef ptrdiff_t l_mem; 28 | #else /* 16-bit ints */ /* }{ */ 29 | typedef unsigned long lu_mem; 30 | typedef long l_mem; 31 | #endif /* } */ 32 | 33 | 34 | /* chars used as small naturals (so that 'char' is reserved for characters) */ 35 | typedef unsigned char lu_byte; 36 | 37 | 38 | /* maximum value for size_t */ 39 | #define MAX_SIZET ((size_t)(~(size_t)0)) 40 | 41 | /* maximum size visible for Lua (must be representable in a lua_Integer */ 42 | #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ 43 | : (size_t)(LUA_MAXINTEGER)) 44 | 45 | 46 | #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)) 47 | 48 | #define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1)) 49 | 50 | 51 | #define MAX_INT INT_MAX /* maximum value of an int */ 52 | 53 | 54 | /* 55 | ** conversion of pointer to unsigned integer: 56 | ** this is for hashing only; there is no problem if the integer 57 | ** cannot hold the whole pointer value 58 | */ 59 | #define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX)) 60 | 61 | 62 | 63 | /* type to ensure maximum alignment */ 64 | #if defined(LUAI_USER_ALIGNMENT_T) 65 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 66 | #else 67 | typedef union { 68 | lua_Number n; 69 | double u; 70 | void *s; 71 | lua_Integer i; 72 | long l; 73 | } L_Umaxalign; 74 | #endif 75 | 76 | 77 | 78 | /* types of 'usual argument conversions' for lua_Number and lua_Integer */ 79 | typedef LUAI_UACNUMBER l_uacNumber; 80 | typedef LUAI_UACINT l_uacInt; 81 | 82 | 83 | /* internal assertions for in-house debugging */ 84 | #if defined(lua_assert) 85 | #define check_exp(c,e) (lua_assert(c), (e)) 86 | /* to avoid problems with conditions too long */ 87 | #define lua_longassert(c) ((c) ? (void)0 : lua_assert(0)) 88 | #else 89 | #define lua_assert(c) ((void)0) 90 | #define check_exp(c,e) (e) 91 | #define lua_longassert(c) ((void)0) 92 | #endif 93 | 94 | /* 95 | ** assertion for checking API calls 96 | */ 97 | #if !defined(luai_apicheck) 98 | #define luai_apicheck(l,e) lua_assert(e) 99 | #endif 100 | 101 | #define api_check(l,e,msg) luai_apicheck(l,(e) && msg) 102 | 103 | 104 | /* macro to avoid warnings about unused variables */ 105 | #if !defined(UNUSED) 106 | #define UNUSED(x) ((void)(x)) 107 | #endif 108 | 109 | 110 | /* type casts (a macro highlights casts in the code) */ 111 | #define cast(t, exp) ((t)(exp)) 112 | 113 | #define cast_void(i) cast(void, (i)) 114 | #define cast_byte(i) cast(lu_byte, (i)) 115 | #define cast_num(i) cast(lua_Number, (i)) 116 | #define cast_int(i) cast(int, (i)) 117 | #define cast_uchar(i) cast(unsigned char, (i)) 118 | 119 | 120 | /* cast a signed lua_Integer to lua_Unsigned */ 121 | #if !defined(l_castS2U) 122 | #define l_castS2U(i) ((lua_Unsigned)(i)) 123 | #endif 124 | 125 | /* 126 | ** cast a lua_Unsigned to a signed lua_Integer; this cast is 127 | ** not strict ISO C, but two-complement architectures should 128 | ** work fine. 129 | */ 130 | #if !defined(l_castU2S) 131 | #define l_castU2S(i) ((lua_Integer)(i)) 132 | #endif 133 | 134 | 135 | /* 136 | ** non-return type 137 | */ 138 | #if defined(__GNUC__) 139 | #define l_noret void __attribute__((noreturn)) 140 | #elif defined(_MSC_VER) && _MSC_VER >= 1200 141 | #define l_noret void __declspec(noreturn) 142 | #else 143 | #define l_noret void 144 | #endif 145 | 146 | 147 | 148 | /* 149 | ** maximum depth for nested C calls and syntactical nested non-terminals 150 | ** in a program. (Value must fit in an unsigned short int.) 151 | */ 152 | #if !defined(LUAI_MAXCCALLS) 153 | #define LUAI_MAXCCALLS 200 154 | #endif 155 | 156 | 157 | 158 | /* 159 | ** type for virtual-machine instructions; 160 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 161 | */ 162 | #if LUAI_BITSINT >= 32 163 | typedef unsigned int Instruction; 164 | #else 165 | typedef unsigned long Instruction; 166 | #endif 167 | 168 | 169 | 170 | /* 171 | ** Maximum length for short strings, that is, strings that are 172 | ** internalized. (Cannot be smaller than reserved words or tags for 173 | ** metamethods, as these strings must be internalized; 174 | ** #("function") = 8, #("__newindex") = 10.) 175 | */ 176 | #if !defined(LUAI_MAXSHORTLEN) 177 | #define LUAI_MAXSHORTLEN 40 178 | #endif 179 | 180 | 181 | /* 182 | ** Initial size for the string table (must be power of 2). 183 | ** The Lua core alone registers ~50 strings (reserved words + 184 | ** metaevent keys + a few others). Libraries would typically add 185 | ** a few dozens more. 186 | */ 187 | #if !defined(MINSTRTABSIZE) 188 | #define MINSTRTABSIZE 128 189 | #endif 190 | 191 | 192 | /* 193 | ** Size of cache for strings in the API. 'N' is the number of 194 | ** sets (better be a prime) and "M" is the size of each set (M == 1 195 | ** makes a direct cache.) 196 | */ 197 | #if !defined(STRCACHE_N) 198 | #define STRCACHE_N 53 199 | #define STRCACHE_M 2 200 | #endif 201 | 202 | 203 | /* minimum size for string buffer */ 204 | #if !defined(LUA_MINBUFFER) 205 | #define LUA_MINBUFFER 32 206 | #endif 207 | 208 | 209 | /* 210 | ** macros that are executed whenever program enters the Lua core 211 | ** ('lua_lock') and leaves the core ('lua_unlock') 212 | */ 213 | #if !defined(lua_lock) 214 | #define lua_lock(L) ((void) 0) 215 | #define lua_unlock(L) ((void) 0) 216 | #endif 217 | 218 | /* 219 | ** macro executed during Lua functions at points where the 220 | ** function can yield. 221 | */ 222 | #if !defined(luai_threadyield) 223 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 224 | #endif 225 | 226 | 227 | /* 228 | ** these macros allow user-specific actions on threads when you defined 229 | ** LUAI_EXTRASPACE and need to do something extra when a thread is 230 | ** created/deleted/resumed/yielded. 231 | */ 232 | #if !defined(luai_userstateopen) 233 | #define luai_userstateopen(L) ((void)L) 234 | #endif 235 | 236 | #if !defined(luai_userstateclose) 237 | #define luai_userstateclose(L) ((void)L) 238 | #endif 239 | 240 | #if !defined(luai_userstatethread) 241 | #define luai_userstatethread(L,L1) ((void)L) 242 | #endif 243 | 244 | #if !defined(luai_userstatefree) 245 | #define luai_userstatefree(L,L1) ((void)L) 246 | #endif 247 | 248 | #if !defined(luai_userstateresume) 249 | #define luai_userstateresume(L,n) ((void)L) 250 | #endif 251 | 252 | #if !defined(luai_userstateyield) 253 | #define luai_userstateyield(L,n) ((void)L) 254 | #endif 255 | 256 | 257 | 258 | /* 259 | ** The luai_num* macros define the primitive operations over numbers. 260 | */ 261 | 262 | /* floor division (defined as 'floor(a/b)') */ 263 | #if !defined(luai_numidiv) 264 | #define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b))) 265 | #endif 266 | 267 | /* float division */ 268 | #if !defined(luai_numdiv) 269 | #define luai_numdiv(L,a,b) ((a)/(b)) 270 | #endif 271 | 272 | /* 273 | ** modulo: defined as 'a - floor(a/b)*b'; this definition gives NaN when 274 | ** 'b' is huge, but the result should be 'a'. 'fmod' gives the result of 275 | ** 'a - trunc(a/b)*b', and therefore must be corrected when 'trunc(a/b) 276 | ** ~= floor(a/b)'. That happens when the division has a non-integer 277 | ** negative result, which is equivalent to the test below. 278 | */ 279 | #if !defined(luai_nummod) 280 | #define luai_nummod(L,a,b,m) \ 281 | { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); } 282 | #endif 283 | 284 | /* exponentiation */ 285 | #if !defined(luai_numpow) 286 | #define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b)) 287 | #endif 288 | 289 | /* the others are quite standard operations */ 290 | #if !defined(luai_numadd) 291 | #define luai_numadd(L,a,b) ((a)+(b)) 292 | #define luai_numsub(L,a,b) ((a)-(b)) 293 | #define luai_nummul(L,a,b) ((a)*(b)) 294 | #define luai_numunm(L,a) (-(a)) 295 | #define luai_numeq(a,b) ((a)==(b)) 296 | #define luai_numlt(a,b) ((a)<(b)) 297 | #define luai_numle(a,b) ((a)<=(b)) 298 | #define luai_numisnan(a) (!luai_numeq((a), (a))) 299 | #endif 300 | 301 | 302 | 303 | 304 | 305 | /* 306 | ** macro to control inclusion of some hard tests on stack reallocation 307 | */ 308 | #if !defined(HARDSTACKTESTS) 309 | #define condmovestack(L,pre,pos) ((void)0) 310 | #else 311 | /* realloc stack keeping its size */ 312 | #define condmovestack(L,pre,pos) \ 313 | { int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_); pos; } 314 | #endif 315 | 316 | #if !defined(HARDMEMTESTS) 317 | #define condchangemem(L,pre,pos) ((void)0) 318 | #else 319 | #define condchangemem(L,pre,pos) \ 320 | { if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } } 321 | #endif 322 | 323 | #endif 324 | -------------------------------------------------------------------------------- /noumenon/lua/src/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.91.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lmem_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lgc.h" 20 | #include "lmem.h" 21 | #include "lobject.h" 22 | #include "lstate.h" 23 | 24 | 25 | 26 | /* 27 | ** About the realloc function: 28 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 29 | ** ('osize' is the old size, 'nsize' is the new size) 30 | ** 31 | ** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no 32 | ** matter 'x'). 33 | ** 34 | ** * frealloc(ud, p, x, 0) frees the block 'p' 35 | ** (in this specific case, frealloc must return NULL); 36 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 37 | ** (which is equivalent to free(NULL) in ISO C) 38 | ** 39 | ** frealloc returns NULL if it cannot create or reallocate the area 40 | ** (any reallocation to an equal or smaller size cannot fail!) 41 | */ 42 | 43 | 44 | 45 | #define MINSIZEARRAY 4 46 | 47 | 48 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 49 | int limit, const char *what) { 50 | void *newblock; 51 | int newsize; 52 | if (*size >= limit/2) { /* cannot double it? */ 53 | if (*size >= limit) /* cannot grow even a little? */ 54 | luaG_runerror(L, "too many %s (limit is %d)", what, limit); 55 | newsize = limit; /* still have at least one free place */ 56 | } 57 | else { 58 | newsize = (*size)*2; 59 | if (newsize < MINSIZEARRAY) 60 | newsize = MINSIZEARRAY; /* minimum size */ 61 | } 62 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 63 | *size = newsize; /* update only when everything else is OK */ 64 | return newblock; 65 | } 66 | 67 | 68 | l_noret luaM_toobig (lua_State *L) { 69 | luaG_runerror(L, "memory allocation error: block too big"); 70 | } 71 | 72 | 73 | 74 | /* 75 | ** generic allocation routine. 76 | */ 77 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 78 | void *newblock; 79 | global_State *g = G(L); 80 | size_t realosize = (block) ? osize : 0; 81 | lua_assert((realosize == 0) == (block == NULL)); 82 | #if defined(HARDMEMTESTS) 83 | if (nsize > realosize && g->gcrunning) 84 | luaC_fullgc(L, 1); /* force a GC whenever possible */ 85 | #endif 86 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); 87 | if (newblock == NULL && nsize > 0) { 88 | lua_assert(nsize > realosize); /* cannot fail when shrinking a block */ 89 | if (g->version) { /* is state fully built? */ 90 | luaC_fullgc(L, 1); /* try to free some memory... */ 91 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 92 | } 93 | if (newblock == NULL) 94 | luaD_throw(L, LUA_ERRMEM); 95 | } 96 | lua_assert((nsize == 0) == (newblock == NULL)); 97 | g->GCdebt = (g->GCdebt + nsize) - realosize; 98 | return newblock; 99 | } 100 | 101 | -------------------------------------------------------------------------------- /noumenon/lua/src/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.43.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | 17 | /* 18 | ** This macro reallocs a vector 'b' from 'on' to 'n' elements, where 19 | ** each element has size 'e'. In case of arithmetic overflow of the 20 | ** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because 21 | ** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e). 22 | ** 23 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof' 24 | ** comparison avoids a runtime comparison when overflow cannot occur. 25 | ** The compiler should be able to optimize the real test by itself, but 26 | ** when it does it, it may give a warning about "comparison is always 27 | ** false due to limited range of data type"; the +1 tricks the compiler, 28 | ** avoiding this warning but also this optimization.) 29 | */ 30 | #define luaM_reallocv(L,b,on,n,e) \ 31 | (((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \ 32 | ? luaM_toobig(L) : cast_void(0)) , \ 33 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) 34 | 35 | /* 36 | ** Arrays of chars do not need any test 37 | */ 38 | #define luaM_reallocvchar(L,b,on,n) \ 39 | cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) 40 | 41 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 42 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 43 | #define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0) 44 | 45 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) 46 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 47 | #define luaM_newvector(L,n,t) \ 48 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 49 | 50 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) 51 | 52 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 53 | if ((nelems)+1 > (size)) \ 54 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 55 | 56 | #define luaM_reallocvector(L, v,oldn,n,t) \ 57 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 58 | 59 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 60 | 61 | /* not to be called directly */ 62 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 63 | size_t size); 64 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 65 | size_t size_elem, int limit, 66 | const char *what); 67 | 68 | #endif 69 | 70 | -------------------------------------------------------------------------------- /noumenon/lua/src/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.55.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lopcodes.h" 16 | 17 | 18 | /* ORDER OP */ 19 | 20 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { 21 | "MOVE", 22 | "LOADK", 23 | "LOADKX", 24 | "LOADBOOL", 25 | "LOADNIL", 26 | "GETUPVAL", 27 | "GETTABUP", 28 | "GETTABLE", 29 | "SETTABUP", 30 | "SETUPVAL", 31 | "SETTABLE", 32 | "NEWTABLE", 33 | "SELF", 34 | "ADD", 35 | "SUB", 36 | "MUL", 37 | "MOD", 38 | "POW", 39 | "DIV", 40 | "IDIV", 41 | "BAND", 42 | "BOR", 43 | "BXOR", 44 | "SHL", 45 | "SHR", 46 | "UNM", 47 | "BNOT", 48 | "NOT", 49 | "LEN", 50 | "CONCAT", 51 | "JMP", 52 | "EQ", 53 | "LT", 54 | "LE", 55 | "TEST", 56 | "TESTSET", 57 | "CALL", 58 | "TAILCALL", 59 | "RETURN", 60 | "FORLOOP", 61 | "FORPREP", 62 | "TFORCALL", 63 | "TFORLOOP", 64 | "SETLIST", 65 | "CLOSURE", 66 | "VARARG", 67 | "EXTRAARG", 68 | NULL 69 | }; 70 | 71 | 72 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 73 | 74 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 75 | /* T A B C mode opcode */ 76 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 77 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 78 | ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ 79 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 80 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ 81 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 82 | ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ 83 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 84 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ 85 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 86 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 87 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 88 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 89 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 90 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 91 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 92 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 93 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 94 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 95 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_IDIV */ 96 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BAND */ 97 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BOR */ 98 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BXOR */ 99 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHL */ 100 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHR */ 101 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 102 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_BNOT */ 103 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 104 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 105 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 106 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 107 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 108 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 109 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 110 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ 111 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 112 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 113 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 114 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 115 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 116 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 117 | ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ 118 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ 119 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 120 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 121 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 122 | ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ 123 | }; 124 | 125 | -------------------------------------------------------------------------------- /noumenon/lua/src/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.76.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression and variable descriptor. 17 | ** Code generation for variables and expressions can be delayed to allow 18 | ** optimizations; An 'expdesc' structure describes a potentially-delayed 19 | ** variable/expression. It has a description of its "main" value plus a 20 | ** list of conditional jumps that can also produce its value (generated 21 | ** by short-circuit operators 'and'/'or'). 22 | */ 23 | 24 | /* kinds of variables/expressions */ 25 | typedef enum { 26 | VVOID, /* when 'expdesc' describes the last expression a list, 27 | this kind means an empty list (so, no expression) */ 28 | VNIL, /* constant nil */ 29 | VTRUE, /* constant true */ 30 | VFALSE, /* constant false */ 31 | VK, /* constant in 'k'; info = index of constant in 'k' */ 32 | VKFLT, /* floating constant; nval = numerical float value */ 33 | VKINT, /* integer constant; nval = numerical integer value */ 34 | VNONRELOC, /* expression has its value in a fixed register; 35 | info = result register */ 36 | VLOCAL, /* local variable; info = local register */ 37 | VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ 38 | VINDEXED, /* indexed variable; 39 | ind.vt = whether 't' is register or upvalue; 40 | ind.t = table register or upvalue; 41 | ind.idx = key's R/K index */ 42 | VJMP, /* expression is a test/comparison; 43 | info = pc of corresponding jump instruction */ 44 | VRELOCABLE, /* expression can put result in any register; 45 | info = instruction pc */ 46 | VCALL, /* expression is a function call; info = instruction pc */ 47 | VVARARG /* vararg expression; info = instruction pc */ 48 | } expkind; 49 | 50 | 51 | #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) 52 | #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) 53 | 54 | typedef struct expdesc { 55 | expkind k; 56 | union { 57 | lua_Integer ival; /* for VKINT */ 58 | lua_Number nval; /* for VKFLT */ 59 | int info; /* for generic use */ 60 | struct { /* for indexed variables (VINDEXED) */ 61 | short idx; /* index (R/K) */ 62 | lu_byte t; /* table (register or upvalue) */ 63 | lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ 64 | } ind; 65 | } u; 66 | int t; /* patch list of 'exit when true' */ 67 | int f; /* patch list of 'exit when false' */ 68 | } expdesc; 69 | 70 | 71 | /* description of active local variable */ 72 | typedef struct Vardesc { 73 | short idx; /* variable index in stack */ 74 | } Vardesc; 75 | 76 | 77 | /* description of pending goto statements and label statements */ 78 | typedef struct Labeldesc { 79 | TString *name; /* label identifier */ 80 | int pc; /* position in code */ 81 | int line; /* line where it appeared */ 82 | lu_byte nactvar; /* local level where it appears in current block */ 83 | } Labeldesc; 84 | 85 | 86 | /* list of labels or gotos */ 87 | typedef struct Labellist { 88 | Labeldesc *arr; /* array */ 89 | int n; /* number of entries in use */ 90 | int size; /* array size */ 91 | } Labellist; 92 | 93 | 94 | /* dynamic structures used by the parser */ 95 | typedef struct Dyndata { 96 | struct { /* list of active local variables */ 97 | Vardesc *arr; 98 | int n; 99 | int size; 100 | } actvar; 101 | Labellist gt; /* list of pending gotos */ 102 | Labellist label; /* list of active labels */ 103 | } Dyndata; 104 | 105 | 106 | /* control of blocks */ 107 | struct BlockCnt; /* defined in lparser.c */ 108 | 109 | 110 | /* state needed to generate code for a given function */ 111 | typedef struct FuncState { 112 | Proto *f; /* current function header */ 113 | struct FuncState *prev; /* enclosing function */ 114 | struct LexState *ls; /* lexical state */ 115 | struct BlockCnt *bl; /* chain of current blocks */ 116 | int pc; /* next position to code (equivalent to 'ncode') */ 117 | int lasttarget; /* 'label' of last 'jump label' */ 118 | int jpc; /* list of pending jumps to 'pc' */ 119 | int nk; /* number of elements in 'k' */ 120 | int np; /* number of elements in 'p' */ 121 | int firstlocal; /* index of first local var (in Dyndata array) */ 122 | short nlocvars; /* number of elements in 'f->locvars' */ 123 | lu_byte nactvar; /* number of active local variables */ 124 | lu_byte nups; /* number of upvalues */ 125 | lu_byte freereg; /* first free register */ 126 | } FuncState; 127 | 128 | 129 | LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 130 | Dyndata *dyd, const char *name, int firstchar); 131 | 132 | 133 | #endif 134 | -------------------------------------------------------------------------------- /noumenon/lua/src/lprefix.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lprefix.h,v 1.2.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Definitions for Lua code that must come before any other header file 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lprefix_h 8 | #define lprefix_h 9 | 10 | 11 | /* 12 | ** Allows POSIX/XSI stuff 13 | */ 14 | #if !defined(LUA_USE_C89) /* { */ 15 | 16 | #if !defined(_XOPEN_SOURCE) 17 | #define _XOPEN_SOURCE 600 18 | #elif _XOPEN_SOURCE == 0 19 | #undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ 20 | #endif 21 | 22 | /* 23 | ** Allows manipulation of large files in gcc and some other compilers 24 | */ 25 | #if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) 26 | #define _LARGEFILE_SOURCE 1 27 | #define _FILE_OFFSET_BITS 64 28 | #endif 29 | 30 | #endif /* } */ 31 | 32 | 33 | /* 34 | ** Windows stuff 35 | */ 36 | #if defined(_WIN32) /* { */ 37 | 38 | #if !defined(_CRT_SECURE_NO_WARNINGS) 39 | #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ 40 | #endif 41 | 42 | #endif /* } */ 43 | 44 | #endif 45 | 46 | -------------------------------------------------------------------------------- /noumenon/lua/src/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $ 3 | ** Global State 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstate_h 8 | #define lstate_h 9 | 10 | #include "lua.h" 11 | 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | #include "lzio.h" 15 | 16 | 17 | /* 18 | 19 | ** Some notes about garbage-collected objects: All objects in Lua must 20 | ** be kept somehow accessible until being freed, so all objects always 21 | ** belong to one (and only one) of these lists, using field 'next' of 22 | ** the 'CommonHeader' for the link: 23 | ** 24 | ** 'allgc': all objects not marked for finalization; 25 | ** 'finobj': all objects marked for finalization; 26 | ** 'tobefnz': all objects ready to be finalized; 27 | ** 'fixedgc': all objects that are not to be collected (currently 28 | ** only small strings, such as reserved words). 29 | ** 30 | ** Moreover, there is another set of lists that control gray objects. 31 | ** These lists are linked by fields 'gclist'. (All objects that 32 | ** can become gray have such a field. The field is not the same 33 | ** in all objects, but it always has this name.) Any gray object 34 | ** must belong to one of these lists, and all objects in these lists 35 | ** must be gray: 36 | ** 37 | ** 'gray': regular gray objects, still waiting to be visited. 38 | ** 'grayagain': objects that must be revisited at the atomic phase. 39 | ** That includes 40 | ** - black objects got in a write barrier; 41 | ** - all kinds of weak tables during propagation phase; 42 | ** - all threads. 43 | ** 'weak': tables with weak values to be cleared; 44 | ** 'ephemeron': ephemeron tables with white->white entries; 45 | ** 'allweak': tables with weak keys and/or weak values to be cleared. 46 | ** The last three lists are used only during the atomic phase. 47 | 48 | */ 49 | 50 | 51 | struct lua_longjmp; /* defined in ldo.c */ 52 | 53 | 54 | /* 55 | ** Atomic type (relative to signals) to better ensure that 'lua_sethook' 56 | ** is thread safe 57 | */ 58 | #if !defined(l_signalT) 59 | #include 60 | #define l_signalT sig_atomic_t 61 | #endif 62 | 63 | 64 | /* extra stack space to handle TM calls and some other extras */ 65 | #define EXTRA_STACK 5 66 | 67 | 68 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 69 | 70 | 71 | /* kinds of Garbage Collection */ 72 | #define KGC_NORMAL 0 73 | #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ 74 | 75 | 76 | typedef struct stringtable { 77 | TString **hash; 78 | int nuse; /* number of elements */ 79 | int size; 80 | } stringtable; 81 | 82 | 83 | /* 84 | ** Information about a call. 85 | ** When a thread yields, 'func' is adjusted to pretend that the 86 | ** top function has only the yielded values in its stack; in that 87 | ** case, the actual 'func' value is saved in field 'extra'. 88 | ** When a function calls another with a continuation, 'extra' keeps 89 | ** the function index so that, in case of errors, the continuation 90 | ** function can be called with the correct top. 91 | */ 92 | typedef struct CallInfo { 93 | StkId func; /* function index in the stack */ 94 | StkId top; /* top for this function */ 95 | struct CallInfo *previous, *next; /* dynamic call link */ 96 | union { 97 | struct { /* only for Lua functions */ 98 | StkId base; /* base for this function */ 99 | const Instruction *savedpc; 100 | } l; 101 | struct { /* only for C functions */ 102 | lua_KFunction k; /* continuation in case of yields */ 103 | ptrdiff_t old_errfunc; 104 | lua_KContext ctx; /* context info. in case of yields */ 105 | } c; 106 | } u; 107 | ptrdiff_t extra; 108 | short nresults; /* expected number of results from this function */ 109 | unsigned short callstatus; 110 | } CallInfo; 111 | 112 | 113 | /* 114 | ** Bits in CallInfo status 115 | */ 116 | #define CIST_OAH (1<<0) /* original value of 'allowhook' */ 117 | #define CIST_LUA (1<<1) /* call is running a Lua function */ 118 | #define CIST_HOOKED (1<<2) /* call is running a debug hook */ 119 | #define CIST_FRESH (1<<3) /* call is running on a fresh invocation 120 | of luaV_execute */ 121 | #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ 122 | #define CIST_TAIL (1<<5) /* call was tail called */ 123 | #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ 124 | #define CIST_LEQ (1<<7) /* using __lt for __le */ 125 | #define CIST_FIN (1<<8) /* call is running a finalizer */ 126 | 127 | #define isLua(ci) ((ci)->callstatus & CIST_LUA) 128 | 129 | /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ 130 | #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) 131 | #define getoah(st) ((st) & CIST_OAH) 132 | 133 | 134 | /* 135 | ** 'global state', shared by all threads of this state 136 | */ 137 | typedef struct global_State { 138 | lua_Alloc frealloc; /* function to reallocate memory */ 139 | void *ud; /* auxiliary data to 'frealloc' */ 140 | l_mem totalbytes; /* number of bytes currently allocated - GCdebt */ 141 | l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ 142 | lu_mem GCmemtrav; /* memory traversed by the GC */ 143 | lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ 144 | stringtable strt; /* hash table for strings */ 145 | TValue l_registry; 146 | unsigned int seed; /* randomized seed for hashes */ 147 | lu_byte currentwhite; 148 | lu_byte gcstate; /* state of garbage collector */ 149 | lu_byte gckind; /* kind of GC running */ 150 | lu_byte gcrunning; /* true if GC is running */ 151 | GCObject *allgc; /* list of all collectable objects */ 152 | GCObject **sweepgc; /* current position of sweep in list */ 153 | GCObject *finobj; /* list of collectable objects with finalizers */ 154 | GCObject *gray; /* list of gray objects */ 155 | GCObject *grayagain; /* list of objects to be traversed atomically */ 156 | GCObject *weak; /* list of tables with weak values */ 157 | GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ 158 | GCObject *allweak; /* list of all-weak tables */ 159 | GCObject *tobefnz; /* list of userdata to be GC */ 160 | GCObject *fixedgc; /* list of objects not to be collected */ 161 | struct lua_State *twups; /* list of threads with open upvalues */ 162 | unsigned int gcfinnum; /* number of finalizers to call in each GC step */ 163 | int gcpause; /* size of pause between successive GCs */ 164 | int gcstepmul; /* GC 'granularity' */ 165 | lua_CFunction panic; /* to be called in unprotected errors */ 166 | struct lua_State *mainthread; 167 | const lua_Number *version; /* pointer to version number */ 168 | TString *memerrmsg; /* memory-error message */ 169 | TString *tmname[TM_N]; /* array with tag-method names */ 170 | struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ 171 | TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ 172 | } global_State; 173 | 174 | 175 | /* 176 | ** 'per thread' state 177 | */ 178 | struct lua_State { 179 | CommonHeader; 180 | unsigned short nci; /* number of items in 'ci' list */ 181 | lu_byte status; 182 | StkId top; /* first free slot in the stack */ 183 | global_State *l_G; 184 | CallInfo *ci; /* call info for current function */ 185 | const Instruction *oldpc; /* last pc traced */ 186 | StkId stack_last; /* last free slot in the stack */ 187 | StkId stack; /* stack base */ 188 | UpVal *openupval; /* list of open upvalues in this stack */ 189 | GCObject *gclist; 190 | struct lua_State *twups; /* list of threads with open upvalues */ 191 | struct lua_longjmp *errorJmp; /* current error recover point */ 192 | CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ 193 | volatile lua_Hook hook; 194 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 195 | int stacksize; 196 | int basehookcount; 197 | int hookcount; 198 | unsigned short nny; /* number of non-yieldable calls in stack */ 199 | unsigned short nCcalls; /* number of nested C calls */ 200 | l_signalT hookmask; 201 | lu_byte allowhook; 202 | }; 203 | 204 | 205 | #define G(L) (L->l_G) 206 | 207 | 208 | /* 209 | ** Union of all collectable objects (only for conversions) 210 | */ 211 | union GCUnion { 212 | GCObject gc; /* common header */ 213 | struct TString ts; 214 | struct Udata u; 215 | union Closure cl; 216 | struct Table h; 217 | struct Proto p; 218 | struct lua_State th; /* thread */ 219 | }; 220 | 221 | 222 | #define cast_u(o) cast(union GCUnion *, (o)) 223 | 224 | /* macros to convert a GCObject into a specific value */ 225 | #define gco2ts(o) \ 226 | check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts)) 227 | #define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u)) 228 | #define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l)) 229 | #define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c)) 230 | #define gco2cl(o) \ 231 | check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl)) 232 | #define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h)) 233 | #define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p)) 234 | #define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th)) 235 | 236 | 237 | /* macro to convert a Lua object into a GCObject */ 238 | #define obj2gco(v) \ 239 | check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc))) 240 | 241 | 242 | /* actual number of total bytes allocated */ 243 | #define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt) 244 | 245 | LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); 246 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 247 | LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); 248 | LUAI_FUNC void luaE_freeCI (lua_State *L); 249 | LUAI_FUNC void luaE_shrinkCI (lua_State *L); 250 | 251 | 252 | #endif 253 | 254 | -------------------------------------------------------------------------------- /noumenon/lua/src/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 2.56.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** String table (keeps all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lstring_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lmem.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | #include "lstring.h" 23 | 24 | 25 | #define MEMERRMSG "not enough memory" 26 | 27 | 28 | /* 29 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to 30 | ** compute its hash 31 | */ 32 | #if !defined(LUAI_HASHLIMIT) 33 | #define LUAI_HASHLIMIT 5 34 | #endif 35 | 36 | 37 | /* 38 | ** equality for long strings 39 | */ 40 | int luaS_eqlngstr (TString *a, TString *b) { 41 | size_t len = a->u.lnglen; 42 | lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR); 43 | return (a == b) || /* same instance or... */ 44 | ((len == b->u.lnglen) && /* equal length and ... */ 45 | (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ 46 | } 47 | 48 | 49 | unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { 50 | unsigned int h = seed ^ cast(unsigned int, l); 51 | size_t step = (l >> LUAI_HASHLIMIT) + 1; 52 | for (; l >= step; l -= step) 53 | h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); 54 | return h; 55 | } 56 | 57 | 58 | unsigned int luaS_hashlongstr (TString *ts) { 59 | lua_assert(ts->tt == LUA_TLNGSTR); 60 | if (ts->extra == 0) { /* no hash? */ 61 | ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash); 62 | ts->extra = 1; /* now it has its hash */ 63 | } 64 | return ts->hash; 65 | } 66 | 67 | 68 | /* 69 | ** resizes the string table 70 | */ 71 | void luaS_resize (lua_State *L, int newsize) { 72 | int i; 73 | stringtable *tb = &G(L)->strt; 74 | if (newsize > tb->size) { /* grow table if needed */ 75 | luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); 76 | for (i = tb->size; i < newsize; i++) 77 | tb->hash[i] = NULL; 78 | } 79 | for (i = 0; i < tb->size; i++) { /* rehash */ 80 | TString *p = tb->hash[i]; 81 | tb->hash[i] = NULL; 82 | while (p) { /* for each node in the list */ 83 | TString *hnext = p->u.hnext; /* save next */ 84 | unsigned int h = lmod(p->hash, newsize); /* new position */ 85 | p->u.hnext = tb->hash[h]; /* chain it */ 86 | tb->hash[h] = p; 87 | p = hnext; 88 | } 89 | } 90 | if (newsize < tb->size) { /* shrink table if needed */ 91 | /* vanishing slice should be empty */ 92 | lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); 93 | luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); 94 | } 95 | tb->size = newsize; 96 | } 97 | 98 | 99 | /* 100 | ** Clear API string cache. (Entries cannot be empty, so fill them with 101 | ** a non-collectable string.) 102 | */ 103 | void luaS_clearcache (global_State *g) { 104 | int i, j; 105 | for (i = 0; i < STRCACHE_N; i++) 106 | for (j = 0; j < STRCACHE_M; j++) { 107 | if (iswhite(g->strcache[i][j])) /* will entry be collected? */ 108 | g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ 109 | } 110 | } 111 | 112 | 113 | /* 114 | ** Initialize the string table and the string cache 115 | */ 116 | void luaS_init (lua_State *L) { 117 | global_State *g = G(L); 118 | int i, j; 119 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ 120 | /* pre-create memory-error message */ 121 | g->memerrmsg = luaS_newliteral(L, MEMERRMSG); 122 | luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ 123 | for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */ 124 | for (j = 0; j < STRCACHE_M; j++) 125 | g->strcache[i][j] = g->memerrmsg; 126 | } 127 | 128 | 129 | 130 | /* 131 | ** creates a new string object 132 | */ 133 | static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) { 134 | TString *ts; 135 | GCObject *o; 136 | size_t totalsize; /* total size of TString object */ 137 | totalsize = sizelstring(l); 138 | o = luaC_newobj(L, tag, totalsize); 139 | ts = gco2ts(o); 140 | ts->hash = h; 141 | ts->extra = 0; 142 | getstr(ts)[l] = '\0'; /* ending 0 */ 143 | return ts; 144 | } 145 | 146 | 147 | TString *luaS_createlngstrobj (lua_State *L, size_t l) { 148 | TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed); 149 | ts->u.lnglen = l; 150 | return ts; 151 | } 152 | 153 | 154 | void luaS_remove (lua_State *L, TString *ts) { 155 | stringtable *tb = &G(L)->strt; 156 | TString **p = &tb->hash[lmod(ts->hash, tb->size)]; 157 | while (*p != ts) /* find previous element */ 158 | p = &(*p)->u.hnext; 159 | *p = (*p)->u.hnext; /* remove element from its list */ 160 | tb->nuse--; 161 | } 162 | 163 | 164 | /* 165 | ** checks whether short string exists and reuses it or creates a new one 166 | */ 167 | static TString *internshrstr (lua_State *L, const char *str, size_t l) { 168 | TString *ts; 169 | global_State *g = G(L); 170 | unsigned int h = luaS_hash(str, l, g->seed); 171 | TString **list = &g->strt.hash[lmod(h, g->strt.size)]; 172 | lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ 173 | for (ts = *list; ts != NULL; ts = ts->u.hnext) { 174 | if (l == ts->shrlen && 175 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { 176 | /* found! */ 177 | if (isdead(g, ts)) /* dead (but not collected yet)? */ 178 | changewhite(ts); /* resurrect it */ 179 | return ts; 180 | } 181 | } 182 | if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) { 183 | luaS_resize(L, g->strt.size * 2); 184 | list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ 185 | } 186 | ts = createstrobj(L, l, LUA_TSHRSTR, h); 187 | memcpy(getstr(ts), str, l * sizeof(char)); 188 | ts->shrlen = cast_byte(l); 189 | ts->u.hnext = *list; 190 | *list = ts; 191 | g->strt.nuse++; 192 | return ts; 193 | } 194 | 195 | 196 | /* 197 | ** new string (with explicit length) 198 | */ 199 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 200 | if (l <= LUAI_MAXSHORTLEN) /* short string? */ 201 | return internshrstr(L, str, l); 202 | else { 203 | TString *ts; 204 | if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char)) 205 | luaM_toobig(L); 206 | ts = luaS_createlngstrobj(L, l); 207 | memcpy(getstr(ts), str, l * sizeof(char)); 208 | return ts; 209 | } 210 | } 211 | 212 | 213 | /* 214 | ** Create or reuse a zero-terminated string, first checking in the 215 | ** cache (using the string address as a key). The cache can contain 216 | ** only zero-terminated strings, so it is safe to use 'strcmp' to 217 | ** check hits. 218 | */ 219 | TString *luaS_new (lua_State *L, const char *str) { 220 | unsigned int i = point2uint(str) % STRCACHE_N; /* hash */ 221 | int j; 222 | TString **p = G(L)->strcache[i]; 223 | for (j = 0; j < STRCACHE_M; j++) { 224 | if (strcmp(str, getstr(p[j])) == 0) /* hit? */ 225 | return p[j]; /* that is it */ 226 | } 227 | /* normal route */ 228 | for (j = STRCACHE_M - 1; j > 0; j--) 229 | p[j] = p[j - 1]; /* move out last element */ 230 | /* new element is first in the list */ 231 | p[0] = luaS_newlstr(L, str, strlen(str)); 232 | return p[0]; 233 | } 234 | 235 | 236 | Udata *luaS_newudata (lua_State *L, size_t s) { 237 | Udata *u; 238 | GCObject *o; 239 | if (s > MAX_SIZE - sizeof(Udata)) 240 | luaM_toobig(L); 241 | o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s)); 242 | u = gco2u(o); 243 | u->len = s; 244 | u->metatable = NULL; 245 | setuservalue(L, u, luaO_nilobject); 246 | return u; 247 | } 248 | 249 | -------------------------------------------------------------------------------- /noumenon/lua/src/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.61.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | #define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) 16 | 17 | #define sizeludata(l) (sizeof(union UUdata) + (l)) 18 | #define sizeudata(u) sizeludata((u)->len) 19 | 20 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 21 | (sizeof(s)/sizeof(char))-1)) 22 | 23 | 24 | /* 25 | ** test whether a string is a reserved word 26 | */ 27 | #define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0) 28 | 29 | 30 | /* 31 | ** equality for short strings, which are always internalized 32 | */ 33 | #define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b)) 34 | 35 | 36 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 37 | LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); 38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 39 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 40 | LUAI_FUNC void luaS_clearcache (global_State *g); 41 | LUAI_FUNC void luaS_init (lua_State *L); 42 | LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); 43 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s); 44 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 45 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 46 | LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); 47 | 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /noumenon/lua/src/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.23.1.2 2018/05/24 19:39:05 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gval(n) (&(n)->i_val) 15 | #define gnext(n) ((n)->i_key.nk.next) 16 | 17 | 18 | /* 'const' to avoid wrong writings that can mess up field 'next' */ 19 | #define gkey(n) cast(const TValue*, (&(n)->i_key.tvk)) 20 | 21 | /* 22 | ** writable version of 'gkey'; allows updates to individual fields, 23 | ** but not to the whole (which has incompatible type) 24 | */ 25 | #define wgkey(n) (&(n)->i_key.nk) 26 | 27 | #define invalidateTMcache(t) ((t)->flags = 0) 28 | 29 | 30 | /* true when 't' is using 'dummynode' as its hash part */ 31 | #define isdummy(t) ((t)->lastfree == NULL) 32 | 33 | 34 | /* allocated size for hash nodes */ 35 | #define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t)) 36 | 37 | 38 | /* returns the key, given the value of a table entry */ 39 | #define keyfromval(v) \ 40 | (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) 41 | 42 | 43 | LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); 44 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, 45 | TValue *value); 46 | LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key); 47 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 48 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 49 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); 50 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 51 | LUAI_FUNC Table *luaH_new (lua_State *L); 52 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, 53 | unsigned int nhsize); 54 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); 55 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 56 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 57 | LUAI_FUNC lua_Unsigned luaH_getn (Table *t); 58 | 59 | 60 | #if defined(LUA_DEBUG) 61 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 62 | LUAI_FUNC int luaH_isdummy (const Table *t); 63 | #endif 64 | 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /noumenon/lua/src/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.38.1.1 2017/04/19 17:39:34 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define ltm_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lobject.h" 20 | #include "lstate.h" 21 | #include "lstring.h" 22 | #include "ltable.h" 23 | #include "ltm.h" 24 | #include "lvm.h" 25 | 26 | 27 | static const char udatatypename[] = "userdata"; 28 | 29 | LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { 30 | "no value", 31 | "nil", "boolean", udatatypename, "number", 32 | "string", "table", "function", udatatypename, "thread", 33 | "proto" /* this last case is used for tests only */ 34 | }; 35 | 36 | 37 | void luaT_init (lua_State *L) { 38 | static const char *const luaT_eventname[] = { /* ORDER TM */ 39 | "__index", "__newindex", 40 | "__gc", "__mode", "__len", "__eq", 41 | "__add", "__sub", "__mul", "__mod", "__pow", 42 | "__div", "__idiv", 43 | "__band", "__bor", "__bxor", "__shl", "__shr", 44 | "__unm", "__bnot", "__lt", "__le", 45 | "__concat", "__call" 46 | }; 47 | int i; 48 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 50 | luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */ 51 | } 52 | } 53 | 54 | 55 | /* 56 | ** function to be used with macro "fasttm": optimized for absence of 57 | ** tag methods 58 | */ 59 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 60 | const TValue *tm = luaH_getshortstr(events, ename); 61 | lua_assert(event <= TM_EQ); 62 | if (ttisnil(tm)) { /* no tag method? */ 63 | events->flags |= cast_byte(1u<metatable; 75 | break; 76 | case LUA_TUSERDATA: 77 | mt = uvalue(o)->metatable; 78 | break; 79 | default: 80 | mt = G(L)->mt[ttnov(o)]; 81 | } 82 | return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject); 83 | } 84 | 85 | 86 | /* 87 | ** Return the name of the type of an object. For tables and userdata 88 | ** with metatable, use their '__name' metafield, if present. 89 | */ 90 | const char *luaT_objtypename (lua_State *L, const TValue *o) { 91 | Table *mt; 92 | if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || 93 | (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { 94 | const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name")); 95 | if (ttisstring(name)) /* is '__name' a string? */ 96 | return getstr(tsvalue(name)); /* use it as type name */ 97 | } 98 | return ttypename(ttnov(o)); /* else use standard type name */ 99 | } 100 | 101 | 102 | void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 103 | const TValue *p2, TValue *p3, int hasres) { 104 | ptrdiff_t result = savestack(L, p3); 105 | StkId func = L->top; 106 | setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ 107 | setobj2s(L, func + 1, p1); /* 1st argument */ 108 | setobj2s(L, func + 2, p2); /* 2nd argument */ 109 | L->top += 3; 110 | if (!hasres) /* no result? 'p3' is third argument */ 111 | setobj2s(L, L->top++, p3); /* 3rd argument */ 112 | /* metamethod may yield only when called from Lua code */ 113 | if (isLua(L->ci)) 114 | luaD_call(L, func, hasres); 115 | else 116 | luaD_callnoyield(L, func, hasres); 117 | if (hasres) { /* if has result, move it to its place */ 118 | p3 = restorestack(L, result); 119 | setobjs2s(L, p3, --L->top); 120 | } 121 | } 122 | 123 | 124 | int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 125 | StkId res, TMS event) { 126 | const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ 127 | if (ttisnil(tm)) 128 | tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ 129 | if (ttisnil(tm)) return 0; 130 | luaT_callTM(L, tm, p1, p2, res, 1); 131 | return 1; 132 | } 133 | 134 | 135 | void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 136 | StkId res, TMS event) { 137 | if (!luaT_callbinTM(L, p1, p2, res, event)) { 138 | switch (event) { 139 | case TM_CONCAT: 140 | luaG_concaterror(L, p1, p2); 141 | /* call never returns, but to avoid warnings: *//* FALLTHROUGH */ 142 | case TM_BAND: case TM_BOR: case TM_BXOR: 143 | case TM_SHL: case TM_SHR: case TM_BNOT: { 144 | lua_Number dummy; 145 | if (tonumber(p1, &dummy) && tonumber(p2, &dummy)) 146 | luaG_tointerror(L, p1, p2); 147 | else 148 | luaG_opinterror(L, p1, p2, "perform bitwise operation on"); 149 | } 150 | /* calls never return, but to avoid warnings: *//* FALLTHROUGH */ 151 | default: 152 | luaG_opinterror(L, p1, p2, "perform arithmetic on"); 153 | } 154 | } 155 | } 156 | 157 | 158 | int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, 159 | TMS event) { 160 | if (!luaT_callbinTM(L, p1, p2, L->top, event)) 161 | return -1; /* no metamethod */ 162 | else 163 | return !l_isfalse(L->top); 164 | } 165 | 166 | -------------------------------------------------------------------------------- /noumenon/lua/src/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.22.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" and "ORDER OP" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with fast access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_MOD, 29 | TM_POW, 30 | TM_DIV, 31 | TM_IDIV, 32 | TM_BAND, 33 | TM_BOR, 34 | TM_BXOR, 35 | TM_SHL, 36 | TM_SHR, 37 | TM_UNM, 38 | TM_BNOT, 39 | TM_LT, 40 | TM_LE, 41 | TM_CONCAT, 42 | TM_CALL, 43 | TM_N /* number of elements in the enum */ 44 | } TMS; 45 | 46 | 47 | 48 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 49 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 50 | 51 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 52 | 53 | #define ttypename(x) luaT_typenames_[(x) + 1] 54 | 55 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 56 | 57 | 58 | LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); 59 | 60 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 61 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 62 | TMS event); 63 | LUAI_FUNC void luaT_init (lua_State *L); 64 | 65 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 66 | const TValue *p2, TValue *p3, int hasres); 67 | LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 68 | StkId res, TMS event); 69 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 70 | StkId res, TMS event); 71 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 72 | const TValue *p2, TMS event); 73 | 74 | 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /noumenon/lua/src/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.45.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | /* version suffix for environment variable names */ 15 | #define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR 16 | 17 | 18 | LUAMOD_API int (luaopen_base) (lua_State *L); 19 | 20 | #define LUA_COLIBNAME "coroutine" 21 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 22 | 23 | #define LUA_TABLIBNAME "table" 24 | LUAMOD_API int (luaopen_table) (lua_State *L); 25 | 26 | #define LUA_IOLIBNAME "io" 27 | LUAMOD_API int (luaopen_io) (lua_State *L); 28 | 29 | #define LUA_OSLIBNAME "os" 30 | LUAMOD_API int (luaopen_os) (lua_State *L); 31 | 32 | #define LUA_STRLIBNAME "string" 33 | LUAMOD_API int (luaopen_string) (lua_State *L); 34 | 35 | #define LUA_UTF8LIBNAME "utf8" 36 | LUAMOD_API int (luaopen_utf8) (lua_State *L); 37 | 38 | #define LUA_BITLIBNAME "bit32" 39 | LUAMOD_API int (luaopen_bit32) (lua_State *L); 40 | 41 | #define LUA_MATHLIBNAME "math" 42 | LUAMOD_API int (luaopen_math) (lua_State *L); 43 | 44 | #define LUA_DBLIBNAME "debug" 45 | LUAMOD_API int (luaopen_debug) (lua_State *L); 46 | 47 | #define LUA_LOADLIBNAME "package" 48 | LUAMOD_API int (luaopen_package) (lua_State *L); 49 | 50 | 51 | /* open all previous libraries */ 52 | LUALIB_API void (luaL_openlibs) (lua_State *L); 53 | 54 | 55 | 56 | #if !defined(lua_assert) 57 | #define lua_assert(x) ((void)0) 58 | #endif 59 | 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /noumenon/lua/src/lundump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.c,v 2.44.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lundump_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lfunc.h" 20 | #include "lmem.h" 21 | #include "lobject.h" 22 | #include "lstring.h" 23 | #include "lundump.h" 24 | #include "lzio.h" 25 | 26 | 27 | #if !defined(luai_verifycode) 28 | #define luai_verifycode(L,b,f) /* empty */ 29 | #endif 30 | 31 | 32 | typedef struct { 33 | lua_State *L; 34 | ZIO *Z; 35 | const char *name; 36 | } LoadState; 37 | 38 | 39 | static l_noret error(LoadState *S, const char *why) { 40 | luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why); 41 | luaD_throw(S->L, LUA_ERRSYNTAX); 42 | } 43 | 44 | 45 | /* 46 | ** All high-level loads go through LoadVector; you can change it to 47 | ** adapt to the endianness of the input 48 | */ 49 | #define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0])) 50 | 51 | static void LoadBlock (LoadState *S, void *b, size_t size) { 52 | if (luaZ_read(S->Z, b, size) != 0) 53 | error(S, "truncated"); 54 | } 55 | 56 | 57 | #define LoadVar(S,x) LoadVector(S,&x,1) 58 | 59 | 60 | static lu_byte LoadByte (LoadState *S) { 61 | lu_byte x; 62 | LoadVar(S, x); 63 | return x; 64 | } 65 | 66 | 67 | static int LoadInt (LoadState *S) { 68 | int x; 69 | LoadVar(S, x); 70 | return x; 71 | } 72 | 73 | 74 | static lua_Number LoadNumber (LoadState *S) { 75 | lua_Number x; 76 | LoadVar(S, x); 77 | return x; 78 | } 79 | 80 | 81 | static lua_Integer LoadInteger (LoadState *S) { 82 | lua_Integer x; 83 | LoadVar(S, x); 84 | return x; 85 | } 86 | 87 | 88 | static TString *LoadString (LoadState *S, Proto *p) { 89 | lua_State *L = S->L; 90 | size_t size = LoadByte(S); 91 | TString *ts; 92 | if (size == 0xFF) 93 | LoadVar(S, size); 94 | if (size == 0) 95 | return NULL; 96 | else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ 97 | char buff[LUAI_MAXSHORTLEN]; 98 | LoadVector(S, buff, size); 99 | ts = luaS_newlstr(L, buff, size); 100 | } 101 | else { /* long string */ 102 | ts = luaS_createlngstrobj(L, size); 103 | setsvalue2s(L, L->top, ts); /* anchor it ('loadVector' can GC) */ 104 | luaD_inctop(L); 105 | LoadVector(S, getstr(ts), size); /* load directly in final place */ 106 | L->top--; /* pop string */ 107 | } 108 | luaC_objbarrier(L, p, ts); 109 | return ts; 110 | } 111 | 112 | 113 | static void LoadCode (LoadState *S, Proto *f) { 114 | int n = LoadInt(S); 115 | f->code = luaM_newvector(S->L, n, Instruction); 116 | f->sizecode = n; 117 | LoadVector(S, f->code, n); 118 | } 119 | 120 | 121 | static void LoadFunction(LoadState *S, Proto *f, TString *psource); 122 | 123 | 124 | static void LoadConstants (LoadState *S, Proto *f) { 125 | int i; 126 | int n = LoadInt(S); 127 | f->k = luaM_newvector(S->L, n, TValue); 128 | f->sizek = n; 129 | for (i = 0; i < n; i++) 130 | setnilvalue(&f->k[i]); 131 | for (i = 0; i < n; i++) { 132 | TValue *o = &f->k[i]; 133 | int t = LoadByte(S); 134 | switch (t) { 135 | case LUA_TNIL: 136 | setnilvalue(o); 137 | break; 138 | case LUA_TBOOLEAN: 139 | setbvalue(o, LoadByte(S)); 140 | break; 141 | case LUA_TNUMFLT: 142 | setfltvalue(o, LoadNumber(S)); 143 | break; 144 | case LUA_TNUMINT: 145 | setivalue(o, LoadInteger(S)); 146 | break; 147 | case LUA_TSHRSTR: 148 | case LUA_TLNGSTR: 149 | setsvalue2n(S->L, o, LoadString(S, f)); 150 | break; 151 | default: 152 | lua_assert(0); 153 | } 154 | } 155 | } 156 | 157 | 158 | static void LoadProtos (LoadState *S, Proto *f) { 159 | int i; 160 | int n = LoadInt(S); 161 | f->p = luaM_newvector(S->L, n, Proto *); 162 | f->sizep = n; 163 | for (i = 0; i < n; i++) 164 | f->p[i] = NULL; 165 | for (i = 0; i < n; i++) { 166 | f->p[i] = luaF_newproto(S->L); 167 | luaC_objbarrier(S->L, f, f->p[i]); 168 | LoadFunction(S, f->p[i], f->source); 169 | } 170 | } 171 | 172 | 173 | static void LoadUpvalues (LoadState *S, Proto *f) { 174 | int i, n; 175 | n = LoadInt(S); 176 | f->upvalues = luaM_newvector(S->L, n, Upvaldesc); 177 | f->sizeupvalues = n; 178 | for (i = 0; i < n; i++) 179 | f->upvalues[i].name = NULL; 180 | for (i = 0; i < n; i++) { 181 | f->upvalues[i].instack = LoadByte(S); 182 | f->upvalues[i].idx = LoadByte(S); 183 | } 184 | } 185 | 186 | 187 | static void LoadDebug (LoadState *S, Proto *f) { 188 | int i, n; 189 | n = LoadInt(S); 190 | f->lineinfo = luaM_newvector(S->L, n, int); 191 | f->sizelineinfo = n; 192 | LoadVector(S, f->lineinfo, n); 193 | n = LoadInt(S); 194 | f->locvars = luaM_newvector(S->L, n, LocVar); 195 | f->sizelocvars = n; 196 | for (i = 0; i < n; i++) 197 | f->locvars[i].varname = NULL; 198 | for (i = 0; i < n; i++) { 199 | f->locvars[i].varname = LoadString(S, f); 200 | f->locvars[i].startpc = LoadInt(S); 201 | f->locvars[i].endpc = LoadInt(S); 202 | } 203 | n = LoadInt(S); 204 | for (i = 0; i < n; i++) 205 | f->upvalues[i].name = LoadString(S, f); 206 | } 207 | 208 | 209 | static void LoadFunction (LoadState *S, Proto *f, TString *psource) { 210 | f->source = LoadString(S, f); 211 | if (f->source == NULL) /* no source in dump? */ 212 | f->source = psource; /* reuse parent's source */ 213 | f->linedefined = LoadInt(S); 214 | f->lastlinedefined = LoadInt(S); 215 | f->numparams = LoadByte(S); 216 | f->is_vararg = LoadByte(S); 217 | f->maxstacksize = LoadByte(S); 218 | LoadCode(S, f); 219 | LoadConstants(S, f); 220 | LoadUpvalues(S, f); 221 | LoadProtos(S, f); 222 | LoadDebug(S, f); 223 | } 224 | 225 | 226 | static void checkliteral (LoadState *S, const char *s, const char *msg) { 227 | char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */ 228 | size_t len = strlen(s); 229 | LoadVector(S, buff, len); 230 | if (memcmp(s, buff, len) != 0) 231 | error(S, msg); 232 | } 233 | 234 | 235 | static void fchecksize (LoadState *S, size_t size, const char *tname) { 236 | if (LoadByte(S) != size) 237 | error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname)); 238 | } 239 | 240 | 241 | #define checksize(S,t) fchecksize(S,sizeof(t),#t) 242 | 243 | static void checkHeader (LoadState *S) { 244 | checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */ 245 | if (LoadByte(S) != LUAC_VERSION) 246 | error(S, "version mismatch in"); 247 | if (LoadByte(S) != LUAC_FORMAT) 248 | error(S, "format mismatch in"); 249 | checkliteral(S, LUAC_DATA, "corrupted"); 250 | checksize(S, int); 251 | checksize(S, size_t); 252 | checksize(S, Instruction); 253 | checksize(S, lua_Integer); 254 | checksize(S, lua_Number); 255 | if (LoadInteger(S) != LUAC_INT) 256 | error(S, "endianness mismatch in"); 257 | if (LoadNumber(S) != LUAC_NUM) 258 | error(S, "float format mismatch in"); 259 | } 260 | 261 | 262 | /* 263 | ** load precompiled chunk 264 | */ 265 | LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { 266 | LoadState S; 267 | LClosure *cl; 268 | if (*name == '@' || *name == '=') 269 | S.name = name + 1; 270 | else if (*name == LUA_SIGNATURE[0]) 271 | S.name = "binary string"; 272 | else 273 | S.name = name; 274 | S.L = L; 275 | S.Z = Z; 276 | checkHeader(&S); 277 | cl = luaF_newLclosure(L, LoadByte(&S)); 278 | setclLvalue(L, L->top, cl); 279 | luaD_inctop(L); 280 | cl->p = luaF_newproto(L); 281 | luaC_objbarrier(L, cl, cl->p); 282 | LoadFunction(&S, cl->p, NULL); 283 | lua_assert(cl->nupvalues == cl->p->sizeupvalues); 284 | luai_verifycode(L, buff, cl->p); 285 | return cl; 286 | } 287 | 288 | -------------------------------------------------------------------------------- /noumenon/lua/src/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.45.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* data to catch conversion errors */ 16 | #define LUAC_DATA "\x19\x93\r\n\x1a\n" 17 | 18 | #define LUAC_INT 0x5678 19 | #define LUAC_NUM cast_num(370.5) 20 | 21 | #define MYINT(s) (s[0]-'0') 22 | #define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) 23 | #define LUAC_FORMAT 0 /* this is the official format */ 24 | 25 | /* load one chunk; from lundump.c */ 26 | LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name); 27 | 28 | /* dump one chunk; from ldump.c */ 29 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, 30 | void* data, int strip); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /noumenon/lua/src/lutf8lib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lutf8lib.c,v 1.16.1.1 2017/04/19 17:29:57 roberto Exp $ 3 | ** Standard library for UTF-8 manipulation 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lutf8lib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include "lua.h" 19 | 20 | #include "lauxlib.h" 21 | #include "lualib.h" 22 | 23 | #define MAXUNICODE 0x10FFFF 24 | 25 | #define iscont(p) ((*(p) & 0xC0) == 0x80) 26 | 27 | 28 | /* from strlib */ 29 | /* translate a relative string position: negative means back from end */ 30 | static lua_Integer u_posrelat (lua_Integer pos, size_t len) { 31 | if (pos >= 0) return pos; 32 | else if (0u - (size_t)pos > len) return 0; 33 | else return (lua_Integer)len + pos + 1; 34 | } 35 | 36 | 37 | /* 38 | ** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid. 39 | */ 40 | static const char *utf8_decode (const char *o, int *val) { 41 | static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF}; 42 | const unsigned char *s = (const unsigned char *)o; 43 | unsigned int c = s[0]; 44 | unsigned int res = 0; /* final result */ 45 | if (c < 0x80) /* ascii? */ 46 | res = c; 47 | else { 48 | int count = 0; /* to count number of continuation bytes */ 49 | while (c & 0x40) { /* still have continuation bytes? */ 50 | int cc = s[++count]; /* read next byte */ 51 | if ((cc & 0xC0) != 0x80) /* not a continuation byte? */ 52 | return NULL; /* invalid byte sequence */ 53 | res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ 54 | c <<= 1; /* to test next bit */ 55 | } 56 | res |= ((c & 0x7F) << (count * 5)); /* add first byte */ 57 | if (count > 3 || res > MAXUNICODE || res <= limits[count]) 58 | return NULL; /* invalid byte sequence */ 59 | s += count; /* skip continuation bytes read */ 60 | } 61 | if (val) *val = res; 62 | return (const char *)s + 1; /* +1 to include first byte */ 63 | } 64 | 65 | 66 | /* 67 | ** utf8len(s [, i [, j]]) --> number of characters that start in the 68 | ** range [i,j], or nil + current position if 's' is not well formed in 69 | ** that interval 70 | */ 71 | static int utflen (lua_State *L) { 72 | int n = 0; 73 | size_t len; 74 | const char *s = luaL_checklstring(L, 1, &len); 75 | lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); 76 | lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); 77 | luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, 78 | "initial position out of string"); 79 | luaL_argcheck(L, --posj < (lua_Integer)len, 3, 80 | "final position out of string"); 81 | while (posi <= posj) { 82 | const char *s1 = utf8_decode(s + posi, NULL); 83 | if (s1 == NULL) { /* conversion error? */ 84 | lua_pushnil(L); /* return nil ... */ 85 | lua_pushinteger(L, posi + 1); /* ... and current position */ 86 | return 2; 87 | } 88 | posi = s1 - s; 89 | n++; 90 | } 91 | lua_pushinteger(L, n); 92 | return 1; 93 | } 94 | 95 | 96 | /* 97 | ** codepoint(s, [i, [j]]) -> returns codepoints for all characters 98 | ** that start in the range [i,j] 99 | */ 100 | static int codepoint (lua_State *L) { 101 | size_t len; 102 | const char *s = luaL_checklstring(L, 1, &len); 103 | lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); 104 | lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len); 105 | int n; 106 | const char *se; 107 | luaL_argcheck(L, posi >= 1, 2, "out of range"); 108 | luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range"); 109 | if (posi > pose) return 0; /* empty interval; return no values */ 110 | if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */ 111 | return luaL_error(L, "string slice too long"); 112 | n = (int)(pose - posi) + 1; 113 | luaL_checkstack(L, n, "string slice too long"); 114 | n = 0; 115 | se = s + pose; 116 | for (s += posi - 1; s < se;) { 117 | int code; 118 | s = utf8_decode(s, &code); 119 | if (s == NULL) 120 | return luaL_error(L, "invalid UTF-8 code"); 121 | lua_pushinteger(L, code); 122 | n++; 123 | } 124 | return n; 125 | } 126 | 127 | 128 | static void pushutfchar (lua_State *L, int arg) { 129 | lua_Integer code = luaL_checkinteger(L, arg); 130 | luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, "value out of range"); 131 | lua_pushfstring(L, "%U", (long)code); 132 | } 133 | 134 | 135 | /* 136 | ** utfchar(n1, n2, ...) -> char(n1)..char(n2)... 137 | */ 138 | static int utfchar (lua_State *L) { 139 | int n = lua_gettop(L); /* number of arguments */ 140 | if (n == 1) /* optimize common case of single char */ 141 | pushutfchar(L, 1); 142 | else { 143 | int i; 144 | luaL_Buffer b; 145 | luaL_buffinit(L, &b); 146 | for (i = 1; i <= n; i++) { 147 | pushutfchar(L, i); 148 | luaL_addvalue(&b); 149 | } 150 | luaL_pushresult(&b); 151 | } 152 | return 1; 153 | } 154 | 155 | 156 | /* 157 | ** offset(s, n, [i]) -> index where n-th character counting from 158 | ** position 'i' starts; 0 means character at 'i'. 159 | */ 160 | static int byteoffset (lua_State *L) { 161 | size_t len; 162 | const char *s = luaL_checklstring(L, 1, &len); 163 | lua_Integer n = luaL_checkinteger(L, 2); 164 | lua_Integer posi = (n >= 0) ? 1 : len + 1; 165 | posi = u_posrelat(luaL_optinteger(L, 3, posi), len); 166 | luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, 167 | "position out of range"); 168 | if (n == 0) { 169 | /* find beginning of current byte sequence */ 170 | while (posi > 0 && iscont(s + posi)) posi--; 171 | } 172 | else { 173 | if (iscont(s + posi)) 174 | return luaL_error(L, "initial position is a continuation byte"); 175 | if (n < 0) { 176 | while (n < 0 && posi > 0) { /* move back */ 177 | do { /* find beginning of previous character */ 178 | posi--; 179 | } while (posi > 0 && iscont(s + posi)); 180 | n++; 181 | } 182 | } 183 | else { 184 | n--; /* do not move for 1st character */ 185 | while (n > 0 && posi < (lua_Integer)len) { 186 | do { /* find beginning of next character */ 187 | posi++; 188 | } while (iscont(s + posi)); /* (cannot pass final '\0') */ 189 | n--; 190 | } 191 | } 192 | } 193 | if (n == 0) /* did it find given character? */ 194 | lua_pushinteger(L, posi + 1); 195 | else /* no such character */ 196 | lua_pushnil(L); 197 | return 1; 198 | } 199 | 200 | 201 | static int iter_aux (lua_State *L) { 202 | size_t len; 203 | const char *s = luaL_checklstring(L, 1, &len); 204 | lua_Integer n = lua_tointeger(L, 2) - 1; 205 | if (n < 0) /* first iteration? */ 206 | n = 0; /* start from here */ 207 | else if (n < (lua_Integer)len) { 208 | n++; /* skip current byte */ 209 | while (iscont(s + n)) n++; /* and its continuations */ 210 | } 211 | if (n >= (lua_Integer)len) 212 | return 0; /* no more codepoints */ 213 | else { 214 | int code; 215 | const char *next = utf8_decode(s + n, &code); 216 | if (next == NULL || iscont(next)) 217 | return luaL_error(L, "invalid UTF-8 code"); 218 | lua_pushinteger(L, n + 1); 219 | lua_pushinteger(L, code); 220 | return 2; 221 | } 222 | } 223 | 224 | 225 | static int iter_codes (lua_State *L) { 226 | luaL_checkstring(L, 1); 227 | lua_pushcfunction(L, iter_aux); 228 | lua_pushvalue(L, 1); 229 | lua_pushinteger(L, 0); 230 | return 3; 231 | } 232 | 233 | 234 | /* pattern to match a single UTF-8 character */ 235 | #define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*" 236 | 237 | 238 | static const luaL_Reg funcs[] = { 239 | {"offset", byteoffset}, 240 | {"codepoint", codepoint}, 241 | {"char", utfchar}, 242 | {"len", utflen}, 243 | {"codes", iter_codes}, 244 | /* placeholders */ 245 | {"charpattern", NULL}, 246 | {NULL, NULL} 247 | }; 248 | 249 | 250 | LUAMOD_API int luaopen_utf8 (lua_State *L) { 251 | luaL_newlib(L, funcs); 252 | lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1); 253 | lua_setfield(L, -2, "charpattern"); 254 | return 1; 255 | } 256 | 257 | -------------------------------------------------------------------------------- /noumenon/lua/src/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.41.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #if !defined(LUA_NOCVTN2S) 17 | #define cvt2str(o) ttisnumber(o) 18 | #else 19 | #define cvt2str(o) 0 /* no conversion from numbers to strings */ 20 | #endif 21 | 22 | 23 | #if !defined(LUA_NOCVTS2N) 24 | #define cvt2num(o) ttisstring(o) 25 | #else 26 | #define cvt2num(o) 0 /* no conversion from strings to numbers */ 27 | #endif 28 | 29 | 30 | /* 31 | ** You can define LUA_FLOORN2I if you want to convert floats to integers 32 | ** by flooring them (instead of raising an error if they are not 33 | ** integral values) 34 | */ 35 | #if !defined(LUA_FLOORN2I) 36 | #define LUA_FLOORN2I 0 37 | #endif 38 | 39 | 40 | #define tonumber(o,n) \ 41 | (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) 42 | 43 | #define tointeger(o,i) \ 44 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) 45 | 46 | #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) 47 | 48 | #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) 49 | 50 | 51 | /* 52 | ** fast track for 'gettable': if 't' is a table and 't[k]' is not nil, 53 | ** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise, 54 | ** return 0 (meaning it will have to check metamethod) with 'slot' 55 | ** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise). 56 | ** 'f' is the raw get function to use. 57 | */ 58 | #define luaV_fastget(L,t,k,slot,f) \ 59 | (!ttistable(t) \ 60 | ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ 61 | : (slot = f(hvalue(t), k), /* else, do raw access */ \ 62 | !ttisnil(slot))) /* result not nil? */ 63 | 64 | /* 65 | ** standard implementation for 'gettable' 66 | */ 67 | #define luaV_gettable(L,t,k,v) { const TValue *slot; \ 68 | if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \ 69 | else luaV_finishget(L,t,k,v,slot); } 70 | 71 | 72 | /* 73 | ** Fast track for set table. If 't' is a table and 't[k]' is not nil, 74 | ** call GC barrier, do a raw 't[k]=v', and return true; otherwise, 75 | ** return false with 'slot' equal to NULL (if 't' is not a table) or 76 | ** 'nil'. (This is needed by 'luaV_finishget'.) Note that, if the macro 77 | ** returns true, there is no need to 'invalidateTMcache', because the 78 | ** call is not creating a new entry. 79 | */ 80 | #define luaV_fastset(L,t,k,slot,f,v) \ 81 | (!ttistable(t) \ 82 | ? (slot = NULL, 0) \ 83 | : (slot = f(hvalue(t), k), \ 84 | ttisnil(slot) ? 0 \ 85 | : (luaC_barrierback(L, hvalue(t), v), \ 86 | setobj2t(L, cast(TValue *,slot), v), \ 87 | 1))) 88 | 89 | 90 | #define luaV_settable(L,t,k,v) { const TValue *slot; \ 91 | if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \ 92 | luaV_finishset(L,t,k,v,slot); } 93 | 94 | 95 | 96 | LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); 97 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 98 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 99 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); 100 | LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); 101 | LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, 102 | StkId val, const TValue *slot); 103 | LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, 104 | StkId val, const TValue *slot); 105 | LUAI_FUNC void luaV_finishOp (lua_State *L); 106 | LUAI_FUNC void luaV_execute (lua_State *L); 107 | LUAI_FUNC void luaV_concat (lua_State *L, int total); 108 | LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y); 109 | LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); 110 | LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); 111 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 112 | 113 | #endif 114 | -------------------------------------------------------------------------------- /noumenon/lua/src/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.37.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lzio_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "llimits.h" 18 | #include "lmem.h" 19 | #include "lstate.h" 20 | #include "lzio.h" 21 | 22 | 23 | int luaZ_fill (ZIO *z) { 24 | size_t size; 25 | lua_State *L = z->L; 26 | const char *buff; 27 | lua_unlock(L); 28 | buff = z->reader(L, z->data, &size); 29 | lua_lock(L); 30 | if (buff == NULL || size == 0) 31 | return EOZ; 32 | z->n = size - 1; /* discount char being returned */ 33 | z->p = buff; 34 | return cast_uchar(*(z->p++)); 35 | } 36 | 37 | 38 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 39 | z->L = L; 40 | z->reader = reader; 41 | z->data = data; 42 | z->n = 0; 43 | z->p = NULL; 44 | } 45 | 46 | 47 | /* --------------------------------------------------------------- read --- */ 48 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 49 | while (n) { 50 | size_t m; 51 | if (z->n == 0) { /* no bytes in buffer? */ 52 | if (luaZ_fill(z) == EOZ) /* try to read more */ 53 | return n; /* no more input; return number of missing bytes */ 54 | else { 55 | z->n++; /* luaZ_fill consumed first byte; put it back */ 56 | z->p--; 57 | } 58 | } 59 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 60 | memcpy(b, z->p, m); 61 | z->n -= m; 62 | z->p += m; 63 | b = (char *)b + m; 64 | n -= m; 65 | } 66 | return 0; 67 | } 68 | 69 | -------------------------------------------------------------------------------- /noumenon/lua/src/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.31.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer { 24 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 41 | (buff)->buffsize, size), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ 50 | 51 | 52 | 53 | /* --------- Private Part ------------------ */ 54 | 55 | struct Zio { 56 | size_t n; /* bytes still unread */ 57 | const char *p; /* current position in buffer */ 58 | lua_Reader reader; /* reader function */ 59 | void *data; /* additional data */ 60 | lua_State *L; /* Lua state (for reader) */ 61 | }; 62 | 63 | 64 | LUAI_FUNC int luaZ_fill (ZIO *z); 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /noumenon/lua/test_lua_wrapper.cpp: -------------------------------------------------------------------------------- 1 | // 东方盛夏 2022 2 | // https://www.zhihu.com/people/da-xia-tian-60 3 | #include "lua_machine.h" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | 13 | const std::string g_luaScript = R"( 14 | stringValue = nil 15 | intValue = nil 16 | realValue = nil 17 | 18 | function LoadMap(map) 19 | text = '{' 20 | for k, v in pairs(map) do 21 | text = text .. k .. ' : ' .. v .. ', ' 22 | end 23 | text = string.sub(text, 1, #text - 2) 24 | text = text .. '}' 25 | return text 26 | end 27 | 28 | function LoadList(list) 29 | text = '{' 30 | for k, v in ipairs(list) do 31 | text = text .. v .. ', ' 32 | end 33 | text = string.sub(text, 1, #text - 2) 34 | text = text .. '}' 35 | return text 36 | end 37 | 38 | function Norm(x, y) 39 | return math.sqrt(x ^ 2 + y ^ 2) 40 | end 41 | 42 | function Void() 43 | text = 'hello lua' 44 | integer = 1357 45 | real = 3.1415926 46 | end 47 | 48 | function Chinese(text) 49 | return "回首向来萧瑟处 " .. text 50 | end 51 | )"; 52 | 53 | int main() 54 | { 55 | setlocale(LC_CTYPE, ""); // 参数为空串时,会自动获取系统的地域信息 56 | // std::wcout.imbue(std::locale("")); // 用这种 C++ 库函数设置后,仍然有乱码,原因未知 57 | 58 | std::wstringstream wss; 59 | std::string text; 60 | LuaMachine lua; 61 | if (!lua.LoadScript(g_luaScript)) { 62 | wss << "[lua compile failed]\n" << UTF8ToWide(lua.errorInfo_); 63 | std::wcout << wss.str(); 64 | return 0; 65 | } 66 | 67 | std::map map; 68 | map.insert({"hello", "world"}); 69 | map.insert({"great", "summer"}); 70 | lua.Call("LoadMap", 1, map); 71 | lua.Pop(text); 72 | wss << "LoadMap: " << UTF8ToWide(text) << std::endl; 73 | 74 | std::vector list {3.14159, 2.71828, 1.414}; 75 | lua.Call("LoadList", 1, list); 76 | lua.Pop(text); 77 | wss << "LoadList: " << UTF8ToWide(text) << std::endl; 78 | 79 | lua.Call("Norm", 1, 3, 4); 80 | int result; 81 | lua.Pop(result); 82 | wss << "Norm: " << result << std::endl; 83 | 84 | lua.Call("Void"); 85 | int integer; 86 | double real; 87 | lua.Get("integer", integer); 88 | lua.Get("real", real); 89 | lua.Get("text", text); 90 | wss << "Void: " << integer << ' ' << real << ' ' << UTF8ToWide(text) << std::endl; 91 | 92 | // 第二个参数等价于 WideToUTF8(_T("也无风雨也无晴")),这里默认转成的 std:string 也是 UTF-8 编码 93 | lua.Call("Chinese", 1, "也无风雨也无晴"); 94 | lua.Pop(text); 95 | wss << "Chinese: " << UTF8ToWide(text) << std::endl; 96 | 97 | std::wcout << wss.str(); 98 | getchar(); 99 | return 0; 100 | } 101 | -------------------------------------------------------------------------------- /noumenon/ui/BUILD.gn: -------------------------------------------------------------------------------- 1 | import("//config.gni") 2 | 3 | config("windows_wrapper_config") { 4 | include_dirs = [ "./" ] 5 | } 6 | 7 | source_set("windows_wrapper") { 8 | public_configs = [ ":windows_wrapper_config" ] 9 | libs = [ 10 | "user32.lib", 11 | "gdi32.lib", 12 | ] 13 | sources = [ "win.cpp" ] 14 | } 15 | 16 | executable("test_windows_wrapper") { 17 | sources = [ "test_windows_wrapper.cpp" ] 18 | public_deps = [ ":windows_wrapper" ] 19 | } 20 | -------------------------------------------------------------------------------- /noumenon/ui/test_windows_wrapper.cpp: -------------------------------------------------------------------------------- 1 | #include "win.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | void OnPaint(HWND handle, WPARAM wParam, LPARAM lParam) 9 | { 10 | PAINTSTRUCT ps; 11 | HDC hdc = BeginPaint(handle, &ps); 12 | HBRUSH brush = CreateSolidBrush(0x22bbff); 13 | RECT rect {0, 0, 200, 200}; 14 | FillRect(hdc, &rect, brush); 15 | EndPaint(handle, &ps); 16 | } 17 | 18 | int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPreInst, LPSTR lpCmd, int nShowMode) 19 | { 20 | Window::Initialise(hInst); 21 | auto window = std::make_shared(); 22 | 23 | window->handlerMap_[WM_PAINT] = OnPaint; 24 | window->handlerMap_[WM_KEYDOWN] = [&window](HWND handle, WPARAM wParam, LPARAM lParam) { 25 | if (wParam == VK_LEFT) { // 按方向左键让窗口销毁并重建 26 | std::wstringstream className; 27 | className << "NewWindowClass" << rand(); 28 | WNDCLASSEX winClass {0}; 29 | winClass.cbSize = sizeof(WNDCLASSEX); 30 | winClass.lpfnWndProc = Window::OnMessage; 31 | winClass.hInstance = window->instance_; 32 | winClass.lpszClassName = className.str().c_str(); 33 | winClass.style = CS_HREDRAW | CS_VREDRAW; 34 | winClass.hbrBackground = CreateSolidBrush((COLORREF)rand()); 35 | RegisterClassEx(&winClass); 36 | 37 | std::wstringstream windowName; 38 | windowName << _T("旧貌换新颜") << rand(); 39 | HWND hwnd = CreateWindow( 40 | className.str().c_str(), 41 | windowName.str().c_str(), 42 | WS_OVERLAPPEDWINDOW, 43 | CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 44 | NULL, NULL, 45 | window->instance_, NULL 46 | ); 47 | auto win = window->windowMap_[handle]; 48 | RECT rect = win->GetRect(); 49 | win->CreateFromHandle(hwnd); 50 | win->SetPosition(rect.left, rect.top, 51 | rect.right - rect.left, rect.bottom - rect.top); 52 | } else if (wParam == VK_RIGHT) { // 按方向右键新建窗口 53 | std::wstringstream windowName; 54 | windowName << _T("桃李满天下") << rand(); 55 | auto win = std::make_shared(); 56 | win->title_ = windowName.str(); 57 | win->handlerMap_ = window->handlerMap_; 58 | win->Create(); 59 | win->Show(SW_SHOWNORMAL); 60 | } else if (wParam == VK_ESCAPE) { // 按 ESC 键结束程序 61 | PostQuitMessage(0); 62 | } 63 | }; 64 | window->handlerMap_[WM_DESTROY] = VoidHandler; 65 | 66 | window->Create(); 67 | window->Show(SW_SHOWNORMAL); 68 | 69 | MSG message; 70 | while (GetMessage(&message, NULL, 0, 0)) { 71 | TranslateMessage(&message); 72 | DispatchMessage(&message); 73 | } 74 | return message.wParam; 75 | } 76 | -------------------------------------------------------------------------------- /noumenon/ui/win.cpp: -------------------------------------------------------------------------------- 1 | // 东方盛夏 2022 2 | // https://www.zhihu.com/people/da-xia-tian-60 3 | #include "win.h" 4 | 5 | HINSTANCE Window::instance_ {nullptr}; 6 | std::map> Window::windowMap_; 7 | std::wstring Window::defaultWindowClassName_ {_T("GamePainterWindowClass")}; 8 | void VoidHandler(HWND handle, WPARAM wParam, LPARAM lParam) {} 9 | 10 | bool Window::Initialise(HINSTANCE instance) 11 | { 12 | instance_ = instance; 13 | WNDCLASSEX winClass {0}; // 结构体成员清零[必要] 14 | winClass.cbSize = sizeof(WNDCLASSEX); // 指明结构体尺寸[WNDCLASSEX 必要,WNDCLASS 不必要] 15 | winClass.lpfnWndProc = Window::OnMessage; 16 | winClass.hInstance = instance; 17 | winClass.lpszClassName = defaultWindowClassName_.c_str(); 18 | winClass.style = CS_HREDRAW | CS_VREDRAW; // 自动重绘背景 19 | winClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); // CreateSolidBrush((COLORREF)rand()); 20 | // winClass.cbClsExtra = 0; // 窗口类的附加内存 21 | // winClass.cbWndExtra = 0; // 窗口的附加内存 22 | // winClass.hIcon = LoadIcon(instance_, MAKEINTRESOURCE(IDI_APPLICATION)); 23 | // winClass.hIconSm = LoadIcon(winClass.hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); 24 | // winClass.hCursor = LoadCursor(NULL, IDC_ARROW); 25 | // winClass.lpszMenuName = menuName_.c_str(); 26 | if(!RegisterClassEx(&winClass)) { 27 | return false; 28 | } 29 | return true; 30 | } 31 | 32 | Window::Window() 33 | { 34 | // 注册占位消息,框架需要对它们做额外的响应处理 35 | handlerMap_[WM_DESTROY] = VoidHandler; 36 | } 37 | 38 | // 使用默认的窗口类创建 39 | bool Window::Create() 40 | { 41 | if (windowMap_.count(handle_)) { 42 | Remove(); 43 | } 44 | return CreateFromClassName(defaultWindowClassName_); 45 | } 46 | 47 | bool Window::CreateFromClassName(const std::wstring& className) // 若需要使用系统控件,可传入系统预设窗口类名 48 | { 49 | if (windowMap_.count(handle_)) { 50 | Remove(); // 关闭和移除当前管理的窗口 51 | } 52 | className_ = className; 53 | handle_ = CreateWindow( 54 | className.c_str(), 55 | title_.c_str(), 56 | WS_OVERLAPPEDWINDOW, 57 | CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 58 | NULL, NULL, 59 | instance_, NULL 60 | ); // CreateWindow 执行过程中会发送 WM_CREATE 消息并等待它处理完,这样本框架就捕获不到 WM_CREATE 61 | 62 | windowMap_.insert({handle_, shared_from_this()}); 63 | // 因此我们在 windowMap_ 赋值后,重发一次 WM_CREATE 消息,此时请忽略消息参数 64 | SendMessage(handle_, WM_CREATE, 0 , 0); 65 | return true; 66 | } 67 | 68 | void Window::CreateFromHandle(HWND handle) 69 | { 70 | if (windowMap_.count(handle_)) { 71 | Remove(); 72 | } 73 | constexpr int BUFFER_SIZE = 256; 74 | std::vector text(BUFFER_SIZE, 0); 75 | GetClassName(handle, text.data(), BUFFER_SIZE); 76 | className_ = text.data(); 77 | GetWindowText(handle, text.data(), BUFFER_SIZE); 78 | title_ = text.data(); 79 | 80 | handle_ = handle; 81 | windowMap_.insert({handle_, shared_from_this()}); 82 | } 83 | 84 | void Window::SetTitle(const std::wstring& title) 85 | { 86 | title_ = title; 87 | if (handle_) { 88 | SetWindowText(handle_, title.c_str()); 89 | } 90 | } 91 | 92 | void Window::SetPosition(int x, int y, int width, int height) 93 | { 94 | SetWindowPos(handle_, NULL, x, y, width, height, SWP_SHOWWINDOW); 95 | UpdateView(); 96 | } 97 | 98 | void Window::UpdateView() 99 | { 100 | RECT rect; 101 | GetClientRect(handle_, &rect); 102 | InvalidateRect(handle_, &rect, true); 103 | UpdateWindow(handle_); // 绕过消息队列,立即调用 WndProc 处理重绘消息 104 | } 105 | 106 | void Window::Show(int mode) 107 | { 108 | ShowWindow(handle_, mode); 109 | UpdateWindow(handle_); 110 | } 111 | 112 | RECT Window::GetRect() 113 | { 114 | RECT rect; 115 | GetWindowRect(handle_, &rect); 116 | return rect; 117 | } 118 | 119 | HDC Window::GetBufferDC() 120 | { 121 | if (!bufferDC_) { 122 | HDC hdc = GetDC(handle_); 123 | bufferDC_ = CreateCompatibleDC(hdc); 124 | int width = GetSystemMetrics(SM_CXFULLSCREEN); 125 | int height = GetSystemMetrics(SM_CYFULLSCREEN); 126 | HBITMAP bitmap = CreateCompatibleBitmap(hdc, width, height); 127 | SelectObject(bufferDC_, bitmap); 128 | SetBkMode(bufferDC_, TRANSPARENT); 129 | ReleaseDC(handle_, hdc); 130 | } 131 | return bufferDC_; 132 | } 133 | 134 | void Window::ExchangeBuffer() 135 | { 136 | HDC hdc = GetDC(handle_); 137 | RECT rect; 138 | GetClientRect(handle_, &rect); 139 | BitBlt(hdc, rect.left, rect.top, rect.right, rect.bottom, 140 | GetBufferDC(), rect.left, rect.top, SRCCOPY); 141 | ReleaseDC(handle_, hdc); 142 | } 143 | 144 | void Window::Remove() 145 | { 146 | destroyPromise_ = std::make_unique>(); 147 | std::future destroyFuture = destroyPromise_->get_future(); 148 | DestroyWindow(handle_); 149 | destroyFuture.wait(); // 等待窗口销毁回调处理完,否则可能导致用户资源泄漏 150 | destroyPromise_ = nullptr; 151 | windowMap_.erase(handle_); 152 | } 153 | 154 | LRESULT Window::OnMessage(HWND handle, UINT message, WPARAM wParam, LPARAM lParam) 155 | { 156 | if (windowMap_.count(handle) && windowMap_[handle]->handlerMap_.count(message)) { 157 | windowMap_[handle]->handlerMap_[message](handle, wParam, lParam); 158 | if (message == WM_DESTROY && windowMap_[handle]->destroyPromise_) { 159 | windowMap_[handle]->destroyPromise_->set_value(true); 160 | } 161 | return 0; 162 | } 163 | return DefWindowProc(handle, message, wParam, lParam); 164 | } -------------------------------------------------------------------------------- /noumenon/ui/win.h: -------------------------------------------------------------------------------- 1 | // 东方盛夏 2022 2 | // https://www.zhihu.com/people/da-xia-tian-60 3 | #ifndef WINDOW_H 4 | #define WINDOW_H 5 | 6 | #ifndef UNICODE 7 | #define UNICODE // for windows.h 8 | #endif 9 | #ifndef _UNICODE 10 | #define _UNICODE // for tchar.h 11 | #endif 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | class Window; 27 | using MSG_HANDLER = std::function; 28 | void VoidHandler(HWND handle, WPARAM wParam, LPARAM lParam); 29 | 30 | class Window : public std::enable_shared_from_this { 31 | public: 32 | static HINSTANCE instance_; 33 | static std::wstring defaultWindowClassName_; 34 | static std::map> windowMap_; 35 | 36 | public: 37 | HWND handle_ {nullptr}; 38 | std::wstring className_; 39 | std::wstring title_; 40 | std::map handlerMap_; 41 | HDC bufferDC_ {nullptr}; // 用于双缓冲绘图 42 | std::unique_ptr> destroyPromise_; 43 | 44 | public: 45 | Window(); 46 | bool Create(); 47 | bool CreateFromClassName(const std::wstring& className); 48 | void CreateFromHandle(HWND handle); 49 | void Remove(); 50 | 51 | void Show(int mode = SW_SHOWNORMAL); 52 | RECT GetRect(); 53 | HDC GetBufferDC(); 54 | void ExchangeBuffer(); 55 | void UpdateView(); 56 | 57 | void SetTitle(const std::wstring& title); 58 | void SetPosition(int x, int y, int width, int height); 59 | 60 | public: 61 | static LRESULT __stdcall OnMessage(HWND handle, UINT message, WPARAM wParam, LPARAM lParam); 62 | static bool Initialise(HINSTANCE instance); 63 | }; 64 | #endif 65 | -------------------------------------------------------------------------------- /noumenon/utils/BUILD.gn: -------------------------------------------------------------------------------- 1 | import("//config.gni") 2 | 3 | config("utils_config") { 4 | include_dirs = [ "./" ] 5 | } 6 | 7 | source_set("utils") { 8 | sources = [ "codec.cpp" ] 9 | public_configs = [ ":utils_config" ] 10 | } 11 | -------------------------------------------------------------------------------- /noumenon/utils/codec.cpp: -------------------------------------------------------------------------------- 1 | // 东方盛夏 2022 2 | // https://www.zhihu.com/people/da-xia-tian-60 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "codec.h" 9 | 10 | /* 11 | 可用“也”字测试各编码转换函数是否正常工作 12 | Unicode UTF-8 GB18030(ANSI本地码) 13 | 4E5F 4B99F D2B2 14 | std::wstringstream wss; 15 | std::cout << (uint8_t)text[0]; 16 | */ 17 | std::wstring UTF8ToWide(const std::string& text) 18 | { 19 | #ifdef WIN32 20 | int n = MultiByteToWideChar(CP_UTF8, 0, text.c_str(), text.size(), NULL, 0); 21 | auto wideText = std::make_unique(n + 1); 22 | n = MultiByteToWideChar(CP_UTF8, 0, text.c_str(), text.size(), wideText.get(), n); 23 | wideText[n] = 0; 24 | wchar_t* wideTextFinal = wideText.get(); 25 | if (wideTextFinal[0] == 0xFEFF) { 26 | ++wideTextFinal; // delete UTF-8 BOM 27 | } 28 | return std::wstring(wideTextFinal); 29 | #else 30 | std::wstring wideText; 31 | try { 32 | std::wstring_convert> convert; 33 | wideText = convert.from_bytes(text); 34 | } catch (const std::exception & e) { 35 | std::cout << e.what() << std::endl; 36 | } 37 | return wideText; 38 | #endif 39 | } 40 | 41 | std::string WideToUTF8(const std::wstring& wideText) { 42 | #ifdef WIN32 43 | int n = WideCharToMultiByte(CP_UTF8, NULL, wideText.c_str(), -1, NULL, 0, NULL, NULL); 44 | auto text = std::make_unique(n + 1); 45 | n = WideCharToMultiByte(CP_UTF8, NULL, wideText.c_str(), -1, text.get(), n, NULL, NULL); 46 | text[n] = 0; 47 | return std::string(text.get()); 48 | #else 49 | std::string text; 50 | try { 51 | std::wstring_convert> convert; 52 | text = convert.to_bytes(wideText); 53 | } catch (const std::exception & e) { 54 | std::cout << e.what() << std::endl; 55 | } 56 | return text; 57 | #endif 58 | } 59 | 60 | #ifdef WIN32 61 | std::string WideToANSI(const std::wstring& wideText) { 62 | int n = WideCharToMultiByte(CP_ACP, NULL, wideText.c_str(), -1, NULL, 0, NULL, NULL); 63 | auto text = std::make_unique(n + 1); 64 | n = WideCharToMultiByte(CP_ACP, NULL, wideText.c_str(), -1, text.get(), n, NULL, NULL); 65 | text[n] = 0; 66 | return std::string(text.get()); 67 | } 68 | #endif -------------------------------------------------------------------------------- /noumenon/utils/codec.h: -------------------------------------------------------------------------------- 1 | // 东方盛夏 2022 2 | // https://www.zhihu.com/people/da-xia-tian-60 3 | #ifndef CODEC_H 4 | #define CODEC_H 5 | #include 6 | 7 | std::wstring UTF8ToWide(const std::string& text); 8 | std::string WideToUTF8(const std::wstring& wideText); 9 | #endif 10 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## 简介 2 | 3 | 这是一个基于 windows 平台的 2D 游戏/应用框架,由 C++ 编写的引擎层和 lua 编写的应用层构成。 4 | 5 | 引擎的编写应用了较多现代 C++ 特性,如使用各种 STL 容器、智能指针和 std::promise 等重建 windows 窗口编程模型,让窗口的创建和管理方式得以简化。在引擎中嵌入 Lua 解释器时,为了让 C++ 调用 Lua 函数的形式能够像引用原生函数一样简洁,使用变参模板、类型转发和函数重载等技巧,将 C/Lua 交互方式从原来的多行代码缩减为一行,封装的模板函数还支持直接传递字典和列表等高级数据结构。引擎的图形图像部分,充分应用了 windows GDI+ 的功能特性,绘图效率较高,且使用了双缓冲绘图技术,保证动画效果稳定、不闪屏。此外,整个 C++ 项目使用 gn/ninja 工具组织构建,编译速度快,且便于维护和扩展。 6 | 7 | 基于 C++ 引擎,用 Lua 编写了一个 2D RPG 游戏 Demo(使用了梦幻西游和传奇的游戏素材),这里应用了较多面向对象设计,使得场景中各个游戏元素能最大程度复用共性代码,程序结构和游戏世界实现了近似同构。 8 | 9 | ## 编译构建 10 | 11 | 有下列两种可选的编译构建方式,构建完成后,将`GamePainter.exe`移动到`usage`目录下运行,可以看到演示游戏画面,鼠标右键控制人物移动,左键点击 NPC 会弹出对话框。 12 | 13 | ### 使用 gn + ninja 构建 14 | 15 | 前置条件: 16 | 17 | - windows 环境 18 | - MSVC 构建工具及 windows SDK 19 | 20 | gn 与 ninja 工具已经预置在 build 目录下,可直接执行以下命令: 21 | 22 | ```shell 23 | build/gn gen -C out 24 | build/ninja -C out 25 | ``` 26 | 27 | ### 手动编译 28 | 29 | 前置条件: 30 | 31 | - windows 环境 32 | - clang/LLVM 工具集 33 | - MSVC 构建工具及 windows SDK 34 | 35 | 执行以下命令: 36 | 37 | ```shell 38 | cd noumenon 39 | clang++ main.cpp ui/win.cpp lua/lua_machine.cpp utils/*.cpp -o GamePainter.exe -Iui -Ilua -Iutils -Llua/bin -llua_static -luser32 -lgdi32 -lgdiplus -lwinmm -lImm32 40 | ``` 41 | 42 | 如果您需要使用其他基于 MSVC 构建的三方库,请改用 `clang-cl` 编译,并添加恰当的 `/MT` 或 `/MD` 选项,可避免 MSVC 运行时库相关冲突(如出现未解析符号告警)。 43 | 44 | ## 效果图 45 | 46 | ![](./usage/效果图1.jpg) 47 | 48 | ![](./usage/效果图2.jpg) 49 | 50 | 51 | -------------------------------------------------------------------------------- /usage/Config/config.lua: -------------------------------------------------------------------------------- 1 | scriptPath = "GameLive" 2 | resPath = "GameRes" 3 | -------------------------------------------------------------------------------- /usage/GameLive/Animation.lua: -------------------------------------------------------------------------------- 1 | -- 动画基类 2 | --[[ 3 | 参数结构约定: 4 | 传入的image为图片信息表,每一个图片元素用 {ID, frame, frameWidth} 组表示 5 | 其中 frame 是图片帧数,FW 是每帧宽度 6 | 之所以用缩写变量是为了尽可能便于书写,否则语句会冗长 7 | --]] 8 | 9 | Animation = {} 10 | function Animation:New(imageList) -- 传入图像ID表 11 | local obj = {} 12 | obj.imageList = imageList 13 | obj.lock = 0 14 | obj.dx = 0 -- 偏移量(用于矫正角色计算点) 15 | obj.dy = 0 16 | obj.imageIndex = 1 -- 当前图像索引 17 | obj.frame = 1 -- 当前帧 18 | obj.frameRepeat = 1 -- 每帧重复次数,可控制动画速率 19 | obj.frameRepeatCounter = 1 -- 当前帧已画次数 20 | setmetatable(obj, self) 21 | self.__index = self 22 | return obj 23 | end 24 | 25 | function Animation:LockFrame(frame) 26 | self.lock = 1 27 | self.frame = frame 28 | end 29 | 30 | function Animation:Show(x, y) 31 | local index = self.imageIndex 32 | local sx = (self.frame - 1) * self.imageList[index].frameWidth 33 | Painter.DrawImageByFrame( 34 | self.imageList[index].ID, 35 | x - self.dx, y - self.dy, 36 | sx, self.imageList[index].frameWidth 37 | ) 38 | if self.lock == 0 then 39 | if self.frameRepeatCounter < self.frameRepeat then 40 | self.frameRepeatCounter = self.frameRepeatCounter + 1 41 | else 42 | if self.frame == self.imageList[index].frameCount then 43 | self.frame = 1 44 | else 45 | self.frame = self.frame + 1 46 | end 47 | self.frameRepeatCounter = 1 48 | end 49 | end 50 | end 51 | 52 | function Animation:SetImage(index) 53 | if self.imageIndex ~= index then 54 | self.imageIndex = index 55 | self.frame = 1 56 | end 57 | end 58 | -------------------------------------------------------------------------------- /usage/GameLive/AnimationX.lua: -------------------------------------------------------------------------------- 1 | -- 动画基类 2 | --[[ 3 | 参数结构约定: 4 | 传入的image为图片信息表,每一个图片元素用 {ID, frame, frameWidth} 组表示 5 | 其中 frame 是图片帧数,frameWidth 是每帧宽度 6 | 用缩写变量是为了尽可能便于书写,否则语句会冗长 7 | --]] 8 | Animation = {} 9 | 10 | function Animation:New(imageList) -- 传入图像ID表 11 | local obj = {} 12 | obj.imageList = imageList 13 | obj.lock = 0 14 | obj.imageIndex = 1 -- 当前图像索引 15 | obj.frame = 1 -- 当前帧 16 | obj.frameRepeat = 1 -- 每帧重复次数,可控制帧率 17 | obj.frameRepeatCounter = 1 -- 当前帧已画次数 18 | setmetatable(obj, self) 19 | self.__index = self 20 | return obj 21 | end 22 | 23 | function Animation:LockFrame(frame) 24 | self.lock = 1 25 | self.frame = frame 26 | end 27 | 28 | function Animation:Show(x, y) 29 | local index = self.imageIndex 30 | local sx, sy = self:GetFrame(self.frame) 31 | Painter.DrawImage(self.imageList[index].ID, x - self.imageList[index].dx, y - self.imageList[index].dy, sx, sy, self.imageList[index].frameWidth, 32 | self.imageList[index].frameHeight) 33 | if self.lock == 0 then 34 | if self.frameRepeatCounter < self.frameRepeat then 35 | self.frameRepeatCounter = self.frameRepeatCounter + 1 36 | else 37 | if self.frame == self.imageList[index].frameCount * self.imageList[index].YF then 38 | self.frame = 1 39 | else 40 | self.frame = self.frame + 1 41 | end 42 | self.frameRepeatCounter = 1 43 | end 44 | end 45 | end 46 | 47 | function Animation:GetFrame(frame) 48 | local index = self.imageIndex 49 | local sx = self.imageList[index].frameWidth * math.floor((frame - 1) % 50 | self.imageList[index].frameCount) 51 | local sy = self.imageList[index].frameHeight * math.floor((frame - 1) / 52 | self.imageList[index].frameCount) 53 | return sx, sy 54 | end 55 | 56 | function Animation:SetImage(index) 57 | if self.imageIndex ~= index then 58 | self.imageIndex = index 59 | self.frame = 1 60 | end 61 | end 62 | 63 | -- 程序开源,请勿用于商业或不良游戏制作 大夏天2015 64 | 65 | -------------------------------------------------------------------------------- /usage/GameLive/Assets.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | 这里的Res载入方式是启动一起载入,显然在正式发布游戏时如果资源过多, 3 | 这是不合理的方法,所以目前只供测试使用,正式搭建游戏时应该动态地合 4 | 理管理资源 5 | --]] 6 | 7 | g_assets = {} 8 | g_assets.HERO_RUNL = Painter.AddImage("GameRes/Image/heroL.png") 9 | g_assets.HERO_RUNR = Painter.AddImage("GameRes/Image/heroR.png") 10 | g_assets.HERO_RUNU = Painter.AddImage("GameRes/Image/heroRU.png") 11 | g_assets.HERO_RUND = Painter.AddImage("GameRes/Image/heroRD.png") 12 | g_assets.HERO_RUNUL = Painter.AddImage("GameRes/Image/heroRUL.png") 13 | g_assets.HERO_RUNUR = Painter.AddImage("GameRes/Image/heroRUR.png") 14 | g_assets.HERO_RUNDL = Painter.AddImage("GameRes/Image/heroRDL.png") 15 | g_assets.HERO_RUNDR = Painter.AddImage("GameRes/Image/heroRDR.png") 16 | g_assets.HERO_STANDL = Painter.AddImage("GameRes/Image/heroLS.png") 17 | g_assets.HERO_STANDR = Painter.AddImage("GameRes/Image/heroRS.png") 18 | 19 | g_assets.BTN_CHECK = Painter.AddImage("GameRes/Image/button1.png") 20 | g_assets.BOX_TEXT = Painter.AddImage("GameRes/Image/Box1.png") 21 | g_assets.BRD_TOOL = Painter.AddImage("GameRes/Image/ToolBoard.png") 22 | g_assets.BOX_SAY = Painter.AddImage("GameRes/Image/Saying.png") 23 | 24 | g_assets.NPC_FOODSALER = Painter.AddImage("GameRes/Image/FoodSaler.png") 25 | 26 | g_assets.MON_BEARL = Painter.AddImage("GameRes/Image/BearL.png") 27 | g_assets.MON_BEARR = Painter.AddImage("GameRes/Image/BearR.png") 28 | 29 | g_assets.MON_MUMUL = Painter.AddImage("GameRes/Image/MumuL.png") 30 | g_assets.MON_MUMUR = Painter.AddImage("GameRes/Image/MumuR.png") 31 | 32 | g_assets.MUSIC_SUMMER = Painter.AddMedia("GameRes/Sound/summer.mp3") 33 | -------------------------------------------------------------------------------- /usage/GameLive/Bear.lua: -------------------------------------------------------------------------------- 1 | -- 野怪北极熊 2 | require("Animation") 3 | require("Define") 4 | require("Monster") 5 | require("TextBox") 6 | 7 | Bear = Monster:New() 8 | function Bear:New(x, y) 9 | local obj = {} 10 | self.wx = x 11 | self.wy = y 12 | local image = {} 13 | image[1] = {} 14 | image[1].ID = g_assets.MON_BEARL 15 | image[1].frameCount = 8 -- 帧数 16 | image[1].frameWidth = 81 -- 每帧宽度 17 | image[2] = {} 18 | image[2].ID = g_assets.MON_BEARR 19 | image[2].frameCount = 8 -- 帧数 20 | image[2].frameWidth = 81 -- 每帧宽度 21 | self.animation = Animation:New(image) 22 | self.animation.frameRepeat = 2 -- 帧重复次数 23 | setmetatable(obj, self) -- 新建对象 24 | self.__index = self -- 设置基类(元表)的__index为自身,使得派生表可访问自身成员 25 | return obj 26 | end 27 | 28 | function Bear:OnLButtonDown(x, y) 29 | if IN_BOX(x, y, self.x, self.y, 80, 80) == 1 then 30 | local t = TextBox:New(self.x - 200, self.y - 120, "可乐的味道真不错啊", 0xFFFFFFFF) 31 | t.imageID = g_assets.BOX_SAY 32 | t.lock = 1 33 | t.width = 244 34 | t.height = 113 35 | t.textX = 19 36 | t.textY = 21 37 | world:Add(t) 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /usage/GameLive/Board.lua: -------------------------------------------------------------------------------- 1 | -- 固定面板控件 2 | require("Define") 3 | require("UI") 4 | 5 | Board = UI:New() 6 | 7 | function Board:New(x, y) 8 | local obj = {} 9 | self.x = x 10 | self.y = y 11 | obj.image = {} 12 | obj.imageID = g_assets.BRD_TOOL 13 | obj.width = 800 14 | obj.height = 205 15 | obj.child = {} -- 子控件集 16 | setmetatable(obj, self) 17 | self.__index = self 18 | return obj 19 | end 20 | 21 | function Board:Change() -- 状态改变 22 | for k, v in pairs(self.child) do 23 | v:Change() 24 | end 25 | self.x = (WIDTH - self.width) / 2 26 | self.y = HEIGHT - self.height; 27 | end 28 | -------------------------------------------------------------------------------- /usage/GameLive/Button.lua: -------------------------------------------------------------------------------- 1 | -- 按钮基类 2 | require("Define") 3 | require("UI") 4 | 5 | Button = UI:New() 6 | 7 | function Button:New(x, y, rx, ry) 8 | local obj = {} 9 | obj.x = x -- 绘图坐标 10 | obj.y = y 11 | obj.rx = rx -- 偏移坐标(用于子控件) 12 | obj.ry = ry 13 | obj.frame = 1 14 | obj.OnClick = {} -- 回调函数 15 | obj.imageID = g_assets.BTN_CHECK 16 | obj.width = 45 17 | obj.height = 24 18 | setmetatable(obj, self) 19 | self.__index = self 20 | return obj 21 | end 22 | 23 | function Button:OnLButtonDown(x, y) 24 | if IN_BOX(x, y, self.x, self.y, self.width, self.height) == 1 then 25 | self.frame = 2 26 | self.mDown = 1 27 | end 28 | end 29 | 30 | function Button:OnLButtonUp(x, y) 31 | self.mDown = 0 32 | self.frame = 1 33 | if IN_BOX(x, y, self.x, self.y, self.width, self.height) == 1 then 34 | self:OnClick() 35 | end 36 | end 37 | 38 | function Button:Update() 39 | self:Change() 40 | self:Draw() 41 | end 42 | -------------------------------------------------------------------------------- /usage/GameLive/Define.lua: -------------------------------------------------------------------------------- 1 | require("State") -- 角色/场景状态 2 | 3 | -- 实时鼠标位置 4 | g_mouseX = 0 5 | g_mouseY = 0 6 | 7 | -- 实时主角位置 8 | g_heroX = 0 9 | g_heroY = 0 10 | 11 | -- 实时地图起点 12 | g_mapOffsetX = 0 13 | g_mapOffsetY = 0 14 | 15 | g_heroState = State:New() -- 主角状态 16 | 17 | -- 窗口宽高 18 | WIDTH = 1500 19 | HEIGHT = 800 20 | 21 | -- 方向常量 22 | NORTH = 1 23 | SOUTH = 2 24 | WEST = 3 25 | EAST = 4 26 | NORTHWEST = 5 27 | NORTHEAST = 6 28 | SOUTHWEST = 7 29 | SOUTHEAST = 8 30 | 31 | -- 数学常量 32 | PI = 3.1415927 -- 圆周率 33 | EPI = 0.392699 -- 八分之一 PI 34 | RAD = 0.017453 -- 1 弧度的度数 35 | 36 | function IN_BOX(x, y, lx, ly, w, h) 37 | if x > lx and x < lx + w then 38 | if y > ly and y < ly + h then 39 | return 1 40 | end 41 | end 42 | return 0 43 | end 44 | 45 | function DRAW_FRAME(id, x, y, frame, w) 46 | Painter.DrawImageByFrame(id, x, y, w * (frame - 1), w) 47 | end 48 | 49 | function DIRECTION(x0, y0, x, y) 50 | local sita = 0 51 | if x > x0 and y < y0 then 52 | sita = math.atan((y0 - y) / (x - x0)) 53 | if sita < EPI then 54 | return EAST 55 | end 56 | if sita > EPI and sita < 3 * EPI then 57 | return NORTHEAST 58 | end 59 | return NORTH 60 | end 61 | 62 | if x < x0 and y < y0 then 63 | sita = math.atan((y0 - y) / (x0 - x)) 64 | if sita < EPI then 65 | return WEST 66 | end 67 | if sita > EPI and sita < 3 * EPI then 68 | return NORTHWEST 69 | end 70 | return NORTH 71 | end 72 | 73 | if x < x0 and y > y0 then 74 | sita = math.atan((y - y0) / (x0 - x)) 75 | if sita < EPI then 76 | return WEST 77 | end 78 | if sita > EPI and sita < 3 * EPI then 79 | return SOUTHWEST 80 | end 81 | return SOUTH 82 | end 83 | if x > x0 and y > y0 then 84 | sita = math.atan((y - y0) / (x - x0)) 85 | if sita < EPI then 86 | return EAST 87 | end 88 | if sita > EPI and sita < 3 * EPI then 89 | return SOUTHEAST 90 | end 91 | return SOUTH 92 | end 93 | end 94 | -------------------------------------------------------------------------------- /usage/GameLive/EastHero.lua: -------------------------------------------------------------------------------- 1 | -- 游戏主角之一 2 | require("Define") 3 | require("Legend") 4 | 5 | EastHero = Legend:New() 6 | 7 | function EastHero:New(x, y) 8 | local obj = {} 9 | self.wx = x 10 | self.wy = y 11 | 12 | local image = {} 13 | image[1] = {} 14 | image[1].ID = g_assets.HERO_STANDL 15 | image[1].frameCount = 4 16 | image[1].frameWidth = 200 17 | image[2] = {} 18 | image[2].ID = g_assets.HERO_STANDR 19 | image[2].frameCount = 4 20 | image[2].frameWidth = 200 21 | image[3] = {} 22 | image[3].ID = g_assets.HERO_RUNL 23 | image[3].frameCount = 6 24 | image[3].frameWidth = 200 25 | image[4] = {} 26 | image[4].ID = g_assets.HERO_RUNR 27 | image[4].frameCount = 6 28 | image[4].frameWidth = 200 29 | image[5] = {} 30 | image[5].ID = g_assets.HERO_RUNU 31 | image[5].frameCount = 6 32 | image[5].frameWidth = 200 33 | image[6] = {} 34 | image[6].ID = g_assets.HERO_RUND 35 | image[6].frameCount = 6 36 | image[6].frameWidth = 200 37 | image[7] = {} 38 | image[7].ID = g_assets.HERO_RUNUL 39 | image[7].frameCount = 6 40 | image[7].frameWidth = 200 41 | image[8] = {} 42 | image[8].ID = g_assets.HERO_RUNUR 43 | image[8].frameCount = 6 44 | image[8].frameWidth = 200 45 | image[9] = {} 46 | image[9].ID = g_assets.HERO_RUNDL 47 | image[9].frameCount = 6 48 | image[9].frameWidth = 200 49 | image[10] = {} 50 | image[10].ID = g_assets.HERO_RUNDR 51 | image[10].frameCount = 6 52 | image[10].frameWidth = 200 53 | 54 | self.animation = Animation:New(image) 55 | self.animation.dx = 100 56 | self.animation.dy = 85 57 | 58 | setmetatable(obj, self) 59 | self.__index = self 60 | return obj 61 | end 62 | -------------------------------------------------------------------------------- /usage/GameLive/FoodSaler.lua: -------------------------------------------------------------------------------- 1 | -- 商人 2 | require("Animation") 3 | require("Button") 4 | require("NPC") 5 | require("TextBox") 6 | 7 | FoodSaler = NPC:New() 8 | function FoodSaler:New(x, y) 9 | local obj = {} 10 | self.wx = x 11 | self.wy = y 12 | local image = {} 13 | image[1] = {} 14 | image[1].ID = g_assets.NPC_FOODSALER 15 | image[1].frameCount = 4 -- 帧数 16 | image[1].frameWidth = 80 -- 每帧宽度 17 | self.animation = Animation:New(image) 18 | self.animation.frameRepeat = 2 19 | setmetatable(obj, self) -- 新建对象 20 | self.__index = self -- 设置基类(元表)的__index为自身,使得派生表可访问自身成员 21 | return obj 22 | end 23 | 24 | function FoodSaler:Near(x, y) -- 判断位置是否在角色身上 25 | if x > self.x and x < self.x + 50 then 26 | if y > self.y and y < self.y + 50 then 27 | return 1 28 | end 29 | end 30 | return 0 31 | end 32 | 33 | function FoodSaler:Story() -- 故事演绎 34 | local textBox = TextBox:New(100, 100, "大丈夫当朝碧海而暮苍梧", 0xFF4A4137) 35 | local button = Button:New(0, 0, 302, 190) 36 | button.OnClick = function() 37 | world:Delete(textBox.ID) 38 | end 39 | textBox:AddChild(button) 40 | world:Add(textBox) 41 | end 42 | -------------------------------------------------------------------------------- /usage/GameLive/Game.lua: -------------------------------------------------------------------------------- 1 | -- 游戏前端核心脚本 2 | package.path = package.path .. ";GameLive/?.lua;" 3 | require("Painter") -- GamePainter传递的C函数库 4 | require("Define") -- 全局常量/变量 5 | require("World") -- 世界管理器 6 | 7 | require("EastHero") -- 主角基类 8 | require("FoodSaler") -- 商人 9 | require("Bear") -- 野怪北极熊 10 | require("Mumu") -- 野怪阿木木 11 | require("Map") -- 地图 12 | require("Board") -- 状态面板 13 | 14 | function GameInit() 15 | require("Assets") 16 | Painter.SetClock(100) 17 | Painter.SetCanvas(WIDTH, HEIGHT) 18 | Painter.SetFont("宋体", 12) 19 | 20 | -- 创建世界元素 21 | map = Map:New("GameRes/Image/长安城b.jpg", -349, -1225, 3800, 2438) 22 | world = World:New() 23 | world:Add(map) 24 | world:Add(Bear:New(904, 1089)) 25 | world:Add(MuMu:New(126, 2137)) 26 | world:Add(FoodSaler:New(803, 1637)) 27 | MY_ID = world:Add(EastHero:New(971, 1728)) 28 | world:Add(Board:New(200,395)) 29 | -- Painter.PlayMedia(g_assets.MUSIC_SUMMER);--背景音乐 30 | end 31 | 32 | function GameLoop() -- 游戏主循环函数,必须含有 33 | -- Painter.DrawImageByScale(imageID, 0, 0, 1, 1) 34 | -- Painter.DrawSolidRect(0, 0, WIDTH, HEIGHT, 0xFF666666) 35 | -- local text = "你好,世界" 36 | -- Painter.DrawString(0, 0, text .. utf8.len(text), 0xFFFFFFFF) 37 | for k, obj in pairs(world.noumena) do 38 | obj:Update() 39 | end 40 | g_heroState.attacking = 0 41 | Painter.UpdateCanvas() 42 | end 43 | 44 | function OnLButtonDown(x, y) -- 鼠标左键按下 45 | for k, obj in pairs(world.noumena) do 46 | if obj.OnLButtonDown then 47 | obj:OnLButtonDown(x, y) 48 | end 49 | end 50 | end 51 | 52 | function OnLButtonUp(x, y) -- 鼠标左键松开 53 | for k, obj in pairs(world.noumena) do 54 | if obj.OnLButtonUp then 55 | obj:OnLButtonUp(x, y) 56 | end 57 | end 58 | end 59 | 60 | function OnMouseMove(x, y) -- 鼠标移动 61 | g_mouseX = x 62 | g_mouseY = y 63 | end 64 | 65 | function OnRButtonDown(x, y) -- 鼠标右键按下 66 | for k, obj in pairs(world.noumena) do 67 | if obj.OnRButtonDown then 68 | obj:OnRButtonDown(x, y) 69 | end 70 | end 71 | end 72 | 73 | function OnLButtonClick(x, y) -- 鼠标左键双击 74 | 75 | end 76 | 77 | function OnMouseWheel(x, y, nDelta) -- 鼠标轮滚动 78 | 79 | end 80 | 81 | function OnVKeyDown(nKey, nRep) -- 虚拟键按下 82 | 83 | end 84 | 85 | function OnCKeyDown(nKey, nRep) -- 字符键按下 86 | 87 | end 88 | 89 | function OnSize(width, height) -- 窗口尺寸变化 90 | WIDTH = width 91 | HEIGHT = height 92 | map:OnSize(width, height) 93 | end 94 | -------------------------------------------------------------------------------- /usage/GameLive/Legend.lua: -------------------------------------------------------------------------------- 1 | -- 游戏主角的基类,这个类只完成对鼠标右键的处理 2 | require("Life") 3 | 4 | Legend = Life:New() -- 每一个角主角,都是一个传奇 5 | function Legend:New() 6 | local obj = {} 7 | obj.mx = 0 8 | obj.my = 0 9 | self.timer = 0 10 | self.speed = 10 11 | setmetatable(obj, self) -- 新建对象 12 | self.__index = self -- 设置基类(元表)的__index为自身,使得派生表可访问自身成员 13 | return obj 14 | end 15 | 16 | function Legend:OnRButtonDown(x, y) -- 响应右键单击 17 | self.mx = x - g_mapOffsetX -- 记下按下鼠标时的世界位置 18 | self.my = y - g_mapOffsetY 19 | self.changed = 1 20 | self.direction = DIRECTION(self.x, self.y, x, y) 21 | 22 | -- 根据当前方向选择动画 23 | if self.direction == EAST then 24 | self.animation:SetImage(4) 25 | elseif self.direction == NORTH then 26 | self.animation:SetImage(5) 27 | elseif self.direction == WEST then 28 | self.animation:SetImage(3) 29 | elseif self.direction == SOUTH then 30 | self.animation:SetImage(6) 31 | elseif self.direction == NORTHEAST then 32 | self.animation:SetImage(8) 33 | elseif self.direction == NORTHWEST then 34 | self.animation:SetImage(7) 35 | elseif self.direction == SOUTHEAST then 36 | self.animation:SetImage(10) 37 | elseif self.direction == SOUTHWEST then 38 | self.animation:SetImage(9) 39 | end 40 | self.timer = 8; 41 | end 42 | 43 | function Legend:Change() -- 状态改变 44 | g_heroX = self.x -- 反馈主角屏幕位置给全局 45 | g_heroY = self.y 46 | if self.timer > 0 then 47 | self:FindRoute() 48 | self.timer = self.timer - 1 49 | else 50 | if self.direction == EAST then -- 已到达 51 | self.animation:SetImage(2) 52 | else 53 | self.animation:SetImage(1) 54 | end 55 | self.changed = 0 56 | end 57 | end 58 | 59 | function Legend:FindRoute() -- 寻路行走(仅供测试效果,有待优化改进) 60 | local dv = self.speed * 0.8 61 | if self.direction == EAST then 62 | self.wx = self.wx + self.speed 63 | elseif self.direction == NORTH then 64 | self.wy = self.wy - self.speed 65 | elseif self.direction == WEST then 66 | self.wx = self.wx - self.speed 67 | elseif self.direction == SOUTH then 68 | self.wy = self.wy + self.speed 69 | elseif self.direction == NORTHEAST then 70 | self.wx = self.wx + dv 71 | self.wy = self.wy - dv 72 | elseif self.direction == NORTHWEST then 73 | self.wx = self.wx - dv 74 | self.wy = self.wy - dv 75 | elseif self.direction == SOUTHEAST then 76 | self.wx = self.wx + dv 77 | self.wy = self.wy + dv 78 | elseif self.direction == SOUTHWEST then 79 | self.wx = self.wx - dv 80 | self.wy = self.wy + dv 81 | end 82 | end 83 | 84 | function Legend:onCKeyDown(nKey, nRep) 85 | 86 | end 87 | 88 | -- 程序开源,版权保留,请勿用于商业或不良游戏制作 大夏天2015 89 | -------------------------------------------------------------------------------- /usage/GameLive/Life.lua: -------------------------------------------------------------------------------- 1 | -- 所有活动元素的基类 2 | Life = {} 3 | function Life:New() 4 | local obj = {} 5 | obj.ID = 0 -- 对象在世界管理器中的ID 6 | obj.x = 0 7 | obj.y = 0 8 | obj.wx = 0 9 | obj.wy = 0 10 | obj.animation = {} -- 对象的动画管理器 11 | obj.direction = WEST -- 朝向 12 | obj.timer = 16 -- 计数器 13 | obj.life = 100 -- 生命值 14 | obj.speed = 0 -- 移动速度 15 | obj.changed = 0 -- 状态改变标志 16 | setmetatable(obj, self) -- 新建对象 17 | self.__index = self -- 设置基类(元表)的__index为自身,使得派生表可访问自身成员 18 | return obj 19 | end 20 | 21 | function Life:Update() -- 对外界更新,成员函数 22 | self:Transform() 23 | self:Change() 24 | self:Draw() 25 | end 26 | 27 | function Life:Draw() 28 | self.animation:Show(self.x, self.y) 29 | end 30 | 31 | function Life:Change() -- 状态更新,由具体角色实现 32 | 33 | end 34 | 35 | function Life:Transform() -- 坐标变换(对象依赖于地图,self.x是绘图坐标,self.wx是世界坐标) 36 | self.x = self.wx + g_mapOffsetX 37 | self.y = self.wy + g_mapOffsetY 38 | end 39 | function Life:OnLButtonDown(x, y) 40 | 41 | end 42 | 43 | function Life:OnRButtonDown(x, y) 44 | 45 | end 46 | 47 | function Life:OnLButtonUp(x, y) 48 | 49 | end 50 | 51 | -------------------------------------------------------------------------------- /usage/GameLive/Map.lua: -------------------------------------------------------------------------------- 1 | -- 地图基类 2 | require("Define") 3 | require("Painter") 4 | 5 | Map = {} 6 | function Map:New(imagePath, x, y, w, h) 7 | local obj = {} 8 | obj.ID = -1 -- 对象在世界管理器中的ID 9 | obj.x = x obj.y = y -- 绘图坐标 10 | obj.imageID = Painter.AddImage(imagePath) 11 | obj.width = w 12 | obj.height = h 13 | obj.speed = 10 14 | obj.haveMask = 0 -- 有无遮罩 15 | obj.maskID = -1 -- 遮罩图ID 16 | setmetatable(obj, self) -- 新建对象 17 | self.__index = self -- 设置基类(元表)的 __index 为自身,使得派生表可访问自身成员 18 | return obj 19 | end 20 | 21 | function Map:LoadMask(imagePath) -- 添加路径遮罩 22 | self.maskID = Painter.AddImage(imagePath) 23 | self.haveMask = 1 24 | end 25 | 26 | function Map:Update() -- 更新状态 27 | self:Change() 28 | self:Draw() 29 | end 30 | 31 | function Map:Draw() 32 | Painter.DrawImageByScale(self.imageID, self.x, self.y, 1, 1) -- 绘制地景 33 | end 34 | 35 | function Map:Change() 36 | if g_heroX > 2 * WIDTH / 3 then 37 | if self.width + self.x - self.speed > WIDTH then 38 | self.x = self.x - self.speed 39 | else 40 | self.x = WIDTH - self.width 41 | end 42 | elseif g_heroX < WIDTH / 4 then 43 | if self.x + self.speed < 0 then 44 | self.x = self.x + self.speed 45 | else 46 | self.x = 0 47 | end 48 | end 49 | 50 | if g_heroY > 2 * HEIGHT / 3 then 51 | if self.height + self.y - self.speed > HEIGHT then 52 | self.y = self.y - self.speed 53 | else 54 | self.y = HEIGHT - self.height 55 | end 56 | elseif g_heroY < HEIGHT / 4 then 57 | if self.y + self.speed < 0 then 58 | self.y = self.y + self.speed 59 | else 60 | self.y = 0 61 | end 62 | end 63 | g_mapOffsetX = self.x 64 | g_mapOffsetY = self.y 65 | end 66 | 67 | function Map:OnSize(width, height) 68 | if self.width + self.x < width then 69 | self.x = width - self.width 70 | end 71 | if self.height + self.y < height then 72 | self.y = height - self.height 73 | end 74 | end 75 | 76 | function Map:MaskCheck() -- 遮罩检测 77 | 78 | end 79 | 80 | function Map:OnRButtonDown(x, y) 81 | 82 | end 83 | 84 | function Map:OnLButtonDown(x, y) 85 | 86 | end 87 | 88 | function Map:OnLButtonUp(x, y) 89 | 90 | end 91 | -------------------------------------------------------------------------------- /usage/GameLive/Monster.lua: -------------------------------------------------------------------------------- 1 | -- 野怪和敌人的基类,描述一种来回移动的对象 2 | require("Life") 3 | 4 | Monster = Life:New() 5 | function Monster:New() 6 | local obj = {} 7 | obj.state = State:New() 8 | setmetatable(obj, self) -- 新建对象 9 | self.__index = self -- 设置基类(元表)的__index为自身,使得派生表可访问自身成员 10 | return obj 11 | end 12 | 13 | function Monster:Change() -- 来回移动 14 | if self.timer > 0 then 15 | self.timer = self.timer - 1 16 | if self.direction == EAST then 17 | self.wx = self.wx + self.speed 18 | else 19 | self.wx = self.wx - self.speed 20 | end 21 | else 22 | self.timer = 16 23 | if self.direction == EAST then 24 | self.direction = WEST 25 | self.animation:SetImage(1) 26 | else 27 | self.direction = EAST 28 | self.animation:SetImage(2) 29 | end 30 | end 31 | 32 | self:OnAttacked() 33 | end 34 | 35 | function Monster:Draw() 36 | self.animation:Show(self.x, self.y) 37 | Painter.DrawString(self.x + 30, self.y - 20, "生命值" .. tostring(self.state.life), 0xFFAA5522) 38 | end 39 | 40 | function Monster:OnAttacked() 41 | if g_heroState.attacking == 1 then 42 | self.state.life = self.state.life - g_heroState.power 43 | end 44 | end 45 | 46 | -------------------------------------------------------------------------------- /usage/GameLive/MuMu.lua: -------------------------------------------------------------------------------- 1 | -- 野怪阿木木 2 | require("Animation") 3 | require("Monster") 4 | 5 | MuMu = Monster:New() 6 | function MuMu:New(x, y) 7 | local obj = {} 8 | self.speed = 5 9 | self.wx = x 10 | self.wy = y 11 | local image = {} 12 | image[1] = {} 13 | image[1].ID = g_assets.MON_MUMUL 14 | image[1].frameCount = 8 -- 帧数 15 | image[1].frameWidth = 80 -- 每帧宽度 16 | image[2] = {} 17 | image[2].ID = g_assets.MON_MUMUR 18 | image[2].frameCount = 8 -- 帧数 19 | image[2].frameWidth = 80 -- 每帧宽度 20 | self.animation = Animation:New(image) 21 | self.animation.frameRepeat = 3 22 | setmetatable(obj, self) -- 新建对象 23 | self.__index = self -- 设置基类(元表)的__index为自身,使得派生表可访问自身成员 24 | return obj 25 | end 26 | -------------------------------------------------------------------------------- /usage/GameLive/NPC.lua: -------------------------------------------------------------------------------- 1 | require("Life") 2 | 3 | NPC = Life:New() -- 其他角色的基类 4 | 5 | function NPC:New() 6 | local obj = {} 7 | setmetatable(obj, self) -- 新建对象 8 | self.__index = self -- 设置基类(元表)的__index为自身,使得派生表可访问自身成员 9 | return obj 10 | end 11 | 12 | function NPC:OnRButtonDown(x, y) 13 | if self:Near(x, y) == 1 then 14 | slef:Story() 15 | end 16 | end 17 | 18 | function NPC:OnLButtonDown(x, y) -- NPC响应左键单击 19 | if self:Near(x, y) == 1 then 20 | self:Story() 21 | end 22 | end 23 | 24 | function NPC:Change() -- 状态改变 25 | 26 | end 27 | -------------------------------------------------------------------------------- /usage/GameLive/State.lua: -------------------------------------------------------------------------------- 1 | -- 状态描述类 2 | State = {} 3 | 4 | function State:New() 5 | local obj = {} 6 | obj.life = 100 -- 生命值 7 | obj.level = 1 -- 等级 8 | obj.power = 10 -- 攻击力 9 | obj.defense = 10 -- 防御力 10 | obj.attacking = 0 -- 是否处于攻击状态 11 | obj.range = 50 -- 攻击范围 12 | obj.frequency = 1 -- 每秒攻击次数 13 | 14 | setmetatable(obj, self) 15 | self.__index = self 16 | return obj 17 | end 18 | -------------------------------------------------------------------------------- /usage/GameLive/TextBox.lua: -------------------------------------------------------------------------------- 1 | require("UI") 2 | require("Define") 3 | 4 | TextBox = UI:New() -- 文本框类 5 | function TextBox:New(x, y, text, textColor) 6 | local obj = {} 7 | obj.x = x 8 | obj.y = y 9 | obj.wx = x - g_mapOffsetX 10 | obj.wy = y - g_mapOffsetY 11 | obj.textX = 55 12 | obj.textY = 69 13 | obj.lineChars = 16 -- 每行字符数 14 | obj.text = text 15 | obj.textColor = textColor 16 | obj.len = string.len(text) / 2 -- 中文 17 | obj.lock = 0 -- 是否固定 18 | obj.mx = 0 -- 鼠标左键按下的位置 19 | obj.my = 0 20 | obj.ox = 0 -- 鼠标左键按下时控件位置 21 | obj.oy = 0 22 | obj.child = {} -- 子控件表 23 | obj.imageID = g_assets.BOX_TEXT -- 默认对话框背景 24 | obj.width = 380 25 | obj.height = 240 26 | setmetatable(obj, self) -- 新建对象 27 | self.__index = self -- 设置基类(元表)的__index为自身,使得派生表可访问自身成员 28 | return obj 29 | end 30 | 31 | function TextBox:Draw() 32 | local index = 1 -- 字索引 33 | local row = 0 -- 行数 34 | if self.lock == 1 then 35 | self.x = self.wx + g_mapOffsetX 36 | self.y = self.wy + g_mapOffsetY 37 | end 38 | Painter.DrawImageByScale(self.imageID, self.x, self.y, 1, 1) 39 | charHeight = 12 40 | while index < self.len do 41 | if index + self.lineChars < self.len then 42 | Painter.DrawString(self.x + self.textX, row * charHeight + self.y + self.textY, 43 | -- utf8:sub(self.text, index, index + self.lineChars), -- 目前有问题,待修复 44 | self.text, self.textColor) 45 | row = row + 1 -- 换行 46 | else 47 | Painter.DrawString(self.x + self.textX, row * charHeight + self.y + self.textY, 48 | -- utf8:sub(self.text, index, self.len), -- 目前有问题,待修复 49 | self.text, self.textColor) 50 | end 51 | index = index + self.lineChars 52 | end 53 | for k, v in pairs(self.child) do 54 | v.x = self.x + v.rx 55 | v.y = self.y + v.ry 56 | v:Draw() 57 | end 58 | end 59 | 60 | function TextBox:OnLButtonDown(x, y) 61 | for k, v in pairs(self.child) do 62 | v:OnLButtonDown(x, y) 63 | end 64 | if IN_BOX(g_mouseX, g_mouseY, self.x, self.y, self.width, self.height) == 1 then 65 | self.mDown = 1 66 | self.mx = x 67 | self.my = y 68 | self.ox = self.x 69 | self.oy = self.y 70 | end 71 | end 72 | 73 | function TextBox:Change() -- 状态改变 74 | for k, v in pairs(self.child) do 75 | v:Change() 76 | end 77 | if self.mDown == 1 then 78 | self.x = self.ox + g_mouseX - self.mx 79 | self.y = self.oy + g_mouseY - self.my 80 | end 81 | end 82 | -------------------------------------------------------------------------------- /usage/GameLive/UI.lua: -------------------------------------------------------------------------------- 1 | -- 界面交互控件的基类 2 | require("Define") 3 | require("Painter") 4 | 5 | UI = {} 6 | function UI:New() 7 | local obj = {} 8 | obj.ID = -1 -- 对象在世界管理器中的ID 9 | obj.mDown = 0 -- 鼠标左键是否按下 10 | obj.imageID = -1 11 | obj.width = 0 12 | obj.height = 0 13 | obj.frame = 1 -- 当前帧 14 | obj.enable = 1 -- 控件是否可用 15 | obj.child = {} -- 子控件(派生类必须重新定义,否则将共用此表,也就是表结构无法继承复制) 16 | obj.nChild = 0 -- 子控件数量 17 | obj.mx = 0 18 | obj.my = 0 19 | setmetatable(obj, self) -- 新建对象 20 | self.__index = self -- 设置基类(元表)的__index为自身,使得派生表可访问自身成员 21 | return obj 22 | end 23 | 24 | function UI:Update() -- 对外界更新,成员函数 25 | if self.enable == 1 then 26 | self:Change() 27 | self:Draw() 28 | end 29 | end 30 | 31 | function UI:Draw() 32 | Painter.DrawImageByFrame(self.imageID, self.x, self.y, (self.frame - 1) * self.width, self.width) 33 | end 34 | 35 | function UI:Change() 36 | 37 | end 38 | 39 | function UI:OnLButtonDown(x, y) 40 | for k, v in pairs(self.child) do 41 | v:OnLButtonDown(x, y) 42 | end 43 | if IN_BOX(g_mouseX, g_mouseY, self.x, self.y, self.width, self.height) then 44 | self.mDown = 1 45 | end 46 | end 47 | 48 | function UI:OnLButtonUp(x, y) 49 | for k, v in pairs(self.child) do 50 | v:OnLButtonUp(x, y) 51 | end 52 | self.mDown = 0 53 | end 54 | 55 | function UI:OnRButtonDown(x, y) 56 | for k, v in pairs(self.child) do 57 | v:OnRButtonDown(x, y) 58 | end 59 | end 60 | 61 | function UI:AddChild(obj) 62 | self.nChild = self.nChild + 1 63 | self.child[self.nChild] = obj 64 | end 65 | -------------------------------------------------------------------------------- /usage/GameLive/World.lua: -------------------------------------------------------------------------------- 1 | -- 运行时对象容器,世界管理器 2 | World = {} 3 | 4 | function World:New() 5 | local obj = {} 6 | obj.noumena = {} -- 世界中的实体集合 7 | obj.maxID = 0 8 | setmetatable(obj, self) 9 | self.__index = self 10 | return obj 11 | end 12 | 13 | function World:Add(obj) 14 | self.maxID = self.maxID + 1 15 | table.insert(self.noumena, self.maxID, obj) 16 | obj.ID = self.maxID 17 | return obj.ID -- 有些时候外界需要知道对象ID 18 | end 19 | 20 | function World:Delete(id) 21 | table.remove(self.noumena, id) 22 | self.maxID = self.maxID - 1 23 | end 24 | 25 | function World:Clear() 26 | for index = 1, self.maxID do 27 | self.noumena:remove(index) -- 等待垃圾回收处理 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /usage/GamePainter.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GamePainter.exe -------------------------------------------------------------------------------- /usage/GameRes/Image/BearL.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/BearL.png -------------------------------------------------------------------------------- /usage/GameRes/Image/BearR.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/BearR.png -------------------------------------------------------------------------------- /usage/GameRes/Image/Box1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/Box1.png -------------------------------------------------------------------------------- /usage/GameRes/Image/ClothSaler.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/ClothSaler.PNG -------------------------------------------------------------------------------- /usage/GameRes/Image/FoodSaler.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/FoodSaler.PNG -------------------------------------------------------------------------------- /usage/GameRes/Image/HeroRDL.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/HeroRDL.png -------------------------------------------------------------------------------- /usage/GameRes/Image/HeroRDR.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/HeroRDR.png -------------------------------------------------------------------------------- /usage/GameRes/Image/HeroRUL.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/HeroRUL.png -------------------------------------------------------------------------------- /usage/GameRes/Image/HeroRUR.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/HeroRUR.png -------------------------------------------------------------------------------- /usage/GameRes/Image/MumuL.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/MumuL.png -------------------------------------------------------------------------------- /usage/GameRes/Image/MumuR.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/MumuR.png -------------------------------------------------------------------------------- /usage/GameRes/Image/Saying.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/Saying.png -------------------------------------------------------------------------------- /usage/GameRes/Image/ToolBoard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/ToolBoard.png -------------------------------------------------------------------------------- /usage/GameRes/Image/button1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/button1.png -------------------------------------------------------------------------------- /usage/GameRes/Image/heroL.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/heroL.png -------------------------------------------------------------------------------- /usage/GameRes/Image/heroLS.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/heroLS.png -------------------------------------------------------------------------------- /usage/GameRes/Image/heroR.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/heroR.png -------------------------------------------------------------------------------- /usage/GameRes/Image/heroRD.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/heroRD.png -------------------------------------------------------------------------------- /usage/GameRes/Image/heroRS.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/heroRS.png -------------------------------------------------------------------------------- /usage/GameRes/Image/heroRU.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/heroRU.png -------------------------------------------------------------------------------- /usage/GameRes/Image/numbers1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/numbers1.png -------------------------------------------------------------------------------- /usage/GameRes/Image/numbers2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/numbers2.png -------------------------------------------------------------------------------- /usage/GameRes/Image/武器.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/武器.png -------------------------------------------------------------------------------- /usage/GameRes/Image/武器run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/武器run.png -------------------------------------------------------------------------------- /usage/GameRes/Image/长安城.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/长安城.jpg -------------------------------------------------------------------------------- /usage/GameRes/Image/长安城b.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/长安城b.jpg -------------------------------------------------------------------------------- /usage/GameRes/Image/长安城c.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/Image/长安城c.jpg -------------------------------------------------------------------------------- /usage/GameRes/sound/summer.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/GameRes/sound/summer.mp3 -------------------------------------------------------------------------------- /usage/效果图1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/效果图1.jpg -------------------------------------------------------------------------------- /usage/效果图2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LikeUSummer/GamePainter/399d1820f499231dcd3715e8cf8cab03e89a9471/usage/效果图2.jpg --------------------------------------------------------------------------------