-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmemory.ts
142 lines (122 loc) · 3.26 KB
/
memory.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
/**
* Wrapper class for the Region data structure, which describes a region of
* WebAssembly's linear memory that has been allocated by the VM.
*
* Note that this class is passed a pointer to the data structure, and the
* Region's members (offset: u32, capacity: u32, length: u32) are read from
* that pointer as they are laid out in the data structure.
*/
export class Region {
/**
* The region's data structure laid out in memory.
*/
public region_info: Uint32Array;
/**
* @param memory The WebAssembly.Memory object that this region is associated
* @param ptr The offset of the region's data structure in memory
*/
constructor(public memory: WebAssembly.Memory, public ptr: number) {
this.region_info = new Uint32Array(memory.buffer, ptr, 3);
}
public get offset(): number {
return this.region_info[0];
}
public set offset(val: number) {
this.region_info[0] = val;
}
public set capacity(val: number) {
this.region_info[1] = val;
}
public get capacity(): number {
return this.region_info[1];
}
public set length(val: number) {
this.region_info[2] = val;
}
public get length(): number {
return this.region_info[2];
}
/**
* Get a byte-slice of the region's data.
*/
public get data(): Uint8Array {
return this.read();
}
/**
* Get a byte-slice of the entire writable region.
*/
public get slice(): Uint8Array {
return new Uint8Array(this.memory.buffer, this.offset, this.capacity);
}
/**
* Get a base64-encoded string of the region's data.
*/
public get b64(): string {
return this.read_b64();
}
/**
* Get a string view of the region's data.
*/
public get str(): string {
return this.read_str();
}
/**
* Parse the object of the region's data as JSON.
*/
public get json(): object {
return this.read_json();
}
/**
* Write a byte-slice to the region.
* @param bytes The bytes to write to the region
*/
public write(bytes: Uint8Array): void {
this.slice.set(bytes);
this.length = bytes.length;
}
/**
* Write bytes encoded as base64 to the region.
* @param b64 bytes encoded as base64
*/
public write_b64(b64: string): void {
this.write(Buffer.from(b64, 'base64'));
}
/**
* Write a string to the region.
* @param str The string to write to the region
*/
public write_str(str: string): void {
this.write(new TextEncoder().encode(str));
}
/**
* Write a JSON object to the region as a string.
* @param obj The object to write to the region
*/
public write_json(obj: object): void {
this.write_str(JSON.stringify(obj));
}
/**
* Reads the region's data as a Uint8Array.
* @returns The byte-slice of the region's data.
*/
public read(): Uint8Array {
return new Uint8Array(this.memory.buffer, this.offset, this.length);
}
public read_b64(): string {
return Buffer.from(this.read()).toString('base64');
}
/**
* Reads the region's data as a String.
* @returns The region's data as a string.
*/
public read_str(): string {
return new TextDecoder().decode(this.read());
}
/**
* Parse the region's data as JSON.
* @returns The region's data as a JSON object.
*/
public read_json(): object {
return JSON.parse(this.read_str());
}
}