Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mruby build #86

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1160,20 +1160,20 @@ partial class Foo
`[MRubyObject]` works by deserializing `mrb_value` directly to a C# type.
See the table below for the support status of mutually convertible types.

| mruby | C# |
|--------------------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `Integer` | `int`, `uint`, `long`, `ulong`, `shot`, `ushot`, `byte`, `sbyte`, `char` |
| `Float` | `float`, `double`, `decimal` |
| `Array` | `T`, `List<>`, `T[,]`, `T[,]`, `T[,,]`, <br />`Tuple<...>`, `ValueTuple<...>`, <br />, `Stack<>`, `Queue<>`, `LinkedList<>`, `HashSet<>`, `SortedSet<>`, <br />`Collection<>`, `BlockingCollection<>`, <br />`ConcurrentQueue<>`, `ConcurrentStack<>`, `ConcurrentBag<>`, <br />`IEnumerable<>`, `ICollection<>`, `IReadOnlyCollection<>`, <br />`IList<>`, `IReadOnlyList<>`, `ISet<>` |
| `Hash` | `Dictionary<,>`, `SortedDictionary<,>`, `ConcurrentDictionary<,>`, <br />`IDictionary<,>`, `IReadOnlyDictionary<,>` |
| `String` | `string`, `Enum` |
| `[Float, Float]` | `Vector2`, `Resolution` |
| `[Integer, Integer]` | `Vector2Int` |
| `[Float, Float, Float]` | `Vector3` |
| `[Int, Int, Int]` | `Vector3Int` |
| `[Float, Float, Float, Float]` | `Vector4`, `Quaternion`, `Rect`, `Bounds`, `Color` |
| `[Int, Int, Int, Int]` | `RectInt`, `BoundsInt`, `Color32` |
| `nil` | `T?`, `Nullable<T>` |
| mruby | C# |
|--------------------------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `Integer` | `int`, `uint`, `long`, `ulong`, `shot`, `ushot`, `byte`, `sbyte`, `char` |
| `Float` | `float`, `double`, `decimal` |
| `Array` | `T`, `List<>`, `T[,]`, `T[,]`, `T[,,]`, <br />`Tuple<...>`, `ValueTuple<...>`, <br />, `Stack<>`, `Queue<>`, `LinkedList<>`, `HashSet<>`, `SortedSet<>`, <br />`Collection<>`, `BlockingCollection<>`, <br />`ConcurrentQueue<>`, `ConcurrentStack<>`, `ConcurrentBag<>`, <br />`IEnumerable<>`, `ICollection<>`, `IReadOnlyCollection<>`, <br />`IList<>`, `IReadOnlyList<>`, `ISet<>` |
| `Hash` | `Dictionary<,>`, `SortedDictionary<,>`, `ConcurrentDictionary<,>`, <br />`IDictionary<,>`, `IReadOnlyDictionary<,>` |
| `String` | `string`, `Enum`, `byte[]` |
| `[Float, Float]` | `Vector2`, `Resolution` |
| `[Integer, Integer]` | `Vector2Int` |
| `[Float, Float, Float]` | `Vector3` |
| `[Int, Int, Int]` | `Vector3Int` |
| `[Float, Float, Float, Float]` | `Vector4`, `Quaternion`, `Rect`, `Bounds`, `Color` |
| `[Int, Int, Int, Int]` | `RectInt`, `BoundsInt`, `Color32` |
| `nil` | `T?`, `Nullable<T>` |

### MRubyContext

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ public void Load(ReadOnlySpan<byte> rubySource)
EvaluateUnsafe(rubySource).Dispose();
}

public T? Evaluate<T>(string rubySource)
public T? Evaluate<T>(string rubySource, MrbValueSerializerOptions options = null)
{
var bytes = System.Text.Encoding.UTF8.GetBytes(rubySource);
return Evaluate<T>(bytes);
}

public T? Evaluate<T>(ReadOnlySpan<byte> rubySource)
public T? Evaluate<T>(ReadOnlySpan<byte> rubySource, MrbValueSerializerOptions options = null)
{
using var result = EvaluateUnsafe(rubySource);
return MrbValueSerializer.Deserialize<T>(result.RawValue, this);
return MrbValueSerializer.Deserialize<T>(result.RawValue, this, options);
}

public MrbValueHandle EvaluateUnsafe(string rubySource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ unsafe struct MrbNString
[StructLayout(LayoutKind.Explicit)]
public struct MrbValueUnion
{
// Assuming MRB_NO_FLOAT is off, MARB_USE_FLOAT is off.
// Assuming MRB_NO_FLOAT is off, MRB_USE_FLOAT is off.
[FieldOffset(0)]
public double F;

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static FormatterCache()

// well known collections

{ typeof(byte[]), ByteArrayFormatter.Instance },
// { typeof(Memory<byte>), ByteMemoryFormatter.Instance },
// { typeof(ReadOnlyMemory<byte>), ByteReadOnlyMemoryFormatter.Instance },
// { typeof(ReadOnlySequence<byte>), ByteReadOnlySequenceFormatter.Instance },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace VitalRouter.MRuby
{
public class ByteArrayFormatter : IMrbValueFormatter<byte[]?>
{
public static readonly ByteArrayFormatter Instance = new();

public unsafe byte[]? Deserialize(MrbValue mrbValue, MRubyContext context, MrbValueSerializerOptions options)
{
if (mrbValue.IsNil) return null;
if (mrbValue.TT == MrbVtype.MRB_TT_ARRAY)
{
return options.Resolver.GetFormatterWithVerify<byte[]>()
.Deserialize(mrbValue, context, options);
}

var s = NativeMethods.MrbToString(context.DangerousGetPtr(), mrbValue);
var result = new byte[s.Length];
var span = new Span<byte>(s.Bytes, s.Length);
span.CopyTo(result);
return result;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;

namespace VitalRouter.MRuby
{
public class StaticCompositeResolver : IMrbValueFormatterResolver
{
public static readonly StaticCompositeResolver Instance = new();

bool frozen;
readonly List<IMrbValueFormatter> formatters = new();
readonly List<IMrbValueFormatterResolver> resolvers = new();

public StaticCompositeResolver AddFormatters(params IMrbValueFormatter[] formatters)
{
if (frozen)
{
throw new InvalidOperationException("Register must call on startup(before use GetFormatter<T>).");
}

this.formatters.AddRange(formatters);
return this;
}

public StaticCompositeResolver AddResolvers(params IMrbValueFormatterResolver[] resolvers)
{
if (frozen)
{
throw new InvalidOperationException("Register must call on startup(before use GetFormatter<T>).");
}

this.resolvers.AddRange(resolvers);
return this;
}

public IMrbValueFormatter<T>? GetFormatter<T>()
{
return Cache<T>.Formatter;
}

static class Cache<T>
{
public static readonly IMrbValueFormatter<T>? Formatter;

static Cache()
{
Instance.frozen = true;
foreach (var item in Instance.formatters)
{
if (item is IMrbValueFormatter<T> f)
{
Formatter = f;
return;
}
}

foreach (var item in Instance.resolvers)
{
var f = item.GetFormatter<T>();
if (f != null)
{
Formatter = f;
return;
}
}
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 14 additions & 15 deletions src/vitalrouter-mruby/tools.rb → src/vitalrouter-mruby/Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'fileutils'
require 'rake'

UNITY_PLUGINS_DIR = File.expand_path('../../VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins', __FILE__)
MRUBY_ROOT = File.expand_path('../ext/mruby', __FILE__)

PLATFORMS = {
'windows-x64' => 'dll',
Expand All @@ -19,17 +18,24 @@
'wasm' => 'a',
}

def copy_to_unity(build_dir)
build_dir = File.expand_path(build_dir)
unity_plugins_dir = File.expand_path('../../VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins', __FILE__)
task :build, ['target'] do |t, args|
build_config_path = File.expand_path("../build_config.#{args.target}.rb", __FILE__)

Dir.chdir(MRUBY_ROOT) do
sh "MRUBY_CONFIG=#{build_config_path} rake"
end
end

task :sync, ['build_dir'] do |t, args|
build_dir = File.expand_path(args.build_dir)

dylibs = []
Dir.foreach(build_dir) do |dir|
ext = PLATFORMS[dir]
next if ext.nil?

src = File.join(build_dir, dir, 'lib', "libmruby.#{ext}")
dst = File.join(unity_plugins_dir, dir, "VitalRouter.MRuby.Native.#{ext}")
dst = File.join(UNITY_PLUGINS_DIR, dir, "VitalRouter.MRuby.Native.#{ext}")
FileUtils.cp src, dst, verbose: true

if ext == 'dylib'
Expand All @@ -39,15 +45,8 @@ def copy_to_unity(build_dir)
end

if dylibs.any?
universal_dylib = File.join(unity_plugins_dir, 'macos-universal', "VitalRouter.MRUby.Native.dylib")
universal_dylib = File.join(UNITY_PLUGINS_DIR, 'macos-universal', "VitalRouter.MRUby.Native.dylib")
sh %Q{lipo -create #{dylibs.join(' ')} -output #{universal_dylib}}
sh %Q{codesign --sign - --force #{universal_dylib}}
end
end

case ARGV[0]
when 'copy_to_unity'
copy_to_unity(ARGV[1])
else
puts "No such command #{ARGV[0]}"
end
Loading
Loading