Skip to content

Commit

Permalink
JCO sample
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelsavara committed Aug 21, 2024
1 parent 5e260b5 commit 34baea9
Show file tree
Hide file tree
Showing 7 changed files with 826 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/mono/sample/wasi/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@

<PropertyGroup>
<OutputPath>bin</OutputPath>
<_ScriptExt Condition="'$(OS)' == 'Windows_NT'">.cmd</_ScriptExt>
<_ScriptExt Condition="'$(OS)' != 'Windows_NT'">.sh</_ScriptExt>
<_Dotnet>$(RepoRoot)dotnet$(_ScriptExt)</_Dotnet>
<_ExeExt Condition="$([MSBuild]::IsOSPlatform('WINDOWS'))">.exe</_ExeExt>
<_Nodejs>$(RepoRoot)node$(_ScriptExt)</_Nodejs>
<_Npm>$(RepoRoot)npm$(_ScriptExt)</_Npm>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<WasiNativeStrip>false</WasiNativeStrip>
Expand Down
25 changes: 21 additions & 4 deletions src/mono/sample/wasi/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<_SampleProjectName>$([System.String]::Copy('$(_SampleProject)').Replace('.csproj',''))</_SampleProjectName>
<_MONO_LOG_LEVEL Condition="false">--env MONO_LOG_LEVEL=debug --env MONO_LOG_MASK=all</_MONO_LOG_LEVEL>
<_DOTNET_WASI_PRINT_EXIT_CODE>--env DOTNET_WASI_PRINT_EXIT_CODE=1</_DOTNET_WASI_PRINT_EXIT_CODE>
<_ExeExt Condition="$([MSBuild]::IsOSPlatform('WINDOWS'))">.exe</_ExeExt>
</PropertyGroup>

<Target Name="BuildSampleInTree"
Expand All @@ -22,9 +21,6 @@
bin/$(Configuration)/AppBundle/dotnet.native.wasm;
">
<PropertyGroup>
<_ScriptExt Condition="'$(OS)' == 'Windows_NT'">.cmd</_ScriptExt>
<_ScriptExt Condition="'$(OS)' != 'Windows_NT'">.sh</_ScriptExt>
<_Dotnet>$(RepoRoot)dotnet$(_ScriptExt)</_Dotnet>
<_AOTFlag Condition="'$(RunAOTCompilation)' != ''">/p:RunAOTCompilation=$(RunAOTCompilation)</_AOTFlag>
</PropertyGroup>
<Exec Command="$(_Dotnet) publish -bl:publish.binlog /p:Configuration=$(Configuration) /p:TargetArchitecture=wasm /p:TargetOS=wasi $(_AOTFlag) $(_SampleProject)" />
Expand Down Expand Up @@ -55,5 +51,26 @@
IgnoreExitCode="true" />
</Target>

<Target Name="TranspileJCO"
Inputs="
Program.cs;
$(_SampleProject);
$(MSBuildProjectFile);
$(TargetFileName);
bin/$(Configuration)/AppBundle/dotnet.native.wasm;
"
Outputs="
bin/$(Configuration)/JCOBundle/dotnet.native.wasm;
">
<Exec Command="npm run transpile"
WorkingDirectory="$(MSBuildProjectDirectory)" />
</Target>


<Target Name="RunSampleWithNodeJS" DependsOnTargets="BuildSampleInTree;TranspileJCO">
<Exec Command="npm run run"
WorkingDirectory="$(MSBuildProjectDirectory)" />
</Target>

<Import Project="$(RepositoryEngineeringDir)testing\wasi-provisioning.targets" />
</Project>
41 changes: 41 additions & 0 deletions src/mono/sample/wasi/jco/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Threading.Tasks;
using System.Threading;
using System.Runtime.CompilerServices;

// keep in sync with src\mono\wasi\testassets\Http.cs
public static class WasiMainWrapper
{
public static async Task<int> MainAsync(string[] args)
{
await Task.Delay(100);

using HttpClient client = new();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("User-Agent", "dotnet WASI unit test");

var query="https://corefx-net-http11.azurewebsites.net/Echo.ashx";
var json = await client.GetStringAsync(query);

Console.WriteLine();
Console.WriteLine("GET "+query);
Console.WriteLine();
Console.WriteLine(json);

return 0;
}

public static int Main(string[] args)
{
return PollWasiEventLoopUntilResolved((Thread)null!, MainAsync(args));

[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "PollWasiEventLoopUntilResolved")]
static extern int PollWasiEventLoopUntilResolved(Thread t, Task<int> mainTask);
}

}
10 changes: 10 additions & 0 deletions src/mono/sample/wasi/jco/Wasi.Jco.Http.Sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<_WasiNeedsHttp>true</_WasiNeedsHttp>
<WasmSingleFileBundle>true</WasmSingleFileBundle>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>

<Target Name="RunSample" DependsOnTargets="RunSampleWithNodeJS" />
</Project>
3 changes: 3 additions & 0 deletions src/mono/sample/wasi/jco/main.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { run } from './Wasi.Jco.Http.Sample.js';

run.run();
Loading

0 comments on commit 34baea9

Please sign in to comment.