Function wasmer_c_api::wasm_c_api::unstable::middlewares::metering::wasmer_metering_set_remaining_points
source · #[no_mangle]
pub unsafe extern "C" fn wasmer_metering_set_remaining_points(
instance: &mut wasm_instance_t,
new_limit: u64
)
Expand description
Set a new amount of points for the given metering middleware.
Example
This example only illustrates the
wasmer_metering_set_remaining_points
function, as the number of
points aren’t updated by the WebAssembly module execution
// Define a dummy “cost function”.
uint64_t cost_function(wasmer_parser_operator_t wasm_operator) {
switch(wasm_operator) {
default:
return 0;
}
}
int main() {
// Set the initial amount of points to 7.
wasmer_metering_t* metering = wasmer_metering_new(7, cost_function);
// Consume `metering` to produce `middleware`.
wasmer_middleware_t* middleware = wasmer_metering_as_middleware(metering);
// Create the configuration (which consumes `middleware`),
// the engine, and the store.
wasm_config_t* config = wasm_config_new();
wasm_config_push_middleware(config, middleware);
wasm_engine_t* engine = wasm_engine_new_with_config(config);
wasm_store_t* store = wasm_store_new(engine);
// Create the module and instantiate it.
wasm_byte_vec_t wat;
wasmer_byte_vec_new_from_string(&wat, "(module)");
wasm_byte_vec_t wasm;
wat2wasm(&wat, &wasm);
wasm_module_t* module = wasm_module_new(store, &wasm);
assert(module);
wasm_extern_vec_t imports = WASM_EMPTY_VEC;
wasm_trap_t* trap = NULL;
wasm_instance_t* instance = wasm_instance_new(store, module, &imports, &trap);
assert(instance);
// Read the number of points.
assert(wasmer_metering_get_remaining_points(instance) == 7);
// Set a new number of points.
wasmer_metering_set_remaining_points(instance, 42);
// Read the number of points.
assert(wasmer_metering_get_remaining_points(instance) == 42);
wasm_instance_delete(instance);
wasm_module_delete(module);
wasm_store_delete(store);
wasm_engine_delete(engine);
return 0;
}