Move Lua to source directory.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/source/lua/Lua.dsp Wed Nov 04 00:31:14 2009 +0000
1.3 @@ -0,0 +1,296 @@
1.4 +# Microsoft Developer Studio Project File - Name="Lua" - Package Owner=<4>
1.5 +# Microsoft Developer Studio Generated Build File, Format Version 6.00
1.6 +# ** DO NOT EDIT **
1.7 +
1.8 +# TARGTYPE "Win32 (x86) Static Library" 0x0104
1.9 +
1.10 +CFG=Lua - Win32 Debug
1.11 +!MESSAGE This is not a valid makefile. To build this project using NMAKE,
1.12 +!MESSAGE use the Export Makefile command and run
1.13 +!MESSAGE
1.14 +!MESSAGE NMAKE /f "Lua.mak".
1.15 +!MESSAGE
1.16 +!MESSAGE You can specify a configuration when running NMAKE
1.17 +!MESSAGE by defining the macro CFG on the command line. For example:
1.18 +!MESSAGE
1.19 +!MESSAGE NMAKE /f "Lua.mak" CFG="Lua - Win32 Debug"
1.20 +!MESSAGE
1.21 +!MESSAGE Possible choices for configuration are:
1.22 +!MESSAGE
1.23 +!MESSAGE "Lua - Win32 Release" (based on "Win32 (x86) Static Library")
1.24 +!MESSAGE "Lua - Win32 Debug" (based on "Win32 (x86) Static Library")
1.25 +!MESSAGE
1.26 +
1.27 +# Begin Project
1.28 +# PROP AllowPerConfigDependencies 0
1.29 +# PROP Scc_ProjName ""
1.30 +# PROP Scc_LocalPath ""
1.31 +CPP=cl.exe
1.32 +RSC=rc.exe
1.33 +
1.34 +!IF "$(CFG)" == "Lua - Win32 Release"
1.35 +
1.36 +# PROP BASE Use_MFC 0
1.37 +# PROP BASE Use_Debug_Libraries 0
1.38 +# PROP BASE Output_Dir "Release"
1.39 +# PROP BASE Intermediate_Dir "Release"
1.40 +# PROP BASE Target_Dir ""
1.41 +# PROP Use_MFC 0
1.42 +# PROP Use_Debug_Libraries 0
1.43 +# PROP Output_Dir "Release"
1.44 +# PROP Intermediate_Dir "Release"
1.45 +# PROP Target_Dir ""
1.46 +# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /YX /FD /c
1.47 +# ADD CPP /nologo /MT /W3 /GX /O2 /I "include" /D "NDEBUG" /D "WIN32" /D "_WINDOWS" /D "LUA_OPNAMES" /YX /FD /c
1.48 +# ADD BASE RSC /l 0x809 /d "NDEBUG"
1.49 +# ADD RSC /l 0x809 /d "NDEBUG"
1.50 +BSC32=bscmake.exe
1.51 +# ADD BASE BSC32 /nologo
1.52 +# ADD BSC32 /nologo
1.53 +LIB32=link.exe -lib
1.54 +# ADD BASE LIB32 /nologo
1.55 +# ADD LIB32 /nologo
1.56 +
1.57 +!ELSEIF "$(CFG)" == "Lua - Win32 Debug"
1.58 +
1.59 +# PROP BASE Use_MFC 0
1.60 +# PROP BASE Use_Debug_Libraries 1
1.61 +# PROP BASE Output_Dir "Debug"
1.62 +# PROP BASE Intermediate_Dir "Debug"
1.63 +# PROP BASE Target_Dir ""
1.64 +# PROP Use_MFC 0
1.65 +# PROP Use_Debug_Libraries 1
1.66 +# PROP Output_Dir "Debug"
1.67 +# PROP Intermediate_Dir "Debug"
1.68 +# PROP Target_Dir ""
1.69 +# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /YX /FD /GZ /c
1.70 +# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /I "include" /D "_DEBUG" /D "WIN32" /D "_WINDOWS" /D "LUA_OPNAMES" /YX /FD /GZ /c
1.71 +# ADD BASE RSC /l 0x809 /d "_DEBUG"
1.72 +# ADD RSC /l 0x809 /d "_DEBUG"
1.73 +BSC32=bscmake.exe
1.74 +# ADD BASE BSC32 /nologo
1.75 +# ADD BSC32 /nologo
1.76 +LIB32=link.exe -lib
1.77 +# ADD BASE LIB32 /nologo
1.78 +# ADD LIB32 /nologo
1.79 +
1.80 +!ENDIF
1.81 +
1.82 +# Begin Target
1.83 +
1.84 +# Name "Lua - Win32 Release"
1.85 +# Name "Lua - Win32 Debug"
1.86 +# Begin Group "Source Files"
1.87 +
1.88 +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
1.89 +# Begin Source File
1.90 +
1.91 +SOURCE=.\src\lapi.c
1.92 +# End Source File
1.93 +# Begin Source File
1.94 +
1.95 +SOURCE=.\src\lcode.c
1.96 +# End Source File
1.97 +# Begin Source File
1.98 +
1.99 +SOURCE=.\src\ldebug.c
1.100 +# End Source File
1.101 +# Begin Source File
1.102 +
1.103 +SOURCE=.\src\ldo.c
1.104 +# End Source File
1.105 +# Begin Source File
1.106 +
1.107 +SOURCE=.\src\ldump.c
1.108 +# End Source File
1.109 +# Begin Source File
1.110 +
1.111 +SOURCE=.\src\lfunc.c
1.112 +# End Source File
1.113 +# Begin Source File
1.114 +
1.115 +SOURCE=.\src\lgc.c
1.116 +# End Source File
1.117 +# Begin Source File
1.118 +
1.119 +SOURCE=.\src\llex.c
1.120 +# End Source File
1.121 +# Begin Source File
1.122 +
1.123 +SOURCE=.\src\lmem.c
1.124 +# End Source File
1.125 +# Begin Source File
1.126 +
1.127 +SOURCE=.\src\lobject.c
1.128 +# End Source File
1.129 +# Begin Source File
1.130 +
1.131 +SOURCE=.\src\lopcodes.c
1.132 +# End Source File
1.133 +# Begin Source File
1.134 +
1.135 +SOURCE=.\src\lparser.c
1.136 +# End Source File
1.137 +# Begin Source File
1.138 +
1.139 +SOURCE=.\src\lstate.c
1.140 +# End Source File
1.141 +# Begin Source File
1.142 +
1.143 +SOURCE=.\src\lstring.c
1.144 +# End Source File
1.145 +# Begin Source File
1.146 +
1.147 +SOURCE=.\src\ltable.c
1.148 +# End Source File
1.149 +# Begin Source File
1.150 +
1.151 +SOURCE=.\src\ltests.c
1.152 +# End Source File
1.153 +# Begin Source File
1.154 +
1.155 +SOURCE=.\src\ltm.c
1.156 +# End Source File
1.157 +# Begin Source File
1.158 +
1.159 +SOURCE=.\src\lundump.c
1.160 +# End Source File
1.161 +# Begin Source File
1.162 +
1.163 +SOURCE=.\src\lvm.c
1.164 +# End Source File
1.165 +# Begin Source File
1.166 +
1.167 +SOURCE=.\src\lzio.c
1.168 +# End Source File
1.169 +# End Group
1.170 +# Begin Group "Header Files"
1.171 +
1.172 +# PROP Default_Filter "h;hpp;hxx;hm;inl"
1.173 +# Begin Source File
1.174 +
1.175 +SOURCE=.\src\lapi.h
1.176 +# End Source File
1.177 +# Begin Source File
1.178 +
1.179 +SOURCE=.\src\lcode.h
1.180 +# End Source File
1.181 +# Begin Source File
1.182 +
1.183 +SOURCE=.\src\ldebug.h
1.184 +# End Source File
1.185 +# Begin Source File
1.186 +
1.187 +SOURCE=.\src\ldo.h
1.188 +# End Source File
1.189 +# Begin Source File
1.190 +
1.191 +SOURCE=.\src\lfunc.h
1.192 +# End Source File
1.193 +# Begin Source File
1.194 +
1.195 +SOURCE=.\src\lgc.h
1.196 +# End Source File
1.197 +# Begin Source File
1.198 +
1.199 +SOURCE=.\src\llex.h
1.200 +# End Source File
1.201 +# Begin Source File
1.202 +
1.203 +SOURCE=.\src\llimits.h
1.204 +# End Source File
1.205 +# Begin Source File
1.206 +
1.207 +SOURCE=.\src\lmem.h
1.208 +# End Source File
1.209 +# Begin Source File
1.210 +
1.211 +SOURCE=.\src\lobject.h
1.212 +# End Source File
1.213 +# Begin Source File
1.214 +
1.215 +SOURCE=.\src\lopcodes.h
1.216 +# End Source File
1.217 +# Begin Source File
1.218 +
1.219 +SOURCE=.\src\lparser.h
1.220 +# End Source File
1.221 +# Begin Source File
1.222 +
1.223 +SOURCE=.\src\lstate.h
1.224 +# End Source File
1.225 +# Begin Source File
1.226 +
1.227 +SOURCE=.\src\lstring.h
1.228 +# End Source File
1.229 +# Begin Source File
1.230 +
1.231 +SOURCE=.\src\ltable.h
1.232 +# End Source File
1.233 +# Begin Source File
1.234 +
1.235 +SOURCE=.\src\ltm.h
1.236 +# End Source File
1.237 +# Begin Source File
1.238 +
1.239 +SOURCE=.\src\lundump.h
1.240 +# End Source File
1.241 +# Begin Source File
1.242 +
1.243 +SOURCE=.\src\lvm.h
1.244 +# End Source File
1.245 +# Begin Source File
1.246 +
1.247 +SOURCE=.\src\lzio.h
1.248 +# End Source File
1.249 +# End Group
1.250 +# Begin Group "Lua Libs"
1.251 +
1.252 +# PROP Default_Filter "*.c;*.h"
1.253 +# Begin Source File
1.254 +
1.255 +SOURCE=.\src\lib\lauxlib.c
1.256 +# End Source File
1.257 +# Begin Source File
1.258 +
1.259 +SOURCE=.\src\lib\lbaselib.c
1.260 +# End Source File
1.261 +# Begin Source File
1.262 +
1.263 +SOURCE=.\src\lib\ldblib.c
1.264 +# End Source File
1.265 +# Begin Source File
1.266 +
1.267 +SOURCE=.\src\lib\liolib.c
1.268 +# End Source File
1.269 +# Begin Source File
1.270 +
1.271 +SOURCE=.\src\lib\lmathlib.c
1.272 +# End Source File
1.273 +# Begin Source File
1.274 +
1.275 +SOURCE=.\src\lib\lstrlib.c
1.276 +# End Source File
1.277 +# Begin Source File
1.278 +
1.279 +SOURCE=.\src\lib\ltablib.c
1.280 +# End Source File
1.281 +# End Group
1.282 +# Begin Group "Interface"
1.283 +
1.284 +# PROP Default_Filter "*.h"
1.285 +# Begin Source File
1.286 +
1.287 +SOURCE=.\include\lauxlib.h
1.288 +# End Source File
1.289 +# Begin Source File
1.290 +
1.291 +SOURCE=.\include\lua.h
1.292 +# End Source File
1.293 +# Begin Source File
1.294 +
1.295 +SOURCE=.\include\lualib.h
1.296 +# End Source File
1.297 +# End Group
1.298 +# End Target
1.299 +# End Project
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/source/lua/Lua.vcproj Wed Nov 04 00:31:14 2009 +0000
2.3 @@ -0,0 +1,743 @@
2.4 +<?xml version="1.0" encoding="Windows-1252"?>
2.5 +<VisualStudioProject
2.6 + ProjectType="Visual C++"
2.7 + Version="7.10"
2.8 + Name="Lua"
2.9 + SccProjectName=""
2.10 + SccLocalPath="">
2.11 + <Platforms>
2.12 + <Platform
2.13 + Name="Win32"/>
2.14 + </Platforms>
2.15 + <Configurations>
2.16 + <Configuration
2.17 + Name="Release|Win32"
2.18 + OutputDirectory=".\Release"
2.19 + IntermediateDirectory=".\Release"
2.20 + ConfigurationType="4"
2.21 + UseOfMFC="0"
2.22 + ATLMinimizesCRunTimeLibraryUsage="FALSE">
2.23 + <Tool
2.24 + Name="VCCLCompilerTool"
2.25 + Optimization="2"
2.26 + InlineFunctionExpansion="1"
2.27 + AdditionalIncludeDirectories="include"
2.28 + PreprocessorDefinitions="NDEBUG;WIN32;_WINDOWS;LUA_OPNAMES"
2.29 + StringPooling="TRUE"
2.30 + RuntimeLibrary="4"
2.31 + EnableFunctionLevelLinking="TRUE"
2.32 + UsePrecompiledHeader="2"
2.33 + PrecompiledHeaderFile=".\Release/Lua.pch"
2.34 + AssemblerListingLocation=".\Release/"
2.35 + ObjectFile=".\Release/"
2.36 + ProgramDataBaseFileName=".\Release/"
2.37 + WarningLevel="3"
2.38 + SuppressStartupBanner="TRUE"
2.39 + CompileAs="0"/>
2.40 + <Tool
2.41 + Name="VCCustomBuildTool"/>
2.42 + <Tool
2.43 + Name="VCLibrarianTool"
2.44 + OutputFile=".\Release\Lua.lib"
2.45 + SuppressStartupBanner="TRUE"/>
2.46 + <Tool
2.47 + Name="VCMIDLTool"/>
2.48 + <Tool
2.49 + Name="VCPostBuildEventTool"/>
2.50 + <Tool
2.51 + Name="VCPreBuildEventTool"/>
2.52 + <Tool
2.53 + Name="VCPreLinkEventTool"/>
2.54 + <Tool
2.55 + Name="VCResourceCompilerTool"
2.56 + PreprocessorDefinitions="NDEBUG"
2.57 + Culture="2057"/>
2.58 + <Tool
2.59 + Name="VCWebServiceProxyGeneratorTool"/>
2.60 + <Tool
2.61 + Name="VCXMLDataGeneratorTool"/>
2.62 + <Tool
2.63 + Name="VCManagedWrapperGeneratorTool"/>
2.64 + <Tool
2.65 + Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
2.66 + </Configuration>
2.67 + <Configuration
2.68 + Name="Debug|Win32"
2.69 + OutputDirectory=".\Debug"
2.70 + IntermediateDirectory=".\Debug"
2.71 + ConfigurationType="4"
2.72 + UseOfMFC="0"
2.73 + ATLMinimizesCRunTimeLibraryUsage="FALSE">
2.74 + <Tool
2.75 + Name="VCCLCompilerTool"
2.76 + Optimization="0"
2.77 + AdditionalIncludeDirectories="include"
2.78 + PreprocessorDefinitions="_DEBUG;WIN32;_WINDOWS;LUA_OPNAMES"
2.79 + BasicRuntimeChecks="3"
2.80 + RuntimeLibrary="5"
2.81 + UsePrecompiledHeader="2"
2.82 + PrecompiledHeaderFile=".\Debug/Lua.pch"
2.83 + AssemblerListingLocation=".\Debug/"
2.84 + ObjectFile=".\Debug/"
2.85 + ProgramDataBaseFileName=".\Debug/"
2.86 + WarningLevel="3"
2.87 + SuppressStartupBanner="TRUE"
2.88 + DebugInformationFormat="4"
2.89 + CompileAs="0"/>
2.90 + <Tool
2.91 + Name="VCCustomBuildTool"/>
2.92 + <Tool
2.93 + Name="VCLibrarianTool"
2.94 + OutputFile=".\Debug\Lua.lib"
2.95 + SuppressStartupBanner="TRUE"/>
2.96 + <Tool
2.97 + Name="VCMIDLTool"/>
2.98 + <Tool
2.99 + Name="VCPostBuildEventTool"/>
2.100 + <Tool
2.101 + Name="VCPreBuildEventTool"/>
2.102 + <Tool
2.103 + Name="VCPreLinkEventTool"/>
2.104 + <Tool
2.105 + Name="VCResourceCompilerTool"
2.106 + PreprocessorDefinitions="_DEBUG"
2.107 + Culture="2057"/>
2.108 + <Tool
2.109 + Name="VCWebServiceProxyGeneratorTool"/>
2.110 + <Tool
2.111 + Name="VCXMLDataGeneratorTool"/>
2.112 + <Tool
2.113 + Name="VCManagedWrapperGeneratorTool"/>
2.114 + <Tool
2.115 + Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
2.116 + </Configuration>
2.117 + </Configurations>
2.118 + <References>
2.119 + </References>
2.120 + <Files>
2.121 + <Filter
2.122 + Name="Source Files"
2.123 + Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat">
2.124 + <File
2.125 + RelativePath="src\lapi.c">
2.126 + <FileConfiguration
2.127 + Name="Release|Win32">
2.128 + <Tool
2.129 + Name="VCCLCompilerTool"
2.130 + Optimization="2"
2.131 + AdditionalIncludeDirectories=""
2.132 + PreprocessorDefinitions=""/>
2.133 + </FileConfiguration>
2.134 + <FileConfiguration
2.135 + Name="Debug|Win32">
2.136 + <Tool
2.137 + Name="VCCLCompilerTool"
2.138 + Optimization="0"
2.139 + AdditionalIncludeDirectories=""
2.140 + PreprocessorDefinitions=""
2.141 + BasicRuntimeChecks="3"/>
2.142 + </FileConfiguration>
2.143 + </File>
2.144 + <File
2.145 + RelativePath="src\lcode.c">
2.146 + <FileConfiguration
2.147 + Name="Release|Win32">
2.148 + <Tool
2.149 + Name="VCCLCompilerTool"
2.150 + Optimization="2"
2.151 + AdditionalIncludeDirectories=""
2.152 + PreprocessorDefinitions=""/>
2.153 + </FileConfiguration>
2.154 + <FileConfiguration
2.155 + Name="Debug|Win32">
2.156 + <Tool
2.157 + Name="VCCLCompilerTool"
2.158 + Optimization="0"
2.159 + AdditionalIncludeDirectories=""
2.160 + PreprocessorDefinitions=""
2.161 + BasicRuntimeChecks="3"/>
2.162 + </FileConfiguration>
2.163 + </File>
2.164 + <File
2.165 + RelativePath="src\ldebug.c">
2.166 + <FileConfiguration
2.167 + Name="Release|Win32">
2.168 + <Tool
2.169 + Name="VCCLCompilerTool"
2.170 + Optimization="2"
2.171 + AdditionalIncludeDirectories=""
2.172 + PreprocessorDefinitions=""/>
2.173 + </FileConfiguration>
2.174 + <FileConfiguration
2.175 + Name="Debug|Win32">
2.176 + <Tool
2.177 + Name="VCCLCompilerTool"
2.178 + Optimization="0"
2.179 + AdditionalIncludeDirectories=""
2.180 + PreprocessorDefinitions=""
2.181 + BasicRuntimeChecks="3"/>
2.182 + </FileConfiguration>
2.183 + </File>
2.184 + <File
2.185 + RelativePath="src\ldo.c">
2.186 + <FileConfiguration
2.187 + Name="Release|Win32">
2.188 + <Tool
2.189 + Name="VCCLCompilerTool"
2.190 + Optimization="2"
2.191 + AdditionalIncludeDirectories=""
2.192 + PreprocessorDefinitions=""/>
2.193 + </FileConfiguration>
2.194 + <FileConfiguration
2.195 + Name="Debug|Win32">
2.196 + <Tool
2.197 + Name="VCCLCompilerTool"
2.198 + Optimization="0"
2.199 + AdditionalIncludeDirectories=""
2.200 + PreprocessorDefinitions=""
2.201 + BasicRuntimeChecks="3"/>
2.202 + </FileConfiguration>
2.203 + </File>
2.204 + <File
2.205 + RelativePath="src\ldump.c">
2.206 + <FileConfiguration
2.207 + Name="Release|Win32">
2.208 + <Tool
2.209 + Name="VCCLCompilerTool"
2.210 + Optimization="2"
2.211 + AdditionalIncludeDirectories=""
2.212 + PreprocessorDefinitions=""/>
2.213 + </FileConfiguration>
2.214 + <FileConfiguration
2.215 + Name="Debug|Win32">
2.216 + <Tool
2.217 + Name="VCCLCompilerTool"
2.218 + Optimization="0"
2.219 + AdditionalIncludeDirectories=""
2.220 + PreprocessorDefinitions=""
2.221 + BasicRuntimeChecks="3"/>
2.222 + </FileConfiguration>
2.223 + </File>
2.224 + <File
2.225 + RelativePath="src\lfunc.c">
2.226 + <FileConfiguration
2.227 + Name="Release|Win32">
2.228 + <Tool
2.229 + Name="VCCLCompilerTool"
2.230 + Optimization="2"
2.231 + AdditionalIncludeDirectories=""
2.232 + PreprocessorDefinitions=""/>
2.233 + </FileConfiguration>
2.234 + <FileConfiguration
2.235 + Name="Debug|Win32">
2.236 + <Tool
2.237 + Name="VCCLCompilerTool"
2.238 + Optimization="0"
2.239 + AdditionalIncludeDirectories=""
2.240 + PreprocessorDefinitions=""
2.241 + BasicRuntimeChecks="3"/>
2.242 + </FileConfiguration>
2.243 + </File>
2.244 + <File
2.245 + RelativePath="src\lgc.c">
2.246 + <FileConfiguration
2.247 + Name="Release|Win32">
2.248 + <Tool
2.249 + Name="VCCLCompilerTool"
2.250 + Optimization="2"
2.251 + AdditionalIncludeDirectories=""
2.252 + PreprocessorDefinitions=""/>
2.253 + </FileConfiguration>
2.254 + <FileConfiguration
2.255 + Name="Debug|Win32">
2.256 + <Tool
2.257 + Name="VCCLCompilerTool"
2.258 + Optimization="0"
2.259 + AdditionalIncludeDirectories=""
2.260 + PreprocessorDefinitions=""
2.261 + BasicRuntimeChecks="3"/>
2.262 + </FileConfiguration>
2.263 + </File>
2.264 + <File
2.265 + RelativePath="src\llex.c">
2.266 + <FileConfiguration
2.267 + Name="Release|Win32">
2.268 + <Tool
2.269 + Name="VCCLCompilerTool"
2.270 + Optimization="2"
2.271 + AdditionalIncludeDirectories=""
2.272 + PreprocessorDefinitions=""/>
2.273 + </FileConfiguration>
2.274 + <FileConfiguration
2.275 + Name="Debug|Win32">
2.276 + <Tool
2.277 + Name="VCCLCompilerTool"
2.278 + Optimization="0"
2.279 + AdditionalIncludeDirectories=""
2.280 + PreprocessorDefinitions=""
2.281 + BasicRuntimeChecks="3"/>
2.282 + </FileConfiguration>
2.283 + </File>
2.284 + <File
2.285 + RelativePath="src\lmem.c">
2.286 + <FileConfiguration
2.287 + Name="Release|Win32">
2.288 + <Tool
2.289 + Name="VCCLCompilerTool"
2.290 + Optimization="2"
2.291 + AdditionalIncludeDirectories=""
2.292 + PreprocessorDefinitions=""/>
2.293 + </FileConfiguration>
2.294 + <FileConfiguration
2.295 + Name="Debug|Win32">
2.296 + <Tool
2.297 + Name="VCCLCompilerTool"
2.298 + Optimization="0"
2.299 + AdditionalIncludeDirectories=""
2.300 + PreprocessorDefinitions=""
2.301 + BasicRuntimeChecks="3"/>
2.302 + </FileConfiguration>
2.303 + </File>
2.304 + <File
2.305 + RelativePath="src\lobject.c">
2.306 + <FileConfiguration
2.307 + Name="Release|Win32">
2.308 + <Tool
2.309 + Name="VCCLCompilerTool"
2.310 + Optimization="2"
2.311 + AdditionalIncludeDirectories=""
2.312 + PreprocessorDefinitions=""/>
2.313 + </FileConfiguration>
2.314 + <FileConfiguration
2.315 + Name="Debug|Win32">
2.316 + <Tool
2.317 + Name="VCCLCompilerTool"
2.318 + Optimization="0"
2.319 + AdditionalIncludeDirectories=""
2.320 + PreprocessorDefinitions=""
2.321 + BasicRuntimeChecks="3"/>
2.322 + </FileConfiguration>
2.323 + </File>
2.324 + <File
2.325 + RelativePath="src\lopcodes.c">
2.326 + <FileConfiguration
2.327 + Name="Release|Win32">
2.328 + <Tool
2.329 + Name="VCCLCompilerTool"
2.330 + Optimization="2"
2.331 + AdditionalIncludeDirectories=""
2.332 + PreprocessorDefinitions=""/>
2.333 + </FileConfiguration>
2.334 + <FileConfiguration
2.335 + Name="Debug|Win32">
2.336 + <Tool
2.337 + Name="VCCLCompilerTool"
2.338 + Optimization="0"
2.339 + AdditionalIncludeDirectories=""
2.340 + PreprocessorDefinitions=""
2.341 + BasicRuntimeChecks="3"/>
2.342 + </FileConfiguration>
2.343 + </File>
2.344 + <File
2.345 + RelativePath="src\lparser.c">
2.346 + <FileConfiguration
2.347 + Name="Release|Win32">
2.348 + <Tool
2.349 + Name="VCCLCompilerTool"
2.350 + Optimization="2"
2.351 + AdditionalIncludeDirectories=""
2.352 + PreprocessorDefinitions=""/>
2.353 + </FileConfiguration>
2.354 + <FileConfiguration
2.355 + Name="Debug|Win32">
2.356 + <Tool
2.357 + Name="VCCLCompilerTool"
2.358 + Optimization="0"
2.359 + AdditionalIncludeDirectories=""
2.360 + PreprocessorDefinitions=""
2.361 + BasicRuntimeChecks="3"/>
2.362 + </FileConfiguration>
2.363 + </File>
2.364 + <File
2.365 + RelativePath="src\lstate.c">
2.366 + <FileConfiguration
2.367 + Name="Release|Win32">
2.368 + <Tool
2.369 + Name="VCCLCompilerTool"
2.370 + Optimization="2"
2.371 + AdditionalIncludeDirectories=""
2.372 + PreprocessorDefinitions=""/>
2.373 + </FileConfiguration>
2.374 + <FileConfiguration
2.375 + Name="Debug|Win32">
2.376 + <Tool
2.377 + Name="VCCLCompilerTool"
2.378 + Optimization="0"
2.379 + AdditionalIncludeDirectories=""
2.380 + PreprocessorDefinitions=""
2.381 + BasicRuntimeChecks="3"/>
2.382 + </FileConfiguration>
2.383 + </File>
2.384 + <File
2.385 + RelativePath="src\lstring.c">
2.386 + <FileConfiguration
2.387 + Name="Release|Win32">
2.388 + <Tool
2.389 + Name="VCCLCompilerTool"
2.390 + Optimization="2"
2.391 + AdditionalIncludeDirectories=""
2.392 + PreprocessorDefinitions=""/>
2.393 + </FileConfiguration>
2.394 + <FileConfiguration
2.395 + Name="Debug|Win32">
2.396 + <Tool
2.397 + Name="VCCLCompilerTool"
2.398 + Optimization="0"
2.399 + AdditionalIncludeDirectories=""
2.400 + PreprocessorDefinitions=""
2.401 + BasicRuntimeChecks="3"/>
2.402 + </FileConfiguration>
2.403 + </File>
2.404 + <File
2.405 + RelativePath="src\ltable.c">
2.406 + <FileConfiguration
2.407 + Name="Release|Win32">
2.408 + <Tool
2.409 + Name="VCCLCompilerTool"
2.410 + Optimization="2"
2.411 + AdditionalIncludeDirectories=""
2.412 + PreprocessorDefinitions=""/>
2.413 + </FileConfiguration>
2.414 + <FileConfiguration
2.415 + Name="Debug|Win32">
2.416 + <Tool
2.417 + Name="VCCLCompilerTool"
2.418 + Optimization="0"
2.419 + AdditionalIncludeDirectories=""
2.420 + PreprocessorDefinitions=""
2.421 + BasicRuntimeChecks="3"/>
2.422 + </FileConfiguration>
2.423 + </File>
2.424 + <File
2.425 + RelativePath="src\ltests.c">
2.426 + <FileConfiguration
2.427 + Name="Release|Win32">
2.428 + <Tool
2.429 + Name="VCCLCompilerTool"
2.430 + Optimization="2"
2.431 + AdditionalIncludeDirectories=""
2.432 + PreprocessorDefinitions=""/>
2.433 + </FileConfiguration>
2.434 + <FileConfiguration
2.435 + Name="Debug|Win32">
2.436 + <Tool
2.437 + Name="VCCLCompilerTool"
2.438 + Optimization="0"
2.439 + AdditionalIncludeDirectories=""
2.440 + PreprocessorDefinitions=""
2.441 + BasicRuntimeChecks="3"/>
2.442 + </FileConfiguration>
2.443 + </File>
2.444 + <File
2.445 + RelativePath="src\ltm.c">
2.446 + <FileConfiguration
2.447 + Name="Release|Win32">
2.448 + <Tool
2.449 + Name="VCCLCompilerTool"
2.450 + Optimization="2"
2.451 + AdditionalIncludeDirectories=""
2.452 + PreprocessorDefinitions=""/>
2.453 + </FileConfiguration>
2.454 + <FileConfiguration
2.455 + Name="Debug|Win32">
2.456 + <Tool
2.457 + Name="VCCLCompilerTool"
2.458 + Optimization="0"
2.459 + AdditionalIncludeDirectories=""
2.460 + PreprocessorDefinitions=""
2.461 + BasicRuntimeChecks="3"/>
2.462 + </FileConfiguration>
2.463 + </File>
2.464 + <File
2.465 + RelativePath="src\lundump.c">
2.466 + <FileConfiguration
2.467 + Name="Release|Win32">
2.468 + <Tool
2.469 + Name="VCCLCompilerTool"
2.470 + Optimization="2"
2.471 + AdditionalIncludeDirectories=""
2.472 + PreprocessorDefinitions=""/>
2.473 + </FileConfiguration>
2.474 + <FileConfiguration
2.475 + Name="Debug|Win32">
2.476 + <Tool
2.477 + Name="VCCLCompilerTool"
2.478 + Optimization="0"
2.479 + AdditionalIncludeDirectories=""
2.480 + PreprocessorDefinitions=""
2.481 + BasicRuntimeChecks="3"/>
2.482 + </FileConfiguration>
2.483 + </File>
2.484 + <File
2.485 + RelativePath="src\lvm.c">
2.486 + <FileConfiguration
2.487 + Name="Release|Win32">
2.488 + <Tool
2.489 + Name="VCCLCompilerTool"
2.490 + Optimization="2"
2.491 + AdditionalIncludeDirectories=""
2.492 + PreprocessorDefinitions=""/>
2.493 + </FileConfiguration>
2.494 + <FileConfiguration
2.495 + Name="Debug|Win32">
2.496 + <Tool
2.497 + Name="VCCLCompilerTool"
2.498 + Optimization="0"
2.499 + AdditionalIncludeDirectories=""
2.500 + PreprocessorDefinitions=""
2.501 + BasicRuntimeChecks="3"/>
2.502 + </FileConfiguration>
2.503 + </File>
2.504 + <File
2.505 + RelativePath="src\lzio.c">
2.506 + <FileConfiguration
2.507 + Name="Release|Win32">
2.508 + <Tool
2.509 + Name="VCCLCompilerTool"
2.510 + Optimization="2"
2.511 + AdditionalIncludeDirectories=""
2.512 + PreprocessorDefinitions=""/>
2.513 + </FileConfiguration>
2.514 + <FileConfiguration
2.515 + Name="Debug|Win32">
2.516 + <Tool
2.517 + Name="VCCLCompilerTool"
2.518 + Optimization="0"
2.519 + AdditionalIncludeDirectories=""
2.520 + PreprocessorDefinitions=""
2.521 + BasicRuntimeChecks="3"/>
2.522 + </FileConfiguration>
2.523 + </File>
2.524 + </Filter>
2.525 + <Filter
2.526 + Name="Header Files"
2.527 + Filter="h;hpp;hxx;hm;inl">
2.528 + <File
2.529 + RelativePath="src\lapi.h">
2.530 + </File>
2.531 + <File
2.532 + RelativePath="src\lcode.h">
2.533 + </File>
2.534 + <File
2.535 + RelativePath="src\ldebug.h">
2.536 + </File>
2.537 + <File
2.538 + RelativePath="src\ldo.h">
2.539 + </File>
2.540 + <File
2.541 + RelativePath="src\lfunc.h">
2.542 + </File>
2.543 + <File
2.544 + RelativePath="src\lgc.h">
2.545 + </File>
2.546 + <File
2.547 + RelativePath="src\llex.h">
2.548 + </File>
2.549 + <File
2.550 + RelativePath="src\llimits.h">
2.551 + </File>
2.552 + <File
2.553 + RelativePath="src\lmem.h">
2.554 + </File>
2.555 + <File
2.556 + RelativePath="src\lobject.h">
2.557 + </File>
2.558 + <File
2.559 + RelativePath="src\lopcodes.h">
2.560 + </File>
2.561 + <File
2.562 + RelativePath="src\lparser.h">
2.563 + </File>
2.564 + <File
2.565 + RelativePath="src\lstate.h">
2.566 + </File>
2.567 + <File
2.568 + RelativePath="src\lstring.h">
2.569 + </File>
2.570 + <File
2.571 + RelativePath="src\ltable.h">
2.572 + </File>
2.573 + <File
2.574 + RelativePath="src\ltm.h">
2.575 + </File>
2.576 + <File
2.577 + RelativePath="src\lundump.h">
2.578 + </File>
2.579 + <File
2.580 + RelativePath="src\lvm.h">
2.581 + </File>
2.582 + <File
2.583 + RelativePath="src\lzio.h">
2.584 + </File>
2.585 + </Filter>
2.586 + <Filter
2.587 + Name="Lua Libs"
2.588 + Filter="*.c;*.h">
2.589 + <File
2.590 + RelativePath="src\lib\lauxlib.c">
2.591 + <FileConfiguration
2.592 + Name="Release|Win32">
2.593 + <Tool
2.594 + Name="VCCLCompilerTool"
2.595 + Optimization="2"
2.596 + AdditionalIncludeDirectories=""
2.597 + PreprocessorDefinitions=""/>
2.598 + </FileConfiguration>
2.599 + <FileConfiguration
2.600 + Name="Debug|Win32">
2.601 + <Tool
2.602 + Name="VCCLCompilerTool"
2.603 + Optimization="0"
2.604 + AdditionalIncludeDirectories=""
2.605 + PreprocessorDefinitions=""
2.606 + BasicRuntimeChecks="3"/>
2.607 + </FileConfiguration>
2.608 + </File>
2.609 + <File
2.610 + RelativePath="src\lib\lbaselib.c">
2.611 + <FileConfiguration
2.612 + Name="Release|Win32">
2.613 + <Tool
2.614 + Name="VCCLCompilerTool"
2.615 + Optimization="2"
2.616 + AdditionalIncludeDirectories=""
2.617 + PreprocessorDefinitions=""/>
2.618 + </FileConfiguration>
2.619 + <FileConfiguration
2.620 + Name="Debug|Win32">
2.621 + <Tool
2.622 + Name="VCCLCompilerTool"
2.623 + Optimization="0"
2.624 + AdditionalIncludeDirectories=""
2.625 + PreprocessorDefinitions=""
2.626 + BasicRuntimeChecks="3"/>
2.627 + </FileConfiguration>
2.628 + </File>
2.629 + <File
2.630 + RelativePath="src\lib\ldblib.c">
2.631 + <FileConfiguration
2.632 + Name="Release|Win32">
2.633 + <Tool
2.634 + Name="VCCLCompilerTool"
2.635 + Optimization="2"
2.636 + AdditionalIncludeDirectories=""
2.637 + PreprocessorDefinitions=""/>
2.638 + </FileConfiguration>
2.639 + <FileConfiguration
2.640 + Name="Debug|Win32">
2.641 + <Tool
2.642 + Name="VCCLCompilerTool"
2.643 + Optimization="0"
2.644 + AdditionalIncludeDirectories=""
2.645 + PreprocessorDefinitions=""
2.646 + BasicRuntimeChecks="3"/>
2.647 + </FileConfiguration>
2.648 + </File>
2.649 + <File
2.650 + RelativePath="src\lib\liolib.c">
2.651 + <FileConfiguration
2.652 + Name="Release|Win32">
2.653 + <Tool
2.654 + Name="VCCLCompilerTool"
2.655 + Optimization="2"
2.656 + AdditionalIncludeDirectories=""
2.657 + PreprocessorDefinitions=""/>
2.658 + </FileConfiguration>
2.659 + <FileConfiguration
2.660 + Name="Debug|Win32">
2.661 + <Tool
2.662 + Name="VCCLCompilerTool"
2.663 + Optimization="0"
2.664 + AdditionalIncludeDirectories=""
2.665 + PreprocessorDefinitions=""
2.666 + BasicRuntimeChecks="3"/>
2.667 + </FileConfiguration>
2.668 + </File>
2.669 + <File
2.670 + RelativePath="src\lib\lmathlib.c">
2.671 + <FileConfiguration
2.672 + Name="Release|Win32">
2.673 + <Tool
2.674 + Name="VCCLCompilerTool"
2.675 + Optimization="2"
2.676 + AdditionalIncludeDirectories=""
2.677 + PreprocessorDefinitions=""/>
2.678 + </FileConfiguration>
2.679 + <FileConfiguration
2.680 + Name="Debug|Win32">
2.681 + <Tool
2.682 + Name="VCCLCompilerTool"
2.683 + Optimization="0"
2.684 + AdditionalIncludeDirectories=""
2.685 + PreprocessorDefinitions=""
2.686 + BasicRuntimeChecks="3"/>
2.687 + </FileConfiguration>
2.688 + </File>
2.689 + <File
2.690 + RelativePath="src\lib\lstrlib.c">
2.691 + <FileConfiguration
2.692 + Name="Release|Win32">
2.693 + <Tool
2.694 + Name="VCCLCompilerTool"
2.695 + Optimization="2"
2.696 + AdditionalIncludeDirectories=""
2.697 + PreprocessorDefinitions=""/>
2.698 + </FileConfiguration>
2.699 + <FileConfiguration
2.700 + Name="Debug|Win32">
2.701 + <Tool
2.702 + Name="VCCLCompilerTool"
2.703 + Optimization="0"
2.704 + AdditionalIncludeDirectories=""
2.705 + PreprocessorDefinitions=""
2.706 + BasicRuntimeChecks="3"/>
2.707 + </FileConfiguration>
2.708 + </File>
2.709 + <File
2.710 + RelativePath="src\lib\ltablib.c">
2.711 + <FileConfiguration
2.712 + Name="Release|Win32">
2.713 + <Tool
2.714 + Name="VCCLCompilerTool"
2.715 + Optimization="2"
2.716 + AdditionalIncludeDirectories=""
2.717 + PreprocessorDefinitions=""/>
2.718 + </FileConfiguration>
2.719 + <FileConfiguration
2.720 + Name="Debug|Win32">
2.721 + <Tool
2.722 + Name="VCCLCompilerTool"
2.723 + Optimization="0"
2.724 + AdditionalIncludeDirectories=""
2.725 + PreprocessorDefinitions=""
2.726 + BasicRuntimeChecks="3"/>
2.727 + </FileConfiguration>
2.728 + </File>
2.729 + </Filter>
2.730 + <Filter
2.731 + Name="Interface"
2.732 + Filter="*.h">
2.733 + <File
2.734 + RelativePath="include\lauxlib.h">
2.735 + </File>
2.736 + <File
2.737 + RelativePath="include\lua.h">
2.738 + </File>
2.739 + <File
2.740 + RelativePath="include\lualib.h">
2.741 + </File>
2.742 + </Filter>
2.743 + </Files>
2.744 + <Globals>
2.745 + </Globals>
2.746 +</VisualStudioProject>
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/source/lua/config Wed Nov 04 00:31:14 2009 +0000
3.3 @@ -0,0 +1,105 @@
3.4 +
3.5 +# This has been modified for Doris environment
3.6 +# $Id$
3.7 +
3.8 +# configuration file for making Lua
3.9 +# see INSTALL for installation instructions
3.10 +
3.11 +# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================
3.12 +
3.13 +# ------------------------------------------------------------------ Lua
3.14 +
3.15 +# Lua uses double for numbers. To change this, uncomment one of the lines below.
3.16 +#NUMBER= -DLUA_NUM_TYPE=double
3.17 +#NUMBER= -DLUA_NUM_TYPE=float
3.18 +#NUMBER= -DLUA_NUM_TYPE=long
3.19 +# Optionally, you may also want change how numbers are converted to strings,
3.20 +# and vice-versa. Look for LUA_NUMBER in llimits.h and in the rest of the code.
3.21 +
3.22 +# If you want support for pipes, uncomment the following line.
3.23 +# You need popen in your C library.
3.24 +#POPEN= -DPOPEN
3.25 +
3.26 +# If you need compatibility with previous versions, edit and uncomment the
3.27 +# definition of COMPAT below.
3.28 +# Use -DLUA_COMPAT_READPATTERN if you need complex read patterns.
3.29 +# Use -DLUA_COMPAT_ARGRET if you need the old semantics that used only the
3.30 +# first value returned by a function when it is called as the last parameter.
3.31 +# Use -DLUA_DEPRECATEDFUNCS if you need the obsolete functions in the standard
3.32 +# Lua library (not recommended).
3.33 +#COMPAT= -DLUA_COMPAT_READPATTERN -DLUA_COMPAT_ARGRET -DLUA_DEPRECATEDFUNCS
3.34 +
3.35 +# ------------------------------------------------------------------ C compiler
3.36 +
3.37 +# You need an ANSI C compiler. gcc is a popular one.
3.38 +CC= gcc
3.39 +WARN= -ansi -pedantic -Wall
3.40 +
3.41 +# On IRIX, cc is a good ANSI compiler.
3.42 +#CC= cc
3.43 +#WARN= -ansi -fullwarn
3.44 +
3.45 +# On Solaris, cc is optional. You may have to add -Dsparc if you use -Xc.
3.46 +#CC= cc
3.47 +#WARN= -Xc # -Dsparc
3.48 +
3.49 +# ------------------------------------------------------------------ C library
3.50 +
3.51 +# If your C library is not POSIX compliant, comment the following line.
3.52 +POSIX= -D_POSIX_SOURCE
3.53 +
3.54 +# If your C library does not have the newer ANSI functions strerror, strcoll,
3.55 +# and locale support, uncomment the following line. SunOs 4.1.x is one example.
3.56 +#OLD_ANSI= -DOLD_ANSI
3.57 +
3.58 +# In SunOs 4.1.x, standard headers in /usr/include are not ANSI,
3.59 +# so uncomment the following line to avoid prototypes warnings.
3.60 +#EXTRA_INCS= -I/usr/5include
3.61 +EXTRA_INCS= -I$(LUA)/../tolua/include
3.62 +
3.63 +# The stand-alone Lua interpreter needs the math functions, which are usually
3.64 +# in libm.a (-lm). If your C library already includes the math functions,
3.65 +# or if you are using a modified interpreter that does not need them,
3.66 +# then comment the following line.
3.67 +EXTRA_LIBS= -lm
3.68 +
3.69 +# ------------------------------------------------------------------ librarian
3.70 +
3.71 +# This should work in all Unix systems.
3.72 +AR= ar rcu
3.73 +
3.74 +# If your system doesn't have (or need) ranlib, use RANLIB=true.
3.75 +# On some systems, "ar s" does what ranlib would do.
3.76 +RANLIB= ranlib
3.77 +#RANLIB= ar s
3.78 +#RANLIB= true
3.79 +
3.80 +# ------------------------------------------------------------------ install
3.81 +
3.82 +# Locations for "make install". You may need to be root do "make install".
3.83 +INSTALL_ROOT= /usr/local
3.84 +INSTALL_BIN= $(INSTALL_ROOT)/bin
3.85 +INSTALL_INC= $(INSTALL_ROOT)/include
3.86 +INSTALL_LIB= $(INSTALL_ROOT)/lib
3.87 +INSTALL_MAN= $(INSTALL_ROOT)/man/man1
3.88 +
3.89 +# You might prefer to use "install" if you have it.
3.90 +INSTALL_EXEC= cp
3.91 +INSTALL_DATA= cp
3.92 +#INSTALL_EXEC= install -m 0755
3.93 +#INSTALL_DATA= install -m 0644
3.94 +
3.95 +# == END OF USER SETTINGS. DO NOT CHANGE ANYTHING BELOW THIS LINE =============
3.96 +
3.97 +BIN= $(LUA)/bin
3.98 +INC= $(LUA)/include
3.99 +LIB= $(LUA)/lib
3.100 +
3.101 +INCS= -I$(INC) $(EXTRA_INCS)
3.102 +DEFS= $(COMPAT) $(NUMBER) $(OLD_ANSI) $(EXTRA_DEFS)
3.103 +
3.104 +CFLAGS= -O2 $(WARN) $(INCS) $(DEFS)
3.105 +
3.106 +V=4.0
3.107 +
3.108 +# (end of config)
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/source/lua/etc/bin2c.c Wed Nov 04 00:31:14 2009 +0000
4.3 @@ -0,0 +1,67 @@
4.4 +/*
4.5 +* bin2c.c
4.6 +* convert files to byte arrays for automatic loading with lua_dobuffer
4.7 +* Luiz Henrique de Figueiredo (lhf@tecgraf.puc-rio.br)
4.8 +* 02 Apr 2003 20:44:31
4.9 +*/
4.10 +
4.11 +#include <ctype.h>
4.12 +#include <stdio.h>
4.13 +#include <stdlib.h>
4.14 +
4.15 +static void dump(FILE* f, int n)
4.16 +{
4.17 + printf("static const unsigned char B%d[]={\n",n);
4.18 + for (n=1;;n++)
4.19 + {
4.20 + int c=getc(f);
4.21 + if (c==EOF) break;
4.22 + printf("%3u,",c);
4.23 + if (n==20) { putchar('\n'); n=0; }
4.24 + }
4.25 + printf("\n};\n\n");
4.26 +}
4.27 +
4.28 +static void fdump(const char* fn, int n)
4.29 +{
4.30 + FILE* f= fopen(fn,"rb"); /* must open in binary mode */
4.31 + if (f==NULL)
4.32 + {
4.33 + fprintf(stderr,"bin2c: cannot open ");
4.34 + perror(fn);
4.35 + exit(1);
4.36 + }
4.37 + else
4.38 + {
4.39 + printf("/* %s */\n",fn);
4.40 + dump(f,n);
4.41 + fclose(f);
4.42 + }
4.43 +}
4.44 +
4.45 +static void emit(const char* fn, int n)
4.46 +{
4.47 + printf(" lua_dobuffer(L,(const char*)B%d,sizeof(B%d),\"%s\");\n",n,n,fn);
4.48 +}
4.49 +
4.50 +int main(int argc, char* argv[])
4.51 +{
4.52 + printf("/* code automatically generated by bin2c -- DO NOT EDIT */\n");
4.53 + printf("{\n");
4.54 + if (argc<2)
4.55 + {
4.56 + dump(stdin,0);
4.57 + emit("=stdin",0);
4.58 + }
4.59 + else
4.60 + {
4.61 + int i;
4.62 + printf("/* #include'ing this file in a C program is equivalent to calling\n");
4.63 + for (i=1; i<argc; i++) printf(" lua_dofile(L,\"%s\");\n",argv[i]);
4.64 + printf("*/\n");
4.65 + for (i=1; i<argc; i++) fdump(argv[i],i);
4.66 + for (i=1; i<argc; i++) emit(argv[i],i);
4.67 + }
4.68 + printf("}\n");
4.69 + return 0;
4.70 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/source/lua/etc/makefile.mak Wed Nov 04 00:31:14 2009 +0000
5.3 @@ -0,0 +1,7 @@
5.4 +# Run this on win32 with: nmake /f makefile.mak
5.5 +all:
5.6 + cl /O2 bin2c.c
5.7 + move bin2c.exe ..\bin
5.8 +
5.9 +clean:
5.10 + del *.exe *.obj
5.11 \ No newline at end of file
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/source/lua/include/lauxlib.h Wed Nov 04 00:31:14 2009 +0000
6.3 @@ -0,0 +1,145 @@
6.4 +/*
6.5 +** $Id$
6.6 +** Auxiliary functions for building Lua libraries
6.7 +** See Copyright Notice in lua.h
6.8 +*/
6.9 +
6.10 +
6.11 +#ifndef lauxlib_h
6.12 +#define lauxlib_h
6.13 +
6.14 +
6.15 +#include <stddef.h>
6.16 +#include <stdio.h>
6.17 +
6.18 +#include "lua.h"
6.19 +
6.20 +
6.21 +#ifndef LUALIB_API
6.22 +#define LUALIB_API LUA_API
6.23 +#endif
6.24 +
6.25 +
6.26 +
6.27 +typedef struct luaL_reg {
6.28 + const char *name;
6.29 + lua_CFunction func;
6.30 +} luaL_reg;
6.31 +
6.32 +
6.33 +LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
6.34 + const luaL_reg *l, int nup);
6.35 +LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *e);
6.36 +LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *e);
6.37 +LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname);
6.38 +LUALIB_API int luaL_argerror (lua_State *L, int numarg, const char *extramsg);
6.39 +LUALIB_API const char *luaL_checklstring (lua_State *L, int numArg, size_t *l);
6.40 +LUALIB_API const char *luaL_optlstring (lua_State *L, int numArg,
6.41 + const char *def, size_t *l);
6.42 +LUALIB_API lua_Number luaL_checknumber (lua_State *L, int numArg);
6.43 +LUALIB_API lua_Number luaL_optnumber (lua_State *L, int nArg, lua_Number def);
6.44 +
6.45 +LUALIB_API void luaL_checkstack (lua_State *L, int sz, const char *msg);
6.46 +LUALIB_API void luaL_checktype (lua_State *L, int narg, int t);
6.47 +LUALIB_API void luaL_checkany (lua_State *L, int narg);
6.48 +
6.49 +LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname);
6.50 +LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname);
6.51 +LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname);
6.52 +
6.53 +LUALIB_API void luaL_where (lua_State *L, int lvl);
6.54 +LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...);
6.55 +
6.56 +LUALIB_API int luaL_findstring (const char *st, const char *const lst[]);
6.57 +
6.58 +LUALIB_API int luaL_ref (lua_State *L, int t);
6.59 +LUALIB_API void luaL_unref (lua_State *L, int t, int ref);
6.60 +
6.61 +LUALIB_API int luaL_getn (lua_State *L, int t);
6.62 +LUALIB_API void luaL_setn (lua_State *L, int t, int n);
6.63 +
6.64 +LUALIB_API int luaL_loadfile (lua_State *L, const char *filename);
6.65 +LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t sz,
6.66 + const char *name);
6.67 +
6.68 +
6.69 +
6.70 +/*
6.71 +** ===============================================================
6.72 +** some useful macros
6.73 +** ===============================================================
6.74 +*/
6.75 +
6.76 +#define luaL_argcheck(L, cond,numarg,extramsg) if (!(cond)) \
6.77 + luaL_argerror(L, numarg,extramsg)
6.78 +#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
6.79 +#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
6.80 +#define luaL_checkint(L,n) ((int)luaL_checknumber(L, n))
6.81 +#define luaL_checklong(L,n) ((long)luaL_checknumber(L, n))
6.82 +#define luaL_optint(L,n,d) ((int)luaL_optnumber(L, n,(lua_Number)(d)))
6.83 +#define luaL_optlong(L,n,d) ((long)luaL_optnumber(L, n,(lua_Number)(d)))
6.84 +
6.85 +
6.86 +/*
6.87 +** {======================================================
6.88 +** Generic Buffer manipulation
6.89 +** =======================================================
6.90 +*/
6.91 +
6.92 +
6.93 +#ifndef LUAL_BUFFERSIZE
6.94 +#define LUAL_BUFFERSIZE BUFSIZ
6.95 +#endif
6.96 +
6.97 +
6.98 +typedef struct luaL_Buffer {
6.99 + char *p; /* current position in buffer */
6.100 + int lvl; /* number of strings in the stack (level) */
6.101 + lua_State *L;
6.102 + char buffer[LUAL_BUFFERSIZE];
6.103 +} luaL_Buffer;
6.104 +
6.105 +#define luaL_putchar(B,c) \
6.106 + ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \
6.107 + (*(B)->p++ = (char)(c)))
6.108 +
6.109 +#define luaL_addsize(B,n) ((B)->p += (n))
6.110 +
6.111 +LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B);
6.112 +LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B);
6.113 +LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);
6.114 +LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s);
6.115 +LUALIB_API void luaL_addvalue (luaL_Buffer *B);
6.116 +LUALIB_API void luaL_pushresult (luaL_Buffer *B);
6.117 +
6.118 +
6.119 +/* }====================================================== */
6.120 +
6.121 +
6.122 +
6.123 +/*
6.124 +** Compatibility macros and functions
6.125 +*/
6.126 +
6.127 +LUALIB_API int lua_dofile (lua_State *L, const char *filename);
6.128 +LUALIB_API int lua_dostring (lua_State *L, const char *str);
6.129 +LUALIB_API int lua_dobuffer (lua_State *L, const char *buff, size_t sz,
6.130 + const char *n);
6.131 +
6.132 +
6.133 +#define luaL_check_lstr luaL_checklstring
6.134 +#define luaL_opt_lstr luaL_optlstring
6.135 +#define luaL_check_number luaL_checknumber
6.136 +#define luaL_opt_number luaL_optnumber
6.137 +#define luaL_arg_check luaL_argcheck
6.138 +#define luaL_check_string luaL_checkstring
6.139 +#define luaL_opt_string luaL_optstring
6.140 +#define luaL_check_int luaL_checkint
6.141 +#define luaL_check_long luaL_checklong
6.142 +#define luaL_opt_int luaL_optint
6.143 +#define luaL_opt_long luaL_optlong
6.144 +
6.145 +
6.146 +#endif
6.147 +
6.148 +
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/source/lua/include/lua.h Wed Nov 04 00:31:14 2009 +0000
7.3 @@ -0,0 +1,391 @@
7.4 +/*
7.5 +** $Id$
7.6 +** Lua - An Extensible Extension Language
7.7 +** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
7.8 +** http://www.lua.org mailto:info@lua.org
7.9 +** See Copyright Notice at the end of this file
7.10 +*/
7.11 +
7.12 +
7.13 +#ifndef lua_h
7.14 +#define lua_h
7.15 +
7.16 +#include <stdarg.h>
7.17 +#include <stddef.h>
7.18 +
7.19 +
7.20 +#define LUA_VERSION "Lua 5.0.2"
7.21 +#define LUA_COPYRIGHT "Copyright (C) 1994-2004 Tecgraf, PUC-Rio"
7.22 +#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
7.23 +
7.24 +
7.25 +
7.26 +/* option for multiple returns in `lua_pcall' and `lua_call' */
7.27 +#define LUA_MULTRET (-1)
7.28 +
7.29 +
7.30 +/*
7.31 +** pseudo-indices
7.32 +*/
7.33 +#define LUA_REGISTRYINDEX (-10000)
7.34 +#define LUA_GLOBALSINDEX (-10001)
7.35 +#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i))
7.36 +
7.37 +
7.38 +/* error codes for `lua_load' and `lua_pcall' */
7.39 +#define LUA_ERRRUN 1
7.40 +#define LUA_ERRFILE 2
7.41 +#define LUA_ERRSYNTAX 3
7.42 +#define LUA_ERRMEM 4
7.43 +#define LUA_ERRERR 5
7.44 +
7.45 +
7.46 +typedef struct lua_State lua_State;
7.47 +
7.48 +typedef int (*lua_CFunction) (lua_State *L);
7.49 +
7.50 +
7.51 +/*
7.52 +** functions that read/write blocks when loading/dumping Lua chunks
7.53 +*/
7.54 +typedef const char * (*lua_Chunkreader) (lua_State *L, void *ud, size_t *sz);
7.55 +
7.56 +typedef int (*lua_Chunkwriter) (lua_State *L, const void* p,
7.57 + size_t sz, void* ud);
7.58 +
7.59 +
7.60 +/*
7.61 +** basic types
7.62 +*/
7.63 +#define LUA_TNONE (-1)
7.64 +
7.65 +#define LUA_TNIL 0
7.66 +#define LUA_TBOOLEAN 1
7.67 +#define LUA_TLIGHTUSERDATA 2
7.68 +#define LUA_TNUMBER 3
7.69 +#define LUA_TSTRING 4
7.70 +#define LUA_TTABLE 5
7.71 +#define LUA_TFUNCTION 6
7.72 +#define LUA_TUSERDATA 7
7.73 +#define LUA_TTHREAD 8
7.74 +
7.75 +
7.76 +/* minimum Lua stack available to a C function */
7.77 +#define LUA_MINSTACK 20
7.78 +
7.79 +
7.80 +/*
7.81 +** generic extra include file
7.82 +*/
7.83 +#ifdef LUA_USER_H
7.84 +#include LUA_USER_H
7.85 +#endif
7.86 +
7.87 +
7.88 +/* type of numbers in Lua */
7.89 +#ifndef LUA_NUMBER
7.90 +typedef double lua_Number;
7.91 +#else
7.92 +typedef LUA_NUMBER lua_Number;
7.93 +#endif
7.94 +
7.95 +
7.96 +/* mark for all API functions */
7.97 +#ifndef LUA_API
7.98 +#define LUA_API extern
7.99 +#endif
7.100 +
7.101 +
7.102 +/*
7.103 +** state manipulation
7.104 +*/
7.105 +LUA_API lua_State *lua_open (void);
7.106 +LUA_API void lua_close (lua_State *L);
7.107 +LUA_API lua_State *lua_newthread (lua_State *L);
7.108 +
7.109 +LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);
7.110 +
7.111 +
7.112 +/*
7.113 +** basic stack manipulation
7.114 +*/
7.115 +LUA_API int lua_gettop (lua_State *L);
7.116 +LUA_API void lua_settop (lua_State *L, int idx);
7.117 +LUA_API void lua_pushvalue (lua_State *L, int idx);
7.118 +LUA_API void lua_remove (lua_State *L, int idx);
7.119 +LUA_API void lua_insert (lua_State *L, int idx);
7.120 +LUA_API void lua_replace (lua_State *L, int idx);
7.121 +LUA_API int lua_checkstack (lua_State *L, int sz);
7.122 +
7.123 +LUA_API void lua_xmove (lua_State *from, lua_State *to, int n);
7.124 +
7.125 +
7.126 +/*
7.127 +** access functions (stack -> C)
7.128 +*/
7.129 +
7.130 +LUA_API int lua_isnumber (lua_State *L, int idx);
7.131 +LUA_API int lua_isstring (lua_State *L, int idx);
7.132 +LUA_API int lua_iscfunction (lua_State *L, int idx);
7.133 +LUA_API int lua_isuserdata (lua_State *L, int idx);
7.134 +LUA_API int lua_type (lua_State *L, int idx);
7.135 +LUA_API const char *lua_typename (lua_State *L, int tp);
7.136 +
7.137 +LUA_API int lua_equal (lua_State *L, int idx1, int idx2);
7.138 +LUA_API int lua_rawequal (lua_State *L, int idx1, int idx2);
7.139 +LUA_API int lua_lessthan (lua_State *L, int idx1, int idx2);
7.140 +
7.141 +LUA_API lua_Number lua_tonumber (lua_State *L, int idx);
7.142 +LUA_API int lua_toboolean (lua_State *L, int idx);
7.143 +LUA_API const char *lua_tostring (lua_State *L, int idx);
7.144 +LUA_API size_t lua_strlen (lua_State *L, int idx);
7.145 +LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx);
7.146 +LUA_API void *lua_touserdata (lua_State *L, int idx);
7.147 +LUA_API lua_State *lua_tothread (lua_State *L, int idx);
7.148 +LUA_API const void *lua_topointer (lua_State *L, int idx);
7.149 +
7.150 +
7.151 +/*
7.152 +** push functions (C -> stack)
7.153 +*/
7.154 +LUA_API void lua_pushnil (lua_State *L);
7.155 +LUA_API void lua_pushnumber (lua_State *L, lua_Number n);
7.156 +LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t l);
7.157 +LUA_API void lua_pushstring (lua_State *L, const char *s);
7.158 +LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
7.159 + va_list argp);
7.160 +LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...);
7.161 +LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
7.162 +LUA_API void lua_pushboolean (lua_State *L, int b);
7.163 +LUA_API void lua_pushlightuserdata (lua_State *L, void *p);
7.164 +
7.165 +
7.166 +/*
7.167 +** get functions (Lua -> stack)
7.168 +*/
7.169 +LUA_API void lua_gettable (lua_State *L, int idx);
7.170 +LUA_API void lua_rawget (lua_State *L, int idx);
7.171 +LUA_API void lua_rawgeti (lua_State *L, int idx, int n);
7.172 +LUA_API void lua_newtable (lua_State *L);
7.173 +LUA_API void *lua_newuserdata (lua_State *L, size_t sz);
7.174 +LUA_API int lua_getmetatable (lua_State *L, int objindex);
7.175 +LUA_API void lua_getfenv (lua_State *L, int idx);
7.176 +
7.177 +
7.178 +/*
7.179 +** set functions (stack -> Lua)
7.180 +*/
7.181 +LUA_API void lua_settable (lua_State *L, int idx);
7.182 +LUA_API void lua_rawset (lua_State *L, int idx);
7.183 +LUA_API void lua_rawseti (lua_State *L, int idx, int n);
7.184 +LUA_API int lua_setmetatable (lua_State *L, int objindex);
7.185 +LUA_API int lua_setfenv (lua_State *L, int idx);
7.186 +
7.187 +
7.188 +/*
7.189 +** `load' and `call' functions (load and run Lua code)
7.190 +*/
7.191 +LUA_API void lua_call (lua_State *L, int nargs, int nresults);
7.192 +LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc);
7.193 +LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud);
7.194 +LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *dt,
7.195 + const char *chunkname);
7.196 +
7.197 +LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data);
7.198 +
7.199 +
7.200 +/*
7.201 +** coroutine functions
7.202 +*/
7.203 +LUA_API int lua_yield (lua_State *L, int nresults);
7.204 +LUA_API int lua_resume (lua_State *L, int narg);
7.205 +
7.206 +/*
7.207 +** garbage-collection functions
7.208 +*/
7.209 +LUA_API int lua_getgcthreshold (lua_State *L);
7.210 +LUA_API int lua_getgccount (lua_State *L);
7.211 +LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold);
7.212 +
7.213 +/*
7.214 +** miscellaneous functions
7.215 +*/
7.216 +
7.217 +LUA_API const char *lua_version (void);
7.218 +
7.219 +LUA_API int lua_error (lua_State *L);
7.220 +
7.221 +LUA_API int lua_next (lua_State *L, int idx);
7.222 +
7.223 +LUA_API void lua_concat (lua_State *L, int n);
7.224 +
7.225 +
7.226 +
7.227 +/*
7.228 +** ===============================================================
7.229 +** some useful macros
7.230 +** ===============================================================
7.231 +*/
7.232 +
7.233 +#define lua_boxpointer(L,u) \
7.234 + (*(void **)(lua_newuserdata(L, sizeof(void *))) = (u))
7.235 +
7.236 +#define lua_unboxpointer(L,i) (*(void **)(lua_touserdata(L, i)))
7.237 +
7.238 +#define lua_pop(L,n) lua_settop(L, -(n)-1)
7.239 +
7.240 +#define lua_register(L,n,f) \
7.241 + (lua_pushstring(L, n), \
7.242 + lua_pushcfunction(L, f), \
7.243 + lua_settable(L, LUA_GLOBALSINDEX))
7.244 +
7.245 +#define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0)
7.246 +
7.247 +#define lua_isfunction(L,n) (lua_type(L,n) == LUA_TFUNCTION)
7.248 +#define lua_istable(L,n) (lua_type(L,n) == LUA_TTABLE)
7.249 +#define lua_islightuserdata(L,n) (lua_type(L,n) == LUA_TLIGHTUSERDATA)
7.250 +#define lua_isnil(L,n) (lua_type(L,n) == LUA_TNIL)
7.251 +#define lua_isboolean(L,n) (lua_type(L,n) == LUA_TBOOLEAN)
7.252 +#define lua_isnone(L,n) (lua_type(L,n) == LUA_TNONE)
7.253 +#define lua_isnoneornil(L, n) (lua_type(L,n) <= 0)
7.254 +
7.255 +#define lua_pushliteral(L, s) \
7.256 + lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
7.257 +
7.258 +
7.259 +
7.260 +/*
7.261 +** compatibility macros and functions
7.262 +*/
7.263 +
7.264 +
7.265 +LUA_API int lua_pushupvalues (lua_State *L);
7.266 +
7.267 +#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX)
7.268 +#define lua_setglobal(L,s) \
7.269 + (lua_pushstring(L, s), lua_insert(L, -2), lua_settable(L, LUA_GLOBALSINDEX))
7.270 +
7.271 +#define lua_getglobal(L,s) \
7.272 + (lua_pushstring(L, s), lua_gettable(L, LUA_GLOBALSINDEX))
7.273 +
7.274 +
7.275 +/* compatibility with ref system */
7.276 +
7.277 +/* pre-defined references */
7.278 +#define LUA_NOREF (-2)
7.279 +#define LUA_REFNIL (-1)
7.280 +
7.281 +#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \
7.282 + (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0))
7.283 +
7.284 +#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref))
7.285 +
7.286 +#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, ref)
7.287 +
7.288 +
7.289 +
7.290 +/*
7.291 +** {======================================================================
7.292 +** useful definitions for Lua kernel and libraries
7.293 +** =======================================================================
7.294 +*/
7.295 +
7.296 +/* formats for Lua numbers */
7.297 +#ifndef LUA_NUMBER_SCAN
7.298 +#define LUA_NUMBER_SCAN "%lf"
7.299 +#endif
7.300 +
7.301 +#ifndef LUA_NUMBER_FMT
7.302 +#define LUA_NUMBER_FMT "%.14g"
7.303 +#endif
7.304 +
7.305 +/* }====================================================================== */
7.306 +
7.307 +
7.308 +/*
7.309 +** {======================================================================
7.310 +** Debug API
7.311 +** =======================================================================
7.312 +*/
7.313 +
7.314 +
7.315 +/*
7.316 +** Event codes
7.317 +*/
7.318 +#define LUA_HOOKCALL 0
7.319 +#define LUA_HOOKRET 1
7.320 +#define LUA_HOOKLINE 2
7.321 +#define LUA_HOOKCOUNT 3
7.322 +#define LUA_HOOKTAILRET 4
7.323 +
7.324 +
7.325 +/*
7.326 +** Event masks
7.327 +*/
7.328 +#define LUA_MASKCALL (1 << LUA_HOOKCALL)
7.329 +#define LUA_MASKRET (1 << LUA_HOOKRET)
7.330 +#define LUA_MASKLINE (1 << LUA_HOOKLINE)
7.331 +#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
7.332 +
7.333 +typedef struct lua_Debug lua_Debug; /* activation record */
7.334 +
7.335 +typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
7.336 +
7.337 +
7.338 +LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar);
7.339 +LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
7.340 +LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);
7.341 +LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);
7.342 +LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n);
7.343 +LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n);
7.344 +
7.345 +LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count);
7.346 +LUA_API lua_Hook lua_gethook (lua_State *L);
7.347 +LUA_API int lua_gethookmask (lua_State *L);
7.348 +LUA_API int lua_gethookcount (lua_State *L);
7.349 +
7.350 +
7.351 +#define LUA_IDSIZE 60
7.352 +
7.353 +struct lua_Debug {
7.354 + int event;
7.355 + const char *name; /* (n) */
7.356 + const char *namewhat; /* (n) `global', `local', `field', `method' */
7.357 + const char *what; /* (S) `Lua', `C', `main', `tail' */
7.358 + const char *source; /* (S) */
7.359 + int currentline; /* (l) */
7.360 + int nups; /* (u) number of upvalues */
7.361 + int linedefined; /* (S) */
7.362 + char short_src[LUA_IDSIZE]; /* (S) */
7.363 + /* private part */
7.364 + int i_ci; /* active function */
7.365 +};
7.366 +
7.367 +/* }====================================================================== */
7.368 +
7.369 +
7.370 +/******************************************************************************
7.371 +* Copyright (C) 1994-2004 Tecgraf, PUC-Rio. All rights reserved.
7.372 +*
7.373 +* Permission is hereby granted, free of charge, to any person obtaining
7.374 +* a copy of this software and associated documentation files (the
7.375 +* "Software"), to deal in the Software without restriction, including
7.376 +* without limitation the rights to use, copy, modify, merge, publish,
7.377 +* distribute, sublicense, and/or sell copies of the Software, and to
7.378 +* permit persons to whom the Software is furnished to do so, subject to
7.379 +* the following conditions:
7.380 +*
7.381 +* The above copyright notice and this permission notice shall be
7.382 +* included in all copies or substantial portions of the Software.
7.383 +*
7.384 +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
7.385 +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
7.386 +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
7.387 +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
7.388 +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
7.389 +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
7.390 +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7.391 +******************************************************************************/
7.392 +
7.393 +
7.394 +#endif
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/source/lua/include/lualib.h Wed Nov 04 00:31:14 2009 +0000
8.3 @@ -0,0 +1,56 @@
8.4 +/*
8.5 +** $Id$
8.6 +** Lua standard libraries
8.7 +** See Copyright Notice in lua.h
8.8 +*/
8.9 +
8.10 +
8.11 +#ifndef lualib_h
8.12 +#define lualib_h
8.13 +
8.14 +#include "lua.h"
8.15 +
8.16 +
8.17 +#ifndef LUALIB_API
8.18 +#define LUALIB_API LUA_API
8.19 +#endif
8.20 +
8.21 +
8.22 +#define LUA_COLIBNAME "coroutine"
8.23 +LUALIB_API int luaopen_base (lua_State *L);
8.24 +
8.25 +#define LUA_TABLIBNAME "table"
8.26 +LUALIB_API int luaopen_table (lua_State *L);
8.27 +
8.28 +#define LUA_IOLIBNAME "io"
8.29 +#define LUA_OSLIBNAME "os"
8.30 +LUALIB_API int luaopen_io (lua_State *L);
8.31 +
8.32 +#define LUA_STRLIBNAME "string"
8.33 +LUALIB_API int luaopen_string (lua_State *L);
8.34 +
8.35 +#define LUA_MATHLIBNAME "math"
8.36 +LUALIB_API int luaopen_math (lua_State *L);
8.37 +
8.38 +#define LUA_DBLIBNAME "debug"
8.39 +LUALIB_API int luaopen_debug (lua_State *L);
8.40 +
8.41 +
8.42 +LUALIB_API int luaopen_loadlib (lua_State *L);
8.43 +
8.44 +
8.45 +/* to help testing the libraries */
8.46 +#ifndef lua_assert
8.47 +#define lua_assert(c) /* empty */
8.48 +#endif
8.49 +
8.50 +
8.51 +/* compatibility code */
8.52 +#define lua_baselibopen luaopen_base
8.53 +#define lua_tablibopen luaopen_table
8.54 +#define lua_iolibopen luaopen_io
8.55 +#define lua_strlibopen luaopen_string
8.56 +#define lua_mathlibopen luaopen_math
8.57 +#define lua_dblibopen luaopen_debug
8.58 +
8.59 +#endif
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/source/lua/include/makefile Wed Nov 04 00:31:14 2009 +0000
9.3 @@ -0,0 +1,17 @@
9.4 +# makefile for Lua distribution (includes)
9.5 +
9.6 +LUA= ..
9.7 +
9.8 +include $(LUA)/config
9.9 +
9.10 +SRCS= lua.h lualib.h luadebug.h lauxlib.h
9.11 +
9.12 +all:
9.13 +
9.14 +clean:
9.15 +
9.16 +co:
9.17 + co -q -f -M $(SRCS)
9.18 +
9.19 +klean: clean
9.20 + rm -f $(SRCS)
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/source/lua/makefile Wed Nov 04 00:31:14 2009 +0000
10.3 @@ -0,0 +1,43 @@
10.4 +# makefile for Lua hierarchy
10.5 +# see INSTALL for installation instructions
10.6 +# see file "config" for customization instructions
10.7 +# $Id$
10.8 +
10.9 +LUA= .
10.10 +
10.11 +include $(LUA)/config
10.12 +
10.13 +# primary targets (only "all" and "clean" are useful after distribution)
10.14 +all clean co klean:
10.15 + cd include; $(MAKE) $@
10.16 + cd src; $(MAKE) $@
10.17 + cd src/luac; $(MAKE) $@
10.18 + cd src/lib; $(MAKE) $@
10.19 +
10.20 +# cd src/lua; $(MAKE) $@
10.21 +
10.22 +# remove debug information from binaries
10.23 +strip:
10.24 + strip bin/lua bin/luac
10.25 +
10.26 +# official installation
10.27 +install: all strip
10.28 + mkdir -p $(INSTALL_BIN) $(INSTALL_INC) $(INSTALL_LIB) $(INSTALL_MAN)
10.29 + $(INSTALL_EXEC) bin/* $(INSTALL_BIN)
10.30 + $(INSTALL_DATA) include/*.h $(INSTALL_INC)
10.31 + $(INSTALL_DATA) lib/lib* $(INSTALL_LIB)
10.32 + $(INSTALL_DATA) doc/*.1 $(INSTALL_MAN)
10.33 +
10.34 +# shared libraries (for Linux)
10.35 +so:
10.36 + ld -o lib/liblua.so.$V -shared src/*.o
10.37 + ld -o lib/liblualib.so.$V -shared src/lib/*.o
10.38 + cd lib; ln -s liblua.so.$V liblua.so; ln -s liblualib.so.$V liblualib.so
10.39 +
10.40 +# binaries using shared libraries
10.41 +sobin:
10.42 + rm -f bin/lua bin/luac
10.43 + cd src/lua; $(MAKE)
10.44 + cd src/luac; $(MAKE)
10.45 +
10.46 +# (end of Makefile)
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
11.2 +++ b/source/lua/readme.txt Wed Nov 04 00:31:14 2009 +0000
11.3 @@ -0,0 +1,2 @@
11.4 +
11.5 +This is Lua 5.0.2 See http://www.lua.org/
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
12.2 +++ b/source/lua/src/Makefile Wed Nov 04 00:31:14 2009 +0000
12.3 @@ -0,0 +1,85 @@
12.4 +# makefile for Lua core library
12.5 +
12.6 +LUA= ..
12.7 +
12.8 +include $(LUA)/config
12.9 +
12.10 +OBJS= \
12.11 + lapi.o \
12.12 + lcode.o \
12.13 + ldebug.o \
12.14 + ldo.o \
12.15 + ldump.o \
12.16 + lfunc.o \
12.17 + lgc.o \
12.18 + llex.o \
12.19 + lmem.o \
12.20 + lobject.o \
12.21 + lopcodes.o \
12.22 + lparser.o \
12.23 + lstate.o \
12.24 + lstring.o \
12.25 + ltable.o \
12.26 + ltests.o \
12.27 + ltm.o \
12.28 + lundump.o \
12.29 + lvm.o \
12.30 + lzio.o
12.31 +
12.32 +SRCS= \
12.33 + lapi.c \
12.34 + lcode.c \
12.35 + ldebug.c \
12.36 + ldo.c \
12.37 + ldump.c \
12.38 + lfunc.c \
12.39 + lgc.c \
12.40 + llex.c \
12.41 + lmem.c \
12.42 + lobject.c \
12.43 + lopcodes.c \
12.44 + lparser.c \
12.45 + lstate.c \
12.46 + lstring.c \
12.47 + ltable.c \
12.48 + ltests.c \
12.49 + ltm.c \
12.50 + lundump.c \
12.51 + lvm.c \
12.52 + lzio.c \
12.53 + lapi.h \
12.54 + lcode.h \
12.55 + ldebug.h \
12.56 + ldo.h \
12.57 + lfunc.h \
12.58 + lgc.h \
12.59 + llex.h \
12.60 + llimits.h \
12.61 + lmem.h \
12.62 + lobject.h \
12.63 + lopcodes.h \
12.64 + lparser.h \
12.65 + lstate.h \
12.66 + lstring.h \
12.67 + ltable.h \
12.68 + ltm.h \
12.69 + lundump.h \
12.70 + lvm.h \
12.71 + lzio.h
12.72 +
12.73 +T= $(LIB)/liblua.a
12.74 +
12.75 +all: $T
12.76 +
12.77 +$T: $(OBJS)
12.78 + $(AR) $@ $(OBJS)
12.79 + $(RANLIB) $@
12.80 +
12.81 +clean:
12.82 + rm -f $(OBJS) $T
12.83 +
12.84 +co:
12.85 + co -q -f -M $(SRCS)
12.86 +
12.87 +klean: clean
12.88 + rm -f $(SRCS)
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
13.2 +++ b/source/lua/src/lapi.c Wed Nov 04 00:31:14 2009 +0000
13.3 @@ -0,0 +1,922 @@
13.4 +/*
13.5 +** $Id$
13.6 +** Lua API
13.7 +** See Copyright Notice in lua.h
13.8 +*/
13.9 +
13.10 +
13.11 +#include <assert.h>
13.12 +#include <string.h>
13.13 +
13.14 +#define lapi_c
13.15 +
13.16 +#include "lua.h"
13.17 +
13.18 +#include "lapi.h"
13.19 +#include "ldebug.h"
13.20 +#include "ldo.h"
13.21 +#include "lfunc.h"
13.22 +#include "lgc.h"
13.23 +#include "lmem.h"
13.24 +#include "lobject.h"
13.25 +#include "lstate.h"
13.26 +#include "lstring.h"
13.27 +#include "ltable.h"
13.28 +#include "ltm.h"
13.29 +#include "lundump.h"
13.30 +#include "lvm.h"
13.31 +
13.32 +
13.33 +const char lua_ident[] =
13.34 + "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
13.35 + "$Authors: " LUA_AUTHORS " $\n"
13.36 + "$URL: www.lua.org $\n";
13.37 +
13.38 +
13.39 +
13.40 +#ifndef api_check
13.41 +#define api_check(L, o) /*{ assert(o); }*/
13.42 +#endif
13.43 +
13.44 +#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
13.45 +
13.46 +#define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;}
13.47 +
13.48 +
13.49 +
13.50 +
13.51 +static TObject *negindex (lua_State *L, int idx) {
13.52 + if (idx > LUA_REGISTRYINDEX) {
13.53 + api_check(L, idx != 0 && -idx <= L->top - L->base);
13.54 + return L->top+idx;
13.55 + }
13.56 + else switch (idx) { /* pseudo-indices */
13.57 + case LUA_REGISTRYINDEX: return registry(L);
13.58 + case LUA_GLOBALSINDEX: return gt(L);
13.59 + default: {
13.60 + TObject *func = (L->base - 1);
13.61 + idx = LUA_GLOBALSINDEX - idx;
13.62 + lua_assert(iscfunction(func));
13.63 + return (idx <= clvalue(func)->c.nupvalues)
13.64 + ? &clvalue(func)->c.upvalue[idx-1]
13.65 + : NULL;
13.66 + }
13.67 + }
13.68 +}
13.69 +
13.70 +
13.71 +static TObject *luaA_index (lua_State *L, int idx) {
13.72 + if (idx > 0) {
13.73 + api_check(L, idx <= L->top - L->base);
13.74 + return L->base + idx - 1;
13.75 + }
13.76 + else {
13.77 + TObject *o = negindex(L, idx);
13.78 + api_check(L, o != NULL);
13.79 + return o;
13.80 + }
13.81 +}
13.82 +
13.83 +
13.84 +static TObject *luaA_indexAcceptable (lua_State *L, int idx) {
13.85 + if (idx > 0) {
13.86 + TObject *o = L->base+(idx-1);
13.87 + api_check(L, idx <= L->stack_last - L->base);
13.88 + if (o >= L->top) return NULL;
13.89 + else return o;
13.90 + }
13.91 + else
13.92 + return negindex(L, idx);
13.93 +}
13.94 +
13.95 +
13.96 +void luaA_pushobject (lua_State *L, const TObject *o) {
13.97 + setobj2s(L->top, o);
13.98 + incr_top(L);
13.99 +}
13.100 +
13.101 +
13.102 +LUA_API int lua_checkstack (lua_State *L, int size) {
13.103 + int res;
13.104 + lua_lock(L);
13.105 + if ((L->top - L->base + size) > LUA_MAXCSTACK)
13.106 + res = 0; /* stack overflow */
13.107 + else {
13.108 + luaD_checkstack(L, size);
13.109 + if (L->ci->top < L->top + size)
13.110 + L->ci->top = L->top + size;
13.111 + res = 1;
13.112 + }
13.113 + lua_unlock(L);
13.114 + return res;
13.115 +}
13.116 +
13.117 +
13.118 +LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
13.119 + int i;
13.120 + lua_lock(to);
13.121 + api_checknelems(from, n);
13.122 + from->top -= n;
13.123 + for (i = 0; i < n; i++) {
13.124 + setobj2s(to->top, from->top + i);
13.125 + api_incr_top(to);
13.126 + }
13.127 + lua_unlock(to);
13.128 +}
13.129 +
13.130 +
13.131 +LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
13.132 + lua_CFunction old;
13.133 + lua_lock(L);
13.134 + old = G(L)->panic;
13.135 + G(L)->panic = panicf;
13.136 + lua_unlock(L);
13.137 + return old;
13.138 +}
13.139 +
13.140 +
13.141 +LUA_API lua_State *lua_newthread (lua_State *L) {
13.142 + lua_State *L1;
13.143 + lua_lock(L);
13.144 + luaC_checkGC(L);
13.145 + L1 = luaE_newthread(L);
13.146 + setthvalue(L->top, L1);
13.147 + api_incr_top(L);
13.148 + lua_unlock(L);
13.149 + lua_userstateopen(L1);
13.150 + return L1;
13.151 +}
13.152 +
13.153 +
13.154 +
13.155 +/*
13.156 +** basic stack manipulation
13.157 +*/
13.158 +
13.159 +
13.160 +LUA_API int lua_gettop (lua_State *L) {
13.161 + return (L->top - L->base);
13.162 +}
13.163 +
13.164 +
13.165 +LUA_API void lua_settop (lua_State *L, int idx) {
13.166 + lua_lock(L);
13.167 + if (idx >= 0) {
13.168 + api_check(L, idx <= L->stack_last - L->base);
13.169 + while (L->top < L->base + idx)
13.170 + setnilvalue(L->top++);
13.171 + L->top = L->base + idx;
13.172 + }
13.173 + else {
13.174 + api_check(L, -(idx+1) <= (L->top - L->base));
13.175 + L->top += idx+1; /* `subtract' index (index is negative) */
13.176 + }
13.177 + lua_unlock(L);
13.178 +}
13.179 +
13.180 +
13.181 +LUA_API void lua_remove (lua_State *L, int idx) {
13.182 + StkId p;
13.183 + lua_lock(L);
13.184 + p = luaA_index(L, idx);
13.185 + while (++p < L->top) setobjs2s(p-1, p);
13.186 + L->top--;
13.187 + lua_unlock(L);
13.188 +}
13.189 +
13.190 +
13.191 +LUA_API void lua_insert (lua_State *L, int idx) {
13.192 + StkId p;
13.193 + StkId q;
13.194 + lua_lock(L);
13.195 + p = luaA_index(L, idx);
13.196 + for (q = L->top; q>p; q--) setobjs2s(q, q-1);
13.197 + setobjs2s(p, L->top);
13.198 + lua_unlock(L);
13.199 +}
13.200 +
13.201 +
13.202 +LUA_API void lua_replace (lua_State *L, int idx) {
13.203 + lua_lock(L);
13.204 + api_checknelems(L, 1);
13.205 + setobj(luaA_index(L, idx), L->top - 1); /* write barrier */
13.206 + L->top--;
13.207 + lua_unlock(L);
13.208 +}
13.209 +
13.210 +
13.211 +LUA_API void lua_pushvalue (lua_State *L, int idx) {
13.212 + lua_lock(L);
13.213 + setobj2s(L->top, luaA_index(L, idx));
13.214 + api_incr_top(L);
13.215 + lua_unlock(L);
13.216 +}
13.217 +
13.218 +
13.219 +
13.220 +/*
13.221 +** access functions (stack -> C)
13.222 +*/
13.223 +
13.224 +
13.225 +LUA_API int lua_type (lua_State *L, int idx) {
13.226 + StkId o = luaA_indexAcceptable(L, idx);
13.227 + return (o == NULL) ? LUA_TNONE : ttype(o);
13.228 +}
13.229 +
13.230 +
13.231 +LUA_API const char *lua_typename (lua_State *L, int t) {
13.232 + UNUSED(L);
13.233 + return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
13.234 +}
13.235 +
13.236 +
13.237 +LUA_API int lua_iscfunction (lua_State *L, int idx) {
13.238 + StkId o = luaA_indexAcceptable(L, idx);
13.239 + return (o == NULL) ? 0 : iscfunction(o);
13.240 +}
13.241 +
13.242 +
13.243 +LUA_API int lua_isnumber (lua_State *L, int idx) {
13.244 + TObject n;
13.245 + const TObject *o = luaA_indexAcceptable(L, idx);
13.246 + return (o != NULL && tonumber(o, &n));
13.247 +}
13.248 +
13.249 +
13.250 +LUA_API int lua_isstring (lua_State *L, int idx) {
13.251 + int t = lua_type(L, idx);
13.252 + return (t == LUA_TSTRING || t == LUA_TNUMBER);
13.253 +}
13.254 +
13.255 +
13.256 +LUA_API int lua_isuserdata (lua_State *L, int idx) {
13.257 + const TObject *o = luaA_indexAcceptable(L, idx);
13.258 + return (o != NULL && (ttisuserdata(o) || ttislightuserdata(o)));
13.259 +}
13.260 +
13.261 +
13.262 +LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
13.263 + StkId o1 = luaA_indexAcceptable(L, index1);
13.264 + StkId o2 = luaA_indexAcceptable(L, index2);
13.265 + return (o1 == NULL || o2 == NULL) ? 0 /* index out of range */
13.266 + : luaO_rawequalObj(o1, o2);
13.267 +}
13.268 +
13.269 +
13.270 +LUA_API int lua_equal (lua_State *L, int index1, int index2) {
13.271 + StkId o1, o2;
13.272 + int i;
13.273 + lua_lock(L); /* may call tag method */
13.274 + o1 = luaA_indexAcceptable(L, index1);
13.275 + o2 = luaA_indexAcceptable(L, index2);
13.276 + i = (o1 == NULL || o2 == NULL) ? 0 /* index out of range */
13.277 + : equalobj(L, o1, o2);
13.278 + lua_unlock(L);
13.279 + return i;
13.280 +}
13.281 +
13.282 +
13.283 +LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
13.284 + StkId o1, o2;
13.285 + int i;
13.286 + lua_lock(L); /* may call tag method */
13.287 + o1 = luaA_indexAcceptable(L, index1);
13.288 + o2 = luaA_indexAcceptable(L, index2);
13.289 + i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
13.290 + : luaV_lessthan(L, o1, o2);
13.291 + lua_unlock(L);
13.292 + return i;
13.293 +}
13.294 +
13.295 +
13.296 +
13.297 +LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
13.298 + TObject n;
13.299 + const TObject *o = luaA_indexAcceptable(L, idx);
13.300 + if (o != NULL && tonumber(o, &n))
13.301 + return nvalue(o);
13.302 + else
13.303 + return 0;
13.304 +}
13.305 +
13.306 +
13.307 +LUA_API int lua_toboolean (lua_State *L, int idx) {
13.308 + const TObject *o = luaA_indexAcceptable(L, idx);
13.309 + return (o != NULL) && !l_isfalse(o);
13.310 +}
13.311 +
13.312 +
13.313 +LUA_API const char *lua_tostring (lua_State *L, int idx) {
13.314 + StkId o = luaA_indexAcceptable(L, idx);
13.315 + if (o == NULL)
13.316 + return NULL;
13.317 + else if (ttisstring(o))
13.318 + return svalue(o);
13.319 + else {
13.320 + const char *s;
13.321 + lua_lock(L); /* `luaV_tostring' may create a new string */
13.322 + s = (luaV_tostring(L, o) ? svalue(o) : NULL);
13.323 + luaC_checkGC(L);
13.324 + lua_unlock(L);
13.325 + return s;
13.326 + }
13.327 +}
13.328 +
13.329 +
13.330 +LUA_API size_t lua_strlen (lua_State *L, int idx) {
13.331 + StkId o = luaA_indexAcceptable(L, idx);
13.332 + if (o == NULL)
13.333 + return 0;
13.334 + else if (ttisstring(o))
13.335 + return tsvalue(o)->tsv.len;
13.336 + else {
13.337 + size_t l;
13.338 + lua_lock(L); /* `luaV_tostring' may create a new string */
13.339 + l = (luaV_tostring(L, o) ? tsvalue(o)->tsv.len : 0);
13.340 + lua_unlock(L);
13.341 + return l;
13.342 + }
13.343 +}
13.344 +
13.345 +
13.346 +LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
13.347 + StkId o = luaA_indexAcceptable(L, idx);
13.348 + return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->c.f;
13.349 +}
13.350 +
13.351 +
13.352 +LUA_API void *lua_touserdata (lua_State *L, int idx) {
13.353 + StkId o = luaA_indexAcceptable(L, idx);
13.354 + if (o == NULL) return NULL;
13.355 + switch (ttype(o)) {
13.356 + case LUA_TUSERDATA: return (uvalue(o) + 1);
13.357 + case LUA_TLIGHTUSERDATA: return pvalue(o);
13.358 + default: return NULL;
13.359 + }
13.360 +}
13.361 +
13.362 +
13.363 +LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
13.364 + StkId o = luaA_indexAcceptable(L, idx);
13.365 + return (o == NULL || !ttisthread(o)) ? NULL : thvalue(o);
13.366 +}
13.367 +
13.368 +
13.369 +LUA_API const void *lua_topointer (lua_State *L, int idx) {
13.370 + StkId o = luaA_indexAcceptable(L, idx);
13.371 + if (o == NULL) return NULL;
13.372 + else {
13.373 + switch (ttype(o)) {
13.374 + case LUA_TTABLE: return hvalue(o);
13.375 + case LUA_TFUNCTION: return clvalue(o);
13.376 + case LUA_TTHREAD: return thvalue(o);
13.377 + case LUA_TUSERDATA:
13.378 + case LUA_TLIGHTUSERDATA:
13.379 + return lua_touserdata(L, idx);
13.380 + default: return NULL;
13.381 + }
13.382 + }
13.383 +}
13.384 +
13.385 +
13.386 +
13.387 +/*
13.388 +** push functions (C -> stack)
13.389 +*/
13.390 +
13.391 +
13.392 +LUA_API void lua_pushnil (lua_State *L) {
13.393 + lua_lock(L);
13.394 + setnilvalue(L->top);
13.395 + api_incr_top(L);
13.396 + lua_unlock(L);
13.397 +}
13.398 +
13.399 +
13.400 +LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
13.401 + lua_lock(L);
13.402 + setnvalue(L->top, n);
13.403 + api_incr_top(L);
13.404 + lua_unlock(L);
13.405 +}
13.406 +
13.407 +
13.408 +LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
13.409 + lua_lock(L);
13.410 + luaC_checkGC(L);
13.411 + setsvalue2s(L->top, luaS_newlstr(L, s, len));
13.412 + api_incr_top(L);
13.413 + lua_unlock(L);
13.414 +}
13.415 +
13.416 +
13.417 +LUA_API void lua_pushstring (lua_State *L, const char *s) {
13.418 + if (s == NULL)
13.419 + lua_pushnil(L);
13.420 + else
13.421 + lua_pushlstring(L, s, strlen(s));
13.422 +}
13.423 +
13.424 +
13.425 +LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
13.426 + va_list argp) {
13.427 + const char *ret;
13.428 + lua_lock(L);
13.429 + luaC_checkGC(L);
13.430 + ret = luaO_pushvfstring(L, fmt, argp);
13.431 + lua_unlock(L);
13.432 + return ret;
13.433 +}
13.434 +
13.435 +
13.436 +LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
13.437 + const char *ret;
13.438 + va_list argp;
13.439 + lua_lock(L);
13.440 + luaC_checkGC(L);
13.441 + va_start(argp, fmt);
13.442 + ret = luaO_pushvfstring(L, fmt, argp);
13.443 + va_end(argp);
13.444 + lua_unlock(L);
13.445 + return ret;
13.446 +}
13.447 +
13.448 +
13.449 +LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
13.450 + Closure *cl;
13.451 + lua_lock(L);
13.452 + luaC_checkGC(L);
13.453 + api_checknelems(L, n);
13.454 + cl = luaF_newCclosure(L, n);
13.455 + cl->c.f = fn;
13.456 + L->top -= n;
13.457 + while (n--)
13.458 + setobj2n(&cl->c.upvalue[n], L->top+n);
13.459 + setclvalue(L->top, cl);
13.460 + api_incr_top(L);
13.461 + lua_unlock(L);
13.462 +}
13.463 +
13.464 +
13.465 +LUA_API void lua_pushboolean (lua_State *L, int b) {
13.466 + lua_lock(L);
13.467 + setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
13.468 + api_incr_top(L);
13.469 + lua_unlock(L);
13.470 +}
13.471 +
13.472 +
13.473 +LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
13.474 + lua_lock(L);
13.475 + setpvalue(L->top, p);
13.476 + api_incr_top(L);
13.477 + lua_unlock(L);
13.478 +}
13.479 +
13.480 +
13.481 +
13.482 +/*
13.483 +** get functions (Lua -> stack)
13.484 +*/
13.485 +
13.486 +
13.487 +LUA_API void lua_gettable (lua_State *L, int idx) {
13.488 + StkId t;
13.489 + lua_lock(L);
13.490 + t = luaA_index(L, idx);
13.491 + setobj2s(L->top - 1, luaV_gettable(L, t, L->top - 1, 0));
13.492 + lua_unlock(L);
13.493 +}
13.494 +
13.495 +
13.496 +LUA_API void lua_rawget (lua_State *L, int idx) {
13.497 + StkId t;
13.498 + lua_lock(L);
13.499 + t = luaA_index(L, idx);
13.500 + api_check(L, ttistable(t));
13.501 + setobj2s(L->top - 1, luaH_get(hvalue(t), L->top - 1));
13.502 + lua_unlock(L);
13.503 +}
13.504 +
13.505 +
13.506 +LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
13.507 + StkId o;
13.508 + lua_lock(L);
13.509 + o = luaA_index(L, idx);
13.510 + api_check(L, ttistable(o));
13.511 + setobj2s(L->top, luaH_getnum(hvalue(o), n));
13.512 + api_incr_top(L);
13.513 + lua_unlock(L);
13.514 +}
13.515 +
13.516 +
13.517 +LUA_API void lua_newtable (lua_State *L) {
13.518 + lua_lock(L);
13.519 + luaC_checkGC(L);
13.520 + sethvalue(L->top, luaH_new(L, 0, 0));
13.521 + api_incr_top(L);
13.522 + lua_unlock(L);
13.523 +}
13.524 +
13.525 +
13.526 +LUA_API int lua_getmetatable (lua_State *L, int objindex) {
13.527 + const TObject *obj;
13.528 + Table *mt = NULL;
13.529 + int res;
13.530 + lua_lock(L);
13.531 + obj = luaA_indexAcceptable(L, objindex);
13.532 + if (obj != NULL) {
13.533 + switch (ttype(obj)) {
13.534 + case LUA_TTABLE:
13.535 + mt = hvalue(obj)->metatable;
13.536 + break;
13.537 + case LUA_TUSERDATA:
13.538 + mt = uvalue(obj)->uv.metatable;
13.539 + break;
13.540 + }
13.541 + }
13.542 + if (mt == NULL || mt == hvalue(defaultmeta(L)))
13.543 + res = 0;
13.544 + else {
13.545 + sethvalue(L->top, mt);
13.546 + api_incr_top(L);
13.547 + res = 1;
13.548 + }
13.549 + lua_unlock(L);
13.550 + return res;
13.551 +}
13.552 +
13.553 +
13.554 +LUA_API void lua_getfenv (lua_State *L, int idx) {
13.555 + StkId o;
13.556 + lua_lock(L);
13.557 + o = luaA_index(L, idx);
13.558 + setobj2s(L->top, isLfunction(o) ? &clvalue(o)->l.g : gt(L));
13.559 + api_incr_top(L);
13.560 + lua_unlock(L);
13.561 +}
13.562 +
13.563 +
13.564 +/*
13.565 +** set functions (stack -> Lua)
13.566 +*/
13.567 +
13.568 +
13.569 +LUA_API void lua_settable (lua_State *L, int idx) {
13.570 + StkId t;
13.571 + lua_lock(L);
13.572 + api_checknelems(L, 2);
13.573 + t = luaA_index(L, idx);
13.574 + luaV_settable(L, t, L->top - 2, L->top - 1);
13.575 + L->top -= 2; /* pop index and value */
13.576 + lua_unlock(L);
13.577 +}
13.578 +
13.579 +
13.580 +LUA_API void lua_rawset (lua_State *L, int idx) {
13.581 + StkId t;
13.582 + lua_lock(L);
13.583 + api_checknelems(L, 2);
13.584 + t = luaA_index(L, idx);
13.585 + api_check(L, ttistable(t));
13.586 + setobj2t(luaH_set(L, hvalue(t), L->top-2), L->top-1); /* write barrier */
13.587 + L->top -= 2;
13.588 + lua_unlock(L);
13.589 +}
13.590 +
13.591 +
13.592 +LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
13.593 + StkId o;
13.594 + lua_lock(L);
13.595 + api_checknelems(L, 1);
13.596 + o = luaA_index(L, idx);
13.597 + api_check(L, ttistable(o));
13.598 + setobj2t(luaH_setnum(L, hvalue(o), n), L->top-1); /* write barrier */
13.599 + L->top--;
13.600 + lua_unlock(L);
13.601 +}
13.602 +
13.603 +
13.604 +LUA_API int lua_setmetatable (lua_State *L, int objindex) {
13.605 + TObject *obj, *mt;
13.606 + int res = 1;
13.607 + lua_lock(L);
13.608 + api_checknelems(L, 1);
13.609 + obj = luaA_index(L, objindex);
13.610 + mt = (!ttisnil(L->top - 1)) ? L->top - 1 : defaultmeta(L);
13.611 + api_check(L, ttistable(mt));
13.612 + switch (ttype(obj)) {
13.613 + case LUA_TTABLE: {
13.614 + hvalue(obj)->metatable = hvalue(mt); /* write barrier */
13.615 + break;
13.616 + }
13.617 + case LUA_TUSERDATA: {
13.618 + uvalue(obj)->uv.metatable = hvalue(mt); /* write barrier */
13.619 + break;
13.620 + }
13.621 + default: {
13.622 + res = 0; /* cannot set */
13.623 + break;
13.624 + }
13.625 + }
13.626 + L->top--;
13.627 + lua_unlock(L);
13.628 + return res;
13.629 +}
13.630 +
13.631 +
13.632 +LUA_API int lua_setfenv (lua_State *L, int idx) {
13.633 + StkId o;
13.634 + int res = 0;
13.635 + lua_lock(L);
13.636 + api_checknelems(L, 1);
13.637 + o = luaA_index(L, idx);
13.638 + L->top--;
13.639 + api_check(L, ttistable(L->top));
13.640 + if (isLfunction(o)) {
13.641 + res = 1;
13.642 + clvalue(o)->l.g = *(L->top);
13.643 + }
13.644 + lua_unlock(L);
13.645 + return res;
13.646 +}
13.647 +
13.648 +
13.649 +/*
13.650 +** `load' and `call' functions (run Lua code)
13.651 +*/
13.652 +
13.653 +LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
13.654 + StkId func;
13.655 + lua_lock(L);
13.656 + api_checknelems(L, nargs+1);
13.657 + func = L->top - (nargs+1);
13.658 + luaD_call(L, func, nresults);
13.659 + lua_unlock(L);
13.660 +}
13.661 +
13.662 +
13.663 +
13.664 +/*
13.665 +** Execute a protected call.
13.666 +*/
13.667 +struct CallS { /* data to `f_call' */
13.668 + StkId func;
13.669 + int nresults;
13.670 +};
13.671 +
13.672 +
13.673 +static void f_call (lua_State *L, void *ud) {
13.674 + struct CallS *c = cast(struct CallS *, ud);
13.675 + luaD_call(L, c->func, c->nresults);
13.676 +}
13.677 +
13.678 +
13.679 +
13.680 +LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
13.681 + struct CallS c;
13.682 + int status;
13.683 + ptrdiff_t func;
13.684 + lua_lock(L);
13.685 + func = (errfunc == 0) ? 0 : savestack(L, luaA_index(L, errfunc));
13.686 + c.func = L->top - (nargs+1); /* function to be called */
13.687 + c.nresults = nresults;
13.688 + status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
13.689 + lua_unlock(L);
13.690 + return status;
13.691 +}
13.692 +
13.693 +
13.694 +/*
13.695 +** Execute a protected C call.
13.696 +*/
13.697 +struct CCallS { /* data to `f_Ccall' */
13.698 + lua_CFunction func;
13.699 + void *ud;
13.700 +};
13.701 +
13.702 +
13.703 +static void f_Ccall (lua_State *L, void *ud) {
13.704 + struct CCallS *c = cast(struct CCallS *, ud);
13.705 + Closure *cl;
13.706 + cl = luaF_newCclosure(L, 0);
13.707 + cl->c.f = c->func;
13.708 + setclvalue(L->top, cl); /* push function */
13.709 + incr_top(L);
13.710 + setpvalue(L->top, c->ud); /* push only argument */
13.711 + incr_top(L);
13.712 + luaD_call(L, L->top - 2, 0);
13.713 +}
13.714 +
13.715 +
13.716 +LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
13.717 + struct CCallS c;
13.718 + int status;
13.719 + lua_lock(L);
13.720 + c.func = func;
13.721 + c.ud = ud;
13.722 + status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
13.723 + lua_unlock(L);
13.724 + return status;
13.725 +}
13.726 +
13.727 +
13.728 +LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *data,
13.729 + const char *chunkname) {
13.730 + ZIO z;
13.731 + int status;
13.732 + int c;
13.733 + lua_lock(L);
13.734 + if (!chunkname) chunkname = "?";
13.735 + luaZ_init(&z, reader, data, chunkname);
13.736 + c = luaZ_lookahead(&z);
13.737 + status = luaD_protectedparser(L, &z, (c == LUA_SIGNATURE[0]));
13.738 + lua_unlock(L);
13.739 + return status;
13.740 +}
13.741 +
13.742 +
13.743 +LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data) {
13.744 + int status;
13.745 + TObject *o;
13.746 + lua_lock(L);
13.747 + api_checknelems(L, 1);
13.748 + o = L->top - 1;
13.749 + if (isLfunction(o) && clvalue(o)->l.nupvalues == 0) {
13.750 + luaU_dump(L, clvalue(o)->l.p, writer, data);
13.751 + status = 1;
13.752 + }
13.753 + else
13.754 + status = 0;
13.755 + lua_unlock(L);
13.756 + return status;
13.757 +}
13.758 +
13.759 +
13.760 +/*
13.761 +** Garbage-collection functions
13.762 +*/
13.763 +
13.764 +/* GC values are expressed in Kbytes: #bytes/2^10 */
13.765 +#define GCscalel(x) ((x)>>10)
13.766 +#define GCscale(x) (cast(int, GCscalel(x)))
13.767 +#define GCunscale(x) (cast(lu_mem, x)<<10)
13.768 +
13.769 +LUA_API int lua_getgcthreshold (lua_State *L) {
13.770 + int threshold;
13.771 + lua_lock(L);
13.772 + threshold = GCscale(G(L)->GCthreshold);
13.773 + lua_unlock(L);
13.774 + return threshold;
13.775 +}
13.776 +
13.777 +LUA_API int lua_getgccount (lua_State *L) {
13.778 + int count;
13.779 + lua_lock(L);
13.780 + count = GCscale(G(L)->nblocks);
13.781 + lua_unlock(L);
13.782 + return count;
13.783 +}
13.784 +
13.785 +LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
13.786 + lua_lock(L);
13.787 + if (cast(lu_mem, newthreshold) > GCscalel(MAX_LUMEM))
13.788 + G(L)->GCthreshold = MAX_LUMEM;
13.789 + else
13.790 + G(L)->GCthreshold = GCunscale(newthreshold);
13.791 + luaC_checkGC(L);
13.792 + lua_unlock(L);
13.793 +}
13.794 +
13.795 +
13.796 +/*
13.797 +** miscellaneous functions
13.798 +*/
13.799 +
13.800 +
13.801 +LUA_API const char *lua_version (void) {
13.802 + return LUA_VERSION;
13.803 +}
13.804 +
13.805 +
13.806 +LUA_API int lua_error (lua_State *L) {
13.807 + lua_lock(L);
13.808 + api_checknelems(L, 1);
13.809 + luaG_errormsg(L);
13.810 + lua_unlock(L);
13.811 + return 0; /* to avoid warnings */
13.812 +}
13.813 +
13.814 +
13.815 +LUA_API int lua_next (lua_State *L, int idx) {
13.816 + StkId t;
13.817 + int more;
13.818 + lua_lock(L);
13.819 + t = luaA_index(L, idx);
13.820 + api_check(L, ttistable(t));
13.821 + more = luaH_next(L, hvalue(t), L->top - 1);
13.822 + if (more) {
13.823 + api_incr_top(L);
13.824 + }
13.825 + else /* no more elements */
13.826 + L->top -= 1; /* remove key */
13.827 + lua_unlock(L);
13.828 + return more;
13.829 +}
13.830 +
13.831 +
13.832 +LUA_API void lua_concat (lua_State *L, int n) {
13.833 + lua_lock(L);
13.834 + luaC_checkGC(L);
13.835 + api_checknelems(L, n);
13.836 + if (n >= 2) {
13.837 + luaV_concat(L, n, L->top - L->base - 1);
13.838 + L->top -= (n-1);
13.839 + }
13.840 + else if (n == 0) { /* push empty string */
13.841 + setsvalue2s(L->top, luaS_newlstr(L, NULL, 0));
13.842 + api_incr_top(L);
13.843 + }
13.844 + /* else n == 1; nothing to do */
13.845 + lua_unlock(L);
13.846 +}
13.847 +
13.848 +
13.849 +LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
13.850 + Udata *u;
13.851 + lua_lock(L);
13.852 + luaC_checkGC(L);
13.853 + u = luaS_newudata(L, size);
13.854 + setuvalue(L->top, u);
13.855 + api_incr_top(L);
13.856 + lua_unlock(L);
13.857 + return u + 1;
13.858 +}
13.859 +
13.860 +
13.861 +LUA_API int lua_pushupvalues (lua_State *L) {
13.862 + Closure *func;
13.863 + int n, i;
13.864 + lua_lock(L);
13.865 + api_check(L, iscfunction(L->base - 1));
13.866 + func = clvalue(L->base - 1);
13.867 + n = func->c.nupvalues;
13.868 + luaD_checkstack(L, n + LUA_MINSTACK);
13.869 + for (i=0; i<n; i++) {
13.870 + setobj2s(L->top, &func->c.upvalue[i]);
13.871 + L->top++;
13.872 + }
13.873 + lua_unlock(L);
13.874 + return n;
13.875 +}
13.876 +
13.877 +
13.878 +static const char *aux_upvalue (lua_State *L, int funcindex, int n,
13.879 + TObject **val) {
13.880 + Closure *f;
13.881 + StkId fi = luaA_index(L, funcindex);
13.882 + if (!ttisfunction(fi)) return NULL;
13.883 + f = clvalue(fi);
13.884 + if (f->c.isC) {
13.885 + if (n > f->c.nupvalues) return NULL;
13.886 + *val = &f->c.upvalue[n-1];
13.887 + return "";
13.888 + }
13.889 + else {
13.890 + Proto *p = f->l.p;
13.891 + if (n > p->sizeupvalues) return NULL;
13.892 + *val = f->l.upvals[n-1]->v;
13.893 + return getstr(p->upvalues[n-1]);
13.894 + }
13.895 +}
13.896 +
13.897 +
13.898 +LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
13.899 + const char *name;
13.900 + TObject *val;
13.901 + lua_lock(L);
13.902 + name = aux_upvalue(L, funcindex, n, &val);
13.903 + if (name) {
13.904 + setobj2s(L->top, val);
13.905 + api_incr_top(L);
13.906 + }
13.907 + lua_unlock(L);
13.908 + return name;
13.909 +}
13.910 +
13.911 +
13.912 +LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
13.913 + const char *name;
13.914 + TObject *val;
13.915 + lua_lock(L);
13.916 + api_checknelems(L, 1);
13.917 + name = aux_upvalue(L, funcindex, n, &val);
13.918 + if (name) {
13.919 + L->top--;
13.920 + setobj(val, L->top); /* write barrier */
13.921 + }
13.922 + lua_unlock(L);
13.923 + return name;
13.924 +}
13.925 +
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
14.2 +++ b/source/lua/src/lapi.h Wed Nov 04 00:31:14 2009 +0000
14.3 @@ -0,0 +1,16 @@
14.4 +/*
14.5 +** $Id$
14.6 +** Auxiliary functions from Lua API
14.7 +** See Copyright Notice in lua.h
14.8 +*/
14.9 +
14.10 +#ifndef lapi_h
14.11 +#define lapi_h
14.12 +
14.13 +
14.14 +#include "lobject.h"
14.15 +
14.16 +
14.17 +void luaA_pushobject (lua_State *L, const TObject *o);
14.18 +
14.19 +#endif
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
15.2 +++ b/source/lua/src/lcode.c Wed Nov 04 00:31:14 2009 +0000
15.3 @@ -0,0 +1,714 @@
15.4 +/*
15.5 +** $Id$
15.6 +** Code generator for Lua
15.7 +** See Copyright Notice in lua.h
15.8 +*/
15.9 +
15.10 +
15.11 +#include <stdlib.h>
15.12 +
15.13 +#define lcode_c
15.14 +
15.15 +#include "lua.h"
15.16 +
15.17 +#include "lcode.h"
15.18 +#include "ldebug.h"
15.19 +#include "ldo.h"
15.20 +#include "llex.h"
15.21 +#include "lmem.h"
15.22 +#include "lobject.h"
15.23 +#include "lopcodes.h"
15.24 +#include "lparser.h"
15.25 +#include "ltable.h"
15.26 +
15.27 +
15.28 +#define hasjumps(e) ((e)->t != (e)->f)
15.29 +
15.30 +
15.31 +void luaK_nil (FuncState *fs, int from, int n) {
15.32 + Instruction *previous;
15.33 + if (fs->pc > fs->lasttarget && /* no jumps to current position? */
15.34 + GET_OPCODE(*(previous = &fs->f->code[fs->pc-1])) == OP_LOADNIL) {
15.35 + int pfrom = GETARG_A(*previous);
15.36 + int pto = GETARG_B(*previous);
15.37 + if (pfrom <= from && from <= pto+1) { /* can connect both? */
15.38 + if (from+n-1 > pto)
15.39 + SETARG_B(*previous, from+n-1);
15.40 + return;
15.41 + }
15.42 + }
15.43 + luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
15.44 +}
15.45 +
15.46 +
15.47 +int luaK_jump (FuncState *fs) {
15.48 + int jpc = fs->jpc; /* save list of jumps to here */
15.49 + int j;
15.50 + fs->jpc = NO_JUMP;
15.51 + j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
15.52 + luaK_concat(fs, &j, jpc); /* keep them on hold */
15.53 + return j;
15.54 +}
15.55 +
15.56 +
15.57 +static int luaK_condjump (FuncState *fs, OpCode op, int A, int B, int C) {
15.58 + luaK_codeABC(fs, op, A, B, C);
15.59 + return luaK_jump(fs);
15.60 +}
15.61 +
15.62 +
15.63 +static void luaK_fixjump (FuncState *fs, int pc, int dest) {
15.64 + Instruction *jmp = &fs->f->code[pc];
15.65 + int offset = dest-(pc+1);
15.66 + lua_assert(dest != NO_JUMP);
15.67 + if (abs(offset) > MAXARG_sBx)
15.68 + luaX_syntaxerror(fs->ls, "control structure too long");
15.69 + SETARG_sBx(*jmp, offset);
15.70 +}
15.71 +
15.72 +
15.73 +/*
15.74 +** returns current `pc' and marks it as a jump target (to avoid wrong
15.75 +** optimizations with consecutive instructions not in the same basic block).
15.76 +*/
15.77 +int luaK_getlabel (FuncState *fs) {
15.78 + fs->lasttarget = fs->pc;
15.79 + return fs->pc;
15.80 +}
15.81 +
15.82 +
15.83 +static int luaK_getjump (FuncState *fs, int pc) {
15.84 + int offset = GETARG_sBx(fs->f->code[pc]);
15.85 + if (offset == NO_JUMP) /* point to itself represents end of list */
15.86 + return NO_JUMP; /* end of list */
15.87 + else
15.88 + return (pc+1)+offset; /* turn offset into absolute position */
15.89 +}
15.90 +
15.91 +
15.92 +static Instruction *getjumpcontrol (FuncState *fs, int pc) {
15.93 + Instruction *pi = &fs->f->code[pc];
15.94 + if (pc >= 1 && testOpMode(GET_OPCODE(*(pi-1)), OpModeT))
15.95 + return pi-1;
15.96 + else
15.97 + return pi;
15.98 +}
15.99 +
15.100 +
15.101 +/*
15.102 +** check whether list has any jump that do not produce a value
15.103 +** (or produce an inverted value)
15.104 +*/
15.105 +static int need_value (FuncState *fs, int list, int cond) {
15.106 + for (; list != NO_JUMP; list = luaK_getjump(fs, list)) {
15.107 + Instruction i = *getjumpcontrol(fs, list);
15.108 + if (GET_OPCODE(i) != OP_TEST || GETARG_C(i) != cond) return 1;
15.109 + }
15.110 + return 0; /* not found */
15.111 +}
15.112 +
15.113 +
15.114 +static void patchtestreg (Instruction *i, int reg) {
15.115 + if (reg == NO_REG) reg = GETARG_B(*i);
15.116 + SETARG_A(*i, reg);
15.117 +}
15.118 +
15.119 +
15.120 +static void luaK_patchlistaux (FuncState *fs, int list,
15.121 + int ttarget, int treg, int ftarget, int freg, int dtarget) {
15.122 + while (list != NO_JUMP) {
15.123 + int next = luaK_getjump(fs, list);
15.124 + Instruction *i = getjumpcontrol(fs, list);
15.125 + if (GET_OPCODE(*i) != OP_TEST) {
15.126 + lua_assert(dtarget != NO_JUMP);
15.127 + luaK_fixjump(fs, list, dtarget); /* jump to default target */
15.128 + }
15.129 + else {
15.130 + if (GETARG_C(*i)) {
15.131 + lua_assert(ttarget != NO_JUMP);
15.132 + patchtestreg(i, treg);
15.133 + luaK_fixjump(fs, list, ttarget);
15.134 + }
15.135 + else {
15.136 + lua_assert(ftarget != NO_JUMP);
15.137 + patchtestreg(i, freg);
15.138 + luaK_fixjump(fs, list, ftarget);
15.139 + }
15.140 + }
15.141 + list = next;
15.142 + }
15.143 +}
15.144 +
15.145 +
15.146 +static void luaK_dischargejpc (FuncState *fs) {
15.147 + luaK_patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc, NO_REG, fs->pc);
15.148 + fs->jpc = NO_JUMP;
15.149 +}
15.150 +
15.151 +
15.152 +void luaK_patchlist (FuncState *fs, int list, int target) {
15.153 + if (target == fs->pc)
15.154 + luaK_patchtohere(fs, list);
15.155 + else {
15.156 + lua_assert(target < fs->pc);
15.157 + luaK_patchlistaux(fs, list, target, NO_REG, target, NO_REG, target);
15.158 + }
15.159 +}
15.160 +
15.161 +
15.162 +void luaK_patchtohere (FuncState *fs, int list) {
15.163 + luaK_getlabel(fs);
15.164 + luaK_concat(fs, &fs->jpc, list);
15.165 +}
15.166 +
15.167 +
15.168 +void luaK_concat (FuncState *fs, int *l1, int l2) {
15.169 + if (l2 == NO_JUMP) return;
15.170 + else if (*l1 == NO_JUMP)
15.171 + *l1 = l2;
15.172 + else {
15.173 + int list = *l1;
15.174 + int next;
15.175 + while ((next = luaK_getjump(fs, list)) != NO_JUMP) /* find last element */
15.176 + list = next;
15.177 + luaK_fixjump(fs, list, l2);
15.178 + }
15.179 +}
15.180 +
15.181 +
15.182 +void luaK_checkstack (FuncState *fs, int n) {
15.183 + int newstack = fs->freereg + n;
15.184 + if (newstack > fs->f->maxstacksize) {
15.185 + if (newstack >= MAXSTACK)
15.186 + luaX_syntaxerror(fs->ls, "function or expression too complex");
15.187 + fs->f->maxstacksize = cast(lu_byte, newstack);
15.188 + }
15.189 +}
15.190 +
15.191 +
15.192 +void luaK_reserveregs (FuncState *fs, int n) {
15.193 + luaK_checkstack(fs, n);
15.194 + fs->freereg += n;
15.195 +}
15.196 +
15.197 +
15.198 +static void freereg (FuncState *fs, int reg) {
15.199 + if (reg >= fs->nactvar && reg < MAXSTACK) {
15.200 + fs->freereg--;
15.201 + lua_assert(reg == fs->freereg);
15.202 + }
15.203 +}
15.204 +
15.205 +
15.206 +static void freeexp (FuncState *fs, expdesc *e) {
15.207 + if (e->k == VNONRELOC)
15.208 + freereg(fs, e->info);
15.209 +}
15.210 +
15.211 +
15.212 +static int addk (FuncState *fs, TObject *k, TObject *v) {
15.213 + const TObject *idx = luaH_get(fs->h, k);
15.214 + if (ttisnumber(idx)) {
15.215 + lua_assert(luaO_rawequalObj(&fs->f->k[cast(int, nvalue(idx))], v));
15.216 + return cast(int, nvalue(idx));
15.217 + }
15.218 + else { /* constant not found; create a new entry */
15.219 + Proto *f = fs->f;
15.220 + luaM_growvector(fs->L, f->k, fs->nk, f->sizek, TObject,
15.221 + MAXARG_Bx, "constant table overflow");
15.222 + setobj2n(&f->k[fs->nk], v);
15.223 + setnvalue(luaH_set(fs->L, fs->h, k), cast(lua_Number, fs->nk));
15.224 + return fs->nk++;
15.225 + }
15.226 +}
15.227 +
15.228 +
15.229 +int luaK_stringK (FuncState *fs, TString *s) {
15.230 + TObject o;
15.231 + setsvalue(&o, s);
15.232 + return addk(fs, &o, &o);
15.233 +}
15.234 +
15.235 +
15.236 +int luaK_numberK (FuncState *fs, lua_Number r) {
15.237 + TObject o;
15.238 + setnvalue(&o, r);
15.239 + return addk(fs, &o, &o);
15.240 +}
15.241 +
15.242 +
15.243 +static int nil_constant (FuncState *fs) {
15.244 + TObject k, v;
15.245 + setnilvalue(&v);
15.246 + sethvalue(&k, fs->h); /* cannot use nil as key; instead use table itself */
15.247 + return addk(fs, &k, &v);
15.248 +}
15.249 +
15.250 +
15.251 +void luaK_setcallreturns (FuncState *fs, expdesc *e, int nresults) {
15.252 + if (e->k == VCALL) { /* expression is an open function call? */
15.253 + SETARG_C(getcode(fs, e), nresults+1);
15.254 + if (nresults == 1) { /* `regular' expression? */
15.255 + e->k = VNONRELOC;
15.256 + e->info = GETARG_A(getcode(fs, e));
15.257 + }
15.258 + }
15.259 +}
15.260 +
15.261 +
15.262 +void luaK_dischargevars (FuncState *fs, expdesc *e) {
15.263 + switch (e->k) {
15.264 + case VLOCAL: {
15.265 + e->k = VNONRELOC;
15.266 + break;
15.267 + }
15.268 + case VUPVAL: {
15.269 + e->info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->info, 0);
15.270 + e->k = VRELOCABLE;
15.271 + break;
15.272 + }
15.273 + case VGLOBAL: {
15.274 + e->info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->info);
15.275 + e->k = VRELOCABLE;
15.276 + break;
15.277 + }
15.278 + case VINDEXED: {
15.279 + freereg(fs, e->aux);
15.280 + freereg(fs, e->info);
15.281 + e->info = luaK_codeABC(fs, OP_GETTABLE, 0, e->info, e->aux);
15.282 + e->k = VRELOCABLE;
15.283 + break;
15.284 + }
15.285 + case VCALL: {
15.286 + luaK_setcallreturns(fs, e, 1);
15.287 + break;
15.288 + }
15.289 + default: break; /* there is one value available (somewhere) */
15.290 + }
15.291 +}
15.292 +
15.293 +
15.294 +static int code_label (FuncState *fs, int A, int b, int jump) {
15.295 + luaK_getlabel(fs); /* those instructions may be jump targets */
15.296 + return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
15.297 +}
15.298 +
15.299 +
15.300 +static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
15.301 + luaK_dischargevars(fs, e);
15.302 + switch (e->k) {
15.303 + case VNIL: {
15.304 + luaK_nil(fs, reg, 1);
15.305 + break;
15.306 + }
15.307 + case VFALSE: case VTRUE: {
15.308 + luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
15.309 + break;
15.310 + }
15.311 + case VK: {
15.312 + luaK_codeABx(fs, OP_LOADK, reg, e->info);
15.313 + break;
15.314 + }
15.315 + case VRELOCABLE: {
15.316 + Instruction *pc = &getcode(fs, e);
15.317 + SETARG_A(*pc, reg);
15.318 + break;
15.319 + }
15.320 + case VNONRELOC: {
15.321 + if (reg != e->info)
15.322 + luaK_codeABC(fs, OP_MOVE, reg, e->info, 0);
15.323 + break;
15.324 + }
15.325 + default: {
15.326 + lua_assert(e->k == VVOID || e->k == VJMP);
15.327 + return; /* nothing to do... */
15.328 + }
15.329 + }
15.330 + e->info = reg;
15.331 + e->k = VNONRELOC;
15.332 +}
15.333 +
15.334 +
15.335 +static void discharge2anyreg (FuncState *fs, expdesc *e) {
15.336 + if (e->k != VNONRELOC) {
15.337 + luaK_reserveregs(fs, 1);
15.338 + discharge2reg(fs, e, fs->freereg-1);
15.339 + }
15.340 +}
15.341 +
15.342 +
15.343 +static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) {
15.344 + discharge2reg(fs, e, reg);
15.345 + if (e->k == VJMP)
15.346 + luaK_concat(fs, &e->t, e->info); /* put this jump in `t' list */
15.347 + if (hasjumps(e)) {
15.348 + int final; /* position after whole expression */
15.349 + int p_f = NO_JUMP; /* position of an eventual LOAD false */
15.350 + int p_t = NO_JUMP; /* position of an eventual LOAD true */
15.351 + if (need_value(fs, e->t, 1) || need_value(fs, e->f, 0)) {
15.352 + int fj = NO_JUMP; /* first jump (over LOAD ops.) */
15.353 + if (e->k != VJMP)
15.354 + fj = luaK_jump(fs);
15.355 + p_f = code_label(fs, reg, 0, 1);
15.356 + p_t = code_label(fs, reg, 1, 0);
15.357 + luaK_patchtohere(fs, fj);
15.358 + }
15.359 + final = luaK_getlabel(fs);
15.360 + luaK_patchlistaux(fs, e->f, p_f, NO_REG, final, reg, p_f);
15.361 + luaK_patchlistaux(fs, e->t, final, reg, p_t, NO_REG, p_t);
15.362 + }
15.363 + e->f = e->t = NO_JUMP;
15.364 + e->info = reg;
15.365 + e->k = VNONRELOC;
15.366 +}
15.367 +
15.368 +
15.369 +void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
15.370 + luaK_dischargevars(fs, e);
15.371 + freeexp(fs, e);
15.372 + luaK_reserveregs(fs, 1);
15.373 + luaK_exp2reg(fs, e, fs->freereg - 1);
15.374 +}
15.375 +
15.376 +
15.377 +int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
15.378 + luaK_dischargevars(fs, e);
15.379 + if (e->k == VNONRELOC) {
15.380 + if (!hasjumps(e)) return e->info; /* exp is already in a register */
15.381 + if (e->info >= fs->nactvar) { /* reg. is not a local? */
15.382 + luaK_exp2reg(fs, e, e->info); /* put value on it */
15.383 + return e->info;
15.384 + }
15.385 + }
15.386 + luaK_exp2nextreg(fs, e); /* default */
15.387 + return e->info;
15.388 +}
15.389 +
15.390 +
15.391 +void luaK_exp2val (FuncState *fs, expdesc *e) {
15.392 + if (hasjumps(e))
15.393 + luaK_exp2anyreg(fs, e);
15.394 + else
15.395 + luaK_dischargevars(fs, e);
15.396 +}
15.397 +
15.398 +
15.399 +int luaK_exp2RK (FuncState *fs, expdesc *e) {
15.400 + luaK_exp2val(fs, e);
15.401 + switch (e->k) {
15.402 + case VNIL: {
15.403 + if (fs->nk + MAXSTACK <= MAXARG_C) { /* constant fit in argC? */
15.404 + e->info = nil_constant(fs);
15.405 + e->k = VK;
15.406 + return e->info + MAXSTACK;
15.407 + }
15.408 + else break;
15.409 + }
15.410 + case VK: {
15.411 + if (e->info + MAXSTACK <= MAXARG_C) /* constant fit in argC? */
15.412 + return e->info + MAXSTACK;
15.413 + else break;
15.414 + }
15.415 + default: break;
15.416 + }
15.417 + /* not a constant in the right range: put it in a register */
15.418 + return luaK_exp2anyreg(fs, e);
15.419 +}
15.420 +
15.421 +
15.422 +void luaK_storevar (FuncState *fs, expdesc *var, expdesc *exp) {
15.423 + switch (var->k) {
15.424 + case VLOCAL: {
15.425 + freeexp(fs, exp);
15.426 + luaK_exp2reg(fs, exp, var->info);
15.427 + return;
15.428 + }
15.429 + case VUPVAL: {
15.430 + int e = luaK_exp2anyreg(fs, exp);
15.431 + luaK_codeABC(fs, OP_SETUPVAL, e, var->info, 0);
15.432 + break;
15.433 + }
15.434 + case VGLOBAL: {
15.435 + int e = luaK_exp2anyreg(fs, exp);
15.436 + luaK_codeABx(fs, OP_SETGLOBAL, e, var->info);
15.437 + break;
15.438 + }
15.439 + case VINDEXED: {
15.440 + int e = luaK_exp2RK(fs, exp);
15.441 + luaK_codeABC(fs, OP_SETTABLE, var->info, var->aux, e);
15.442 + break;
15.443 + }
15.444 + default: {
15.445 + lua_assert(0); /* invalid var kind to store */
15.446 + break;
15.447 + }
15.448 + }
15.449 + freeexp(fs, exp);
15.450 +}
15.451 +
15.452 +
15.453 +void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
15.454 + int func;
15.455 + luaK_exp2anyreg(fs, e);
15.456 + freeexp(fs, e);
15.457 + func = fs->freereg;
15.458 + luaK_reserveregs(fs, 2);
15.459 + luaK_codeABC(fs, OP_SELF, func, e->info, luaK_exp2RK(fs, key));
15.460 + freeexp(fs, key);
15.461 + e->info = func;
15.462 + e->k = VNONRELOC;
15.463 +}
15.464 +
15.465 +
15.466 +static void invertjump (FuncState *fs, expdesc *e) {
15.467 + Instruction *pc = getjumpcontrol(fs, e->info);
15.468 + lua_assert(testOpMode(GET_OPCODE(*pc), OpModeT) &&
15.469 + GET_OPCODE(*pc) != OP_TEST);
15.470 + SETARG_A(*pc, !(GETARG_A(*pc)));
15.471 +}
15.472 +
15.473 +
15.474 +static int jumponcond (FuncState *fs, expdesc *e, int cond) {
15.475 + if (e->k == VRELOCABLE) {
15.476 + Instruction ie = getcode(fs, e);
15.477 + if (GET_OPCODE(ie) == OP_NOT) {
15.478 + fs->pc--; /* remove previous OP_NOT */
15.479 + return luaK_condjump(fs, OP_TEST, NO_REG, GETARG_B(ie), !cond);
15.480 + }
15.481 + /* else go through */
15.482 + }
15.483 + discharge2anyreg(fs, e);
15.484 + freeexp(fs, e);
15.485 + return luaK_condjump(fs, OP_TEST, NO_REG, e->info, cond);
15.486 +}
15.487 +
15.488 +
15.489 +void luaK_goiftrue (FuncState *fs, expdesc *e) {
15.490 + int pc; /* pc of last jump */
15.491 + luaK_dischargevars(fs, e);
15.492 + switch (e->k) {
15.493 + case VK: case VTRUE: {
15.494 + pc = NO_JUMP; /* always true; do nothing */
15.495 + break;
15.496 + }
15.497 + case VFALSE: {
15.498 + pc = luaK_jump(fs); /* always jump */
15.499 + break;
15.500 + }
15.501 + case VJMP: {
15.502 + invertjump(fs, e);
15.503 + pc = e->info;
15.504 + break;
15.505 + }
15.506 + default: {
15.507 + pc = jumponcond(fs, e, 0);
15.508 + break;
15.509 + }
15.510 + }
15.511 + luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
15.512 +}
15.513 +
15.514 +
15.515 +void luaK_goiffalse (FuncState *fs, expdesc *e) {
15.516 + int pc; /* pc of last jump */
15.517 + luaK_dischargevars(fs, e);
15.518 + switch (e->k) {
15.519 + case VNIL: case VFALSE: {
15.520 + pc = NO_JUMP; /* always false; do nothing */
15.521 + break;
15.522 + }
15.523 + case VTRUE: {
15.524 + pc = luaK_jump(fs); /* always jump */
15.525 + break;
15.526 + }
15.527 + case VJMP: {
15.528 + pc = e->info;
15.529 + break;
15.530 + }
15.531 + default: {
15.532 + pc = jumponcond(fs, e, 1);
15.533 + break;
15.534 + }
15.535 + }
15.536 + luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
15.537 +}
15.538 +
15.539 +
15.540 +static void codenot (FuncState *fs, expdesc *e) {
15.541 + luaK_dischargevars(fs, e);
15.542 + switch (e->k) {
15.543 + case VNIL: case VFALSE: {
15.544 + e->k = VTRUE;
15.545 + break;
15.546 + }
15.547 + case VK: case VTRUE: {
15.548 + e->k = VFALSE;
15.549 + break;
15.550 + }
15.551 + case VJMP: {
15.552 + invertjump(fs, e);
15.553 + break;
15.554 + }
15.555 + case VRELOCABLE:
15.556 + case VNONRELOC: {
15.557 + discharge2anyreg(fs, e);
15.558 + freeexp(fs, e);
15.559 + e->info = luaK_codeABC(fs, OP_NOT, 0, e->info, 0);
15.560 + e->k = VRELOCABLE;
15.561 + break;
15.562 + }
15.563 + default: {
15.564 + lua_assert(0); /* cannot happen */
15.565 + break;
15.566 + }
15.567 + }
15.568 + /* interchange true and false lists */
15.569 + { int temp = e->f; e->f = e->t; e->t = temp; }
15.570 +}
15.571 +
15.572 +
15.573 +void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
15.574 + t->aux = luaK_exp2RK(fs, k);
15.575 + t->k = VINDEXED;
15.576 +}
15.577 +
15.578 +
15.579 +void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
15.580 + if (op == OPR_MINUS) {
15.581 + luaK_exp2val(fs, e);
15.582 + if (e->k == VK && ttisnumber(&fs->f->k[e->info]))
15.583 + e->info = luaK_numberK(fs, -nvalue(&fs->f->k[e->info]));
15.584 + else {
15.585 + luaK_exp2anyreg(fs, e);
15.586 + freeexp(fs, e);
15.587 + e->info = luaK_codeABC(fs, OP_UNM, 0, e->info, 0);
15.588 + e->k = VRELOCABLE;
15.589 + }
15.590 + }
15.591 + else /* op == NOT */
15.592 + codenot(fs, e);
15.593 +}
15.594 +
15.595 +
15.596 +void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
15.597 + switch (op) {
15.598 + case OPR_AND: {
15.599 + luaK_goiftrue(fs, v);
15.600 + luaK_patchtohere(fs, v->t);
15.601 + v->t = NO_JUMP;
15.602 + break;
15.603 + }
15.604 + case OPR_OR: {
15.605 + luaK_goiffalse(fs, v);
15.606 + luaK_patchtohere(fs, v->f);
15.607 + v->f = NO_JUMP;
15.608 + break;
15.609 + }
15.610 + case OPR_CONCAT: {
15.611 + luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
15.612 + break;
15.613 + }
15.614 + default: {
15.615 + luaK_exp2RK(fs, v);
15.616 + break;
15.617 + }
15.618 + }
15.619 +}
15.620 +
15.621 +
15.622 +static void codebinop (FuncState *fs, expdesc *res, BinOpr op,
15.623 + int o1, int o2) {
15.624 + if (op <= OPR_POW) { /* arithmetic operator? */
15.625 + OpCode opc = cast(OpCode, (op - OPR_ADD) + OP_ADD); /* ORDER OP */
15.626 + res->info = luaK_codeABC(fs, opc, 0, o1, o2);
15.627 + res->k = VRELOCABLE;
15.628 + }
15.629 + else { /* test operator */
15.630 + static const OpCode ops[] = {OP_EQ, OP_EQ, OP_LT, OP_LE, OP_LT, OP_LE};
15.631 + int cond = 1;
15.632 + if (op >= OPR_GT) { /* `>' or `>='? */
15.633 + int temp; /* exchange args and replace by `<' or `<=' */
15.634 + temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
15.635 + }
15.636 + else if (op == OPR_NE) cond = 0;
15.637 + res->info = luaK_condjump(fs, ops[op - OPR_NE], cond, o1, o2);
15.638 + res->k = VJMP;
15.639 + }
15.640 +}
15.641 +
15.642 +
15.643 +void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
15.644 + switch (op) {
15.645 + case OPR_AND: {
15.646 + lua_assert(e1->t == NO_JUMP); /* list must be closed */
15.647 + luaK_dischargevars(fs, e2);
15.648 + luaK_concat(fs, &e1->f, e2->f);
15.649 + e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->t = e2->t;
15.650 + break;
15.651 + }
15.652 + case OPR_OR: {
15.653 + lua_assert(e1->f == NO_JUMP); /* list must be closed */
15.654 + luaK_dischargevars(fs, e2);
15.655 + luaK_concat(fs, &e1->t, e2->t);
15.656 + e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->f = e2->f;
15.657 + break;
15.658 + }
15.659 + case OPR_CONCAT: {
15.660 + luaK_exp2val(fs, e2);
15.661 + if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
15.662 + lua_assert(e1->info == GETARG_B(getcode(fs, e2))-1);
15.663 + freeexp(fs, e1);
15.664 + SETARG_B(getcode(fs, e2), e1->info);
15.665 + e1->k = e2->k; e1->info = e2->info;
15.666 + }
15.667 + else {
15.668 + luaK_exp2nextreg(fs, e2);
15.669 + freeexp(fs, e2);
15.670 + freeexp(fs, e1);
15.671 + e1->info = luaK_codeABC(fs, OP_CONCAT, 0, e1->info, e2->info);
15.672 + e1->k = VRELOCABLE;
15.673 + }
15.674 + break;
15.675 + }
15.676 + default: {
15.677 + int o1 = luaK_exp2RK(fs, e1);
15.678 + int o2 = luaK_exp2RK(fs, e2);
15.679 + freeexp(fs, e2);
15.680 + freeexp(fs, e1);
15.681 + codebinop(fs, e1, op, o1, o2);
15.682 + }
15.683 + }
15.684 +}
15.685 +
15.686 +
15.687 +void luaK_fixline (FuncState *fs, int line) {
15.688 + fs->f->lineinfo[fs->pc - 1] = line;
15.689 +}
15.690 +
15.691 +
15.692 +int luaK_code (FuncState *fs, Instruction i, int line) {
15.693 + Proto *f = fs->f;
15.694 + luaK_dischargejpc(fs); /* `pc' will change */
15.695 + /* put new instruction in code array */
15.696 + luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
15.697 + MAX_INT, "code size overflow");
15.698 + f->code[fs->pc] = i;
15.699 + /* save corresponding line information */
15.700 + luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
15.701 + MAX_INT, "code size overflow");
15.702 + f->lineinfo[fs->pc] = line;
15.703 + return fs->pc++;
15.704 +}
15.705 +
15.706 +
15.707 +int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
15.708 + lua_assert(getOpMode(o) == iABC);
15.709 + return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline);
15.710 +}
15.711 +
15.712 +
15.713 +int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
15.714 + lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
15.715 + return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline);
15.716 +}
15.717 +
16.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
16.2 +++ b/source/lua/src/lcode.h Wed Nov 04 00:31:14 2009 +0000
16.3 @@ -0,0 +1,74 @@
16.4 +/*
16.5 +** $Id$
16.6 +** Code generator for Lua
16.7 +** See Copyright Notice in lua.h
16.8 +*/
16.9 +
16.10 +#ifndef lcode_h
16.11 +#define lcode_h
16.12 +
16.13 +#include "llex.h"
16.14 +#include "lobject.h"
16.15 +#include "lopcodes.h"
16.16 +#include "lparser.h"
16.17 +
16.18 +
16.19 +/*
16.20 +** Marks the end of a patch list. It is an invalid value both as an absolute
16.21 +** address, and as a list link (would link an element to itself).
16.22 +*/
16.23 +#define NO_JUMP (-1)
16.24 +
16.25 +
16.26 +/*
16.27 +** grep "ORDER OPR" if you change these enums
16.28 +*/
16.29 +typedef enum BinOpr {
16.30 + OPR_ADD, OPR_SUB, OPR_MULT, OPR_DIV, OPR_POW,
16.31 + OPR_CONCAT,
16.32 + OPR_NE, OPR_EQ,
16.33 + OPR_LT, OPR_LE, OPR_GT, OPR_GE,
16.34 + OPR_AND, OPR_OR,
16.35 + OPR_NOBINOPR
16.36 +} BinOpr;
16.37 +
16.38 +#define binopistest(op) ((op) >= OPR_NE)
16.39 +
16.40 +typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_NOUNOPR } UnOpr;
16.41 +
16.42 +
16.43 +#define getcode(fs,e) ((fs)->f->code[(e)->info])
16.44 +
16.45 +#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)
16.46 +
16.47 +int luaK_code (FuncState *fs, Instruction i, int line);
16.48 +int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
16.49 +int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C);
16.50 +void luaK_fixline (FuncState *fs, int line);
16.51 +void luaK_nil (FuncState *fs, int from, int n);
16.52 +void luaK_reserveregs (FuncState *fs, int n);
16.53 +void luaK_checkstack (FuncState *fs, int n);
16.54 +int luaK_stringK (FuncState *fs, TString *s);
16.55 +int luaK_numberK (FuncState *fs, lua_Number r);
16.56 +void luaK_dischargevars (FuncState *fs, expdesc *e);
16.57 +int luaK_exp2anyreg (FuncState *fs, expdesc *e);
16.58 +void luaK_exp2nextreg (FuncState *fs, expdesc *e);
16.59 +void luaK_exp2val (FuncState *fs, expdesc *e);
16.60 +int luaK_exp2RK (FuncState *fs, expdesc *e);
16.61 +void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
16.62 +void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
16.63 +void luaK_goiftrue (FuncState *fs, expdesc *e);
16.64 +void luaK_goiffalse (FuncState *fs, expdesc *e);
16.65 +void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
16.66 +void luaK_setcallreturns (FuncState *fs, expdesc *var, int nresults);
16.67 +int luaK_jump (FuncState *fs);
16.68 +void luaK_patchlist (FuncState *fs, int list, int target);
16.69 +void luaK_patchtohere (FuncState *fs, int list);
16.70 +void luaK_concat (FuncState *fs, int *l1, int l2);
16.71 +int luaK_getlabel (FuncState *fs);
16.72 +void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v);
16.73 +void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
16.74 +void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2);
16.75 +
16.76 +
16.77 +#endif
17.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
17.2 +++ b/source/lua/src/ldebug.c Wed Nov 04 00:31:14 2009 +0000
17.3 @@ -0,0 +1,585 @@
17.4 +/*
17.5 +** $Id$
17.6 +** Debug Interface
17.7 +** See Copyright Notice in lua.h
17.8 +*/
17.9 +
17.10 +
17.11 +#include <stdlib.h>
17.12 +#include <string.h>
17.13 +
17.14 +#define ldebug_c
17.15 +
17.16 +#include "lua.h"
17.17 +
17.18 +#include "lapi.h"
17.19 +#include "lcode.h"
17.20 +#include "ldebug.h"
17.21 +#include "ldo.h"
17.22 +#include "lfunc.h"
17.23 +#include "lobject.h"
17.24 +#include "lopcodes.h"
17.25 +#include "lstate.h"
17.26 +#include "lstring.h"
17.27 +#include "ltable.h"
17.28 +#include "ltm.h"
17.29 +#include "lvm.h"
17.30 +
17.31 +
17.32 +
17.33 +static const char *getfuncname (CallInfo *ci, const char **name);
17.34 +
17.35 +
17.36 +#define isLua(ci) (!((ci)->state & CI_C))
17.37 +
17.38 +
17.39 +static int currentpc (CallInfo *ci) {
17.40 + if (!isLua(ci)) return -1; /* function is not a Lua function? */
17.41 + if (ci->state & CI_HASFRAME) /* function has a frame? */
17.42 + ci->u.l.savedpc = *ci->u.l.pc; /* use `pc' from there */
17.43 + /* function's pc is saved */
17.44 + return pcRel(ci->u.l.savedpc, ci_func(ci)->l.p);
17.45 +}
17.46 +
17.47 +
17.48 +static int currentline (CallInfo *ci) {
17.49 + int pc = currentpc(ci);
17.50 + if (pc < 0)
17.51 + return -1; /* only active lua functions have current-line information */
17.52 + else
17.53 + return getline(ci_func(ci)->l.p, pc);
17.54 +}
17.55 +
17.56 +
17.57 +void luaG_inithooks (lua_State *L) {
17.58 + CallInfo *ci;
17.59 + for (ci = L->ci; ci != L->base_ci; ci--) /* update all `savedpc's */
17.60 + currentpc(ci);
17.61 + L->hookinit = 1;
17.62 +}
17.63 +
17.64 +
17.65 +/*
17.66 +** this function can be called asynchronous (e.g. during a signal)
17.67 +*/
17.68 +LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
17.69 + if (func == NULL || mask == 0) { /* turn off hooks? */
17.70 + mask = 0;
17.71 + func = NULL;
17.72 + }
17.73 + L->hook = func;
17.74 + L->basehookcount = count;
17.75 + resethookcount(L);
17.76 + L->hookmask = cast(lu_byte, mask);
17.77 + L->hookinit = 0;
17.78 + return 1;
17.79 +}
17.80 +
17.81 +
17.82 +LUA_API lua_Hook lua_gethook (lua_State *L) {
17.83 + return L->hook;
17.84 +}
17.85 +
17.86 +
17.87 +LUA_API int lua_gethookmask (lua_State *L) {
17.88 + return L->hookmask;
17.89 +}
17.90 +
17.91 +
17.92 +LUA_API int lua_gethookcount (lua_State *L) {
17.93 + return L->basehookcount;
17.94 +}
17.95 +
17.96 +
17.97 +LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
17.98 + int status;
17.99 + CallInfo *ci;
17.100 + lua_lock(L);
17.101 + for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) {
17.102 + level--;
17.103 + if (!(ci->state & CI_C)) /* Lua function? */
17.104 + level -= ci->u.l.tailcalls; /* skip lost tail calls */
17.105 + }
17.106 + if (level > 0 || ci == L->base_ci) status = 0; /* there is no such level */
17.107 + else if (level < 0) { /* level is of a lost tail call */
17.108 + status = 1;
17.109 + ar->i_ci = 0;
17.110 + }
17.111 + else {
17.112 + status = 1;
17.113 + ar->i_ci = ci - L->base_ci;
17.114 + }
17.115 + lua_unlock(L);
17.116 + return status;
17.117 +}
17.118 +
17.119 +
17.120 +static Proto *getluaproto (CallInfo *ci) {
17.121 + return (isLua(ci) ? ci_func(ci)->l.p : NULL);
17.122 +}
17.123 +
17.124 +
17.125 +LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
17.126 + const char *name;
17.127 + CallInfo *ci;
17.128 + Proto *fp;
17.129 + lua_lock(L);
17.130 + name = NULL;
17.131 + ci = L->base_ci + ar->i_ci;
17.132 + fp = getluaproto(ci);
17.133 + if (fp) { /* is a Lua function? */
17.134 + name = luaF_getlocalname(fp, n, currentpc(ci));
17.135 + if (name)
17.136 + luaA_pushobject(L, ci->base+(n-1)); /* push value */
17.137 + }
17.138 + lua_unlock(L);
17.139 + return name;
17.140 +}
17.141 +
17.142 +
17.143 +LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
17.144 + const char *name;
17.145 + CallInfo *ci;
17.146 + Proto *fp;
17.147 + lua_lock(L);
17.148 + name = NULL;
17.149 + ci = L->base_ci + ar->i_ci;
17.150 + fp = getluaproto(ci);
17.151 + L->top--; /* pop new value */
17.152 + if (fp) { /* is a Lua function? */
17.153 + name = luaF_getlocalname(fp, n, currentpc(ci));
17.154 + if (!name || name[0] == '(') /* `(' starts private locals */
17.155 + name = NULL;
17.156 + else
17.157 + setobjs2s(ci->base+(n-1), L->top);
17.158 + }
17.159 + lua_unlock(L);
17.160 + return name;
17.161 +}
17.162 +
17.163 +
17.164 +static void funcinfo (lua_Debug *ar, StkId func) {
17.165 + Closure *cl = clvalue(func);
17.166 + if (cl->c.isC) {
17.167 + ar->source = "=[C]";
17.168 + ar->linedefined = -1;
17.169 + ar->what = "C";
17.170 + }
17.171 + else {
17.172 + ar->source = getstr(cl->l.p->source);
17.173 + ar->linedefined = cl->l.p->lineDefined;
17.174 + ar->what = (ar->linedefined == 0) ? "main" : "Lua";
17.175 + }
17.176 + luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
17.177 +}
17.178 +
17.179 +
17.180 +static const char *travglobals (lua_State *L, const TObject *o) {
17.181 + Table *g = hvalue(gt(L));
17.182 + int i = sizenode(g);
17.183 + while (i--) {
17.184 + Node *n = gnode(g, i);
17.185 + if (luaO_rawequalObj(o, gval(n)) && ttisstring(gkey(n)))
17.186 + return getstr(tsvalue(gkey(n)));
17.187 + }
17.188 + return NULL;
17.189 +}
17.190 +
17.191 +
17.192 +static void info_tailcall (lua_State *L, lua_Debug *ar) {
17.193 + ar->name = ar->namewhat = "";
17.194 + ar->what = "tail";
17.195 + ar->linedefined = ar->currentline = -1;
17.196 + ar->source = "=(tail call)";
17.197 + luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
17.198 + ar->nups = 0;
17.199 + setnilvalue(L->top);
17.200 +}
17.201 +
17.202 +
17.203 +static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
17.204 + StkId f, CallInfo *ci) {
17.205 + int status = 1;
17.206 + for (; *what; what++) {
17.207 + switch (*what) {
17.208 + case 'S': {
17.209 + funcinfo(ar, f);
17.210 + break;
17.211 + }
17.212 + case 'l': {
17.213 + ar->currentline = (ci) ? currentline(ci) : -1;
17.214 + break;
17.215 + }
17.216 + case 'u': {
17.217 + ar->nups = clvalue(f)->c.nupvalues;
17.218 + break;
17.219 + }
17.220 + case 'n': {
17.221 + ar->namewhat = (ci) ? getfuncname(ci, &ar->name) : NULL;
17.222 + if (ar->namewhat == NULL) {
17.223 + /* try to find a global name */
17.224 + if ((ar->name = travglobals(L, f)) != NULL)
17.225 + ar->namewhat = "global";
17.226 + else ar->namewhat = ""; /* not found */
17.227 + }
17.228 + break;
17.229 + }
17.230 + case 'f': {
17.231 + setobj2s(L->top, f);
17.232 + break;
17.233 + }
17.234 + default: status = 0; /* invalid option */
17.235 + }
17.236 + }
17.237 + return status;
17.238 +}
17.239 +
17.240 +
17.241 +LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
17.242 + int status = 1;
17.243 + lua_lock(L);
17.244 + if (*what == '>') {
17.245 + StkId f = L->top - 1;
17.246 + if (!ttisfunction(f))
17.247 + luaG_runerror(L, "value for `lua_getinfo' is not a function");
17.248 + status = auxgetinfo(L, what + 1, ar, f, NULL);
17.249 + L->top--; /* pop function */
17.250 + }
17.251 + else if (ar->i_ci != 0) { /* no tail call? */
17.252 + CallInfo *ci = L->base_ci + ar->i_ci;
17.253 + lua_assert(ttisfunction(ci->base - 1));
17.254 + status = auxgetinfo(L, what, ar, ci->base - 1, ci);
17.255 + }
17.256 + else
17.257 + info_tailcall(L, ar);
17.258 + if (strchr(what, 'f')) incr_top(L);
17.259 + lua_unlock(L);
17.260 + return status;
17.261 +}
17.262 +
17.263 +
17.264 +/*
17.265 +** {======================================================
17.266 +** Symbolic Execution and code checker
17.267 +** =======================================================
17.268 +*/
17.269 +
17.270 +#define check(x) if (!(x)) return 0;
17.271 +
17.272 +#define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
17.273 +
17.274 +#define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
17.275 +
17.276 +
17.277 +
17.278 +static int precheck (const Proto *pt) {
17.279 + check(pt->maxstacksize <= MAXSTACK);
17.280 + check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
17.281 + lua_assert(pt->numparams+pt->is_vararg <= pt->maxstacksize);
17.282 + check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
17.283 + return 1;
17.284 +}
17.285 +
17.286 +
17.287 +static int checkopenop (const Proto *pt, int pc) {
17.288 + Instruction i = pt->code[pc+1];
17.289 + switch (GET_OPCODE(i)) {
17.290 + case OP_CALL:
17.291 + case OP_TAILCALL:
17.292 + case OP_RETURN: {
17.293 + check(GETARG_B(i) == 0);
17.294 + return 1;
17.295 + }
17.296 + case OP_SETLISTO: return 1;
17.297 + default: return 0; /* invalid instruction after an open call */
17.298 + }
17.299 +}
17.300 +
17.301 +
17.302 +static int checkRK (const Proto *pt, int r) {
17.303 + return (r < pt->maxstacksize || (r >= MAXSTACK && r-MAXSTACK < pt->sizek));
17.304 +}
17.305 +
17.306 +
17.307 +static Instruction luaG_symbexec (const Proto *pt, int lastpc, int reg) {
17.308 + int pc;
17.309 + int last; /* stores position of last instruction that changed `reg' */
17.310 + last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */
17.311 + check(precheck(pt));
17.312 + for (pc = 0; pc < lastpc; pc++) {
17.313 + const Instruction i = pt->code[pc];
17.314 + OpCode op = GET_OPCODE(i);
17.315 + int a = GETARG_A(i);
17.316 + int b = 0;
17.317 + int c = 0;
17.318 + checkreg(pt, a);
17.319 + switch (getOpMode(op)) {
17.320 + case iABC: {
17.321 + b = GETARG_B(i);
17.322 + c = GETARG_C(i);
17.323 + if (testOpMode(op, OpModeBreg)) {
17.324 + checkreg(pt, b);
17.325 + }
17.326 + else if (testOpMode(op, OpModeBrk))
17.327 + check(checkRK(pt, b));
17.328 + if (testOpMode(op, OpModeCrk))
17.329 + check(checkRK(pt, c));
17.330 + break;
17.331 + }
17.332 + case iABx: {
17.333 + b = GETARG_Bx(i);
17.334 + if (testOpMode(op, OpModeK)) check(b < pt->sizek);
17.335 + break;
17.336 + }
17.337 + case iAsBx: {
17.338 + b = GETARG_sBx(i);
17.339 + break;
17.340 + }
17.341 + }
17.342 + if (testOpMode(op, OpModesetA)) {
17.343 + if (a == reg) last = pc; /* change register `a' */
17.344 + }
17.345 + if (testOpMode(op, OpModeT)) {
17.346 + check(pc+2 < pt->sizecode); /* check skip */
17.347 + check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
17.348 + }
17.349 + switch (op) {
17.350 + case OP_LOADBOOL: {
17.351 + check(c == 0 || pc+2 < pt->sizecode); /* check its jump */
17.352 + break;
17.353 + }
17.354 + case OP_LOADNIL: {
17.355 + if (a <= reg && reg <= b)
17.356 + last = pc; /* set registers from `a' to `b' */
17.357 + break;
17.358 + }
17.359 + case OP_GETUPVAL:
17.360 + case OP_SETUPVAL: {
17.361 + check(b < pt->nups);
17.362 + break;
17.363 + }
17.364 + case OP_GETGLOBAL:
17.365 + case OP_SETGLOBAL: {
17.366 + check(ttisstring(&pt->k[b]));
17.367 + break;
17.368 + }
17.369 + case OP_SELF: {
17.370 + checkreg(pt, a+1);
17.371 + if (reg == a+1) last = pc;
17.372 + break;
17.373 + }
17.374 + case OP_CONCAT: {
17.375 + /* `c' is a register, and at least two operands */
17.376 + check(c < MAXSTACK && b < c);
17.377 + break;
17.378 + }
17.379 + case OP_TFORLOOP:
17.380 + checkreg(pt, a+c+5);
17.381 + if (reg >= a) last = pc; /* affect all registers above base */
17.382 + /* go through */
17.383 + case OP_FORLOOP:
17.384 + checkreg(pt, a+2);
17.385 + /* go through */
17.386 + case OP_JMP: {
17.387 + int dest = pc+1+b;
17.388 + check(0 <= dest && dest < pt->sizecode);
17.389 + /* not full check and jump is forward and do not skip `lastpc'? */
17.390 + if (reg != NO_REG && pc < dest && dest <= lastpc)
17.391 + pc += b; /* do the jump */
17.392 + break;
17.393 + }
17.394 + case OP_CALL:
17.395 + case OP_TAILCALL: {
17.396 + if (b != 0) {
17.397 + checkreg(pt, a+b-1);
17.398 + }
17.399 + c--; /* c = num. returns */
17.400 + if (c == LUA_MULTRET) {
17.401 + check(checkopenop(pt, pc));
17.402 + }
17.403 + else if (c != 0)
17.404 + checkreg(pt, a+c-1);
17.405 + if (reg >= a) last = pc; /* affect all registers above base */
17.406 + break;
17.407 + }
17.408 + case OP_RETURN: {
17.409 + b--; /* b = num. returns */
17.410 + if (b > 0) checkreg(pt, a+b-1);
17.411 + break;
17.412 + }
17.413 + case OP_SETLIST: {
17.414 + checkreg(pt, a + (b&(LFIELDS_PER_FLUSH-1)) + 1);
17.415 + break;
17.416 + }
17.417 + case OP_CLOSURE: {
17.418 + int nup;
17.419 + check(b < pt->sizep);
17.420 + nup = pt->p[b]->nups;
17.421 + check(pc + nup < pt->sizecode);
17.422 + for (; nup>0; nup--) {
17.423 + OpCode op1 = GET_OPCODE(pt->code[pc+nup]);
17.424 + check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
17.425 + }
17.426 + break;
17.427 + }
17.428 + default: break;
17.429 + }
17.430 + }
17.431 + return pt->code[last];
17.432 +}
17.433 +
17.434 +#undef check
17.435 +#undef checkjump
17.436 +#undef checkreg
17.437 +
17.438 +/* }====================================================== */
17.439 +
17.440 +
17.441 +int luaG_checkcode (const Proto *pt) {
17.442 + return luaG_symbexec(pt, pt->sizecode, NO_REG);
17.443 +}
17.444 +
17.445 +
17.446 +static const char *kname (Proto *p, int c) {
17.447 + c = c - MAXSTACK;
17.448 + if (c >= 0 && ttisstring(&p->k[c]))
17.449 + return svalue(&p->k[c]);
17.450 + else
17.451 + return "?";
17.452 +}
17.453 +
17.454 +
17.455 +static const char *getobjname (CallInfo *ci, int stackpos, const char **name) {
17.456 + if (isLua(ci)) { /* a Lua function? */
17.457 + Proto *p = ci_func(ci)->l.p;
17.458 + int pc = currentpc(ci);
17.459 + Instruction i;
17.460 + *name = luaF_getlocalname(p, stackpos+1, pc);
17.461 + if (*name) /* is a local? */
17.462 + return "local";
17.463 + i = luaG_symbexec(p, pc, stackpos); /* try symbolic execution */
17.464 + lua_assert(pc != -1);
17.465 + switch (GET_OPCODE(i)) {
17.466 + case OP_GETGLOBAL: {
17.467 + int g = GETARG_Bx(i); /* global index */
17.468 + lua_assert(ttisstring(&p->k[g]));
17.469 + *name = svalue(&p->k[g]);
17.470 + return "global";
17.471 + }
17.472 + case OP_MOVE: {
17.473 + int a = GETARG_A(i);
17.474 + int b = GETARG_B(i); /* move from `b' to `a' */
17.475 + if (b < a)
17.476 + return getobjname(ci, b, name); /* get name for `b' */
17.477 + break;
17.478 + }
17.479 + case OP_GETTABLE: {
17.480 + int k = GETARG_C(i); /* key index */
17.481 + *name = kname(p, k);
17.482 + return "field";
17.483 + }
17.484 + case OP_SELF: {
17.485 + int k = GETARG_C(i); /* key index */
17.486 + *name = kname(p, k);
17.487 + return "method";
17.488 + }
17.489 + default: break;
17.490 + }
17.491 + }
17.492 + return NULL; /* no useful name found */
17.493 +}
17.494 +
17.495 +
17.496 +static const char *getfuncname (CallInfo *ci, const char **name) {
17.497 + Instruction i;
17.498 + if ((isLua(ci) && ci->u.l.tailcalls > 0) || !isLua(ci - 1))
17.499 + return NULL; /* calling function is not Lua (or is unknown) */
17.500 + ci--; /* calling function */
17.501 + i = ci_func(ci)->l.p->code[currentpc(ci)];
17.502 + if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL)
17.503 + return getobjname(ci, GETARG_A(i), name);
17.504 + else
17.505 + return NULL; /* no useful name can be found */
17.506 +}
17.507 +
17.508 +
17.509 +/* only ANSI way to check whether a pointer points to an array */
17.510 +static int isinstack (CallInfo *ci, const TObject *o) {
17.511 + StkId p;
17.512 + for (p = ci->base; p < ci->top; p++)
17.513 + if (o == p) return 1;
17.514 + return 0;
17.515 +}
17.516 +
17.517 +
17.518 +void luaG_typeerror (lua_State *L, const TObject *o, const char *op) {
17.519 + const char *name = NULL;
17.520 + const char *t = luaT_typenames[ttype(o)];
17.521 + const char *kind = (isinstack(L->ci, o)) ?
17.522 + getobjname(L->ci, o - L->base, &name) : NULL;
17.523 + if (kind)
17.524 + luaG_runerror(L, "attempt to %s %s `%s' (a %s value)",
17.525 + op, kind, name, t);
17.526 + else
17.527 + luaG_runerror(L, "attempt to %s a %s value", op, t);
17.528 +}
17.529 +
17.530 +
17.531 +void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
17.532 + if (ttisstring(p1)) p1 = p2;
17.533 + lua_assert(!ttisstring(p1));
17.534 + luaG_typeerror(L, p1, "concatenate");
17.535 +}
17.536 +
17.537 +
17.538 +void luaG_aritherror (lua_State *L, const TObject *p1, const TObject *p2) {
17.539 + TObject temp;
17.540 + if (luaV_tonumber(p1, &temp) == NULL)
17.541 + p2 = p1; /* first operand is wrong */
17.542 + luaG_typeerror(L, p2, "perform arithmetic on");
17.543 +}
17.544 +
17.545 +
17.546 +int luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
17.547 + const char *t1 = luaT_typenames[ttype(p1)];
17.548 + const char *t2 = luaT_typenames[ttype(p2)];
17.549 + if (t1[2] == t2[2])
17.550 + luaG_runerror(L, "attempt to compare two %s values", t1);
17.551 + else
17.552 + luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
17.553 + return 0;
17.554 +}
17.555 +
17.556 +
17.557 +static void addinfo (lua_State *L, const char *msg) {
17.558 + CallInfo *ci = L->ci;
17.559 + if (isLua(ci)) { /* is Lua code? */
17.560 + char buff[LUA_IDSIZE]; /* add file:line information */
17.561 + int line = currentline(ci);
17.562 + luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
17.563 + luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
17.564 + }
17.565 +}
17.566 +
17.567 +
17.568 +void luaG_errormsg (lua_State *L) {
17.569 + if (L->errfunc != 0) { /* is there an error handling function? */
17.570 + StkId errfunc = restorestack(L, L->errfunc);
17.571 + if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
17.572 + setobjs2s(L->top, L->top - 1); /* move argument */
17.573 + setobjs2s(L->top - 1, errfunc); /* push function */
17.574 + incr_top(L);
17.575 + luaD_call(L, L->top - 2, 1); /* call it */
17.576 + }
17.577 + luaD_throw(L, LUA_ERRRUN);
17.578 +}
17.579 +
17.580 +
17.581 +void luaG_runerror (lua_State *L, const char *fmt, ...) {
17.582 + va_list argp;
17.583 + va_start(argp, fmt);
17.584 + addinfo(L, luaO_pushvfstring(L, fmt, argp));
17.585 + va_end(argp);
17.586 + luaG_errormsg(L);
17.587 +}
17.588 +
18.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
18.2 +++ b/source/lua/src/ldebug.h Wed Nov 04 00:31:14 2009 +0000
18.3 @@ -0,0 +1,31 @@
18.4 +/*
18.5 +** $Id$
18.6 +** Auxiliary functions from Debug Interface module
18.7 +** See Copyright Notice in lua.h
18.8 +*/
18.9 +
18.10 +#ifndef ldebug_h
18.11 +#define ldebug_h
18.12 +
18.13 +
18.14 +#include "lstate.h"
18.15 +
18.16 +
18.17 +#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1)
18.18 +
18.19 +#define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0)
18.20 +
18.21 +#define resethookcount(L) (L->hookcount = L->basehookcount)
18.22 +
18.23 +
18.24 +void luaG_inithooks (lua_State *L);
18.25 +void luaG_typeerror (lua_State *L, const TObject *o, const char *opname);
18.26 +void luaG_concaterror (lua_State *L, StkId p1, StkId p2);
18.27 +void luaG_aritherror (lua_State *L, const TObject *p1, const TObject *p2);
18.28 +int luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2);
18.29 +void luaG_runerror (lua_State *L, const char *fmt, ...);
18.30 +void luaG_errormsg (lua_State *L);
18.31 +int luaG_checkcode (const Proto *pt);
18.32 +
18.33 +
18.34 +#endif
19.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
19.2 +++ b/source/lua/src/ldo.c Wed Nov 04 00:31:14 2009 +0000
19.3 @@ -0,0 +1,471 @@
19.4 +/*
19.5 +** $Id$
19.6 +** Stack and Call structure of Lua
19.7 +** See Copyright Notice in lua.h
19.8 +*/
19.9 +
19.10 +
19.11 +#include <setjmp.h>
19.12 +#include <stdlib.h>
19.13 +#include <string.h>
19.14 +
19.15 +#define ldo_c
19.16 +
19.17 +#include "lua.h"
19.18 +
19.19 +#include "ldebug.h"
19.20 +#include "ldo.h"
19.21 +#include "lfunc.h"
19.22 +#include "lgc.h"
19.23 +#include "lmem.h"
19.24 +#include "lobject.h"
19.25 +#include "lopcodes.h"
19.26 +#include "lparser.h"
19.27 +#include "lstate.h"
19.28 +#include "lstring.h"
19.29 +#include "ltable.h"
19.30 +#include "ltm.h"
19.31 +#include "lundump.h"
19.32 +#include "lvm.h"
19.33 +#include "lzio.h"
19.34 +
19.35 +
19.36 +
19.37 +
19.38 +/*
19.39 +** {======================================================
19.40 +** Error-recovery functions (based on long jumps)
19.41 +** =======================================================
19.42 +*/
19.43 +
19.44 +
19.45 +/* chain list of long jump buffers */
19.46 +struct lua_longjmp {
19.47 + struct lua_longjmp *previous;
19.48 + jmp_buf b;
19.49 + volatile int status; /* error code */
19.50 +};
19.51 +
19.52 +
19.53 +static void seterrorobj (lua_State *L, int errcode, StkId oldtop) {
19.54 + switch (errcode) {
19.55 + case LUA_ERRMEM: {
19.56 + setsvalue2s(oldtop, luaS_new(L, MEMERRMSG));
19.57 + break;
19.58 + }
19.59 + case LUA_ERRERR: {
19.60 + setsvalue2s(oldtop, luaS_new(L, "error in error handling"));
19.61 + break;
19.62 + }
19.63 + case LUA_ERRSYNTAX:
19.64 + case LUA_ERRRUN: {
19.65 + setobjs2s(oldtop, L->top - 1); /* error message on current top */
19.66 + break;
19.67 + }
19.68 + }
19.69 + L->top = oldtop + 1;
19.70 +}
19.71 +
19.72 +
19.73 +void luaD_throw (lua_State *L, int errcode) {
19.74 + if (L->errorJmp) {
19.75 + L->errorJmp->status = errcode;
19.76 + longjmp(L->errorJmp->b, 1);
19.77 + }
19.78 + else {
19.79 + G(L)->panic(L);
19.80 + exit(EXIT_FAILURE);
19.81 + }
19.82 +}
19.83 +
19.84 +
19.85 +int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
19.86 + struct lua_longjmp lj;
19.87 + lj.status = 0;
19.88 + lj.previous = L->errorJmp; /* chain new error handler */
19.89 + L->errorJmp = &lj;
19.90 + if (setjmp(lj.b) == 0)
19.91 + (*f)(L, ud);
19.92 + L->errorJmp = lj.previous; /* restore old error handler */
19.93 + return lj.status;
19.94 +}
19.95 +
19.96 +
19.97 +static void restore_stack_limit (lua_State *L) {
19.98 + L->stack_last = L->stack+L->stacksize-1;
19.99 + if (L->size_ci > LUA_MAXCALLS) { /* there was an overflow? */
19.100 + int inuse = (L->ci - L->base_ci);
19.101 + if (inuse + 1 < LUA_MAXCALLS) /* can `undo' overflow? */
19.102 + luaD_reallocCI(L, LUA_MAXCALLS);
19.103 + }
19.104 +}
19.105 +
19.106 +/* }====================================================== */
19.107 +
19.108 +
19.109 +static void correctstack (lua_State *L, TObject *oldstack) {
19.110 + CallInfo *ci;
19.111 + GCObject *up;
19.112 + L->top = (L->top - oldstack) + L->stack;
19.113 + for (up = L->openupval; up != NULL; up = up->gch.next)
19.114 + gcotouv(up)->v = (gcotouv(up)->v - oldstack) + L->stack;
19.115 + for (ci = L->base_ci; ci <= L->ci; ci++) {
19.116 + ci->top = (ci->top - oldstack) + L->stack;
19.117 + ci->base = (ci->base - oldstack) + L->stack;
19.118 + }
19.119 + L->base = L->ci->base;
19.120 +}
19.121 +
19.122 +
19.123 +void luaD_reallocstack (lua_State *L, int newsize) {
19.124 + TObject *oldstack = L->stack;
19.125 + luaM_reallocvector(L, L->stack, L->stacksize, newsize, TObject);
19.126 + L->stacksize = newsize;
19.127 + L->stack_last = L->stack+newsize-1-EXTRA_STACK;
19.128 + correctstack(L, oldstack);
19.129 +}
19.130 +
19.131 +
19.132 +void luaD_reallocCI (lua_State *L, int newsize) {
19.133 + CallInfo *oldci = L->base_ci;
19.134 + luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
19.135 + L->size_ci = cast(unsigned short, newsize);
19.136 + L->ci = (L->ci - oldci) + L->base_ci;
19.137 + L->end_ci = L->base_ci + L->size_ci;
19.138 +}
19.139 +
19.140 +
19.141 +void luaD_growstack (lua_State *L, int n) {
19.142 + if (n <= L->stacksize) /* double size is enough? */
19.143 + luaD_reallocstack(L, 2*L->stacksize);
19.144 + else
19.145 + luaD_reallocstack(L, L->stacksize + n + EXTRA_STACK);
19.146 +}
19.147 +
19.148 +
19.149 +static void luaD_growCI (lua_State *L) {
19.150 + if (L->size_ci > LUA_MAXCALLS) /* overflow while handling overflow? */
19.151 + luaD_throw(L, LUA_ERRERR);
19.152 + else {
19.153 + luaD_reallocCI(L, 2*L->size_ci);
19.154 + if (L->size_ci > LUA_MAXCALLS)
19.155 + luaG_runerror(L, "stack overflow");
19.156 + }
19.157 +}
19.158 +
19.159 +
19.160 +void luaD_callhook (lua_State *L, int event, int line) {
19.161 + lua_Hook hook = L->hook;
19.162 + if (hook && L->allowhook) {
19.163 + ptrdiff_t top = savestack(L, L->top);
19.164 + ptrdiff_t ci_top = savestack(L, L->ci->top);
19.165 + lua_Debug ar;
19.166 + ar.event = event;
19.167 + ar.currentline = line;
19.168 + if (event == LUA_HOOKTAILRET)
19.169 + ar.i_ci = 0; /* tail call; no debug information about it */
19.170 + else
19.171 + ar.i_ci = L->ci - L->base_ci;
19.172 + luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
19.173 + L->ci->top = L->top + LUA_MINSTACK;
19.174 + L->allowhook = 0; /* cannot call hooks inside a hook */
19.175 + lua_unlock(L);
19.176 + (*hook)(L, &ar);
19.177 + lua_lock(L);
19.178 + lua_assert(!L->allowhook);
19.179 + L->allowhook = 1;
19.180 + L->ci->top = restorestack(L, ci_top);
19.181 + L->top = restorestack(L, top);
19.182 + }
19.183 +}
19.184 +
19.185 +
19.186 +static void adjust_varargs (lua_State *L, int nfixargs, StkId base) {
19.187 + int i;
19.188 + Table *htab;
19.189 + TObject nname;
19.190 + int actual = L->top - base; /* actual number of arguments */
19.191 + if (actual < nfixargs) {
19.192 + luaD_checkstack(L, nfixargs - actual);
19.193 + for (; actual < nfixargs; ++actual)
19.194 + setnilvalue(L->top++);
19.195 + }
19.196 + actual -= nfixargs; /* number of extra arguments */
19.197 + htab = luaH_new(L, actual, 1); /* create `arg' table */
19.198 + for (i=0; i<actual; i++) /* put extra arguments into `arg' table */
19.199 + setobj2n(luaH_setnum(L, htab, i+1), L->top - actual + i);
19.200 + /* store counter in field `n' */
19.201 + setsvalue(&nname, luaS_newliteral(L, "n"));
19.202 + setnvalue(luaH_set(L, htab, &nname), cast(lua_Number, actual));
19.203 + L->top -= actual; /* remove extra elements from the stack */
19.204 + sethvalue(L->top, htab);
19.205 + incr_top(L);
19.206 +}
19.207 +
19.208 +
19.209 +static StkId tryfuncTM (lua_State *L, StkId func) {
19.210 + const TObject *tm = luaT_gettmbyobj(L, func, TM_CALL);
19.211 + StkId p;
19.212 + ptrdiff_t funcr = savestack(L, func);
19.213 + if (!ttisfunction(tm))
19.214 + luaG_typeerror(L, func, "call");
19.215 + /* Open a hole inside the stack at `func' */
19.216 + for (p = L->top; p > func; p--) setobjs2s(p, p-1);
19.217 + incr_top(L);
19.218 + func = restorestack(L, funcr); /* previous call may change stack */
19.219 + setobj2s(func, tm); /* tag method is the new function to be called */
19.220 + return func;
19.221 +}
19.222 +
19.223 +
19.224 +StkId luaD_precall (lua_State *L, StkId func) {
19.225 + LClosure *cl;
19.226 + ptrdiff_t funcr = savestack(L, func);
19.227 + if (!ttisfunction(func)) /* `func' is not a function? */
19.228 + func = tryfuncTM(L, func); /* check the `function' tag method */
19.229 + if (L->ci + 1 == L->end_ci) luaD_growCI(L);
19.230 + else condhardstacktests(luaD_reallocCI(L, L->size_ci));
19.231 + cl = &clvalue(func)->l;
19.232 + if (!cl->isC) { /* Lua function? prepare its call */
19.233 + CallInfo *ci;
19.234 + Proto *p = cl->p;
19.235 + if (p->is_vararg) /* varargs? */
19.236 + adjust_varargs(L, p->numparams, func+1);
19.237 + luaD_checkstack(L, p->maxstacksize);
19.238 + ci = ++L->ci; /* now `enter' new function */
19.239 + L->base = L->ci->base = restorestack(L, funcr) + 1;
19.240 + ci->top = L->base + p->maxstacksize;
19.241 + ci->u.l.savedpc = p->code; /* starting point */
19.242 + ci->u.l.tailcalls = 0;
19.243 + ci->state = CI_SAVEDPC;
19.244 + while (L->top < ci->top)
19.245 + setnilvalue(L->top++);
19.246 + L->top = ci->top;
19.247 + return NULL;
19.248 + }
19.249 + else { /* if is a C function, call it */
19.250 + CallInfo *ci;
19.251 + int n;
19.252 + luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
19.253 + ci = ++L->ci; /* now `enter' new function */
19.254 + L->base = L->ci->base = restorestack(L, funcr) + 1;
19.255 + ci->top = L->top + LUA_MINSTACK;
19.256 + ci->state = CI_C; /* a C function */
19.257 + if (L->hookmask & LUA_MASKCALL)
19.258 + luaD_callhook(L, LUA_HOOKCALL, -1);
19.259 + lua_unlock(L);
19.260 +#ifdef LUA_COMPATUPVALUES
19.261 + lua_pushupvalues(L);
19.262 +#endif
19.263 + n = (*clvalue(L->base - 1)->c.f)(L); /* do the actual call */
19.264 + lua_lock(L);
19.265 + return L->top - n;
19.266 + }
19.267 +}
19.268 +
19.269 +
19.270 +static StkId callrethooks (lua_State *L, StkId firstResult) {
19.271 + ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
19.272 + luaD_callhook(L, LUA_HOOKRET, -1);
19.273 + if (!(L->ci->state & CI_C)) { /* Lua function? */
19.274 + while (L->ci->u.l.tailcalls--) /* call hook for eventual tail calls */
19.275 + luaD_callhook(L, LUA_HOOKTAILRET, -1);
19.276 + }
19.277 + return restorestack(L, fr);
19.278 +}
19.279 +
19.280 +
19.281 +void luaD_poscall (lua_State *L, int wanted, StkId firstResult) {
19.282 + StkId res;
19.283 + if (L->hookmask & LUA_MASKRET)
19.284 + firstResult = callrethooks(L, firstResult);
19.285 + res = L->base - 1; /* res == final position of 1st result */
19.286 + L->ci--;
19.287 + L->base = L->ci->base; /* restore base */
19.288 + /* move results to correct place */
19.289 + while (wanted != 0 && firstResult < L->top) {
19.290 + setobjs2s(res++, firstResult++);
19.291 + wanted--;
19.292 + }
19.293 + while (wanted-- > 0)
19.294 + setnilvalue(res++);
19.295 + L->top = res;
19.296 +}
19.297 +
19.298 +
19.299 +/*
19.300 +** Call a function (C or Lua). The function to be called is at *func.
19.301 +** The arguments are on the stack, right after the function.
19.302 +** When returns, all the results are on the stack, starting at the original
19.303 +** function position.
19.304 +*/
19.305 +void luaD_call (lua_State *L, StkId func, int nResults) {
19.306 + StkId firstResult;
19.307 + lua_assert(!(L->ci->state & CI_CALLING));
19.308 + if (++L->nCcalls >= LUA_MAXCCALLS) {
19.309 + if (L->nCcalls == LUA_MAXCCALLS)
19.310 + luaG_runerror(L, "C stack overflow");
19.311 + else if (L->nCcalls >= (LUA_MAXCCALLS + (LUA_MAXCCALLS>>3)))
19.312 + luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
19.313 + }
19.314 + firstResult = luaD_precall(L, func);
19.315 + if (firstResult == NULL) /* is a Lua function? */
19.316 + firstResult = luaV_execute(L); /* call it */
19.317 + luaD_poscall(L, nResults, firstResult);
19.318 + L->nCcalls--;
19.319 + luaC_checkGC(L);
19.320 +}
19.321 +
19.322 +
19.323 +static void resume (lua_State *L, void *ud) {
19.324 + StkId firstResult;
19.325 + int nargs = *cast(int *, ud);
19.326 + CallInfo *ci = L->ci;
19.327 + if (ci == L->base_ci) { /* no activation record? */
19.328 + lua_assert(nargs < L->top - L->base);
19.329 + luaD_precall(L, L->top - (nargs + 1)); /* start coroutine */
19.330 + }
19.331 + else { /* inside a yield */
19.332 + lua_assert(ci->state & CI_YIELD);
19.333 + if (ci->state & CI_C) { /* `common' yield? */
19.334 + /* finish interrupted execution of `OP_CALL' */
19.335 + int nresults;
19.336 + lua_assert((ci-1)->state & CI_SAVEDPC);
19.337 + lua_assert(GET_OPCODE(*((ci-1)->u.l.savedpc - 1)) == OP_CALL ||
19.338 + GET_OPCODE(*((ci-1)->u.l.savedpc - 1)) == OP_TAILCALL);
19.339 + nresults = GETARG_C(*((ci-1)->u.l.savedpc - 1)) - 1;
19.340 + luaD_poscall(L, nresults, L->top - nargs); /* complete it */
19.341 + if (nresults >= 0) L->top = L->ci->top;
19.342 + }
19.343 + else { /* yielded inside a hook: just continue its execution */
19.344 + ci->state &= ~CI_YIELD;
19.345 + }
19.346 + }
19.347 + firstResult = luaV_execute(L);
19.348 + if (firstResult != NULL) /* return? */
19.349 + luaD_poscall(L, LUA_MULTRET, firstResult); /* finalize this coroutine */
19.350 +}
19.351 +
19.352 +
19.353 +static int resume_error (lua_State *L, const char *msg) {
19.354 + L->top = L->ci->base;
19.355 + setsvalue2s(L->top, luaS_new(L, msg));
19.356 + incr_top(L);
19.357 + lua_unlock(L);
19.358 + return LUA_ERRRUN;
19.359 +}
19.360 +
19.361 +
19.362 +LUA_API int lua_resume (lua_State *L, int nargs) {
19.363 + int status;
19.364 + lu_byte old_allowhooks;
19.365 + lua_lock(L);
19.366 + if (L->ci == L->base_ci) {
19.367 + if (nargs >= L->top - L->base)
19.368 + return resume_error(L, "cannot resume dead coroutine");
19.369 + }
19.370 + else if (!(L->ci->state & CI_YIELD)) /* not inside a yield? */
19.371 + return resume_error(L, "cannot resume non-suspended coroutine");
19.372 + old_allowhooks = L->allowhook;
19.373 + lua_assert(L->errfunc == 0 && L->nCcalls == 0);
19.374 + status = luaD_rawrunprotected(L, resume, &nargs);
19.375 + if (status != 0) { /* error? */
19.376 + L->ci = L->base_ci; /* go back to initial level */
19.377 + L->base = L->ci->base;
19.378 + L->nCcalls = 0;
19.379 + luaF_close(L, L->base); /* close eventual pending closures */
19.380 + seterrorobj(L, status, L->base);
19.381 + L->allowhook = old_allowhooks;
19.382 + restore_stack_limit(L);
19.383 + }
19.384 + lua_unlock(L);
19.385 + return status;
19.386 +}
19.387 +
19.388 +
19.389 +LUA_API int lua_yield (lua_State *L, int nresults) {
19.390 + CallInfo *ci;
19.391 + lua_lock(L);
19.392 + ci = L->ci;
19.393 + if (L->nCcalls > 0)
19.394 + luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
19.395 + if (ci->state & CI_C) { /* usual yield */
19.396 + if ((ci-1)->state & CI_C)
19.397 + luaG_runerror(L, "cannot yield a C function");
19.398 + if (L->top - nresults > L->base) { /* is there garbage in the stack? */
19.399 + int i;
19.400 + for (i=0; i<nresults; i++) /* move down results */
19.401 + setobjs2s(L->base + i, L->top - nresults + i);
19.402 + L->top = L->base + nresults;
19.403 + }
19.404 + } /* else it's an yield inside a hook: nothing to do */
19.405 + ci->state |= CI_YIELD;
19.406 + lua_unlock(L);
19.407 + return -1;
19.408 +}
19.409 +
19.410 +
19.411 +int luaD_pcall (lua_State *L, Pfunc func, void *u,
19.412 + ptrdiff_t old_top, ptrdiff_t ef) {
19.413 + int status;
19.414 + unsigned short oldnCcalls = L->nCcalls;
19.415 + ptrdiff_t old_ci = saveci(L, L->ci);
19.416 + lu_byte old_allowhooks = L->allowhook;
19.417 + ptrdiff_t old_errfunc = L->errfunc;
19.418 + L->errfunc = ef;
19.419 + status = luaD_rawrunprotected(L, func, u);
19.420 + if (status != 0) { /* an error occurred? */
19.421 + StkId oldtop = restorestack(L, old_top);
19.422 + luaF_close(L, oldtop); /* close eventual pending closures */
19.423 + seterrorobj(L, status, oldtop);
19.424 + L->nCcalls = oldnCcalls;
19.425 + L->ci = restoreci(L, old_ci);
19.426 + L->base = L->ci->base;
19.427 + L->allowhook = old_allowhooks;
19.428 + restore_stack_limit(L);
19.429 + }
19.430 + L->errfunc = old_errfunc;
19.431 + return status;
19.432 +}
19.433 +
19.434 +
19.435 +
19.436 +/*
19.437 +** Execute a protected parser.
19.438 +*/
19.439 +struct SParser { /* data to `f_parser' */
19.440 + ZIO *z;
19.441 + Mbuffer buff; /* buffer to be used by the scanner */
19.442 + int bin;
19.443 +};
19.444 +
19.445 +static void f_parser (lua_State *L, void *ud) {
19.446 + struct SParser *p;
19.447 + Proto *tf;
19.448 + Closure *cl;
19.449 + luaC_checkGC(L);
19.450 + p = cast(struct SParser *, ud);
19.451 + tf = p->bin ? luaU_undump(L, p->z, &p->buff) : luaY_parser(L, p->z, &p->buff);
19.452 + cl = luaF_newLclosure(L, 0, gt(L));
19.453 + cl->l.p = tf;
19.454 + setclvalue(L->top, cl);
19.455 + incr_top(L);
19.456 +}
19.457 +
19.458 +
19.459 +int luaD_protectedparser (lua_State *L, ZIO *z, int bin) {
19.460 + struct SParser p;
19.461 + int status;
19.462 + ptrdiff_t oldtopr = savestack(L, L->top); /* save current top */
19.463 + p.z = z; p.bin = bin;
19.464 + luaZ_initbuffer(L, &p.buff);
19.465 + status = luaD_rawrunprotected(L, f_parser, &p);
19.466 + luaZ_freebuffer(L, &p.buff);
19.467 + if (status != 0) { /* error? */
19.468 + StkId oldtop = restorestack(L, oldtopr);
19.469 + seterrorobj(L, status, oldtop);
19.470 + }
19.471 + return status;
19.472 +}
19.473 +
19.474 +
20.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
20.2 +++ b/source/lua/src/ldo.h Wed Nov 04 00:31:14 2009 +0000
20.3 @@ -0,0 +1,60 @@
20.4 +/*
20.5 +** $Id$
20.6 +** Stack and Call structure of Lua
20.7 +** See Copyright Notice in lua.h
20.8 +*/
20.9 +
20.10 +#ifndef ldo_h
20.11 +#define ldo_h
20.12 +
20.13 +
20.14 +#include "lobject.h"
20.15 +#include "lstate.h"
20.16 +#include "lzio.h"
20.17 +
20.18 +
20.19 +/*
20.20 +** macro to control inclusion of some hard tests on stack reallocation
20.21 +*/
20.22 +#ifndef HARDSTACKTESTS
20.23 +#define condhardstacktests(x) { /* empty */ }
20.24 +#else
20.25 +#define condhardstacktests(x) x
20.26 +#endif
20.27 +
20.28 +
20.29 +#define luaD_checkstack(L,n) \
20.30 + if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TObject)) \
20.31 + luaD_growstack(L, n); \
20.32 + else condhardstacktests(luaD_reallocstack(L, L->stacksize));
20.33 +
20.34 +
20.35 +#define incr_top(L) {luaD_checkstack(L,1); L->top++;}
20.36 +
20.37 +#define savestack(L,p) ((char *)(p) - (char *)L->stack)
20.38 +#define restorestack(L,n) ((TObject *)((char *)L->stack + (n)))
20.39 +
20.40 +#define saveci(L,p) ((char *)(p) - (char *)L->base_ci)
20.41 +#define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n)))
20.42 +
20.43 +
20.44 +/* type of protected functions, to be ran by `runprotected' */
20.45 +typedef void (*Pfunc) (lua_State *L, void *ud);
20.46 +
20.47 +void luaD_resetprotection (lua_State *L);
20.48 +int luaD_protectedparser (lua_State *L, ZIO *z, int bin);
20.49 +void luaD_callhook (lua_State *L, int event, int line);
20.50 +StkId luaD_precall (lua_State *L, StkId func);
20.51 +void luaD_call (lua_State *L, StkId func, int nResults);
20.52 +int luaD_pcall (lua_State *L, Pfunc func, void *u,
20.53 + ptrdiff_t oldtop, ptrdiff_t ef);
20.54 +void luaD_poscall (lua_State *L, int wanted, StkId firstResult);
20.55 +void luaD_reallocCI (lua_State *L, int newsize);
20.56 +void luaD_reallocstack (lua_State *L, int newsize);
20.57 +void luaD_growstack (lua_State *L, int n);
20.58 +
20.59 +void luaD_throw (lua_State *L, int errcode);
20.60 +int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
20.61 +
20.62 +
20.63 +#endif
21.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
21.2 +++ b/source/lua/src/ldump.c Wed Nov 04 00:31:14 2009 +0000
21.3 @@ -0,0 +1,170 @@
21.4 +/*
21.5 +** $Id$
21.6 +** save bytecodes
21.7 +** See Copyright Notice in lua.h
21.8 +*/
21.9 +
21.10 +#include <stddef.h>
21.11 +
21.12 +#define ldump_c
21.13 +
21.14 +#include "lua.h"
21.15 +
21.16 +#include "lobject.h"
21.17 +#include "lopcodes.h"
21.18 +#include "lstate.h"
21.19 +#include "lundump.h"
21.20 +
21.21 +#define DumpVector(b,n,size,D) DumpBlock(b,(n)*(size),D)
21.22 +#define DumpLiteral(s,D) DumpBlock("" s,(sizeof(s))-1,D)
21.23 +
21.24 +typedef struct {
21.25 + lua_State* L;
21.26 + lua_Chunkwriter write;
21.27 + void* data;
21.28 +} DumpState;
21.29 +
21.30 +static void DumpBlock(const void* b, size_t size, DumpState* D)
21.31 +{
21.32 + lua_unlock(D->L);
21.33 + (*D->write)(D->L,b,size,D->data);
21.34 + lua_lock(D->L);
21.35 +}
21.36 +
21.37 +static void DumpByte(int y, DumpState* D)
21.38 +{
21.39 + char x=(char)y;
21.40 + DumpBlock(&x,sizeof(x),D);
21.41 +}
21.42 +
21.43 +static void DumpInt(int x, DumpState* D)
21.44 +{
21.45 + DumpBlock(&x,sizeof(x),D);
21.46 +}
21.47 +
21.48 +static void DumpSize(size_t x, DumpState* D)
21.49 +{
21.50 + DumpBlock(&x,sizeof(x),D);
21.51 +}
21.52 +
21.53 +static void DumpNumber(lua_Number x, DumpState* D)
21.54 +{
21.55 + DumpBlock(&x,sizeof(x),D);
21.56 +}
21.57 +
21.58 +static void DumpString(TString* s, DumpState* D)
21.59 +{
21.60 + if (s==NULL || getstr(s)==NULL)
21.61 + DumpSize(0,D);
21.62 + else
21.63 + {
21.64 + size_t size=s->tsv.len+1; /* include trailing '\0' */
21.65 + DumpSize(size,D);
21.66 + DumpBlock(getstr(s),size,D);
21.67 + }
21.68 +}
21.69 +
21.70 +static void DumpCode(const Proto* f, DumpState* D)
21.71 +{
21.72 + DumpInt(f->sizecode,D);
21.73 + DumpVector(f->code,f->sizecode,sizeof(*f->code),D);
21.74 +}
21.75 +
21.76 +static void DumpLocals(const Proto* f, DumpState* D)
21.77 +{
21.78 + int i,n=f->sizelocvars;
21.79 + DumpInt(n,D);
21.80 + for (i=0; i<n; i++)
21.81 + {
21.82 + DumpString(f->locvars[i].varname,D);
21.83 + DumpInt(f->locvars[i].startpc,D);
21.84 + DumpInt(f->locvars[i].endpc,D);
21.85 + }
21.86 +}
21.87 +
21.88 +static void DumpLines(const Proto* f, DumpState* D)
21.89 +{
21.90 + DumpInt(f->sizelineinfo,D);
21.91 + DumpVector(f->lineinfo,f->sizelineinfo,sizeof(*f->lineinfo),D);
21.92 +}
21.93 +
21.94 +static void DumpUpvalues(const Proto* f, DumpState* D)
21.95 +{
21.96 + int i,n=f->sizeupvalues;
21.97 + DumpInt(n,D);
21.98 + for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
21.99 +}
21.100 +
21.101 +static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
21.102 +
21.103 +static void DumpConstants(const Proto* f, DumpState* D)
21.104 +{
21.105 + int i,n;
21.106 + DumpInt(n=f->sizek,D);
21.107 + for (i=0; i<n; i++)
21.108 + {
21.109 + const TObject* o=&f->k[i];
21.110 + DumpByte(ttype(o),D);
21.111 + switch (ttype(o))
21.112 + {
21.113 + case LUA_TNUMBER:
21.114 + DumpNumber(nvalue(o),D);
21.115 + break;
21.116 + case LUA_TSTRING:
21.117 + DumpString(tsvalue(o),D);
21.118 + break;
21.119 + case LUA_TNIL:
21.120 + break;
21.121 + default:
21.122 + lua_assert(0); /* cannot happen */
21.123 + break;
21.124 + }
21.125 + }
21.126 + DumpInt(n=f->sizep,D);
21.127 + for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
21.128 +}
21.129 +
21.130 +static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
21.131 +{
21.132 + DumpString((f->source==p) ? NULL : f->source,D);
21.133 + DumpInt(f->lineDefined,D);
21.134 + DumpByte(f->nups,D);
21.135 + DumpByte(f->numparams,D);
21.136 + DumpByte(f->is_vararg,D);
21.137 + DumpByte(f->maxstacksize,D);
21.138 + DumpLines(f,D);
21.139 + DumpLocals(f,D);
21.140 + DumpUpvalues(f,D);
21.141 + DumpConstants(f,D);
21.142 + DumpCode(f,D);
21.143 +}
21.144 +
21.145 +static void DumpHeader(DumpState* D)
21.146 +{
21.147 + DumpLiteral(LUA_SIGNATURE,D);
21.148 + DumpByte(VERSION,D);
21.149 + DumpByte(luaU_endianness(),D);
21.150 + DumpByte(sizeof(int),D);
21.151 + DumpByte(sizeof(size_t),D);
21.152 + DumpByte(sizeof(Instruction),D);
21.153 + DumpByte(SIZE_OP,D);
21.154 + DumpByte(SIZE_A,D);
21.155 + DumpByte(SIZE_B,D);
21.156 + DumpByte(SIZE_C,D);
21.157 + DumpByte(sizeof(lua_Number),D);
21.158 + DumpNumber(TEST_NUMBER,D);
21.159 +}
21.160 +
21.161 +/*
21.162 +** dump function as precompiled chunk
21.163 +*/
21.164 +void luaU_dump (lua_State* L, const Proto* Main, lua_Chunkwriter w, void* data)
21.165 +{
21.166 + DumpState D;
21.167 + D.L=L;
21.168 + D.write=w;
21.169 + D.data=data;
21.170 + DumpHeader(&D);
21.171 + DumpFunction(Main,NULL,&D);
21.172 +}
21.173 +
22.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
22.2 +++ b/source/lua/src/lfunc.c Wed Nov 04 00:31:14 2009 +0000
22.3 @@ -0,0 +1,135 @@
22.4 +/*
22.5 +** $Id$
22.6 +** Auxiliary functions to manipulate prototypes and closures
22.7 +** See Copyright Notice in lua.h
22.8 +*/
22.9 +
22.10 +
22.11 +#include <stdlib.h>
22.12 +
22.13 +#define lfunc_c
22.14 +
22.15 +#include "lua.h"
22.16 +
22.17 +#include "lfunc.h"
22.18 +#include "lgc.h"
22.19 +#include "lmem.h"
22.20 +#include "lobject.h"
22.21 +#include "lstate.h"
22.22 +
22.23 +
22.24 +#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \
22.25 + cast(int, sizeof(TObject)*((n)-1)))
22.26 +
22.27 +#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \
22.28 + cast(int, sizeof(TObject *)*((n)-1)))
22.29 +
22.30 +
22.31 +
22.32 +Closure *luaF_newCclosure (lua_State *L, int nelems) {
22.33 + Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
22.34 + luaC_link(L, valtogco(c), LUA_TFUNCTION);
22.35 + c->c.isC = 1;
22.36 + c->c.nupvalues = cast(lu_byte, nelems);
22.37 + return c;
22.38 +}
22.39 +
22.40 +
22.41 +Closure *luaF_newLclosure (lua_State *L, int nelems, TObject *e) {
22.42 + Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
22.43 + luaC_link(L, valtogco(c), LUA_TFUNCTION);
22.44 + c->l.isC = 0;
22.45 + c->l.g = *e;
22.46 + c->l.nupvalues = cast(lu_byte, nelems);
22.47 + return c;
22.48 +}
22.49 +
22.50 +
22.51 +UpVal *luaF_findupval (lua_State *L, StkId level) {
22.52 + GCObject **pp = &L->openupval;
22.53 + UpVal *p;
22.54 + UpVal *v;
22.55 + while ((p = ngcotouv(*pp)) != NULL && p->v >= level) {
22.56 + if (p->v == level) return p;
22.57 + pp = &p->next;
22.58 + }
22.59 + v = luaM_new(L, UpVal); /* not found: create a new one */
22.60 + v->tt = LUA_TUPVAL;
22.61 + v->marked = 1; /* open upvalues should not be collected */
22.62 + v->v = level; /* current value lives in the stack */
22.63 + v->next = *pp; /* chain it in the proper position */
22.64 + *pp = valtogco(v);
22.65 + return v;
22.66 +}
22.67 +
22.68 +
22.69 +void luaF_close (lua_State *L, StkId level) {
22.70 + UpVal *p;
22.71 + while ((p = ngcotouv(L->openupval)) != NULL && p->v >= level) {
22.72 + setobj(&p->value, p->v); /* save current value (write barrier) */
22.73 + p->v = &p->value; /* now current value lives here */
22.74 + L->openupval = p->next; /* remove from `open' list */
22.75 + luaC_link(L, valtogco(p), LUA_TUPVAL);
22.76 + }
22.77 +}
22.78 +
22.79 +
22.80 +Proto *luaF_newproto (lua_State *L) {
22.81 + Proto *f = luaM_new(L, Proto);
22.82 + luaC_link(L, valtogco(f), LUA_TPROTO);
22.83 + f->k = NULL;
22.84 + f->sizek = 0;
22.85 + f->p = NULL;
22.86 + f->sizep = 0;
22.87 + f->code = NULL;
22.88 + f->sizecode = 0;
22.89 + f->sizelineinfo = 0;
22.90 + f->sizeupvalues = 0;
22.91 + f->nups = 0;
22.92 + f->upvalues = NULL;
22.93 + f->numparams = 0;
22.94 + f->is_vararg = 0;
22.95 + f->maxstacksize = 0;
22.96 + f->lineinfo = NULL;
22.97 + f->sizelocvars = 0;
22.98 + f->locvars = NULL;
22.99 + f->lineDefined = 0;
22.100 + f->source = NULL;
22.101 + return f;
22.102 +}
22.103 +
22.104 +
22.105 +void luaF_freeproto (lua_State *L, Proto *f) {
22.106 + luaM_freearray(L, f->code, f->sizecode, Instruction);
22.107 + luaM_freearray(L, f->p, f->sizep, Proto *);
22.108 + luaM_freearray(L, f->k, f->sizek, TObject);
22.109 + luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
22.110 + luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
22.111 + luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *);
22.112 + luaM_freelem(L, f);
22.113 +}
22.114 +
22.115 +
22.116 +void luaF_freeclosure (lua_State *L, Closure *c) {
22.117 + int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
22.118 + sizeLclosure(c->l.nupvalues);
22.119 + luaM_free(L, c, size);
22.120 +}
22.121 +
22.122 +
22.123 +/*
22.124 +** Look for n-th local variable at line `line' in function `func'.
22.125 +** Returns NULL if not found.
22.126 +*/
22.127 +const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
22.128 + int i;
22.129 + for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
22.130 + if (pc < f->locvars[i].endpc) { /* is variable active? */
22.131 + local_number--;
22.132 + if (local_number == 0)
22.133 + return getstr(f->locvars[i].varname);
22.134 + }
22.135 + }
22.136 + return NULL; /* not found */
22.137 +}
22.138 +
23.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
23.2 +++ b/source/lua/src/lfunc.h Wed Nov 04 00:31:14 2009 +0000
23.3 @@ -0,0 +1,25 @@
23.4 +/*
23.5 +** $Id$
23.6 +** Auxiliary functions to manipulate prototypes and closures
23.7 +** See Copyright Notice in lua.h
23.8 +*/
23.9 +
23.10 +#ifndef lfunc_h
23.11 +#define lfunc_h
23.12 +
23.13 +
23.14 +#include "lobject.h"
23.15 +
23.16 +
23.17 +Proto *luaF_newproto (lua_State *L);
23.18 +Closure *luaF_newCclosure (lua_State *L, int nelems);
23.19 +Closure *luaF_newLclosure (lua_State *L, int nelems, TObject *e);
23.20 +UpVal *luaF_findupval (lua_State *L, StkId level);
23.21 +void luaF_close (lua_State *L, StkId level);
23.22 +void luaF_freeproto (lua_State *L, Proto *f);
23.23 +void luaF_freeclosure (lua_State *L, Closure *c);
23.24 +
23.25 +const char *luaF_getlocalname (const Proto *func, int local_number, int pc);
23.26 +
23.27 +
23.28 +#endif
24.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
24.2 +++ b/source/lua/src/lgc.c Wed Nov 04 00:31:14 2009 +0000
24.3 @@ -0,0 +1,498 @@
24.4 +/*
24.5 +** $Id$
24.6 +** Garbage Collector
24.7 +** See Copyright Notice in lua.h
24.8 +*/
24.9 +
24.10 +#include <string.h>
24.11 +
24.12 +#define lgc_c
24.13 +
24.14 +#include "lua.h"
24.15 +
24.16 +#include "ldebug.h"
24.17 +#include "ldo.h"
24.18 +#include "lfunc.h"
24.19 +#include "lgc.h"
24.20 +#include "lmem.h"
24.21 +#include "lobject.h"
24.22 +#include "lstate.h"
24.23 +#include "lstring.h"
24.24 +#include "ltable.h"
24.25 +#include "ltm.h"
24.26 +
24.27 +
24.28 +typedef struct GCState {
24.29 + GCObject *tmark; /* list of marked objects to be traversed */
24.30 + GCObject *wk; /* list of traversed key-weak tables (to be cleared) */
24.31 + GCObject *wv; /* list of traversed value-weak tables */
24.32 + GCObject *wkv; /* list of traversed key-value weak tables */
24.33 + global_State *g;
24.34 +} GCState;
24.35 +
24.36 +
24.37 +/*
24.38 +** some userful bit tricks
24.39 +*/
24.40 +#define setbit(x,b) ((x) |= (1<<(b)))
24.41 +#define resetbit(x,b) ((x) &= cast(lu_byte, ~(1<<(b))))
24.42 +#define testbit(x,b) ((x) & (1<<(b)))
24.43 +
24.44 +#define unmark(x) resetbit((x)->gch.marked, 0)
24.45 +#define ismarked(x) ((x)->gch.marked & ((1<<4)|1))
24.46 +
24.47 +#define stringmark(s) setbit((s)->tsv.marked, 0)
24.48 +
24.49 +
24.50 +#define isfinalized(u) (!testbit((u)->uv.marked, 1))
24.51 +#define markfinalized(u) resetbit((u)->uv.marked, 1)
24.52 +
24.53 +
24.54 +#define KEYWEAKBIT 1
24.55 +#define VALUEWEAKBIT 2
24.56 +#define KEYWEAK (1<<KEYWEAKBIT)
24.57 +#define VALUEWEAK (1<<VALUEWEAKBIT)
24.58 +
24.59 +
24.60 +
24.61 +#define markobject(st,o) { checkconsistency(o); \
24.62 + if (iscollectable(o) && !ismarked(gcvalue(o))) reallymarkobject(st,gcvalue(o)); }
24.63 +
24.64 +#define condmarkobject(st,o,c) { checkconsistency(o); \
24.65 + if (iscollectable(o) && !ismarked(gcvalue(o)) && (c)) \
24.66 + reallymarkobject(st,gcvalue(o)); }
24.67 +
24.68 +#define markvalue(st,t) { if (!ismarked(valtogco(t))) \
24.69 + reallymarkobject(st, valtogco(t)); }
24.70 +
24.71 +
24.72 +
24.73 +static void reallymarkobject (GCState *st, GCObject *o) {
24.74 + lua_assert(!ismarked(o));
24.75 + setbit(o->gch.marked, 0); /* mark object */
24.76 + switch (o->gch.tt) {
24.77 + case LUA_TUSERDATA: {
24.78 + markvalue(st, gcotou(o)->uv.metatable);
24.79 + break;
24.80 + }
24.81 + case LUA_TFUNCTION: {
24.82 + gcotocl(o)->c.gclist = st->tmark;
24.83 + st->tmark = o;
24.84 + break;
24.85 + }
24.86 + case LUA_TTABLE: {
24.87 + gcotoh(o)->gclist = st->tmark;
24.88 + st->tmark = o;
24.89 + break;
24.90 + }
24.91 + case LUA_TTHREAD: {
24.92 + gcototh(o)->gclist = st->tmark;
24.93 + st->tmark = o;
24.94 + break;
24.95 + }
24.96 + case LUA_TPROTO: {
24.97 + gcotop(o)->gclist = st->tmark;
24.98 + st->tmark = o;
24.99 + break;
24.100 + }
24.101 + default: lua_assert(o->gch.tt == LUA_TSTRING);
24.102 + }
24.103 +}
24.104 +
24.105 +
24.106 +static void marktmu (GCState *st) {
24.107 + GCObject *u;
24.108 + for (u = st->g->tmudata; u; u = u->gch.next) {
24.109 + unmark(u); /* may be marked, if left from previous GC */
24.110 + reallymarkobject(st, u);
24.111 + }
24.112 +}
24.113 +
24.114 +
24.115 +/* move `dead' udata that need finalization to list `tmudata' */
24.116 +size_t luaC_separateudata (lua_State *L) {
24.117 + size_t deadmem = 0;
24.118 + GCObject **p = &G(L)->rootudata;
24.119 + GCObject *curr;
24.120 + GCObject *collected = NULL; /* to collect udata with gc event */
24.121 + GCObject **lastcollected = &collected;
24.122 + while ((curr = *p) != NULL) {
24.123 + lua_assert(curr->gch.tt == LUA_TUSERDATA);
24.124 + if (ismarked(curr) || isfinalized(gcotou(curr)))
24.125 + p = &curr->gch.next; /* don't bother with them */
24.126 +
24.127 + else if (fasttm(L, gcotou(curr)->uv.metatable, TM_GC) == NULL) {
24.128 + markfinalized(gcotou(curr)); /* don't need finalization */
24.129 + p = &curr->gch.next;
24.130 + }
24.131 + else { /* must call its gc method */
24.132 + deadmem += sizeudata(gcotou(curr)->uv.len);
24.133 + *p = curr->gch.next;
24.134 + curr->gch.next = NULL; /* link `curr' at the end of `collected' list */
24.135 + *lastcollected = curr;
24.136 + lastcollected = &curr->gch.next;
24.137 + }
24.138 + }
24.139 + /* insert collected udata with gc event into `tmudata' list */
24.140 + *lastcollected = G(L)->tmudata;
24.141 + G(L)->tmudata = collected;
24.142 + return deadmem;
24.143 +}
24.144 +
24.145 +
24.146 +static void removekey (Node *n) {
24.147 + setnilvalue(gval(n)); /* remove corresponding value ... */
24.148 + if (iscollectable(gkey(n)))
24.149 + setttype(gkey(n), LUA_TNONE); /* dead key; remove it */
24.150 +}
24.151 +
24.152 +
24.153 +static void traversetable (GCState *st, Table *h) {
24.154 + int i;
24.155 + int weakkey = 0;
24.156 + int weakvalue = 0;
24.157 + const TObject *mode;
24.158 + markvalue(st, h->metatable);
24.159 + lua_assert(h->lsizenode || h->node == st->g->dummynode);
24.160 + mode = gfasttm(st->g, h->metatable, TM_MODE);
24.161 + if (mode && ttisstring(mode)) { /* is there a weak mode? */
24.162 + weakkey = (strchr(svalue(mode), 'k') != NULL);
24.163 + weakvalue = (strchr(svalue(mode), 'v') != NULL);
24.164 + if (weakkey || weakvalue) { /* is really weak? */
24.165 + GCObject **weaklist;
24.166 + h->marked &= ~(KEYWEAK | VALUEWEAK); /* clear bits */
24.167 + h->marked |= cast(lu_byte, (weakkey << KEYWEAKBIT) |
24.168 + (weakvalue << VALUEWEAKBIT));
24.169 + weaklist = (weakkey && weakvalue) ? &st->wkv :
24.170 + (weakkey) ? &st->wk :
24.171 + &st->wv;
24.172 + h->gclist = *weaklist; /* must be cleared after GC, ... */
24.173 + *weaklist = valtogco(h); /* ... so put in the appropriate list */
24.174 + }
24.175 + }
24.176 + if (!weakvalue) {
24.177 + i = h->sizearray;
24.178 + while (i--)
24.179 + markobject(st, &h->array[i]);
24.180 + }
24.181 + i = sizenode(h);
24.182 + while (i--) {
24.183 + Node *n = gnode(h, i);
24.184 + if (!ttisnil(gval(n))) {
24.185 + lua_assert(!ttisnil(gkey(n)));
24.186 + condmarkobject(st, gkey(n), !weakkey);
24.187 + condmarkobject(st, gval(n), !weakvalue);
24.188 + }
24.189 + }
24.190 +}
24.191 +
24.192 +
24.193 +static void traverseproto (GCState *st, Proto *f) {
24.194 + int i;
24.195 + stringmark(f->source);
24.196 + for (i=0; i<f->sizek; i++) { /* mark literal strings */
24.197 + if (ttisstring(f->k+i))
24.198 + stringmark(tsvalue(f->k+i));
24.199 + }
24.200 + for (i=0; i<f->sizeupvalues; i++) /* mark upvalue names */
24.201 + stringmark(f->upvalues[i]);
24.202 + for (i=0; i<f->sizep; i++) /* mark nested protos */
24.203 + markvalue(st, f->p[i]);
24.204 + for (i=0; i<f->sizelocvars; i++) /* mark local-variable names */
24.205 + stringmark(f->locvars[i].varname);
24.206 + lua_assert(luaG_checkcode(f));
24.207 +}
24.208 +
24.209 +
24.210 +
24.211 +static void traverseclosure (GCState *st, Closure *cl) {
24.212 + if (cl->c.isC) {
24.213 + int i;
24.214 + for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */
24.215 + markobject(st, &cl->c.upvalue[i]);
24.216 + }
24.217 + else {
24.218 + int i;
24.219 + lua_assert(cl->l.nupvalues == cl->l.p->nups);
24.220 + markvalue(st, hvalue(&cl->l.g));
24.221 + markvalue(st, cl->l.p);
24.222 + for (i=0; i<cl->l.nupvalues; i++) { /* mark its upvalues */
24.223 + UpVal *u = cl->l.upvals[i];
24.224 + if (!u->marked) {
24.225 + markobject(st, &u->value);
24.226 + u->marked = 1;
24.227 + }
24.228 + }
24.229 + }
24.230 +}
24.231 +
24.232 +
24.233 +static void checkstacksizes (lua_State *L, StkId max) {
24.234 + int used = L->ci - L->base_ci; /* number of `ci' in use */
24.235 + if (4*used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci)
24.236 + luaD_reallocCI(L, L->size_ci/2); /* still big enough... */
24.237 + else condhardstacktests(luaD_reallocCI(L, L->size_ci));
24.238 + used = max - L->stack; /* part of stack in use */
24.239 + if (4*used < L->stacksize && 2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize)
24.240 + luaD_reallocstack(L, L->stacksize/2); /* still big enough... */
24.241 + else condhardstacktests(luaD_reallocstack(L, L->stacksize));
24.242 +}
24.243 +
24.244 +
24.245 +static void traversestack (GCState *st, lua_State *L1) {
24.246 + StkId o, lim;
24.247 + CallInfo *ci;
24.248 + markobject(st, gt(L1));
24.249 + lim = L1->top;
24.250 + for (ci = L1->base_ci; ci <= L1->ci; ci++) {
24.251 + lua_assert(ci->top <= L1->stack_last);
24.252 + lua_assert(ci->state & (CI_C | CI_HASFRAME | CI_SAVEDPC));
24.253 + if (lim < ci->top)
24.254 + lim = ci->top;
24.255 + }
24.256 + for (o = L1->stack; o < L1->top; o++)
24.257 + markobject(st, o);
24.258 + for (; o <= lim; o++)
24.259 + setnilvalue(o);
24.260 + checkstacksizes(L1, lim);
24.261 +}
24.262 +
24.263 +
24.264 +static void propagatemarks (GCState *st) {
24.265 + while (st->tmark) { /* traverse marked objects */
24.266 + switch (st->tmark->gch.tt) {
24.267 + case LUA_TTABLE: {
24.268 + Table *h = gcotoh(st->tmark);
24.269 + st->tmark = h->gclist;
24.270 + traversetable(st, h);
24.271 + break;
24.272 + }
24.273 + case LUA_TFUNCTION: {
24.274 + Closure *cl = gcotocl(st->tmark);
24.275 + st->tmark = cl->c.gclist;
24.276 + traverseclosure(st, cl);
24.277 + break;
24.278 + }
24.279 + case LUA_TTHREAD: {
24.280 + lua_State *th = gcototh(st->tmark);
24.281 + st->tmark = th->gclist;
24.282 + traversestack(st, th);
24.283 + break;
24.284 + }
24.285 + case LUA_TPROTO: {
24.286 + Proto *p = gcotop(st->tmark);
24.287 + st->tmark = p->gclist;
24.288 + traverseproto(st, p);
24.289 + break;
24.290 + }
24.291 + default: lua_assert(0);
24.292 + }
24.293 + }
24.294 +}
24.295 +
24.296 +
24.297 +static int valismarked (const TObject *o) {
24.298 + if (ttisstring(o))
24.299 + stringmark(tsvalue(o)); /* strings are `values', so are never weak */
24.300 + return !iscollectable(o) || testbit(o->value.gc->gch.marked, 0);
24.301 +}
24.302 +
24.303 +
24.304 +/*
24.305 +** clear collected keys from weaktables
24.306 +*/
24.307 +static void cleartablekeys (GCObject *l) {
24.308 + while (l) {
24.309 + Table *h = gcotoh(l);
24.310 + int i = sizenode(h);
24.311 + lua_assert(h->marked & KEYWEAK);
24.312 + while (i--) {
24.313 + Node *n = gnode(h, i);
24.314 + if (!valismarked(gkey(n))) /* key was collected? */
24.315 + removekey(n); /* remove entry from table */
24.316 + }
24.317 + l = h->gclist;
24.318 + }
24.319 +}
24.320 +
24.321 +
24.322 +/*
24.323 +** clear collected values from weaktables
24.324 +*/
24.325 +static void cleartablevalues (GCObject *l) {
24.326 + while (l) {
24.327 + Table *h = gcotoh(l);
24.328 + int i = h->sizearray;
24.329 + lua_assert(h->marked & VALUEWEAK);
24.330 + while (i--) {
24.331 + TObject *o = &h->array[i];
24.332 + if (!valismarked(o)) /* value was collected? */
24.333 + setnilvalue(o); /* remove value */
24.334 + }
24.335 + i = sizenode(h);
24.336 + while (i--) {
24.337 + Node *n = gnode(h, i);
24.338 + if (!valismarked(gval(n))) /* value was collected? */
24.339 + removekey(n); /* remove entry from table */
24.340 + }
24.341 + l = h->gclist;
24.342 + }
24.343 +}
24.344 +
24.345 +
24.346 +static void freeobj (lua_State *L, GCObject *o) {
24.347 + switch (o->gch.tt) {
24.348 + case LUA_TPROTO: luaF_freeproto(L, gcotop(o)); break;
24.349 + case LUA_TFUNCTION: luaF_freeclosure(L, gcotocl(o)); break;
24.350 + case LUA_TUPVAL: luaM_freelem(L, gcotouv(o)); break;
24.351 + case LUA_TTABLE: luaH_free(L, gcotoh(o)); break;
24.352 + case LUA_TTHREAD: {
24.353 + lua_assert(gcototh(o) != L && gcototh(o) != G(L)->mainthread);
24.354 + luaE_freethread(L, gcototh(o));
24.355 + break;
24.356 + }
24.357 + case LUA_TSTRING: {
24.358 + luaM_free(L, o, sizestring(gcotots(o)->tsv.len));
24.359 + break;
24.360 + }
24.361 + case LUA_TUSERDATA: {
24.362 + luaM_free(L, o, sizeudata(gcotou(o)->uv.len));
24.363 + break;
24.364 + }
24.365 + default: lua_assert(0);
24.366 + }
24.367 +}
24.368 +
24.369 +
24.370 +static int sweeplist (lua_State *L, GCObject **p, int limit) {
24.371 + GCObject *curr;
24.372 + int count = 0; /* number of collected items */
24.373 + while ((curr = *p) != NULL) {
24.374 + if (curr->gch.marked > limit) {
24.375 + unmark(curr);
24.376 + p = &curr->gch.next;
24.377 + }
24.378 + else {
24.379 + count++;
24.380 + *p = curr->gch.next;
24.381 + freeobj(L, curr);
24.382 + }
24.383 + }
24.384 + return count;
24.385 +}
24.386 +
24.387 +
24.388 +static void sweepstrings (lua_State *L, int all) {
24.389 + int i;
24.390 + for (i=0; i<G(L)->strt.size; i++) { /* for each list */
24.391 + G(L)->strt.nuse -= sweeplist(L, &G(L)->strt.hash[i], all);
24.392 + }
24.393 +}
24.394 +
24.395 +
24.396 +static void checkSizes (lua_State *L, size_t deadmem) {
24.397 + /* check size of string hash */
24.398 + if (G(L)->strt.nuse < cast(ls_nstr, G(L)->strt.size/4) &&
24.399 + G(L)->strt.size > MINSTRTABSIZE*2)
24.400 + luaS_resize(L, G(L)->strt.size/2); /* table is too big */
24.401 + /* check size of buffer */
24.402 + if (luaZ_sizebuffer(&G(L)->buff) > LUA_MINBUFFER*2) { /* buffer too big? */
24.403 + size_t newsize = luaZ_sizebuffer(&G(L)->buff) / 2;
24.404 + luaZ_resizebuffer(L, &G(L)->buff, newsize);
24.405 + }
24.406 + G(L)->GCthreshold = 2*G(L)->nblocks - deadmem; /* new threshold */
24.407 +}
24.408 +
24.409 +
24.410 +static void do1gcTM (lua_State *L, Udata *udata) {
24.411 + const TObject *tm = fasttm(L, udata->uv.metatable, TM_GC);
24.412 + if (tm != NULL) {
24.413 + setobj2s(L->top, tm);
24.414 + setuvalue(L->top+1, udata);
24.415 + L->top += 2;
24.416 + luaD_call(L, L->top - 2, 0);
24.417 + }
24.418 +}
24.419 +
24.420 +
24.421 +void luaC_callGCTM (lua_State *L) {
24.422 + lu_byte oldah = L->allowhook;
24.423 + L->allowhook = 0; /* stop debug hooks during GC tag methods */
24.424 + L->top++; /* reserve space to keep udata while runs its gc method */
24.425 + while (G(L)->tmudata != NULL) {
24.426 + GCObject *o = G(L)->tmudata;
24.427 + Udata *udata = gcotou(o);
24.428 + G(L)->tmudata = udata->uv.next; /* remove udata from `tmudata' */
24.429 + udata->uv.next = G(L)->rootudata; /* return it to `root' list */
24.430 + G(L)->rootudata = o;
24.431 + setuvalue(L->top - 1, udata); /* keep a reference to it */
24.432 + unmark(o);
24.433 + markfinalized(udata);
24.434 + do1gcTM(L, udata);
24.435 + }
24.436 + L->top--;
24.437 + L->allowhook = oldah; /* restore hooks */
24.438 +}
24.439 +
24.440 +
24.441 +void luaC_sweep (lua_State *L, int all) {
24.442 + if (all) all = 256; /* larger than any mark */
24.443 + sweeplist(L, &G(L)->rootudata, all);
24.444 + sweepstrings(L, all);
24.445 + sweeplist(L, &G(L)->rootgc, all);
24.446 +}
24.447 +
24.448 +
24.449 +/* mark root set */
24.450 +static void markroot (GCState *st, lua_State *L) {
24.451 + global_State *g = st->g;
24.452 + markobject(st, defaultmeta(L));
24.453 + markobject(st, registry(L));
24.454 + traversestack(st, g->mainthread);
24.455 + if (L != g->mainthread) /* another thread is running? */
24.456 + markvalue(st, L); /* cannot collect it */
24.457 +}
24.458 +
24.459 +
24.460 +static size_t mark (lua_State *L) {
24.461 + size_t deadmem;
24.462 + GCState st;
24.463 + GCObject *wkv;
24.464 + st.g = G(L);
24.465 + st.tmark = NULL;
24.466 + st.wkv = st.wk = st.wv = NULL;
24.467 + markroot(&st, L);
24.468 + propagatemarks(&st); /* mark all reachable objects */
24.469 + cleartablevalues(st.wkv);
24.470 + cleartablevalues(st.wv);
24.471 + wkv = st.wkv; /* keys must be cleared after preserving udata */
24.472 + st.wkv = NULL;
24.473 + st.wv = NULL;
24.474 + deadmem = luaC_separateudata(L); /* separate userdata to be preserved */
24.475 + marktmu(&st); /* mark `preserved' userdata */
24.476 + propagatemarks(&st); /* remark, to propagate `preserveness' */
24.477 + cleartablekeys(wkv);
24.478 + /* `propagatemarks' may resuscitate some weak tables; clear them too */
24.479 + cleartablekeys(st.wk);
24.480 + cleartablevalues(st.wv);
24.481 + cleartablekeys(st.wkv);
24.482 + cleartablevalues(st.wkv);
24.483 + return deadmem;
24.484 +}
24.485 +
24.486 +
24.487 +void luaC_collectgarbage (lua_State *L) {
24.488 + size_t deadmem = mark(L);
24.489 + luaC_sweep(L, 0);
24.490 + checkSizes(L, deadmem);
24.491 + luaC_callGCTM(L);
24.492 +}
24.493 +
24.494 +
24.495 +void luaC_link (lua_State *L, GCObject *o, lu_byte tt) {
24.496 + o->gch.next = G(L)->rootgc;
24.497 + G(L)->rootgc = o;
24.498 + o->gch.marked = 0;
24.499 + o->gch.tt = tt;
24.500 +}
24.501 +
25.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
25.2 +++ b/source/lua/src/lgc.h Wed Nov 04 00:31:14 2009 +0000
25.3 @@ -0,0 +1,25 @@
25.4 +/*
25.5 +** $Id$
25.6 +** Garbage Collector
25.7 +** See Copyright Notice in lua.h
25.8 +*/
25.9 +
25.10 +#ifndef lgc_h
25.11 +#define lgc_h
25.12 +
25.13 +
25.14 +#include "lobject.h"
25.15 +
25.16 +
25.17 +#define luaC_checkGC(L) { lua_assert(!(L->ci->state & CI_CALLING)); \
25.18 + if (G(L)->nblocks >= G(L)->GCthreshold) luaC_collectgarbage(L); }
25.19 +
25.20 +
25.21 +size_t luaC_separateudata (lua_State *L);
25.22 +void luaC_callGCTM (lua_State *L);
25.23 +void luaC_sweep (lua_State *L, int all);
25.24 +void luaC_collectgarbage (lua_State *L);
25.25 +void luaC_link (lua_State *L, GCObject *o, lu_byte tt);
25.26 +
25.27 +
25.28 +#endif
26.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
26.2 +++ b/source/lua/src/lib/README.txt Wed Nov 04 00:31:14 2009 +0000
26.3 @@ -0,0 +1,8 @@
26.4 +This is the standard Lua library.
26.5 +
26.6 +The code of the standard library can be read as an example of how to export
26.7 +C functions to Lua. The easiest library to read is lmathlib.c.
26.8 +
26.9 +The library is implemented entirely on top of the official Lua API as declared
26.10 +in lua.h, using lauxlib.c, which contains several useful functions for writing
26.11 +libraries. We encourage developers to use lauxlib.c in their own libraries.
27.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
27.2 +++ b/source/lua/src/lib/lauxlib.c Wed Nov 04 00:31:14 2009 +0000
27.3 @@ -0,0 +1,591 @@
27.4 +/*
27.5 +** $Id$
27.6 +** Auxiliary functions for building Lua libraries
27.7 +** See Copyright Notice in lua.h
27.8 +*/
27.9 +
27.10 +
27.11 +#include <ctype.h>
27.12 +#include <errno.h>
27.13 +#include <stdarg.h>
27.14 +#include <stdio.h>
27.15 +#include <string.h>
27.16 +
27.17 +
27.18 +/* This file uses only the official API of Lua.
27.19 +** Any function declared here could be written as an application function.
27.20 +*/
27.21 +
27.22 +#define lauxlib_c
27.23 +
27.24 +#include "lua.h"
27.25 +
27.26 +#include "lauxlib.h"
27.27 +
27.28 +
27.29 +/* number of prereserved references (for internal use) */
27.30 +#define RESERVED_REFS 2
27.31 +
27.32 +/* reserved references */
27.33 +#define FREELIST_REF 1 /* free list of references */
27.34 +#define ARRAYSIZE_REF 2 /* array sizes */
27.35 +
27.36 +
27.37 +/* convert a stack index to positive */
27.38 +#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
27.39 + lua_gettop(L) + (i) + 1)
27.40 +
27.41 +
27.42 +/*
27.43 +** {======================================================
27.44 +** Error-report functions
27.45 +** =======================================================
27.46 +*/
27.47 +
27.48 +
27.49 +LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
27.50 + lua_Debug ar;
27.51 + lua_getstack(L, 0, &ar);
27.52 + lua_getinfo(L, "n", &ar);
27.53 + if (strcmp(ar.namewhat, "method") == 0) {
27.54 + narg--; /* do not count `self' */
27.55 + if (narg == 0) /* error is in the self argument itself? */
27.56 + return luaL_error(L, "calling `%s' on bad self (%s)", ar.name, extramsg);
27.57 + }
27.58 + if (ar.name == NULL)
27.59 + ar.name = "?";
27.60 + return luaL_error(L, "bad argument #%d to `%s' (%s)",
27.61 + narg, ar.name, extramsg);
27.62 +}
27.63 +
27.64 +
27.65 +LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
27.66 + const char *msg = lua_pushfstring(L, "%s expected, got %s",
27.67 + tname, lua_typename(L, lua_type(L,narg)));
27.68 + return luaL_argerror(L, narg, msg);
27.69 +}
27.70 +
27.71 +
27.72 +static void tag_error (lua_State *L, int narg, int tag) {
27.73 + luaL_typerror(L, narg, lua_typename(L, tag));
27.74 +}
27.75 +
27.76 +
27.77 +LUALIB_API void luaL_where (lua_State *L, int level) {
27.78 + lua_Debug ar;
27.79 + if (lua_getstack(L, level, &ar)) { /* check function at level */
27.80 + lua_getinfo(L, "Snl", &ar); /* get info about it */
27.81 + if (ar.currentline > 0) { /* is there info? */
27.82 + lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
27.83 + return;
27.84 + }
27.85 + }
27.86 + lua_pushliteral(L, ""); /* else, no information available... */
27.87 +}
27.88 +
27.89 +
27.90 +LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
27.91 + va_list argp;
27.92 + va_start(argp, fmt);
27.93 + luaL_where(L, 1);
27.94 + lua_pushvfstring(L, fmt, argp);
27.95 + va_end(argp);
27.96 + lua_concat(L, 2);
27.97 + return lua_error(L);
27.98 +}
27.99 +
27.100 +/* }====================================================== */
27.101 +
27.102 +
27.103 +LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
27.104 + int i;
27.105 + for (i=0; list[i]; i++)
27.106 + if (strcmp(list[i], name) == 0)
27.107 + return i;
27.108 + return -1; /* name not found */
27.109 +}
27.110 +
27.111 +
27.112 +LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
27.113 + lua_pushstring(L, tname);
27.114 + lua_rawget(L, LUA_REGISTRYINDEX); /* get registry.name */
27.115 + if (!lua_isnil(L, -1)) /* name already in use? */
27.116 + return 0; /* leave previous value on top, but return 0 */
27.117 + lua_pop(L, 1);
27.118 + lua_newtable(L); /* create metatable */
27.119 + lua_pushstring(L, tname);
27.120 + lua_pushvalue(L, -2);
27.121 + lua_rawset(L, LUA_REGISTRYINDEX); /* registry.name = metatable */
27.122 + lua_pushvalue(L, -1);
27.123 + lua_pushstring(L, tname);
27.124 + lua_rawset(L, LUA_REGISTRYINDEX); /* registry[metatable] = name */
27.125 + return 1;
27.126 +}
27.127 +
27.128 +
27.129 +LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname) {
27.130 + lua_pushstring(L, tname);
27.131 + lua_rawget(L, LUA_REGISTRYINDEX);
27.132 +}
27.133 +
27.134 +
27.135 +LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
27.136 + const char *tn;
27.137 + if (!lua_getmetatable(L, ud)) return NULL; /* no metatable? */
27.138 + lua_rawget(L, LUA_REGISTRYINDEX); /* get registry[metatable] */
27.139 + tn = lua_tostring(L, -1);
27.140 + if (tn && (strcmp(tn, tname) == 0)) {
27.141 + lua_pop(L, 1);
27.142 + return lua_touserdata(L, ud);
27.143 + }
27.144 + else {
27.145 + lua_pop(L, 1);
27.146 + return NULL;
27.147 + }
27.148 +}
27.149 +
27.150 +
27.151 +LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
27.152 + if (!lua_checkstack(L, space))
27.153 + luaL_error(L, "stack overflow (%s)", mes);
27.154 +}
27.155 +
27.156 +
27.157 +LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
27.158 + if (lua_type(L, narg) != t)
27.159 + tag_error(L, narg, t);
27.160 +}
27.161 +
27.162 +
27.163 +LUALIB_API void luaL_checkany (lua_State *L, int narg) {
27.164 + if (lua_type(L, narg) == LUA_TNONE)
27.165 + luaL_argerror(L, narg, "value expected");
27.166 +}
27.167 +
27.168 +
27.169 +LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
27.170 + const char *s = lua_tostring(L, narg);
27.171 + if (!s) tag_error(L, narg, LUA_TSTRING);
27.172 + if (len) *len = lua_strlen(L, narg);
27.173 + return s;
27.174 +}
27.175 +
27.176 +
27.177 +LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
27.178 + const char *def, size_t *len) {
27.179 + if (lua_isnoneornil(L, narg)) {
27.180 + if (len)
27.181 + *len = (def ? strlen(def) : 0);
27.182 + return def;
27.183 + }
27.184 + else return luaL_checklstring(L, narg, len);
27.185 +}
27.186 +
27.187 +
27.188 +LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
27.189 + lua_Number d = lua_tonumber(L, narg);
27.190 + if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
27.191 + tag_error(L, narg, LUA_TNUMBER);
27.192 + return d;
27.193 +}
27.194 +
27.195 +
27.196 +LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
27.197 + if (lua_isnoneornil(L, narg)) return def;
27.198 + else return luaL_checknumber(L, narg);
27.199 +}
27.200 +
27.201 +
27.202 +LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
27.203 + if (!lua_getmetatable(L, obj)) /* no metatable? */
27.204 + return 0;
27.205 + lua_pushstring(L, event);
27.206 + lua_rawget(L, -2);
27.207 + if (lua_isnil(L, -1)) {
27.208 + lua_pop(L, 2); /* remove metatable and metafield */
27.209 + return 0;
27.210 + }
27.211 + else {
27.212 + lua_remove(L, -2); /* remove only metatable */
27.213 + return 1;
27.214 + }
27.215 +}
27.216 +
27.217 +
27.218 +LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
27.219 + obj = abs_index(L, obj);
27.220 + if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
27.221 + return 0;
27.222 + lua_pushvalue(L, obj);
27.223 + lua_call(L, 1, 1);
27.224 + return 1;
27.225 +}
27.226 +
27.227 +
27.228 +LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
27.229 + const luaL_reg *l, int nup) {
27.230 + if (libname) {
27.231 + lua_pushstring(L, libname);
27.232 + lua_gettable(L, LUA_GLOBALSINDEX); /* check whether lib already exists */
27.233 + if (lua_isnil(L, -1)) { /* no? */
27.234 + lua_pop(L, 1);
27.235 + lua_newtable(L); /* create it */
27.236 + lua_pushstring(L, libname);
27.237 + lua_pushvalue(L, -2);
27.238 + lua_settable(L, LUA_GLOBALSINDEX); /* register it with given name */
27.239 + }
27.240 + lua_insert(L, -(nup+1)); /* move library table to below upvalues */
27.241 + }
27.242 + for (; l->name; l++) {
27.243 + int i;
27.244 + lua_pushstring(L, l->name);
27.245 + for (i=0; i<nup; i++) /* copy upvalues to the top */
27.246 + lua_pushvalue(L, -(nup+1));
27.247 + lua_pushcclosure(L, l->func, nup);
27.248 + lua_settable(L, -(nup+3));
27.249 + }
27.250 + lua_pop(L, nup); /* remove upvalues */
27.251 +}
27.252 +
27.253 +
27.254 +
27.255 +/*
27.256 +** {======================================================
27.257 +** getn-setn: size for arrays
27.258 +** =======================================================
27.259 +*/
27.260 +
27.261 +static int checkint (lua_State *L, int topop) {
27.262 + int n = (int)lua_tonumber(L, -1);
27.263 + if (n == 0 && !lua_isnumber(L, -1)) n = -1;
27.264 + lua_pop(L, topop);
27.265 + return n;
27.266 +}
27.267 +
27.268 +
27.269 +static void getsizes (lua_State *L) {
27.270 + lua_rawgeti(L, LUA_REGISTRYINDEX, ARRAYSIZE_REF);
27.271 + if (lua_isnil(L, -1)) { /* no `size' table? */
27.272 + lua_pop(L, 1); /* remove nil */
27.273 + lua_newtable(L); /* create it */
27.274 + lua_pushvalue(L, -1); /* `size' will be its own metatable */
27.275 + lua_setmetatable(L, -2);
27.276 + lua_pushliteral(L, "__mode");
27.277 + lua_pushliteral(L, "k");
27.278 + lua_rawset(L, -3); /* metatable(N).__mode = "k" */
27.279 + lua_pushvalue(L, -1);
27.280 + lua_rawseti(L, LUA_REGISTRYINDEX, ARRAYSIZE_REF); /* store in register */
27.281 + }
27.282 +}
27.283 +
27.284 +
27.285 +void luaL_setn (lua_State *L, int t, int n) {
27.286 + t = abs_index(L, t);
27.287 + lua_pushliteral(L, "n");
27.288 + lua_rawget(L, t);
27.289 + if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */
27.290 + lua_pushliteral(L, "n"); /* use it */
27.291 + lua_pushnumber(L, (lua_Number)n);
27.292 + lua_rawset(L, t);
27.293 + }
27.294 + else { /* use `sizes' */
27.295 + getsizes(L);
27.296 + lua_pushvalue(L, t);
27.297 + lua_pushnumber(L, (lua_Number)n);
27.298 + lua_rawset(L, -3); /* sizes[t] = n */
27.299 + lua_pop(L, 1); /* remove `sizes' */
27.300 + }
27.301 +}
27.302 +
27.303 +
27.304 +int luaL_getn (lua_State *L, int t) {
27.305 + int n;
27.306 + t = abs_index(L, t);
27.307 + lua_pushliteral(L, "n"); /* try t.n */
27.308 + lua_rawget(L, t);
27.309 + if ((n = checkint(L, 1)) >= 0) return n;
27.310 + getsizes(L); /* else try sizes[t] */
27.311 + lua_pushvalue(L, t);
27.312 + lua_rawget(L, -2);
27.313 + if ((n = checkint(L, 2)) >= 0) return n;
27.314 + for (n = 1; ; n++) { /* else must count elements */
27.315 + lua_rawgeti(L, t, n);
27.316 + if (lua_isnil(L, -1)) break;
27.317 + lua_pop(L, 1);
27.318 + }
27.319 + lua_pop(L, 1);
27.320 + return n - 1;
27.321 +}
27.322 +
27.323 +/* }====================================================== */
27.324 +
27.325 +
27.326 +
27.327 +/*
27.328 +** {======================================================
27.329 +** Generic Buffer manipulation
27.330 +** =======================================================
27.331 +*/
27.332 +
27.333 +
27.334 +#define bufflen(B) ((B)->p - (B)->buffer)
27.335 +#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
27.336 +
27.337 +#define LIMIT (LUA_MINSTACK/2)
27.338 +
27.339 +
27.340 +static int emptybuffer (luaL_Buffer *B) {
27.341 + size_t l = bufflen(B);
27.342 + if (l == 0) return 0; /* put nothing on stack */
27.343 + else {
27.344 + lua_pushlstring(B->L, B->buffer, l);
27.345 + B->p = B->buffer;
27.346 + B->lvl++;
27.347 + return 1;
27.348 + }
27.349 +}
27.350 +
27.351 +
27.352 +static void adjuststack (luaL_Buffer *B) {
27.353 + if (B->lvl > 1) {
27.354 + lua_State *L = B->L;
27.355 + int toget = 1; /* number of levels to concat */
27.356 + size_t toplen = lua_strlen(L, -1);
27.357 + do {
27.358 + size_t l = lua_strlen(L, -(toget+1));
27.359 + if (B->lvl - toget + 1 >= LIMIT || toplen > l) {
27.360 + toplen += l;
27.361 + toget++;
27.362 + }
27.363 + else break;
27.364 + } while (toget < B->lvl);
27.365 + lua_concat(L, toget);
27.366 + B->lvl = B->lvl - toget + 1;
27.367 + }
27.368 +}
27.369 +
27.370 +
27.371 +LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
27.372 + if (emptybuffer(B))
27.373 + adjuststack(B);
27.374 + return B->buffer;
27.375 +}
27.376 +
27.377 +
27.378 +LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
27.379 + while (l--)
27.380 + luaL_putchar(B, *s++);
27.381 +}
27.382 +
27.383 +
27.384 +LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
27.385 + luaL_addlstring(B, s, strlen(s));
27.386 +}
27.387 +
27.388 +
27.389 +LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
27.390 + emptybuffer(B);
27.391 + lua_concat(B->L, B->lvl);
27.392 + B->lvl = 1;
27.393 +}
27.394 +
27.395 +
27.396 +LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
27.397 + lua_State *L = B->L;
27.398 + size_t vl = lua_strlen(L, -1);
27.399 + if (vl <= bufffree(B)) { /* fit into buffer? */
27.400 + memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */
27.401 + B->p += vl;
27.402 + lua_pop(L, 1); /* remove from stack */
27.403 + }
27.404 + else {
27.405 + if (emptybuffer(B))
27.406 + lua_insert(L, -2); /* put buffer before new value */
27.407 + B->lvl++; /* add new value into B stack */
27.408 + adjuststack(B);
27.409 + }
27.410 +}
27.411 +
27.412 +
27.413 +LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
27.414 + B->L = L;
27.415 + B->p = B->buffer;
27.416 + B->lvl = 0;
27.417 +}
27.418 +
27.419 +/* }====================================================== */
27.420 +
27.421 +
27.422 +LUALIB_API int luaL_ref (lua_State *L, int t) {
27.423 + int ref;
27.424 + t = abs_index(L, t);
27.425 + if (lua_isnil(L, -1)) {
27.426 + lua_pop(L, 1); /* remove from stack */
27.427 + return LUA_REFNIL; /* `nil' has a unique fixed reference */
27.428 + }
27.429 + lua_rawgeti(L, t, FREELIST_REF); /* get first free element */
27.430 + ref = (int)lua_tonumber(L, -1); /* ref = t[FREELIST_REF] */
27.431 + lua_pop(L, 1); /* remove it from stack */
27.432 + if (ref != 0) { /* any free element? */
27.433 + lua_rawgeti(L, t, ref); /* remove it from list */
27.434 + lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */
27.435 + }
27.436 + else { /* no free elements */
27.437 + ref = luaL_getn(L, t);
27.438 + if (ref < RESERVED_REFS)
27.439 + ref = RESERVED_REFS; /* skip reserved references */
27.440 + ref++; /* create new reference */
27.441 + luaL_setn(L, t, ref);
27.442 + }
27.443 + lua_rawseti(L, t, ref);
27.444 + return ref;
27.445 +}
27.446 +
27.447 +
27.448 +LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
27.449 + if (ref >= 0) {
27.450 + t = abs_index(L, t);
27.451 + lua_rawgeti(L, t, FREELIST_REF);
27.452 + lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */
27.453 + lua_pushnumber(L, (lua_Number)ref);
27.454 + lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */
27.455 + }
27.456 +}
27.457 +
27.458 +
27.459 +
27.460 +/*
27.461 +** {======================================================
27.462 +** Load functions
27.463 +** =======================================================
27.464 +*/
27.465 +
27.466 +typedef struct LoadF {
27.467 + FILE *f;
27.468 + char buff[LUAL_BUFFERSIZE];
27.469 +} LoadF;
27.470 +
27.471 +
27.472 +static const char *getF (lua_State *L, void *ud, size_t *size) {
27.473 + LoadF *lf = (LoadF *)ud;
27.474 + (void)L;
27.475 + if (feof(lf->f)) return NULL;
27.476 + *size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f);
27.477 + return (*size > 0) ? lf->buff : NULL;
27.478 +}
27.479 +
27.480 +
27.481 +static int errfile (lua_State *L, int fnameindex) {
27.482 + const char *filename = lua_tostring(L, fnameindex) + 1;
27.483 + lua_pushfstring(L, "cannot read %s: %s", filename, strerror(errno));
27.484 + lua_remove(L, fnameindex);
27.485 + return LUA_ERRFILE;
27.486 +}
27.487 +
27.488 +
27.489 +LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
27.490 + LoadF lf;
27.491 + int status, readstatus;
27.492 + int c;
27.493 + int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
27.494 + if (filename == NULL) {
27.495 + lua_pushliteral(L, "=stdin");
27.496 + lf.f = stdin;
27.497 + }
27.498 + else {
27.499 + lua_pushfstring(L, "@%s", filename);
27.500 + lf.f = fopen(filename, "r");
27.501 + }
27.502 + if (lf.f == NULL) return errfile(L, fnameindex); /* unable to open file */
27.503 + c = ungetc(getc(lf.f), lf.f);
27.504 + if (!(isspace(c) || isprint(c)) && lf.f != stdin) { /* binary file? */
27.505 + fclose(lf.f);
27.506 + lf.f = fopen(filename, "rb"); /* reopen in binary mode */
27.507 + if (lf.f == NULL) return errfile(L, fnameindex); /* unable to reopen file */
27.508 + }
27.509 + status = lua_load(L, getF, &lf, lua_tostring(L, -1));
27.510 + readstatus = ferror(lf.f);
27.511 + if (lf.f != stdin) fclose(lf.f); /* close file (even in case of errors) */
27.512 + if (readstatus) {
27.513 + lua_settop(L, fnameindex); /* ignore results from `lua_load' */
27.514 + return errfile(L, fnameindex);
27.515 + }
27.516 + lua_remove(L, fnameindex);
27.517 + return status;
27.518 +}
27.519 +
27.520 +
27.521 +typedef struct LoadS {
27.522 + const char *s;
27.523 + size_t size;
27.524 +} LoadS;
27.525 +
27.526 +
27.527 +static const char *getS (lua_State *L, void *ud, size_t *size) {
27.528 + LoadS *ls = (LoadS *)ud;
27.529 + (void)L;
27.530 + if (ls->size == 0) return NULL;
27.531 + *size = ls->size;
27.532 + ls->size = 0;
27.533 + return ls->s;
27.534 +}
27.535 +
27.536 +
27.537 +LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
27.538 + const char *name) {
27.539 + LoadS ls;
27.540 + ls.s = buff;
27.541 + ls.size = size;
27.542 + return lua_load(L, getS, &ls, name);
27.543 +}
27.544 +
27.545 +/* }====================================================== */
27.546 +
27.547 +
27.548 +/*
27.549 +** {======================================================
27.550 +** compatibility code
27.551 +** =======================================================
27.552 +*/
27.553 +
27.554 +
27.555 +static void callalert (lua_State *L, int status) {
27.556 + if (status != 0) {
27.557 + lua_getglobal(L, "_ALERT");
27.558 + if (lua_isfunction(L, -1)) {
27.559 + lua_insert(L, -2);
27.560 + lua_call(L, 1, 0);
27.561 + }
27.562 + else { /* no _ALERT function; print it on stderr */
27.563 + fprintf(stderr, "%s\n", lua_tostring(L, -2));
27.564 + lua_pop(L, 2); /* remove error message and _ALERT */
27.565 + }
27.566 + }
27.567 +}
27.568 +
27.569 +
27.570 +static int aux_do (lua_State *L, int status) {
27.571 + if (status == 0) { /* parse OK? */
27.572 + status = lua_pcall(L, 0, LUA_MULTRET, 0); /* call main */
27.573 + }
27.574 + callalert(L, status);
27.575 + return status;
27.576 +}
27.577 +
27.578 +
27.579 +LUALIB_API int lua_dofile (lua_State *L, const char *filename) {
27.580 + return aux_do(L, luaL_loadfile(L, filename));
27.581 +}
27.582 +
27.583 +
27.584 +LUALIB_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
27.585 + const char *name) {
27.586 + return aux_do(L, luaL_loadbuffer(L, buff, size, name));
27.587 +}
27.588 +
27.589 +
27.590 +LUALIB_API int lua_dostring (lua_State *L, const char *str) {
27.591 + return lua_dobuffer(L, str, strlen(str), str);
27.592 +}
27.593 +
27.594 +/* }====================================================== */
28.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
28.2 +++ b/source/lua/src/lib/lbaselib.c Wed Nov 04 00:31:14 2009 +0000
28.3 @@ -0,0 +1,675 @@
28.4 +/*
28.5 +** $Id$
28.6 +** Basic library
28.7 +** See Copyright Notice in lua.h
28.8 +*/
28.9 +
28.10 +
28.11 +
28.12 +#include <ctype.h>
28.13 +#include <stdio.h>
28.14 +#include <stdlib.h>
28.15 +#include <string.h>
28.16 +
28.17 +#define lbaselib_c
28.18 +
28.19 +#include "lua.h"
28.20 +
28.21 +#include "lauxlib.h"
28.22 +#include "lualib.h"
28.23 +
28.24 +
28.25 +
28.26 +
28.27 +/*
28.28 +** If your system does not support `stdout', you can just remove this function.
28.29 +** If you need, you can define your own `print' function, following this
28.30 +** model but changing `fputs' to put the strings at a proper place
28.31 +** (a console window or a log file, for instance).
28.32 +*/
28.33 +static int luaB_print (lua_State *L) {
28.34 + int n = lua_gettop(L); /* number of arguments */
28.35 + int i;
28.36 + lua_getglobal(L, "tostring");
28.37 + for (i=1; i<=n; i++) {
28.38 + const char *s;
28.39 + lua_pushvalue(L, -1); /* function to be called */
28.40 + lua_pushvalue(L, i); /* value to print */
28.41 + lua_call(L, 1, 1);
28.42 + s = lua_tostring(L, -1); /* get result */
28.43 + if (s == NULL)
28.44 + return luaL_error(L, "`tostring' must return a string to `print'");
28.45 + if (i>1) fputs("\t", stdout);
28.46 + fputs(s, stdout);
28.47 + lua_pop(L, 1); /* pop result */
28.48 + }
28.49 + fputs("\n", stdout);
28.50 + return 0;
28.51 +}
28.52 +
28.53 +
28.54 +static int luaB_tonumber (lua_State *L) {
28.55 + int base = luaL_optint(L, 2, 10);
28.56 + if (base == 10) { /* standard conversion */
28.57 + luaL_checkany(L, 1);
28.58 + if (lua_isnumber(L, 1)) {
28.59 + lua_pushnumber(L, lua_tonumber(L, 1));
28.60 + return 1;
28.61 + }
28.62 + }
28.63 + else {
28.64 + const char *s1 = luaL_checkstring(L, 1);
28.65 + char *s2;
28.66 + unsigned long n;
28.67 + luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
28.68 + n = strtoul(s1, &s2, base);
28.69 + if (s1 != s2) { /* at least one valid digit? */
28.70 + while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
28.71 + if (*s2 == '\0') { /* no invalid trailing characters? */
28.72 + lua_pushnumber(L, (lua_Number)n);
28.73 + return 1;
28.74 + }
28.75 + }
28.76 + }
28.77 + lua_pushnil(L); /* else not a number */
28.78 + return 1;
28.79 +}
28.80 +
28.81 +
28.82 +static int luaB_error (lua_State *L) {
28.83 + int level = luaL_optint(L, 2, 1);
28.84 + luaL_checkany(L, 1);
28.85 + if (!lua_isstring(L, 1) || level == 0)
28.86 + lua_pushvalue(L, 1); /* propagate error message without changes */
28.87 + else { /* add extra information */
28.88 + luaL_where(L, level);
28.89 + lua_pushvalue(L, 1);
28.90 + lua_concat(L, 2);
28.91 + }
28.92 + return lua_error(L);
28.93 +}
28.94 +
28.95 +
28.96 +static int luaB_getmetatable (lua_State *L) {
28.97 + luaL_checkany(L, 1);
28.98 + if (!lua_getmetatable(L, 1)) {
28.99 + lua_pushnil(L);
28.100 + return 1; /* no metatable */
28.101 + }
28.102 + luaL_getmetafield(L, 1, "__metatable");
28.103 + return 1; /* returns either __metatable field (if present) or metatable */
28.104 +}
28.105 +
28.106 +
28.107 +static int luaB_setmetatable (lua_State *L) {
28.108 + int t = lua_type(L, 2);
28.109 + luaL_checktype(L, 1, LUA_TTABLE);
28.110 + luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
28.111 + "nil or table expected");
28.112 + if (luaL_getmetafield(L, 1, "__metatable"))
28.113 + luaL_error(L, "cannot change a protected metatable");
28.114 + lua_settop(L, 2);
28.115 + lua_setmetatable(L, 1);
28.116 + return 1;
28.117 +}
28.118 +
28.119 +
28.120 +static void getfunc (lua_State *L) {
28.121 + if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
28.122 + else {
28.123 + lua_Debug ar;
28.124 + int level = luaL_optint(L, 1, 1);
28.125 + luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
28.126 + if (lua_getstack(L, level, &ar) == 0)
28.127 + luaL_argerror(L, 1, "invalid level");
28.128 + lua_getinfo(L, "f", &ar);
28.129 + if (lua_isnil(L, -1))
28.130 + luaL_error(L, "no function environment for tail call at level %d",
28.131 + level);
28.132 + }
28.133 +}
28.134 +
28.135 +
28.136 +static int aux_getfenv (lua_State *L) {
28.137 + lua_getfenv(L, -1);
28.138 + lua_pushliteral(L, "__fenv");
28.139 + lua_rawget(L, -2);
28.140 + return !lua_isnil(L, -1);
28.141 +}
28.142 +
28.143 +
28.144 +static int luaB_getfenv (lua_State *L) {
28.145 + getfunc(L);
28.146 + if (!aux_getfenv(L)) /* __fenv not defined? */
28.147 + lua_pop(L, 1); /* remove it, to return real environment */
28.148 + return 1;
28.149 +}
28.150 +
28.151 +
28.152 +static int luaB_setfenv (lua_State *L) {
28.153 + luaL_checktype(L, 2, LUA_TTABLE);
28.154 + getfunc(L);
28.155 + if (aux_getfenv(L)) /* __fenv defined? */
28.156 + luaL_error(L, "`setfenv' cannot change a protected environment");
28.157 + else
28.158 + lua_pop(L, 2); /* remove __fenv and real environment table */
28.159 + lua_pushvalue(L, 2);
28.160 + if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0)
28.161 + lua_replace(L, LUA_GLOBALSINDEX);
28.162 + else if (lua_setfenv(L, -2) == 0)
28.163 + luaL_error(L, "`setfenv' cannot change environment of given function");
28.164 + return 0;
28.165 +}
28.166 +
28.167 +
28.168 +static int luaB_rawequal (lua_State *L) {
28.169 + luaL_checkany(L, 1);
28.170 + luaL_checkany(L, 2);
28.171 + lua_pushboolean(L, lua_rawequal(L, 1, 2));
28.172 + return 1;
28.173 +}
28.174 +
28.175 +
28.176 +static int luaB_rawget (lua_State *L) {
28.177 + luaL_checktype(L, 1, LUA_TTABLE);
28.178 + luaL_checkany(L, 2);
28.179 + lua_rawget(L, 1);
28.180 + return 1;
28.181 +}
28.182 +
28.183 +static int luaB_rawset (lua_State *L) {
28.184 + luaL_checktype(L, 1, LUA_TTABLE);
28.185 + luaL_checkany(L, 2);
28.186 + luaL_checkany(L, 3);
28.187 + lua_rawset(L, 1);
28.188 + return 1;
28.189 +}
28.190 +
28.191 +
28.192 +static int luaB_gcinfo (lua_State *L) {
28.193 + lua_pushnumber(L, (lua_Number)lua_getgccount(L));
28.194 + lua_pushnumber(L, (lua_Number)lua_getgcthreshold(L));
28.195 + return 2;
28.196 +}
28.197 +
28.198 +
28.199 +static int luaB_collectgarbage (lua_State *L) {
28.200 + lua_setgcthreshold(L, luaL_optint(L, 1, 0));
28.201 + return 0;
28.202 +}
28.203 +
28.204 +
28.205 +static int luaB_type (lua_State *L) {
28.206 + luaL_checkany(L, 1);
28.207 + lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
28.208 + return 1;
28.209 +}
28.210 +
28.211 +
28.212 +static int luaB_next (lua_State *L) {
28.213 + luaL_checktype(L, 1, LUA_TTABLE);
28.214 + lua_settop(L, 2); /* create a 2nd argument if there isn't one */
28.215 + if (lua_next(L, 1))
28.216 + return 2;
28.217 + else {
28.218 + lua_pushnil(L);
28.219 + return 1;
28.220 + }
28.221 +}
28.222 +
28.223 +
28.224 +static int luaB_pairs (lua_State *L) {
28.225 + luaL_checktype(L, 1, LUA_TTABLE);
28.226 + lua_pushliteral(L, "next");
28.227 + lua_rawget(L, LUA_GLOBALSINDEX); /* return generator, */
28.228 + lua_pushvalue(L, 1); /* state, */
28.229 + lua_pushnil(L); /* and initial value */
28.230 + return 3;
28.231 +}
28.232 +
28.233 +
28.234 +static int luaB_ipairs (lua_State *L) {
28.235 + lua_Number i = lua_tonumber(L, 2);
28.236 + luaL_checktype(L, 1, LUA_TTABLE);
28.237 + if (i == 0 && lua_isnone(L, 2)) { /* `for' start? */
28.238 + lua_pushliteral(L, "ipairs");
28.239 + lua_rawget(L, LUA_GLOBALSINDEX); /* return generator, */
28.240 + lua_pushvalue(L, 1); /* state, */
28.241 + lua_pushnumber(L, 0); /* and initial value */
28.242 + return 3;
28.243 + }
28.244 + else { /* `for' step */
28.245 + i++; /* next value */
28.246 + lua_pushnumber(L, i);
28.247 + lua_rawgeti(L, 1, (int)i);
28.248 + return (lua_isnil(L, -1)) ? 0 : 2;
28.249 + }
28.250 +}
28.251 +
28.252 +
28.253 +static int load_aux (lua_State *L, int status) {
28.254 + if (status == 0) /* OK? */
28.255 + return 1;
28.256 + else {
28.257 + lua_pushnil(L);
28.258 + lua_insert(L, -2); /* put before error message */
28.259 + return 2; /* return nil plus error message */
28.260 + }
28.261 +}
28.262 +
28.263 +
28.264 +static int luaB_loadstring (lua_State *L) {
28.265 + size_t l;
28.266 + const char *s = luaL_checklstring(L, 1, &l);
28.267 + const char *chunkname = luaL_optstring(L, 2, s);
28.268 + return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
28.269 +}
28.270 +
28.271 +
28.272 +static int luaB_loadfile (lua_State *L) {
28.273 + const char *fname = luaL_optstring(L, 1, NULL);
28.274 + return load_aux(L, luaL_loadfile(L, fname));
28.275 +}
28.276 +
28.277 +
28.278 +static int luaB_dofile (lua_State *L) {
28.279 + const char *fname = luaL_optstring(L, 1, NULL);
28.280 + int n = lua_gettop(L);
28.281 + int status = luaL_loadfile(L, fname);
28.282 + if (status != 0) lua_error(L);
28.283 + lua_call(L, 0, LUA_MULTRET);
28.284 + return lua_gettop(L) - n;
28.285 +}
28.286 +
28.287 +
28.288 +static int luaB_assert (lua_State *L) {
28.289 + luaL_checkany(L, 1);
28.290 + if (!lua_toboolean(L, 1))
28.291 + return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
28.292 + lua_settop(L, 1);
28.293 + return 1;
28.294 +}
28.295 +
28.296 +
28.297 +static int luaB_unpack (lua_State *L) {
28.298 + int n, i;
28.299 + luaL_checktype(L, 1, LUA_TTABLE);
28.300 + n = luaL_getn(L, 1);
28.301 + luaL_checkstack(L, n, "table too big to unpack");
28.302 + for (i=1; i<=n; i++) /* push arg[1...n] */
28.303 + lua_rawgeti(L, 1, i);
28.304 + return n;
28.305 +}
28.306 +
28.307 +
28.308 +static int luaB_pcall (lua_State *L) {
28.309 + int status;
28.310 + luaL_checkany(L, 1);
28.311 + status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
28.312 + lua_pushboolean(L, (status == 0));
28.313 + lua_insert(L, 1);
28.314 + return lua_gettop(L); /* return status + all results */
28.315 +}
28.316 +
28.317 +
28.318 +static int luaB_xpcall (lua_State *L) {
28.319 + int status;
28.320 + luaL_checkany(L, 2);
28.321 + lua_settop(L, 2);
28.322 + lua_insert(L, 1); /* put error function under function to be called */
28.323 + status = lua_pcall(L, 0, LUA_MULTRET, 1);
28.324 + lua_pushboolean(L, (status == 0));
28.325 + lua_replace(L, 1);
28.326 + return lua_gettop(L); /* return status + all results */
28.327 +}
28.328 +
28.329 +
28.330 +static int luaB_tostring (lua_State *L) {
28.331 + char buff[128];
28.332 + luaL_checkany(L, 1);
28.333 + if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
28.334 + return 1; /* use its value */
28.335 + switch (lua_type(L, 1)) {
28.336 + case LUA_TNUMBER:
28.337 + lua_pushstring(L, lua_tostring(L, 1));
28.338 + return 1;
28.339 + case LUA_TSTRING:
28.340 + lua_pushvalue(L, 1);
28.341 + return 1;
28.342 + case LUA_TBOOLEAN:
28.343 + lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
28.344 + return 1;
28.345 + case LUA_TTABLE:
28.346 + sprintf(buff, "table: %p", lua_topointer(L, 1));
28.347 + break;
28.348 + case LUA_TFUNCTION:
28.349 + sprintf(buff, "function: %p", lua_topointer(L, 1));
28.350 + break;
28.351 + case LUA_TUSERDATA:
28.352 + case LUA_TLIGHTUSERDATA:
28.353 + sprintf(buff, "userdata: %p", lua_touserdata(L, 1));
28.354 + break;
28.355 + case LUA_TTHREAD:
28.356 + sprintf(buff, "thread: %p", (void *)lua_tothread(L, 1));
28.357 + break;
28.358 + case LUA_TNIL:
28.359 + lua_pushliteral(L, "nil");
28.360 + return 1;
28.361 + }
28.362 + lua_pushstring(L, buff);
28.363 + return 1;
28.364 +}
28.365 +
28.366 +
28.367 +static int luaB_newproxy (lua_State *L) {
28.368 + lua_settop(L, 1);
28.369 + lua_newuserdata(L, 0); /* create proxy */
28.370 + if (lua_toboolean(L, 1) == 0)
28.371 + return 1; /* no metatable */
28.372 + else if (lua_isboolean(L, 1)) {
28.373 + lua_newtable(L); /* create a new metatable `m' ... */
28.374 + lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
28.375 + lua_pushboolean(L, 1);
28.376 + lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
28.377 + }
28.378 + else {
28.379 + int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
28.380 + if (lua_getmetatable(L, 1)) {
28.381 + lua_rawget(L, lua_upvalueindex(1));
28.382 + validproxy = lua_toboolean(L, -1);
28.383 + lua_pop(L, 1); /* remove value */
28.384 + }
28.385 + luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
28.386 + lua_getmetatable(L, 1); /* metatable is valid; get it */
28.387 + }
28.388 + lua_setmetatable(L, 2);
28.389 + return 1;
28.390 +}
28.391 +
28.392 +
28.393 +/*
28.394 +** {======================================================
28.395 +** `require' function
28.396 +** =======================================================
28.397 +*/
28.398 +
28.399 +
28.400 +/* name of global that holds table with loaded packages */
28.401 +#define REQTAB "_LOADED"
28.402 +
28.403 +/* name of global that holds the search path for packages */
28.404 +#define LUA_PATH "LUA_PATH"
28.405 +
28.406 +#ifndef LUA_PATH_SEP
28.407 +#define LUA_PATH_SEP ';'
28.408 +#endif
28.409 +
28.410 +#ifndef LUA_PATH_MARK
28.411 +#define LUA_PATH_MARK '?'
28.412 +#endif
28.413 +
28.414 +#ifndef LUA_PATH_DEFAULT
28.415 +#define LUA_PATH_DEFAULT "?;?.lua"
28.416 +#endif
28.417 +
28.418 +
28.419 +static const char *getpath (lua_State *L) {
28.420 + const char *path;
28.421 + lua_getglobal(L, LUA_PATH); /* try global variable */
28.422 + path = lua_tostring(L, -1);
28.423 + lua_pop(L, 1);
28.424 + if (path) return path;
28.425 + path = getenv(LUA_PATH); /* else try environment variable */
28.426 + if (path) return path;
28.427 + return LUA_PATH_DEFAULT; /* else use default */
28.428 +}
28.429 +
28.430 +
28.431 +static const char *pushnextpath (lua_State *L, const char *path) {
28.432 + const char *l;
28.433 + if (*path == '\0') return NULL; /* no more paths */
28.434 + if (*path == LUA_PATH_SEP) path++; /* skip separator */
28.435 + l = strchr(path, LUA_PATH_SEP); /* find next separator */
28.436 + if (l == NULL) l = path+strlen(path);
28.437 + lua_pushlstring(L, path, l - path); /* directory name */
28.438 + return l;
28.439 +}
28.440 +
28.441 +
28.442 +static void pushcomposename (lua_State *L) {
28.443 + const char *path = lua_tostring(L, -1);
28.444 + const char *wild;
28.445 + int n = 1;
28.446 + while ((wild = strchr(path, LUA_PATH_MARK)) != NULL) {
28.447 + /* is there stack space for prefix, name, and eventual last sufix? */
28.448 + luaL_checkstack(L, 3, "too many marks in a path component");
28.449 + lua_pushlstring(L, path, wild - path); /* push prefix */
28.450 + lua_pushvalue(L, 1); /* push package name (in place of MARK) */
28.451 + path = wild + 1; /* continue after MARK */
28.452 + n += 2;
28.453 + }
28.454 + lua_pushstring(L, path); /* push last sufix (`n' already includes this) */
28.455 + lua_concat(L, n);
28.456 +}
28.457 +
28.458 +
28.459 +static int luaB_require (lua_State *L) {
28.460 + const char *path;
28.461 + int status = LUA_ERRFILE; /* not found (yet) */
28.462 + luaL_checkstring(L, 1);
28.463 + lua_settop(L, 1);
28.464 + lua_getglobal(L, REQTAB);
28.465 + if (!lua_istable(L, 2)) return luaL_error(L, "`" REQTAB "' is not a table");
28.466 + path = getpath(L);
28.467 + lua_pushvalue(L, 1); /* check package's name in book-keeping table */
28.468 + lua_rawget(L, 2);
28.469 + if (lua_toboolean(L, -1)) /* is it there? */
28.470 + return 1; /* package is already loaded; return its result */
28.471 + else { /* must load it */
28.472 + while (status == LUA_ERRFILE) {
28.473 + lua_settop(L, 3); /* reset stack position */
28.474 + if ((path = pushnextpath(L, path)) == NULL) break;
28.475 + pushcomposename(L);
28.476 + status = luaL_loadfile(L, lua_tostring(L, -1)); /* try to load it */
28.477 + }
28.478 + }
28.479 + switch (status) {
28.480 + case 0: {
28.481 + lua_getglobal(L, "_REQUIREDNAME"); /* save previous name */
28.482 + lua_insert(L, -2); /* put it below function */
28.483 + lua_pushvalue(L, 1);
28.484 + lua_setglobal(L, "_REQUIREDNAME"); /* set new name */
28.485 + lua_call(L, 0, 1); /* run loaded module */
28.486 + lua_insert(L, -2); /* put result below previous name */
28.487 + lua_setglobal(L, "_REQUIREDNAME"); /* reset to previous name */
28.488 + if (lua_isnil(L, -1)) { /* no/nil return? */
28.489 + lua_pushboolean(L, 1);
28.490 + lua_replace(L, -2); /* replace to true */
28.491 + }
28.492 + lua_pushvalue(L, 1);
28.493 + lua_pushvalue(L, -2);
28.494 + lua_rawset(L, 2); /* mark it as loaded */
28.495 + return 1; /* return value */
28.496 + }
28.497 + case LUA_ERRFILE: { /* file not found */
28.498 + return luaL_error(L, "could not load package `%s' from path `%s'",
28.499 + lua_tostring(L, 1), getpath(L));
28.500 + }
28.501 + default: {
28.502 + return luaL_error(L, "error loading package `%s' (%s)",
28.503 + lua_tostring(L, 1), lua_tostring(L, -1));
28.504 + }
28.505 + }
28.506 +}
28.507 +
28.508 +/* }====================================================== */
28.509 +
28.510 +
28.511 +static const luaL_reg base_funcs[] = {
28.512 + {"error", luaB_error},
28.513 + {"getmetatable", luaB_getmetatable},
28.514 + {"setmetatable", luaB_setmetatable},
28.515 + {"getfenv", luaB_getfenv},
28.516 + {"setfenv", luaB_setfenv},
28.517 + {"next", luaB_next},
28.518 + {"ipairs", luaB_ipairs},
28.519 + {"pairs", luaB_pairs},
28.520 + {"print", luaB_print},
28.521 + {"tonumber", luaB_tonumber},
28.522 + {"tostring", luaB_tostring},
28.523 + {"type", luaB_type},
28.524 + {"assert", luaB_assert},
28.525 + {"unpack", luaB_unpack},
28.526 + {"rawequal", luaB_rawequal},
28.527 + {"rawget", luaB_rawget},
28.528 + {"rawset", luaB_rawset},
28.529 + {"pcall", luaB_pcall},
28.530 + {"xpcall", luaB_xpcall},
28.531 + {"collectgarbage", luaB_collectgarbage},
28.532 + {"gcinfo", luaB_gcinfo},
28.533 + {"loadfile", luaB_loadfile},
28.534 + {"dofile", luaB_dofile},
28.535 + {"loadstring", luaB_loadstring},
28.536 + {"require", luaB_require},
28.537 + {NULL, NULL}
28.538 +};
28.539 +
28.540 +
28.541 +/*
28.542 +** {======================================================
28.543 +** Coroutine library
28.544 +** =======================================================
28.545 +*/
28.546 +
28.547 +static int auxresume (lua_State *L, lua_State *co, int narg) {
28.548 + int status;
28.549 + if (!lua_checkstack(co, narg))
28.550 + luaL_error(L, "too many arguments to resume");
28.551 + lua_xmove(L, co, narg);
28.552 + status = lua_resume(co, narg);
28.553 + if (status == 0) {
28.554 + int nres = lua_gettop(co);
28.555 + if (!lua_checkstack(L, nres))
28.556 + luaL_error(L, "too many results to resume");
28.557 + lua_xmove(co, L, nres); /* move yielded values */
28.558 + return nres;
28.559 + }
28.560 + else {
28.561 + lua_xmove(co, L, 1); /* move error message */
28.562 + return -1; /* error flag */
28.563 + }
28.564 +}
28.565 +
28.566 +
28.567 +static int luaB_coresume (lua_State *L) {
28.568 + lua_State *co = lua_tothread(L, 1);
28.569 + int r;
28.570 + luaL_argcheck(L, co, 1, "coroutine expected");
28.571 + r = auxresume(L, co, lua_gettop(L) - 1);
28.572 + if (r < 0) {
28.573 + lua_pushboolean(L, 0);
28.574 + lua_insert(L, -2);
28.575 + return 2; /* return false + error message */
28.576 + }
28.577 + else {
28.578 + lua_pushboolean(L, 1);
28.579 + lua_insert(L, -(r + 1));
28.580 + return r + 1; /* return true + `resume' returns */
28.581 + }
28.582 +}
28.583 +
28.584 +
28.585 +static int luaB_auxwrap (lua_State *L) {
28.586 + lua_State *co = lua_tothread(L, lua_upvalueindex(1));
28.587 + int r = auxresume(L, co, lua_gettop(L));
28.588 + if (r < 0) {
28.589 + if (lua_isstring(L, -1)) { /* error object is a string? */
28.590 + luaL_where(L, 1); /* add extra info */
28.591 + lua_insert(L, -2);
28.592 + lua_concat(L, 2);
28.593 + }
28.594 + lua_error(L); /* propagate error */
28.595 + }
28.596 + return r;
28.597 +}
28.598 +
28.599 +
28.600 +static int luaB_cocreate (lua_State *L) {
28.601 + lua_State *NL = lua_newthread(L);
28.602 + luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
28.603 + "Lua function expected");
28.604 + lua_pushvalue(L, 1); /* move function to top */
28.605 + lua_xmove(L, NL, 1); /* move function from L to NL */
28.606 + return 1;
28.607 +}
28.608 +
28.609 +
28.610 +static int luaB_cowrap (lua_State *L) {
28.611 + luaB_cocreate(L);
28.612 + lua_pushcclosure(L, luaB_auxwrap, 1);
28.613 + return 1;
28.614 +}
28.615 +
28.616 +
28.617 +static int luaB_yield (lua_State *L) {
28.618 + return lua_yield(L, lua_gettop(L));
28.619 +}
28.620 +
28.621 +
28.622 +static int luaB_costatus (lua_State *L) {
28.623 + lua_State *co = lua_tothread(L, 1);
28.624 + luaL_argcheck(L, co, 1, "coroutine expected");
28.625 + if (L == co) lua_pushliteral(L, "running");
28.626 + else {
28.627 + lua_Debug ar;
28.628 + if (lua_getstack(co, 0, &ar) == 0 && lua_gettop(co) == 0)
28.629 + lua_pushliteral(L, "dead");
28.630 + else
28.631 + lua_pushliteral(L, "suspended");
28.632 + }
28.633 + return 1;
28.634 +}
28.635 +
28.636 +
28.637 +static const luaL_reg co_funcs[] = {
28.638 + {"create", luaB_cocreate},
28.639 + {"wrap", luaB_cowrap},
28.640 + {"resume", luaB_coresume},
28.641 + {"yield", luaB_yield},
28.642 + {"status", luaB_costatus},
28.643 + {NULL, NULL}
28.644 +};
28.645 +
28.646 +/* }====================================================== */
28.647 +
28.648 +
28.649 +
28.650 +static void base_open (lua_State *L) {
28.651 + lua_pushliteral(L, "_G");
28.652 + lua_pushvalue(L, LUA_GLOBALSINDEX);
28.653 + luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */
28.654 + lua_pushliteral(L, "_VERSION");
28.655 + lua_pushliteral(L, LUA_VERSION);
28.656 + lua_rawset(L, -3); /* set global _VERSION */
28.657 + /* `newproxy' needs a weaktable as upvalue */
28.658 + lua_pushliteral(L, "newproxy");
28.659 + lua_newtable(L); /* new table `w' */
28.660 + lua_pushvalue(L, -1); /* `w' will be its own metatable */
28.661 + lua_setmetatable(L, -2);
28.662 + lua_pushliteral(L, "__mode");
28.663 + lua_pushliteral(L, "k");
28.664 + lua_rawset(L, -3); /* metatable(w).__mode = "k" */
28.665 + lua_pushcclosure(L, luaB_newproxy, 1);
28.666 + lua_rawset(L, -3); /* set global `newproxy' */
28.667 + lua_rawset(L, -1); /* set global _G */
28.668 +}
28.669 +
28.670 +
28.671 +LUALIB_API int luaopen_base (lua_State *L) {
28.672 + base_open(L);
28.673 + luaL_openlib(L, LUA_COLIBNAME, co_funcs, 0);
28.674 + lua_newtable(L);
28.675 + lua_setglobal(L, REQTAB);
28.676 + return 0;
28.677 +}
28.678 +
29.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
29.2 +++ b/source/lua/src/lib/ldblib.c Wed Nov 04 00:31:14 2009 +0000
29.3 @@ -0,0 +1,299 @@
29.4 +/*
29.5 +** $Id$
29.6 +** Interface from Lua to its debug API
29.7 +** See Copyright Notice in lua.h
29.8 +*/
29.9 +
29.10 +
29.11 +#include <stdio.h>
29.12 +#include <stdlib.h>
29.13 +#include <string.h>
29.14 +
29.15 +#define ldblib_c
29.16 +
29.17 +#include "lua.h"
29.18 +
29.19 +#include "lauxlib.h"
29.20 +#include "lualib.h"
29.21 +
29.22 +
29.23 +
29.24 +static void settabss (lua_State *L, const char *i, const char *v) {
29.25 + lua_pushstring(L, i);
29.26 + lua_pushstring(L, v);
29.27 + lua_rawset(L, -3);
29.28 +}
29.29 +
29.30 +
29.31 +static void settabsi (lua_State *L, const char *i, int v) {
29.32 + lua_pushstring(L, i);
29.33 + lua_pushnumber(L, (lua_Number)v);
29.34 + lua_rawset(L, -3);
29.35 +}
29.36 +
29.37 +
29.38 +static int getinfo (lua_State *L) {
29.39 + lua_Debug ar;
29.40 + const char *options = luaL_optstring(L, 2, "flnSu");
29.41 + if (lua_isnumber(L, 1)) {
29.42 + if (!lua_getstack(L, (int)(lua_tonumber(L, 1)), &ar)) {
29.43 + lua_pushnil(L); /* level out of range */
29.44 + return 1;
29.45 + }
29.46 + }
29.47 + else if (lua_isfunction(L, 1)) {
29.48 + lua_pushfstring(L, ">%s", options);
29.49 + options = lua_tostring(L, -1);
29.50 + lua_pushvalue(L, 1);
29.51 + }
29.52 + else
29.53 + return luaL_argerror(L, 1, "function or level expected");
29.54 + if (!lua_getinfo(L, options, &ar))
29.55 + return luaL_argerror(L, 2, "invalid option");
29.56 + lua_newtable(L);
29.57 + for (; *options; options++) {
29.58 + switch (*options) {
29.59 + case 'S':
29.60 + settabss(L, "source", ar.source);
29.61 + settabss(L, "short_src", ar.short_src);
29.62 + settabsi(L, "linedefined", ar.linedefined);
29.63 + settabss(L, "what", ar.what);
29.64 + break;
29.65 + case 'l':
29.66 + settabsi(L, "currentline", ar.currentline);
29.67 + break;
29.68 + case 'u':
29.69 + settabsi(L, "nups", ar.nups);
29.70 + break;
29.71 + case 'n':
29.72 + settabss(L, "name", ar.name);
29.73 + settabss(L, "namewhat", ar.namewhat);
29.74 + break;
29.75 + case 'f':
29.76 + lua_pushliteral(L, "func");
29.77 + lua_pushvalue(L, -3);
29.78 + lua_rawset(L, -3);
29.79 + break;
29.80 + }
29.81 + }
29.82 + return 1; /* return table */
29.83 +}
29.84 +
29.85 +
29.86 +static int getlocal (lua_State *L) {
29.87 + lua_Debug ar;
29.88 + const char *name;
29.89 + if (!lua_getstack(L, luaL_checkint(L, 1), &ar)) /* level out of range? */
29.90 + return luaL_argerror(L, 1, "level out of range");
29.91 + name = lua_getlocal(L, &ar, luaL_checkint(L, 2));
29.92 + if (name) {
29.93 + lua_pushstring(L, name);
29.94 + lua_pushvalue(L, -2);
29.95 + return 2;
29.96 + }
29.97 + else {
29.98 + lua_pushnil(L);
29.99 + return 1;
29.100 + }
29.101 +}
29.102 +
29.103 +
29.104 +static int setlocal (lua_State *L) {
29.105 + lua_Debug ar;
29.106 + if (!lua_getstack(L, luaL_checkint(L, 1), &ar)) /* level out of range? */
29.107 + return luaL_argerror(L, 1, "level out of range");
29.108 + luaL_checkany(L, 3);
29.109 + lua_pushstring(L, lua_setlocal(L, &ar, luaL_checkint(L, 2)));
29.110 + return 1;
29.111 +}
29.112 +
29.113 +
29.114 +static int auxupvalue (lua_State *L, int get) {
29.115 + const char *name;
29.116 + int n = luaL_checkint(L, 2);
29.117 + luaL_checktype(L, 1, LUA_TFUNCTION);
29.118 + if (lua_iscfunction(L, 1)) return 0; /* cannot touch C upvalues from Lua */
29.119 + name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
29.120 + if (name == NULL) return 0;
29.121 + lua_pushstring(L, name);
29.122 + lua_insert(L, -(get+1));
29.123 + return get + 1;
29.124 +}
29.125 +
29.126 +
29.127 +static int getupvalue (lua_State *L) {
29.128 + return auxupvalue(L, 1);
29.129 +}
29.130 +
29.131 +
29.132 +static int setupvalue (lua_State *L) {
29.133 + luaL_checkany(L, 3);
29.134 + return auxupvalue(L, 0);
29.135 +}
29.136 +
29.137 +
29.138 +
29.139 +static const char KEY_HOOK = 'h';
29.140 +
29.141 +
29.142 +static void hookf (lua_State *L, lua_Debug *ar) {
29.143 + static const char *const hooknames[] =
29.144 + {"call", "return", "line", "count", "tail return"};
29.145 + lua_pushlightuserdata(L, (void *)&KEY_HOOK);
29.146 + lua_rawget(L, LUA_REGISTRYINDEX);
29.147 + if (lua_isfunction(L, -1)) {
29.148 + lua_pushstring(L, hooknames[(int)ar->event]);
29.149 + if (ar->currentline >= 0)
29.150 + lua_pushnumber(L, (lua_Number)ar->currentline);
29.151 + else lua_pushnil(L);
29.152 + lua_assert(lua_getinfo(L, "lS", ar));
29.153 + lua_call(L, 2, 0);
29.154 + }
29.155 + else
29.156 + lua_pop(L, 1); /* pop result from gettable */
29.157 +}
29.158 +
29.159 +
29.160 +static int makemask (const char *smask, int count) {
29.161 + int mask = 0;
29.162 + if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
29.163 + if (strchr(smask, 'r')) mask |= LUA_MASKRET;
29.164 + if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
29.165 + if (count > 0) mask |= LUA_MASKCOUNT;
29.166 + return mask;
29.167 +}
29.168 +
29.169 +
29.170 +static char *unmakemask (int mask, char *smask) {
29.171 + int i = 0;
29.172 + if (mask & LUA_MASKCALL) smask[i++] = 'c';
29.173 + if (mask & LUA_MASKRET) smask[i++] = 'r';
29.174 + if (mask & LUA_MASKLINE) smask[i++] = 'l';
29.175 + smask[i] = '\0';
29.176 + return smask;
29.177 +}
29.178 +
29.179 +
29.180 +static int sethook (lua_State *L) {
29.181 + if (lua_isnoneornil(L, 1)) {
29.182 + lua_settop(L, 1);
29.183 + lua_sethook(L, NULL, 0, 0); /* turn off hooks */
29.184 + }
29.185 + else {
29.186 + const char *smask = luaL_checkstring(L, 2);
29.187 + int count = luaL_optint(L, 3, 0);
29.188 + luaL_checktype(L, 1, LUA_TFUNCTION);
29.189 + lua_sethook(L, hookf, makemask(smask, count), count);
29.190 + }
29.191 + lua_pushlightuserdata(L, (void *)&KEY_HOOK);
29.192 + lua_pushvalue(L, 1);
29.193 + lua_rawset(L, LUA_REGISTRYINDEX); /* set new hook */
29.194 + return 0;
29.195 +}
29.196 +
29.197 +
29.198 +static int gethook (lua_State *L) {
29.199 + char buff[5];
29.200 + int mask = lua_gethookmask(L);
29.201 + lua_Hook hook = lua_gethook(L);
29.202 + if (hook != NULL && hook != hookf) /* external hook? */
29.203 + lua_pushliteral(L, "external hook");
29.204 + else {
29.205 + lua_pushlightuserdata(L, (void *)&KEY_HOOK);
29.206 + lua_rawget(L, LUA_REGISTRYINDEX); /* get hook */
29.207 + }
29.208 + lua_pushstring(L, unmakemask(mask, buff));
29.209 + lua_pushnumber(L, (lua_Number)lua_gethookcount(L));
29.210 + return 3;
29.211 +}
29.212 +
29.213 +
29.214 +static int debug (lua_State *L) {
29.215 + for (;;) {
29.216 + char buffer[250];
29.217 + fputs("lua_debug> ", stderr);
29.218 + if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
29.219 + strcmp(buffer, "cont\n") == 0)
29.220 + return 0;
29.221 + lua_dostring(L, buffer);
29.222 + lua_settop(L, 0); /* remove eventual returns */
29.223 + }
29.224 +}
29.225 +
29.226 +
29.227 +#define LEVELS1 12 /* size of the first part of the stack */
29.228 +#define LEVELS2 10 /* size of the second part of the stack */
29.229 +
29.230 +static int errorfb (lua_State *L) {
29.231 + int level = 1; /* skip level 0 (it's this function) */
29.232 + int firstpart = 1; /* still before eventual `...' */
29.233 + lua_Debug ar;
29.234 + if (lua_gettop(L) == 0)
29.235 + lua_pushliteral(L, "");
29.236 + else if (!lua_isstring(L, 1)) return 1; /* no string message */
29.237 + else lua_pushliteral(L, "\n");
29.238 + lua_pushliteral(L, "stack traceback:");
29.239 + while (lua_getstack(L, level++, &ar)) {
29.240 + if (level > LEVELS1 && firstpart) {
29.241 + /* no more than `LEVELS2' more levels? */
29.242 + if (!lua_getstack(L, level+LEVELS2, &ar))
29.243 + level--; /* keep going */
29.244 + else {
29.245 + lua_pushliteral(L, "\n\t..."); /* too many levels */
29.246 + while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */
29.247 + level++;
29.248 + }
29.249 + firstpart = 0;
29.250 + continue;
29.251 + }
29.252 + lua_pushliteral(L, "\n\t");
29.253 + lua_getinfo(L, "Snl", &ar);
29.254 + lua_pushfstring(L, "%s:", ar.short_src);
29.255 + if (ar.currentline > 0)
29.256 + lua_pushfstring(L, "%d:", ar.currentline);
29.257 + switch (*ar.namewhat) {
29.258 + case 'g': /* global */
29.259 + case 'l': /* local */
29.260 + case 'f': /* field */
29.261 + case 'm': /* method */
29.262 + lua_pushfstring(L, " in function `%s'", ar.name);
29.263 + break;
29.264 + default: {
29.265 + if (*ar.what == 'm') /* main? */
29.266 + lua_pushfstring(L, " in main chunk");
29.267 + else if (*ar.what == 'C' || *ar.what == 't')
29.268 + lua_pushliteral(L, " ?"); /* C function or tail call */
29.269 + else
29.270 + lua_pushfstring(L, " in function <%s:%d>",
29.271 + ar.short_src, ar.linedefined);
29.272 + }
29.273 + }
29.274 + lua_concat(L, lua_gettop(L));
29.275 + }
29.276 + lua_concat(L, lua_gettop(L));
29.277 + return 1;
29.278 +}
29.279 +
29.280 +
29.281 +static const luaL_reg dblib[] = {
29.282 + {"getlocal", getlocal},
29.283 + {"getinfo", getinfo},
29.284 + {"gethook", gethook},
29.285 + {"getupvalue", getupvalue},
29.286 + {"sethook", sethook},
29.287 + {"setlocal", setlocal},
29.288 + {"setupvalue", setupvalue},
29.289 + {"debug", debug},
29.290 + {"traceback", errorfb},
29.291 + {NULL, NULL}
29.292 +};
29.293 +
29.294 +
29.295 +LUALIB_API int luaopen_debug (lua_State *L) {
29.296 + luaL_openlib(L, LUA_DBLIBNAME, dblib, 0);
29.297 + lua_pushliteral(L, "_TRACEBACK");
29.298 + lua_pushcfunction(L, errorfb);
29.299 + lua_settable(L, LUA_GLOBALSINDEX);
29.300 + return 1;
29.301 +}
29.302 +
30.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
30.2 +++ b/source/lua/src/lib/liolib.c Wed Nov 04 00:31:14 2009 +0000
30.3 @@ -0,0 +1,750 @@
30.4 +/*
30.5 +** $Id$
30.6 +** Standard I/O (and system) library
30.7 +** See Copyright Notice in lua.h
30.8 +*/
30.9 +
30.10 +
30.11 +#include <errno.h>
30.12 +#include <locale.h>
30.13 +#include <stdio.h>
30.14 +#include <stdlib.h>
30.15 +#include <string.h>
30.16 +#include <time.h>
30.17 +
30.18 +#define liolib_c
30.19 +
30.20 +#include "lua.h"
30.21 +
30.22 +#include "lauxlib.h"
30.23 +#include "lualib.h"
30.24 +
30.25 +
30.26 +
30.27 +/*
30.28 +** by default, gcc does not get `tmpname'
30.29 +*/
30.30 +#ifndef USE_TMPNAME
30.31 +#ifdef __GNUC__
30.32 +#define USE_TMPNAME 0
30.33 +#else
30.34 +#define USE_TMPNAME 1
30.35 +#endif
30.36 +#endif
30.37 +
30.38 +
30.39 +/*
30.40 +** by default, posix systems get `popen'
30.41 +*/
30.42 +#ifndef USE_POPEN
30.43 +#ifdef _POSIX_C_SOURCE
30.44 +#if _POSIX_C_SOURCE >= 2
30.45 +#define USE_POPEN 1
30.46 +#endif
30.47 +#endif
30.48 +#endif
30.49 +
30.50 +#ifndef USE_POPEN
30.51 +#define USE_POPEN 0
30.52 +#endif
30.53 +
30.54 +
30.55 +
30.56 +
30.57 +/*
30.58 +** {======================================================
30.59 +** FILE Operations
30.60 +** =======================================================
30.61 +*/
30.62 +
30.63 +
30.64 +#if !USE_POPEN
30.65 +#define pclose(f) (-1)
30.66 +#endif
30.67 +
30.68 +
30.69 +#define FILEHANDLE "FILE*"
30.70 +
30.71 +#define IO_INPUT "_input"
30.72 +#define IO_OUTPUT "_output"
30.73 +
30.74 +
30.75 +static int pushresult (lua_State *L, int i, const char *filename) {
30.76 + if (i) {
30.77 + lua_pushboolean(L, 1);
30.78 + return 1;
30.79 + }
30.80 + else {
30.81 + lua_pushnil(L);
30.82 + if (filename)
30.83 + lua_pushfstring(L, "%s: %s", filename, strerror(errno));
30.84 + else
30.85 + lua_pushfstring(L, "%s", strerror(errno));
30.86 + lua_pushnumber(L, errno);
30.87 + return 3;
30.88 + }
30.89 +}
30.90 +
30.91 +
30.92 +static FILE **topfile (lua_State *L, int findex) {
30.93 + FILE **f = (FILE **)luaL_checkudata(L, findex, FILEHANDLE);
30.94 + if (f == NULL) luaL_argerror(L, findex, "bad file");
30.95 + return f;
30.96 +}
30.97 +
30.98 +
30.99 +static int io_type (lua_State *L) {
30.100 + FILE **f = (FILE **)luaL_checkudata(L, 1, FILEHANDLE);
30.101 + if (f == NULL) lua_pushnil(L);
30.102 + else if (*f == NULL)
30.103 + lua_pushliteral(L, "closed file");
30.104 + else
30.105 + lua_pushliteral(L, "file");
30.106 + return 1;
30.107 +}
30.108 +
30.109 +
30.110 +static FILE *tofile (lua_State *L, int findex) {
30.111 + FILE **f = topfile(L, findex);
30.112 + if (*f == NULL)
30.113 + luaL_error(L, "attempt to use a closed file");
30.114 + return *f;
30.115 +}
30.116 +
30.117 +
30.118 +
30.119 +/*
30.120 +** When creating file handles, always creates a `closed' file handle
30.121 +** before opening the actual file; so, if there is a memory error, the
30.122 +** file is not left opened.
30.123 +*/
30.124 +static FILE **newfile (lua_State *L) {
30.125 + FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
30.126 + *pf = NULL; /* file handle is currently `closed' */
30.127 + luaL_getmetatable(L, FILEHANDLE);
30.128 + lua_setmetatable(L, -2);
30.129 + return pf;
30.130 +}
30.131 +
30.132 +
30.133 +/*
30.134 +** assumes that top of the stack is the `io' library, and next is
30.135 +** the `io' metatable
30.136 +*/
30.137 +static void registerfile (lua_State *L, FILE *f, const char *name,
30.138 + const char *impname) {
30.139 + lua_pushstring(L, name);
30.140 + *newfile(L) = f;
30.141 + if (impname) {
30.142 + lua_pushstring(L, impname);
30.143 + lua_pushvalue(L, -2);
30.144 + lua_settable(L, -6); /* metatable[impname] = file */
30.145 + }
30.146 + lua_settable(L, -3); /* io[name] = file */
30.147 +}
30.148 +
30.149 +
30.150 +static int aux_close (lua_State *L) {
30.151 + FILE *f = tofile(L, 1);
30.152 + if (f == stdin || f == stdout || f == stderr)
30.153 + return 0; /* file cannot be closed */
30.154 + else {
30.155 + int ok = (pclose(f) != -1) || (fclose(f) == 0);
30.156 + if (ok)
30.157 + *(FILE **)lua_touserdata(L, 1) = NULL; /* mark file as closed */
30.158 + return ok;
30.159 + }
30.160 +}
30.161 +
30.162 +
30.163 +static int io_close (lua_State *L) {
30.164 + if (lua_isnone(L, 1) && lua_type(L, lua_upvalueindex(1)) == LUA_TTABLE) {
30.165 + lua_pushstring(L, IO_OUTPUT);
30.166 + lua_rawget(L, lua_upvalueindex(1));
30.167 + }
30.168 + return pushresult(L, aux_close(L), NULL);
30.169 +}
30.170 +
30.171 +
30.172 +static int io_gc (lua_State *L) {
30.173 + FILE **f = topfile(L, 1);
30.174 + if (*f != NULL) /* ignore closed files */
30.175 + aux_close(L);
30.176 + return 0;
30.177 +}
30.178 +
30.179 +
30.180 +static int io_tostring (lua_State *L) {
30.181 + char buff[128];
30.182 + FILE **f = topfile(L, 1);
30.183 + if (*f == NULL)
30.184 + strcpy(buff, "closed");
30.185 + else
30.186 + sprintf(buff, "%p", lua_touserdata(L, 1));
30.187 + lua_pushfstring(L, "file (%s)", buff);
30.188 + return 1;
30.189 +}
30.190 +
30.191 +
30.192 +static int io_open (lua_State *L) {
30.193 + const char *filename = luaL_checkstring(L, 1);
30.194 + const char *mode = luaL_optstring(L, 2, "r");
30.195 + FILE **pf = newfile(L);
30.196 + *pf = fopen(filename, mode);
30.197 + return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
30.198 +}
30.199 +
30.200 +
30.201 +static int io_popen (lua_State *L) {
30.202 +#if !USE_POPEN
30.203 + luaL_error(L, "`popen' not supported");
30.204 + return 0;
30.205 +#else
30.206 + const char *filename = luaL_checkstring(L, 1);
30.207 + const char *mode = luaL_optstring(L, 2, "r");
30.208 + FILE **pf = newfile(L);
30.209 + *pf = popen(filename, mode);
30.210 + return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
30.211 +#endif
30.212 +}
30.213 +
30.214 +
30.215 +static int io_tmpfile (lua_State *L) {
30.216 + FILE **pf = newfile(L);
30.217 + *pf = tmpfile();
30.218 + return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
30.219 +}
30.220 +
30.221 +
30.222 +static FILE *getiofile (lua_State *L, const char *name) {
30.223 + lua_pushstring(L, name);
30.224 + lua_rawget(L, lua_upvalueindex(1));
30.225 + return tofile(L, -1);
30.226 +}
30.227 +
30.228 +
30.229 +static int g_iofile (lua_State *L, const char *name, const char *mode) {
30.230 + if (!lua_isnoneornil(L, 1)) {
30.231 + const char *filename = lua_tostring(L, 1);
30.232 + lua_pushstring(L, name);
30.233 + if (filename) {
30.234 + FILE **pf = newfile(L);
30.235 + *pf = fopen(filename, mode);
30.236 + if (*pf == NULL) {
30.237 + lua_pushfstring(L, "%s: %s", filename, strerror(errno));
30.238 + luaL_argerror(L, 1, lua_tostring(L, -1));
30.239 + }
30.240 + }
30.241 + else {
30.242 + tofile(L, 1); /* check that it's a valid file handle */
30.243 + lua_pushvalue(L, 1);
30.244 + }
30.245 + lua_rawset(L, lua_upvalueindex(1));
30.246 + }
30.247 + /* return current value */
30.248 + lua_pushstring(L, name);
30.249 + lua_rawget(L, lua_upvalueindex(1));
30.250 + return 1;
30.251 +}
30.252 +
30.253 +
30.254 +static int io_input (lua_State *L) {
30.255 + return g_iofile(L, IO_INPUT, "r");
30.256 +}
30.257 +
30.258 +
30.259 +static int io_output (lua_State *L) {
30.260 + return g_iofile(L, IO_OUTPUT, "w");
30.261 +}
30.262 +
30.263 +
30.264 +static int io_readline (lua_State *L);
30.265 +
30.266 +
30.267 +static void aux_lines (lua_State *L, int idx, int close) {
30.268 + lua_pushliteral(L, FILEHANDLE);
30.269 + lua_rawget(L, LUA_REGISTRYINDEX);
30.270 + lua_pushvalue(L, idx);
30.271 + lua_pushboolean(L, close); /* close/not close file when finished */
30.272 + lua_pushcclosure(L, io_readline, 3);
30.273 +}
30.274 +
30.275 +
30.276 +static int f_lines (lua_State *L) {
30.277 + tofile(L, 1); /* check that it's a valid file handle */
30.278 + aux_lines(L, 1, 0);
30.279 + return 1;
30.280 +}
30.281 +
30.282 +
30.283 +static int io_lines (lua_State *L) {
30.284 + if (lua_isnoneornil(L, 1)) { /* no arguments? */
30.285 + lua_pushstring(L, IO_INPUT);
30.286 + lua_rawget(L, lua_upvalueindex(1)); /* will iterate over default input */
30.287 + return f_lines(L);
30.288 + }
30.289 + else {
30.290 + const char *filename = luaL_checkstring(L, 1);
30.291 + FILE **pf = newfile(L);
30.292 + *pf = fopen(filename, "r");
30.293 + luaL_argcheck(L, *pf, 1, strerror(errno));
30.294 + aux_lines(L, lua_gettop(L), 1);
30.295 + return 1;
30.296 + }
30.297 +}
30.298 +
30.299 +
30.300 +/*
30.301 +** {======================================================
30.302 +** READ
30.303 +** =======================================================
30.304 +*/
30.305 +
30.306 +
30.307 +static int read_number (lua_State *L, FILE *f) {
30.308 + lua_Number d;
30.309 + if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
30.310 + lua_pushnumber(L, d);
30.311 + return 1;
30.312 + }
30.313 + else return 0; /* read fails */
30.314 +}
30.315 +
30.316 +
30.317 +static int test_eof (lua_State *L, FILE *f) {
30.318 + int c = getc(f);
30.319 + ungetc(c, f);
30.320 + lua_pushlstring(L, NULL, 0);
30.321 + return (c != EOF);
30.322 +}
30.323 +
30.324 +
30.325 +static int read_line (lua_State *L, FILE *f) {
30.326 + luaL_Buffer b;
30.327 + luaL_buffinit(L, &b);
30.328 + for (;;) {
30.329 + size_t l;
30.330 + char *p = luaL_prepbuffer(&b);
30.331 + if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
30.332 + luaL_pushresult(&b); /* close buffer */
30.333 + return (lua_strlen(L, -1) > 0); /* check whether read something */
30.334 + }
30.335 + l = strlen(p);
30.336 + if (p[l-1] != '\n')
30.337 + luaL_addsize(&b, l);
30.338 + else {
30.339 + luaL_addsize(&b, l - 1); /* do not include `eol' */
30.340 + luaL_pushresult(&b); /* close buffer */
30.341 + return 1; /* read at least an `eol' */
30.342 + }
30.343 + }
30.344 +}
30.345 +
30.346 +
30.347 +static int read_chars (lua_State *L, FILE *f, size_t n) {
30.348 + size_t rlen; /* how much to read */
30.349 + size_t nr; /* number of chars actually read */
30.350 + luaL_Buffer b;
30.351 + luaL_buffinit(L, &b);
30.352 + rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
30.353 + do {
30.354 + char *p = luaL_prepbuffer(&b);
30.355 + if (rlen > n) rlen = n; /* cannot read more than asked */
30.356 + nr = fread(p, sizeof(char), rlen, f);
30.357 + luaL_addsize(&b, nr);
30.358 + n -= nr; /* still have to read `n' chars */
30.359 + } while (n > 0 && nr == rlen); /* until end of count or eof */
30.360 + luaL_pushresult(&b); /* close buffer */
30.361 + return (n == 0 || lua_strle