Skip to content

Commit

Permalink
Only sandbox dynamic javascript evaluation, allows using workers in w…
Browse files Browse the repository at this point in the history
…ebviews
  • Loading branch information
kmichel committed Sep 16, 2014
1 parent 0bcd1f8 commit 391023b
Show file tree
Hide file tree
Showing 8 changed files with 401 additions and 332 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ <h1>G-Code Q'n'dirty toolpath simulator</h1>
this.set('computing', true);
_this.set('lineSegmentMap', []);
this.get('simulatedPath').clear();
gcodeSimulation.tryToParseInWorker(this.get('code'), {x: 0, y: 0, z: 0}, Ember.run.bind(_this, handleResult), Ember.run.bind(_this, function (fragment) {
gcodeSimulation.parseInWorker(this.get('code'), {x: 0, y: 0, z: 0}, Ember.run.bind(_this, handleResult), Ember.run.bind(_this, function (fragment) {
_this.get('fragmentFile').pushObject(fragment);
Ember.run.throttle(_this, _this.flushFragmentFile, 500);
}));
Expand Down
312 changes: 7 additions & 305 deletions webapp/CAM.html

Large diffs are not rendered by default.

317 changes: 317 additions & 0 deletions webapp/CAM.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion webapp/cnc/cam.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,13 @@ define(['cnc/bezier', 'cnc/clipper', 'libs/simplify', 'cnc/util', 'libs/extracte
geom: geom,
pushOnPath: pushOnPath,
Machine: Machine,
ConstantZPolygonToolpath: ConstantZPolygonToolpath,
GeneralPolylineToolpath: GeneralPolylineToolpath,
decomposePolytreeInTopLevelPolygons: decomposePolytreeInTopLevelPolygons,
polyOp: polyOp,
simplifyPolygons: simplifyPolygons,
pathDefToClipper: pathDefToClipper,
dumpGCode: dumpGCode,
simplifyScaleAndCreatePathDef: simplifyScaleAndCreatePathDef
};
});
});
22 changes: 1 addition & 21 deletions webapp/cnc/gcode/gcodeSimulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,9 @@ define(['cnc/gcode/simulation', 'cnc/gcode/parser', 'cnc/util', 'require'], func
resultHandler(simulateGCode(code, initialPosition, fragmentHandler));
}

/**
*
* why not always in worker? because workers don't work in webviews in chrome apps
* https://code.google.com/p/chromium/issues/detail?id=159303
*/
function tryToParseInWorker(code, initialPosition, resultHandler, fragmentHandler) {
try {
parseInWorker(code, initialPosition, resultHandler, fragmentHandler);
} catch (error) {
console.log('worker error', error);
console.log('trying again without worker');
// It doesn't work in immediate code, maybe some ember thing with run.bind.
// Ember code is not readable enough to find a good explanation.
setTimeout(function () {
parseImmediately(code, initialPosition, resultHandler, fragmentHandler);
}, 0);
}
}

return {
simulateGCode: simulateGCode,
parseImmediately: parseImmediately,
parseInWorker: parseInWorker,
tryToParseInWorker: tryToParseInWorker,
simulateWorkerSide: simulateWorkerSide};
});
});
4 changes: 2 additions & 2 deletions webapp/cnc/ui/jsEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ define(['Ember', 'ace'], function (Em, ace) {
var _this = this;
this.set('editor', ace.edit(this.get('element')));
var editor = this.get('editor');
editor.session.setUseWorker(false);
editor.session.setUseWorker(true);
editor.setTheme("ace/theme/chaos");
editor.session.setMode("ace/mode/javascript");
editor.on('change', function () {
Expand Down Expand Up @@ -45,4 +45,4 @@ define(['Ember', 'ace'], function (Em, ace) {
}.property()
});
return {JSEditorComponent: JSEditorComponent};
});
});
68 changes: 68 additions & 0 deletions webapp/js_sandbox.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!DOCTYPE html>
<html>
<body>
<div id="paper"></div>
<script src="libs/require.js"></script>
<script src="libs/jquery.min.js"></script>
<script>
require(['cnc/cam', 'libs/svg'], function (cam, svg) {
window.addEventListener('message', function (event) {
switch (event.data.command) {
case 'eval':
try {
var code = event.data.code;
var paper = new svg(document.getElementById("paper"));
var machine = new cam.Machine(paper);

function sendResponse() {
var operationsData = [];
for (var i = 0; i < machine.operations.length; ++i) {
var operation = machine.operations[i];
operationsData.push({
className: operation.constructor.name,
path: operation.path
});
}
event.source.postMessage({
command: 'eval-result',
success: true,
result: {
operations: operationsData,
svg: paper.node.innerHTML
},
requestIndex: event.data.requestIndex
}, event.origin);
}

function sendError(message) {
event.source.postMessage({
command: 'eval-result',
success: false,
error: message,
requestIndex: event.data.requestIndex
}, event.origin);
}

var waitForWhenDone = true;

function whenDone() {
if (waitForWhenDone)
sendResponse();
else
throw new Exception("You must return true at the end of your code if you use whenDone");
}

waitForWhenDone = new Function(['cam', 'machine', 'whenDone'], code)(cam, machine, whenDone);
} catch (error) {
sendError(error.message);
return;
}
if (!waitForWhenDone)
sendResponse();
break;
}
});
});
</script>
</body>
</html>
4 changes: 2 additions & 2 deletions webapp/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
}
},
"sandbox": {
"pages": ["CAM.html", "oldCAM.html"]
"pages": ["js_sandbox.html"]
}
}
}

0 comments on commit 391023b

Please sign in to comment.