Skip to content

Commit

Permalink
incremental changes
Browse files Browse the repository at this point in the history
  • Loading branch information
peey committed Oct 14, 2018
1 parent 8f06808 commit 0db548a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 81 deletions.
43 changes: 0 additions & 43 deletions client/emscripten.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,6 @@
<body>

<script type='text/javascript'>
var Module = {
preRun: [],
postRun: [],
print: (function() {
return function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
console.log(text);
};
})(),
printErr: function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
if (0) { // XXX disabled for safety typeof dump == 'function') {
dump(text + '\n'); // fast, straight to the real console
} else {
console.error(text);
}
},
setStatus: function(text) {
if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
if (text === Module.setStatus.last.text) return;
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
var now = Date.now();
if (m && now - Module.setStatus.last.time < 30) return; // if this is a progress update, skip it if too soon
Module.setStatus.last.time = now;
Module.setStatus.last.text = text;
if (m) {
text = m[1];
}
},
totalDependencies: 0,
monitorRunDependencies: function(left) {
this.totalDependencies = Math.max(this.totalDependencies, left);
Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
}
};
Module.setStatus('Downloading...');
window.onerror = function(event) {
// TODO: do not warn on ok events like simulating an infinite loop or exitStatus
Module.setStatus('Exception thrown, see JavaScript console');
Module.setStatus = function(text) {
if (text) Module.printErr('[post-exception status] ' + text);
};
};
</script>
<script async type="text/javascript" src="primes/primes.js"></script>
</body>
Expand Down
7 changes: 1 addition & 6 deletions client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@

<script>
</script>
<script src="index.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="index.js"></script>
<script>
var vm = {
runningATask: ko.observable(false),
tasksDone: ko.observable(0)
}
ko.applyBindings(vm)
</script>
</body>
</html>
132 changes: 100 additions & 32 deletions client/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
// knockout
var vm = {
runningATask: ko.observable(false),
tasksDone: ko.observable(0)
}
ko.applyBindings(vm)

// other stuff
function noop() {}

const go = new Go();

memoized_wasm = {}
function getCompiledWasmPromise(path) {
if (!memoized_wasm[path]) {
return WebAssembly.compileStreaming(fetch(path)).then((module) => memoized_wasm[path] = module)
} else {
return Promise.resolve(memoized_wasm[path])
}
}

var s = gotalk.connection().on('open', function () {
console.log('socket opened');
Expand All @@ -12,50 +28,102 @@ var s = gotalk.connection().on('open', function () {

gotalk.handle("receive-job", function (params, result) {

console.log("got a job: ", params);
vm.runningATask(true);
console.log("got a job: ", params);
vm.runningATask(true);

setTimeout(function () {
instantiate_go_script(params)
}, 0);

result({"is_okay": "Okay"});
result({"is_okay": "Okay"});
});
}).on('close', function (err) {
console.log('socket closed', err);
}).on('notification', function (n, v) {
console.log('received notification "' + name + '":', value);
});


function instantiate_go_script(params) {
WebAssembly.instantiateStreaming(fetch(params.wasm), go.importObject).then((result) => {
setTimeout(function () {
var log = console.log;
var output = "";
console.log = function () {
var args = Array.prototype.slice.call(arguments);
output += args.map(function (x) {
return String(x)
});
log.apply(this, args);
};
const go = new Go();
getCompiledWasmPromise(params.wasm)
.then(function (compiled) {
console.log("got compiled", compiled)
return WebAssembly.instantiate(compiled, go.importObject)
})
.then((instantiated) => {
console.log("got instantiated", instantiated)
setTimeout(function () {
var log = console.log;
var output = "";
console.log = function () {
var args = Array.prototype.slice.call(arguments);
output += args.map(function (x) {
return String(x)
});
log.apply(this, args);
};

go.argv = ["js"].concat(params.parameters.split(" "));
go.run(result.instance);

s.request("job-complete", {
job_id: params.job_id,
program_id: params.program_id,
parameters: params.parameters,
result: output
}, noop);

log("finished job", output);
vm.runningATask(false);
vm.tasksDone(vm.tasksDone() + 1)
}, 0);
});
};
go.argv = ["js"].concat(params.parameters.split(" "));
go.run(instantiated);

s.request("job-complete", {
job_id: params.job_id,
program_id: params.program_id,
parameters: params.parameters,
result: output
}, noop);

log("finished job", output);
vm.runningATask(false);
vm.tasksDone(vm.tasksDone() + 1)
}
)
})
.catch(x => console.log("error", x));
};

function instantiate_emscripten(params) {
var Module = {
preRun: [],
postRun: [],
print: (function() {
return function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
console.log(text);
};
})(),
printErr: function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
if (0) { // XXX disabled for safety typeof dump == 'function') {
dump(text + '\n'); // fast, straight to the real console
} else {
console.error(text);
}
},
setStatus: function(text) {
if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
if (text === Module.setStatus.last.text) return;
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
var now = Date.now();
if (m && now - Module.setStatus.last.time < 30) return; // if this is a progress update, skip it if too soon
Module.setStatus.last.time = now;
Module.setStatus.last.text = text;
if (m) {
text = m[1];
}
},
totalDependencies: 0,
monitorRunDependencies: function(left) {
this.totalDependencies = Math.max(this.totalDependencies, left);
Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
}
};
Module.setStatus('Downloading...');
window.onerror = function(event) {
// TODO: do not warn on ok events like simulating an infinite loop or exitStatus
Module.setStatus('Exception thrown, see JavaScript console');
Module.setStatus = function(text) {
if (text) Module.printErr('[post-exception status] ' + text);
};
};
}

0 comments on commit 0db548a

Please sign in to comment.