Browse Source

update: README_JA.md/README.md

nuskey8 1 month ago
parent
commit
2f7ecf3147
3 changed files with 105 additions and 75 deletions
  1. 44 28
      README.md
  2. 48 35
      README_JA.md
  3. 13 12
      src/Lua/Lua.csproj

+ 44 - 28
README.md

@@ -90,7 +90,7 @@ var isNil = results[0].Type == LuaValueType.Nil;
 Below is a table showing the type mapping between Lua and C#.
 
 | Lua               | C#                       |
-|-------------------|--------------------------|
+| ----------------- | ------------------------ |
 | `nil`             | `LuaValue.Nil`           |
 | `boolean`         | `bool`                   |
 | `string`          | `string`                 |
@@ -193,14 +193,17 @@ var funcResults = await state.CallAsync(func, [1, 2]);
 // 3
 Console.WriteLine(funcResults[0]);
 ```
-If you are concerned about array allocation, you can directly handle the stack as follows.
-```csharp
+
+To avoid array allocation, an API is also provided that passes arguments using the stack.
+
+```cs
 var func = results[0];
 var basePos = state.Stack.Count;
 state.Push(func);
 state.Push(1);
 state.Push(2);
-var funcResultsCount = await state.CallAsync(funcIndex:basePos, returnBase: basePos);
+var funcResultsCount = await state.CallAsync(funcIndex: basePos, returnBase: basePos);
+
 using (var reader = state.ReadStack(funcResultsCount))
 {
     var span = reader.AsSpan();
@@ -210,6 +213,7 @@ using (var reader = state.ReadStack(funcResultsCount))
     }
 }
 ```
+
 ### Calling C# Functions from Lua
 
 It is possible to create a `LuaFunction` from a lambda expression.
@@ -250,18 +254,23 @@ return add(1, 2)
 > [!TIP]  
 > Defining functions with `LuaFunction` can be somewhat verbose. When adding multiple functions, it is recommended to use the Source Generator with the `[LuaObject]` attribute. For more details, see the [LuaObject](#luaobject) section.
 
-## Lua API
-Besides normal function calls, you can perform various Lua operations from C#.
+## Low-Level API
+
+In addition to normal function calls, it is possible to directly call Lua's low-level API.
+
 ```csharp
 await state.CallAsync(func, [arg1, arg2]); // func(arg1,arg2) in lua
+
 await state.AddAsync(arg1, arg2); // arg1 + arg2 in lua
 await state.SubAsync(arg1, arg2); // arg1 - arg2 in lua
 await state.MulAsync(arg1, arg2); // arg1 * arg2 in lua
 await state.DivAsync(arg1, arg2); // arg1 / arg2 in lua
 await state.ModAsync(arg1, arg2); // arg1 % arg2 in lua
+
 await state.EqualsAsync(arg1, arg2); // arg1 == arg2 in lua
 await state.LessThanAsync(arg1, arg2); // arg1 < arg2 in lua
 await state.LessThanOrEqualsAsync(arg1, arg2); // arg1 <= arg2 in lua
+
 await state.ConcatAsync([arg1, arg2, arg3]); // arg1 .. arg2 .. arg3 in lua
 
 await state.GetTableAsync(table, key); // table[key] in lua
@@ -485,7 +494,9 @@ print(v1 - v2) -- <-1, -1, -1>
 ## Module Loading
 
 In Lua, you can load modules using the `require` function. In regular Lua, modules are managed by searchers within the `package.searchers` function list. In addition to this, Lua-CSharp provides `ILuaModuleLoader` as a module loading mechanism.
-This is executed before `package.searchers`.
+
+> [!NOTE]
+> Module resolution by `ILuaModuleLoader` is performed before `package.searchers`.
 
 ```cs
 public interface ILuaModuleLoader
@@ -508,24 +519,22 @@ state.ModuleLoader = CompositeModuleLoader.Create(
 
 Loaded modules are cached in the `package.loaded` table, just like regular Lua. This can be accessed via `LuaState.LoadedModules`.
 
-## Sandboxing
+## LuaPlatform
+
 In Lua-CSharp, environment abstraction is provided as `LuaPlatform` for sandboxing.
+
 ```cs
-var state = LuaState.Create(new LuaPlatform(FileSystem: new FileSystem(),
-            OsEnvironment: new SystemOsEnvironment(),
-            StandardIO: new ConsoleStandardIO(),
-            TimeProvider: TimeProvider.System));
-```
+var platform = new LuaPlatform(
+    FileSystem: new FileSystem(),
+    OsEnvironment: new SystemOsEnvironment(),
+    StandardIO: new ConsoleStandardIO(),
+    TimeProvider: TimeProvider.System);
 
-[ILuaFileSystem](https://github.com/nuskey8/Lua-CSharp/blob/main/src/Lua/IO/ILuaFileSystem.cs),
-[ILuaOsEnvironment](https://github.com/nuskey8/Lua-CSharp/blob/main/src/Lua/Platforms/ILuaOsEnvironment.cs
-),
-[ILuaStandardIO](https://github.com/nuskey8/Lua-CSharp/blob/main/src/Lua/IO/ILuaStandardIO.cs
-),
-[ILuaStream](https://github.com/nuskey8/Lua-CSharp/blob/main/src/Lua/IO/ILuaStream.cs
-)
+var state = LuaState.Create(platform);
+```
 
 These are used for `require`, `print`, `dofile`, and the `os` module.
+
 ## Exception Handling
 
 Lua script runtime exceptions throw exceptions that inherit from `LuaException`. You can catch these to handle errors during execution.
@@ -545,7 +554,7 @@ catch (LuaRuntimeException)
 }
 catch(OperationCanceledException)
 {
-    // Handle cancel exceptions
+    // Handle cancel exceptions
     // LuaCanceledException allows you to get the cancellation point within Lua.
 }
 ```
@@ -594,16 +603,23 @@ state.ModuleLoader = new ResourcesModuleLoader();
 // Use Addressables for module loading (requires the Addressables package)
 state.ModuleLoader = new AddressablesModuleLoader();
 ```
-### Platform abstraction
-As elements of LuaPlatform,
-`UnityStandardIO` and
-`UnityApplicationOsEnvironment` are provided.
 
+### UnityStandardIO / UnityApplicationOsEnvironment
+
+`UnityStandardIO` and `UnityApplicationOsEnvironment` are provided as LuaPlatform elements for Unity.
+
+```cs
+var platform = new LuaPlatform(
+    FileSystem: new FileSystem(),
+    OsEnvironment: new UnityApplicationOsEnvironment(), // OsEnvironment for Unity
+    StandardIO: new UnityStandardIO(), // StandardIO for Unity
+    TimeProvider: TimeProvider.System);
+var state = LuaState.Create(platform);
+```
 
-`UnityStandardIO` makes things like `print` output to `Debug.Log`.
-With `UnityApplicationOsEnvironment`, environment variables can be set as a dictionary, and `Application.Quit()` will be called by `os.exit()`.
+With `UnityStandardIO`, standard output such as `print` will be output to `Debug.Log()`. With `UnityApplicationOsEnvironment`, environment variables can be set using a Dictionary, and `os.exit()` will call `Application.Quit()`.
 
-These are only simplified versions, so they are not highly recommended for actual use.
+These are simple implementations only, so for actual use it is recommended creating your own implementations as needed.
 
 ## Compatibility
 

+ 48 - 35
README_JA.md

@@ -90,7 +90,7 @@ var isNil = results[0].Type == LuaValueType.Nil;
 Lua-C#間の型の対応を以下に示します。
 
 | Lua               | C#                       |
-|-------------------|--------------------------|
+| ----------------- | ------------------------ |
 | `nil`             | `LuaValue.Nil`           |
 | `boolean`         | `bool`                   |
 | `string`          | `string`                 |
@@ -193,14 +193,15 @@ var funcResults = await state.CallAsync(func, [1, 2]);
 Console.WriteLine(funcResults[0]);
 ```
 
-配列のアロケーションが気になる場合は
-```csharp
+配列のアロケーションを回避するために、スタックを用いて引数をやり取りするAPIも提供されています。
+
+```cs
 var func = results[0];
 var basePos = state.Stack.Count;
 state.Push(func);
 state.Push(1);
 state.Push(2);
-var funcResultsCount = await state.CallAsync(funcIndex:basePos, returnBase: basePos);
+var funcResultsCount = await state.CallAsync(funcIndex: basePos, returnBase: basePos);
 using (var reader = state.ReadStack(funcResultsCount))
 {
     var span = reader.AsSpan();
@@ -223,16 +224,15 @@ state.Environment["add"] = new LuaFunction((context, ct) =>
     var arg0 = context.GetArgument<double>(0);
     var arg1 = context.GetArgument<double>(1);
 
-    // context戻り値を渡す
-    context.Return(arg0 + arg1);
+    // context.Return()で戻り値を渡す
+    var count = context.Return(arg0 + arg1);
     
-    // 複数なら以下のようにまとめて渡す必要がある
+    // 複数の戻り値を渡すことも可能
     // context.Return(arg0, arg1);
     // context.Return([arg0, arg1]);
 
     // 戻り値の数を返す
-    return new(1);
-    // return new(context.Return(arg0 + arg1)); //でも可
+    return new(count);
 });
 
 // Luaスクリプトを実行
@@ -251,18 +251,23 @@ return add(1, 2)
 > [!TIP]
 > `LuaFunction`による関数の定義はやや記述量が多いため、関数をまとめて追加する際には`[LuaObject]`属性によるSource Generatorの使用を推奨します。詳細は[LuaObject](#luaobject)の項目を参照してください。
 
-## Lua API
-通常の関数呼出し以外でもC#から様々なLuaの操作を行えます。
+## 低レベルAPI
+
+通常の関数呼び出し以外にも、Luaの低レベルなAPIを直接呼び出すことが可能です。
+
 ```csharp
 await state.CallAsync(func, [arg1, arg2]); // func(arg1,arg2) in lua
+
 await state.AddAsync(arg1, arg2); // arg1 + arg2 in lua
 await state.SubAsync(arg1, arg2); // arg1 - arg2 in lua
 await state.MulAsync(arg1, arg2); // arg1 * arg2 in lua
 await state.DivAsync(arg1, arg2); // arg1 / arg2 in lua
 await state.ModAsync(arg1, arg2); // arg1 % arg2 in lua
+
 await state.EqualsAsync(arg1, arg2); // arg1 == arg2 in lua
 await state.LessThanAsync(arg1, arg2); // arg1 < arg2 in lua
 await state.LessThanOrEqualsAsync(arg1, arg2); // arg1 <= arg2 in lua
+
 await state.ConcatAsync([arg1, arg2, arg3]); // arg1 .. arg2 .. arg3 in lua
 
 await state.GetTableAsync(table, key); // table[key] in lua
@@ -487,7 +492,9 @@ print(v1 - v2) -- <-1, -1, -1>
 ## モジュールの読み込み
 
 Luaでは`require`関数を用いてモジュールを読み込むことができます。通常のLuaでは`package.searchers`の検索関数を用いてモジュールの管理を行いますが、Lua-CSharpではそれに加えて`ILuaModuleLoader`がモジュール読み込みの機構として提供されています。
-これはpackage.searchersより先に実行されます。
+
+> [!NOTE]
+> `ILuaModuleLoader`によるモジュールの解決は`package.searchers`より先に実行されます。
 
 ```cs
 public interface ILuaModuleLoader
@@ -510,24 +517,22 @@ state.ModuleLoader = CompositeModuleLoader.Create(
 
 また、ロード済みのモジュールは通常のLua同様に`package.loaded`テーブルにキャッシュされます。これは`LuaState.LoadedModules`からアクセスすることが可能です。
 
-## サンドボックス化
-Lua-CSharpではサンドボックス化のために環境の抽象化を`LuaPlatform`として提供しています。
+## LuaPlatform
+
+Lua-CSharpではサンドボックス化のための機能として、スクリプト環境の抽象化を`LuaPlatform`として提供しています。
+
 ```cs
-var state = LuaState.Create(new LuaPlatform(FileSystem: new FileSystem(),
-            OsEnvironment: new SystemOsEnvironment(),
-            StandardIO: new ConsoleStandardIO(),
-            TimeProvider: TimeProvider.System));
+var platform = new LuaPlatform(
+    FileSystem: new FileSystem(),
+    OsEnvironment: new SystemOsEnvironment(),
+    StandardIO: new ConsoleStandardIO(),
+    TimeProvider: TimeProvider.System);
+
+var state = LuaState.Create(platform);
 ```
 
-[ILuaFileSystem](https://github.com/nuskey8/Lua-CSharp/blob/main/src/Lua/IO/ILuaFileSystem.cs),
-[ILuaOsEnvironment](https://github.com/nuskey8/Lua-CSharp/blob/main/src/Lua/Platforms/ILuaOsEnvironment.cs
-),
-[ILuaStandardIO](https://github.com/nuskey8/Lua-CSharp/blob/main/src/Lua/IO/ILuaStandardIO.cs
-),
-[ILuaStream](https://github.com/nuskey8/Lua-CSharp/blob/main/src/Lua/IO/ILuaStream.cs
-)
+これらは`require`, `print`, `dofile`や`os`モジュールなどで使用されます。
 
-これらは`require`,`print`,`dofile`や`os`moduleなどで使用されます。
 ## 例外処理
 
 Luaスクリプトの実行時例外は`LuaRuntimeException`を継承した例外をスローします。これをcatchすることでエラー時の処理を行うことができます。
@@ -545,10 +550,10 @@ catch (LuaRuntimeException)
 {
     // 実行時例外が発生した際の処理
 }
-catch(OperationCanceledException)
+catch (OperationCanceledException)
 {
     // キャンセルが発生した際の処理
-    // LuaCanceledExceptionならlua内でのキャンセル位置を取得可能
+    // LuaCanceledExceptionを代わりに用いることで、Luaスクリプト内部でのキャンセル位置を取得可能
 }
 ```
 
@@ -597,15 +602,23 @@ state.ModuleLoader = new ResourcesModuleLoader();
 state.ModuleLoader = new AddressablesModuleLoader();
 ```
 
-### Platform抽象化
-LuaPlatformの要素として
-`UnityStandardIO`,
-`UnityApplicationOsEnvironment`が提供されています。
+### UnityStandardIO / UnityApplicationOsEnvironment
+
+Unity向けのLuaPlatformの要素として`UnityStandardIO`、`UnityApplicationOsEnvironment`が提供されています。
+
+```cs
+var platform = new LuaPlatform(
+    FileSystem: new FileSystem(),
+    OsEnvironment: new UnityApplicationOsEnvironment(), // Unity向けのOsEnvironment
+    StandardIO: new UnityStandardIO(), // Unity向けのStandardIO
+    TimeProvider: TimeProvider.System);
+var state = LuaState.Create(platform);
+```
+
+`UnityStandardIO`では`print`などの標準出力が`Debug.Log()`に出力されるようになります。`UnityApplicationOsEnvironment`では環境変数をDictionaryで設定することが可能となり、`os.exit()`で`Application.Quit()`が呼ばれるようになります。
 
-`UnityStandardIO`ではprintなどがDebug.Logに出力されるようになります。
-`UnityApplicationOsEnvironment`では環境変数を辞書で設定でき、`os.exit()`で`Application.Quit()`が呼ばれるようになります。
+これらはあくまで簡易的な実装であるため、実運用では必要に応じて独自実装を作成することを推奨します。
 
-あくまで簡易的なものなので、実際の運用ではあまり推奨されません。
 ## 互換性
 
 Lua-CSharpは.NETとの統合を念頭に設計されているため、C実装とは互換性がない仕様がいくつか存在します。

+ 13 - 12
src/Lua/Lua.csproj

@@ -19,28 +19,29 @@
             <PrivateAssets>all</PrivateAssets>
         </PackageReference>
 
-        <None Include="..\Lua.SourceGenerator\bin\$(Configuration)\netstandard2.0\Lua.SourceGenerator.dll"
-              PackagePath="analyzers\dotnet\cs"
-              Pack="true"
-              Visible="false"/>
+        <None
+            Include="..\Lua.SourceGenerator\bin\$(Configuration)\netstandard2.0\Lua.SourceGenerator.dll"
+            PackagePath="analyzers\dotnet\cs"
+            Pack="true"
+            Visible="false" />
     </ItemGroup>
     <ItemGroup Condition="$(TargetFramework) == 'net6.0' Or $(TargetFramework) == 'netstandard2.1'">
-        <PackageReference Include="Microsoft.Bcl.TimeProvider" Version="8.0.0"/>
+        <PackageReference Include="Microsoft.Bcl.TimeProvider" Version="8.0.0" />
     </ItemGroup>
     <ItemGroup Condition="$(TargetFramework) == 'netstandard2.1'">
-        <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0"/>
+        <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
     </ItemGroup>
 
     <ItemGroup>
-        <None Include="../../Icon.png" Pack="true" PackagePath="/"/>
-        <None Include="..\..\README.md" Pack="true" PackagePath="README.md"/>
-        <EmbeddedResource Include="..\..\LICENSE"/>
+        <None Include="../../Icon.png" Pack="true" PackagePath="/" />
+        <None Include="..\..\README.md" Pack="true" PackagePath="README.md" />
+        <EmbeddedResource Include="..\..\LICENSE" />
     </ItemGroup>
 
     <ItemGroup>
         <ProjectReference Include="..\Lua.SourceGenerator\Lua.SourceGenerator.csproj"
-                          OutputItemType="Analyzer"
-                          ReferenceOutputAssembly="false"/>
+            OutputItemType="Analyzer"
+            ReferenceOutputAssembly="false" />
     </ItemGroup>
 
-</Project>
+</Project>