-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathjs-value.ts
220 lines (207 loc) · 5.56 KB
/
js-value.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
import { Memory } from "./memory.js";
import { assertNever, JavaScriptValueKindAndFlags, pointer } from "./types.js";
export const enum Kind {
Boolean = 0,
String = 1,
Number = 2,
Object = 3,
Null = 4,
Undefined = 5,
Function = 6,
Symbol = 7,
BigInt = 8,
}
export const decode = (
kind: Kind,
payload1: number,
payload2: number,
memory: Memory
) => {
switch (kind) {
case Kind.Boolean:
switch (payload1) {
case 0:
return false;
case 1:
return true;
}
case Kind.Number:
return payload2;
case Kind.String:
case Kind.Object:
case Kind.Function:
case Kind.Symbol:
case Kind.BigInt:
return memory.getObject(payload1);
case Kind.Null:
return null;
case Kind.Undefined:
return undefined;
default:
assertNever(kind, `JSValue Type kind "${kind}" is not supported`);
}
};
// Note:
// `decodeValues` assumes that the size of RawJSValue is 16.
export const decodeArray = (ptr: pointer, length: number, memory: Memory) => {
// fast path for empty array
if (length === 0) {
return [];
}
let result = [];
// It's safe to hold DataView here because WebAssembly.Memory.buffer won't
// change within this function.
const view = memory.dataView();
for (let index = 0; index < length; index++) {
const base = ptr + 16 * index;
const kind = view.getUint32(base, true);
const payload1 = view.getUint32(base + 4, true);
const payload2 = view.getFloat64(base + 8, true);
result.push(decode(kind, payload1, payload2, memory));
}
return result;
};
// A helper function to encode a RawJSValue into a pointers.
// Please prefer to use `writeAndReturnKindBits` to avoid unnecessary
// memory stores.
// This function should be used only when kind flag is stored in memory.
export const write = (
value: any,
kind_ptr: pointer,
payload1_ptr: pointer,
payload2_ptr: pointer,
is_exception: boolean,
memory: Memory
) => {
const kind = writeAndReturnKindBits(
value,
payload1_ptr,
payload2_ptr,
is_exception,
memory
);
memory.writeUint32(kind_ptr, kind);
};
export function decompose(value: any, memory: Memory): {
kind: JavaScriptValueKindAndFlags;
payload1: number;
payload2: number;
} {
if (value === null) {
return {
kind: Kind.Null,
payload1: 0,
payload2: 0,
}
}
const type = typeof value;
switch (type) {
case "boolean": {
return {
kind: Kind.Boolean,
payload1: value ? 1 : 0,
payload2: 0,
}
}
case "number": {
return {
kind: Kind.Number,
payload1: 0,
payload2: value,
}
}
case "string": {
return {
kind: Kind.String,
payload1: memory.retain(value),
payload2: 0,
}
}
case "undefined": {
return {
kind: Kind.Undefined,
payload1: 0,
payload2: 0,
}
}
case "object": {
return {
kind: Kind.Object,
payload1: memory.retain(value),
payload2: 0,
}
}
case "function": {
return {
kind: Kind.Function,
payload1: memory.retain(value),
payload2: 0,
}
}
case "symbol": {
return {
kind: Kind.Symbol,
payload1: memory.retain(value),
payload2: 0,
}
}
case "bigint": {
return {
kind: Kind.BigInt,
payload1: memory.retain(value),
payload2: 0,
}
}
default:
assertNever(type, `Type "${type}" is not supported yet`);
}
throw new Error("unreachable");
}
export const writeAndReturnKindBits = (
value: any,
payload1_ptr: pointer,
payload2_ptr: pointer,
is_exception: boolean,
memory: Memory
): JavaScriptValueKindAndFlags => {
const exceptionBit = (is_exception ? 1 : 0) << 31;
if (value === null) {
return exceptionBit | Kind.Null;
}
const writeRef = (kind: Kind) => {
memory.writeUint32(payload1_ptr, memory.retain(value));
return exceptionBit | kind;
};
const type = typeof value;
switch (type) {
case "boolean": {
memory.writeUint32(payload1_ptr, value ? 1 : 0);
return exceptionBit | Kind.Boolean;
}
case "number": {
memory.writeFloat64(payload2_ptr, value);
return exceptionBit | Kind.Number;
}
case "string": {
return writeRef(Kind.String);
}
case "undefined": {
return exceptionBit | Kind.Undefined;
}
case "object": {
return writeRef(Kind.Object);
}
case "function": {
return writeRef(Kind.Function);
}
case "symbol": {
return writeRef(Kind.Symbol);
}
case "bigint": {
return writeRef(Kind.BigInt);
}
default:
assertNever(type, `Type "${type}" is not supported yet`);
}
throw new Error("Unreachable");
};