-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
255 lines (233 loc) · 7.36 KB
/
index.js
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
/* @flow */
import {forceInline} from "../../performance";
import randomString from "../../random/string";
import type Backing from "backing";
import type {Realm} from "../..";
export const STRING_LENGTH_OFFSET = 0;
export const STRING_HASH_OFFSET = 4;
export const STRING_HEADER_SIZE = 8;
export const STRING_DATA_OFFSET = STRING_HEADER_SIZE;
/**
* Make a simple string type for the given realm.
*/
export function make (realm: Realm): PrimitiveType<string> {
const {StringType} = realm;
const RawString = new StringType({
name: 'String',
byteLength: 8, // Pointer
byteAlignment: 8,
constructor (input) {
if (this instanceof RawString) {
throw new TypeError(`String is not a constructor.`);
}
else {
return input == null ? '' : ''+input;
}
},
cast (input: any): string {
return input == null ? '' : ''+input;
},
accepts (input: any): boolean {
return typeof input === 'string';
},
initialize (backing: Backing, pointerAddress: float64, initialInput?: string): void {
if (!initialInput) {
backing.setFloat64(pointerAddress, 0);
}
else {
backing.setFloat64(pointerAddress, createRawString(backing, initialInput));
}
},
store (backing: Backing, pointerAddress: float64, input: string): void {
const existing: float64 = backing.getFloat64(pointerAddress);
if (!input) {
if (existing !== 0) {
backing.gc.unref(existing);
backing.setFloat64(pointerAddress, 0);
}
}
else {
const address: float64 = createRawString(backing, input);
if (address !== existing) {
if (existing !== 0) {
backing.gc.unref(existing);
}
backing.setFloat64(pointerAddress, address);
}
}
},
load (backing: Backing, address: float64): string {
return getString(backing, backing.getFloat64(address));
},
clear (backing: Backing, address: float64): void {
const existing: float64 = backing.getFloat64(address);
if (existing !== 0) {
backing.gc.unref(existing);
backing.setFloat64(address, 0);
}
},
randomValue: randomString,
emptyValue (): string {
return '';
},
hashValue: hashString,
equal (valueA: string, valueB: string) {
return valueA === valueB;
},
flowType () {
return `string`;
}
});
return RawString;
}
/**
* Store the given raw string and return the address.
* The string will NOT be interned.
*/
function createRawString (backing: Backing, input: string): float64 {
let hash = 0x811c9dc5;
let allAscii = true;
const length = input.length;
for (let i = 0; i < length; i++) {
const code: uint16 = input.charCodeAt(i);
if (code > 127) {
allAscii = false;
}
hash ^= code;
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
}
hash = hash >>> 0;
trace: `Got hash ${hash} for ${allAscii ? 'ascii' : 'multi-byte'} string of ${length} characters.`;
return storeString(backing, input, hash, allAscii);
}
forceInline(createRawString);
/**
* Store a string and return its address.
*/
function storeString (backing: Backing, input: string, hash: uint32, allAscii: boolean): float64 {
if (allAscii) {
return storeAsciiString(backing, input, hash);
}
else {
return storeMultibyteString(backing, input, hash);
}
}
forceInline(storeString);
function storeAsciiString (backing: Backing, input: string, hash: uint32): float64 {
const length = input.length;
trace: `Storing an ascii string of ${length} character(s): ${JSON.stringify(input)}`;
const byteLength = length + STRING_HEADER_SIZE;
const address: float64 = backing.gc.alloc(byteLength, 0, 1);
backing.setInt32(address, length);
backing.setUint32(address + STRING_HASH_OFFSET, hash);
const offset = backing.offsetFor(address + STRING_DATA_OFFSET);
const chars: Uint8Array = backing.arenaFor(address).uint8Array;
for (let i = 0; i < length; i++) {
chars[offset + i] = input.charCodeAt(i);
}
return address;
}
forceInline(storeAsciiString);
function storeMultibyteString (backing: Backing, input: string, hash: uint32): float64 {
const length = input.length;
const byteLength = length + length + STRING_HEADER_SIZE;
const address: float64 = backing.gc.alloc(byteLength, 0, 1);
backing.setInt32(address, -length);
backing.setUint32(address + STRING_HASH_OFFSET, hash);
const offset = backing.offsetFor(address + STRING_DATA_OFFSET) >> 1;
const chars: Uint16Array = backing.arenaFor(address).uint16Array;
for (let i = 0; i < length; i++) {
chars[offset + i] = input.charCodeAt(i);
}
return address;
}
forceInline(storeMultibyteString);
/**
* Returns the hash for the given string.
*/
function hashString (input: string): uint32 {
let hash = 0x811c9dc5;
for (let i = 0; i < input.length; i++) {
hash ^= input.charCodeAt(i);
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
}
return hash >>> 0;
}
forceInline(hashString);
/**
* Check that the string stored at the given address matches the given input + hash.
*/
function checkEqual (backing: Backing, address: float64, input: string, hash: uint32, allAscii: boolean): boolean {
assert: Math.floor(address) === address;
trace: `Checking address ${address} vs ${input} (${hash}).`;
if (getStringHash(backing, address) !== hash) {
trace: `Hash does not match ${hash}.`;
return false;
}
let length = backing.getInt32(address);
assert: Math.floor(length) === length && Math.abs(length) < Math.pow(2, 31);
trace: `Got raw string length ${length} ${Math.floor(length)}`;
if (length < 0) {
if (allAscii) {
return false;
}
length = -length;
if (length !== input.length) {
return false;
}
const arena = backing.arenaFor(address);
const chars: Uint16Array = arena.uint16Array;
const offset = (backing.offsetFor(address + STRING_HEADER_SIZE)) >> 1;
for (let i = 0; i < length; i++) {
if (input.charCodeAt(i) !== chars[offset + i]) {
return false;
}
}
return true;
}
else {
if (!allAscii) {
return false;
}
else if (length !== input.length) {
return false;
}
const arena = backing.arenaFor(address);
const chars: Uint8Array = arena.uint8Array;
const offset = backing.offsetFor(address + STRING_HEADER_SIZE);
for (let i = 0; i < length; i++) {
if (input.charCodeAt(i) !== chars[offset + i]) {
return false;
}
}
return true;
}
}
forceInline(checkEqual);
/**
* Read the string at the given address.
*/
function getString (backing: Backing, address: float64): string {
if (address === 0) {
return '';
}
const arena = backing.arenaFor(address);
let offset: uint32 = backing.offsetFor(address);
const length: int32 = arena.int32Array[offset >> 2];
if (length < 0) {
offset = (offset + STRING_DATA_OFFSET) >> 1;
return String.fromCharCode(...arena.uint16Array.slice(offset, offset + Math.abs(length)));
}
else {
offset = (offset + STRING_DATA_OFFSET);
return String.fromCharCode(...arena.uint8Array.slice(offset, offset + Math.abs(length)));
}
}
forceInline(getString);
/**
* Read the hash for the given string.
*/
function getStringHash (backing: Backing, address: float64): uint32 {
return backing.getUint32(address + STRING_HASH_OFFSET);
}
forceInline(getStringHash);