summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--luatest.sln20
-rw-r--r--luatest/luatest.vcxproj2
-rw-r--r--luatest/luatest.vcxproj.filters3
-rw-r--r--math/math.vcxproj130
-rw-r--r--math/math.vcxproj.filters9
-rw-r--r--math/math.vcxproj.user4
-rw-r--r--src/00-misc/math_return_table/init.lua0
-rw-r--r--src/00-misc/math_return_table/matrix.lua0
-rw-r--r--src/00-misc/math_return_table/misc/guid.lua0
-rw-r--r--src/00-misc/math_return_table/misc/init.lua0
-rw-r--r--src/00-misc/math_return_table/misc/lerp.lua0
-rw-r--r--src/00-misc/math_return_table/misc/rand.lua0
-rw-r--r--src/00-misc/math_return_table/misc/uuid.lua0
-rw-r--r--src/00-misc/math_return_table/quaternion.lua0
-rw-r--r--src/00-misc/math_return_table/vector.lua0
-rw-r--r--src/00-misc/misc.lua8
-rw-r--r--src/00-misc/vec.lua3
-rw-r--r--src/lua51/lapi.c9
-rw-r--r--src/lua51/lauxlib.c2
-rw-r--r--src/lua51/lauxlib.h2
-rw-r--r--src/lua51/lcode.c1
-rw-r--r--src/lua51/ldo.c39
-rw-r--r--src/lua51/lgc.c68
-rw-r--r--src/lua51/lgc.h28
-rw-r--r--src/lua51/llex.c4
-rw-r--r--src/lua51/llex.h1
-rw-r--r--src/lua51/lobject.h62
-rw-r--r--src/lua51/lopcodes.c7
-rw-r--r--src/lua51/lopcodes.h33
-rw-r--r--src/lua51/lparser.c5
-rw-r--r--src/lua51/lstate.c1
-rw-r--r--src/lua51/lstate.h52
-rw-r--r--src/lua51/lstring.c39
-rw-r--r--src/lua51/ltable.c36
-rw-r--r--src/lua51/lua.h1
-rw-r--r--src/lua51/lvm.c24
-rw-r--r--src/math/math.cpp37
-rw-r--r--src/math/math.h9
38 files changed, 537 insertions, 102 deletions
diff --git a/luatest.sln b/luatest.sln
index 9c39c00..38e85e0 100644
--- a/luatest.sln
+++ b/luatest.sln
@@ -5,6 +5,10 @@ VisualStudioVersion = 15.0.28307.168
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "luatest", "luatest\luatest.vcxproj", "{A034D469-27DF-4900-B3E8-C430D3A90D71}"
EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "math", "math\math.vcxproj", "{602A64C7-2EB4-42D7-9AE0-CCA3E69E16A0}"
+EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lua", "lua\lua.vcxproj", "{71F99467-FABE-417A-BE91-8D4465FFA775}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
@@ -21,6 +25,22 @@ Global
{A034D469-27DF-4900-B3E8-C430D3A90D71}.Release|x64.Build.0 = Release|x64
{A034D469-27DF-4900-B3E8-C430D3A90D71}.Release|x86.ActiveCfg = Release|Win32
{A034D469-27DF-4900-B3E8-C430D3A90D71}.Release|x86.Build.0 = Release|Win32
+ {602A64C7-2EB4-42D7-9AE0-CCA3E69E16A0}.Debug|x64.ActiveCfg = Debug|x64
+ {602A64C7-2EB4-42D7-9AE0-CCA3E69E16A0}.Debug|x64.Build.0 = Debug|x64
+ {602A64C7-2EB4-42D7-9AE0-CCA3E69E16A0}.Debug|x86.ActiveCfg = Debug|Win32
+ {602A64C7-2EB4-42D7-9AE0-CCA3E69E16A0}.Debug|x86.Build.0 = Debug|Win32
+ {602A64C7-2EB4-42D7-9AE0-CCA3E69E16A0}.Release|x64.ActiveCfg = Release|x64
+ {602A64C7-2EB4-42D7-9AE0-CCA3E69E16A0}.Release|x64.Build.0 = Release|x64
+ {602A64C7-2EB4-42D7-9AE0-CCA3E69E16A0}.Release|x86.ActiveCfg = Release|Win32
+ {602A64C7-2EB4-42D7-9AE0-CCA3E69E16A0}.Release|x86.Build.0 = Release|Win32
+ {71F99467-FABE-417A-BE91-8D4465FFA775}.Debug|x64.ActiveCfg = Debug|x64
+ {71F99467-FABE-417A-BE91-8D4465FFA775}.Debug|x64.Build.0 = Debug|x64
+ {71F99467-FABE-417A-BE91-8D4465FFA775}.Debug|x86.ActiveCfg = Debug|Win32
+ {71F99467-FABE-417A-BE91-8D4465FFA775}.Debug|x86.Build.0 = Debug|Win32
+ {71F99467-FABE-417A-BE91-8D4465FFA775}.Release|x64.ActiveCfg = Release|x64
+ {71F99467-FABE-417A-BE91-8D4465FFA775}.Release|x64.Build.0 = Release|x64
+ {71F99467-FABE-417A-BE91-8D4465FFA775}.Release|x86.ActiveCfg = Release|Win32
+ {71F99467-FABE-417A-BE91-8D4465FFA775}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/luatest/luatest.vcxproj b/luatest/luatest.vcxproj
index 9bb3c86..68136a4 100644
--- a/luatest/luatest.vcxproj
+++ b/luatest/luatest.vcxproj
@@ -22,7 +22,7 @@
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{A034D469-27DF-4900-B3E8-C430D3A90D71}</ProjectGuid>
<RootNamespace>luatest</RootNamespace>
- <WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
diff --git a/luatest/luatest.vcxproj.filters b/luatest/luatest.vcxproj.filters
index 1dcd0aa..c3f8d56 100644
--- a/luatest/luatest.vcxproj.filters
+++ b/luatest/luatest.vcxproj.filters
@@ -19,6 +19,9 @@
<Filter Include="00-misc">
<UniqueIdentifier>{f3e095fb-8219-46c2-9d97-20539e925d7c}</UniqueIdentifier>
</Filter>
+ <Filter Include="05-modules">
+ <UniqueIdentifier>{47f87cf7-c82c-4329-ac70-1cc49d9f3910}</UniqueIdentifier>
+ </Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\lua51\lapi.c">
diff --git a/math/math.vcxproj b/math/math.vcxproj
new file mode 100644
index 0000000..44b07bf
--- /dev/null
+++ b/math/math.vcxproj
@@ -0,0 +1,130 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup Label="ProjectConfigurations">
+ <ProjectConfiguration Include="Debug|Win32">
+ <Configuration>Debug</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|Win32">
+ <Configuration>Release</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Debug|x64">
+ <Configuration>Debug</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|x64">
+ <Configuration>Release</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ </ItemGroup>
+ <PropertyGroup Label="Globals">
+ <VCProjectVersion>15.0</VCProjectVersion>
+ <ProjectGuid>{602A64C7-2EB4-42D7-9AE0-CCA3E69E16A0}</ProjectGuid>
+ <RootNamespace>math</RootNamespace>
+ <WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+ <ConfigurationType>DynamicLibrary</ConfigurationType>
+ <UseDebugLibraries>true</UseDebugLibraries>
+ <PlatformToolset>v141</PlatformToolset>
+ <CharacterSet>MultiByte</CharacterSet>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+ <ConfigurationType>DynamicLibrary</ConfigurationType>
+ <UseDebugLibraries>false</UseDebugLibraries>
+ <PlatformToolset>v141</PlatformToolset>
+ <WholeProgramOptimization>true</WholeProgramOptimization>
+ <CharacterSet>MultiByte</CharacterSet>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>true</UseDebugLibraries>
+ <PlatformToolset>v141</PlatformToolset>
+ <CharacterSet>MultiByte</CharacterSet>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>false</UseDebugLibraries>
+ <PlatformToolset>v141</PlatformToolset>
+ <WholeProgramOptimization>true</WholeProgramOptimization>
+ <CharacterSet>MultiByte</CharacterSet>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+ <ImportGroup Label="ExtensionSettings">
+ </ImportGroup>
+ <ImportGroup Label="Shared">
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <PropertyGroup Label="UserMacros" />
+ <PropertyGroup />
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <ClCompile>
+ <WarningLevel>Level3</WarningLevel>
+ <Optimization>Disabled</Optimization>
+ <SDLCheck>true</SDLCheck>
+ <ConformanceMode>true</ConformanceMode>
+ </ClCompile>
+ <Link>
+ <AdditionalLibraryDirectories>$(OutDir)</AdditionalLibraryDirectories>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+ <ClCompile>
+ <WarningLevel>Level3</WarningLevel>
+ <Optimization>Disabled</Optimization>
+ <SDLCheck>true</SDLCheck>
+ <ConformanceMode>true</ConformanceMode>
+ </ClCompile>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <ClCompile>
+ <WarningLevel>Level3</WarningLevel>
+ <Optimization>MaxSpeed</Optimization>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <IntrinsicFunctions>true</IntrinsicFunctions>
+ <SDLCheck>true</SDLCheck>
+ <ConformanceMode>true</ConformanceMode>
+ </ClCompile>
+ <Link>
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
+ <OptimizeReferences>true</OptimizeReferences>
+ <AdditionalLibraryDirectories>$(OutDir)</AdditionalLibraryDirectories>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+ <ClCompile>
+ <WarningLevel>Level3</WarningLevel>
+ <Optimization>MaxSpeed</Optimization>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <IntrinsicFunctions>true</IntrinsicFunctions>
+ <SDLCheck>true</SDLCheck>
+ <ConformanceMode>true</ConformanceMode>
+ </ClCompile>
+ <Link>
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
+ <OptimizeReferences>true</OptimizeReferences>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemGroup>
+ <ClCompile Include="..\src\math\math.cpp" />
+ </ItemGroup>
+ <ItemGroup>
+ <ClInclude Include="..\src\math\math.h" />
+ </ItemGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+ <ImportGroup Label="ExtensionTargets">
+ </ImportGroup>
+</Project> \ No newline at end of file
diff --git a/math/math.vcxproj.filters b/math/math.vcxproj.filters
new file mode 100644
index 0000000..67045a2
--- /dev/null
+++ b/math/math.vcxproj.filters
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup>
+ <ClCompile Include="..\src\math\math.cpp" />
+ </ItemGroup>
+ <ItemGroup>
+ <ClInclude Include="..\src\math\math.h" />
+ </ItemGroup>
+</Project> \ No newline at end of file
diff --git a/math/math.vcxproj.user b/math/math.vcxproj.user
new file mode 100644
index 0000000..be25078
--- /dev/null
+++ b/math/math.vcxproj.user
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup />
+</Project> \ No newline at end of file
diff --git a/src/00-misc/math_return_table/init.lua b/src/00-misc/math_return_table/init.lua
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/00-misc/math_return_table/init.lua
diff --git a/src/00-misc/math_return_table/matrix.lua b/src/00-misc/math_return_table/matrix.lua
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/00-misc/math_return_table/matrix.lua
diff --git a/src/00-misc/math_return_table/misc/guid.lua b/src/00-misc/math_return_table/misc/guid.lua
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/00-misc/math_return_table/misc/guid.lua
diff --git a/src/00-misc/math_return_table/misc/init.lua b/src/00-misc/math_return_table/misc/init.lua
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/00-misc/math_return_table/misc/init.lua
diff --git a/src/00-misc/math_return_table/misc/lerp.lua b/src/00-misc/math_return_table/misc/lerp.lua
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/00-misc/math_return_table/misc/lerp.lua
diff --git a/src/00-misc/math_return_table/misc/rand.lua b/src/00-misc/math_return_table/misc/rand.lua
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/00-misc/math_return_table/misc/rand.lua
diff --git a/src/00-misc/math_return_table/misc/uuid.lua b/src/00-misc/math_return_table/misc/uuid.lua
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/00-misc/math_return_table/misc/uuid.lua
diff --git a/src/00-misc/math_return_table/quaternion.lua b/src/00-misc/math_return_table/quaternion.lua
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/00-misc/math_return_table/quaternion.lua
diff --git a/src/00-misc/math_return_table/vector.lua b/src/00-misc/math_return_table/vector.lua
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/00-misc/math_return_table/vector.lua
diff --git a/src/00-misc/misc.lua b/src/00-misc/misc.lua
index e73d2e3..8894dbb 100644
--- a/src/00-misc/misc.lua
+++ b/src/00-misc/misc.lua
@@ -1,3 +1,5 @@
-package.preload["00-misc.vec"] = loadfile("00-misc/vec.lua")
-
-require("00-misc.vec")
+a = "a"
+b = "b"
+c = "c"
+c = nil
+collectgarbage()
diff --git a/src/00-misc/vec.lua b/src/00-misc/vec.lua
index 2862a16..9d81f40 100644
--- a/src/00-misc/vec.lua
+++ b/src/00-misc/vec.lua
@@ -1 +1,2 @@
-print("vec.lua") \ No newline at end of file
+print("vec.lua")
+
diff --git a/src/lua51/lapi.c b/src/lua51/lapi.c
index 5d5145d..0678908 100644
--- a/src/lua51/lapi.c
+++ b/src/lua51/lapi.c
@@ -801,7 +801,7 @@ static void f_call (lua_State *L, void *ud) {
}
-
+//c
LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
struct CallS c;
int status;
@@ -816,7 +816,7 @@ LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
api_checkvalidindex(L, o);
func = savestack(L, o);
}
- c.func = L->top - (nargs+1); /* function to be called */
+ c.func = L->top - (nargs+1); /* function to be called */ //µ÷Óõĺ¯ÊýµÄÖ¸Õë,ÊÇf_parserº¯ÊýµÄÊä³ö£¬·ÅÔÚÕ»¶¥µÄÄǸöÖ¸Õë
c.nresults = nresults;
status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
adjustresults(L, nresults);
@@ -859,6 +859,8 @@ LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
}
+//c ¼ÓÔØ´úÂ룬²¢±àÒëÉú³Éº¯ÊýÔ­ÐÍ µ÷ÓÃÁËf_parser
+//c loadÍêÖ®ºó»áÉú³ÉÒ»¸öclosure£¨°üº¬proto)ÔÚÕ»¶¥
LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
const char *chunkname) {
ZIO z;
@@ -866,7 +868,8 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
lua_lock(L);
if (!chunkname) chunkname = "?";
luaZ_init(L, &z, reader, data);
- status = luaD_protectedparser(L, &z, chunkname);
+ //c ±£»¤Ä£Ê½Ï±àÒëÔ´´úÂë
+ status = luaD_protectedparser(L, &z, chunkname); // ±àÒë
lua_unlock(L);
return status;
}
diff --git a/src/lua51/lauxlib.c b/src/lua51/lauxlib.c
index 10f14e2..f1bdd39 100644
--- a/src/lua51/lauxlib.c
+++ b/src/lua51/lauxlib.c
@@ -548,7 +548,7 @@ static int errfile (lua_State *L, const char *what, int fnameindex) {
return LUA_ERRFILE;
}
-
+//c ½øÐдʷ¨·ÖÎöºÍÓï·¨·ÖÎö
LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
LoadF lf;
int status, readstatus;
diff --git a/src/lua51/lauxlib.h b/src/lua51/lauxlib.h
index 3425823..6ac7967 100644
--- a/src/lua51/lauxlib.h
+++ b/src/lua51/lauxlib.h
@@ -119,7 +119,7 @@ LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx,
#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
/*
-** {======================================================
+** ======================================================
** Generic Buffer manipulation
** =======================================================
*/
diff --git a/src/lua51/lcode.c b/src/lua51/lcode.c
index 679cb9c..e1da87b 100644
--- a/src/lua51/lcode.c
+++ b/src/lua51/lcode.c
@@ -786,6 +786,7 @@ void luaK_fixline (FuncState *fs, int line) {
}
+//c Ö¸ÁîÉú³É
static int luaK_code (FuncState *fs, Instruction i, int line) {
Proto *f = fs->f;
dischargejpc(fs); /* `pc' will change */
diff --git a/src/lua51/ldo.c b/src/lua51/ldo.c
index d1bf786..eac8d03 100644
--- a/src/lua51/ldo.c
+++ b/src/lua51/ldo.c
@@ -149,6 +149,7 @@ void luaD_reallocstack (lua_State *L, int newsize) {
}
+//c
void luaD_reallocCI (lua_State *L, int newsize) {
CallInfo *oldci = L->base_ci;
luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
@@ -165,7 +166,8 @@ void luaD_growstack (lua_State *L, int n) {
luaD_reallocstack(L, L->stacksize + n);
}
-
+//c µ±·ÖÅäµÄcallinfoÊý×é²»¹»´óµÄ£¬ÒÔ2µÄÖ¸ÊýÀ©Õ¹Ëü
+//c callinfoµÄ´óС²»»á³¬¹ýLUAI_MAXCALLS
static CallInfo *growCI (lua_State *L) {
if (L->size_ci > LUAI_MAXCALLS) /* overflow while handling overflow? */
luaD_throw(L, LUA_ERRERR);
@@ -256,12 +258,14 @@ static StkId tryfuncTM (lua_State *L, StkId func) {
}
-
+//c Èç¹û²»¹»´óÁË£¬À©ÈÝ£»È»ºó·µ»Ø++L->ci£¬¼´ÏÂÒ»¸öci
#define inc_ci(L) \
((L->ci == L->end_ci) ? growCI(L) : \
(condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci))
+//c ×öº¯Êýµ÷ÓÃǰµÄ×¼±¸
+//c ½«º¯ÊýÔ­Ð͵ÄÖ¸Áîcode·ÅÔÚsavedpcÖУ¬×¼±¸¾ÍÐ÷
int luaD_precall (lua_State *L, StkId func, int nresults) {
LClosure *cl;
ptrdiff_t funcr;
@@ -269,7 +273,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
func = tryfuncTM(L, func); /* check the `function' tag method */
funcr = savestack(L, func);
cl = &clvalue(func)->l;
- L->ci->savedpc = L->savedpc;
+ L->ci->savedpc = L->savedpc;//c ÏȰѵ±Ç°ÐéÄâ»úÖ´ÐеÄλÖñ£´æÏÂÀ´£¬Áô¸øºóÃæµ÷ÓÃÍ꺯Êýºóluad_poscall»Ö¸´µ½Õâ¸öλÖÃ
if (!cl->isC) { /* Lua function? prepare its call */
CallInfo *ci;
StkId st, base;
@@ -294,6 +298,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
L->savedpc = p->code; /* starting point */
ci->tailcalls = 0;
ci->nresults = nresults;
+ //c ½«¶àÓàµÄ²ÎÊý¸³ÖµÎªnil£¬±ÈÈ纯Êý¶¨ÒåÐèÒª3¸ö²ÎÊý£¬µ«ÊÇÖ»´©ÁË1¸ö£¬ÄÇôÁíÍâÁ½¸ö¸³ÖµÎªnil
for (st = L->top; st < ci->top; st++)
setnilvalue(st);
L->top = ci->top;
@@ -305,14 +310,20 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
return PCRLUA;
}
else { /* if is a C function, call it */
+ //c ÕâÀïÄÜ·¢ÏÖÁ½¸öÏÞÖÆ£¬Ò»¸öÊǺ¯Êýµ÷ÓõÄǶÌ×ÏÞÖÆLUAI_MAXCALL£¬Ò»¸öÊǺ¯Êýµ÷ÓÃÐèÒªµÄÊý¾ÝÕ»´óС²»Äܳ¬¹ý
+ //c lua_StateÊý¾ÝÕ»µÄ´óСBASIC_STACK_SIZE + EXTRA_STACK£¨ÔÚstack_initº¯ÊýÖУ©
+
CallInfo *ci;
int n;
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
+ //c ÕÒÒ»¸öеĵ÷ÓÃCallInfo¶ÔÏó£¬Èç¹ûlua_stateµÄcallinfoÊý×é²»¹»´óÁË£¬À©Õ¹Ëü£¬µ«²»Äܳ¬¹ýLUAI_MAXCALLS
+ //c Äõ½ÏÂÒ»¸öcallinfo¶ÔÏó
ci = inc_ci(L); /* now `enter' new function */
- ci->func = restorestack(L, funcr);
- L->base = ci->base = ci->func + 1;
- ci->top = L->top + LUA_MINSTACK;
- lua_assert(ci->top <= L->stack_last);
+ ci->func = restorestack(L, funcr);//c callinfoµÄº¯ÊýÔ­ÐÍ
+ //c ´ÓÕâÀïÄÜ¿´³ö£¬lua_stateµÄÊý¾ÝÕ»µÄbaseÊǸú×ŵ÷Óñ䶯µÄ
+ L->base = ci->base = ci->func + 1;//c ÉèÖõ±Ç°µ÷ÓõÄÊý¾ÝÕ»
+ ci->top = L->top + LUA_MINSTACK;//c ÉèÖõ±Ç°µ÷ÓõÄ×î´óÊý¾ÝÕ»ÈÝÁ¿
+ lua_assert(ci->top <= L->stack_last); //c µ÷ÓõÄÕ»¶¥²»Äܳ¬¹ýluaÊý¾ÝÕ»µÄÉÏÏÞ
ci->nresults = nresults;
if (L->hookmask & LUA_MASKCALL)
luaD_callhook(L, LUA_HOOKCALL, -1);
@@ -340,6 +351,7 @@ static StkId callrethooks (lua_State *L, StkId firstResult) {
}
+//c º¯ÊýÖ´ÐÐÍê±Ïºó£¬½«lua_state»Ö¸´µ½ÉÏÒ»´Îº¯Êýµ÷ÓõÄ״̬
int luaD_poscall (lua_State *L, StkId firstResult) {
StkId res;
int wanted, i;
@@ -350,7 +362,7 @@ int luaD_poscall (lua_State *L, StkId firstResult) {
res = ci->func; /* res == final position of 1st result */
wanted = ci->nresults;
L->base = (ci - 1)->base; /* restore base */
- L->savedpc = (ci - 1)->savedpc; /* restore savedpc */
+ L->savedpc = (ci - 1)->savedpc; /* restore savedpc */ //c »Ö¸´µ½µ÷ÓÃ֮ǰµÄ״̬£¬ºÜºÃÀí½â£¬ÔÚ¸¸º¯Êý´¦Õâ¸öº¯ÊýÖ´ÐÐÍêÁË£¬¿ÉÒÔ½Ó×Å×ßÁË
/* move results to correct place */
for (i = wanted; i != 0 && firstResult < L->top; i--)
setobjs2s(L, res++, firstResult++);
@@ -361,6 +373,7 @@ int luaD_poscall (lua_State *L, StkId firstResult) {
}
+//c Ö´ÐдúÂëµÄÈë¿Ú£¬²»ÏÞÓÚº¯Êý£¨ÒòΪlua´úÂë¶Î¾ÍÊÇ·âװΪÁ˺¯Êý£©
/*
** Call a function (C or Lua). The function to be called is at *func.
** The arguments are on the stack, right after the function.
@@ -374,6 +387,8 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
}
+ //c luad_precall½«º¯ÊýÖ¸Áî·ÅÔÚlua_stateµÄsavedpc×ֶΣ¬×¼±¸¾ÍÐ÷
+ //c luaV_executeÖ´Ðк¯ÊýÔ­Ð͵ÄÖ¸ÁÊÇÖ´ÐÐÖ¸ÁîµÄÈë¿Ú
if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */
luaV_execute(L, 1); /* call it */
L->nCcalls--;
@@ -453,6 +468,7 @@ LUA_API int lua_yield (lua_State *L, int nresults) {
}
+//c µ÷Óú¯Êý
int luaD_pcall (lua_State *L, Pfunc func, void *u,
ptrdiff_t old_top, ptrdiff_t ef) {
int status;
@@ -488,6 +504,8 @@ struct SParser { /* data to `f_parser' */
const char *name;
};
+//c ´Ê·¨·ÖÎöºÍÓï·¨·ÖÎö£¬µ÷ÓÃluaY_parser
+//c Ö´ÐÐÍêºó½«closureÁôÔÚÕ»¶¥
static void f_parser (lua_State *L, void *ud) {
int i;
Proto *tf;
@@ -499,18 +517,21 @@ static void f_parser (lua_State *L, void *ud) {
&p->buff, p->name);
cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));
cl->l.p = tf;
+ //c ³õʼ»¯upvalue
for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */
cl->l.upvals[i] = luaF_newupval(L);
- setclvalue(L, L->top, cl);
+ setclvalue(L, L->top, cl);//°Ñ±Õ°ü·ÅÔÚÕ»¶¥
incr_top(L);
}
+//c ±£»¤Ä£Ê½Ï±àÒë´úÂë
int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) {
struct SParser p;
int status;
p.z = z; p.name = name;
luaZ_initbuffer(L, &p.buff);
+ //cÔÚ±£»¤Ä£Ê½ÏÂÖ´ÐÐ f_parser º¯Êý
status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
luaZ_freebuffer(L, &p.buff);
return status;
diff --git a/src/lua51/lgc.c b/src/lua51/lgc.c
index e909c79..f4dbdfb 100644
--- a/src/lua51/lgc.c
+++ b/src/lua51/lgc.c
@@ -48,7 +48,7 @@
#define VALUEWEAK bitmask(VALUEWEAKBIT)
-
+// ±ê¼Ç¶ÔÏó£¬ÓеãÀàËÆÒ»¸ö°²È«ÉùÃ÷£¬markÁ˵ĶÔÏ󲻻ᱻ»ØÊÕ£¬»á±»±ê¼ÇΪ»ÒÉ«£¨´ó¶àÊýÇé¿öÏ£©»òºÚÉ«
#define markvalue(g,o) { checkconsistency(o); \
if (iscollectable(o) && iswhite(gcvalue(o))) reallymarkobject(g,gcvalue(o)); }
@@ -66,16 +66,24 @@ static void removeentry (Node *n) {
}
+//c ±ê¼Çobject£¨²»±ê¼ÇÕâ¸öobjectÒýÓõÄobject£¬udata³ýÍ⣬ÐèÒª±ê¼ÇudataµÄmtºÍenv£©
+//c ÖÁÓÚ±ê¼ÇobjectÒýÓõÄobjectÁôÔÚɨÃè½×¶Î
static void reallymarkobject (global_State *g, GCObject *o) {
lua_assert(iswhite(o) && !isdead(g, o));
+ //¸ÄΪ»ÒÉ«
white2gray(o);
+ //¶ÔÓڴ󲿷ÖÀàÐÍ£¬¶¼ÊǰÑgcobject¼Óµ½grayÁ´±í
switch (o->gch.tt) {
case LUA_TSTRING: {
+ // ×Ö·û´®²»»áÒýÓÃÆäËûÊý¾Ý£¬ËùÒÔÂÔ¹ý£¬²»Óüӵ½gray list
return;
}
case LUA_TUSERDATA: {
+ // udata±¾ÉíÒ²²»»áÒýÓÃÆäËû¶ÔÏó£¬ËùÒÔ²»ÐèҪɨÃ裬ֱ½Ó±ê¼ÇΪºÚÉ«
Table *mt = gco2u(o)->metatable;
+ // Ö±½Ó±ê¼ÇΪºÚÉ«
gray2black(o); /* udata are never gray */
+ // ±ê¼ÇÒ»ÏÂËüµÄmtºÍenv±í
if (mt) markobject(g, mt);
markobject(g, gco2u(o)->env);
return;
@@ -83,6 +91,8 @@ static void reallymarkobject (global_State *g, GCObject *o) {
case LUA_TUPVAL: {
UpVal *uv = gco2uv(o);
markvalue(g, uv->v);
+ // µ±Õâ¸öupvalueÊÇclosed״̬£¬±íʾÕâ¸öuvÒѾ­Ã»ÓÐÓëÆäËûÊý¾ÝµÄÒýÓùØÏµ
+ // Ö±½Ó±ê¼ÇºÚÉ«
if (uv->v == &uv->u.value) /* closed? */
gray2black(o); /* open upvalues are never black */
return;
@@ -154,7 +164,8 @@ size_t luaC_separateudata (lua_State *L, int all) {
return deadmem;
}
-
+// ±éÀútable
+// Èç¹ûÊÇÈõ±í£¬·µ»Ø1
static int traversetable (global_State *g, Table *h) {
int i;
int weakkey = 0;
@@ -170,16 +181,23 @@ static int traversetable (global_State *g, Table *h) {
h->marked &= ~(KEYWEAK | VALUEWEAK); /* clear bits */
h->marked |= cast_byte((weakkey << KEYWEAKBIT) |
(weakvalue << VALUEWEAKBIT));
+ // ¼Óµ½weakÁ´±í
h->gclist = g->weak; /* must be cleared after GC, ... */
g->weak = obj2gco(h); /* ... so put in the appropriate list */
}
}
- if (weakkey && weakvalue) return 1;
+ if (weakkey && weakvalue) return 1; // Èç¹ûÊÇÈõ±í£¬·µ»Ø1
+
+ // Èç¹û²»ÊÇÈõ±í£¬±éÀútableµÄÉ¢Áв¿·ÖºÍÊý×鲿·ÖËùÓÐÔªËØ
+
+ //Êý×é
if (!weakvalue) {
i = h->sizearray;
while (i--)
markvalue(g, &h->array[i]);
}
+
+ //É¢ÁÐ
i = sizenode(h);
while (i--) {
Node *n = gnode(h, i);
@@ -277,24 +295,25 @@ static void traversestack (global_State *g, lua_State *l) {
static l_mem propagatemark (global_State *g) {
GCObject *o = g->gray;
lua_assert(isgray(o));
- gray2black(o);
+ gray2black(o);//Ô¤Ïȱê¼ÇΪºÚÉ«
switch (o->gch.tt) {
- case LUA_TTABLE: {
+ case LUA_TTABLE: { //ɨÃètable£¬³¢ÊÔ±ê¼ÇÊý×éºÍ¹þÏ£²¿·Ö
Table *h = gco2h(o);
g->gray = h->gclist;
+ // Èç¹ûÊÇÈõ±í£¬»ØÍ˻ػÒɫ״̬
if (traversetable(g, h)) /* table is weak? */
black2gray(o); /* keep it gray */
return sizeof(Table) + sizeof(TValue) * h->sizearray +
sizeof(Node) * sizenode(h);
}
- case LUA_TFUNCTION: {
+ case LUA_TFUNCTION: { //ɨÃ躯Êý£¬³¢ÊÔ±ê¼Çenv¡¢upvalue
Closure *cl = gco2cl(o);
g->gray = cl->c.gclist;
traverseclosure(g, cl);
return (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) :
sizeLclosure(cl->l.nupvalues);
}
- case LUA_TTHREAD: {
+ case LUA_TTHREAD: { //ɨÃèÏ̶߳ÔÏó,
lua_State *th = gco2th(o);
g->gray = th->gclist;
th->gclist = g->grayagain;
@@ -428,11 +447,15 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
}
+//c ¼ì²é×Ö·û´®Í°µÄ´óС£¬Èç¹ûÌ«´óÁË£¬½«¿ÕµÄͰɾµô£¬ÖØÐ·ÖÅäͰ
+//c
static void checkSizes (lua_State *L) {
global_State *g = G(L);
/* check size of string hash */
+ //c Èç¹ûͰµÄ×Ü´óСÊÇÓõ½µÄͰ루¼´×Ö·û´®ÊýÁ¿£©µÄ4±¶£¬ÇÒÊÇMINSTRTABSIZEµÄ2±¶
+ //c ¸øÍ°ÊÝÉí
if (g->strt.nuse < cast(lu_int32, g->strt.size/4) &&
- g->strt.size > MINSTRTABSIZE*2)
+ g->strt.size > MINSTRTABSIZE*2)
luaS_resize(L, g->strt.size/2); /* table is too big */
/* check size of buffer */
if (luaZ_sizebuffer(&g->buff) > LUA_MINBUFFER*2) { /* buffer too big? */
@@ -497,15 +520,21 @@ static void markmt (global_State *g) {
}
+//c ³õʼ»¯root
+//c ½«mainthread£¬
/* mark root set */
static void markroot (lua_State *L) {
global_State *g = G(L);
+ // Çå¿Õ
g->gray = NULL;
g->grayagain = NULL;
g->weak = NULL;
+ // 񈬀mainthread
markobject(g, g->mainthread);
/* make global table be traversed before main stack */
+ // ±ê¼Ç _G Ϊ»ÒÉ«
markvalue(g, gt(g->mainthread));
+ // ±ê¼Ç×¢²á±íΪ»ÒÉ«
markvalue(g, registry(L));
markmt(g);
g->gcstate = GCSpropagate;
@@ -553,15 +582,16 @@ static void atomic (lua_State *L) {
}
+//c! µ¥²½GCµÄÈë¿Ú
static l_mem singlestep (lua_State *L) {
global_State *g = G(L);
/*lua_checkmemory(L);*/
switch (g->gcstate) {
- case GCSpause: {
+ case GCSpause: { // ³õʼ»¯
markroot(L); /* start a new collection */
return 0;
}
- case GCSpropagate: {
+ case GCSpropagate: { // ɨÃè²¢±ê¼Ç
if (g->gray)
return propagatemark(g);
else { /* no more `gray' objects */
@@ -681,27 +711,33 @@ void luaC_barrierback (lua_State *L, Table *t) {
g->grayagain = o;
}
-
+//c! ½«Ò»¸öн¨µÄGCGamobject£¨udata³ýÍ⣬¼ÓÔØÆäËûµØ·½£¬¾ßÌå¿´luaS_newudata£©¼ÓÈëroot£¬²¢±ê¼ÇΪ°×É«
+//c upvalue ²»Ò»¶¨»áÉèΪwhite£¬ËùÒÔ²»»áµ÷Õâ¸öº¯Êý
+//c ËäÈ»±»¼ÓÈëÁËrootgc£¬µ«ÊDz»»á±»ÇáÒ×»ØÊÕ£¬luaÓÐË«°×É«»º³å¸ÅÄî
+//c ·ÖΪcurrentwhiteºÍotherwhite¡£Èç¹ûij¸ö¶ÔÏó´´½¨ÔÚGCµÄ±ê¼Ç½×¶ÎÒÔºó£¬ËüµÄwhiteºÍ±ê¼ÇʱµÄwhite²»ÊÇÒ»¸öwhite£¬
+//c ÔÚ»ØÊս׶λáÅжÏһϣ¬²»»á»ØÊÕÕâ¸ö¶ÔÏó
+//c global_StateµÄcurrentwhite switch·¢ÉúÔÚ±ê¼Ç½×¶Îºó
void luaC_link (lua_State *L, GCObject *o, lu_byte tt) {
global_State *g = G(L);
o->gch.next = g->rootgc;
g->rootgc = o;
- o->gch.marked = luaC_white(g);
- o->gch.tt = tt;
+ o->gch.marked = luaC_white(g);//±ê¼ÇΪcurrent white
+ o->gch.tt = tt;//ÉèÖÃÊý¾ÝÀàÐÍ
}
-
+//c! ½«Ò»¸öupvalue¼ÓÈëroot£¬ÓÉÓÚupvalueÊǶÔÒѾ­´æÔڵĶÔÏóµÄ¼ä½ÓÒýÓã¬ËùÒÔºÍÆÕͨ¶ÔÏó²»Ì«Ò»Ñù
void luaC_linkupval (lua_State *L, UpVal *uv) {
global_State *g = G(L);
GCObject *o = obj2gco(uv);
o->gch.next = g->rootgc; /* link upvalue into `rootgc' list */
g->rootgc = o;
+ // ÕâÀïºÍÆÕͨ¶ÔÏó²»Ò»Ñù
if (isgray(o)) {
- if (g->gcstate == GCSpropagate) {
+ if (g->gcstate == GCSpropagate) {//Èç¹ûÔÚɨÃè½×¶Î£¬Ö±½Ó½«¶ÔÏóÖÃΪºÚÉ«
gray2black(o); /* closed upvalues need barrier */
luaC_barrier(L, uv, uv->v);
}
- else { /* sweep phase: sweep it (turning it into white) */
+ else { /* ·ñÔòµÄ»°ºÍÆÕͨ¶ÔÏóÒ»ÑùÖÃΪ°×É« */ /* sweep phase: sweep it (turning it into white) */
makewhite(g, o);
lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
}
diff --git a/src/lua51/lgc.h b/src/lua51/lgc.h
index 5a8dc60..9fa686a 100644
--- a/src/lua51/lgc.h
+++ b/src/lua51/lgc.h
@@ -14,11 +14,11 @@
/*
** Possible states of the Garbage Collector
*/
-#define GCSpause 0
-#define GCSpropagate 1
-#define GCSsweepstring 2
-#define GCSsweep 3
-#define GCSfinalize 4
+#define GCSpause 0 // ÔÝÍ£
+#define GCSpropagate 1 // ɨÃ裬ÕýÔÚ±éÀú»ÒÉ«½Úµã£¬¼ì²éÒýÓÃÇé¿ö
+#define GCSsweepstring 2 // ×Ö·û´®»ØÊÕ½×¶Î
+#define GCSsweep 3 // ³ýÁË×Ö·û´®µÄÆäËû¶ÔÏóµÄ»ØÊÕ½×¶Î
+#define GCSfinalize 4 // ÖÕÖ¹½×¶Î
/*
@@ -51,15 +51,15 @@
*/
-#define WHITE0BIT 0
-#define WHITE1BIT 1
+#define WHITE0BIT 0 // 01
+#define WHITE1BIT 1 // 10
#define BLACKBIT 2
-#define FINALIZEDBIT 3
-#define KEYWEAKBIT 3
-#define VALUEWEAKBIT 4
-#define FIXEDBIT 5
-#define SFIXEDBIT 6
-#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
+#define FINALIZEDBIT 3 //±ê¼ÇûÓб»ÒýÓõÄudata
+#define KEYWEAKBIT 3 //±ê¼ÇÈõ±íµÄkey
+#define VALUEWEAKBIT 4 // ±ê¼ÇÈõ±íµÄvalue
+#define FIXEDBIT 5 // ±ê¼Çlua_stateÖ÷Ï̶߳ÔÏ󲻿ɻØÊÕ
+#define SFIXEDBIT 6 // ±ê¼Çlua¹Ø¼ü×Ö²»Òª»ØÊÕ
+#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) // ½á¹ûÊÇ11
#define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
@@ -74,6 +74,8 @@
#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
+// g->currentwhite & WHITEBITSÊÇ current white
+// g->currentwhite ^ WHITEBITSÊÇ otherwhite
#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
diff --git a/src/lua51/llex.c b/src/lua51/llex.c
index 88c6790..fe4e3ee 100644
--- a/src/lua51/llex.c
+++ b/src/lua51/llex.c
@@ -33,6 +33,7 @@
#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
+//c ¶ÔÓ¦RESERVEDÖеÄö¾Ù, tokens
/* ORDER RESERVED */
const char *const luaX_tokens [] = {
"and", "break", "do", "else", "elseif",
@@ -61,12 +62,15 @@ static void save (LexState *ls, int c) {
}
+//c ÉèÖñ£Áô×Ö
void luaX_init (lua_State *L) {
int i;
for (i=0; i<NUM_RESERVED; i++) {
TString *ts = luaS_new(L, luaX_tokens[i]);
luaS_fix(ts); /* reserved words are never collected */
lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN);
+ //c ±ê¼ÇÕâ¸ö×Ö·û´®Îª±£Áô×Ö£¬¼´²»ÎªÁã
+ //c luaX_tokensÖеÄË÷Òý
ts->tsv.reserved = cast_byte(i+1); /* reserved word */
}
}
diff --git a/src/lua51/llex.h b/src/lua51/llex.h
index a9201ce..8a013f9 100644
--- a/src/lua51/llex.h
+++ b/src/lua51/llex.h
@@ -17,6 +17,7 @@
#define TOKEN_LEN (sizeof("function")/sizeof(char))
+//c tokensö¾Ù£¬ÓÃÀ´×öÖÕ½á·û
/*
* WARNING: if you change the order of this enumeration,
* grep "ORDER RESERVED"
diff --git a/src/lua51/lobject.h b/src/lua51/lobject.h
index f1e447e..af6b63a 100644
--- a/src/lua51/lobject.h
+++ b/src/lua51/lobject.h
@@ -40,6 +40,10 @@ typedef union GCObject GCObject;
** Common Header for all collectable objects (in macro form, to be
** included in other objects)
*/
+// ÐèÒªÀ¬»ø»ØÊÕµÄÀàÐͰüº¬Õâ¸öÍ·
+//c next Ö¸ÏòÏÂÒ»¸ögcÁ´±íµÄ³ÉÔ±
+//c tt Êý¾ÝÀàÐÍ
+//c GC±ê¼Ç£¬ÓÃÀ´±£´æÑÕÉ«,Óа×É«£¨2ÖÖ£©,»ÒÉ«ºÍºÚÉ«
#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
@@ -53,6 +57,8 @@ typedef struct GCheader {
+//c lua_TValueϵڶþ¸ß¼¶µÄÊý¾Ý·â×°ÀàÐÍ£¬²»º¬Êý¾ÝÀàÐͱêʶ
+//c °üº¬ÐèÒªGCµÄÀàÐͺͲ»ÐèÒªGCµÄÀàÐÍ
/*
** Union of all Lua values
*/
@@ -68,8 +74,10 @@ typedef union {
** Tagged Values
*/
+//c ÓÃÔÚÁ½¸öµØ·½£¬lua_TValueºÍTKey
#define TValuefields Value value; int tt
+//c ×îÉϲãµÄValueÀàÐÍ£¬°üº¬ÖµºÍÀàÐÍ
typedef struct lua_TValue {
TValuefields;
} TValue;
@@ -112,7 +120,7 @@ typedef struct lua_TValue {
lua_assert(!iscollectable(obj) || \
((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
-
+//c ½«TValue½á¹¹¾ßÌåΪ¶ÔÓ¦µÄÀàÐͺÍÖµ£»¼´ÐÞ¸ÄTValueµÄttºÍvalue²¿·Ö
/* Macros to set values */
#define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
@@ -193,6 +201,9 @@ typedef struct lua_TValue {
typedef TValue *StkId; /* index to stack elements */
+//c ×Ö·û´®
+//c ÿ¸ö×Ö·û´®¶¼ÐèÒª¼ÆËãhash£¬ÓÃÀ´±È½ÏºÍ²éÕÒ×Ö·û´®
+//c Èç¹ûÕâ¸ö×Ö·û´®ÒѾ­´æÔÚ£¬²»ÔÙÉú³ÉеÄ×Ö·û´®£¬¶øÊÇʹÓþɵÄ
/*
** String headers for string table
*/
@@ -200,8 +211,8 @@ typedef union TString {
L_Umaxalign dummy; /* ensures maximum alignment for strings */
struct {
CommonHeader;
- lu_byte reserved;
- unsigned int hash;
+ lu_byte reserved;//±ê¼Ç×Ö·û´®ÊÇ·ñÊÇluaµÄ¹Ø¼ü×Ö£¬Èç¹ûÊÇ£¬²»»á±»GC»ØÊÕ
+ unsigned int hash;//×Ö·û´®µÄ¹þÏ£Öµ£¬±È½Ï×Ö·û´®µÄÒÀ¾Ý
size_t len;
} tsv;
} TString;
@@ -210,8 +221,7 @@ typedef union TString {
#define getstr(ts) cast(const char *, (ts) + 1)
#define svalue(o) getstr(rawtsvalue(o))
-
-
+// userdata£¬ºÍTString±È½ÏÏñ
typedef union Udata {
L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
struct {
@@ -223,18 +233,22 @@ typedef union Udata {
} Udata;
-
-
+//c! º¯ÊýÔ­ÐÍ£¬ÊǹµÍ¨Ç°¶ËºÍºó¶Ë£¨·ÖÎö½×¶ÎºÍÖ´Ðн׶Σ©µÄÊý¾Ý
+//c! Ö÷ÒªÓÐ ×Ö½ÚÂë¡¢³£Á¿¡¢¾Ö²¿±äÁ¿¡¢upvalue
/*
** Function Prototypes
*/
typedef struct Proto {
CommonHeader;
+ //c º¯ÊýµÄ³£Á¿
TValue *k; /* constants used by the function */
+ //c º¯Êý×Ö½ÚÂë
Instruction *code;
struct Proto **p; /* functions defined inside the function */
int *lineinfo; /* map from opcodes to source lines */
+ //c ¾Ö²¿±äÁ¿
struct LocVar *locvars; /* information about local variables */
+ //c upvalue
TString **upvalues; /* upvalue names */
TString *source;
int sizeupvalues;
@@ -259,6 +273,7 @@ typedef struct Proto {
#define VARARG_NEEDSARG 4
+//c ¾Ö²¿±äÁ¿
typedef struct LocVar {
TString *varname;
int startpc; /* first point where variable is active */
@@ -312,6 +327,7 @@ typedef union Closure {
} Closure;
+//c ÅжϺ¯ÊýÀàÐÍ£¬5.2ͨ¹ý¸ßλÅжϣ¬ÔÚ»ù´¡ÀàÐÍ»ù´¡ÉϲúÉú±äÌå variant
#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
#define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
@@ -319,36 +335,54 @@ typedef union Closure {
/*
** Tables
*/
-
+//c ±íµÄij¸öÔªËØµÄkey
typedef union TKey {
+ //nk ÓÃÀ´Äõ½ÏÂÒ»¸önodeµÄÖ¸Õë
struct {
- TValuefields;
+ TValuefields; // ¾ÍÊÇTValue
+ //c Ö¸ÏòÏÂÒ»¸önode
struct Node *next; /* for chaining */
- } nk;
- TValue tvk;
+ } nk; // next key
+ //tvk ÓÃÀ´Äñ¾nodeµÄkeyÖµ
+ TValue tvk; // key value
} TKey;
-
+//c ±íµÄµ¥¸öÔªËØ
typedef struct Node {
- TValue i_val;
- TKey i_key;
+ TValue i_val;//value
+ TKey i_key; //key
} Node;
+//c ±í
typedef struct Table {
CommonHeader;
+ //c ±ê¼ÇÕâ¸ö±íÓÐÄÄЩԪ·½·¨£¬µÚÒ»´Î²éÕÒʱΪ0£¬Èç¹ûÓÐij¸öÔª·½·¨£¬½«¶ÔӦλÖÃΪ1
+ //c Ï´βéÕÒʱ¾Í²»ÐèÒª²éÕÒ×Ö·û´®ÁË
+ //c flag»áÔÚluaH_setÇå¿Õ
lu_byte flags; /* 1<<p means tagmethod(p) is not present */
+ //c lsizenode=log2(length of hash table) ÓÉ´Ë¿É֪ɢÁбí´óСһ¶¨ÊÇ2µÄÃÝ
+ //c ËùÒÔÈç¹ûÉ¢ÁбíÒªÀ©Õ¹£¬ÔÚÔ­´óС»ù´¡ÉÏÀ©Õ¹Ò»±¶
+ //c ÒªµÃµ½É¢Áбí´óС£¬Ö»ÐèÒªÒÆÎ» length of hash table = 1 << lsizenode
lu_byte lsizenode; /* log2 of size of `node' array */
+ //c ¸Ã±íµÄÔª±í
struct Table *metatable;
+ //c Êý×鲿·Ö
TValue *array; /* array part */
+ //c É¢ÁÐͰÆðʼλÖÃ
+ //c ¶à¸öͰ£¬Í°ÄÚͨ¹ýTKey->nk->nextÁ¬½Ó
Node *node;
+ //c ¿ÕλÖÃ
Node *lastfree; /* any free position is before this position */
+ //c ÔÚglobal_StateÖÐÓйØgcµÄÁ´±íµÄÏÂÒ»¸ö£¨Èç¹ûÕâ¸ö¶ÔÏó±»¼ÓÈëÁ˵ϰ£©
GCObject *gclist;
+ //c Êý×鲿·ÖµÄ´óС
int sizearray; /* size of `array' array */
} Table;
+//c ¼ÆËã¹þÏ£Öµ£¬ÓÃÔÚºÜ¶àµØ·½
/*
** `module' operation for hashing (size is always a power of 2)
*/
diff --git a/src/lua51/lopcodes.c b/src/lua51/lopcodes.c
index 4cc7452..64652bb 100644
--- a/src/lua51/lopcodes.c
+++ b/src/lua51/lopcodes.c
@@ -55,7 +55,12 @@ const char *const luaP_opnames[NUM_OPCODES+1] = {
NULL
};
-
+//c ÏÞÖÆÃ¿¸öÖ¸ÁîµÄ¾ßÌå¸ñʽ£¬ÓÃÀ´ºóÐøÅжÏ
+//c t ÊDz»ÊÇÂß¼­ÅжÏÀàÖ¸ÁÈçÏàµÈ£¬´óÓÚ£¬Ð¡ÓÚ
+//c a ´ËÖ¸Áî»á²»»á¸³Öµ¸ø¼Ä´æÆ÷A
+//c b ²ÎÊý¸ñʽOpArgMask,ÈçÊǼĴæÆ÷£¬Ö¸ÁîÌø×ªÆ«ÒÆ£¬³£Êý
+//c c ͬÉÏ
+//c m Ö¸Áî¸ñʽ
#define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m))
const lu_byte luaP_opmodes[NUM_OPCODES] = {
diff --git a/src/lua51/lopcodes.h b/src/lua51/lopcodes.h
index 41224d6..8c0d1a8 100644
--- a/src/lua51/lopcodes.h
+++ b/src/lua51/lopcodes.h
@@ -34,13 +34,24 @@ enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */
/*
** size and position of opcode arguments.
*/
-#define SIZE_C 9
-#define SIZE_B 9
-#define SIZE_Bx (SIZE_C + SIZE_B)
-#define SIZE_A 8
+/*
+ÈýÖÖÖ¸ÁîµÄ¸ñʽ
+iABC B:9 C:9 A:8 Opcode:6
+iABx Bx:18 A:8 Opcode:6
+iAsBx sBx:18 A:8 Opcode:6
+×¢£ºsBxÊÇsigned BX
+¼Ä´æÆ÷¾ÍÊÇÏà¶ÔÓÚcallinfoºÍlua_stateµÄµ±Ç°µ÷ÓõÄbaseµÄij¸öÆ«ÒÆ£¨¼´ABCÖµ)
+*/
+//c µ¥¸öÖ¸ÁîInstructionµÄÿ²¿·ÖµÄ´óС
+// 9 + 9 + 8 + 6 = 32 bits
+#define SIZE_B 9
+#define SIZE_C 9
+#define SIZE_Bx (SIZE_C + SIZE_B)
+#define SIZE_A 8
#define SIZE_OP 6
+//c ÿ¸ö²¿·ÖÔÚInstructionÖÐµÄÆ«ÒÆÁ¿£¬´ÓµÍλµ½¸ßλ
#define POS_OP 0
#define POS_A (POS_OP + SIZE_OP)
#define POS_C (POS_A + SIZE_A)
@@ -76,7 +87,7 @@ enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */
/*
** the following macros help to manipulate instructions
*/
-
+//c »ñÈ¡ºÍÉèÖÃInstructionÖеÄij¸ö²¿·Ö
#define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
@@ -115,9 +126,11 @@ enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */
** Macros to operate RK indices
*/
+//c ¸ßλΪ1ʱ´ú±í³£Êý
/* this bit 1 means constant (0 means register) */
#define BITRK (1 << (SIZE_B - 1))
+//c ¼ì²éxÊDz»Êdz£Á¿£¬¹æ¶¨¸ßλÊÇ1µÄ»°¾ÍÊdz£Á¿£¬È¥³£Á¿±íÀï²é£¬¶ø²»ÊǼĴæÆ÷
/* test whether value is a constant */
#define ISK(x) ((x) & BITRK)
@@ -241,12 +254,12 @@ OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */
** bit 6: instruction set register A
** bit 7: operator is a test
*/
-
+//c Ö¸ÁîµÄ²ÎÊý¸ñʽ
enum OpArgMask {
- OpArgN, /* argument is not used */
- OpArgU, /* argument is used */
- OpArgR, /* argument is a register or a jump offset */
- OpArgK /* argument is a constant or register/constant */
+ OpArgN, /* argument is not used */ // δʹÓã¨Ã»ÓÐ×ùλR()ºÍRK()µÄ²ÎÊýʹÓã©
+ OpArgU, /* argument is used */ // ʹÓõÄ
+ OpArgR, /* argument is a register or a jump offset */ // ¼Ä´æÆ÷¡¢Ìø×ªÆ«ÒÆ
+ OpArgK /* argument is a constant or register/constant */ // ¼Ä´æÆ÷¡¢³£Á¿
};
LUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES];
diff --git a/src/lua51/lparser.c b/src/lua51/lparser.c
index dda7488..7da34e7 100644
--- a/src/lua51/lparser.c
+++ b/src/lua51/lparser.c
@@ -380,9 +380,10 @@ static void close_func (LexState *ls) {
}
+//c! ±àÒëÉú³É×Ö½ÚÂ룬·ÖÎö½×¶ÎµÄΨһÈë¿Ú£¬·µ»ØprotoÖ¸Õë
Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) {
struct LexState lexstate;
- struct FuncState funcstate;
+ struct FuncState funcstate; // ·ÖÎö¹ý³ÌÖеÄÁÙʱÊý¾Ý
lexstate.buff = buff;
luaX_setinput(L, &lexstate, z, luaS_new(L, name));
open_func(&lexstate, &funcstate);
@@ -394,7 +395,7 @@ Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) {
lua_assert(funcstate.prev == NULL);
lua_assert(funcstate.f->nups == 0);
lua_assert(lexstate.fs == NULL);
- return funcstate.f;
+ return funcstate.f; //c ×îÖÕÉú³ÉµÄ×Ö½ÚÂë
}
diff --git a/src/lua51/lstate.c b/src/lua51/lstate.c
index 4313b83..7e55cd0 100644
--- a/src/lua51/lstate.c
+++ b/src/lua51/lstate.c
@@ -39,6 +39,7 @@ typedef struct LG {
+//c ³õʼ»¯Ò»¸ölua_stateµÄÕ»
static void stack_init (lua_State *L1, lua_State *L) {
/* initialize CallInfo array */
L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);
diff --git a/src/lua51/lstate.h b/src/lua51/lstate.h
index 3bc575b..1ec30a8 100644
--- a/src/lua51/lstate.h
+++ b/src/lua51/lstate.h
@@ -35,20 +35,31 @@ struct lua_longjmp; /* defined in ldo.c */
+//c É¢ÁÐͰ
+//c È«¾Ö´æ·Å×Ö·û´®µÄµØ·½¡£Èç¹ûÒ»¸ö×Ö·û´®ÔÚ´Ë´æÔÚ£¬²»ÓÃÖØÐÂÉú³É¡£
+//c Èç¹û±íÀ©ÕŵÄÌ«À÷º¦£¬Ã¿¸öͰµÄ×Ö·û´®Ì«¶à£¬»á½øÐÐÒ»´Îrehash£¬ÖØÐ·ÖÅäÿ¸öͰµÄÊý¾ÝÁ¿
+//c rehashÔÚlstring.c -> luaS_resize
typedef struct stringtable {
- GCObject **hash;
- lu_int32 nuse; /* number of elements */
- int size;
+ GCObject **hash; //c ×Ö·û´®£¬ÒòΪÊÇÉ¢ÁÐͰ£¬ËùÒÔÊÇ**
+ lu_int32 nuse; //c ͰÓõ½µÄÈÝÁ¿£¬ÒòΪ²»Ò»¶¨size¶¼Óõ½ÁË /* number of elements */
+ int size; //c ͰµÄ×ÜÈÝÁ¿£¬Êdz£Öµ
+ // nuseºÍsizeÊÇÓÃÀ´¶¯Ì¬¿ØÖÆÍ°ÈÝÁ¿µÄ¹Ø¼ü
} stringtable;
+//c µ±Ç°º¯ÊýµÄµ÷ÓÃÐÅÏ¢£¬ºÍlua_StateµÄµ÷Óò¿·ÖÀàËÆ
+//c ¶¼ÓÐtop, baseÁ©¸öÓëÕ»Ïà¹ØµÄ³ÉÔ±
+//c StkIdÒýÓõÄÓÀÔ¶ÊÇlua_StateÕ»ÉϵÄÄÚÈÝ
/*
** informations about a call
*/
typedef struct CallInfo {
+ //c µ±Ç°µ÷ÓõÄbaseÖµ£¬¼´ÔÚÕ»ÉϲÙ×÷µÄ¿ªÊ¼Î»ÖÃ
StkId base; /* base for this function */
+ //c º¯ÊýÔ­ÐÍÔÚlua_StateÊý¾ÝÕ»ÉϵÄλÖÃ
StkId func; /* function index in the stack */
StkId top; /* top for this function */
+ //c µ±Ç°Ö´Ðе½µÄÖ¸ÁîλÖÃ
const Instruction *savedpc;
int nresults; /* expected number of results from this function */
int tailcalls; /* number of tail calls lost under this entry */
@@ -61,31 +72,46 @@ typedef struct CallInfo {
#define f_isLua(ci) (!ci_func(ci)->c.isC)
#define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci))
-
+//c µ±Ç°ÐéÄâ»úËùÓÐЭ³Ì¹²ÓõÄÄÚÈÝ
/*
** `global state', shared by all threads of this state
*/
typedef struct global_State {
+ //c string table,¿ªÉ¢ÁÐ±í£¬´æ´¢ËùÓеÄ×Ö·û´®
stringtable strt; /* hash table for strings */
lua_Alloc frealloc; /* function to reallocate memory */
void *ud; /* auxiliary data to `frealloc' */
- lu_byte currentwhite;
+ lu_byte currentwhite;//µ±Ç°°×
+ //c µ±Ç°µÄGC״̬£¬ÓÐ5¸ö£¬ÔÚlgc.h¶¨Òå
lu_byte gcstate; /* state of garbage collector */
+ //c strtÖÐ×Ö·û´®É¢ÁÐͰË÷Òý£¬×Ö·û´®»ØÊÕ½×¶Îÿ´Î»ØÊÕÒ»¸öÉ¢ÁÐͰµÄ×Ö·û´®£¬
int sweepstrgc; /* position of sweep in `strt' */
+ //c °×É«Á´±í¡£¿É»ØÊյĶÔÏ󣬻áÔÚ»ØÊս׶α»»ØÊÕ
+ //c ËùÓÐн¨µÄ¶ÔÏó¶¼»áÔÝ´æÔÚÕâÀµ«²»»á±»»ØÊÕ£¬ÒòΪluaÓÐË«°×É«»úÖÆ
GCObject *rootgc; /* list of all collectable objects */
+ //c ±£´ærootgcÖе±Ç°»ØÊÕµ½µÄλÖã¬Ï´δÓÕâ¸öλÖüÌÐø»ØÊÕ
GCObject **sweepgc; /* position of sweep in `rootgc' */
+ //c »ÒÉ«Á´±í
GCObject *gray; /* list of gray objects */
+ //c ²»¿É±»´ò¶ÏµÄ¶ÔÏóµÄ»ÒÉ«Á´±í
GCObject *grayagain; /* list of objects to be traversed atomically */
+ //c Èõ±í
GCObject *weak; /* list of weak tables (to be cleared) */
+ //c ÓÐ__gc·½·¨µÄuserdata
GCObject *tmudata; /* last element of list of userdata to be GC */
Mbuffer buff; /* temporary buffer for string concatentation */
lu_mem GCthreshold;
+ //c ¿ªÊ¼½øÐÐGCµÄãÐÖµ£¬µ±³¬¹ýÕâ¸öֵʱ¿ªÊ¼GC
lu_mem totalbytes; /* number of bytes currently allocated */
+ //c ÄÚ´æ´óСµÄ¹À¼ÆÖµ
lu_mem estimate; /* an estimate of number of bytes actually in use */
lu_mem gcdept; /* how much GC is `behind schedule' */
+ //c ¿ØÖÆÏÂÒ»ÂÖGC¿ªÊ¼Ê±»ú
int gcpause; /* size of pause between successive GCs */
+ //c ¿ØÖÆGC»ØÊÕËÙ¶È
int gcstepmul; /* GC `granularity' */
lua_CFunction panic; /* to be called in unprotected errors */
+ //c ×¢²á±í
TValue l_registry;
struct lua_State *mainthread;
UpVal uvhead; /* head of double-linked list of all open upvalues */
@@ -94,20 +120,29 @@ typedef struct global_State {
} global_State;
+//c Ò»¸ölua_state£¬¿ÉÒÔ¿´×öÊÇÒ»¸ö
+//c StkIdÒýÓõÄÓÀÔ¶ÊÇlua_StateÕ»ÉϵÄÄÚÈÝ
/*
** `per thread' state
*/
struct lua_State {
CommonHeader;
lu_byte status;
+ //c µ±Ç°Õ»µÄÏÂÒ»¸ö¿ÉÓÃλÖÃ
StkId top; /* first free slot in the stack */
+ //c µ±Ç°*º¯ÊýÕ»*µÄ»ùµØÖ·£¬¸øÄ³¸öº¯ÊýÓã¬ÔÚluad_precallº¯ÊýÖÐÉèÖÃL->base = ci->base = ci->func + 1;
StkId base; /* base of current function */
global_State *l_G;
+ //c µ±Ç°º¯ÊýµÄµ÷ÓÃÐÅÏ¢£¬ÊÇbase_ciÊý×éÖеÄij¸ö
CallInfo *ci; /* call info for current function */
const Instruction *savedpc; /* `savedpc' of current function */
+ //c lua_StateÊý¾ÝÕ»µÄÖÕµã
StkId stack_last; /* last free slot in the stack */
+ //c Êý¾ÝÕ»(Õ»Êý×éµÄÆðʼµØÖ·)£¨Êý¾ÝÕ»µÄÆðʼµã£©
StkId stack; /* stack base */
+ //c callinfoÊý×éµÄÖÕµã
CallInfo *end_ci; /* points after end of ci array*/
+ //c callinfo Êý×飬 ÑØ×Åbase_ci±éÀú¿ÉÒԵõ½ÍêÕûµÄluaµ÷ÓÃÁ´
CallInfo *base_ci; /* array of CallInfo's */
int stacksize;
int size_ci; /* size of array `base_ci' */
@@ -118,6 +153,7 @@ struct lua_State {
int basehookcount;
int hookcount;
lua_Hook hook;
+ //c _G global table
TValue l_gt; /* table of globals */
TValue env; /* temporary place for environments */
GCObject *openupval; /* list of open upvalues in this stack */
@@ -130,6 +166,10 @@ struct lua_State {
#define G(L) (L->l_G)
+//c ÐèÒªGCµÄÀàÐÍ»ùÀ࣬ͨ¹ýGCObjectÄܹ»ÒýÓõ½GCheaderÖж¨ÒåµÄ/
+//c #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
+//c ÕâÖÖд·¨ÊÇÒ»ÖÖʵÏÖC¼Ì³ÐµÄ·½Ê½
+//c ÿ¸ö
/*
** Union of all collectable objects
*/
@@ -144,7 +184,7 @@ union GCObject {
struct lua_State th; /* thread */
};
-
+//c TValue\GCObject »ñÈ¡unionÖеÄÄÚÈÝ
/* macros to convert a GCObject into a specific value */
#define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
#define gco2ts(o) (&rawgco2ts(o)->tsv)
diff --git a/src/lua51/lstring.c b/src/lua51/lstring.c
index 4911315..ec1fcdb 100644
--- a/src/lua51/lstring.c
+++ b/src/lua51/lstring.c
@@ -19,25 +19,29 @@
+//c ÖØÐ·ÖÅäglobal_State->strtÿ¸öͰµÄÊý¾ÝÁ¿
+//c µ±Í°µÄ´óСԶ³¬×Ö·û´®ÈÝÁ¿£¬ÖØÐ·ÖÅä´óС£¬É¾µôһЩ¿ÕͰ£¨¿Õ룩
void luaS_resize (lua_State *L, int newsize) {
GCObject **newhash;
stringtable *tb;
int i;
- if (G(L)->gcstate == GCSsweepstring)
+ if (G(L)->gcstate == GCSsweepstring)//Èç¹ûGCÔÚ»ØÊÕ×Ö·û´®½×¶Î£¬²»Òªrehash
return; /* cannot resize during GC traverse */
- newhash = luaM_newvector(L, newsize, GCObject *);
- tb = &G(L)->strt;
+ newhash = luaM_newvector(L, newsize, GCObject *); //½¨Á¢Ò»¸öеÄÉ¢ÁÐͰ£¬²¢Çå¿Õ
+ tb = &G(L)->strt;//¾ÉµÄÉ¢ÁÐͰ
for (i=0; i<newsize; i++) newhash[i] = NULL;
- /* rehash */
+ //±éÀú¾ÍµÄÉ¢ÁÐͰ£¬²¢ÌîÈëеÄÉ¢ÁÐͰ
+ /* rehash */
for (i=0; i<tb->size; i++) {
GCObject *p = tb->hash[i];
while (p) { /* for each node in the list */
GCObject *next = p->gch.next; /* save next */
unsigned int h = gco2ts(p)->hash;
+ //еÄÉ¢ÁÐÖµ
int h1 = lmod(h, newsize); /* new position */
lua_assert(cast_int(h%newsize) == lmod(h, newsize));
- p->gch.next = newhash[h1]; /* chain it */
- newhash[h1] = p;
+ p->gch.next = newhash[h1];//c ½ÓÔÚͬһ¸öhashµÄ×îÇ°Ãæ /* chain it */
+ newhash[h1] = p; //c ½«p×÷ΪͰÄÚµÚÒ»¸ö´æÆðÀ´
p = next;
}
}
@@ -47,6 +51,7 @@ void luaS_resize (lua_State *L, int newsize) {
}
+//c н¨×Ö·û´®
static TString *newlstr (lua_State *L, const char *str, size_t l,
unsigned int h) {
TString *ts;
@@ -62,42 +67,53 @@ static TString *newlstr (lua_State *L, const char *str, size_t l,
memcpy(ts+1, str, l*sizeof(char));
((char *)(ts+1))[l] = '\0'; /* ending 0 */
tb = &G(L)->strt;
+ // ¼ÆËãhashÖµ
h = lmod(h, tb->size);
ts->tsv.next = tb->hash[h]; /* chain new entry */
tb->hash[h] = obj2gco(ts);
tb->nuse++;
+ //c ¸ø×Ö·û´®Í¨À©ÈÝ£¬Èç¹û×Ö·û´®ÊýÁ¿´óÓÚͰÈÝÁ¿
+ //c ¸øÍ°À©ÈÝΪ2±¶
if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
luaS_resize(L, tb->size*2); /* too crowded */
return ts;
}
+//c ´´½¨×Ö·û´®
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
GCObject *o;
unsigned int h = cast(unsigned int, l); /* seed */
size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
size_t l1;
+ //c Èç¹û×Ö·û´®·Ç³£³¤£¬²»ÒªÖðλ¼ÆËãÉ¢ÁÐÖµ£¬Ã¿step²½È¡Ò»¸ö×Ö·û¼ÆËã¼´¿É
for (l1=l; l1>=step; l1-=step) /* compute hash */
h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));
for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
o != NULL;
o = o->gch.next) {
TString *ts = rawgco2ts(o);
- if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
+ if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {//c Èç¹ûÉ¢ÁÐÖµÏàͬ£¬ÓÃmemcmp¿ìËÙ¶Ô±È
+ //c Èç¹û×Ö·û´®±»±ê¼ÇÁË»ØÊÕ(gch.marked)£¬ÖØÐ±ê¼ÇËü²»Òª»ØÊÕ
/* string may be dead */
if (isdead(G(L), o)) changewhite(o);
return ts;
}
}
+ //c Èç¹ûglobal->strtÀïûÓÐÕâ¸ö×Ö·û´®£¬Ð½¨Á¢Ò»¸ö£¬²¢¼Óµ½¹þϣͰÀï
return newlstr(L, str, l, h); /* not found */
}
-
+//c ´´½¨userdata
+//c userdataµÄgcºÍÆÕͨ¶ÔÏó£¨udataºÍuv³ýÍâµÄ£©ÊÇ·Ö¿ªµÄ
Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
Udata *u;
if (s > MAX_SIZET - sizeof(Udata))
luaM_toobig(L);
u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata)));
+ //c udataºÍÆÕͨ¶ÔÏóÔÚGCÉϵÄÇø±ðÔÚÓÚ²»µ÷ÓÃluaC_link£¬ÒòΪ²»»á¼ÓÔÚ
+ //c G(L)->rootgcÁ´ÉÏ£¬¶øÊǼÓÔÚG(L)->mainthreadºóÃæ
+ //c udata±ê¼ÇΪ white
u->uv.marked = luaC_white(G(L)); /* is not finalized */
u->uv.tt = LUA_TUSERDATA;
u->uv.len = s;
@@ -105,7 +121,10 @@ Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
u->uv.env = e;
/* chain it on udata list (after main thread) */
u->uv.next = G(L)->mainthread->next;
- G(L)->mainthread->next = obj2gco(u);
- return u;
+ // ¿É¼ûËùÓÐuserdata¶¼¸úÔÚmainthreadÖ®ºóµÚÒ»¸ö
+ // ÕâÑùÊÇΪÁË·½±ã£¬ÊÇÒ»¸öhack
+ // ÓÉÓÚuserdata¿ÉÄܻᶨÒåÁË__gc£¬ËùÒÔͳһ´¦Àí
+ G(L)->mainthread->next = obj2gco(u);
+ return u;
}
diff --git a/src/lua51/ltable.c b/src/lua51/ltable.c
index ec84f4f..f489a03 100644
--- a/src/lua51/ltable.c
+++ b/src/lua51/ltable.c
@@ -159,6 +159,9 @@ static int findindex (lua_State *L, Table *t, StkId key) {
}
+//c tableµÄ±éÀú²Ù×÷£¬·µ»ØÏÂÒ»¸önode
+//c ÏȼÆËãkeyµÄhash£¬Èç¹ûÔÚarray·¶Î§ÄÚ£¬ÔÚarrayÖÐÕÒ£¬·ñÔòÔÚhashÖÐÕÒ
+//c ×¢Òâʵ¼ÊÉϲ»»áÁ½¸öforÑ­»·¶¼×ß
int luaH_next (lua_State *L, Table *t, StkId key) {
int i = findindex(L, t, key); /* find original element */
for (i++; i < t->sizearray; i++) { /* try first array part */
@@ -208,8 +211,10 @@ static int computesizes (int nums[], int *narray) {
}
+//c ·µ»Ø1»ò0£¬ÊÇ·ñ½«Õâ¸ökeyËã½øÒª¼ÓÈë±íµÄÊý×鲿·Ö
static int countint (const TValue *key, int *nums) {
int k = arrayindex(key);
+ //c ËùÒÔÊý×ÖkeyµÄ×î´ó·¶Î§ÊÇ0~MAXASIZE
if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
nums[ceillog2(k)]++; /* count as such */
return 1;
@@ -294,6 +299,9 @@ static void setnodevector (lua_State *L, Table *t, int size) {
}
+//c ÖØÐ·ÖÅätableµÄ¹þÏ£²¿·Ö´óСΪ
+//nasize ÊÇ array size
+//nhsize ÊÇ hash table size
static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
int i;
int oldasize = t->sizearray;
@@ -330,18 +338,26 @@ void luaH_resizearray (lua_State *L, Table *t, int nasize) {
}
+//c ¸øtableµÄÉ¢ÁÐͰ²¿·ÖÖØÐÂhash
+//c ÓÃÓÚ¸øtableÀ©ÈÝ
static void rehash (lua_State *L, Table *t, const TValue *ek) {
+ //c ÏȼÆËãÂäÔÚ2^(i-1)~2^i·¶Î§Äڵĸ÷¸öÇø¼äµÄÔªËØÊýÁ¿£¬ÓÃÓÚ¾ö²ßÐÂÈÝÁ¿
int nasize, na;
+ //c ÒÔ2µÄÖ¸Êý¼¶À©Õ¹µÄÊý×飬¼Ç¼µÄÊÇÖ¸Êý¡£ÄÜ¿´³öÀ´±íµÄ×î´ó´óСÊÇ2^MAXBITS
int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */
int i;
int totaluse;
for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */
+ //c ±éÀúÊý×鲿·ÖºÍÉ¢ÁÐͰ²¿·Ö£¬ÕÒ³öÕýÕûÊýkeyµÄÖµµÄÊýÁ¿£¬ÒԴ˸üÐÂnums[]
nasize = numusearray(t, nums); /* count keys in array part */
totaluse = nasize; /* all those keys are integer keys */
totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */
/* count extra key */
+ //c ¼ÓÉÏÕâ¸ökey£¨Èç¹ûÔÚ·¶Î§Äڵϰ£©
nasize += countint(ek, nums);
totaluse++;
+ //c ÕÒµ½Ò»¸öλÖÃi£¬Õâ¸öλÖÃ֮ǰµÄÔªËØ¸öÊý´óÓÚ50%£¬¼´2^i/2¡£Õâ¸öλÖÃÖ®ºóÒâζ×ÅÌ«¿ÕÁË£¬Ð§ÂʱȽϵÍ
+ //c Õâ¸öλÖÃ
/* compute new size for array part */
na = computesizes(nums, &nasize);
/* resize the table to new computed sizes */
@@ -354,7 +370,7 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) {
** }=============================================================
*/
-
+//c н¨table
Table *luaH_new (lua_State *L, int narray, int nhash) {
Table *t = luaM_new(L, Table);
luaC_link(L, obj2gco(t), LUA_TTABLE);
@@ -388,7 +404,7 @@ static Node *getfreepos (Table *t) {
}
-
+//c ±íн¨key
/*
** inserts a new key into a hash table; first, check whether key's main
** position is free. If not, check whether colliding node is in its main
@@ -398,30 +414,41 @@ static Node *getfreepos (Table *t) {
*/
static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
Node *mp = mainposition(t, key);
- if (!ttisnil(gval(mp)) || mp == dummynode) {
+ if (!ttisnil(gval(mp)) || mp == dummynode) {//c mainpositionÉÏÒѾ­ÓÐÊý¾Ý
Node *othern;
Node *n = getfreepos(t); /* get a free place */
+ //c Èç¹ûûÓпÕ룬À©Õ¹hash table´óСΪ2±¶
if (n == NULL) { /* cannot find a free place? */
rehash(L, t, key); /* grow table */
+ //c À©Õ¹Íê´óСºóÖØÊÔ
return luaH_set(L, t, key); /* re-insert key into grown table */
}
lua_assert(n != dummynode);
+ //c ÏÈ¿´Ò»ÏÂÏÖÔÚmainpositionÉϵÄÕâ¸önode£¬ËüµÄmainpositionÊDz»ÊÇÕâ¸öÖµ
+ //c ²»Êǵϰ¸øÐµÄkeyÈ÷
othern = mainposition(t, key2tval(mp));
if (othern != mp) { /* is colliding node out of its main position? */
+ // °Ñmp¿Õ³öÀ´,mpÀïµÄÖµÒÆµ½n(freeposition)
/* yes; move colliding node into free position */
+ //? ÕâÐÐû¿´¶®
while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
gnext(othern) = n; /* redo the chain with `n' in place of `mp' */
*n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
gnext(mp) = NULL; /* now `mp' is free */
setnilvalue(gval(mp));
+ //mpÊÇÒª¸³ÖµµÄλÖ㬼´ÐÂkeyµÄÔªËØµÄλÖÃ
}
else { /* colliding node is in its own main position */
/* new node will go into free position */
+ //c ½«free position²åÈëµ½mpºóµÚÒ»¸ö
gnext(n) = gnext(mp); /* chain new position */
gnext(mp) = n;
+ //c ÐÞ¸ÄÒ»ÏÂmpÖ¸Õ룬ָÏòfreeposition£¬Áô¸öºóÐøÊ¹ÓÃ
mp = n;
}
}
+ //c Èç¹ûmainpositionλÖÃÉÏÊǿյģ¬¿ÉÒÔÖ±½ÓÓÃmpλÖôæ
+ //c ¸³Öµ
gkey(mp)->value = key->value; gkey(mp)->tt = key->tt;
luaC_barriert(L, t, key);
lua_assert(ttisnil(gval(mp)));
@@ -553,6 +580,7 @@ static int unbound_search (Table *t, unsigned int j) {
}
+//c »ñµÃtable³¤¶È
/*
** Try to find a boundary in table `t'. A `boundary' is an integer index
** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
@@ -560,7 +588,9 @@ static int unbound_search (Table *t, unsigned int j) {
int luaH_getn (Table *t) {
unsigned int j = t->sizearray;
if (j > 0 && ttisnil(&t->array[j - 1])) {
+ // ¶þ·Ö²éÕÒ£¬ÕÒµ½
/* there is a boundary in the array part: (binary) search for it */
+ // iÊÇ×ó½ç£¬jÊÇÓÒ½ç
unsigned int i = 0;
while (j - i > 1) {
unsigned int m = (i+j)/2;
diff --git a/src/lua51/lua.h b/src/lua51/lua.h
index a4b73e7..9f45735 100644
--- a/src/lua51/lua.h
+++ b/src/lua51/lua.h
@@ -71,6 +71,7 @@ typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
*/
#define LUA_TNONE (-1)
+//c ¶ÔÍâµÄ»ù´¡ÀàÐÍ£¬µ«ÊÇÄÚ²¿»áÇø·Ö±Õ°ü(C\Lua)¡¢º¯Êý(C\Lua)¡¢userdataµÄÀàÐÍ
#define LUA_TNIL 0
#define LUA_TBOOLEAN 1
#define LUA_TLIGHTUSERDATA 2
diff --git a/src/lua51/lvm.c b/src/lua51/lvm.c
index e0a0cd8..85e214a 100644
--- a/src/lua51/lvm.c
+++ b/src/lua51/lvm.c
@@ -343,7 +343,7 @@ static void Arith (lua_State *L, StkId ra, const TValue *rb,
*/
#define runtime_check(L, c) { if (!(c)) break; }
-
+//c »ñµÃÖ¸ÁîÖеÄij¸ö²¿·ÖµÄÖµ£¬²¢¸ù¾Ýopmode½øÐÐУÑé
#define RA(i) (base+GETARG_A(i))
/* to be used after possible stack reallocation */
#define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
@@ -374,17 +374,25 @@ static void Arith (lua_State *L, StkId ra, const TValue *rb,
+//c Ö´ÐÐluaº¯Êý£¨²»º¬Cº¯Êý£¬Cº¯ÊýÔÚluaD_precallÖ´ÐÐ)
+//c ¶ÁÈ¡×Ö½ÚÂë²¢ÔËÐУ¬¹µÍ¨Ç°¶ËºÍºó¶ËµÄÇÅÁº
+//c ÔÚldo.c->luaD_callº¯ÊýÖÐÖ´ÐУ¬Ç°Ò»²½ÊÇluaD_precall()
void luaV_execute (lua_State *L, int nexeccalls) {
- LClosure *cl;
- StkId base;
- TValue *k;
- const Instruction *pc;
+ //c ¹ÜÀíÖ¸ÁîºÍÊý¾ÝÕ»µÄ4¸ö±äÁ¿£¬»áËæ×ź¯Êýµ÷Óöø¸Ä±ä
+ LClosure *cl;// lua closure£¬°üº¬º¯ÊýÔ­ÐÍ¡¢upvalue¡¢»·¾³env
+ StkId base;// µ±Ç°µ÷ÓõÄbaseµØÖ·
+ TValue *k;// µ±Ç°º¯ÊýÔ­ÐÍÀïÃæµÄ³£Êý±í
+ const Instruction *pc; //c Ö´ÐеÄÖ¸Áî
+
+ //c ´Ëº¯ÊýÒѾ­·µ»Ø»òµ÷ÓÃÁËÆäËûº¯Êý
reentry: /* entry point */
- lua_assert(isLua(L->ci));
- pc = L->savedpc;
+ lua_assert(isLua(L->ci)); // ×öÒ»¸ö·À»¤£¬ÖØÈë±ØÈ»ÊÇlua´úÂ룬OP_CALLºÍOP_TAILCALL»áÓÃluaD_precall´¦ÀíCº¯Êýµ÷ÓÃ
+
+ pc = L->savedpc;//c ×ʼÊÇluaD_precallÖи³ÖµµÄº¯ÊýÔ­Ð͵ÄÖ¸Áͷ
cl = &clvalue(L->ci->func)->l;
base = L->base;
- k = cl->p->k;
+ k = cl->p->k; //c µ±Ç°º¯ÊýÔ­Ð͵ij£Á¿£¬ËùÓк¯Êýµ÷Óö¼»áÓõ½µÄ
+
/* main loop of interpreter */
for (;;) {
const Instruction i = *pc++;
diff --git a/src/math/math.cpp b/src/math/math.cpp
new file mode 100644
index 0000000..acb215f
--- /dev/null
+++ b/src/math/math.cpp
@@ -0,0 +1,37 @@
+#include "math.h"
+
+#pragma comment(lib, "lua.lib")
+
+#define MATH_API __declspec(dllexport)
+
+MATH_API int /*__cdecl*/ luaopen_math_vector(lua_State* L)
+{
+ lua_pushstring(L, "math.vector module");
+
+ return 1;
+}
+
+
+extern "C" MATH_API int /*__cdecl*/ luaopen_math_matrix(lua_State* L)
+{
+ lua_pushstring(L, "math.matrix module");
+
+ return 1;
+}
+
+extern "C" MATH_API int /*__cdecl*/ luaopen_math_quaternion(lua_State* L)
+{
+ lua_pushstring(L, "math.quaternion module");
+
+ return 1;
+}
+
+MATH_API void foo()
+{
+
+}
+
+class MATH_API Vector
+{
+
+};
diff --git a/src/math/math.h b/src/math/math.h
new file mode 100644
index 0000000..494b725
--- /dev/null
+++ b/src/math/math.h
@@ -0,0 +1,9 @@
+#pragma once
+
+extern "C" {
+#include "../lua51/lua.h"
+#include "../lua51/lualib.h"
+#include "../lua51/lauxlib.h"
+}
+
+//#define MATH_API __declspec(dllexport)