From 716d07604cd312005f6b16956979b519935f6e0f Mon Sep 17 00:00:00 2001 From: Ivan <2103732+codebien@users.noreply.github.com> Date: Wed, 5 Jan 2022 17:40:33 +0100 Subject: [PATCH] goja: bump version It includes: * optional chaining * error wrapping for interrupts * source maps fixes --- go.mod | 4 +- go.sum | 4 ++ vendor/github.com/dop251/goja/ast/node.go | 8 +++ vendor/github.com/dop251/goja/compiler.go | 8 +++ .../github.com/dop251/goja/compiler_expr.go | 62 ++++++++++++++++++- .../github.com/dop251/goja/compiler_stmt.go | 8 ++- .../dop251/goja/parser/expression.go | 24 ++++++- vendor/github.com/dop251/goja/parser/lexer.go | 7 ++- .../dop251/goja/parser/statement.go | 1 + vendor/github.com/dop251/goja/runtime.go | 11 ++++ .../dop251/goja/token/token_const.go | 2 + vendor/github.com/dop251/goja/vm.go | 14 +++++ .../x/text/internal/language/language.go | 43 +++++++++++-- .../x/text/internal/language/parse.go | 7 +++ vendor/golang.org/x/text/language/parse.go | 22 +++++++ vendor/modules.txt | 4 +- 16 files changed, 217 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index e5d9eb8b848f..14a47af1f807 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/PuerkitoBio/goquery v1.6.1 github.com/Soontao/goHttpDigestClient v0.0.0-20170320082612-6d28bb1415c5 github.com/andybalholm/brotli v1.0.3 - github.com/dop251/goja v0.0.0-20211211112501-fb27c91c26ed + github.com/dop251/goja v0.0.0-20220104154337-b93494d0c5d9 github.com/fatih/color v1.12.0 github.com/golang/protobuf v1.4.3 github.com/gorilla/websocket v1.4.2 @@ -54,7 +54,7 @@ require ( github.com/onsi/gomega v1.10.1 // indirect github.com/tidwall/match v1.1.1 // indirect golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 // indirect - golang.org/x/text v0.3.7-0.20210503195748-5c7c50ebbd4f // indirect + golang.org/x/text v0.3.7 // indirect google.golang.org/genproto v0.0.0-20200903010400-9bfcb5116336 // indirect gopkg.in/guregu/null.v2 v2.1.2 // indirect ) diff --git a/go.sum b/go.sum index e4c1c848ef1d..d8c4f81db11b 100644 --- a/go.sum +++ b/go.sum @@ -59,6 +59,8 @@ github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91 h1:Izz0+t1Z5nI16 github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/dop251/goja v0.0.0-20211211112501-fb27c91c26ed h1:93Xt1lY7ReTv+7uDM6UHe818CEvLLn5HE1gpB7d3MS8= github.com/dop251/goja v0.0.0-20211211112501-fb27c91c26ed/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk= +github.com/dop251/goja v0.0.0-20220104154337-b93494d0c5d9 h1:QYh60R7QllMj/JKMtE2hCrwRqDbQq8vsUD63SDdVpHw= +github.com/dop251/goja v0.0.0-20220104154337-b93494d0c5d9/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk= github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -384,6 +386,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7-0.20210503195748-5c7c50ebbd4f h1:yQJrRE0hDxDFmZLlRaw+3vusO4fwNHgHIjUOMO7bHYI= golang.org/x/text v0.3.7-0.20210503195748-5c7c50ebbd4f/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac h1:7zkz7BUtwNFFqcowJ+RIgu2MaV/MapERkDIy+mwPyjs= diff --git a/vendor/github.com/dop251/goja/ast/node.go b/vendor/github.com/dop251/goja/ast/node.go index e9a5389a8a40..c0a1691983ba 100644 --- a/vendor/github.com/dop251/goja/ast/node.go +++ b/vendor/github.com/dop251/goja/ast/node.go @@ -118,6 +118,14 @@ type ( Identifier Identifier } + OptionalChain struct { + Expression + } + + Optional struct { + Expression + } + FunctionLiteral struct { Function file.Idx Name *Identifier diff --git a/vendor/github.com/dop251/goja/compiler.go b/vendor/github.com/dop251/goja/compiler.go index 12359ed84385..863310df529b 100644 --- a/vendor/github.com/dop251/goja/compiler.go +++ b/vendor/github.com/dop251/goja/compiler.go @@ -21,6 +21,7 @@ const ( blockWith blockScope blockIterScope + blockOptChain ) const ( @@ -384,6 +385,13 @@ func (p *Program) sourceOffset(pc int) int { return 0 } +func (p *Program) addSrcMap(srcPos int) { + if len(p.srcMap) > 0 && p.srcMap[len(p.srcMap)-1].srcPos == srcPos { + return + } + p.srcMap = append(p.srcMap, srcMapItem{pc: len(p.code), srcPos: srcPos}) +} + func (s *scope) lookupName(name unistring.String) (binding *binding, noDynamics bool) { noDynamics = true toStash := false diff --git a/vendor/github.com/dop251/goja/compiler_expr.go b/vendor/github.com/dop251/goja/compiler_expr.go index 5bfeb8dba8c3..85344d44195b 100644 --- a/vendor/github.com/dop251/goja/compiler_expr.go +++ b/vendor/github.com/dop251/goja/compiler_expr.go @@ -188,6 +188,16 @@ type compiledSpreadCallArgument struct { expr compiledExpr } +type compiledOptionalChain struct { + baseCompiledExpr + expr compiledExpr +} + +type compiledOptional struct { + baseCompiledExpr + expr compiledExpr +} + func (e *defaultDeleteExpr) emitGetter(putOnStack bool) { e.expr.emitGetter(false) if putOnStack { @@ -264,6 +274,18 @@ func (c *compiler) compileExpression(v ast.Expression) compiledExpr { return c.compileObjectAssignmentPattern(v) case *ast.ArrayPattern: return c.compileArrayAssignmentPattern(v) + case *ast.OptionalChain: + r := &compiledOptionalChain{ + expr: c.compileExpression(v.Expression), + } + r.init(c, v.Idx0()) + return r + case *ast.Optional: + r := &compiledOptional{ + expr: c.compileExpression(v.Expression), + } + r.init(c, v.Idx0()) + return r default: panic(fmt.Errorf("Unknown expression type: %T", v)) } @@ -300,7 +322,7 @@ func (e *baseCompiledExpr) emitUnary(func(), func(), bool, bool) { func (e *baseCompiledExpr) addSrcMap() { if e.offset >= 0 { - e.c.p.srcMap = append(e.c.p.srcMap, srcMapItem{pc: len(e.c.p.code), srcPos: e.offset}) + e.c.p.addSrcMap(e.offset) } } @@ -1948,6 +1970,12 @@ func (c *compiler) emitCallee(callee compiledExpr) (calleeName unistring.String) case *compiledIdentifierExpr: calleeName = callee.name callee.emitGetterAndCallee() + case *compiledOptionalChain: + c.startOptChain() + c.emitCallee(callee.expr) + c.endOptChain() + case *compiledOptional: + c.emitCallee(callee.expr) default: c.emit(loadUndef) callee.emitGetter(true) @@ -2362,3 +2390,35 @@ func (e *compiledSpreadCallArgument) emitGetter(putOnStack bool) { e.c.emit(pushSpread) } } + +func (c *compiler) startOptChain() { + c.block = &block{ + typ: blockOptChain, + outer: c.block, + } +} + +func (c *compiler) endOptChain() { + lbl := len(c.p.code) + for _, item := range c.block.breaks { + c.p.code[item] = jopt(lbl - item) + } + c.block = c.block.outer +} + +func (e *compiledOptionalChain) emitGetter(putOnStack bool) { + e.c.startOptChain() + e.expr.emitGetter(true) + e.c.endOptChain() + if !putOnStack { + e.c.emit(pop) + } +} + +func (e *compiledOptional) emitGetter(putOnStack bool) { + e.expr.emitGetter(putOnStack) + if putOnStack { + e.c.block.breaks = append(e.c.block.breaks, len(e.c.p.code)) + e.c.emit(nil) + } +} diff --git a/vendor/github.com/dop251/goja/compiler_stmt.go b/vendor/github.com/dop251/goja/compiler_stmt.go index 05d3b875ce76..e658997d7d4f 100644 --- a/vendor/github.com/dop251/goja/compiler_stmt.go +++ b/vendor/github.com/dop251/goja/compiler_stmt.go @@ -202,9 +202,13 @@ func (c *compiler) compileTryStatement(v *ast.TryStatement, needResult bool) { c.leaveBlock() } +func (c *compiler) addSrcMap(node ast.Node) { + c.p.addSrcMap(int(node.Idx0()) - 1) +} + func (c *compiler) compileThrowStatement(v *ast.ThrowStatement) { - //c.p.srcMap = append(c.p.srcMap, srcMapItem{pc: len(c.p.code), srcPos: int(v.Throw) - 1}) c.compileExpression(v.Argument).emitGetter(true) + c.addSrcMap(v) c.emit(throw) } @@ -752,6 +756,7 @@ func (c *compiler) emitVarAssign(name unistring.String, offset int, init compile if init != nil { c.emitVarRef(name, offset) c.emitNamed(init, name) + c.p.addSrcMap(offset) c.emit(initValueP) } } @@ -775,6 +780,7 @@ func (c *compiler) emitLexicalAssign(name unistring.String, offset int, init com } if init != nil { c.emitNamed(init, name) + c.p.addSrcMap(offset) } else { if isConst { c.throwSyntaxError(offset, "Missing initializer in const declaration") diff --git a/vendor/github.com/dop251/goja/parser/expression.go b/vendor/github.com/dop251/goja/parser/expression.go index 7491e25c3bea..568f89113a80 100644 --- a/vendor/github.com/dop251/goja/parser/expression.go +++ b/vendor/github.com/dop251/goja/parser/expression.go @@ -544,7 +544,8 @@ func (self *_parser) parseCallExpression(left ast.Expression) ast.Expression { } func (self *_parser) parseDotMember(left ast.Expression) ast.Expression { - period := self.expect(token.PERIOD) + period := self.idx + self.next() literal := self.parsedLiteral idx := self.idx @@ -653,11 +654,14 @@ func (self *_parser) parseLeftHandSideExpressionAllowCall() ast.Expression { }() var left ast.Expression + start := self.idx if self.token == token.NEW { left = self.parseNewExpression() } else { left = self.parsePrimaryExpression() } + + optionalChain := false L: for { switch self.token { @@ -668,12 +672,30 @@ L: case token.LEFT_PARENTHESIS: left = self.parseCallExpression(left) case token.BACKTICK: + if optionalChain { + self.error(self.idx, "Invalid template literal on optional chain") + self.nextStatement() + return &ast.BadExpression{From: start, To: self.idx} + } left = self.parseTaggedTemplateLiteral(left) + case token.QUESTION_DOT: + optionalChain = true + left = &ast.Optional{Expression: left} + + switch self.peek() { + case token.LEFT_BRACKET, token.LEFT_PARENTHESIS, token.BACKTICK: + self.next() + default: + left = self.parseDotMember(left) + } default: break L } } + if optionalChain { + left = &ast.OptionalChain{Expression: left} + } return left } diff --git a/vendor/github.com/dop251/goja/parser/lexer.go b/vendor/github.com/dop251/goja/parser/lexer.go index 37e1aacb2f2b..b7cfa1b3ca86 100644 --- a/vendor/github.com/dop251/goja/parser/lexer.go +++ b/vendor/github.com/dop251/goja/parser/lexer.go @@ -405,7 +405,12 @@ func (self *_parser) scan() (tkn token.Token, literal string, parsedLiteral unis case '~': tkn = token.BITWISE_NOT case '?': - tkn = token.QUESTION_MARK + if self.chr == '.' && !isDecimalDigit(self._peek()) { + self.read() + tkn = token.QUESTION_DOT + } else { + tkn = token.QUESTION_MARK + } case '"', '\'': insertSemicolon = true tkn = token.STRING diff --git a/vendor/github.com/dop251/goja/parser/statement.go b/vendor/github.com/dop251/goja/parser/statement.go index 580d0440645d..8c5320c59394 100644 --- a/vendor/github.com/dop251/goja/parser/statement.go +++ b/vendor/github.com/dop251/goja/parser/statement.go @@ -269,6 +269,7 @@ func (self *_parser) parseThrowStatement() ast.Statement { } node := &ast.ThrowStatement{ + Throw: idx, Argument: self.parseExpression(), } diff --git a/vendor/github.com/dop251/goja/runtime.go b/vendor/github.com/dop251/goja/runtime.go index 01edbb7c3b3e..914497240b2b 100644 --- a/vendor/github.com/dop251/goja/runtime.go +++ b/vendor/github.com/dop251/goja/runtime.go @@ -294,11 +294,22 @@ type uncatchableException struct { err error } +func (ue *uncatchableException) Unwrap() error { + return ue.err +} + type InterruptedError struct { Exception iface interface{} } +func (e *InterruptedError) Unwrap() error { + if err, ok := e.iface.(error); ok { + return err + } + return nil +} + type StackOverflowError struct { Exception } diff --git a/vendor/github.com/dop251/goja/token/token_const.go b/vendor/github.com/dop251/goja/token/token_const.go index f3b8a404ccd5..61e2f12f04c1 100644 --- a/vendor/github.com/dop251/goja/token/token_const.go +++ b/vendor/github.com/dop251/goja/token/token_const.go @@ -67,6 +67,7 @@ const ( SEMICOLON // ; COLON // : QUESTION_MARK // ? + QUESTION_DOT // ?. ARROW // => ELLIPSIS // ... BACKTICK // ` @@ -174,6 +175,7 @@ var token2string = [...]string{ SEMICOLON: ";", COLON: ":", QUESTION_MARK: "?", + QUESTION_DOT: "?.", ARROW: "=>", ELLIPSIS: "...", BACKTICK: "`", diff --git a/vendor/github.com/dop251/goja/vm.go b/vendor/github.com/dop251/goja/vm.go index cc987b1dbe68..46c755acef57 100644 --- a/vendor/github.com/dop251/goja/vm.go +++ b/vendor/github.com/dop251/goja/vm.go @@ -3340,6 +3340,20 @@ func (j jdefP) exec(vm *vm) { vm.sp-- } +type jopt int32 + +func (j jopt) exec(vm *vm) { + switch vm.stack[vm.sp-1] { + case _null: + vm.stack[vm.sp-1] = _undefined + fallthrough + case _undefined: + vm.pc += int(j) + default: + vm.pc++ + } +} + type _not struct{} var not _not diff --git a/vendor/golang.org/x/text/internal/language/language.go b/vendor/golang.org/x/text/internal/language/language.go index f41aedcfc8aa..6105bc7fadc1 100644 --- a/vendor/golang.org/x/text/internal/language/language.go +++ b/vendor/golang.org/x/text/internal/language/language.go @@ -251,6 +251,13 @@ func (t Tag) Parent() Tag { // ParseExtension parses s as an extension and returns it on success. func ParseExtension(s string) (ext string, err error) { + defer func() { + if recover() != nil { + ext = "" + err = ErrSyntax + } + }() + scan := makeScannerString(s) var end int if n := len(scan.token); n != 1 { @@ -461,7 +468,14 @@ func (t Tag) findTypeForKey(key string) (start, sep, end int, hasExt bool) { // ParseBase parses a 2- or 3-letter ISO 639 code. // It returns a ValueError if s is a well-formed but unknown language identifier // or another error if another error occurred. -func ParseBase(s string) (Language, error) { +func ParseBase(s string) (l Language, err error) { + defer func() { + if recover() != nil { + l = 0 + err = ErrSyntax + } + }() + if n := len(s); n < 2 || 3 < n { return 0, ErrSyntax } @@ -472,7 +486,14 @@ func ParseBase(s string) (Language, error) { // ParseScript parses a 4-letter ISO 15924 code. // It returns a ValueError if s is a well-formed but unknown script identifier // or another error if another error occurred. -func ParseScript(s string) (Script, error) { +func ParseScript(s string) (scr Script, err error) { + defer func() { + if recover() != nil { + scr = 0 + err = ErrSyntax + } + }() + if len(s) != 4 { return 0, ErrSyntax } @@ -489,7 +510,14 @@ func EncodeM49(r int) (Region, error) { // ParseRegion parses a 2- or 3-letter ISO 3166-1 or a UN M.49 code. // It returns a ValueError if s is a well-formed but unknown region identifier // or another error if another error occurred. -func ParseRegion(s string) (Region, error) { +func ParseRegion(s string) (r Region, err error) { + defer func() { + if recover() != nil { + r = 0 + err = ErrSyntax + } + }() + if n := len(s); n < 2 || 3 < n { return 0, ErrSyntax } @@ -578,7 +606,14 @@ type Variant struct { // ParseVariant parses and returns a Variant. An error is returned if s is not // a valid variant. -func ParseVariant(s string) (Variant, error) { +func ParseVariant(s string) (v Variant, err error) { + defer func() { + if recover() != nil { + v = Variant{} + err = ErrSyntax + } + }() + s = strings.ToLower(s) if id, ok := variantIndex[s]; ok { return Variant{id, s}, nil diff --git a/vendor/golang.org/x/text/internal/language/parse.go b/vendor/golang.org/x/text/internal/language/parse.go index c696fd0bd867..47ee0fed174f 100644 --- a/vendor/golang.org/x/text/internal/language/parse.go +++ b/vendor/golang.org/x/text/internal/language/parse.go @@ -232,6 +232,13 @@ func Parse(s string) (t Tag, err error) { if s == "" { return Und, ErrSyntax } + defer func() { + if recover() != nil { + t = Und + err = ErrSyntax + return + } + }() if len(s) <= maxAltTaglen { b := [maxAltTaglen]byte{} for i, c := range s { diff --git a/vendor/golang.org/x/text/language/parse.go b/vendor/golang.org/x/text/language/parse.go index 11acfd885627..59b04100806a 100644 --- a/vendor/golang.org/x/text/language/parse.go +++ b/vendor/golang.org/x/text/language/parse.go @@ -43,6 +43,13 @@ func Parse(s string) (t Tag, err error) { // https://www.unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers. // The resulting tag is canonicalized using the canonicalization type c. func (c CanonType) Parse(s string) (t Tag, err error) { + defer func() { + if recover() != nil { + t = Tag{} + err = language.ErrSyntax + } + }() + tt, err := language.Parse(s) if err != nil { return makeTag(tt), err @@ -79,6 +86,13 @@ func Compose(part ...interface{}) (t Tag, err error) { // tag is returned after canonicalizing using CanonType c. If one or more errors // are encountered, one of the errors is returned. func (c CanonType) Compose(part ...interface{}) (t Tag, err error) { + defer func() { + if recover() != nil { + t = Tag{} + err = language.ErrSyntax + } + }() + var b language.Builder if err = update(&b, part...); err != nil { return und, err @@ -142,6 +156,14 @@ var errInvalidWeight = errors.New("ParseAcceptLanguage: invalid weight") // Tags with a weight of zero will be dropped. An error will be returned if the // input could not be parsed. func ParseAcceptLanguage(s string) (tag []Tag, q []float32, err error) { + defer func() { + if recover() != nil { + tag = nil + q = nil + err = language.ErrSyntax + } + }() + var entry string for s != "" { if entry, s = split(s, ','); entry == "" { diff --git a/vendor/modules.txt b/vendor/modules.txt index 9b44def9b4d4..2aa03f4ec00d 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -23,7 +23,7 @@ github.com/davecgh/go-spew/spew ## explicit github.com/dlclark/regexp2 github.com/dlclark/regexp2/syntax -# github.com/dop251/goja v0.0.0-20211211112501-fb27c91c26ed +# github.com/dop251/goja v0.0.0-20220104154337-b93494d0c5d9 ## explicit; go 1.14 github.com/dop251/goja github.com/dop251/goja/ast @@ -178,7 +178,7 @@ golang.org/x/sys/windows # golang.org/x/term v0.0.0-20210503060354-a79de5458b56 ## explicit; go 1.17 golang.org/x/term -# golang.org/x/text v0.3.7-0.20210503195748-5c7c50ebbd4f +# golang.org/x/text v0.3.7 ## explicit; go 1.17 golang.org/x/text/cases golang.org/x/text/collate