Skip to content

Wasm marshaler API for user-defined types; migrate Uri and DateTime to new API #47640

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

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 5 additions & 0 deletions eng/testing/tests.wasm.targets
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@
<_WasmVFSFilesToCopy Include="@(WasmFilesToIncludeInFileSystem)" />
<_WasmVFSFilesToCopy TargetPath="%(FileName)%(Extension)" Condition="'%(TargetPath)' == ''" />

<!-- the custom marshaler support requires the .csproj to include a list of custom marshalers and types
that will get included in the generated mono-config so that the runtime knows about them. Without this
any application or test relying on custom marshalers will break -->
<_WasmItemsToPass Include="@(WasmMarshaledType)" OriginalItemName__="WasmMarshaledType" />

<!-- Example of passing items to the project

<_WasmItemsToPass Include="@(BundleFiles)" OriginalItemName__="BundleFiles" ConditionToUse__="'$(Foo)' != 'true'" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<linker>
<assembly fullname="System.Private.Runtime.InteropServices.JavaScript">
<!-- We need to preserve these marshalers during the library build, because whether they are used is not known -->
<!-- until end users' applications are built. At that point they can be linked out -->
<type fullname="System.Runtime.InteropServices.JavaScript.UriMarshaler" />
<type fullname="System.Runtime.InteropServices.JavaScript.DateTimeMarshaler" />
<type fullname="System.Runtime.InteropServices.JavaScript.DateTimeOffsetMarshaler" />
</assembly>
</linker>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<linker>
<assembly fullname="System.Private.Runtime.InteropServices.JavaScript">
<!-- HACK: Hard-coded preserve for all custom marshalers until we implement the msbuild task to generate this on-demand -->
<type fullname="System.Runtime.InteropServices.JavaScript.UriMarshaler" />
<type fullname="System.Runtime.InteropServices.JavaScript.DateTimeMarshaler" />
<type fullname="System.Runtime.InteropServices.JavaScript.DateTimeOffsetMarshaler" />
</assembly>
</linker>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup Condition="'$(TargetsBrowser)' == 'true'">
<Compile Include="$(CommonPath)Interop\Browser\Interop.Runtime.cs" Link="Common\Interop\Browser\Interop.Runtime.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Codegen.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Runtime.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Runtime.CS.Owned.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Runtime.JS.Owned.cs" />
Expand All @@ -31,6 +32,10 @@
<Compile Include="System\Runtime\InteropServices\JavaScript\HostObject.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Function.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\JSObject.References.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\DateTimeMarshaler.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\DateTimeOffsetMarshaler.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\UriMarshaler.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Types.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Runtime" />
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace System.Runtime.InteropServices.JavaScript
{
public static class DateTimeMarshaler
{
public static string JavaScriptToInterchangeTransform => @"
switch (typeof (value)) {
case 'number':
return value;
default:
if (value instanceof Date) {
return value.valueOf();
} else
throw new Error('Value must be a number (msecs since unix epoch), or a Date');
}
";
public static string InterchangeToJavaScriptTransform => "return new Date(value)";

public static DateTime FromJavaScript (double msecsSinceEpoch)
{
return DateTimeOffset.FromUnixTimeMilliseconds((long)msecsSinceEpoch).UtcDateTime;
}

public static double ToJavaScript (in DateTime dt)
{
return (double)new DateTimeOffset(dt).ToUnixTimeMilliseconds();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace System.Runtime.InteropServices.JavaScript
{
public static class DateTimeOffsetMarshaler
{
public static string JavaScriptToInterchangeTransform => DateTimeMarshaler.JavaScriptToInterchangeTransform;
public static string InterchangeToJavaScriptTransform => DateTimeMarshaler.InterchangeToJavaScriptTransform;

public static DateTimeOffset FromJavaScript (double msecsSinceEpoch)
{
return DateTimeOffset.FromUnixTimeMilliseconds((long)msecsSinceEpoch);
}

public static double ToJavaScript (in DateTimeOffset dto)
{
return (double)dto.ToUnixTimeMilliseconds();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal void AddInFlight()
InFlightCounter++;
if (InFlightCounter == 1)
{
Debug.Assert(InFlight == null);
Debug.Assert(InFlight == null, "InFlight == null");
InFlight = GCHandle.Alloc(this, GCHandleType.Normal);
}
}
Expand All @@ -61,12 +61,12 @@ internal void ReleaseInFlight()
{
lock (this)
{
Debug.Assert(InFlightCounter != 0);
Debug.Assert(InFlightCounter != 0, "InFlightCounter != 0");

InFlightCounter--;
if (InFlightCounter == 0)
{
Debug.Assert(InFlight.HasValue);
Debug.Assert(InFlight.HasValue, "InFlight.HasValue");
InFlight.Value.Free();
InFlight = null;
}
Expand Down
Loading