Skip to content

Commit 04b30fa

Browse files
committed
setGas, v1.0.10
1 parent 36d8107 commit 04b30fa

File tree

5 files changed

+75
-6
lines changed

5 files changed

+75
-6
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ Fully resets all global variables of the VM, might be useful for VM reuse betwee
9191
- **Parameters**
9292
Doesn't take parameters, returns undefined
9393

94+
### `setGas(config)`
95+
96+
Updates the gas configuration for the Duktape execution context associated with the current instance.
97+
98+
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.
99+
100+
- **Parameters**
101+
- `config` _(Object)_: An object containing the new gas configuration parameters.
102+
- `gasLimit` _(number)_: The maximum amount of gas that can be consumed. Often represents the upper limit of computational steps or memory usage.
103+
- `memoryByteCost` _(number)_: The cost per byte of memory used by the Duktape context, contributing to the total gas consumed.
104+
- `gasUsed` _(number)_: The amount of gas already consumed. This can be set to initialize or reset the consumption counter.
105+
94106
## Building
95107

96108
You can build Glomium from source by executing following commands:

bindings.cpp

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ napi_value flush_global(napi_env env,napi_callback_info info){
4141
napi_get_value_external(env, args[0], (void **)&ctx);
4242
duk_push_bare_object(ctx);
4343
duk_set_global_object(ctx);
44-
napi_value undefined;
44+
duk_memory_functions *funcs;
45+
46+
duk_get_memory_functions(ctx, funcs);
47+
HeapConfig *heapData = (HeapConfig *)funcs->udata;
48+
GasData *gasData = heapData->gasConfig;
49+
gasData->gas_used=0
50+
napi_value undefined;
4551
napi_get_undefined(env, &undefined);
4652
return undefined;
4753
}
@@ -67,9 +73,9 @@ napi_value create_context(napi_env env, napi_callback_info info)
6773
napi_get_value_uint32(env, prop_value, &mem_cost_per_byte);
6874

6975
auto *gasData = new GasData;
70-
gasData->gas_limit = gas_limit;
76+
gasData->gas_limit = 999999;//just a big enough value for Duktape to warm up
7177
gasData->gas_used = 0;
72-
gasData->mem_cost_per_byte = mem_cost_per_byte;
78+
gasData->mem_cost_per_byte = 0;
7379

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

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

93102
return externalCtx;
94103
}
104+
napi_value setGas(napi_env env, napi_callback_info info)
105+
{
106+
size_t argc = 2;
107+
napi_value args[2], config_object;
108+
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
109+
110+
if (argc < 2)
111+
{
112+
napi_throw_type_error(env, nullptr, "Expected two arguments: context and configuration object");
113+
return nullptr;
114+
}
115+
116+
duk_context *ctx;
117+
napi_get_value_external(env, args[0], (void **)&ctx);
118+
119+
config_object = args[1];
120+
uint32_t gas_limit, mem_cost_per_byte, used_gas;
121+
napi_value temp_value;
122+
123+
napi_get_named_property(env, config_object, "gasLimit", &temp_value);
124+
napi_get_value_uint32(env, temp_value, &gas_limit);
125+
126+
napi_get_named_property(env, config_object, "memCostPerByte", &temp_value);
127+
napi_get_value_uint32(env, temp_value, &mem_cost_per_byte);
128+
129+
napi_get_named_property(env, config_object, "usedGas", &temp_value);
130+
napi_get_value_uint32(env, temp_value, &used_gas);
131+
132+
duk_memory_functions *funcs;
133+
duk_get_memory_functions(ctx, &funcs);
134+
HeapConfig *heapData = (HeapConfig *)funcs->udata;
135+
GasData *gasData = heapData->gasConfig;
136+
137+
gasData->gas_limit = gas_limit;
138+
gasData->mem_cost_per_byte = mem_cost_per_byte;
139+
gasData->gas_used = used_gas;
140+
141+
napi_value undefined;
142+
napi_get_undefined(env, &undefined);
143+
return undefined;
144+
}
95145

96146
napi_value set_global(napi_env env, napi_callback_info info)
97147
{
@@ -196,12 +246,16 @@ napi_value eval_string(napi_env env, napi_callback_info info)
196246

197247
napi_value Init(napi_env env, napi_value exports)
198248
{
199-
napi_value createContext, evalString, setGlobal, getGlobal, flushGlobal;
249+
napi_value createContext, evalString, setGlobal, getGlobal, flushGlobal,setGas;
200250
napi_create_function(env, nullptr, NAPI_AUTO_LENGTH, create_context, nullptr, &createContext);
201251
napi_set_named_property(env, exports, "createContext", createContext);
252+
202253
napi_create_function(env, nullptr, NAPI_AUTO_LENGTH, flush_global, nullptr, &flushGlobal);
203254
napi_set_named_property(env, exports, "flushGlobal", flushGlobal);
204255

256+
napi_create_function(env, nullptr, NAPI_AUTO_LENGTH, set_gas, nullptr, &setGas);
257+
napi_set_named_property(env, exports, "flushGlobal", setGas);
258+
205259
napi_create_function(env, nullptr, NAPI_AUTO_LENGTH, set_global, nullptr, &setGlobal);
206260
napi_set_named_property(env, exports, "setGlobal", setGlobal);
207261

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@ class Glomium {
2020
clear() {
2121
return duktapeBindings.flushGlobal(this.context)
2222
}
23+
setGas({gasLimit, memoryByteCost,gasUsed}) {
24+
return duktapeBindings.setGas(this.context,{gasLimit:gasLimit,memCostPerByte:memoryByteCost,gasUsed:gasUsed})
25+
}
2326
}
2427
module.exports=Glomium

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "glomium",
3-
"version": "1.0.9",
3+
"version": "1.0.10",
44
"description": "Duktape bindings for node.js",
55
"main": "index.js",
66
"scripts": {

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ const result = glomium.run(`
2424

2525
console.log(result); // Should output 'Hello, world!'
2626
console.log(glomium.get("hello"))
27-
glomium.clearGlobal()
27+
glomium.clear()
2828
console.log(glomium.get("hello"))

0 commit comments

Comments
 (0)