@live-codes/browser-compilers 0.4.8 → 0.4.12

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.
@@ -0,0 +1,99 @@
1
+ // This is a minimal interface to the gnuplot worker. More sophisticated include virtual file-system lists and a reset function.
2
+
3
+ var Gnuplot = function (js_filename) {
4
+ this.worker = new Worker(js_filename);
5
+ this.output = [];
6
+ this.error = [];
7
+ this.isRunning = false;
8
+
9
+ // These two should be overwritten by application.
10
+ this.onOutput = function (text) {
11
+ console.log('Gnuplot output: ' + text);
12
+ };
13
+ this.onError = function (text) {
14
+ console.log('Gnuplot error: ' + text);
15
+ };
16
+
17
+ this.transaction = 1;
18
+ this.callbacks = [];
19
+ this.postCommand = function (cmd_block, callback) {
20
+ var id = this.transaction; // fresh id
21
+ cmd_block.transaction = id; // give data object a tag
22
+ this.callbacks[id] = callback;
23
+ this.worker.postMessage(cmd_block);
24
+ this.transaction++;
25
+ };
26
+
27
+ var that = this;
28
+ this.worker.addEventListener(
29
+ 'message',
30
+ function (e) {
31
+ // console.log('gnuplot: ', e.data); //enable for debug
32
+ var data = e.data;
33
+ if (data.transaction < 0) {
34
+ if (data.transaction == -1) {
35
+ that.output.push(data.content);
36
+ that.onOutput(data.content);
37
+ }
38
+ if (data.transaction == -2) {
39
+ that.error.push(data.content);
40
+ that.onError(data.content);
41
+ }
42
+ return;
43
+ }
44
+ if (data.content == 'FINISH') that.isRunning = false;
45
+ if (data.transaction && that.callbacks[data.transaction]) {
46
+ that.callbacks[data.transaction](data);
47
+ delete that.callbacks[data.transaction];
48
+ }
49
+ },
50
+ false,
51
+ );
52
+
53
+ this.worker.postMessage({}); // supposed to do this to start the worker?
54
+ };
55
+
56
+ Gnuplot.prototype.putFile = function (name_, contents, callback) {
57
+ var data = {
58
+ name: name_,
59
+ content: contents,
60
+ cmd: 'putFile',
61
+ };
62
+ this.postCommand(data, callback);
63
+ };
64
+
65
+ // to read output
66
+ Gnuplot.prototype.getFile = function (name_, callback) {
67
+ var data = {
68
+ name: name_,
69
+ cmd: 'getFile',
70
+ };
71
+ this.postCommand(data, callback);
72
+ };
73
+
74
+ Gnuplot.prototype.getFileList = function (callback) {
75
+ var data = {
76
+ cmd: 'getFileList',
77
+ };
78
+ this.postCommand(data, callback);
79
+ };
80
+
81
+ Gnuplot.prototype.removeFiles = function (files, callback) {
82
+ var data = {
83
+ name: files,
84
+ cmd: 'removeFiles',
85
+ };
86
+ this.postCommand(data, callback);
87
+ };
88
+
89
+ Gnuplot.prototype.run = function (script, onFinish) {
90
+ if (this.isRunning) return false;
91
+ this.putFile('foo', script);
92
+ var data = {
93
+ content: ['foo'],
94
+ cmd: 'run',
95
+ };
96
+ this.isRunning = true;
97
+ this.postCommand(data, onFinish);
98
+ return true;
99
+ };
@@ -0,0 +1,85 @@
1
+ // @ts-nocheck
2
+ // based on https://github.com/MikeRalphson/turbopascal/blob/master/tpc.js
3
+ const turbopascal = {
4
+ createCompiler: (options = {}) =>
5
+ new Promise((resolve) => {
6
+ const inputCallback =
7
+ options.inputCallback ||
8
+ ((callback) => {
9
+ const input = window.prompt('please supply the input');
10
+ callback(input);
11
+ });
12
+ const outputCallback = options.outputCallback || console.log;
13
+ const errorCallback = options.errorCallback || console.warn;
14
+
15
+ requirejs.config({
16
+ baseUrl: 'https://cdn.jsdelivr.net/npm/turbopascal@1.0.3',
17
+ paths: {
18
+ underscore: 'https://cdn.jsdelivr.net/npm/underscore@1.13.2/underscore-umd.min',
19
+ },
20
+ });
21
+
22
+ require([
23
+ 'Stream',
24
+ 'Lexer',
25
+ 'CommentStripper',
26
+ 'Parser',
27
+ 'Compiler',
28
+ 'Machine',
29
+ 'SymbolTable',
30
+ ], function (Stream, Lexer, CommentStripper, Parser, Compiler, Machine, SymbolTable) {
31
+ function compile(source, run, DEBUG_TRACE) {
32
+ let result = { parsed: false, compiled: false };
33
+ if (!source) return result;
34
+
35
+ let stream = new Stream(source);
36
+ let lexer = new CommentStripper(new Lexer(stream));
37
+ let parser = new Parser(lexer);
38
+
39
+ try {
40
+ // Create the symbol table of built-in constants, functions, and procedures.
41
+ let builtinSymbolTable = SymbolTable.makeBuiltinSymbolTable();
42
+
43
+ // Parse the program into a parse tree. Create the symbol table as we go.
44
+ let root = parser.parse(builtinSymbolTable);
45
+ result.tree = root.print('');
46
+ result.parsed = true;
47
+
48
+ // Compile to bytecode.
49
+ let compiler = new Compiler();
50
+ let bytecode = compiler.compile(root);
51
+ result.compiled = true;
52
+ result.bytecode = bytecode;
53
+ result.pSrc = bytecode.print();
54
+
55
+ if (run) {
56
+ // Execute the bytecode.
57
+ let machine = new Machine(bytecode, this.keyboard);
58
+ if (DEBUG_TRACE) {
59
+ machine.setDebugCallback(function (state) {
60
+ $state.append(state + '\\n');
61
+ });
62
+ }
63
+ machine.setFinishCallback(function (runningTime) {});
64
+ machine.setOutputCallback(function (line) {
65
+ outputCallback(line);
66
+ });
67
+ machine.setOutChCallback(function (line) {
68
+ outputCallback(line);
69
+ });
70
+ machine.setInputCallback(function (callback) {
71
+ inputCallback(callback);
72
+ });
73
+ machine.run();
74
+ }
75
+ } catch (e) {
76
+ // Print errors.
77
+ const msg = e.getMessage() || e.message || e;
78
+ errorCallback(msg);
79
+ }
80
+ return result;
81
+ }
82
+ resolve(compile);
83
+ });
84
+ }),
85
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-codes/browser-compilers",
3
- "version": "0.4.8",
3
+ "version": "0.4.12",
4
4
  "description": "Compilers that run in the browser, for use in livecodes.io",
5
5
  "author": "Hatem Hosny",
6
6
  "license": "MIT",
@@ -9,7 +9,7 @@
9
9
  "url": "https://github.com/live-codes/browser-compilers"
10
10
  },
11
11
  "scripts": {
12
- "start": "live-server dist --cors",
12
+ "start": "live-server dist --port=8081 --no-browser --cors",
13
13
  "build": "run-s clean build:vendors copy:types",
14
14
  "build:vendors": "node ./scripts/vendors.js",
15
15
  "copy:types": "recursive-copy vendor_modules/types dist/types",
@@ -21,7 +21,7 @@ function mkdirp(dir) {
21
21
  }
22
22
  }
23
23
 
24
- var vendor_modules = path.resolve(__dirname + '/../vendor_modules/src');
24
+ var vendor_modules_src = path.resolve(__dirname + '/../vendor_modules/src');
25
25
  var targetDir = path.resolve(__dirname + '/../dist');
26
26
 
27
27
  /** @type {Partial<esbuild.BuildOptions>} */
@@ -75,21 +75,21 @@ esbuild.buildSync({
75
75
  // asciidoctor.css
76
76
  mkdirp(targetDir + '/asciidoctor.css');
77
77
  fs.copyFileSync(
78
- path.resolve(vendor_modules + '/asciidoctor.css/asciidoctor.css'),
78
+ path.resolve(vendor_modules_src + '/asciidoctor.css/asciidoctor.css'),
79
79
  path.resolve(targetDir + '/asciidoctor.css/asciidoctor.css'),
80
80
  );
81
81
 
82
82
  // stylus
83
83
  mkdirp(targetDir + '/stylus');
84
84
  fs.copyFileSync(
85
- path.resolve(vendor_modules + '/stylus/stylus.min.js'),
85
+ path.resolve(vendor_modules_src + '/stylus/stylus.min.js'),
86
86
  path.resolve(targetDir + '/stylus/stylus.min.js'),
87
87
  );
88
88
 
89
89
  // pug
90
90
  mkdirp(targetDir + '/pug');
91
91
  fs.copyFileSync(
92
- path.resolve(vendor_modules + '/pug/pug.min.js'),
92
+ path.resolve(vendor_modules_src + '/pug/pug.min.js'),
93
93
  path.resolve(targetDir + '/pug/pug.min.js'),
94
94
  );
95
95
 
@@ -147,7 +147,7 @@ esbuild.buildSync({
147
147
  // clientside-haml-js
148
148
  mkdirp(targetDir + '/clientside-haml-js');
149
149
  fs.copyFileSync(
150
- path.resolve(vendor_modules + '/clientside-haml-js/haml.js'),
150
+ path.resolve(vendor_modules_src + '/clientside-haml-js/haml.js'),
151
151
  path.resolve(targetDir + '/clientside-haml-js/haml.js'),
152
152
  );
153
153
 
@@ -163,12 +163,12 @@ esbuild.build({
163
163
  // livescript
164
164
  mkdirp(targetDir + '/livescript');
165
165
  fs.copyFileSync(
166
- path.resolve(vendor_modules + '/livescript/livescript-min.js'),
166
+ path.resolve(vendor_modules_src + '/livescript/livescript-min.js'),
167
167
  path.resolve(targetDir + '/livescript/livescript-min.js'),
168
168
  );
169
169
  // prelude.ls (livescript base library)
170
170
  fs.copyFileSync(
171
- path.resolve(vendor_modules + '/livescript/prelude-browser-min.js'),
171
+ path.resolve(vendor_modules_src + '/livescript/prelude-browser-min.js'),
172
172
  path.resolve(targetDir + '/livescript/prelude-browser-min.js'),
173
173
  );
174
174
 
@@ -208,7 +208,7 @@ esbuild.build({
208
208
  // JSCPP
209
209
  mkdirp(targetDir + '/jscpp');
210
210
  fs.copyFileSync(
211
- path.resolve(vendor_modules + '/jscpp/JSCPP.es5.min.js'),
211
+ path.resolve(vendor_modules_src + '/jscpp/JSCPP.es5.min.js'),
212
212
  path.resolve(targetDir + '/jscpp/JSCPP.es5.min.js'),
213
213
  );
214
214
 
@@ -216,18 +216,36 @@ fs.copyFileSync(
216
216
  mkdirp(targetDir + '/wacl');
217
217
  mkdirp(targetDir + '/wacl/tcl');
218
218
  fs.copyFileSync(
219
- path.resolve(vendor_modules + '/wacl/tcl/wacl.js'),
219
+ path.resolve(vendor_modules_src + '/wacl/tcl/wacl.js'),
220
220
  path.resolve(targetDir + '/wacl/tcl/wacl.js'),
221
221
  );
222
222
  fs.copyFileSync(
223
- path.resolve(vendor_modules + '/wacl/tcl/wacl.wasm'),
223
+ path.resolve(vendor_modules_src + '/wacl/tcl/wacl.wasm'),
224
224
  path.resolve(targetDir + '/wacl/tcl/wacl.wasm'),
225
225
  );
226
226
  fs.copyFileSync(
227
- path.resolve(vendor_modules + '/wacl/tcl/wacl-custom.data'),
227
+ path.resolve(vendor_modules_src + '/wacl/tcl/wacl-custom.data'),
228
228
  path.resolve(targetDir + '/wacl/tcl/wacl-custom.data'),
229
229
  );
230
230
  fs.copyFileSync(
231
- path.resolve(vendor_modules + '/wacl/tcl/wacl-library.data'),
231
+ path.resolve(vendor_modules_src + '/wacl/tcl/wacl-library.data'),
232
232
  path.resolve(targetDir + '/wacl/tcl/wacl-library.data'),
233
233
  );
234
+
235
+ // turbopascal
236
+ mkdirp(targetDir + '/turbopascal');
237
+ fs.copyFileSync(
238
+ path.resolve(vendor_modules_src + '/turbopascal/turbopascal.js'),
239
+ path.resolve(targetDir + '/turbopascal/turbopascal.js'),
240
+ );
241
+
242
+ // gnuplot
243
+ mkdirp(targetDir + '/gnuplot');
244
+ fs.copyFileSync(
245
+ path.resolve(vendor_modules_src + '/gnuplot-JS/www/gnuplot.js'),
246
+ path.resolve(targetDir + '/gnuplot/gnuplot.js'),
247
+ );
248
+ fs.copyFileSync(
249
+ path.resolve(vendor_modules_src + '/gnuplot-JS/www/gnuplot_api.js'),
250
+ path.resolve(targetDir + '/gnuplot/gnuplot_api.js'),
251
+ );
@@ -10,6 +10,8 @@ clientside-haml-js: [MIT License](https://github.com/uglyog/clientside-haml-js/b
10
10
 
11
11
  CoffeeScript: [MIT License](https://github.com/jashkenas/coffeescript/tree/07f644c39223e016aceedd2cd71b5941579b5659)
12
12
 
13
+ gnuplot-JS: [MIT License](https://github.com/chhu/gnuplot-JS/blob/62a3c8488ad00c97807ba48ae75738ca3af607fe/LICENSE)
14
+
13
15
  JSCPP: [MIT License](https://github.com/felixhao28/JSCPP/blob/befbd6b48666007151c259c5dd291ab028ad4c04/LICENSE)
14
16
 
15
17
  Less: [Apache License 2.0](https://github.com/less/less.js/blob/870f9b2d8136bfbcdc9e1293bb0def51b54f9276/LICENSE)
@@ -46,6 +48,8 @@ Stylus: [MIT License](https://github.com/stylus/stylus/blob/59bc665db295981d4e3f
46
48
 
47
49
  Svelte: [MIT License](https://github.com/sveltejs/svelte/blob/dafbdc286eef3de2243088a9a826e6899e20465c/LICENSE)
48
50
 
51
+ turbopascal: [BSD 2-Clause License](https://github.com/MikeRalphson/turbopascal/blob/cfcc5ee6d93f32664bb6127aa751c68841ca5c1d/LICENSE)
52
+
49
53
  wacl: [BSD 3-Clause License](https://github.com/ecky-l/wacl/blob/9daacabb0102a9986f33263261350edfeebdd83b/LICENSE)
50
54
 
51
55
  wasm-refmt: [MIT License](https://github.com/xtuc/webassemblyjs/blob/45f733aa96476d74c8ac57598e13406a48a6fdc8/LICENSE)
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Christian Huettig
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,55 @@
1
+ Portable JS Gnuplot
2
+ *******************
3
+
4
+ This is the result of compiling gnuplot with emscripten, combined with an API so it is usable straight out of the box in your browser.
5
+ The www directory contains the necessary files and a compiled version of gnuplot 4.6.3. The gnuplot-4.6.3 directory contains everything to
6
+ build the gnuplot sources with emscripten.
7
+
8
+ How to compile gnuplot
9
+ **********************
10
+
11
+ - Install llvm / emscripten, follow instructions at http://emscripten.org
12
+ - Download the gnuplot-4.6.3.tar.gz sources at http://gnuplot.info and untar.
13
+ - Copy the contents of this repo's gnuplot-4.6.3 into the folder where you unpacked the sources (should be the same name).
14
+ - [optional] adjust src/term.h and remove unneeded terminals to reduce final size
15
+ - [optional] patch src/axis.c with the patch file to fix axis descriptions: cd src;patch <../gnuplot-4.6.3.patch
16
+ - Run em_make to configure and build, you should have a gnuplot.js in your directory. WAIT at the end! The final step takes long.
17
+
18
+ How the API works
19
+ *****************
20
+
21
+ The gnuplot API (gnuplot_api.js) creates a worker (JS-thread) that loads gnuplot.js and handles all the work. No browser checks are done to check for HTML5 features.
22
+ The API lets you interact with the worker to "upload" or "download" files. Emscripten uses a mock-up for file I/O, so keep in mind that files are kept in memory as arrays
23
+ and that you need to push / pull any file-content to / from the fake file system within the worker. The API provides some convenience functions for this.
24
+
25
+ The demo site from the www directory is online at http://gnuplot.respawned.com
26
+
27
+ A minimal implementation should look like this (untested! see index.html for working version):
28
+
29
+ <img id="result"/>
30
+ <script src='gnuplot_api.js'></script>
31
+ <script>
32
+ gnuplot = new Gnuplot('gnuplot.js');
33
+ //gnuplot.putFile("myData.dat", "1 2 3 4 5 6 7 8 9 0"); // No callback needed here
34
+ gnuplot.run("my gnuplot script as a string", function(e) {
35
+ gnuplot.getFile('out.svg', function(e) {
36
+ if (!e.content) {
37
+ alert("Output file out.svg not found!");
38
+ return;
39
+ }
40
+ var img = document.getElementById('result');
41
+ try {
42
+ var ab = new Uint8Array(e.content);
43
+ var blob = new Blob([ab], {"type": "image\/svg+xml"});
44
+ window.URL = window.URL || window.webkitURL;
45
+ img.src = window.URL.createObjectURL(blob);
46
+ } catch (err) { // in case blob / URL missing, fallback to data-uri
47
+ var rstr = '';
48
+ for (var i = 0; i < e.content.length; i++)
49
+ rstr += String.fromCharCode(e.content[i]);
50
+ img.src = 'data:image\/svg+xml;base64,' + btoa(rstr);
51
+ }
52
+ });
53
+ });
54
+ </script>
55
+
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ emconfigure ./configure --without-readline --without-x --disable-h3d-quadtree --disable-wxwidgets
3
+ #emmake make clean
4
+ emmake make
5
+ rm gnuplot.o
6
+ mv src/gnuplot ./gnuplot.o
7
+ em++ -s INVOKE_RUN=0 -O2 -s ASM_JS=1 gnuplot.o -o gnuplot.js --post-js post.js --pre-js pre.js
@@ -0,0 +1,13 @@
1
+ --- gnuplot-4.6.3/src/axis.c 2012-11-08 18:13:08.000000000 +0100
2
+ +++ gnuplot-4.6.3_mod/src/axis.c 2013-05-11 11:08:08.791587160 +0200
3
+ @@ -467,8 +467,9 @@ copy_or_invent_formatstring(AXIS_INDEX a
4
+ int precision = (ceil(-log10(fabs(axmax-axmin))));
5
+ if ((axmin*axmax > 0) && precision > 4)
6
+ sprintf(ticfmt[axis],"%%.%df", (precision>14) ? 14 : precision);
7
+ + else
8
+ + strcpy(ticfmt[axis], "%g");
9
+ }
10
+ -
11
+ return ticfmt[axis];
12
+ }
13
+
@@ -0,0 +1,17 @@
1
+ shouldRunNow = true;
2
+ self['Runtime'] = Runtime;
3
+ self['FS'] = FS;
4
+ };
5
+ gnuplot_create();
6
+ // This is to avoid name mangling from closure compilers
7
+ self['FS'] = FS;
8
+ self['FS']['root'] = FS.root;
9
+ self['FS']['deleteFile'] = FS.deleteFile;
10
+ self['FS']['findObject'] = FS.findObject;
11
+ self['FS']['createDataFile'] = FS.createDataFile;
12
+ self['FS']['getFileContents'] = function(name) {
13
+ var file = FS.findObject(name);
14
+ if (!file) return null;
15
+ return file.contents;
16
+ };
17
+
@@ -0,0 +1,62 @@
1
+ self.addEventListener('message', function(e) {
2
+ var data = e.data;
3
+ if (!data) return;
4
+ var cmd = data['cmd'];
5
+ var transaction = data['transaction'];
6
+ var name = data['name'];
7
+ var content = data['content'];
8
+ var result = {
9
+ transaction: transaction
10
+ };
11
+ switch (cmd) {
12
+ case 'run':
13
+ try {
14
+ shouldRunNow = true;
15
+ if (content)
16
+ Module.run(content);
17
+ else
18
+ Module.run();
19
+ } catch(err) {
20
+
21
+ Module.printErr('Exit called, reset state.');
22
+ gnuplot_create();
23
+ };
24
+ result['content'] = 'FINISH';
25
+ self.postMessage(result);
26
+ break;
27
+
28
+ case 'putFile':
29
+ if (FS.findObject(name))
30
+ FS.deleteFile(name);
31
+ var arrc = content;
32
+ if (typeof(content) == "string")
33
+ arrc = Module['intArrayFromString'](content, true);
34
+ FS.createDataFile('/', name, arrc);
35
+ result['content'] = 'OK';
36
+ self.postMessage(result);
37
+ break;
38
+
39
+ case 'getFile':
40
+ var file = FS.findObject(name);
41
+ result['content'] = file.contents || 0;
42
+ self.postMessage(result);
43
+ break;
44
+
45
+ default:
46
+ result['content'] = 'unknown cmd';
47
+ self.postMessage(result);
48
+ };
49
+ }, false);
50
+
51
+
52
+ var Module = {
53
+ 'noInitialRun': true,
54
+ print: function(text) {
55
+ self.postMessage({'transaction': -1, 'content': text});
56
+ },
57
+ printErr: function(text) {
58
+ self.postMessage({'transaction': -2, 'content': text});
59
+ },
60
+
61
+ };
62
+ function gnuplot_create() {