-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathjs-value.ts
161 lines (148 loc) · 4.43 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
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`);
}
};
export function makeStringDecoder(sharedMemory: boolean, textDecoder: TextDecoder): (ptr: pointer, length: number, memory: Memory) => string {
// NOTE: TextDecoder can't decode typed arrays backed by SharedArrayBuffer
return sharedMemory == true
? ((bytes_ptr: pointer, length: number, memory: Memory) => {
const bytes = memory
.bytes()
.slice(bytes_ptr, bytes_ptr + length);
return textDecoder.decode(bytes);
})
: ((bytes_ptr: pointer, length: number, memory: Memory) => {
const bytes = memory
.bytes()
.subarray(bytes_ptr, bytes_ptr + length);
return textDecoder.decode(bytes);
})
}
// 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 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");
};