hermes-estree 0.32.1 → 0.33.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/generated/HermesESTreeSelectorTypes.js.flow +262 -27
- package/dist/generated/predicates.js +120 -0
- package/dist/generated/predicates.js.flow +72 -0
- package/dist/predicates.js +2 -4
- package/dist/predicates.js.flow +5 -4
- package/dist/src/generated/predicates.js +120 -0
- package/dist/src/predicates.js +2 -4
- package/dist/types.js.flow +103 -11
- package/package.json +1 -1
- package/dist/HermesAST.js.flow +0 -57
- package/dist/HermesASTAdapter.js +0 -192
- package/dist/HermesASTAdapter.js.flow +0 -189
- package/dist/HermesParser.js +0 -108
- package/dist/HermesParser.js.flow +0 -161
- package/dist/HermesParserDecodeUTF8String.js +0 -68
- package/dist/HermesParserDecodeUTF8String.js.flow +0 -65
- package/dist/HermesParserDeserializer.js +0 -242
- package/dist/HermesParserDeserializer.js.flow +0 -260
- package/dist/HermesParserNodeDeserializers.js +0 -2477
- package/dist/HermesParserNodeDeserializers.js.flow +0 -16
- package/dist/HermesParserWASM.js +0 -6
- package/dist/HermesParserWASM.js.flow +0 -87
- package/dist/HermesToESTreeAdapter.js +0 -439
- package/dist/HermesToESTreeAdapter.js.flow +0 -421
- package/dist/ParserOptions.js +0 -18
- package/dist/ParserOptions.js.flow +0 -41
- package/dist/babel/TransformESTreeToBabel.js +0 -1123
- package/dist/babel/TransformESTreeToBabel.js.flow +0 -1277
- package/dist/estree/StripFlowTypes.js +0 -175
- package/dist/estree/StripFlowTypes.js.flow +0 -158
- package/dist/estree/StripFlowTypesForBabel.js +0 -215
- package/dist/estree/StripFlowTypesForBabel.js.flow +0 -216
- package/dist/estree/TransformComponentSyntax.js +0 -788
- package/dist/estree/TransformComponentSyntax.js.flow +0 -864
- package/dist/estree/TransformEnumSyntax.js +0 -106
- package/dist/estree/TransformEnumSyntax.js.flow +0 -125
- package/dist/estree/TransformMatchSyntax.js +0 -1006
- package/dist/estree/TransformMatchSyntax.js.flow +0 -912
- package/dist/generated/ESTreeVisitorKeys.js +0 -220
- package/dist/generated/ESTreeVisitorKeys.js.flow +0 -15
- package/dist/generated/ParserVisitorKeys.js +0 -794
- package/dist/generated/ParserVisitorKeys.js.flow +0 -17
- package/dist/getModuleDocblock.js +0 -112
- package/dist/getModuleDocblock.js.flow +0 -118
- package/dist/src/HermesASTAdapter.js +0 -192
- package/dist/src/HermesParser.js +0 -108
- package/dist/src/HermesParserDecodeUTF8String.js +0 -68
- package/dist/src/HermesParserDeserializer.js +0 -242
- package/dist/src/HermesParserNodeDeserializers.js +0 -2477
- package/dist/src/HermesToESTreeAdapter.js +0 -439
- package/dist/src/ParserOptions.js +0 -18
- package/dist/src/babel/TransformESTreeToBabel.js +0 -1123
- package/dist/src/estree/StripFlowTypes.js +0 -175
- package/dist/src/estree/StripFlowTypesForBabel.js +0 -215
- package/dist/src/estree/TransformComponentSyntax.js +0 -788
- package/dist/src/estree/TransformEnumSyntax.js +0 -106
- package/dist/src/estree/TransformMatchSyntax.js +0 -1006
- package/dist/src/generated/ESTreeVisitorKeys.js +0 -220
- package/dist/src/generated/ParserVisitorKeys.js +0 -794
- package/dist/src/getModuleDocblock.js +0 -112
- package/dist/src/transform/SimpleTransform.js +0 -136
- package/dist/src/transform/astArrayMutationHelpers.js +0 -62
- package/dist/src/transform/astNodeMutationHelpers.js +0 -200
- package/dist/src/traverse/SimpleTraverser.js +0 -137
- package/dist/src/traverse/getVisitorKeys.js +0 -37
- package/dist/src/utils/Builders.js +0 -191
- package/dist/src/utils/GenID.js +0 -41
- package/dist/src/utils/createSyntaxError.js +0 -25
- package/dist/src/utils/mutateESTreeASTForPrettier.js +0 -127
- package/dist/transform/SimpleTransform.js +0 -136
- package/dist/transform/SimpleTransform.js.flow +0 -169
- package/dist/transform/astArrayMutationHelpers.js +0 -62
- package/dist/transform/astArrayMutationHelpers.js.flow +0 -71
- package/dist/transform/astNodeMutationHelpers.js +0 -200
- package/dist/transform/astNodeMutationHelpers.js.flow +0 -246
- package/dist/traverse/SimpleTraverser.js +0 -137
- package/dist/traverse/SimpleTraverser.js.flow +0 -133
- package/dist/traverse/getVisitorKeys.js +0 -37
- package/dist/traverse/getVisitorKeys.js.flow +0 -36
- package/dist/utils/Builders.js +0 -191
- package/dist/utils/Builders.js.flow +0 -218
- package/dist/utils/GenID.js +0 -41
- package/dist/utils/GenID.js.flow +0 -38
- package/dist/utils/createSyntaxError.js +0 -25
- package/dist/utils/createSyntaxError.js.flow +0 -24
- package/dist/utils/mutateESTreeASTForPrettier.js +0 -127
- package/dist/utils/mutateESTreeASTForPrettier.js.flow +0 -130
package/dist/HermesParser.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
Object.defineProperty(exports, "__esModule", {
|
|
13
|
-
value: true
|
|
14
|
-
});
|
|
15
|
-
exports.parse = parse;
|
|
16
|
-
|
|
17
|
-
var _HermesParserDeserializer = _interopRequireDefault(require("./HermesParserDeserializer"));
|
|
18
|
-
|
|
19
|
-
var _HermesParserWASM = _interopRequireDefault(require("./HermesParserWASM"));
|
|
20
|
-
|
|
21
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
|
-
|
|
23
|
-
let HermesParserWASM;
|
|
24
|
-
let hermesParse;
|
|
25
|
-
let hermesParseResult_free;
|
|
26
|
-
let hermesParseResult_getError;
|
|
27
|
-
let hermesParseResult_getErrorLine;
|
|
28
|
-
let hermesParseResult_getErrorColumn;
|
|
29
|
-
let hermesParseResult_getProgramBuffer;
|
|
30
|
-
let hermesParseResult_getPositionBuffer;
|
|
31
|
-
let hermesParseResult_getPositionBufferSize;
|
|
32
|
-
/**
|
|
33
|
-
* Init the WASM wrapper code generated by `emscripten` to preparse the
|
|
34
|
-
* HermesParser WASM code.
|
|
35
|
-
*/
|
|
36
|
-
|
|
37
|
-
function initHermesParserWASM() {
|
|
38
|
-
if (HermesParserWASM != null) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
HermesParserWASM = (0, _HermesParserWASM.default)({
|
|
43
|
-
/**
|
|
44
|
-
* The emscripten version of `quit` unconditionally assigns the `status` to
|
|
45
|
-
* `process.exitCode` which overrides any pre-existing code that has been
|
|
46
|
-
* set, even if it is non zero. For our use case we never want an
|
|
47
|
-
* `exitCode` to be set so this override removes that functionality.
|
|
48
|
-
*/
|
|
49
|
-
quit(_status, toThrow) {
|
|
50
|
-
throw toThrow;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
});
|
|
54
|
-
hermesParse = HermesParserWASM.cwrap('hermesParse', 'number', ['number', 'number', 'number', 'number', 'number', 'number', 'number']);
|
|
55
|
-
hermesParseResult_free = HermesParserWASM.cwrap('hermesParseResult_free', 'void', ['number']);
|
|
56
|
-
hermesParseResult_getError = HermesParserWASM.cwrap('hermesParseResult_getError', 'string', ['number']);
|
|
57
|
-
hermesParseResult_getErrorLine = HermesParserWASM.cwrap('hermesParseResult_getErrorLine', 'number', ['number']);
|
|
58
|
-
hermesParseResult_getErrorColumn = HermesParserWASM.cwrap('hermesParseResult_getErrorColumn', 'number', ['number']);
|
|
59
|
-
hermesParseResult_getProgramBuffer = HermesParserWASM.cwrap('hermesParseResult_getProgramBuffer', 'number', ['number']);
|
|
60
|
-
hermesParseResult_getPositionBuffer = HermesParserWASM.cwrap('hermesParseResult_getPositionBuffer', 'number', ['number']);
|
|
61
|
-
hermesParseResult_getPositionBufferSize = HermesParserWASM.cwrap('hermesParseResult_getPositionBufferSize', 'number', ['number']);
|
|
62
|
-
} // Copy a string into the WASM heap and null-terminate
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
function copyToHeap(buffer, addr) {
|
|
66
|
-
HermesParserWASM.HEAP8.set(buffer, addr);
|
|
67
|
-
HermesParserWASM.HEAP8[addr + buffer.length] = 0;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function parse(source, options) {
|
|
71
|
-
initHermesParserWASM(); // Allocate space on heap for source text
|
|
72
|
-
|
|
73
|
-
const sourceBuffer = Buffer.from(source, 'utf8');
|
|
74
|
-
|
|
75
|
-
const sourceAddr = HermesParserWASM._malloc(sourceBuffer.length + 1);
|
|
76
|
-
|
|
77
|
-
if (!sourceAddr) {
|
|
78
|
-
throw new Error('Parser out of memory');
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
try {
|
|
82
|
-
// Copy source text onto WASM heap
|
|
83
|
-
copyToHeap(sourceBuffer, sourceAddr);
|
|
84
|
-
const parseResult = hermesParse(sourceAddr, sourceBuffer.length + 1, options.flow === 'detect', options.enableExperimentalComponentSyntax, options.enableExperimentalFlowMatchSyntax, options.tokens, options.allowReturnOutsideFunction);
|
|
85
|
-
|
|
86
|
-
try {
|
|
87
|
-
// Extract and throw error from parse result if parsing failed
|
|
88
|
-
const err = hermesParseResult_getError(parseResult);
|
|
89
|
-
|
|
90
|
-
if (err) {
|
|
91
|
-
const syntaxError = new SyntaxError(err); // $FlowExpectedError[prop-missing]
|
|
92
|
-
|
|
93
|
-
syntaxError.loc = {
|
|
94
|
-
line: hermesParseResult_getErrorLine(parseResult),
|
|
95
|
-
column: hermesParseResult_getErrorColumn(parseResult)
|
|
96
|
-
};
|
|
97
|
-
throw syntaxError;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const deserializer = new _HermesParserDeserializer.default(hermesParseResult_getProgramBuffer(parseResult), hermesParseResult_getPositionBuffer(parseResult), hermesParseResult_getPositionBufferSize(parseResult), HermesParserWASM, options);
|
|
101
|
-
return deserializer.deserialize();
|
|
102
|
-
} finally {
|
|
103
|
-
hermesParseResult_free(parseResult);
|
|
104
|
-
}
|
|
105
|
-
} finally {
|
|
106
|
-
HermesParserWASM._free(sourceAddr);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
* @flow strict
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
'use strict';
|
|
12
|
-
|
|
13
|
-
import type {HermesNode} from './HermesAST';
|
|
14
|
-
import type {ParserOptions} from './ParserOptions';
|
|
15
|
-
|
|
16
|
-
import HermesParserDeserializer from './HermesParserDeserializer';
|
|
17
|
-
import HermesParserWASMModule from './HermesParserWASM';
|
|
18
|
-
|
|
19
|
-
let HermesParserWASM;
|
|
20
|
-
let hermesParse;
|
|
21
|
-
let hermesParseResult_free;
|
|
22
|
-
let hermesParseResult_getError;
|
|
23
|
-
let hermesParseResult_getErrorLine;
|
|
24
|
-
let hermesParseResult_getErrorColumn;
|
|
25
|
-
let hermesParseResult_getProgramBuffer;
|
|
26
|
-
let hermesParseResult_getPositionBuffer;
|
|
27
|
-
let hermesParseResult_getPositionBufferSize;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Init the WASM wrapper code generated by `emscripten` to preparse the
|
|
31
|
-
* HermesParser WASM code.
|
|
32
|
-
*/
|
|
33
|
-
function initHermesParserWASM() {
|
|
34
|
-
if (HermesParserWASM != null) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
HermesParserWASM = HermesParserWASMModule({
|
|
39
|
-
/**
|
|
40
|
-
* The emscripten version of `quit` unconditionally assigns the `status` to
|
|
41
|
-
* `process.exitCode` which overrides any pre-existing code that has been
|
|
42
|
-
* set, even if it is non zero. For our use case we never want an
|
|
43
|
-
* `exitCode` to be set so this override removes that functionality.
|
|
44
|
-
*/
|
|
45
|
-
quit(_status: number, toThrow: Error) {
|
|
46
|
-
throw toThrow;
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
hermesParse = HermesParserWASM.cwrap('hermesParse', 'number', [
|
|
51
|
-
'number',
|
|
52
|
-
'number',
|
|
53
|
-
'number',
|
|
54
|
-
'number',
|
|
55
|
-
'number',
|
|
56
|
-
'number',
|
|
57
|
-
'number',
|
|
58
|
-
]);
|
|
59
|
-
|
|
60
|
-
hermesParseResult_free = HermesParserWASM.cwrap(
|
|
61
|
-
'hermesParseResult_free',
|
|
62
|
-
'void',
|
|
63
|
-
['number'],
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
hermesParseResult_getError = HermesParserWASM.cwrap(
|
|
67
|
-
'hermesParseResult_getError',
|
|
68
|
-
'string',
|
|
69
|
-
['number'],
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
hermesParseResult_getErrorLine = HermesParserWASM.cwrap(
|
|
73
|
-
'hermesParseResult_getErrorLine',
|
|
74
|
-
'number',
|
|
75
|
-
['number'],
|
|
76
|
-
);
|
|
77
|
-
|
|
78
|
-
hermesParseResult_getErrorColumn = HermesParserWASM.cwrap(
|
|
79
|
-
'hermesParseResult_getErrorColumn',
|
|
80
|
-
'number',
|
|
81
|
-
['number'],
|
|
82
|
-
);
|
|
83
|
-
|
|
84
|
-
hermesParseResult_getProgramBuffer = HermesParserWASM.cwrap(
|
|
85
|
-
'hermesParseResult_getProgramBuffer',
|
|
86
|
-
'number',
|
|
87
|
-
['number'],
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
hermesParseResult_getPositionBuffer = HermesParserWASM.cwrap(
|
|
91
|
-
'hermesParseResult_getPositionBuffer',
|
|
92
|
-
'number',
|
|
93
|
-
['number'],
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
hermesParseResult_getPositionBufferSize = HermesParserWASM.cwrap(
|
|
97
|
-
'hermesParseResult_getPositionBufferSize',
|
|
98
|
-
'number',
|
|
99
|
-
['number'],
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Copy a string into the WASM heap and null-terminate
|
|
104
|
-
function copyToHeap(buffer: Buffer, addr: number) {
|
|
105
|
-
HermesParserWASM.HEAP8.set(buffer, addr);
|
|
106
|
-
HermesParserWASM.HEAP8[addr + buffer.length] = 0;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export function parse(source: string, options: ParserOptions): HermesNode {
|
|
110
|
-
initHermesParserWASM();
|
|
111
|
-
|
|
112
|
-
// Allocate space on heap for source text
|
|
113
|
-
const sourceBuffer = Buffer.from(source, 'utf8');
|
|
114
|
-
const sourceAddr = HermesParserWASM._malloc(sourceBuffer.length + 1);
|
|
115
|
-
if (!sourceAddr) {
|
|
116
|
-
throw new Error('Parser out of memory');
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
try {
|
|
120
|
-
// Copy source text onto WASM heap
|
|
121
|
-
copyToHeap(sourceBuffer, sourceAddr);
|
|
122
|
-
|
|
123
|
-
const parseResult = hermesParse(
|
|
124
|
-
sourceAddr,
|
|
125
|
-
sourceBuffer.length + 1,
|
|
126
|
-
options.flow === 'detect',
|
|
127
|
-
options.enableExperimentalComponentSyntax,
|
|
128
|
-
options.enableExperimentalFlowMatchSyntax,
|
|
129
|
-
options.tokens,
|
|
130
|
-
options.allowReturnOutsideFunction,
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
try {
|
|
134
|
-
// Extract and throw error from parse result if parsing failed
|
|
135
|
-
const err = hermesParseResult_getError(parseResult);
|
|
136
|
-
if (err) {
|
|
137
|
-
const syntaxError = new SyntaxError(err);
|
|
138
|
-
// $FlowExpectedError[prop-missing]
|
|
139
|
-
syntaxError.loc = {
|
|
140
|
-
line: hermesParseResult_getErrorLine(parseResult),
|
|
141
|
-
column: hermesParseResult_getErrorColumn(parseResult),
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
throw syntaxError;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const deserializer = new HermesParserDeserializer(
|
|
148
|
-
hermesParseResult_getProgramBuffer(parseResult),
|
|
149
|
-
hermesParseResult_getPositionBuffer(parseResult),
|
|
150
|
-
hermesParseResult_getPositionBufferSize(parseResult),
|
|
151
|
-
HermesParserWASM,
|
|
152
|
-
options,
|
|
153
|
-
);
|
|
154
|
-
return deserializer.deserialize();
|
|
155
|
-
} finally {
|
|
156
|
-
hermesParseResult_free(parseResult);
|
|
157
|
-
}
|
|
158
|
-
} finally {
|
|
159
|
-
HermesParserWASM._free(sourceAddr);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
'use strict';
|
|
11
|
-
/**
|
|
12
|
-
* Decode a UTF-8 encoded string from Hermes with a known length.
|
|
13
|
-
* Based on Emscripten's UTF8ToString with the following differences:
|
|
14
|
-
* - Always reads all bytes up to the given length, including null bytes. This
|
|
15
|
-
* means that we can decode strings that contain null bytes in the middle.
|
|
16
|
-
* - Allow UTF-8 encoded code points that are part of a surrogate pair, even though
|
|
17
|
-
* this is technically invalid UTF-8 that UTF8ToString would convert to 0xfffd.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
Object.defineProperty(exports, "__esModule", {
|
|
21
|
-
value: true
|
|
22
|
-
});
|
|
23
|
-
exports.default = HermesParserDecodeUTF8String;
|
|
24
|
-
|
|
25
|
-
function HermesParserDecodeUTF8String(ptrIn, length, heap) {
|
|
26
|
-
let ptr = ptrIn;
|
|
27
|
-
const endPtr = ptr + length;
|
|
28
|
-
let str = '';
|
|
29
|
-
|
|
30
|
-
while (ptr < endPtr) {
|
|
31
|
-
// ASCII characters fit in single byte code point
|
|
32
|
-
let u0 = heap[ptr++];
|
|
33
|
-
|
|
34
|
-
if (!(u0 & 0x80)) {
|
|
35
|
-
str += String.fromCharCode(u0);
|
|
36
|
-
continue;
|
|
37
|
-
} // Two byte code point
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const u1 = heap[ptr++] & 0x3f;
|
|
41
|
-
|
|
42
|
-
if ((u0 & 0xe0) === 0xc0) {
|
|
43
|
-
str += String.fromCharCode((u0 & 0x1f) << 6 | u1);
|
|
44
|
-
continue;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const u2 = heap[ptr++] & 0x3f;
|
|
48
|
-
|
|
49
|
-
if ((u0 & 0xf0) === 0xe0) {
|
|
50
|
-
// Three byte code point
|
|
51
|
-
u0 = (u0 & 0x0f) << 12 | u1 << 6 | u2;
|
|
52
|
-
} else {
|
|
53
|
-
// Four byte code point
|
|
54
|
-
u0 = (u0 & 0x07) << 18 | u1 << 12 | u2 << 6 | heap[ptr++] & 0x3f;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (u0 < 0x10000) {
|
|
58
|
-
// Code point fits into a single UTF-16 code unit
|
|
59
|
-
str += String.fromCharCode(u0);
|
|
60
|
-
} else {
|
|
61
|
-
// Code point does not fit into single UTF-16 code unit so convert to surrogate pair
|
|
62
|
-
u0 -= 0x10000;
|
|
63
|
-
str += String.fromCharCode(0xd800 | u0 >> 10, 0xdc00 | u0 & 0x3ff);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return str;
|
|
68
|
-
}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
* @flow strict
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
'use strict';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Decode a UTF-8 encoded string from Hermes with a known length.
|
|
15
|
-
* Based on Emscripten's UTF8ToString with the following differences:
|
|
16
|
-
* - Always reads all bytes up to the given length, including null bytes. This
|
|
17
|
-
* means that we can decode strings that contain null bytes in the middle.
|
|
18
|
-
* - Allow UTF-8 encoded code points that are part of a surrogate pair, even though
|
|
19
|
-
* this is technically invalid UTF-8 that UTF8ToString would convert to 0xfffd.
|
|
20
|
-
*/
|
|
21
|
-
export default function HermesParserDecodeUTF8String(
|
|
22
|
-
ptrIn: number,
|
|
23
|
-
length: number,
|
|
24
|
-
heap: Uint8Array,
|
|
25
|
-
): string {
|
|
26
|
-
let ptr = ptrIn;
|
|
27
|
-
const endPtr = ptr + length;
|
|
28
|
-
let str = '';
|
|
29
|
-
|
|
30
|
-
while (ptr < endPtr) {
|
|
31
|
-
// ASCII characters fit in single byte code point
|
|
32
|
-
let u0 = heap[ptr++];
|
|
33
|
-
if (!(u0 & 0x80)) {
|
|
34
|
-
str += String.fromCharCode(u0);
|
|
35
|
-
continue;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Two byte code point
|
|
39
|
-
const u1 = heap[ptr++] & 0x3f;
|
|
40
|
-
if ((u0 & 0xe0) === 0xc0) {
|
|
41
|
-
str += String.fromCharCode(((u0 & 0x1f) << 6) | u1);
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const u2 = heap[ptr++] & 0x3f;
|
|
46
|
-
if ((u0 & 0xf0) === 0xe0) {
|
|
47
|
-
// Three byte code point
|
|
48
|
-
u0 = ((u0 & 0x0f) << 12) | (u1 << 6) | u2;
|
|
49
|
-
} else {
|
|
50
|
-
// Four byte code point
|
|
51
|
-
u0 = ((u0 & 0x07) << 18) | (u1 << 12) | (u2 << 6) | (heap[ptr++] & 0x3f);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (u0 < 0x10000) {
|
|
55
|
-
// Code point fits into a single UTF-16 code unit
|
|
56
|
-
str += String.fromCharCode(u0);
|
|
57
|
-
} else {
|
|
58
|
-
// Code point does not fit into single UTF-16 code unit so convert to surrogate pair
|
|
59
|
-
u0 -= 0x10000;
|
|
60
|
-
str += String.fromCharCode(0xd800 | (u0 >> 10), 0xdc00 | (u0 & 0x3ff));
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return str;
|
|
65
|
-
}
|
|
@@ -1,242 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
Object.defineProperty(exports, "__esModule", {
|
|
13
|
-
value: true
|
|
14
|
-
});
|
|
15
|
-
exports.default = void 0;
|
|
16
|
-
|
|
17
|
-
var _HermesParserDecodeUTF8String = _interopRequireDefault(require("./HermesParserDecodeUTF8String"));
|
|
18
|
-
|
|
19
|
-
var _HermesParserNodeDeserializers = _interopRequireDefault(require("./HermesParserNodeDeserializers"));
|
|
20
|
-
|
|
21
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
|
-
|
|
23
|
-
class HermesParserDeserializer {
|
|
24
|
-
// Matches StoredComment::Kind enum in JSLexer.h
|
|
25
|
-
// Matches TokenType enum in HermesParserJSSerializer.h
|
|
26
|
-
constructor(programBuffer, positionBuffer, positionBufferSize, wasmParser, options) {
|
|
27
|
-
this.programBufferIdx = void 0;
|
|
28
|
-
this.positionBufferIdx = void 0;
|
|
29
|
-
this.positionBufferSize = void 0;
|
|
30
|
-
this.locMap = void 0;
|
|
31
|
-
this.HEAPU8 = void 0;
|
|
32
|
-
this.HEAPU32 = void 0;
|
|
33
|
-
this.HEAPF64 = void 0;
|
|
34
|
-
this.options = void 0;
|
|
35
|
-
this.commentTypes = ['CommentLine', 'CommentBlock', 'InterpreterDirective'];
|
|
36
|
-
this.tokenTypes = ['Boolean', 'Identifier', 'Keyword', 'Null', 'Numeric', 'BigInt', 'Punctuator', 'String', 'RegularExpression', 'Template', 'JSXText'];
|
|
37
|
-
// Program and position buffer are memory addresses, so we must convert
|
|
38
|
-
// into indices into HEAPU32 (an array of 4-byte integers).
|
|
39
|
-
this.programBufferIdx = programBuffer / 4;
|
|
40
|
-
this.positionBufferIdx = positionBuffer / 4;
|
|
41
|
-
this.positionBufferSize = positionBufferSize;
|
|
42
|
-
this.locMap = {};
|
|
43
|
-
this.HEAPU8 = wasmParser.HEAPU8;
|
|
44
|
-
this.HEAPU32 = wasmParser.HEAPU32;
|
|
45
|
-
this.HEAPF64 = wasmParser.HEAPF64;
|
|
46
|
-
this.options = options;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Consume and return the next 4 bytes in the program buffer.
|
|
50
|
-
*/
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
next() {
|
|
54
|
-
const num = this.HEAPU32[this.programBufferIdx++];
|
|
55
|
-
return num;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
deserialize() {
|
|
59
|
-
const program = {
|
|
60
|
-
type: 'Program',
|
|
61
|
-
loc: this.addEmptyLoc(),
|
|
62
|
-
body: this.deserializeNodeList(),
|
|
63
|
-
comments: this.deserializeComments()
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
if (this.options.tokens === true) {
|
|
67
|
-
program.tokens = this.deserializeTokens();
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
this.fillLocs();
|
|
71
|
-
return program;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Booleans are serialized as a single 4-byte integer.
|
|
75
|
-
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
deserializeBoolean() {
|
|
79
|
-
return Boolean(this.next());
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Numbers are serialized directly into program buffer, taking up 8 bytes
|
|
83
|
-
* preceded by 4 bytes of alignment padding if necessary.
|
|
84
|
-
*/
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
deserializeNumber() {
|
|
88
|
-
let floatIdx; // Numbers are aligned on 8-byte boundaries, so skip padding if we are at
|
|
89
|
-
// an odd index into the 4-byte aligned program buffer.
|
|
90
|
-
|
|
91
|
-
if (this.programBufferIdx % 2 === 0) {
|
|
92
|
-
floatIdx = this.programBufferIdx / 2;
|
|
93
|
-
this.programBufferIdx += 2;
|
|
94
|
-
} else {
|
|
95
|
-
floatIdx = (this.programBufferIdx + 1) / 2;
|
|
96
|
-
this.programBufferIdx += 3;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return this.HEAPF64[floatIdx];
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Strings are serialized as a 4-byte pointer into the heap, followed
|
|
103
|
-
* by their size as a 4-byte integer. The size is only present if the
|
|
104
|
-
* pointer is non-null.
|
|
105
|
-
*/
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
deserializeString() {
|
|
109
|
-
const ptr = this.next();
|
|
110
|
-
|
|
111
|
-
if (ptr === 0) {
|
|
112
|
-
return null;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const size = this.next();
|
|
116
|
-
return (0, _HermesParserDecodeUTF8String.default)(ptr, size, this.HEAPU8);
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Nodes are serialized as a 4-byte integer denoting their node kind,
|
|
120
|
-
* followed by a 4-byte loc ID, followed by serialized node properties.
|
|
121
|
-
*
|
|
122
|
-
* If the node kind is 0 the node is null, otherwise the node kind - 1 is an
|
|
123
|
-
* index into the array of node deserialization functions.
|
|
124
|
-
*/
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
deserializeNode() {
|
|
128
|
-
const nodeType = this.next();
|
|
129
|
-
|
|
130
|
-
if (nodeType === 0) {
|
|
131
|
-
return null;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const nodeDeserializer = _HermesParserNodeDeserializers.default[nodeType - 1].bind(this);
|
|
135
|
-
|
|
136
|
-
return nodeDeserializer();
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Node lists are serialized as a 4-byte integer denoting the number of
|
|
140
|
-
* elements in the list, followed by the serialized elements.
|
|
141
|
-
*/
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
deserializeNodeList() {
|
|
145
|
-
const size = this.next();
|
|
146
|
-
const nodeList = [];
|
|
147
|
-
|
|
148
|
-
for (let i = 0; i < size; i++) {
|
|
149
|
-
nodeList.push(this.deserializeNode());
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return nodeList;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Comments are serialized as a node list, where each comment is serialized
|
|
156
|
-
* as a 4-byte integer denoting comment type, followed by a 4-byte value
|
|
157
|
-
* denoting the loc ID, followed by a serialized string for the comment value.
|
|
158
|
-
*/
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
deserializeComments() {
|
|
162
|
-
const size = this.next();
|
|
163
|
-
const comments = [];
|
|
164
|
-
|
|
165
|
-
for (let i = 0; i < size; i++) {
|
|
166
|
-
const commentType = this.commentTypes[this.next()];
|
|
167
|
-
const loc = this.addEmptyLoc();
|
|
168
|
-
const value = this.deserializeString();
|
|
169
|
-
comments.push({
|
|
170
|
-
type: commentType,
|
|
171
|
-
loc,
|
|
172
|
-
value
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return comments;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
deserializeTokens() {
|
|
180
|
-
const size = this.next();
|
|
181
|
-
const tokens = [];
|
|
182
|
-
|
|
183
|
-
for (let i = 0; i < size; i++) {
|
|
184
|
-
const tokenType = this.tokenTypes[this.next()];
|
|
185
|
-
const loc = this.addEmptyLoc();
|
|
186
|
-
const value = this.deserializeString();
|
|
187
|
-
tokens.push({
|
|
188
|
-
type: tokenType,
|
|
189
|
-
loc,
|
|
190
|
-
value
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
return tokens;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* While deserializing the AST locations are represented by
|
|
198
|
-
* a 4-byte loc ID. This is used to create a map of loc IDs to empty loc
|
|
199
|
-
* objects that are filled after the AST has been deserialized.
|
|
200
|
-
*/
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
addEmptyLoc() {
|
|
204
|
-
const loc = {};
|
|
205
|
-
this.locMap[this.next()] = loc;
|
|
206
|
-
return loc;
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Positions are serialized as a loc ID which denotes which loc it is associated with,
|
|
210
|
-
* followed by kind which denotes whether it is a start or end position,
|
|
211
|
-
* followed by line, column, and offset (4-bytes each).
|
|
212
|
-
*/
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
fillLocs() {
|
|
216
|
-
for (let i = 0; i < this.positionBufferSize; i++) {
|
|
217
|
-
const locId = this.HEAPU32[this.positionBufferIdx++];
|
|
218
|
-
const kind = this.HEAPU32[this.positionBufferIdx++];
|
|
219
|
-
const line = this.HEAPU32[this.positionBufferIdx++];
|
|
220
|
-
const column = this.HEAPU32[this.positionBufferIdx++];
|
|
221
|
-
const offset = this.HEAPU32[this.positionBufferIdx++];
|
|
222
|
-
const loc = this.locMap[locId];
|
|
223
|
-
|
|
224
|
-
if (kind === 0) {
|
|
225
|
-
loc.start = {
|
|
226
|
-
line,
|
|
227
|
-
column
|
|
228
|
-
};
|
|
229
|
-
loc.rangeStart = offset;
|
|
230
|
-
} else {
|
|
231
|
-
loc.end = {
|
|
232
|
-
line,
|
|
233
|
-
column
|
|
234
|
-
};
|
|
235
|
-
loc.rangeEnd = offset;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
exports.default = HermesParserDeserializer;
|