-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supported ArrayBuffer serialization.
- Loading branch information
Showing
14 changed files
with
526 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//////////////////////////////////////////////////////////////////////////// | ||
// | ||
// DupeNukem - WebView attachable full-duplex asynchronous interoperable | ||
// independent messaging library between .NET and JavaScript. | ||
// | ||
// Copyright (c) Kouji Matsui (@kozy_kekyo, @[email protected]) | ||
// | ||
// Licensed under Apache-v2: https://opensource.org/licenses/Apache-2.0 | ||
// | ||
//////////////////////////////////////////////////////////////////////////// | ||
|
||
using System; | ||
using System.Threading; | ||
using Newtonsoft.Json; | ||
|
||
namespace DupeNukem.Internal; | ||
|
||
internal sealed class ClosureConverter : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) => | ||
typeof(Delegate).IsAssignableFrom(objectType); | ||
|
||
public override object? ReadJson( | ||
JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) | ||
{ | ||
ConverterContext.AssertValidState(); | ||
|
||
var typedValue = serializer.Deserialize<TypedValue>(reader); | ||
if (typedValue.Type == TypedValueTypes.Closure) | ||
{ | ||
var name = typedValue.Body.ToObject<string>(serializer)!; | ||
if (name.StartsWith("__peerClosures__.closure_$")) | ||
{ | ||
return ConverterContext.Current.RegisterPeerClosure(name, objectType); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public override void WriteJson( | ||
JsonWriter writer, object? value, JsonSerializer serializer) | ||
{ | ||
ConverterContext.AssertValidState(); | ||
|
||
if (value is Delegate dlg) | ||
{ | ||
var name = ConverterContext.Current.RegisterHostClosure(dlg); | ||
var typedValue = new TypedValue(TypedValueTypes.Closure, name); | ||
serializer.Serialize(writer, typedValue); | ||
} | ||
else | ||
{ | ||
writer.WriteNull(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
//////////////////////////////////////////////////////////////////////////// | ||
// | ||
// DupeNukem - WebView attachable full-duplex asynchronous interoperable | ||
// independent messaging library between .NET and JavaScript. | ||
// | ||
// Copyright (c) Kouji Matsui (@kozy_kekyo, @[email protected]) | ||
// | ||
// Licensed under Apache-v2: https://opensource.org/licenses/Apache-2.0 | ||
// | ||
//////////////////////////////////////////////////////////////////////////// | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
|
||
namespace DupeNukem.Internal; | ||
|
||
internal static class ConverterContext | ||
{ | ||
// JsonSerializer cannot pass application-specific context information | ||
// to the Converter during serialization runs. | ||
// The ConverterContext class is used to identify the corresponding Messenger instance | ||
// during serialization. | ||
|
||
private sealed class MessengerContext | ||
{ | ||
private IMessenger? messenger; | ||
private int count; | ||
|
||
public IMessenger Current | ||
{ | ||
get | ||
{ | ||
Debug.Assert(this.messenger != null); | ||
Debug.Assert(this.count >= 1); | ||
return this.messenger!; | ||
} | ||
} | ||
|
||
public void Enter(IMessenger messenger) | ||
{ | ||
if (this.count <= 0) | ||
{ | ||
Debug.Assert(this.messenger == null); | ||
this.messenger = messenger; | ||
this.count = 1; | ||
} | ||
else | ||
{ | ||
Debug.Assert(this.messenger == messenger); | ||
this.count++; | ||
} | ||
} | ||
|
||
public void Exit(IMessenger messenger) | ||
{ | ||
Debug.Assert(this.messenger == messenger); | ||
Debug.Assert(this.count >= 1); | ||
this.count--; | ||
if (this.count <= 0) | ||
{ | ||
this.messenger = null!; | ||
this.count = 0; | ||
} | ||
} | ||
} | ||
|
||
private static readonly ThreadLocal<MessengerContext> messengers = | ||
new(() => new MessengerContext()); | ||
|
||
public static Messenger Current | ||
{ | ||
get | ||
{ | ||
AssertValidState(); | ||
return (Messenger)messengers.Value!.Current; | ||
} | ||
} | ||
|
||
[Conditional("DEBUG")] | ||
public static void AssertValidState() => | ||
Debug.Assert( | ||
messengers.Value!.Current is Messenger, | ||
"Invalid state: Not called correctly."); | ||
|
||
public static void Enter(IMessenger messenger) => | ||
messengers.Value!.Enter(messenger); | ||
|
||
public static void Exit(IMessenger messenger) => | ||
messengers.Value!.Exit(messenger); | ||
|
||
public static void Run(IMessenger messenger, Action action) | ||
{ | ||
messengers.Value!.Enter(messenger); | ||
try | ||
{ | ||
action(); | ||
} | ||
finally | ||
{ | ||
messengers.Value!.Exit(messenger); | ||
} | ||
} | ||
|
||
public static T Run<T>(IMessenger messenger, Func<T> action) | ||
{ | ||
messengers.Value!.Enter(messenger); | ||
try | ||
{ | ||
return action(); | ||
} | ||
finally | ||
{ | ||
messengers.Value!.Exit(messenger); | ||
} | ||
} | ||
} |
67 changes: 0 additions & 67 deletions
67
DupeNukem.Core/Internal/DeserializingRegisteredObjectRegistry.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.