Skip to content

Commit 34baea9

Browse files
committed
JCO sample
1 parent 5e260b5 commit 34baea9

File tree

7 files changed

+826
-4
lines changed

7 files changed

+826
-4
lines changed

src/mono/sample/wasi/Directory.Build.props

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515

1616
<PropertyGroup>
1717
<OutputPath>bin</OutputPath>
18+
<_ScriptExt Condition="'$(OS)' == 'Windows_NT'">.cmd</_ScriptExt>
19+
<_ScriptExt Condition="'$(OS)' != 'Windows_NT'">.sh</_ScriptExt>
20+
<_Dotnet>$(RepoRoot)dotnet$(_ScriptExt)</_Dotnet>
21+
<_ExeExt Condition="$([MSBuild]::IsOSPlatform('WINDOWS'))">.exe</_ExeExt>
22+
<_Nodejs>$(RepoRoot)node$(_ScriptExt)</_Nodejs>
23+
<_Npm>$(RepoRoot)npm$(_ScriptExt)</_Npm>
1824
</PropertyGroup>
1925
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
2026
<WasiNativeStrip>false</WasiNativeStrip>

src/mono/sample/wasi/Directory.Build.targets

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<_SampleProjectName>$([System.String]::Copy('$(_SampleProject)').Replace('.csproj',''))</_SampleProjectName>
99
<_MONO_LOG_LEVEL Condition="false">--env MONO_LOG_LEVEL=debug --env MONO_LOG_MASK=all</_MONO_LOG_LEVEL>
1010
<_DOTNET_WASI_PRINT_EXIT_CODE>--env DOTNET_WASI_PRINT_EXIT_CODE=1</_DOTNET_WASI_PRINT_EXIT_CODE>
11-
<_ExeExt Condition="$([MSBuild]::IsOSPlatform('WINDOWS'))">.exe</_ExeExt>
1211
</PropertyGroup>
1312

1413
<Target Name="BuildSampleInTree"
@@ -22,9 +21,6 @@
2221
bin/$(Configuration)/AppBundle/dotnet.native.wasm;
2322
">
2423
<PropertyGroup>
25-
<_ScriptExt Condition="'$(OS)' == 'Windows_NT'">.cmd</_ScriptExt>
26-
<_ScriptExt Condition="'$(OS)' != 'Windows_NT'">.sh</_ScriptExt>
27-
<_Dotnet>$(RepoRoot)dotnet$(_ScriptExt)</_Dotnet>
2824
<_AOTFlag Condition="'$(RunAOTCompilation)' != ''">/p:RunAOTCompilation=$(RunAOTCompilation)</_AOTFlag>
2925
</PropertyGroup>
3026
<Exec Command="$(_Dotnet) publish -bl:publish.binlog /p:Configuration=$(Configuration) /p:TargetArchitecture=wasm /p:TargetOS=wasi $(_AOTFlag) $(_SampleProject)" />
@@ -55,5 +51,26 @@
5551
IgnoreExitCode="true" />
5652
</Target>
5753

54+
<Target Name="TranspileJCO"
55+
Inputs="
56+
Program.cs;
57+
$(_SampleProject);
58+
$(MSBuildProjectFile);
59+
$(TargetFileName);
60+
bin/$(Configuration)/AppBundle/dotnet.native.wasm;
61+
"
62+
Outputs="
63+
bin/$(Configuration)/JCOBundle/dotnet.native.wasm;
64+
">
65+
<Exec Command="npm run transpile"
66+
WorkingDirectory="$(MSBuildProjectDirectory)" />
67+
</Target>
68+
69+
70+
<Target Name="RunSampleWithNodeJS" DependsOnTargets="BuildSampleInTree;TranspileJCO">
71+
<Exec Command="npm run run"
72+
WorkingDirectory="$(MSBuildProjectDirectory)" />
73+
</Target>
74+
5875
<Import Project="$(RepositoryEngineeringDir)testing\wasi-provisioning.targets" />
5976
</Project>

src/mono/sample/wasi/jco/Program.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Net.Http.Headers;
6+
using System.Net.Http;
7+
using System.Threading.Tasks;
8+
using System.Threading;
9+
using System.Runtime.CompilerServices;
10+
11+
// keep in sync with src\mono\wasi\testassets\Http.cs
12+
public static class WasiMainWrapper
13+
{
14+
public static async Task<int> MainAsync(string[] args)
15+
{
16+
await Task.Delay(100);
17+
18+
using HttpClient client = new();
19+
client.DefaultRequestHeaders.Accept.Clear();
20+
client.DefaultRequestHeaders.Add("User-Agent", "dotnet WASI unit test");
21+
22+
var query="https://corefx-net-http11.azurewebsites.net/Echo.ashx";
23+
var json = await client.GetStringAsync(query);
24+
25+
Console.WriteLine();
26+
Console.WriteLine("GET "+query);
27+
Console.WriteLine();
28+
Console.WriteLine(json);
29+
30+
return 0;
31+
}
32+
33+
public static int Main(string[] args)
34+
{
35+
return PollWasiEventLoopUntilResolved((Thread)null!, MainAsync(args));
36+
37+
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "PollWasiEventLoopUntilResolved")]
38+
static extern int PollWasiEventLoopUntilResolved(Thread t, Task<int> mainTask);
39+
}
40+
41+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
4+
<_WasiNeedsHttp>true</_WasiNeedsHttp>
5+
<WasmSingleFileBundle>true</WasmSingleFileBundle>
6+
<InvariantGlobalization>true</InvariantGlobalization>
7+
</PropertyGroup>
8+
9+
<Target Name="RunSample" DependsOnTargets="RunSampleWithNodeJS" />
10+
</Project>

src/mono/sample/wasi/jco/main.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { run } from './Wasi.Jco.Http.Sample.js';
2+
3+
run.run();

0 commit comments

Comments
 (0)