Skip to content

Commit

Permalink
Fix examples involving script for the JS browser in chapter 9+ (#1346)
Browse files Browse the repository at this point in the history
* Fix examples involving script for the JS browser in chapter 9+

* none
  • Loading branch information
chrishtr authored Sep 20, 2024
1 parent d5ace72 commit fa263b0
Show file tree
Hide file tree
Showing 12 changed files with 13 additions and 35 deletions.
3 changes: 1 addition & 2 deletions src/lab12.hints
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@
{"code": "callback", "js": "callback"},
{"code": "self.file.flush()", "js": "self.file.flush()"},
{"code": "self.node_to_handle = {}", "js": " this.node_to_handle = new Map();"},
{"code": "DEFAULT_STYLE_SHEET.copy()", "js": "constants.DEFAULT_STYLE_SHEET.slice()"},
{"code": "open('runtime12.js').read()", "js": "filesystem.open('runtime12.js').read()"}
{"code": "DEFAULT_STYLE_SHEET.copy()", "js": "constants.DEFAULT_STYLE_SHEET.slice()"}
]
8 changes: 1 addition & 7 deletions src/lab13.hints
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
{"code": "b'Browser'", "js": "'Browser'"},
{"code": "MeasureTime()", "js": "new MeasureTime()"},
{"code": "print", "js": "console.log"},
{"code": "elt not in self.node_to_handle", "type": "map"},
{"code": "self.node_to_handle = {}", "js": " this.node_to_handle = new Map();"},
{"code": "self.node_to_handle.get(elt, -1)", "js": "this.node_to_handle.get(elt) ?? -1"},
{"code": "self.node_to_handle[elt]", "js": "this.node_to_handle.get(elt)"},
{"code": "len(self.node_to_handle)", "js": "this.node_to_handle.size"},
{"code": "self.node_to_handle[elt] = handle", "js": " this.node_to_handle.set(elt, handle);"},
{"code": "('OpenGL initialized: vendor={},' + 'renderer={}').format(OpenGL.GL.glGetString(OpenGL.GL.GL_VENDOR), OpenGL.GL.glGetString(OpenGL.GL.GL_RENDERER))", "js": ""},
{"code": "self.s[self.i] not in chars", "type": "str"},
{"code": "property not in old_style", "type": "dict"},
Expand All @@ -17,6 +12,5 @@
{"code": "node not in self.composited_updates", "type": "list"},
{"code": "new_parent in new_effects", "type": "dict"},
{"code": "rect.outset(1, 1)", "js": "rect.outset(1, 1)"},
{"code": "sys.exit()", "js": ""},
{"code": "open('runtime13.js').read()", "js": "filesystem.open('runtime13.js').read()"}
{"code": "sys.exit()", "js": ""}
]
15 changes: 0 additions & 15 deletions src/lab13.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,21 +473,6 @@ def __init__(self, tab):
self.node_to_handle = {}
self.handle_to_node = {}

def dispatch_event(self, type, elt):
handle = self.node_to_handle.get(elt, -1)
do_default = self.interp.evaljs(
EVENT_DISPATCH_JS, type=type, handle=handle)
return not do_default

def get_handle(self, elt):
if elt not in self.node_to_handle:
handle = len(self.node_to_handle)
self.node_to_handle[elt] = handle
self.handle_to_node[handle] = elt
else:
handle = self.node_to_handle[elt]
return handle

def style_set(self, handle, s):
elt = self.handle_to_node[handle]
elt.attributes["style"] = s;
Expand Down
3 changes: 1 addition & 2 deletions src/lab9.hints
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
{"code": "self.node_to_handle[elt]", "js": "this.node_to_handle.get(elt)"},
{"code": "len(self.node_to_handle)", "js": "this.node_to_handle.size"},
{"code": "self.node_to_handle[elt] = handle", "js": " this.node_to_handle.set(elt, handle);"},
{"code": "DEFAULT_STYLE_SHEET.copy()", "js": "constants.DEFAULT_STYLE_SHEET.slice()"},
{"code": "open('runtime9.js').read()", "js": "filesystem.open('runtime9.js').read()"}
{"code": "DEFAULT_STYLE_SHEET.copy()", "js": "constants.DEFAULT_STYLE_SHEET.slice()"}
]
3 changes: 1 addition & 2 deletions src/lab9.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ def __init__(self, tab):
self.interp.export_function("getAttribute",
self.getAttribute)
self.interp.export_function("innerHTML_set", self.innerHTML_set)
with open("runtime9.js") as f:
self.interp.evaljs(RUNTIME_JS)
self.interp.evaljs(RUNTIME_JS)

self.node_to_handle = {}
self.handle_to_node = {}
Expand Down
1 change: 1 addition & 0 deletions www/widgets/dukpy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ addEventListener("message", (e) => {
val = eval?.(e.data.body);
} catch (e) {
console.log('Script crashed');
console.log(e);
}
if (val instanceof Function || val instanceof Object) val = null;
$$POSTMESSAGE({"type": "return", "data": val});
Expand Down
2 changes: 1 addition & 1 deletion www/widgets/lab12-browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
await b.render();
interval = setInterval(async function() {
if (b.needs_animation_frame) {
await b.render();
b.needs_animation_frame = false;
await b.render();
}
await b.raster_and_draw();
await b.schedule_animation_frame();
Expand Down
2 changes: 1 addition & 1 deletion www/widgets/lab13-browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
await b.render();
interval = setInterval(async function() {
if (b.needs_animation_frame) {
await b.render();
b.needs_animation_frame = false;
await b.render();
}
await b.composite_raster_and_draw();
await b.schedule_animation_frame();
Expand Down
2 changes: 1 addition & 1 deletion www/widgets/lab14-browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
await b.render();
interval = setInterval(async function() {
if (b.needs_animation_frame) {
await b.render();
b.needs_animation_frame = false;
await b.render();
}
await b.composite_raster_and_draw();
await b.schedule_animation_frame();
Expand Down
2 changes: 1 addition & 1 deletion www/widgets/lab15-browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
await b.render();
interval = setInterval(async function() {
if (b.needs_animation_frame) {
await b.render();
b.needs_animation_frame = false;
await b.render();
}
await b.composite_raster_and_draw();
await b.schedule_animation_frame();
Expand Down
2 changes: 1 addition & 1 deletion www/widgets/lab16-browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
await b.render();
interval = setInterval(async function() {
if (b.needs_animation_frame) {
await b.render();
b.needs_animation_frame = false;
await b.render();
}
await b.composite_raster_and_draw();
await b.schedule_animation_frame();
Expand Down
5 changes: 3 additions & 2 deletions www/widgets/rt.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ class socket {
let [line1] = this.input.split("\r\n", 1);
let [method, path, protocol] = line1.split(" ");
this.url = this.scheme + "://" + this.host + path;
if (this.host == "localhost" && rt_constants.URLS["local://" + this.port]) {
if (this.host == "localhost" && rt_constants.URLS["local://" + this.port] &&
path.indexOf("example") == -1) {
let s = new socket({family: "inet", type: "stream", proto: "tcp"});
s.is_proxy_socket = true;
s.output = this.input;
Expand All @@ -129,7 +130,7 @@ class socket {
this.output = typeof response === "function" ? response() : response;
this.idx = 0;
this.closed = false;
} else if (this.host == "browser.engineering") {
} else if (this.host == "browser.engineering" || this.host == "localhost") {
let response = await fetch(path);
this.output = "HTTP/1.0 " + response.status + " " + response.statusText + "\r\n";
for (let [header, value] of response.headers.entries()) {
Expand Down

0 comments on commit fa263b0

Please sign in to comment.