rapydscript-ng 0.7.23 → 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.
Files changed (114) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/README.md +72 -26
  3. package/bin/package.json +1 -0
  4. package/bin/rapydscript +65 -62
  5. package/editor-plugins/nvim/rapydscript/README.md +184 -0
  6. package/editor-plugins/nvim/rapydscript/bin/rapydscript.so +0 -0
  7. package/editor-plugins/nvim/rapydscript/ftdetect/rapydscript.lua +10 -0
  8. package/editor-plugins/nvim/rapydscript/lua/rapydscript/health.lua +88 -0
  9. package/editor-plugins/nvim/rapydscript/lua/rapydscript/init.lua +279 -0
  10. package/local-agent.md +28 -0
  11. package/package.json +4 -5
  12. package/release/baselib-plain-pretty.js +448 -326
  13. package/release/baselib-plain-ugly.js +3 -3
  14. package/release/compiler.js +2528 -1672
  15. package/release/signatures.json +17 -17
  16. package/src/ast.pyj +73 -25
  17. package/src/baselib-builtins.pyj +2 -2
  18. package/src/baselib-containers.pyj +153 -151
  19. package/src/baselib-internal.pyj +3 -3
  20. package/src/baselib-str.pyj +5 -5
  21. package/src/lib/aes.pyj +1 -1
  22. package/src/lib/encodings.pyj +1 -1
  23. package/src/lib/pythonize.pyj +1 -1
  24. package/src/lib/re.pyj +2 -2
  25. package/src/lib/traceback.pyj +165 -28
  26. package/src/lib/uuid.pyj +1 -1
  27. package/src/output/codegen.pyj +10 -3
  28. package/src/output/functions.pyj +31 -40
  29. package/src/output/literals.pyj +11 -14
  30. package/src/output/loops.pyj +25 -87
  31. package/src/output/modules.pyj +5 -47
  32. package/src/output/statements.pyj +1 -1
  33. package/src/output/stream.pyj +58 -31
  34. package/src/parse.pyj +777 -427
  35. package/src/tokenizer.pyj +16 -4
  36. package/src/utils.pyj +1 -1
  37. package/test/_treeshake_mod.pyj +30 -0
  38. package/test/_treeshake_side_effect.pyj +9 -0
  39. package/test/ast_serialization.pyj +392 -0
  40. package/test/async.pyj +95 -0
  41. package/test/baselib.pyj +1 -2
  42. package/test/embedded_compiler.pyj +57 -0
  43. package/test/fmt.pyj +201 -0
  44. package/test/generic.pyj +7 -3
  45. package/test/imports.pyj +8 -6
  46. package/test/internationalization.pyj +38 -37
  47. package/test/lint.pyj +120 -115
  48. package/test/lsp.pyj +222 -0
  49. package/test/repl.pyj +70 -65
  50. package/test/sourcemaps.pyj +315 -0
  51. package/test/starargs.pyj +46 -39
  52. package/test/traceback.pyj +263 -0
  53. package/test/treeshaking.pyj +181 -0
  54. package/test/web_repl_export.pyj +88 -0
  55. package/tools/ast_serialize.mjs +557 -0
  56. package/tools/cli.mjs +510 -0
  57. package/tools/compile.mjs +201 -0
  58. package/tools/compiler.mjs +127 -0
  59. package/tools/{completer.js → completer.mjs} +6 -6
  60. package/tools/{embedded_compiler.js → embedded_compiler.mjs} +17 -13
  61. package/tools/export.js +109 -56
  62. package/tools/fmt.mjs +735 -0
  63. package/tools/{gettext.js → gettext.mjs} +46 -53
  64. package/tools/{ini.js → ini.mjs} +9 -10
  65. package/tools/{lint.js → lint.mjs} +107 -56
  66. package/tools/lsp.mjs +914 -0
  67. package/tools/lsp_protocol.mjs +259 -0
  68. package/tools/lsp_symbols.mjs +418 -0
  69. package/tools/{msgfmt.js → msgfmt.mjs} +18 -18
  70. package/tools/{repl.js → repl.mjs} +52 -41
  71. package/tools/{self.js → self.mjs} +56 -46
  72. package/tools/sourcemap.mjs +123 -0
  73. package/tools/test.mjs +152 -0
  74. package/tools/treeshake.mjs +400 -0
  75. package/tools/{utils.js → utils.mjs} +26 -23
  76. package/tools/{web_repl.js → web_repl.mjs} +15 -13
  77. package/tools/web_repl_export.mjs +93 -0
  78. package/tree-sitter/README.md +101 -0
  79. package/tree-sitter/grammar.js +992 -0
  80. package/tree-sitter/package.json +43 -0
  81. package/tree-sitter/queries/rapydscript/highlights.scm +232 -0
  82. package/tree-sitter/queries/rapydscript/indents.scm +64 -0
  83. package/tree-sitter/queries/rapydscript/injections.scm +14 -0
  84. package/tree-sitter/queries/rapydscript/locals.scm +108 -0
  85. package/tree-sitter/src/grammar.json +4976 -0
  86. package/tree-sitter/src/node-types.json +2901 -0
  87. package/tree-sitter/src/parser.c +196465 -0
  88. package/tree-sitter/src/scanner.c +294 -0
  89. package/tree-sitter/src/tree_sitter/alloc.h +54 -0
  90. package/tree-sitter/src/tree_sitter/array.h +330 -0
  91. package/tree-sitter/src/tree_sitter/parser.h +286 -0
  92. package/tree-sitter/test/corpus/chaining.txt +99 -0
  93. package/tree-sitter/test/corpus/classes.txt +147 -0
  94. package/tree-sitter/test/corpus/comprehensions.txt +155 -0
  95. package/tree-sitter/test/corpus/containers.txt +242 -0
  96. package/tree-sitter/test/corpus/control_flow.txt +361 -0
  97. package/tree-sitter/test/corpus/decorators.txt +64 -0
  98. package/tree-sitter/test/corpus/existential.txt +102 -0
  99. package/tree-sitter/test/corpus/functions.txt +293 -0
  100. package/tree-sitter/test/corpus/imports.txt +117 -0
  101. package/tree-sitter/test/corpus/literals.txt +135 -0
  102. package/tree-sitter/test/corpus/operators.txt +296 -0
  103. package/tree-sitter/test/corpus/statements.txt +189 -0
  104. package/tree-sitter/test/corpus/strings.txt +90 -0
  105. package/tree-sitter/test/corpus/subscripts.txt +227 -0
  106. package/tree-sitter/tree-sitter.json +36 -0
  107. package/web-repl/env.js +35 -23
  108. package/web-repl/main.js +8 -8
  109. package/bin/export +0 -75
  110. package/bin/web-repl-export +0 -102
  111. package/tools/cli.js +0 -523
  112. package/tools/compile.js +0 -184
  113. package/tools/compiler.js +0 -84
  114. package/tools/test.js +0 -110
@@ -1,10 +1,10 @@
1
1
  /* vim:fileencoding=utf-8
2
- *
2
+ *
3
3
  * Copyright (C) 2015 Kovid Goyal <kovid at kovidgoyal.net>
4
4
  *
5
5
  * Distributed under terms of the BSD license
6
6
  */
7
- "use strict"; /*jshint node:true */
7
+ "use strict";
8
8
 
9
9
  function unesc(string) {
10
10
  return string.replace(/\\"/g, '"').replace(/\\n/g, '\n').replace(/\\r/g, '\r').replace(/\\t/g, '\t').replace(/\\\\/g, '\\');
@@ -41,7 +41,7 @@ function parse(data, on_error) {
41
41
  var match = /^nplurals\s*=\s*(\d+)\s*;/.exec(plural_forms);
42
42
  if (!match || match[1] === undefined) fatal('Invalid Plural-Forms header:', plural_forms);
43
43
  nplurals = parseInt(match[1]);
44
- }
44
+ }
45
45
  else if (line.startsWith('Language:')) {
46
46
  language = line.slice('Language:'.length).trim();
47
47
  }
@@ -87,7 +87,7 @@ function parse(data, on_error) {
87
87
  } else if (line.startsWith('msgid ')) {
88
88
  current_entry.msgid = read_string(line.slice('msgid '.length));
89
89
  current_entry.lnum = lnum;
90
- state = function(line, lines) {
90
+ state = function(line, lines) {
91
91
  continuation(line, lines, function(x) { current_entry.msgid += x; }, after_msgid);
92
92
  };
93
93
  } else {
@@ -98,16 +98,16 @@ function parse(data, on_error) {
98
98
  function after_msgid(line, lines) {
99
99
  if (line.startsWith('msgid_plural ')) {
100
100
  current_entry.msgid_plural = read_string(line.slice('msgid_plural '.length));
101
- state = function(line, lines) {
101
+ state = function(line, lines) {
102
102
  continuation(line, lines, function(x) { current_entry.msgid_plural += x; }, msgstr);
103
103
  };
104
- }
105
-
104
+ }
105
+
106
106
  else if (line.startsWith('msgstr ') || line.startsWith('msgstr[')) {
107
107
  state = msgstr;
108
108
  msgstr(line, lines);
109
- }
110
-
109
+ }
110
+
111
111
  else fatal('Expecting either msgstr or msgid_plural at line number:', lnum);
112
112
 
113
113
  }
@@ -116,10 +116,10 @@ function parse(data, on_error) {
116
116
  if (line.startsWith('msgstr ')) {
117
117
  if (current_entry.msgid_plural !== null) fatal('Expecting msgstr[0] at line number:', lnum);
118
118
  current_entry.msgstr.push(read_string(line.slice('msgstr '.length)));
119
- state = function(line, lines) {
119
+ state = function(line, lines) {
120
120
  continuation(line, lines, function(x) { current_entry.msgstr[current_entry.msgstr.length - 1] += x; }, msgstr);
121
121
  };
122
- }
122
+ }
123
123
 
124
124
  else if (line[0] === '#' || line.startsWith('msgid ')) {
125
125
  if (!current_entry.msgstr.length) fatal('Expecting msgstr at line number:', lnum);
@@ -134,7 +134,7 @@ function parse(data, on_error) {
134
134
  if (!pnum || pnum[1] === undefined) fatal('Malformed msgstr at line number:', lnum);
135
135
  var idx = parseInt(pnum[1]);
136
136
  current_entry.msgstr[idx] = read_string(line.slice(pnum[0].length));
137
- state = function(line, lines) {
137
+ state = function(line, lines) {
138
138
  continuation(line, lines, function(x) { current_entry.msgstr[idx] += x; }, msgstr);
139
139
  };
140
140
  }
@@ -159,9 +159,9 @@ function read_stdin(cont) {
159
159
  var chunks = [];
160
160
  process.stdin.setEncoding('utf8');
161
161
 
162
- process.stdin.on('readable', function () {
162
+ process.stdin.on('readable', function () {
163
163
  var chunk = process.stdin.read();
164
- if (chunk) chunks.push(chunk);
164
+ if (chunk) chunks.push(chunk);
165
165
  });
166
166
 
167
167
  process.stdin.on('end', function() { cont(chunks.join('')); });
@@ -176,12 +176,12 @@ function serialize_catalog(catalog, options) {
176
176
  return JSON.stringify({'plural_forms':catalog.plural_forms, 'entries':entries, 'language':catalog.language});
177
177
  }
178
178
 
179
- module.exports.cli = function(argv, base_path, src_path, lib_path) {
179
+ export function cli(argv, base_path, src_path, lib_path) {
180
180
  read_stdin(function process(data) {
181
181
  var catalog = parse(data);
182
182
  console.log(serialize_catalog(catalog, argv));
183
183
  });
184
- };
184
+ }
185
185
 
186
- module.exports.parse = parse;
187
- module.exports.build = function(data, options) { return serialize_catalog(parse(data), options); };
186
+ export { parse };
187
+ export function build(data, options) { return serialize_catalog(parse(data), options); }
@@ -4,20 +4,25 @@
4
4
  *
5
5
  * Distributed under terms of the BSD license.
6
6
  */
7
- "use strict"; /*jshint node:true */
8
-
9
- var fs = require('fs');
10
- var path = require('path');
11
- var vm = require('vm');
12
- var util = require('util');
13
- var utils = require('./utils');
14
- var completelib = require('./completer');
7
+ "use strict";
8
+
9
+ import fs from 'fs';
10
+ import path from 'path';
11
+ import vm from 'vm';
12
+ import util from 'util';
13
+ import { createRequire } from 'module';
14
+ import * as utils from './utils.mjs';
15
+ import completelib from './completer.mjs';
16
+
15
17
  var colored = utils.safe_colored;
16
- var RapydScript = (typeof create_rapydscript_compiler === 'function') ? create_rapydscript_compiler() : require('./compiler').create_compiler();
18
+ var _rapydscript_compiler = typeof create_rapydscript_compiler !== 'undefined' ? create_rapydscript_compiler : globalThis.create_rapydscript_compiler;
17
19
  var has_prop = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty);
18
20
 
21
+ // In Node.js ESM context, require is not available; use createRequire.
22
+ var _cjs_require = typeof require !== 'undefined' ? require : createRequire(process.cwd() + '/');
23
+
19
24
  function create_ctx(baselib, show_js, console) {
20
- var ctx = vm.createContext({'console':console, 'show_js': !!show_js, 'RapydScript':RapydScript, 'require':require});
25
+ var ctx = vm.createContext({'console':console, 'show_js': !!show_js, 'RapydScript':null, 'require':_cjs_require});
21
26
  vm.runInContext(baselib, ctx, {'filename':'baselib-plain-pretty.js'});
22
27
  vm.runInContext('var __name__ = "__repl__";', ctx);
23
28
  return ctx;
@@ -42,40 +47,55 @@ function repl_defaults(options) {
42
47
  if (!options.ps1) options.ps1 = '>>> ';
43
48
  if (!options.ps2) options.ps2 = '... ';
44
49
  if (!options.console) options.console = console;
45
- if (!options.readline) options.readline = require('readline');
50
+ if (!options.readline) options.readline = _cjs_require('readline');
46
51
  if (options.terminal === undefined) options.terminal = options.output.isTTY;
47
52
  if (options.histfile === undefined) options.histfile = path.join(cachedir, 'rapydscript-repl.history');
48
-
53
+
49
54
  options.colored = (options.terminal) ? colored : (function (string) { return string; });
50
55
  options.historySize = options.history_size || 1000;
51
56
  return options;
52
57
  }
53
58
 
54
- function read_history(options) {
59
+ async function read_history(options) {
55
60
  if (options.histfile) {
56
61
  try {
57
- return fs.readFileSync(options.histfile, 'utf-8').split('\n');
62
+ return (await fs.promises.readFile(options.histfile, 'utf-8')).split('\n');
58
63
  } catch (e) { return []; }
59
64
  }
65
+ return [];
60
66
  }
61
67
 
62
- function write_history(options, history) {
68
+ async function write_history(options, history) {
63
69
  if (options.histfile) {
64
- history = history.join('\n');
65
70
  try {
66
- return fs.writeFileSync(options.histfile, history, 'utf-8');
71
+ await fs.promises.writeFile(options.histfile, history.join('\n'), 'utf-8');
67
72
  } catch (e) {}
68
73
  }
69
74
  }
70
75
 
71
76
 
72
- module.exports = function(options) {
77
+ export default async function(options) {
78
+ var RapydScript = _rapydscript_compiler();
73
79
  options = repl_defaults(options);
74
80
  options.completer = completer;
75
81
  var rl = options.readline.createInterface(options);
76
82
  var ps1 = options.colored(options.ps1, 'green');
77
83
  var ps2 = options.colored(options.ps2, 'yellow');
78
- var ctx = create_ctx(print_ast(RapydScript.parse('(def ():\n yield 1\n)'), true), options.show_js, options.console);
84
+
85
+ // Read baselib synchronously: the repl must initialize synchronously so
86
+ // that the readline interface and prompt are ready before returning.
87
+ var baselib_plain = fs.readFileSync(path.join(options.lib_path, 'baselib-plain-pretty.js'), 'utf-8');
88
+
89
+ function print_ast(ast, keep_baselib) {
90
+ var output_options = {omit_baselib:!keep_baselib, write_name:false, private_scope:false, beautify:true, keep_docstrings:true};
91
+ if (keep_baselib) output_options.baselib_plain = baselib_plain;
92
+ var output = new RapydScript.OutputStream(output_options);
93
+ ast.print(output);
94
+ return output.get();
95
+ }
96
+
97
+ var ctx = create_ctx(print_ast(await RapydScript.parse('(def ():\n yield 1\n)'), true), options.show_js, options.console);
98
+ ctx.RapydScript = RapydScript;
79
99
  var buffer = [];
80
100
  var more = false;
81
101
  var LINE_CONTINUATION_CHARS = ':\\';
@@ -90,15 +110,6 @@ module.exports = function(options) {
90
110
  options.console.log(options.colored('Use show_js=True to have the REPL show the compiled JavaScript before executing it.', 'green', true));
91
111
  options.console.log();
92
112
 
93
- function print_ast(ast, keep_baselib) {
94
- var output_options = {omit_baselib:!keep_baselib, write_name:false, private_scope:false, beautify:true, keep_docstrings:true};
95
- if (keep_baselib) output_options.baselib_plain = fs.readFileSync(path.join(options.lib_path, 'baselib-plain-pretty.js'), 'utf-8');
96
- var output = new RapydScript.OutputStream(output_options);
97
- ast.print(output);
98
- return output.get();
99
- }
100
-
101
-
102
113
  function resetbuffer() { buffer = []; }
103
114
 
104
115
  function completer(line) {
@@ -142,11 +153,11 @@ module.exports = function(options) {
142
153
  }
143
154
  }
144
155
 
145
- function compile_source(source) {
156
+ async function compile_source(source) {
146
157
  var classes = (toplevel) ? toplevel.classes : undefined;
147
158
  var scoped_flags = (toplevel) ? toplevel.scoped_flags: undefined;
148
159
  try {
149
- toplevel = RapydScript.parse(source, {
160
+ toplevel = await RapydScript.parse(source, {
150
161
  'filename':'<repl>',
151
162
  'basedir': process.cwd(),
152
163
  'libdir': options.imp_path,
@@ -174,33 +185,33 @@ module.exports = function(options) {
174
185
  return false;
175
186
  }
176
187
 
177
- function push(line) {
188
+ async function push(line) {
178
189
  buffer.push(line);
179
190
  var ll = line.trimRight();
180
191
  if (ll && LINE_CONTINUATION_CHARS.indexOf(ll.substr(ll.length - 1)) > -1)
181
192
  return true;
182
193
  var source = buffer.join('\n');
183
194
  if (!source.trim()) { resetbuffer(); return false; }
184
- var incomplete = compile_source(source);
195
+ var incomplete = await compile_source(source);
185
196
  if (!incomplete) resetbuffer();
186
197
  return incomplete;
187
198
  }
188
199
 
189
- rl.on('line', function(line) {
200
+ rl.on('line', async function(line) {
190
201
  if (more) {
191
- // We are in a block
202
+ // We are in a block
192
203
  var line_is_empty = !line.trimLeft();
193
204
  if (line_is_empty && buffer.length && !buffer[buffer.length - 1].trimLeft()) {
194
205
  // We have two empty lines, evaluate the block
195
- more = push(line.trimLeft());
206
+ more = await push(line.trimLeft());
196
207
  } else buffer.push(line);
197
- } else more = push(line); // Not in a block, evaluate line
208
+ } else more = await push(line); // Not in a block, evaluate line
198
209
  prompt();
199
210
  })
200
-
201
- .on('close', function() {
211
+
212
+ .on('close', async function() {
202
213
  options.console.log('Bye!');
203
- if (rl.history) write_history(options, rl.history);
214
+ if (rl.history) await write_history(options, rl.history);
204
215
  process.exit(0);
205
216
  })
206
217
 
@@ -216,6 +227,6 @@ module.exports = function(options) {
216
227
  prompt();
217
228
  });
218
229
 
219
- rl.history = read_history(options);
230
+ read_history(options).then(function(history) { rl.history = history; });
220
231
  prompt();
221
- };
232
+ }
@@ -4,25 +4,31 @@
4
4
  *
5
5
  * Distributed under terms of the BSD license.
6
6
  */
7
- "use strict"; /*jshint node:true */
8
7
 
9
- var path = require('path');
10
- var crypto = require('crypto');
11
- var fs = require('fs');
12
- var vm = require('vm');
13
- var zlib = require('zlib');
8
+ import path from 'path';
9
+ import crypto from 'crypto';
10
+ import fs from 'fs';
11
+ import vm from 'vm';
12
+ import zlib from 'zlib';
13
+ import { createRequire } from 'module';
14
+ import { fileURLToPath } from 'url';
15
+ import { create_compiler } from './compiler.mjs';
14
16
 
15
- function compile_baselib(RapydScript, src_path) {
16
- var items = fs.readdirSync(src_path).filter(function(name) {
17
+ const require = createRequire(import.meta.url);
18
+ const __filename = fileURLToPath(import.meta.url);
19
+
20
+ async function compile_baselib(RapydScript, src_path) {
21
+ var items = (await fs.promises.readdir(src_path)).filter(function(name) {
17
22
  return name.slice(0, 'baselib-'.length) === 'baselib-' && name.slice(-4) == '.pyj';
18
23
  });
19
24
  var ans = {'pretty': '', 'ugly': ''};
20
25
 
21
- items.sort().forEach(function(fname) {
26
+ items.sort();
27
+ for (const fname of items) {
22
28
  var name = fname.slice('baselib-'.length, -4), ast;
23
- var raw = fs.readFileSync(path.join(src_path, fname), 'utf-8');
29
+ var raw = await fs.promises.readFile(path.join(src_path, fname), 'utf-8');
24
30
  try {
25
- ast = RapydScript.parse(raw, {filename:fname, basedir:src_path});
31
+ ast = await RapydScript.parse(raw, {filename:fname, basedir:src_path});
26
32
  } catch (e) {
27
33
  if (!(e instanceof RapydScript.SyntaxError)) throw e;
28
34
  console.error(e.toString());
@@ -30,52 +36,56 @@ function compile_baselib(RapydScript, src_path) {
30
36
  }
31
37
  [true, false].forEach(function (beautify) {
32
38
  var output = new RapydScript.OutputStream({
33
- beautify: beautify, write_name: false, private_scope:false, omit_baselib: true,
39
+ beautify: beautify, write_name: false, private_scope:false, omit_baselib: true,
34
40
  });
35
41
  ast.print(output);
36
42
  ans[(beautify) ? 'pretty' : 'ugly'] += output.get();
37
43
  });
38
- });
44
+ }
39
45
  return ans;
40
46
  }
41
47
 
42
- function check_for_changes(base_path, src_path, signatures) {
48
+ async function check_for_changes(base_path, src_path, signatures) {
43
49
  // Check if any of the files involved in the build process have changed,
44
50
  // as compared to the saved sha1 hashes from the last build
45
51
  var saved_hashes = {}, hashes = {}, sources = {};
46
52
  var compiler_changed = false, compiler_hash, source_hash;
47
53
  try {
48
- saved_hashes = JSON.parse(fs.readFileSync(signatures, 'utf-8'));
54
+ saved_hashes = JSON.parse(await fs.promises.readFile(signatures, 'utf-8'));
49
55
  } catch (e) {
50
56
  if (e.code != 'ENOENT') throw (e);
51
57
  }
52
58
 
53
59
  var src_file_names = [];
54
60
 
55
- function process_dir(p) {
56
- fs.readdirSync(p).forEach(function(name) {
61
+ async function process_dir(p) {
62
+ var entries = await fs.promises.readdir(p);
63
+ for (const name of entries) {
57
64
  var fp = path.join(p, name);
58
65
  if (name.substr(-4) === '.pyj') src_file_names.push(path.relative(src_path, fp));
59
- else if (name != 'lib' && fs.statSync(fp).isDirectory()) process_dir(fp);
60
- });
66
+ else if (name != 'lib') {
67
+ var stat = await fs.promises.stat(fp);
68
+ if (stat.isDirectory()) await process_dir(fp);
69
+ }
70
+ }
61
71
  }
62
- process_dir(src_path);
72
+ await process_dir(src_path);
63
73
 
64
74
  compiler_hash = crypto.createHash('sha1');
65
75
  source_hash = crypto.createHash('sha1');
66
- src_file_names.forEach(function(fname) {
76
+ for (const fname of src_file_names) {
67
77
  var src = path.join(src_path, fname);
68
- sources[src] = fs.readFileSync(src, 'utf-8');
78
+ sources[src] = await fs.promises.readFile(src, 'utf-8');
69
79
  compiler_hash.update(sources[src]);
70
80
  source_hash.update(sources[src]);
71
81
  var h = crypto.createHash('sha1');
72
82
  h.update(sources[src]);
73
83
  hashes[fname.split('.')[0]] = h.digest('hex');
74
- });
75
- var compiler_files = [module.filename, path.join(base_path, 'tools', 'compiler.js')];
76
- compiler_files.forEach(function(fpath) {
77
- compiler_hash.update(fs.readFileSync(fpath, 'utf-8'));
78
- });
84
+ }
85
+ var compiler_files = [__filename, path.join(base_path, 'tools', 'compiler.mjs')];
86
+ for (const fpath of compiler_files) {
87
+ compiler_hash.update(await fs.promises.readFile(fpath, 'utf-8'));
88
+ }
79
89
  hashes['#compiler#'] = compiler_hash.digest('hex');
80
90
  hashes['#compiled_with#'] = saved_hashes['#compiler#'] || 'unknown';
81
91
  source_hash = source_hash.digest('hex');
@@ -91,15 +101,15 @@ function check_for_changes(base_path, src_path, signatures) {
91
101
  }
92
102
 
93
103
 
94
- function compile(src_path, lib_path, sources, source_hash, profile) {
104
+ async function compile(src_path, lib_path, sources, source_hash, profile) {
95
105
  var file = path.join(src_path, 'compiler.pyj');
96
106
  var t1 = new Date().getTime();
97
- var RapydScript = require('./compiler').create_compiler();
107
+ var RapydScript = await create_compiler();
98
108
  var output_options, profiler, cpu_profile;
99
- var compiled_baselib = compile_baselib(RapydScript, src_path);
109
+ var compiled_baselib = await compile_baselib(RapydScript, src_path);
100
110
  var out_path = path.join(path.dirname(lib_path), 'dev');
101
111
  try {
102
- fs.mkdirSync(out_path);
112
+ await fs.promises.mkdir(out_path);
103
113
  } catch (e) {
104
114
  if (e.code != 'EEXIST') throw e;
105
115
  }
@@ -107,8 +117,8 @@ function compile(src_path, lib_path, sources, source_hash, profile) {
107
117
 
108
118
  var raw = sources[file], toplevel;
109
119
 
110
- function parse_file(code, file) {
111
- return RapydScript.parse(code, {
120
+ async function parse_file(code, file) {
121
+ return await RapydScript.parse(code, {
112
122
  filename: file,
113
123
  basedir: path.dirname(file),
114
124
  libdir: path.join(src_path, 'lib'),
@@ -120,10 +130,10 @@ function compile(src_path, lib_path, sources, source_hash, profile) {
120
130
  profiler = require('v8-profiler');
121
131
  profiler.startProfiling();
122
132
  }
123
- toplevel = parse_file(raw, file);
133
+ toplevel = await parse_file(raw, file);
124
134
  if (profile) {
125
135
  cpu_profile = profiler.stopProfiling();
126
- fs.writeFileSync('self.cpuprofile', JSON.stringify(cpu_profile), 'utf-8');
136
+ await fs.promises.writeFile('self.cpuprofile', JSON.stringify(cpu_profile), 'utf-8');
127
137
  }
128
138
  } catch (e) {
129
139
  if (!(e instanceof RapydScript.SyntaxError)) throw e;
@@ -133,30 +143,30 @@ function compile(src_path, lib_path, sources, source_hash, profile) {
133
143
  var output = new RapydScript.OutputStream(output_options);
134
144
  toplevel.print(output);
135
145
  output = output.get().replace('__COMPILER_VERSION__', source_hash);
136
- fs.writeFileSync(path.join(out_path, 'compiler.js'), output, "utf8");
137
- fs.writeFileSync(path.join(out_path, 'baselib-plain-pretty.js'), compiled_baselib.pretty, 'utf-8');
138
- fs.writeFileSync(path.join(out_path, 'baselib-plain-ugly.js'), compiled_baselib.ugly, 'utf-8');
146
+ await fs.promises.writeFile(path.join(out_path, 'compiler.js'), output, "utf8");
147
+ await fs.promises.writeFile(path.join(out_path, 'baselib-plain-pretty.js'), compiled_baselib.pretty, 'utf-8');
148
+ await fs.promises.writeFile(path.join(out_path, 'baselib-plain-ugly.js'), compiled_baselib.ugly, 'utf-8');
139
149
  console.log('Compiler built in', (new Date().getTime() - t1)/1000, 'seconds\n');
140
150
  return output;
141
151
  }
142
152
 
143
- function run_single_compile(base_path, src_path, lib_path, profile) {
153
+ async function run_single_compile(base_path, src_path, lib_path, profile) {
144
154
  var out_path = path.join(path.dirname(lib_path), 'dev');
145
155
  var signatures = path.join(out_path, 'signatures.json');
146
- var temp = check_for_changes(base_path, src_path, signatures);
156
+ var temp = await check_for_changes(base_path, src_path, signatures);
147
157
  var source_hash = temp[0], compiler_changed = temp[1], sources = temp[2], hashes = temp[3];
148
-
158
+
149
159
  if (compiler_changed) {
150
- compile(src_path, lib_path, sources, source_hash, profile);
151
- fs.writeFileSync(signatures, JSON.stringify(hashes, null, 4));
160
+ await compile(src_path, lib_path, sources, source_hash, profile);
161
+ await fs.promises.writeFile(signatures, JSON.stringify(hashes, null, 4));
152
162
  } else console.log('Compiler is built with the up-to-date version of itself');
153
163
  return compiler_changed;
154
164
  }
155
165
 
156
- module.exports = function compile_self(base_path, src_path, lib_path, complete, profile) {
166
+ export default async function compile_self(base_path, src_path, lib_path, complete, profile) {
157
167
  var changed;
158
168
  do {
159
- changed = run_single_compile(base_path, src_path, lib_path, profile);
169
+ changed = await run_single_compile(base_path, src_path, lib_path, profile);
160
170
  lib_path = path.join(path.dirname(lib_path), 'dev');
161
171
  } while (changed && complete);
162
- };
172
+ }
@@ -0,0 +1,123 @@
1
+ /*
2
+ * sourcemap.js
3
+ * Copyright (C) 2015 Kovid Goyal <kovid at kovidgoyal.net>
4
+ *
5
+ * Distributed under terms of the BSD license.
6
+ */
7
+ "use strict";
8
+
9
+ import path from 'path';
10
+
11
+ // Base64 VLQ encoding for source maps (see http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/)
12
+ var BASE64_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
13
+ var VLQ_BASE_SHIFT = 5;
14
+ var VLQ_BASE = 1 << VLQ_BASE_SHIFT; // 32
15
+ var VLQ_BASE_MASK = VLQ_BASE - 1; // 31
16
+ var VLQ_CONTINUATION_BIT = VLQ_BASE; // 32
17
+
18
+ function to_vlq_signed(value) {
19
+ return (value < 0) ? ((-value) << 1) + 1 : (value << 1);
20
+ }
21
+
22
+ function encode_vlq(value) {
23
+ var encoded = '';
24
+ var vlq = to_vlq_signed(value);
25
+ do {
26
+ var digit = vlq & VLQ_BASE_MASK;
27
+ vlq >>>= VLQ_BASE_SHIFT;
28
+ if (vlq > 0) digit |= VLQ_CONTINUATION_BIT;
29
+ encoded += BASE64_CHARS[digit];
30
+ } while (vlq > 0);
31
+ return encoded;
32
+ }
33
+
34
+ function encode_segment(fields) {
35
+ return fields.map(encode_vlq).join('');
36
+ }
37
+
38
+ // Generate a source map JSON object from an array of segments.
39
+ // Each segment is: [gen_line_0based, gen_col, src_file, src_line_0based, src_col]
40
+ // output_file: the name of the generated JS file
41
+ // source_root: optional prefix for source paths
42
+ function generate_source_map(segments, output_file, source_root) {
43
+ if (!segments || !segments.length) {
44
+ return JSON.stringify({
45
+ version: 3,
46
+ file: output_file || '',
47
+ sourceRoot: source_root || '',
48
+ sources: [],
49
+ names: [],
50
+ mappings: ''
51
+ });
52
+ }
53
+
54
+ // Build sources index
55
+ var sources_index = {};
56
+ var sources = [];
57
+ segments.forEach(function(seg) {
58
+ var src = seg[2];
59
+ if (src && !(src in sources_index)) {
60
+ sources_index[src] = sources.length;
61
+ sources.push(src);
62
+ }
63
+ });
64
+
65
+ // Sort segments by generated position
66
+ var sorted = segments.slice().sort(function(a, b) {
67
+ if (a[0] !== b[0]) return a[0] - b[0];
68
+ return a[1] - b[1];
69
+ });
70
+
71
+ // Remove duplicate segments at the same generated position
72
+ var deduped = [];
73
+ var prev_gen_line = -1, prev_gen_col = -1;
74
+ sorted.forEach(function(seg) {
75
+ if (seg[0] === prev_gen_line && seg[1] === prev_gen_col) return;
76
+ prev_gen_line = seg[0];
77
+ prev_gen_col = seg[1];
78
+ deduped.push(seg);
79
+ });
80
+
81
+ // Find max line
82
+ var max_line = 0;
83
+ deduped.forEach(function(seg) { if (seg[0] > max_line) max_line = seg[0]; });
84
+
85
+ // Group segments by generated line
86
+ var by_line = [];
87
+ for (var i = 0; i <= max_line; i++) by_line.push([]);
88
+ deduped.forEach(function(seg) { by_line[seg[0]].push(seg); });
89
+
90
+ // Encode mappings
91
+ var prev_src_idx = 0, prev_src_line = 0, prev_src_col = 0;
92
+ var mappings_lines = by_line.map(function(line_segs) {
93
+ var prev_col = 0;
94
+ return line_segs.map(function(seg) {
95
+ var gen_col = seg[1];
96
+ var src_idx = sources_index[seg[2]];
97
+ var src_line = seg[3];
98
+ var src_col = seg[4];
99
+ var result = encode_segment([
100
+ gen_col - prev_col,
101
+ src_idx - prev_src_idx,
102
+ src_line - prev_src_line,
103
+ src_col - prev_src_col
104
+ ]);
105
+ prev_col = gen_col;
106
+ prev_src_idx = src_idx;
107
+ prev_src_line = src_line;
108
+ prev_src_col = src_col;
109
+ return result;
110
+ }).join(',');
111
+ });
112
+
113
+ return JSON.stringify({
114
+ version: 3,
115
+ file: output_file ? path.basename(output_file) : '',
116
+ sourceRoot: source_root || '',
117
+ sources: sources,
118
+ names: [],
119
+ mappings: mappings_lines.join(';')
120
+ });
121
+ }
122
+
123
+ export { generate_source_map };