-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
266 lines (225 loc) · 7.73 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
declare const Host: {
inputBytes: () => void;
inputString: () => void;
outputBytes: (output: any) => void;
outputString: (output: any) => void;
};
// @ts-ignore
const { call, time, assert, group, reset, mock_input } = Host.getFunctions();
interface MemoryHandle {
offset: number;
length: number;
free(): void;
readBytes(): ArrayBuffer;
readString(): string;
}
const NULL = new ArrayBuffer(0);
// Provides access to data in Extism memory
export class MemoryView extends DataView {
// @ts-ignore: TextDecoder
static #decoder = new TextDecoder();
constructor(memory?: MemoryHandle) {
super(memory ? memory.readBytes() : NULL);
}
// Returns true when the underlying memory handle is empty or undefined.
isEmpty(): boolean {
return this.buffer.byteLength === 0;
}
// Get the JSON representation of a value stored in Extism memory
json(): any {
try {
return JSON.parse(this.text());
} catch (e) {
const text = this.text();
throw new Error(`Failed to parse JSON: ${text}. Error: ${e}`);
}
}
// Get the string representation of a value stored in Extism memory
text(): string {
return MemoryView.#decoder.decode(this.buffer);
}
// Read bytes from Extism memory into an ArrayBuffer
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");
}
setBigUint64(
_byteOffset: number,
_value: bigint,
_littleEndian?: boolean,
): void {
throw new Error("Cannot set values on MemoryView");
}
}
type Input = string | ArrayBuffer | object | undefined;
Host.inputBytes = function () {
throw "Tests do not accept any input";
};
Host.inputString = Host.inputBytes;
Host.outputBytes = function (n: any) {
throw "Tests should not return any output";
};
Host.outputString = Host.outputBytes;
function convertInput(input: Input): MemoryHandle {
const ty = typeof input;
if (ty === "string") {
// @ts-ignore
return Memory.fromString(input);
} else if (input instanceof ArrayBuffer) {
// @ts-ignore
return Memory.fromBuffer(input);
} else if (input === undefined) {
// @ts-ignore
return Memory.fromString("");
} else {
// @ts-ignore
return Memory.fromJsonObject(input);
}
}
export class Test {
// call a function from the Extism plugin being tested, passing in `Input` and returning the output as `MemoryView`, which
// can be used to convert the type to a JavaScript native value.
static call(
funcName: string,
input: Input,
): MemoryView {
// @ts-ignore: Memory
const a = Memory.fromString(funcName);
const b = convertInput(input);
const c = call(a.offset, b.offset);
a.free();
b.free();
// @ts-ignore: Memory
return new MemoryView(Memory.find(c));
}
// read the mock test input provided by the test runner, returns `MemoryView`.
// this input is defined in an xtp.toml file, or by the --mock-input-data or --mock-input-file flags.
static mockInput(): MemoryView {
const offset = mock_input();
if (offset === 0) {
throw new Error(
"Failed to fetch mock input, not provided by test runner.",
);
}
// @ts-ignore: Memory
return new MemoryView(Memory.find(offset));
}
// call a function from the Extism plugin being tested, passing in `Input` and get the number of nanoseconds spent in the function.
static timeNanoseconds(funcName: string, input: Input): number {
// @ts-ignore: Memory
const a = Memory.fromString(funcName);
const b = convertInput(input);
const c = time(a.offset, b.offset);
a.free();
b.free();
return c;
}
// Reset the loaded plugin, clearing all state.
static reset() {
reset();
}
static startGroup(name: string) {
// @ts-ignore: Memory
const nameMem = Memory.fromString(name);
group(nameMem.offset);
nameMem.free();
}
// Run a test group, resetting the plugin before and after the group is run.
static group(name: string, callback: () => void) {
Test.reset();
Test.startGroup(name);
callback();
Test.reset();
}
// call a function from the Extism plugin being tested, passing in `Input` and get the number of seconds spent in the function.
static timeSeconds(funcName: string, input: Input): number {
return Test.timeNanoseconds(funcName, input) / 1e9;
}
// assert that the `outcome` is true, naming the assertion with `name`, which will be used as a label in the CLI runner. The `reason` argument
// will be used to print a message when the assertion fails, this should contain some additional information about values being compared.
static assert(name: string, outcome: boolean, reason: string) {
// @ts-ignore: Memory
const a = Memory.fromString(name);
// @ts-ignore: Memory
const b = Memory.fromString(reason);
assert(a.offset, !!outcome, b.offset);
a.free();
b.free();
}
// assert that `x` and `y` are equal, naming the assertion with `msg`, which will be used as a label in the CLI runner.
static assertEqual(msg: string, x: unknown, y: unknown) {
Test.assert(msg, x === y, `Expected ${x} === ${y}\n${stack()}`);
}
// assert that `x` and `y` are not equal, naming the assertion with `msg`, which will be used as a label in the CLI runner.
static assertNotEqual(msg: string, x: unknown, y: unknown) {
Test.assert(msg, x !== y, `Expected ${x} !== ${y}\n${stack()}`);
}
// assert that `x` is greater than `y`, naming the assertion with `msg`, which will be used as a label in the CLI runner.
static assertGreaterThan(msg: string, x: any, y: any) {
Test.assert(msg, x > y, `Expected ${x} > ${y}\n${stack()}`);
}
// assert that `x` is greater than or equal to `y`, naming the assertion with `msg`, which will be used as a label in the CLI runner.
static assertGreaterThanOrEqualTo(msg: string, x: any, y: any) {
Test.assert(msg, x >= y, `Expected ${x} >= ${y}\n${stack()}`);
}
// assert that `x` is less than `y`, naming the assertion with `msg`, which will be used as a label in the CLI runner.
static assertLessThan(msg: string, x: any, y: any) {
Test.assert(msg, x < y, `Expected ${x} < ${y}\n${stack()}`);
}
// assert that `x` is less than or equal to `y`, naming the assertion with `msg`, which will be used as a label in the CLI runner.
static assertLessThanOrEqualTo(msg: string, x: any, y: any) {
Test.assert(msg, x <= y, `Expected ${x} <= ${y}\n${stack()}`);
}
}
const stack = (): string => {
let stack = new Error().stack || "";
stack.trim();
stack = stack.split("\n").slice(1).join("\n");
return stack;
};