Commit 7d19fcd6 authored by Michael Droettboom's avatar Michael Droettboom

Fix bugs found from examples

parent 6153e635
......@@ -54,7 +54,7 @@ EM_JS(int, hiwire_string_utf8, (int ptr), {
});
EM_JS(int, hiwire_bytes, (int ptr, int len), {
var bytes = new Uint8Array(Module.HEAPU8.buffer, ptr, len);
var bytes = new Uint8ClampedArray(Module.HEAPU8.buffer, ptr, len);
return Module.hiwire_new_value(bytes);
});
......@@ -140,9 +140,13 @@ EM_JS(void, hiwire_call_member, (int idobj, int ptrname, int idargs), {
});
EM_JS(void, hiwire_new, (int idobj, int idargs), {
function newCall(Cls) {
return new (Function.prototype.bind.apply(Cls, arguments));
}
var jsobj = Module.hiwire_get_value(idobj);
var jsargs = Module.hiwire_get_value(idargs);
return Module.hiwire_new_value(new (Function.prototype.bind.apply(jsobj, jsargs)));
jsargs.unshift(jsobj);
return Module.hiwire_new_value(newCall.apply(newCall, jsargs));
});
EM_JS(void, hiwire_get_length, (int idobj), {
......
......@@ -79,7 +79,7 @@ static PyObject* JsProxy_Call(PyObject *o, PyObject *args, PyObject *kwargs) {
int idargs = hiwire_array();
for (Py_ssize_t i; i < nargs; ++i) {
for (Py_ssize_t i = 0; i < nargs; ++i) {
int idarg = python2js(PyTuple_GET_ITEM(args, i));
hiwire_push_array(idargs, idarg);
hiwire_decref(idarg);
......@@ -99,7 +99,7 @@ static PyObject* JsProxy_New(PyObject *o, PyObject *args, PyObject *kwargs) {
int idargs = hiwire_array();
for (Py_ssize_t i; i < nargs; ++i) {
for (Py_ssize_t i = 0; i < nargs; ++i) {
int idarg = python2js(PyTuple_GET_ITEM(args, i));
hiwire_push_array(idargs, idarg);
hiwire_decref(idarg);
......
......@@ -130,7 +130,7 @@ var languagePluginLoader = new Promise((resolve, reject) => {
let div = document.createElement('div');
div.className = 'rendered_html';
var element;
if ('_repr_html_' in val) {
if (val._repr_html_ !== undefined) {
let result = val._repr_html_();
if (typeof result === 'string') {
div.appendChild(new DOMParser().parseFromString(
......
......@@ -118,9 +118,7 @@ For example, say we had the following Python function that we wanted to call fro
%% code {"language":"py"}
# python
def square(x, integer=False):
if integer:
x = int(x)
def square(x):
return x * x
%% md
......@@ -129,14 +127,14 @@ Since calling conventions are a bit different in Python than in Javascript, all
%% js
// javascript
var square = pyodide.pyimport("square")
square([2.5], {integer: true})
square(2.5)
%% md
This is equivalent to the following Python syntax:
%% code {"language":"py"}
# python
square(2.5, integer=True)
square(2.5)
%% md
You can also get the attributes of objects in a similar way. Say we had an instance of the following Python custom class:
......
......@@ -135,7 +135,6 @@ class FigureCanvasWasm(backend_agg.FigureCanvasAgg):
rubberband.setAttribute('tabindex', '0')
# Event handlers are added to the canvas "on top", even though most of the
# activity happens in the canvas below.
rubberband.addEventListener('click', self.onclick)
rubberband.addEventListener('mousemove', self.onmousemove)
rubberband.addEventListener('mouseup', self.onmouseup)
rubberband.addEventListener('mousedown', self.onmousedown)
......@@ -170,9 +169,9 @@ class FigureCanvasWasm(backend_agg.FigureCanvasAgg):
canvas = self.get_element('canvas')
image_data = ImageData.new(
self.buffer_rgba(),
width, height);
ctx = canvas.getContext("2d");
ctx.putImageData(image_data, 0, 0);
width, height)
ctx = canvas.getContext("2d")
ctx.putImageData(image_data, 0, 0)
self._idle_scheduled = False
def draw_idle(self):
......@@ -198,10 +197,6 @@ class FigureCanvasWasm(backend_agg.FigureCanvasAgg):
button = 3
return x, y, button
def onclick(self, event):
x, y, button = self._convert_mouse_event(event)
self.button_click_event(x, y, button, guiEvent=event)
def onmousemove(self, event):
x, y, button = self._convert_mouse_event(event)
self.motion_notify_event(x, y, guiEvent=event)
......@@ -215,7 +210,7 @@ class FigureCanvasWasm(backend_agg.FigureCanvasAgg):
self.button_press_event(x, y, button, guiEvent=event)
def onmouseenter(self, event):
window.addEventListener('contextmenu', ignore)
return
# When the mouse is over the figure, get keyboard focus
self.get_element('rubberband').focus()
self.enter_notify_event(guiEvent=event)
......
......@@ -141,6 +141,14 @@ def test_jsproxy(selenium):
"document.body.children[0].tagName") == 'DIV'
assert selenium.run(
"repr(document)") == '[object HTMLDocument]'
selenium.run_js(
"window.square = function (x) { return x*x; }")
assert selenium.run(
"from js import square\n"
"square(2)") == 4
assert selenium.run(
"from js import ImageData\n"
"ImageData.new(64, 64)")
def test_open_url(selenium):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment