Skip to content

Commit 7f8cefa

Browse files
committed
adapted David's hooks API directly into pytutor.js
1 parent 4ba183a commit 7f8cefa

3 files changed

Lines changed: 84 additions & 6 deletions

File tree

‎v3/js/hooks.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
/* API for adding a hook, created by David Pritchard
2+
https://github.com/daveagp
3+
4+
[THIS FILE IS DEPRECATED SINCE PHILIP HAS ADAPTED THE HOOKS API DIRECTLY
5+
INTO PYTUTOR.JS]
26
37
An external user should call
48
add_pytutor_hook("hook_name_here", function(args) {...})
@@ -30,7 +34,7 @@ add_pytutor_hook(
3034

3135
var add_pytutor_hook = function(hook_name, func) {
3236
if (pytutor_hooks[hook_name])
33-
pytutor_hooks[hook_name].push();
37+
pytutor_hooks[hook_name].push(func);
3438
else
3539
pytutor_hooks[hook_name] = [func];
3640
}

‎v3/js/opt-frontend-common.js‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -609,11 +609,6 @@ function populateTogetherJsShareUrl() {
609609
// END - shared session stuff
610610

611611

612-
// Global hook for ExecutionVisualizer.
613-
var try_hook = function(hook_name, args) {
614-
return [false]; // just a stub
615-
}
616-
617612
var myVisualizer = null; // singleton ExecutionVisualizer instance
618613

619614
var rawInputLst = []; // a list of strings inputted by the user in response to raw_input or mouse_input events

‎v3/js/pytutor.js‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ function ExecutionVisualizer(domRootID, dat, params) {
249249

250250
this.classAttrsHidden = {}; // kludgy hack for 'show/hide attributes' for class objects
251251

252+
// API for adding a hook, created by David Pritchard
253+
this.pytutor_hooks = {}; // keys, hook names; values, list of functions
254+
252255
try_hook("end_constructor", {myViz:this});
253256

254257
this.hasRendered = false;
@@ -257,6 +260,82 @@ function ExecutionVisualizer(domRootID, dat, params) {
257260

258261
}
259262

263+
264+
/* API for adding a hook, created by David Pritchard
265+
https://github.com/daveagp
266+
267+
[this documentation is a bit deprecated since Philip made try_hook a
268+
method of ExecutionVisualizer, but the general ideas remains]
269+
270+
An external user should call
271+
add_pytutor_hook("hook_name_here", function(args) {...})
272+
args will be a javascript object with several named properties;
273+
this is meant to be similar to Python's keyword arguments.
274+
275+
The hooked function should return an array whose first element is a boolean:
276+
true if it completely handled the situation (no further hooks
277+
nor the base function should be called); false otherwise (wasn't handled).
278+
If the hook semantically represents a function that returns something,
279+
the second value of the returned array is that semantic return value.
280+
281+
E.g. for the Java visualizer a simplified version of a hook we use is:
282+
283+
add_pytutor_hook(
284+
"isPrimitiveType",
285+
function(args) {
286+
var obj = args.obj; // unpack
287+
if (obj instanceof Array && obj[0] == "CHAR-LITERAL")
288+
return [true, true]; // yes we handled it, yes it's primitive
289+
return [false]; // didn't handle it, let someone else
290+
});
291+
292+
Hook callbacks can return false or undefined (i.e. no return
293+
value) in lieu of [false].
294+
295+
NB: If multiple functions are added to a hook, the oldest goes first.
296+
*/
297+
ExecutionVisualizer.prototype.add_pytutor_hook = function(hook_name, func) {
298+
if (this.pytutor_hooks[hook_name])
299+
this.pytutor_hooks[hook_name].push(func);
300+
else
301+
this.pytutor_hooks[hook_name] = [func];
302+
}
303+
304+
/*
305+
306+
[this documentation is a bit deprecated since Philip made try_hook a
307+
method of ExecutionVisualizer, but the general ideas remains]
308+
309+
try_hook(hook_name, args): how the internal codebase invokes a hook.
310+
args will be a javascript object with several named properties;
311+
this is meant to be similar to Python's keyword arguments.
312+
E.g.,
313+
314+
function isPrimitiveType(obj) {
315+
var hook_result = try_hook("isPrimitiveType", {obj:obj});
316+
if (hook_result[0]) return hook_result[1];
317+
// go on as normal if the hook didn't handle it
318+
319+
Although add_pytutor_hook allows the hooked function to
320+
return false or undefined, try_hook will always return
321+
something with the strict format [false], [true] or [true, ...].
322+
*/
323+
ExecutionVisualizer.prototype.try_hook = function(hook_name, args) {
324+
if (this.pytutor_hooks[hook_name]) {
325+
for (var i=0; i<this.pytutor_hooks[hook_name].length; i++) {
326+
327+
// apply w/o "this", and pack sole arg into array as required by apply
328+
var handled_and_result
329+
= this.pytutor_hooks[hook_name][i].apply(null, [args]);
330+
331+
if (handled_and_result && handled_and_result[0])
332+
return handled_and_result;
333+
}
334+
}
335+
return [false];
336+
}
337+
338+
260339
// for managing state related to pesky jsPlumb connectors, need to reset
261340
// before every call to renderDataStructures, or else all hell breaks
262341
// loose. yeah, this is kludgy and stateful, but at least all of the

0 commit comments

Comments
 (0)