question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception creating string representation of javascript Object if constructor is undefined

See original GitHub issue

🐛 Bug

I’ve been trying to prove out building a simple VueJS app with PyScript and running into strange errors of the form:

JsException(TypeError: Cannot read properties of undefined (reading 'name')) TypeError: Cannot read properties of undefined (reading 'name')

After much debugging, I see at least one of the errors is happening when pyodide attempts to create a string representation of a javascript object when the object has an undefined constructor. Using an object like this seems to be common in Vue (the ‘publicThis’ variable used for method binding is of this form).

To Reproduce

debug.html

<!DOCTYPE html>
<html>
  <head>
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
  </head>
  <body>
<script>
  obj = Object();
  obj.constructor = undefined;
</script>
    <py-script src="./debug.py"></py-script>
    <py-repl></py-repl>
  </body>
</html>

debug.py

import pyodide
import js

try:
    str(js.obj)
except Exception as err:
    console.log(f"{err!r}", err)

javascript console

pyodide.asm.js:14 JsException(TypeError: Cannot read properties of undefined (reading 'name')) TypeError: Cannot read properties of undefined (reading 'name')
    at hiwire_is_typedarray (pyodide.asm.js:14:227151)
    at pyodide.asm.wasm:0xee398
    at pyodide.asm.wasm:0xeea6e
    at js2python (pyodide.asm.js:14:230955)
    at pyodide.asm.wasm:0xefd61
    at pyodide.asm.wasm:0x178583
    at pyodide.asm.wasm:0x2005f9
    at pyodide.asm.wasm:0x1f98b5
    at pyodide.asm.wasm:0x1f97a0
    at pyodide.asm.wasm:0x1f6ad9

Expected behavior

To produce a string representation with no exceptions thrown.

Environment

  • Pyodide Version: 0.20.0
  • Browser version: Brave 1.40.113 Chromium 103.0.5060.114 (macOS 12.4 Monterey)
  • Any other relevant information:

Related Issue

Somewhat unrelated; the pyodide.create_proxy() object does not have a javascript .bind() method, and otherwise does not conform to the javascript function api that VueJS is expecting. I had to wrap my pyodide proxy objects in a javascript function to get it to work at all.

This may be fixed (https://github.com/pyodide/pyodide/pull/2769), although a bind that is a no-op would not provide any access to the bound javascript ‘this’ object if the python function needs access to that to perform its job (and I think that a Vue component method does need such access to function).

jsfn_wrap = js.Function("proxy_fn", """\
function wrapper() {
   // wrap this in an Object to avoid pyodide string-ification bug
   return proxy_fn.apply({this: this}, arguments);
}
return wrapper;
""")

def function_proxy(py_func):
    proxy = pyodide.create_proxy(py_func)
    jsfn = jsfn_wrap(proxy)
    return jsfn

Additional context

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
hoodmanecommented, Aug 4, 2022

I believe the primary issue reported here is fixed on master:

>>> from pyodide.code import run_js
>>> o = run_js("Object.create(null)")
>>> str(o)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: Pyodide cannot generate a repr for this Javascript object because it has no 'toString' method

I think this is the best we can do because o.toString() itself throws an error.

0reactions
hoodmanecommented, Aug 24, 2022

It would be good to close this issue and create two new ones:

  1. improve the repr for JsProxy
  2. what to do about bind and this and libraries that rely on them?
Read more comments on GitHub >

github_iconTop Results From Across the Web

Constructor is Undefined issues - java - Stack Overflow
I'm currently writing a program that should read through a text file, create an array of different types of ...
Read more >
Object.prototype.constructor - JavaScript - MDN Web Docs
The constructor data property of an Object instance returns a reference to the constructor function that created the instance object.
Read more >
7 Tips to Handle undefined in JavaScript - Dmitri Pavlutin
A detailed article about 'undefined' keyword in JavaScript. 7 tips on how to handle correctly 'undefined' and increase code durability.
Read more >
Errors | Node.js v19.3.0 Documentation
All JavaScript errors are handled as exceptions that immediately generate and throw ... The string representing the stack trace is lazily generated when...
Read more >
JavaScript Object - w3resource
"Everything" in JavaScript acts like an object such as a String, a Number, an Array, a Function..., with two exceptions, null and undefined....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found