-
Notifications
You must be signed in to change notification settings - Fork 42
/
entrypoints.hack
66 lines (60 loc) · 2.09 KB
/
entrypoints.hack
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
/*
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
namespace Facebook\HHAST;
use namespace HH\Lib\Str;
use function HH\ffp_parse_string_native;
async function from_file_async(
File $file,
): Awaitable<Script> {
$cache = __Private\ParserCache::get();
$data = $cache->fetch($file);
if ($data is nonnull) {
return __Private\from_decoded_json($data, $file->getPath());
}
using $odf = new __Private\OnDiskFile($file);
$path = $odf->getPath();
/* HH_IGNORE_ERROR[4107] hhi missing for this builtin */ /* HH_IGNORE_ERROR[2049] */
$json = ffp_parse_string_native($file->getContents());
$data = \json_decode(
$json,
/* as array = */ true,
/* depth = */ 512 /* == default */,
\JSON_FB_HACK_ARRAYS,
);
$no_type_refinement_please = $data;
if (!is_dict($no_type_refinement_please)) {
// Perhaps we had invalid UTF8 - but JSON must be UTF8
//
// While some can be converted to UTF-8,
// this isn't guaranteed - JSON literally can't represent all the legal values
// so the source it returns is useless.
//
// Given that, we don't even need to attempt to do the right conversion - we
// can just do something cheap and throw away the result - so, we can just go
// over the bytes, and throw them away if they're not 7-bit clean.
$ascii = '';
$len = Str\length($json);
for ($i = 0; $i < $len; ++$i) {
$byte = $json[$i];
if ((\ord($byte) & (1 << 7)) === 0) {
$ascii .= $byte;
}
}
$data = \json_decode($ascii, true, 512, \JSON_FB_HACK_ARRAYS);
$no_type_refinement_please = $data;
}
if (!is_dict($no_type_refinement_please)) {
throw new HHParseError($path, 'hh_parse did not output valid JSON');
}
// Use the raw source rather than the re-encoded, as byte offsets may have
// changed while re-encoding
$data['program_text'] = $file->getContents();
$cache->store($file, $data);
return __Private\from_decoded_json($data, $file->getPath());
}