Skip to content

Commit

Permalink
cleanup: update MemoryView to extend DataView
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Aug 26, 2024
1 parent 7d52537 commit 831eb94
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ with low-level memory access.

```ts
// Provides access to data in Extism memory
export class MemoryView {
export class MemoryView extends DataView {
...

// Returns true if the underlying memory handle is empty or undefined.
Expand Down
Binary file modified examples/basic/test.wasm
Binary file not shown.
69 changes: 49 additions & 20 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ interface MemoryHandle {
readString(): string;
}

const NULL = new ArrayBuffer(0);

// Provides access to data in Extism memory
export class MemoryView {
memory: MemoryHandle | null = null;
export class MemoryView extends DataView {
static #decoder = new TextDecoder();

constructor(memory: MemoryHandle | undefined) {
if (memory){
this.memory = memory;
}
constructor(memory?: MemoryHandle) {
super(memory ? memory.readBytes() : NULL)
}

// Returns true if the underlying memory handle is empty or undefined.
// Returns true when the underlying memory handle is empty or undefined.
isEmpty(): boolean {
return this.memory === null || this.memory.offset === 0;
return this.buffer.byteLength === 0;
}

// Get the JSON representation of a value stored in Extism memory
Expand All @@ -38,23 +38,52 @@ export class MemoryView {

// Get the string representation of a value stored in Extism memory
text(): string {
if (this.memory === null){
return "";
}
return this.memory.readString();
return MemoryView.#decoder.decode(this.buffer)
}

// Read bytes from Extism memory into an ArrayBuffer
arrayBuffer(): ArrayBuffer {
if (this.memory === null){
return new ArrayBuffer(0);
}
return this.memory.readBytes();
arrayBuffer(): ArrayBufferLike {
return this.buffer;
}

setInt8(_byteOffset: number, _value: number): void {
throw new Error('Cannot set values on MemoryView');
}

setInt16(_byteOffset: number, _value: number, _littleEndian?: boolean): void {
throw new Error('Cannot set values on MemoryView');
}

setInt32(_byteOffset: number, _value: number, _littleEndian?: boolean): void {
throw new Error('Cannot set values on MemoryView');
}

setUint8(_byteOffset: number, _value: number): void {
throw new Error('Cannot set values on MemoryView');
}

setUint16(_byteOffset: number, _value: number, _littleEndian?: boolean): void {
throw new Error('Cannot set values on MemoryView');
}

setUint32(_byteOffset: number, _value: number, _littleEndian?: boolean): void {
throw new Error('Cannot set values on MemoryView');
}

setFloat32(_byteOffset: number, _value: number, _littleEndian?: boolean): void {
throw new Error('Cannot set values on MemoryView');
}

setFloat64(_byteOffset: number, _value: number, _littleEndian?: boolean): void {
throw new Error('Cannot set values on MemoryView');
}

setBigInt64(_byteOffset: number, _value: bigint, _littleEndian?: boolean): void {
throw new Error('Cannot set values on MemoryView');
}

// Return the low-level memory handle
memoryHandle(): MemoryHandle | null {
return this.memory;
setBigUint64(_byteOffset: number, _value: bigint, _littleEndian?: boolean): void {
throw new Error('Cannot set values on MemoryView');
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "XTP test harness",
"main": "index.ts",
"scripts": {
"check": "tsc index.ts --noEmit"
"check": "tsc index.ts --noEmit --target es6"
},
"author": "Dylibso <[email protected]>",
"license": "BSD-3-Clause",
Expand Down

0 comments on commit 831eb94

Please sign in to comment.