Skip to content

Getting JavaScript TypeError on None-value parameter check #588

@phorward

Description

@phorward

Hi!

Given the following piece of code

def domCreateElement(tag, ns=None):
	"""
		Creates a new HTML/SVG/... tag
		  :param ns: the namespace. Default: HTML. Possible values: HTML, SVG, XBL, XUL
    """
	uri = None

	if ns == "SVG":
		uri = "http://www.w3.org/2000/svg"
	elif ns == "XBL":
		uri = "http://www.mozilla.org/xbl"
	elif ns == "XUL":
		uri = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"

	if uri:
		return document.createElement(uri, tag)

	return document.createElement(tag)

I'm getting this error in JavaScript with the latest Transcrypt 3.7.11, but also before:

 [Show/hide message details.] TypeError: right-hand side of 'in' should be an object, got null[Learn More] org.transcrypt.__runtime__.js:1541:22
__eq__
http://localhost:8000/__target__/org.transcrypt.__runtime__.js:1541:22
domCreateElement
http://localhost:8000/__target__/html5.core.js:74:26
__call__
http://localhost:8000/__target__/org.transcrypt.__runtime__.js:166:29
get __init__/<
http://localhost:8000/__target__/html5.core.js:601:32
__call__
http://localhost:8000/__target__/org.transcrypt.__runtime__.js:166:29
get __init__/<
http://localhost:8000/__target__/html5.core.js:5563:16
__call__
http://localhost:8000/__target__/org.transcrypt.__runtime__.js:166:29
get __init__/<
http://localhost:8000/__target__/game.js:23:16
__new__
http://localhost:8000/__target__/org.transcrypt.__runtime__.js:149:22
cls
http://localhost:8000/__target__/org.transcrypt.__runtime__.js:92:33
__call__
http://localhost:8000/__target__/org.transcrypt.__runtime__.js:166:29
<anonymous>
http://localhost:8000/__target__/game.js:98:67
<anonymous>
http://localhost:8000/__target__/game.js:93:16
InnerModuleEvaluation self-hosted:4355:5 InnerModuleEvaluation self-hosted:4342:17 evaluation self-hosted:4301:9 

The reason for this is, that the if ns == "SVG": is causing the code

if (__t__ (__eq__ (ns, 'SVG'))) {
/* 000049 */ 		var uri = 'http://www.w3.org/2000/svg';
/* 000049 */ 	}

but the variable ns is null. I don't know why, but both Firefox and Chromium interpret ns is of type object, so the first part of the function __eq__ is satisfied, but the second part fails. I've modified __eq__ to read the following, to get more information about a before the error occurs:

export function __eq__ (a, b) {
    console.log(a);
    console.log(typeof a);
    console.log(typeof a == 'object');

    if (typeof a == 'object' && '__eq__' in a) {
        return a.__eq__ (b);
    }
    else {
        return a == b;
    }
};

Output is:

null org.transcrypt.__runtime__.js:1541:18
object org.transcrypt.__runtime__.js:1542:18
true org.transcrypt.__runtime__.js:1543:18

so a is null but also an object?

To resolves the issue, the function like __eq__ must look like the following, but this change to the condition may also be added to the following functions like __neq__ etc.

export function __eq__ (a, b) {
    if (a !== null && typeof a == 'object' && '__eq__' in a) {
        return a.__eq__ (b);
    }
    else {
        return a == b;
    }
};

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions