Skip to content

Commit

Permalink
adjust to API changes in dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Dec 16, 2022
1 parent f93fad1 commit 1148134
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 45 deletions.
2 changes: 1 addition & 1 deletion languageserver/integration/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Test_ExecuteScript(t *testing.T) {
{args: []json.RawMessage{[]byte("1"), []byte("2")}, err: "invalid URI argument: 1"},
{args: []json.RawMessage{locationURL, []byte("3")}, err: "invalid script arguments: 3"},
{args: []json.RawMessage{locationURL, invalidCadenceArg}, err: "invalid script arguments cadence encoding format: {foo}, error: invalid character 'f' looking for beginning of object key string"},
{args: []json.RawMessage{locationURL, invalidCadenceValue}, err: `invalid script arguments cadence encoding format: [{ "type": "Bool", "value": "we are the knights who say niii" }], error: failed to decode value: invalid JSON Cadence structure`},
{args: []json.RawMessage{locationURL, invalidCadenceValue}, err: `invalid script arguments cadence encoding format: [{ "type": "Bool", "value": "we are the knights who say niii" }], error: failed to decode JSON-Cadence value: expected JSON bool, got we are the knights who say niii`},
})

t.Run("successful script execution with arguments", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions languageserver/integration/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type contractInfo struct {
startPos *ast.Position
kind contractKind
name string
parameters []*sema.Parameter
parameters []sema.Parameter
pragmaArgumentStrings []string
pragmaArguments [][]Argument
pragmaSignersNames []string
Expand All @@ -65,7 +65,7 @@ func (c *contractInfo) update(uri protocol.DocumentURI, version int32, checker *
c.name = contractDeclaration.Identifier.String()
c.startPos = &contractDeclaration.StartPos
c.kind = contractTypeDeclaration
contractType := checker.Elaboration.CompositeDeclarationTypes[contractDeclaration]
contractType := checker.Elaboration.CompositeDeclarationType(contractDeclaration)
c.parameters = contractType.ConstructorParameters
} else if contractInterfaceDeclaration != nil {
docString = contractInterfaceDeclaration.DocString
Expand Down
6 changes: 4 additions & 2 deletions languageserver/integration/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
)

func Test_ContractUpdate(t *testing.T) {

const code = `
/// pragma signers Alice
pub contract HelloWorld {
Expand All @@ -47,8 +48,9 @@ func Test_ContractUpdate(t *testing.T) {
self.greeting = a
}
}
`
program, err := parser.ParseProgram([]byte(code), nil)
`

program, err := parser.ParseProgram(nil, []byte(code), parser.Config{})
require.NoError(t, err)

location := common.StringLocation("foo")
Expand Down
7 changes: 3 additions & 4 deletions languageserver/integration/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type entryPointInfo struct {
documentVersion int32
startPos *ast.Position
kind entryPointKind
parameters []*sema.Parameter
parameters []sema.Parameter
pragmaArgumentStrings []string
pragmaArguments [][]Argument // todo can we refactor into a 1D list
pragmaSignerNames []string
Expand All @@ -67,14 +67,14 @@ func (e *entryPointInfo) update(uri protocol.DocumentURI, version int32, checker

if transactionDeclaration != nil {
docString = transactionDeclaration.DocString
transactionType := checker.Elaboration.TransactionDeclarationTypes[transactionDeclaration]
transactionType := checker.Elaboration.TransactionDeclarationType(transactionDeclaration)
e.startPos = &transactionDeclaration.StartPos
e.kind = entryPointKindTransaction
e.parameters = transactionType.Parameters
e.numberOfSigners = len(transactionType.PrepareParameters)
} else if functionDeclaration != nil {
docString = functionDeclaration.DocString
functionType := checker.Elaboration.FunctionDeclarationFunctionTypes[functionDeclaration]
functionType := checker.Elaboration.FunctionDeclarationFunctionType(functionDeclaration)
e.startPos = &functionDeclaration.StartPos
e.kind = entryPointKindScript
e.parameters = functionType.Parameters
Expand Down Expand Up @@ -125,7 +125,6 @@ func (e *entryPointInfo) update(uri protocol.DocumentURI, version int32, checker

// codelens shows an execute button when there is exactly one valid entry point
// (valid script function or transaction declaration) and no other actionable declarations.
//
func (e *entryPointInfo) codelens(client flowClient) []*protocol.CodeLens {
if e.kind == entryPointKindUnknown || e.startPos == nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion languageserver/integration/entrypoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
)

func buildEntrypoint(t *testing.T, code string) entryPointInfo {
program, err := parser.ParseProgram([]byte(code), nil)
program, err := parser.ParseProgram(nil, []byte(code), parser.Config{})
require.NoError(t, err)

location := common.StringLocation("foo")
Expand Down
2 changes: 1 addition & 1 deletion languageserver/server/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Parameter struct {
Type string `json:"type"`
}

func encodeParameters(parameters []*sema.Parameter) []Parameter {
func encodeParameters(parameters []sema.Parameter) []Parameter {

encodedParameters := make([]Parameter, len(parameters))

Expand Down
28 changes: 15 additions & 13 deletions languageserver/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,7 @@ func (s *Server) prepareFunctionMemberCompletionItem(
func (s *Server) prepareParametersCompletionItem(
item *protocol.CompletionItem,
name string,
parameters []*sema.Parameter,
parameters []sema.Parameter,
) {
item.InsertTextFormat = protocol.SnippetTextFormat

Expand Down Expand Up @@ -1706,7 +1706,7 @@ func (s *Server) InlayHint(
})

for _, variableDeclaration := range variableDeclarations {
targetType := checker.Elaboration.VariableDeclarationTypes[variableDeclaration].TargetType
targetType := checker.Elaboration.VariableDeclarationTypes(variableDeclaration).TargetType
if targetType == nil { // bugfix getting nil target
continue // todo this should never occur
}
Expand Down Expand Up @@ -1934,7 +1934,7 @@ func parse(code, location string, log func(*protocol.LogMessageParams)) (*ast.Pr
defer sentry.RecoverWithContext(ctx)

start := time.Now()
program, err := parser.ParseProgram([]byte(code), nil)
program, err := parser.ParseProgram(nil, []byte(code), parser.Config{})
elapsed := time.Since(start)

log(&protocol.LogMessageParams{
Expand Down Expand Up @@ -1976,7 +1976,7 @@ func (s *Server) resolveImport(location common.Location) (program *ast.Program,
return nil, err
}

return parser.ParseProgram([]byte(code), nil)
return parser.ParseProgram(nil, []byte(code), parser.Config{})
}

func (s *Server) GetDocument(uri protocol.DocumentURI) (doc Document, ok bool) {
Expand Down Expand Up @@ -2067,7 +2067,7 @@ func (s *Server) getContractInitializerParameters(args ...json2.RawMessage) (int
return []Parameter{}, nil
}

compositeType := checker.Elaboration.CompositeDeclarationTypes[compositeDeclaration]
compositeType := checker.Elaboration.CompositeDeclarationType(compositeDeclaration)

encodedParameters := encodeParameters(compositeType.ConstructorParameters)

Expand Down Expand Up @@ -2206,11 +2206,11 @@ func (s *Server) convertError(
switch ty := err.Type.(type) {
case *sema.CompositeType:
declarationGetter = func(elaboration *sema.Elaboration) ast.Declaration {
return elaboration.CompositeTypeDeclarations[ty]
return elaboration.CompositeTypeDeclaration(ty)
}
case *sema.InterfaceType:
declarationGetter = func(elaboration *sema.Elaboration) ast.Declaration {
return elaboration.InterfaceTypeDeclarations[ty]
return elaboration.InterfaceTypeDeclaration(ty)
}
}

Expand Down Expand Up @@ -2492,7 +2492,7 @@ func formatNewMember(member *sema.Member, indentation string) string {

var returnType string
returnTypeAnnotation := functionType.ReturnTypeAnnotation
if returnTypeAnnotation != nil && returnTypeAnnotation.Type != sema.VoidType {
if returnTypeAnnotation.Type != nil && returnTypeAnnotation.Type != sema.VoidType {
returnType = fmt.Sprintf(": %s", returnTypeAnnotation.QualifiedString())
}

Expand Down Expand Up @@ -2556,8 +2556,9 @@ func (s *Server) maybeAddDeclarationActionsResolver(
case *ast.InvocationExpression:
isInvoked = parent.InvokedExpression == errorExpression

invocationArgumentTypes = checker.Elaboration.InvocationExpressionTypes[parent].ArgumentTypes
invocationReturnType = checker.Elaboration.InvocationExpressionTypes[parent].ReturnType
invocationExpressionTypes := checker.Elaboration.InvocationExpressionTypes(parent)
invocationArgumentTypes = invocationExpressionTypes.ArgumentTypes
invocationReturnType = invocationExpressionTypes.ReturnType

invocationArgumentLabels = make([]string, 0, len(parent.Arguments))
for _, argument := range parent.Arguments {
Expand Down Expand Up @@ -2652,7 +2653,7 @@ func (s *Server) maybeAddDeclarationActionsResolver(
insertionPos = memberInsertionPosGetter(checker, false)

memberExpression := errorExpression.(*ast.MemberExpression)
expectedType := checker.Elaboration.MemberExpressionExpectedTypes[memberExpression]
expectedType := checker.Elaboration.MemberExpressionExpectedType(memberExpression)

var typeString string
if expectedType != nil {
Expand Down Expand Up @@ -2785,9 +2786,10 @@ func (s *Server) handleImport(
error,
) {
switch importedLocation {
case stdlib.CryptoChecker.Location:
case stdlib.CryptoCheckerLocation:
cryptoChecker := stdlib.CryptoChecker()
return sema.ElaborationImport{
Elaboration: stdlib.CryptoChecker.Elaboration,
Elaboration: cryptoChecker.Elaboration,
}, nil

default:
Expand Down
72 changes: 51 additions & 21 deletions languageserver/server/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ type standardLibrary struct {
baseValueActivation *sema.VariableActivation
}

var _ stdlib.Logger = standardLibrary{}
var _ stdlib.UnsafeRandomGenerator = standardLibrary{}
var _ stdlib.BlockAtHeightProvider = standardLibrary{}
var _ stdlib.CurrentBlockProvider = standardLibrary{}
var _ stdlib.PublicAccountHandler = standardLibrary{}
var _ stdlib.AccountCreator = standardLibrary{}
var _ stdlib.StandardLibraryHandler = standardLibrary{}

func (standardLibrary) ProgramLog(_ string) error {
// Implementation should never be called,
Expand Down Expand Up @@ -205,24 +200,59 @@ func (standardLibrary) CreateAccount(_ common.Address) (address common.Address,
panic(errors.NewUnreachableError())
}

func newStandardLibrary() (result standardLibrary) {
// TODO: either:
// - use runtime's script environment. requires it to become more configurable
// - separate out stdlib definitions from script environment, and use them here instead
func (standardLibrary) ValidatePublicKey(_ *stdlib.PublicKey) error {
// Implementation should never be called,
// only its definition is used for type-checking
panic(errors.NewUnreachableError())
}

func (standardLibrary) VerifySignature(
_ []byte,
_ string,
_ []byte,
_ []byte,
_ sema.SignatureAlgorithm,
_ sema.HashAlgorithm,
) (bool, error) {
// Implementation should never be called,
// only its definition is used for type-checking
panic(errors.NewUnreachableError())
}

func (standardLibrary) BLSVerifyPOP(_ *stdlib.PublicKey, _ []byte) (bool, error) {
// Implementation should never be called,
// only its definition is used for type-checking
panic(errors.NewUnreachableError())
}

func (standardLibrary) Hash(_ []byte, _ string, _ sema.HashAlgorithm) ([]byte, error) {
// Implementation should never be called,
// only its definition is used for type-checking
panic(errors.NewUnreachableError())
}

func (standardLibrary) AccountKeysCount(_ common.Address) (uint64, error) {
// Implementation should never be called,
// only its definition is used for type-checking
panic(errors.NewUnreachableError())
}

func (standardLibrary) BLSAggregatePublicKeys(_ []*stdlib.PublicKey) (*stdlib.PublicKey, error) {
// Implementation should never be called,
// only its definition is used for type-checking
panic(errors.NewUnreachableError())
}

func (standardLibrary) BLSAggregateSignatures(_ [][]byte) ([]byte, error) {
// Implementation should never be called,
// only its definition is used for type-checking
panic(errors.NewUnreachableError())
}

func newStandardLibrary() (result standardLibrary) {
result.baseValueActivation = sema.NewVariableActivation(sema.BaseValueActivation)
for _, valueDeclaration := range append(
stdlib.BuiltinValues[:],
stdlib.NewLogFunction(result),
stdlib.NewUnsafeRandomFunction(result),
stdlib.NewGetBlockFunction(result),
stdlib.NewGetCurrentBlockFunction(result),
stdlib.NewGetAccountFunction(result),
stdlib.NewAuthAccountConstructor(result),
stdlib.NewGetAuthAccountFunction(result),
) {
for _, valueDeclaration := range stdlib.DefaultStandardLibraryValues(result) {
result.baseValueActivation.DeclareValue(valueDeclaration)
}

return
}

0 comments on commit 1148134

Please sign in to comment.