Skip to content

Commit

Permalink
setGas, v1.0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
angrymouse committed Nov 9, 2023
1 parent 36d8107 commit 04b30fa
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ Fully resets all global variables of the VM, might be useful for VM reuse betwee
- **Parameters**
Doesn't take parameters, returns undefined

### `setGas(config)`

Updates the gas configuration for the Duktape execution context associated with the current instance.

This function sets new limits and costs associated with the Duktape context's resource consumption, commonly referred to as "gas" in the context of execution constraints and sandboxing.

- **Parameters**
- `config` _(Object)_: An object containing the new gas configuration parameters.
- `gasLimit` _(number)_: The maximum amount of gas that can be consumed. Often represents the upper limit of computational steps or memory usage.
- `memoryByteCost` _(number)_: The cost per byte of memory used by the Duktape context, contributing to the total gas consumed.
- `gasUsed` _(number)_: The amount of gas already consumed. This can be set to initialize or reset the consumption counter.

## Building

You can build Glomium from source by executing following commands:
Expand Down
62 changes: 58 additions & 4 deletions bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ napi_value flush_global(napi_env env,napi_callback_info info){
napi_get_value_external(env, args[0], (void **)&ctx);
duk_push_bare_object(ctx);
duk_set_global_object(ctx);
napi_value undefined;
duk_memory_functions *funcs;

duk_get_memory_functions(ctx, funcs);
HeapConfig *heapData = (HeapConfig *)funcs->udata;
GasData *gasData = heapData->gasConfig;
gasData->gas_used=0
napi_value undefined;
napi_get_undefined(env, &undefined);
return undefined;
}
Expand All @@ -67,9 +73,9 @@ napi_value create_context(napi_env env, napi_callback_info info)
napi_get_value_uint32(env, prop_value, &mem_cost_per_byte);

auto *gasData = new GasData;
gasData->gas_limit = gas_limit;
gasData->gas_limit = 999999;//just a big enough value for Duktape to warm up
gasData->gas_used = 0;
gasData->mem_cost_per_byte = mem_cost_per_byte;
gasData->mem_cost_per_byte = 0;

auto *heapConfig = new HeapConfig;
heapConfig->gasConfig = gasData;
Expand All @@ -87,11 +93,55 @@ napi_value create_context(napi_env env, napi_callback_info info)

duk_push_bare_object(ctx);
duk_set_global_object(ctx);
gasData->gas_limit = gas_limit;
gasData->mem_cost_per_byte = mem_cost_per_byte;
gasData->gas_used = 0; // we don't really want to count warmup as a used gas as it's not dependent on usercode
napi_value externalCtx;
napi_create_external(env, ctx, cleanup_context, nullptr, &externalCtx);

return externalCtx;
}
napi_value setGas(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value args[2], config_object;
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

if (argc < 2)
{
napi_throw_type_error(env, nullptr, "Expected two arguments: context and configuration object");
return nullptr;
}

duk_context *ctx;
napi_get_value_external(env, args[0], (void **)&ctx);

config_object = args[1];
uint32_t gas_limit, mem_cost_per_byte, used_gas;
napi_value temp_value;

napi_get_named_property(env, config_object, "gasLimit", &temp_value);
napi_get_value_uint32(env, temp_value, &gas_limit);

napi_get_named_property(env, config_object, "memCostPerByte", &temp_value);
napi_get_value_uint32(env, temp_value, &mem_cost_per_byte);

napi_get_named_property(env, config_object, "usedGas", &temp_value);
napi_get_value_uint32(env, temp_value, &used_gas);

duk_memory_functions *funcs;
duk_get_memory_functions(ctx, &funcs);
HeapConfig *heapData = (HeapConfig *)funcs->udata;
GasData *gasData = heapData->gasConfig;

gasData->gas_limit = gas_limit;
gasData->mem_cost_per_byte = mem_cost_per_byte;
gasData->gas_used = used_gas;

napi_value undefined;
napi_get_undefined(env, &undefined);
return undefined;
}

napi_value set_global(napi_env env, napi_callback_info info)
{
Expand Down Expand Up @@ -196,12 +246,16 @@ napi_value eval_string(napi_env env, napi_callback_info info)

napi_value Init(napi_env env, napi_value exports)
{
napi_value createContext, evalString, setGlobal, getGlobal, flushGlobal;
napi_value createContext, evalString, setGlobal, getGlobal, flushGlobal,setGas;
napi_create_function(env, nullptr, NAPI_AUTO_LENGTH, create_context, nullptr, &createContext);
napi_set_named_property(env, exports, "createContext", createContext);

napi_create_function(env, nullptr, NAPI_AUTO_LENGTH, flush_global, nullptr, &flushGlobal);
napi_set_named_property(env, exports, "flushGlobal", flushGlobal);

napi_create_function(env, nullptr, NAPI_AUTO_LENGTH, set_gas, nullptr, &setGas);
napi_set_named_property(env, exports, "flushGlobal", setGas);

napi_create_function(env, nullptr, NAPI_AUTO_LENGTH, set_global, nullptr, &setGlobal);
napi_set_named_property(env, exports, "setGlobal", setGlobal);

Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ class Glomium {
clear() {
return duktapeBindings.flushGlobal(this.context)
}
setGas({gasLimit, memoryByteCost,gasUsed}) {
return duktapeBindings.setGas(this.context,{gasLimit:gasLimit,memCostPerByte:memoryByteCost,gasUsed:gasUsed})
}
}
module.exports=Glomium
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "glomium",
"version": "1.0.9",
"version": "1.0.10",
"description": "Duktape bindings for node.js",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ const result = glomium.run(`

console.log(result); // Should output 'Hello, world!'
console.log(glomium.get("hello"))
glomium.clearGlobal()
glomium.clear()
console.log(glomium.get("hello"))

0 comments on commit 04b30fa

Please sign in to comment.