Skip to content

Commit 01e47a9

Browse files
committed
Hacks
1 parent 462cdd0 commit 01e47a9

File tree

10 files changed

+138
-52
lines changed

10 files changed

+138
-52
lines changed

stopify-compiler-cloud-function/test.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

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: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface Language {
1010
const compilerBase = 'https://us-central1-arjun-umass.cloudfunctions.net/stopify';
1111

1212
export function runtimeOpts(name: string): stopifyCompiler.Opts {
13-
if (name === 'pyjs') {
13+
if (name === 'pyjs' || name === 'js') {
1414
return {
1515
filename: '',
1616
estimator: 'reservoir',
@@ -113,16 +113,28 @@ object Runner extends JSApp {
113113
stepSupported: false,
114114
aceMode: 'python',
115115
defaultCode:
116-
`def run_forever():
117-
i = 0
118-
while(True):
119-
if i > 10000:
120-
i = 0
121-
i += 1
122-
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)
123121
124-
run_forever()
125-
`,
122+
print (fib(15))`,
126123
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;
127133
}
134+
return fib(n-1) + fib(n-2);
128135
}
136+
137+
fib(15);`,
138+
compileUrl: `${compilerBase}/js`
139+
},
140+
};

stopify-static-website/ts/stopify.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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>
@@ -240,8 +241,8 @@ class StopifyEditor extends React.Component<{ language: string }, StopifyEditorS
240241
else {
241242
// The "key" in the iframe is unique and forces a full reload.
242243
rhs = <iframe key={this.state.rhs.url} ref={(frame) => this.iframe = frame}
243-
src={this.state.rhs.url} width='100%' height='100%'
244-
style={{border: 'none', overflow: 'hidden'}}>
244+
src={this.state.rhs.url} width='100%'
245+
style={{border: 'none', overflow: 'hidden', height: '80vh' }}>
245246
</iframe>;
246247
}
247248
return <div className="row display-flex">

stopify/src/stopify/stopifyCallCC.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@ import suspendStop from './suspendStop';
55
import suspendStep from './suspendStep';
66
import { timeSlow } from '../generic';
77

8-
const allowed = [
9-
"Object",
10-
"exports",
11-
"require",
12-
"console",
13-
"global",
14-
"window",
15-
"document",
16-
"setTimeout",
17-
"captureCC"
18-
];
19-
208
export const visitor: Visitor = {
219
Program(path: NodePath<t.Program>, state) {
2210
const opts: callcc.CompilerOpts = state.opts;
@@ -40,10 +28,6 @@ export const visitor: Visitor = {
4028

4129
callcc.fastFreshId.init(path);
4230
const plugs: any[] = [];
43-
// Cleanup globals when not running in `func` compile mode
44-
if (!state.opts.compileFunction) {
45-
plugs.push([callcc.cleanupGlobals, { allowed }])
46-
}
4731

4832
timeSlow('hygiene, etc.', () =>
4933
callcc.transformFromAst(path, [
@@ -60,7 +44,7 @@ export const visitor: Visitor = {
6044

6145
callcc.fastFreshId.cleanup()
6246

63-
if (opts.compileFunction) {
47+
if (opts.compileFunction === true || opts.compileFunction === 'module') {
6448
// Do nothing
6549
}
6650
else if (opts.requireRuntime) {
@@ -90,7 +74,7 @@ export const visitor: Visitor = {
9074
[t.identifier('$__R')]))]));
9175
}
9276

93-
if (!opts.compileFunction) {
77+
if (opts.compileFunction === false) {
9478
path.node.body.push(
9579
t.expressionStatement(
9680
t.callExpression(

0 commit comments

Comments
 (0)