hermes-parser 0.4.8 → 0.5.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.
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its 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
+ // flowlint unclear-type:off
12
+
13
+ export interface HermesPosition {
14
+ /** >= 1 */
15
+ +line: number;
16
+ /** >= 0 */
17
+ +column: number;
18
+ }
19
+ export type HermesSourceLocation = {
20
+ start?: HermesPosition,
21
+ end?: HermesPosition,
22
+ rangeStart?: number,
23
+ rangeEnd?: number,
24
+ };
25
+ export type HermesNode = {
26
+ type: string,
27
+ [string]: any,
28
+ };
29
+ export type HermesToken = {
30
+ type:
31
+ | 'Boolean'
32
+ | 'Identifier'
33
+ | 'Keyword'
34
+ | 'Null'
35
+ | 'Numeric'
36
+ | 'Punctuator'
37
+ | 'String'
38
+ | 'RegularExpression'
39
+ | 'Template'
40
+ | 'JSXText',
41
+ loc: HermesSourceLocation,
42
+ value: ?string,
43
+ };
44
+ export type HermesComment = {
45
+ type: 'CommentLine' | 'CommentBlock' | 'InterpreterDirective',
46
+ loc: HermesSourceLocation,
47
+ value: ?string,
48
+ };
49
+ export type HermesProgram = {
50
+ type: 'Program',
51
+ loc: HermesSourceLocation,
52
+ body: Array<?HermesNode>,
53
+ comments: Array<HermesComment>,
54
+ tokens?: Array<HermesToken>,
55
+ interpreter?: ?HermesComment,
56
+ };
@@ -1,12 +1,11 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its 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
- * @format
8
- */
9
- 'use strict';
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+
8
+ var _visitorKeys = require("./generated/visitor-keys");
10
9
 
11
10
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
12
11
 
@@ -14,20 +13,20 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+
14
13
 
15
14
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
16
15
 
17
- var _require = require('./HermesParserVisitorKeys'),
18
- HERMES_AST_VISITOR_KEYS = _require.HERMES_AST_VISITOR_KEYS,
19
- NODE_CHILD = _require.NODE_CHILD,
20
- NODE_LIST_CHILD = _require.NODE_LIST_CHILD;
16
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
17
+
21
18
  /**
22
19
  * The base class for transforming the Hermes AST to the desired output format.
23
20
  * Extended by concrete adapters which output an ESTree or Babel AST.
24
21
  */
25
-
26
-
27
22
  var HermesASTAdapter = /*#__PURE__*/function () {
28
- function HermesASTAdapter(options, code) {
23
+ function HermesASTAdapter(options) {
29
24
  _classCallCheck(this, HermesASTAdapter);
30
25
 
26
+ _defineProperty(this, "sourceFilename", void 0);
27
+
28
+ _defineProperty(this, "sourceType", void 0);
29
+
31
30
  this.sourceFilename = options.sourceFilename;
32
31
  this.sourceType = options.sourceType;
33
32
  }
@@ -71,25 +70,24 @@ var HermesASTAdapter = /*#__PURE__*/function () {
71
70
 
72
71
  }, {
73
72
  key: "mapNode",
74
- value: function mapNode(node) {
75
- this.fixSourceLocation(node);
76
- return this.mapNodeDefault(node);
73
+ value: function mapNode(_node) {
74
+ throw new Error('Implemented in subclasses');
77
75
  }
78
76
  }, {
79
77
  key: "mapNodeDefault",
80
78
  value: function mapNodeDefault(node) {
81
- var visitorKeys = HERMES_AST_VISITOR_KEYS[node.type];
79
+ var visitorKeys = _visitorKeys.HERMES_AST_VISITOR_KEYS[node.type];
82
80
 
83
81
  for (var key in visitorKeys) {
84
82
  var childType = visitorKeys[key];
85
83
 
86
- if (childType === NODE_CHILD) {
84
+ if (childType === _visitorKeys.NODE_CHILD) {
87
85
  var child = node[key];
88
86
 
89
87
  if (child != null) {
90
88
  node[key] = this.mapNode(child);
91
89
  }
92
- } else if (childType === NODE_LIST_CHILD) {
90
+ } else if (childType === _visitorKeys.NODE_LIST_CHILD) {
93
91
  var children = node[key];
94
92
 
95
93
  for (var i = 0; i < children.length; i++) {
@@ -111,7 +109,7 @@ var HermesASTAdapter = /*#__PURE__*/function () {
111
109
 
112
110
  }, {
113
111
  key: "fixSourceLocation",
114
- value: function fixSourceLocation(node) {
112
+ value: function fixSourceLocation(_node) {
115
113
  throw new Error('Implemented in subclasses');
116
114
  }
117
115
  }, {
@@ -135,7 +133,8 @@ var HermesASTAdapter = /*#__PURE__*/function () {
135
133
  }
136
134
  }, {
137
135
  key: "mapEmpty",
138
- value: function mapEmpty(node) {
136
+ value: function mapEmpty(_node) {
137
+ // $FlowExpectedError
139
138
  return null;
140
139
  }
141
140
  }, {
@@ -195,4 +194,4 @@ var HermesASTAdapter = /*#__PURE__*/function () {
195
194
  return HermesASTAdapter;
196
195
  }();
197
196
 
198
- module.exports = HermesASTAdapter;
197
+ exports["default"] = HermesASTAdapter;
@@ -0,0 +1,178 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its 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
8
+ * @format
9
+ */
10
+
11
+ /*
12
+ This class does some very "javascripty" things in the name of
13
+ performance which are ultimately impossible to soundly type.
14
+
15
+ So instead of adding strict types and a large number of suppression
16
+ comments, instead it is left untyped and subclasses are strictly
17
+ typed via a separate flow declaration file.
18
+ */
19
+
20
+ import type {HermesNode} from './HermesAST';
21
+ import type {ParserOptions} from './ParserOptions';
22
+
23
+ import {
24
+ HERMES_AST_VISITOR_KEYS,
25
+ NODE_CHILD,
26
+ NODE_LIST_CHILD,
27
+ } from './generated/visitor-keys';
28
+
29
+ /**
30
+ * The base class for transforming the Hermes AST to the desired output format.
31
+ * Extended by concrete adapters which output an ESTree or Babel AST.
32
+ */
33
+ export default class HermesASTAdapter {
34
+ sourceFilename: ParserOptions['sourceFilename'];
35
+ sourceType: ParserOptions['sourceType'];
36
+
37
+ constructor(options: ParserOptions) {
38
+ this.sourceFilename = options.sourceFilename;
39
+ this.sourceType = options.sourceType;
40
+ }
41
+
42
+ /**
43
+ * Transform the input Hermes AST to the desired output format.
44
+ * This modifies the input AST in place instead of constructing a new AST.
45
+ */
46
+ transform(program: HermesNode): ?HermesNode {
47
+ // Comments are not traversed via visitor keys
48
+ const comments = program.comments;
49
+ for (let i = 0; i < comments.length; i++) {
50
+ const comment = comments[i];
51
+ this.fixSourceLocation(comment);
52
+ comments[i] = this.mapComment(comment);
53
+ }
54
+
55
+ // The first comment may be an interpreter directive and is stored directly on the program node
56
+ program.interpreter =
57
+ comments.length > 0 && comments[0].type === 'InterpreterDirective'
58
+ ? comments.shift()
59
+ : null;
60
+
61
+ // Tokens are not traversed via visitor keys
62
+ const tokens = program.tokens;
63
+ if (tokens) {
64
+ for (let i = 0; i < tokens.length; i++) {
65
+ this.fixSourceLocation(tokens[i]);
66
+ }
67
+ }
68
+
69
+ return this.mapNode(program);
70
+ }
71
+
72
+ /**
73
+ * Transform a Hermes AST node to the output AST format.
74
+ *
75
+ * This may modify the input node in-place and return that same node, or a completely
76
+ * new node may be constructed and returned. Overriden in child classes.
77
+ */
78
+ mapNode(_node: HermesNode): HermesNode {
79
+ throw new Error('Implemented in subclasses');
80
+ }
81
+
82
+ mapNodeDefault(node: HermesNode): HermesNode {
83
+ const visitorKeys = HERMES_AST_VISITOR_KEYS[node.type];
84
+ for (const key in visitorKeys) {
85
+ const childType = visitorKeys[key];
86
+ if (childType === NODE_CHILD) {
87
+ const child = node[key];
88
+ if (child != null) {
89
+ node[key] = this.mapNode(child);
90
+ }
91
+ } else if (childType === NODE_LIST_CHILD) {
92
+ const children = node[key];
93
+ for (let i = 0; i < children.length; i++) {
94
+ const child = children[i];
95
+ if (child != null) {
96
+ children[i] = this.mapNode(child);
97
+ }
98
+ }
99
+ }
100
+ }
101
+
102
+ return node;
103
+ }
104
+
105
+ /**
106
+ * Update the source location for this node depending on the output AST format.
107
+ * This can modify the input node in-place. Overriden in child classes.
108
+ */
109
+ fixSourceLocation(_node: HermesNode): void {
110
+ throw new Error('Implemented in subclasses');
111
+ }
112
+
113
+ getSourceType(): ParserOptions['sourceType'] {
114
+ return this.sourceType ?? 'script';
115
+ }
116
+
117
+ setModuleSourceType(): void {
118
+ if (this.sourceType == null) {
119
+ this.sourceType = 'module';
120
+ }
121
+ }
122
+
123
+ mapComment(node: HermesNode): HermesNode {
124
+ return node;
125
+ }
126
+
127
+ mapEmpty(_node: HermesNode): HermesNode {
128
+ // $FlowExpectedError
129
+ return null;
130
+ }
131
+
132
+ mapImportDeclaration(node: HermesNode): HermesNode {
133
+ if (node.importKind === 'value') {
134
+ this.setModuleSourceType();
135
+ }
136
+
137
+ return this.mapNodeDefault(node);
138
+ }
139
+
140
+ mapImportSpecifier(node: HermesNode): HermesNode {
141
+ if (node.importKind === 'value') {
142
+ node.importKind = null;
143
+ }
144
+
145
+ return this.mapNodeDefault(node);
146
+ }
147
+
148
+ mapExportDefaultDeclaration(node: HermesNode): HermesNode {
149
+ this.setModuleSourceType();
150
+ return this.mapNodeDefault(node);
151
+ }
152
+
153
+ mapExportNamedDeclaration(node: HermesNode): HermesNode {
154
+ if (node.exportKind === 'value') {
155
+ this.setModuleSourceType();
156
+ }
157
+
158
+ return this.mapNodeDefault(node);
159
+ }
160
+
161
+ mapExportAllDeclaration(node: HermesNode): HermesNode {
162
+ if (node.exportKind === 'value') {
163
+ this.setModuleSourceType();
164
+ }
165
+
166
+ return this.mapNodeDefault(node);
167
+ }
168
+
169
+ mapPrivateProperty(node: HermesNode): HermesNode {
170
+ throw new SyntaxError(
171
+ this.formatError(node, 'Private properties are not supported'),
172
+ );
173
+ }
174
+
175
+ formatError(node: HermesNode, message: string): string {
176
+ return `${message} (${node.loc.start.line}:${node.loc.start.column})`;
177
+ }
178
+ }
@@ -4,33 +4,50 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
+ *
7
8
  * @format
8
9
  */
9
10
  'use strict';
10
11
 
11
- var HermesParserDeserializer = require('./HermesParserDeserializer');
12
+ Object.defineProperty(exports, "__esModule", {
13
+ value: true
14
+ });
15
+ exports.parse = parse;
12
16
 
13
- var HermesParserWASM = require('./HermesParserWASM');
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
+ var hermesParse = _HermesParserWASM["default"].cwrap('hermesParse', 'number', ['number', 'number', 'number', 'number', 'number']);
24
+
25
+ var hermesParseResult_free = _HermesParserWASM["default"].cwrap('hermesParseResult_free', 'void', ['number']);
26
+
27
+ var hermesParseResult_getError = _HermesParserWASM["default"].cwrap('hermesParseResult_getError', 'string', ['number']);
28
+
29
+ var hermesParseResult_getErrorLine = _HermesParserWASM["default"].cwrap('hermesParseResult_getErrorLine', 'number', ['number']);
30
+
31
+ var hermesParseResult_getErrorColumn = _HermesParserWASM["default"].cwrap('hermesParseResult_getErrorColumn', 'number', ['number']);
32
+
33
+ var hermesParseResult_getProgramBuffer = _HermesParserWASM["default"].cwrap('hermesParseResult_getProgramBuffer', 'number', ['number']);
34
+
35
+ var hermesParseResult_getPositionBuffer = _HermesParserWASM["default"].cwrap('hermesParseResult_getPositionBuffer', 'number', ['number']);
36
+
37
+ var hermesParseResult_getPositionBufferSize = _HermesParserWASM["default"].cwrap('hermesParseResult_getPositionBufferSize', 'number', ['number']); // Copy a string into the WASM heap and null-terminate
14
38
 
15
- var hermesParse = HermesParserWASM.cwrap('hermesParse', 'number', ['number', 'number', 'number', 'number', 'number']);
16
- var hermesParseResult_free = HermesParserWASM.cwrap('hermesParseResult_free', 'void', ['number']);
17
- var hermesParseResult_getError = HermesParserWASM.cwrap('hermesParseResult_getError', 'string', ['number']);
18
- var hermesParseResult_getErrorLine = HermesParserWASM.cwrap('hermesParseResult_getErrorLine', 'number', ['number']);
19
- var hermesParseResult_getErrorColumn = HermesParserWASM.cwrap('hermesParseResult_getErrorColumn', 'number', ['number']);
20
- var hermesParseResult_getProgramBuffer = HermesParserWASM.cwrap('hermesParseResult_getProgramBuffer', 'number', ['number']);
21
- var hermesParseResult_getPositionBuffer = HermesParserWASM.cwrap('hermesParseResult_getPositionBuffer', 'number', ['number']);
22
- var hermesParseResult_getPositionBufferSize = HermesParserWASM.cwrap('hermesParseResult_getPositionBufferSize', 'number', ['number']); // Copy a string into the WASM heap and null-terminate
23
39
 
24
40
  function copyToHeap(buffer, addr) {
25
- HermesParserWASM.HEAP8.set(buffer, addr);
26
- HermesParserWASM.HEAP8[addr + buffer.length] = 0;
41
+ _HermesParserWASM["default"].HEAP8.set(buffer, addr);
42
+
43
+ _HermesParserWASM["default"].HEAP8[addr + buffer.length] = 0;
27
44
  }
28
45
 
29
46
  function parse(source, options) {
30
47
  // Allocate space on heap for source text
31
48
  var sourceBuffer = Buffer.from(source, 'utf8');
32
49
 
33
- var sourceAddr = HermesParserWASM._malloc(sourceBuffer.length + 1);
50
+ var sourceAddr = _HermesParserWASM["default"]._malloc(sourceBuffer.length + 1);
34
51
 
35
52
  if (!sourceAddr) {
36
53
  throw new Error('Parser out of memory');
@@ -46,7 +63,8 @@ function parse(source, options) {
46
63
  var err = hermesParseResult_getError(parseResult);
47
64
 
48
65
  if (err) {
49
- var syntaxError = new SyntaxError(err);
66
+ var syntaxError = new SyntaxError(err); // $FlowExpectedError[prop-missing]
67
+
50
68
  syntaxError.loc = {
51
69
  line: hermesParseResult_getErrorLine(parseResult),
52
70
  column: hermesParseResult_getErrorColumn(parseResult)
@@ -54,16 +72,12 @@ function parse(source, options) {
54
72
  throw syntaxError;
55
73
  }
56
74
 
57
- var deserializer = new HermesParserDeserializer(hermesParseResult_getProgramBuffer(parseResult), hermesParseResult_getPositionBuffer(parseResult), hermesParseResult_getPositionBufferSize(parseResult), HermesParserWASM, options);
75
+ var deserializer = new _HermesParserDeserializer["default"](hermesParseResult_getProgramBuffer(parseResult), hermesParseResult_getPositionBuffer(parseResult), hermesParseResult_getPositionBufferSize(parseResult), _HermesParserWASM["default"], options);
58
76
  return deserializer.deserialize();
59
77
  } finally {
60
78
  hermesParseResult_free(parseResult);
61
79
  }
62
80
  } finally {
63
- HermesParserWASM._free(sourceAddr);
81
+ _HermesParserWASM["default"]._free(sourceAddr);
64
82
  }
65
- }
66
-
67
- module.exports = {
68
- parse: parse
69
- };
83
+ }
@@ -0,0 +1,123 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its 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 HermesParserWASM from './HermesParserWASM';
18
+
19
+ const hermesParse = HermesParserWASM.cwrap('hermesParse', 'number', [
20
+ 'number',
21
+ 'number',
22
+ 'number',
23
+ 'number',
24
+ 'number',
25
+ ]);
26
+
27
+ const hermesParseResult_free = HermesParserWASM.cwrap(
28
+ 'hermesParseResult_free',
29
+ 'void',
30
+ ['number'],
31
+ );
32
+
33
+ const hermesParseResult_getError = HermesParserWASM.cwrap(
34
+ 'hermesParseResult_getError',
35
+ 'string',
36
+ ['number'],
37
+ );
38
+
39
+ const hermesParseResult_getErrorLine = HermesParserWASM.cwrap(
40
+ 'hermesParseResult_getErrorLine',
41
+ 'number',
42
+ ['number'],
43
+ );
44
+
45
+ const hermesParseResult_getErrorColumn = HermesParserWASM.cwrap(
46
+ 'hermesParseResult_getErrorColumn',
47
+ 'number',
48
+ ['number'],
49
+ );
50
+
51
+ const hermesParseResult_getProgramBuffer = HermesParserWASM.cwrap(
52
+ 'hermesParseResult_getProgramBuffer',
53
+ 'number',
54
+ ['number'],
55
+ );
56
+
57
+ const hermesParseResult_getPositionBuffer = HermesParserWASM.cwrap(
58
+ 'hermesParseResult_getPositionBuffer',
59
+ 'number',
60
+ ['number'],
61
+ );
62
+
63
+ const hermesParseResult_getPositionBufferSize = HermesParserWASM.cwrap(
64
+ 'hermesParseResult_getPositionBufferSize',
65
+ 'number',
66
+ ['number'],
67
+ );
68
+
69
+ // Copy a string into the WASM heap and null-terminate
70
+ function copyToHeap(buffer, addr) {
71
+ HermesParserWASM.HEAP8.set(buffer, addr);
72
+ HermesParserWASM.HEAP8[addr + buffer.length] = 0;
73
+ }
74
+
75
+ export function parse(source: string, options: ParserOptions): HermesNode {
76
+ // Allocate space on heap for source text
77
+ const sourceBuffer = Buffer.from(source, 'utf8');
78
+ const sourceAddr = HermesParserWASM._malloc(sourceBuffer.length + 1);
79
+ if (!sourceAddr) {
80
+ throw new Error('Parser out of memory');
81
+ }
82
+
83
+ try {
84
+ // Copy source text onto WASM heap
85
+ copyToHeap(sourceBuffer, sourceAddr);
86
+
87
+ const parseResult = hermesParse(
88
+ sourceAddr,
89
+ sourceBuffer.length + 1,
90
+ options.flow === 'detect',
91
+ options.tokens,
92
+ options.allowReturnOutsideFunction,
93
+ );
94
+
95
+ try {
96
+ // Extract and throw error from parse result if parsing failed
97
+ const err = hermesParseResult_getError(parseResult);
98
+ if (err) {
99
+ const syntaxError = new SyntaxError(err);
100
+ // $FlowExpectedError[prop-missing]
101
+ syntaxError.loc = {
102
+ line: hermesParseResult_getErrorLine(parseResult),
103
+ column: hermesParseResult_getErrorColumn(parseResult),
104
+ };
105
+
106
+ throw syntaxError;
107
+ }
108
+
109
+ const deserializer = new HermesParserDeserializer(
110
+ hermesParseResult_getProgramBuffer(parseResult),
111
+ hermesParseResult_getPositionBuffer(parseResult),
112
+ hermesParseResult_getPositionBufferSize(parseResult),
113
+ HermesParserWASM,
114
+ options,
115
+ );
116
+ return deserializer.deserialize();
117
+ } finally {
118
+ hermesParseResult_free(parseResult);
119
+ }
120
+ } finally {
121
+ HermesParserWASM._free(sourceAddr);
122
+ }
123
+ }
@@ -4,6 +4,7 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
+ *
7
8
  * @format
8
9
  */
9
10
  'use strict';
@@ -16,7 +17,13 @@
16
17
  * this is technically invalid UTF-8 that UTF8ToString would convert to 0xfffd.
17
18
  */
18
19
 
19
- function HermesParserDecodeUTF8String(ptr, length, heap) {
20
+ Object.defineProperty(exports, "__esModule", {
21
+ value: true
22
+ });
23
+ exports["default"] = HermesParserDecodeUTF8String;
24
+
25
+ function HermesParserDecodeUTF8String(ptrIn, length, heap) {
26
+ var ptr = ptrIn;
20
27
  var endPtr = ptr + length;
21
28
  var str = '';
22
29
 
@@ -58,6 +65,4 @@ function HermesParserDecodeUTF8String(ptr, length, heap) {
58
65
  }
59
66
 
60
67
  return str;
61
- }
62
-
63
- module.exports = HermesParserDecodeUTF8String;
68
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its 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
+ }