Skip to content

Commit

Permalink
Merge pull request #1421 from goplus/main
Browse files Browse the repository at this point in the history
v1.1.7
  • Loading branch information
xushiwei authored Sep 23, 2023
2 parents dff1ae3 + e2bd0d8 commit d8dc878
Show file tree
Hide file tree
Showing 30 changed files with 1,144 additions and 104 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
[![Interpreter](https://img.shields.io/badge/interpreter-iGo+-seagreen.svg)](https://github.com/goplus/igop)
-->

[![Build Status](https://github.com/goplus/gox/actions/workflows/go.yml/badge.svg)](https://github.com/goplus/gox/actions/workflows/go.yml)
[![Build Status](https://github.com/goplus/gop/actions/workflows/go.yml/badge.svg)](https://github.com/goplus/gop/actions/workflows/go.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/goplus/gop)](https://goreportcard.com/report/github.com/goplus/gop)
[![Coverage Status](https://codecov.io/gh/goplus/gop/branch/main/graph/badge.svg)](https://codecov.io/gh/goplus/gop)
[![GitHub release](https://img.shields.io/github/v/tag/goplus/gop.svg?label=release)](https://github.com/goplus/gop/releases)
Expand Down
6 changes: 3 additions & 3 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ func (*ChanType) exprNode() {}
// Convenience functions for Idents

// NewIdent creates a new Ident without position.
// Useful for ASTs generated by code other than the Go parser.
// Useful for ASTs generated by code other than the Go+ parser.
func NewIdent(name string) *Ident { return &Ident{token.NoPos, name, nil} }

// IsExported reports whether name starts with an upper-case letter.
Expand Down Expand Up @@ -1103,7 +1103,7 @@ func (*FuncDecl) declNode() {}

type FileType = int16

// A File node represents a Go source file.
// A File node represents a Go+ source file.
//
// The Comments list contains all comments in the source file in order of
// appearance, including the comments that are pointed to from other nodes
Expand Down Expand Up @@ -1150,7 +1150,7 @@ func (f *File) End() token.Pos {
}

// A Package node represents a set of source files
// collectively building a Go package.
// collectively building a Go+ package.
type Package struct {
Name string // package name
Scope *Scope // package scope across all files
Expand Down
4 changes: 2 additions & 2 deletions ast/goptest/gopq.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import (
"go/token"

"github.com/goplus/gop/ast/gopq"
"github.com/goplus/gop/parser/parsertest"
"github.com/goplus/gop/parser/fsx/memfs"
)

// -----------------------------------------------------------------------------

// New creates a nodeset object that represents a Go+ dom tree.
func New(script string) (gopq.NodeSet, error) {
fset := token.NewFileSet()
fs := parsertest.NewSingleFileFS("/foo", "bar.gop", script)
fs := memfs.SingleFile("/foo", "bar.gop", script)
return gopq.NewSourceFrom(fset, fs, "/foo", nil, 0)
}

Expand Down
6 changes: 3 additions & 3 deletions cl/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func newGmx(ctx *pkgCtx, pkg *gox.Package, file string, f *ast.File, conf *Confi
if idx := strings.Index(name, "."); idx > 0 {
name = name[:idx]
if name == "main" {
name = "_main"
name = gt.Class
}
}
}
Expand Down Expand Up @@ -139,7 +139,7 @@ func getStringConst(spx *gox.PkgRef, name string) string {
return ""
}

func getFields(ctx *blockCtx, f *ast.File) (specs []ast.Spec) {
func getFields(f *ast.File) (specs []ast.Spec) {
decls := f.Decls
i, n := 0, len(decls)
for i < n {
Expand All @@ -153,7 +153,7 @@ func getFields(ctx *blockCtx, f *ast.File) (specs []ast.Spec) {
}
break
}
i++ // skip import, if any
i++ // skip import/const, if any
}
return
}
Expand Down
4 changes: 2 additions & 2 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ func preloadGopFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, con
}
syms := parent.syms
pos := f.Pos()
specs := getFields(ctx, f)
specs := getFields(f)
ld := getTypeLoader(parent, syms, pos, classType)
ld.typ = func() {
if debugLoad {
Expand Down Expand Up @@ -812,7 +812,7 @@ func preloadFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, genCod
}
case token.CONST:
pkg := ctx.pkg
cdecl := pkg.NewConstDecl(pkg.Types.Scope())
cdecl := pkg.NewConstDefs(pkg.Types.Scope())
for _, spec := range d.Specs {
vSpec := spec.(*ast.ValueSpec)
if debugLoad {
Expand Down
6 changes: 3 additions & 3 deletions cl/compile_spx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"github.com/goplus/gop/cl"
"github.com/goplus/gop/parser"
"github.com/goplus/gop/parser/parsertest"
"github.com/goplus/gop/parser/fsx/memfs"
"github.com/goplus/gop/scanner"
"github.com/goplus/mod/modfile"
)
Expand Down Expand Up @@ -77,7 +77,7 @@ func gopSpxTestExConf(t *testing.T, name string, conf *cl.Config, gmx, spxcode,
cl.SetDisableRecover(true)
defer cl.SetDisableRecover(false)

fs := parsertest.NewTwoFilesFS("/foo", spxfile, spxcode, gmxfile, gmx)
fs := memfs.TwoFiles("/foo", spxfile, spxcode, gmxfile, gmx)
pkgs, err := parser.ParseFSDir(gblFset, fs, "/foo", spxParserConf())
if err != nil {
scanner.PrintError(os.Stderr, err)
Expand All @@ -101,7 +101,7 @@ func gopSpxTestExConf(t *testing.T, name string, conf *cl.Config, gmx, spxcode,
}

func gopSpxErrorTestEx(t *testing.T, msg, gmx, spxcode, gmxfile, spxfile string) {
fs := parsertest.NewTwoFilesFS("/foo", spxfile, spxcode, gmxfile, gmx)
fs := memfs.TwoFiles("/foo", spxfile, spxcode, gmxfile, gmx)
pkgs, err := parser.ParseFSDir(gblFset, fs, "/foo", spxParserConf())
if err != nil {
scanner.PrintError(os.Stderr, err)
Expand Down
50 changes: 46 additions & 4 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/goplus/gop"
"github.com/goplus/gop/cl"
"github.com/goplus/gop/parser"
"github.com/goplus/gop/parser/parsertest"
"github.com/goplus/gop/parser/fsx/memfs"
"github.com/goplus/gop/scanner"
"github.com/goplus/gop/token"
"github.com/goplus/gox"
Expand Down Expand Up @@ -69,17 +69,17 @@ func gopClTest(t *testing.T, gopcode, expected string) {
}

func gopClTestFile(t *testing.T, gopcode, expected string, fname string) {
fs := parsertest.NewSingleFileFS("/foo", fname, gopcode)
fs := memfs.SingleFile("/foo", fname, gopcode)
gopClTestFS(t, gblConf, fs, "main", expected)
}

func gopClTestEx(t *testing.T, conf *cl.Config, pkgname, gopcode, expected string) {
fs := parsertest.NewSingleFileFS("/foo", "bar.gop", gopcode)
fs := memfs.SingleFile("/foo", "bar.gop", gopcode)
gopClTestFS(t, conf, fs, pkgname, expected)
}

func gopMixedClTest(t *testing.T, pkgname, gocode, gopcode, expected string) {
fs := parsertest.NewTwoFilesFS("/foo", "a.go", gocode, "b.gop", gopcode)
fs := memfs.TwoFiles("/foo", "a.go", gocode, "b.gop", gopcode)
gopClTestFS(t, gblConf, fs, pkgname, expected)
}

Expand Down Expand Up @@ -4515,3 +4515,45 @@ func main() {
}
`)
}

func TestClassFileMember(t *testing.T) {
gopClTestFile(t, `type Engine struct {
}
func (e *Engine) EnterPointerLock() {
}
func (e *Engine) SetEnable(b bool) {
}
func Engine() *Engine {
return &Engine{}
}
func Test() {
engine.setEnable true
engine.enterPointerLock
}
`, `package main
type Engine struct {
}
func (e *Engine) EnterPointerLock() {
}
func (e *Engine) SetEnable(b bool) {
}
type Rect struct {
}
func (this *Rect) Engine() *Engine {
return &Engine{}
}
func (this *Rect) Test() {
this.Engine().SetEnable(true)
this.Engine().EnterPointerLock()
}
`, "Rect.gox")
}
4 changes: 2 additions & 2 deletions cl/error_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"github.com/goplus/gop/cl"
"github.com/goplus/gop/parser"
"github.com/goplus/gop/parser/parsertest"
"github.com/goplus/gop/parser/fsx/memfs"
"github.com/goplus/gop/scanner"
)

Expand All @@ -34,7 +34,7 @@ func codeErrorTest(t *testing.T, msg, src string) {
}

func codeErrorTestEx(t *testing.T, pkgname, filename, msg, src string) {
fs := parsertest.NewSingleFileFS("/foo", filename, src)
fs := memfs.SingleFile("/foo", filename, src)
pkgs, err := parser.ParseFSDir(gblFset, fs, "/foo", parser.Config{})
if err != nil {
scanner.PrintError(os.Stderr, err)
Expand Down
2 changes: 1 addition & 1 deletion cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func compileIdent(ctx *blockCtx, ident *ast.Ident, flags int) (obj *gox.PkgRef,
sig := fn.Ancestor().Type().(*types.Signature)
if recv := sig.Recv(); recv != nil {
ctx.cb.Val(recv)
if compileMember(ctx, ident, name, flags) == nil { // class member object
if compileMember(ctx, ident, name, flags&^clCommandWithoutArgs) == nil { // class member object
return
}
ctx.cb.InternalStack().PopN(1)
Expand Down
4 changes: 2 additions & 2 deletions cl/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/goplus/gop/cl"
"github.com/goplus/gop/parser"
"github.com/goplus/gop/parser/parsertest"
"github.com/goplus/gop/parser/fsx/memfs"
"github.com/goplus/gop/scanner"
)

Expand Down Expand Up @@ -83,7 +83,7 @@ func genGo(t *testing.T, conf *cl.Config, gopcode string) []byte {
cl.SetDisableRecover(true)
defer cl.SetDisableRecover(false)

fs := parsertest.NewSingleFileFS("/foo", "bar.gop", gopcode)
fs := memfs.SingleFile("/foo", "bar.gop", gopcode)
pkgs, err := parser.ParseFSDir(gblFset, fs, "/foo", parser.Config{Mode: parser.ParseComments})
if err != nil {
scanner.PrintError(os.Stderr, err)
Expand Down
4 changes: 2 additions & 2 deletions cl/typeparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/goplus/gop/cl"
"github.com/goplus/gop/parser"
"github.com/goplus/gop/parser/parsertest"
"github.com/goplus/gop/parser/fsx/memfs"
)

func TestTypeParamsFunc(t *testing.T) {
Expand Down Expand Up @@ -203,7 +203,7 @@ func mixedErrorTest(t *testing.T, msg, gocode, gopcode string) {
}

func mixedErrorTestEx(t *testing.T, pkgname, msg, gocode, gopcode string) {
fs := parsertest.NewTwoFilesFS("/foo", "a.go", gocode, "b.gop", gopcode)
fs := memfs.TwoFiles("/foo", "a.go", gocode, "b.gop", gopcode)
pkgs, err := parser.ParseFSDir(gblFset, fs, "/foo", parser.Config{})
if err != nil {
scanner.PrintError(os.Stderr, err)
Expand Down
36 changes: 36 additions & 0 deletions parser/fsx/fsys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package fsx

import (
"io/fs"
"os"
"path/filepath"
)

// -----------------------------------------------------------------------------

// FileSystem represents a file system.
type FileSystem interface {
ReadDir(dirname string) ([]fs.DirEntry, error)
ReadFile(filename string) ([]byte, error)
Join(elem ...string) string
}

// -----------------------------------------------------------------------------

type localFS struct{}

func (p localFS) ReadDir(dirname string) ([]fs.DirEntry, error) {
return os.ReadDir(dirname)
}

func (p localFS) ReadFile(filename string) ([]byte, error) {
return os.ReadFile(filename)
}

func (p localFS) Join(elem ...string) string {
return filepath.Join(elem...)
}

var Local FileSystem = localFS{}

// -----------------------------------------------------------------------------
94 changes: 94 additions & 0 deletions parser/fsx/memfs/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package memfs

import (
"bytes"
"io"
"io/fs"
"os"
"path/filepath"
)

type FileFS struct {
data []byte
info fs.DirEntry
}

type dirEntry struct {
fs.FileInfo
}

func (p *dirEntry) Type() fs.FileMode {
return p.FileInfo.Mode().Type()
}

func (p *dirEntry) Info() (fs.FileInfo, error) {
return p.FileInfo, nil
}

func File(filename string, src interface{}) (f *FileFS, err error) {
var data []byte
var info fs.DirEntry
if src != nil {
data, err = readSource(src)
if err != nil {
return
}
info = &memFileInfo{name: filename, size: len(data)}
} else {
fi, e := os.Stat(filename)
if e != nil {
return nil, e
}
data, err = os.ReadFile(filename)
if err != nil {
return
}
info = &dirEntry{fi}
}
return &FileFS{data: data, info: info}, nil
}

func (p *FileFS) ReadDir(dirname string) ([]fs.DirEntry, error) {
return []fs.DirEntry{p.info}, nil
}

func (p *FileFS) ReadFile(filename string) ([]byte, error) {
return p.data, nil
}

func (p *FileFS) Join(elem ...string) string {
return filepath.Join(elem...)
}

func readSource(src interface{}) ([]byte, error) {
switch s := src.(type) {
case string:
return []byte(s), nil
case []byte:
return s, nil
case *bytes.Buffer:
// is io.Reader, but src is already available in []byte form
if s != nil {
return s.Bytes(), nil
}
case io.Reader:
return io.ReadAll(s)
}
return nil, os.ErrInvalid
}
Loading

0 comments on commit d8dc878

Please sign in to comment.