Skip to content

Commit 66038f1

Browse files
committed
feat(wasm): Add MemoryManager support
1 parent aa3e6e9 commit 66038f1

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

src/Uno.UI/WasmScripts/Uno.UI.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,11 @@ declare namespace Uno.Storage.Streams {
11041104
static truncateAsync(streamId: string, length: number): Promise<string>;
11051105
}
11061106
}
1107+
declare namespace Windows.System {
1108+
class MemoryManager {
1109+
static getAppMemoryUsage(): any;
1110+
}
1111+
}
11071112
interface Navigator {
11081113
wakeLock: WakeLock;
11091114
}

src/Uno.UI/WasmScripts/Uno.UI.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4038,6 +4038,24 @@ var Uno;
40384038
})(Streams = Storage.Streams || (Storage.Streams = {}));
40394039
})(Storage = Uno.Storage || (Uno.Storage = {}));
40404040
})(Uno || (Uno = {}));
4041+
var Windows;
4042+
(function (Windows) {
4043+
var System;
4044+
(function (System) {
4045+
class MemoryManager {
4046+
static getAppMemoryUsage() {
4047+
if (typeof Module === "object") {
4048+
// Returns an approximate memory usage for the current wasm module.
4049+
// Initial buffer size is determine by the initial wasm memory defined in
4050+
// emscripten.
4051+
return Module.HEAPU8.length;
4052+
}
4053+
return 0;
4054+
}
4055+
}
4056+
System.MemoryManager = MemoryManager;
4057+
})(System = Windows.System || (Windows.System = {}));
4058+
})(Windows || (Windows = {}));
40414059
var WakeLockType;
40424060
(function (WakeLockType) {
40434061
WakeLockType["screen"] = "screen";
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Windows.System {
2+
3+
export class MemoryManager {
4+
5+
static getAppMemoryUsage() {
6+
if (typeof Module === "object") {
7+
8+
// Returns an approximate memory usage for the current wasm module.
9+
// Initial buffer size is determine by the initial wasm memory defined in
10+
// emscripten.
11+
return (<any>Module).HEAPU8.length;
12+
}
13+
return 0;
14+
}
15+
}
16+
}

src/Uno.UWP/Generated/3.0.0.0/Windows.System/MemoryManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Windows.System
77
#endif
88
public static partial class MemoryManager
99
{
10-
#if false || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
10+
#if false || __IOS__ || NET461 || false || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
1111
[global::Uno.NotImplemented("__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
1212
public static ulong AppMemoryUsage
1313
{
@@ -27,7 +27,7 @@ public static ulong AppMemoryUsage
2727
}
2828
}
2929
#endif
30-
#if false || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
30+
#if false || __IOS__ || NET461 || false || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
3131
[global::Uno.NotImplemented("__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
3232
public static ulong AppMemoryUsageLimit
3333
{
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Globalization;
3+
using Uno.Foundation;
4+
5+
namespace Windows.System
6+
{
7+
public partial class MemoryManager
8+
{
9+
private const string JsType = "Windows.System.MemoryManager";
10+
11+
public static ulong AppMemoryUsage
12+
{
13+
get
14+
{
15+
if(ulong.TryParse(WebAssemblyRuntime.InvokeJS(
16+
$"{JsType}.getAppMemoryUsage()"),
17+
NumberStyles.Any,
18+
CultureInfo.InvariantCulture, out var value))
19+
{
20+
return value;
21+
}
22+
23+
throw new Exception($"getAppMemoryUsage returned an unsupported value");
24+
}
25+
}
26+
27+
public static ulong AppMemoryUsageLimit
28+
{
29+
get
30+
{
31+
if (Environment.GetEnvironmentVariable("UNO_BOOTSTRAP_EMSCRIPTEN_MAXIMUM_MEMORY") == "4GB")
32+
{
33+
return 4ul * 1024 * 1024 * 1024;
34+
}
35+
36+
return 2ul * 1024 * 1024 * 1024;
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)