Skip to content

Undefined values in objects cause ReferenceErrors through various operations #1916

@KevOrr

Description

@KevOrr

Related: #1691

Investigating this a bit more, @djeis97 had an idea that this might be a bigger problem. Indeed, the following expression incorrectly raises a ReferenceError:

[undefined][0]

This is because [undefined] is a temporary value, so it has no references. jspeFactorFunctionCall correctly evaluates this expression to a JsVar NAME whose varData is the integer 0, and whose firstChild is undefined ([r1,l1] Name Integer 0 undefined). But at the end of that function, it unlocks the parent value, which in this case is the array [undefined].

When jsvUnLock unlocks this array, the array's lock count is 0, so its ref count becomes 0, so it is freed. This in turn decrements our name var's refcount to 0 ([r0,l1] Name Integer 0 undefined).

Later, jspParse calls jsvCheckReferenceError, which sees [r0,l1] Name Integer 0 undefined and treats it as a ReferenceError.

Fundamentally, this seems to be an issue because [r0,l_] Name _ undefined are used both for indicating a ReferenceError, and for representing the reference obtained by indexing into an object with no references itself.

Other examples of this error:

x = [undefined]
x.pop()
({a: undefined})['a']

cc @kiranshila

Metadata

Metadata

Assignees

No one assigned

    Labels

    Standards IssueA place where Espruino doesn't conform to the ECMAscript standardhigh priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions