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.
- package/.agignore +1 -1
- package/.github/workflows/ci.yml +38 -38
- package/=template.pyj +5 -5
- package/CHANGELOG.md +18 -0
- package/HACKING.md +103 -103
- package/LICENSE +24 -24
- package/README.md +715 -169
- package/TODO.md +9 -2
- package/add-toc-to-readme +2 -2
- package/bin/export +75 -75
- package/bin/rapydscript +70 -70
- package/bin/web-repl-export +102 -102
- package/build +2 -2
- package/language-service/index.js +36 -27
- package/package.json +1 -1
- package/publish.py +37 -37
- package/release/baselib-plain-pretty.js +2358 -168
- package/release/baselib-plain-ugly.js +73 -3
- package/release/compiler.js +6282 -3092
- package/release/signatures.json +31 -30
- package/session.vim +4 -4
- package/setup.cfg +2 -2
- package/src/ast.pyj +1 -0
- package/src/baselib-builtins.pyj +340 -2
- package/src/baselib-bytes.pyj +664 -0
- package/src/baselib-errors.pyj +1 -1
- package/src/baselib-internal.pyj +267 -60
- package/src/baselib-itertools.pyj +110 -97
- package/src/baselib-str.pyj +22 -4
- package/src/compiler.pyj +36 -36
- package/src/errors.pyj +30 -30
- package/src/lib/abc.pyj +317 -0
- package/src/lib/aes.pyj +646 -646
- package/src/lib/copy.pyj +120 -120
- package/src/lib/dataclasses.pyj +532 -0
- package/src/lib/elementmaker.pyj +83 -83
- package/src/lib/encodings.pyj +126 -126
- package/src/lib/enum.pyj +125 -0
- package/src/lib/gettext.pyj +569 -569
- package/src/lib/itertools.pyj +580 -580
- package/src/lib/math.pyj +193 -193
- package/src/lib/operator.pyj +11 -11
- package/src/lib/pythonize.pyj +20 -20
- package/src/lib/random.pyj +118 -118
- package/src/lib/re.pyj +504 -470
- package/src/lib/react.pyj +74 -74
- package/src/lib/traceback.pyj +63 -63
- package/src/lib/typing.pyj +577 -0
- package/src/lib/uuid.pyj +77 -77
- package/src/monaco-language-service/builtins.js +14 -4
- package/src/monaco-language-service/diagnostics.js +19 -20
- package/src/monaco-language-service/dts.js +550 -550
- package/src/output/classes.pyj +62 -26
- package/src/output/comments.pyj +45 -45
- package/src/output/exceptions.pyj +201 -201
- package/src/output/functions.pyj +78 -5
- package/src/output/jsx.pyj +164 -164
- package/src/output/loops.pyj +5 -2
- package/src/output/operators.pyj +100 -34
- package/src/output/treeshake.pyj +182 -182
- package/src/output/utils.pyj +72 -72
- package/src/parse.pyj +80 -16
- package/src/string_interpolation.pyj +72 -72
- package/src/tokenizer.pyj +9 -4
- package/src/unicode_aliases.pyj +576 -576
- package/src/utils.pyj +192 -192
- package/test/_import_one.pyj +37 -37
- package/test/_import_two/__init__.pyj +11 -11
- package/test/_import_two/level2/deep.pyj +4 -4
- package/test/_import_two/other.pyj +6 -6
- package/test/_import_two/sub.pyj +13 -13
- package/test/abc.pyj +291 -0
- package/test/aes_vectors.pyj +421 -421
- package/test/annotations.pyj +80 -80
- package/test/arithmetic_nostrict.pyj +88 -0
- package/test/arithmetic_types.pyj +169 -0
- package/test/baselib.pyj +91 -0
- package/test/bytes.pyj +467 -0
- package/test/classes.pyj +1 -0
- package/test/comparison_ops.pyj +173 -0
- package/test/dataclasses.pyj +253 -0
- package/test/decorators.pyj +77 -77
- package/test/docstrings.pyj +39 -39
- package/test/elementmaker_test.pyj +45 -45
- package/test/enum.pyj +134 -0
- package/test/eval_exec.pyj +56 -0
- package/test/format.pyj +148 -0
- package/test/functions.pyj +151 -151
- package/test/generators.pyj +41 -41
- package/test/generic.pyj +370 -370
- package/test/imports.pyj +72 -72
- package/test/internationalization.pyj +73 -73
- package/test/lint.pyj +164 -164
- package/test/loops.pyj +85 -85
- package/test/numpy.pyj +734 -734
- package/test/object.pyj +64 -0
- package/test/omit_function_metadata.pyj +20 -20
- package/test/python_compat.pyj +17 -15
- package/test/python_features.pyj +70 -15
- package/test/regexp.pyj +83 -55
- package/test/repl.pyj +121 -121
- package/test/scoped_flags.pyj +76 -76
- package/test/tuples.pyj +96 -0
- package/test/typing.pyj +469 -0
- package/test/unit/index.js +116 -7
- package/test/unit/language-service-dts.js +543 -543
- package/test/unit/language-service-hover.js +455 -455
- package/test/unit/language-service.js +84 -0
- package/test/unit/web-repl.js +804 -1
- package/test/vars_locals_globals.pyj +94 -0
- package/tools/cli.js +558 -547
- package/tools/compile.js +224 -219
- package/tools/completer.js +131 -131
- package/tools/embedded_compiler.js +262 -251
- package/tools/gettext.js +185 -185
- package/tools/ini.js +65 -65
- package/tools/lint.js +16 -19
- package/tools/msgfmt.js +187 -187
- package/tools/repl.js +223 -223
- package/tools/test.js +118 -118
- package/tools/utils.js +128 -128
- package/tools/web_repl.js +95 -95
- package/try +41 -41
- package/web-repl/env.js +196 -196
- package/web-repl/index.html +163 -163
- package/web-repl/main.js +252 -252
- package/web-repl/prism.css +139 -139
- package/web-repl/prism.js +113 -113
- package/web-repl/rapydscript.js +224 -224
- package/web-repl/sha1.js +25 -25
- package/PYTHON_DIFFERENCES_REPORT.md +0 -291
- 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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
flags_str
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
runjs
|
|
28
|
-
runjs(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
var
|
|
43
|
-
var
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
vlq
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
var
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
var
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
var
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
by_line[m.gen_line].
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
var
|
|
78
|
-
var
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
var
|
|
82
|
-
var
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
var
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
vlq_encode(
|
|
89
|
-
vlq_encode(
|
|
90
|
-
vlq_encode(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
var
|
|
112
|
-
output
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
var
|
|
137
|
-
var
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
var
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
);
|
|
196
|
-
var
|
|
197
|
-
var
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
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
|
+
|