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