Module 0x1::reflect
Functionality for reflection in Move.
- Enum
ReflectionError - Constants
- Function
resolve - Function
error_code - Function
native_resolve - Specification
use 0x1::error;
use 0x1::features;
use 0x1::result;
use 0x1::string;
Enum ReflectionError
Represents errors returned by the reflection API. TODO: make this public once language version 2.4 is available
enum ReflectionError has copy, drop, store
Variants
InvalidIdentifier
Fields
FunctionNotFound
Fields
FunctionNotAccessible
Fields
FunctionIncompatibleType
Fields
FunctionNotInstantiated
Fields
Constants
This error indicates that the reflection feature is not enabled.
const E_FEATURE_NOT_ENABLED: u64 = 0;
Function resolve
Resolves a function specified by address and symbolic name, with expected type, into a typed function value.
Example usage:
let fn : |address|u64 has store = reflect::resolve(@somewhere, utf8(b"mod"), utf8(b"fn")).unwrap();
assert!(fn(my_addr) == some_value)
See ReflectionError for the possible errors which can result. On successful resolution,
a function value is returned which can be safely used in future executions as indicated by the requested
type.
In order to be accessible, the resolved function must be public. This prevents reflection to work around the languages modular encapsulation guarantees.
A small set of framework functions are additionally forbidden from being resolved (the call
returns FunctionNotAccessible), because their rules are enforced by the bytecode verifier at
the call site and cannot be upheld for a dynamically-resolved function value. Currently this
is only 0x1::event::emit.
The resolved function can be generic, in which case the instantiation must be inferrible
from the provided FuncType. For example, public fun foo<T>(T), with FunType = |u64|,
T = u64 can be derived. If not all type parameters can be inferred, an error will be
produced.
public fun resolve<FuncType>(addr: address, module_name: &string::String, func_name: &string::String): result::Result<FuncType, reflect::ReflectionError>
Implementation
public fun resolve<FuncType>(
addr: address, module_name: &String, func_name: &String
): Result<FuncType, ReflectionError> {
assert!(
features::is_function_reflection_enabled(),
error::invalid_state(E_FEATURE_NOT_ENABLED)
);
native_resolve(addr, module_name, func_name)
}
Function error_code
Returns numerical code associated with error.
public fun error_code(self: reflect::ReflectionError): u64
Implementation
public fun error_code(self: ReflectionError): u64 {
match(self) {
InvalidIdentifier => 0,
FunctionNotFound => 1,
FunctionNotAccessible => 2,
FunctionIncompatibleType => 3,
FunctionNotInstantiated => 4
}
}
Function native_resolve
fun native_resolve<FuncType>(addr: address, module_name: &string::String, func_name: &string::String): result::Result<FuncType, reflect::ReflectionError>
Implementation
native fun native_resolve<FuncType>(
addr: address, module_name: &String, func_name: &String
): Result<FuncType, ReflectionError>;
Specification
Function resolve
public fun resolve<FuncType>(addr: address, module_name: &string::String, func_name: &string::String): result::Result<FuncType, reflect::ReflectionError>
pragma verify = false;
pragma opaque;