Skip to content

Commit a2c5296

Browse files
committed
put hooks stuff in own file
1 parent 6386dac commit a2c5296

2 files changed

Lines changed: 74 additions & 68 deletions

File tree

‎v3/js/hooks.js‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* API for adding a hook.
2+
3+
An external user should call
4+
add_pytutor_hook("hook_name_here", function(args) {...})
5+
args will be a javascript object with several named properties;
6+
this is meant to be similar to Python's keyword arguments.
7+
8+
The hooked function should return an array whose first element is a boolean:
9+
true if it completely handled the situation (no further hooks
10+
nor the base function should be called); false otherwise (wasn't handled).
11+
If the hook semantically represents a function that returns something,
12+
the second value of the returned array is that semantic return value.
13+
14+
E.g. for the Java visualizer a simplified version of a hook we use is:
15+
16+
add_pytutor_hook(
17+
"isPrimitiveType",
18+
function(args) {
19+
var obj = args.obj; // unpack
20+
if (obj instanceof Array && obj[0] == "CHAR-LITERAL")
21+
return [true, true]; // yes we handled it, yes it's primitive
22+
return [false]; // didn't handle it, let someone else
23+
});
24+
25+
Hook callbacks can return false or undefined (i.e. no return
26+
value) in lieu of [false].
27+
28+
NB: If multiple functions are added to a hook, the oldest goes first.
29+
*/
30+
31+
var add_pytutor_hook = function(hook_name, func) {
32+
if (pytutor_hooks[hook_name])
33+
pytutor_hooks[hook_name].push();
34+
else
35+
pytutor_hooks[hook_name] = [func];
36+
}
37+
38+
// this is global in order to reach static functions like isPrimitiveType
39+
var pytutor_hooks = {}; // keys, hook names; values, list of functions
40+
41+
/*
42+
try_hook(hook_name, args): how the internal codebase invokes a hook.
43+
args will be a javascript object with several named properties;
44+
this is meant to be similar to Python's keyword arguments.
45+
E.g.,
46+
47+
function isPrimitiveType(obj) {
48+
var hook_result = try_hook("isPrimitiveType", {obj:obj});
49+
if (hook_result[0]) return hook_result[1];
50+
// go on as normal if the hook didn't handle it
51+
52+
Although add_pytutor_hook allows the hooked function to
53+
return false or undefined, try_hook will always return
54+
something with the strict format [false], [true] or [true, ...].
55+
*/
56+
57+
var try_hook = function(hook_name, args) {
58+
if (pytutor_hooks[hook_name]) {
59+
for (var i=0; i<pytutor_hooks[hook_name].length; i++) {
60+
61+
// apply w/o "this", and pack sole arg into array as required by apply
62+
var handled_and_result
63+
= pytutor_hooks[hook_name][i].apply(null, [args]);
64+
65+
if (handled_and_result && handled_and_result[0])
66+
return handled_and_result;
67+
}
68+
}
69+
return [false];
70+
}
71+

‎v3/js/pytutor.js‎

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -63,75 +63,10 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6363
6464
*/
6565

66-
/* API for adding a hook.
67-
68-
An external user should call
69-
add_pytutor_hook("hook_name_here", function(args) {...})
70-
args will be a javascript object with several named properties;
71-
this is meant to be similar to Python's keyword arguments.
72-
73-
The hooked function should return an array whose first element is a boolean:
74-
true if it completely handled the situation (no further hooks
75-
nor the base function should be called); false otherwise (wasn't handled).
76-
If the hook semantically represents a function that returns something,
77-
the second value of the returned array is that semantic return value.
78-
79-
E.g. for the Java visualizer a simplified version of a hook we use is:
80-
81-
add_pytutor_hook(
82-
"isPrimitiveType",
83-
function(args) {
84-
var obj = args.obj; // unpack
85-
if (obj instanceof Array && obj[0] == "CHAR-LITERAL")
86-
return [true, true]; // yes we handled it, yes it's primitive
87-
return [false]; // didn't handle it, let someone else
88-
});
89-
90-
Hook callbacks can return false or undefined (i.e. no return
91-
value) in lieu of [false].
92-
93-
NB: If multiple functions are added to a hook, the oldest goes first.
94-
*/
95-
96-
var add_pytutor_hook = function(hook_name, func) {
97-
if (pytutor_hooks[hook_name])
98-
pytutor_hooks[hook_name].push();
99-
else
100-
pytutor_hooks[hook_name] = [func];
101-
}
102-
103-
// this is global in order to reach static functions like isPrimitiveType
104-
var pytutor_hooks = {}; // keys, hook names; values, list of functions
105-
106-
/*
107-
try_hook(hook_name, args): how the internal codebase invokes a hook.
108-
args will be a javascript object with several named properties;
109-
this is meant to be similar to Python's keyword arguments.
110-
E.g.,
111-
112-
function isPrimitiveType(obj) {
113-
var hook_result = try_hook("isPrimitiveType", {obj:obj});
114-
if (hook_result[0]) return hook_result[1];
115-
// go on as normal if the hook didn't handle it
116-
117-
Although add_pytutor_hook allows the hooked function to
118-
return false or undefined, try_hook will always return
119-
something with the strict format [false], [true] or [true, ...].
120-
*/
121-
66+
// include hooks.js after you include pytutor.js, if you want to use hooks
67+
// see hooks.js for more instructions.
12268
var try_hook = function(hook_name, args) {
123-
if (pytutor_hooks[hook_name]) {
124-
for (var i=0; i<pytutor_hooks[hook_name].length; i++) {
125-
126-
// apply w/o "this", and pack sole arg into array as required by apply
127-
var handled_and_result
128-
= pytutor_hooks[hook_name][i].apply(null, [args]);
129-
130-
if (handled_and_result && handled_and_result[0])
131-
return handled_and_result;
132-
}
133-
}
134-
return [false];
69+
return [false]; // just a stub
13570
}
13671

13772
var SVG_ARROW_POLYGON = '0,3 12,3 12,0 18,5 12,10 12,7 0,7';

0 commit comments

Comments
 (0)