Skip to content

Commit dd72ddf

Browse files
committed
Deployed web site
1 parent 6517831 commit dd72ddf

File tree

11 files changed

+191
-52
lines changed

11 files changed

+191
-52
lines changed

stopify-compiler-cloud-function/ts/index.ts

Lines changed: 105 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ genericCompiler('pyjs', `${thirdPartyCompilers}/pyjs`, {
104104
es: 'sane',
105105
hofs: 'builtin',
106106
jsArgs: 'faithful',
107-
requireRuntime: false
107+
requireRuntime: false,
108+
eval: false
108109
});
109110

110111
genericCompiler('emscripten', `${thirdPartyCompilers}/emscripten`, {
@@ -115,7 +116,8 @@ genericCompiler('emscripten', `${thirdPartyCompilers}/emscripten`, {
115116
es: 'sane',
116117
hofs: 'builtin',
117118
jsArgs: 'simple',
118-
requireRuntime: false
119+
requireRuntime: false,
120+
eval: false
119121
});
120122

121123
genericCompiler('bucklescript', `${thirdPartyCompilers}/bucklescript`, {
@@ -126,7 +128,8 @@ genericCompiler('bucklescript', `${thirdPartyCompilers}/bucklescript`, {
126128
es: 'sane',
127129
hofs: 'builtin',
128130
jsArgs: 'simple',
129-
requireRuntime: false
131+
requireRuntime: false,
132+
eval: false
130133
});
131134

132135
genericCompiler('scalajs', `${thirdPartyCompilers}/scalajs`, {
@@ -137,7 +140,8 @@ genericCompiler('scalajs', `${thirdPartyCompilers}/scalajs`, {
137140
es: 'sane',
138141
hofs: 'builtin',
139142
jsArgs: 'simple',
140-
requireRuntime: false
143+
requireRuntime: false,
144+
eval: false
141145
});
142146

143147
genericCompiler('clojurescript', `${thirdPartyCompilers}/clojurescript`, {
@@ -148,7 +152,8 @@ genericCompiler('clojurescript', `${thirdPartyCompilers}/clojurescript`, {
148152
es: 'sane',
149153
hofs: 'builtin',
150154
jsArgs: 'simple',
151-
requireRuntime: false
155+
requireRuntime: false,
156+
eval: false
152157
});
153158

154159
genericCompiler('dart2js', `${thirdPartyCompilers}/dart2js`, {
@@ -159,5 +164,99 @@ genericCompiler('dart2js', `${thirdPartyCompilers}/dart2js`, {
159164
es: 'sane',
160165
hofs: 'builtin',
161166
jsArgs: 'simple',
162-
requireRuntime: false
167+
requireRuntime: false,
168+
eval: false
169+
});
170+
171+
stopify.post('/pyjs-fast', bodyParser.text({ type: '*/*' }), async (req, resp) => {
172+
try {
173+
resp.set('Access-Control-Allow-Origin', '*');
174+
resp.set('Access-Control-Allow-Methods', 'POST');
175+
176+
const { filename, exists } = await checkCache('pyjs-fast', req.body);
177+
if (exists) {
178+
return resp.send(filename);
179+
}
180+
console.info(`Compiling PyJS (fast) program (${req.body.length} bytes)`);
181+
const url = `${thirdPartyCompilers}/pyjs-fast`;
182+
const jsCode = await request.post(url, { headers, body: req.body });
183+
console.info(`Stopifying program (${jsCode.length} bytes)`);
184+
const dir = await tmpDir();
185+
try {
186+
const jsPath = `${dir}/original.js`;
187+
await fs.writeFile(jsPath, jsCode + '\npygwtOnLoad();');
188+
const stopifiedJsCode = await stopifyCompiler.stopify(jsPath, {
189+
compileFunction: 'module',
190+
getters: false,
191+
debug: false,
192+
captureMethod: 'lazy',
193+
newMethod: 'wrapper',
194+
es: 'sane',
195+
hofs: 'builtin',
196+
jsArgs: 'faithful',
197+
requireRuntime: false,
198+
eval: false
199+
});
200+
const prelude = await fs.readFile(__dirname + '/../pyjs_prelude.lazy.wrapper.faithful.js');
201+
await bucket.file(filename).save(prelude + stopifiedJsCode + `
202+
$__R.delimit(function () {
203+
$S.onEnd();
204+
});`
205+
);
206+
return resp.send(filename);
207+
}
208+
finally {
209+
await fs.remove(dir);
210+
}
211+
}
212+
catch (exn) {
213+
resp.statusCode = 503;
214+
const reason =
215+
(exn.name === 'StatusCodeError' ? exn.response.body : exn).toString();
216+
console.error(`Error: ${reason}`);
217+
return resp.send(reason.toString());
218+
}
219+
});
220+
221+
stopify.post('/js', bodyParser.text({ type: '*/*' }), async (req, resp) => {
222+
try {
223+
resp.set('Access-Control-Allow-Origin', '*');
224+
resp.set('Access-Control-Allow-Methods', 'POST');
225+
226+
const { filename, exists } = await checkCache('js', req.body);
227+
if (exists) {
228+
return resp.send(filename);
229+
}
230+
console.info(`Compiling JavaScript program (${req.body.length} bytes)`);
231+
const jsCode = req.body;
232+
const dir = await tmpDir();
233+
try {
234+
const jsPath = `${dir}/original.js`;
235+
await fs.writeFile(jsPath, jsCode);
236+
const stopifiedJsCode = await stopifyCompiler.stopify(jsPath, {
237+
compileFunction: false,
238+
getters: false,
239+
debug: true,
240+
captureMethod: 'lazy',
241+
newMethod: 'wrapper',
242+
es: 'sane',
243+
hofs: 'builtin',
244+
jsArgs: 'faithful',
245+
requireRuntime: false,
246+
eval: false
247+
});
248+
await bucket.file(filename).save(stopifiedJsCode);
249+
return resp.send(filename);
250+
}
251+
finally {
252+
await fs.remove(dir);
253+
}
254+
}
255+
catch (exn) {
256+
resp.statusCode = 503;
257+
const reason =
258+
(exn.name === 'StatusCodeError' ? exn.response.body : exn).toString();
259+
console.error(`Error: ${reason}`);
260+
return resp.send(reason.toString());
261+
}
163262
});

stopify-compiler-cloud-function/ts/testServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import * as index from './index';
33

44
const app = express();
55
app.use(index.stopifyTesting);
6-
app.listen(8080, '0.0.0.0');
6+
app.listen(8081, '0.0.0.0');
77

stopify-continuations/src/callcc/boxAssignables.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ const visitor = {
218218
// topFunction. It shouldn't be boxed since we want to preserve its
219219
// signature.
220220
if (vars.includes(path.node.id.name) &&
221-
!(state.opts.compileFunction && (<any>path.node).topFunction)) {
221+
!(state.opts.compileFunction === true && (<any>path.node).topFunction)) {
222222
const fun = t.functionExpression(
223223
fastFreshId.fresh('fun'),
224224
path.node.params,

stopify-continuations/src/callcc/callcc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const visitor: Visitor = {
9696
path.stop();
9797

9898
let toShift;
99-
if (opts.compileFunction) {
99+
if (opts.compileFunction === true) {
100100
if (t.isFunctionDeclaration(path.node.body[0])) {
101101
toShift = (<t.FunctionDeclaration>path.node.body[0]).body.body
102102
}

stopify-continuations/src/callcc/delimitTopLevel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const visitor = {
5252
// leave intact
5353
}
5454
else {
55-
if (state.opts.compileFunction && (<any>body[i]).topFunction) {
55+
if (state.opts.compileFunction !== false && (<any>body[i]).topFunction) {
5656
}
5757
else {
5858
body[i] = delimitStmt(body[i]);

stopify-continuations/src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
export type CaptureMethod = 'eager' | 'retval' | 'lazy' | 'original' | 'fudge';
22
export type HandleNew = 'direct' | 'wrapper'
3+
export type CompileFunction = boolean | 'module';
34

45
export interface CompilerOpts {
5-
compileFunction?: boolean,
6+
compileFunction?: CompileFunction,
67
getters: boolean,
78
debug: boolean,
89
captureMethod: CaptureMethod,

stopify-static-website/ts/languages.ts

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as stopifyCompiler from 'stopify';
2+
13
export interface Language {
24
compileUrl: string,
35
defaultCode: string
@@ -7,6 +9,31 @@ export interface Language {
79

810
const compilerBase = 'https://us-central1-arjun-umass.cloudfunctions.net/stopify';
911

12+
export function runtimeOpts(name: string): stopifyCompiler.Opts {
13+
if (name === 'pyjs' || name === 'js') {
14+
return {
15+
filename: '',
16+
estimator: 'reservoir',
17+
yieldInterval: 100,
18+
timePerElapsed: 1,
19+
resampleInterval: 1,
20+
variance: false,
21+
env: 'chrome',
22+
stop: undefined
23+
};
24+
}
25+
26+
return {
27+
filename: '',
28+
estimator: 'countdown',
29+
yieldInterval: 1,
30+
timePerElapsed: 1,
31+
resampleInterval: 1,
32+
variance: false,
33+
env: 'chrome',
34+
stop: undefined
35+
};
36+
}
1037

1138
export const langs: { [name: string]: Language } = {
1239
'Dart': {
@@ -86,16 +113,28 @@ object Runner extends JSApp {
86113
stepSupported: false,
87114
aceMode: 'python',
88115
defaultCode:
89-
`def run_forever():
90-
i = 0
91-
while(True):
92-
if i > 10000:
93-
i = 0
94-
i += 1
95-
print i
116+
`def fib(n):
117+
print "fib(" + str(n) + ")"
118+
if n == 0 or n == 1:
119+
return 1
120+
return fib(n-1) + fib(n-2)
96121
97-
run_forever()
98-
`,
99-
compileUrl: `${compilerBase}/pyjs`
122+
print (fib(15))`,
123+
compileUrl: `${compilerBase}/pyjs-fast`
124+
},
125+
JavaScript: {
126+
stepSupported: true,
127+
aceMode: 'js',
128+
defaultCode:
129+
`function fib(n) {
130+
console.log('fib(' + n + ')');
131+
if (n === 0 || n === 1) {
132+
return 1;
100133
}
134+
return fib(n-1) + fib(n-2);
101135
}
136+
137+
fib(15);`,
138+
compileUrl: `${compilerBase}/js`
139+
},
140+
};

stopify-static-website/ts/stopify.tsx

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import AceEditor from 'react-ace';
44
import { StopifyAce } from './StopifyAce';
55
import * as browser from 'detect-browser'
66
import * as ace from 'brace';
7-
import { langs } from './languages';
7+
import { langs, runtimeOpts } from './languages';
88

99
import * as stopifyCompiler from 'stopify';
1010

@@ -40,6 +40,7 @@ class MultilingualStopifyEditor extends React.Component<{}, {language: string}>
4040
<ul className="dropdown-menu">
4141
<li><a href="#" onClick={() => this.setState({ language: 'Dart' })}>Dart</a></li>
4242
<li><a href="#" onClick={() => this.setState({ language: 'Python' })}>Python</a></li>
43+
<li><a href="#" onClick={() => this.setState({ language: 'JavaScript' })}>JavaScript</a></li>
4344
<li><a href="#" onClick={() => this.setState({ language: 'Scala' })}>Scala</a></li>
4445
<li><a href="#" onClick={() => this.setState({ language: 'OCaml' })}>OCaml</a></li>
4546
<li><a href="#" onClick={() => this.setState({ language: 'C++' })}>C++</a></li>
@@ -141,16 +142,7 @@ class StopifyEditor extends React.Component<{ language: string }, StopifyEditorS
141142
}
142143
}))
143144
.then(path => {
144-
const opts: stopifyCompiler.Opts = {
145-
filename: path,
146-
estimator: 'countdown',
147-
yieldInterval: 1,
148-
timePerElapsed: 1,
149-
resampleInterval: 1,
150-
variance: false,
151-
env: browser.name as any,
152-
stop: undefined
153-
};
145+
const opts = runtimeOpts(this.state.language);
154146
this.setState({
155147
rhs: { type: 'iframe', url: './container.html', opts: opts, path: path }
156148
});
@@ -249,8 +241,8 @@ class StopifyEditor extends React.Component<{ language: string }, StopifyEditorS
249241
else {
250242
// The "key" in the iframe is unique and forces a full reload.
251243
rhs = <iframe key={this.state.rhs.url} ref={(frame) => this.iframe = frame}
252-
src={this.state.rhs.url} width='100%' height='100%'
253-
style={{border: 'none', overflow: 'hidden'}}>
244+
src={this.state.rhs.url} width='100%'
245+
style={{border: 'none', overflow: 'hidden', height: '80vh' }}>
254246
</iframe>;
255247
}
256248
return <div className="row display-flex">
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as fs from 'fs-extra';
2+
import * as path from 'path';
3+
import * as request from 'request-promise-native';
4+
import { tmpDir, exec } from './misc';
5+
6+
/**
7+
* Compiles a Python module using PyJS, but does not link it to the PyJS
8+
* runtime system. The produced module is called 'main' and the PyJS runtime
9+
* must be written to load 'main'.
10+
*
11+
* @param code body of the python module
12+
*/
13+
export async function compile(code: string): Promise<string> {
14+
const dir = await tmpDir();
15+
try {
16+
await fs.writeFile(`${dir}/main.py`, code);
17+
return await exec(`pyjscompile main.py`, dir);
18+
}
19+
finally {
20+
await fs.remove(dir);
21+
}
22+
}

stopify-third-party-compiler-container/server/ts/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as bucklescript from './bucklescript';
77
import * as clojurescript from './clojurescript';
88
import * as scalajs from './scalajs';
99
import * as dart2js from './dart2js';
10+
import * as pyjsFast from './pyjsFast';
1011

1112
export const app = express();
1213
app.use(morgan('short'));
@@ -31,5 +32,6 @@ compiler('/bucklescript', bucklescript.compile);
3132
compiler('/clojurescript', clojurescript.compile);
3233
compiler('/scalajs', scalajs.compile);
3334
compiler('/dart2js', dart2js.compile);
35+
compiler('/pyjs-fast', pyjsFast.compile);
3436

3537
app.listen(8080);

0 commit comments

Comments
 (0)