From ebd4c97582491eaded89b4628421a551842340c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Mon, 24 Oct 2022 11:21:44 +0200 Subject: [PATCH 1/2] Add IMemoryView as exported type. --- src/mono/wasm/runtime/dotnet.d.ts | 22 +++++++++++++++++++++- src/mono/wasm/runtime/export-types.ts | 3 ++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/mono/wasm/runtime/dotnet.d.ts b/src/mono/wasm/runtime/dotnet.d.ts index a65ce52c7de712..a8bdb5fee7f30d 100644 --- a/src/mono/wasm/runtime/dotnet.d.ts +++ b/src/mono/wasm/runtime/dotnet.d.ts @@ -236,6 +236,26 @@ declare type ModuleAPI = { declare function createDotnetRuntime(moduleFactory: DotnetModuleConfig | ((api: RuntimeAPI) => DotnetModuleConfig)): Promise; declare type CreateDotnetRuntimeType = typeof createDotnetRuntime; +interface IMemoryView { + /** + * copies elements from provided source to the wasm memory. + * target has to have the elements of the same type as the underlying C# array. + * same as TypedArray.set() + */ + set(source: TypedArray, targetOffset?: number): void; + /** + * copies elements from wasm memory to provided target. + * target has to have the elements of the same type as the underlying C# array. + */ + copyTo(target: TypedArray, sourceOffset?: number): void; + /** + * same as TypedArray.slice() + */ + slice(start?: number, end?: number): TypedArray; + get length(): number; + get byteLength(): number; +} + declare global { function getDotnetRuntime(runtimeId: number): RuntimeAPI | undefined; } @@ -243,4 +263,4 @@ declare global { declare const dotnet: ModuleAPI["dotnet"]; declare const exit: ModuleAPI["exit"]; -export { CreateDotnetRuntimeType, DotnetModuleConfig, EmscriptenModule, ModuleAPI, MonoConfig, RuntimeAPI, createDotnetRuntime as default, dotnet, exit }; +export { CreateDotnetRuntimeType, DotnetModuleConfig, EmscriptenModule, IMemoryView, ModuleAPI, MonoConfig, RuntimeAPI, createDotnetRuntime as default, dotnet, exit }; diff --git a/src/mono/wasm/runtime/export-types.ts b/src/mono/wasm/runtime/export-types.ts index ea27e07e62acc8..a1ad9df1e8ba6c 100644 --- a/src/mono/wasm/runtime/export-types.ts +++ b/src/mono/wasm/runtime/export-types.ts @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +import { IMemoryView } from "./marshal"; import { createDotnetRuntime, CreateDotnetRuntimeType, DotnetModuleConfig, RuntimeAPI, MonoConfig, ModuleAPI } from "./types"; import { EmscriptenModule } from "./types/emscripten"; @@ -21,6 +22,6 @@ declare const exit: ModuleAPI["exit"]; export { EmscriptenModule, - RuntimeAPI, ModuleAPI, DotnetModuleConfig, CreateDotnetRuntimeType, MonoConfig, + RuntimeAPI, ModuleAPI, DotnetModuleConfig, CreateDotnetRuntimeType, MonoConfig, IMemoryView, dotnet, exit }; From fa9fccfd9449c032f98da468b6d56623b432cecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Mon, 24 Oct 2022 12:02:29 +0200 Subject: [PATCH 2/2] IMemoryView extends IDisposable. --- src/mono/wasm/runtime/dotnet.d.ts | 6 +++++- src/mono/wasm/runtime/marshal.ts | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/mono/wasm/runtime/dotnet.d.ts b/src/mono/wasm/runtime/dotnet.d.ts index a8bdb5fee7f30d..66b7dd2ebb7266 100644 --- a/src/mono/wasm/runtime/dotnet.d.ts +++ b/src/mono/wasm/runtime/dotnet.d.ts @@ -236,7 +236,11 @@ declare type ModuleAPI = { declare function createDotnetRuntime(moduleFactory: DotnetModuleConfig | ((api: RuntimeAPI) => DotnetModuleConfig)): Promise; declare type CreateDotnetRuntimeType = typeof createDotnetRuntime; -interface IMemoryView { +interface IDisposable { + dispose(): void; + get isDisposed(): boolean; +} +interface IMemoryView extends IDisposable { /** * copies elements from provided source to the wasm memory. * target has to have the elements of the same type as the underlying C# array. diff --git a/src/mono/wasm/runtime/marshal.ts b/src/mono/wasm/runtime/marshal.ts index f20a620c5fb76b..c03ef13aea778c 100644 --- a/src/mono/wasm/runtime/marshal.ts +++ b/src/mono/wasm/runtime/marshal.ts @@ -376,7 +376,7 @@ export const enum MemoryViewType { Double = 2, } -abstract class MemoryView implements IMemoryView, IDisposable { +abstract class MemoryView implements IMemoryView { protected constructor(public _pointer: VoidPtr, public _length: number, public _viewType: MemoryViewType) { } @@ -432,7 +432,7 @@ abstract class MemoryView implements IMemoryView, IDisposable { } } -export interface IMemoryView { +export interface IMemoryView extends IDisposable { /** * copies elements from provided source to the wasm memory. * target has to have the elements of the same type as the underlying C# array. @@ -453,7 +453,7 @@ export interface IMemoryView { get byteLength(): number; } -export class Span extends MemoryView implements IDisposable { +export class Span extends MemoryView { private is_disposed = false; public constructor(pointer: VoidPtr, length: number, viewType: MemoryViewType) { super(pointer, length, viewType);