@property causes errors due to incorrect generated code

Open
#407 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript, python
Domain
compilers

Research direction

Reproduce the MWE and inspect the generated props.js output alongside the get implementation shown in the issue. Trace the generated Foo property definitions and property access, then verify that both properties reference the correct implementation functions and return values rather than attempting to call them.

Written by the indexing model from the issue text.

Description

IS: exploratory IS: limitation

MWE:

class Foo:
	@property
	def bar(self):
		return 'Hello World'
	
	@property
	def name(self):
		return 'John Smith'

foo = Foo()

print(foo.bar) # Should print "Hello World"
print(foo.name) # Should print "John Smith"

The relevant Transcrypt output is:

var Foo = __class__ ('Foo', 'props', [object], {
	__impl__bar: function (self) {
		return 'Hello World';
	},
	get bar () {return __get__ (this, this.__impl__bar);},
	__impl__name: function (self) {
		return 'John Smith';
	},
	get py_name () {return __get__ (this, this.__impl__name);}
})
Object.defineProperty (Foo, '__impl__bar', property.call (Foo, Foo.Foo.__impl__bar));;
Object.defineProperty (Foo, '__impl__name', property.call (Foo, Foo.Foo.__implpy_name));;;

Note that __impl__bar is defined as Foo.__impl__bar, but is referenced in the defineProperty call as Foo.Foo.__impl__bar. This causes an error, as Foo.Foo is undefined:

.../props.js:2450
                Object.defineProperty (Foo, '__impl__bar', property.call (Foo, Foo.Foo.__impl__bar));;
                                                                                       ^

TypeError: Cannot read property '__impl__bar' of undefined

A simple workaround is to patch the output: perl -0777 -pi -e 's/property.call \((.*?), \g1.\g1.__impl__(.*?)\)/property.call ($1, $1.__impl__$2)/g' path/to/file.js

Note that for Foo.name, there is a slightly different problem as well, in that the function is defined as __impl__name, but is referenced as __implpy_name, causing a further error.

Again, a simple workaround suffices for now: perl -0777 -pi -e 's/property.call \((.*?), \g1.\g1.__implpy_(.*?)\)/property.call ($1, $1.__impl__$2)/g' path/to/file.js

Even after applying these patches however, @property is still not functional because e.g. foo.bar ends up being wrapped as a function that attempts to "call" foo.bar. This, of course, fails, because foo.bar is not a function.

The issue appears to be in the implementation __get__:

var __get__ = function (self, func, quotedFuncName) {
	if (self) {
		if (self.hasOwnProperty ('__class__') || typeof self == 'string' || self instanceof String) {           // Object before the dot
			if (quotedFuncName) {                                   // Memoize call since fcall is on, by installing bound function in instance
				// ...
				// The property is defined as a function instead of a value

Inserting a statement if(typeof(func) != "function"){return func;} at the very beginning of this function seems to fix the issue.

Dominant language
Python
Stars
2.9k
Forks
218
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from TranscryptOrg/Transcrypt

All issues in TranscryptOrg/Transcrypt

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.