robertkrimen / otto

A JavaScript interpreter in Go (golang)
http://godoc.org/github.com/robertkrimen/otto
MIT License
8.01k stars 584 forks source link

Parser don't allow leading 0 #487

Open sinux-l5d opened 1 year ago

sinux-l5d commented 1 year ago

Hi, I'm trying to parse a js file from a website, but the parser fails where my browser is fine. The relevant js line is: "AddGenWeeks(22,08,2022,1,52,form.elements["weeks"]);"

package main

import (
    "github.com/robertkrimen/otto/parser"
)

func main() {
    _, err := parser.ParseFile(nil, "", "AddGenWeeks(22,8,2022,1,52,form.elements['weeks']);", 0) // works
    if err != nil {
        panic(err)
    }
    _, err = parser.ParseFile(nil, "", "AddGenWeeks(22,08,2022,1,52,form.elements['weeks']);", 0) // don't works
    if err != nil {
        panic(err)
    }
}

Is it expected result? Is there any workaround?

Asday commented 1 year ago

https://262.ecma-international.org/5.1/#sec-7.8.3 leading zeroes are not permitted under es5. Your browser's javascript engine has been running es6 for a very long time, which otto does not support, as stated in the readme.

sinux-l5d commented 1 year ago

Okey thanks, never actually coded for es5, so didn't know about this. I guess I'll have to find another way.

Asday commented 1 year ago

Consider transpiling your ES6 to ES5 to run it under otto.

stevenh commented 1 year ago

@Asday is correct that's not a ES5 feature, but I'm happy to accept PR's which push us towards ES6.

Asday commented 1 year ago

This is probably the incorrect location for this discussion, but I was wondering about that, @stevenh.

Do you suppose that ES6 support should be something separated from the ES5 support by some measure? Perhaps, on an es6 branch, until some measure of feature completeness is achieved?

If you were to accept PRs that implement little features here and there, especially if they're simple low-hanging fruit such as math functions and leading zeroes in numeric literals, I could imagine new users would start using otto, use some ES6 features, think that they all work, and then become frustrated when shiny-new-library.js doesn't work in hard to debug ways.

They already don't read that bit in the readme that says "ES5 only", so I don't think making it a more complex "ES5 only except this, this, this, this, and this" would help either.

stevenh commented 1 year ago

Before answering that are you aware of any backwards incompatible changes in ES6?

Asday commented 1 year ago

Not one.

stevenh commented 1 year ago

In that case it sounds like we can make gradual progress towards ES6 without the need for a separate branch which would increase the overhead. We'll just need to create a way of tracking, so it's clear what has been done.

Obviously encouraging people to contribute enhancements / bug fixes to address issues as they occur will be key to that.