rapydscript-ns 0.8.3 → 0.8.4

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.
Files changed (116) hide show
  1. package/.agignore +1 -1
  2. package/.github/workflows/ci.yml +38 -38
  3. package/=template.pyj +5 -5
  4. package/CHANGELOG.md +8 -0
  5. package/HACKING.md +103 -103
  6. package/LICENSE +24 -24
  7. package/PYTHON_DIFFERENCES_REPORT.md +2 -2
  8. package/PYTHON_FEATURE_COVERAGE.md +13 -13
  9. package/README.md +670 -6
  10. package/TODO.md +5 -6
  11. package/add-toc-to-readme +2 -2
  12. package/bin/export +75 -75
  13. package/bin/rapydscript +70 -70
  14. package/bin/web-repl-export +102 -102
  15. package/build +2 -2
  16. package/language-service/index.js +155 -6
  17. package/package.json +1 -1
  18. package/publish.py +37 -37
  19. package/release/baselib-plain-pretty.js +2006 -229
  20. package/release/baselib-plain-ugly.js +70 -3
  21. package/release/compiler.js +11554 -3870
  22. package/release/signatures.json +31 -29
  23. package/session.vim +4 -4
  24. package/setup.cfg +2 -2
  25. package/src/ast.pyj +93 -1
  26. package/src/baselib-builtins.pyj +22 -1
  27. package/src/baselib-containers.pyj +99 -0
  28. package/src/baselib-errors.pyj +44 -0
  29. package/src/baselib-internal.pyj +94 -4
  30. package/src/baselib-itertools.pyj +97 -97
  31. package/src/baselib-str.pyj +24 -0
  32. package/src/compiler.pyj +36 -36
  33. package/src/errors.pyj +30 -30
  34. package/src/lib/aes.pyj +646 -646
  35. package/src/lib/copy.pyj +120 -0
  36. package/src/lib/elementmaker.pyj +83 -83
  37. package/src/lib/encodings.pyj +126 -126
  38. package/src/lib/gettext.pyj +569 -569
  39. package/src/lib/itertools.pyj +580 -580
  40. package/src/lib/math.pyj +193 -193
  41. package/src/lib/operator.pyj +11 -11
  42. package/src/lib/pythonize.pyj +20 -20
  43. package/src/lib/random.pyj +118 -118
  44. package/src/lib/re.pyj +470 -470
  45. package/src/lib/react.pyj +74 -0
  46. package/src/lib/traceback.pyj +63 -63
  47. package/src/lib/uuid.pyj +77 -77
  48. package/src/monaco-language-service/builtins.js +5 -0
  49. package/src/monaco-language-service/diagnostics.js +25 -3
  50. package/src/monaco-language-service/dts.js +550 -550
  51. package/src/output/classes.pyj +108 -8
  52. package/src/output/codegen.pyj +16 -2
  53. package/src/output/comments.pyj +45 -45
  54. package/src/output/exceptions.pyj +201 -105
  55. package/src/output/functions.pyj +9 -0
  56. package/src/output/jsx.pyj +164 -0
  57. package/src/output/literals.pyj +28 -2
  58. package/src/output/modules.pyj +1 -1
  59. package/src/output/operators.pyj +8 -2
  60. package/src/output/statements.pyj +2 -2
  61. package/src/output/stream.pyj +1 -0
  62. package/src/output/treeshake.pyj +182 -182
  63. package/src/output/utils.pyj +72 -72
  64. package/src/parse.pyj +417 -113
  65. package/src/string_interpolation.pyj +72 -72
  66. package/src/tokenizer.pyj +29 -0
  67. package/src/unicode_aliases.pyj +576 -576
  68. package/src/utils.pyj +192 -192
  69. package/test/_import_one.pyj +37 -37
  70. package/test/_import_two/__init__.pyj +11 -11
  71. package/test/_import_two/level2/deep.pyj +4 -4
  72. package/test/_import_two/other.pyj +6 -6
  73. package/test/_import_two/sub.pyj +13 -13
  74. package/test/aes_vectors.pyj +421 -421
  75. package/test/annotations.pyj +80 -80
  76. package/test/decorators.pyj +77 -77
  77. package/test/docstrings.pyj +39 -39
  78. package/test/elementmaker_test.pyj +45 -45
  79. package/test/functions.pyj +151 -151
  80. package/test/generators.pyj +41 -41
  81. package/test/generic.pyj +370 -370
  82. package/test/imports.pyj +72 -72
  83. package/test/internationalization.pyj +73 -73
  84. package/test/lint.pyj +164 -164
  85. package/test/loops.pyj +85 -85
  86. package/test/numpy.pyj +734 -734
  87. package/test/omit_function_metadata.pyj +20 -20
  88. package/test/python_features.pyj +19 -6
  89. package/test/regexp.pyj +55 -55
  90. package/test/repl.pyj +121 -121
  91. package/test/scoped_flags.pyj +76 -76
  92. package/test/unit/index.js +2177 -64
  93. package/test/unit/language-service-dts.js +543 -543
  94. package/test/unit/language-service-hover.js +455 -455
  95. package/test/unit/language-service.js +590 -4
  96. package/test/unit/web-repl.js +303 -0
  97. package/tools/cli.js +547 -547
  98. package/tools/compile.js +219 -219
  99. package/tools/completer.js +131 -131
  100. package/tools/embedded_compiler.js +251 -251
  101. package/tools/gettext.js +185 -185
  102. package/tools/ini.js +65 -65
  103. package/tools/msgfmt.js +187 -187
  104. package/tools/repl.js +223 -223
  105. package/tools/test.js +118 -118
  106. package/tools/utils.js +128 -128
  107. package/tools/web_repl.js +95 -95
  108. package/try +41 -41
  109. package/web-repl/env.js +196 -74
  110. package/web-repl/index.html +163 -163
  111. package/web-repl/main.js +252 -254
  112. package/web-repl/prism.css +139 -139
  113. package/web-repl/prism.js +113 -113
  114. package/web-repl/rapydscript.js +224 -102
  115. package/web-repl/sha1.js +25 -25
  116. package/hack_demo.pyj +0 -112
@@ -1,251 +1,251 @@
1
- /* vim:fileencoding=utf-8
2
- *
3
- * Copyright (C) 2016 Kovid Goyal <kovid at kovidgoyal.net>
4
- *
5
- * Distributed under terms of the BSD license
6
- */
7
- "use strict"; /*jshint node:true */
8
-
9
- var has_prop = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty);
10
-
11
- function build_scoped_flags(flags_str) {
12
- var result = Object.create(null);
13
- if (!flags_str) return result;
14
- flags_str.split(',').forEach(function(flag) {
15
- flag = flag.trim();
16
- if (!flag) return;
17
- var val = true;
18
- if (flag.slice(0, 3) === 'no_') { val = false; flag = flag.slice(3); }
19
- result[flag] = val;
20
- });
21
- return result;
22
- }
23
-
24
- module.exports = function(compiler, baselib, runjs, name, vf_context) {
25
- var LINE_CONTINUATION_CHARS = ':\\';
26
- runjs = runjs || eval;
27
- runjs(print_ast(compiler.parse(''), true));
28
- runjs('var __name__ = "' + (name || '__embedded__') + '";');
29
-
30
- function print_ast(ast, keep_baselib, keep_docstrings, js_version, private_scope, write_name, omit_function_metadata, pythonize_strings) {
31
- var output_options = {omit_baselib:!keep_baselib, write_name:!!write_name, private_scope:!!private_scope, beautify:true, js_version: (js_version || 6), keep_docstrings:keep_docstrings, omit_function_metadata:!!omit_function_metadata, pythonize_strings:!!pythonize_strings};
32
- if (keep_baselib) output_options.baselib_plain = baselib;
33
- var output = new compiler.OutputStream(output_options);
34
- ast.print(output);
35
- return output.get();
36
- }
37
-
38
- // --- Source map support ---
39
-
40
- function vlq_encode(value) {
41
- var BASE64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
42
- var vlq = value < 0 ? ((-value) << 1) | 1 : value << 1;
43
- var result = '';
44
- do {
45
- var digit = vlq & 31;
46
- vlq >>>= 5;
47
- if (vlq > 0) digit |= 32;
48
- result += BASE64[digit];
49
- } while (vlq > 0);
50
- return result;
51
- }
52
-
53
- function build_source_map(raw_mappings, source_name, source_content) {
54
- if (!raw_mappings.length) {
55
- return JSON.stringify({version:3, sources:[source_name], sourcesContent:[source_content||null], names:[], mappings:''});
56
- }
57
- raw_mappings.sort(function(a, b) {
58
- return a.gen_line !== b.gen_line ? a.gen_line - b.gen_line : a.gen_col - b.gen_col;
59
- });
60
- // Deduplicate same generated position (keep first)
61
- var deduped = [];
62
- var prev_key = null;
63
- for (var i = 0; i < raw_mappings.length; i++) {
64
- var m = raw_mappings[i];
65
- var key = m.gen_line + ':' + m.gen_col;
66
- if (key !== prev_key) { deduped.push(m); prev_key = key; }
67
- }
68
- var max_gen_line = deduped[deduped.length - 1].gen_line;
69
- var by_line = Object.create(null);
70
- for (var i = 0; i < deduped.length; i++) {
71
- var m = deduped[i];
72
- if (!by_line[m.gen_line]) by_line[m.gen_line] = [];
73
- by_line[m.gen_line].push(m);
74
- }
75
- // prev_src_* track deltas across all generated lines (Source Map v3 spec)
76
- var prev_src_line_0 = 0;
77
- var prev_src_col = 0;
78
- var lines_out = [];
79
- for (var line = 1; line <= max_gen_line; line++) {
80
- var prev_gen_col = 0;
81
- var segs = [];
82
- var lm = by_line[line] || [];
83
- for (var j = 0; j < lm.length; j++) {
84
- var m = lm[j];
85
- var src_line_0 = m.src_line - 1;
86
- segs.push(
87
- vlq_encode(m.gen_col - prev_gen_col) +
88
- vlq_encode(0) +
89
- vlq_encode(src_line_0 - prev_src_line_0) +
90
- vlq_encode(m.src_col - prev_src_col)
91
- );
92
- prev_gen_col = m.gen_col;
93
- prev_src_line_0 = src_line_0;
94
- prev_src_col = m.src_col;
95
- }
96
- lines_out.push(segs.join(','));
97
- }
98
- return JSON.stringify({
99
- version: 3,
100
- sources: [source_name],
101
- sourcesContent: [source_content || null],
102
- names: [],
103
- mappings: lines_out.join(';'),
104
- });
105
- }
106
-
107
- function print_ast_with_sourcemap(ast, keep_baselib, keep_docstrings, js_version, private_scope, write_name, omit_function_metadata, pythonize_strings, source_name, source_content) {
108
- var output_options = {omit_baselib:!keep_baselib, write_name:!!write_name, private_scope:!!private_scope, beautify:true, js_version:(js_version||6), keep_docstrings:keep_docstrings, omit_function_metadata:!!omit_function_metadata, pythonize_strings:!!pythonize_strings};
109
- if (keep_baselib) output_options.baselib_plain = baselib;
110
- var raw_mappings = [];
111
- var output = new compiler.OutputStream(output_options);
112
- output.push_node = function(node) {
113
- if (node && node.start && node.start.line) {
114
- raw_mappings.push({
115
- gen_line: this.current_line,
116
- gen_col: this.current_col,
117
- src_line: node.start.line,
118
- src_col: node.start.col,
119
- });
120
- }
121
- this._stack.push(node);
122
- };
123
- ast.print(output);
124
- return {
125
- code: output.get(),
126
- sourceMap: build_source_map(raw_mappings, source_name, source_content),
127
- };
128
- }
129
-
130
- return {
131
- 'toplevel': null,
132
-
133
- 'compile': function streaming_compile(code, opts) {
134
- opts = opts || {};
135
- var classes = (this.toplevel) ? this.toplevel.classes : undefined;
136
- var inherited_flags = (this.toplevel) ? this.toplevel.scoped_flags : undefined;
137
- var scoped_flags = Object.assign(
138
- Object.create(null),
139
- opts.python_flags ? build_scoped_flags(opts.python_flags) : {},
140
- inherited_flags || {}
141
- );
142
- var vf = (opts.virtual_files && vf_context) ? opts.virtual_files : null;
143
- var parse_opts = {
144
- 'filename': opts.filename || '<embedded>',
145
- 'basedir': '__stdlib__',
146
- 'classes': classes,
147
- 'scoped_flags': scoped_flags,
148
- 'discard_asserts': opts.discard_asserts,
149
- };
150
- if (vf) {
151
- vf_context.set(vf);
152
- parse_opts.import_dirs = ['__virtual__'];
153
- }
154
- try {
155
- this.toplevel = compiler.parse(code, parse_opts);
156
- } finally {
157
- if (vf) vf_context.clear();
158
- }
159
- if (opts.tree_shake && compiler.tree_shake &&
160
- this.toplevel.imports && Object.keys(this.toplevel.imports).length) {
161
- compiler.tree_shake(this.toplevel, {
162
- parse: compiler.parse,
163
- import_dirs: [],
164
- basedir: undefined,
165
- libdir: undefined,
166
- });
167
- }
168
- var ans = print_ast(this.toplevel, opts.keep_baselib, opts.keep_docstrings, opts.js_version, opts.private_scope, opts.write_name, opts.omit_function_metadata, opts.pythonize_strings);
169
- if (opts.export_main) {
170
- ans = ans.replace(/^(function\smain)/gm, 'export $1')
171
- .replace(/^(async\sfunction\smain)/gm, 'export $1');
172
- }
173
- if (classes) {
174
- var exports = {};
175
- var self = this;
176
- this.toplevel.exports.forEach(function (name) { exports[name] = true; });
177
- Object.getOwnPropertyNames(classes).forEach(function (name) {
178
- if (!has_prop(exports, name) && !has_prop(self.toplevel.classes, name))
179
- self.toplevel.classes[name] = classes[name];
180
- });
181
- }
182
- scoped_flags = this.toplevel.scoped_flags;
183
-
184
- return ans;
185
- },
186
-
187
- 'compile_with_sourcemap': function compile_with_sourcemap(code, opts) {
188
- opts = opts || {};
189
- var classes = (this.toplevel) ? this.toplevel.classes : undefined;
190
- var inherited_flags = (this.toplevel) ? this.toplevel.scoped_flags : undefined;
191
- var scoped_flags = Object.assign(
192
- Object.create(null),
193
- opts.python_flags ? build_scoped_flags(opts.python_flags) : {},
194
- inherited_flags || {}
195
- );
196
- var vf = (opts.virtual_files && vf_context) ? opts.virtual_files : null;
197
- var parse_opts = {
198
- 'filename': opts.filename || '<embedded>',
199
- 'basedir': '__stdlib__',
200
- 'classes': classes,
201
- 'scoped_flags': scoped_flags,
202
- 'discard_asserts': opts.discard_asserts,
203
- };
204
- if (vf) {
205
- vf_context.set(vf);
206
- parse_opts.import_dirs = ['__virtual__'];
207
- }
208
- try {
209
- this.toplevel = compiler.parse(code, parse_opts);
210
- } finally {
211
- if (vf) vf_context.clear();
212
- }
213
- if (opts.tree_shake && compiler.tree_shake &&
214
- this.toplevel.imports && Object.keys(this.toplevel.imports).length) {
215
- compiler.tree_shake(this.toplevel, {
216
- parse: compiler.parse,
217
- import_dirs: [],
218
- basedir: undefined,
219
- libdir: undefined,
220
- });
221
- }
222
- var result = print_ast_with_sourcemap(
223
- this.toplevel,
224
- opts.keep_baselib, opts.keep_docstrings, opts.js_version,
225
- opts.private_scope, opts.write_name, opts.omit_function_metadata,
226
- opts.pythonize_strings,
227
- opts.filename || '<input>',
228
- code
229
- );
230
- var compiled_code = result.code;
231
- if (opts.export_main) {
232
- compiled_code = compiled_code
233
- .replace(/^(function\smain)/gm, 'export $1')
234
- .replace(/^(async\sfunction\smain)/gm, 'export $1');
235
- }
236
- if (classes) {
237
- var exports_map = {};
238
- var self_ref = this;
239
- this.toplevel.exports.forEach(function(name) { exports_map[name] = true; });
240
- Object.getOwnPropertyNames(classes).forEach(function(name) {
241
- if (!has_prop(exports_map, name) && !has_prop(self_ref.toplevel.classes, name))
242
- self_ref.toplevel.classes[name] = classes[name];
243
- });
244
- }
245
- scoped_flags = this.toplevel.scoped_flags;
246
- return { code: compiled_code, sourceMap: result.sourceMap };
247
- },
248
-
249
- };
250
- };
251
-
1
+ /* vim:fileencoding=utf-8
2
+ *
3
+ * Copyright (C) 2016 Kovid Goyal <kovid at kovidgoyal.net>
4
+ *
5
+ * Distributed under terms of the BSD license
6
+ */
7
+ "use strict"; /*jshint node:true */
8
+
9
+ var has_prop = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty);
10
+
11
+ function build_scoped_flags(flags_str) {
12
+ var result = Object.create(null);
13
+ if (!flags_str) return result;
14
+ flags_str.split(',').forEach(function(flag) {
15
+ flag = flag.trim();
16
+ if (!flag) return;
17
+ var val = true;
18
+ if (flag.slice(0, 3) === 'no_') { val = false; flag = flag.slice(3); }
19
+ result[flag] = val;
20
+ });
21
+ return result;
22
+ }
23
+
24
+ module.exports = function(compiler, baselib, runjs, name, vf_context) {
25
+ var LINE_CONTINUATION_CHARS = ':\\';
26
+ runjs = runjs || eval;
27
+ runjs(print_ast(compiler.parse(''), true));
28
+ runjs('var __name__ = "' + (name || '__embedded__') + '";');
29
+
30
+ function print_ast(ast, keep_baselib, keep_docstrings, js_version, private_scope, write_name, omit_function_metadata, pythonize_strings) {
31
+ var output_options = {omit_baselib:!keep_baselib, write_name:!!write_name, private_scope:!!private_scope, beautify:true, js_version: (js_version || 6), keep_docstrings:keep_docstrings, omit_function_metadata:!!omit_function_metadata, pythonize_strings:!!pythonize_strings};
32
+ if (keep_baselib) output_options.baselib_plain = baselib;
33
+ var output = new compiler.OutputStream(output_options);
34
+ ast.print(output);
35
+ return output.get();
36
+ }
37
+
38
+ // --- Source map support ---
39
+
40
+ function vlq_encode(value) {
41
+ var BASE64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
42
+ var vlq = value < 0 ? ((-value) << 1) | 1 : value << 1;
43
+ var result = '';
44
+ do {
45
+ var digit = vlq & 31;
46
+ vlq >>>= 5;
47
+ if (vlq > 0) digit |= 32;
48
+ result += BASE64[digit];
49
+ } while (vlq > 0);
50
+ return result;
51
+ }
52
+
53
+ function build_source_map(raw_mappings, source_name, source_content) {
54
+ if (!raw_mappings.length) {
55
+ return JSON.stringify({version:3, sources:[source_name], sourcesContent:[source_content||null], names:[], mappings:''});
56
+ }
57
+ raw_mappings.sort(function(a, b) {
58
+ return a.gen_line !== b.gen_line ? a.gen_line - b.gen_line : a.gen_col - b.gen_col;
59
+ });
60
+ // Deduplicate same generated position (keep first)
61
+ var deduped = [];
62
+ var prev_key = null;
63
+ for (var i = 0; i < raw_mappings.length; i++) {
64
+ var m = raw_mappings[i];
65
+ var key = m.gen_line + ':' + m.gen_col;
66
+ if (key !== prev_key) { deduped.push(m); prev_key = key; }
67
+ }
68
+ var max_gen_line = deduped[deduped.length - 1].gen_line;
69
+ var by_line = Object.create(null);
70
+ for (var i = 0; i < deduped.length; i++) {
71
+ var m = deduped[i];
72
+ if (!by_line[m.gen_line]) by_line[m.gen_line] = [];
73
+ by_line[m.gen_line].push(m);
74
+ }
75
+ // prev_src_* track deltas across all generated lines (Source Map v3 spec)
76
+ var prev_src_line_0 = 0;
77
+ var prev_src_col = 0;
78
+ var lines_out = [];
79
+ for (var line = 1; line <= max_gen_line; line++) {
80
+ var prev_gen_col = 0;
81
+ var segs = [];
82
+ var lm = by_line[line] || [];
83
+ for (var j = 0; j < lm.length; j++) {
84
+ var m = lm[j];
85
+ var src_line_0 = m.src_line - 1;
86
+ segs.push(
87
+ vlq_encode(m.gen_col - prev_gen_col) +
88
+ vlq_encode(0) +
89
+ vlq_encode(src_line_0 - prev_src_line_0) +
90
+ vlq_encode(m.src_col - prev_src_col)
91
+ );
92
+ prev_gen_col = m.gen_col;
93
+ prev_src_line_0 = src_line_0;
94
+ prev_src_col = m.src_col;
95
+ }
96
+ lines_out.push(segs.join(','));
97
+ }
98
+ return JSON.stringify({
99
+ version: 3,
100
+ sources: [source_name],
101
+ sourcesContent: [source_content || null],
102
+ names: [],
103
+ mappings: lines_out.join(';'),
104
+ });
105
+ }
106
+
107
+ function print_ast_with_sourcemap(ast, keep_baselib, keep_docstrings, js_version, private_scope, write_name, omit_function_metadata, pythonize_strings, source_name, source_content) {
108
+ var output_options = {omit_baselib:!keep_baselib, write_name:!!write_name, private_scope:!!private_scope, beautify:true, js_version:(js_version||6), keep_docstrings:keep_docstrings, omit_function_metadata:!!omit_function_metadata, pythonize_strings:!!pythonize_strings};
109
+ if (keep_baselib) output_options.baselib_plain = baselib;
110
+ var raw_mappings = [];
111
+ var output = new compiler.OutputStream(output_options);
112
+ output.push_node = function(node) {
113
+ if (node && node.start && node.start.line) {
114
+ raw_mappings.push({
115
+ gen_line: this.current_line,
116
+ gen_col: this.current_col,
117
+ src_line: node.start.line,
118
+ src_col: node.start.col,
119
+ });
120
+ }
121
+ this._stack.push(node);
122
+ };
123
+ ast.print(output);
124
+ return {
125
+ code: output.get(),
126
+ sourceMap: build_source_map(raw_mappings, source_name, source_content),
127
+ };
128
+ }
129
+
130
+ return {
131
+ 'toplevel': null,
132
+
133
+ 'compile': function streaming_compile(code, opts) {
134
+ opts = opts || {};
135
+ var classes = (this.toplevel) ? this.toplevel.classes : undefined;
136
+ var inherited_flags = (this.toplevel) ? this.toplevel.scoped_flags : undefined;
137
+ var scoped_flags = Object.assign(
138
+ Object.create(null),
139
+ opts.python_flags ? build_scoped_flags(opts.python_flags) : {},
140
+ inherited_flags || {}
141
+ );
142
+ var vf = (opts.virtual_files && vf_context) ? opts.virtual_files : null;
143
+ var parse_opts = {
144
+ 'filename': opts.filename || '<embedded>',
145
+ 'basedir': '__stdlib__',
146
+ 'classes': classes,
147
+ 'scoped_flags': scoped_flags,
148
+ 'discard_asserts': opts.discard_asserts,
149
+ };
150
+ if (vf) {
151
+ vf_context.set(vf);
152
+ parse_opts.import_dirs = ['__virtual__'];
153
+ }
154
+ try {
155
+ this.toplevel = compiler.parse(code, parse_opts);
156
+ } finally {
157
+ if (vf) vf_context.clear();
158
+ }
159
+ if (opts.tree_shake && compiler.tree_shake &&
160
+ this.toplevel.imports && Object.keys(this.toplevel.imports).length) {
161
+ compiler.tree_shake(this.toplevel, {
162
+ parse: compiler.parse,
163
+ import_dirs: [],
164
+ basedir: undefined,
165
+ libdir: undefined,
166
+ });
167
+ }
168
+ var ans = print_ast(this.toplevel, opts.keep_baselib, opts.keep_docstrings, opts.js_version, opts.private_scope, opts.write_name, opts.omit_function_metadata, opts.pythonize_strings);
169
+ if (opts.export_main) {
170
+ ans = ans.replace(/^(function\smain)/gm, 'export $1')
171
+ .replace(/^(async\sfunction\smain)/gm, 'export $1');
172
+ }
173
+ if (classes) {
174
+ var exports = {};
175
+ var self = this;
176
+ this.toplevel.exports.forEach(function (name) { exports[name] = true; });
177
+ Object.getOwnPropertyNames(classes).forEach(function (name) {
178
+ if (!has_prop(exports, name) && !has_prop(self.toplevel.classes, name))
179
+ self.toplevel.classes[name] = classes[name];
180
+ });
181
+ }
182
+ scoped_flags = this.toplevel.scoped_flags;
183
+
184
+ return ans;
185
+ },
186
+
187
+ 'compile_with_sourcemap': function compile_with_sourcemap(code, opts) {
188
+ opts = opts || {};
189
+ var classes = (this.toplevel) ? this.toplevel.classes : undefined;
190
+ var inherited_flags = (this.toplevel) ? this.toplevel.scoped_flags : undefined;
191
+ var scoped_flags = Object.assign(
192
+ Object.create(null),
193
+ opts.python_flags ? build_scoped_flags(opts.python_flags) : {},
194
+ inherited_flags || {}
195
+ );
196
+ var vf = (opts.virtual_files && vf_context) ? opts.virtual_files : null;
197
+ var parse_opts = {
198
+ 'filename': opts.filename || '<embedded>',
199
+ 'basedir': '__stdlib__',
200
+ 'classes': classes,
201
+ 'scoped_flags': scoped_flags,
202
+ 'discard_asserts': opts.discard_asserts,
203
+ };
204
+ if (vf) {
205
+ vf_context.set(vf);
206
+ parse_opts.import_dirs = ['__virtual__'];
207
+ }
208
+ try {
209
+ this.toplevel = compiler.parse(code, parse_opts);
210
+ } finally {
211
+ if (vf) vf_context.clear();
212
+ }
213
+ if (opts.tree_shake && compiler.tree_shake &&
214
+ this.toplevel.imports && Object.keys(this.toplevel.imports).length) {
215
+ compiler.tree_shake(this.toplevel, {
216
+ parse: compiler.parse,
217
+ import_dirs: [],
218
+ basedir: undefined,
219
+ libdir: undefined,
220
+ });
221
+ }
222
+ var result = print_ast_with_sourcemap(
223
+ this.toplevel,
224
+ opts.keep_baselib, opts.keep_docstrings, opts.js_version,
225
+ opts.private_scope, opts.write_name, opts.omit_function_metadata,
226
+ opts.pythonize_strings,
227
+ opts.filename || '<input>',
228
+ code
229
+ );
230
+ var compiled_code = result.code;
231
+ if (opts.export_main) {
232
+ compiled_code = compiled_code
233
+ .replace(/^(function\smain)/gm, 'export $1')
234
+ .replace(/^(async\sfunction\smain)/gm, 'export $1');
235
+ }
236
+ if (classes) {
237
+ var exports_map = {};
238
+ var self_ref = this;
239
+ this.toplevel.exports.forEach(function(name) { exports_map[name] = true; });
240
+ Object.getOwnPropertyNames(classes).forEach(function(name) {
241
+ if (!has_prop(exports_map, name) && !has_prop(self_ref.toplevel.classes, name))
242
+ self_ref.toplevel.classes[name] = classes[name];
243
+ });
244
+ }
245
+ scoped_flags = this.toplevel.scoped_flags;
246
+ return { code: compiled_code, sourceMap: result.sourceMap };
247
+ },
248
+
249
+ };
250
+ };
251
+