[Wasm] Add simple browser sample (#40460)
authorSteve Pfister <steveisok@users.noreply.github.com>
Fri, 7 Aug 2020 19:46:47 +0000 (15:46 -0400)
committerGitHub <noreply@github.com>
Fri, 7 Aug 2020 19:46:47 +0000 (15:46 -0400)
src/mono/netcore/sample/wasm/browser/Makefile [new file with mode: 0644]
src/mono/netcore/sample/wasm/browser/Program.cs [new file with mode: 0644]
src/mono/netcore/sample/wasm/browser/WasmSample.csproj [new file with mode: 0644]
src/mono/netcore/sample/wasm/browser/index.html [new file with mode: 0644]
src/mono/netcore/sample/wasm/browser/runtime.js [new file with mode: 0644]
src/mono/netcore/sample/wasm/browser/server.py [new file with mode: 0644]
src/mono/netcore/sample/wasm/console/Makefile [moved from src/mono/netcore/sample/wasm/Makefile with 95% similarity]
src/mono/netcore/sample/wasm/console/Program.cs [moved from src/mono/netcore/sample/wasm/Program.cs with 100% similarity]
src/mono/netcore/sample/wasm/console/WasmSample.csproj [moved from src/mono/netcore/sample/wasm/WasmSample.csproj with 100% similarity]

diff --git a/src/mono/netcore/sample/wasm/browser/Makefile b/src/mono/netcore/sample/wasm/browser/Makefile
new file mode 100644 (file)
index 0000000..d5227ac
--- /dev/null
@@ -0,0 +1,16 @@
+TOP=../../../../../..
+
+DOTNET_Q_ARGS=--nologo -v:q -consoleloggerparameters:NoSummary
+
+CONFIG?=Release
+
+all: build
+
+build:
+       $(TOP)/.dotnet/dotnet build $(DOTNET_Q_ARGS) /p:TargetArchitecture=wasm /p:TargetOS=Browser /p:Configuration=$(CONFIG) WasmSample.csproj
+
+clean:
+       rm -rf bin
+
+run:
+       cd bin/$(CONFIG)/publish && python server.py
\ No newline at end of file
diff --git a/src/mono/netcore/sample/wasm/browser/Program.cs b/src/mono/netcore/sample/wasm/browser/Program.cs
new file mode 100644 (file)
index 0000000..36e09e4
--- /dev/null
@@ -0,0 +1,21 @@
+// -*- indent-tabs-mode: nil -*-
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System;
+
+namespace Sample
+{
+    public class Test
+    {
+        public static void Main(String[] args)
+        {
+            Console.WriteLine ("Hello, World!");
+        }
+
+        public static int TestMeaning()
+        {
+            return 42;
+        }
+    }
+}
diff --git a/src/mono/netcore/sample/wasm/browser/WasmSample.csproj b/src/mono/netcore/sample/wasm/browser/WasmSample.csproj
new file mode 100644 (file)
index 0000000..e268098
--- /dev/null
@@ -0,0 +1,57 @@
+<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="BuildApp">
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <OutputPath>bin</OutputPath>
+    <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
+    <TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
+    <TargetArchitecture>wasm</TargetArchitecture>
+    <TargetOS>Browser</TargetOS>
+    <MicrosoftNetCoreAppRuntimePackDir>$(ArtifactsBinDir)microsoft.netcore.app.runtime.browser-wasm\$(Configuration)\runtimes\browser-wasm\</MicrosoftNetCoreAppRuntimePackDir>
+    <BuildDir>$(MSBuildThisFileDirectory)obj\$(Configuration)\wasm</BuildDir>
+    <AppDir>$(MSBuildThisFileDirectory)bin\$(Configuration)\publish</AppDir>
+  </PropertyGroup>
+
+  <Target Name="RebuildWasmAppBuilder">
+    <ItemGroup>
+      <WasmAppBuildProject Include="$(RepoTasksDir)mobile.tasks\WasmAppBuilder\WasmAppBuilder.csproj" />
+    </ItemGroup>
+
+    <MSBuild Projects="@(WasmAppBuildProject)"
+             Properties="Configuration=$(Configuration);MSBuildRestoreSessionId=$([System.Guid]::NewGuid())"
+             Targets="Restore"/>
+
+    <MSBuild Projects="@(WasmAppBuildProject)"
+             Properties="Configuration=$(Configuration)"
+             Targets="Build;Publish"/>
+  </Target>
+
+  <UsingTask TaskName="WasmAppBuilder" 
+             AssemblyFile="$(ArtifactsBinDir)WasmAppBuilder\$(Configuration)\$(NetCoreAppCurrent)\publish\WasmAppBuilder.dll"/>
+
+  <Target Name="BuildApp" DependsOnTargets="RebuildWasmAppBuilder;Build">
+    <ItemGroup>
+      <AssemblySearchPaths Include="bin"/>
+      <AssemblySearchPaths Include="$(MicrosoftNetCoreAppRuntimePackDir)native"/>
+      <AssemblySearchPaths Include="$(MicrosoftNetCoreAppRuntimePackDir)lib\$(NetCoreAppCurrent)"/>
+    </ItemGroup>
+    <WasmAppBuilder
+      AppDir="$(AppDir)"
+      ExtraAssemblies="$(MicrosoftNetCoreAppRuntimePackDir)lib\$(NetCoreAppCurrent)\System.Runtime.InteropServices.JavaScript.dll"
+      MicrosoftNetCoreAppRuntimePackDir="$(MicrosoftNetCoreAppRuntimePackDir)"
+      MainAssembly="bin\WasmSample.dll"
+      MainJS="runtime.js"
+      AssemblySearchPaths="@(AssemblySearchPaths)"/>
+    <Exec Command="chmod a+x $(AppDir)/run-v8.sh" />
+    <Copy SourceFiles="bin\index.html;bin\server.py" DestinationFolder="$(AppDir)" />
+  </Target>
+
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+    <Content Include="index.html">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </Content>
+    <Content Include="server.py">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </Content>
+  </ItemGroup>
+</Project>
diff --git a/src/mono/netcore/sample/wasm/browser/index.html b/src/mono/netcore/sample/wasm/browser/index.html
new file mode 100644 (file)
index 0000000..4cf3a1e
--- /dev/null
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<!--  Licensed to the .NET Foundation under one or more agreements. -->
+<!-- The .NET Foundation licenses this file to you under the MIT license. -->
+<html>
+  <head>
+    <title>TESTS</title>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  </head>
+  <body>
+    <h3 id="header">Wasm Browser Sample</h3>
+    Result from Sample.Test.TestMeaaning: <span id="out"></span>
+    <script type='text/javascript'>
+      var App = {
+        init: function () {
+          var ret = BINDING.call_static_method("[WasmSample] Sample.Test:TestMeaning", []);
+          document.getElementById("out").innerHTML = ret;
+          console.log ("ready");
+        },
+      };
+    </script>
+    <script type="text/javascript" src="mono-config.js"></script>
+    <script type="text/javascript" src="runtime.js"></script>
+
+    <script defer src="dotnet.js"></script>
+
+  </body>
+</html>
\ No newline at end of file
diff --git a/src/mono/netcore/sample/wasm/browser/runtime.js b/src/mono/netcore/sample/wasm/browser/runtime.js
new file mode 100644 (file)
index 0000000..7db3cb4
--- /dev/null
@@ -0,0 +1,15 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+var Module = { 
+    onRuntimeInitialized: function () {
+        config.loaded_cb = function () {
+            App.init ();
+        };
+        config.fetch_file_cb = function (asset) {
+            return fetch (asset, { credentials: 'same-origin' });
+        }
+
+        MONO.mono_load_runtime_and_bcl_args (config);
+    },
+};
diff --git a/src/mono/netcore/sample/wasm/browser/server.py b/src/mono/netcore/sample/wasm/browser/server.py
new file mode 100644 (file)
index 0000000..ab1e3ed
--- /dev/null
@@ -0,0 +1,37 @@
+# Licensed to the .NET Foundation under one or more agreements.
+# The .NET Foundation licenses this file to you under the MIT license.
+
+import sys
+
+if sys.version_info[0] == 2:
+
+    import SimpleHTTPServer
+    import SocketServer
+
+    PORT = 8000
+
+    class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
+        pass
+
+    Handler.extensions_map['.wasm'] = 'application/wasm'
+
+    httpd = SocketServer.TCPServer(("", PORT), Handler)
+
+    print ("python 2 serving at port", PORT)
+    httpd.serve_forever()
+
+
+if sys.version_info[0] == 3:
+    
+    import http.server
+    import socketserver
+
+    PORT = 8000
+
+    Handler = http.server.SimpleHTTPRequestHandler
+    Handler.extensions_map['.wasm'] = 'application/wasm'
+
+    with socketserver.TCPServer(("", PORT), Handler) as httpd:
+        print("python 3 serving at port", PORT)
+        httpd.serve_forever()
+
similarity index 95%
rename from src/mono/netcore/sample/wasm/Makefile
rename to src/mono/netcore/sample/wasm/console/Makefile
index 16d37d4..40cbf04 100644 (file)
@@ -1,4 +1,4 @@
-TOP=../../../../..
+TOP=../../../../../..
 
 DOTNET_Q_ARGS=--nologo -v:q -consoleloggerparameters:NoSummary