Skip to content

Commit

Permalink
Implement init and close for FluffyEvmRef.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhartnett committed Oct 29, 2024
1 parent ba1cbed commit b369c93
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
60 changes: 60 additions & 0 deletions fluffy/evm/evm.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Fluffy
# Copyright (c) 2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.

{.push raises: [].}

import ../../nimbus/transaction/evmc_vm_glue

when not defined(evmc_enabled):
{.
error:
"The Fluffy EVM requires evmc to be enabled. Compile with the -d:evmc_enabled flag."
.}

type FluffyEvmRef* = ref object
vmPtr: ptr evmc_vm

func init(T: type FluffyEvmRef): T =
FluffyEvmRef(vmPtr: evmc_create_nimbus_evm())

func isClosed*(evm: FluffyEvmRef): bool =
evm.vmPtr.isNil()

proc close(evm: FluffyEvmRef) =
if not evm.vmPtr.isNil():
evm.vmPtr.destroy(evm.vmPtr)
evm.vmPtr = nil

# desProc(vm)
# proc create_evm*() =
# let vm = evmc_create_nimbus_evm()
# echo "vm.abi_version: ", vm.abi_version
# echo "vm.name: ", vm.name
# echo "vm.version: ", vm.version
# echo "vm.destroy: ", vm.destroy.isNil()
# echo "vm.execute: ", vm.execute.isNil()

# # let vm = (ref evmc_vm)(
# # abi_version: EVMC_ABI_VERSION,
# # name: evmcName,
# # version: evmcVersion,
# # destroy: evmcDestroy,
# # execute: evmcExecute,
# # get_capabilities: evmcGetCapabilities,
# # set_option: evmcSetOption
# # )

# let desProc = vm.destroy
# desProc(vm)

when isMainModule:
let evm = FluffyEvmRef.init()

echo evm.isClosed()
evm.close()
echo evm.isClosed()
evm.close()
2 changes: 2 additions & 0 deletions fluffy/evm/evm.nim.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-d:
"evmc_enabled"
4 changes: 3 additions & 1 deletion nimbus/transaction/evmc_vm_glue.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import
./host_types, evmc/evmc,
".."/[evm/types, evm/computation, evm/state_transactions]

export evmc

proc evmcReleaseResult(result: var evmc_result) {.cdecl.} =
dealloc(result.output_data)

Expand Down Expand Up @@ -85,7 +87,7 @@ proc evmcSetOption(vm: ptr evmc_vm, name, value: cstring): evmc_set_option_resul
proc evmcDestroy(vm: ptr evmc_vm) {.cdecl.} =
GC_unref(cast[ref evmc_vm](vm))

proc evmc_create_nimbus_evm(): ptr evmc_vm {.cdecl, exportc.} =
proc evmc_create_nimbus_evm*(): ptr evmc_vm {.cdecl, exportc.} =
## Entry point to the Nimbus EVM, using an EVMC compatible interface.
## This is an exported C function. EVMC specifies the function must
## have this name format when exported from a shared library.
Expand Down

0 comments on commit b369c93

Please sign in to comment.