sweet-js/sweet-core

Unable to compile operator

Jamesernator opened this issue · 1 comments

Using the latest version (3.0.10) this results in an error:

Number.prototype[Symbol.add] = new Function('other', 'return this + other')
Symbol.add = Symbol('add')

operator + left 13 = (left, right) => {
    return #`(${ left })[Symbol.add](${ right })`
}

class Point {
    constructor(x, y) {
        this.x = x
        this.y = y
    }

    [Symbol.add](other) {
        return new Point(this.x + other.x, this.y + other.y)
    }
}

const x = new Point(3,4) + new Point(5,6)
Error message Error: Unknown object: {"value":{"token":{"typeCode":3,"type":{"klass":{"name":"Numeric"},"name":""},"value":3,"slice":{"text":"3","start":397,"startLocation":{"line":20,"column":21,"filename":"","position":397},"end":398},"octal":false,"noctal":false},"bindings":{"_map":{}},"scopesets":{"all":[{"name":"outsideEdge_1"}],"phase":{"0":[{"name":"insideEdge0_2"}]}}},"type":"RawSyntax","loc":null}

I'm not sure what the minimal cause of the bug is, but I did find that if I do:

const x = (new Point(3,4)) + (new Point(5,6))

// Or something like
function Point(x, y) {
    return {
        x, y,
        [Symbol.add](other) {
            return Point(this.x + other.x, this.y + other.y)
        }
    }
}

const x = Point(3, 4) + Point(4,5)

Then it's happy.

So it's probably some interaction between operator overloading and new.

I just ran into a potentially similar problem with new:

let f = x => new C(x)

throws an error but

let f = x => { return new C(x); }

does not. Might be unrelated but I suspect it has something to do with how we parse new.