rapydscript-ns 0.8.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 -0
- package/.gitattributes +4 -0
- package/.github/workflows/ci.yml +38 -0
- package/.github/workflows/web-repl-page-deploy.yml +42 -0
- package/=template.pyj +5 -0
- package/CHANGELOG.md +456 -0
- package/CONTRIBUTORS +13 -0
- package/HACKING.md +103 -0
- package/LICENSE +24 -0
- package/README.md +2512 -0
- package/TODO.md +327 -0
- package/add-toc-to-readme +2 -0
- package/bin/export +75 -0
- package/bin/rapydscript +70 -0
- package/bin/web-repl-export +102 -0
- package/build +3 -0
- package/package.json +46 -0
- package/publish.py +37 -0
- package/release/baselib-plain-pretty.js +4370 -0
- package/release/baselib-plain-ugly.js +3 -0
- package/release/compiler.js +18394 -0
- package/release/signatures.json +31 -0
- package/session.vim +4 -0
- package/setup.cfg +2 -0
- package/src/ast.pyj +1356 -0
- package/src/baselib-builtins.pyj +279 -0
- package/src/baselib-containers.pyj +723 -0
- package/src/baselib-errors.pyj +37 -0
- package/src/baselib-internal.pyj +421 -0
- package/src/baselib-itertools.pyj +97 -0
- package/src/baselib-str.pyj +798 -0
- package/src/compiler.pyj +36 -0
- package/src/errors.pyj +30 -0
- package/src/lib/aes.pyj +646 -0
- package/src/lib/collections.pyj +695 -0
- package/src/lib/elementmaker.pyj +83 -0
- package/src/lib/encodings.pyj +126 -0
- package/src/lib/functools.pyj +148 -0
- package/src/lib/gettext.pyj +569 -0
- package/src/lib/itertools.pyj +580 -0
- package/src/lib/math.pyj +193 -0
- package/src/lib/numpy.pyj +2101 -0
- package/src/lib/operator.pyj +11 -0
- package/src/lib/pythonize.pyj +20 -0
- package/src/lib/random.pyj +118 -0
- package/src/lib/re.pyj +470 -0
- package/src/lib/traceback.pyj +63 -0
- package/src/lib/uuid.pyj +77 -0
- package/src/monaco-language-service/analyzer.js +526 -0
- package/src/monaco-language-service/builtins.js +543 -0
- package/src/monaco-language-service/completions.js +498 -0
- package/src/monaco-language-service/diagnostics.js +643 -0
- package/src/monaco-language-service/dts.js +550 -0
- package/src/monaco-language-service/hover.js +121 -0
- package/src/monaco-language-service/index.js +386 -0
- package/src/monaco-language-service/scope.js +162 -0
- package/src/monaco-language-service/signature.js +144 -0
- package/src/output/__init__.pyj +0 -0
- package/src/output/classes.pyj +296 -0
- package/src/output/codegen.pyj +492 -0
- package/src/output/comments.pyj +45 -0
- package/src/output/exceptions.pyj +105 -0
- package/src/output/functions.pyj +491 -0
- package/src/output/literals.pyj +109 -0
- package/src/output/loops.pyj +444 -0
- package/src/output/modules.pyj +329 -0
- package/src/output/operators.pyj +429 -0
- package/src/output/statements.pyj +463 -0
- package/src/output/stream.pyj +309 -0
- package/src/output/treeshake.pyj +182 -0
- package/src/output/utils.pyj +72 -0
- package/src/parse.pyj +3106 -0
- package/src/string_interpolation.pyj +72 -0
- package/src/tokenizer.pyj +702 -0
- package/src/unicode_aliases.pyj +576 -0
- package/src/utils.pyj +192 -0
- package/test/_import_one.pyj +37 -0
- package/test/_import_two/__init__.pyj +11 -0
- package/test/_import_two/level2/__init__.pyj +0 -0
- package/test/_import_two/level2/deep.pyj +4 -0
- package/test/_import_two/other.pyj +6 -0
- package/test/_import_two/sub.pyj +13 -0
- package/test/aes_vectors.pyj +421 -0
- package/test/annotations.pyj +80 -0
- package/test/baselib.pyj +319 -0
- package/test/classes.pyj +452 -0
- package/test/collections.pyj +152 -0
- package/test/decorators.pyj +77 -0
- package/test/dict_spread.pyj +76 -0
- package/test/docstrings.pyj +39 -0
- package/test/elementmaker_test.pyj +45 -0
- package/test/ellipsis.pyj +49 -0
- package/test/functions.pyj +151 -0
- package/test/generators.pyj +41 -0
- package/test/generic.pyj +370 -0
- package/test/imports.pyj +72 -0
- package/test/internationalization.pyj +73 -0
- package/test/lint.pyj +164 -0
- package/test/loops.pyj +85 -0
- package/test/numpy.pyj +734 -0
- package/test/omit_function_metadata.pyj +20 -0
- package/test/regexp.pyj +55 -0
- package/test/repl.pyj +121 -0
- package/test/scoped_flags.pyj +76 -0
- package/test/starargs.pyj +506 -0
- package/test/starred_assign.pyj +104 -0
- package/test/str.pyj +198 -0
- package/test/subscript_tuple.pyj +53 -0
- package/test/unit/fixtures/fibonacci_expected.js +46 -0
- package/test/unit/index.js +2989 -0
- package/test/unit/language-service-builtins.js +815 -0
- package/test/unit/language-service-completions.js +1067 -0
- package/test/unit/language-service-dts.js +543 -0
- package/test/unit/language-service-hover.js +455 -0
- package/test/unit/language-service-scope.js +833 -0
- package/test/unit/language-service-signature.js +458 -0
- package/test/unit/language-service.js +705 -0
- package/test/unit/run-language-service.js +41 -0
- package/test/unit/web-repl.js +484 -0
- package/tools/build-language-service.js +190 -0
- package/tools/cli.js +547 -0
- package/tools/compile.js +219 -0
- package/tools/compiler.js +108 -0
- package/tools/completer.js +131 -0
- package/tools/embedded_compiler.js +251 -0
- package/tools/export.js +316 -0
- package/tools/gettext.js +185 -0
- package/tools/ini.js +65 -0
- package/tools/lint.js +705 -0
- package/tools/msgfmt.js +187 -0
- package/tools/repl.js +223 -0
- package/tools/self.js +162 -0
- package/tools/test.js +118 -0
- package/tools/utils.js +128 -0
- package/tools/web_repl.js +95 -0
- package/try +41 -0
- package/web-repl/env.js +74 -0
- package/web-repl/index.html +163 -0
- package/web-repl/language-service.js +4084 -0
- package/web-repl/main.js +254 -0
- package/web-repl/prism.css +139 -0
- package/web-repl/prism.js +113 -0
- package/web-repl/rapydscript.js +435 -0
- package/web-repl/sha1.js +25 -0
|
@@ -0,0 +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
|
+
|
package/tools/export.js
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2015 Kovid Goyal <kovid at kovidgoyal.net>
|
|
3
|
+
*
|
|
4
|
+
* Distributed under terms of the BSD license
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
var vm = require('vm');
|
|
8
|
+
var native_require = require;
|
|
9
|
+
|
|
10
|
+
function normalize_array(parts, allowAboveRoot) {
|
|
11
|
+
var res = [];
|
|
12
|
+
for (var i = 0; i < parts.length; i++) {
|
|
13
|
+
var p = parts[i];
|
|
14
|
+
|
|
15
|
+
// ignore empty parts
|
|
16
|
+
if (!p || p === '.')
|
|
17
|
+
continue;
|
|
18
|
+
|
|
19
|
+
if (p === '..') {
|
|
20
|
+
if (res.length && res[res.length - 1] !== '..') {
|
|
21
|
+
res.pop();
|
|
22
|
+
} else if (allowAboveRoot) {
|
|
23
|
+
res.push('..');
|
|
24
|
+
}
|
|
25
|
+
} else {
|
|
26
|
+
res.push(p);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return res;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function normalize(path) {
|
|
34
|
+
var is_abs = path && path[0] === '/';
|
|
35
|
+
var trailing_slash = path && path[path.length - 1] === '/';
|
|
36
|
+
path = normalize_array(path.split('/'), !is_abs).join('/');
|
|
37
|
+
|
|
38
|
+
if (!path && !is_abs) {
|
|
39
|
+
path = '.';
|
|
40
|
+
}
|
|
41
|
+
if (path && trailing_slash) {
|
|
42
|
+
path += '/';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return (is_abs ? '/' : '') + path;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function dirname(path) {
|
|
49
|
+
var idx = path.lastIndexOf('/');
|
|
50
|
+
if (idx != -1) path = path.slice(0, idx);
|
|
51
|
+
else path = '';
|
|
52
|
+
return path;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function basename(path) {
|
|
56
|
+
var idx = path.lastIndexOf('/');
|
|
57
|
+
if (idx != -1) path = path.slice(idx + 1);
|
|
58
|
+
return path;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
var cache = {};
|
|
62
|
+
|
|
63
|
+
function load(filepath) {
|
|
64
|
+
var cached = cache[filepath];
|
|
65
|
+
if (cached) return cached.exports;
|
|
66
|
+
var module = {'id':filepath, 'exports':{}};
|
|
67
|
+
cache[filepath] = module;
|
|
68
|
+
|
|
69
|
+
var content = data[filepath];
|
|
70
|
+
if (Array.isArray(content)) content = data[content[0]];
|
|
71
|
+
if (!content) throw 'Failed to load: ' + JSON.stringify(filepath);
|
|
72
|
+
|
|
73
|
+
if (filepath.slice(-5) == '.json') { module.exports = JSON.parse(content); return module.exports; }
|
|
74
|
+
|
|
75
|
+
var base = dirname(filepath);
|
|
76
|
+
function mrequire(x) {
|
|
77
|
+
return vrequire(x, base);
|
|
78
|
+
}
|
|
79
|
+
content = content.replace(/^\#\!.*/, '');
|
|
80
|
+
var wrapped = '(function(exports, require, module, __filename, __dirname, create_rapydscript_compiler) { ';
|
|
81
|
+
wrapped += content + '\n;})';
|
|
82
|
+
try {
|
|
83
|
+
vm.runInThisContext(wrapped, {'filename': filepath})(module.exports, mrequire, module, filepath, dirname(filepath), create_compiler);
|
|
84
|
+
} catch (e) {
|
|
85
|
+
console.error(e);
|
|
86
|
+
delete cache[filepath];
|
|
87
|
+
throw e;
|
|
88
|
+
}
|
|
89
|
+
return module.exports;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function has(x, y) { return Object.prototype.hasOwnProperty.call(x, y); }
|
|
93
|
+
|
|
94
|
+
function try_files(candidate) {
|
|
95
|
+
if (has(data, candidate)) return candidate;
|
|
96
|
+
if (has(data, candidate + '.js')) return candidate + '.js';
|
|
97
|
+
if (has(data, candidate + '.json')) return candidate + '.json';
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function find_in_modules_dir(name, base) {
|
|
102
|
+
var candidate = normalize(base + (base ? '/':'') + 'node_modules/' + name);
|
|
103
|
+
var q = try_files(candidate);
|
|
104
|
+
if (q) return q;
|
|
105
|
+
|
|
106
|
+
var pj = candidate + '/package.json';
|
|
107
|
+
if (has(data, pj)) {
|
|
108
|
+
var ans = normalize(candidate + '/' + JSON.parse(data[pj]).main);
|
|
109
|
+
if (has(data, ans)) return ans;
|
|
110
|
+
}
|
|
111
|
+
var index = candidate + '/index.js';
|
|
112
|
+
if (has(data, index)) return index;
|
|
113
|
+
|
|
114
|
+
var p = dirname(base);
|
|
115
|
+
if (p) return find_in_modules_dir(name, p);
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function find_module(name, base) {
|
|
120
|
+
if (name[0] == '/') throw 'Cannot find absolute module: ' + name;
|
|
121
|
+
if (name.slice(0, 2) == './' || name.slice(0, 3) == '../') {
|
|
122
|
+
var candidate = normalize((base ? base + '/' : base) + name);
|
|
123
|
+
return try_files(candidate);
|
|
124
|
+
}
|
|
125
|
+
var q = try_files(name);
|
|
126
|
+
if (q) return q;
|
|
127
|
+
return find_in_modules_dir(name, base);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function vrequire(name, base) {
|
|
131
|
+
var exports = {};
|
|
132
|
+
var modpath = '';
|
|
133
|
+
base = base || '';
|
|
134
|
+
// console.log('vrequire', name, base);
|
|
135
|
+
if (!name) throw new Error('Cannot load a module from an empty name');
|
|
136
|
+
|
|
137
|
+
modpath = find_module(name, base);
|
|
138
|
+
if (!modpath && name && './'.indexOf(name[0]) === -1) {
|
|
139
|
+
try {
|
|
140
|
+
return native_require(name);
|
|
141
|
+
} catch (e) {}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (!modpath) throw new Error("Failed to find module: " + JSON.stringify(name) + " with base: " + JSON.stringify(base));
|
|
145
|
+
return load(modpath);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
var UglifyJS = null, regenerator = null;
|
|
149
|
+
var crypto = null, fs = require('fs');
|
|
150
|
+
|
|
151
|
+
var current_virtual_files = null;
|
|
152
|
+
|
|
153
|
+
function readfile_wrapper(name, encoding) {
|
|
154
|
+
if (current_virtual_files && name.indexOf('__virtual__/') === 0) {
|
|
155
|
+
var rel = name.slice('__virtual__/'.length);
|
|
156
|
+
if (rel.slice(-11) === '.pyj-cached') rel = rel.slice(0, -11);
|
|
157
|
+
else if (rel.slice(-4) === '.pyj') rel = rel.slice(0, -4);
|
|
158
|
+
if (rel.slice(-9) === '/__init__') rel = rel.slice(0, -9);
|
|
159
|
+
if (Object.prototype.hasOwnProperty.call(current_virtual_files, rel)) {
|
|
160
|
+
return current_virtual_files[rel];
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return fs.readFileSync(name, encoding);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function writefile_wrapper(name, content) {
|
|
167
|
+
// Silently discard cache writes for virtual files; __virtual__ is not a real directory.
|
|
168
|
+
if (current_virtual_files && name.indexOf('__virtual__/') === 0) return;
|
|
169
|
+
return fs.writeFileSync(name, content);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function uglify(x) {
|
|
173
|
+
if (!UglifyJS) UglifyJS = vrequire("uglify-js");
|
|
174
|
+
ans = UglifyJS.minify(x);
|
|
175
|
+
if (ans.error) throw ans.error;
|
|
176
|
+
return ans.code;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function regenerate(code, beautify) {
|
|
180
|
+
var orig = fs.readFileSync;
|
|
181
|
+
fs.readFileSync = function(name) {
|
|
182
|
+
if (!has(data, name)) {
|
|
183
|
+
throw {message: "Failed to readfile from data: " + name};
|
|
184
|
+
}
|
|
185
|
+
return data[name];
|
|
186
|
+
};
|
|
187
|
+
if (!regenerator) regenerator = vrequire('regenerator');
|
|
188
|
+
var ans;
|
|
189
|
+
if (code) {
|
|
190
|
+
try {
|
|
191
|
+
ans = regenerator.compile(code).code;
|
|
192
|
+
} catch (e) {
|
|
193
|
+
console.error('regenerator failed for code: ' + code + 'with error stack:\n' + e.stack);
|
|
194
|
+
throw e;
|
|
195
|
+
}
|
|
196
|
+
if (!beautify) ans = uglify(ans);
|
|
197
|
+
} else {
|
|
198
|
+
// Return the runtime
|
|
199
|
+
ans = regenerator.compile('', {includeRuntime:true}).code;
|
|
200
|
+
start = ans.indexOf('=') + 1;
|
|
201
|
+
end = ans.lastIndexOf('typeof');
|
|
202
|
+
end = ans.lastIndexOf('}(', end);
|
|
203
|
+
ans = ans.slice(start + 1, end);
|
|
204
|
+
if (!beautify) {
|
|
205
|
+
var extra = '})()';
|
|
206
|
+
ans = uglify(ans + extra).slice(0, extra.length);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
fs.readFileSync = orig;
|
|
210
|
+
return ans;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (typeof this != 'object' || typeof this.sha1sum !== 'function') {
|
|
214
|
+
var sha1sum = function (data) {
|
|
215
|
+
if (!crypto) crypto = require('crypto');
|
|
216
|
+
var h = crypto.createHash('sha1');
|
|
217
|
+
h.update(data);
|
|
218
|
+
return h.digest('hex');
|
|
219
|
+
};
|
|
220
|
+
} else var sha1sum = this.sha1sum;
|
|
221
|
+
|
|
222
|
+
function create_compiler() {
|
|
223
|
+
var compilerjs = data['compiler.js'];
|
|
224
|
+
var module = {'id':'compiler', 'exports':{}};
|
|
225
|
+
var wrapped = '(function(module, exports, readfile, writefile, sha1sum, regenerate) {' + data['compiler.js'] + ';\n})';
|
|
226
|
+
vm.runInThisContext(wrapped, {'filename': 'compiler.js'})(module, module.exports, readfile_wrapper, writefile_wrapper, sha1sum, regenerate);
|
|
227
|
+
return module.exports;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
var RapydScript = null;
|
|
231
|
+
|
|
232
|
+
function compile(code, filename, options) {
|
|
233
|
+
if (!RapydScript) RapydScript = create_compiler();
|
|
234
|
+
options = options || {};
|
|
235
|
+
var vf = options.virtual_files || null;
|
|
236
|
+
var prev_virtual_files = current_virtual_files;
|
|
237
|
+
var import_dirs = options.import_dirs || [];
|
|
238
|
+
if (vf) {
|
|
239
|
+
current_virtual_files = vf;
|
|
240
|
+
import_dirs = ['__virtual__'].concat(import_dirs);
|
|
241
|
+
}
|
|
242
|
+
try {
|
|
243
|
+
var ast = RapydScript.parse(code, {
|
|
244
|
+
filename: filename || '<eval>',
|
|
245
|
+
basedir: options.basedir || dirname(filename || ''),
|
|
246
|
+
libdir: options.libdir,
|
|
247
|
+
import_dirs: import_dirs,
|
|
248
|
+
});
|
|
249
|
+
var out_ops = {
|
|
250
|
+
beautify: (options.beautify === undefined ? true : options.beautify),
|
|
251
|
+
private_scope: !options.bare,
|
|
252
|
+
omit_baselib: !!options.omit_baselib,
|
|
253
|
+
js_version: options.js_version || 5,
|
|
254
|
+
};
|
|
255
|
+
if (!out_ops.omit_baselib) out_ops.baselib_plain = data['baselib-plain-' + (out_ops.beautify ? 'pretty' : 'ugly') + '.js'];
|
|
256
|
+
var out = new RapydScript.OutputStream(out_ops);
|
|
257
|
+
ast.print(out);
|
|
258
|
+
return out.get();
|
|
259
|
+
} finally {
|
|
260
|
+
current_virtual_files = prev_virtual_files;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function create_embedded_compiler(runjs) {
|
|
265
|
+
var c = vrequire('tools/embedded_compiler.js');
|
|
266
|
+
return c(create_compiler(), data['baselib-plain-pretty.js'], runjs);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function web_repl() {
|
|
270
|
+
var repl = vrequire('tools/web_repl.js');
|
|
271
|
+
var vf_context = {
|
|
272
|
+
set: function(vf) { current_virtual_files = vf; },
|
|
273
|
+
clear: function() { current_virtual_files = null; }
|
|
274
|
+
};
|
|
275
|
+
return repl(create_compiler(), data['baselib-plain-pretty.js'], vf_context);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function init_repl(options) {
|
|
279
|
+
var repl = vrequire('tools/repl.js');
|
|
280
|
+
options.baselib = data['baselib-plain-pretty.js'];
|
|
281
|
+
return repl(options);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function gettext_parse(catalog, code, filename) {
|
|
285
|
+
g = vrequire('tools/gettext.js');
|
|
286
|
+
g.gettext(catalog, code, filename);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function gettext_output(catalog, options, write) {
|
|
290
|
+
g = vrequire('tools/gettext.js');
|
|
291
|
+
g.write_output(catalog, options, write);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function msgfmt(data, options) {
|
|
295
|
+
m = vrequire('tools/msgfmt.js');
|
|
296
|
+
return m.build(data, options);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function completer(compiler, options) {
|
|
300
|
+
m = vrequire('tools/completer.js');
|
|
301
|
+
return m(compiler, options);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (typeof exports === 'object') {
|
|
305
|
+
exports.compile = compile;
|
|
306
|
+
exports.create_embedded_compiler = create_embedded_compiler;
|
|
307
|
+
exports.web_repl = web_repl;
|
|
308
|
+
exports.init_repl = init_repl;
|
|
309
|
+
exports.gettext_parse = gettext_parse;
|
|
310
|
+
exports.gettext_output = gettext_output;
|
|
311
|
+
exports.msgfmt = msgfmt;
|
|
312
|
+
exports.rs_version = rs_version;
|
|
313
|
+
exports.file_data = data;
|
|
314
|
+
exports.completer = completer;
|
|
315
|
+
if (typeof rs_commit_sha === 'string') exports.rs_commit_sha = rs_commit_sha;
|
|
316
|
+
}
|