Skip to content

Commit 18e6ea2

Browse files
authored
Merge pull request #332 from onflow/jribbink/ls-internal-evm
2 parents 3b5e24b + 5189a5d commit 18e6ea2

File tree

6 files changed

+87
-8
lines changed

6 files changed

+87
-8
lines changed

languageserver/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/onflow/cadence v1.0.0-preview.39
1111
github.com/onflow/cadence-tools/lint v1.0.0-preview.35
1212
github.com/onflow/cadence-tools/test v1.0.0-preview.35
13+
github.com/onflow/flow-go v0.36.4-0.20240724205438-14f9fddeda2b
1314
github.com/onflow/flow-go-sdk v1.0.0-preview.42
1415
github.com/onflow/flowkit/v2 v2.0.0-stable-cadence-alpha.27
1516
github.com/sourcegraph/jsonrpc2 v0.1.0
@@ -155,7 +156,6 @@ require (
155156
github.com/onflow/flow-emulator v1.0.0-preview.36 // indirect
156157
github.com/onflow/flow-ft/lib/go/contracts v1.0.0 // indirect
157158
github.com/onflow/flow-ft/lib/go/templates v1.0.0 // indirect
158-
github.com/onflow/flow-go v0.36.4-0.20240724205438-14f9fddeda2b // indirect
159159
github.com/onflow/flow-nft/lib/go/contracts v1.2.1 // indirect
160160
github.com/onflow/flow-nft/lib/go/templates v1.2.0 // indirect
161161
github.com/onflow/flow/protobuf/go/flow v0.4.5 // indirect

languageserver/integration/integration.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func NewFlowIntegration(s *server.Server, enableFlowClient bool) (*FlowIntegrati
5252
server.WithDiagnosticProvider(diagnostics),
5353
server.WithStringImportResolver(resolve.stringImport),
5454
server.WithInitializationOptionsHandler(integration.initialize),
55+
server.WithExtendedStandardLibraryValues(FVMStandardLibraryValues()...),
5556
}
5657

5758
if enableFlowClient {

languageserver/integration/stdlib.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Cadence - The resource-oriented smart contract programming language
3+
*
4+
* Copyright 2019-2022 Dapper Labs, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package integration
20+
21+
import (
22+
"github.com/onflow/cadence/runtime/common"
23+
"github.com/onflow/cadence/runtime/stdlib"
24+
evmstdlib "github.com/onflow/flow-go/fvm/evm/stdlib"
25+
)
26+
27+
// FVMtandardLibraryValues returns the standard library values which are provided by the FVM
28+
// these are not part of the Cadence standard library
29+
func FVMStandardLibraryValues() []stdlib.StandardLibraryValue {
30+
return []stdlib.StandardLibraryValue{
31+
// InternalEVM contract
32+
{
33+
Name: evmstdlib.InternalEVMContractName,
34+
Type: evmstdlib.InternalEVMContractType,
35+
Value: evmstdlib.NewInternalEVMContractValue(nil, nil, common.AddressLocation{}),
36+
Kind: common.DeclarationKindContract,
37+
},
38+
}
39+
}

languageserver/server/server.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ type Server struct {
188188
checkerStandardConfig *sema.Config
189189
// checkerScriptConfig is a config used to check scripts
190190
checkerScriptConfig *sema.Config
191+
// standardLibrary is the default standard library
192+
standardLibrary *standardLibrary
193+
// scriptStandardLibrary is the standard library for scripts
194+
scriptStandardLibrary *standardLibrary
191195
}
192196

193197
type Option func(*Server) error
@@ -278,6 +282,21 @@ func WithMemberAccountAccessHandler(handler sema.MemberAccountAccessHandlerFunc)
278282
}
279283
}
280284

285+
// WithStandardLibraryValues returns a server option that adds the given function
286+
// as a function that is used to resolve standard library values (in addition to the default standard library)
287+
//
288+
// This is used to provide parts of the standard library that are present in some given environment
289+
// but are not native to the language.
290+
func WithExtendedStandardLibraryValues(values ...stdlib.StandardLibraryValue) Option {
291+
return func(server *Server) error {
292+
for _, value := range values {
293+
server.standardLibrary.baseValueActivation.DeclareValue(value)
294+
server.scriptStandardLibrary.baseValueActivation.DeclareValue(value)
295+
}
296+
return nil
297+
}
298+
}
299+
281300
const GetEntryPointParametersCommand = "cadence.server.getEntryPointParameters"
282301
const GetContractInitializerParametersCommand = "cadence.server.getContractInitializerParameters"
283302
const ParseEntryPointArgumentsCommand = "cadence.server.parseEntryPointArguments"
@@ -307,15 +326,19 @@ func NewServer() (*Server, error) {
307326
}
308327
}
309328

329+
// create standard libraries
330+
server.standardLibrary = newStandardLibrary()
331+
server.scriptStandardLibrary = newScriptStandardLibrary()
332+
310333
// create checker configurations
311-
server.checkerStandardConfig = newCheckerConfig(server, newStandardLibrary())
312-
server.checkerScriptConfig = newCheckerConfig(server, newScriptStandardLibrary())
334+
server.checkerStandardConfig = newCheckerConfig(server, server.standardLibrary)
335+
server.checkerScriptConfig = newCheckerConfig(server, server.scriptStandardLibrary)
313336

314337
return server, nil
315338
}
316339

317340
// newCheckerConfig creates a checker config based on the standard library provided set to base value activations.
318-
func newCheckerConfig(s *Server, lib standardLibrary) *sema.Config {
341+
func newCheckerConfig(s *Server, lib *standardLibrary) *sema.Config {
319342
return &sema.Config{
320343
BaseValueActivationHandler: func(_ common.Location) *sema.VariableActivation {
321344
return lib.baseValueActivation

languageserver/server/stdlib.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,20 @@ func (standardLibrary) IsContractBeingAdded(_ common.AddressLocation) bool {
273273
panic(errors.NewUnreachableError())
274274
}
275275

276-
func newStandardLibrary() (result standardLibrary) {
276+
func newStandardLibrary() *standardLibrary {
277+
result := &standardLibrary{}
277278
result.baseValueActivation = sema.NewVariableActivation(sema.BaseValueActivation)
278279
for _, valueDeclaration := range stdlib.DefaultStandardLibraryValues(result) {
279280
result.baseValueActivation.DeclareValue(valueDeclaration)
280281
}
281-
return
282+
return result
282283
}
283284

284-
func newScriptStandardLibrary() (result standardLibrary) {
285+
func newScriptStandardLibrary() *standardLibrary {
286+
result := &standardLibrary{}
285287
result.baseValueActivation = sema.NewVariableActivation(sema.BaseValueActivation)
286288
for _, declaration := range stdlib.DefaultScriptStandardLibraryValues(result) {
287289
result.baseValueActivation.DeclareValue(declaration)
288290
}
289-
return
291+
return result
290292
}

languageserver/test/index.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,20 @@ describe("diagnostics", () => {
319319
["unused result"]
320320
)
321321
)
322+
323+
test("InternalEVM contract exists", async() =>
324+
testCode(
325+
`
326+
access(all)
327+
fun main() {
328+
// Checks that the InternalEVM contract exists
329+
// Also that it has the correct value (i.e. the run function exists)
330+
log(InternalEVM.run)
331+
}
332+
`,
333+
[]
334+
)
335+
)
322336

323337
type TestDoc = {
324338
name: string

0 commit comments

Comments
 (0)