Skip to content

Commit 4c3001d

Browse files
[release/7.0] [wasm] Add IMemoryView as exported type (#77397)
* Add IMemoryView as exported type. * IMemoryView extends IDisposable. Co-authored-by: Marek Fišera <[email protected]>
1 parent 12a44e9 commit 4c3001d

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

src/mono/wasm/runtime/dotnet.d.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,35 @@ declare type ModuleAPI = {
236236
declare function createDotnetRuntime(moduleFactory: DotnetModuleConfig | ((api: RuntimeAPI) => DotnetModuleConfig)): Promise<RuntimeAPI>;
237237
declare type CreateDotnetRuntimeType = typeof createDotnetRuntime;
238238

239+
interface IDisposable {
240+
dispose(): void;
241+
get isDisposed(): boolean;
242+
}
243+
interface IMemoryView extends IDisposable {
244+
/**
245+
* copies elements from provided source to the wasm memory.
246+
* target has to have the elements of the same type as the underlying C# array.
247+
* same as TypedArray.set()
248+
*/
249+
set(source: TypedArray, targetOffset?: number): void;
250+
/**
251+
* copies elements from wasm memory to provided target.
252+
* target has to have the elements of the same type as the underlying C# array.
253+
*/
254+
copyTo(target: TypedArray, sourceOffset?: number): void;
255+
/**
256+
* same as TypedArray.slice()
257+
*/
258+
slice(start?: number, end?: number): TypedArray;
259+
get length(): number;
260+
get byteLength(): number;
261+
}
262+
239263
declare global {
240264
function getDotnetRuntime(runtimeId: number): RuntimeAPI | undefined;
241265
}
242266

243267
declare const dotnet: ModuleAPI["dotnet"];
244268
declare const exit: ModuleAPI["exit"];
245269

246-
export { CreateDotnetRuntimeType, DotnetModuleConfig, EmscriptenModule, ModuleAPI, MonoConfig, RuntimeAPI, createDotnetRuntime as default, dotnet, exit };
270+
export { CreateDotnetRuntimeType, DotnetModuleConfig, EmscriptenModule, IMemoryView, ModuleAPI, MonoConfig, RuntimeAPI, createDotnetRuntime as default, dotnet, exit };

src/mono/wasm/runtime/export-types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
import { IMemoryView } from "./marshal";
45
import { createDotnetRuntime, CreateDotnetRuntimeType, DotnetModuleConfig, RuntimeAPI, MonoConfig, ModuleAPI } from "./types";
56
import { EmscriptenModule } from "./types/emscripten";
67

@@ -21,6 +22,6 @@ declare const exit: ModuleAPI["exit"];
2122

2223
export {
2324
EmscriptenModule,
24-
RuntimeAPI, ModuleAPI, DotnetModuleConfig, CreateDotnetRuntimeType, MonoConfig,
25+
RuntimeAPI, ModuleAPI, DotnetModuleConfig, CreateDotnetRuntimeType, MonoConfig, IMemoryView,
2526
dotnet, exit
2627
};

src/mono/wasm/runtime/marshal.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ export const enum MemoryViewType {
376376
Double = 2,
377377
}
378378

379-
abstract class MemoryView implements IMemoryView, IDisposable {
379+
abstract class MemoryView implements IMemoryView {
380380
protected constructor(public _pointer: VoidPtr, public _length: number, public _viewType: MemoryViewType) {
381381
}
382382

@@ -432,7 +432,7 @@ abstract class MemoryView implements IMemoryView, IDisposable {
432432
}
433433
}
434434

435-
export interface IMemoryView {
435+
export interface IMemoryView extends IDisposable {
436436
/**
437437
* copies elements from provided source to the wasm memory.
438438
* target has to have the elements of the same type as the underlying C# array.
@@ -453,7 +453,7 @@ export interface IMemoryView {
453453
get byteLength(): number;
454454
}
455455

456-
export class Span extends MemoryView implements IDisposable {
456+
export class Span extends MemoryView {
457457
private is_disposed = false;
458458
public constructor(pointer: VoidPtr, length: number, viewType: MemoryViewType) {
459459
super(pointer, length, viewType);

0 commit comments

Comments
 (0)